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;
34 module_param_named(trace, uvc_gadget_trace_param, uint, 0644);
35 MODULE_PARM_DESC(trace, "Trace level bitmask");
37 /* --------------------------------------------------------------------------
38 * Function descriptors
41 /* string IDs are assigned dynamically */
43 #define UVC_STRING_CONTROL_IDX 0
44 #define UVC_STRING_STREAMING_IDX 1
46 static struct usb_string uvc_en_us_strings[] = {
47 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
48 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
52 static struct usb_gadget_strings uvc_stringtab = {
53 .language = 0x0409, /* en-us */
54 .strings = uvc_en_us_strings,
57 static struct usb_gadget_strings *uvc_function_strings[] = {
62 #define UVC_INTF_VIDEO_CONTROL 0
63 #define UVC_INTF_VIDEO_STREAMING 1
65 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
67 static struct usb_interface_assoc_descriptor uvc_iad = {
68 .bLength = sizeof(uvc_iad),
69 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
72 .bFunctionClass = USB_CLASS_VIDEO,
73 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
74 .bFunctionProtocol = 0x00,
78 static struct usb_interface_descriptor uvc_control_intf = {
79 .bLength = USB_DT_INTERFACE_SIZE,
80 .bDescriptorType = USB_DT_INTERFACE,
81 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
82 .bAlternateSetting = 0,
84 .bInterfaceClass = USB_CLASS_VIDEO,
85 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
86 .bInterfaceProtocol = 0x00,
90 static struct usb_endpoint_descriptor uvc_control_ep = {
91 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
93 .bEndpointAddress = USB_DIR_IN,
94 .bmAttributes = USB_ENDPOINT_XFER_INT,
95 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
99 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
100 .bLength = sizeof(uvc_ss_control_comp),
101 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
102 /* The following 3 values can be tweaked if necessary. */
105 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
108 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
109 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
110 .bDescriptorType = USB_DT_CS_ENDPOINT,
111 .bDescriptorSubType = UVC_EP_INTERRUPT,
112 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
115 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
116 .bLength = USB_DT_INTERFACE_SIZE,
117 .bDescriptorType = USB_DT_INTERFACE,
118 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
119 .bAlternateSetting = 0,
121 .bInterfaceClass = USB_CLASS_VIDEO,
122 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
123 .bInterfaceProtocol = 0x00,
127 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
128 .bLength = USB_DT_INTERFACE_SIZE,
129 .bDescriptorType = USB_DT_INTERFACE,
130 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
131 .bAlternateSetting = 1,
133 .bInterfaceClass = USB_CLASS_VIDEO,
134 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
135 .bInterfaceProtocol = 0x00,
139 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
140 .bLength = USB_DT_ENDPOINT_SIZE,
141 .bDescriptorType = USB_DT_ENDPOINT,
142 .bEndpointAddress = USB_DIR_IN,
143 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
144 | USB_ENDPOINT_XFER_ISOC,
145 /* The wMaxPacketSize and bInterval values will be initialized from
150 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
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
161 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
162 .bLength = USB_DT_ENDPOINT_SIZE,
163 .bDescriptorType = USB_DT_ENDPOINT,
165 .bEndpointAddress = USB_DIR_IN,
166 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
167 | USB_ENDPOINT_XFER_ISOC,
168 /* The wMaxPacketSize and bInterval values will be initialized from
173 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
174 .bLength = sizeof(uvc_ss_streaming_comp),
175 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
176 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
177 * initialized from module parameters.
181 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
182 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
183 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
187 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
188 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
189 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
193 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
194 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
195 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
196 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
200 void uvc_set_trace_param(unsigned int trace)
202 uvc_gadget_trace_param = trace;
204 EXPORT_SYMBOL(uvc_set_trace_param);
206 /* --------------------------------------------------------------------------
211 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
213 struct uvc_device *uvc = req->context;
214 struct v4l2_event v4l2_event;
215 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
217 if (uvc->event_setup_out) {
218 uvc->event_setup_out = 0;
220 memset(&v4l2_event, 0, sizeof(v4l2_event));
221 v4l2_event.type = UVC_EVENT_DATA;
222 uvc_event->data.length = req->actual;
223 memcpy(&uvc_event->data.data, req->buf, req->actual);
224 v4l2_event_queue(&uvc->vdev, &v4l2_event);
229 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
231 struct uvc_device *uvc = to_uvc(f);
232 struct v4l2_event v4l2_event;
233 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
235 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
236 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
237 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
240 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
241 INFO(f->config->cdev, "invalid request type\n");
245 /* Stall too big requests. */
246 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
249 /* Tell the complete callback to generate an event for the next request
250 * that will be enqueued by UVCIOC_SEND_RESPONSE.
252 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
253 uvc->event_length = le16_to_cpu(ctrl->wLength);
255 memset(&v4l2_event, 0, sizeof(v4l2_event));
256 v4l2_event.type = UVC_EVENT_SETUP;
257 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
258 v4l2_event_queue(&uvc->vdev, &v4l2_event);
263 void uvc_function_setup_continue(struct uvc_device *uvc)
265 struct usb_composite_dev *cdev = uvc->func.config->cdev;
267 usb_composite_setup_continue(cdev);
271 uvc_function_get_alt(struct usb_function *f, unsigned interface)
273 struct uvc_device *uvc = to_uvc(f);
275 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
277 if (interface == uvc->control_intf)
279 else if (interface != uvc->streaming_intf)
282 return uvc->video.ep->enabled ? 1 : 0;
286 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
288 struct uvc_device *uvc = to_uvc(f);
289 struct usb_composite_dev *cdev = f->config->cdev;
290 struct v4l2_event v4l2_event;
291 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
294 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
296 if (interface == uvc->control_intf) {
300 INFO(cdev, "reset UVC Control\n");
301 usb_ep_disable(uvc->control_ep);
303 if (!uvc->control_ep->desc)
304 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
307 usb_ep_enable(uvc->control_ep);
309 if (uvc->state == UVC_STATE_DISCONNECTED) {
310 memset(&v4l2_event, 0, sizeof(v4l2_event));
311 v4l2_event.type = UVC_EVENT_CONNECT;
312 uvc_event->speed = cdev->gadget->speed;
313 v4l2_event_queue(&uvc->vdev, &v4l2_event);
315 uvc->state = UVC_STATE_CONNECTED;
321 if (interface != uvc->streaming_intf)
325 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
326 return alt ? -EINVAL : 0;
331 if (uvc->state != UVC_STATE_STREAMING)
335 usb_ep_disable(uvc->video.ep);
337 memset(&v4l2_event, 0, sizeof(v4l2_event));
338 v4l2_event.type = UVC_EVENT_STREAMOFF;
339 v4l2_event_queue(&uvc->vdev, &v4l2_event);
341 uvc->state = UVC_STATE_CONNECTED;
345 if (uvc->state != UVC_STATE_CONNECTED)
351 INFO(cdev, "reset UVC\n");
352 usb_ep_disable(uvc->video.ep);
354 ret = config_ep_by_speed(f->config->cdev->gadget,
355 &(uvc->func), uvc->video.ep);
358 usb_ep_enable(uvc->video.ep);
360 memset(&v4l2_event, 0, sizeof(v4l2_event));
361 v4l2_event.type = UVC_EVENT_STREAMON;
362 v4l2_event_queue(&uvc->vdev, &v4l2_event);
363 return USB_GADGET_DELAYED_STATUS;
371 uvc_function_disable(struct usb_function *f)
373 struct uvc_device *uvc = to_uvc(f);
374 struct v4l2_event v4l2_event;
376 INFO(f->config->cdev, "uvc_function_disable\n");
378 memset(&v4l2_event, 0, sizeof(v4l2_event));
379 v4l2_event.type = UVC_EVENT_DISCONNECT;
380 v4l2_event_queue(&uvc->vdev, &v4l2_event);
382 uvc->state = UVC_STATE_DISCONNECTED;
384 usb_ep_disable(uvc->video.ep);
385 usb_ep_disable(uvc->control_ep);
388 /* --------------------------------------------------------------------------
389 * Connection / disconnection
393 uvc_function_connect(struct uvc_device *uvc)
395 struct usb_composite_dev *cdev = uvc->func.config->cdev;
398 if ((ret = usb_function_activate(&uvc->func)) < 0)
399 INFO(cdev, "UVC connect failed with %d\n", ret);
403 uvc_function_disconnect(struct uvc_device *uvc)
405 struct usb_composite_dev *cdev = uvc->func.config->cdev;
408 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
409 INFO(cdev, "UVC disconnect failed with %d\n", ret);
412 /* --------------------------------------------------------------------------
413 * USB probe and disconnect
416 static ssize_t function_name_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 struct uvc_device *uvc = dev_get_drvdata(dev);
421 return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);
424 static DEVICE_ATTR_RO(function_name);
427 uvc_register_video(struct uvc_device *uvc)
429 struct usb_composite_dev *cdev = uvc->func.config->cdev;
432 /* TODO reference counting. */
433 uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
434 uvc->vdev.fops = &uvc_v4l2_fops;
435 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
436 uvc->vdev.release = video_device_release_empty;
437 uvc->vdev.vfl_dir = VFL_DIR_TX;
438 uvc->vdev.lock = &uvc->video.mutex;
439 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
441 video_set_drvdata(&uvc->vdev, uvc);
443 ret = video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
447 ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
449 video_unregister_device(&uvc->vdev);
456 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
458 memcpy(mem, desc, (desc)->bLength); \
460 mem += (desc)->bLength; \
463 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
465 const struct usb_descriptor_header * const *__src; \
466 for (__src = src; *__src; ++__src) { \
467 memcpy(mem, *__src, (*__src)->bLength); \
469 mem += (*__src)->bLength; \
473 static struct usb_descriptor_header **
474 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
476 struct uvc_input_header_descriptor *uvc_streaming_header;
477 struct uvc_header_descriptor *uvc_control_header;
478 const struct uvc_descriptor_header * const *uvc_control_desc;
479 const struct uvc_descriptor_header * const *uvc_streaming_cls;
480 const struct usb_descriptor_header * const *uvc_streaming_std;
481 const struct usb_descriptor_header * const *src;
482 struct usb_descriptor_header **dst;
483 struct usb_descriptor_header **hdr;
484 unsigned int control_size;
485 unsigned int streaming_size;
491 case USB_SPEED_SUPER:
492 uvc_control_desc = uvc->desc.ss_control;
493 uvc_streaming_cls = uvc->desc.ss_streaming;
494 uvc_streaming_std = uvc_ss_streaming;
498 uvc_control_desc = uvc->desc.fs_control;
499 uvc_streaming_cls = uvc->desc.hs_streaming;
500 uvc_streaming_std = uvc_hs_streaming;
505 uvc_control_desc = uvc->desc.fs_control;
506 uvc_streaming_cls = uvc->desc.fs_streaming;
507 uvc_streaming_std = uvc_fs_streaming;
511 if (!uvc_control_desc || !uvc_streaming_cls)
512 return ERR_PTR(-ENODEV);
514 /* Descriptors layout
518 * Class-specific UVC control descriptors
521 * uvc_ss_control_comp (for SS only)
522 * uvc_streaming_intf_alt0
523 * Class-specific UVC streaming descriptors
524 * uvc_{fs|hs}_streaming
527 /* Count descriptors and compute their size. */
530 bytes = uvc_iad.bLength + uvc_control_intf.bLength
531 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
532 + uvc_streaming_intf_alt0.bLength;
534 if (speed == USB_SPEED_SUPER) {
535 bytes += uvc_ss_control_comp.bLength;
541 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
543 control_size += (*src)->bLength;
544 bytes += (*src)->bLength;
547 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
549 streaming_size += (*src)->bLength;
550 bytes += (*src)->bLength;
553 for (src = uvc_streaming_std; *src; ++src) {
554 bytes += (*src)->bLength;
558 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
564 mem += (n_desc + 1) * sizeof(*src);
566 /* Copy the descriptors. */
567 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
568 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
570 uvc_control_header = mem;
571 UVC_COPY_DESCRIPTORS(mem, dst,
572 (const struct usb_descriptor_header **)uvc_control_desc);
573 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
574 uvc_control_header->bInCollection = 1;
575 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
577 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
578 if (speed == USB_SPEED_SUPER)
579 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
581 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
582 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
584 uvc_streaming_header = mem;
585 UVC_COPY_DESCRIPTORS(mem, dst,
586 (const struct usb_descriptor_header**)uvc_streaming_cls);
587 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
588 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
590 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
597 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
599 struct usb_composite_dev *cdev = c->cdev;
600 struct uvc_device *uvc = to_uvc(f);
601 struct usb_string *us;
602 unsigned int max_packet_mult;
603 unsigned int max_packet_size;
605 struct f_uvc_opts *opts;
608 INFO(cdev, "uvc_function_bind\n");
610 opts = fi_to_f_uvc_opts(f->fi);
611 /* Sanity check the streaming endpoint module parameters.
613 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
614 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
615 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
617 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
618 if (opts->streaming_maxburst &&
619 (opts->streaming_maxpacket % 1024) != 0) {
620 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
621 INFO(cdev, "overriding streaming_maxpacket to %d\n",
622 opts->streaming_maxpacket);
625 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
628 * NOTE: We assume that the user knows what they are doing and won't
629 * give parameters that their UDC doesn't support.
631 if (opts->streaming_maxpacket <= 1024) {
633 max_packet_size = opts->streaming_maxpacket;
634 } else if (opts->streaming_maxpacket <= 2048) {
636 max_packet_size = opts->streaming_maxpacket / 2;
639 max_packet_size = opts->streaming_maxpacket / 3;
642 uvc_fs_streaming_ep.wMaxPacketSize =
643 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
644 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
646 uvc_hs_streaming_ep.wMaxPacketSize =
647 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
649 /* A high-bandwidth endpoint must specify a bInterval value of 1 */
650 if (max_packet_mult > 1)
651 uvc_hs_streaming_ep.bInterval = 1;
653 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
655 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
656 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
657 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
658 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
659 uvc_ss_streaming_comp.wBytesPerInterval =
660 cpu_to_le16(max_packet_size * max_packet_mult *
661 (opts->streaming_maxburst + 1));
663 /* Allocate endpoints. */
664 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
666 INFO(cdev, "Unable to allocate control EP\n");
669 uvc->control_ep = ep;
671 if (gadget_is_superspeed(c->cdev->gadget))
672 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
673 &uvc_ss_streaming_comp);
674 else if (gadget_is_dualspeed(cdev->gadget))
675 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
677 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
680 INFO(cdev, "Unable to allocate streaming EP\n");
685 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
686 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
687 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
689 us = usb_gstrings_attach(cdev, uvc_function_strings,
690 ARRAY_SIZE(uvc_en_us_strings));
695 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
696 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
697 ret = us[UVC_STRING_STREAMING_IDX].id;
698 uvc_streaming_intf_alt0.iInterface = ret;
699 uvc_streaming_intf_alt1.iInterface = ret;
701 /* Allocate interface IDs. */
702 if ((ret = usb_interface_id(c, f)) < 0)
704 uvc_iad.bFirstInterface = ret;
705 uvc_control_intf.bInterfaceNumber = ret;
706 uvc->control_intf = ret;
708 if ((ret = usb_interface_id(c, f)) < 0)
710 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
711 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
712 uvc->streaming_intf = ret;
714 /* Copy descriptors */
715 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
716 if (IS_ERR(f->fs_descriptors)) {
717 ret = PTR_ERR(f->fs_descriptors);
718 f->fs_descriptors = NULL;
721 if (gadget_is_dualspeed(cdev->gadget)) {
722 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
723 if (IS_ERR(f->hs_descriptors)) {
724 ret = PTR_ERR(f->hs_descriptors);
725 f->hs_descriptors = NULL;
729 if (gadget_is_superspeed(c->cdev->gadget)) {
730 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
731 if (IS_ERR(f->ss_descriptors)) {
732 ret = PTR_ERR(f->ss_descriptors);
733 f->ss_descriptors = NULL;
738 /* Preallocate control endpoint request. */
739 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
740 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
741 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
746 uvc->control_req->buf = uvc->control_buf;
747 uvc->control_req->complete = uvc_function_ep0_complete;
748 uvc->control_req->context = uvc;
750 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
751 printk(KERN_INFO "v4l2_device_register failed\n");
755 /* Initialise video. */
756 ret = uvcg_video_init(&uvc->video);
760 /* Register a V4L2 device. */
761 ret = uvc_register_video(uvc);
763 printk(KERN_INFO "Unable to register video device\n");
770 v4l2_device_unregister(&uvc->v4l2_dev);
772 if (uvc->control_req)
773 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
774 kfree(uvc->control_buf);
776 usb_free_all_descriptors(f);
780 /* --------------------------------------------------------------------------
781 * USB gadget function
784 static void uvc_free_inst(struct usb_function_instance *f)
786 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
788 mutex_destroy(&opts->lock);
792 static struct usb_function_instance *uvc_alloc_inst(void)
794 struct f_uvc_opts *opts;
795 struct uvc_camera_terminal_descriptor *cd;
796 struct uvc_processing_unit_descriptor *pd;
797 struct uvc_output_terminal_descriptor *od;
798 struct uvc_color_matching_descriptor *md;
799 struct uvc_descriptor_header **ctl_cls;
801 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
803 return ERR_PTR(-ENOMEM);
804 opts->func_inst.free_func_inst = uvc_free_inst;
805 mutex_init(&opts->lock);
807 cd = &opts->uvc_camera_terminal;
808 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
809 cd->bDescriptorType = USB_DT_CS_INTERFACE;
810 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
812 cd->wTerminalType = cpu_to_le16(0x0201);
813 cd->bAssocTerminal = 0;
815 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
816 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
817 cd->wOcularFocalLength = cpu_to_le16(0);
818 cd->bControlSize = 3;
819 cd->bmControls[0] = 2;
820 cd->bmControls[1] = 0;
821 cd->bmControls[2] = 0;
823 pd = &opts->uvc_processing;
824 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
825 pd->bDescriptorType = USB_DT_CS_INTERFACE;
826 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
829 pd->wMaxMultiplier = cpu_to_le16(16*1024);
830 pd->bControlSize = 2;
831 pd->bmControls[0] = 1;
832 pd->bmControls[1] = 0;
835 od = &opts->uvc_output_terminal;
836 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
837 od->bDescriptorType = USB_DT_CS_INTERFACE;
838 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
840 od->wTerminalType = cpu_to_le16(0x0101);
841 od->bAssocTerminal = 0;
845 md = &opts->uvc_color_matching;
846 md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
847 md->bDescriptorType = USB_DT_CS_INTERFACE;
848 md->bDescriptorSubType = UVC_VS_COLORFORMAT;
849 md->bColorPrimaries = 1;
850 md->bTransferCharacteristics = 1;
851 md->bMatrixCoefficients = 4;
853 /* Prepare fs control class descriptors for configfs-based gadgets */
854 ctl_cls = opts->uvc_fs_control_cls;
855 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
856 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
857 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
858 ctl_cls[3] = (struct uvc_descriptor_header *)od;
859 ctl_cls[4] = NULL; /* NULL-terminate */
861 (const struct uvc_descriptor_header * const *)ctl_cls;
863 /* Prepare hs control class descriptors for configfs-based gadgets */
864 ctl_cls = opts->uvc_ss_control_cls;
865 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
866 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
867 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
868 ctl_cls[3] = (struct uvc_descriptor_header *)od;
869 ctl_cls[4] = NULL; /* NULL-terminate */
871 (const struct uvc_descriptor_header * const *)ctl_cls;
873 opts->streaming_interval = 1;
874 opts->streaming_maxpacket = 1024;
876 uvcg_attach_configfs(opts);
877 return &opts->func_inst;
880 static void uvc_free(struct usb_function *f)
882 struct uvc_device *uvc = to_uvc(f);
883 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
889 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
891 struct usb_composite_dev *cdev = c->cdev;
892 struct uvc_device *uvc = to_uvc(f);
894 INFO(cdev, "%s\n", __func__);
896 device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
897 video_unregister_device(&uvc->vdev);
898 v4l2_device_unregister(&uvc->v4l2_dev);
900 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
901 kfree(uvc->control_buf);
903 usb_free_all_descriptors(f);
906 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
908 struct uvc_device *uvc;
909 struct f_uvc_opts *opts;
910 struct uvc_descriptor_header **strm_cls;
912 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
914 return ERR_PTR(-ENOMEM);
916 mutex_init(&uvc->video.mutex);
917 uvc->state = UVC_STATE_DISCONNECTED;
918 opts = fi_to_f_uvc_opts(fi);
920 mutex_lock(&opts->lock);
921 if (opts->uvc_fs_streaming_cls) {
922 strm_cls = opts->uvc_fs_streaming_cls;
924 (const struct uvc_descriptor_header * const *)strm_cls;
926 if (opts->uvc_hs_streaming_cls) {
927 strm_cls = opts->uvc_hs_streaming_cls;
929 (const struct uvc_descriptor_header * const *)strm_cls;
931 if (opts->uvc_ss_streaming_cls) {
932 strm_cls = opts->uvc_ss_streaming_cls;
934 (const struct uvc_descriptor_header * const *)strm_cls;
937 uvc->desc.fs_control = opts->fs_control;
938 uvc->desc.ss_control = opts->ss_control;
939 uvc->desc.fs_streaming = opts->fs_streaming;
940 uvc->desc.hs_streaming = opts->hs_streaming;
941 uvc->desc.ss_streaming = opts->ss_streaming;
943 mutex_unlock(&opts->lock);
945 /* Register the function. */
946 uvc->func.name = "uvc";
947 uvc->func.bind = uvc_function_bind;
948 uvc->func.unbind = uvc_unbind;
949 uvc->func.get_alt = uvc_function_get_alt;
950 uvc->func.set_alt = uvc_function_set_alt;
951 uvc->func.disable = uvc_function_disable;
952 uvc->func.setup = uvc_function_setup;
953 uvc->func.free_func = uvc_free;
954 uvc->func.bind_deactivated = true;
959 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
960 MODULE_LICENSE("GPL");
961 MODULE_AUTHOR("Laurent Pinchart");