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/string.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/video.h>
23 #include <linux/vmalloc.h>
24 #include <linux/wait.h>
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-event.h>
31 unsigned int uvc_gadget_trace_param
;
33 /*-------------------------------------------------------------------------*/
35 /* module parameters specific to the Video streaming endpoint */
36 static unsigned streaming_interval
= 1;
37 module_param(streaming_interval
, uint
, S_IRUGO
|S_IWUSR
);
38 MODULE_PARM_DESC(streaming_interval
, "1 - 16");
40 static unsigned streaming_maxpacket
= 1024;
41 module_param(streaming_maxpacket
, uint
, S_IRUGO
|S_IWUSR
);
42 MODULE_PARM_DESC(streaming_maxpacket
, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
44 static unsigned streaming_mult
;
45 module_param(streaming_mult
, uint
, S_IRUGO
|S_IWUSR
);
46 MODULE_PARM_DESC(streaming_mult
, "0 - 2 (hs/ss only)");
48 static unsigned streaming_maxburst
;
49 module_param(streaming_maxburst
, uint
, S_IRUGO
|S_IWUSR
);
50 MODULE_PARM_DESC(streaming_maxburst
, "0 - 15 (ss only)");
52 /* --------------------------------------------------------------------------
53 * Function descriptors
56 /* string IDs are assigned dynamically */
58 #define UVC_STRING_ASSOCIATION_IDX 0
59 #define UVC_STRING_CONTROL_IDX 1
60 #define UVC_STRING_STREAMING_IDX 2
62 static struct usb_string uvc_en_us_strings
[] = {
63 [UVC_STRING_ASSOCIATION_IDX
].s
= "UVC Camera",
64 [UVC_STRING_CONTROL_IDX
].s
= "Video Control",
65 [UVC_STRING_STREAMING_IDX
].s
= "Video Streaming",
69 static struct usb_gadget_strings uvc_stringtab
= {
70 .language
= 0x0409, /* en-us */
71 .strings
= uvc_en_us_strings
,
74 static struct usb_gadget_strings
*uvc_function_strings
[] = {
79 #define UVC_INTF_VIDEO_CONTROL 0
80 #define UVC_INTF_VIDEO_STREAMING 1
82 #define STATUS_BYTECOUNT 16 /* 16 bytes status */
84 static struct usb_interface_assoc_descriptor uvc_iad __initdata
= {
85 .bLength
= sizeof(uvc_iad
),
86 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
89 .bFunctionClass
= USB_CLASS_VIDEO
,
90 .bFunctionSubClass
= UVC_SC_VIDEO_INTERFACE_COLLECTION
,
91 .bFunctionProtocol
= 0x00,
95 static struct usb_interface_descriptor uvc_control_intf __initdata
= {
96 .bLength
= USB_DT_INTERFACE_SIZE
,
97 .bDescriptorType
= USB_DT_INTERFACE
,
98 .bInterfaceNumber
= UVC_INTF_VIDEO_CONTROL
,
99 .bAlternateSetting
= 0,
101 .bInterfaceClass
= USB_CLASS_VIDEO
,
102 .bInterfaceSubClass
= UVC_SC_VIDEOCONTROL
,
103 .bInterfaceProtocol
= 0x00,
107 static struct usb_endpoint_descriptor uvc_fs_control_ep __initdata
= {
108 .bLength
= USB_DT_ENDPOINT_SIZE
,
109 .bDescriptorType
= USB_DT_ENDPOINT
,
110 .bEndpointAddress
= USB_DIR_IN
,
111 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
112 .wMaxPacketSize
= cpu_to_le16(STATUS_BYTECOUNT
),
116 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata
= {
117 .bLength
= UVC_DT_CONTROL_ENDPOINT_SIZE
,
118 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
119 .bDescriptorSubType
= UVC_EP_INTERRUPT
,
120 .wMaxTransferSize
= cpu_to_le16(STATUS_BYTECOUNT
),
123 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata
= {
124 .bLength
= USB_DT_INTERFACE_SIZE
,
125 .bDescriptorType
= USB_DT_INTERFACE
,
126 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
127 .bAlternateSetting
= 0,
129 .bInterfaceClass
= USB_CLASS_VIDEO
,
130 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
131 .bInterfaceProtocol
= 0x00,
135 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata
= {
136 .bLength
= USB_DT_INTERFACE_SIZE
,
137 .bDescriptorType
= USB_DT_INTERFACE
,
138 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
139 .bAlternateSetting
= 1,
141 .bInterfaceClass
= USB_CLASS_VIDEO
,
142 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
143 .bInterfaceProtocol
= 0x00,
147 static struct usb_endpoint_descriptor uvc_fs_streaming_ep
= {
148 .bLength
= USB_DT_ENDPOINT_SIZE
,
149 .bDescriptorType
= USB_DT_ENDPOINT
,
150 .bEndpointAddress
= USB_DIR_IN
,
151 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
152 .wMaxPacketSize
= cpu_to_le16(512),
156 static struct usb_endpoint_descriptor uvc_hs_streaming_ep
= {
157 .bLength
= USB_DT_ENDPOINT_SIZE
,
158 .bDescriptorType
= USB_DT_ENDPOINT
,
159 .bEndpointAddress
= USB_DIR_IN
,
160 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
161 .wMaxPacketSize
= cpu_to_le16(1024),
165 /* super speed support */
166 static struct usb_endpoint_descriptor uvc_ss_control_ep __initdata
= {
167 .bLength
= USB_DT_ENDPOINT_SIZE
,
168 .bDescriptorType
= USB_DT_ENDPOINT
,
170 .bEndpointAddress
= USB_DIR_IN
,
171 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
172 .wMaxPacketSize
= cpu_to_le16(STATUS_BYTECOUNT
),
176 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata
= {
177 .bLength
= sizeof uvc_ss_control_comp
,
178 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
180 /* the following 3 values can be tweaked if necessary */
181 /* .bMaxBurst = 0, */
182 /* .bmAttributes = 0, */
183 .wBytesPerInterval
= cpu_to_le16(STATUS_BYTECOUNT
),
186 static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata
= {
187 .bLength
= USB_DT_ENDPOINT_SIZE
,
188 .bDescriptorType
= USB_DT_ENDPOINT
,
190 .bEndpointAddress
= USB_DIR_IN
,
191 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
192 .wMaxPacketSize
= cpu_to_le16(1024),
196 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp
= {
197 .bLength
= sizeof uvc_ss_streaming_comp
,
198 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
200 /* the following 3 values can be tweaked if necessary */
203 .wBytesPerInterval
= cpu_to_le16(1024),
206 static const struct usb_descriptor_header
* const uvc_fs_streaming
[] = {
207 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
208 (struct usb_descriptor_header
*) &uvc_fs_streaming_ep
,
212 static const struct usb_descriptor_header
* const uvc_hs_streaming
[] = {
213 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
214 (struct usb_descriptor_header
*) &uvc_hs_streaming_ep
,
218 static const struct usb_descriptor_header
* const uvc_ss_streaming
[] = {
219 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
220 (struct usb_descriptor_header
*) &uvc_ss_streaming_ep
,
221 (struct usb_descriptor_header
*) &uvc_ss_streaming_comp
,
225 /* --------------------------------------------------------------------------
230 uvc_function_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
232 struct uvc_device
*uvc
= req
->context
;
233 struct v4l2_event v4l2_event
;
234 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
236 if (uvc
->event_setup_out
) {
237 uvc
->event_setup_out
= 0;
239 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
240 v4l2_event
.type
= UVC_EVENT_DATA
;
241 uvc_event
->data
.length
= req
->actual
;
242 memcpy(&uvc_event
->data
.data
, req
->buf
, req
->actual
);
243 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
248 uvc_function_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
250 struct uvc_device
*uvc
= to_uvc(f
);
251 struct v4l2_event v4l2_event
;
252 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
254 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
255 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
256 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
259 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_CLASS
) {
260 INFO(f
->config
->cdev
, "invalid request type\n");
264 /* Stall too big requests. */
265 if (le16_to_cpu(ctrl
->wLength
) > UVC_MAX_REQUEST_SIZE
)
268 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
269 v4l2_event
.type
= UVC_EVENT_SETUP
;
270 memcpy(&uvc_event
->req
, ctrl
, sizeof(uvc_event
->req
));
271 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
277 uvc_function_get_alt(struct usb_function
*f
, unsigned interface
)
279 struct uvc_device
*uvc
= to_uvc(f
);
281 INFO(f
->config
->cdev
, "uvc_function_get_alt(%u)\n", interface
);
283 if (interface
== uvc
->control_intf
)
285 else if (interface
!= uvc
->streaming_intf
)
288 return uvc
->state
== UVC_STATE_STREAMING
? 1 : 0;
292 uvc_function_set_alt(struct usb_function
*f
, unsigned interface
, unsigned alt
)
294 struct uvc_device
*uvc
= to_uvc(f
);
295 struct v4l2_event v4l2_event
;
296 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
299 INFO(f
->config
->cdev
, "uvc_function_set_alt(%u, %u)\n", interface
, alt
);
301 if (interface
== uvc
->control_intf
) {
305 if (uvc
->state
== UVC_STATE_DISCONNECTED
) {
306 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
307 v4l2_event
.type
= UVC_EVENT_CONNECT
;
308 uvc_event
->speed
= f
->config
->cdev
->gadget
->speed
;
309 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
311 uvc
->state
= UVC_STATE_CONNECTED
;
317 if (interface
!= uvc
->streaming_intf
)
321 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
322 return alt ? -EINVAL : 0;
327 if (uvc
->state
!= UVC_STATE_STREAMING
)
331 usb_ep_disable(uvc
->video
.ep
);
333 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
334 v4l2_event
.type
= UVC_EVENT_STREAMOFF
;
335 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
337 uvc
->state
= UVC_STATE_CONNECTED
;
341 if (uvc
->state
!= UVC_STATE_CONNECTED
)
345 ret
= config_ep_by_speed(f
->config
->cdev
->gadget
,
346 &(uvc
->func
), uvc
->video
.ep
);
349 usb_ep_enable(uvc
->video
.ep
);
352 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
353 v4l2_event
.type
= UVC_EVENT_STREAMON
;
354 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
356 uvc
->state
= UVC_STATE_STREAMING
;
367 uvc_function_disable(struct usb_function
*f
)
369 struct uvc_device
*uvc
= to_uvc(f
);
370 struct v4l2_event v4l2_event
;
372 INFO(f
->config
->cdev
, "uvc_function_disable\n");
374 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
375 v4l2_event
.type
= UVC_EVENT_DISCONNECT
;
376 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
378 uvc
->state
= UVC_STATE_DISCONNECTED
;
381 /* --------------------------------------------------------------------------
382 * Connection / disconnection
386 uvc_function_connect(struct uvc_device
*uvc
)
388 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
391 if ((ret
= usb_function_activate(&uvc
->func
)) < 0)
392 INFO(cdev
, "UVC connect failed with %d\n", ret
);
396 uvc_function_disconnect(struct uvc_device
*uvc
)
398 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
401 if ((ret
= usb_function_deactivate(&uvc
->func
)) < 0)
402 INFO(cdev
, "UVC disconnect failed with %d\n", ret
);
405 /* --------------------------------------------------------------------------
406 * USB probe and disconnect
410 uvc_register_video(struct uvc_device
*uvc
)
412 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
413 struct video_device
*video
;
415 /* TODO reference counting. */
416 video
= video_device_alloc();
420 video
->parent
= &cdev
->gadget
->dev
;
421 video
->fops
= &uvc_v4l2_fops
;
422 video
->release
= video_device_release
;
423 strlcpy(video
->name
, cdev
->gadget
->name
, sizeof(video
->name
));
426 video_set_drvdata(video
, uvc
);
428 return video_register_device(video
, VFL_TYPE_GRABBER
, -1);
431 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
433 memcpy(mem, desc, (desc)->bLength); \
435 mem += (desc)->bLength; \
438 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
440 const struct usb_descriptor_header * const *__src; \
441 for (__src = src; *__src; ++__src) { \
442 memcpy(mem, *__src, (*__src)->bLength); \
444 mem += (*__src)->bLength; \
448 static struct usb_descriptor_header
** __init
449 uvc_copy_descriptors(struct uvc_device
*uvc
, enum usb_device_speed speed
)
451 struct uvc_input_header_descriptor
*uvc_streaming_header
;
452 struct uvc_header_descriptor
*uvc_control_header
;
453 const struct uvc_descriptor_header
* const *uvc_control_desc
;
454 const struct uvc_descriptor_header
* const *uvc_streaming_cls
;
455 const struct usb_descriptor_header
* const *uvc_streaming_std
;
456 const struct usb_descriptor_header
* const *src
;
457 static struct usb_endpoint_descriptor
*uvc_control_ep
;
458 struct usb_descriptor_header
**dst
;
459 struct usb_descriptor_header
**hdr
;
460 unsigned int control_size
;
461 unsigned int streaming_size
;
467 case USB_SPEED_SUPER
:
468 uvc_control_desc
= uvc
->desc
.ss_control
;
469 uvc_streaming_cls
= uvc
->desc
.ss_streaming
;
470 uvc_streaming_std
= uvc_ss_streaming
;
471 uvc_control_ep
= &uvc_ss_control_ep
;
475 uvc_control_desc
= uvc
->desc
.fs_control
;
476 uvc_streaming_cls
= uvc
->desc
.hs_streaming
;
477 uvc_streaming_std
= uvc_hs_streaming
;
478 uvc_control_ep
= &uvc_fs_control_ep
;
483 uvc_control_desc
= uvc
->desc
.fs_control
;
484 uvc_streaming_cls
= uvc
->desc
.fs_streaming
;
485 uvc_streaming_std
= uvc_fs_streaming
;
486 uvc_control_ep
= &uvc_fs_control_ep
;
490 /* Descriptors layout
494 * Class-specific UVC control descriptors
497 * uvc_streaming_intf_alt0
498 * Class-specific UVC streaming descriptors
499 * uvc_{fs|hs}_streaming
502 /* Count descriptors and compute their size. */
505 bytes
= uvc_iad
.bLength
+ uvc_control_intf
.bLength
506 + uvc_control_ep
->bLength
+ uvc_control_cs_ep
.bLength
507 + uvc_streaming_intf_alt0
.bLength
;
509 if (speed
== USB_SPEED_SUPER
) {
510 bytes
+= uvc_ss_control_comp
.bLength
;
516 for (src
= (const struct usb_descriptor_header
**)uvc_control_desc
;
518 control_size
+= (*src
)->bLength
;
519 bytes
+= (*src
)->bLength
;
522 for (src
= (const struct usb_descriptor_header
**)uvc_streaming_cls
;
524 streaming_size
+= (*src
)->bLength
;
525 bytes
+= (*src
)->bLength
;
528 for (src
= uvc_streaming_std
; *src
; ++src
) {
529 bytes
+= (*src
)->bLength
;
533 mem
= kmalloc((n_desc
+ 1) * sizeof(*src
) + bytes
, GFP_KERNEL
);
539 mem
+= (n_desc
+ 1) * sizeof(*src
);
541 /* Copy the descriptors. */
542 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_iad
);
543 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_intf
);
545 uvc_control_header
= mem
;
546 UVC_COPY_DESCRIPTORS(mem
, dst
,
547 (const struct usb_descriptor_header
**)uvc_control_desc
);
548 uvc_control_header
->wTotalLength
= cpu_to_le16(control_size
);
549 uvc_control_header
->bInCollection
= 1;
550 uvc_control_header
->baInterfaceNr
[0] = uvc
->streaming_intf
;
552 UVC_COPY_DESCRIPTOR(mem
, dst
, uvc_control_ep
);
553 if (speed
== USB_SPEED_SUPER
)
554 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_ss_control_comp
);
556 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_cs_ep
);
557 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_streaming_intf_alt0
);
559 uvc_streaming_header
= mem
;
560 UVC_COPY_DESCRIPTORS(mem
, dst
,
561 (const struct usb_descriptor_header
**)uvc_streaming_cls
);
562 uvc_streaming_header
->wTotalLength
= cpu_to_le16(streaming_size
);
563 uvc_streaming_header
->bEndpointAddress
=
564 uvc_fs_streaming_ep
.bEndpointAddress
;
566 UVC_COPY_DESCRIPTORS(mem
, dst
, uvc_streaming_std
);
573 uvc_function_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
575 struct usb_composite_dev
*cdev
= c
->cdev
;
576 struct uvc_device
*uvc
= to_uvc(f
);
578 INFO(cdev
, "uvc_function_unbind\n");
580 video_unregister_device(uvc
->vdev
);
581 uvc
->control_ep
->driver_data
= NULL
;
582 uvc
->video
.ep
->driver_data
= NULL
;
584 uvc_en_us_strings
[UVC_STRING_ASSOCIATION_IDX
].id
= 0;
585 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
586 kfree(uvc
->control_buf
);
588 usb_free_all_descriptors(f
);
594 uvc_function_bind(struct usb_configuration
*c
, struct usb_function
*f
)
596 struct usb_composite_dev
*cdev
= c
->cdev
;
597 struct uvc_device
*uvc
= to_uvc(f
);
601 INFO(cdev
, "uvc_function_bind\n");
603 /* sanity check the streaming endpoint module parameters */
604 if (streaming_interval
< 1)
605 streaming_interval
= 1;
606 if (streaming_interval
> 16)
607 streaming_interval
= 16;
608 if (streaming_mult
> 2)
610 if (streaming_maxburst
> 15)
611 streaming_maxburst
= 15;
614 * fill in the FS video streaming specific descriptors from the
617 uvc_fs_streaming_ep
.wMaxPacketSize
= streaming_maxpacket
> 1023 ?
618 1023 : streaming_maxpacket
;
619 uvc_fs_streaming_ep
.bInterval
= streaming_interval
;
621 /* Allocate endpoints. */
622 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_fs_control_ep
);
624 INFO(cdev
, "Unable to allocate control EP\n");
627 uvc
->control_ep
= ep
;
628 ep
->driver_data
= uvc
;
630 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_fs_streaming_ep
);
632 INFO(cdev
, "Unable to allocate streaming EP\n");
636 ep
->driver_data
= uvc
;
638 /* Allocate interface IDs. */
639 if ((ret
= usb_interface_id(c
, f
)) < 0)
641 uvc_iad
.bFirstInterface
= ret
;
642 uvc_control_intf
.bInterfaceNumber
= ret
;
643 uvc
->control_intf
= ret
;
645 if ((ret
= usb_interface_id(c
, f
)) < 0)
647 uvc_streaming_intf_alt0
.bInterfaceNumber
= ret
;
648 uvc_streaming_intf_alt1
.bInterfaceNumber
= ret
;
649 uvc
->streaming_intf
= ret
;
651 /* sanity check the streaming endpoint module parameters */
652 if (streaming_maxpacket
> 1024)
653 streaming_maxpacket
= 1024;
655 * Fill in the HS descriptors from the module parameters for the Video
656 * Streaming endpoint.
657 * NOTE: We assume that the user knows what they are doing and won't
658 * give parameters that their UDC doesn't support.
660 uvc_hs_streaming_ep
.wMaxPacketSize
= streaming_maxpacket
;
661 uvc_hs_streaming_ep
.wMaxPacketSize
|= streaming_mult
<< 11;
662 uvc_hs_streaming_ep
.bInterval
= streaming_interval
;
663 uvc_hs_streaming_ep
.bEndpointAddress
=
664 uvc_fs_streaming_ep
.bEndpointAddress
;
667 * Fill in the SS descriptors from the module parameters for the Video
668 * Streaming endpoint.
669 * NOTE: We assume that the user knows what they are doing and won't
670 * give parameters that their UDC doesn't support.
672 uvc_ss_streaming_ep
.wMaxPacketSize
= streaming_maxpacket
;
673 uvc_ss_streaming_ep
.bInterval
= streaming_interval
;
674 uvc_ss_streaming_comp
.bmAttributes
= streaming_mult
;
675 uvc_ss_streaming_comp
.bMaxBurst
= streaming_maxburst
;
676 uvc_ss_streaming_comp
.wBytesPerInterval
=
677 streaming_maxpacket
* (streaming_mult
+ 1) *
678 (streaming_maxburst
+ 1);
679 uvc_ss_streaming_ep
.bEndpointAddress
=
680 uvc_fs_streaming_ep
.bEndpointAddress
;
682 /* Copy descriptors */
683 f
->fs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_FULL
);
684 if (gadget_is_dualspeed(cdev
->gadget
))
685 f
->hs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_HIGH
);
686 if (gadget_is_superspeed(c
->cdev
->gadget
))
687 f
->ss_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_SUPER
);
689 /* Preallocate control endpoint request. */
690 uvc
->control_req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
691 uvc
->control_buf
= kmalloc(UVC_MAX_REQUEST_SIZE
, GFP_KERNEL
);
692 if (uvc
->control_req
== NULL
|| uvc
->control_buf
== NULL
) {
697 uvc
->control_req
->buf
= uvc
->control_buf
;
698 uvc
->control_req
->complete
= uvc_function_ep0_complete
;
699 uvc
->control_req
->context
= uvc
;
701 /* Avoid letting this gadget enumerate until the userspace server is
704 if ((ret
= usb_function_deactivate(f
)) < 0)
707 /* Initialise video. */
708 ret
= uvc_video_init(&uvc
->video
);
712 /* Register a V4L2 device. */
713 ret
= uvc_register_video(uvc
);
715 printk(KERN_INFO
"Unable to register video device\n");
723 video_device_release(uvc
->vdev
);
726 uvc
->control_ep
->driver_data
= NULL
;
728 uvc
->video
.ep
->driver_data
= NULL
;
730 if (uvc
->control_req
) {
731 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
732 kfree(uvc
->control_buf
);
735 usb_free_all_descriptors(f
);
739 /* --------------------------------------------------------------------------
740 * USB gadget function
744 * uvc_bind_config - add a UVC function to a configuration
745 * @c: the configuration to support the UVC instance
746 * Context: single threaded during gadget setup
748 * Returns zero on success, else negative errno.
750 * Caller must have called @uvc_setup(). Caller is also responsible for
751 * calling @uvc_cleanup() before module unload.
754 uvc_bind_config(struct usb_configuration
*c
,
755 const struct uvc_descriptor_header
* const *fs_control
,
756 const struct uvc_descriptor_header
* const *ss_control
,
757 const struct uvc_descriptor_header
* const *fs_streaming
,
758 const struct uvc_descriptor_header
* const *hs_streaming
,
759 const struct uvc_descriptor_header
* const *ss_streaming
)
761 struct uvc_device
*uvc
;
764 /* TODO Check if the USB device controller supports the required
767 if (!gadget_is_dualspeed(c
->cdev
->gadget
))
770 uvc
= kzalloc(sizeof(*uvc
), GFP_KERNEL
);
774 uvc
->state
= UVC_STATE_DISCONNECTED
;
776 /* Validate the descriptors. */
777 if (fs_control
== NULL
|| fs_control
[0] == NULL
||
778 fs_control
[0]->bDescriptorSubType
!= UVC_VC_HEADER
)
781 if (ss_control
== NULL
|| ss_control
[0] == NULL
||
782 ss_control
[0]->bDescriptorSubType
!= UVC_VC_HEADER
)
785 if (fs_streaming
== NULL
|| fs_streaming
[0] == NULL
||
786 fs_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
789 if (hs_streaming
== NULL
|| hs_streaming
[0] == NULL
||
790 hs_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
793 if (ss_streaming
== NULL
|| ss_streaming
[0] == NULL
||
794 ss_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
797 uvc
->desc
.fs_control
= fs_control
;
798 uvc
->desc
.ss_control
= ss_control
;
799 uvc
->desc
.fs_streaming
= fs_streaming
;
800 uvc
->desc
.hs_streaming
= hs_streaming
;
801 uvc
->desc
.ss_streaming
= ss_streaming
;
803 /* Allocate string descriptor numbers. */
804 if (uvc_en_us_strings
[UVC_STRING_ASSOCIATION_IDX
].id
== 0) {
805 ret
= usb_string_ids_tab(c
->cdev
, uvc_en_us_strings
);
809 uvc_en_us_strings
[UVC_STRING_ASSOCIATION_IDX
].id
;
810 uvc_control_intf
.iInterface
=
811 uvc_en_us_strings
[UVC_STRING_CONTROL_IDX
].id
;
812 ret
= uvc_en_us_strings
[UVC_STRING_STREAMING_IDX
].id
;
813 uvc_streaming_intf_alt0
.iInterface
= ret
;
814 uvc_streaming_intf_alt1
.iInterface
= ret
;
817 /* Register the function. */
818 uvc
->func
.name
= "uvc";
819 uvc
->func
.strings
= uvc_function_strings
;
820 uvc
->func
.bind
= uvc_function_bind
;
821 uvc
->func
.unbind
= uvc_function_unbind
;
822 uvc
->func
.get_alt
= uvc_function_get_alt
;
823 uvc
->func
.set_alt
= uvc_function_set_alt
;
824 uvc
->func
.disable
= uvc_function_disable
;
825 uvc
->func
.setup
= uvc_function_setup
;
827 ret
= usb_add_function(c
, &uvc
->func
);
838 module_param_named(trace
, uvc_gadget_trace_param
, uint
, S_IRUGO
|S_IWUSR
);
839 MODULE_PARM_DESC(trace
, "Trace level bitmask");