1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_gadget.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 #include <linux/device.h>
10 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/string.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
19 #include <linux/usb/g_uvc.h>
20 #include <linux/usb/video.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-event.h>
29 #include "uvc_configfs.h"
31 #include "uvc_video.h"
33 unsigned int uvc_gadget_trace_param;
35 /* --------------------------------------------------------------------------
36 * Function descriptors
39 /* string IDs are assigned dynamically */
41 #define UVC_STRING_CONTROL_IDX 0
42 #define UVC_STRING_STREAMING_IDX 1
44 static struct usb_string uvc_en_us_strings[] = {
45 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
46 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
50 static struct usb_gadget_strings uvc_stringtab = {
51 .language = 0x0409, /* en-us */
52 .strings = uvc_en_us_strings,
55 static struct usb_gadget_strings *uvc_function_strings[] = {
60 #define UVC_INTF_VIDEO_CONTROL 0
61 #define UVC_INTF_VIDEO_STREAMING 1
63 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
65 static struct usb_interface_assoc_descriptor uvc_iad = {
66 .bLength = sizeof(uvc_iad),
67 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
70 .bFunctionClass = USB_CLASS_VIDEO,
71 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
72 .bFunctionProtocol = 0x00,
76 static struct usb_interface_descriptor uvc_control_intf = {
77 .bLength = USB_DT_INTERFACE_SIZE,
78 .bDescriptorType = USB_DT_INTERFACE,
79 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
80 .bAlternateSetting = 0,
82 .bInterfaceClass = USB_CLASS_VIDEO,
83 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
84 .bInterfaceProtocol = 0x00,
88 static struct usb_endpoint_descriptor uvc_control_ep = {
89 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT,
91 .bEndpointAddress = USB_DIR_IN,
92 .bmAttributes = USB_ENDPOINT_XFER_INT,
93 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
97 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
98 .bLength = sizeof(uvc_ss_control_comp),
99 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
100 /* The following 3 values can be tweaked if necessary. */
103 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
106 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
107 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_CS_ENDPOINT,
109 .bDescriptorSubType = UVC_EP_INTERRUPT,
110 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
113 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
114 .bLength = USB_DT_INTERFACE_SIZE,
115 .bDescriptorType = USB_DT_INTERFACE,
116 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
117 .bAlternateSetting = 0,
119 .bInterfaceClass = USB_CLASS_VIDEO,
120 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
121 .bInterfaceProtocol = 0x00,
125 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
126 .bLength = USB_DT_INTERFACE_SIZE,
127 .bDescriptorType = USB_DT_INTERFACE,
128 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
129 .bAlternateSetting = 1,
131 .bInterfaceClass = USB_CLASS_VIDEO,
132 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
133 .bInterfaceProtocol = 0x00,
137 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
138 .bLength = USB_DT_ENDPOINT_SIZE,
139 .bDescriptorType = USB_DT_ENDPOINT,
140 .bEndpointAddress = USB_DIR_IN,
141 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
142 | USB_ENDPOINT_XFER_ISOC,
143 /* The wMaxPacketSize and bInterval values will be initialized from
148 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
149 .bLength = USB_DT_ENDPOINT_SIZE,
150 .bDescriptorType = USB_DT_ENDPOINT,
151 .bEndpointAddress = USB_DIR_IN,
152 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
153 | USB_ENDPOINT_XFER_ISOC,
154 /* The wMaxPacketSize and bInterval values will be initialized from
159 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
160 .bLength = USB_DT_ENDPOINT_SIZE,
161 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
165 | USB_ENDPOINT_XFER_ISOC,
166 /* The wMaxPacketSize and bInterval values will be initialized from
171 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
172 .bLength = sizeof(uvc_ss_streaming_comp),
173 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
174 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
175 * initialized from module parameters.
179 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
180 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
181 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
185 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
186 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
187 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
191 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
192 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
193 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
194 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
198 void uvc_set_trace_param(unsigned int trace)
200 uvc_gadget_trace_param = trace;
202 EXPORT_SYMBOL(uvc_set_trace_param);
204 /* --------------------------------------------------------------------------
209 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
211 struct uvc_device *uvc = req->context;
212 struct v4l2_event v4l2_event;
213 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
215 if (uvc->event_setup_out) {
216 uvc->event_setup_out = 0;
218 memset(&v4l2_event, 0, sizeof(v4l2_event));
219 v4l2_event.type = UVC_EVENT_DATA;
220 uvc_event->data.length = req->actual;
221 memcpy(&uvc_event->data.data, req->buf, req->actual);
222 v4l2_event_queue(&uvc->vdev, &v4l2_event);
227 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
229 struct uvc_device *uvc = to_uvc(f);
230 struct v4l2_event v4l2_event;
231 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
233 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
234 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
235 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
238 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
239 INFO(f->config->cdev, "invalid request type\n");
243 /* Stall too big requests. */
244 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
247 /* Tell the complete callback to generate an event for the next request
248 * that will be enqueued by UVCIOC_SEND_RESPONSE.
250 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
251 uvc->event_length = le16_to_cpu(ctrl->wLength);
253 memset(&v4l2_event, 0, sizeof(v4l2_event));
254 v4l2_event.type = UVC_EVENT_SETUP;
255 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
256 v4l2_event_queue(&uvc->vdev, &v4l2_event);
261 void uvc_function_setup_continue(struct uvc_device *uvc)
263 struct usb_composite_dev *cdev = uvc->func.config->cdev;
265 usb_composite_setup_continue(cdev);
269 uvc_function_get_alt(struct usb_function *f, unsigned interface)
271 struct uvc_device *uvc = to_uvc(f);
273 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
275 if (interface == uvc->control_intf)
277 else if (interface != uvc->streaming_intf)
280 return uvc->video.ep->enabled ? 1 : 0;
284 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
286 struct uvc_device *uvc = to_uvc(f);
287 struct usb_composite_dev *cdev = f->config->cdev;
288 struct v4l2_event v4l2_event;
289 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
292 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
294 if (interface == uvc->control_intf) {
298 INFO(cdev, "reset UVC Control\n");
299 usb_ep_disable(uvc->control_ep);
301 if (!uvc->control_ep->desc)
302 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
305 usb_ep_enable(uvc->control_ep);
307 if (uvc->state == UVC_STATE_DISCONNECTED) {
308 memset(&v4l2_event, 0, sizeof(v4l2_event));
309 v4l2_event.type = UVC_EVENT_CONNECT;
310 uvc_event->speed = cdev->gadget->speed;
311 v4l2_event_queue(&uvc->vdev, &v4l2_event);
313 uvc->state = UVC_STATE_CONNECTED;
319 if (interface != uvc->streaming_intf)
323 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
324 return alt ? -EINVAL : 0;
329 if (uvc->state != UVC_STATE_STREAMING)
333 usb_ep_disable(uvc->video.ep);
335 memset(&v4l2_event, 0, sizeof(v4l2_event));
336 v4l2_event.type = UVC_EVENT_STREAMOFF;
337 v4l2_event_queue(&uvc->vdev, &v4l2_event);
339 uvc->state = UVC_STATE_CONNECTED;
343 if (uvc->state != UVC_STATE_CONNECTED)
349 INFO(cdev, "reset UVC\n");
350 usb_ep_disable(uvc->video.ep);
352 ret = config_ep_by_speed(f->config->cdev->gadget,
353 &(uvc->func), uvc->video.ep);
356 usb_ep_enable(uvc->video.ep);
358 memset(&v4l2_event, 0, sizeof(v4l2_event));
359 v4l2_event.type = UVC_EVENT_STREAMON;
360 v4l2_event_queue(&uvc->vdev, &v4l2_event);
361 return USB_GADGET_DELAYED_STATUS;
369 uvc_function_disable(struct usb_function *f)
371 struct uvc_device *uvc = to_uvc(f);
372 struct v4l2_event v4l2_event;
374 INFO(f->config->cdev, "uvc_function_disable\n");
376 memset(&v4l2_event, 0, sizeof(v4l2_event));
377 v4l2_event.type = UVC_EVENT_DISCONNECT;
378 v4l2_event_queue(&uvc->vdev, &v4l2_event);
380 uvc->state = UVC_STATE_DISCONNECTED;
382 usb_ep_disable(uvc->video.ep);
383 usb_ep_disable(uvc->control_ep);
386 /* --------------------------------------------------------------------------
387 * Connection / disconnection
391 uvc_function_connect(struct uvc_device *uvc)
393 struct usb_composite_dev *cdev = uvc->func.config->cdev;
396 if ((ret = usb_function_activate(&uvc->func)) < 0)
397 INFO(cdev, "UVC connect failed with %d\n", ret);
401 uvc_function_disconnect(struct uvc_device *uvc)
403 struct usb_composite_dev *cdev = uvc->func.config->cdev;
406 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
407 INFO(cdev, "UVC disconnect failed with %d\n", ret);
410 /* --------------------------------------------------------------------------
411 * USB probe and disconnect
415 uvc_register_video(struct uvc_device *uvc)
417 struct usb_composite_dev *cdev = uvc->func.config->cdev;
419 /* TODO reference counting. */
420 uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
421 uvc->vdev.fops = &uvc_v4l2_fops;
422 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
423 uvc->vdev.release = video_device_release_empty;
424 uvc->vdev.vfl_dir = VFL_DIR_TX;
425 uvc->vdev.lock = &uvc->video.mutex;
426 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
428 video_set_drvdata(&uvc->vdev, uvc);
430 return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
433 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
435 memcpy(mem, desc, (desc)->bLength); \
437 mem += (desc)->bLength; \
440 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
442 const struct usb_descriptor_header * const *__src; \
443 for (__src = src; *__src; ++__src) { \
444 memcpy(mem, *__src, (*__src)->bLength); \
446 mem += (*__src)->bLength; \
450 static struct usb_descriptor_header **
451 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
453 struct uvc_input_header_descriptor *uvc_streaming_header;
454 struct uvc_header_descriptor *uvc_control_header;
455 const struct uvc_descriptor_header * const *uvc_control_desc;
456 const struct uvc_descriptor_header * const *uvc_streaming_cls;
457 const struct usb_descriptor_header * const *uvc_streaming_std;
458 const struct usb_descriptor_header * const *src;
459 struct usb_descriptor_header **dst;
460 struct usb_descriptor_header **hdr;
461 unsigned int control_size;
462 unsigned int streaming_size;
468 case USB_SPEED_SUPER:
469 uvc_control_desc = uvc->desc.ss_control;
470 uvc_streaming_cls = uvc->desc.ss_streaming;
471 uvc_streaming_std = uvc_ss_streaming;
475 uvc_control_desc = uvc->desc.fs_control;
476 uvc_streaming_cls = uvc->desc.hs_streaming;
477 uvc_streaming_std = uvc_hs_streaming;
482 uvc_control_desc = uvc->desc.fs_control;
483 uvc_streaming_cls = uvc->desc.fs_streaming;
484 uvc_streaming_std = uvc_fs_streaming;
488 if (!uvc_control_desc || !uvc_streaming_cls)
489 return ERR_PTR(-ENODEV);
491 /* Descriptors layout
495 * Class-specific UVC control descriptors
498 * uvc_ss_control_comp (for SS only)
499 * uvc_streaming_intf_alt0
500 * Class-specific UVC streaming descriptors
501 * uvc_{fs|hs}_streaming
504 /* Count descriptors and compute their size. */
507 bytes = uvc_iad.bLength + uvc_control_intf.bLength
508 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
509 + uvc_streaming_intf_alt0.bLength;
511 if (speed == USB_SPEED_SUPER) {
512 bytes += uvc_ss_control_comp.bLength;
518 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
520 control_size += (*src)->bLength;
521 bytes += (*src)->bLength;
524 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
526 streaming_size += (*src)->bLength;
527 bytes += (*src)->bLength;
530 for (src = uvc_streaming_std; *src; ++src) {
531 bytes += (*src)->bLength;
535 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
541 mem += (n_desc + 1) * sizeof(*src);
543 /* Copy the descriptors. */
544 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
545 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
547 uvc_control_header = mem;
548 UVC_COPY_DESCRIPTORS(mem, dst,
549 (const struct usb_descriptor_header **)uvc_control_desc);
550 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
551 uvc_control_header->bInCollection = 1;
552 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
554 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
555 if (speed == USB_SPEED_SUPER)
556 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
558 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
559 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
561 uvc_streaming_header = mem;
562 UVC_COPY_DESCRIPTORS(mem, dst,
563 (const struct usb_descriptor_header**)uvc_streaming_cls);
564 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
565 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
567 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
574 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
576 struct usb_composite_dev *cdev = c->cdev;
577 struct uvc_device *uvc = to_uvc(f);
578 struct usb_string *us;
579 unsigned int max_packet_mult;
580 unsigned int max_packet_size;
582 struct f_uvc_opts *opts;
585 INFO(cdev, "uvc_function_bind\n");
587 opts = fi_to_f_uvc_opts(f->fi);
588 /* Sanity check the streaming endpoint module parameters.
590 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
591 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
592 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
594 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
595 if (opts->streaming_maxburst &&
596 (opts->streaming_maxpacket % 1024) != 0) {
597 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
598 INFO(cdev, "overriding streaming_maxpacket to %d\n",
599 opts->streaming_maxpacket);
602 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
605 * NOTE: We assume that the user knows what they are doing and won't
606 * give parameters that their UDC doesn't support.
608 if (opts->streaming_maxpacket <= 1024) {
610 max_packet_size = opts->streaming_maxpacket;
611 } else if (opts->streaming_maxpacket <= 2048) {
613 max_packet_size = opts->streaming_maxpacket / 2;
616 max_packet_size = opts->streaming_maxpacket / 3;
619 uvc_fs_streaming_ep.wMaxPacketSize =
620 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
621 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
623 uvc_hs_streaming_ep.wMaxPacketSize =
624 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
625 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
627 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
628 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
629 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
630 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
631 uvc_ss_streaming_comp.wBytesPerInterval =
632 cpu_to_le16(max_packet_size * max_packet_mult *
633 (opts->streaming_maxburst + 1));
635 /* Allocate endpoints. */
636 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
638 INFO(cdev, "Unable to allocate control EP\n");
641 uvc->control_ep = ep;
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");
657 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
658 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
659 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
661 us = usb_gstrings_attach(cdev, uvc_function_strings,
662 ARRAY_SIZE(uvc_en_us_strings));
667 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
668 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
669 ret = us[UVC_STRING_STREAMING_IDX].id;
670 uvc_streaming_intf_alt0.iInterface = ret;
671 uvc_streaming_intf_alt1.iInterface = ret;
673 /* Allocate interface IDs. */
674 if ((ret = usb_interface_id(c, f)) < 0)
676 uvc_iad.bFirstInterface = ret;
677 uvc_control_intf.bInterfaceNumber = ret;
678 uvc->control_intf = ret;
680 if ((ret = usb_interface_id(c, f)) < 0)
682 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
683 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
684 uvc->streaming_intf = ret;
686 /* Copy descriptors */
687 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
688 if (IS_ERR(f->fs_descriptors)) {
689 ret = PTR_ERR(f->fs_descriptors);
690 f->fs_descriptors = NULL;
693 if (gadget_is_dualspeed(cdev->gadget)) {
694 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
695 if (IS_ERR(f->hs_descriptors)) {
696 ret = PTR_ERR(f->hs_descriptors);
697 f->hs_descriptors = NULL;
701 if (gadget_is_superspeed(c->cdev->gadget)) {
702 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
703 if (IS_ERR(f->ss_descriptors)) {
704 ret = PTR_ERR(f->ss_descriptors);
705 f->ss_descriptors = NULL;
710 /* Preallocate control endpoint request. */
711 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
712 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
713 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
718 uvc->control_req->buf = uvc->control_buf;
719 uvc->control_req->complete = uvc_function_ep0_complete;
720 uvc->control_req->context = uvc;
722 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
723 printk(KERN_INFO "v4l2_device_register failed\n");
727 /* Initialise video. */
728 ret = uvcg_video_init(&uvc->video);
732 /* Register a V4L2 device. */
733 ret = uvc_register_video(uvc);
735 printk(KERN_INFO "Unable to register video device\n");
742 v4l2_device_unregister(&uvc->v4l2_dev);
744 if (uvc->control_req)
745 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
746 kfree(uvc->control_buf);
748 usb_free_all_descriptors(f);
752 /* --------------------------------------------------------------------------
753 * USB gadget function
756 static void uvc_free_inst(struct usb_function_instance *f)
758 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
760 mutex_destroy(&opts->lock);
764 static struct usb_function_instance *uvc_alloc_inst(void)
766 struct f_uvc_opts *opts;
767 struct uvc_camera_terminal_descriptor *cd;
768 struct uvc_processing_unit_descriptor *pd;
769 struct uvc_output_terminal_descriptor *od;
770 struct uvc_color_matching_descriptor *md;
771 struct uvc_descriptor_header **ctl_cls;
773 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
775 return ERR_PTR(-ENOMEM);
776 opts->func_inst.free_func_inst = uvc_free_inst;
777 mutex_init(&opts->lock);
779 cd = &opts->uvc_camera_terminal;
780 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
781 cd->bDescriptorType = USB_DT_CS_INTERFACE;
782 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
784 cd->wTerminalType = cpu_to_le16(0x0201);
785 cd->bAssocTerminal = 0;
787 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
788 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
789 cd->wOcularFocalLength = cpu_to_le16(0);
790 cd->bControlSize = 3;
791 cd->bmControls[0] = 2;
792 cd->bmControls[1] = 0;
793 cd->bmControls[2] = 0;
795 pd = &opts->uvc_processing;
796 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
797 pd->bDescriptorType = USB_DT_CS_INTERFACE;
798 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
801 pd->wMaxMultiplier = cpu_to_le16(16*1024);
802 pd->bControlSize = 2;
803 pd->bmControls[0] = 1;
804 pd->bmControls[1] = 0;
807 od = &opts->uvc_output_terminal;
808 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
809 od->bDescriptorType = USB_DT_CS_INTERFACE;
810 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
812 od->wTerminalType = cpu_to_le16(0x0101);
813 od->bAssocTerminal = 0;
817 md = &opts->uvc_color_matching;
818 md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
819 md->bDescriptorType = USB_DT_CS_INTERFACE;
820 md->bDescriptorSubType = UVC_VS_COLORFORMAT;
821 md->bColorPrimaries = 1;
822 md->bTransferCharacteristics = 1;
823 md->bMatrixCoefficients = 4;
825 /* Prepare fs control class descriptors for configfs-based gadgets */
826 ctl_cls = opts->uvc_fs_control_cls;
827 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
828 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
829 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
830 ctl_cls[3] = (struct uvc_descriptor_header *)od;
831 ctl_cls[4] = NULL; /* NULL-terminate */
833 (const struct uvc_descriptor_header * const *)ctl_cls;
835 /* Prepare hs control class descriptors for configfs-based gadgets */
836 ctl_cls = opts->uvc_ss_control_cls;
837 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
838 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
839 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
840 ctl_cls[3] = (struct uvc_descriptor_header *)od;
841 ctl_cls[4] = NULL; /* NULL-terminate */
843 (const struct uvc_descriptor_header * const *)ctl_cls;
845 opts->streaming_interval = 1;
846 opts->streaming_maxpacket = 1024;
848 uvcg_attach_configfs(opts);
849 return &opts->func_inst;
852 static void uvc_free(struct usb_function *f)
854 struct uvc_device *uvc = to_uvc(f);
855 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
861 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
863 struct usb_composite_dev *cdev = c->cdev;
864 struct uvc_device *uvc = to_uvc(f);
866 INFO(cdev, "%s\n", __func__);
868 video_unregister_device(&uvc->vdev);
869 v4l2_device_unregister(&uvc->v4l2_dev);
871 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
872 kfree(uvc->control_buf);
874 usb_free_all_descriptors(f);
877 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
879 struct uvc_device *uvc;
880 struct f_uvc_opts *opts;
881 struct uvc_descriptor_header **strm_cls;
883 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
885 return ERR_PTR(-ENOMEM);
887 mutex_init(&uvc->video.mutex);
888 uvc->state = UVC_STATE_DISCONNECTED;
889 opts = fi_to_f_uvc_opts(fi);
891 mutex_lock(&opts->lock);
892 if (opts->uvc_fs_streaming_cls) {
893 strm_cls = opts->uvc_fs_streaming_cls;
895 (const struct uvc_descriptor_header * const *)strm_cls;
897 if (opts->uvc_hs_streaming_cls) {
898 strm_cls = opts->uvc_hs_streaming_cls;
900 (const struct uvc_descriptor_header * const *)strm_cls;
902 if (opts->uvc_ss_streaming_cls) {
903 strm_cls = opts->uvc_ss_streaming_cls;
905 (const struct uvc_descriptor_header * const *)strm_cls;
908 uvc->desc.fs_control = opts->fs_control;
909 uvc->desc.ss_control = opts->ss_control;
910 uvc->desc.fs_streaming = opts->fs_streaming;
911 uvc->desc.hs_streaming = opts->hs_streaming;
912 uvc->desc.ss_streaming = opts->ss_streaming;
914 mutex_unlock(&opts->lock);
916 /* Register the function. */
917 uvc->func.name = "uvc";
918 uvc->func.bind = uvc_function_bind;
919 uvc->func.unbind = uvc_unbind;
920 uvc->func.get_alt = uvc_function_get_alt;
921 uvc->func.set_alt = uvc_function_set_alt;
922 uvc->func.disable = uvc_function_disable;
923 uvc->func.setup = uvc_function_setup;
924 uvc->func.free_func = uvc_free;
925 uvc->func.bind_deactivated = true;
930 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
931 MODULE_LICENSE("GPL");
932 MODULE_AUTHOR("Laurent Pinchart");