xref: /linux/drivers/usb/gadget/function/f_uvc.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
200a2430fSAndrzej Pietrasiewicz /*
300a2430fSAndrzej Pietrasiewicz  *	uvc_gadget.c  --  USB Video Class Gadget driver
400a2430fSAndrzej Pietrasiewicz  *
500a2430fSAndrzej Pietrasiewicz  *	Copyright (C) 2009-2010
600a2430fSAndrzej Pietrasiewicz  *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
700a2430fSAndrzej Pietrasiewicz  */
800a2430fSAndrzej Pietrasiewicz 
900a2430fSAndrzej Pietrasiewicz #include <linux/device.h>
1000a2430fSAndrzej Pietrasiewicz #include <linux/errno.h>
1100a2430fSAndrzej Pietrasiewicz #include <linux/fs.h>
12284eb166SLaurent Pinchart #include <linux/kernel.h>
1300a2430fSAndrzej Pietrasiewicz #include <linux/list.h>
14284eb166SLaurent Pinchart #include <linux/module.h>
1500a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h>
1600a2430fSAndrzej Pietrasiewicz #include <linux/string.h>
1700a2430fSAndrzej Pietrasiewicz #include <linux/usb/ch9.h>
1800a2430fSAndrzej Pietrasiewicz #include <linux/usb/gadget.h>
19284eb166SLaurent Pinchart #include <linux/usb/g_uvc.h>
2000a2430fSAndrzej Pietrasiewicz #include <linux/usb/video.h>
2100a2430fSAndrzej Pietrasiewicz #include <linux/vmalloc.h>
2200a2430fSAndrzej Pietrasiewicz #include <linux/wait.h>
2300a2430fSAndrzej Pietrasiewicz 
2400a2430fSAndrzej Pietrasiewicz #include <media/v4l2-dev.h>
2500a2430fSAndrzej Pietrasiewicz #include <media/v4l2-event.h>
2600a2430fSAndrzej Pietrasiewicz 
2700a2430fSAndrzej Pietrasiewicz #include "uvc.h"
2846919a23SAndrzej Pietrasiewicz #include "uvc_configfs.h"
293a83c16eSAndrzej Pietrasiewicz #include "uvc_v4l2.h"
303a83c16eSAndrzej Pietrasiewicz #include "uvc_video.h"
3100a2430fSAndrzej Pietrasiewicz 
3200a2430fSAndrzej Pietrasiewicz unsigned int uvc_gadget_trace_param;
3320970d82SLaurent Pinchart module_param_named(trace, uvc_gadget_trace_param, uint, 0644);
3420970d82SLaurent Pinchart MODULE_PARM_DESC(trace, "Trace level bitmask");
3500a2430fSAndrzej Pietrasiewicz 
3600a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
3700a2430fSAndrzej Pietrasiewicz  * Function descriptors
3800a2430fSAndrzej Pietrasiewicz  */
3900a2430fSAndrzej Pietrasiewicz 
4000a2430fSAndrzej Pietrasiewicz /* string IDs are assigned dynamically */
4100a2430fSAndrzej Pietrasiewicz 
4200a2430fSAndrzej Pietrasiewicz static struct usb_string uvc_en_us_strings[] = {
43324e4f85SDan Vacura 	/* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */
4400a2430fSAndrzej Pietrasiewicz 	[UVC_STRING_STREAMING_IDX].s = "Video Streaming",
4500a2430fSAndrzej Pietrasiewicz 	{  }
4600a2430fSAndrzej Pietrasiewicz };
4700a2430fSAndrzej Pietrasiewicz 
4800a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings uvc_stringtab = {
4900a2430fSAndrzej Pietrasiewicz 	.language = 0x0409,	/* en-us */
5000a2430fSAndrzej Pietrasiewicz 	.strings = uvc_en_us_strings,
5100a2430fSAndrzej Pietrasiewicz };
5200a2430fSAndrzej Pietrasiewicz 
5300a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings *uvc_function_strings[] = {
5400a2430fSAndrzej Pietrasiewicz 	&uvc_stringtab,
5500a2430fSAndrzej Pietrasiewicz 	NULL,
5600a2430fSAndrzej Pietrasiewicz };
5700a2430fSAndrzej Pietrasiewicz 
5800a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_CONTROL			0
5900a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_STREAMING		1
6000a2430fSAndrzej Pietrasiewicz 
6100a2430fSAndrzej Pietrasiewicz #define UVC_STATUS_MAX_PACKET_SIZE		16	/* 16 bytes status */
6200a2430fSAndrzej Pietrasiewicz 
636d11ed76SAndrzej Pietrasiewicz static struct usb_interface_assoc_descriptor uvc_iad = {
6400a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_iad),
6500a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
6600a2430fSAndrzej Pietrasiewicz 	.bFirstInterface	= 0,
6700a2430fSAndrzej Pietrasiewicz 	.bInterfaceCount	= 2,
6800a2430fSAndrzej Pietrasiewicz 	.bFunctionClass		= USB_CLASS_VIDEO,
6900a2430fSAndrzej Pietrasiewicz 	.bFunctionSubClass	= UVC_SC_VIDEO_INTERFACE_COLLECTION,
7000a2430fSAndrzej Pietrasiewicz 	.bFunctionProtocol	= 0x00,
7100a2430fSAndrzej Pietrasiewicz 	.iFunction		= 0,
7200a2430fSAndrzej Pietrasiewicz };
7300a2430fSAndrzej Pietrasiewicz 
746d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_control_intf = {
7500a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
7600a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
7700a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL,
7800a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
79130c4dcbSDaniel Scally 	.bNumEndpoints		= 0,
8000a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
8100a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOCONTROL,
8200a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
8300a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
8400a2430fSAndrzej Pietrasiewicz };
8500a2430fSAndrzej Pietrasiewicz 
863078212cSDaniel Scally static struct usb_endpoint_descriptor uvc_interrupt_ep = {
8700a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
8800a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
8900a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
9000a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
9100a2430fSAndrzej Pietrasiewicz 	.wMaxPacketSize		= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
9200a2430fSAndrzej Pietrasiewicz 	.bInterval		= 8,
9300a2430fSAndrzej Pietrasiewicz };
9400a2430fSAndrzej Pietrasiewicz 
953078212cSDaniel Scally static struct usb_ss_ep_comp_descriptor uvc_ss_interrupt_comp = {
963078212cSDaniel Scally 	.bLength		= sizeof(uvc_ss_interrupt_comp),
9700a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
9800a2430fSAndrzej Pietrasiewicz 	/* The following 3 values can be tweaked if necessary. */
9900a2430fSAndrzej Pietrasiewicz 	.bMaxBurst		= 0,
10000a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= 0,
10100a2430fSAndrzej Pietrasiewicz 	.wBytesPerInterval	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
10200a2430fSAndrzej Pietrasiewicz };
10300a2430fSAndrzej Pietrasiewicz 
1043078212cSDaniel Scally static struct uvc_control_endpoint_descriptor uvc_interrupt_cs_ep = {
10500a2430fSAndrzej Pietrasiewicz 	.bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE,
10600a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_CS_ENDPOINT,
10700a2430fSAndrzej Pietrasiewicz 	.bDescriptorSubType	= UVC_EP_INTERRUPT,
10800a2430fSAndrzej Pietrasiewicz 	.wMaxTransferSize	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
10900a2430fSAndrzej Pietrasiewicz };
11000a2430fSAndrzej Pietrasiewicz 
1116d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
11200a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
11300a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
11400a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
11500a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
11600a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 0,
11700a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
11800a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
11900a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
12000a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
12100a2430fSAndrzej Pietrasiewicz };
12200a2430fSAndrzej Pietrasiewicz 
1236d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
12400a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
12500a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
12600a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
12700a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 1,
12800a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 1,
12900a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
13000a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
13100a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
13200a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
13300a2430fSAndrzej Pietrasiewicz };
13400a2430fSAndrzej Pietrasiewicz 
1356d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
13600a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
13700a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
13800a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
13900a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
14000a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
141c5d337a3SLaurent Pinchart 	/*
142c5d337a3SLaurent Pinchart 	 * The wMaxPacketSize and bInterval values will be initialized from
14300a2430fSAndrzej Pietrasiewicz 	 * module parameters.
14400a2430fSAndrzej Pietrasiewicz 	 */
14500a2430fSAndrzej Pietrasiewicz };
14600a2430fSAndrzej Pietrasiewicz 
1476d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
14800a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
14900a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
15000a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
15100a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
15200a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
153c5d337a3SLaurent Pinchart 	/*
154c5d337a3SLaurent Pinchart 	 * The wMaxPacketSize and bInterval values will be initialized from
15500a2430fSAndrzej Pietrasiewicz 	 * module parameters.
15600a2430fSAndrzej Pietrasiewicz 	 */
15700a2430fSAndrzej Pietrasiewicz };
15800a2430fSAndrzej Pietrasiewicz 
1596d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
16000a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
16100a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
16200a2430fSAndrzej Pietrasiewicz 
16300a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
16400a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
16500a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
166c5d337a3SLaurent Pinchart 	/*
167c5d337a3SLaurent Pinchart 	 * The wMaxPacketSize and bInterval values will be initialized from
16800a2430fSAndrzej Pietrasiewicz 	 * module parameters.
16900a2430fSAndrzej Pietrasiewicz 	 */
17000a2430fSAndrzej Pietrasiewicz };
17100a2430fSAndrzej Pietrasiewicz 
1726d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
17300a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_ss_streaming_comp),
17400a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
175c5d337a3SLaurent Pinchart 	/*
176c5d337a3SLaurent Pinchart 	 * The bMaxBurst, bmAttributes and wBytesPerInterval values will be
17700a2430fSAndrzej Pietrasiewicz 	 * initialized from module parameters.
17800a2430fSAndrzej Pietrasiewicz 	 */
17900a2430fSAndrzej Pietrasiewicz };
18000a2430fSAndrzej Pietrasiewicz 
18100a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
18200a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
18300a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_fs_streaming_ep,
18400a2430fSAndrzej Pietrasiewicz 	NULL,
18500a2430fSAndrzej Pietrasiewicz };
18600a2430fSAndrzej Pietrasiewicz 
18700a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
18800a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
18900a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_hs_streaming_ep,
19000a2430fSAndrzej Pietrasiewicz 	NULL,
19100a2430fSAndrzej Pietrasiewicz };
19200a2430fSAndrzej Pietrasiewicz 
19300a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
19400a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
19500a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_ss_streaming_ep,
19600a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_ss_streaming_comp,
19700a2430fSAndrzej Pietrasiewicz 	NULL,
19800a2430fSAndrzej Pietrasiewicz };
19900a2430fSAndrzej Pietrasiewicz 
20000a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
20100a2430fSAndrzej Pietrasiewicz  * Control requests
20200a2430fSAndrzej Pietrasiewicz  */
20300a2430fSAndrzej Pietrasiewicz 
20400a2430fSAndrzej Pietrasiewicz static void
uvc_function_ep0_complete(struct usb_ep * ep,struct usb_request * req)20500a2430fSAndrzej Pietrasiewicz uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
20600a2430fSAndrzej Pietrasiewicz {
20700a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = req->context;
20800a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
20900a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
21000a2430fSAndrzej Pietrasiewicz 
21100a2430fSAndrzej Pietrasiewicz 	if (uvc->event_setup_out) {
21200a2430fSAndrzej Pietrasiewicz 		uvc->event_setup_out = 0;
21300a2430fSAndrzej Pietrasiewicz 
21400a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
21500a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_DATA;
2164c92670bSSzymon Heidrich 		uvc_event->data.length = min_t(unsigned int, req->actual,
2174c92670bSSzymon Heidrich 			sizeof(uvc_event->data.data));
2184c92670bSSzymon Heidrich 		memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length);
219dbe98b30SHans Verkuil 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
22000a2430fSAndrzej Pietrasiewicz 	}
22100a2430fSAndrzej Pietrasiewicz }
22200a2430fSAndrzej Pietrasiewicz 
22300a2430fSAndrzej Pietrasiewicz static int
uvc_function_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)22400a2430fSAndrzej Pietrasiewicz uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
22500a2430fSAndrzej Pietrasiewicz {
22600a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
22700a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
22800a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
229d182bf15SMichael Grzeschik 	unsigned int interface = le16_to_cpu(ctrl->wIndex) & 0xff;
230d182bf15SMichael Grzeschik 	struct usb_ctrlrequest *mctrl;
23100a2430fSAndrzej Pietrasiewicz 
23200a2430fSAndrzej Pietrasiewicz 	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
233dc0f755bSLaurent Pinchart 		uvcg_info(f, "invalid request type\n");
23400a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
23500a2430fSAndrzej Pietrasiewicz 	}
23600a2430fSAndrzej Pietrasiewicz 
23700a2430fSAndrzej Pietrasiewicz 	/* Stall too big requests. */
23800a2430fSAndrzej Pietrasiewicz 	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
23900a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
24000a2430fSAndrzej Pietrasiewicz 
241c5d337a3SLaurent Pinchart 	/*
242c5d337a3SLaurent Pinchart 	 * Tell the complete callback to generate an event for the next request
24326a029f2SLaurent Pinchart 	 * that will be enqueued by UVCIOC_SEND_RESPONSE.
24426a029f2SLaurent Pinchart 	 */
24526a029f2SLaurent Pinchart 	uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
24626a029f2SLaurent Pinchart 	uvc->event_length = le16_to_cpu(ctrl->wLength);
24726a029f2SLaurent Pinchart 
24800a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
24900a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_SETUP;
25000a2430fSAndrzej Pietrasiewicz 	memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
251d182bf15SMichael Grzeschik 
252d182bf15SMichael Grzeschik 	/* check for the interface number, fixup the interface number in
253d182bf15SMichael Grzeschik 	 * the ctrl request so the userspace doesn't have to bother with
254d182bf15SMichael Grzeschik 	 * offset and configfs parsing
255d182bf15SMichael Grzeschik 	 */
256d182bf15SMichael Grzeschik 	mctrl = &uvc_event->req;
257d182bf15SMichael Grzeschik 	mctrl->wIndex &= ~cpu_to_le16(0xff);
258d182bf15SMichael Grzeschik 	if (interface == uvc->streaming_intf)
259d182bf15SMichael Grzeschik 		mctrl->wIndex = cpu_to_le16(UVC_STRING_STREAMING_IDX);
260d182bf15SMichael Grzeschik 
261dbe98b30SHans Verkuil 	v4l2_event_queue(&uvc->vdev, &v4l2_event);
26200a2430fSAndrzej Pietrasiewicz 
26300a2430fSAndrzej Pietrasiewicz 	return 0;
26400a2430fSAndrzej Pietrasiewicz }
26500a2430fSAndrzej Pietrasiewicz 
uvc_function_setup_continue(struct uvc_device * uvc,int disable_ep)266991544dcSAvichal Rakesh void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
26700a2430fSAndrzej Pietrasiewicz {
26800a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
26900a2430fSAndrzej Pietrasiewicz 
270991544dcSAvichal Rakesh 	if (disable_ep && uvc->video.ep)
271991544dcSAvichal Rakesh 		usb_ep_disable(uvc->video.ep);
272991544dcSAvichal Rakesh 
27300a2430fSAndrzej Pietrasiewicz 	usb_composite_setup_continue(cdev);
27400a2430fSAndrzej Pietrasiewicz }
27500a2430fSAndrzej Pietrasiewicz 
27600a2430fSAndrzej Pietrasiewicz static int
uvc_function_get_alt(struct usb_function * f,unsigned interface)27700a2430fSAndrzej Pietrasiewicz uvc_function_get_alt(struct usb_function *f, unsigned interface)
27800a2430fSAndrzej Pietrasiewicz {
27900a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
28000a2430fSAndrzej Pietrasiewicz 
281dc0f755bSLaurent Pinchart 	uvcg_info(f, "%s(%u)\n", __func__, interface);
28200a2430fSAndrzej Pietrasiewicz 
28300a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf)
28400a2430fSAndrzej Pietrasiewicz 		return 0;
28500a2430fSAndrzej Pietrasiewicz 	else if (interface != uvc->streaming_intf)
28600a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
28700a2430fSAndrzej Pietrasiewicz 	else
288d62bf8c1SRobert Baldyga 		return uvc->video.ep->enabled ? 1 : 0;
28900a2430fSAndrzej Pietrasiewicz }
29000a2430fSAndrzej Pietrasiewicz 
29100a2430fSAndrzej Pietrasiewicz static int
uvc_function_set_alt(struct usb_function * f,unsigned interface,unsigned alt)29200a2430fSAndrzej Pietrasiewicz uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
29300a2430fSAndrzej Pietrasiewicz {
29400a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
295c92bae75SFelipe Balbi 	struct usb_composite_dev *cdev = f->config->cdev;
29600a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
29700a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
29800a2430fSAndrzej Pietrasiewicz 	int ret;
29900a2430fSAndrzej Pietrasiewicz 
300dc0f755bSLaurent Pinchart 	uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
30100a2430fSAndrzej Pietrasiewicz 
30200a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf) {
30300a2430fSAndrzej Pietrasiewicz 		if (alt)
30400a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
30500a2430fSAndrzej Pietrasiewicz 
306130c4dcbSDaniel Scally 		if (uvc->enable_interrupt_ep) {
3073078212cSDaniel Scally 			uvcg_info(f, "reset UVC interrupt endpoint\n");
3083078212cSDaniel Scally 			usb_ep_disable(uvc->interrupt_ep);
30962e37078SFelipe Balbi 
3103078212cSDaniel Scally 			if (!uvc->interrupt_ep->desc)
311130c4dcbSDaniel Scally 				if (config_ep_by_speed(cdev->gadget, f,
312130c4dcbSDaniel Scally 						       uvc->interrupt_ep))
31362e37078SFelipe Balbi 					return -EINVAL;
31462e37078SFelipe Balbi 
3153078212cSDaniel Scally 			usb_ep_enable(uvc->interrupt_ep);
316130c4dcbSDaniel Scally 		}
31762e37078SFelipe Balbi 
31800a2430fSAndrzej Pietrasiewicz 		if (uvc->state == UVC_STATE_DISCONNECTED) {
31900a2430fSAndrzej Pietrasiewicz 			memset(&v4l2_event, 0, sizeof(v4l2_event));
32000a2430fSAndrzej Pietrasiewicz 			v4l2_event.type = UVC_EVENT_CONNECT;
321c92bae75SFelipe Balbi 			uvc_event->speed = cdev->gadget->speed;
322dbe98b30SHans Verkuil 			v4l2_event_queue(&uvc->vdev, &v4l2_event);
32300a2430fSAndrzej Pietrasiewicz 
32400a2430fSAndrzej Pietrasiewicz 			uvc->state = UVC_STATE_CONNECTED;
32500a2430fSAndrzej Pietrasiewicz 		}
32600a2430fSAndrzej Pietrasiewicz 
32700a2430fSAndrzej Pietrasiewicz 		return 0;
32800a2430fSAndrzej Pietrasiewicz 	}
32900a2430fSAndrzej Pietrasiewicz 
33000a2430fSAndrzej Pietrasiewicz 	if (interface != uvc->streaming_intf)
33100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
33200a2430fSAndrzej Pietrasiewicz 
33300a2430fSAndrzej Pietrasiewicz 	/* TODO
33400a2430fSAndrzej Pietrasiewicz 	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
33500a2430fSAndrzej Pietrasiewicz 		return alt ? -EINVAL : 0;
33600a2430fSAndrzej Pietrasiewicz 	*/
33700a2430fSAndrzej Pietrasiewicz 
33800a2430fSAndrzej Pietrasiewicz 	switch (alt) {
33900a2430fSAndrzej Pietrasiewicz 	case 0:
34000a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_STREAMING)
34100a2430fSAndrzej Pietrasiewicz 			return 0;
34200a2430fSAndrzej Pietrasiewicz 
34300a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
34400a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMOFF;
345dbe98b30SHans Verkuil 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
34600a2430fSAndrzej Pietrasiewicz 
347991544dcSAvichal Rakesh 		return USB_GADGET_DELAYED_STATUS;
34800a2430fSAndrzej Pietrasiewicz 
34900a2430fSAndrzej Pietrasiewicz 	case 1:
35000a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_CONNECTED)
35100a2430fSAndrzej Pietrasiewicz 			return 0;
35200a2430fSAndrzej Pietrasiewicz 
353c92bae75SFelipe Balbi 		if (!uvc->video.ep)
354c92bae75SFelipe Balbi 			return -EINVAL;
355c92bae75SFelipe Balbi 
356dc0f755bSLaurent Pinchart 		uvcg_info(f, "reset UVC\n");
357c92bae75SFelipe Balbi 		usb_ep_disable(uvc->video.ep);
358c92bae75SFelipe Balbi 
35900a2430fSAndrzej Pietrasiewicz 		ret = config_ep_by_speed(f->config->cdev->gadget,
36000a2430fSAndrzej Pietrasiewicz 				&(uvc->func), uvc->video.ep);
36100a2430fSAndrzej Pietrasiewicz 		if (ret)
36200a2430fSAndrzej Pietrasiewicz 			return ret;
36300a2430fSAndrzej Pietrasiewicz 		usb_ep_enable(uvc->video.ep);
36400a2430fSAndrzej Pietrasiewicz 
36500a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
36600a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMON;
367dbe98b30SHans Verkuil 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
36800a2430fSAndrzej Pietrasiewicz 		return USB_GADGET_DELAYED_STATUS;
36900a2430fSAndrzej Pietrasiewicz 
37000a2430fSAndrzej Pietrasiewicz 	default:
37100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
37200a2430fSAndrzej Pietrasiewicz 	}
37300a2430fSAndrzej Pietrasiewicz }
37400a2430fSAndrzej Pietrasiewicz 
37500a2430fSAndrzej Pietrasiewicz static void
uvc_function_disable(struct usb_function * f)37600a2430fSAndrzej Pietrasiewicz uvc_function_disable(struct usb_function *f)
37700a2430fSAndrzej Pietrasiewicz {
37800a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
37900a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
38000a2430fSAndrzej Pietrasiewicz 
381dc0f755bSLaurent Pinchart 	uvcg_info(f, "%s()\n", __func__);
38200a2430fSAndrzej Pietrasiewicz 
38300a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
38400a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_DISCONNECT;
385dbe98b30SHans Verkuil 	v4l2_event_queue(&uvc->vdev, &v4l2_event);
38600a2430fSAndrzej Pietrasiewicz 
38700a2430fSAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
388e3122f5fSFelipe Balbi 
389e3122f5fSFelipe Balbi 	usb_ep_disable(uvc->video.ep);
390130c4dcbSDaniel Scally 	if (uvc->enable_interrupt_ep)
3913078212cSDaniel Scally 		usb_ep_disable(uvc->interrupt_ep);
39200a2430fSAndrzej Pietrasiewicz }
39300a2430fSAndrzej Pietrasiewicz 
39400a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
39500a2430fSAndrzej Pietrasiewicz  * Connection / disconnection
39600a2430fSAndrzej Pietrasiewicz  */
39700a2430fSAndrzej Pietrasiewicz 
39800a2430fSAndrzej Pietrasiewicz void
uvc_function_connect(struct uvc_device * uvc)39900a2430fSAndrzej Pietrasiewicz uvc_function_connect(struct uvc_device *uvc)
40000a2430fSAndrzej Pietrasiewicz {
40100a2430fSAndrzej Pietrasiewicz 	int ret;
40200a2430fSAndrzej Pietrasiewicz 
40300a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_activate(&uvc->func)) < 0)
404dc0f755bSLaurent Pinchart 		uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);
40500a2430fSAndrzej Pietrasiewicz }
40600a2430fSAndrzej Pietrasiewicz 
40700a2430fSAndrzej Pietrasiewicz void
uvc_function_disconnect(struct uvc_device * uvc)40800a2430fSAndrzej Pietrasiewicz uvc_function_disconnect(struct uvc_device *uvc)
40900a2430fSAndrzej Pietrasiewicz {
41000a2430fSAndrzej Pietrasiewicz 	int ret;
41100a2430fSAndrzej Pietrasiewicz 
41200a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
413dc0f755bSLaurent Pinchart 		uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);
41400a2430fSAndrzej Pietrasiewicz }
41500a2430fSAndrzej Pietrasiewicz 
41600a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
41700a2430fSAndrzej Pietrasiewicz  * USB probe and disconnect
41800a2430fSAndrzej Pietrasiewicz  */
41900a2430fSAndrzej Pietrasiewicz 
function_name_show(struct device * dev,struct device_attribute * attr,char * buf)420d7af78b9SKieran Bingham static ssize_t function_name_show(struct device *dev,
421d7af78b9SKieran Bingham 				  struct device_attribute *attr, char *buf)
422d7af78b9SKieran Bingham {
423d7af78b9SKieran Bingham 	struct uvc_device *uvc = dev_get_drvdata(dev);
424d7af78b9SKieran Bingham 
425d7af78b9SKieran Bingham 	return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);
426d7af78b9SKieran Bingham }
427d7af78b9SKieran Bingham 
428d7af78b9SKieran Bingham static DEVICE_ATTR_RO(function_name);
429d7af78b9SKieran Bingham 
43000a2430fSAndrzej Pietrasiewicz static int
uvc_register_video(struct uvc_device * uvc)43100a2430fSAndrzej Pietrasiewicz uvc_register_video(struct uvc_device *uvc)
43200a2430fSAndrzej Pietrasiewicz {
43300a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
434d7af78b9SKieran Bingham 	int ret;
43500a2430fSAndrzej Pietrasiewicz 
43600a2430fSAndrzej Pietrasiewicz 	/* TODO reference counting. */
437a15e17acSNathan Chancellor 	memset(&uvc->vdev, 0, sizeof(uvc->vdev));
438dbe98b30SHans Verkuil 	uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
439b9b82d3dSMichael Grzeschik 	uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev;
440dbe98b30SHans Verkuil 	uvc->vdev.fops = &uvc_v4l2_fops;
441dbe98b30SHans Verkuil 	uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
442dbe98b30SHans Verkuil 	uvc->vdev.release = video_device_release_empty;
443dbe98b30SHans Verkuil 	uvc->vdev.vfl_dir = VFL_DIR_TX;
444dbe98b30SHans Verkuil 	uvc->vdev.lock = &uvc->video.mutex;
4451397e3ecSHans Verkuil 	uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
446b7db5733SWolfram Sang 	strscpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
44700a2430fSAndrzej Pietrasiewicz 
448dbe98b30SHans Verkuil 	video_set_drvdata(&uvc->vdev, uvc);
44900a2430fSAndrzej Pietrasiewicz 
4500ceba550SHans Verkuil 	ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1);
451d7af78b9SKieran Bingham 	if (ret < 0)
452d7af78b9SKieran Bingham 		return ret;
453d7af78b9SKieran Bingham 
454d7af78b9SKieran Bingham 	ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
455d7af78b9SKieran Bingham 	if (ret < 0) {
456d7af78b9SKieran Bingham 		video_unregister_device(&uvc->vdev);
457d7af78b9SKieran Bingham 		return ret;
458d7af78b9SKieran Bingham 	}
459d7af78b9SKieran Bingham 
460d7af78b9SKieran Bingham 	return 0;
46100a2430fSAndrzej Pietrasiewicz }
46200a2430fSAndrzej Pietrasiewicz 
46300a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
46400a2430fSAndrzej Pietrasiewicz 	do { \
46500a2430fSAndrzej Pietrasiewicz 		memcpy(mem, desc, (desc)->bLength); \
46600a2430fSAndrzej Pietrasiewicz 		*(dst)++ = mem; \
46700a2430fSAndrzej Pietrasiewicz 		mem += (desc)->bLength; \
46800a2430fSAndrzej Pietrasiewicz 	} while (0);
46900a2430fSAndrzej Pietrasiewicz 
47000a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
47100a2430fSAndrzej Pietrasiewicz 	do { \
47200a2430fSAndrzej Pietrasiewicz 		const struct usb_descriptor_header * const *__src; \
47300a2430fSAndrzej Pietrasiewicz 		for (__src = src; *__src; ++__src) { \
47400a2430fSAndrzej Pietrasiewicz 			memcpy(mem, *__src, (*__src)->bLength); \
47500a2430fSAndrzej Pietrasiewicz 			*dst++ = mem; \
47600a2430fSAndrzej Pietrasiewicz 			mem += (*__src)->bLength; \
47700a2430fSAndrzej Pietrasiewicz 		} \
47800a2430fSAndrzej Pietrasiewicz 	} while (0)
47900a2430fSAndrzej Pietrasiewicz 
480a7289452SDaniel Scally #define UVC_COPY_XU_DESCRIPTOR(mem, dst, desc)					\
481a7289452SDaniel Scally 	do {									\
482a7289452SDaniel Scally 		*(dst)++ = mem;							\
483a7289452SDaniel Scally 		memcpy(mem, desc, 22); /* bLength to bNrInPins */		\
484a7289452SDaniel Scally 		mem += 22;							\
485a7289452SDaniel Scally 										\
486a7289452SDaniel Scally 		memcpy(mem, (desc)->baSourceID, (desc)->bNrInPins);		\
487a7289452SDaniel Scally 		mem += (desc)->bNrInPins;					\
488a7289452SDaniel Scally 										\
489a7289452SDaniel Scally 		memcpy(mem, &(desc)->bControlSize, 1);				\
490a7289452SDaniel Scally 		mem++;								\
491a7289452SDaniel Scally 										\
492a7289452SDaniel Scally 		memcpy(mem, (desc)->bmControls, (desc)->bControlSize);		\
493a7289452SDaniel Scally 		mem += (desc)->bControlSize;					\
494a7289452SDaniel Scally 										\
495a7289452SDaniel Scally 		memcpy(mem, &(desc)->iExtension, 1);				\
496a7289452SDaniel Scally 		mem++;								\
497a7289452SDaniel Scally 	} while (0)
498a7289452SDaniel Scally 
4996d11ed76SAndrzej Pietrasiewicz static struct usb_descriptor_header **
uvc_copy_descriptors(struct uvc_device * uvc,enum usb_device_speed speed)50000a2430fSAndrzej Pietrasiewicz uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
50100a2430fSAndrzej Pietrasiewicz {
50200a2430fSAndrzej Pietrasiewicz 	struct uvc_input_header_descriptor *uvc_streaming_header;
50300a2430fSAndrzej Pietrasiewicz 	struct uvc_header_descriptor *uvc_control_header;
50400a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_control_desc;
50500a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_streaming_cls;
50600a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *uvc_streaming_std;
50700a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *src;
50800a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **dst;
50900a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **hdr;
510a7289452SDaniel Scally 	struct uvcg_extension *xu;
51100a2430fSAndrzej Pietrasiewicz 	unsigned int control_size;
51200a2430fSAndrzej Pietrasiewicz 	unsigned int streaming_size;
51300a2430fSAndrzej Pietrasiewicz 	unsigned int n_desc;
51400a2430fSAndrzej Pietrasiewicz 	unsigned int bytes;
51500a2430fSAndrzej Pietrasiewicz 	void *mem;
51600a2430fSAndrzej Pietrasiewicz 
51700a2430fSAndrzej Pietrasiewicz 	switch (speed) {
518c70793fbSShuzhen Wang 	case USB_SPEED_SUPER_PLUS:
51900a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_SUPER:
52000a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.ss_control;
52100a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.ss_streaming;
52200a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_ss_streaming;
52300a2430fSAndrzej Pietrasiewicz 		break;
52400a2430fSAndrzej Pietrasiewicz 
52500a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_HIGH:
52600a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
52700a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.hs_streaming;
52800a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_hs_streaming;
52900a2430fSAndrzej Pietrasiewicz 		break;
53000a2430fSAndrzej Pietrasiewicz 
53100a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_FULL:
53200a2430fSAndrzej Pietrasiewicz 	default:
53300a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
53400a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.fs_streaming;
53500a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_fs_streaming;
53600a2430fSAndrzej Pietrasiewicz 		break;
53700a2430fSAndrzej Pietrasiewicz 	}
53800a2430fSAndrzej Pietrasiewicz 
5396c25955eSAndrzej Pietrasiewicz 	if (!uvc_control_desc || !uvc_streaming_cls)
5406c25955eSAndrzej Pietrasiewicz 		return ERR_PTR(-ENODEV);
5416c25955eSAndrzej Pietrasiewicz 
542c5d337a3SLaurent Pinchart 	/*
543c5d337a3SLaurent Pinchart 	 * Descriptors layout
54400a2430fSAndrzej Pietrasiewicz 	 *
54500a2430fSAndrzej Pietrasiewicz 	 * uvc_iad
54600a2430fSAndrzej Pietrasiewicz 	 * uvc_control_intf
54700a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC control descriptors
5483078212cSDaniel Scally 	 * uvc_interrupt_ep
5493078212cSDaniel Scally 	 * uvc_interrupt_cs_ep
5503078212cSDaniel Scally 	 * uvc_ss_interrupt_comp (for SS only)
55100a2430fSAndrzej Pietrasiewicz 	 * uvc_streaming_intf_alt0
55200a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC streaming descriptors
55300a2430fSAndrzej Pietrasiewicz 	 * uvc_{fs|hs}_streaming
55400a2430fSAndrzej Pietrasiewicz 	 */
55500a2430fSAndrzej Pietrasiewicz 
55600a2430fSAndrzej Pietrasiewicz 	/* Count descriptors and compute their size. */
55700a2430fSAndrzej Pietrasiewicz 	control_size = 0;
55800a2430fSAndrzej Pietrasiewicz 	streaming_size = 0;
55900a2430fSAndrzej Pietrasiewicz 	bytes = uvc_iad.bLength + uvc_control_intf.bLength
56000a2430fSAndrzej Pietrasiewicz 	      + uvc_streaming_intf_alt0.bLength;
56100a2430fSAndrzej Pietrasiewicz 
562130c4dcbSDaniel Scally 	n_desc = 3;
563130c4dcbSDaniel Scally 	if (uvc->enable_interrupt_ep) {
564130c4dcbSDaniel Scally 		bytes += uvc_interrupt_ep.bLength + uvc_interrupt_cs_ep.bLength;
565130c4dcbSDaniel Scally 		n_desc += 2;
566130c4dcbSDaniel Scally 
567c70793fbSShuzhen Wang 		if (speed == USB_SPEED_SUPER ||
568c70793fbSShuzhen Wang 		    speed == USB_SPEED_SUPER_PLUS) {
5693078212cSDaniel Scally 			bytes += uvc_ss_interrupt_comp.bLength;
570130c4dcbSDaniel Scally 			n_desc += 1;
571130c4dcbSDaniel Scally 		}
57200a2430fSAndrzej Pietrasiewicz 	}
57300a2430fSAndrzej Pietrasiewicz 
57400a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_control_desc;
57500a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
57600a2430fSAndrzej Pietrasiewicz 		control_size += (*src)->bLength;
57700a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
57800a2430fSAndrzej Pietrasiewicz 		n_desc++;
57900a2430fSAndrzej Pietrasiewicz 	}
580a7289452SDaniel Scally 
581a7289452SDaniel Scally 	list_for_each_entry(xu, uvc->desc.extension_units, list) {
582a7289452SDaniel Scally 		control_size += xu->desc.bLength;
583a7289452SDaniel Scally 		bytes += xu->desc.bLength;
584a7289452SDaniel Scally 		n_desc++;
585a7289452SDaniel Scally 	}
586a7289452SDaniel Scally 
58700a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
58800a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
58900a2430fSAndrzej Pietrasiewicz 		streaming_size += (*src)->bLength;
59000a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
59100a2430fSAndrzej Pietrasiewicz 		n_desc++;
59200a2430fSAndrzej Pietrasiewicz 	}
59300a2430fSAndrzej Pietrasiewicz 	for (src = uvc_streaming_std; *src; ++src) {
59400a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
59500a2430fSAndrzej Pietrasiewicz 		n_desc++;
59600a2430fSAndrzej Pietrasiewicz 	}
59700a2430fSAndrzej Pietrasiewicz 
59800a2430fSAndrzej Pietrasiewicz 	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
59900a2430fSAndrzej Pietrasiewicz 	if (mem == NULL)
60000a2430fSAndrzej Pietrasiewicz 		return NULL;
60100a2430fSAndrzej Pietrasiewicz 
60200a2430fSAndrzej Pietrasiewicz 	hdr = mem;
60300a2430fSAndrzej Pietrasiewicz 	dst = mem;
60400a2430fSAndrzej Pietrasiewicz 	mem += (n_desc + 1) * sizeof(*src);
60500a2430fSAndrzej Pietrasiewicz 
60600a2430fSAndrzej Pietrasiewicz 	/* Copy the descriptors. */
60700a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
60800a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
60900a2430fSAndrzej Pietrasiewicz 
61000a2430fSAndrzej Pietrasiewicz 	uvc_control_header = mem;
61100a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
61200a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header **)uvc_control_desc);
613a7289452SDaniel Scally 
614a7289452SDaniel Scally 	list_for_each_entry(xu, uvc->desc.extension_units, list)
615a7289452SDaniel Scally 		UVC_COPY_XU_DESCRIPTOR(mem, dst, &xu->desc);
616a7289452SDaniel Scally 
61700a2430fSAndrzej Pietrasiewicz 	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
61800a2430fSAndrzej Pietrasiewicz 	uvc_control_header->bInCollection = 1;
61900a2430fSAndrzej Pietrasiewicz 	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
62000a2430fSAndrzej Pietrasiewicz 
621130c4dcbSDaniel Scally 	if (uvc->enable_interrupt_ep) {
6223078212cSDaniel Scally 		UVC_COPY_DESCRIPTOR(mem, dst, &uvc_interrupt_ep);
623c70793fbSShuzhen Wang 		if (speed == USB_SPEED_SUPER ||
624c70793fbSShuzhen Wang 		    speed == USB_SPEED_SUPER_PLUS)
6253078212cSDaniel Scally 			UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_interrupt_comp);
62600a2430fSAndrzej Pietrasiewicz 
6273078212cSDaniel Scally 		UVC_COPY_DESCRIPTOR(mem, dst, &uvc_interrupt_cs_ep);
628130c4dcbSDaniel Scally 	}
629130c4dcbSDaniel Scally 
63000a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
63100a2430fSAndrzej Pietrasiewicz 
63200a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header = mem;
63300a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
63400a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header**)uvc_streaming_cls);
63500a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
63600a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
63700a2430fSAndrzej Pietrasiewicz 
63800a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
63900a2430fSAndrzej Pietrasiewicz 
64000a2430fSAndrzej Pietrasiewicz 	*dst = NULL;
64100a2430fSAndrzej Pietrasiewicz 	return hdr;
64200a2430fSAndrzej Pietrasiewicz }
64300a2430fSAndrzej Pietrasiewicz 
6446d11ed76SAndrzej Pietrasiewicz static int
uvc_function_bind(struct usb_configuration * c,struct usb_function * f)64500a2430fSAndrzej Pietrasiewicz uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
64600a2430fSAndrzej Pietrasiewicz {
64700a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
64800a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
6499963f744SDaniel Scally 	struct uvcg_extension *xu;
65013443799SAndrzej Pietrasiewicz 	struct usb_string *us;
65100a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_mult;
65200a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_size;
65300a2430fSAndrzej Pietrasiewicz 	struct usb_ep *ep;
6546d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
65500a2430fSAndrzej Pietrasiewicz 	int ret = -EINVAL;
65600a2430fSAndrzej Pietrasiewicz 
657dc0f755bSLaurent Pinchart 	uvcg_info(f, "%s()\n", __func__);
65800a2430fSAndrzej Pietrasiewicz 
659bbea6de1SAndrzej Pietrasiewicz 	opts = fi_to_f_uvc_opts(f->fi);
660c5d337a3SLaurent Pinchart 	/* Sanity check the streaming endpoint module parameters. */
6616d11ed76SAndrzej Pietrasiewicz 	opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
6626d11ed76SAndrzej Pietrasiewicz 	opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
6636d11ed76SAndrzej Pietrasiewicz 	opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
6646d11ed76SAndrzej Pietrasiewicz 
66516bb05d9SRoger Quadros 	/* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
66616bb05d9SRoger Quadros 	if (opts->streaming_maxburst &&
66716bb05d9SRoger Quadros 	    (opts->streaming_maxpacket % 1024) != 0) {
66816bb05d9SRoger Quadros 		opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
669dc0f755bSLaurent Pinchart 		uvcg_info(f, "overriding streaming_maxpacket to %d\n",
67016bb05d9SRoger Quadros 			  opts->streaming_maxpacket);
67116bb05d9SRoger Quadros 	}
67216bb05d9SRoger Quadros 
673c5d337a3SLaurent Pinchart 	/*
674c5d337a3SLaurent Pinchart 	 * Fill in the FS/HS/SS Video Streaming specific descriptors from the
6756d11ed76SAndrzej Pietrasiewicz 	 * module parameters.
6766d11ed76SAndrzej Pietrasiewicz 	 *
6776d11ed76SAndrzej Pietrasiewicz 	 * NOTE: We assume that the user knows what they are doing and won't
6786d11ed76SAndrzej Pietrasiewicz 	 * give parameters that their UDC doesn't support.
6796d11ed76SAndrzej Pietrasiewicz 	 */
6806d11ed76SAndrzej Pietrasiewicz 	if (opts->streaming_maxpacket <= 1024) {
6816d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 1;
6826d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket;
6836d11ed76SAndrzej Pietrasiewicz 	} else if (opts->streaming_maxpacket <= 2048) {
6846d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 2;
6856d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket / 2;
6866d11ed76SAndrzej Pietrasiewicz 	} else {
6876d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 3;
6886d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket / 3;
6896d11ed76SAndrzej Pietrasiewicz 	}
6906d11ed76SAndrzej Pietrasiewicz 
6916d11ed76SAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.wMaxPacketSize =
692e102609fSLaurent Pinchart 		cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
6936d11ed76SAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
6946d11ed76SAndrzej Pietrasiewicz 
695e102609fSLaurent Pinchart 	uvc_hs_streaming_ep.wMaxPacketSize =
696e102609fSLaurent Pinchart 		cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
69726adde04SPawel Laszczak 
69826adde04SPawel Laszczak 	/* A high-bandwidth endpoint must specify a bInterval value of 1 */
69926adde04SPawel Laszczak 	if (max_packet_mult > 1)
70026adde04SPawel Laszczak 		uvc_hs_streaming_ep.bInterval = 1;
70126adde04SPawel Laszczak 	else
7026d11ed76SAndrzej Pietrasiewicz 		uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
7036d11ed76SAndrzej Pietrasiewicz 
704e102609fSLaurent Pinchart 	uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
7056d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
7066d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
7076d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
7086d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.wBytesPerInterval =
709e102609fSLaurent Pinchart 		cpu_to_le16(max_packet_size * max_packet_mult *
71009424c50SRoger Quadros 			    (opts->streaming_maxburst + 1));
71100a2430fSAndrzej Pietrasiewicz 
71200a2430fSAndrzej Pietrasiewicz 	/* Allocate endpoints. */
713130c4dcbSDaniel Scally 	if (opts->enable_interrupt_ep) {
7143078212cSDaniel Scally 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_interrupt_ep);
71500a2430fSAndrzej Pietrasiewicz 		if (!ep) {
716130c4dcbSDaniel Scally 			uvcg_info(f, "Unable to allocate interrupt EP\n");
71700a2430fSAndrzej Pietrasiewicz 			goto error;
71800a2430fSAndrzej Pietrasiewicz 		}
7193078212cSDaniel Scally 		uvc->interrupt_ep = ep;
720130c4dcbSDaniel Scally 		uvc_control_intf.bNumEndpoints = 1;
721130c4dcbSDaniel Scally 	}
722130c4dcbSDaniel Scally 	uvc->enable_interrupt_ep = opts->enable_interrupt_ep;
72300a2430fSAndrzej Pietrasiewicz 
724895ee5aeSFrank Li 	/*
725895ee5aeSFrank Li 	 * gadget_is_{super|dual}speed() API check UDC controller capitblity. It should pass down
726895ee5aeSFrank Li 	 * highest speed endpoint descriptor to UDC controller. So UDC controller driver can reserve
727895ee5aeSFrank Li 	 * enough resource at check_config(), especially mult and maxburst. So UDC driver (such as
728895ee5aeSFrank Li 	 * cdns3) can know need at least (mult + 1) * (maxburst + 1) * wMaxPacketSize internal
729895ee5aeSFrank Li 	 * memory for this uvc functions. This is the only straightforward method to resolve the UDC
730895ee5aeSFrank Li 	 * resource allocation issue in the current gadget framework.
731895ee5aeSFrank Li 	 */
732895ee5aeSFrank Li 	if (gadget_is_superspeed(c->cdev->gadget))
733895ee5aeSFrank Li 		ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
734895ee5aeSFrank Li 					  &uvc_ss_streaming_comp);
735895ee5aeSFrank Li 	else if (gadget_is_dualspeed(cdev->gadget))
736895ee5aeSFrank Li 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
737895ee5aeSFrank Li 	else
73800a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
739895ee5aeSFrank Li 
74000a2430fSAndrzej Pietrasiewicz 	if (!ep) {
741dc0f755bSLaurent Pinchart 		uvcg_info(f, "Unable to allocate streaming EP\n");
74200a2430fSAndrzej Pietrasiewicz 		goto error;
74300a2430fSAndrzej Pietrasiewicz 	}
74400a2430fSAndrzej Pietrasiewicz 	uvc->video.ep = ep;
74500a2430fSAndrzej Pietrasiewicz 
746895ee5aeSFrank Li 	uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
74700a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
74800a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
74900a2430fSAndrzej Pietrasiewicz 
7509963f744SDaniel Scally 	/*
7519963f744SDaniel Scally 	 * XUs can have an arbitrary string descriptor describing them. If they
7529963f744SDaniel Scally 	 * have one pick up the ID.
7539963f744SDaniel Scally 	 */
7549963f744SDaniel Scally 	list_for_each_entry(xu, &opts->extension_units, list)
7559963f744SDaniel Scally 		if (xu->string_descriptor_index)
7569963f744SDaniel Scally 			xu->desc.iExtension = cdev->usb_strings[xu->string_descriptor_index].id;
7579963f744SDaniel Scally 
758cf13d6e4SDaniel Scally 	/*
759cf13d6e4SDaniel Scally 	 * We attach the hard-coded defaults incase the user does not provide
760cf13d6e4SDaniel Scally 	 * any more appropriate strings through configfs.
761cf13d6e4SDaniel Scally 	 */
762324e4f85SDan Vacura 	uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name;
76313443799SAndrzej Pietrasiewicz 	us = usb_gstrings_attach(cdev, uvc_function_strings,
76413443799SAndrzej Pietrasiewicz 				 ARRAY_SIZE(uvc_en_us_strings));
76513443799SAndrzej Pietrasiewicz 	if (IS_ERR(us)) {
76613443799SAndrzej Pietrasiewicz 		ret = PTR_ERR(us);
7676d11ed76SAndrzej Pietrasiewicz 		goto error;
76813443799SAndrzej Pietrasiewicz 	}
769cf13d6e4SDaniel Scally 
770cf13d6e4SDaniel Scally 	uvc_iad.iFunction = opts->iad_index ? cdev->usb_strings[opts->iad_index].id :
771cf13d6e4SDaniel Scally 			    us[UVC_STRING_CONTROL_IDX].id;
772cf13d6e4SDaniel Scally 	uvc_streaming_intf_alt0.iInterface = opts->vs0_index ?
773cf13d6e4SDaniel Scally 					     cdev->usb_strings[opts->vs0_index].id :
774cf13d6e4SDaniel Scally 					     us[UVC_STRING_STREAMING_IDX].id;
775cf13d6e4SDaniel Scally 	uvc_streaming_intf_alt1.iInterface = opts->vs1_index ?
776cf13d6e4SDaniel Scally 					     cdev->usb_strings[opts->vs1_index].id :
777cf13d6e4SDaniel Scally 					     us[UVC_STRING_STREAMING_IDX].id;
7786d11ed76SAndrzej Pietrasiewicz 
77900a2430fSAndrzej Pietrasiewicz 	/* Allocate interface IDs. */
78000a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
78100a2430fSAndrzej Pietrasiewicz 		goto error;
78200a2430fSAndrzej Pietrasiewicz 	uvc_iad.bFirstInterface = ret;
78300a2430fSAndrzej Pietrasiewicz 	uvc_control_intf.bInterfaceNumber = ret;
78400a2430fSAndrzej Pietrasiewicz 	uvc->control_intf = ret;
785bf715448SLaurent Pinchart 	opts->control_interface = ret;
78600a2430fSAndrzej Pietrasiewicz 
78700a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
78800a2430fSAndrzej Pietrasiewicz 		goto error;
78900a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
79000a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
79100a2430fSAndrzej Pietrasiewicz 	uvc->streaming_intf = ret;
792bf715448SLaurent Pinchart 	opts->streaming_interface = ret;
79300a2430fSAndrzej Pietrasiewicz 
79400a2430fSAndrzej Pietrasiewicz 	/* Copy descriptors */
79500a2430fSAndrzej Pietrasiewicz 	f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
7966c25955eSAndrzej Pietrasiewicz 	if (IS_ERR(f->fs_descriptors)) {
7976c25955eSAndrzej Pietrasiewicz 		ret = PTR_ERR(f->fs_descriptors);
7986c25955eSAndrzej Pietrasiewicz 		f->fs_descriptors = NULL;
7996c25955eSAndrzej Pietrasiewicz 		goto error;
8006c25955eSAndrzej Pietrasiewicz 	}
80146decc82SLinyu Yuan 
80200a2430fSAndrzej Pietrasiewicz 	f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
8036c25955eSAndrzej Pietrasiewicz 	if (IS_ERR(f->hs_descriptors)) {
8046c25955eSAndrzej Pietrasiewicz 		ret = PTR_ERR(f->hs_descriptors);
8056c25955eSAndrzej Pietrasiewicz 		f->hs_descriptors = NULL;
8066c25955eSAndrzej Pietrasiewicz 		goto error;
8076c25955eSAndrzej Pietrasiewicz 	}
80846decc82SLinyu Yuan 
80900a2430fSAndrzej Pietrasiewicz 	f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
8106c25955eSAndrzej Pietrasiewicz 	if (IS_ERR(f->ss_descriptors)) {
8116c25955eSAndrzej Pietrasiewicz 		ret = PTR_ERR(f->ss_descriptors);
8126c25955eSAndrzej Pietrasiewicz 		f->ss_descriptors = NULL;
8136c25955eSAndrzej Pietrasiewicz 		goto error;
8146c25955eSAndrzej Pietrasiewicz 	}
81500a2430fSAndrzej Pietrasiewicz 
816c70793fbSShuzhen Wang 	f->ssp_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER_PLUS);
817c70793fbSShuzhen Wang 	if (IS_ERR(f->ssp_descriptors)) {
818c70793fbSShuzhen Wang 		ret = PTR_ERR(f->ssp_descriptors);
819c70793fbSShuzhen Wang 		f->ssp_descriptors = NULL;
820c70793fbSShuzhen Wang 		goto error;
821c70793fbSShuzhen Wang 	}
822c70793fbSShuzhen Wang 
82300a2430fSAndrzej Pietrasiewicz 	/* Preallocate control endpoint request. */
82400a2430fSAndrzej Pietrasiewicz 	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
82500a2430fSAndrzej Pietrasiewicz 	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
82600a2430fSAndrzej Pietrasiewicz 	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
82700a2430fSAndrzej Pietrasiewicz 		ret = -ENOMEM;
82800a2430fSAndrzej Pietrasiewicz 		goto error;
82900a2430fSAndrzej Pietrasiewicz 	}
83000a2430fSAndrzej Pietrasiewicz 
83100a2430fSAndrzej Pietrasiewicz 	uvc->control_req->buf = uvc->control_buf;
83200a2430fSAndrzej Pietrasiewicz 	uvc->control_req->complete = uvc_function_ep0_complete;
83300a2430fSAndrzej Pietrasiewicz 	uvc->control_req->context = uvc;
83400a2430fSAndrzej Pietrasiewicz 
83500a2430fSAndrzej Pietrasiewicz 	if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
836dc0f755bSLaurent Pinchart 		uvcg_err(f, "failed to register V4L2 device\n");
83700a2430fSAndrzej Pietrasiewicz 		goto error;
83800a2430fSAndrzej Pietrasiewicz 	}
83900a2430fSAndrzej Pietrasiewicz 
84000a2430fSAndrzej Pietrasiewicz 	/* Initialise video. */
841dc0f755bSLaurent Pinchart 	ret = uvcg_video_init(&uvc->video, uvc);
84200a2430fSAndrzej Pietrasiewicz 	if (ret < 0)
843f0c48566SZqiang 		goto v4l2_error;
84400a2430fSAndrzej Pietrasiewicz 
84500a2430fSAndrzej Pietrasiewicz 	/* Register a V4L2 device. */
84600a2430fSAndrzej Pietrasiewicz 	ret = uvc_register_video(uvc);
84700a2430fSAndrzej Pietrasiewicz 	if (ret < 0) {
848dc0f755bSLaurent Pinchart 		uvcg_err(f, "failed to register video device\n");
849f0c48566SZqiang 		goto v4l2_error;
85000a2430fSAndrzej Pietrasiewicz 	}
85100a2430fSAndrzej Pietrasiewicz 
85200a2430fSAndrzej Pietrasiewicz 	return 0;
85300a2430fSAndrzej Pietrasiewicz 
854f0c48566SZqiang v4l2_error:
85500a2430fSAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
856f0c48566SZqiang error:
857e7379857SAndrzej Pietrasiewicz 	if (uvc->control_req)
85800a2430fSAndrzej Pietrasiewicz 		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
85900a2430fSAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
86000a2430fSAndrzej Pietrasiewicz 
86100a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
86200a2430fSAndrzej Pietrasiewicz 	return ret;
86300a2430fSAndrzej Pietrasiewicz }
86400a2430fSAndrzej Pietrasiewicz 
86500a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
86600a2430fSAndrzej Pietrasiewicz  * USB gadget function
86700a2430fSAndrzej Pietrasiewicz  */
86800a2430fSAndrzej Pietrasiewicz 
uvc_free_inst(struct usb_function_instance * f)8696d11ed76SAndrzej Pietrasiewicz static void uvc_free_inst(struct usb_function_instance *f)
8706d11ed76SAndrzej Pietrasiewicz {
871bbea6de1SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
8726d11ed76SAndrzej Pietrasiewicz 
87346919a23SAndrzej Pietrasiewicz 	mutex_destroy(&opts->lock);
8746d11ed76SAndrzej Pietrasiewicz 	kfree(opts);
8756d11ed76SAndrzej Pietrasiewicz }
8766d11ed76SAndrzej Pietrasiewicz 
uvc_alloc_inst(void)8776d11ed76SAndrzej Pietrasiewicz static struct usb_function_instance *uvc_alloc_inst(void)
8786d11ed76SAndrzej Pietrasiewicz {
8796d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
88046919a23SAndrzej Pietrasiewicz 	struct uvc_camera_terminal_descriptor *cd;
88146919a23SAndrzej Pietrasiewicz 	struct uvc_processing_unit_descriptor *pd;
88246919a23SAndrzej Pietrasiewicz 	struct uvc_output_terminal_descriptor *od;
88346919a23SAndrzej Pietrasiewicz 	struct uvc_descriptor_header **ctl_cls;
884efbf0af7SLaurent Pinchart 	int ret;
8856d11ed76SAndrzej Pietrasiewicz 
8866d11ed76SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
8876d11ed76SAndrzej Pietrasiewicz 	if (!opts)
8886d11ed76SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
8896d11ed76SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = uvc_free_inst;
89046919a23SAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
8916d11ed76SAndrzej Pietrasiewicz 
89246919a23SAndrzej Pietrasiewicz 	cd = &opts->uvc_camera_terminal;
89346919a23SAndrzej Pietrasiewicz 	cd->bLength			= UVC_DT_CAMERA_TERMINAL_SIZE(3);
89446919a23SAndrzej Pietrasiewicz 	cd->bDescriptorType		= USB_DT_CS_INTERFACE;
89546919a23SAndrzej Pietrasiewicz 	cd->bDescriptorSubType		= UVC_VC_INPUT_TERMINAL;
89646919a23SAndrzej Pietrasiewicz 	cd->bTerminalID			= 1;
89746919a23SAndrzej Pietrasiewicz 	cd->wTerminalType		= cpu_to_le16(0x0201);
89846919a23SAndrzej Pietrasiewicz 	cd->bAssocTerminal		= 0;
89946919a23SAndrzej Pietrasiewicz 	cd->iTerminal			= 0;
90046919a23SAndrzej Pietrasiewicz 	cd->wObjectiveFocalLengthMin	= cpu_to_le16(0);
90146919a23SAndrzej Pietrasiewicz 	cd->wObjectiveFocalLengthMax	= cpu_to_le16(0);
90246919a23SAndrzej Pietrasiewicz 	cd->wOcularFocalLength		= cpu_to_le16(0);
90346919a23SAndrzej Pietrasiewicz 	cd->bControlSize		= 3;
90446919a23SAndrzej Pietrasiewicz 	cd->bmControls[0]		= 2;
90546919a23SAndrzej Pietrasiewicz 	cd->bmControls[1]		= 0;
90646919a23SAndrzej Pietrasiewicz 	cd->bmControls[2]		= 0;
90746919a23SAndrzej Pietrasiewicz 
90846919a23SAndrzej Pietrasiewicz 	pd = &opts->uvc_processing;
90946919a23SAndrzej Pietrasiewicz 	pd->bLength			= UVC_DT_PROCESSING_UNIT_SIZE(2);
91046919a23SAndrzej Pietrasiewicz 	pd->bDescriptorType		= USB_DT_CS_INTERFACE;
91146919a23SAndrzej Pietrasiewicz 	pd->bDescriptorSubType		= UVC_VC_PROCESSING_UNIT;
91246919a23SAndrzej Pietrasiewicz 	pd->bUnitID			= 2;
91346919a23SAndrzej Pietrasiewicz 	pd->bSourceID			= 1;
91446919a23SAndrzej Pietrasiewicz 	pd->wMaxMultiplier		= cpu_to_le16(16*1024);
91546919a23SAndrzej Pietrasiewicz 	pd->bControlSize		= 2;
91646919a23SAndrzej Pietrasiewicz 	pd->bmControls[0]		= 1;
91746919a23SAndrzej Pietrasiewicz 	pd->bmControls[1]		= 0;
91846919a23SAndrzej Pietrasiewicz 	pd->iProcessing			= 0;
9196a154ec9SPawel Laszczak 	pd->bmVideoStandards		= 0;
92046919a23SAndrzej Pietrasiewicz 
92146919a23SAndrzej Pietrasiewicz 	od = &opts->uvc_output_terminal;
92246919a23SAndrzej Pietrasiewicz 	od->bLength			= UVC_DT_OUTPUT_TERMINAL_SIZE;
92346919a23SAndrzej Pietrasiewicz 	od->bDescriptorType		= USB_DT_CS_INTERFACE;
92446919a23SAndrzej Pietrasiewicz 	od->bDescriptorSubType		= UVC_VC_OUTPUT_TERMINAL;
92546919a23SAndrzej Pietrasiewicz 	od->bTerminalID			= 3;
92646919a23SAndrzej Pietrasiewicz 	od->wTerminalType		= cpu_to_le16(0x0101);
92746919a23SAndrzej Pietrasiewicz 	od->bAssocTerminal		= 0;
92846919a23SAndrzej Pietrasiewicz 	od->bSourceID			= 2;
92946919a23SAndrzej Pietrasiewicz 	od->iTerminal			= 0;
93046919a23SAndrzej Pietrasiewicz 
9310525210cSDaniel Scally 	/*
9320525210cSDaniel Scally 	 * With the ability to add XUs to the UVC function graph, we need to be
9330525210cSDaniel Scally 	 * able to allocate unique unit IDs to them. The IDs are 1-based, with
9340525210cSDaniel Scally 	 * the CT, PU and OT above consuming the first 3.
9350525210cSDaniel Scally 	 */
9360525210cSDaniel Scally 	opts->last_unit_id		= 3;
9370525210cSDaniel Scally 
93846919a23SAndrzej Pietrasiewicz 	/* Prepare fs control class descriptors for configfs-based gadgets */
93946919a23SAndrzej Pietrasiewicz 	ctl_cls = opts->uvc_fs_control_cls;
94046919a23SAndrzej Pietrasiewicz 	ctl_cls[0] = NULL;	/* assigned elsewhere by configfs */
94146919a23SAndrzej Pietrasiewicz 	ctl_cls[1] = (struct uvc_descriptor_header *)cd;
94246919a23SAndrzej Pietrasiewicz 	ctl_cls[2] = (struct uvc_descriptor_header *)pd;
94346919a23SAndrzej Pietrasiewicz 	ctl_cls[3] = (struct uvc_descriptor_header *)od;
94446919a23SAndrzej Pietrasiewicz 	ctl_cls[4] = NULL;	/* NULL-terminate */
94546919a23SAndrzej Pietrasiewicz 	opts->fs_control =
94646919a23SAndrzej Pietrasiewicz 		(const struct uvc_descriptor_header * const *)ctl_cls;
94746919a23SAndrzej Pietrasiewicz 
94846919a23SAndrzej Pietrasiewicz 	/* Prepare hs control class descriptors for configfs-based gadgets */
94946919a23SAndrzej Pietrasiewicz 	ctl_cls = opts->uvc_ss_control_cls;
95046919a23SAndrzej Pietrasiewicz 	ctl_cls[0] = NULL;	/* assigned elsewhere by configfs */
95146919a23SAndrzej Pietrasiewicz 	ctl_cls[1] = (struct uvc_descriptor_header *)cd;
95246919a23SAndrzej Pietrasiewicz 	ctl_cls[2] = (struct uvc_descriptor_header *)pd;
95346919a23SAndrzej Pietrasiewicz 	ctl_cls[3] = (struct uvc_descriptor_header *)od;
95446919a23SAndrzej Pietrasiewicz 	ctl_cls[4] = NULL;	/* NULL-terminate */
95546919a23SAndrzej Pietrasiewicz 	opts->ss_control =
95646919a23SAndrzej Pietrasiewicz 		(const struct uvc_descriptor_header * const *)ctl_cls;
95746919a23SAndrzej Pietrasiewicz 
9580525210cSDaniel Scally 	INIT_LIST_HEAD(&opts->extension_units);
9590525210cSDaniel Scally 
96046919a23SAndrzej Pietrasiewicz 	opts->streaming_interval = 1;
96146919a23SAndrzej Pietrasiewicz 	opts->streaming_maxpacket = 1024;
962324e4f85SDan Vacura 	snprintf(opts->function_name, sizeof(opts->function_name), "UVC Camera");
96346919a23SAndrzej Pietrasiewicz 
964efbf0af7SLaurent Pinchart 	ret = uvcg_attach_configfs(opts);
965efbf0af7SLaurent Pinchart 	if (ret < 0) {
966efbf0af7SLaurent Pinchart 		kfree(opts);
967efbf0af7SLaurent Pinchart 		return ERR_PTR(ret);
968efbf0af7SLaurent Pinchart 	}
969efbf0af7SLaurent Pinchart 
9706d11ed76SAndrzej Pietrasiewicz 	return &opts->func_inst;
9716d11ed76SAndrzej Pietrasiewicz }
9726d11ed76SAndrzej Pietrasiewicz 
uvc_free(struct usb_function * f)9736d11ed76SAndrzej Pietrasiewicz static void uvc_free(struct usb_function *f)
9746d11ed76SAndrzej Pietrasiewicz {
9756d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
97646919a23SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
97746919a23SAndrzej Pietrasiewicz 					       func_inst);
978f1fd91a0SAndrzej Pietrasiewicz 	if (!opts->header)
979588b9e85SMichael Grzeschik 		config_item_put(&uvc->header->item);
98046919a23SAndrzej Pietrasiewicz 	--opts->refcnt;
9816d11ed76SAndrzej Pietrasiewicz 	kfree(uvc);
9826d11ed76SAndrzej Pietrasiewicz }
9836d11ed76SAndrzej Pietrasiewicz 
uvc_function_unbind(struct usb_configuration * c,struct usb_function * f)984e6bab2b6SMichael Tretter static void uvc_function_unbind(struct usb_configuration *c,
985e6bab2b6SMichael Tretter 				struct usb_function *f)
9866d11ed76SAndrzej Pietrasiewicz {
9876d11ed76SAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
9886d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
9899b91a652SMichael Grzeschik 	struct uvc_video *video = &uvc->video;
990b81ac439SDan Vacura 	long wait_ret = 1;
9916d11ed76SAndrzej Pietrasiewicz 
992e6bab2b6SMichael Tretter 	uvcg_info(f, "%s()\n", __func__);
9936d11ed76SAndrzej Pietrasiewicz 
9949b91a652SMichael Grzeschik 	if (video->async_wq)
9959b91a652SMichael Grzeschik 		destroy_workqueue(video->async_wq);
9969b91a652SMichael Grzeschik 
997c5d337a3SLaurent Pinchart 	/*
998c5d337a3SLaurent Pinchart 	 * If we know we're connected via v4l2, then there should be a cleanup
999b81ac439SDan Vacura 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
1000b81ac439SDan Vacura 	 * though the video device removal uevent. Allow some time for the
1001b81ac439SDan Vacura 	 * application to close out before things get deleted.
1002b81ac439SDan Vacura 	 */
1003b81ac439SDan Vacura 	if (uvc->func_connected) {
1004b81ac439SDan Vacura 		uvcg_dbg(f, "waiting for clean disconnect\n");
1005b81ac439SDan Vacura 		wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1006b81ac439SDan Vacura 				uvc->func_connected == false, msecs_to_jiffies(500));
1007b81ac439SDan Vacura 		uvcg_dbg(f, "done waiting with ret: %ld\n", wait_ret);
1008b81ac439SDan Vacura 	}
1009b81ac439SDan Vacura 
1010d7af78b9SKieran Bingham 	device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
1011dbe98b30SHans Verkuil 	video_unregister_device(&uvc->vdev);
10126d11ed76SAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
10136d11ed76SAndrzej Pietrasiewicz 
1014b81ac439SDan Vacura 	if (uvc->func_connected) {
1015c5d337a3SLaurent Pinchart 		/*
1016c5d337a3SLaurent Pinchart 		 * Wait for the release to occur to ensure there are no longer any
1017b81ac439SDan Vacura 		 * pending operations that may cause panics when resources are cleaned
1018b81ac439SDan Vacura 		 * up.
1019b81ac439SDan Vacura 		 */
1020b81ac439SDan Vacura 		uvcg_warn(f, "%s no clean disconnect, wait for release\n", __func__);
1021b81ac439SDan Vacura 		wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1022b81ac439SDan Vacura 				uvc->func_connected == false, msecs_to_jiffies(1000));
1023b81ac439SDan Vacura 		uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret);
1024b81ac439SDan Vacura 	}
1025b81ac439SDan Vacura 
10266d11ed76SAndrzej Pietrasiewicz 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
10276d11ed76SAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
10286d11ed76SAndrzej Pietrasiewicz 
10296d11ed76SAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
10306d11ed76SAndrzej Pietrasiewicz }
10316d11ed76SAndrzej Pietrasiewicz 
uvc_alloc(struct usb_function_instance * fi)10324a6698b8SFengguang Wu static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
10336d11ed76SAndrzej Pietrasiewicz {
10346d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc;
10356d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
103646919a23SAndrzej Pietrasiewicz 	struct uvc_descriptor_header **strm_cls;
1037588b9e85SMichael Grzeschik 	struct config_item *streaming, *header, *h;
10386d11ed76SAndrzej Pietrasiewicz 
10396d11ed76SAndrzej Pietrasiewicz 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
10406d11ed76SAndrzej Pietrasiewicz 	if (uvc == NULL)
10416d11ed76SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
10426d11ed76SAndrzej Pietrasiewicz 
1043d8e96c4bSHans Verkuil 	mutex_init(&uvc->video.mutex);
10446d11ed76SAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
1045b81ac439SDan Vacura 	init_waitqueue_head(&uvc->func_connected_queue);
1046bbea6de1SAndrzej Pietrasiewicz 	opts = fi_to_f_uvc_opts(fi);
10476d11ed76SAndrzej Pietrasiewicz 
104846919a23SAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
104946919a23SAndrzej Pietrasiewicz 	if (opts->uvc_fs_streaming_cls) {
105046919a23SAndrzej Pietrasiewicz 		strm_cls = opts->uvc_fs_streaming_cls;
105146919a23SAndrzej Pietrasiewicz 		opts->fs_streaming =
105246919a23SAndrzej Pietrasiewicz 			(const struct uvc_descriptor_header * const *)strm_cls;
105346919a23SAndrzej Pietrasiewicz 	}
105446919a23SAndrzej Pietrasiewicz 	if (opts->uvc_hs_streaming_cls) {
105546919a23SAndrzej Pietrasiewicz 		strm_cls = opts->uvc_hs_streaming_cls;
105646919a23SAndrzej Pietrasiewicz 		opts->hs_streaming =
105746919a23SAndrzej Pietrasiewicz 			(const struct uvc_descriptor_header * const *)strm_cls;
105846919a23SAndrzej Pietrasiewicz 	}
105946919a23SAndrzej Pietrasiewicz 	if (opts->uvc_ss_streaming_cls) {
106046919a23SAndrzej Pietrasiewicz 		strm_cls = opts->uvc_ss_streaming_cls;
106146919a23SAndrzej Pietrasiewicz 		opts->ss_streaming =
106246919a23SAndrzej Pietrasiewicz 			(const struct uvc_descriptor_header * const *)strm_cls;
106346919a23SAndrzej Pietrasiewicz 	}
106446919a23SAndrzej Pietrasiewicz 
10656d11ed76SAndrzej Pietrasiewicz 	uvc->desc.fs_control = opts->fs_control;
10666d11ed76SAndrzej Pietrasiewicz 	uvc->desc.ss_control = opts->ss_control;
10676d11ed76SAndrzej Pietrasiewicz 	uvc->desc.fs_streaming = opts->fs_streaming;
10686d11ed76SAndrzej Pietrasiewicz 	uvc->desc.hs_streaming = opts->hs_streaming;
10696d11ed76SAndrzej Pietrasiewicz 	uvc->desc.ss_streaming = opts->ss_streaming;
1070588b9e85SMichael Grzeschik 
1071f1fd91a0SAndrzej Pietrasiewicz 	if (opts->header) {
1072f1fd91a0SAndrzej Pietrasiewicz 		uvc->header = opts->header;
1073f1fd91a0SAndrzej Pietrasiewicz 	} else {
1074588b9e85SMichael Grzeschik 		streaming = config_group_find_item(&opts->func_inst.group, "streaming");
1075588b9e85SMichael Grzeschik 		if (!streaming)
1076588b9e85SMichael Grzeschik 			goto err_config;
1077588b9e85SMichael Grzeschik 
1078588b9e85SMichael Grzeschik 		header = config_group_find_item(to_config_group(streaming), "header");
1079588b9e85SMichael Grzeschik 		config_item_put(streaming);
1080588b9e85SMichael Grzeschik 		if (!header)
1081588b9e85SMichael Grzeschik 			goto err_config;
1082588b9e85SMichael Grzeschik 
1083588b9e85SMichael Grzeschik 		h = config_group_find_item(to_config_group(header), "h");
1084588b9e85SMichael Grzeschik 		config_item_put(header);
1085588b9e85SMichael Grzeschik 		if (!h)
1086588b9e85SMichael Grzeschik 			goto err_config;
1087588b9e85SMichael Grzeschik 
1088588b9e85SMichael Grzeschik 		uvc->header = to_uvcg_streaming_header(h);
1089588b9e85SMichael Grzeschik 		if (!uvc->header->linked) {
1090588b9e85SMichael Grzeschik 			mutex_unlock(&opts->lock);
1091588b9e85SMichael Grzeschik 			kfree(uvc);
1092588b9e85SMichael Grzeschik 			return ERR_PTR(-EBUSY);
1093588b9e85SMichael Grzeschik 		}
1094f1fd91a0SAndrzej Pietrasiewicz 	}
1095588b9e85SMichael Grzeschik 
1096a7289452SDaniel Scally 	uvc->desc.extension_units = &opts->extension_units;
1097a7289452SDaniel Scally 
109846919a23SAndrzej Pietrasiewicz 	++opts->refcnt;
109946919a23SAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
11006d11ed76SAndrzej Pietrasiewicz 
11016d11ed76SAndrzej Pietrasiewicz 	/* Register the function. */
11026d11ed76SAndrzej Pietrasiewicz 	uvc->func.name = "uvc";
11036d11ed76SAndrzej Pietrasiewicz 	uvc->func.bind = uvc_function_bind;
1104e6bab2b6SMichael Tretter 	uvc->func.unbind = uvc_function_unbind;
11056d11ed76SAndrzej Pietrasiewicz 	uvc->func.get_alt = uvc_function_get_alt;
11066d11ed76SAndrzej Pietrasiewicz 	uvc->func.set_alt = uvc_function_set_alt;
11076d11ed76SAndrzej Pietrasiewicz 	uvc->func.disable = uvc_function_disable;
11086d11ed76SAndrzej Pietrasiewicz 	uvc->func.setup = uvc_function_setup;
11096d11ed76SAndrzej Pietrasiewicz 	uvc->func.free_func = uvc_free;
1110f277bf27SRobert Baldyga 	uvc->func.bind_deactivated = true;
11116d11ed76SAndrzej Pietrasiewicz 
11126d11ed76SAndrzej Pietrasiewicz 	return &uvc->func;
1113588b9e85SMichael Grzeschik 
1114588b9e85SMichael Grzeschik err_config:
1115588b9e85SMichael Grzeschik 	mutex_unlock(&opts->lock);
1116588b9e85SMichael Grzeschik 	kfree(uvc);
1117588b9e85SMichael Grzeschik 	return ERR_PTR(-ENOENT);
11186d11ed76SAndrzej Pietrasiewicz }
11196d11ed76SAndrzej Pietrasiewicz 
11206d11ed76SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
1121*1cb9ba5eSJeff Johnson MODULE_DESCRIPTION("USB Video Class Gadget driver");
11226d11ed76SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
11236d11ed76SAndrzej Pietrasiewicz MODULE_AUTHOR("Laurent Pinchart");
1124