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 int streaming_interval
= 1;
37 module_param(streaming_interval
, uint
, S_IRUGO
|S_IWUSR
);
38 MODULE_PARM_DESC(streaming_interval
, "1 - 16");
40 static unsigned int streaming_maxpacket
= 1024;
41 module_param(streaming_maxpacket
, uint
, S_IRUGO
|S_IWUSR
);
42 MODULE_PARM_DESC(streaming_maxpacket
, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
44 static unsigned int streaming_maxburst
;
45 module_param(streaming_maxburst
, uint
, S_IRUGO
|S_IWUSR
);
46 MODULE_PARM_DESC(streaming_maxburst
, "0 - 15 (ss only)");
48 /* --------------------------------------------------------------------------
49 * Function descriptors
52 /* string IDs are assigned dynamically */
54 #define UVC_STRING_CONTROL_IDX 0
55 #define UVC_STRING_STREAMING_IDX 1
57 static struct usb_string uvc_en_us_strings
[] = {
58 [UVC_STRING_CONTROL_IDX
].s
= "UVC Camera",
59 [UVC_STRING_STREAMING_IDX
].s
= "Video Streaming",
63 static struct usb_gadget_strings uvc_stringtab
= {
64 .language
= 0x0409, /* en-us */
65 .strings
= uvc_en_us_strings
,
68 static struct usb_gadget_strings
*uvc_function_strings
[] = {
73 #define UVC_INTF_VIDEO_CONTROL 0
74 #define UVC_INTF_VIDEO_STREAMING 1
76 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
78 static struct usb_interface_assoc_descriptor uvc_iad __initdata
= {
79 .bLength
= sizeof(uvc_iad
),
80 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
83 .bFunctionClass
= USB_CLASS_VIDEO
,
84 .bFunctionSubClass
= UVC_SC_VIDEO_INTERFACE_COLLECTION
,
85 .bFunctionProtocol
= 0x00,
89 static struct usb_interface_descriptor uvc_control_intf __initdata
= {
90 .bLength
= USB_DT_INTERFACE_SIZE
,
91 .bDescriptorType
= USB_DT_INTERFACE
,
92 .bInterfaceNumber
= UVC_INTF_VIDEO_CONTROL
,
93 .bAlternateSetting
= 0,
95 .bInterfaceClass
= USB_CLASS_VIDEO
,
96 .bInterfaceSubClass
= UVC_SC_VIDEOCONTROL
,
97 .bInterfaceProtocol
= 0x00,
101 static struct usb_endpoint_descriptor uvc_control_ep __initdata
= {
102 .bLength
= USB_DT_ENDPOINT_SIZE
,
103 .bDescriptorType
= USB_DT_ENDPOINT
,
104 .bEndpointAddress
= USB_DIR_IN
,
105 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
106 .wMaxPacketSize
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
110 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata
= {
111 .bLength
= sizeof(uvc_ss_control_comp
),
112 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
113 /* The following 3 values can be tweaked if necessary. */
116 .wBytesPerInterval
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
119 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata
= {
120 .bLength
= UVC_DT_CONTROL_ENDPOINT_SIZE
,
121 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
122 .bDescriptorSubType
= UVC_EP_INTERRUPT
,
123 .wMaxTransferSize
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
126 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata
= {
127 .bLength
= USB_DT_INTERFACE_SIZE
,
128 .bDescriptorType
= USB_DT_INTERFACE
,
129 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
130 .bAlternateSetting
= 0,
132 .bInterfaceClass
= USB_CLASS_VIDEO
,
133 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
134 .bInterfaceProtocol
= 0x00,
138 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata
= {
139 .bLength
= USB_DT_INTERFACE_SIZE
,
140 .bDescriptorType
= USB_DT_INTERFACE
,
141 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
142 .bAlternateSetting
= 1,
144 .bInterfaceClass
= USB_CLASS_VIDEO
,
145 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
146 .bInterfaceProtocol
= 0x00,
150 static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata
= {
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_hs_streaming_ep __initdata
= {
162 .bLength
= USB_DT_ENDPOINT_SIZE
,
163 .bDescriptorType
= USB_DT_ENDPOINT
,
164 .bEndpointAddress
= USB_DIR_IN
,
165 .bmAttributes
= USB_ENDPOINT_SYNC_ASYNC
166 | USB_ENDPOINT_XFER_ISOC
,
167 /* The wMaxPacketSize and bInterval values will be initialized from
172 static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata
= {
173 .bLength
= USB_DT_ENDPOINT_SIZE
,
174 .bDescriptorType
= USB_DT_ENDPOINT
,
176 .bEndpointAddress
= USB_DIR_IN
,
177 .bmAttributes
= USB_ENDPOINT_SYNC_ASYNC
178 | USB_ENDPOINT_XFER_ISOC
,
179 /* The wMaxPacketSize and bInterval values will be initialized from
184 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata
= {
185 .bLength
= sizeof(uvc_ss_streaming_comp
),
186 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
187 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
188 * initialized from module parameters.
192 static const struct usb_descriptor_header
* const uvc_fs_streaming
[] = {
193 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
194 (struct usb_descriptor_header
*) &uvc_fs_streaming_ep
,
198 static const struct usb_descriptor_header
* const uvc_hs_streaming
[] = {
199 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
200 (struct usb_descriptor_header
*) &uvc_hs_streaming_ep
,
204 static const struct usb_descriptor_header
* const uvc_ss_streaming
[] = {
205 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
206 (struct usb_descriptor_header
*) &uvc_ss_streaming_ep
,
207 (struct usb_descriptor_header
*) &uvc_ss_streaming_comp
,
211 /* --------------------------------------------------------------------------
216 uvc_function_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
218 struct uvc_device
*uvc
= req
->context
;
219 struct v4l2_event v4l2_event
;
220 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
222 if (uvc
->event_setup_out
) {
223 uvc
->event_setup_out
= 0;
225 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
226 v4l2_event
.type
= UVC_EVENT_DATA
;
227 uvc_event
->data
.length
= req
->actual
;
228 memcpy(&uvc_event
->data
.data
, req
->buf
, req
->actual
);
229 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
234 uvc_function_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
236 struct uvc_device
*uvc
= to_uvc(f
);
237 struct v4l2_event v4l2_event
;
238 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
240 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
241 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
242 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
245 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_CLASS
) {
246 INFO(f
->config
->cdev
, "invalid request type\n");
250 /* Stall too big requests. */
251 if (le16_to_cpu(ctrl
->wLength
) > UVC_MAX_REQUEST_SIZE
)
254 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
255 v4l2_event
.type
= UVC_EVENT_SETUP
;
256 memcpy(&uvc_event
->req
, ctrl
, sizeof(uvc_event
->req
));
257 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
262 void uvc_function_setup_continue(struct uvc_device
*uvc
)
264 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
266 usb_composite_setup_continue(cdev
);
270 uvc_function_get_alt(struct usb_function
*f
, unsigned interface
)
272 struct uvc_device
*uvc
= to_uvc(f
);
274 INFO(f
->config
->cdev
, "uvc_function_get_alt(%u)\n", interface
);
276 if (interface
== uvc
->control_intf
)
278 else if (interface
!= uvc
->streaming_intf
)
281 return uvc
->state
== UVC_STATE_STREAMING
? 1 : 0;
285 uvc_function_set_alt(struct usb_function
*f
, unsigned interface
, unsigned alt
)
287 struct uvc_device
*uvc
= to_uvc(f
);
288 struct v4l2_event v4l2_event
;
289 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
292 INFO(f
->config
->cdev
, "uvc_function_set_alt(%u, %u)\n", interface
, alt
);
294 if (interface
== uvc
->control_intf
) {
298 if (uvc
->state
== UVC_STATE_DISCONNECTED
) {
299 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
300 v4l2_event
.type
= UVC_EVENT_CONNECT
;
301 uvc_event
->speed
= f
->config
->cdev
->gadget
->speed
;
302 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
304 uvc
->state
= UVC_STATE_CONNECTED
;
310 if (interface
!= uvc
->streaming_intf
)
314 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
315 return alt ? -EINVAL : 0;
320 if (uvc
->state
!= UVC_STATE_STREAMING
)
324 usb_ep_disable(uvc
->video
.ep
);
326 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
327 v4l2_event
.type
= UVC_EVENT_STREAMOFF
;
328 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
330 uvc
->state
= UVC_STATE_CONNECTED
;
334 if (uvc
->state
!= UVC_STATE_CONNECTED
)
338 ret
= config_ep_by_speed(f
->config
->cdev
->gadget
,
339 &(uvc
->func
), uvc
->video
.ep
);
342 usb_ep_enable(uvc
->video
.ep
);
345 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
346 v4l2_event
.type
= UVC_EVENT_STREAMON
;
347 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
348 return USB_GADGET_DELAYED_STATUS
;
356 uvc_function_disable(struct usb_function
*f
)
358 struct uvc_device
*uvc
= to_uvc(f
);
359 struct v4l2_event v4l2_event
;
361 INFO(f
->config
->cdev
, "uvc_function_disable\n");
363 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
364 v4l2_event
.type
= UVC_EVENT_DISCONNECT
;
365 v4l2_event_queue(uvc
->vdev
, &v4l2_event
);
367 uvc
->state
= UVC_STATE_DISCONNECTED
;
370 /* --------------------------------------------------------------------------
371 * Connection / disconnection
375 uvc_function_connect(struct uvc_device
*uvc
)
377 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
380 if ((ret
= usb_function_activate(&uvc
->func
)) < 0)
381 INFO(cdev
, "UVC connect failed with %d\n", ret
);
385 uvc_function_disconnect(struct uvc_device
*uvc
)
387 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
390 if ((ret
= usb_function_deactivate(&uvc
->func
)) < 0)
391 INFO(cdev
, "UVC disconnect failed with %d\n", ret
);
394 /* --------------------------------------------------------------------------
395 * USB probe and disconnect
399 uvc_register_video(struct uvc_device
*uvc
)
401 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
402 struct video_device
*video
;
404 /* TODO reference counting. */
405 video
= video_device_alloc();
409 video
->v4l2_dev
= &uvc
->v4l2_dev
;
410 video
->fops
= &uvc_v4l2_fops
;
411 video
->release
= video_device_release
;
412 strlcpy(video
->name
, cdev
->gadget
->name
, sizeof(video
->name
));
415 video_set_drvdata(video
, uvc
);
417 return video_register_device(video
, VFL_TYPE_GRABBER
, -1);
420 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
422 memcpy(mem, desc, (desc)->bLength); \
424 mem += (desc)->bLength; \
427 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
429 const struct usb_descriptor_header * const *__src; \
430 for (__src = src; *__src; ++__src) { \
431 memcpy(mem, *__src, (*__src)->bLength); \
433 mem += (*__src)->bLength; \
437 static struct usb_descriptor_header
** __init
438 uvc_copy_descriptors(struct uvc_device
*uvc
, enum usb_device_speed speed
)
440 struct uvc_input_header_descriptor
*uvc_streaming_header
;
441 struct uvc_header_descriptor
*uvc_control_header
;
442 const struct uvc_descriptor_header
* const *uvc_control_desc
;
443 const struct uvc_descriptor_header
* const *uvc_streaming_cls
;
444 const struct usb_descriptor_header
* const *uvc_streaming_std
;
445 const struct usb_descriptor_header
* const *src
;
446 struct usb_descriptor_header
**dst
;
447 struct usb_descriptor_header
**hdr
;
448 unsigned int control_size
;
449 unsigned int streaming_size
;
455 case USB_SPEED_SUPER
:
456 uvc_control_desc
= uvc
->desc
.ss_control
;
457 uvc_streaming_cls
= uvc
->desc
.ss_streaming
;
458 uvc_streaming_std
= uvc_ss_streaming
;
462 uvc_control_desc
= uvc
->desc
.fs_control
;
463 uvc_streaming_cls
= uvc
->desc
.hs_streaming
;
464 uvc_streaming_std
= uvc_hs_streaming
;
469 uvc_control_desc
= uvc
->desc
.fs_control
;
470 uvc_streaming_cls
= uvc
->desc
.fs_streaming
;
471 uvc_streaming_std
= uvc_fs_streaming
;
475 /* Descriptors layout
479 * Class-specific UVC control descriptors
482 * uvc_ss_control_comp (for SS only)
483 * uvc_streaming_intf_alt0
484 * Class-specific UVC streaming descriptors
485 * uvc_{fs|hs}_streaming
488 /* Count descriptors and compute their size. */
491 bytes
= uvc_iad
.bLength
+ uvc_control_intf
.bLength
492 + uvc_control_ep
.bLength
+ uvc_control_cs_ep
.bLength
493 + uvc_streaming_intf_alt0
.bLength
;
495 if (speed
== USB_SPEED_SUPER
) {
496 bytes
+= uvc_ss_control_comp
.bLength
;
502 for (src
= (const struct usb_descriptor_header
**)uvc_control_desc
;
504 control_size
+= (*src
)->bLength
;
505 bytes
+= (*src
)->bLength
;
508 for (src
= (const struct usb_descriptor_header
**)uvc_streaming_cls
;
510 streaming_size
+= (*src
)->bLength
;
511 bytes
+= (*src
)->bLength
;
514 for (src
= uvc_streaming_std
; *src
; ++src
) {
515 bytes
+= (*src
)->bLength
;
519 mem
= kmalloc((n_desc
+ 1) * sizeof(*src
) + bytes
, GFP_KERNEL
);
525 mem
+= (n_desc
+ 1) * sizeof(*src
);
527 /* Copy the descriptors. */
528 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_iad
);
529 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_intf
);
531 uvc_control_header
= mem
;
532 UVC_COPY_DESCRIPTORS(mem
, dst
,
533 (const struct usb_descriptor_header
**)uvc_control_desc
);
534 uvc_control_header
->wTotalLength
= cpu_to_le16(control_size
);
535 uvc_control_header
->bInCollection
= 1;
536 uvc_control_header
->baInterfaceNr
[0] = uvc
->streaming_intf
;
538 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_ep
);
539 if (speed
== USB_SPEED_SUPER
)
540 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_ss_control_comp
);
542 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_cs_ep
);
543 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_streaming_intf_alt0
);
545 uvc_streaming_header
= mem
;
546 UVC_COPY_DESCRIPTORS(mem
, dst
,
547 (const struct usb_descriptor_header
**)uvc_streaming_cls
);
548 uvc_streaming_header
->wTotalLength
= cpu_to_le16(streaming_size
);
549 uvc_streaming_header
->bEndpointAddress
= uvc
->video
.ep
->address
;
551 UVC_COPY_DESCRIPTORS(mem
, dst
, uvc_streaming_std
);
558 uvc_function_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
560 struct usb_composite_dev
*cdev
= c
->cdev
;
561 struct uvc_device
*uvc
= to_uvc(f
);
563 INFO(cdev
, "uvc_function_unbind\n");
565 video_unregister_device(uvc
->vdev
);
566 v4l2_device_unregister(&uvc
->v4l2_dev
);
567 uvc
->control_ep
->driver_data
= NULL
;
568 uvc
->video
.ep
->driver_data
= NULL
;
570 uvc_en_us_strings
[UVC_STRING_CONTROL_IDX
].id
= 0;
571 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
572 kfree(uvc
->control_buf
);
574 usb_free_all_descriptors(f
);
580 uvc_function_bind(struct usb_configuration
*c
, struct usb_function
*f
)
582 struct usb_composite_dev
*cdev
= c
->cdev
;
583 struct uvc_device
*uvc
= to_uvc(f
);
584 unsigned int max_packet_mult
;
585 unsigned int max_packet_size
;
589 INFO(cdev
, "uvc_function_bind\n");
591 /* Sanity check the streaming endpoint module parameters.
593 streaming_interval
= clamp(streaming_interval
, 1U, 16U);
594 streaming_maxpacket
= clamp(streaming_maxpacket
, 1U, 3072U);
595 streaming_maxburst
= min(streaming_maxburst
, 15U);
597 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
600 * NOTE: We assume that the user knows what they are doing and won't
601 * give parameters that their UDC doesn't support.
603 if (streaming_maxpacket
<= 1024) {
605 max_packet_size
= streaming_maxpacket
;
606 } else if (streaming_maxpacket
<= 2048) {
608 max_packet_size
= streaming_maxpacket
/ 2;
611 max_packet_size
= streaming_maxpacket
/ 3;
614 uvc_fs_streaming_ep
.wMaxPacketSize
= min(streaming_maxpacket
, 1023U);
615 uvc_fs_streaming_ep
.bInterval
= streaming_interval
;
617 uvc_hs_streaming_ep
.wMaxPacketSize
= max_packet_size
;
618 uvc_hs_streaming_ep
.wMaxPacketSize
|= ((max_packet_mult
- 1) << 11);
619 uvc_hs_streaming_ep
.bInterval
= streaming_interval
;
621 uvc_ss_streaming_ep
.wMaxPacketSize
= max_packet_size
;
622 uvc_ss_streaming_ep
.bInterval
= streaming_interval
;
623 uvc_ss_streaming_comp
.bmAttributes
= max_packet_mult
- 1;
624 uvc_ss_streaming_comp
.bMaxBurst
= streaming_maxburst
;
625 uvc_ss_streaming_comp
.wBytesPerInterval
=
626 max_packet_size
* max_packet_mult
* streaming_maxburst
;
628 /* Allocate endpoints. */
629 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_control_ep
);
631 INFO(cdev
, "Unable to allocate control EP\n");
634 uvc
->control_ep
= ep
;
635 ep
->driver_data
= uvc
;
637 if (gadget_is_superspeed(c
->cdev
->gadget
))
638 ep
= usb_ep_autoconfig_ss(cdev
->gadget
, &uvc_ss_streaming_ep
,
639 &uvc_ss_streaming_comp
);
640 else if (gadget_is_dualspeed(cdev
->gadget
))
641 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_hs_streaming_ep
);
643 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_fs_streaming_ep
);
646 INFO(cdev
, "Unable to allocate streaming EP\n");
650 ep
->driver_data
= uvc
;
652 uvc_fs_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
653 uvc_hs_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
654 uvc_ss_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
656 /* Allocate interface IDs. */
657 if ((ret
= usb_interface_id(c
, f
)) < 0)
659 uvc_iad
.bFirstInterface
= ret
;
660 uvc_control_intf
.bInterfaceNumber
= ret
;
661 uvc
->control_intf
= ret
;
663 if ((ret
= usb_interface_id(c
, f
)) < 0)
665 uvc_streaming_intf_alt0
.bInterfaceNumber
= ret
;
666 uvc_streaming_intf_alt1
.bInterfaceNumber
= ret
;
667 uvc
->streaming_intf
= ret
;
669 /* Copy descriptors */
670 f
->fs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_FULL
);
671 if (gadget_is_dualspeed(cdev
->gadget
))
672 f
->hs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_HIGH
);
673 if (gadget_is_superspeed(c
->cdev
->gadget
))
674 f
->ss_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_SUPER
);
676 /* Preallocate control endpoint request. */
677 uvc
->control_req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
678 uvc
->control_buf
= kmalloc(UVC_MAX_REQUEST_SIZE
, GFP_KERNEL
);
679 if (uvc
->control_req
== NULL
|| uvc
->control_buf
== NULL
) {
684 uvc
->control_req
->buf
= uvc
->control_buf
;
685 uvc
->control_req
->complete
= uvc_function_ep0_complete
;
686 uvc
->control_req
->context
= uvc
;
688 /* Avoid letting this gadget enumerate until the userspace server is
691 if ((ret
= usb_function_deactivate(f
)) < 0)
694 if (v4l2_device_register(&cdev
->gadget
->dev
, &uvc
->v4l2_dev
)) {
695 printk(KERN_INFO
"v4l2_device_register failed\n");
699 /* Initialise video. */
700 ret
= uvc_video_init(&uvc
->video
);
704 /* Register a V4L2 device. */
705 ret
= uvc_register_video(uvc
);
707 printk(KERN_INFO
"Unable to register video device\n");
714 v4l2_device_unregister(&uvc
->v4l2_dev
);
716 video_device_release(uvc
->vdev
);
719 uvc
->control_ep
->driver_data
= NULL
;
721 uvc
->video
.ep
->driver_data
= NULL
;
723 if (uvc
->control_req
) {
724 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
725 kfree(uvc
->control_buf
);
728 usb_free_all_descriptors(f
);
732 /* --------------------------------------------------------------------------
733 * USB gadget function
737 * uvc_bind_config - add a UVC function to a configuration
738 * @c: the configuration to support the UVC instance
739 * Context: single threaded during gadget setup
741 * Returns zero on success, else negative errno.
743 * Caller must have called @uvc_setup(). Caller is also responsible for
744 * calling @uvc_cleanup() before module unload.
747 uvc_bind_config(struct usb_configuration
*c
,
748 const struct uvc_descriptor_header
* const *fs_control
,
749 const struct uvc_descriptor_header
* const *ss_control
,
750 const struct uvc_descriptor_header
* const *fs_streaming
,
751 const struct uvc_descriptor_header
* const *hs_streaming
,
752 const struct uvc_descriptor_header
* const *ss_streaming
)
754 struct uvc_device
*uvc
;
757 /* TODO Check if the USB device controller supports the required
760 if (!gadget_is_dualspeed(c
->cdev
->gadget
))
763 uvc
= kzalloc(sizeof(*uvc
), GFP_KERNEL
);
767 uvc
->state
= UVC_STATE_DISCONNECTED
;
769 /* Validate the descriptors. */
770 if (fs_control
== NULL
|| fs_control
[0] == NULL
||
771 fs_control
[0]->bDescriptorSubType
!= UVC_VC_HEADER
)
774 if (ss_control
== NULL
|| ss_control
[0] == NULL
||
775 ss_control
[0]->bDescriptorSubType
!= UVC_VC_HEADER
)
778 if (fs_streaming
== NULL
|| fs_streaming
[0] == NULL
||
779 fs_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
782 if (hs_streaming
== NULL
|| hs_streaming
[0] == NULL
||
783 hs_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
786 if (ss_streaming
== NULL
|| ss_streaming
[0] == NULL
||
787 ss_streaming
[0]->bDescriptorSubType
!= UVC_VS_INPUT_HEADER
)
790 uvc
->desc
.fs_control
= fs_control
;
791 uvc
->desc
.ss_control
= ss_control
;
792 uvc
->desc
.fs_streaming
= fs_streaming
;
793 uvc
->desc
.hs_streaming
= hs_streaming
;
794 uvc
->desc
.ss_streaming
= ss_streaming
;
796 /* String descriptors are global, we only need to allocate string IDs
797 * for the first UVC function. UVC functions beyond the first (if any)
798 * will reuse the same IDs.
800 if (uvc_en_us_strings
[UVC_STRING_CONTROL_IDX
].id
== 0) {
801 ret
= usb_string_ids_tab(c
->cdev
, uvc_en_us_strings
);
805 uvc_en_us_strings
[UVC_STRING_CONTROL_IDX
].id
;
806 uvc_control_intf
.iInterface
=
807 uvc_en_us_strings
[UVC_STRING_CONTROL_IDX
].id
;
808 ret
= uvc_en_us_strings
[UVC_STRING_STREAMING_IDX
].id
;
809 uvc_streaming_intf_alt0
.iInterface
= ret
;
810 uvc_streaming_intf_alt1
.iInterface
= ret
;
813 /* Register the function. */
814 uvc
->func
.name
= "uvc";
815 uvc
->func
.strings
= uvc_function_strings
;
816 uvc
->func
.bind
= uvc_function_bind
;
817 uvc
->func
.unbind
= uvc_function_unbind
;
818 uvc
->func
.get_alt
= uvc_function_get_alt
;
819 uvc
->func
.set_alt
= uvc_function_set_alt
;
820 uvc
->func
.disable
= uvc_function_disable
;
821 uvc
->func
.setup
= uvc_function_setup
;
823 ret
= usb_add_function(c
, &uvc
->func
);
834 module_param_named(trace
, uvc_gadget_trace_param
, uint
, S_IRUGO
|S_IWUSR
);
835 MODULE_PARM_DESC(trace
, "Trace level bitmask");