xref: /openbmc/linux/drivers/usb/gadget/function/f_uvc.c (revision 09424c50b7dff40cb30011c09114404a4656e023)
100a2430fSAndrzej Pietrasiewicz /*
200a2430fSAndrzej Pietrasiewicz  *	uvc_gadget.c  --  USB Video Class Gadget driver
300a2430fSAndrzej Pietrasiewicz  *
400a2430fSAndrzej Pietrasiewicz  *	Copyright (C) 2009-2010
500a2430fSAndrzej Pietrasiewicz  *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
600a2430fSAndrzej Pietrasiewicz  *
700a2430fSAndrzej Pietrasiewicz  *	This program is free software; you can redistribute it and/or modify
800a2430fSAndrzej Pietrasiewicz  *	it under the terms of the GNU General Public License as published by
900a2430fSAndrzej Pietrasiewicz  *	the Free Software Foundation; either version 2 of the License, or
1000a2430fSAndrzej Pietrasiewicz  *	(at your option) any later version.
1100a2430fSAndrzej Pietrasiewicz  */
1200a2430fSAndrzej Pietrasiewicz 
1300a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h>
146d11ed76SAndrzej Pietrasiewicz #include <linux/module.h>
1500a2430fSAndrzej Pietrasiewicz #include <linux/device.h>
1600a2430fSAndrzej Pietrasiewicz #include <linux/errno.h>
1700a2430fSAndrzej Pietrasiewicz #include <linux/fs.h>
1800a2430fSAndrzej Pietrasiewicz #include <linux/list.h>
1900a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h>
2000a2430fSAndrzej Pietrasiewicz #include <linux/string.h>
2100a2430fSAndrzej Pietrasiewicz #include <linux/usb/ch9.h>
2200a2430fSAndrzej Pietrasiewicz #include <linux/usb/gadget.h>
2300a2430fSAndrzej Pietrasiewicz #include <linux/usb/video.h>
2400a2430fSAndrzej Pietrasiewicz #include <linux/vmalloc.h>
2500a2430fSAndrzej Pietrasiewicz #include <linux/wait.h>
2600a2430fSAndrzej Pietrasiewicz 
2700a2430fSAndrzej Pietrasiewicz #include <media/v4l2-dev.h>
2800a2430fSAndrzej Pietrasiewicz #include <media/v4l2-event.h>
2900a2430fSAndrzej Pietrasiewicz 
3046919a23SAndrzej Pietrasiewicz #include "u_uvc.h"
3100a2430fSAndrzej Pietrasiewicz #include "uvc.h"
3246919a23SAndrzej Pietrasiewicz #include "uvc_configfs.h"
333a83c16eSAndrzej Pietrasiewicz #include "uvc_v4l2.h"
343a83c16eSAndrzej Pietrasiewicz #include "uvc_video.h"
3500a2430fSAndrzej Pietrasiewicz 
3600a2430fSAndrzej Pietrasiewicz unsigned int uvc_gadget_trace_param;
3700a2430fSAndrzej Pietrasiewicz 
3800a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
3900a2430fSAndrzej Pietrasiewicz  * Function descriptors
4000a2430fSAndrzej Pietrasiewicz  */
4100a2430fSAndrzej Pietrasiewicz 
4200a2430fSAndrzej Pietrasiewicz /* string IDs are assigned dynamically */
4300a2430fSAndrzej Pietrasiewicz 
4400a2430fSAndrzej Pietrasiewicz #define UVC_STRING_CONTROL_IDX			0
4500a2430fSAndrzej Pietrasiewicz #define UVC_STRING_STREAMING_IDX		1
4600a2430fSAndrzej Pietrasiewicz 
4700a2430fSAndrzej Pietrasiewicz static struct usb_string uvc_en_us_strings[] = {
4800a2430fSAndrzej Pietrasiewicz 	[UVC_STRING_CONTROL_IDX].s = "UVC Camera",
4900a2430fSAndrzej Pietrasiewicz 	[UVC_STRING_STREAMING_IDX].s = "Video Streaming",
5000a2430fSAndrzej Pietrasiewicz 	{  }
5100a2430fSAndrzej Pietrasiewicz };
5200a2430fSAndrzej Pietrasiewicz 
5300a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings uvc_stringtab = {
5400a2430fSAndrzej Pietrasiewicz 	.language = 0x0409,	/* en-us */
5500a2430fSAndrzej Pietrasiewicz 	.strings = uvc_en_us_strings,
5600a2430fSAndrzej Pietrasiewicz };
5700a2430fSAndrzej Pietrasiewicz 
5800a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings *uvc_function_strings[] = {
5900a2430fSAndrzej Pietrasiewicz 	&uvc_stringtab,
6000a2430fSAndrzej Pietrasiewicz 	NULL,
6100a2430fSAndrzej Pietrasiewicz };
6200a2430fSAndrzej Pietrasiewicz 
6300a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_CONTROL			0
6400a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_STREAMING		1
6500a2430fSAndrzej Pietrasiewicz 
6600a2430fSAndrzej Pietrasiewicz #define UVC_STATUS_MAX_PACKET_SIZE		16	/* 16 bytes status */
6700a2430fSAndrzej Pietrasiewicz 
686d11ed76SAndrzej Pietrasiewicz static struct usb_interface_assoc_descriptor uvc_iad = {
6900a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_iad),
7000a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
7100a2430fSAndrzej Pietrasiewicz 	.bFirstInterface	= 0,
7200a2430fSAndrzej Pietrasiewicz 	.bInterfaceCount	= 2,
7300a2430fSAndrzej Pietrasiewicz 	.bFunctionClass		= USB_CLASS_VIDEO,
7400a2430fSAndrzej Pietrasiewicz 	.bFunctionSubClass	= UVC_SC_VIDEO_INTERFACE_COLLECTION,
7500a2430fSAndrzej Pietrasiewicz 	.bFunctionProtocol	= 0x00,
7600a2430fSAndrzej Pietrasiewicz 	.iFunction		= 0,
7700a2430fSAndrzej Pietrasiewicz };
7800a2430fSAndrzej Pietrasiewicz 
796d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_control_intf = {
8000a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
8100a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
8200a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL,
8300a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
8400a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 1,
8500a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
8600a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOCONTROL,
8700a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
8800a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
8900a2430fSAndrzej Pietrasiewicz };
9000a2430fSAndrzej Pietrasiewicz 
916d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_control_ep = {
9200a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
9300a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
9400a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
9500a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
9600a2430fSAndrzej Pietrasiewicz 	.wMaxPacketSize		= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
9700a2430fSAndrzej Pietrasiewicz 	.bInterval		= 8,
9800a2430fSAndrzej Pietrasiewicz };
9900a2430fSAndrzej Pietrasiewicz 
1006d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
10100a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_ss_control_comp),
10200a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
10300a2430fSAndrzej Pietrasiewicz 	/* The following 3 values can be tweaked if necessary. */
10400a2430fSAndrzej Pietrasiewicz 	.bMaxBurst		= 0,
10500a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= 0,
10600a2430fSAndrzej Pietrasiewicz 	.wBytesPerInterval	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
10700a2430fSAndrzej Pietrasiewicz };
10800a2430fSAndrzej Pietrasiewicz 
1096d11ed76SAndrzej Pietrasiewicz static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
11000a2430fSAndrzej Pietrasiewicz 	.bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE,
11100a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_CS_ENDPOINT,
11200a2430fSAndrzej Pietrasiewicz 	.bDescriptorSubType	= UVC_EP_INTERRUPT,
11300a2430fSAndrzej Pietrasiewicz 	.wMaxTransferSize	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
11400a2430fSAndrzej Pietrasiewicz };
11500a2430fSAndrzej Pietrasiewicz 
1166d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
11700a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
11800a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
11900a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
12000a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
12100a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 0,
12200a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
12300a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
12400a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
12500a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
12600a2430fSAndrzej Pietrasiewicz };
12700a2430fSAndrzej Pietrasiewicz 
1286d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
12900a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
13000a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
13100a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
13200a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 1,
13300a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 1,
13400a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
13500a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
13600a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
13700a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
13800a2430fSAndrzej Pietrasiewicz };
13900a2430fSAndrzej Pietrasiewicz 
1406d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
14100a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
14200a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
14300a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
14400a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
14500a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
14600a2430fSAndrzej Pietrasiewicz 	/* The wMaxPacketSize and bInterval values will be initialized from
14700a2430fSAndrzej Pietrasiewicz 	 * module parameters.
14800a2430fSAndrzej Pietrasiewicz 	 */
14900a2430fSAndrzej Pietrasiewicz };
15000a2430fSAndrzej Pietrasiewicz 
1516d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
15200a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
15300a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
15400a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
15500a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
15600a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
15700a2430fSAndrzej Pietrasiewicz 	/* The wMaxPacketSize and bInterval values will be initialized from
15800a2430fSAndrzej Pietrasiewicz 	 * module parameters.
15900a2430fSAndrzej Pietrasiewicz 	 */
16000a2430fSAndrzej Pietrasiewicz };
16100a2430fSAndrzej Pietrasiewicz 
1626d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
16300a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
16400a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
16500a2430fSAndrzej Pietrasiewicz 
16600a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
16700a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
16800a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
16900a2430fSAndrzej Pietrasiewicz 	/* The wMaxPacketSize and bInterval values will be initialized from
17000a2430fSAndrzej Pietrasiewicz 	 * module parameters.
17100a2430fSAndrzej Pietrasiewicz 	 */
17200a2430fSAndrzej Pietrasiewicz };
17300a2430fSAndrzej Pietrasiewicz 
1746d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
17500a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_ss_streaming_comp),
17600a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
17700a2430fSAndrzej Pietrasiewicz 	/* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
17800a2430fSAndrzej Pietrasiewicz 	 * initialized from module parameters.
17900a2430fSAndrzej Pietrasiewicz 	 */
18000a2430fSAndrzej Pietrasiewicz };
18100a2430fSAndrzej Pietrasiewicz 
18200a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
18300a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
18400a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_fs_streaming_ep,
18500a2430fSAndrzej Pietrasiewicz 	NULL,
18600a2430fSAndrzej Pietrasiewicz };
18700a2430fSAndrzej Pietrasiewicz 
18800a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
18900a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
19000a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_hs_streaming_ep,
19100a2430fSAndrzej Pietrasiewicz 	NULL,
19200a2430fSAndrzej Pietrasiewicz };
19300a2430fSAndrzej Pietrasiewicz 
19400a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
19500a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
19600a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_ss_streaming_ep,
19700a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_ss_streaming_comp,
19800a2430fSAndrzej Pietrasiewicz 	NULL,
19900a2430fSAndrzej Pietrasiewicz };
20000a2430fSAndrzej Pietrasiewicz 
2016d11ed76SAndrzej Pietrasiewicz void uvc_set_trace_param(unsigned int trace)
2026d11ed76SAndrzej Pietrasiewicz {
2036d11ed76SAndrzej Pietrasiewicz 	uvc_gadget_trace_param = trace;
2046d11ed76SAndrzej Pietrasiewicz }
2056d11ed76SAndrzej Pietrasiewicz EXPORT_SYMBOL(uvc_set_trace_param);
2066d11ed76SAndrzej Pietrasiewicz 
20700a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
20800a2430fSAndrzej Pietrasiewicz  * Control requests
20900a2430fSAndrzej Pietrasiewicz  */
21000a2430fSAndrzej Pietrasiewicz 
21100a2430fSAndrzej Pietrasiewicz static void
21200a2430fSAndrzej Pietrasiewicz uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
21300a2430fSAndrzej Pietrasiewicz {
21400a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = req->context;
21500a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
21600a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
21700a2430fSAndrzej Pietrasiewicz 
21800a2430fSAndrzej Pietrasiewicz 	if (uvc->event_setup_out) {
21900a2430fSAndrzej Pietrasiewicz 		uvc->event_setup_out = 0;
22000a2430fSAndrzej Pietrasiewicz 
22100a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
22200a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_DATA;
22300a2430fSAndrzej Pietrasiewicz 		uvc_event->data.length = req->actual;
22400a2430fSAndrzej Pietrasiewicz 		memcpy(&uvc_event->data.data, req->buf, req->actual);
225dbe98b30SHans Verkuil 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
22600a2430fSAndrzej Pietrasiewicz 	}
22700a2430fSAndrzej Pietrasiewicz }
22800a2430fSAndrzej Pietrasiewicz 
22900a2430fSAndrzej Pietrasiewicz static int
23000a2430fSAndrzej Pietrasiewicz uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
23100a2430fSAndrzej Pietrasiewicz {
23200a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
23300a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
23400a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
23500a2430fSAndrzej Pietrasiewicz 
23600a2430fSAndrzej Pietrasiewicz 	/* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
23700a2430fSAndrzej Pietrasiewicz 	 *	ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
23800a2430fSAndrzej Pietrasiewicz 	 *	le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
23900a2430fSAndrzej Pietrasiewicz 	 */
24000a2430fSAndrzej Pietrasiewicz 
24100a2430fSAndrzej Pietrasiewicz 	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
24200a2430fSAndrzej Pietrasiewicz 		INFO(f->config->cdev, "invalid request type\n");
24300a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
24400a2430fSAndrzej Pietrasiewicz 	}
24500a2430fSAndrzej Pietrasiewicz 
24600a2430fSAndrzej Pietrasiewicz 	/* Stall too big requests. */
24700a2430fSAndrzej Pietrasiewicz 	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
24800a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
24900a2430fSAndrzej Pietrasiewicz 
25026a029f2SLaurent Pinchart 	/* Tell the complete callback to generate an event for the next request
25126a029f2SLaurent Pinchart 	 * that will be enqueued by UVCIOC_SEND_RESPONSE.
25226a029f2SLaurent Pinchart 	 */
25326a029f2SLaurent Pinchart 	uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
25426a029f2SLaurent Pinchart 	uvc->event_length = le16_to_cpu(ctrl->wLength);
25526a029f2SLaurent Pinchart 
25600a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
25700a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_SETUP;
25800a2430fSAndrzej Pietrasiewicz 	memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
259dbe98b30SHans Verkuil 	v4l2_event_queue(&uvc->vdev, &v4l2_event);
26000a2430fSAndrzej Pietrasiewicz 
26100a2430fSAndrzej Pietrasiewicz 	return 0;
26200a2430fSAndrzej Pietrasiewicz }
26300a2430fSAndrzej Pietrasiewicz 
26400a2430fSAndrzej Pietrasiewicz void uvc_function_setup_continue(struct uvc_device *uvc)
26500a2430fSAndrzej Pietrasiewicz {
26600a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
26700a2430fSAndrzej Pietrasiewicz 
26800a2430fSAndrzej Pietrasiewicz 	usb_composite_setup_continue(cdev);
26900a2430fSAndrzej Pietrasiewicz }
27000a2430fSAndrzej Pietrasiewicz 
27100a2430fSAndrzej Pietrasiewicz static int
27200a2430fSAndrzej Pietrasiewicz uvc_function_get_alt(struct usb_function *f, unsigned interface)
27300a2430fSAndrzej Pietrasiewicz {
27400a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
27500a2430fSAndrzej Pietrasiewicz 
27600a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
27700a2430fSAndrzej Pietrasiewicz 
27800a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf)
27900a2430fSAndrzej Pietrasiewicz 		return 0;
28000a2430fSAndrzej Pietrasiewicz 	else if (interface != uvc->streaming_intf)
28100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
28200a2430fSAndrzej Pietrasiewicz 	else
283d62bf8c1SRobert Baldyga 		return uvc->video.ep->enabled ? 1 : 0;
28400a2430fSAndrzej Pietrasiewicz }
28500a2430fSAndrzej Pietrasiewicz 
28600a2430fSAndrzej Pietrasiewicz static int
28700a2430fSAndrzej Pietrasiewicz uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
28800a2430fSAndrzej Pietrasiewicz {
28900a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
290c92bae75SFelipe Balbi 	struct usb_composite_dev *cdev = f->config->cdev;
29100a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
29200a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
29300a2430fSAndrzej Pietrasiewicz 	int ret;
29400a2430fSAndrzej Pietrasiewicz 
295c92bae75SFelipe Balbi 	INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
29600a2430fSAndrzej Pietrasiewicz 
29700a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf) {
29800a2430fSAndrzej Pietrasiewicz 		if (alt)
29900a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
30000a2430fSAndrzej Pietrasiewicz 
30162e37078SFelipe Balbi 		INFO(cdev, "reset UVC Control\n");
30262e37078SFelipe Balbi 		usb_ep_disable(uvc->control_ep);
30362e37078SFelipe Balbi 
30462e37078SFelipe Balbi 		if (!uvc->control_ep->desc)
30562e37078SFelipe Balbi 			if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
30662e37078SFelipe Balbi 				return -EINVAL;
30762e37078SFelipe Balbi 
30862e37078SFelipe Balbi 		usb_ep_enable(uvc->control_ep);
30962e37078SFelipe Balbi 
31000a2430fSAndrzej Pietrasiewicz 		if (uvc->state == UVC_STATE_DISCONNECTED) {
31100a2430fSAndrzej Pietrasiewicz 			memset(&v4l2_event, 0, sizeof(v4l2_event));
31200a2430fSAndrzej Pietrasiewicz 			v4l2_event.type = UVC_EVENT_CONNECT;
313c92bae75SFelipe Balbi 			uvc_event->speed = cdev->gadget->speed;
314dbe98b30SHans Verkuil 			v4l2_event_queue(&uvc->vdev, &v4l2_event);
31500a2430fSAndrzej Pietrasiewicz 
31600a2430fSAndrzej Pietrasiewicz 			uvc->state = UVC_STATE_CONNECTED;
31700a2430fSAndrzej Pietrasiewicz 		}
31800a2430fSAndrzej Pietrasiewicz 
31900a2430fSAndrzej Pietrasiewicz 		return 0;
32000a2430fSAndrzej Pietrasiewicz 	}
32100a2430fSAndrzej Pietrasiewicz 
32200a2430fSAndrzej Pietrasiewicz 	if (interface != uvc->streaming_intf)
32300a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
32400a2430fSAndrzej Pietrasiewicz 
32500a2430fSAndrzej Pietrasiewicz 	/* TODO
32600a2430fSAndrzej Pietrasiewicz 	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
32700a2430fSAndrzej Pietrasiewicz 		return alt ? -EINVAL : 0;
32800a2430fSAndrzej Pietrasiewicz 	*/
32900a2430fSAndrzej Pietrasiewicz 
33000a2430fSAndrzej Pietrasiewicz 	switch (alt) {
33100a2430fSAndrzej Pietrasiewicz 	case 0:
33200a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_STREAMING)
33300a2430fSAndrzej Pietrasiewicz 			return 0;
33400a2430fSAndrzej Pietrasiewicz 
335d62bf8c1SRobert Baldyga 		if (uvc->video.ep)
33600a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(uvc->video.ep);
33700a2430fSAndrzej Pietrasiewicz 
33800a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
33900a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMOFF;
340dbe98b30SHans Verkuil 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
34100a2430fSAndrzej Pietrasiewicz 
34200a2430fSAndrzej Pietrasiewicz 		uvc->state = UVC_STATE_CONNECTED;
34300a2430fSAndrzej Pietrasiewicz 		return 0;
34400a2430fSAndrzej Pietrasiewicz 
34500a2430fSAndrzej Pietrasiewicz 	case 1:
34600a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_CONNECTED)
34700a2430fSAndrzej Pietrasiewicz 			return 0;
34800a2430fSAndrzej Pietrasiewicz 
349c92bae75SFelipe Balbi 		if (!uvc->video.ep)
350c92bae75SFelipe Balbi 			return -EINVAL;
351c92bae75SFelipe Balbi 
352c92bae75SFelipe Balbi 		INFO(cdev, "reset UVC\n");
353c92bae75SFelipe Balbi 		usb_ep_disable(uvc->video.ep);
354c92bae75SFelipe Balbi 
35500a2430fSAndrzej Pietrasiewicz 		ret = config_ep_by_speed(f->config->cdev->gadget,
35600a2430fSAndrzej Pietrasiewicz 				&(uvc->func), uvc->video.ep);
35700a2430fSAndrzej Pietrasiewicz 		if (ret)
35800a2430fSAndrzej Pietrasiewicz 			return ret;
35900a2430fSAndrzej Pietrasiewicz 		usb_ep_enable(uvc->video.ep);
36000a2430fSAndrzej Pietrasiewicz 
36100a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
36200a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMON;
363dbe98b30SHans Verkuil 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
36400a2430fSAndrzej Pietrasiewicz 		return USB_GADGET_DELAYED_STATUS;
36500a2430fSAndrzej Pietrasiewicz 
36600a2430fSAndrzej Pietrasiewicz 	default:
36700a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
36800a2430fSAndrzej Pietrasiewicz 	}
36900a2430fSAndrzej Pietrasiewicz }
37000a2430fSAndrzej Pietrasiewicz 
37100a2430fSAndrzej Pietrasiewicz static void
37200a2430fSAndrzej Pietrasiewicz uvc_function_disable(struct usb_function *f)
37300a2430fSAndrzej Pietrasiewicz {
37400a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
37500a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
37600a2430fSAndrzej Pietrasiewicz 
37700a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_disable\n");
37800a2430fSAndrzej Pietrasiewicz 
37900a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
38000a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_DISCONNECT;
381dbe98b30SHans Verkuil 	v4l2_event_queue(&uvc->vdev, &v4l2_event);
38200a2430fSAndrzej Pietrasiewicz 
38300a2430fSAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
384e3122f5fSFelipe Balbi 
385e3122f5fSFelipe Balbi 	usb_ep_disable(uvc->video.ep);
386e3122f5fSFelipe Balbi 	usb_ep_disable(uvc->control_ep);
38700a2430fSAndrzej Pietrasiewicz }
38800a2430fSAndrzej Pietrasiewicz 
38900a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
39000a2430fSAndrzej Pietrasiewicz  * Connection / disconnection
39100a2430fSAndrzej Pietrasiewicz  */
39200a2430fSAndrzej Pietrasiewicz 
39300a2430fSAndrzej Pietrasiewicz void
39400a2430fSAndrzej Pietrasiewicz uvc_function_connect(struct uvc_device *uvc)
39500a2430fSAndrzej Pietrasiewicz {
39600a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
39700a2430fSAndrzej Pietrasiewicz 	int ret;
39800a2430fSAndrzej Pietrasiewicz 
39900a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_activate(&uvc->func)) < 0)
40000a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "UVC connect failed with %d\n", ret);
40100a2430fSAndrzej Pietrasiewicz }
40200a2430fSAndrzej Pietrasiewicz 
40300a2430fSAndrzej Pietrasiewicz void
40400a2430fSAndrzej Pietrasiewicz uvc_function_disconnect(struct uvc_device *uvc)
40500a2430fSAndrzej Pietrasiewicz {
40600a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
40700a2430fSAndrzej Pietrasiewicz 	int ret;
40800a2430fSAndrzej Pietrasiewicz 
40900a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
41000a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "UVC disconnect failed with %d\n", ret);
41100a2430fSAndrzej Pietrasiewicz }
41200a2430fSAndrzej Pietrasiewicz 
41300a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
41400a2430fSAndrzej Pietrasiewicz  * USB probe and disconnect
41500a2430fSAndrzej Pietrasiewicz  */
41600a2430fSAndrzej Pietrasiewicz 
41700a2430fSAndrzej Pietrasiewicz static int
41800a2430fSAndrzej Pietrasiewicz uvc_register_video(struct uvc_device *uvc)
41900a2430fSAndrzej Pietrasiewicz {
42000a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
42100a2430fSAndrzej Pietrasiewicz 
42200a2430fSAndrzej Pietrasiewicz 	/* TODO reference counting. */
423dbe98b30SHans Verkuil 	uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
424dbe98b30SHans Verkuil 	uvc->vdev.fops = &uvc_v4l2_fops;
425dbe98b30SHans Verkuil 	uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
426dbe98b30SHans Verkuil 	uvc->vdev.release = video_device_release_empty;
427dbe98b30SHans Verkuil 	uvc->vdev.vfl_dir = VFL_DIR_TX;
428dbe98b30SHans Verkuil 	uvc->vdev.lock = &uvc->video.mutex;
429dbe98b30SHans Verkuil 	strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
43000a2430fSAndrzej Pietrasiewicz 
431dbe98b30SHans Verkuil 	video_set_drvdata(&uvc->vdev, uvc);
43200a2430fSAndrzej Pietrasiewicz 
433dbe98b30SHans Verkuil 	return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
43400a2430fSAndrzej Pietrasiewicz }
43500a2430fSAndrzej Pietrasiewicz 
43600a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
43700a2430fSAndrzej Pietrasiewicz 	do { \
43800a2430fSAndrzej Pietrasiewicz 		memcpy(mem, desc, (desc)->bLength); \
43900a2430fSAndrzej Pietrasiewicz 		*(dst)++ = mem; \
44000a2430fSAndrzej Pietrasiewicz 		mem += (desc)->bLength; \
44100a2430fSAndrzej Pietrasiewicz 	} while (0);
44200a2430fSAndrzej Pietrasiewicz 
44300a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
44400a2430fSAndrzej Pietrasiewicz 	do { \
44500a2430fSAndrzej Pietrasiewicz 		const struct usb_descriptor_header * const *__src; \
44600a2430fSAndrzej Pietrasiewicz 		for (__src = src; *__src; ++__src) { \
44700a2430fSAndrzej Pietrasiewicz 			memcpy(mem, *__src, (*__src)->bLength); \
44800a2430fSAndrzej Pietrasiewicz 			*dst++ = mem; \
44900a2430fSAndrzej Pietrasiewicz 			mem += (*__src)->bLength; \
45000a2430fSAndrzej Pietrasiewicz 		} \
45100a2430fSAndrzej Pietrasiewicz 	} while (0)
45200a2430fSAndrzej Pietrasiewicz 
4536d11ed76SAndrzej Pietrasiewicz static struct usb_descriptor_header **
45400a2430fSAndrzej Pietrasiewicz uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
45500a2430fSAndrzej Pietrasiewicz {
45600a2430fSAndrzej Pietrasiewicz 	struct uvc_input_header_descriptor *uvc_streaming_header;
45700a2430fSAndrzej Pietrasiewicz 	struct uvc_header_descriptor *uvc_control_header;
45800a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_control_desc;
45900a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_streaming_cls;
46000a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *uvc_streaming_std;
46100a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *src;
46200a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **dst;
46300a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **hdr;
46400a2430fSAndrzej Pietrasiewicz 	unsigned int control_size;
46500a2430fSAndrzej Pietrasiewicz 	unsigned int streaming_size;
46600a2430fSAndrzej Pietrasiewicz 	unsigned int n_desc;
46700a2430fSAndrzej Pietrasiewicz 	unsigned int bytes;
46800a2430fSAndrzej Pietrasiewicz 	void *mem;
46900a2430fSAndrzej Pietrasiewicz 
47000a2430fSAndrzej Pietrasiewicz 	switch (speed) {
47100a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_SUPER:
47200a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.ss_control;
47300a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.ss_streaming;
47400a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_ss_streaming;
47500a2430fSAndrzej Pietrasiewicz 		break;
47600a2430fSAndrzej Pietrasiewicz 
47700a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_HIGH:
47800a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
47900a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.hs_streaming;
48000a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_hs_streaming;
48100a2430fSAndrzej Pietrasiewicz 		break;
48200a2430fSAndrzej Pietrasiewicz 
48300a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_FULL:
48400a2430fSAndrzej Pietrasiewicz 	default:
48500a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
48600a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.fs_streaming;
48700a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_fs_streaming;
48800a2430fSAndrzej Pietrasiewicz 		break;
48900a2430fSAndrzej Pietrasiewicz 	}
49000a2430fSAndrzej Pietrasiewicz 
4916c25955eSAndrzej Pietrasiewicz 	if (!uvc_control_desc || !uvc_streaming_cls)
4926c25955eSAndrzej Pietrasiewicz 		return ERR_PTR(-ENODEV);
4936c25955eSAndrzej Pietrasiewicz 
49400a2430fSAndrzej Pietrasiewicz 	/* Descriptors layout
49500a2430fSAndrzej Pietrasiewicz 	 *
49600a2430fSAndrzej Pietrasiewicz 	 * uvc_iad
49700a2430fSAndrzej Pietrasiewicz 	 * uvc_control_intf
49800a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC control descriptors
49900a2430fSAndrzej Pietrasiewicz 	 * uvc_control_ep
50000a2430fSAndrzej Pietrasiewicz 	 * uvc_control_cs_ep
50100a2430fSAndrzej Pietrasiewicz 	 * uvc_ss_control_comp (for SS only)
50200a2430fSAndrzej Pietrasiewicz 	 * uvc_streaming_intf_alt0
50300a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC streaming descriptors
50400a2430fSAndrzej Pietrasiewicz 	 * uvc_{fs|hs}_streaming
50500a2430fSAndrzej Pietrasiewicz 	 */
50600a2430fSAndrzej Pietrasiewicz 
50700a2430fSAndrzej Pietrasiewicz 	/* Count descriptors and compute their size. */
50800a2430fSAndrzej Pietrasiewicz 	control_size = 0;
50900a2430fSAndrzej Pietrasiewicz 	streaming_size = 0;
51000a2430fSAndrzej Pietrasiewicz 	bytes = uvc_iad.bLength + uvc_control_intf.bLength
51100a2430fSAndrzej Pietrasiewicz 	      + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
51200a2430fSAndrzej Pietrasiewicz 	      + uvc_streaming_intf_alt0.bLength;
51300a2430fSAndrzej Pietrasiewicz 
51400a2430fSAndrzej Pietrasiewicz 	if (speed == USB_SPEED_SUPER) {
51500a2430fSAndrzej Pietrasiewicz 		bytes += uvc_ss_control_comp.bLength;
51600a2430fSAndrzej Pietrasiewicz 		n_desc = 6;
51700a2430fSAndrzej Pietrasiewicz 	} else {
51800a2430fSAndrzej Pietrasiewicz 		n_desc = 5;
51900a2430fSAndrzej Pietrasiewicz 	}
52000a2430fSAndrzej Pietrasiewicz 
52100a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_control_desc;
52200a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
52300a2430fSAndrzej Pietrasiewicz 		control_size += (*src)->bLength;
52400a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
52500a2430fSAndrzej Pietrasiewicz 		n_desc++;
52600a2430fSAndrzej Pietrasiewicz 	}
52700a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
52800a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
52900a2430fSAndrzej Pietrasiewicz 		streaming_size += (*src)->bLength;
53000a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
53100a2430fSAndrzej Pietrasiewicz 		n_desc++;
53200a2430fSAndrzej Pietrasiewicz 	}
53300a2430fSAndrzej Pietrasiewicz 	for (src = uvc_streaming_std; *src; ++src) {
53400a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
53500a2430fSAndrzej Pietrasiewicz 		n_desc++;
53600a2430fSAndrzej Pietrasiewicz 	}
53700a2430fSAndrzej Pietrasiewicz 
53800a2430fSAndrzej Pietrasiewicz 	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
53900a2430fSAndrzej Pietrasiewicz 	if (mem == NULL)
54000a2430fSAndrzej Pietrasiewicz 		return NULL;
54100a2430fSAndrzej Pietrasiewicz 
54200a2430fSAndrzej Pietrasiewicz 	hdr = mem;
54300a2430fSAndrzej Pietrasiewicz 	dst = mem;
54400a2430fSAndrzej Pietrasiewicz 	mem += (n_desc + 1) * sizeof(*src);
54500a2430fSAndrzej Pietrasiewicz 
54600a2430fSAndrzej Pietrasiewicz 	/* Copy the descriptors. */
54700a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
54800a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
54900a2430fSAndrzej Pietrasiewicz 
55000a2430fSAndrzej Pietrasiewicz 	uvc_control_header = mem;
55100a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
55200a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header **)uvc_control_desc);
55300a2430fSAndrzej Pietrasiewicz 	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
55400a2430fSAndrzej Pietrasiewicz 	uvc_control_header->bInCollection = 1;
55500a2430fSAndrzej Pietrasiewicz 	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
55600a2430fSAndrzej Pietrasiewicz 
55700a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
55800a2430fSAndrzej Pietrasiewicz 	if (speed == USB_SPEED_SUPER)
55900a2430fSAndrzej Pietrasiewicz 		UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
56000a2430fSAndrzej Pietrasiewicz 
56100a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
56200a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
56300a2430fSAndrzej Pietrasiewicz 
56400a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header = mem;
56500a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
56600a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header**)uvc_streaming_cls);
56700a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
56800a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
56900a2430fSAndrzej Pietrasiewicz 
57000a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
57100a2430fSAndrzej Pietrasiewicz 
57200a2430fSAndrzej Pietrasiewicz 	*dst = NULL;
57300a2430fSAndrzej Pietrasiewicz 	return hdr;
57400a2430fSAndrzej Pietrasiewicz }
57500a2430fSAndrzej Pietrasiewicz 
5766d11ed76SAndrzej Pietrasiewicz static int
57700a2430fSAndrzej Pietrasiewicz uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
57800a2430fSAndrzej Pietrasiewicz {
57900a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
58000a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
58113443799SAndrzej Pietrasiewicz 	struct usb_string *us;
58200a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_mult;
58300a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_size;
58400a2430fSAndrzej Pietrasiewicz 	struct usb_ep *ep;
5856d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
58600a2430fSAndrzej Pietrasiewicz 	int ret = -EINVAL;
58700a2430fSAndrzej Pietrasiewicz 
58800a2430fSAndrzej Pietrasiewicz 	INFO(cdev, "uvc_function_bind\n");
58900a2430fSAndrzej Pietrasiewicz 
590bbea6de1SAndrzej Pietrasiewicz 	opts = fi_to_f_uvc_opts(f->fi);
5916d11ed76SAndrzej Pietrasiewicz 	/* Sanity check the streaming endpoint module parameters.
5926d11ed76SAndrzej Pietrasiewicz 	 */
5936d11ed76SAndrzej Pietrasiewicz 	opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
5946d11ed76SAndrzej Pietrasiewicz 	opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
5956d11ed76SAndrzej Pietrasiewicz 	opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
5966d11ed76SAndrzej Pietrasiewicz 
5976d11ed76SAndrzej Pietrasiewicz 	/* Fill in the FS/HS/SS Video Streaming specific descriptors from the
5986d11ed76SAndrzej Pietrasiewicz 	 * module parameters.
5996d11ed76SAndrzej Pietrasiewicz 	 *
6006d11ed76SAndrzej Pietrasiewicz 	 * NOTE: We assume that the user knows what they are doing and won't
6016d11ed76SAndrzej Pietrasiewicz 	 * give parameters that their UDC doesn't support.
6026d11ed76SAndrzej Pietrasiewicz 	 */
6036d11ed76SAndrzej Pietrasiewicz 	if (opts->streaming_maxpacket <= 1024) {
6046d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 1;
6056d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket;
6066d11ed76SAndrzej Pietrasiewicz 	} else if (opts->streaming_maxpacket <= 2048) {
6076d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 2;
6086d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket / 2;
6096d11ed76SAndrzej Pietrasiewicz 	} else {
6106d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 3;
6116d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket / 3;
6126d11ed76SAndrzej Pietrasiewicz 	}
6136d11ed76SAndrzej Pietrasiewicz 
6146d11ed76SAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.wMaxPacketSize =
615e102609fSLaurent Pinchart 		cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
6166d11ed76SAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
6176d11ed76SAndrzej Pietrasiewicz 
618e102609fSLaurent Pinchart 	uvc_hs_streaming_ep.wMaxPacketSize =
619e102609fSLaurent Pinchart 		cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
6206d11ed76SAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
6216d11ed76SAndrzej Pietrasiewicz 
622e102609fSLaurent Pinchart 	uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
6236d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
6246d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
6256d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
6266d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.wBytesPerInterval =
627e102609fSLaurent Pinchart 		cpu_to_le16(max_packet_size * max_packet_mult *
628*09424c50SRoger Quadros 			    (opts->streaming_maxburst + 1));
62900a2430fSAndrzej Pietrasiewicz 
63000a2430fSAndrzej Pietrasiewicz 	/* Allocate endpoints. */
63100a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
63200a2430fSAndrzej Pietrasiewicz 	if (!ep) {
63300a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "Unable to allocate control EP\n");
63400a2430fSAndrzej Pietrasiewicz 		goto error;
63500a2430fSAndrzej Pietrasiewicz 	}
63600a2430fSAndrzej Pietrasiewicz 	uvc->control_ep = ep;
63700a2430fSAndrzej Pietrasiewicz 
63800a2430fSAndrzej Pietrasiewicz 	if (gadget_is_superspeed(c->cdev->gadget))
63900a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
64000a2430fSAndrzej Pietrasiewicz 					  &uvc_ss_streaming_comp);
64100a2430fSAndrzej Pietrasiewicz 	else if (gadget_is_dualspeed(cdev->gadget))
64200a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
64300a2430fSAndrzej Pietrasiewicz 	else
64400a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
64500a2430fSAndrzej Pietrasiewicz 
64600a2430fSAndrzej Pietrasiewicz 	if (!ep) {
64700a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "Unable to allocate streaming EP\n");
64800a2430fSAndrzej Pietrasiewicz 		goto error;
64900a2430fSAndrzej Pietrasiewicz 	}
65000a2430fSAndrzej Pietrasiewicz 	uvc->video.ep = ep;
65100a2430fSAndrzej Pietrasiewicz 
65200a2430fSAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
65300a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
65400a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
65500a2430fSAndrzej Pietrasiewicz 
65613443799SAndrzej Pietrasiewicz 	us = usb_gstrings_attach(cdev, uvc_function_strings,
65713443799SAndrzej Pietrasiewicz 				 ARRAY_SIZE(uvc_en_us_strings));
65813443799SAndrzej Pietrasiewicz 	if (IS_ERR(us)) {
65913443799SAndrzej Pietrasiewicz 		ret = PTR_ERR(us);
6606d11ed76SAndrzej Pietrasiewicz 		goto error;
66113443799SAndrzej Pietrasiewicz 	}
66213443799SAndrzej Pietrasiewicz 	uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
66313443799SAndrzej Pietrasiewicz 	uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
66413443799SAndrzej Pietrasiewicz 	ret = us[UVC_STRING_STREAMING_IDX].id;
6656d11ed76SAndrzej Pietrasiewicz 	uvc_streaming_intf_alt0.iInterface = ret;
6666d11ed76SAndrzej Pietrasiewicz 	uvc_streaming_intf_alt1.iInterface = ret;
6676d11ed76SAndrzej Pietrasiewicz 
66800a2430fSAndrzej Pietrasiewicz 	/* Allocate interface IDs. */
66900a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
67000a2430fSAndrzej Pietrasiewicz 		goto error;
67100a2430fSAndrzej Pietrasiewicz 	uvc_iad.bFirstInterface = ret;
67200a2430fSAndrzej Pietrasiewicz 	uvc_control_intf.bInterfaceNumber = ret;
67300a2430fSAndrzej Pietrasiewicz 	uvc->control_intf = ret;
67400a2430fSAndrzej Pietrasiewicz 
67500a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
67600a2430fSAndrzej Pietrasiewicz 		goto error;
67700a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
67800a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
67900a2430fSAndrzej Pietrasiewicz 	uvc->streaming_intf = ret;
68000a2430fSAndrzej Pietrasiewicz 
68100a2430fSAndrzej Pietrasiewicz 	/* Copy descriptors */
68200a2430fSAndrzej Pietrasiewicz 	f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
6836c25955eSAndrzej Pietrasiewicz 	if (IS_ERR(f->fs_descriptors)) {
6846c25955eSAndrzej Pietrasiewicz 		ret = PTR_ERR(f->fs_descriptors);
6856c25955eSAndrzej Pietrasiewicz 		f->fs_descriptors = NULL;
6866c25955eSAndrzej Pietrasiewicz 		goto error;
6876c25955eSAndrzej Pietrasiewicz 	}
6886c25955eSAndrzej Pietrasiewicz 	if (gadget_is_dualspeed(cdev->gadget)) {
68900a2430fSAndrzej Pietrasiewicz 		f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
6906c25955eSAndrzej Pietrasiewicz 		if (IS_ERR(f->hs_descriptors)) {
6916c25955eSAndrzej Pietrasiewicz 			ret = PTR_ERR(f->hs_descriptors);
6926c25955eSAndrzej Pietrasiewicz 			f->hs_descriptors = NULL;
6936c25955eSAndrzej Pietrasiewicz 			goto error;
6946c25955eSAndrzej Pietrasiewicz 		}
6956c25955eSAndrzej Pietrasiewicz 	}
6966c25955eSAndrzej Pietrasiewicz 	if (gadget_is_superspeed(c->cdev->gadget)) {
69700a2430fSAndrzej Pietrasiewicz 		f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
6986c25955eSAndrzej Pietrasiewicz 		if (IS_ERR(f->ss_descriptors)) {
6996c25955eSAndrzej Pietrasiewicz 			ret = PTR_ERR(f->ss_descriptors);
7006c25955eSAndrzej Pietrasiewicz 			f->ss_descriptors = NULL;
7016c25955eSAndrzej Pietrasiewicz 			goto error;
7026c25955eSAndrzej Pietrasiewicz 		}
7036c25955eSAndrzej Pietrasiewicz 	}
70400a2430fSAndrzej Pietrasiewicz 
70500a2430fSAndrzej Pietrasiewicz 	/* Preallocate control endpoint request. */
70600a2430fSAndrzej Pietrasiewicz 	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
70700a2430fSAndrzej Pietrasiewicz 	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
70800a2430fSAndrzej Pietrasiewicz 	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
70900a2430fSAndrzej Pietrasiewicz 		ret = -ENOMEM;
71000a2430fSAndrzej Pietrasiewicz 		goto error;
71100a2430fSAndrzej Pietrasiewicz 	}
71200a2430fSAndrzej Pietrasiewicz 
71300a2430fSAndrzej Pietrasiewicz 	uvc->control_req->buf = uvc->control_buf;
71400a2430fSAndrzej Pietrasiewicz 	uvc->control_req->complete = uvc_function_ep0_complete;
71500a2430fSAndrzej Pietrasiewicz 	uvc->control_req->context = uvc;
71600a2430fSAndrzej Pietrasiewicz 
71700a2430fSAndrzej Pietrasiewicz 	if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
71800a2430fSAndrzej Pietrasiewicz 		printk(KERN_INFO "v4l2_device_register failed\n");
71900a2430fSAndrzej Pietrasiewicz 		goto error;
72000a2430fSAndrzej Pietrasiewicz 	}
72100a2430fSAndrzej Pietrasiewicz 
72200a2430fSAndrzej Pietrasiewicz 	/* Initialise video. */
7237ea95b11SAndrzej Pietrasiewicz 	ret = uvcg_video_init(&uvc->video);
72400a2430fSAndrzej Pietrasiewicz 	if (ret < 0)
72500a2430fSAndrzej Pietrasiewicz 		goto error;
72600a2430fSAndrzej Pietrasiewicz 
72700a2430fSAndrzej Pietrasiewicz 	/* Register a V4L2 device. */
72800a2430fSAndrzej Pietrasiewicz 	ret = uvc_register_video(uvc);
72900a2430fSAndrzej Pietrasiewicz 	if (ret < 0) {
73000a2430fSAndrzej Pietrasiewicz 		printk(KERN_INFO "Unable to register video device\n");
73100a2430fSAndrzej Pietrasiewicz 		goto error;
73200a2430fSAndrzej Pietrasiewicz 	}
73300a2430fSAndrzej Pietrasiewicz 
73400a2430fSAndrzej Pietrasiewicz 	return 0;
73500a2430fSAndrzej Pietrasiewicz 
73600a2430fSAndrzej Pietrasiewicz error:
73700a2430fSAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
73800a2430fSAndrzej Pietrasiewicz 
739e7379857SAndrzej Pietrasiewicz 	if (uvc->control_req)
74000a2430fSAndrzej Pietrasiewicz 		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
74100a2430fSAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
74200a2430fSAndrzej Pietrasiewicz 
74300a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
74400a2430fSAndrzej Pietrasiewicz 	return ret;
74500a2430fSAndrzej Pietrasiewicz }
74600a2430fSAndrzej Pietrasiewicz 
74700a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
74800a2430fSAndrzej Pietrasiewicz  * USB gadget function
74900a2430fSAndrzej Pietrasiewicz  */
75000a2430fSAndrzej Pietrasiewicz 
7516d11ed76SAndrzej Pietrasiewicz static void uvc_free_inst(struct usb_function_instance *f)
7526d11ed76SAndrzej Pietrasiewicz {
753bbea6de1SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
7546d11ed76SAndrzej Pietrasiewicz 
75546919a23SAndrzej Pietrasiewicz 	mutex_destroy(&opts->lock);
7566d11ed76SAndrzej Pietrasiewicz 	kfree(opts);
7576d11ed76SAndrzej Pietrasiewicz }
7586d11ed76SAndrzej Pietrasiewicz 
7596d11ed76SAndrzej Pietrasiewicz static struct usb_function_instance *uvc_alloc_inst(void)
7606d11ed76SAndrzej Pietrasiewicz {
7616d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
76246919a23SAndrzej Pietrasiewicz 	struct uvc_camera_terminal_descriptor *cd;
76346919a23SAndrzej Pietrasiewicz 	struct uvc_processing_unit_descriptor *pd;
76446919a23SAndrzej Pietrasiewicz 	struct uvc_output_terminal_descriptor *od;
76546919a23SAndrzej Pietrasiewicz 	struct uvc_color_matching_descriptor *md;
76646919a23SAndrzej Pietrasiewicz 	struct uvc_descriptor_header **ctl_cls;
7676d11ed76SAndrzej Pietrasiewicz 
7686d11ed76SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
7696d11ed76SAndrzej Pietrasiewicz 	if (!opts)
7706d11ed76SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
7716d11ed76SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = uvc_free_inst;
77246919a23SAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
7736d11ed76SAndrzej Pietrasiewicz 
77446919a23SAndrzej Pietrasiewicz 	cd = &opts->uvc_camera_terminal;
77546919a23SAndrzej Pietrasiewicz 	cd->bLength			= UVC_DT_CAMERA_TERMINAL_SIZE(3);
77646919a23SAndrzej Pietrasiewicz 	cd->bDescriptorType		= USB_DT_CS_INTERFACE;
77746919a23SAndrzej Pietrasiewicz 	cd->bDescriptorSubType		= UVC_VC_INPUT_TERMINAL;
77846919a23SAndrzej Pietrasiewicz 	cd->bTerminalID			= 1;
77946919a23SAndrzej Pietrasiewicz 	cd->wTerminalType		= cpu_to_le16(0x0201);
78046919a23SAndrzej Pietrasiewicz 	cd->bAssocTerminal		= 0;
78146919a23SAndrzej Pietrasiewicz 	cd->iTerminal			= 0;
78246919a23SAndrzej Pietrasiewicz 	cd->wObjectiveFocalLengthMin	= cpu_to_le16(0);
78346919a23SAndrzej Pietrasiewicz 	cd->wObjectiveFocalLengthMax	= cpu_to_le16(0);
78446919a23SAndrzej Pietrasiewicz 	cd->wOcularFocalLength		= cpu_to_le16(0);
78546919a23SAndrzej Pietrasiewicz 	cd->bControlSize		= 3;
78646919a23SAndrzej Pietrasiewicz 	cd->bmControls[0]		= 2;
78746919a23SAndrzej Pietrasiewicz 	cd->bmControls[1]		= 0;
78846919a23SAndrzej Pietrasiewicz 	cd->bmControls[2]		= 0;
78946919a23SAndrzej Pietrasiewicz 
79046919a23SAndrzej Pietrasiewicz 	pd = &opts->uvc_processing;
79146919a23SAndrzej Pietrasiewicz 	pd->bLength			= UVC_DT_PROCESSING_UNIT_SIZE(2);
79246919a23SAndrzej Pietrasiewicz 	pd->bDescriptorType		= USB_DT_CS_INTERFACE;
79346919a23SAndrzej Pietrasiewicz 	pd->bDescriptorSubType		= UVC_VC_PROCESSING_UNIT;
79446919a23SAndrzej Pietrasiewicz 	pd->bUnitID			= 2;
79546919a23SAndrzej Pietrasiewicz 	pd->bSourceID			= 1;
79646919a23SAndrzej Pietrasiewicz 	pd->wMaxMultiplier		= cpu_to_le16(16*1024);
79746919a23SAndrzej Pietrasiewicz 	pd->bControlSize		= 2;
79846919a23SAndrzej Pietrasiewicz 	pd->bmControls[0]		= 1;
79946919a23SAndrzej Pietrasiewicz 	pd->bmControls[1]		= 0;
80046919a23SAndrzej Pietrasiewicz 	pd->iProcessing			= 0;
80146919a23SAndrzej Pietrasiewicz 
80246919a23SAndrzej Pietrasiewicz 	od = &opts->uvc_output_terminal;
80346919a23SAndrzej Pietrasiewicz 	od->bLength			= UVC_DT_OUTPUT_TERMINAL_SIZE;
80446919a23SAndrzej Pietrasiewicz 	od->bDescriptorType		= USB_DT_CS_INTERFACE;
80546919a23SAndrzej Pietrasiewicz 	od->bDescriptorSubType		= UVC_VC_OUTPUT_TERMINAL;
80646919a23SAndrzej Pietrasiewicz 	od->bTerminalID			= 3;
80746919a23SAndrzej Pietrasiewicz 	od->wTerminalType		= cpu_to_le16(0x0101);
80846919a23SAndrzej Pietrasiewicz 	od->bAssocTerminal		= 0;
80946919a23SAndrzej Pietrasiewicz 	od->bSourceID			= 2;
81046919a23SAndrzej Pietrasiewicz 	od->iTerminal			= 0;
81146919a23SAndrzej Pietrasiewicz 
81246919a23SAndrzej Pietrasiewicz 	md = &opts->uvc_color_matching;
81346919a23SAndrzej Pietrasiewicz 	md->bLength			= UVC_DT_COLOR_MATCHING_SIZE;
81446919a23SAndrzej Pietrasiewicz 	md->bDescriptorType		= USB_DT_CS_INTERFACE;
81546919a23SAndrzej Pietrasiewicz 	md->bDescriptorSubType		= UVC_VS_COLORFORMAT;
81646919a23SAndrzej Pietrasiewicz 	md->bColorPrimaries		= 1;
81746919a23SAndrzej Pietrasiewicz 	md->bTransferCharacteristics	= 1;
81846919a23SAndrzej Pietrasiewicz 	md->bMatrixCoefficients		= 4;
81946919a23SAndrzej Pietrasiewicz 
82046919a23SAndrzej Pietrasiewicz 	/* Prepare fs control class descriptors for configfs-based gadgets */
82146919a23SAndrzej Pietrasiewicz 	ctl_cls = opts->uvc_fs_control_cls;
82246919a23SAndrzej Pietrasiewicz 	ctl_cls[0] = NULL;	/* assigned elsewhere by configfs */
82346919a23SAndrzej Pietrasiewicz 	ctl_cls[1] = (struct uvc_descriptor_header *)cd;
82446919a23SAndrzej Pietrasiewicz 	ctl_cls[2] = (struct uvc_descriptor_header *)pd;
82546919a23SAndrzej Pietrasiewicz 	ctl_cls[3] = (struct uvc_descriptor_header *)od;
82646919a23SAndrzej Pietrasiewicz 	ctl_cls[4] = NULL;	/* NULL-terminate */
82746919a23SAndrzej Pietrasiewicz 	opts->fs_control =
82846919a23SAndrzej Pietrasiewicz 		(const struct uvc_descriptor_header * const *)ctl_cls;
82946919a23SAndrzej Pietrasiewicz 
83046919a23SAndrzej Pietrasiewicz 	/* Prepare hs control class descriptors for configfs-based gadgets */
83146919a23SAndrzej Pietrasiewicz 	ctl_cls = opts->uvc_ss_control_cls;
83246919a23SAndrzej Pietrasiewicz 	ctl_cls[0] = NULL;	/* assigned elsewhere by configfs */
83346919a23SAndrzej Pietrasiewicz 	ctl_cls[1] = (struct uvc_descriptor_header *)cd;
83446919a23SAndrzej Pietrasiewicz 	ctl_cls[2] = (struct uvc_descriptor_header *)pd;
83546919a23SAndrzej Pietrasiewicz 	ctl_cls[3] = (struct uvc_descriptor_header *)od;
83646919a23SAndrzej Pietrasiewicz 	ctl_cls[4] = NULL;	/* NULL-terminate */
83746919a23SAndrzej Pietrasiewicz 	opts->ss_control =
83846919a23SAndrzej Pietrasiewicz 		(const struct uvc_descriptor_header * const *)ctl_cls;
83946919a23SAndrzej Pietrasiewicz 
84046919a23SAndrzej Pietrasiewicz 	opts->streaming_interval = 1;
84146919a23SAndrzej Pietrasiewicz 	opts->streaming_maxpacket = 1024;
84246919a23SAndrzej Pietrasiewicz 
84346919a23SAndrzej Pietrasiewicz 	uvcg_attach_configfs(opts);
8446d11ed76SAndrzej Pietrasiewicz 	return &opts->func_inst;
8456d11ed76SAndrzej Pietrasiewicz }
8466d11ed76SAndrzej Pietrasiewicz 
8476d11ed76SAndrzej Pietrasiewicz static void uvc_free(struct usb_function *f)
8486d11ed76SAndrzej Pietrasiewicz {
8496d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
85046919a23SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
85146919a23SAndrzej Pietrasiewicz 					       func_inst);
85246919a23SAndrzej Pietrasiewicz 	--opts->refcnt;
8536d11ed76SAndrzej Pietrasiewicz 	kfree(uvc);
8546d11ed76SAndrzej Pietrasiewicz }
8556d11ed76SAndrzej Pietrasiewicz 
8566d11ed76SAndrzej Pietrasiewicz static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
8576d11ed76SAndrzej Pietrasiewicz {
8586d11ed76SAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
8596d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
8606d11ed76SAndrzej Pietrasiewicz 
8616d11ed76SAndrzej Pietrasiewicz 	INFO(cdev, "%s\n", __func__);
8626d11ed76SAndrzej Pietrasiewicz 
863dbe98b30SHans Verkuil 	video_unregister_device(&uvc->vdev);
8646d11ed76SAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
8656d11ed76SAndrzej Pietrasiewicz 
8666d11ed76SAndrzej Pietrasiewicz 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
8676d11ed76SAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
8686d11ed76SAndrzej Pietrasiewicz 
8696d11ed76SAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
8706d11ed76SAndrzej Pietrasiewicz }
8716d11ed76SAndrzej Pietrasiewicz 
8724a6698b8SFengguang Wu static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
8736d11ed76SAndrzej Pietrasiewicz {
8746d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc;
8756d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
87646919a23SAndrzej Pietrasiewicz 	struct uvc_descriptor_header **strm_cls;
8776d11ed76SAndrzej Pietrasiewicz 
8786d11ed76SAndrzej Pietrasiewicz 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
8796d11ed76SAndrzej Pietrasiewicz 	if (uvc == NULL)
8806d11ed76SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
8816d11ed76SAndrzej Pietrasiewicz 
882d8e96c4bSHans Verkuil 	mutex_init(&uvc->video.mutex);
8836d11ed76SAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
884bbea6de1SAndrzej Pietrasiewicz 	opts = fi_to_f_uvc_opts(fi);
8856d11ed76SAndrzej Pietrasiewicz 
88646919a23SAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
88746919a23SAndrzej Pietrasiewicz 	if (opts->uvc_fs_streaming_cls) {
88846919a23SAndrzej Pietrasiewicz 		strm_cls = opts->uvc_fs_streaming_cls;
88946919a23SAndrzej Pietrasiewicz 		opts->fs_streaming =
89046919a23SAndrzej Pietrasiewicz 			(const struct uvc_descriptor_header * const *)strm_cls;
89146919a23SAndrzej Pietrasiewicz 	}
89246919a23SAndrzej Pietrasiewicz 	if (opts->uvc_hs_streaming_cls) {
89346919a23SAndrzej Pietrasiewicz 		strm_cls = opts->uvc_hs_streaming_cls;
89446919a23SAndrzej Pietrasiewicz 		opts->hs_streaming =
89546919a23SAndrzej Pietrasiewicz 			(const struct uvc_descriptor_header * const *)strm_cls;
89646919a23SAndrzej Pietrasiewicz 	}
89746919a23SAndrzej Pietrasiewicz 	if (opts->uvc_ss_streaming_cls) {
89846919a23SAndrzej Pietrasiewicz 		strm_cls = opts->uvc_ss_streaming_cls;
89946919a23SAndrzej Pietrasiewicz 		opts->ss_streaming =
90046919a23SAndrzej Pietrasiewicz 			(const struct uvc_descriptor_header * const *)strm_cls;
90146919a23SAndrzej Pietrasiewicz 	}
90246919a23SAndrzej Pietrasiewicz 
9036d11ed76SAndrzej Pietrasiewicz 	uvc->desc.fs_control = opts->fs_control;
9046d11ed76SAndrzej Pietrasiewicz 	uvc->desc.ss_control = opts->ss_control;
9056d11ed76SAndrzej Pietrasiewicz 	uvc->desc.fs_streaming = opts->fs_streaming;
9066d11ed76SAndrzej Pietrasiewicz 	uvc->desc.hs_streaming = opts->hs_streaming;
9076d11ed76SAndrzej Pietrasiewicz 	uvc->desc.ss_streaming = opts->ss_streaming;
90846919a23SAndrzej Pietrasiewicz 	++opts->refcnt;
90946919a23SAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
9106d11ed76SAndrzej Pietrasiewicz 
9116d11ed76SAndrzej Pietrasiewicz 	/* Register the function. */
9126d11ed76SAndrzej Pietrasiewicz 	uvc->func.name = "uvc";
9136d11ed76SAndrzej Pietrasiewicz 	uvc->func.bind = uvc_function_bind;
9146d11ed76SAndrzej Pietrasiewicz 	uvc->func.unbind = uvc_unbind;
9156d11ed76SAndrzej Pietrasiewicz 	uvc->func.get_alt = uvc_function_get_alt;
9166d11ed76SAndrzej Pietrasiewicz 	uvc->func.set_alt = uvc_function_set_alt;
9176d11ed76SAndrzej Pietrasiewicz 	uvc->func.disable = uvc_function_disable;
9186d11ed76SAndrzej Pietrasiewicz 	uvc->func.setup = uvc_function_setup;
9196d11ed76SAndrzej Pietrasiewicz 	uvc->func.free_func = uvc_free;
920f277bf27SRobert Baldyga 	uvc->func.bind_deactivated = true;
9216d11ed76SAndrzej Pietrasiewicz 
9226d11ed76SAndrzej Pietrasiewicz 	return &uvc->func;
9236d11ed76SAndrzej Pietrasiewicz }
9246d11ed76SAndrzej Pietrasiewicz 
9256d11ed76SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
9266d11ed76SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
9276d11ed76SAndrzej Pietrasiewicz MODULE_AUTHOR("Laurent Pinchart");
928