xref: /openbmc/linux/drivers/usb/gadget/function/f_uvc.c (revision 3a83c16ef0e03e2ca2f1ce547a7cba53a62d0e0d)
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>
1400a2430fSAndrzej Pietrasiewicz #include <linux/device.h>
1500a2430fSAndrzej Pietrasiewicz #include <linux/errno.h>
1600a2430fSAndrzej Pietrasiewicz #include <linux/fs.h>
1700a2430fSAndrzej Pietrasiewicz #include <linux/list.h>
1800a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h>
1900a2430fSAndrzej Pietrasiewicz #include <linux/string.h>
2000a2430fSAndrzej Pietrasiewicz #include <linux/usb/ch9.h>
2100a2430fSAndrzej Pietrasiewicz #include <linux/usb/gadget.h>
2200a2430fSAndrzej Pietrasiewicz #include <linux/usb/video.h>
2300a2430fSAndrzej Pietrasiewicz #include <linux/vmalloc.h>
2400a2430fSAndrzej Pietrasiewicz #include <linux/wait.h>
2500a2430fSAndrzej Pietrasiewicz 
2600a2430fSAndrzej Pietrasiewicz #include <media/v4l2-dev.h>
2700a2430fSAndrzej Pietrasiewicz #include <media/v4l2-event.h>
2800a2430fSAndrzej Pietrasiewicz 
2900a2430fSAndrzej Pietrasiewicz #include "uvc.h"
30*3a83c16eSAndrzej Pietrasiewicz #include "uvc_v4l2.h"
31*3a83c16eSAndrzej Pietrasiewicz #include "uvc_video.h"
3200a2430fSAndrzej Pietrasiewicz 
3300a2430fSAndrzej Pietrasiewicz unsigned int uvc_gadget_trace_param;
34efb540c8SAndrzej Pietrasiewicz static unsigned int streaming_interval;
35efb540c8SAndrzej Pietrasiewicz static unsigned int streaming_maxpacket;
3600a2430fSAndrzej Pietrasiewicz static unsigned int streaming_maxburst;
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 
6800a2430fSAndrzej Pietrasiewicz static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
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 
7900a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_control_intf __initdata = {
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 
9100a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
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 
10000a2430fSAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
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 
10900a2430fSAndrzej Pietrasiewicz static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
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 
11600a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
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 
12800a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
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 
14000a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
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 
15100a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
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 
16200a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
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 
17400a2430fSAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
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 
20100a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
20200a2430fSAndrzej Pietrasiewicz  * Control requests
20300a2430fSAndrzej Pietrasiewicz  */
20400a2430fSAndrzej Pietrasiewicz 
20500a2430fSAndrzej Pietrasiewicz static void
20600a2430fSAndrzej Pietrasiewicz uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
20700a2430fSAndrzej Pietrasiewicz {
20800a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = req->context;
20900a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
21000a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
21100a2430fSAndrzej Pietrasiewicz 
21200a2430fSAndrzej Pietrasiewicz 	if (uvc->event_setup_out) {
21300a2430fSAndrzej Pietrasiewicz 		uvc->event_setup_out = 0;
21400a2430fSAndrzej Pietrasiewicz 
21500a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
21600a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_DATA;
21700a2430fSAndrzej Pietrasiewicz 		uvc_event->data.length = req->actual;
21800a2430fSAndrzej Pietrasiewicz 		memcpy(&uvc_event->data.data, req->buf, req->actual);
21900a2430fSAndrzej Pietrasiewicz 		v4l2_event_queue(uvc->vdev, &v4l2_event);
22000a2430fSAndrzej Pietrasiewicz 	}
22100a2430fSAndrzej Pietrasiewicz }
22200a2430fSAndrzej Pietrasiewicz 
22300a2430fSAndrzej Pietrasiewicz static int
22400a2430fSAndrzej Pietrasiewicz uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
22500a2430fSAndrzej Pietrasiewicz {
22600a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
22700a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
22800a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
22900a2430fSAndrzej Pietrasiewicz 
23000a2430fSAndrzej Pietrasiewicz 	/* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
23100a2430fSAndrzej Pietrasiewicz 	 *	ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
23200a2430fSAndrzej Pietrasiewicz 	 *	le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
23300a2430fSAndrzej Pietrasiewicz 	 */
23400a2430fSAndrzej Pietrasiewicz 
23500a2430fSAndrzej Pietrasiewicz 	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
23600a2430fSAndrzej Pietrasiewicz 		INFO(f->config->cdev, "invalid request type\n");
23700a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
23800a2430fSAndrzej Pietrasiewicz 	}
23900a2430fSAndrzej Pietrasiewicz 
24000a2430fSAndrzej Pietrasiewicz 	/* Stall too big requests. */
24100a2430fSAndrzej Pietrasiewicz 	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
24200a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
24300a2430fSAndrzej Pietrasiewicz 
24426a029f2SLaurent Pinchart 	/* Tell the complete callback to generate an event for the next request
24526a029f2SLaurent Pinchart 	 * that will be enqueued by UVCIOC_SEND_RESPONSE.
24626a029f2SLaurent Pinchart 	 */
24726a029f2SLaurent Pinchart 	uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
24826a029f2SLaurent Pinchart 	uvc->event_length = le16_to_cpu(ctrl->wLength);
24926a029f2SLaurent Pinchart 
25000a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
25100a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_SETUP;
25200a2430fSAndrzej Pietrasiewicz 	memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
25300a2430fSAndrzej Pietrasiewicz 	v4l2_event_queue(uvc->vdev, &v4l2_event);
25400a2430fSAndrzej Pietrasiewicz 
25500a2430fSAndrzej Pietrasiewicz 	return 0;
25600a2430fSAndrzej Pietrasiewicz }
25700a2430fSAndrzej Pietrasiewicz 
25800a2430fSAndrzej Pietrasiewicz void uvc_function_setup_continue(struct uvc_device *uvc)
25900a2430fSAndrzej Pietrasiewicz {
26000a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
26100a2430fSAndrzej Pietrasiewicz 
26200a2430fSAndrzej Pietrasiewicz 	usb_composite_setup_continue(cdev);
26300a2430fSAndrzej Pietrasiewicz }
26400a2430fSAndrzej Pietrasiewicz 
26500a2430fSAndrzej Pietrasiewicz static int
26600a2430fSAndrzej Pietrasiewicz uvc_function_get_alt(struct usb_function *f, unsigned interface)
26700a2430fSAndrzej Pietrasiewicz {
26800a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
26900a2430fSAndrzej Pietrasiewicz 
27000a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
27100a2430fSAndrzej Pietrasiewicz 
27200a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf)
27300a2430fSAndrzej Pietrasiewicz 		return 0;
27400a2430fSAndrzej Pietrasiewicz 	else if (interface != uvc->streaming_intf)
27500a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
27600a2430fSAndrzej Pietrasiewicz 	else
27700a2430fSAndrzej Pietrasiewicz 		return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
27800a2430fSAndrzej Pietrasiewicz }
27900a2430fSAndrzej Pietrasiewicz 
28000a2430fSAndrzej Pietrasiewicz static int
28100a2430fSAndrzej Pietrasiewicz uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
28200a2430fSAndrzej Pietrasiewicz {
28300a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
28400a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
28500a2430fSAndrzej Pietrasiewicz 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
28600a2430fSAndrzej Pietrasiewicz 	int ret;
28700a2430fSAndrzej Pietrasiewicz 
28800a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
28900a2430fSAndrzej Pietrasiewicz 
29000a2430fSAndrzej Pietrasiewicz 	if (interface == uvc->control_intf) {
29100a2430fSAndrzej Pietrasiewicz 		if (alt)
29200a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
29300a2430fSAndrzej Pietrasiewicz 
29400a2430fSAndrzej Pietrasiewicz 		if (uvc->state == UVC_STATE_DISCONNECTED) {
29500a2430fSAndrzej Pietrasiewicz 			memset(&v4l2_event, 0, sizeof(v4l2_event));
29600a2430fSAndrzej Pietrasiewicz 			v4l2_event.type = UVC_EVENT_CONNECT;
29700a2430fSAndrzej Pietrasiewicz 			uvc_event->speed = f->config->cdev->gadget->speed;
29800a2430fSAndrzej Pietrasiewicz 			v4l2_event_queue(uvc->vdev, &v4l2_event);
29900a2430fSAndrzej Pietrasiewicz 
30000a2430fSAndrzej Pietrasiewicz 			uvc->state = UVC_STATE_CONNECTED;
30100a2430fSAndrzej Pietrasiewicz 		}
30200a2430fSAndrzej Pietrasiewicz 
30300a2430fSAndrzej Pietrasiewicz 		return 0;
30400a2430fSAndrzej Pietrasiewicz 	}
30500a2430fSAndrzej Pietrasiewicz 
30600a2430fSAndrzej Pietrasiewicz 	if (interface != uvc->streaming_intf)
30700a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
30800a2430fSAndrzej Pietrasiewicz 
30900a2430fSAndrzej Pietrasiewicz 	/* TODO
31000a2430fSAndrzej Pietrasiewicz 	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
31100a2430fSAndrzej Pietrasiewicz 		return alt ? -EINVAL : 0;
31200a2430fSAndrzej Pietrasiewicz 	*/
31300a2430fSAndrzej Pietrasiewicz 
31400a2430fSAndrzej Pietrasiewicz 	switch (alt) {
31500a2430fSAndrzej Pietrasiewicz 	case 0:
31600a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_STREAMING)
31700a2430fSAndrzej Pietrasiewicz 			return 0;
31800a2430fSAndrzej Pietrasiewicz 
31900a2430fSAndrzej Pietrasiewicz 		if (uvc->video.ep)
32000a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(uvc->video.ep);
32100a2430fSAndrzej Pietrasiewicz 
32200a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
32300a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMOFF;
32400a2430fSAndrzej Pietrasiewicz 		v4l2_event_queue(uvc->vdev, &v4l2_event);
32500a2430fSAndrzej Pietrasiewicz 
32600a2430fSAndrzej Pietrasiewicz 		uvc->state = UVC_STATE_CONNECTED;
32700a2430fSAndrzej Pietrasiewicz 		return 0;
32800a2430fSAndrzej Pietrasiewicz 
32900a2430fSAndrzej Pietrasiewicz 	case 1:
33000a2430fSAndrzej Pietrasiewicz 		if (uvc->state != UVC_STATE_CONNECTED)
33100a2430fSAndrzej Pietrasiewicz 			return 0;
33200a2430fSAndrzej Pietrasiewicz 
33300a2430fSAndrzej Pietrasiewicz 		if (uvc->video.ep) {
33400a2430fSAndrzej Pietrasiewicz 			ret = config_ep_by_speed(f->config->cdev->gadget,
33500a2430fSAndrzej Pietrasiewicz 					&(uvc->func), uvc->video.ep);
33600a2430fSAndrzej Pietrasiewicz 			if (ret)
33700a2430fSAndrzej Pietrasiewicz 				return ret;
33800a2430fSAndrzej Pietrasiewicz 			usb_ep_enable(uvc->video.ep);
33900a2430fSAndrzej Pietrasiewicz 		}
34000a2430fSAndrzej Pietrasiewicz 
34100a2430fSAndrzej Pietrasiewicz 		memset(&v4l2_event, 0, sizeof(v4l2_event));
34200a2430fSAndrzej Pietrasiewicz 		v4l2_event.type = UVC_EVENT_STREAMON;
34300a2430fSAndrzej Pietrasiewicz 		v4l2_event_queue(uvc->vdev, &v4l2_event);
34400a2430fSAndrzej Pietrasiewicz 		return USB_GADGET_DELAYED_STATUS;
34500a2430fSAndrzej Pietrasiewicz 
34600a2430fSAndrzej Pietrasiewicz 	default:
34700a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
34800a2430fSAndrzej Pietrasiewicz 	}
34900a2430fSAndrzej Pietrasiewicz }
35000a2430fSAndrzej Pietrasiewicz 
35100a2430fSAndrzej Pietrasiewicz static void
35200a2430fSAndrzej Pietrasiewicz uvc_function_disable(struct usb_function *f)
35300a2430fSAndrzej Pietrasiewicz {
35400a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
35500a2430fSAndrzej Pietrasiewicz 	struct v4l2_event v4l2_event;
35600a2430fSAndrzej Pietrasiewicz 
35700a2430fSAndrzej Pietrasiewicz 	INFO(f->config->cdev, "uvc_function_disable\n");
35800a2430fSAndrzej Pietrasiewicz 
35900a2430fSAndrzej Pietrasiewicz 	memset(&v4l2_event, 0, sizeof(v4l2_event));
36000a2430fSAndrzej Pietrasiewicz 	v4l2_event.type = UVC_EVENT_DISCONNECT;
36100a2430fSAndrzej Pietrasiewicz 	v4l2_event_queue(uvc->vdev, &v4l2_event);
36200a2430fSAndrzej Pietrasiewicz 
36300a2430fSAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
36400a2430fSAndrzej Pietrasiewicz }
36500a2430fSAndrzej Pietrasiewicz 
36600a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
36700a2430fSAndrzej Pietrasiewicz  * Connection / disconnection
36800a2430fSAndrzej Pietrasiewicz  */
36900a2430fSAndrzej Pietrasiewicz 
37000a2430fSAndrzej Pietrasiewicz void
37100a2430fSAndrzej Pietrasiewicz uvc_function_connect(struct uvc_device *uvc)
37200a2430fSAndrzej Pietrasiewicz {
37300a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
37400a2430fSAndrzej Pietrasiewicz 	int ret;
37500a2430fSAndrzej Pietrasiewicz 
37600a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_activate(&uvc->func)) < 0)
37700a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "UVC connect failed with %d\n", ret);
37800a2430fSAndrzej Pietrasiewicz }
37900a2430fSAndrzej Pietrasiewicz 
38000a2430fSAndrzej Pietrasiewicz void
38100a2430fSAndrzej Pietrasiewicz uvc_function_disconnect(struct uvc_device *uvc)
38200a2430fSAndrzej Pietrasiewicz {
38300a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
38400a2430fSAndrzej Pietrasiewicz 	int ret;
38500a2430fSAndrzej Pietrasiewicz 
38600a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
38700a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "UVC disconnect failed with %d\n", ret);
38800a2430fSAndrzej Pietrasiewicz }
38900a2430fSAndrzej Pietrasiewicz 
39000a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
39100a2430fSAndrzej Pietrasiewicz  * USB probe and disconnect
39200a2430fSAndrzej Pietrasiewicz  */
39300a2430fSAndrzej Pietrasiewicz 
39400a2430fSAndrzej Pietrasiewicz static int
39500a2430fSAndrzej Pietrasiewicz uvc_register_video(struct uvc_device *uvc)
39600a2430fSAndrzej Pietrasiewicz {
39700a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
39800a2430fSAndrzej Pietrasiewicz 	struct video_device *video;
39900a2430fSAndrzej Pietrasiewicz 
40000a2430fSAndrzej Pietrasiewicz 	/* TODO reference counting. */
40100a2430fSAndrzej Pietrasiewicz 	video = video_device_alloc();
40200a2430fSAndrzej Pietrasiewicz 	if (video == NULL)
40300a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
40400a2430fSAndrzej Pietrasiewicz 
40500a2430fSAndrzej Pietrasiewicz 	video->v4l2_dev = &uvc->v4l2_dev;
40600a2430fSAndrzej Pietrasiewicz 	video->fops = &uvc_v4l2_fops;
407a1d27a4bSLaurent Pinchart 	video->ioctl_ops = &uvc_v4l2_ioctl_ops;
40800a2430fSAndrzej Pietrasiewicz 	video->release = video_device_release;
409a1d27a4bSLaurent Pinchart 	video->vfl_dir = VFL_DIR_TX;
41000a2430fSAndrzej Pietrasiewicz 	strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
41100a2430fSAndrzej Pietrasiewicz 
41200a2430fSAndrzej Pietrasiewicz 	uvc->vdev = video;
41300a2430fSAndrzej Pietrasiewicz 	video_set_drvdata(video, uvc);
41400a2430fSAndrzej Pietrasiewicz 
41500a2430fSAndrzej Pietrasiewicz 	return video_register_device(video, VFL_TYPE_GRABBER, -1);
41600a2430fSAndrzej Pietrasiewicz }
41700a2430fSAndrzej Pietrasiewicz 
41800a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
41900a2430fSAndrzej Pietrasiewicz 	do { \
42000a2430fSAndrzej Pietrasiewicz 		memcpy(mem, desc, (desc)->bLength); \
42100a2430fSAndrzej Pietrasiewicz 		*(dst)++ = mem; \
42200a2430fSAndrzej Pietrasiewicz 		mem += (desc)->bLength; \
42300a2430fSAndrzej Pietrasiewicz 	} while (0);
42400a2430fSAndrzej Pietrasiewicz 
42500a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
42600a2430fSAndrzej Pietrasiewicz 	do { \
42700a2430fSAndrzej Pietrasiewicz 		const struct usb_descriptor_header * const *__src; \
42800a2430fSAndrzej Pietrasiewicz 		for (__src = src; *__src; ++__src) { \
42900a2430fSAndrzej Pietrasiewicz 			memcpy(mem, *__src, (*__src)->bLength); \
43000a2430fSAndrzej Pietrasiewicz 			*dst++ = mem; \
43100a2430fSAndrzej Pietrasiewicz 			mem += (*__src)->bLength; \
43200a2430fSAndrzej Pietrasiewicz 		} \
43300a2430fSAndrzej Pietrasiewicz 	} while (0)
43400a2430fSAndrzej Pietrasiewicz 
43500a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header ** __init
43600a2430fSAndrzej Pietrasiewicz uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
43700a2430fSAndrzej Pietrasiewicz {
43800a2430fSAndrzej Pietrasiewicz 	struct uvc_input_header_descriptor *uvc_streaming_header;
43900a2430fSAndrzej Pietrasiewicz 	struct uvc_header_descriptor *uvc_control_header;
44000a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_control_desc;
44100a2430fSAndrzej Pietrasiewicz 	const struct uvc_descriptor_header * const *uvc_streaming_cls;
44200a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *uvc_streaming_std;
44300a2430fSAndrzej Pietrasiewicz 	const struct usb_descriptor_header * const *src;
44400a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **dst;
44500a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header **hdr;
44600a2430fSAndrzej Pietrasiewicz 	unsigned int control_size;
44700a2430fSAndrzej Pietrasiewicz 	unsigned int streaming_size;
44800a2430fSAndrzej Pietrasiewicz 	unsigned int n_desc;
44900a2430fSAndrzej Pietrasiewicz 	unsigned int bytes;
45000a2430fSAndrzej Pietrasiewicz 	void *mem;
45100a2430fSAndrzej Pietrasiewicz 
45200a2430fSAndrzej Pietrasiewicz 	switch (speed) {
45300a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_SUPER:
45400a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.ss_control;
45500a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.ss_streaming;
45600a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_ss_streaming;
45700a2430fSAndrzej Pietrasiewicz 		break;
45800a2430fSAndrzej Pietrasiewicz 
45900a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_HIGH:
46000a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
46100a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.hs_streaming;
46200a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_hs_streaming;
46300a2430fSAndrzej Pietrasiewicz 		break;
46400a2430fSAndrzej Pietrasiewicz 
46500a2430fSAndrzej Pietrasiewicz 	case USB_SPEED_FULL:
46600a2430fSAndrzej Pietrasiewicz 	default:
46700a2430fSAndrzej Pietrasiewicz 		uvc_control_desc = uvc->desc.fs_control;
46800a2430fSAndrzej Pietrasiewicz 		uvc_streaming_cls = uvc->desc.fs_streaming;
46900a2430fSAndrzej Pietrasiewicz 		uvc_streaming_std = uvc_fs_streaming;
47000a2430fSAndrzej Pietrasiewicz 		break;
47100a2430fSAndrzej Pietrasiewicz 	}
47200a2430fSAndrzej Pietrasiewicz 
47300a2430fSAndrzej Pietrasiewicz 	/* Descriptors layout
47400a2430fSAndrzej Pietrasiewicz 	 *
47500a2430fSAndrzej Pietrasiewicz 	 * uvc_iad
47600a2430fSAndrzej Pietrasiewicz 	 * uvc_control_intf
47700a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC control descriptors
47800a2430fSAndrzej Pietrasiewicz 	 * uvc_control_ep
47900a2430fSAndrzej Pietrasiewicz 	 * uvc_control_cs_ep
48000a2430fSAndrzej Pietrasiewicz 	 * uvc_ss_control_comp (for SS only)
48100a2430fSAndrzej Pietrasiewicz 	 * uvc_streaming_intf_alt0
48200a2430fSAndrzej Pietrasiewicz 	 * Class-specific UVC streaming descriptors
48300a2430fSAndrzej Pietrasiewicz 	 * uvc_{fs|hs}_streaming
48400a2430fSAndrzej Pietrasiewicz 	 */
48500a2430fSAndrzej Pietrasiewicz 
48600a2430fSAndrzej Pietrasiewicz 	/* Count descriptors and compute their size. */
48700a2430fSAndrzej Pietrasiewicz 	control_size = 0;
48800a2430fSAndrzej Pietrasiewicz 	streaming_size = 0;
48900a2430fSAndrzej Pietrasiewicz 	bytes = uvc_iad.bLength + uvc_control_intf.bLength
49000a2430fSAndrzej Pietrasiewicz 	      + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
49100a2430fSAndrzej Pietrasiewicz 	      + uvc_streaming_intf_alt0.bLength;
49200a2430fSAndrzej Pietrasiewicz 
49300a2430fSAndrzej Pietrasiewicz 	if (speed == USB_SPEED_SUPER) {
49400a2430fSAndrzej Pietrasiewicz 		bytes += uvc_ss_control_comp.bLength;
49500a2430fSAndrzej Pietrasiewicz 		n_desc = 6;
49600a2430fSAndrzej Pietrasiewicz 	} else {
49700a2430fSAndrzej Pietrasiewicz 		n_desc = 5;
49800a2430fSAndrzej Pietrasiewicz 	}
49900a2430fSAndrzej Pietrasiewicz 
50000a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_control_desc;
50100a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
50200a2430fSAndrzej Pietrasiewicz 		control_size += (*src)->bLength;
50300a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
50400a2430fSAndrzej Pietrasiewicz 		n_desc++;
50500a2430fSAndrzej Pietrasiewicz 	}
50600a2430fSAndrzej Pietrasiewicz 	for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
50700a2430fSAndrzej Pietrasiewicz 	     *src; ++src) {
50800a2430fSAndrzej Pietrasiewicz 		streaming_size += (*src)->bLength;
50900a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
51000a2430fSAndrzej Pietrasiewicz 		n_desc++;
51100a2430fSAndrzej Pietrasiewicz 	}
51200a2430fSAndrzej Pietrasiewicz 	for (src = uvc_streaming_std; *src; ++src) {
51300a2430fSAndrzej Pietrasiewicz 		bytes += (*src)->bLength;
51400a2430fSAndrzej Pietrasiewicz 		n_desc++;
51500a2430fSAndrzej Pietrasiewicz 	}
51600a2430fSAndrzej Pietrasiewicz 
51700a2430fSAndrzej Pietrasiewicz 	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
51800a2430fSAndrzej Pietrasiewicz 	if (mem == NULL)
51900a2430fSAndrzej Pietrasiewicz 		return NULL;
52000a2430fSAndrzej Pietrasiewicz 
52100a2430fSAndrzej Pietrasiewicz 	hdr = mem;
52200a2430fSAndrzej Pietrasiewicz 	dst = mem;
52300a2430fSAndrzej Pietrasiewicz 	mem += (n_desc + 1) * sizeof(*src);
52400a2430fSAndrzej Pietrasiewicz 
52500a2430fSAndrzej Pietrasiewicz 	/* Copy the descriptors. */
52600a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
52700a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
52800a2430fSAndrzej Pietrasiewicz 
52900a2430fSAndrzej Pietrasiewicz 	uvc_control_header = mem;
53000a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
53100a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header **)uvc_control_desc);
53200a2430fSAndrzej Pietrasiewicz 	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
53300a2430fSAndrzej Pietrasiewicz 	uvc_control_header->bInCollection = 1;
53400a2430fSAndrzej Pietrasiewicz 	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
53500a2430fSAndrzej Pietrasiewicz 
53600a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
53700a2430fSAndrzej Pietrasiewicz 	if (speed == USB_SPEED_SUPER)
53800a2430fSAndrzej Pietrasiewicz 		UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
53900a2430fSAndrzej Pietrasiewicz 
54000a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
54100a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
54200a2430fSAndrzej Pietrasiewicz 
54300a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header = mem;
54400a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst,
54500a2430fSAndrzej Pietrasiewicz 		(const struct usb_descriptor_header**)uvc_streaming_cls);
54600a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
54700a2430fSAndrzej Pietrasiewicz 	uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
54800a2430fSAndrzej Pietrasiewicz 
54900a2430fSAndrzej Pietrasiewicz 	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
55000a2430fSAndrzej Pietrasiewicz 
55100a2430fSAndrzej Pietrasiewicz 	*dst = NULL;
55200a2430fSAndrzej Pietrasiewicz 	return hdr;
55300a2430fSAndrzej Pietrasiewicz }
55400a2430fSAndrzej Pietrasiewicz 
55500a2430fSAndrzej Pietrasiewicz static void
55600a2430fSAndrzej Pietrasiewicz uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
55700a2430fSAndrzej Pietrasiewicz {
55800a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
55900a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
56000a2430fSAndrzej Pietrasiewicz 
56100a2430fSAndrzej Pietrasiewicz 	INFO(cdev, "uvc_function_unbind\n");
56200a2430fSAndrzej Pietrasiewicz 
56300a2430fSAndrzej Pietrasiewicz 	video_unregister_device(uvc->vdev);
56400a2430fSAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
56500a2430fSAndrzej Pietrasiewicz 	uvc->control_ep->driver_data = NULL;
56600a2430fSAndrzej Pietrasiewicz 	uvc->video.ep->driver_data = NULL;
56700a2430fSAndrzej Pietrasiewicz 
56800a2430fSAndrzej Pietrasiewicz 	uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
56900a2430fSAndrzej Pietrasiewicz 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
57000a2430fSAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
57100a2430fSAndrzej Pietrasiewicz 
57200a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
57300a2430fSAndrzej Pietrasiewicz 
57400a2430fSAndrzej Pietrasiewicz 	kfree(uvc);
57500a2430fSAndrzej Pietrasiewicz }
57600a2430fSAndrzej Pietrasiewicz 
57700a2430fSAndrzej Pietrasiewicz static int __init
57800a2430fSAndrzej Pietrasiewicz uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
57900a2430fSAndrzej Pietrasiewicz {
58000a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev *cdev = c->cdev;
58100a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc = to_uvc(f);
58200a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_mult;
58300a2430fSAndrzej Pietrasiewicz 	unsigned int max_packet_size;
58400a2430fSAndrzej Pietrasiewicz 	struct usb_ep *ep;
58500a2430fSAndrzej Pietrasiewicz 	int ret = -EINVAL;
58600a2430fSAndrzej Pietrasiewicz 
58700a2430fSAndrzej Pietrasiewicz 	INFO(cdev, "uvc_function_bind\n");
58800a2430fSAndrzej Pietrasiewicz 
58900a2430fSAndrzej Pietrasiewicz 	/* Sanity check the streaming endpoint module parameters.
59000a2430fSAndrzej Pietrasiewicz 	 */
59100a2430fSAndrzej Pietrasiewicz 	streaming_interval = clamp(streaming_interval, 1U, 16U);
59200a2430fSAndrzej Pietrasiewicz 	streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
59300a2430fSAndrzej Pietrasiewicz 	streaming_maxburst = min(streaming_maxburst, 15U);
59400a2430fSAndrzej Pietrasiewicz 
59500a2430fSAndrzej Pietrasiewicz 	/* Fill in the FS/HS/SS Video Streaming specific descriptors from the
59600a2430fSAndrzej Pietrasiewicz 	 * module parameters.
59700a2430fSAndrzej Pietrasiewicz 	 *
59800a2430fSAndrzej Pietrasiewicz 	 * NOTE: We assume that the user knows what they are doing and won't
59900a2430fSAndrzej Pietrasiewicz 	 * give parameters that their UDC doesn't support.
60000a2430fSAndrzej Pietrasiewicz 	 */
60100a2430fSAndrzej Pietrasiewicz 	if (streaming_maxpacket <= 1024) {
60200a2430fSAndrzej Pietrasiewicz 		max_packet_mult = 1;
60300a2430fSAndrzej Pietrasiewicz 		max_packet_size = streaming_maxpacket;
60400a2430fSAndrzej Pietrasiewicz 	} else if (streaming_maxpacket <= 2048) {
60500a2430fSAndrzej Pietrasiewicz 		max_packet_mult = 2;
60600a2430fSAndrzej Pietrasiewicz 		max_packet_size = streaming_maxpacket / 2;
60700a2430fSAndrzej Pietrasiewicz 	} else {
60800a2430fSAndrzej Pietrasiewicz 		max_packet_mult = 3;
60900a2430fSAndrzej Pietrasiewicz 		max_packet_size = streaming_maxpacket / 3;
61000a2430fSAndrzej Pietrasiewicz 	}
61100a2430fSAndrzej Pietrasiewicz 
61200a2430fSAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
61300a2430fSAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bInterval = streaming_interval;
61400a2430fSAndrzej Pietrasiewicz 
61500a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
61600a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
61700a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bInterval = streaming_interval;
61800a2430fSAndrzej Pietrasiewicz 
61900a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
62000a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bInterval = streaming_interval;
62100a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
62200a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
62300a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_comp.wBytesPerInterval =
62400a2430fSAndrzej Pietrasiewicz 		max_packet_size * max_packet_mult * streaming_maxburst;
62500a2430fSAndrzej Pietrasiewicz 
62600a2430fSAndrzej Pietrasiewicz 	/* Allocate endpoints. */
62700a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
62800a2430fSAndrzej Pietrasiewicz 	if (!ep) {
62900a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "Unable to allocate control EP\n");
63000a2430fSAndrzej Pietrasiewicz 		goto error;
63100a2430fSAndrzej Pietrasiewicz 	}
63200a2430fSAndrzej Pietrasiewicz 	uvc->control_ep = ep;
63300a2430fSAndrzej Pietrasiewicz 	ep->driver_data = uvc;
63400a2430fSAndrzej Pietrasiewicz 
63500a2430fSAndrzej Pietrasiewicz 	if (gadget_is_superspeed(c->cdev->gadget))
63600a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
63700a2430fSAndrzej Pietrasiewicz 					  &uvc_ss_streaming_comp);
63800a2430fSAndrzej Pietrasiewicz 	else if (gadget_is_dualspeed(cdev->gadget))
63900a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
64000a2430fSAndrzej Pietrasiewicz 	else
64100a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
64200a2430fSAndrzej Pietrasiewicz 
64300a2430fSAndrzej Pietrasiewicz 	if (!ep) {
64400a2430fSAndrzej Pietrasiewicz 		INFO(cdev, "Unable to allocate streaming EP\n");
64500a2430fSAndrzej Pietrasiewicz 		goto error;
64600a2430fSAndrzej Pietrasiewicz 	}
64700a2430fSAndrzej Pietrasiewicz 	uvc->video.ep = ep;
64800a2430fSAndrzej Pietrasiewicz 	ep->driver_data = uvc;
64900a2430fSAndrzej Pietrasiewicz 
65000a2430fSAndrzej Pietrasiewicz 	uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
65100a2430fSAndrzej Pietrasiewicz 	uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
65200a2430fSAndrzej Pietrasiewicz 	uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
65300a2430fSAndrzej Pietrasiewicz 
65400a2430fSAndrzej Pietrasiewicz 	/* Allocate interface IDs. */
65500a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
65600a2430fSAndrzej Pietrasiewicz 		goto error;
65700a2430fSAndrzej Pietrasiewicz 	uvc_iad.bFirstInterface = ret;
65800a2430fSAndrzej Pietrasiewicz 	uvc_control_intf.bInterfaceNumber = ret;
65900a2430fSAndrzej Pietrasiewicz 	uvc->control_intf = ret;
66000a2430fSAndrzej Pietrasiewicz 
66100a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_interface_id(c, f)) < 0)
66200a2430fSAndrzej Pietrasiewicz 		goto error;
66300a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
66400a2430fSAndrzej Pietrasiewicz 	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
66500a2430fSAndrzej Pietrasiewicz 	uvc->streaming_intf = ret;
66600a2430fSAndrzej Pietrasiewicz 
66700a2430fSAndrzej Pietrasiewicz 	/* Copy descriptors */
66800a2430fSAndrzej Pietrasiewicz 	f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
66900a2430fSAndrzej Pietrasiewicz 	if (gadget_is_dualspeed(cdev->gadget))
67000a2430fSAndrzej Pietrasiewicz 		f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
67100a2430fSAndrzej Pietrasiewicz 	if (gadget_is_superspeed(c->cdev->gadget))
67200a2430fSAndrzej Pietrasiewicz 		f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
67300a2430fSAndrzej Pietrasiewicz 
67400a2430fSAndrzej Pietrasiewicz 	/* Preallocate control endpoint request. */
67500a2430fSAndrzej Pietrasiewicz 	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
67600a2430fSAndrzej Pietrasiewicz 	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
67700a2430fSAndrzej Pietrasiewicz 	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
67800a2430fSAndrzej Pietrasiewicz 		ret = -ENOMEM;
67900a2430fSAndrzej Pietrasiewicz 		goto error;
68000a2430fSAndrzej Pietrasiewicz 	}
68100a2430fSAndrzej Pietrasiewicz 
68200a2430fSAndrzej Pietrasiewicz 	uvc->control_req->buf = uvc->control_buf;
68300a2430fSAndrzej Pietrasiewicz 	uvc->control_req->complete = uvc_function_ep0_complete;
68400a2430fSAndrzej Pietrasiewicz 	uvc->control_req->context = uvc;
68500a2430fSAndrzej Pietrasiewicz 
68600a2430fSAndrzej Pietrasiewicz 	/* Avoid letting this gadget enumerate until the userspace server is
68700a2430fSAndrzej Pietrasiewicz 	 * active.
68800a2430fSAndrzej Pietrasiewicz 	 */
68900a2430fSAndrzej Pietrasiewicz 	if ((ret = usb_function_deactivate(f)) < 0)
69000a2430fSAndrzej Pietrasiewicz 		goto error;
69100a2430fSAndrzej Pietrasiewicz 
69200a2430fSAndrzej Pietrasiewicz 	if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
69300a2430fSAndrzej Pietrasiewicz 		printk(KERN_INFO "v4l2_device_register failed\n");
69400a2430fSAndrzej Pietrasiewicz 		goto error;
69500a2430fSAndrzej Pietrasiewicz 	}
69600a2430fSAndrzej Pietrasiewicz 
69700a2430fSAndrzej Pietrasiewicz 	/* Initialise video. */
6987ea95b11SAndrzej Pietrasiewicz 	ret = uvcg_video_init(&uvc->video);
69900a2430fSAndrzej Pietrasiewicz 	if (ret < 0)
70000a2430fSAndrzej Pietrasiewicz 		goto error;
70100a2430fSAndrzej Pietrasiewicz 
70200a2430fSAndrzej Pietrasiewicz 	/* Register a V4L2 device. */
70300a2430fSAndrzej Pietrasiewicz 	ret = uvc_register_video(uvc);
70400a2430fSAndrzej Pietrasiewicz 	if (ret < 0) {
70500a2430fSAndrzej Pietrasiewicz 		printk(KERN_INFO "Unable to register video device\n");
70600a2430fSAndrzej Pietrasiewicz 		goto error;
70700a2430fSAndrzej Pietrasiewicz 	}
70800a2430fSAndrzej Pietrasiewicz 
70900a2430fSAndrzej Pietrasiewicz 	return 0;
71000a2430fSAndrzej Pietrasiewicz 
71100a2430fSAndrzej Pietrasiewicz error:
71200a2430fSAndrzej Pietrasiewicz 	v4l2_device_unregister(&uvc->v4l2_dev);
71300a2430fSAndrzej Pietrasiewicz 	if (uvc->vdev)
71400a2430fSAndrzej Pietrasiewicz 		video_device_release(uvc->vdev);
71500a2430fSAndrzej Pietrasiewicz 
71600a2430fSAndrzej Pietrasiewicz 	if (uvc->control_ep)
71700a2430fSAndrzej Pietrasiewicz 		uvc->control_ep->driver_data = NULL;
71800a2430fSAndrzej Pietrasiewicz 	if (uvc->video.ep)
71900a2430fSAndrzej Pietrasiewicz 		uvc->video.ep->driver_data = NULL;
72000a2430fSAndrzej Pietrasiewicz 
721e7379857SAndrzej Pietrasiewicz 	if (uvc->control_req)
72200a2430fSAndrzej Pietrasiewicz 		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
72300a2430fSAndrzej Pietrasiewicz 	kfree(uvc->control_buf);
72400a2430fSAndrzej Pietrasiewicz 
72500a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
72600a2430fSAndrzej Pietrasiewicz 	return ret;
72700a2430fSAndrzej Pietrasiewicz }
72800a2430fSAndrzej Pietrasiewicz 
72900a2430fSAndrzej Pietrasiewicz /* --------------------------------------------------------------------------
73000a2430fSAndrzej Pietrasiewicz  * USB gadget function
73100a2430fSAndrzej Pietrasiewicz  */
73200a2430fSAndrzej Pietrasiewicz 
73300a2430fSAndrzej Pietrasiewicz /**
73400a2430fSAndrzej Pietrasiewicz  * uvc_bind_config - add a UVC function to a configuration
73500a2430fSAndrzej Pietrasiewicz  * @c: the configuration to support the UVC instance
73600a2430fSAndrzej Pietrasiewicz  * Context: single threaded during gadget setup
73700a2430fSAndrzej Pietrasiewicz  *
73800a2430fSAndrzej Pietrasiewicz  * Returns zero on success, else negative errno.
73900a2430fSAndrzej Pietrasiewicz  *
74000a2430fSAndrzej Pietrasiewicz  * Caller must have called @uvc_setup(). Caller is also responsible for
74100a2430fSAndrzej Pietrasiewicz  * calling @uvc_cleanup() before module unload.
74200a2430fSAndrzej Pietrasiewicz  */
74300a2430fSAndrzej Pietrasiewicz int __init
74400a2430fSAndrzej Pietrasiewicz uvc_bind_config(struct usb_configuration *c,
74500a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *fs_control,
74600a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *ss_control,
74700a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *fs_streaming,
74800a2430fSAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *hs_streaming,
749efb540c8SAndrzej Pietrasiewicz 		const struct uvc_descriptor_header * const *ss_streaming,
750efb540c8SAndrzej Pietrasiewicz 		unsigned int stream_interv, unsigned int stream_maxpkt,
751efb540c8SAndrzej Pietrasiewicz 		unsigned int stream_maxburst, unsigned int trace)
75200a2430fSAndrzej Pietrasiewicz {
75300a2430fSAndrzej Pietrasiewicz 	struct uvc_device *uvc;
75400a2430fSAndrzej Pietrasiewicz 	int ret = 0;
75500a2430fSAndrzej Pietrasiewicz 
75600a2430fSAndrzej Pietrasiewicz 	/* TODO Check if the USB device controller supports the required
75700a2430fSAndrzej Pietrasiewicz 	 * features.
75800a2430fSAndrzej Pietrasiewicz 	 */
75900a2430fSAndrzej Pietrasiewicz 	if (!gadget_is_dualspeed(c->cdev->gadget))
76000a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
76100a2430fSAndrzej Pietrasiewicz 
76200a2430fSAndrzej Pietrasiewicz 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
76300a2430fSAndrzej Pietrasiewicz 	if (uvc == NULL)
76400a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
76500a2430fSAndrzej Pietrasiewicz 
76600a2430fSAndrzej Pietrasiewicz 	uvc->state = UVC_STATE_DISCONNECTED;
76700a2430fSAndrzej Pietrasiewicz 
76800a2430fSAndrzej Pietrasiewicz 	/* Validate the descriptors. */
76900a2430fSAndrzej Pietrasiewicz 	if (fs_control == NULL || fs_control[0] == NULL ||
77000a2430fSAndrzej Pietrasiewicz 	    fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
77100a2430fSAndrzej Pietrasiewicz 		goto error;
77200a2430fSAndrzej Pietrasiewicz 
77300a2430fSAndrzej Pietrasiewicz 	if (ss_control == NULL || ss_control[0] == NULL ||
77400a2430fSAndrzej Pietrasiewicz 	    ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
77500a2430fSAndrzej Pietrasiewicz 		goto error;
77600a2430fSAndrzej Pietrasiewicz 
77700a2430fSAndrzej Pietrasiewicz 	if (fs_streaming == NULL || fs_streaming[0] == NULL ||
77800a2430fSAndrzej Pietrasiewicz 	    fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
77900a2430fSAndrzej Pietrasiewicz 		goto error;
78000a2430fSAndrzej Pietrasiewicz 
78100a2430fSAndrzej Pietrasiewicz 	if (hs_streaming == NULL || hs_streaming[0] == NULL ||
78200a2430fSAndrzej Pietrasiewicz 	    hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
78300a2430fSAndrzej Pietrasiewicz 		goto error;
78400a2430fSAndrzej Pietrasiewicz 
78500a2430fSAndrzej Pietrasiewicz 	if (ss_streaming == NULL || ss_streaming[0] == NULL ||
78600a2430fSAndrzej Pietrasiewicz 	    ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
78700a2430fSAndrzej Pietrasiewicz 		goto error;
78800a2430fSAndrzej Pietrasiewicz 
789efb540c8SAndrzej Pietrasiewicz 	streaming_interval = stream_interv;
790efb540c8SAndrzej Pietrasiewicz 	streaming_maxpacket = stream_maxpkt;
791efb540c8SAndrzej Pietrasiewicz 	streaming_maxburst = stream_maxburst;
792efb540c8SAndrzej Pietrasiewicz 	uvc_gadget_trace_param = trace;
79300a2430fSAndrzej Pietrasiewicz 	uvc->desc.fs_control = fs_control;
79400a2430fSAndrzej Pietrasiewicz 	uvc->desc.ss_control = ss_control;
79500a2430fSAndrzej Pietrasiewicz 	uvc->desc.fs_streaming = fs_streaming;
79600a2430fSAndrzej Pietrasiewicz 	uvc->desc.hs_streaming = hs_streaming;
79700a2430fSAndrzej Pietrasiewicz 	uvc->desc.ss_streaming = ss_streaming;
79800a2430fSAndrzej Pietrasiewicz 
79900a2430fSAndrzej Pietrasiewicz 	/* String descriptors are global, we only need to allocate string IDs
80000a2430fSAndrzej Pietrasiewicz 	 * for the first UVC function. UVC functions beyond the first (if any)
80100a2430fSAndrzej Pietrasiewicz 	 * will reuse the same IDs.
80200a2430fSAndrzej Pietrasiewicz 	 */
80300a2430fSAndrzej Pietrasiewicz 	if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
80400a2430fSAndrzej Pietrasiewicz 		ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
80500a2430fSAndrzej Pietrasiewicz 		if (ret)
80600a2430fSAndrzej Pietrasiewicz 			goto error;
80700a2430fSAndrzej Pietrasiewicz 		uvc_iad.iFunction =
80800a2430fSAndrzej Pietrasiewicz 			uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
80900a2430fSAndrzej Pietrasiewicz 		uvc_control_intf.iInterface =
81000a2430fSAndrzej Pietrasiewicz 			uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
81100a2430fSAndrzej Pietrasiewicz 		ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
81200a2430fSAndrzej Pietrasiewicz 		uvc_streaming_intf_alt0.iInterface = ret;
81300a2430fSAndrzej Pietrasiewicz 		uvc_streaming_intf_alt1.iInterface = ret;
81400a2430fSAndrzej Pietrasiewicz 	}
81500a2430fSAndrzej Pietrasiewicz 
81600a2430fSAndrzej Pietrasiewicz 	/* Register the function. */
81700a2430fSAndrzej Pietrasiewicz 	uvc->func.name = "uvc";
81800a2430fSAndrzej Pietrasiewicz 	uvc->func.strings = uvc_function_strings;
81900a2430fSAndrzej Pietrasiewicz 	uvc->func.bind = uvc_function_bind;
82000a2430fSAndrzej Pietrasiewicz 	uvc->func.unbind = uvc_function_unbind;
82100a2430fSAndrzej Pietrasiewicz 	uvc->func.get_alt = uvc_function_get_alt;
82200a2430fSAndrzej Pietrasiewicz 	uvc->func.set_alt = uvc_function_set_alt;
82300a2430fSAndrzej Pietrasiewicz 	uvc->func.disable = uvc_function_disable;
82400a2430fSAndrzej Pietrasiewicz 	uvc->func.setup = uvc_function_setup;
82500a2430fSAndrzej Pietrasiewicz 
82600a2430fSAndrzej Pietrasiewicz 	ret = usb_add_function(c, &uvc->func);
82700a2430fSAndrzej Pietrasiewicz 	if (ret)
82800a2430fSAndrzej Pietrasiewicz 		kfree(uvc);
82900a2430fSAndrzej Pietrasiewicz 
83000a2430fSAndrzej Pietrasiewicz 	return ret;
83100a2430fSAndrzej Pietrasiewicz 
83200a2430fSAndrzej Pietrasiewicz error:
83300a2430fSAndrzej Pietrasiewicz 	kfree(uvc);
83400a2430fSAndrzej Pietrasiewicz 	return ret;
83500a2430fSAndrzej Pietrasiewicz }
83600a2430fSAndrzej Pietrasiewicz 
83700a2430fSAndrzej Pietrasiewicz 
838