Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * uvc_gadget.c -- USB Video Class Gadget driver
4 *
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
8
9#include <linux/device.h>
10#include <linux/errno.h>
11#include <linux/fs.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>
23
24#include <media/v4l2-dev.h>
25#include <media/v4l2-event.h>
26
27#include "uvc.h"
28#include "uvc_configfs.h"
29#include "uvc_v4l2.h"
30#include "uvc_video.h"
31
32unsigned int uvc_gadget_trace_param;
33module_param_named(trace, uvc_gadget_trace_param, uint, 0644);
34MODULE_PARM_DESC(trace, "Trace level bitmask");
35
36/* --------------------------------------------------------------------------
37 * Function descriptors
38 */
39
40/* string IDs are assigned dynamically */
41
42static struct usb_string uvc_en_us_strings[] = {
43 /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */
44 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
45 { }
46};
47
48static struct usb_gadget_strings uvc_stringtab = {
49 .language = 0x0409, /* en-us */
50 .strings = uvc_en_us_strings,
51};
52
53static struct usb_gadget_strings *uvc_function_strings[] = {
54 &uvc_stringtab,
55 NULL,
56};
57
58#define UVC_INTF_VIDEO_CONTROL 0
59#define UVC_INTF_VIDEO_STREAMING 1
60
61#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
62
63static struct usb_interface_assoc_descriptor uvc_iad = {
64 .bLength = sizeof(uvc_iad),
65 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
66 .bFirstInterface = 0,
67 .bInterfaceCount = 2,
68 .bFunctionClass = USB_CLASS_VIDEO,
69 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
70 .bFunctionProtocol = 0x00,
71 .iFunction = 0,
72};
73
74static struct usb_interface_descriptor uvc_control_intf = {
75 .bLength = USB_DT_INTERFACE_SIZE,
76 .bDescriptorType = USB_DT_INTERFACE,
77 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
78 .bAlternateSetting = 0,
79 .bNumEndpoints = 0,
80 .bInterfaceClass = USB_CLASS_VIDEO,
81 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
82 .bInterfaceProtocol = 0x00,
83 .iInterface = 0,
84};
85
86static struct usb_endpoint_descriptor uvc_interrupt_ep = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89 .bEndpointAddress = USB_DIR_IN,
90 .bmAttributes = USB_ENDPOINT_XFER_INT,
91 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
92 .bInterval = 8,
93};
94
95static struct usb_ss_ep_comp_descriptor uvc_ss_interrupt_comp = {
96 .bLength = sizeof(uvc_ss_interrupt_comp),
97 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
98 /* The following 3 values can be tweaked if necessary. */
99 .bMaxBurst = 0,
100 .bmAttributes = 0,
101 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
102};
103
104static struct uvc_control_endpoint_descriptor uvc_interrupt_cs_ep = {
105 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
106 .bDescriptorType = USB_DT_CS_ENDPOINT,
107 .bDescriptorSubType = UVC_EP_INTERRUPT,
108 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
109};
110
111static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
112 .bLength = USB_DT_INTERFACE_SIZE,
113 .bDescriptorType = USB_DT_INTERFACE,
114 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
115 .bAlternateSetting = 0,
116 .bNumEndpoints = 0,
117 .bInterfaceClass = USB_CLASS_VIDEO,
118 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
119 .bInterfaceProtocol = 0x00,
120 .iInterface = 0,
121};
122
123static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
124 .bLength = USB_DT_INTERFACE_SIZE,
125 .bDescriptorType = USB_DT_INTERFACE,
126 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
127 .bAlternateSetting = 1,
128 .bNumEndpoints = 1,
129 .bInterfaceClass = USB_CLASS_VIDEO,
130 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
131 .bInterfaceProtocol = 0x00,
132 .iInterface = 0,
133};
134
135static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT,
138 .bEndpointAddress = USB_DIR_IN,
139 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
140 | USB_ENDPOINT_XFER_ISOC,
141 /*
142 * The wMaxPacketSize and bInterval values will be initialized from
143 * module parameters.
144 */
145};
146
147static 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 /*
154 * The wMaxPacketSize and bInterval values will be initialized from
155 * module parameters.
156 */
157};
158
159static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
160 .bLength = USB_DT_ENDPOINT_SIZE,
161 .bDescriptorType = USB_DT_ENDPOINT,
162
163 .bEndpointAddress = USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
165 | USB_ENDPOINT_XFER_ISOC,
166 /*
167 * The wMaxPacketSize and bInterval values will be initialized from
168 * module parameters.
169 */
170};
171
172static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
173 .bLength = sizeof(uvc_ss_streaming_comp),
174 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
175 /*
176 * The bMaxBurst, bmAttributes and wBytesPerInterval values will be
177 * initialized from module parameters.
178 */
179};
180
181static 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,
184 NULL,
185};
186
187static 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,
190 NULL,
191};
192
193static 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,
197 NULL,
198};
199
200/* --------------------------------------------------------------------------
201 * Control requests
202 */
203
204static void
205uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
206{
207 struct uvc_device *uvc = req->context;
208 struct v4l2_event v4l2_event;
209 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
210
211 if (uvc->event_setup_out) {
212 uvc->event_setup_out = 0;
213
214 memset(&v4l2_event, 0, sizeof(v4l2_event));
215 v4l2_event.type = UVC_EVENT_DATA;
216 uvc_event->data.length = min_t(unsigned int, req->actual,
217 sizeof(uvc_event->data.data));
218 memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length);
219 v4l2_event_queue(&uvc->vdev, &v4l2_event);
220 }
221}
222
223static int
224uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
225{
226 struct uvc_device *uvc = to_uvc(f);
227 struct v4l2_event v4l2_event;
228 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
229 unsigned int interface = le16_to_cpu(ctrl->wIndex) & 0xff;
230 struct usb_ctrlrequest *mctrl;
231
232 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
233 uvcg_info(f, "invalid request type\n");
234 return -EINVAL;
235 }
236
237 /* Stall too big requests. */
238 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
239 return -EINVAL;
240
241 /*
242 * Tell the complete callback to generate an event for the next request
243 * that will be enqueued by UVCIOC_SEND_RESPONSE.
244 */
245 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
246 uvc->event_length = le16_to_cpu(ctrl->wLength);
247
248 memset(&v4l2_event, 0, sizeof(v4l2_event));
249 v4l2_event.type = UVC_EVENT_SETUP;
250 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
251
252 /* check for the interface number, fixup the interface number in
253 * the ctrl request so the userspace doesn't have to bother with
254 * offset and configfs parsing
255 */
256 mctrl = &uvc_event->req;
257 mctrl->wIndex &= ~cpu_to_le16(0xff);
258 if (interface == uvc->streaming_intf)
259 mctrl->wIndex = cpu_to_le16(UVC_STRING_STREAMING_IDX);
260
261 v4l2_event_queue(&uvc->vdev, &v4l2_event);
262
263 return 0;
264}
265
266void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
267{
268 struct usb_composite_dev *cdev = uvc->func.config->cdev;
269
270 if (disable_ep && uvc->video.ep)
271 usb_ep_disable(uvc->video.ep);
272
273 usb_composite_setup_continue(cdev);
274}
275
276static int
277uvc_function_get_alt(struct usb_function *f, unsigned interface)
278{
279 struct uvc_device *uvc = to_uvc(f);
280
281 uvcg_info(f, "%s(%u)\n", __func__, interface);
282
283 if (interface == uvc->control_intf)
284 return 0;
285 else if (interface != uvc->streaming_intf)
286 return -EINVAL;
287 else
288 return uvc->video.ep->enabled ? 1 : 0;
289}
290
291static int
292uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
293{
294 struct uvc_device *uvc = to_uvc(f);
295 struct usb_composite_dev *cdev = f->config->cdev;
296 struct v4l2_event v4l2_event;
297 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
298 int ret;
299
300 uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
301
302 if (interface == uvc->control_intf) {
303 if (alt)
304 return -EINVAL;
305
306 if (uvc->enable_interrupt_ep) {
307 uvcg_info(f, "reset UVC interrupt endpoint\n");
308 usb_ep_disable(uvc->interrupt_ep);
309
310 if (!uvc->interrupt_ep->desc)
311 if (config_ep_by_speed(cdev->gadget, f,
312 uvc->interrupt_ep))
313 return -EINVAL;
314
315 usb_ep_enable(uvc->interrupt_ep);
316 }
317
318 if (uvc->state == UVC_STATE_DISCONNECTED) {
319 memset(&v4l2_event, 0, sizeof(v4l2_event));
320 v4l2_event.type = UVC_EVENT_CONNECT;
321 uvc_event->speed = cdev->gadget->speed;
322 v4l2_event_queue(&uvc->vdev, &v4l2_event);
323
324 uvc->state = UVC_STATE_CONNECTED;
325 }
326
327 return 0;
328 }
329
330 if (interface != uvc->streaming_intf)
331 return -EINVAL;
332
333 /* TODO
334 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
335 return alt ? -EINVAL : 0;
336 */
337
338 switch (alt) {
339 case 0:
340 if (uvc->state != UVC_STATE_STREAMING)
341 return 0;
342
343 memset(&v4l2_event, 0, sizeof(v4l2_event));
344 v4l2_event.type = UVC_EVENT_STREAMOFF;
345 v4l2_event_queue(&uvc->vdev, &v4l2_event);
346
347 return USB_GADGET_DELAYED_STATUS;
348
349 case 1:
350 if (uvc->state != UVC_STATE_CONNECTED)
351 return 0;
352
353 if (!uvc->video.ep)
354 return -EINVAL;
355
356 uvcg_info(f, "reset UVC\n");
357 usb_ep_disable(uvc->video.ep);
358
359 ret = config_ep_by_speed(f->config->cdev->gadget,
360 &(uvc->func), uvc->video.ep);
361 if (ret)
362 return ret;
363 usb_ep_enable(uvc->video.ep);
364
365 memset(&v4l2_event, 0, sizeof(v4l2_event));
366 v4l2_event.type = UVC_EVENT_STREAMON;
367 v4l2_event_queue(&uvc->vdev, &v4l2_event);
368 return USB_GADGET_DELAYED_STATUS;
369
370 default:
371 return -EINVAL;
372 }
373}
374
375static void
376uvc_function_disable(struct usb_function *f)
377{
378 struct uvc_device *uvc = to_uvc(f);
379 struct v4l2_event v4l2_event;
380
381 uvcg_info(f, "%s()\n", __func__);
382
383 memset(&v4l2_event, 0, sizeof(v4l2_event));
384 v4l2_event.type = UVC_EVENT_DISCONNECT;
385 v4l2_event_queue(&uvc->vdev, &v4l2_event);
386
387 uvc->state = UVC_STATE_DISCONNECTED;
388
389 usb_ep_disable(uvc->video.ep);
390 if (uvc->enable_interrupt_ep)
391 usb_ep_disable(uvc->interrupt_ep);
392}
393
394/* --------------------------------------------------------------------------
395 * Connection / disconnection
396 */
397
398void
399uvc_function_connect(struct uvc_device *uvc)
400{
401 int ret;
402
403 if ((ret = usb_function_activate(&uvc->func)) < 0)
404 uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);
405}
406
407void
408uvc_function_disconnect(struct uvc_device *uvc)
409{
410 int ret;
411
412 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
413 uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);
414}
415
416/* --------------------------------------------------------------------------
417 * USB probe and disconnect
418 */
419
420static ssize_t function_name_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423 struct uvc_device *uvc = dev_get_drvdata(dev);
424
425 return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);
426}
427
428static DEVICE_ATTR_RO(function_name);
429
430static int
431uvc_register_video(struct uvc_device *uvc)
432{
433 struct usb_composite_dev *cdev = uvc->func.config->cdev;
434 int ret;
435
436 /* TODO reference counting. */
437 memset(&uvc->vdev, 0, sizeof(uvc->vdev));
438 uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
439 uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev;
440 uvc->vdev.fops = &uvc_v4l2_fops;
441 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
442 uvc->vdev.release = video_device_release_empty;
443 uvc->vdev.vfl_dir = VFL_DIR_TX;
444 uvc->vdev.lock = &uvc->video.mutex;
445 uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
446 strscpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
447
448 video_set_drvdata(&uvc->vdev, uvc);
449
450 ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1);
451 if (ret < 0)
452 return ret;
453
454 ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
455 if (ret < 0) {
456 video_unregister_device(&uvc->vdev);
457 return ret;
458 }
459
460 return 0;
461}
462
463#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
464 do { \
465 memcpy(mem, desc, (desc)->bLength); \
466 *(dst)++ = mem; \
467 mem += (desc)->bLength; \
468 } while (0);
469
470#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
471 do { \
472 const struct usb_descriptor_header * const *__src; \
473 for (__src = src; *__src; ++__src) { \
474 memcpy(mem, *__src, (*__src)->bLength); \
475 *dst++ = mem; \
476 mem += (*__src)->bLength; \
477 } \
478 } while (0)
479
480#define UVC_COPY_XU_DESCRIPTOR(mem, dst, desc) \
481 do { \
482 *(dst)++ = mem; \
483 memcpy(mem, desc, 22); /* bLength to bNrInPins */ \
484 mem += 22; \
485 \
486 memcpy(mem, (desc)->baSourceID, (desc)->bNrInPins); \
487 mem += (desc)->bNrInPins; \
488 \
489 memcpy(mem, &(desc)->bControlSize, 1); \
490 mem++; \
491 \
492 memcpy(mem, (desc)->bmControls, (desc)->bControlSize); \
493 mem += (desc)->bControlSize; \
494 \
495 memcpy(mem, &(desc)->iExtension, 1); \
496 mem++; \
497 } while (0)
498
499static struct usb_descriptor_header **
500uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
501{
502 struct uvc_input_header_descriptor *uvc_streaming_header;
503 struct uvc_header_descriptor *uvc_control_header;
504 const struct uvc_descriptor_header * const *uvc_control_desc;
505 const struct uvc_descriptor_header * const *uvc_streaming_cls;
506 const struct usb_descriptor_header * const *uvc_streaming_std;
507 const struct usb_descriptor_header * const *src;
508 struct usb_descriptor_header **dst;
509 struct usb_descriptor_header **hdr;
510 struct uvcg_extension *xu;
511 unsigned int control_size;
512 unsigned int streaming_size;
513 unsigned int n_desc;
514 unsigned int bytes;
515 void *mem;
516
517 switch (speed) {
518 case USB_SPEED_SUPER_PLUS:
519 case USB_SPEED_SUPER:
520 uvc_control_desc = uvc->desc.ss_control;
521 uvc_streaming_cls = uvc->desc.ss_streaming;
522 uvc_streaming_std = uvc_ss_streaming;
523 break;
524
525 case USB_SPEED_HIGH:
526 uvc_control_desc = uvc->desc.fs_control;
527 uvc_streaming_cls = uvc->desc.hs_streaming;
528 uvc_streaming_std = uvc_hs_streaming;
529 break;
530
531 case USB_SPEED_FULL:
532 default:
533 uvc_control_desc = uvc->desc.fs_control;
534 uvc_streaming_cls = uvc->desc.fs_streaming;
535 uvc_streaming_std = uvc_fs_streaming;
536 break;
537 }
538
539 if (!uvc_control_desc || !uvc_streaming_cls)
540 return ERR_PTR(-ENODEV);
541
542 /*
543 * Descriptors layout
544 *
545 * uvc_iad
546 * uvc_control_intf
547 * Class-specific UVC control descriptors
548 * uvc_interrupt_ep
549 * uvc_interrupt_cs_ep
550 * uvc_ss_interrupt_comp (for SS only)
551 * uvc_streaming_intf_alt0
552 * Class-specific UVC streaming descriptors
553 * uvc_{fs|hs}_streaming
554 */
555
556 /* Count descriptors and compute their size. */
557 control_size = 0;
558 streaming_size = 0;
559 bytes = uvc_iad.bLength + uvc_control_intf.bLength
560 + uvc_streaming_intf_alt0.bLength;
561
562 n_desc = 3;
563 if (uvc->enable_interrupt_ep) {
564 bytes += uvc_interrupt_ep.bLength + uvc_interrupt_cs_ep.bLength;
565 n_desc += 2;
566
567 if (speed == USB_SPEED_SUPER ||
568 speed == USB_SPEED_SUPER_PLUS) {
569 bytes += uvc_ss_interrupt_comp.bLength;
570 n_desc += 1;
571 }
572 }
573
574 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
575 *src; ++src) {
576 control_size += (*src)->bLength;
577 bytes += (*src)->bLength;
578 n_desc++;
579 }
580
581 list_for_each_entry(xu, uvc->desc.extension_units, list) {
582 control_size += xu->desc.bLength;
583 bytes += xu->desc.bLength;
584 n_desc++;
585 }
586
587 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
588 *src; ++src) {
589 streaming_size += (*src)->bLength;
590 bytes += (*src)->bLength;
591 n_desc++;
592 }
593 for (src = uvc_streaming_std; *src; ++src) {
594 bytes += (*src)->bLength;
595 n_desc++;
596 }
597
598 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
599 if (mem == NULL)
600 return NULL;
601
602 hdr = mem;
603 dst = mem;
604 mem += (n_desc + 1) * sizeof(*src);
605
606 /* Copy the descriptors. */
607 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
608 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
609
610 uvc_control_header = mem;
611 UVC_COPY_DESCRIPTORS(mem, dst,
612 (const struct usb_descriptor_header **)uvc_control_desc);
613
614 list_for_each_entry(xu, uvc->desc.extension_units, list)
615 UVC_COPY_XU_DESCRIPTOR(mem, dst, &xu->desc);
616
617 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
618 uvc_control_header->bInCollection = 1;
619 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
620
621 if (uvc->enable_interrupt_ep) {
622 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_interrupt_ep);
623 if (speed == USB_SPEED_SUPER ||
624 speed == USB_SPEED_SUPER_PLUS)
625 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_interrupt_comp);
626
627 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_interrupt_cs_ep);
628 }
629
630 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
631
632 uvc_streaming_header = mem;
633 UVC_COPY_DESCRIPTORS(mem, dst,
634 (const struct usb_descriptor_header**)uvc_streaming_cls);
635 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
636 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
637
638 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
639
640 *dst = NULL;
641 return hdr;
642}
643
644static int
645uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
646{
647 struct usb_composite_dev *cdev = c->cdev;
648 struct uvc_device *uvc = to_uvc(f);
649 struct uvcg_extension *xu;
650 struct usb_string *us;
651 unsigned int max_packet_mult;
652 unsigned int max_packet_size;
653 struct usb_ep *ep;
654 struct f_uvc_opts *opts;
655 int ret = -EINVAL;
656
657 uvcg_info(f, "%s()\n", __func__);
658
659 opts = fi_to_f_uvc_opts(f->fi);
660 /* Sanity check the streaming endpoint module parameters. */
661 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
662 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
663 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
664
665 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
666 if (opts->streaming_maxburst &&
667 (opts->streaming_maxpacket % 1024) != 0) {
668 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
669 uvcg_info(f, "overriding streaming_maxpacket to %d\n",
670 opts->streaming_maxpacket);
671 }
672
673 /*
674 * Fill in the FS/HS/SS Video Streaming specific descriptors from the
675 * module parameters.
676 *
677 * NOTE: We assume that the user knows what they are doing and won't
678 * give parameters that their UDC doesn't support.
679 */
680 if (opts->streaming_maxpacket <= 1024) {
681 max_packet_mult = 1;
682 max_packet_size = opts->streaming_maxpacket;
683 } else if (opts->streaming_maxpacket <= 2048) {
684 max_packet_mult = 2;
685 max_packet_size = opts->streaming_maxpacket / 2;
686 } else {
687 max_packet_mult = 3;
688 max_packet_size = opts->streaming_maxpacket / 3;
689 }
690
691 uvc_fs_streaming_ep.wMaxPacketSize =
692 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
693 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
694
695 uvc_hs_streaming_ep.wMaxPacketSize =
696 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
697
698 /* A high-bandwidth endpoint must specify a bInterval value of 1 */
699 if (max_packet_mult > 1)
700 uvc_hs_streaming_ep.bInterval = 1;
701 else
702 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
703
704 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
705 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
706 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
707 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
708 uvc_ss_streaming_comp.wBytesPerInterval =
709 cpu_to_le16(max_packet_size * max_packet_mult *
710 (opts->streaming_maxburst + 1));
711
712 /* Allocate endpoints. */
713 if (opts->enable_interrupt_ep) {
714 ep = usb_ep_autoconfig(cdev->gadget, &uvc_interrupt_ep);
715 if (!ep) {
716 uvcg_info(f, "Unable to allocate interrupt EP\n");
717 goto error;
718 }
719 uvc->interrupt_ep = ep;
720 uvc_control_intf.bNumEndpoints = 1;
721 }
722 uvc->enable_interrupt_ep = opts->enable_interrupt_ep;
723
724 /*
725 * gadget_is_{super|dual}speed() API check UDC controller capitblity. It should pass down
726 * highest speed endpoint descriptor to UDC controller. So UDC controller driver can reserve
727 * enough resource at check_config(), especially mult and maxburst. So UDC driver (such as
728 * cdns3) can know need at least (mult + 1) * (maxburst + 1) * wMaxPacketSize internal
729 * memory for this uvc functions. This is the only straightforward method to resolve the UDC
730 * resource allocation issue in the current gadget framework.
731 */
732 if (gadget_is_superspeed(c->cdev->gadget))
733 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
734 &uvc_ss_streaming_comp);
735 else if (gadget_is_dualspeed(cdev->gadget))
736 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
737 else
738 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
739
740 if (!ep) {
741 uvcg_info(f, "Unable to allocate streaming EP\n");
742 goto error;
743 }
744 uvc->video.ep = ep;
745
746 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
747 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
748 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
749
750 /*
751 * XUs can have an arbitrary string descriptor describing them. If they
752 * have one pick up the ID.
753 */
754 list_for_each_entry(xu, &opts->extension_units, list)
755 if (xu->string_descriptor_index)
756 xu->desc.iExtension = cdev->usb_strings[xu->string_descriptor_index].id;
757
758 /*
759 * We attach the hard-coded defaults incase the user does not provide
760 * any more appropriate strings through configfs.
761 */
762 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name;
763 us = usb_gstrings_attach(cdev, uvc_function_strings,
764 ARRAY_SIZE(uvc_en_us_strings));
765 if (IS_ERR(us)) {
766 ret = PTR_ERR(us);
767 goto error;
768 }
769
770 uvc_iad.iFunction = opts->iad_index ? cdev->usb_strings[opts->iad_index].id :
771 us[UVC_STRING_CONTROL_IDX].id;
772 uvc_streaming_intf_alt0.iInterface = opts->vs0_index ?
773 cdev->usb_strings[opts->vs0_index].id :
774 us[UVC_STRING_STREAMING_IDX].id;
775 uvc_streaming_intf_alt1.iInterface = opts->vs1_index ?
776 cdev->usb_strings[opts->vs1_index].id :
777 us[UVC_STRING_STREAMING_IDX].id;
778
779 /* Allocate interface IDs. */
780 if ((ret = usb_interface_id(c, f)) < 0)
781 goto error;
782 uvc_iad.bFirstInterface = ret;
783 uvc_control_intf.bInterfaceNumber = ret;
784 uvc->control_intf = ret;
785 opts->control_interface = ret;
786
787 if ((ret = usb_interface_id(c, f)) < 0)
788 goto error;
789 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
790 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
791 uvc->streaming_intf = ret;
792 opts->streaming_interface = ret;
793
794 /* Copy descriptors */
795 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
796 if (IS_ERR(f->fs_descriptors)) {
797 ret = PTR_ERR(f->fs_descriptors);
798 f->fs_descriptors = NULL;
799 goto error;
800 }
801
802 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
803 if (IS_ERR(f->hs_descriptors)) {
804 ret = PTR_ERR(f->hs_descriptors);
805 f->hs_descriptors = NULL;
806 goto error;
807 }
808
809 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
810 if (IS_ERR(f->ss_descriptors)) {
811 ret = PTR_ERR(f->ss_descriptors);
812 f->ss_descriptors = NULL;
813 goto error;
814 }
815
816 f->ssp_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER_PLUS);
817 if (IS_ERR(f->ssp_descriptors)) {
818 ret = PTR_ERR(f->ssp_descriptors);
819 f->ssp_descriptors = NULL;
820 goto error;
821 }
822
823 /* Preallocate control endpoint request. */
824 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
825 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
826 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
827 ret = -ENOMEM;
828 goto error;
829 }
830
831 uvc->control_req->buf = uvc->control_buf;
832 uvc->control_req->complete = uvc_function_ep0_complete;
833 uvc->control_req->context = uvc;
834
835 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
836 uvcg_err(f, "failed to register V4L2 device\n");
837 goto error;
838 }
839
840 /* Initialise video. */
841 ret = uvcg_video_init(&uvc->video, uvc);
842 if (ret < 0)
843 goto v4l2_error;
844
845 /* Register a V4L2 device. */
846 ret = uvc_register_video(uvc);
847 if (ret < 0) {
848 uvcg_err(f, "failed to register video device\n");
849 goto v4l2_error;
850 }
851
852 return 0;
853
854v4l2_error:
855 v4l2_device_unregister(&uvc->v4l2_dev);
856error:
857 if (uvc->control_req)
858 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
859 kfree(uvc->control_buf);
860
861 usb_free_all_descriptors(f);
862 return ret;
863}
864
865/* --------------------------------------------------------------------------
866 * USB gadget function
867 */
868
869static void uvc_free_inst(struct usb_function_instance *f)
870{
871 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
872
873 mutex_destroy(&opts->lock);
874 kfree(opts);
875}
876
877static struct usb_function_instance *uvc_alloc_inst(void)
878{
879 struct f_uvc_opts *opts;
880 struct uvc_camera_terminal_descriptor *cd;
881 struct uvc_processing_unit_descriptor *pd;
882 struct uvc_output_terminal_descriptor *od;
883 struct uvc_descriptor_header **ctl_cls;
884 int ret;
885
886 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
887 if (!opts)
888 return ERR_PTR(-ENOMEM);
889 opts->func_inst.free_func_inst = uvc_free_inst;
890 mutex_init(&opts->lock);
891
892 cd = &opts->uvc_camera_terminal;
893 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
894 cd->bDescriptorType = USB_DT_CS_INTERFACE;
895 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
896 cd->bTerminalID = 1;
897 cd->wTerminalType = cpu_to_le16(0x0201);
898 cd->bAssocTerminal = 0;
899 cd->iTerminal = 0;
900 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
901 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
902 cd->wOcularFocalLength = cpu_to_le16(0);
903 cd->bControlSize = 3;
904 cd->bmControls[0] = 2;
905 cd->bmControls[1] = 0;
906 cd->bmControls[2] = 0;
907
908 pd = &opts->uvc_processing;
909 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
910 pd->bDescriptorType = USB_DT_CS_INTERFACE;
911 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
912 pd->bUnitID = 2;
913 pd->bSourceID = 1;
914 pd->wMaxMultiplier = cpu_to_le16(16*1024);
915 pd->bControlSize = 2;
916 pd->bmControls[0] = 1;
917 pd->bmControls[1] = 0;
918 pd->iProcessing = 0;
919 pd->bmVideoStandards = 0;
920
921 od = &opts->uvc_output_terminal;
922 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
923 od->bDescriptorType = USB_DT_CS_INTERFACE;
924 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
925 od->bTerminalID = 3;
926 od->wTerminalType = cpu_to_le16(0x0101);
927 od->bAssocTerminal = 0;
928 od->bSourceID = 2;
929 od->iTerminal = 0;
930
931 /*
932 * With the ability to add XUs to the UVC function graph, we need to be
933 * able to allocate unique unit IDs to them. The IDs are 1-based, with
934 * the CT, PU and OT above consuming the first 3.
935 */
936 opts->last_unit_id = 3;
937
938 /* Prepare fs control class descriptors for configfs-based gadgets */
939 ctl_cls = opts->uvc_fs_control_cls;
940 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
941 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
942 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
943 ctl_cls[3] = (struct uvc_descriptor_header *)od;
944 ctl_cls[4] = NULL; /* NULL-terminate */
945 opts->fs_control =
946 (const struct uvc_descriptor_header * const *)ctl_cls;
947
948 /* Prepare hs control class descriptors for configfs-based gadgets */
949 ctl_cls = opts->uvc_ss_control_cls;
950 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
951 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
952 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
953 ctl_cls[3] = (struct uvc_descriptor_header *)od;
954 ctl_cls[4] = NULL; /* NULL-terminate */
955 opts->ss_control =
956 (const struct uvc_descriptor_header * const *)ctl_cls;
957
958 INIT_LIST_HEAD(&opts->extension_units);
959
960 opts->streaming_interval = 1;
961 opts->streaming_maxpacket = 1024;
962 snprintf(opts->function_name, sizeof(opts->function_name), "UVC Camera");
963
964 ret = uvcg_attach_configfs(opts);
965 if (ret < 0) {
966 kfree(opts);
967 return ERR_PTR(ret);
968 }
969
970 return &opts->func_inst;
971}
972
973static void uvc_free(struct usb_function *f)
974{
975 struct uvc_device *uvc = to_uvc(f);
976 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
977 func_inst);
978 if (!opts->header)
979 config_item_put(&uvc->header->item);
980 --opts->refcnt;
981 kfree(uvc);
982}
983
984static void uvc_function_unbind(struct usb_configuration *c,
985 struct usb_function *f)
986{
987 struct usb_composite_dev *cdev = c->cdev;
988 struct uvc_device *uvc = to_uvc(f);
989 struct uvc_video *video = &uvc->video;
990 long wait_ret = 1;
991
992 uvcg_info(f, "%s()\n", __func__);
993
994 if (video->async_wq)
995 destroy_workqueue(video->async_wq);
996
997 /*
998 * If we know we're connected via v4l2, then there should be a cleanup
999 * of the device from userspace either via UVC_EVENT_DISCONNECT or
1000 * though the video device removal uevent. Allow some time for the
1001 * application to close out before things get deleted.
1002 */
1003 if (uvc->func_connected) {
1004 uvcg_dbg(f, "waiting for clean disconnect\n");
1005 wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1006 uvc->func_connected == false, msecs_to_jiffies(500));
1007 uvcg_dbg(f, "done waiting with ret: %ld\n", wait_ret);
1008 }
1009
1010 device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
1011 video_unregister_device(&uvc->vdev);
1012 v4l2_device_unregister(&uvc->v4l2_dev);
1013
1014 if (uvc->func_connected) {
1015 /*
1016 * Wait for the release to occur to ensure there are no longer any
1017 * pending operations that may cause panics when resources are cleaned
1018 * up.
1019 */
1020 uvcg_warn(f, "%s no clean disconnect, wait for release\n", __func__);
1021 wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1022 uvc->func_connected == false, msecs_to_jiffies(1000));
1023 uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret);
1024 }
1025
1026 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
1027 kfree(uvc->control_buf);
1028
1029 usb_free_all_descriptors(f);
1030}
1031
1032static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
1033{
1034 struct uvc_device *uvc;
1035 struct f_uvc_opts *opts;
1036 struct uvc_descriptor_header **strm_cls;
1037 struct config_item *streaming, *header, *h;
1038
1039 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
1040 if (uvc == NULL)
1041 return ERR_PTR(-ENOMEM);
1042
1043 mutex_init(&uvc->video.mutex);
1044 uvc->state = UVC_STATE_DISCONNECTED;
1045 init_waitqueue_head(&uvc->func_connected_queue);
1046 opts = fi_to_f_uvc_opts(fi);
1047
1048 mutex_lock(&opts->lock);
1049 if (opts->uvc_fs_streaming_cls) {
1050 strm_cls = opts->uvc_fs_streaming_cls;
1051 opts->fs_streaming =
1052 (const struct uvc_descriptor_header * const *)strm_cls;
1053 }
1054 if (opts->uvc_hs_streaming_cls) {
1055 strm_cls = opts->uvc_hs_streaming_cls;
1056 opts->hs_streaming =
1057 (const struct uvc_descriptor_header * const *)strm_cls;
1058 }
1059 if (opts->uvc_ss_streaming_cls) {
1060 strm_cls = opts->uvc_ss_streaming_cls;
1061 opts->ss_streaming =
1062 (const struct uvc_descriptor_header * const *)strm_cls;
1063 }
1064
1065 uvc->desc.fs_control = opts->fs_control;
1066 uvc->desc.ss_control = opts->ss_control;
1067 uvc->desc.fs_streaming = opts->fs_streaming;
1068 uvc->desc.hs_streaming = opts->hs_streaming;
1069 uvc->desc.ss_streaming = opts->ss_streaming;
1070
1071 if (opts->header) {
1072 uvc->header = opts->header;
1073 } else {
1074 streaming = config_group_find_item(&opts->func_inst.group, "streaming");
1075 if (!streaming)
1076 goto err_config;
1077
1078 header = config_group_find_item(to_config_group(streaming), "header");
1079 config_item_put(streaming);
1080 if (!header)
1081 goto err_config;
1082
1083 h = config_group_find_item(to_config_group(header), "h");
1084 config_item_put(header);
1085 if (!h)
1086 goto err_config;
1087
1088 uvc->header = to_uvcg_streaming_header(h);
1089 if (!uvc->header->linked) {
1090 mutex_unlock(&opts->lock);
1091 kfree(uvc);
1092 return ERR_PTR(-EBUSY);
1093 }
1094 }
1095
1096 uvc->desc.extension_units = &opts->extension_units;
1097
1098 ++opts->refcnt;
1099 mutex_unlock(&opts->lock);
1100
1101 /* Register the function. */
1102 uvc->func.name = "uvc";
1103 uvc->func.bind = uvc_function_bind;
1104 uvc->func.unbind = uvc_function_unbind;
1105 uvc->func.get_alt = uvc_function_get_alt;
1106 uvc->func.set_alt = uvc_function_set_alt;
1107 uvc->func.disable = uvc_function_disable;
1108 uvc->func.setup = uvc_function_setup;
1109 uvc->func.free_func = uvc_free;
1110 uvc->func.bind_deactivated = true;
1111
1112 return &uvc->func;
1113
1114err_config:
1115 mutex_unlock(&opts->lock);
1116 kfree(uvc);
1117 return ERR_PTR(-ENOENT);
1118}
1119
1120DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
1121MODULE_LICENSE("GPL");
1122MODULE_AUTHOR("Laurent Pinchart");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * uvc_gadget.c -- USB Video Class Gadget driver
4 *
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
8
9#include <linux/device.h>
10#include <linux/errno.h>
11#include <linux/fs.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>
23
24#include <media/v4l2-dev.h>
25#include <media/v4l2-event.h>
26
27#include "u_uvc.h"
28#include "uvc.h"
29#include "uvc_configfs.h"
30#include "uvc_v4l2.h"
31#include "uvc_video.h"
32
33unsigned int uvc_gadget_trace_param;
34module_param_named(trace, uvc_gadget_trace_param, uint, 0644);
35MODULE_PARM_DESC(trace, "Trace level bitmask");
36
37/* --------------------------------------------------------------------------
38 * Function descriptors
39 */
40
41/* string IDs are assigned dynamically */
42
43#define UVC_STRING_CONTROL_IDX 0
44#define UVC_STRING_STREAMING_IDX 1
45
46static struct usb_string uvc_en_us_strings[] = {
47 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
48 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
49 { }
50};
51
52static struct usb_gadget_strings uvc_stringtab = {
53 .language = 0x0409, /* en-us */
54 .strings = uvc_en_us_strings,
55};
56
57static struct usb_gadget_strings *uvc_function_strings[] = {
58 &uvc_stringtab,
59 NULL,
60};
61
62#define UVC_INTF_VIDEO_CONTROL 0
63#define UVC_INTF_VIDEO_STREAMING 1
64
65#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
66
67static struct usb_interface_assoc_descriptor uvc_iad = {
68 .bLength = sizeof(uvc_iad),
69 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
70 .bFirstInterface = 0,
71 .bInterfaceCount = 2,
72 .bFunctionClass = USB_CLASS_VIDEO,
73 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
74 .bFunctionProtocol = 0x00,
75 .iFunction = 0,
76};
77
78static 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,
83 .bNumEndpoints = 1,
84 .bInterfaceClass = USB_CLASS_VIDEO,
85 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
86 .bInterfaceProtocol = 0x00,
87 .iInterface = 0,
88};
89
90static 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),
96 .bInterval = 8,
97};
98
99static 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. */
103 .bMaxBurst = 0,
104 .bmAttributes = 0,
105 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
106};
107
108static 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),
113};
114
115static 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,
120 .bNumEndpoints = 0,
121 .bInterfaceClass = USB_CLASS_VIDEO,
122 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
123 .bInterfaceProtocol = 0x00,
124 .iInterface = 0,
125};
126
127static 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,
132 .bNumEndpoints = 1,
133 .bInterfaceClass = USB_CLASS_VIDEO,
134 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
135 .bInterfaceProtocol = 0x00,
136 .iInterface = 0,
137};
138
139static 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
146 * module parameters.
147 */
148};
149
150static 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
157 * module parameters.
158 */
159};
160
161static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
162 .bLength = USB_DT_ENDPOINT_SIZE,
163 .bDescriptorType = USB_DT_ENDPOINT,
164
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
169 * module parameters.
170 */
171};
172
173static 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.
178 */
179};
180
181static 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,
184 NULL,
185};
186
187static 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,
190 NULL,
191};
192
193static 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,
197 NULL,
198};
199
200/* --------------------------------------------------------------------------
201 * Control requests
202 */
203
204static void
205uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
206{
207 struct uvc_device *uvc = req->context;
208 struct v4l2_event v4l2_event;
209 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
210
211 if (uvc->event_setup_out) {
212 uvc->event_setup_out = 0;
213
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);
219 }
220}
221
222static int
223uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
224{
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;
228
229 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
230 uvcg_info(f, "invalid request type\n");
231 return -EINVAL;
232 }
233
234 /* Stall too big requests. */
235 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
236 return -EINVAL;
237
238 /* Tell the complete callback to generate an event for the next request
239 * that will be enqueued by UVCIOC_SEND_RESPONSE.
240 */
241 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
242 uvc->event_length = le16_to_cpu(ctrl->wLength);
243
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);
248
249 return 0;
250}
251
252void uvc_function_setup_continue(struct uvc_device *uvc)
253{
254 struct usb_composite_dev *cdev = uvc->func.config->cdev;
255
256 usb_composite_setup_continue(cdev);
257}
258
259static int
260uvc_function_get_alt(struct usb_function *f, unsigned interface)
261{
262 struct uvc_device *uvc = to_uvc(f);
263
264 uvcg_info(f, "%s(%u)\n", __func__, interface);
265
266 if (interface == uvc->control_intf)
267 return 0;
268 else if (interface != uvc->streaming_intf)
269 return -EINVAL;
270 else
271 return uvc->video.ep->enabled ? 1 : 0;
272}
273
274static int
275uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
276{
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;
281 int ret;
282
283 uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
284
285 if (interface == uvc->control_intf) {
286 if (alt)
287 return -EINVAL;
288
289 uvcg_info(f, "reset UVC Control\n");
290 usb_ep_disable(uvc->control_ep);
291
292 if (!uvc->control_ep->desc)
293 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
294 return -EINVAL;
295
296 usb_ep_enable(uvc->control_ep);
297
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);
303
304 uvc->state = UVC_STATE_CONNECTED;
305 }
306
307 return 0;
308 }
309
310 if (interface != uvc->streaming_intf)
311 return -EINVAL;
312
313 /* TODO
314 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
315 return alt ? -EINVAL : 0;
316 */
317
318 switch (alt) {
319 case 0:
320 if (uvc->state != UVC_STATE_STREAMING)
321 return 0;
322
323 if (uvc->video.ep)
324 usb_ep_disable(uvc->video.ep);
325
326 memset(&v4l2_event, 0, sizeof(v4l2_event));
327 v4l2_event.type = UVC_EVENT_STREAMOFF;
328 v4l2_event_queue(&uvc->vdev, &v4l2_event);
329
330 uvc->state = UVC_STATE_CONNECTED;
331 return 0;
332
333 case 1:
334 if (uvc->state != UVC_STATE_CONNECTED)
335 return 0;
336
337 if (!uvc->video.ep)
338 return -EINVAL;
339
340 uvcg_info(f, "reset UVC\n");
341 usb_ep_disable(uvc->video.ep);
342
343 ret = config_ep_by_speed(f->config->cdev->gadget,
344 &(uvc->func), uvc->video.ep);
345 if (ret)
346 return ret;
347 usb_ep_enable(uvc->video.ep);
348
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;
353
354 default:
355 return -EINVAL;
356 }
357}
358
359static void
360uvc_function_disable(struct usb_function *f)
361{
362 struct uvc_device *uvc = to_uvc(f);
363 struct v4l2_event v4l2_event;
364
365 uvcg_info(f, "%s()\n", __func__);
366
367 memset(&v4l2_event, 0, sizeof(v4l2_event));
368 v4l2_event.type = UVC_EVENT_DISCONNECT;
369 v4l2_event_queue(&uvc->vdev, &v4l2_event);
370
371 uvc->state = UVC_STATE_DISCONNECTED;
372
373 usb_ep_disable(uvc->video.ep);
374 usb_ep_disable(uvc->control_ep);
375}
376
377/* --------------------------------------------------------------------------
378 * Connection / disconnection
379 */
380
381void
382uvc_function_connect(struct uvc_device *uvc)
383{
384 int ret;
385
386 if ((ret = usb_function_activate(&uvc->func)) < 0)
387 uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);
388}
389
390void
391uvc_function_disconnect(struct uvc_device *uvc)
392{
393 int ret;
394
395 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
396 uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);
397}
398
399/* --------------------------------------------------------------------------
400 * USB probe and disconnect
401 */
402
403static ssize_t function_name_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
406 struct uvc_device *uvc = dev_get_drvdata(dev);
407
408 return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);
409}
410
411static DEVICE_ATTR_RO(function_name);
412
413static int
414uvc_register_video(struct uvc_device *uvc)
415{
416 struct usb_composite_dev *cdev = uvc->func.config->cdev;
417 int ret;
418
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));
428
429 video_set_drvdata(&uvc->vdev, uvc);
430
431 ret = video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
432 if (ret < 0)
433 return ret;
434
435 ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
436 if (ret < 0) {
437 video_unregister_device(&uvc->vdev);
438 return ret;
439 }
440
441 return 0;
442}
443
444#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
445 do { \
446 memcpy(mem, desc, (desc)->bLength); \
447 *(dst)++ = mem; \
448 mem += (desc)->bLength; \
449 } while (0);
450
451#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
452 do { \
453 const struct usb_descriptor_header * const *__src; \
454 for (__src = src; *__src; ++__src) { \
455 memcpy(mem, *__src, (*__src)->bLength); \
456 *dst++ = mem; \
457 mem += (*__src)->bLength; \
458 } \
459 } while (0)
460
461static struct usb_descriptor_header **
462uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
463{
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;
474 unsigned int n_desc;
475 unsigned int bytes;
476 void *mem;
477
478 switch (speed) {
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;
483 break;
484
485 case USB_SPEED_HIGH:
486 uvc_control_desc = uvc->desc.fs_control;
487 uvc_streaming_cls = uvc->desc.hs_streaming;
488 uvc_streaming_std = uvc_hs_streaming;
489 break;
490
491 case USB_SPEED_FULL:
492 default:
493 uvc_control_desc = uvc->desc.fs_control;
494 uvc_streaming_cls = uvc->desc.fs_streaming;
495 uvc_streaming_std = uvc_fs_streaming;
496 break;
497 }
498
499 if (!uvc_control_desc || !uvc_streaming_cls)
500 return ERR_PTR(-ENODEV);
501
502 /* Descriptors layout
503 *
504 * uvc_iad
505 * uvc_control_intf
506 * Class-specific UVC control descriptors
507 * uvc_control_ep
508 * uvc_control_cs_ep
509 * uvc_ss_control_comp (for SS only)
510 * uvc_streaming_intf_alt0
511 * Class-specific UVC streaming descriptors
512 * uvc_{fs|hs}_streaming
513 */
514
515 /* Count descriptors and compute their size. */
516 control_size = 0;
517 streaming_size = 0;
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;
521
522 if (speed == USB_SPEED_SUPER) {
523 bytes += uvc_ss_control_comp.bLength;
524 n_desc = 6;
525 } else {
526 n_desc = 5;
527 }
528
529 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
530 *src; ++src) {
531 control_size += (*src)->bLength;
532 bytes += (*src)->bLength;
533 n_desc++;
534 }
535 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
536 *src; ++src) {
537 streaming_size += (*src)->bLength;
538 bytes += (*src)->bLength;
539 n_desc++;
540 }
541 for (src = uvc_streaming_std; *src; ++src) {
542 bytes += (*src)->bLength;
543 n_desc++;
544 }
545
546 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
547 if (mem == NULL)
548 return NULL;
549
550 hdr = mem;
551 dst = mem;
552 mem += (n_desc + 1) * sizeof(*src);
553
554 /* Copy the descriptors. */
555 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
556 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
557
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;
564
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);
568
569 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
570 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
571
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;
577
578 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
579
580 *dst = NULL;
581 return hdr;
582}
583
584static int
585uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
586{
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;
592 struct usb_ep *ep;
593 struct f_uvc_opts *opts;
594 int ret = -EINVAL;
595
596 uvcg_info(f, "%s()\n", __func__);
597
598 opts = fi_to_f_uvc_opts(f->fi);
599 /* Sanity check the streaming endpoint module parameters.
600 */
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);
604
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);
611 }
612
613 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
614 * module parameters.
615 *
616 * NOTE: We assume that the user knows what they are doing and won't
617 * give parameters that their UDC doesn't support.
618 */
619 if (opts->streaming_maxpacket <= 1024) {
620 max_packet_mult = 1;
621 max_packet_size = opts->streaming_maxpacket;
622 } else if (opts->streaming_maxpacket <= 2048) {
623 max_packet_mult = 2;
624 max_packet_size = opts->streaming_maxpacket / 2;
625 } else {
626 max_packet_mult = 3;
627 max_packet_size = opts->streaming_maxpacket / 3;
628 }
629
630 uvc_fs_streaming_ep.wMaxPacketSize =
631 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
632 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
633
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;
637
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));
645
646 /* Allocate endpoints. */
647 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
648 if (!ep) {
649 uvcg_info(f, "Unable to allocate control EP\n");
650 goto error;
651 }
652 uvc->control_ep = ep;
653
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);
659 else
660 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
661
662 if (!ep) {
663 uvcg_info(f, "Unable to allocate streaming EP\n");
664 goto error;
665 }
666 uvc->video.ep = ep;
667
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;
671
672 us = usb_gstrings_attach(cdev, uvc_function_strings,
673 ARRAY_SIZE(uvc_en_us_strings));
674 if (IS_ERR(us)) {
675 ret = PTR_ERR(us);
676 goto error;
677 }
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;
683
684 /* Allocate interface IDs. */
685 if ((ret = usb_interface_id(c, f)) < 0)
686 goto error;
687 uvc_iad.bFirstInterface = ret;
688 uvc_control_intf.bInterfaceNumber = ret;
689 uvc->control_intf = ret;
690 opts->control_interface = ret;
691
692 if ((ret = usb_interface_id(c, f)) < 0)
693 goto error;
694 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
695 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
696 uvc->streaming_intf = ret;
697 opts->streaming_interface = ret;
698
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;
704 goto error;
705 }
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;
711 goto error;
712 }
713 }
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;
719 goto error;
720 }
721 }
722
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) {
727 ret = -ENOMEM;
728 goto error;
729 }
730
731 uvc->control_req->buf = uvc->control_buf;
732 uvc->control_req->complete = uvc_function_ep0_complete;
733 uvc->control_req->context = uvc;
734
735 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
736 uvcg_err(f, "failed to register V4L2 device\n");
737 goto error;
738 }
739
740 /* Initialise video. */
741 ret = uvcg_video_init(&uvc->video, uvc);
742 if (ret < 0)
743 goto error;
744
745 /* Register a V4L2 device. */
746 ret = uvc_register_video(uvc);
747 if (ret < 0) {
748 uvcg_err(f, "failed to register video device\n");
749 goto error;
750 }
751
752 return 0;
753
754error:
755 v4l2_device_unregister(&uvc->v4l2_dev);
756
757 if (uvc->control_req)
758 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
759 kfree(uvc->control_buf);
760
761 usb_free_all_descriptors(f);
762 return ret;
763}
764
765/* --------------------------------------------------------------------------
766 * USB gadget function
767 */
768
769static void uvc_free_inst(struct usb_function_instance *f)
770{
771 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
772
773 mutex_destroy(&opts->lock);
774 kfree(opts);
775}
776
777static struct usb_function_instance *uvc_alloc_inst(void)
778{
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;
785 int ret;
786
787 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
788 if (!opts)
789 return ERR_PTR(-ENOMEM);
790 opts->func_inst.free_func_inst = uvc_free_inst;
791 mutex_init(&opts->lock);
792
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;
797 cd->bTerminalID = 1;
798 cd->wTerminalType = cpu_to_le16(0x0201);
799 cd->bAssocTerminal = 0;
800 cd->iTerminal = 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;
808
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;
813 pd->bUnitID = 2;
814 pd->bSourceID = 1;
815 pd->wMaxMultiplier = cpu_to_le16(16*1024);
816 pd->bControlSize = 2;
817 pd->bmControls[0] = 1;
818 pd->bmControls[1] = 0;
819 pd->iProcessing = 0;
820
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;
825 od->bTerminalID = 3;
826 od->wTerminalType = cpu_to_le16(0x0101);
827 od->bAssocTerminal = 0;
828 od->bSourceID = 2;
829 od->iTerminal = 0;
830
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;
838
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 */
846 opts->fs_control =
847 (const struct uvc_descriptor_header * const *)ctl_cls;
848
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 */
856 opts->ss_control =
857 (const struct uvc_descriptor_header * const *)ctl_cls;
858
859 opts->streaming_interval = 1;
860 opts->streaming_maxpacket = 1024;
861
862 ret = uvcg_attach_configfs(opts);
863 if (ret < 0) {
864 kfree(opts);
865 return ERR_PTR(ret);
866 }
867
868 return &opts->func_inst;
869}
870
871static void uvc_free(struct usb_function *f)
872{
873 struct uvc_device *uvc = to_uvc(f);
874 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
875 func_inst);
876 --opts->refcnt;
877 kfree(uvc);
878}
879
880static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
881{
882 struct usb_composite_dev *cdev = c->cdev;
883 struct uvc_device *uvc = to_uvc(f);
884
885 uvcg_info(f, "%s\n", __func__);
886
887 device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
888 video_unregister_device(&uvc->vdev);
889 v4l2_device_unregister(&uvc->v4l2_dev);
890
891 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
892 kfree(uvc->control_buf);
893
894 usb_free_all_descriptors(f);
895}
896
897static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
898{
899 struct uvc_device *uvc;
900 struct f_uvc_opts *opts;
901 struct uvc_descriptor_header **strm_cls;
902
903 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
904 if (uvc == NULL)
905 return ERR_PTR(-ENOMEM);
906
907 mutex_init(&uvc->video.mutex);
908 uvc->state = UVC_STATE_DISCONNECTED;
909 opts = fi_to_f_uvc_opts(fi);
910
911 mutex_lock(&opts->lock);
912 if (opts->uvc_fs_streaming_cls) {
913 strm_cls = opts->uvc_fs_streaming_cls;
914 opts->fs_streaming =
915 (const struct uvc_descriptor_header * const *)strm_cls;
916 }
917 if (opts->uvc_hs_streaming_cls) {
918 strm_cls = opts->uvc_hs_streaming_cls;
919 opts->hs_streaming =
920 (const struct uvc_descriptor_header * const *)strm_cls;
921 }
922 if (opts->uvc_ss_streaming_cls) {
923 strm_cls = opts->uvc_ss_streaming_cls;
924 opts->ss_streaming =
925 (const struct uvc_descriptor_header * const *)strm_cls;
926 }
927
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;
933 ++opts->refcnt;
934 mutex_unlock(&opts->lock);
935
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;
946
947 return &uvc->func;
948}
949
950DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
951MODULE_LICENSE("GPL");
952MODULE_AUTHOR("Laurent Pinchart");