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/module.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
18 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/string.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/video.h>
24 #include <linux/vmalloc.h>
25 #include <linux/wait.h>
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-event.h>
32 #include "uvc_configfs.h"
34 #include "uvc_video.h"
36 unsigned int uvc_gadget_trace_param;
38 /* --------------------------------------------------------------------------
39 * Function descriptors
42 /* string IDs are assigned dynamically */
44 #define UVC_STRING_CONTROL_IDX 0
45 #define UVC_STRING_STREAMING_IDX 1
47 static struct usb_string uvc_en_us_strings[] = {
48 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
49 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
53 static struct usb_gadget_strings uvc_stringtab = {
54 .language = 0x0409, /* en-us */
55 .strings = uvc_en_us_strings,
58 static struct usb_gadget_strings *uvc_function_strings[] = {
63 #define UVC_INTF_VIDEO_CONTROL 0
64 #define UVC_INTF_VIDEO_STREAMING 1
66 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
68 static struct usb_interface_assoc_descriptor uvc_iad = {
69 .bLength = sizeof(uvc_iad),
70 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
73 .bFunctionClass = USB_CLASS_VIDEO,
74 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
75 .bFunctionProtocol = 0x00,
79 static struct usb_interface_descriptor uvc_control_intf = {
80 .bLength = USB_DT_INTERFACE_SIZE,
81 .bDescriptorType = USB_DT_INTERFACE,
82 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
83 .bAlternateSetting = 0,
85 .bInterfaceClass = USB_CLASS_VIDEO,
86 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
87 .bInterfaceProtocol = 0x00,
91 static struct usb_endpoint_descriptor uvc_control_ep = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_INT,
96 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
100 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
101 .bLength = sizeof(uvc_ss_control_comp),
102 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
103 /* The following 3 values can be tweaked if necessary. */
106 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
109 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
110 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
111 .bDescriptorType = USB_DT_CS_ENDPOINT,
112 .bDescriptorSubType = UVC_EP_INTERRUPT,
113 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
116 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
117 .bLength = USB_DT_INTERFACE_SIZE,
118 .bDescriptorType = USB_DT_INTERFACE,
119 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
120 .bAlternateSetting = 0,
122 .bInterfaceClass = USB_CLASS_VIDEO,
123 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
124 .bInterfaceProtocol = 0x00,
128 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
129 .bLength = USB_DT_INTERFACE_SIZE,
130 .bDescriptorType = USB_DT_INTERFACE,
131 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
132 .bAlternateSetting = 1,
134 .bInterfaceClass = USB_CLASS_VIDEO,
135 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
136 .bInterfaceProtocol = 0x00,
140 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
141 .bLength = USB_DT_ENDPOINT_SIZE,
142 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = USB_DIR_IN,
144 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
145 | USB_ENDPOINT_XFER_ISOC,
146 /* The wMaxPacketSize and bInterval values will be initialized from
151 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
152 .bLength = USB_DT_ENDPOINT_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_IN,
155 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
156 | USB_ENDPOINT_XFER_ISOC,
157 /* The wMaxPacketSize and bInterval values will be initialized from
162 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .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
174 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
175 .bLength = sizeof(uvc_ss_streaming_comp),
176 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
177 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
178 * initialized from module parameters.
182 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
183 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
184 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
188 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
189 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
190 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
194 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
195 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
196 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
197 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
201 void uvc_set_trace_param(unsigned int trace)
203 uvc_gadget_trace_param = trace;
205 EXPORT_SYMBOL(uvc_set_trace_param);
207 /* --------------------------------------------------------------------------
212 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
214 struct uvc_device *uvc = req->context;
215 struct v4l2_event v4l2_event;
216 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
218 if (uvc->event_setup_out) {
219 uvc->event_setup_out = 0;
221 memset(&v4l2_event, 0, sizeof(v4l2_event));
222 v4l2_event.type = UVC_EVENT_DATA;
223 uvc_event->data.length = req->actual;
224 memcpy(&uvc_event->data.data, req->buf, req->actual);
225 v4l2_event_queue(&uvc->vdev, &v4l2_event);
230 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
232 struct uvc_device *uvc = to_uvc(f);
233 struct v4l2_event v4l2_event;
234 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
236 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
237 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
238 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
241 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
242 INFO(f->config->cdev, "invalid request type\n");
246 /* Stall too big requests. */
247 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
250 /* Tell the complete callback to generate an event for the next request
251 * that will be enqueued by UVCIOC_SEND_RESPONSE.
253 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
254 uvc->event_length = le16_to_cpu(ctrl->wLength);
256 memset(&v4l2_event, 0, sizeof(v4l2_event));
257 v4l2_event.type = UVC_EVENT_SETUP;
258 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
259 v4l2_event_queue(&uvc->vdev, &v4l2_event);
264 void uvc_function_setup_continue(struct uvc_device *uvc)
266 struct usb_composite_dev *cdev = uvc->func.config->cdev;
268 usb_composite_setup_continue(cdev);
272 uvc_function_get_alt(struct usb_function *f, unsigned interface)
274 struct uvc_device *uvc = to_uvc(f);
276 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
278 if (interface == uvc->control_intf)
280 else if (interface != uvc->streaming_intf)
283 return uvc->video.ep->enabled ? 1 : 0;
287 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
289 struct uvc_device *uvc = to_uvc(f);
290 struct usb_composite_dev *cdev = f->config->cdev;
291 struct v4l2_event v4l2_event;
292 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
295 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
297 if (interface == uvc->control_intf) {
301 INFO(cdev, "reset UVC Control\n");
302 usb_ep_disable(uvc->control_ep);
304 if (!uvc->control_ep->desc)
305 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
308 usb_ep_enable(uvc->control_ep);
310 if (uvc->state == UVC_STATE_DISCONNECTED) {
311 memset(&v4l2_event, 0, sizeof(v4l2_event));
312 v4l2_event.type = UVC_EVENT_CONNECT;
313 uvc_event->speed = cdev->gadget->speed;
314 v4l2_event_queue(&uvc->vdev, &v4l2_event);
316 uvc->state = UVC_STATE_CONNECTED;
322 if (interface != uvc->streaming_intf)
326 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
327 return alt ? -EINVAL : 0;
332 if (uvc->state != UVC_STATE_STREAMING)
336 usb_ep_disable(uvc->video.ep);
338 memset(&v4l2_event, 0, sizeof(v4l2_event));
339 v4l2_event.type = UVC_EVENT_STREAMOFF;
340 v4l2_event_queue(&uvc->vdev, &v4l2_event);
342 uvc->state = UVC_STATE_CONNECTED;
346 if (uvc->state != UVC_STATE_CONNECTED)
352 INFO(cdev, "reset UVC\n");
353 usb_ep_disable(uvc->video.ep);
355 ret = config_ep_by_speed(f->config->cdev->gadget,
356 &(uvc->func), uvc->video.ep);
359 usb_ep_enable(uvc->video.ep);
361 memset(&v4l2_event, 0, sizeof(v4l2_event));
362 v4l2_event.type = UVC_EVENT_STREAMON;
363 v4l2_event_queue(&uvc->vdev, &v4l2_event);
364 return USB_GADGET_DELAYED_STATUS;
372 uvc_function_disable(struct usb_function *f)
374 struct uvc_device *uvc = to_uvc(f);
375 struct v4l2_event v4l2_event;
377 INFO(f->config->cdev, "uvc_function_disable\n");
379 memset(&v4l2_event, 0, sizeof(v4l2_event));
380 v4l2_event.type = UVC_EVENT_DISCONNECT;
381 v4l2_event_queue(&uvc->vdev, &v4l2_event);
383 uvc->state = UVC_STATE_DISCONNECTED;
385 usb_ep_disable(uvc->video.ep);
386 usb_ep_disable(uvc->control_ep);
389 /* --------------------------------------------------------------------------
390 * Connection / disconnection
394 uvc_function_connect(struct uvc_device *uvc)
396 struct usb_composite_dev *cdev = uvc->func.config->cdev;
399 if ((ret = usb_function_activate(&uvc->func)) < 0)
400 INFO(cdev, "UVC connect failed with %d\n", ret);
404 uvc_function_disconnect(struct uvc_device *uvc)
406 struct usb_composite_dev *cdev = uvc->func.config->cdev;
409 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
410 INFO(cdev, "UVC disconnect failed with %d\n", ret);
413 /* --------------------------------------------------------------------------
414 * USB probe and disconnect
418 uvc_register_video(struct uvc_device *uvc)
420 struct usb_composite_dev *cdev = uvc->func.config->cdev;
422 /* TODO reference counting. */
423 uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
424 uvc->vdev.fops = &uvc_v4l2_fops;
425 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
426 uvc->vdev.release = video_device_release_empty;
427 uvc->vdev.vfl_dir = VFL_DIR_TX;
428 uvc->vdev.lock = &uvc->video.mutex;
429 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
431 video_set_drvdata(&uvc->vdev, uvc);
433 return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
436 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
438 memcpy(mem, desc, (desc)->bLength); \
440 mem += (desc)->bLength; \
443 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
445 const struct usb_descriptor_header * const *__src; \
446 for (__src = src; *__src; ++__src) { \
447 memcpy(mem, *__src, (*__src)->bLength); \
449 mem += (*__src)->bLength; \
453 static struct usb_descriptor_header **
454 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
456 struct uvc_input_header_descriptor *uvc_streaming_header;
457 struct uvc_header_descriptor *uvc_control_header;
458 const struct uvc_descriptor_header * const *uvc_control_desc;
459 const struct uvc_descriptor_header * const *uvc_streaming_cls;
460 const struct usb_descriptor_header * const *uvc_streaming_std;
461 const struct usb_descriptor_header * const *src;
462 struct usb_descriptor_header **dst;
463 struct usb_descriptor_header **hdr;
464 unsigned int control_size;
465 unsigned int streaming_size;
471 case USB_SPEED_SUPER:
472 uvc_control_desc = uvc->desc.ss_control;
473 uvc_streaming_cls = uvc->desc.ss_streaming;
474 uvc_streaming_std = uvc_ss_streaming;
478 uvc_control_desc = uvc->desc.fs_control;
479 uvc_streaming_cls = uvc->desc.hs_streaming;
480 uvc_streaming_std = uvc_hs_streaming;
485 uvc_control_desc = uvc->desc.fs_control;
486 uvc_streaming_cls = uvc->desc.fs_streaming;
487 uvc_streaming_std = uvc_fs_streaming;
491 if (!uvc_control_desc || !uvc_streaming_cls)
492 return ERR_PTR(-ENODEV);
494 /* Descriptors layout
498 * Class-specific UVC control descriptors
501 * uvc_ss_control_comp (for SS only)
502 * uvc_streaming_intf_alt0
503 * Class-specific UVC streaming descriptors
504 * uvc_{fs|hs}_streaming
507 /* Count descriptors and compute their size. */
510 bytes = uvc_iad.bLength + uvc_control_intf.bLength
511 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
512 + uvc_streaming_intf_alt0.bLength;
514 if (speed == USB_SPEED_SUPER) {
515 bytes += uvc_ss_control_comp.bLength;
521 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
523 control_size += (*src)->bLength;
524 bytes += (*src)->bLength;
527 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
529 streaming_size += (*src)->bLength;
530 bytes += (*src)->bLength;
533 for (src = uvc_streaming_std; *src; ++src) {
534 bytes += (*src)->bLength;
538 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
544 mem += (n_desc + 1) * sizeof(*src);
546 /* Copy the descriptors. */
547 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
548 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
550 uvc_control_header = mem;
551 UVC_COPY_DESCRIPTORS(mem, dst,
552 (const struct usb_descriptor_header **)uvc_control_desc);
553 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
554 uvc_control_header->bInCollection = 1;
555 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
557 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
558 if (speed == USB_SPEED_SUPER)
559 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
561 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
562 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
564 uvc_streaming_header = mem;
565 UVC_COPY_DESCRIPTORS(mem, dst,
566 (const struct usb_descriptor_header**)uvc_streaming_cls);
567 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
568 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
570 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
577 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
579 struct usb_composite_dev *cdev = c->cdev;
580 struct uvc_device *uvc = to_uvc(f);
581 struct usb_string *us;
582 unsigned int max_packet_mult;
583 unsigned int max_packet_size;
585 struct f_uvc_opts *opts;
588 INFO(cdev, "uvc_function_bind\n");
590 opts = fi_to_f_uvc_opts(f->fi);
591 /* Sanity check the streaming endpoint module parameters.
593 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
594 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
595 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
597 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
598 if (opts->streaming_maxburst &&
599 (opts->streaming_maxpacket % 1024) != 0) {
600 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
601 INFO(cdev, "overriding streaming_maxpacket to %d\n",
602 opts->streaming_maxpacket);
605 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
608 * NOTE: We assume that the user knows what they are doing and won't
609 * give parameters that their UDC doesn't support.
611 if (opts->streaming_maxpacket <= 1024) {
613 max_packet_size = opts->streaming_maxpacket;
614 } else if (opts->streaming_maxpacket <= 2048) {
616 max_packet_size = opts->streaming_maxpacket / 2;
619 max_packet_size = opts->streaming_maxpacket / 3;
622 uvc_fs_streaming_ep.wMaxPacketSize =
623 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
624 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
626 uvc_hs_streaming_ep.wMaxPacketSize =
627 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
629 /* A high-bandwidth endpoint must specify a bInterval value of 1 */
630 if (max_packet_mult > 1)
631 uvc_hs_streaming_ep.bInterval = 1;
633 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
635 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
636 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
637 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
638 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
639 uvc_ss_streaming_comp.wBytesPerInterval =
640 cpu_to_le16(max_packet_size * max_packet_mult *
641 (opts->streaming_maxburst + 1));
643 /* Allocate endpoints. */
644 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
646 INFO(cdev, "Unable to allocate control EP\n");
649 uvc->control_ep = ep;
651 if (gadget_is_superspeed(c->cdev->gadget))
652 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
653 &uvc_ss_streaming_comp);
654 else if (gadget_is_dualspeed(cdev->gadget))
655 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
657 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
660 INFO(cdev, "Unable to allocate streaming EP\n");
665 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
666 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
667 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
669 us = usb_gstrings_attach(cdev, uvc_function_strings,
670 ARRAY_SIZE(uvc_en_us_strings));
675 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
676 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
677 ret = us[UVC_STRING_STREAMING_IDX].id;
678 uvc_streaming_intf_alt0.iInterface = ret;
679 uvc_streaming_intf_alt1.iInterface = ret;
681 /* Allocate interface IDs. */
682 if ((ret = usb_interface_id(c, f)) < 0)
684 uvc_iad.bFirstInterface = ret;
685 uvc_control_intf.bInterfaceNumber = ret;
686 uvc->control_intf = ret;
688 if ((ret = usb_interface_id(c, f)) < 0)
690 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
691 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
692 uvc->streaming_intf = ret;
694 /* Copy descriptors */
695 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
696 if (IS_ERR(f->fs_descriptors)) {
697 ret = PTR_ERR(f->fs_descriptors);
698 f->fs_descriptors = NULL;
701 if (gadget_is_dualspeed(cdev->gadget)) {
702 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
703 if (IS_ERR(f->hs_descriptors)) {
704 ret = PTR_ERR(f->hs_descriptors);
705 f->hs_descriptors = NULL;
709 if (gadget_is_superspeed(c->cdev->gadget)) {
710 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
711 if (IS_ERR(f->ss_descriptors)) {
712 ret = PTR_ERR(f->ss_descriptors);
713 f->ss_descriptors = NULL;
718 /* Preallocate control endpoint request. */
719 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
720 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
721 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
726 uvc->control_req->buf = uvc->control_buf;
727 uvc->control_req->complete = uvc_function_ep0_complete;
728 uvc->control_req->context = uvc;
730 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
731 printk(KERN_INFO "v4l2_device_register failed\n");
735 /* Initialise video. */
736 ret = uvcg_video_init(&uvc->video);
740 /* Register a V4L2 device. */
741 ret = uvc_register_video(uvc);
743 printk(KERN_INFO "Unable to register video device\n");
750 v4l2_device_unregister(&uvc->v4l2_dev);
752 if (uvc->control_req)
753 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
754 kfree(uvc->control_buf);
756 usb_free_all_descriptors(f);
760 /* --------------------------------------------------------------------------
761 * USB gadget function
764 static void uvc_free_inst(struct usb_function_instance *f)
766 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
768 mutex_destroy(&opts->lock);
772 static struct usb_function_instance *uvc_alloc_inst(void)
774 struct f_uvc_opts *opts;
775 struct uvc_camera_terminal_descriptor *cd;
776 struct uvc_processing_unit_descriptor *pd;
777 struct uvc_output_terminal_descriptor *od;
778 struct uvc_color_matching_descriptor *md;
779 struct uvc_descriptor_header **ctl_cls;
781 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
783 return ERR_PTR(-ENOMEM);
784 opts->func_inst.free_func_inst = uvc_free_inst;
785 mutex_init(&opts->lock);
787 cd = &opts->uvc_camera_terminal;
788 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
789 cd->bDescriptorType = USB_DT_CS_INTERFACE;
790 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
792 cd->wTerminalType = cpu_to_le16(0x0201);
793 cd->bAssocTerminal = 0;
795 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
796 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
797 cd->wOcularFocalLength = cpu_to_le16(0);
798 cd->bControlSize = 3;
799 cd->bmControls[0] = 2;
800 cd->bmControls[1] = 0;
801 cd->bmControls[2] = 0;
803 pd = &opts->uvc_processing;
804 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
805 pd->bDescriptorType = USB_DT_CS_INTERFACE;
806 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
809 pd->wMaxMultiplier = cpu_to_le16(16*1024);
810 pd->bControlSize = 2;
811 pd->bmControls[0] = 1;
812 pd->bmControls[1] = 0;
815 od = &opts->uvc_output_terminal;
816 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
817 od->bDescriptorType = USB_DT_CS_INTERFACE;
818 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
820 od->wTerminalType = cpu_to_le16(0x0101);
821 od->bAssocTerminal = 0;
825 md = &opts->uvc_color_matching;
826 md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
827 md->bDescriptorType = USB_DT_CS_INTERFACE;
828 md->bDescriptorSubType = UVC_VS_COLORFORMAT;
829 md->bColorPrimaries = 1;
830 md->bTransferCharacteristics = 1;
831 md->bMatrixCoefficients = 4;
833 /* Prepare fs control class descriptors for configfs-based gadgets */
834 ctl_cls = opts->uvc_fs_control_cls;
835 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
836 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
837 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
838 ctl_cls[3] = (struct uvc_descriptor_header *)od;
839 ctl_cls[4] = NULL; /* NULL-terminate */
841 (const struct uvc_descriptor_header * const *)ctl_cls;
843 /* Prepare hs control class descriptors for configfs-based gadgets */
844 ctl_cls = opts->uvc_ss_control_cls;
845 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
846 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
847 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
848 ctl_cls[3] = (struct uvc_descriptor_header *)od;
849 ctl_cls[4] = NULL; /* NULL-terminate */
851 (const struct uvc_descriptor_header * const *)ctl_cls;
853 opts->streaming_interval = 1;
854 opts->streaming_maxpacket = 1024;
856 uvcg_attach_configfs(opts);
857 return &opts->func_inst;
860 static void uvc_free(struct usb_function *f)
862 struct uvc_device *uvc = to_uvc(f);
863 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
869 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
871 struct usb_composite_dev *cdev = c->cdev;
872 struct uvc_device *uvc = to_uvc(f);
874 INFO(cdev, "%s\n", __func__);
876 video_unregister_device(&uvc->vdev);
877 v4l2_device_unregister(&uvc->v4l2_dev);
879 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
880 kfree(uvc->control_buf);
882 usb_free_all_descriptors(f);
885 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
887 struct uvc_device *uvc;
888 struct f_uvc_opts *opts;
889 struct uvc_descriptor_header **strm_cls;
891 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
893 return ERR_PTR(-ENOMEM);
895 mutex_init(&uvc->video.mutex);
896 uvc->state = UVC_STATE_DISCONNECTED;
897 opts = fi_to_f_uvc_opts(fi);
899 mutex_lock(&opts->lock);
900 if (opts->uvc_fs_streaming_cls) {
901 strm_cls = opts->uvc_fs_streaming_cls;
903 (const struct uvc_descriptor_header * const *)strm_cls;
905 if (opts->uvc_hs_streaming_cls) {
906 strm_cls = opts->uvc_hs_streaming_cls;
908 (const struct uvc_descriptor_header * const *)strm_cls;
910 if (opts->uvc_ss_streaming_cls) {
911 strm_cls = opts->uvc_ss_streaming_cls;
913 (const struct uvc_descriptor_header * const *)strm_cls;
916 uvc->desc.fs_control = opts->fs_control;
917 uvc->desc.ss_control = opts->ss_control;
918 uvc->desc.fs_streaming = opts->fs_streaming;
919 uvc->desc.hs_streaming = opts->hs_streaming;
920 uvc->desc.ss_streaming = opts->ss_streaming;
922 mutex_unlock(&opts->lock);
924 /* Register the function. */
925 uvc->func.name = "uvc";
926 uvc->func.bind = uvc_function_bind;
927 uvc->func.unbind = uvc_unbind;
928 uvc->func.get_alt = uvc_function_get_alt;
929 uvc->func.set_alt = uvc_function_set_alt;
930 uvc->func.disable = uvc_function_disable;
931 uvc->func.setup = uvc_function_setup;
932 uvc->func.free_func = uvc_free;
933 uvc->func.bind_deactivated = true;
938 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
939 MODULE_LICENSE("GPL");
940 MODULE_AUTHOR("Laurent Pinchart");