1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_gadget.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/string.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
19 #include <linux/usb/video.h>
20 #include <linux/vmalloc.h>
21 #include <linux/wait.h>
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-event.h>
28 #include "uvc_configfs.h"
30 #include "uvc_video.h"
32 unsigned int uvc_gadget_trace_param
;
34 /* --------------------------------------------------------------------------
35 * Function descriptors
38 /* string IDs are assigned dynamically */
40 #define UVC_STRING_CONTROL_IDX 0
41 #define UVC_STRING_STREAMING_IDX 1
43 static struct usb_string uvc_en_us_strings
[] = {
44 [UVC_STRING_CONTROL_IDX
].s
= "UVC Camera",
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 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
64 static struct usb_interface_assoc_descriptor uvc_iad
= {
65 .bLength
= sizeof(uvc_iad
),
66 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
69 .bFunctionClass
= USB_CLASS_VIDEO
,
70 .bFunctionSubClass
= UVC_SC_VIDEO_INTERFACE_COLLECTION
,
71 .bFunctionProtocol
= 0x00,
75 static struct usb_interface_descriptor uvc_control_intf
= {
76 .bLength
= USB_DT_INTERFACE_SIZE
,
77 .bDescriptorType
= USB_DT_INTERFACE
,
78 .bInterfaceNumber
= UVC_INTF_VIDEO_CONTROL
,
79 .bAlternateSetting
= 0,
81 .bInterfaceClass
= USB_CLASS_VIDEO
,
82 .bInterfaceSubClass
= UVC_SC_VIDEOCONTROL
,
83 .bInterfaceProtocol
= 0x00,
87 static struct usb_endpoint_descriptor uvc_control_ep
= {
88 .bLength
= USB_DT_ENDPOINT_SIZE
,
89 .bDescriptorType
= USB_DT_ENDPOINT
,
90 .bEndpointAddress
= USB_DIR_IN
,
91 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
92 .wMaxPacketSize
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
96 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp
= {
97 .bLength
= sizeof(uvc_ss_control_comp
),
98 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
99 /* The following 3 values can be tweaked if necessary. */
102 .wBytesPerInterval
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
105 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep
= {
106 .bLength
= UVC_DT_CONTROL_ENDPOINT_SIZE
,
107 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
108 .bDescriptorSubType
= UVC_EP_INTERRUPT
,
109 .wMaxTransferSize
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
112 static struct usb_interface_descriptor uvc_streaming_intf_alt0
= {
113 .bLength
= USB_DT_INTERFACE_SIZE
,
114 .bDescriptorType
= USB_DT_INTERFACE
,
115 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
116 .bAlternateSetting
= 0,
118 .bInterfaceClass
= USB_CLASS_VIDEO
,
119 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
120 .bInterfaceProtocol
= 0x00,
124 static struct usb_interface_descriptor uvc_streaming_intf_alt1
= {
125 .bLength
= USB_DT_INTERFACE_SIZE
,
126 .bDescriptorType
= USB_DT_INTERFACE
,
127 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
128 .bAlternateSetting
= 1,
130 .bInterfaceClass
= USB_CLASS_VIDEO
,
131 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
132 .bInterfaceProtocol
= 0x00,
136 static struct usb_endpoint_descriptor uvc_fs_streaming_ep
= {
137 .bLength
= USB_DT_ENDPOINT_SIZE
,
138 .bDescriptorType
= USB_DT_ENDPOINT
,
139 .bEndpointAddress
= USB_DIR_IN
,
140 .bmAttributes
= USB_ENDPOINT_SYNC_ASYNC
141 | USB_ENDPOINT_XFER_ISOC
,
142 /* The wMaxPacketSize and bInterval values will be initialized from
147 static struct usb_endpoint_descriptor uvc_hs_streaming_ep
= {
148 .bLength
= USB_DT_ENDPOINT_SIZE
,
149 .bDescriptorType
= USB_DT_ENDPOINT
,
150 .bEndpointAddress
= USB_DIR_IN
,
151 .bmAttributes
= USB_ENDPOINT_SYNC_ASYNC
152 | USB_ENDPOINT_XFER_ISOC
,
153 /* The wMaxPacketSize and bInterval values will be initialized from
158 static struct usb_endpoint_descriptor uvc_ss_streaming_ep
= {
159 .bLength
= USB_DT_ENDPOINT_SIZE
,
160 .bDescriptorType
= USB_DT_ENDPOINT
,
162 .bEndpointAddress
= USB_DIR_IN
,
163 .bmAttributes
= USB_ENDPOINT_SYNC_ASYNC
164 | USB_ENDPOINT_XFER_ISOC
,
165 /* The wMaxPacketSize and bInterval values will be initialized from
170 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp
= {
171 .bLength
= sizeof(uvc_ss_streaming_comp
),
172 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
173 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
174 * initialized from module parameters.
178 static const struct usb_descriptor_header
* const uvc_fs_streaming
[] = {
179 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
180 (struct usb_descriptor_header
*) &uvc_fs_streaming_ep
,
184 static const struct usb_descriptor_header
* const uvc_hs_streaming
[] = {
185 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
186 (struct usb_descriptor_header
*) &uvc_hs_streaming_ep
,
190 static const struct usb_descriptor_header
* const uvc_ss_streaming
[] = {
191 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
192 (struct usb_descriptor_header
*) &uvc_ss_streaming_ep
,
193 (struct usb_descriptor_header
*) &uvc_ss_streaming_comp
,
197 void uvc_set_trace_param(unsigned int trace
)
199 uvc_gadget_trace_param
= trace
;
201 EXPORT_SYMBOL(uvc_set_trace_param
);
203 /* --------------------------------------------------------------------------
208 uvc_function_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
210 struct uvc_device
*uvc
= req
->context
;
211 struct v4l2_event v4l2_event
;
212 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
214 if (uvc
->event_setup_out
) {
215 uvc
->event_setup_out
= 0;
217 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
218 v4l2_event
.type
= UVC_EVENT_DATA
;
219 uvc_event
->data
.length
= req
->actual
;
220 memcpy(&uvc_event
->data
.data
, req
->buf
, req
->actual
);
221 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
226 uvc_function_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
228 struct uvc_device
*uvc
= to_uvc(f
);
229 struct v4l2_event v4l2_event
;
230 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
232 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
233 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
234 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
237 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_CLASS
) {
238 INFO(f
->config
->cdev
, "invalid request type\n");
242 /* Stall too big requests. */
243 if (le16_to_cpu(ctrl
->wLength
) > UVC_MAX_REQUEST_SIZE
)
246 /* Tell the complete callback to generate an event for the next request
247 * that will be enqueued by UVCIOC_SEND_RESPONSE.
249 uvc
->event_setup_out
= !(ctrl
->bRequestType
& USB_DIR_IN
);
250 uvc
->event_length
= le16_to_cpu(ctrl
->wLength
);
252 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
253 v4l2_event
.type
= UVC_EVENT_SETUP
;
254 memcpy(&uvc_event
->req
, ctrl
, sizeof(uvc_event
->req
));
255 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
260 void uvc_function_setup_continue(struct uvc_device
*uvc
)
262 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
264 usb_composite_setup_continue(cdev
);
268 uvc_function_get_alt(struct usb_function
*f
, unsigned interface
)
270 struct uvc_device
*uvc
= to_uvc(f
);
272 INFO(f
->config
->cdev
, "uvc_function_get_alt(%u)\n", interface
);
274 if (interface
== uvc
->control_intf
)
276 else if (interface
!= uvc
->streaming_intf
)
279 return uvc
->video
.ep
->enabled
? 1 : 0;
283 uvc_function_set_alt(struct usb_function
*f
, unsigned interface
, unsigned alt
)
285 struct uvc_device
*uvc
= to_uvc(f
);
286 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
287 struct v4l2_event v4l2_event
;
288 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
291 INFO(cdev
, "uvc_function_set_alt(%u, %u)\n", interface
, alt
);
293 if (interface
== uvc
->control_intf
) {
297 INFO(cdev
, "reset UVC Control\n");
298 usb_ep_disable(uvc
->control_ep
);
300 if (!uvc
->control_ep
->desc
)
301 if (config_ep_by_speed(cdev
->gadget
, f
, uvc
->control_ep
))
304 usb_ep_enable(uvc
->control_ep
);
306 if (uvc
->state
== UVC_STATE_DISCONNECTED
) {
307 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
308 v4l2_event
.type
= UVC_EVENT_CONNECT
;
309 uvc_event
->speed
= cdev
->gadget
->speed
;
310 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
312 uvc
->state
= UVC_STATE_CONNECTED
;
318 if (interface
!= uvc
->streaming_intf
)
322 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
323 return alt ? -EINVAL : 0;
328 if (uvc
->state
!= UVC_STATE_STREAMING
)
332 usb_ep_disable(uvc
->video
.ep
);
334 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
335 v4l2_event
.type
= UVC_EVENT_STREAMOFF
;
336 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
338 uvc
->state
= UVC_STATE_CONNECTED
;
342 if (uvc
->state
!= UVC_STATE_CONNECTED
)
348 INFO(cdev
, "reset UVC\n");
349 usb_ep_disable(uvc
->video
.ep
);
351 ret
= config_ep_by_speed(f
->config
->cdev
->gadget
,
352 &(uvc
->func
), uvc
->video
.ep
);
355 usb_ep_enable(uvc
->video
.ep
);
357 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
358 v4l2_event
.type
= UVC_EVENT_STREAMON
;
359 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
360 return USB_GADGET_DELAYED_STATUS
;
368 uvc_function_disable(struct usb_function
*f
)
370 struct uvc_device
*uvc
= to_uvc(f
);
371 struct v4l2_event v4l2_event
;
373 INFO(f
->config
->cdev
, "uvc_function_disable\n");
375 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
376 v4l2_event
.type
= UVC_EVENT_DISCONNECT
;
377 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
379 uvc
->state
= UVC_STATE_DISCONNECTED
;
381 usb_ep_disable(uvc
->video
.ep
);
382 usb_ep_disable(uvc
->control_ep
);
385 /* --------------------------------------------------------------------------
386 * Connection / disconnection
390 uvc_function_connect(struct uvc_device
*uvc
)
392 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
395 if ((ret
= usb_function_activate(&uvc
->func
)) < 0)
396 INFO(cdev
, "UVC connect failed with %d\n", ret
);
400 uvc_function_disconnect(struct uvc_device
*uvc
)
402 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
405 if ((ret
= usb_function_deactivate(&uvc
->func
)) < 0)
406 INFO(cdev
, "UVC disconnect failed with %d\n", ret
);
409 /* --------------------------------------------------------------------------
410 * USB probe and disconnect
414 uvc_register_video(struct uvc_device
*uvc
)
416 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
418 /* TODO reference counting. */
419 uvc
->vdev
.v4l2_dev
= &uvc
->v4l2_dev
;
420 uvc
->vdev
.fops
= &uvc_v4l2_fops
;
421 uvc
->vdev
.ioctl_ops
= &uvc_v4l2_ioctl_ops
;
422 uvc
->vdev
.release
= video_device_release_empty
;
423 uvc
->vdev
.vfl_dir
= VFL_DIR_TX
;
424 uvc
->vdev
.lock
= &uvc
->video
.mutex
;
425 strlcpy(uvc
->vdev
.name
, cdev
->gadget
->name
, sizeof(uvc
->vdev
.name
));
427 video_set_drvdata(&uvc
->vdev
, uvc
);
429 return video_register_device(&uvc
->vdev
, VFL_TYPE_GRABBER
, -1);
432 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
434 memcpy(mem, desc, (desc)->bLength); \
436 mem += (desc)->bLength; \
439 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
441 const struct usb_descriptor_header * const *__src; \
442 for (__src = src; *__src; ++__src) { \
443 memcpy(mem, *__src, (*__src)->bLength); \
445 mem += (*__src)->bLength; \
449 static struct usb_descriptor_header
**
450 uvc_copy_descriptors(struct uvc_device
*uvc
, enum usb_device_speed speed
)
452 struct uvc_input_header_descriptor
*uvc_streaming_header
;
453 struct uvc_header_descriptor
*uvc_control_header
;
454 const struct uvc_descriptor_header
* const *uvc_control_desc
;
455 const struct uvc_descriptor_header
* const *uvc_streaming_cls
;
456 const struct usb_descriptor_header
* const *uvc_streaming_std
;
457 const struct usb_descriptor_header
* const *src
;
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
;
474 uvc_control_desc
= uvc
->desc
.fs_control
;
475 uvc_streaming_cls
= uvc
->desc
.hs_streaming
;
476 uvc_streaming_std
= uvc_hs_streaming
;
481 uvc_control_desc
= uvc
->desc
.fs_control
;
482 uvc_streaming_cls
= uvc
->desc
.fs_streaming
;
483 uvc_streaming_std
= uvc_fs_streaming
;
487 if (!uvc_control_desc
|| !uvc_streaming_cls
)
488 return ERR_PTR(-ENODEV
);
490 /* Descriptors layout
494 * Class-specific UVC control descriptors
497 * uvc_ss_control_comp (for SS only)
498 * uvc_streaming_intf_alt0
499 * Class-specific UVC streaming descriptors
500 * uvc_{fs|hs}_streaming
503 /* Count descriptors and compute their size. */
506 bytes
= uvc_iad
.bLength
+ uvc_control_intf
.bLength
507 + uvc_control_ep
.bLength
+ uvc_control_cs_ep
.bLength
508 + uvc_streaming_intf_alt0
.bLength
;
510 if (speed
== USB_SPEED_SUPER
) {
511 bytes
+= uvc_ss_control_comp
.bLength
;
517 for (src
= (const struct usb_descriptor_header
**)uvc_control_desc
;
519 control_size
+= (*src
)->bLength
;
520 bytes
+= (*src
)->bLength
;
523 for (src
= (const struct usb_descriptor_header
**)uvc_streaming_cls
;
525 streaming_size
+= (*src
)->bLength
;
526 bytes
+= (*src
)->bLength
;
529 for (src
= uvc_streaming_std
; *src
; ++src
) {
530 bytes
+= (*src
)->bLength
;
534 mem
= kmalloc((n_desc
+ 1) * sizeof(*src
) + bytes
, GFP_KERNEL
);
540 mem
+= (n_desc
+ 1) * sizeof(*src
);
542 /* Copy the descriptors. */
543 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_iad
);
544 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_intf
);
546 uvc_control_header
= mem
;
547 UVC_COPY_DESCRIPTORS(mem
, dst
,
548 (const struct usb_descriptor_header
**)uvc_control_desc
);
549 uvc_control_header
->wTotalLength
= cpu_to_le16(control_size
);
550 uvc_control_header
->bInCollection
= 1;
551 uvc_control_header
->baInterfaceNr
[0] = uvc
->streaming_intf
;
553 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_ep
);
554 if (speed
== USB_SPEED_SUPER
)
555 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_ss_control_comp
);
557 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_cs_ep
);
558 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_streaming_intf_alt0
);
560 uvc_streaming_header
= mem
;
561 UVC_COPY_DESCRIPTORS(mem
, dst
,
562 (const struct usb_descriptor_header
**)uvc_streaming_cls
);
563 uvc_streaming_header
->wTotalLength
= cpu_to_le16(streaming_size
);
564 uvc_streaming_header
->bEndpointAddress
= uvc
->video
.ep
->address
;
566 UVC_COPY_DESCRIPTORS(mem
, dst
, uvc_streaming_std
);
573 uvc_function_bind(struct usb_configuration
*c
, struct usb_function
*f
)
575 struct usb_composite_dev
*cdev
= c
->cdev
;
576 struct uvc_device
*uvc
= to_uvc(f
);
577 struct usb_string
*us
;
578 unsigned int max_packet_mult
;
579 unsigned int max_packet_size
;
581 struct f_uvc_opts
*opts
;
584 INFO(cdev
, "uvc_function_bind\n");
586 opts
= fi_to_f_uvc_opts(f
->fi
);
587 /* Sanity check the streaming endpoint module parameters.
589 opts
->streaming_interval
= clamp(opts
->streaming_interval
, 1U, 16U);
590 opts
->streaming_maxpacket
= clamp(opts
->streaming_maxpacket
, 1U, 3072U);
591 opts
->streaming_maxburst
= min(opts
->streaming_maxburst
, 15U);
593 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
594 if (opts
->streaming_maxburst
&&
595 (opts
->streaming_maxpacket
% 1024) != 0) {
596 opts
->streaming_maxpacket
= roundup(opts
->streaming_maxpacket
, 1024);
597 INFO(cdev
, "overriding streaming_maxpacket to %d\n",
598 opts
->streaming_maxpacket
);
601 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
604 * NOTE: We assume that the user knows what they are doing and won't
605 * give parameters that their UDC doesn't support.
607 if (opts
->streaming_maxpacket
<= 1024) {
609 max_packet_size
= opts
->streaming_maxpacket
;
610 } else if (opts
->streaming_maxpacket
<= 2048) {
612 max_packet_size
= opts
->streaming_maxpacket
/ 2;
615 max_packet_size
= opts
->streaming_maxpacket
/ 3;
618 uvc_fs_streaming_ep
.wMaxPacketSize
=
619 cpu_to_le16(min(opts
->streaming_maxpacket
, 1023U));
620 uvc_fs_streaming_ep
.bInterval
= opts
->streaming_interval
;
622 uvc_hs_streaming_ep
.wMaxPacketSize
=
623 cpu_to_le16(max_packet_size
| ((max_packet_mult
- 1) << 11));
624 uvc_hs_streaming_ep
.bInterval
= opts
->streaming_interval
;
626 uvc_ss_streaming_ep
.wMaxPacketSize
= cpu_to_le16(max_packet_size
);
627 uvc_ss_streaming_ep
.bInterval
= opts
->streaming_interval
;
628 uvc_ss_streaming_comp
.bmAttributes
= max_packet_mult
- 1;
629 uvc_ss_streaming_comp
.bMaxBurst
= opts
->streaming_maxburst
;
630 uvc_ss_streaming_comp
.wBytesPerInterval
=
631 cpu_to_le16(max_packet_size
* max_packet_mult
*
632 (opts
->streaming_maxburst
+ 1));
634 /* Allocate endpoints. */
635 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_control_ep
);
637 INFO(cdev
, "Unable to allocate control EP\n");
640 uvc
->control_ep
= ep
;
642 if (gadget_is_superspeed(c
->cdev
->gadget
))
643 ep
= usb_ep_autoconfig_ss(cdev
->gadget
, &uvc_ss_streaming_ep
,
644 &uvc_ss_streaming_comp
);
645 else if (gadget_is_dualspeed(cdev
->gadget
))
646 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_hs_streaming_ep
);
648 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_fs_streaming_ep
);
651 INFO(cdev
, "Unable to allocate streaming EP\n");
656 uvc_fs_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
657 uvc_hs_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
658 uvc_ss_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
660 us
= usb_gstrings_attach(cdev
, uvc_function_strings
,
661 ARRAY_SIZE(uvc_en_us_strings
));
666 uvc_iad
.iFunction
= us
[UVC_STRING_CONTROL_IDX
].id
;
667 uvc_control_intf
.iInterface
= us
[UVC_STRING_CONTROL_IDX
].id
;
668 ret
= us
[UVC_STRING_STREAMING_IDX
].id
;
669 uvc_streaming_intf_alt0
.iInterface
= ret
;
670 uvc_streaming_intf_alt1
.iInterface
= ret
;
672 /* Allocate interface IDs. */
673 if ((ret
= usb_interface_id(c
, f
)) < 0)
675 uvc_iad
.bFirstInterface
= ret
;
676 uvc_control_intf
.bInterfaceNumber
= ret
;
677 uvc
->control_intf
= ret
;
679 if ((ret
= usb_interface_id(c
, f
)) < 0)
681 uvc_streaming_intf_alt0
.bInterfaceNumber
= ret
;
682 uvc_streaming_intf_alt1
.bInterfaceNumber
= ret
;
683 uvc
->streaming_intf
= ret
;
685 /* Copy descriptors */
686 f
->fs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_FULL
);
687 if (IS_ERR(f
->fs_descriptors
)) {
688 ret
= PTR_ERR(f
->fs_descriptors
);
689 f
->fs_descriptors
= NULL
;
692 if (gadget_is_dualspeed(cdev
->gadget
)) {
693 f
->hs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_HIGH
);
694 if (IS_ERR(f
->hs_descriptors
)) {
695 ret
= PTR_ERR(f
->hs_descriptors
);
696 f
->hs_descriptors
= NULL
;
700 if (gadget_is_superspeed(c
->cdev
->gadget
)) {
701 f
->ss_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_SUPER
);
702 if (IS_ERR(f
->ss_descriptors
)) {
703 ret
= PTR_ERR(f
->ss_descriptors
);
704 f
->ss_descriptors
= NULL
;
709 /* Preallocate control endpoint request. */
710 uvc
->control_req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
711 uvc
->control_buf
= kmalloc(UVC_MAX_REQUEST_SIZE
, GFP_KERNEL
);
712 if (uvc
->control_req
== NULL
|| uvc
->control_buf
== NULL
) {
717 uvc
->control_req
->buf
= uvc
->control_buf
;
718 uvc
->control_req
->complete
= uvc_function_ep0_complete
;
719 uvc
->control_req
->context
= uvc
;
721 if (v4l2_device_register(&cdev
->gadget
->dev
, &uvc
->v4l2_dev
)) {
722 printk(KERN_INFO
"v4l2_device_register failed\n");
726 /* Initialise video. */
727 ret
= uvcg_video_init(&uvc
->video
);
731 /* Register a V4L2 device. */
732 ret
= uvc_register_video(uvc
);
734 printk(KERN_INFO
"Unable to register video device\n");
741 v4l2_device_unregister(&uvc
->v4l2_dev
);
743 if (uvc
->control_req
)
744 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
745 kfree(uvc
->control_buf
);
747 usb_free_all_descriptors(f
);
751 /* --------------------------------------------------------------------------
752 * USB gadget function
755 static void uvc_free_inst(struct usb_function_instance
*f
)
757 struct f_uvc_opts
*opts
= fi_to_f_uvc_opts(f
);
759 mutex_destroy(&opts
->lock
);
763 static struct usb_function_instance
*uvc_alloc_inst(void)
765 struct f_uvc_opts
*opts
;
766 struct uvc_camera_terminal_descriptor
*cd
;
767 struct uvc_processing_unit_descriptor
*pd
;
768 struct uvc_output_terminal_descriptor
*od
;
769 struct uvc_color_matching_descriptor
*md
;
770 struct uvc_descriptor_header
**ctl_cls
;
772 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
774 return ERR_PTR(-ENOMEM
);
775 opts
->func_inst
.free_func_inst
= uvc_free_inst
;
776 mutex_init(&opts
->lock
);
778 cd
= &opts
->uvc_camera_terminal
;
779 cd
->bLength
= UVC_DT_CAMERA_TERMINAL_SIZE(3);
780 cd
->bDescriptorType
= USB_DT_CS_INTERFACE
;
781 cd
->bDescriptorSubType
= UVC_VC_INPUT_TERMINAL
;
783 cd
->wTerminalType
= cpu_to_le16(0x0201);
784 cd
->bAssocTerminal
= 0;
786 cd
->wObjectiveFocalLengthMin
= cpu_to_le16(0);
787 cd
->wObjectiveFocalLengthMax
= cpu_to_le16(0);
788 cd
->wOcularFocalLength
= cpu_to_le16(0);
789 cd
->bControlSize
= 3;
790 cd
->bmControls
[0] = 2;
791 cd
->bmControls
[1] = 0;
792 cd
->bmControls
[2] = 0;
794 pd
= &opts
->uvc_processing
;
795 pd
->bLength
= UVC_DT_PROCESSING_UNIT_SIZE(2);
796 pd
->bDescriptorType
= USB_DT_CS_INTERFACE
;
797 pd
->bDescriptorSubType
= UVC_VC_PROCESSING_UNIT
;
800 pd
->wMaxMultiplier
= cpu_to_le16(16*1024);
801 pd
->bControlSize
= 2;
802 pd
->bmControls
[0] = 1;
803 pd
->bmControls
[1] = 0;
806 od
= &opts
->uvc_output_terminal
;
807 od
->bLength
= UVC_DT_OUTPUT_TERMINAL_SIZE
;
808 od
->bDescriptorType
= USB_DT_CS_INTERFACE
;
809 od
->bDescriptorSubType
= UVC_VC_OUTPUT_TERMINAL
;
811 od
->wTerminalType
= cpu_to_le16(0x0101);
812 od
->bAssocTerminal
= 0;
816 md
= &opts
->uvc_color_matching
;
817 md
->bLength
= UVC_DT_COLOR_MATCHING_SIZE
;
818 md
->bDescriptorType
= USB_DT_CS_INTERFACE
;
819 md
->bDescriptorSubType
= UVC_VS_COLORFORMAT
;
820 md
->bColorPrimaries
= 1;
821 md
->bTransferCharacteristics
= 1;
822 md
->bMatrixCoefficients
= 4;
824 /* Prepare fs control class descriptors for configfs-based gadgets */
825 ctl_cls
= opts
->uvc_fs_control_cls
;
826 ctl_cls
[0] = NULL
; /* assigned elsewhere by configfs */
827 ctl_cls
[1] = (struct uvc_descriptor_header
*)cd
;
828 ctl_cls
[2] = (struct uvc_descriptor_header
*)pd
;
829 ctl_cls
[3] = (struct uvc_descriptor_header
*)od
;
830 ctl_cls
[4] = NULL
; /* NULL-terminate */
832 (const struct uvc_descriptor_header
* const *)ctl_cls
;
834 /* Prepare hs control class descriptors for configfs-based gadgets */
835 ctl_cls
= opts
->uvc_ss_control_cls
;
836 ctl_cls
[0] = NULL
; /* assigned elsewhere by configfs */
837 ctl_cls
[1] = (struct uvc_descriptor_header
*)cd
;
838 ctl_cls
[2] = (struct uvc_descriptor_header
*)pd
;
839 ctl_cls
[3] = (struct uvc_descriptor_header
*)od
;
840 ctl_cls
[4] = NULL
; /* NULL-terminate */
842 (const struct uvc_descriptor_header
* const *)ctl_cls
;
844 opts
->streaming_interval
= 1;
845 opts
->streaming_maxpacket
= 1024;
847 uvcg_attach_configfs(opts
);
848 return &opts
->func_inst
;
851 static void uvc_free(struct usb_function
*f
)
853 struct uvc_device
*uvc
= to_uvc(f
);
854 struct f_uvc_opts
*opts
= container_of(f
->fi
, struct f_uvc_opts
,
860 static void uvc_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
862 struct usb_composite_dev
*cdev
= c
->cdev
;
863 struct uvc_device
*uvc
= to_uvc(f
);
865 INFO(cdev
, "%s\n", __func__
);
867 video_unregister_device(&uvc
->vdev
);
868 v4l2_device_unregister(&uvc
->v4l2_dev
);
870 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
871 kfree(uvc
->control_buf
);
873 usb_free_all_descriptors(f
);
876 static struct usb_function
*uvc_alloc(struct usb_function_instance
*fi
)
878 struct uvc_device
*uvc
;
879 struct f_uvc_opts
*opts
;
880 struct uvc_descriptor_header
**strm_cls
;
882 uvc
= kzalloc(sizeof(*uvc
), GFP_KERNEL
);
884 return ERR_PTR(-ENOMEM
);
886 mutex_init(&uvc
->video
.mutex
);
887 uvc
->state
= UVC_STATE_DISCONNECTED
;
888 opts
= fi_to_f_uvc_opts(fi
);
890 mutex_lock(&opts
->lock
);
891 if (opts
->uvc_fs_streaming_cls
) {
892 strm_cls
= opts
->uvc_fs_streaming_cls
;
894 (const struct uvc_descriptor_header
* const *)strm_cls
;
896 if (opts
->uvc_hs_streaming_cls
) {
897 strm_cls
= opts
->uvc_hs_streaming_cls
;
899 (const struct uvc_descriptor_header
* const *)strm_cls
;
901 if (opts
->uvc_ss_streaming_cls
) {
902 strm_cls
= opts
->uvc_ss_streaming_cls
;
904 (const struct uvc_descriptor_header
* const *)strm_cls
;
907 uvc
->desc
.fs_control
= opts
->fs_control
;
908 uvc
->desc
.ss_control
= opts
->ss_control
;
909 uvc
->desc
.fs_streaming
= opts
->fs_streaming
;
910 uvc
->desc
.hs_streaming
= opts
->hs_streaming
;
911 uvc
->desc
.ss_streaming
= opts
->ss_streaming
;
913 mutex_unlock(&opts
->lock
);
915 /* Register the function. */
916 uvc
->func
.name
= "uvc";
917 uvc
->func
.bind
= uvc_function_bind
;
918 uvc
->func
.unbind
= uvc_unbind
;
919 uvc
->func
.get_alt
= uvc_function_get_alt
;
920 uvc
->func
.set_alt
= uvc_function_set_alt
;
921 uvc
->func
.disable
= uvc_function_disable
;
922 uvc
->func
.setup
= uvc_function_setup
;
923 uvc
->func
.free_func
= uvc_free
;
924 uvc
->func
.bind_deactivated
= true;
929 DECLARE_USB_FUNCTION_INIT(uvc
, uvc_alloc_inst
, uvc_alloc
);
930 MODULE_LICENSE("GPL");
931 MODULE_AUTHOR("Laurent Pinchart");