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/string.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/video.h>
23 #include <linux/vmalloc.h>
24 #include <linux/wait.h>
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-event.h>
31 unsigned int uvc_gadget_trace_param;
33 /*-------------------------------------------------------------------------*/
35 /* module parameters specific to the Video streaming endpoint */
36 static unsigned int streaming_interval = 1;
37 module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
38 MODULE_PARM_DESC(streaming_interval, "1 - 16");
40 static unsigned int streaming_maxpacket = 1024;
41 module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
42 MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
44 static unsigned int streaming_maxburst;
45 module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
46 MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
48 /* --------------------------------------------------------------------------
49 * Function descriptors
52 /* string IDs are assigned dynamically */
54 #define UVC_STRING_CONTROL_IDX 0
55 #define UVC_STRING_STREAMING_IDX 1
57 static struct usb_string uvc_en_us_strings[] = {
58 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
59 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
63 static struct usb_gadget_strings uvc_stringtab = {
64 .language = 0x0409, /* en-us */
65 .strings = uvc_en_us_strings,
68 static struct usb_gadget_strings *uvc_function_strings[] = {
73 #define UVC_INTF_VIDEO_CONTROL 0
74 #define UVC_INTF_VIDEO_STREAMING 1
76 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
78 static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
79 .bLength = sizeof(uvc_iad),
80 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
83 .bFunctionClass = USB_CLASS_VIDEO,
84 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
85 .bFunctionProtocol = 0x00,
89 static struct usb_interface_descriptor uvc_control_intf __initdata = {
90 .bLength = USB_DT_INTERFACE_SIZE,
91 .bDescriptorType = USB_DT_INTERFACE,
92 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
93 .bAlternateSetting = 0,
95 .bInterfaceClass = USB_CLASS_VIDEO,
96 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
97 .bInterfaceProtocol = 0x00,
101 static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
102 .bLength = USB_DT_ENDPOINT_SIZE,
103 .bDescriptorType = USB_DT_ENDPOINT,
104 .bEndpointAddress = USB_DIR_IN,
105 .bmAttributes = USB_ENDPOINT_XFER_INT,
106 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
110 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
111 .bLength = sizeof(uvc_ss_control_comp),
112 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
113 /* The following 3 values can be tweaked if necessary. */
116 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
119 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
120 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
121 .bDescriptorType = USB_DT_CS_ENDPOINT,
122 .bDescriptorSubType = UVC_EP_INTERRUPT,
123 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
126 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
127 .bLength = USB_DT_INTERFACE_SIZE,
128 .bDescriptorType = USB_DT_INTERFACE,
129 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
130 .bAlternateSetting = 0,
132 .bInterfaceClass = USB_CLASS_VIDEO,
133 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
134 .bInterfaceProtocol = 0x00,
138 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
139 .bLength = USB_DT_INTERFACE_SIZE,
140 .bDescriptorType = USB_DT_INTERFACE,
141 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
142 .bAlternateSetting = 1,
144 .bInterfaceClass = USB_CLASS_VIDEO,
145 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
146 .bInterfaceProtocol = 0x00,
150 static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bEndpointAddress = USB_DIR_IN,
154 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
155 | USB_ENDPOINT_XFER_ISOC,
156 /* The wMaxPacketSize and bInterval values will be initialized from
163 static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
168 | USB_ENDPOINT_XFER_ISOC,
169 /* The wMaxPacketSize and bInterval values will be initialized from
176 static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
177 .bLength = USB_DT_ENDPOINT_SIZE,
178 .bDescriptorType = USB_DT_ENDPOINT,
180 .bEndpointAddress = USB_DIR_IN,
181 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
182 | USB_ENDPOINT_XFER_ISOC,
183 /* The wMaxPacketSize and bInterval values will be initialized from
190 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
191 .bLength = sizeof(uvc_ss_streaming_comp),
192 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
193 /* The following 3 values can be tweaked if necessary. */
196 .wBytesPerInterval = cpu_to_le16(1024),
199 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
200 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
201 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
205 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
206 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
207 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
211 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
212 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
213 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
214 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
218 /* --------------------------------------------------------------------------
223 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
225 struct uvc_device *uvc = req->context;
226 struct v4l2_event v4l2_event;
227 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
229 if (uvc->event_setup_out) {
230 uvc->event_setup_out = 0;
232 memset(&v4l2_event, 0, sizeof(v4l2_event));
233 v4l2_event.type = UVC_EVENT_DATA;
234 uvc_event->data.length = req->actual;
235 memcpy(&uvc_event->data.data, req->buf, req->actual);
236 v4l2_event_queue(uvc->vdev, &v4l2_event);
241 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
243 struct uvc_device *uvc = to_uvc(f);
244 struct v4l2_event v4l2_event;
245 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
247 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
248 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
249 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
252 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
253 INFO(f->config->cdev, "invalid request type\n");
257 /* Stall too big requests. */
258 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
261 memset(&v4l2_event, 0, sizeof(v4l2_event));
262 v4l2_event.type = UVC_EVENT_SETUP;
263 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
264 v4l2_event_queue(uvc->vdev, &v4l2_event);
269 void uvc_function_setup_continue(struct uvc_device *uvc)
271 struct usb_composite_dev *cdev = uvc->func.config->cdev;
273 usb_composite_setup_continue(cdev);
277 uvc_function_get_alt(struct usb_function *f, unsigned interface)
279 struct uvc_device *uvc = to_uvc(f);
281 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
283 if (interface == uvc->control_intf)
285 else if (interface != uvc->streaming_intf)
288 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
292 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
294 struct uvc_device *uvc = to_uvc(f);
295 struct v4l2_event v4l2_event;
296 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
299 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
301 if (interface == uvc->control_intf) {
305 if (uvc->state == UVC_STATE_DISCONNECTED) {
306 memset(&v4l2_event, 0, sizeof(v4l2_event));
307 v4l2_event.type = UVC_EVENT_CONNECT;
308 uvc_event->speed = f->config->cdev->gadget->speed;
309 v4l2_event_queue(uvc->vdev, &v4l2_event);
311 uvc->state = UVC_STATE_CONNECTED;
317 if (interface != uvc->streaming_intf)
321 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
322 return alt ? -EINVAL : 0;
327 if (uvc->state != UVC_STATE_STREAMING)
331 usb_ep_disable(uvc->video.ep);
333 memset(&v4l2_event, 0, sizeof(v4l2_event));
334 v4l2_event.type = UVC_EVENT_STREAMOFF;
335 v4l2_event_queue(uvc->vdev, &v4l2_event);
337 uvc->state = UVC_STATE_CONNECTED;
341 if (uvc->state != UVC_STATE_CONNECTED)
345 ret = config_ep_by_speed(f->config->cdev->gadget,
346 &(uvc->func), uvc->video.ep);
349 usb_ep_enable(uvc->video.ep);
352 memset(&v4l2_event, 0, sizeof(v4l2_event));
353 v4l2_event.type = UVC_EVENT_STREAMON;
354 v4l2_event_queue(uvc->vdev, &v4l2_event);
355 return USB_GADGET_DELAYED_STATUS;
363 uvc_function_disable(struct usb_function *f)
365 struct uvc_device *uvc = to_uvc(f);
366 struct v4l2_event v4l2_event;
368 INFO(f->config->cdev, "uvc_function_disable\n");
370 memset(&v4l2_event, 0, sizeof(v4l2_event));
371 v4l2_event.type = UVC_EVENT_DISCONNECT;
372 v4l2_event_queue(uvc->vdev, &v4l2_event);
374 uvc->state = UVC_STATE_DISCONNECTED;
377 /* --------------------------------------------------------------------------
378 * Connection / disconnection
382 uvc_function_connect(struct uvc_device *uvc)
384 struct usb_composite_dev *cdev = uvc->func.config->cdev;
387 if ((ret = usb_function_activate(&uvc->func)) < 0)
388 INFO(cdev, "UVC connect failed with %d\n", ret);
392 uvc_function_disconnect(struct uvc_device *uvc)
394 struct usb_composite_dev *cdev = uvc->func.config->cdev;
397 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
398 INFO(cdev, "UVC disconnect failed with %d\n", ret);
401 /* --------------------------------------------------------------------------
402 * USB probe and disconnect
406 uvc_register_video(struct uvc_device *uvc)
408 struct usb_composite_dev *cdev = uvc->func.config->cdev;
409 struct video_device *video;
411 /* TODO reference counting. */
412 video = video_device_alloc();
416 video->parent = &cdev->gadget->dev;
417 video->fops = &uvc_v4l2_fops;
418 video->release = video_device_release;
419 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
422 video_set_drvdata(video, uvc);
424 return video_register_device(video, VFL_TYPE_GRABBER, -1);
427 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
429 memcpy(mem, desc, (desc)->bLength); \
431 mem += (desc)->bLength; \
434 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
436 const struct usb_descriptor_header * const *__src; \
437 for (__src = src; *__src; ++__src) { \
438 memcpy(mem, *__src, (*__src)->bLength); \
440 mem += (*__src)->bLength; \
444 static struct usb_descriptor_header ** __init
445 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
447 struct uvc_input_header_descriptor *uvc_streaming_header;
448 struct uvc_header_descriptor *uvc_control_header;
449 const struct uvc_descriptor_header * const *uvc_control_desc;
450 const struct uvc_descriptor_header * const *uvc_streaming_cls;
451 const struct usb_descriptor_header * const *uvc_streaming_std;
452 const struct usb_descriptor_header * const *src;
453 struct usb_descriptor_header **dst;
454 struct usb_descriptor_header **hdr;
455 unsigned int control_size;
456 unsigned int streaming_size;
462 case USB_SPEED_SUPER:
463 uvc_control_desc = uvc->desc.ss_control;
464 uvc_streaming_cls = uvc->desc.ss_streaming;
465 uvc_streaming_std = uvc_ss_streaming;
469 uvc_control_desc = uvc->desc.fs_control;
470 uvc_streaming_cls = uvc->desc.hs_streaming;
471 uvc_streaming_std = uvc_hs_streaming;
476 uvc_control_desc = uvc->desc.fs_control;
477 uvc_streaming_cls = uvc->desc.fs_streaming;
478 uvc_streaming_std = uvc_fs_streaming;
482 /* Descriptors layout
486 * Class-specific UVC control descriptors
489 * uvc_ss_control_comp (for SS only)
490 * uvc_streaming_intf_alt0
491 * Class-specific UVC streaming descriptors
492 * uvc_{fs|hs}_streaming
495 /* Count descriptors and compute their size. */
498 bytes = uvc_iad.bLength + uvc_control_intf.bLength
499 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
500 + uvc_streaming_intf_alt0.bLength;
502 if (speed == USB_SPEED_SUPER) {
503 bytes += uvc_ss_control_comp.bLength;
509 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
511 control_size += (*src)->bLength;
512 bytes += (*src)->bLength;
515 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
517 streaming_size += (*src)->bLength;
518 bytes += (*src)->bLength;
521 for (src = uvc_streaming_std; *src; ++src) {
522 bytes += (*src)->bLength;
526 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
532 mem += (n_desc + 1) * sizeof(*src);
534 /* Copy the descriptors. */
535 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
536 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
538 uvc_control_header = mem;
539 UVC_COPY_DESCRIPTORS(mem, dst,
540 (const struct usb_descriptor_header **)uvc_control_desc);
541 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
542 uvc_control_header->bInCollection = 1;
543 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
545 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
546 if (speed == USB_SPEED_SUPER)
547 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
549 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
550 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
552 uvc_streaming_header = mem;
553 UVC_COPY_DESCRIPTORS(mem, dst,
554 (const struct usb_descriptor_header**)uvc_streaming_cls);
555 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
556 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
558 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
565 uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
567 struct usb_composite_dev *cdev = c->cdev;
568 struct uvc_device *uvc = to_uvc(f);
570 INFO(cdev, "uvc_function_unbind\n");
572 video_unregister_device(uvc->vdev);
573 uvc->control_ep->driver_data = NULL;
574 uvc->video.ep->driver_data = NULL;
576 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
577 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
578 kfree(uvc->control_buf);
580 usb_free_all_descriptors(f);
586 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
588 struct usb_composite_dev *cdev = c->cdev;
589 struct uvc_device *uvc = to_uvc(f);
590 unsigned int max_packet_mult;
591 unsigned int max_packet_size;
595 INFO(cdev, "uvc_function_bind\n");
597 /* Sanity check the streaming endpoint module parameters.
599 streaming_interval = clamp(streaming_interval, 1U, 16U);
600 streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
601 streaming_maxburst = min(streaming_maxburst, 15U);
603 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
606 * NOTE: We assume that the user knows what they are doing and won't
607 * give parameters that their UDC doesn't support.
609 if (streaming_maxpacket <= 1024) {
611 max_packet_size = streaming_maxpacket;
612 } else if (streaming_maxpacket <= 2048) {
614 max_packet_size = streaming_maxpacket / 2;
617 max_packet_size = streaming_maxpacket / 3;
620 uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
621 uvc_fs_streaming_ep.bInterval = streaming_interval;
623 uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
624 uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
625 uvc_hs_streaming_ep.bInterval = streaming_interval;
627 uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
628 uvc_ss_streaming_ep.bInterval = streaming_interval;
629 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
630 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
631 uvc_ss_streaming_comp.wBytesPerInterval =
632 max_packet_size * max_packet_mult * streaming_maxburst;
634 /* Allocate endpoints. */
635 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
637 INFO(cdev, "Unable to allocate control EP\n");
640 uvc->control_ep = ep;
641 ep->driver_data = uvc;
643 if (gadget_is_superspeed(c->cdev->gadget))
644 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
645 &uvc_ss_streaming_comp);
646 else if (gadget_is_dualspeed(cdev->gadget))
647 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
649 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
652 INFO(cdev, "Unable to allocate streaming EP\n");
656 ep->driver_data = uvc;
658 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
659 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
660 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
662 /* Allocate interface IDs. */
663 if ((ret = usb_interface_id(c, f)) < 0)
665 uvc_iad.bFirstInterface = ret;
666 uvc_control_intf.bInterfaceNumber = ret;
667 uvc->control_intf = ret;
669 if ((ret = usb_interface_id(c, f)) < 0)
671 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
672 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
673 uvc->streaming_intf = ret;
675 /* Copy descriptors */
676 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
677 if (gadget_is_dualspeed(cdev->gadget))
678 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
679 if (gadget_is_superspeed(c->cdev->gadget))
680 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
682 /* Preallocate control endpoint request. */
683 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
684 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
685 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
690 uvc->control_req->buf = uvc->control_buf;
691 uvc->control_req->complete = uvc_function_ep0_complete;
692 uvc->control_req->context = uvc;
694 /* Avoid letting this gadget enumerate until the userspace server is
697 if ((ret = usb_function_deactivate(f)) < 0)
700 /* Initialise video. */
701 ret = uvc_video_init(&uvc->video);
705 /* Register a V4L2 device. */
706 ret = uvc_register_video(uvc);
708 printk(KERN_INFO "Unable to register video device\n");
716 video_device_release(uvc->vdev);
719 uvc->control_ep->driver_data = NULL;
721 uvc->video.ep->driver_data = NULL;
723 if (uvc->control_req) {
724 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
725 kfree(uvc->control_buf);
728 usb_free_all_descriptors(f);
732 /* --------------------------------------------------------------------------
733 * USB gadget function
737 * uvc_bind_config - add a UVC function to a configuration
738 * @c: the configuration to support the UVC instance
739 * Context: single threaded during gadget setup
741 * Returns zero on success, else negative errno.
743 * Caller must have called @uvc_setup(). Caller is also responsible for
744 * calling @uvc_cleanup() before module unload.
747 uvc_bind_config(struct usb_configuration *c,
748 const struct uvc_descriptor_header * const *fs_control,
749 const struct uvc_descriptor_header * const *ss_control,
750 const struct uvc_descriptor_header * const *fs_streaming,
751 const struct uvc_descriptor_header * const *hs_streaming,
752 const struct uvc_descriptor_header * const *ss_streaming)
754 struct uvc_device *uvc;
757 /* TODO Check if the USB device controller supports the required
760 if (!gadget_is_dualspeed(c->cdev->gadget))
763 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
767 uvc->state = UVC_STATE_DISCONNECTED;
769 /* Validate the descriptors. */
770 if (fs_control == NULL || fs_control[0] == NULL ||
771 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
774 if (ss_control == NULL || ss_control[0] == NULL ||
775 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
778 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
779 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
782 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
783 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
786 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
787 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
790 uvc->desc.fs_control = fs_control;
791 uvc->desc.ss_control = ss_control;
792 uvc->desc.fs_streaming = fs_streaming;
793 uvc->desc.hs_streaming = hs_streaming;
794 uvc->desc.ss_streaming = ss_streaming;
796 /* String descriptors are global, we only need to allocate string IDs
797 * for the first UVC function. UVC functions beyond the first (if any)
798 * will reuse the same IDs.
800 if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
801 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
805 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
806 uvc_control_intf.iInterface =
807 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
808 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
809 uvc_streaming_intf_alt0.iInterface = ret;
810 uvc_streaming_intf_alt1.iInterface = ret;
813 /* Register the function. */
814 uvc->func.name = "uvc";
815 uvc->func.strings = uvc_function_strings;
816 uvc->func.bind = uvc_function_bind;
817 uvc->func.unbind = uvc_function_unbind;
818 uvc->func.get_alt = uvc_function_get_alt;
819 uvc->func.set_alt = uvc_function_set_alt;
820 uvc->func.disable = uvc_function_disable;
821 uvc->func.setup = uvc_function_setup;
823 ret = usb_add_function(c, &uvc->func);
834 module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
835 MODULE_PARM_DESC(trace, "Trace level bitmask");