xref: /openbmc/linux/drivers/usb/gadget/function/f_uvc.c (revision 6d11ed76c45dd7c8322c2d03575f2164cc725c18)
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>
14*6d11ed76SAndrzej 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 
3000a2430fSAndrzej Pietrasiewicz #include "uvc.h"
313a83c16eSAndrzej Pietrasiewicz #include "uvc_v4l2.h"
323a83c16eSAndrzej Pietrasiewicz #include "uvc_video.h"
33*6d11ed76SAndrzej Pietrasiewicz #include "u_uvc.h"
3400a2430fSAndrzej Pietrasiewicz 
3500a2430fSAndrzej Pietrasiewicz unsigned int uvc_gadget_trace_param;
36*6d11ed76SAndrzej Pietrasiewicz #ifdef USBF_UVC_INCLUDED
37efb540c8SAndrzej Pietrasiewicz static unsigned int streaming_interval;
38efb540c8SAndrzej Pietrasiewicz static unsigned int streaming_maxpacket;
3900a2430fSAndrzej Pietrasiewicz static unsigned int streaming_maxburst;
40*6d11ed76SAndrzej Pietrasiewicz #endif
4100a2430fSAndrzej Pietrasiewicz 
4200a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
4300a2430fSAndrzej Pietrasiewicz  * Function descriptors
4400a2430fSAndrzej Pietrasiewicz  */
4500a2430fSAndrzej Pietrasiewicz 
4600a2430fSAndrzej Pietrasiewicz /* string IDs are assigned dynamically */
4700a2430fSAndrzej Pietrasiewicz 
4800a2430fSAndrzej Pietrasiewicz #define UVC_STRING_CONTROL_IDX			0
4900a2430fSAndrzej Pietrasiewicz #define UVC_STRING_STREAMING_IDX		1
5000a2430fSAndrzej Pietrasiewicz 
5100a2430fSAndrzej Pietrasiewicz static struct usb_string uvc_en_us_strings[] = {
5200a2430fSAndrzej Pietrasiewicz 	[UVC_STRING_CONTROL_IDX].s = "UVC Camera",
5300a2430fSAndrzej Pietrasiewicz 	[UVC_STRING_STREAMING_IDX].s = "Video Streaming",
5400a2430fSAndrzej Pietrasiewicz 	{  }
5500a2430fSAndrzej Pietrasiewicz };
5600a2430fSAndrzej Pietrasiewicz 
5700a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings uvc_stringtab = {
5800a2430fSAndrzej Pietrasiewicz 	.language = 0x0409,	/* en-us */
5900a2430fSAndrzej Pietrasiewicz 	.strings = uvc_en_us_strings,
6000a2430fSAndrzej Pietrasiewicz };
6100a2430fSAndrzej Pietrasiewicz 
6200a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings *uvc_function_strings[] = {
6300a2430fSAndrzej Pietrasiewicz 	&uvc_stringtab,
6400a2430fSAndrzej Pietrasiewicz 	NULL,
6500a2430fSAndrzej Pietrasiewicz };
6600a2430fSAndrzej Pietrasiewicz 
6700a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_CONTROL			0
6800a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_STREAMING		1
6900a2430fSAndrzej Pietrasiewicz 
7000a2430fSAndrzej Pietrasiewicz #define UVC_STATUS_MAX_PACKET_SIZE		16	/* 16 bytes status */
7100a2430fSAndrzej Pietrasiewicz 
72*6d11ed76SAndrzej Pietrasiewicz static struct usb_interface_assoc_descriptor uvc_iad = {
7300a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_iad),
7400a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
7500a2430fSAndrzej Pietrasiewicz 	.bFirstInterface	= 0,
7600a2430fSAndrzej Pietrasiewicz 	.bInterfaceCount	= 2,
7700a2430fSAndrzej Pietrasiewicz 	.bFunctionClass		= USB_CLASS_VIDEO,
7800a2430fSAndrzej Pietrasiewicz 	.bFunctionSubClass	= UVC_SC_VIDEO_INTERFACE_COLLECTION,
7900a2430fSAndrzej Pietrasiewicz 	.bFunctionProtocol	= 0x00,
8000a2430fSAndrzej Pietrasiewicz 	.iFunction		= 0,
8100a2430fSAndrzej Pietrasiewicz };
8200a2430fSAndrzej Pietrasiewicz 
83*6d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_control_intf = {
8400a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
8500a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
8600a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL,
8700a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
8800a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 1,
8900a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
9000a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOCONTROL,
9100a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
9200a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
9300a2430fSAndrzej Pietrasiewicz };
9400a2430fSAndrzej Pietrasiewicz 
95*6d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_control_ep = {
9600a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
9700a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
9800a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
9900a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
10000a2430fSAndrzej Pietrasiewicz 	.wMaxPacketSize		= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
10100a2430fSAndrzej Pietrasiewicz 	.bInterval		= 8,
10200a2430fSAndrzej Pietrasiewicz };
10300a2430fSAndrzej Pietrasiewicz 
104*6d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
10500a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_ss_control_comp),
10600a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
10700a2430fSAndrzej Pietrasiewicz 	/* The following 3 values can be tweaked if necessary. */
10800a2430fSAndrzej Pietrasiewicz 	.bMaxBurst		= 0,
10900a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= 0,
11000a2430fSAndrzej Pietrasiewicz 	.wBytesPerInterval	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
11100a2430fSAndrzej Pietrasiewicz };
11200a2430fSAndrzej Pietrasiewicz 
113*6d11ed76SAndrzej Pietrasiewicz static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
11400a2430fSAndrzej Pietrasiewicz 	.bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE,
11500a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_CS_ENDPOINT,
11600a2430fSAndrzej Pietrasiewicz 	.bDescriptorSubType	= UVC_EP_INTERRUPT,
11700a2430fSAndrzej Pietrasiewicz 	.wMaxTransferSize	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
11800a2430fSAndrzej Pietrasiewicz };
11900a2430fSAndrzej Pietrasiewicz 
120*6d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
12100a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
12200a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
12300a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
12400a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
12500a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 0,
12600a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
12700a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
12800a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
12900a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
13000a2430fSAndrzej Pietrasiewicz };
13100a2430fSAndrzej Pietrasiewicz 
132*6d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
13300a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_INTERFACE_SIZE,
13400a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
13500a2430fSAndrzej Pietrasiewicz 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
13600a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 1,
13700a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 1,
13800a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_VIDEO,
13900a2430fSAndrzej Pietrasiewicz 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
14000a2430fSAndrzej Pietrasiewicz 	.bInterfaceProtocol	= 0x00,
14100a2430fSAndrzej Pietrasiewicz 	.iInterface		= 0,
14200a2430fSAndrzej Pietrasiewicz };
14300a2430fSAndrzej Pietrasiewicz 
144*6d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
14500a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
14600a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
14700a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
14800a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
14900a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
15000a2430fSAndrzej Pietrasiewicz 	/* The wMaxPacketSize and bInterval values will be initialized from
15100a2430fSAndrzej Pietrasiewicz 	 * module parameters.
15200a2430fSAndrzej Pietrasiewicz 	 */
15300a2430fSAndrzej Pietrasiewicz };
15400a2430fSAndrzej Pietrasiewicz 
155*6d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
15600a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
15700a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
15800a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
15900a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
16000a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
16100a2430fSAndrzej Pietrasiewicz 	/* The wMaxPacketSize and bInterval values will be initialized from
16200a2430fSAndrzej Pietrasiewicz 	 * module parameters.
16300a2430fSAndrzej Pietrasiewicz 	 */
16400a2430fSAndrzej Pietrasiewicz };
16500a2430fSAndrzej Pietrasiewicz 
166*6d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
16700a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
16800a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
16900a2430fSAndrzej Pietrasiewicz 
17000a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
17100a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
17200a2430fSAndrzej Pietrasiewicz 				| USB_ENDPOINT_XFER_ISOC,
17300a2430fSAndrzej Pietrasiewicz 	/* The wMaxPacketSize and bInterval values will be initialized from
17400a2430fSAndrzej Pietrasiewicz 	 * module parameters.
17500a2430fSAndrzej Pietrasiewicz 	 */
17600a2430fSAndrzej Pietrasiewicz };
17700a2430fSAndrzej Pietrasiewicz 
178*6d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
17900a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof(uvc_ss_streaming_comp),
18000a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
18100a2430fSAndrzej Pietrasiewicz 	/* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
18200a2430fSAndrzej Pietrasiewicz 	 * initialized from module parameters.
18300a2430fSAndrzej Pietrasiewicz 	 */
18400a2430fSAndrzej Pietrasiewicz };
18500a2430fSAndrzej Pietrasiewicz 
18600a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
18700a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
18800a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_fs_streaming_ep,
18900a2430fSAndrzej Pietrasiewicz 	NULL,
19000a2430fSAndrzej Pietrasiewicz };
19100a2430fSAndrzej Pietrasiewicz 
19200a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
19300a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
19400a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_hs_streaming_ep,
19500a2430fSAndrzej Pietrasiewicz 	NULL,
19600a2430fSAndrzej Pietrasiewicz };
19700a2430fSAndrzej Pietrasiewicz 
19800a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
19900a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
20000a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_ss_streaming_ep,
20100a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *) &uvc_ss_streaming_comp,
20200a2430fSAndrzej Pietrasiewicz 	NULL,
20300a2430fSAndrzej Pietrasiewicz };
20400a2430fSAndrzej Pietrasiewicz 
205*6d11ed76SAndrzej Pietrasiewicz #ifndef USBF_UVC_INCLUDED
206*6d11ed76SAndrzej Pietrasiewicz 
207*6d11ed76SAndrzej Pietrasiewicz void uvc_set_trace_param(unsigned int trace)
208*6d11ed76SAndrzej Pietrasiewicz {
209*6d11ed76SAndrzej Pietrasiewicz 	uvc_gadget_trace_param = trace;
210*6d11ed76SAndrzej Pietrasiewicz }
211*6d11ed76SAndrzej Pietrasiewicz EXPORT_SYMBOL(uvc_set_trace_param);
212*6d11ed76SAndrzej Pietrasiewicz 
213*6d11ed76SAndrzej Pietrasiewicz #endif
214*6d11ed76SAndrzej Pietrasiewicz 
21500a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
21600a2430fSAndrzej Pietrasiewicz  * Control requests
21700a2430fSAndrzej Pietrasiewicz  */
21800a2430fSAndrzej Pietrasiewicz 
21900a2430fSAndrzej Pietrasiewicz static void
22000a2430fSAndrzej Pietrasiewicz uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
22100a2430fSAndrzej Pietrasiewicz {
22200a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = req->context;
22300a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
22400a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
22500a2430fSAndrzej Pietrasiewicz 
22600a2430fSAndrzej Pietrasiewicz 	if (uvc->event_setup_out) {
22700a2430fSAndrzej Pietrasiewicz 		uvc->event_setup_out = 0;
22800a2430fSAndrzej Pietrasiewicz 
22900a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
23000a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_DATA;
23100a2430fSAndrzej Pietrasiewicz 		uvc_event->data.length = req->actual;
23200a2430fSAndrzej Pietrasiewicz 		memcpy(&uvc_event->data.data, req->buf, req->actual);
23300a2430fSAndrzej Pietrasiewicz 		v4l2_event_queue(uvc->vdev, &v4l2_event);
23400a2430fSAndrzej Pietrasiewicz 	}
23500a2430fSAndrzej Pietrasiewicz }
23600a2430fSAndrzej Pietrasiewicz 
23700a2430fSAndrzej Pietrasiewicz static int
23800a2430fSAndrzej Pietrasiewicz uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
23900a2430fSAndrzej Pietrasiewicz {
24000a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
24100a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
24200a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
24300a2430fSAndrzej Pietrasiewicz 
24400a2430fSAndrzej Pietrasiewicz 	/* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
24500a2430fSAndrzej Pietrasiewicz 	 *	ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
24600a2430fSAndrzej Pietrasiewicz 	 *	le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
24700a2430fSAndrzej Pietrasiewicz 	 */
24800a2430fSAndrzej Pietrasiewicz 
24900a2430fSAndrzej Pietrasiewicz 	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
25000a2430fSAndrzej Pietrasiewicz 		INFO(f->config->cdev, "invalid request type\n");
25100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
25200a2430fSAndrzej Pietrasiewicz 	}
25300a2430fSAndrzej Pietrasiewicz 
25400a2430fSAndrzej Pietrasiewicz 	/* Stall too big requests. */
25500a2430fSAndrzej Pietrasiewicz 	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
25600a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
25700a2430fSAndrzej Pietrasiewicz 
25826a029f2SLaurent Pinchart 	/* Tell the complete callback to generate an event for the next request
25926a029f2SLaurent Pinchart 	 * that will be enqueued by UVCIOC_SEND_RESPONSE.
26026a029f2SLaurent Pinchart 	 */
26126a029f2SLaurent Pinchart 	uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
26226a029f2SLaurent Pinchart 	uvc->event_length = le16_to_cpu(ctrl->wLength);
26326a029f2SLaurent Pinchart 
26400a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
26500a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_SETUP;
26600a2430fSAndrzej Pietrasiewicz 	memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
26700a2430fSAndrzej Pietrasiewicz 	v4l2_event_queue(uvc->vdev, &v4l2_event);
26800a2430fSAndrzej Pietrasiewicz 
26900a2430fSAndrzej Pietrasiewicz 	return 0;
27000a2430fSAndrzej Pietrasiewicz }
27100a2430fSAndrzej Pietrasiewicz 
27200a2430fSAndrzej Pietrasiewicz void uvc_function_setup_continue(struct uvc_device *uvc)
27300a2430fSAndrzej Pietrasiewicz {
27400a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
27500a2430fSAndrzej Pietrasiewicz 
27600a2430fSAndrzej Pietrasiewicz 	usb_composite_setup_continue(cdev);
27700a2430fSAndrzej Pietrasiewicz }
27800a2430fSAndrzej Pietrasiewicz 
27900a2430fSAndrzej Pietrasiewicz static int
28000a2430fSAndrzej Pietrasiewicz uvc_function_get_alt(struct usb_function *f, unsigned interface)
28100a2430fSAndrzej Pietrasiewicz {
28200a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
28300a2430fSAndrzej Pietrasiewicz 
28400a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
28500a2430fSAndrzej Pietrasiewicz 
28600a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf)
28700a2430fSAndrzej Pietrasiewicz 		return 0;
28800a2430fSAndrzej Pietrasiewicz 	else if (interface != uvc->streaming_intf)
28900a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
29000a2430fSAndrzej Pietrasiewicz 	else
29100a2430fSAndrzej Pietrasiewicz 		return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
29200a2430fSAndrzej Pietrasiewicz }
29300a2430fSAndrzej Pietrasiewicz 
29400a2430fSAndrzej Pietrasiewicz static int
29500a2430fSAndrzej Pietrasiewicz uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
29600a2430fSAndrzej Pietrasiewicz {
29700a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
29800a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
29900a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
30000a2430fSAndrzej Pietrasiewicz 	int ret;
30100a2430fSAndrzej Pietrasiewicz 
30200a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
30300a2430fSAndrzej Pietrasiewicz 
30400a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf) {
30500a2430fSAndrzej Pietrasiewicz 		if (alt)
30600a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
30700a2430fSAndrzej Pietrasiewicz 
30800a2430fSAndrzej Pietrasiewicz 		if (uvc->state == UVC_STATE_DISCONNECTED) {
30900a2430fSAndrzej Pietrasiewicz 			memset(&v4l2_event, 0, sizeof(v4l2_event));
31000a2430fSAndrzej Pietrasiewicz 			v4l2_event.type = UVC_EVENT_CONNECT;
31100a2430fSAndrzej Pietrasiewicz 			uvc_event->speed = f->config->cdev->gadget->speed;
31200a2430fSAndrzej Pietrasiewicz 			v4l2_event_queue(uvc->vdev, &v4l2_event);
31300a2430fSAndrzej Pietrasiewicz 
31400a2430fSAndrzej Pietrasiewicz 			uvc->state = UVC_STATE_CONNECTED;
31500a2430fSAndrzej Pietrasiewicz 		}
31600a2430fSAndrzej Pietrasiewicz 
31700a2430fSAndrzej Pietrasiewicz 		return 0;
31800a2430fSAndrzej Pietrasiewicz 	}
31900a2430fSAndrzej Pietrasiewicz 
32000a2430fSAndrzej Pietrasiewicz 	if (interface != uvc->streaming_intf)
32100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
32200a2430fSAndrzej Pietrasiewicz 
32300a2430fSAndrzej Pietrasiewicz 	/* TODO
32400a2430fSAndrzej Pietrasiewicz 	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
32500a2430fSAndrzej Pietrasiewicz 		return alt ? -EINVAL : 0;
32600a2430fSAndrzej Pietrasiewicz 	*/
32700a2430fSAndrzej Pietrasiewicz 
32800a2430fSAndrzej Pietrasiewicz 	switch (alt) {
32900a2430fSAndrzej Pietrasiewicz 	case 0:
33000a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_STREAMING)
33100a2430fSAndrzej Pietrasiewicz 			return 0;
33200a2430fSAndrzej Pietrasiewicz 
33300a2430fSAndrzej Pietrasiewicz 		if (uvc->video.ep)
33400a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(uvc->video.ep);
33500a2430fSAndrzej Pietrasiewicz 
33600a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
33700a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMOFF;
33800a2430fSAndrzej Pietrasiewicz 		v4l2_event_queue(uvc->vdev, &v4l2_event);
33900a2430fSAndrzej Pietrasiewicz 
34000a2430fSAndrzej Pietrasiewicz 		uvc->state = UVC_STATE_CONNECTED;
34100a2430fSAndrzej Pietrasiewicz 		return 0;
34200a2430fSAndrzej Pietrasiewicz 
34300a2430fSAndrzej Pietrasiewicz 	case 1:
34400a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_CONNECTED)
34500a2430fSAndrzej Pietrasiewicz 			return 0;
34600a2430fSAndrzej Pietrasiewicz 
34700a2430fSAndrzej Pietrasiewicz 		if (uvc->video.ep) {
34800a2430fSAndrzej Pietrasiewicz 			ret = config_ep_by_speed(f->config->cdev->gadget,
34900a2430fSAndrzej Pietrasiewicz 					&(uvc->func), uvc->video.ep);
35000a2430fSAndrzej Pietrasiewicz 			if (ret)
35100a2430fSAndrzej Pietrasiewicz 				return ret;
35200a2430fSAndrzej Pietrasiewicz 			usb_ep_enable(uvc->video.ep);
35300a2430fSAndrzej Pietrasiewicz 		}
35400a2430fSAndrzej Pietrasiewicz 
35500a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
35600a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMON;
35700a2430fSAndrzej Pietrasiewicz 		v4l2_event_queue(uvc->vdev, &v4l2_event);
35800a2430fSAndrzej Pietrasiewicz 		return USB_GADGET_DELAYED_STATUS;
35900a2430fSAndrzej Pietrasiewicz 
36000a2430fSAndrzej Pietrasiewicz 	default:
36100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
36200a2430fSAndrzej Pietrasiewicz 	}
36300a2430fSAndrzej Pietrasiewicz }
36400a2430fSAndrzej Pietrasiewicz 
36500a2430fSAndrzej Pietrasiewicz static void
36600a2430fSAndrzej Pietrasiewicz uvc_function_disable(struct usb_function *f)
36700a2430fSAndrzej Pietrasiewicz {
36800a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
36900a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
37000a2430fSAndrzej Pietrasiewicz 
37100a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_disable\n");
37200a2430fSAndrzej Pietrasiewicz 
37300a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
37400a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_DISCONNECT;
37500a2430fSAndrzej Pietrasiewicz 	v4l2_event_queue(uvc->vdev, &v4l2_event);
37600a2430fSAndrzej Pietrasiewicz 
37700a2430fSAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
37800a2430fSAndrzej Pietrasiewicz }
37900a2430fSAndrzej Pietrasiewicz 
38000a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
38100a2430fSAndrzej Pietrasiewicz  * Connection / disconnection
38200a2430fSAndrzej Pietrasiewicz  */
38300a2430fSAndrzej Pietrasiewicz 
38400a2430fSAndrzej Pietrasiewicz void
38500a2430fSAndrzej Pietrasiewicz uvc_function_connect(struct uvc_device *uvc)
38600a2430fSAndrzej Pietrasiewicz {
38700a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
38800a2430fSAndrzej Pietrasiewicz 	int ret;
38900a2430fSAndrzej Pietrasiewicz 
39000a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_activate(&uvc->func)) < 0)
39100a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "UVC connect failed with %d\n", ret);
39200a2430fSAndrzej Pietrasiewicz }
39300a2430fSAndrzej Pietrasiewicz 
39400a2430fSAndrzej Pietrasiewicz void
39500a2430fSAndrzej Pietrasiewicz uvc_function_disconnect(struct uvc_device *uvc)
39600a2430fSAndrzej Pietrasiewicz {
39700a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
39800a2430fSAndrzej Pietrasiewicz 	int ret;
39900a2430fSAndrzej Pietrasiewicz 
40000a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
40100a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "UVC disconnect failed with %d\n", ret);
40200a2430fSAndrzej Pietrasiewicz }
40300a2430fSAndrzej Pietrasiewicz 
40400a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
40500a2430fSAndrzej Pietrasiewicz  * USB probe and disconnect
40600a2430fSAndrzej Pietrasiewicz  */
40700a2430fSAndrzej Pietrasiewicz 
40800a2430fSAndrzej Pietrasiewicz static int
40900a2430fSAndrzej Pietrasiewicz uvc_register_video(struct uvc_device *uvc)
41000a2430fSAndrzej Pietrasiewicz {
41100a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
41200a2430fSAndrzej Pietrasiewicz 	struct video_device *video;
41300a2430fSAndrzej Pietrasiewicz 
41400a2430fSAndrzej Pietrasiewicz 	/* TODO reference counting. */
41500a2430fSAndrzej Pietrasiewicz 	video = video_device_alloc();
41600a2430fSAndrzej Pietrasiewicz 	if (video == NULL)
41700a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
41800a2430fSAndrzej Pietrasiewicz 
41900a2430fSAndrzej Pietrasiewicz 	video->v4l2_dev = &uvc->v4l2_dev;
42000a2430fSAndrzej Pietrasiewicz 	video->fops = &uvc_v4l2_fops;
421a1d27a4bSLaurent Pinchart 	video->ioctl_ops = &uvc_v4l2_ioctl_ops;
42200a2430fSAndrzej Pietrasiewicz 	video->release = video_device_release;
423a1d27a4bSLaurent Pinchart 	video->vfl_dir = VFL_DIR_TX;
42400a2430fSAndrzej Pietrasiewicz 	strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
42500a2430fSAndrzej Pietrasiewicz 
42600a2430fSAndrzej Pietrasiewicz 	uvc->vdev = video;
42700a2430fSAndrzej Pietrasiewicz 	video_set_drvdata(video, uvc);
42800a2430fSAndrzej Pietrasiewicz 
42900a2430fSAndrzej Pietrasiewicz 	return video_register_device(video, VFL_TYPE_GRABBER, -1);
43000a2430fSAndrzej Pietrasiewicz }
43100a2430fSAndrzej Pietrasiewicz 
43200a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
43300a2430fSAndrzej Pietrasiewicz 	do { \
43400a2430fSAndrzej Pietrasiewicz 		memcpy(mem, desc, (desc)->bLength); \
43500a2430fSAndrzej Pietrasiewicz 		*(dst)++ = mem; \
43600a2430fSAndrzej Pietrasiewicz 		mem += (desc)->bLength; \
43700a2430fSAndrzej Pietrasiewicz 	} while (0);
43800a2430fSAndrzej Pietrasiewicz 
43900a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
44000a2430fSAndrzej Pietrasiewicz 	do { \
44100a2430fSAndrzej Pietrasiewicz 		const struct usb_descriptor_header * const *__src; \
44200a2430fSAndrzej Pietrasiewicz 		for (__src = src; *__src; ++__src) { \
44300a2430fSAndrzej Pietrasiewicz 			memcpy(mem, *__src, (*__src)->bLength); \
44400a2430fSAndrzej Pietrasiewicz 			*dst++ = mem; \
44500a2430fSAndrzej Pietrasiewicz 			mem += (*__src)->bLength; \
44600a2430fSAndrzej Pietrasiewicz 		} \
44700a2430fSAndrzej Pietrasiewicz 	} while (0)
44800a2430fSAndrzej Pietrasiewicz 
449*6d11ed76SAndrzej Pietrasiewicz static struct usb_descriptor_header **
45000a2430fSAndrzej Pietrasiewicz uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
45100a2430fSAndrzej Pietrasiewicz {
45200a2430fSAndrzej Pietrasiewicz 	struct uvc_input_header_descriptor *uvc_streaming_header;
45300a2430fSAndrzej Pietrasiewicz 	struct uvc_header_descriptor *uvc_control_header;
45400a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_control_desc;
45500a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_streaming_cls;
45600a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *uvc_streaming_std;
45700a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *src;
45800a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **dst;
45900a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **hdr;
46000a2430fSAndrzej Pietrasiewicz 	unsigned int control_size;
46100a2430fSAndrzej Pietrasiewicz 	unsigned int streaming_size;
46200a2430fSAndrzej Pietrasiewicz 	unsigned int n_desc;
46300a2430fSAndrzej Pietrasiewicz 	unsigned int bytes;
46400a2430fSAndrzej Pietrasiewicz 	void *mem;
46500a2430fSAndrzej Pietrasiewicz 
46600a2430fSAndrzej Pietrasiewicz 	switch (speed) {
46700a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_SUPER:
46800a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.ss_control;
46900a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.ss_streaming;
47000a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_ss_streaming;
47100a2430fSAndrzej Pietrasiewicz 		break;
47200a2430fSAndrzej Pietrasiewicz 
47300a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_HIGH:
47400a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
47500a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.hs_streaming;
47600a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_hs_streaming;
47700a2430fSAndrzej Pietrasiewicz 		break;
47800a2430fSAndrzej Pietrasiewicz 
47900a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_FULL:
48000a2430fSAndrzej Pietrasiewicz 	default:
48100a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
48200a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.fs_streaming;
48300a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_fs_streaming;
48400a2430fSAndrzej Pietrasiewicz 		break;
48500a2430fSAndrzej Pietrasiewicz 	}
48600a2430fSAndrzej Pietrasiewicz 
48700a2430fSAndrzej Pietrasiewicz 	/* Descriptors layout
48800a2430fSAndrzej Pietrasiewicz 	 *
48900a2430fSAndrzej Pietrasiewicz 	 * uvc_iad
49000a2430fSAndrzej Pietrasiewicz 	 * uvc_control_intf
49100a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC control descriptors
49200a2430fSAndrzej Pietrasiewicz 	 * uvc_control_ep
49300a2430fSAndrzej Pietrasiewicz 	 * uvc_control_cs_ep
49400a2430fSAndrzej Pietrasiewicz 	 * uvc_ss_control_comp (for SS only)
49500a2430fSAndrzej Pietrasiewicz 	 * uvc_streaming_intf_alt0
49600a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC streaming descriptors
49700a2430fSAndrzej Pietrasiewicz 	 * uvc_{fs|hs}_streaming
49800a2430fSAndrzej Pietrasiewicz 	 */
49900a2430fSAndrzej Pietrasiewicz 
50000a2430fSAndrzej Pietrasiewicz 	/* Count descriptors and compute their size. */
50100a2430fSAndrzej Pietrasiewicz 	control_size = 0;
50200a2430fSAndrzej Pietrasiewicz 	streaming_size = 0;
50300a2430fSAndrzej Pietrasiewicz 	bytes = uvc_iad.bLength + uvc_control_intf.bLength
50400a2430fSAndrzej Pietrasiewicz 	      + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
50500a2430fSAndrzej Pietrasiewicz 	      + uvc_streaming_intf_alt0.bLength;
50600a2430fSAndrzej Pietrasiewicz 
50700a2430fSAndrzej Pietrasiewicz 	if (speed == USB_SPEED_SUPER) {
50800a2430fSAndrzej Pietrasiewicz 		bytes += uvc_ss_control_comp.bLength;
50900a2430fSAndrzej Pietrasiewicz 		n_desc = 6;
51000a2430fSAndrzej Pietrasiewicz 	} else {
51100a2430fSAndrzej Pietrasiewicz 		n_desc = 5;
51200a2430fSAndrzej Pietrasiewicz 	}
51300a2430fSAndrzej Pietrasiewicz 
51400a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_control_desc;
51500a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
51600a2430fSAndrzej Pietrasiewicz 		control_size += (*src)->bLength;
51700a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
51800a2430fSAndrzej Pietrasiewicz 		n_desc++;
51900a2430fSAndrzej Pietrasiewicz 	}
52000a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
52100a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
52200a2430fSAndrzej Pietrasiewicz 		streaming_size += (*src)->bLength;
52300a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
52400a2430fSAndrzej Pietrasiewicz 		n_desc++;
52500a2430fSAndrzej Pietrasiewicz 	}
52600a2430fSAndrzej Pietrasiewicz 	for (src = uvc_streaming_std; *src; ++src) {
52700a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
52800a2430fSAndrzej Pietrasiewicz 		n_desc++;
52900a2430fSAndrzej Pietrasiewicz 	}
53000a2430fSAndrzej Pietrasiewicz 
53100a2430fSAndrzej Pietrasiewicz 	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
53200a2430fSAndrzej Pietrasiewicz 	if (mem == NULL)
53300a2430fSAndrzej Pietrasiewicz 		return NULL;
53400a2430fSAndrzej Pietrasiewicz 
53500a2430fSAndrzej Pietrasiewicz 	hdr = mem;
53600a2430fSAndrzej Pietrasiewicz 	dst = mem;
53700a2430fSAndrzej Pietrasiewicz 	mem += (n_desc + 1) * sizeof(*src);
53800a2430fSAndrzej Pietrasiewicz 
53900a2430fSAndrzej Pietrasiewicz 	/* Copy the descriptors. */
54000a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
54100a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
54200a2430fSAndrzej Pietrasiewicz 
54300a2430fSAndrzej Pietrasiewicz 	uvc_control_header = mem;
54400a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
54500a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header **)uvc_control_desc);
54600a2430fSAndrzej Pietrasiewicz 	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
54700a2430fSAndrzej Pietrasiewicz 	uvc_control_header->bInCollection = 1;
54800a2430fSAndrzej Pietrasiewicz 	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
54900a2430fSAndrzej Pietrasiewicz 
55000a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
55100a2430fSAndrzej Pietrasiewicz 	if (speed == USB_SPEED_SUPER)
55200a2430fSAndrzej Pietrasiewicz 		UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
55300a2430fSAndrzej Pietrasiewicz 
55400a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
55500a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
55600a2430fSAndrzej Pietrasiewicz 
55700a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header = mem;
55800a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
55900a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header**)uvc_streaming_cls);
56000a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
56100a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
56200a2430fSAndrzej Pietrasiewicz 
56300a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
56400a2430fSAndrzej Pietrasiewicz 
56500a2430fSAndrzej Pietrasiewicz 	*dst = NULL;
56600a2430fSAndrzej Pietrasiewicz 	return hdr;
56700a2430fSAndrzej Pietrasiewicz }
56800a2430fSAndrzej Pietrasiewicz 
569*6d11ed76SAndrzej Pietrasiewicz #ifdef USBF_UVC_INCLUDED
57000a2430fSAndrzej Pietrasiewicz static void
57100a2430fSAndrzej Pietrasiewicz uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
57200a2430fSAndrzej Pietrasiewicz {
57300a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
57400a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
57500a2430fSAndrzej Pietrasiewicz 
57600a2430fSAndrzej Pietrasiewicz 	INFO(cdev, "uvc_function_unbind\n");
57700a2430fSAndrzej Pietrasiewicz 
57800a2430fSAndrzej Pietrasiewicz 	video_unregister_device(uvc->vdev);
57900a2430fSAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
58000a2430fSAndrzej Pietrasiewicz 	uvc->control_ep->driver_data = NULL;
58100a2430fSAndrzej Pietrasiewicz 	uvc->video.ep->driver_data = NULL;
58200a2430fSAndrzej Pietrasiewicz 
58300a2430fSAndrzej Pietrasiewicz 	uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
58400a2430fSAndrzej Pietrasiewicz 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
58500a2430fSAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
58600a2430fSAndrzej Pietrasiewicz 
58700a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
58800a2430fSAndrzej Pietrasiewicz 
58900a2430fSAndrzej Pietrasiewicz 	kfree(uvc);
59000a2430fSAndrzej Pietrasiewicz }
591*6d11ed76SAndrzej Pietrasiewicz #endif
59200a2430fSAndrzej Pietrasiewicz 
593*6d11ed76SAndrzej Pietrasiewicz static int
59400a2430fSAndrzej Pietrasiewicz uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
59500a2430fSAndrzej Pietrasiewicz {
59600a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
59700a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
59800a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_mult;
59900a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_size;
60000a2430fSAndrzej Pietrasiewicz 	struct usb_ep *ep;
601*6d11ed76SAndrzej Pietrasiewicz #ifndef USBF_UVC_INCLUDED
602*6d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
603*6d11ed76SAndrzej Pietrasiewicz #endif
60400a2430fSAndrzej Pietrasiewicz 	int ret = -EINVAL;
60500a2430fSAndrzej Pietrasiewicz 
60600a2430fSAndrzej Pietrasiewicz 	INFO(cdev, "uvc_function_bind\n");
60700a2430fSAndrzej Pietrasiewicz 
608*6d11ed76SAndrzej Pietrasiewicz #ifdef USBF_UVC_INCLUDED
60900a2430fSAndrzej Pietrasiewicz 	/* Sanity check the streaming endpoint module parameters.
61000a2430fSAndrzej Pietrasiewicz 	 */
61100a2430fSAndrzej Pietrasiewicz 	streaming_interval = clamp(streaming_interval, 1U, 16U);
61200a2430fSAndrzej Pietrasiewicz 	streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
61300a2430fSAndrzej Pietrasiewicz 	streaming_maxburst = min(streaming_maxburst, 15U);
61400a2430fSAndrzej Pietrasiewicz 
61500a2430fSAndrzej Pietrasiewicz 	/* Fill in the FS/HS/SS Video Streaming specific descriptors from the
61600a2430fSAndrzej Pietrasiewicz 	 * module parameters.
61700a2430fSAndrzej Pietrasiewicz 	 *
61800a2430fSAndrzej Pietrasiewicz 	 * NOTE: We assume that the user knows what they are doing and won't
61900a2430fSAndrzej Pietrasiewicz 	 * give parameters that their UDC doesn't support.
62000a2430fSAndrzej Pietrasiewicz 	 */
62100a2430fSAndrzej Pietrasiewicz 	if (streaming_maxpacket <= 1024) {
62200a2430fSAndrzej Pietrasiewicz 		max_packet_mult = 1;
62300a2430fSAndrzej Pietrasiewicz 		max_packet_size = streaming_maxpacket;
62400a2430fSAndrzej Pietrasiewicz 	} else if (streaming_maxpacket <= 2048) {
62500a2430fSAndrzej Pietrasiewicz 		max_packet_mult = 2;
62600a2430fSAndrzej Pietrasiewicz 		max_packet_size = streaming_maxpacket / 2;
62700a2430fSAndrzej Pietrasiewicz 	} else {
62800a2430fSAndrzej Pietrasiewicz 		max_packet_mult = 3;
62900a2430fSAndrzej Pietrasiewicz 		max_packet_size = streaming_maxpacket / 3;
63000a2430fSAndrzej Pietrasiewicz 	}
63100a2430fSAndrzej Pietrasiewicz 
63200a2430fSAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
63300a2430fSAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bInterval = streaming_interval;
63400a2430fSAndrzej Pietrasiewicz 
63500a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
63600a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
63700a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bInterval = streaming_interval;
63800a2430fSAndrzej Pietrasiewicz 
63900a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
64000a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bInterval = streaming_interval;
64100a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
64200a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
64300a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.wBytesPerInterval =
64400a2430fSAndrzej Pietrasiewicz 		max_packet_size * max_packet_mult * streaming_maxburst;
645*6d11ed76SAndrzej Pietrasiewicz #else
646*6d11ed76SAndrzej Pietrasiewicz 	opts = to_f_uvc_opts(f->fi);
647*6d11ed76SAndrzej Pietrasiewicz 	/* Sanity check the streaming endpoint module parameters.
648*6d11ed76SAndrzej Pietrasiewicz 	 */
649*6d11ed76SAndrzej Pietrasiewicz 	opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
650*6d11ed76SAndrzej Pietrasiewicz 	opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
651*6d11ed76SAndrzej Pietrasiewicz 	opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
652*6d11ed76SAndrzej Pietrasiewicz 
653*6d11ed76SAndrzej Pietrasiewicz 	/* Fill in the FS/HS/SS Video Streaming specific descriptors from the
654*6d11ed76SAndrzej Pietrasiewicz 	 * module parameters.
655*6d11ed76SAndrzej Pietrasiewicz 	 *
656*6d11ed76SAndrzej Pietrasiewicz 	 * NOTE: We assume that the user knows what they are doing and won't
657*6d11ed76SAndrzej Pietrasiewicz 	 * give parameters that their UDC doesn't support.
658*6d11ed76SAndrzej Pietrasiewicz 	 */
659*6d11ed76SAndrzej Pietrasiewicz 	if (opts->streaming_maxpacket <= 1024) {
660*6d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 1;
661*6d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket;
662*6d11ed76SAndrzej Pietrasiewicz 	} else if (opts->streaming_maxpacket <= 2048) {
663*6d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 2;
664*6d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket / 2;
665*6d11ed76SAndrzej Pietrasiewicz 	} else {
666*6d11ed76SAndrzej Pietrasiewicz 		max_packet_mult = 3;
667*6d11ed76SAndrzej Pietrasiewicz 		max_packet_size = opts->streaming_maxpacket / 3;
668*6d11ed76SAndrzej Pietrasiewicz 	}
669*6d11ed76SAndrzej Pietrasiewicz 
670*6d11ed76SAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.wMaxPacketSize =
671*6d11ed76SAndrzej Pietrasiewicz 		min(opts->streaming_maxpacket, 1023U);
672*6d11ed76SAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
673*6d11ed76SAndrzej Pietrasiewicz 
674*6d11ed76SAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
675*6d11ed76SAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
676*6d11ed76SAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
677*6d11ed76SAndrzej Pietrasiewicz 
678*6d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
679*6d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
680*6d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
681*6d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
682*6d11ed76SAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.wBytesPerInterval =
683*6d11ed76SAndrzej Pietrasiewicz 		max_packet_size * max_packet_mult * opts->streaming_maxburst;
684*6d11ed76SAndrzej Pietrasiewicz #endif
68500a2430fSAndrzej Pietrasiewicz 
68600a2430fSAndrzej Pietrasiewicz 	/* Allocate endpoints. */
68700a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
68800a2430fSAndrzej Pietrasiewicz 	if (!ep) {
68900a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "Unable to allocate control EP\n");
69000a2430fSAndrzej Pietrasiewicz 		goto error;
69100a2430fSAndrzej Pietrasiewicz 	}
69200a2430fSAndrzej Pietrasiewicz 	uvc->control_ep = ep;
69300a2430fSAndrzej Pietrasiewicz 	ep->driver_data = uvc;
69400a2430fSAndrzej Pietrasiewicz 
69500a2430fSAndrzej Pietrasiewicz 	if (gadget_is_superspeed(c->cdev->gadget))
69600a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
69700a2430fSAndrzej Pietrasiewicz 					  &uvc_ss_streaming_comp);
69800a2430fSAndrzej Pietrasiewicz 	else if (gadget_is_dualspeed(cdev->gadget))
69900a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
70000a2430fSAndrzej Pietrasiewicz 	else
70100a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
70200a2430fSAndrzej Pietrasiewicz 
70300a2430fSAndrzej Pietrasiewicz 	if (!ep) {
70400a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "Unable to allocate streaming EP\n");
70500a2430fSAndrzej Pietrasiewicz 		goto error;
70600a2430fSAndrzej Pietrasiewicz 	}
70700a2430fSAndrzej Pietrasiewicz 	uvc->video.ep = ep;
70800a2430fSAndrzej Pietrasiewicz 	ep->driver_data = uvc;
70900a2430fSAndrzej Pietrasiewicz 
71000a2430fSAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
71100a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
71200a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
71300a2430fSAndrzej Pietrasiewicz 
714*6d11ed76SAndrzej Pietrasiewicz 	/* String descriptors are global, we only need to allocate string IDs
715*6d11ed76SAndrzej Pietrasiewicz 	 * for the first UVC function. UVC functions beyond the first (if any)
716*6d11ed76SAndrzej Pietrasiewicz 	 * will reuse the same IDs.
717*6d11ed76SAndrzej Pietrasiewicz 	 */
718*6d11ed76SAndrzej Pietrasiewicz 	if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
719*6d11ed76SAndrzej Pietrasiewicz 		ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
720*6d11ed76SAndrzej Pietrasiewicz 		if (ret)
721*6d11ed76SAndrzej Pietrasiewicz 			goto error;
722*6d11ed76SAndrzej Pietrasiewicz 		uvc_iad.iFunction =
723*6d11ed76SAndrzej Pietrasiewicz 			uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
724*6d11ed76SAndrzej Pietrasiewicz 		uvc_control_intf.iInterface =
725*6d11ed76SAndrzej Pietrasiewicz 			uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
726*6d11ed76SAndrzej Pietrasiewicz 		ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
727*6d11ed76SAndrzej Pietrasiewicz 		uvc_streaming_intf_alt0.iInterface = ret;
728*6d11ed76SAndrzej Pietrasiewicz 		uvc_streaming_intf_alt1.iInterface = ret;
729*6d11ed76SAndrzej Pietrasiewicz 	}
730*6d11ed76SAndrzej Pietrasiewicz 
73100a2430fSAndrzej Pietrasiewicz 	/* Allocate interface IDs. */
73200a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
73300a2430fSAndrzej Pietrasiewicz 		goto error;
73400a2430fSAndrzej Pietrasiewicz 	uvc_iad.bFirstInterface = ret;
73500a2430fSAndrzej Pietrasiewicz 	uvc_control_intf.bInterfaceNumber = ret;
73600a2430fSAndrzej Pietrasiewicz 	uvc->control_intf = ret;
73700a2430fSAndrzej Pietrasiewicz 
73800a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
73900a2430fSAndrzej Pietrasiewicz 		goto error;
74000a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
74100a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
74200a2430fSAndrzej Pietrasiewicz 	uvc->streaming_intf = ret;
74300a2430fSAndrzej Pietrasiewicz 
74400a2430fSAndrzej Pietrasiewicz 	/* Copy descriptors */
74500a2430fSAndrzej Pietrasiewicz 	f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
74600a2430fSAndrzej Pietrasiewicz 	if (gadget_is_dualspeed(cdev->gadget))
74700a2430fSAndrzej Pietrasiewicz 		f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
74800a2430fSAndrzej Pietrasiewicz 	if (gadget_is_superspeed(c->cdev->gadget))
74900a2430fSAndrzej Pietrasiewicz 		f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
75000a2430fSAndrzej Pietrasiewicz 
75100a2430fSAndrzej Pietrasiewicz 	/* Preallocate control endpoint request. */
75200a2430fSAndrzej Pietrasiewicz 	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
75300a2430fSAndrzej Pietrasiewicz 	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
75400a2430fSAndrzej Pietrasiewicz 	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
75500a2430fSAndrzej Pietrasiewicz 		ret = -ENOMEM;
75600a2430fSAndrzej Pietrasiewicz 		goto error;
75700a2430fSAndrzej Pietrasiewicz 	}
75800a2430fSAndrzej Pietrasiewicz 
75900a2430fSAndrzej Pietrasiewicz 	uvc->control_req->buf = uvc->control_buf;
76000a2430fSAndrzej Pietrasiewicz 	uvc->control_req->complete = uvc_function_ep0_complete;
76100a2430fSAndrzej Pietrasiewicz 	uvc->control_req->context = uvc;
76200a2430fSAndrzej Pietrasiewicz 
76300a2430fSAndrzej Pietrasiewicz 	/* Avoid letting this gadget enumerate until the userspace server is
76400a2430fSAndrzej Pietrasiewicz 	 * active.
76500a2430fSAndrzej Pietrasiewicz 	 */
76600a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_deactivate(f)) < 0)
76700a2430fSAndrzej Pietrasiewicz 		goto error;
76800a2430fSAndrzej Pietrasiewicz 
76900a2430fSAndrzej Pietrasiewicz 	if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
77000a2430fSAndrzej Pietrasiewicz 		printk(KERN_INFO "v4l2_device_register failed\n");
77100a2430fSAndrzej Pietrasiewicz 		goto error;
77200a2430fSAndrzej Pietrasiewicz 	}
77300a2430fSAndrzej Pietrasiewicz 
77400a2430fSAndrzej Pietrasiewicz 	/* Initialise video. */
7757ea95b11SAndrzej Pietrasiewicz 	ret = uvcg_video_init(&uvc->video);
77600a2430fSAndrzej Pietrasiewicz 	if (ret < 0)
77700a2430fSAndrzej Pietrasiewicz 		goto error;
77800a2430fSAndrzej Pietrasiewicz 
77900a2430fSAndrzej Pietrasiewicz 	/* Register a V4L2 device. */
78000a2430fSAndrzej Pietrasiewicz 	ret = uvc_register_video(uvc);
78100a2430fSAndrzej Pietrasiewicz 	if (ret < 0) {
78200a2430fSAndrzej Pietrasiewicz 		printk(KERN_INFO "Unable to register video device\n");
78300a2430fSAndrzej Pietrasiewicz 		goto error;
78400a2430fSAndrzej Pietrasiewicz 	}
78500a2430fSAndrzej Pietrasiewicz 
78600a2430fSAndrzej Pietrasiewicz 	return 0;
78700a2430fSAndrzej Pietrasiewicz 
78800a2430fSAndrzej Pietrasiewicz error:
78900a2430fSAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
79000a2430fSAndrzej Pietrasiewicz 	if (uvc->vdev)
79100a2430fSAndrzej Pietrasiewicz 		video_device_release(uvc->vdev);
79200a2430fSAndrzej Pietrasiewicz 
79300a2430fSAndrzej Pietrasiewicz 	if (uvc->control_ep)
79400a2430fSAndrzej Pietrasiewicz 		uvc->control_ep->driver_data = NULL;
79500a2430fSAndrzej Pietrasiewicz 	if (uvc->video.ep)
79600a2430fSAndrzej Pietrasiewicz 		uvc->video.ep->driver_data = NULL;
79700a2430fSAndrzej Pietrasiewicz 
798e7379857SAndrzej Pietrasiewicz 	if (uvc->control_req)
79900a2430fSAndrzej Pietrasiewicz 		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
80000a2430fSAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
80100a2430fSAndrzej Pietrasiewicz 
80200a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
80300a2430fSAndrzej Pietrasiewicz 	return ret;
80400a2430fSAndrzej Pietrasiewicz }
80500a2430fSAndrzej Pietrasiewicz 
80600a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
80700a2430fSAndrzej Pietrasiewicz  * USB gadget function
80800a2430fSAndrzej Pietrasiewicz  */
80900a2430fSAndrzej Pietrasiewicz 
810*6d11ed76SAndrzej Pietrasiewicz #ifdef USBF_UVC_INCLUDED
81100a2430fSAndrzej Pietrasiewicz /**
81200a2430fSAndrzej Pietrasiewicz  * uvc_bind_config - add a UVC function to a configuration
81300a2430fSAndrzej Pietrasiewicz  * @c: the configuration to support the UVC instance
81400a2430fSAndrzej Pietrasiewicz  * Context: single threaded during gadget setup
81500a2430fSAndrzej Pietrasiewicz  *
81600a2430fSAndrzej Pietrasiewicz  * Returns zero on success, else negative errno.
81700a2430fSAndrzej Pietrasiewicz  *
81800a2430fSAndrzej Pietrasiewicz  * Caller must have called @uvc_setup(). Caller is also responsible for
81900a2430fSAndrzej Pietrasiewicz  * calling @uvc_cleanup() before module unload.
82000a2430fSAndrzej Pietrasiewicz  */
82100a2430fSAndrzej Pietrasiewicz int __init
82200a2430fSAndrzej Pietrasiewicz uvc_bind_config(struct usb_configuration *c,
82300a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *fs_control,
82400a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *ss_control,
82500a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *fs_streaming,
82600a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *hs_streaming,
827efb540c8SAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *ss_streaming,
828efb540c8SAndrzej Pietrasiewicz 		unsigned int stream_interv, unsigned int stream_maxpkt,
829efb540c8SAndrzej Pietrasiewicz 		unsigned int stream_maxburst, unsigned int trace)
83000a2430fSAndrzej Pietrasiewicz {
83100a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc;
83200a2430fSAndrzej Pietrasiewicz 	int ret = 0;
83300a2430fSAndrzej Pietrasiewicz 
83400a2430fSAndrzej Pietrasiewicz 	/* TODO Check if the USB device controller supports the required
83500a2430fSAndrzej Pietrasiewicz 	 * features.
83600a2430fSAndrzej Pietrasiewicz 	 */
83700a2430fSAndrzej Pietrasiewicz 	if (!gadget_is_dualspeed(c->cdev->gadget))
83800a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
83900a2430fSAndrzej Pietrasiewicz 
84000a2430fSAndrzej Pietrasiewicz 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
84100a2430fSAndrzej Pietrasiewicz 	if (uvc == NULL)
84200a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
84300a2430fSAndrzej Pietrasiewicz 
84400a2430fSAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
84500a2430fSAndrzej Pietrasiewicz 
84600a2430fSAndrzej Pietrasiewicz 	/* Validate the descriptors. */
84700a2430fSAndrzej Pietrasiewicz 	if (fs_control == NULL || fs_control[0] == NULL ||
84800a2430fSAndrzej Pietrasiewicz 	    fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
84900a2430fSAndrzej Pietrasiewicz 		goto error;
85000a2430fSAndrzej Pietrasiewicz 
85100a2430fSAndrzej Pietrasiewicz 	if (ss_control == NULL || ss_control[0] == NULL ||
85200a2430fSAndrzej Pietrasiewicz 	    ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
85300a2430fSAndrzej Pietrasiewicz 		goto error;
85400a2430fSAndrzej Pietrasiewicz 
85500a2430fSAndrzej Pietrasiewicz 	if (fs_streaming == NULL || fs_streaming[0] == NULL ||
85600a2430fSAndrzej Pietrasiewicz 	    fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
85700a2430fSAndrzej Pietrasiewicz 		goto error;
85800a2430fSAndrzej Pietrasiewicz 
85900a2430fSAndrzej Pietrasiewicz 	if (hs_streaming == NULL || hs_streaming[0] == NULL ||
86000a2430fSAndrzej Pietrasiewicz 	    hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
86100a2430fSAndrzej Pietrasiewicz 		goto error;
86200a2430fSAndrzej Pietrasiewicz 
86300a2430fSAndrzej Pietrasiewicz 	if (ss_streaming == NULL || ss_streaming[0] == NULL ||
86400a2430fSAndrzej Pietrasiewicz 	    ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
86500a2430fSAndrzej Pietrasiewicz 		goto error;
86600a2430fSAndrzej Pietrasiewicz 
867efb540c8SAndrzej Pietrasiewicz 	streaming_interval = stream_interv;
868efb540c8SAndrzej Pietrasiewicz 	streaming_maxpacket = stream_maxpkt;
869efb540c8SAndrzej Pietrasiewicz 	streaming_maxburst = stream_maxburst;
870efb540c8SAndrzej Pietrasiewicz 	uvc_gadget_trace_param = trace;
87100a2430fSAndrzej Pietrasiewicz 	uvc->desc.fs_control = fs_control;
87200a2430fSAndrzej Pietrasiewicz 	uvc->desc.ss_control = ss_control;
87300a2430fSAndrzej Pietrasiewicz 	uvc->desc.fs_streaming = fs_streaming;
87400a2430fSAndrzej Pietrasiewicz 	uvc->desc.hs_streaming = hs_streaming;
87500a2430fSAndrzej Pietrasiewicz 	uvc->desc.ss_streaming = ss_streaming;
87600a2430fSAndrzej Pietrasiewicz 
87700a2430fSAndrzej Pietrasiewicz 	/* Register the function. */
87800a2430fSAndrzej Pietrasiewicz 	uvc->func.name = "uvc";
87900a2430fSAndrzej Pietrasiewicz 	uvc->func.strings = uvc_function_strings;
88000a2430fSAndrzej Pietrasiewicz 	uvc->func.bind = uvc_function_bind;
88100a2430fSAndrzej Pietrasiewicz 	uvc->func.unbind = uvc_function_unbind;
88200a2430fSAndrzej Pietrasiewicz 	uvc->func.get_alt = uvc_function_get_alt;
88300a2430fSAndrzej Pietrasiewicz 	uvc->func.set_alt = uvc_function_set_alt;
88400a2430fSAndrzej Pietrasiewicz 	uvc->func.disable = uvc_function_disable;
88500a2430fSAndrzej Pietrasiewicz 	uvc->func.setup = uvc_function_setup;
88600a2430fSAndrzej Pietrasiewicz 
88700a2430fSAndrzej Pietrasiewicz 	ret = usb_add_function(c, &uvc->func);
88800a2430fSAndrzej Pietrasiewicz 	if (ret)
88900a2430fSAndrzej Pietrasiewicz 		kfree(uvc);
89000a2430fSAndrzej Pietrasiewicz 
89100a2430fSAndrzej Pietrasiewicz 	return ret;
89200a2430fSAndrzej Pietrasiewicz 
89300a2430fSAndrzej Pietrasiewicz error:
89400a2430fSAndrzej Pietrasiewicz 	kfree(uvc);
89500a2430fSAndrzej Pietrasiewicz 	return ret;
89600a2430fSAndrzej Pietrasiewicz }
89700a2430fSAndrzej Pietrasiewicz 
898*6d11ed76SAndrzej Pietrasiewicz #else
89900a2430fSAndrzej Pietrasiewicz 
900*6d11ed76SAndrzej Pietrasiewicz static void uvc_free_inst(struct usb_function_instance *f)
901*6d11ed76SAndrzej Pietrasiewicz {
902*6d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts = to_f_uvc_opts(f);
903*6d11ed76SAndrzej Pietrasiewicz 
904*6d11ed76SAndrzej Pietrasiewicz 	kfree(opts);
905*6d11ed76SAndrzej Pietrasiewicz }
906*6d11ed76SAndrzej Pietrasiewicz 
907*6d11ed76SAndrzej Pietrasiewicz static struct usb_function_instance *uvc_alloc_inst(void)
908*6d11ed76SAndrzej Pietrasiewicz {
909*6d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
910*6d11ed76SAndrzej Pietrasiewicz 
911*6d11ed76SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
912*6d11ed76SAndrzej Pietrasiewicz 	if (!opts)
913*6d11ed76SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
914*6d11ed76SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = uvc_free_inst;
915*6d11ed76SAndrzej Pietrasiewicz 
916*6d11ed76SAndrzej Pietrasiewicz 	return &opts->func_inst;
917*6d11ed76SAndrzej Pietrasiewicz }
918*6d11ed76SAndrzej Pietrasiewicz 
919*6d11ed76SAndrzej Pietrasiewicz static void uvc_free(struct usb_function *f)
920*6d11ed76SAndrzej Pietrasiewicz {
921*6d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
922*6d11ed76SAndrzej Pietrasiewicz 
923*6d11ed76SAndrzej Pietrasiewicz 	kfree(uvc);
924*6d11ed76SAndrzej Pietrasiewicz }
925*6d11ed76SAndrzej Pietrasiewicz 
926*6d11ed76SAndrzej Pietrasiewicz static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
927*6d11ed76SAndrzej Pietrasiewicz {
928*6d11ed76SAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
929*6d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
930*6d11ed76SAndrzej Pietrasiewicz 
931*6d11ed76SAndrzej Pietrasiewicz 	INFO(cdev, "%s\n", __func__);
932*6d11ed76SAndrzej Pietrasiewicz 
933*6d11ed76SAndrzej Pietrasiewicz 	video_unregister_device(uvc->vdev);
934*6d11ed76SAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
935*6d11ed76SAndrzej Pietrasiewicz 	uvc->control_ep->driver_data = NULL;
936*6d11ed76SAndrzej Pietrasiewicz 	uvc->video.ep->driver_data = NULL;
937*6d11ed76SAndrzej Pietrasiewicz 
938*6d11ed76SAndrzej Pietrasiewicz 	uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
939*6d11ed76SAndrzej Pietrasiewicz 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
940*6d11ed76SAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
941*6d11ed76SAndrzej Pietrasiewicz 
942*6d11ed76SAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
943*6d11ed76SAndrzej Pietrasiewicz }
944*6d11ed76SAndrzej Pietrasiewicz 
945*6d11ed76SAndrzej Pietrasiewicz struct usb_function *uvc_alloc(struct usb_function_instance *fi)
946*6d11ed76SAndrzej Pietrasiewicz {
947*6d11ed76SAndrzej Pietrasiewicz 	struct uvc_device *uvc;
948*6d11ed76SAndrzej Pietrasiewicz 	struct f_uvc_opts *opts;
949*6d11ed76SAndrzej Pietrasiewicz 
950*6d11ed76SAndrzej Pietrasiewicz 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
951*6d11ed76SAndrzej Pietrasiewicz 	if (uvc == NULL)
952*6d11ed76SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
953*6d11ed76SAndrzej Pietrasiewicz 
954*6d11ed76SAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
955*6d11ed76SAndrzej Pietrasiewicz 	opts = to_f_uvc_opts(fi);
956*6d11ed76SAndrzej Pietrasiewicz 
957*6d11ed76SAndrzej Pietrasiewicz 	uvc->desc.fs_control = opts->fs_control;
958*6d11ed76SAndrzej Pietrasiewicz 	uvc->desc.ss_control = opts->ss_control;
959*6d11ed76SAndrzej Pietrasiewicz 	uvc->desc.fs_streaming = opts->fs_streaming;
960*6d11ed76SAndrzej Pietrasiewicz 	uvc->desc.hs_streaming = opts->hs_streaming;
961*6d11ed76SAndrzej Pietrasiewicz 	uvc->desc.ss_streaming = opts->ss_streaming;
962*6d11ed76SAndrzej Pietrasiewicz 
963*6d11ed76SAndrzej Pietrasiewicz 	/* Register the function. */
964*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.name = "uvc";
965*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.strings = uvc_function_strings;
966*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.bind = uvc_function_bind;
967*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.unbind = uvc_unbind;
968*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.get_alt = uvc_function_get_alt;
969*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.set_alt = uvc_function_set_alt;
970*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.disable = uvc_function_disable;
971*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.setup = uvc_function_setup;
972*6d11ed76SAndrzej Pietrasiewicz 	uvc->func.free_func = uvc_free;
973*6d11ed76SAndrzej Pietrasiewicz 
974*6d11ed76SAndrzej Pietrasiewicz 	return &uvc->func;
975*6d11ed76SAndrzej Pietrasiewicz }
976*6d11ed76SAndrzej Pietrasiewicz 
977*6d11ed76SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
978*6d11ed76SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
979*6d11ed76SAndrzej Pietrasiewicz MODULE_AUTHOR("Laurent Pinchart");
980*6d11ed76SAndrzej Pietrasiewicz 
981*6d11ed76SAndrzej Pietrasiewicz #endif
982