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/device.h>
10 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/module.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/g_uvc.h>
20 #include <linux/usb/video.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-event.h>
29 #include "uvc_configfs.h"
31 #include "uvc_video.h"
33 unsigned int uvc_gadget_trace_param
;
34 module_param_named(trace
, uvc_gadget_trace_param
, uint
, 0644);
35 MODULE_PARM_DESC(trace
, "Trace level bitmask");
37 /* --------------------------------------------------------------------------
38 * Function descriptors
41 /* string IDs are assigned dynamically */
43 #define UVC_STRING_CONTROL_IDX 0
44 #define UVC_STRING_STREAMING_IDX 1
46 static struct usb_string uvc_en_us_strings
[] = {
47 [UVC_STRING_CONTROL_IDX
].s
= "UVC Camera",
48 [UVC_STRING_STREAMING_IDX
].s
= "Video Streaming",
52 static struct usb_gadget_strings uvc_stringtab
= {
53 .language
= 0x0409, /* en-us */
54 .strings
= uvc_en_us_strings
,
57 static struct usb_gadget_strings
*uvc_function_strings
[] = {
62 #define UVC_INTF_VIDEO_CONTROL 0
63 #define UVC_INTF_VIDEO_STREAMING 1
65 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
67 static struct usb_interface_assoc_descriptor uvc_iad
= {
68 .bLength
= sizeof(uvc_iad
),
69 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
72 .bFunctionClass
= USB_CLASS_VIDEO
,
73 .bFunctionSubClass
= UVC_SC_VIDEO_INTERFACE_COLLECTION
,
74 .bFunctionProtocol
= 0x00,
78 static struct usb_interface_descriptor uvc_control_intf
= {
79 .bLength
= USB_DT_INTERFACE_SIZE
,
80 .bDescriptorType
= USB_DT_INTERFACE
,
81 .bInterfaceNumber
= UVC_INTF_VIDEO_CONTROL
,
82 .bAlternateSetting
= 0,
84 .bInterfaceClass
= USB_CLASS_VIDEO
,
85 .bInterfaceSubClass
= UVC_SC_VIDEOCONTROL
,
86 .bInterfaceProtocol
= 0x00,
90 static struct usb_endpoint_descriptor uvc_control_ep
= {
91 .bLength
= USB_DT_ENDPOINT_SIZE
,
92 .bDescriptorType
= USB_DT_ENDPOINT
,
93 .bEndpointAddress
= USB_DIR_IN
,
94 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
95 .wMaxPacketSize
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
99 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp
= {
100 .bLength
= sizeof(uvc_ss_control_comp
),
101 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
102 /* The following 3 values can be tweaked if necessary. */
105 .wBytesPerInterval
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
108 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep
= {
109 .bLength
= UVC_DT_CONTROL_ENDPOINT_SIZE
,
110 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
111 .bDescriptorSubType
= UVC_EP_INTERRUPT
,
112 .wMaxTransferSize
= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE
),
115 static struct usb_interface_descriptor uvc_streaming_intf_alt0
= {
116 .bLength
= USB_DT_INTERFACE_SIZE
,
117 .bDescriptorType
= USB_DT_INTERFACE
,
118 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
119 .bAlternateSetting
= 0,
121 .bInterfaceClass
= USB_CLASS_VIDEO
,
122 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
123 .bInterfaceProtocol
= 0x00,
127 static struct usb_interface_descriptor uvc_streaming_intf_alt1
= {
128 .bLength
= USB_DT_INTERFACE_SIZE
,
129 .bDescriptorType
= USB_DT_INTERFACE
,
130 .bInterfaceNumber
= UVC_INTF_VIDEO_STREAMING
,
131 .bAlternateSetting
= 1,
133 .bInterfaceClass
= USB_CLASS_VIDEO
,
134 .bInterfaceSubClass
= UVC_SC_VIDEOSTREAMING
,
135 .bInterfaceProtocol
= 0x00,
139 static struct usb_endpoint_descriptor uvc_fs_streaming_ep
= {
140 .bLength
= USB_DT_ENDPOINT_SIZE
,
141 .bDescriptorType
= USB_DT_ENDPOINT
,
142 .bEndpointAddress
= USB_DIR_IN
,
143 .bmAttributes
= USB_ENDPOINT_SYNC_ASYNC
144 | USB_ENDPOINT_XFER_ISOC
,
145 /* The wMaxPacketSize and bInterval values will be initialized from
150 static struct usb_endpoint_descriptor uvc_hs_streaming_ep
= {
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_ss_streaming_ep
= {
162 .bLength
= USB_DT_ENDPOINT_SIZE
,
163 .bDescriptorType
= USB_DT_ENDPOINT
,
165 .bEndpointAddress
= USB_DIR_IN
,
166 .bmAttributes
= USB_ENDPOINT_SYNC_ASYNC
167 | USB_ENDPOINT_XFER_ISOC
,
168 /* The wMaxPacketSize and bInterval values will be initialized from
173 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp
= {
174 .bLength
= sizeof(uvc_ss_streaming_comp
),
175 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
176 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
177 * initialized from module parameters.
181 static const struct usb_descriptor_header
* const uvc_fs_streaming
[] = {
182 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
183 (struct usb_descriptor_header
*) &uvc_fs_streaming_ep
,
187 static const struct usb_descriptor_header
* const uvc_hs_streaming
[] = {
188 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
189 (struct usb_descriptor_header
*) &uvc_hs_streaming_ep
,
193 static const struct usb_descriptor_header
* const uvc_ss_streaming
[] = {
194 (struct usb_descriptor_header
*) &uvc_streaming_intf_alt1
,
195 (struct usb_descriptor_header
*) &uvc_ss_streaming_ep
,
196 (struct usb_descriptor_header
*) &uvc_ss_streaming_comp
,
200 /* --------------------------------------------------------------------------
205 uvc_function_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
207 struct uvc_device
*uvc
= req
->context
;
208 struct v4l2_event v4l2_event
;
209 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
211 if (uvc
->event_setup_out
) {
212 uvc
->event_setup_out
= 0;
214 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
215 v4l2_event
.type
= UVC_EVENT_DATA
;
216 uvc_event
->data
.length
= req
->actual
;
217 memcpy(&uvc_event
->data
.data
, req
->buf
, req
->actual
);
218 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
223 uvc_function_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
225 struct uvc_device
*uvc
= to_uvc(f
);
226 struct v4l2_event v4l2_event
;
227 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
229 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_CLASS
) {
230 uvcg_info(f
, "invalid request type\n");
234 /* Stall too big requests. */
235 if (le16_to_cpu(ctrl
->wLength
) > UVC_MAX_REQUEST_SIZE
)
238 /* Tell the complete callback to generate an event for the next request
239 * that will be enqueued by UVCIOC_SEND_RESPONSE.
241 uvc
->event_setup_out
= !(ctrl
->bRequestType
& USB_DIR_IN
);
242 uvc
->event_length
= le16_to_cpu(ctrl
->wLength
);
244 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
245 v4l2_event
.type
= UVC_EVENT_SETUP
;
246 memcpy(&uvc_event
->req
, ctrl
, sizeof(uvc_event
->req
));
247 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
252 void uvc_function_setup_continue(struct uvc_device
*uvc
)
254 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
256 usb_composite_setup_continue(cdev
);
260 uvc_function_get_alt(struct usb_function
*f
, unsigned interface
)
262 struct uvc_device
*uvc
= to_uvc(f
);
264 uvcg_info(f
, "%s(%u)\n", __func__
, interface
);
266 if (interface
== uvc
->control_intf
)
268 else if (interface
!= uvc
->streaming_intf
)
271 return uvc
->video
.ep
->enabled
? 1 : 0;
275 uvc_function_set_alt(struct usb_function
*f
, unsigned interface
, unsigned alt
)
277 struct uvc_device
*uvc
= to_uvc(f
);
278 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
279 struct v4l2_event v4l2_event
;
280 struct uvc_event
*uvc_event
= (void *)&v4l2_event
.u
.data
;
283 uvcg_info(f
, "%s(%u, %u)\n", __func__
, interface
, alt
);
285 if (interface
== uvc
->control_intf
) {
289 uvcg_info(f
, "reset UVC Control\n");
290 usb_ep_disable(uvc
->control_ep
);
292 if (!uvc
->control_ep
->desc
)
293 if (config_ep_by_speed(cdev
->gadget
, f
, uvc
->control_ep
))
296 usb_ep_enable(uvc
->control_ep
);
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
= 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
)
340 uvcg_info(f
, "reset UVC\n");
341 usb_ep_disable(uvc
->video
.ep
);
343 ret
= config_ep_by_speed(f
->config
->cdev
->gadget
,
344 &(uvc
->func
), uvc
->video
.ep
);
347 usb_ep_enable(uvc
->video
.ep
);
349 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
350 v4l2_event
.type
= UVC_EVENT_STREAMON
;
351 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
352 return USB_GADGET_DELAYED_STATUS
;
360 uvc_function_disable(struct usb_function
*f
)
362 struct uvc_device
*uvc
= to_uvc(f
);
363 struct v4l2_event v4l2_event
;
365 uvcg_info(f
, "%s()\n", __func__
);
367 memset(&v4l2_event
, 0, sizeof(v4l2_event
));
368 v4l2_event
.type
= UVC_EVENT_DISCONNECT
;
369 v4l2_event_queue(&uvc
->vdev
, &v4l2_event
);
371 uvc
->state
= UVC_STATE_DISCONNECTED
;
373 usb_ep_disable(uvc
->video
.ep
);
374 usb_ep_disable(uvc
->control_ep
);
377 /* --------------------------------------------------------------------------
378 * Connection / disconnection
382 uvc_function_connect(struct uvc_device
*uvc
)
386 if ((ret
= usb_function_activate(&uvc
->func
)) < 0)
387 uvcg_info(&uvc
->func
, "UVC connect failed with %d\n", ret
);
391 uvc_function_disconnect(struct uvc_device
*uvc
)
395 if ((ret
= usb_function_deactivate(&uvc
->func
)) < 0)
396 uvcg_info(&uvc
->func
, "UVC disconnect failed with %d\n", ret
);
399 /* --------------------------------------------------------------------------
400 * USB probe and disconnect
403 static ssize_t
function_name_show(struct device
*dev
,
404 struct device_attribute
*attr
, char *buf
)
406 struct uvc_device
*uvc
= dev_get_drvdata(dev
);
408 return sprintf(buf
, "%s\n", uvc
->func
.fi
->group
.cg_item
.ci_name
);
411 static DEVICE_ATTR_RO(function_name
);
414 uvc_register_video(struct uvc_device
*uvc
)
416 struct usb_composite_dev
*cdev
= uvc
->func
.config
->cdev
;
419 /* TODO reference counting. */
420 uvc
->vdev
.v4l2_dev
= &uvc
->v4l2_dev
;
421 uvc
->vdev
.fops
= &uvc_v4l2_fops
;
422 uvc
->vdev
.ioctl_ops
= &uvc_v4l2_ioctl_ops
;
423 uvc
->vdev
.release
= video_device_release_empty
;
424 uvc
->vdev
.vfl_dir
= VFL_DIR_TX
;
425 uvc
->vdev
.lock
= &uvc
->video
.mutex
;
426 uvc
->vdev
.device_caps
= V4L2_CAP_VIDEO_OUTPUT
| V4L2_CAP_STREAMING
;
427 strlcpy(uvc
->vdev
.name
, cdev
->gadget
->name
, sizeof(uvc
->vdev
.name
));
429 video_set_drvdata(&uvc
->vdev
, uvc
);
431 ret
= video_register_device(&uvc
->vdev
, VFL_TYPE_GRABBER
, -1);
435 ret
= device_create_file(&uvc
->vdev
.dev
, &dev_attr_function_name
);
437 video_unregister_device(&uvc
->vdev
);
444 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
446 memcpy(mem, desc, (desc)->bLength); \
448 mem += (desc)->bLength; \
451 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
453 const struct usb_descriptor_header * const *__src; \
454 for (__src = src; *__src; ++__src) { \
455 memcpy(mem, *__src, (*__src)->bLength); \
457 mem += (*__src)->bLength; \
461 static struct usb_descriptor_header
**
462 uvc_copy_descriptors(struct uvc_device
*uvc
, enum usb_device_speed speed
)
464 struct uvc_input_header_descriptor
*uvc_streaming_header
;
465 struct uvc_header_descriptor
*uvc_control_header
;
466 const struct uvc_descriptor_header
* const *uvc_control_desc
;
467 const struct uvc_descriptor_header
* const *uvc_streaming_cls
;
468 const struct usb_descriptor_header
* const *uvc_streaming_std
;
469 const struct usb_descriptor_header
* const *src
;
470 struct usb_descriptor_header
**dst
;
471 struct usb_descriptor_header
**hdr
;
472 unsigned int control_size
;
473 unsigned int streaming_size
;
479 case USB_SPEED_SUPER
:
480 uvc_control_desc
= uvc
->desc
.ss_control
;
481 uvc_streaming_cls
= uvc
->desc
.ss_streaming
;
482 uvc_streaming_std
= uvc_ss_streaming
;
486 uvc_control_desc
= uvc
->desc
.fs_control
;
487 uvc_streaming_cls
= uvc
->desc
.hs_streaming
;
488 uvc_streaming_std
= uvc_hs_streaming
;
493 uvc_control_desc
= uvc
->desc
.fs_control
;
494 uvc_streaming_cls
= uvc
->desc
.fs_streaming
;
495 uvc_streaming_std
= uvc_fs_streaming
;
499 if (!uvc_control_desc
|| !uvc_streaming_cls
)
500 return ERR_PTR(-ENODEV
);
502 /* Descriptors layout
506 * Class-specific UVC control descriptors
509 * uvc_ss_control_comp (for SS only)
510 * uvc_streaming_intf_alt0
511 * Class-specific UVC streaming descriptors
512 * uvc_{fs|hs}_streaming
515 /* Count descriptors and compute their size. */
518 bytes
= uvc_iad
.bLength
+ uvc_control_intf
.bLength
519 + uvc_control_ep
.bLength
+ uvc_control_cs_ep
.bLength
520 + uvc_streaming_intf_alt0
.bLength
;
522 if (speed
== USB_SPEED_SUPER
) {
523 bytes
+= uvc_ss_control_comp
.bLength
;
529 for (src
= (const struct usb_descriptor_header
**)uvc_control_desc
;
531 control_size
+= (*src
)->bLength
;
532 bytes
+= (*src
)->bLength
;
535 for (src
= (const struct usb_descriptor_header
**)uvc_streaming_cls
;
537 streaming_size
+= (*src
)->bLength
;
538 bytes
+= (*src
)->bLength
;
541 for (src
= uvc_streaming_std
; *src
; ++src
) {
542 bytes
+= (*src
)->bLength
;
546 mem
= kmalloc((n_desc
+ 1) * sizeof(*src
) + bytes
, GFP_KERNEL
);
552 mem
+= (n_desc
+ 1) * sizeof(*src
);
554 /* Copy the descriptors. */
555 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_iad
);
556 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_intf
);
558 uvc_control_header
= mem
;
559 UVC_COPY_DESCRIPTORS(mem
, dst
,
560 (const struct usb_descriptor_header
**)uvc_control_desc
);
561 uvc_control_header
->wTotalLength
= cpu_to_le16(control_size
);
562 uvc_control_header
->bInCollection
= 1;
563 uvc_control_header
->baInterfaceNr
[0] = uvc
->streaming_intf
;
565 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_ep
);
566 if (speed
== USB_SPEED_SUPER
)
567 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_ss_control_comp
);
569 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_control_cs_ep
);
570 UVC_COPY_DESCRIPTOR(mem
, dst
, &uvc_streaming_intf_alt0
);
572 uvc_streaming_header
= mem
;
573 UVC_COPY_DESCRIPTORS(mem
, dst
,
574 (const struct usb_descriptor_header
**)uvc_streaming_cls
);
575 uvc_streaming_header
->wTotalLength
= cpu_to_le16(streaming_size
);
576 uvc_streaming_header
->bEndpointAddress
= uvc
->video
.ep
->address
;
578 UVC_COPY_DESCRIPTORS(mem
, dst
, uvc_streaming_std
);
585 uvc_function_bind(struct usb_configuration
*c
, struct usb_function
*f
)
587 struct usb_composite_dev
*cdev
= c
->cdev
;
588 struct uvc_device
*uvc
= to_uvc(f
);
589 struct usb_string
*us
;
590 unsigned int max_packet_mult
;
591 unsigned int max_packet_size
;
593 struct f_uvc_opts
*opts
;
596 uvcg_info(f
, "%s()\n", __func__
);
598 opts
= fi_to_f_uvc_opts(f
->fi
);
599 /* Sanity check the streaming endpoint module parameters.
601 opts
->streaming_interval
= clamp(opts
->streaming_interval
, 1U, 16U);
602 opts
->streaming_maxpacket
= clamp(opts
->streaming_maxpacket
, 1U, 3072U);
603 opts
->streaming_maxburst
= min(opts
->streaming_maxburst
, 15U);
605 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
606 if (opts
->streaming_maxburst
&&
607 (opts
->streaming_maxpacket
% 1024) != 0) {
608 opts
->streaming_maxpacket
= roundup(opts
->streaming_maxpacket
, 1024);
609 uvcg_info(f
, "overriding streaming_maxpacket to %d\n",
610 opts
->streaming_maxpacket
);
613 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
616 * NOTE: We assume that the user knows what they are doing and won't
617 * give parameters that their UDC doesn't support.
619 if (opts
->streaming_maxpacket
<= 1024) {
621 max_packet_size
= opts
->streaming_maxpacket
;
622 } else if (opts
->streaming_maxpacket
<= 2048) {
624 max_packet_size
= opts
->streaming_maxpacket
/ 2;
627 max_packet_size
= opts
->streaming_maxpacket
/ 3;
630 uvc_fs_streaming_ep
.wMaxPacketSize
=
631 cpu_to_le16(min(opts
->streaming_maxpacket
, 1023U));
632 uvc_fs_streaming_ep
.bInterval
= opts
->streaming_interval
;
634 uvc_hs_streaming_ep
.wMaxPacketSize
=
635 cpu_to_le16(max_packet_size
| ((max_packet_mult
- 1) << 11));
636 uvc_hs_streaming_ep
.bInterval
= opts
->streaming_interval
;
638 uvc_ss_streaming_ep
.wMaxPacketSize
= cpu_to_le16(max_packet_size
);
639 uvc_ss_streaming_ep
.bInterval
= opts
->streaming_interval
;
640 uvc_ss_streaming_comp
.bmAttributes
= max_packet_mult
- 1;
641 uvc_ss_streaming_comp
.bMaxBurst
= opts
->streaming_maxburst
;
642 uvc_ss_streaming_comp
.wBytesPerInterval
=
643 cpu_to_le16(max_packet_size
* max_packet_mult
*
644 (opts
->streaming_maxburst
+ 1));
646 /* Allocate endpoints. */
647 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_control_ep
);
649 uvcg_info(f
, "Unable to allocate control EP\n");
652 uvc
->control_ep
= ep
;
654 if (gadget_is_superspeed(c
->cdev
->gadget
))
655 ep
= usb_ep_autoconfig_ss(cdev
->gadget
, &uvc_ss_streaming_ep
,
656 &uvc_ss_streaming_comp
);
657 else if (gadget_is_dualspeed(cdev
->gadget
))
658 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_hs_streaming_ep
);
660 ep
= usb_ep_autoconfig(cdev
->gadget
, &uvc_fs_streaming_ep
);
663 uvcg_info(f
, "Unable to allocate streaming EP\n");
668 uvc_fs_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
669 uvc_hs_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
670 uvc_ss_streaming_ep
.bEndpointAddress
= uvc
->video
.ep
->address
;
672 us
= usb_gstrings_attach(cdev
, uvc_function_strings
,
673 ARRAY_SIZE(uvc_en_us_strings
));
678 uvc_iad
.iFunction
= us
[UVC_STRING_CONTROL_IDX
].id
;
679 uvc_control_intf
.iInterface
= us
[UVC_STRING_CONTROL_IDX
].id
;
680 ret
= us
[UVC_STRING_STREAMING_IDX
].id
;
681 uvc_streaming_intf_alt0
.iInterface
= ret
;
682 uvc_streaming_intf_alt1
.iInterface
= ret
;
684 /* Allocate interface IDs. */
685 if ((ret
= usb_interface_id(c
, f
)) < 0)
687 uvc_iad
.bFirstInterface
= ret
;
688 uvc_control_intf
.bInterfaceNumber
= ret
;
689 uvc
->control_intf
= ret
;
690 opts
->control_interface
= ret
;
692 if ((ret
= usb_interface_id(c
, f
)) < 0)
694 uvc_streaming_intf_alt0
.bInterfaceNumber
= ret
;
695 uvc_streaming_intf_alt1
.bInterfaceNumber
= ret
;
696 uvc
->streaming_intf
= ret
;
697 opts
->streaming_interface
= ret
;
699 /* Copy descriptors */
700 f
->fs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_FULL
);
701 if (IS_ERR(f
->fs_descriptors
)) {
702 ret
= PTR_ERR(f
->fs_descriptors
);
703 f
->fs_descriptors
= NULL
;
706 if (gadget_is_dualspeed(cdev
->gadget
)) {
707 f
->hs_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_HIGH
);
708 if (IS_ERR(f
->hs_descriptors
)) {
709 ret
= PTR_ERR(f
->hs_descriptors
);
710 f
->hs_descriptors
= NULL
;
714 if (gadget_is_superspeed(c
->cdev
->gadget
)) {
715 f
->ss_descriptors
= uvc_copy_descriptors(uvc
, USB_SPEED_SUPER
);
716 if (IS_ERR(f
->ss_descriptors
)) {
717 ret
= PTR_ERR(f
->ss_descriptors
);
718 f
->ss_descriptors
= NULL
;
723 /* Preallocate control endpoint request. */
724 uvc
->control_req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
725 uvc
->control_buf
= kmalloc(UVC_MAX_REQUEST_SIZE
, GFP_KERNEL
);
726 if (uvc
->control_req
== NULL
|| uvc
->control_buf
== NULL
) {
731 uvc
->control_req
->buf
= uvc
->control_buf
;
732 uvc
->control_req
->complete
= uvc_function_ep0_complete
;
733 uvc
->control_req
->context
= uvc
;
735 if (v4l2_device_register(&cdev
->gadget
->dev
, &uvc
->v4l2_dev
)) {
736 uvcg_err(f
, "failed to register V4L2 device\n");
740 /* Initialise video. */
741 ret
= uvcg_video_init(&uvc
->video
, uvc
);
745 /* Register a V4L2 device. */
746 ret
= uvc_register_video(uvc
);
748 uvcg_err(f
, "failed to register video device\n");
755 v4l2_device_unregister(&uvc
->v4l2_dev
);
757 if (uvc
->control_req
)
758 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
759 kfree(uvc
->control_buf
);
761 usb_free_all_descriptors(f
);
765 /* --------------------------------------------------------------------------
766 * USB gadget function
769 static void uvc_free_inst(struct usb_function_instance
*f
)
771 struct f_uvc_opts
*opts
= fi_to_f_uvc_opts(f
);
773 mutex_destroy(&opts
->lock
);
777 static struct usb_function_instance
*uvc_alloc_inst(void)
779 struct f_uvc_opts
*opts
;
780 struct uvc_camera_terminal_descriptor
*cd
;
781 struct uvc_processing_unit_descriptor
*pd
;
782 struct uvc_output_terminal_descriptor
*od
;
783 struct uvc_color_matching_descriptor
*md
;
784 struct uvc_descriptor_header
**ctl_cls
;
787 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
789 return ERR_PTR(-ENOMEM
);
790 opts
->func_inst
.free_func_inst
= uvc_free_inst
;
791 mutex_init(&opts
->lock
);
793 cd
= &opts
->uvc_camera_terminal
;
794 cd
->bLength
= UVC_DT_CAMERA_TERMINAL_SIZE(3);
795 cd
->bDescriptorType
= USB_DT_CS_INTERFACE
;
796 cd
->bDescriptorSubType
= UVC_VC_INPUT_TERMINAL
;
798 cd
->wTerminalType
= cpu_to_le16(0x0201);
799 cd
->bAssocTerminal
= 0;
801 cd
->wObjectiveFocalLengthMin
= cpu_to_le16(0);
802 cd
->wObjectiveFocalLengthMax
= cpu_to_le16(0);
803 cd
->wOcularFocalLength
= cpu_to_le16(0);
804 cd
->bControlSize
= 3;
805 cd
->bmControls
[0] = 2;
806 cd
->bmControls
[1] = 0;
807 cd
->bmControls
[2] = 0;
809 pd
= &opts
->uvc_processing
;
810 pd
->bLength
= UVC_DT_PROCESSING_UNIT_SIZE(2);
811 pd
->bDescriptorType
= USB_DT_CS_INTERFACE
;
812 pd
->bDescriptorSubType
= UVC_VC_PROCESSING_UNIT
;
815 pd
->wMaxMultiplier
= cpu_to_le16(16*1024);
816 pd
->bControlSize
= 2;
817 pd
->bmControls
[0] = 1;
818 pd
->bmControls
[1] = 0;
821 od
= &opts
->uvc_output_terminal
;
822 od
->bLength
= UVC_DT_OUTPUT_TERMINAL_SIZE
;
823 od
->bDescriptorType
= USB_DT_CS_INTERFACE
;
824 od
->bDescriptorSubType
= UVC_VC_OUTPUT_TERMINAL
;
826 od
->wTerminalType
= cpu_to_le16(0x0101);
827 od
->bAssocTerminal
= 0;
831 md
= &opts
->uvc_color_matching
;
832 md
->bLength
= UVC_DT_COLOR_MATCHING_SIZE
;
833 md
->bDescriptorType
= USB_DT_CS_INTERFACE
;
834 md
->bDescriptorSubType
= UVC_VS_COLORFORMAT
;
835 md
->bColorPrimaries
= 1;
836 md
->bTransferCharacteristics
= 1;
837 md
->bMatrixCoefficients
= 4;
839 /* Prepare fs control class descriptors for configfs-based gadgets */
840 ctl_cls
= opts
->uvc_fs_control_cls
;
841 ctl_cls
[0] = NULL
; /* assigned elsewhere by configfs */
842 ctl_cls
[1] = (struct uvc_descriptor_header
*)cd
;
843 ctl_cls
[2] = (struct uvc_descriptor_header
*)pd
;
844 ctl_cls
[3] = (struct uvc_descriptor_header
*)od
;
845 ctl_cls
[4] = NULL
; /* NULL-terminate */
847 (const struct uvc_descriptor_header
* const *)ctl_cls
;
849 /* Prepare hs control class descriptors for configfs-based gadgets */
850 ctl_cls
= opts
->uvc_ss_control_cls
;
851 ctl_cls
[0] = NULL
; /* assigned elsewhere by configfs */
852 ctl_cls
[1] = (struct uvc_descriptor_header
*)cd
;
853 ctl_cls
[2] = (struct uvc_descriptor_header
*)pd
;
854 ctl_cls
[3] = (struct uvc_descriptor_header
*)od
;
855 ctl_cls
[4] = NULL
; /* NULL-terminate */
857 (const struct uvc_descriptor_header
* const *)ctl_cls
;
859 opts
->streaming_interval
= 1;
860 opts
->streaming_maxpacket
= 1024;
862 ret
= uvcg_attach_configfs(opts
);
868 return &opts
->func_inst
;
871 static void uvc_free(struct usb_function
*f
)
873 struct uvc_device
*uvc
= to_uvc(f
);
874 struct f_uvc_opts
*opts
= container_of(f
->fi
, struct f_uvc_opts
,
880 static void uvc_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
882 struct usb_composite_dev
*cdev
= c
->cdev
;
883 struct uvc_device
*uvc
= to_uvc(f
);
885 uvcg_info(f
, "%s\n", __func__
);
887 device_remove_file(&uvc
->vdev
.dev
, &dev_attr_function_name
);
888 video_unregister_device(&uvc
->vdev
);
889 v4l2_device_unregister(&uvc
->v4l2_dev
);
891 usb_ep_free_request(cdev
->gadget
->ep0
, uvc
->control_req
);
892 kfree(uvc
->control_buf
);
894 usb_free_all_descriptors(f
);
897 static struct usb_function
*uvc_alloc(struct usb_function_instance
*fi
)
899 struct uvc_device
*uvc
;
900 struct f_uvc_opts
*opts
;
901 struct uvc_descriptor_header
**strm_cls
;
903 uvc
= kzalloc(sizeof(*uvc
), GFP_KERNEL
);
905 return ERR_PTR(-ENOMEM
);
907 mutex_init(&uvc
->video
.mutex
);
908 uvc
->state
= UVC_STATE_DISCONNECTED
;
909 opts
= fi_to_f_uvc_opts(fi
);
911 mutex_lock(&opts
->lock
);
912 if (opts
->uvc_fs_streaming_cls
) {
913 strm_cls
= opts
->uvc_fs_streaming_cls
;
915 (const struct uvc_descriptor_header
* const *)strm_cls
;
917 if (opts
->uvc_hs_streaming_cls
) {
918 strm_cls
= opts
->uvc_hs_streaming_cls
;
920 (const struct uvc_descriptor_header
* const *)strm_cls
;
922 if (opts
->uvc_ss_streaming_cls
) {
923 strm_cls
= opts
->uvc_ss_streaming_cls
;
925 (const struct uvc_descriptor_header
* const *)strm_cls
;
928 uvc
->desc
.fs_control
= opts
->fs_control
;
929 uvc
->desc
.ss_control
= opts
->ss_control
;
930 uvc
->desc
.fs_streaming
= opts
->fs_streaming
;
931 uvc
->desc
.hs_streaming
= opts
->hs_streaming
;
932 uvc
->desc
.ss_streaming
= opts
->ss_streaming
;
934 mutex_unlock(&opts
->lock
);
936 /* Register the function. */
937 uvc
->func
.name
= "uvc";
938 uvc
->func
.bind
= uvc_function_bind
;
939 uvc
->func
.unbind
= uvc_unbind
;
940 uvc
->func
.get_alt
= uvc_function_get_alt
;
941 uvc
->func
.set_alt
= uvc_function_set_alt
;
942 uvc
->func
.disable
= uvc_function_disable
;
943 uvc
->func
.setup
= uvc_function_setup
;
944 uvc
->func
.free_func
= uvc_free
;
945 uvc
->func
.bind_deactivated
= true;
950 DECLARE_USB_FUNCTION_INIT(uvc
, uvc_alloc_inst
, uvc_alloc
);
951 MODULE_LICENSE("GPL");
952 MODULE_AUTHOR("Laurent Pinchart");