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
;
339 video
->fops
= &uvc_v4l2_fops
;
340 video
->release
= video_device_release
;
341 strncpy(video
->name
, cdev
->gadget
->name
, sizeof(video
->name
));
344 video_set_drvdata(video
, uvc
);
346 return video_register_device(video
, VFL_TYPE_GRABBER
, -1);
349 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
351 memcpy(mem, desc, (desc)->bLength); \
353 mem += (desc)->bLength; \
356 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
358 const struct usb_descriptor_header * const *__src; \
359 for (__src = src; *__src; ++__src) { \
360 memcpy(mem, *__src, (*__src)->bLength); \
362 mem += (*__src)->bLength; \
366 static struct usb_descriptor_header
** __init
367 uvc_copy_descriptors(struct uvc_device
*uvc
, enum usb_device_speed speed
)
369 struct uvc_input_header_descriptor
*uvc_streaming_header
;
370 struct uvc_header_descriptor
*uvc_control_header
;
371 const struct uvc_descriptor_header
* const *uvc_streaming_cls
;
372 const struct usb_descriptor_header
* const *uvc_streaming_std
;
373 const struct usb_descriptor_header
* const *src
;
374 struct usb_descriptor_header
**dst
;
375 struct usb_descriptor_header
**hdr
;
376 unsigned int control_size
;
377 unsigned int streaming_size
;
382 uvc_streaming_cls
= (speed
== USB_SPEED_FULL
)
383 ? uvc
->desc
.fs_streaming
: uvc
->desc
.hs_streaming
;
384 uvc_streaming_std
= (speed
== USB_SPEED_FULL
)
385 ? uvc_fs_streaming
: uvc_hs_streaming
;
387 /* Descriptors layout
391 * Class-specific UVC control descriptors
394 * uvc_streaming_intf_alt0
395 * Class-specific UVC streaming descriptors
396 * uvc_{fs|hs}_streaming
399 /* Count descriptors and compute their size. */
402 bytes
= uvc_iad
.bLength
+ uvc_control_intf
.bLength
403 + uvc_control_ep
.bLength
+ uvc_control_cs_ep
.bLength
404 + uvc_streaming_intf_alt0
.bLength
;
407 for (src
= (const struct usb_descriptor_header
**)uvc
->desc
.control
; *src
; ++src
) {
408 control_size
+= (*src
)->bLength
;
409 bytes
+= (*src
)->bLength
;
412 for (src
= (const struct usb_descriptor_header
**)uvc_streaming_cls
; *src
; ++src
) {
413 streaming_size
+= (*src
)->bLength
;
414 bytes
+= (*src
)->bLength
;
417 for (src
= uvc_streaming_std
; *src
; ++src
) {
418 bytes
+= (*src
)->bLength
;
422 mem
= kmalloc((n_desc
+ 1) * sizeof(*src
) + bytes
, GFP_KERNEL
);
428 mem
+= (n_desc
+ 1) * sizeof(*src
);
430 /* Copy the descriptors. */
431 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_iad
);
432 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_intf
);
434 uvc_control_header
= mem
;
435 UVC_COPY_DESCRIPTORS(mem
, dst
,
436 (const struct usb_descriptor_header
**)uvc
->desc
.control
);
437 uvc_control_header
->wTotalLength
= cpu_to_le16(control_size
);
438 uvc_control_header
->bInCollection
= 1;
439 uvc_control_header
->baInterfaceNr
[0] = uvc
->streaming_intf
;
441 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_ep
);
442 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_cs_ep
);
443 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_streaming_intf_alt0
);
445 uvc_streaming_header
= mem
;
446 UVC_COPY_DESCRIPTORS(mem
, dst
,
447 (const struct usb_descriptor_header
**)uvc_streaming_cls
);
448 uvc_streaming_header
->wTotalLength
= cpu_to_le16(streaming_size
);
449 uvc_streaming_header
->bEndpointAddress
= uvc_streaming_ep
.bEndpointAddress
;
451 UVC_COPY_DESCRIPTORS(mem
, dst
, uvc_streaming_std
);
458 uvc_function_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
460 struct usb_composite_dev
*cdev
= c
->cdev
;
461 struct uvc_device
*uvc
= to_uvc(f
);
463 INFO(cdev
, "uvc_function_unbind\n");
466 if (uvc
->vdev
->minor
== -1)
467 video_device_release(uvc
->vdev
);
469 video_unregister_device(uvc
->vdev
);
474 uvc
->control_ep
->driver_data
= NULL
;
476 uvc
->video
.ep
->driver_data
= NULL
;
478 if (uvc
->control_req
) {
479 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
480 kfree(uvc
->control_buf
);
483 kfree(f
->descriptors
);
484 kfree(f
->hs_descriptors
);
490 uvc_function_bind(struct usb_configuration
*c
, struct usb_function
*f
)
492 struct usb_composite_dev
*cdev
= c
->cdev
;
493 struct uvc_device
*uvc
= to_uvc(f
);
497 INFO(cdev
, "uvc_function_bind\n");
499 /* Allocate endpoints. */
500 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_control_ep
);
502 INFO(cdev
, "Unable to allocate control EP\n");
505 uvc
->control_ep
= ep
;
506 ep
->driver_data
= uvc
;
508 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_streaming_ep
);
510 INFO(cdev
, "Unable to allocate streaming EP\n");
514 ep
->driver_data
= uvc
;
516 /* Allocate interface IDs. */
517 if ((ret
= usb_interface_id(c
, f
)) < 0)
519 uvc_iad
.bFirstInterface
= ret
;
520 uvc_control_intf
.bInterfaceNumber
= ret
;
521 uvc
->control_intf
= ret
;
523 if ((ret
= usb_interface_id(c
, f
)) < 0)
525 uvc_streaming_intf_alt0
.bInterfaceNumber
= ret
;
526 uvc_streaming_intf_alt1
.bInterfaceNumber
= ret
;
527 uvc
->streaming_intf
= ret
;
529 /* Copy descriptors. */
530 f
->descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_FULL
);
531 f
->hs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_HIGH
);
533 /* Preallocate control endpoint request. */
534 uvc
->control_req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
535 uvc
->control_buf
= kmalloc(UVC_MAX_REQUEST_SIZE
, GFP_KERNEL
);
536 if (uvc
->control_req
== NULL
|| uvc
->control_buf
== NULL
) {
541 uvc
->control_req
->buf
= uvc
->control_buf
;
542 uvc
->control_req
->complete
= uvc_function_ep0_complete
;
543 uvc
->control_req
->context
= uvc
;
545 /* Avoid letting this gadget enumerate until the userspace server is
548 if ((ret
= usb_function_deactivate(f
)) < 0)
551 /* Initialise video. */
552 ret
= uvc_video_init(&uvc
->video
);
556 /* Register a V4L2 device. */
557 ret
= uvc_register_video(uvc
);
559 printk(KERN_INFO
"Unable to register video device\n");
566 uvc_function_unbind(c
, f
);
570 /* --------------------------------------------------------------------------
571 * USB gadget function
575 * uvc_bind_config - add a UVC function to a configuration
576 * @c: the configuration to support the UVC instance
577 * Context: single threaded during gadget setup
579 * Returns zero on success, else negative errno.
581 * Caller must have called @uvc_setup(). Caller is also responsible for
582 * calling @uvc_cleanup() before module unload.
585 uvc_bind_config(struct usb_configuration
*c
,
586 const struct uvc_descriptor_header
* const *control
,
587 const struct uvc_descriptor_header
* const *fs_streaming
,
588 const struct uvc_descriptor_header
* const *hs_streaming
)
590 struct uvc_device
*uvc
;
593 /* TODO Check if the USB device controller supports the required
596 if (!gadget_is_dualspeed(c
->cdev
->gadget
))
599 uvc
= kzalloc(sizeof(*uvc
), GFP_KERNEL
);
603 uvc
->state
= UVC_STATE_DISCONNECTED
;
605 /* Validate the descriptors. */
606 if (control
== NULL
|| control
[0] == NULL
||
607 control
[0]->bDescriptorSubType
!= UVC_VC_HEADER
)
610 if (fs_streaming
== NULL
|| fs_streaming
[0] == NULL
||
611 fs_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
614 if (hs_streaming
== NULL
|| hs_streaming
[0] == NULL
||
615 hs_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
618 uvc
->desc
.control
= control
;
619 uvc
->desc
.fs_streaming
= fs_streaming
;
620 uvc
->desc
.hs_streaming
= hs_streaming
;
622 /* Allocate string descriptor numbers. */
623 if ((ret
= usb_string_id(c
->cdev
)) < 0)
625 uvc_en_us_strings
[UVC_STRING_ASSOCIATION_IDX
].id
= ret
;
626 uvc_iad
.iFunction
= ret
;
628 if ((ret
= usb_string_id(c
->cdev
)) < 0)
630 uvc_en_us_strings
[UVC_STRING_CONTROL_IDX
].id
= ret
;
631 uvc_control_intf
.iInterface
= ret
;
633 if ((ret
= usb_string_id(c
->cdev
)) < 0)
635 uvc_en_us_strings
[UVC_STRING_STREAMING_IDX
].id
= ret
;
636 uvc_streaming_intf_alt0
.iInterface
= ret
;
637 uvc_streaming_intf_alt1
.iInterface
= ret
;
639 /* Register the function. */
640 uvc
->func
.name
= "uvc";
641 uvc
->func
.strings
= uvc_function_strings
;
642 uvc
->func
.bind
= uvc_function_bind
;
643 uvc
->func
.unbind
= uvc_function_unbind
;
644 uvc
->func
.get_alt
= uvc_function_get_alt
;
645 uvc
->func
.set_alt
= uvc_function_set_alt
;
646 uvc
->func
.disable
= uvc_function_disable
;
647 uvc
->func
.setup
= uvc_function_setup
;
649 ret
= usb_add_function(c
, &uvc
->func
);
660 module_param_named(trace
, uvc_gadget_trace_param
, uint
, S_IRUGO
|S_IWUSR
);
661 MODULE_PARM_DESC(trace
, "Trace level bitmask");