2 * uvc_gadget.c -- USB Video Class Gadget driver
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/video.h>
22 #include <linux/vmalloc.h>
23 #include <linux/wait.h>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-event.h>
30 unsigned int uvc_gadget_trace_param;
32 /* --------------------------------------------------------------------------
33 * Function descriptors
36 /* string IDs are assigned dynamically */
38 #define UVC_STRING_ASSOCIATION_IDX 0
39 #define UVC_STRING_CONTROL_IDX 1
40 #define UVC_STRING_STREAMING_IDX 2
42 static struct usb_string uvc_en_us_strings[] = {
43 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
44 [UVC_STRING_CONTROL_IDX].s = "Video Control",
45 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
49 static struct usb_gadget_strings uvc_stringtab = {
50 .language = 0x0409, /* en-us */
51 .strings = uvc_en_us_strings,
54 static struct usb_gadget_strings *uvc_function_strings[] = {
59 #define UVC_INTF_VIDEO_CONTROL 0
60 #define UVC_INTF_VIDEO_STREAMING 1
62 static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
63 .bLength = sizeof(uvc_iad),
64 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
67 .bFunctionClass = USB_CLASS_VIDEO,
68 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
69 .bFunctionProtocol = 0x00,
73 static struct usb_interface_descriptor uvc_control_intf __initdata = {
74 .bLength = USB_DT_INTERFACE_SIZE,
75 .bDescriptorType = USB_DT_INTERFACE,
76 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
77 .bAlternateSetting = 0,
79 .bInterfaceClass = USB_CLASS_VIDEO,
80 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
81 .bInterfaceProtocol = 0x00,
85 static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
86 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
88 .bEndpointAddress = USB_DIR_IN,
89 .bmAttributes = USB_ENDPOINT_XFER_INT,
90 .wMaxPacketSize = cpu_to_le16(16),
94 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
95 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
96 .bDescriptorType = USB_DT_CS_ENDPOINT,
97 .bDescriptorSubType = UVC_EP_INTERRUPT,
98 .wMaxTransferSize = cpu_to_le16(16),
101 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
102 .bLength = USB_DT_INTERFACE_SIZE,
103 .bDescriptorType = USB_DT_INTERFACE,
104 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
105 .bAlternateSetting = 0,
107 .bInterfaceClass = USB_CLASS_VIDEO,
108 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
109 .bInterfaceProtocol = 0x00,
113 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
114 .bLength = USB_DT_INTERFACE_SIZE,
115 .bDescriptorType = USB_DT_INTERFACE,
116 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
117 .bAlternateSetting = 1,
119 .bInterfaceClass = USB_CLASS_VIDEO,
120 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
121 .bInterfaceProtocol = 0x00,
125 static struct usb_endpoint_descriptor uvc_streaming_ep = {
126 .bLength = USB_DT_ENDPOINT_SIZE,
127 .bDescriptorType = USB_DT_ENDPOINT,
128 .bEndpointAddress = USB_DIR_IN,
129 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
130 .wMaxPacketSize = cpu_to_le16(512),
134 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
135 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
136 (struct usb_descriptor_header *) &uvc_streaming_ep,
140 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
141 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
142 (struct usb_descriptor_header *) &uvc_streaming_ep,
146 /* --------------------------------------------------------------------------
151 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
153 struct uvc_device *uvc = req->context;
154 struct v4l2_event v4l2_event;
155 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
157 if (uvc->event_setup_out) {
158 uvc->event_setup_out = 0;
160 memset(&v4l2_event, 0, sizeof(v4l2_event));
161 v4l2_event.type = UVC_EVENT_DATA;
162 uvc_event->data.length = req->actual;
163 memcpy(&uvc_event->data.data, req->buf, req->actual);
164 v4l2_event_queue(uvc->vdev, &v4l2_event);
169 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
171 struct uvc_device *uvc = to_uvc(f);
172 struct v4l2_event v4l2_event;
173 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
175 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
176 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
177 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
180 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
181 INFO(f->config->cdev, "invalid request type\n");
185 /* Stall too big requests. */
186 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
189 memset(&v4l2_event, 0, sizeof(v4l2_event));
190 v4l2_event.type = UVC_EVENT_SETUP;
191 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
192 v4l2_event_queue(uvc->vdev, &v4l2_event);
198 uvc_function_get_alt(struct usb_function *f, unsigned interface)
200 struct uvc_device *uvc = to_uvc(f);
202 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
204 if (interface == uvc->control_intf)
206 else if (interface != uvc->streaming_intf)
209 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
213 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
215 struct uvc_device *uvc = to_uvc(f);
216 struct v4l2_event v4l2_event;
217 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
219 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
221 if (interface == uvc->control_intf) {
225 if (uvc->state == UVC_STATE_DISCONNECTED) {
226 memset(&v4l2_event, 0, sizeof(v4l2_event));
227 v4l2_event.type = UVC_EVENT_CONNECT;
228 uvc_event->speed = f->config->cdev->gadget->speed;
229 v4l2_event_queue(uvc->vdev, &v4l2_event);
231 uvc->state = UVC_STATE_CONNECTED;
237 if (interface != uvc->streaming_intf)
241 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
242 return alt ? -EINVAL : 0;
247 if (uvc->state != UVC_STATE_STREAMING)
251 usb_ep_disable(uvc->video.ep);
253 memset(&v4l2_event, 0, sizeof(v4l2_event));
254 v4l2_event.type = UVC_EVENT_STREAMOFF;
255 v4l2_event_queue(uvc->vdev, &v4l2_event);
257 uvc->state = UVC_STATE_CONNECTED;
261 if (uvc->state != UVC_STATE_CONNECTED)
265 uvc->video.ep->desc = &uvc_streaming_ep;
266 usb_ep_enable(uvc->video.ep);
269 memset(&v4l2_event, 0, sizeof(v4l2_event));
270 v4l2_event.type = UVC_EVENT_STREAMON;
271 v4l2_event_queue(uvc->vdev, &v4l2_event);
273 uvc->state = UVC_STATE_STREAMING;
284 uvc_function_disable(struct usb_function *f)
286 struct uvc_device *uvc = to_uvc(f);
287 struct v4l2_event v4l2_event;
289 INFO(f->config->cdev, "uvc_function_disable\n");
291 memset(&v4l2_event, 0, sizeof(v4l2_event));
292 v4l2_event.type = UVC_EVENT_DISCONNECT;
293 v4l2_event_queue(uvc->vdev, &v4l2_event);
295 uvc->state = UVC_STATE_DISCONNECTED;
298 /* --------------------------------------------------------------------------
299 * Connection / disconnection
303 uvc_function_connect(struct uvc_device *uvc)
305 struct usb_composite_dev *cdev = uvc->func.config->cdev;
308 if ((ret = usb_function_activate(&uvc->func)) < 0)
309 INFO(cdev, "UVC connect failed with %d\n", ret);
313 uvc_function_disconnect(struct uvc_device *uvc)
315 struct usb_composite_dev *cdev = uvc->func.config->cdev;
318 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
319 INFO(cdev, "UVC disconnect failed with %d\n", ret);
322 /* --------------------------------------------------------------------------
323 * USB probe and disconnect
327 uvc_register_video(struct uvc_device *uvc)
329 struct usb_composite_dev *cdev = uvc->func.config->cdev;
330 struct video_device *video;
332 /* TODO reference counting. */
333 video = video_device_alloc();
337 video->parent = &cdev->gadget->dev;
338 video->fops = &uvc_v4l2_fops;
339 video->release = video_device_release;
340 strncpy(video->name, cdev->gadget->name, sizeof(video->name));
343 video_set_drvdata(video, uvc);
345 return video_register_device(video, VFL_TYPE_GRABBER, -1);
348 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
350 memcpy(mem, desc, (desc)->bLength); \
352 mem += (desc)->bLength; \
355 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
357 const struct usb_descriptor_header * const *__src; \
358 for (__src = src; *__src; ++__src) { \
359 memcpy(mem, *__src, (*__src)->bLength); \
361 mem += (*__src)->bLength; \
365 static struct usb_descriptor_header ** __init
366 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
368 struct uvc_input_header_descriptor *uvc_streaming_header;
369 struct uvc_header_descriptor *uvc_control_header;
370 const struct uvc_descriptor_header * const *uvc_streaming_cls;
371 const struct usb_descriptor_header * const *uvc_streaming_std;
372 const struct usb_descriptor_header * const *src;
373 struct usb_descriptor_header **dst;
374 struct usb_descriptor_header **hdr;
375 unsigned int control_size;
376 unsigned int streaming_size;
381 uvc_streaming_cls = (speed == USB_SPEED_FULL)
382 ? uvc->desc.fs_streaming : uvc->desc.hs_streaming;
383 uvc_streaming_std = (speed == USB_SPEED_FULL)
384 ? uvc_fs_streaming : uvc_hs_streaming;
386 /* Descriptors layout
390 * Class-specific UVC control descriptors
393 * uvc_streaming_intf_alt0
394 * Class-specific UVC streaming descriptors
395 * uvc_{fs|hs}_streaming
398 /* Count descriptors and compute their size. */
401 bytes = uvc_iad.bLength + uvc_control_intf.bLength
402 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
403 + uvc_streaming_intf_alt0.bLength;
406 for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) {
407 control_size += (*src)->bLength;
408 bytes += (*src)->bLength;
411 for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) {
412 streaming_size += (*src)->bLength;
413 bytes += (*src)->bLength;
416 for (src = uvc_streaming_std; *src; ++src) {
417 bytes += (*src)->bLength;
421 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
427 mem += (n_desc + 1) * sizeof(*src);
429 /* Copy the descriptors. */
430 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
431 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
433 uvc_control_header = mem;
434 UVC_COPY_DESCRIPTORS(mem, dst,
435 (const struct usb_descriptor_header**)uvc->desc.control);
436 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
437 uvc_control_header->bInCollection = 1;
438 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
440 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
441 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
442 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
444 uvc_streaming_header = mem;
445 UVC_COPY_DESCRIPTORS(mem, dst,
446 (const struct usb_descriptor_header**)uvc_streaming_cls);
447 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
448 uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress;
450 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
457 uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
459 struct usb_composite_dev *cdev = c->cdev;
460 struct uvc_device *uvc = to_uvc(f);
462 INFO(cdev, "uvc_function_unbind\n");
464 video_unregister_device(uvc->vdev);
465 uvc->control_ep->driver_data = NULL;
466 uvc->video.ep->driver_data = NULL;
468 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
469 kfree(uvc->control_buf);
471 kfree(f->descriptors);
472 kfree(f->hs_descriptors);
478 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
480 struct usb_composite_dev *cdev = c->cdev;
481 struct uvc_device *uvc = to_uvc(f);
485 INFO(cdev, "uvc_function_bind\n");
487 /* Allocate endpoints. */
488 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
490 INFO(cdev, "Unable to allocate control EP\n");
493 uvc->control_ep = ep;
494 ep->driver_data = uvc;
496 ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep);
498 INFO(cdev, "Unable to allocate streaming EP\n");
502 ep->driver_data = uvc;
504 /* Allocate interface IDs. */
505 if ((ret = usb_interface_id(c, f)) < 0)
507 uvc_iad.bFirstInterface = ret;
508 uvc_control_intf.bInterfaceNumber = ret;
509 uvc->control_intf = ret;
511 if ((ret = usb_interface_id(c, f)) < 0)
513 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
514 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
515 uvc->streaming_intf = ret;
517 /* Copy descriptors. */
518 f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
519 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
521 /* Preallocate control endpoint request. */
522 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
523 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
524 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
529 uvc->control_req->buf = uvc->control_buf;
530 uvc->control_req->complete = uvc_function_ep0_complete;
531 uvc->control_req->context = uvc;
533 /* Avoid letting this gadget enumerate until the userspace server is
536 if ((ret = usb_function_deactivate(f)) < 0)
539 /* Initialise video. */
540 ret = uvc_video_init(&uvc->video);
544 /* Register a V4L2 device. */
545 ret = uvc_register_video(uvc);
547 printk(KERN_INFO "Unable to register video device\n");
555 video_device_release(uvc->vdev);
558 uvc->control_ep->driver_data = NULL;
560 uvc->video.ep->driver_data = NULL;
562 if (uvc->control_req) {
563 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
564 kfree(uvc->control_buf);
567 kfree(f->descriptors);
568 kfree(f->hs_descriptors);
569 kfree(f->ss_descriptors);
573 /* --------------------------------------------------------------------------
574 * USB gadget function
578 * uvc_bind_config - add a UVC function to a configuration
579 * @c: the configuration to support the UVC instance
580 * Context: single threaded during gadget setup
582 * Returns zero on success, else negative errno.
584 * Caller must have called @uvc_setup(). Caller is also responsible for
585 * calling @uvc_cleanup() before module unload.
588 uvc_bind_config(struct usb_configuration *c,
589 const struct uvc_descriptor_header * const *control,
590 const struct uvc_descriptor_header * const *fs_streaming,
591 const struct uvc_descriptor_header * const *hs_streaming)
593 struct uvc_device *uvc;
596 /* TODO Check if the USB device controller supports the required
599 if (!gadget_is_dualspeed(c->cdev->gadget))
602 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
606 uvc->state = UVC_STATE_DISCONNECTED;
608 /* Validate the descriptors. */
609 if (control == NULL || control[0] == NULL ||
610 control[0]->bDescriptorSubType != UVC_VC_HEADER)
613 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
614 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
617 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
618 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
621 uvc->desc.control = control;
622 uvc->desc.fs_streaming = fs_streaming;
623 uvc->desc.hs_streaming = hs_streaming;
625 /* Allocate string descriptor numbers. */
626 if ((ret = usb_string_id(c->cdev)) < 0)
628 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
629 uvc_iad.iFunction = ret;
631 if ((ret = usb_string_id(c->cdev)) < 0)
633 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
634 uvc_control_intf.iInterface = ret;
636 if ((ret = usb_string_id(c->cdev)) < 0)
638 uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
639 uvc_streaming_intf_alt0.iInterface = ret;
640 uvc_streaming_intf_alt1.iInterface = ret;
642 /* Register the function. */
643 uvc->func.name = "uvc";
644 uvc->func.strings = uvc_function_strings;
645 uvc->func.bind = uvc_function_bind;
646 uvc->func.unbind = uvc_function_unbind;
647 uvc->func.get_alt = uvc_function_get_alt;
648 uvc->func.set_alt = uvc_function_set_alt;
649 uvc->func.disable = uvc_function_disable;
650 uvc->func.setup = uvc_function_setup;
652 ret = usb_add_function(c, &uvc->func);
663 module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
664 MODULE_PARM_DESC(trace, "Trace level bitmask");