2 * uvc_gadget.c -- USB Video Class Gadget driver
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/video.h>
22 #include <linux/vmalloc.h>
23 #include <linux/wait.h>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-event.h>
30 unsigned int uvc_gadget_trace_param;
32 /*-------------------------------------------------------------------------*/
34 /* module parameters specific to the Video streaming endpoint */
35 static unsigned streaming_interval = 1;
36 module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
37 MODULE_PARM_DESC(streaming_interval, "1 - 16");
39 static unsigned streaming_maxpacket = 1024;
40 module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
41 MODULE_PARM_DESC(streaming_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
43 static unsigned streaming_mult;
44 module_param(streaming_mult, uint, S_IRUGO|S_IWUSR);
45 MODULE_PARM_DESC(streaming_mult, "0 - 2 (hs/ss only)");
47 static unsigned streaming_maxburst;
48 module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
49 MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
51 /* --------------------------------------------------------------------------
52 * Function descriptors
55 /* string IDs are assigned dynamically */
57 #define UVC_STRING_ASSOCIATION_IDX 0
58 #define UVC_STRING_CONTROL_IDX 1
59 #define UVC_STRING_STREAMING_IDX 2
61 static struct usb_string uvc_en_us_strings[] = {
62 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
63 [UVC_STRING_CONTROL_IDX].s = "Video Control",
64 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
68 static struct usb_gadget_strings uvc_stringtab = {
69 .language = 0x0409, /* en-us */
70 .strings = uvc_en_us_strings,
73 static struct usb_gadget_strings *uvc_function_strings[] = {
78 #define UVC_INTF_VIDEO_CONTROL 0
79 #define UVC_INTF_VIDEO_STREAMING 1
81 #define STATUS_BYTECOUNT 16 /* 16 bytes status */
83 static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
84 .bLength = sizeof(uvc_iad),
85 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
88 .bFunctionClass = USB_CLASS_VIDEO,
89 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
90 .bFunctionProtocol = 0x00,
94 static struct usb_interface_descriptor uvc_control_intf __initdata = {
95 .bLength = USB_DT_INTERFACE_SIZE,
96 .bDescriptorType = USB_DT_INTERFACE,
97 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
98 .bAlternateSetting = 0,
100 .bInterfaceClass = USB_CLASS_VIDEO,
101 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
102 .bInterfaceProtocol = 0x00,
106 static struct usb_endpoint_descriptor uvc_fs_control_ep __initdata = {
107 .bLength = USB_DT_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_ENDPOINT,
109 .bEndpointAddress = USB_DIR_IN,
110 .bmAttributes = USB_ENDPOINT_XFER_INT,
111 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
115 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
116 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
117 .bDescriptorType = USB_DT_CS_ENDPOINT,
118 .bDescriptorSubType = UVC_EP_INTERRUPT,
119 .wMaxTransferSize = cpu_to_le16(STATUS_BYTECOUNT),
122 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
123 .bLength = USB_DT_INTERFACE_SIZE,
124 .bDescriptorType = USB_DT_INTERFACE,
125 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
126 .bAlternateSetting = 0,
128 .bInterfaceClass = USB_CLASS_VIDEO,
129 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
130 .bInterfaceProtocol = 0x00,
134 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
135 .bLength = USB_DT_INTERFACE_SIZE,
136 .bDescriptorType = USB_DT_INTERFACE,
137 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
138 .bAlternateSetting = 1,
140 .bInterfaceClass = USB_CLASS_VIDEO,
141 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
142 .bInterfaceProtocol = 0x00,
146 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
147 .bLength = USB_DT_ENDPOINT_SIZE,
148 .bDescriptorType = USB_DT_ENDPOINT,
149 .bEndpointAddress = USB_DIR_IN,
150 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
151 .wMaxPacketSize = cpu_to_le16(512),
155 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
156 .bLength = USB_DT_ENDPOINT_SIZE,
157 .bDescriptorType = USB_DT_ENDPOINT,
158 .bEndpointAddress = USB_DIR_IN,
159 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
160 .wMaxPacketSize = cpu_to_le16(1024),
164 /* super speed support */
165 static struct usb_endpoint_descriptor uvc_ss_control_ep __initdata = {
166 .bLength = USB_DT_ENDPOINT_SIZE,
167 .bDescriptorType = USB_DT_ENDPOINT,
169 .bEndpointAddress = USB_DIR_IN,
170 .bmAttributes = USB_ENDPOINT_XFER_INT,
171 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
175 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
176 .bLength = sizeof uvc_ss_control_comp,
177 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
179 /* the following 3 values can be tweaked if necessary */
180 /* .bMaxBurst = 0, */
181 /* .bmAttributes = 0, */
182 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
185 static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
186 .bLength = USB_DT_ENDPOINT_SIZE,
187 .bDescriptorType = USB_DT_ENDPOINT,
189 .bEndpointAddress = USB_DIR_IN,
190 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
191 .wMaxPacketSize = cpu_to_le16(1024),
195 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
196 .bLength = sizeof uvc_ss_streaming_comp,
197 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
199 /* the following 3 values can be tweaked if necessary */
202 .wBytesPerInterval = cpu_to_le16(1024),
205 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
206 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
207 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
211 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
212 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
213 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
217 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
218 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
219 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
220 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
224 /* --------------------------------------------------------------------------
229 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
231 struct uvc_device *uvc = req->context;
232 struct v4l2_event v4l2_event;
233 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
235 if (uvc->event_setup_out) {
236 uvc->event_setup_out = 0;
238 memset(&v4l2_event, 0, sizeof(v4l2_event));
239 v4l2_event.type = UVC_EVENT_DATA;
240 uvc_event->data.length = req->actual;
241 memcpy(&uvc_event->data.data, req->buf, req->actual);
242 v4l2_event_queue(uvc->vdev, &v4l2_event);
247 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
249 struct uvc_device *uvc = to_uvc(f);
250 struct v4l2_event v4l2_event;
251 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
253 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
254 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
255 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
258 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
259 INFO(f->config->cdev, "invalid request type\n");
263 /* Stall too big requests. */
264 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
267 memset(&v4l2_event, 0, sizeof(v4l2_event));
268 v4l2_event.type = UVC_EVENT_SETUP;
269 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
270 v4l2_event_queue(uvc->vdev, &v4l2_event);
276 uvc_function_get_alt(struct usb_function *f, unsigned interface)
278 struct uvc_device *uvc = to_uvc(f);
280 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
282 if (interface == uvc->control_intf)
284 else if (interface != uvc->streaming_intf)
287 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
291 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
293 struct uvc_device *uvc = to_uvc(f);
294 struct v4l2_event v4l2_event;
295 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
298 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
300 if (interface == uvc->control_intf) {
304 if (uvc->state == UVC_STATE_DISCONNECTED) {
305 memset(&v4l2_event, 0, sizeof(v4l2_event));
306 v4l2_event.type = UVC_EVENT_CONNECT;
307 uvc_event->speed = f->config->cdev->gadget->speed;
308 v4l2_event_queue(uvc->vdev, &v4l2_event);
310 uvc->state = UVC_STATE_CONNECTED;
316 if (interface != uvc->streaming_intf)
320 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
321 return alt ? -EINVAL : 0;
326 if (uvc->state != UVC_STATE_STREAMING)
330 usb_ep_disable(uvc->video.ep);
332 memset(&v4l2_event, 0, sizeof(v4l2_event));
333 v4l2_event.type = UVC_EVENT_STREAMOFF;
334 v4l2_event_queue(uvc->vdev, &v4l2_event);
336 uvc->state = UVC_STATE_CONNECTED;
340 if (uvc->state != UVC_STATE_CONNECTED)
344 ret = config_ep_by_speed(f->config->cdev->gadget,
345 &(uvc->func), uvc->video.ep);
348 usb_ep_enable(uvc->video.ep);
351 memset(&v4l2_event, 0, sizeof(v4l2_event));
352 v4l2_event.type = UVC_EVENT_STREAMON;
353 v4l2_event_queue(uvc->vdev, &v4l2_event);
355 uvc->state = UVC_STATE_STREAMING;
366 uvc_function_disable(struct usb_function *f)
368 struct uvc_device *uvc = to_uvc(f);
369 struct v4l2_event v4l2_event;
371 INFO(f->config->cdev, "uvc_function_disable\n");
373 memset(&v4l2_event, 0, sizeof(v4l2_event));
374 v4l2_event.type = UVC_EVENT_DISCONNECT;
375 v4l2_event_queue(uvc->vdev, &v4l2_event);
377 uvc->state = UVC_STATE_DISCONNECTED;
380 /* --------------------------------------------------------------------------
381 * Connection / disconnection
385 uvc_function_connect(struct uvc_device *uvc)
387 struct usb_composite_dev *cdev = uvc->func.config->cdev;
390 if ((ret = usb_function_activate(&uvc->func)) < 0)
391 INFO(cdev, "UVC connect failed with %d\n", ret);
395 uvc_function_disconnect(struct uvc_device *uvc)
397 struct usb_composite_dev *cdev = uvc->func.config->cdev;
400 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
401 INFO(cdev, "UVC disconnect failed with %d\n", ret);
404 /* --------------------------------------------------------------------------
405 * USB probe and disconnect
409 uvc_register_video(struct uvc_device *uvc)
411 struct usb_composite_dev *cdev = uvc->func.config->cdev;
412 struct video_device *video;
414 /* TODO reference counting. */
415 video = video_device_alloc();
419 video->parent = &cdev->gadget->dev;
420 video->fops = &uvc_v4l2_fops;
421 video->release = video_device_release;
422 strncpy(video->name, cdev->gadget->name, sizeof(video->name));
425 video_set_drvdata(video, uvc);
427 return video_register_device(video, VFL_TYPE_GRABBER, -1);
430 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
432 memcpy(mem, desc, (desc)->bLength); \
434 mem += (desc)->bLength; \
437 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
439 const struct usb_descriptor_header * const *__src; \
440 for (__src = src; *__src; ++__src) { \
441 memcpy(mem, *__src, (*__src)->bLength); \
443 mem += (*__src)->bLength; \
447 static struct usb_descriptor_header ** __init
448 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
450 struct uvc_input_header_descriptor *uvc_streaming_header;
451 struct uvc_header_descriptor *uvc_control_header;
452 const struct uvc_descriptor_header * const *uvc_control_desc;
453 const struct uvc_descriptor_header * const *uvc_streaming_cls;
454 const struct usb_descriptor_header * const *uvc_streaming_std;
455 const struct usb_descriptor_header * const *src;
456 static struct usb_endpoint_descriptor *uvc_control_ep;
457 struct usb_descriptor_header **dst;
458 struct usb_descriptor_header **hdr;
459 unsigned int control_size;
460 unsigned int streaming_size;
466 case USB_SPEED_SUPER:
467 uvc_control_desc = uvc->desc.ss_control;
468 uvc_streaming_cls = uvc->desc.ss_streaming;
469 uvc_streaming_std = uvc_ss_streaming;
470 uvc_control_ep = &uvc_ss_control_ep;
474 uvc_control_desc = uvc->desc.fs_control;
475 uvc_streaming_cls = uvc->desc.hs_streaming;
476 uvc_streaming_std = uvc_hs_streaming;
477 uvc_control_ep = &uvc_fs_control_ep;
482 uvc_control_desc = uvc->desc.fs_control;
483 uvc_streaming_cls = uvc->desc.fs_streaming;
484 uvc_streaming_std = uvc_fs_streaming;
485 uvc_control_ep = &uvc_fs_control_ep;
489 /* Descriptors layout
493 * Class-specific UVC control descriptors
496 * uvc_streaming_intf_alt0
497 * Class-specific UVC streaming descriptors
498 * uvc_{fs|hs}_streaming
501 /* Count descriptors and compute their size. */
504 bytes = uvc_iad.bLength + uvc_control_intf.bLength
505 + uvc_control_ep->bLength + uvc_control_cs_ep.bLength
506 + uvc_streaming_intf_alt0.bLength;
508 if (speed == USB_SPEED_SUPER) {
509 bytes += uvc_ss_control_comp.bLength;
515 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
517 control_size += (*src)->bLength;
518 bytes += (*src)->bLength;
521 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
523 streaming_size += (*src)->bLength;
524 bytes += (*src)->bLength;
527 for (src = uvc_streaming_std; *src; ++src) {
528 bytes += (*src)->bLength;
532 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
538 mem += (n_desc + 1) * sizeof(*src);
540 /* Copy the descriptors. */
541 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
542 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
544 uvc_control_header = mem;
545 UVC_COPY_DESCRIPTORS(mem, dst,
546 (const struct usb_descriptor_header **)uvc_control_desc);
547 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
548 uvc_control_header->bInCollection = 1;
549 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
551 UVC_COPY_DESCRIPTOR(mem, dst, uvc_control_ep);
552 if (speed == USB_SPEED_SUPER)
553 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
555 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
556 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
558 uvc_streaming_header = mem;
559 UVC_COPY_DESCRIPTORS(mem, dst,
560 (const struct usb_descriptor_header**)uvc_streaming_cls);
561 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
562 uvc_streaming_header->bEndpointAddress =
563 uvc_fs_streaming_ep.bEndpointAddress;
565 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
572 uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
574 struct usb_composite_dev *cdev = c->cdev;
575 struct uvc_device *uvc = to_uvc(f);
577 INFO(cdev, "uvc_function_unbind\n");
579 video_unregister_device(uvc->vdev);
580 uvc->control_ep->driver_data = NULL;
581 uvc->video.ep->driver_data = NULL;
583 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0;
584 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
585 kfree(uvc->control_buf);
587 usb_free_all_descriptors(f);
593 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
595 struct usb_composite_dev *cdev = c->cdev;
596 struct uvc_device *uvc = to_uvc(f);
600 INFO(cdev, "uvc_function_bind\n");
602 /* sanity check the streaming endpoint module parameters */
603 if (streaming_interval < 1)
604 streaming_interval = 1;
605 if (streaming_interval > 16)
606 streaming_interval = 16;
607 if (streaming_mult > 2)
609 if (streaming_maxburst > 15)
610 streaming_maxburst = 15;
613 * fill in the FS video streaming specific descriptors from the
616 uvc_fs_streaming_ep.wMaxPacketSize = streaming_maxpacket > 1023 ?
617 1023 : streaming_maxpacket;
618 uvc_fs_streaming_ep.bInterval = streaming_interval;
620 /* Allocate endpoints. */
621 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_control_ep);
623 INFO(cdev, "Unable to allocate control EP\n");
626 uvc->control_ep = ep;
627 ep->driver_data = uvc;
629 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
631 INFO(cdev, "Unable to allocate streaming EP\n");
635 ep->driver_data = uvc;
637 /* Allocate interface IDs. */
638 if ((ret = usb_interface_id(c, f)) < 0)
640 uvc_iad.bFirstInterface = ret;
641 uvc_control_intf.bInterfaceNumber = ret;
642 uvc->control_intf = ret;
644 if ((ret = usb_interface_id(c, f)) < 0)
646 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
647 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
648 uvc->streaming_intf = ret;
650 /* sanity check the streaming endpoint module parameters */
651 if (streaming_maxpacket > 1024)
652 streaming_maxpacket = 1024;
654 * Fill in the HS descriptors from the module parameters for the Video
655 * Streaming endpoint.
656 * NOTE: We assume that the user knows what they are doing and won't
657 * give parameters that their UDC doesn't support.
659 uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket;
660 uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11;
661 uvc_hs_streaming_ep.bInterval = streaming_interval;
662 uvc_hs_streaming_ep.bEndpointAddress =
663 uvc_fs_streaming_ep.bEndpointAddress;
666 * Fill in the SS descriptors from the module parameters for the Video
667 * Streaming endpoint.
668 * NOTE: We assume that the user knows what they are doing and won't
669 * give parameters that their UDC doesn't support.
671 uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket;
672 uvc_ss_streaming_ep.bInterval = streaming_interval;
673 uvc_ss_streaming_comp.bmAttributes = streaming_mult;
674 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
675 uvc_ss_streaming_comp.wBytesPerInterval =
676 streaming_maxpacket * (streaming_mult + 1) *
677 (streaming_maxburst + 1);
678 uvc_ss_streaming_ep.bEndpointAddress =
679 uvc_fs_streaming_ep.bEndpointAddress;
681 /* Copy descriptors */
682 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
683 if (gadget_is_dualspeed(cdev->gadget))
684 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
685 if (gadget_is_superspeed(c->cdev->gadget))
686 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
688 /* Preallocate control endpoint request. */
689 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
690 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
691 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
696 uvc->control_req->buf = uvc->control_buf;
697 uvc->control_req->complete = uvc_function_ep0_complete;
698 uvc->control_req->context = uvc;
700 /* Avoid letting this gadget enumerate until the userspace server is
703 if ((ret = usb_function_deactivate(f)) < 0)
706 /* Initialise video. */
707 ret = uvc_video_init(&uvc->video);
711 /* Register a V4L2 device. */
712 ret = uvc_register_video(uvc);
714 printk(KERN_INFO "Unable to register video device\n");
722 video_device_release(uvc->vdev);
725 uvc->control_ep->driver_data = NULL;
727 uvc->video.ep->driver_data = NULL;
729 if (uvc->control_req) {
730 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
731 kfree(uvc->control_buf);
734 usb_free_all_descriptors(f);
738 /* --------------------------------------------------------------------------
739 * USB gadget function
743 * uvc_bind_config - add a UVC function to a configuration
744 * @c: the configuration to support the UVC instance
745 * Context: single threaded during gadget setup
747 * Returns zero on success, else negative errno.
749 * Caller must have called @uvc_setup(). Caller is also responsible for
750 * calling @uvc_cleanup() before module unload.
753 uvc_bind_config(struct usb_configuration *c,
754 const struct uvc_descriptor_header * const *fs_control,
755 const struct uvc_descriptor_header * const *ss_control,
756 const struct uvc_descriptor_header * const *fs_streaming,
757 const struct uvc_descriptor_header * const *hs_streaming,
758 const struct uvc_descriptor_header * const *ss_streaming)
760 struct uvc_device *uvc;
763 /* TODO Check if the USB device controller supports the required
766 if (!gadget_is_dualspeed(c->cdev->gadget))
769 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
773 uvc->state = UVC_STATE_DISCONNECTED;
775 /* Validate the descriptors. */
776 if (fs_control == NULL || fs_control[0] == NULL ||
777 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
780 if (ss_control == NULL || ss_control[0] == NULL ||
781 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
784 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
785 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
788 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
789 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
792 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
793 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
796 uvc->desc.fs_control = fs_control;
797 uvc->desc.ss_control = ss_control;
798 uvc->desc.fs_streaming = fs_streaming;
799 uvc->desc.hs_streaming = hs_streaming;
800 uvc->desc.ss_streaming = ss_streaming;
802 /* Allocate string descriptor numbers. */
803 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
804 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
808 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id;
809 uvc_control_intf.iInterface =
810 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
811 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
812 uvc_streaming_intf_alt0.iInterface = ret;
813 uvc_streaming_intf_alt1.iInterface = ret;
816 /* Register the function. */
817 uvc->func.name = "uvc";
818 uvc->func.strings = uvc_function_strings;
819 uvc->func.bind = uvc_function_bind;
820 uvc->func.unbind = uvc_function_unbind;
821 uvc->func.get_alt = uvc_function_get_alt;
822 uvc->func.set_alt = uvc_function_set_alt;
823 uvc->func.disable = uvc_function_disable;
824 uvc->func.setup = uvc_function_setup;
826 ret = usb_add_function(c, &uvc->func);
837 module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
838 MODULE_PARM_DESC(trace, "Trace level bitmask");