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
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");
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
46 static struct usb_string uvc_en_us_strings[] = {
47 /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */
48 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
49 { }
50 };
51
52 static struct usb_gadget_strings uvc_stringtab = {
53 .language = 0x0409, /* en-us */
54 .strings = uvc_en_us_strings,
55 };
56
57 static 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
67 static 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
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,
83 .bNumEndpoints = 1,
84 .bInterfaceClass = USB_CLASS_VIDEO,
85 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
86 .bInterfaceProtocol = 0x00,
87 .iInterface = 0,
88 };
89
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),
96 .bInterval = 8,
97 };
98
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. */
103 .bMaxBurst = 0,
104 .bmAttributes = 0,
105 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
106 };
107
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),
113 };
114
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,
120 .bNumEndpoints = 0,
121 .bInterfaceClass = USB_CLASS_VIDEO,
122 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
123 .bInterfaceProtocol = 0x00,
124 .iInterface = 0,
125 };
126
127 static struct usb_interface_descriptor uvc_bulk_streaming_intf_alt0 = {
128 .bLength = USB_DT_INTERFACE_SIZE,
129 .bDescriptorType = USB_DT_INTERFACE,
130 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
131 .bAlternateSetting = 0,
132 .bNumEndpoints = 1,
133 .bInterfaceClass = USB_CLASS_VIDEO,
134 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
135 .bInterfaceProtocol = 0x00,
136 .iInterface = 0,
137 };
138
139 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
140 .bLength = USB_DT_INTERFACE_SIZE,
141 .bDescriptorType = USB_DT_INTERFACE,
142 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
143 .bAlternateSetting = 1,
144 .bNumEndpoints = 1,
145 .bInterfaceClass = USB_CLASS_VIDEO,
146 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
147 .bInterfaceProtocol = 0x00,
148 .iInterface = 0,
149 };
150
151 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
152 .bLength = USB_DT_ENDPOINT_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_IN,
155 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
156 | USB_ENDPOINT_XFER_ISOC,
157 /* The wMaxPacketSize and bInterval values will be initialized from
158 * module parameters.
159 */
160 };
161
162 static struct usb_endpoint_descriptor uvc_fs_bulk_streaming_ep = {
163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT,
165 .bEndpointAddress = USB_DIR_IN,
166 .bmAttributes = USB_ENDPOINT_XFER_BULK,
167 /* The wMaxPacketSize and bInterval values will be initialized from
168 * module parameters.
169 */
170 };
171
172 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
173 .bLength = USB_DT_ENDPOINT_SIZE,
174 .bDescriptorType = USB_DT_ENDPOINT,
175 .bEndpointAddress = USB_DIR_IN,
176 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
177 | USB_ENDPOINT_XFER_ISOC,
178 /* The wMaxPacketSize and bInterval values will be initialized from
179 * module parameters.
180 */
181 };
182
183 static struct usb_endpoint_descriptor uvc_hs_bulk_streaming_ep = {
184 .bLength = USB_DT_ENDPOINT_SIZE,
185 .bDescriptorType = USB_DT_ENDPOINT,
186 .bEndpointAddress = USB_DIR_IN,
187 .bmAttributes = USB_ENDPOINT_XFER_BULK,
188 /* The wMaxPacketSize and bInterval values will be initialized from
189 * module parameters.
190 */
191 };
192
193 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
194 .bLength = USB_DT_ENDPOINT_SIZE,
195 .bDescriptorType = USB_DT_ENDPOINT,
196
197 .bEndpointAddress = USB_DIR_IN,
198 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
199 | USB_ENDPOINT_XFER_ISOC,
200 /* The wMaxPacketSize and bInterval values will be initialized from
201 * module parameters.
202 */
203 };
204
205 static struct usb_endpoint_descriptor uvc_ss_bulk_streaming_ep = {
206 .bLength = USB_DT_ENDPOINT_SIZE,
207 .bDescriptorType = USB_DT_ENDPOINT,
208
209 .bEndpointAddress = USB_DIR_IN,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
211 /* The wMaxPacketSize and bInterval values will be initialized from
212 * module parameters.
213 */
214 };
215
216 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
217 .bLength = sizeof(uvc_ss_streaming_comp),
218 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
219 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
220 * initialized from module parameters.
221 */
222 };
223
224 static struct usb_ss_ep_comp_descriptor uvc_ss_bulk_streaming_comp = {
225 .bLength = sizeof(uvc_ss_bulk_streaming_comp),
226 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
227 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
228 * initialized from module parameters.
229 */
230 };
231
232 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
233 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
234 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
235 NULL,
236 };
237
238 static const struct usb_descriptor_header * const uvc_fs_bulk_streaming[] = {
239 (struct usb_descriptor_header *)&uvc_fs_bulk_streaming_ep,
240 NULL,
241 };
242
243 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
244 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
245 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
246 NULL,
247 };
248
249 static const struct usb_descriptor_header * const uvc_hs_bulk_streaming[] = {
250 (struct usb_descriptor_header *)&uvc_hs_bulk_streaming_ep,
251 NULL,
252 };
253
254 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
255 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
256 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
257 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
258 NULL,
259 };
260
261 static const struct usb_descriptor_header * const uvc_ss_bulk_streaming[] = {
262 (struct usb_descriptor_header *)&uvc_ss_bulk_streaming_ep,
263 (struct usb_descriptor_header *)&uvc_ss_bulk_streaming_comp,
264 NULL,
265 };
266
267 /* --------------------------------------------------------------------------
268 * Control requests
269 */
270
271 static void
uvc_function_ep0_complete(struct usb_ep * ep,struct usb_request * req)272 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
273 {
274 struct uvc_device *uvc = req->context;
275 struct v4l2_event v4l2_event;
276 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
277
278 uvc_trace(UVC_TRACE_CONTROL,
279 "event_setup_out %d, data len %d\n",
280 uvc->event_setup_out, req->actual);
281
282 if (uvc->event_setup_out) {
283 uvc->event_setup_out = 0;
284
285 memset(&v4l2_event, 0, sizeof(v4l2_event));
286 v4l2_event.type = UVC_EVENT_DATA;
287 uvc_event->data.length = min_t(unsigned int, req->actual,
288 sizeof(uvc_event->data.data));
289 memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length);
290 v4l2_event_queue(&uvc->vdev, &v4l2_event);
291 }
292 }
293
294 static int
uvc_function_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)295 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
296 {
297 struct uvc_device *uvc = to_uvc(f);
298 struct v4l2_event v4l2_event;
299 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
300
301 uvc_trace(UVC_TRACE_CONTROL,
302 "setup request %02x %02x value %04x index %04x %04x\n",
303 ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
304 le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
305
306 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
307 uvcg_info(f, "invalid request type\n");
308 return -EINVAL;
309 }
310
311 /* Stall too big requests. */
312 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
313 return -EINVAL;
314
315 /* Tell the complete callback to generate an event for the next request
316 * that will be enqueued by UVCIOC_SEND_RESPONSE.
317 */
318 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
319 uvc->event_length = le16_to_cpu(ctrl->wLength);
320
321 memset(&v4l2_event, 0, sizeof(v4l2_event));
322 v4l2_event.type = UVC_EVENT_SETUP;
323 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
324 v4l2_event_queue(&uvc->vdev, &v4l2_event);
325
326 return 0;
327 }
328
uvc_function_setup_continue(struct uvc_device * uvc)329 void uvc_function_setup_continue(struct uvc_device *uvc)
330 {
331 struct usb_composite_dev *cdev = uvc->func.config->cdev;
332
333 usb_composite_setup_continue(cdev);
334 }
335
336 static int
uvc_function_get_alt(struct usb_function * f,unsigned interface)337 uvc_function_get_alt(struct usb_function *f, unsigned interface)
338 {
339 struct uvc_device *uvc = to_uvc(f);
340 struct f_uvc_opts *opts;
341
342 uvcg_info(f, "%s(%u)\n", __func__, interface);
343
344 opts = fi_to_f_uvc_opts(f->fi);
345
346 if (interface == uvc->control_intf)
347 return 0;
348 else if (interface != uvc->streaming_intf)
349 return -EINVAL;
350 else if (!opts->streaming_bulk)
351 return uvc->video.ep->enabled ? 1 : 0;
352 else
353 /*
354 * Alt settings in an interface are supported only for
355 * ISOC endpoints as there are different alt-settings for
356 * zero-bandwidth and full-bandwidth cases, but the same
357 * is not true for BULK endpoints, as they have a single
358 * alt-setting.
359 */
360 return 0;
361 }
362
363 static int
uvc_function_set_alt(struct usb_function * f,unsigned interface,unsigned alt)364 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
365 {
366 struct uvc_device *uvc = to_uvc(f);
367 struct usb_composite_dev *cdev = f->config->cdev;
368 struct v4l2_event v4l2_event;
369 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
370 struct f_uvc_opts *opts;
371 int ret;
372
373 uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
374
375 opts = fi_to_f_uvc_opts(f->fi);
376
377 if (interface == uvc->control_intf) {
378 if (alt)
379 return -EINVAL;
380
381 uvcg_info(f, "reset UVC Control\n");
382 usb_ep_disable(uvc->control_ep);
383
384 if (!uvc->control_ep->desc)
385 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
386 return -EINVAL;
387
388 usb_ep_enable(uvc->control_ep);
389
390 if (uvc->event_suspend) {
391 memset(&v4l2_event, 0, sizeof(v4l2_event));
392 v4l2_event.type = UVC_EVENT_RESUME;
393 v4l2_event_queue(&uvc->vdev, &v4l2_event);
394 uvc->event_suspend = 0;
395 uvc_trace(UVC_TRACE_SUSPEND, "send UVC_EVENT_RESUME\n");
396 }
397
398 if (uvc->state == UVC_STATE_DISCONNECTED) {
399 memset(&v4l2_event, 0, sizeof(v4l2_event));
400 v4l2_event.type = UVC_EVENT_CONNECT;
401 uvc_event->speed = cdev->gadget->speed;
402 v4l2_event_queue(&uvc->vdev, &v4l2_event);
403
404 uvc->state = UVC_STATE_CONNECTED;
405 }
406
407 return 0;
408 }
409
410 if (interface != uvc->streaming_intf)
411 return -EINVAL;
412
413 if (!opts->streaming_bulk) {
414 switch (alt) {
415 case 0:
416 if (uvc->state != UVC_STATE_STREAMING)
417 return 0;
418
419 if (uvc->video.ep)
420 usb_ep_disable(uvc->video.ep);
421
422 memset(&v4l2_event, 0, sizeof(v4l2_event));
423 v4l2_event.type = UVC_EVENT_STREAMOFF;
424 v4l2_event_queue(&uvc->vdev, &v4l2_event);
425
426 uvc->state = UVC_STATE_CONNECTED;
427 return 0;
428
429 case 1:
430 if (uvc->state != UVC_STATE_CONNECTED)
431 return 0;
432
433 if (!uvc->video.ep)
434 return -EINVAL;
435
436 INFO(cdev, "reset UVC\n");
437 usb_ep_disable(uvc->video.ep);
438
439 ret = config_ep_by_speed(f->config->cdev->gadget,
440 &uvc->func, uvc->video.ep);
441 if (ret)
442 return ret;
443 usb_ep_enable(uvc->video.ep);
444
445 memset(&v4l2_event, 0, sizeof(v4l2_event));
446 v4l2_event.type = UVC_EVENT_STREAMON;
447 v4l2_event_queue(&uvc->vdev, &v4l2_event);
448 return USB_GADGET_DELAYED_STATUS;
449
450 default:
451 return -EINVAL;
452 }
453 } else {
454 switch (uvc->state) {
455 case UVC_STATE_CONNECTED:
456 if (uvc->video.ep &&
457 !uvc->video.ep->enabled) {
458 /*
459 * Enable the video streaming endpoint,
460 * but don't change the 'uvc->state'.
461 */
462 ret = config_ep_by_speed(cdev->gadget,
463 &uvc->func,
464 uvc->video.ep);
465 if (ret)
466 return ret;
467 ret = usb_ep_enable(uvc->video.ep);
468 if (ret)
469 return ret;
470 } else {
471 memset(&v4l2_event, 0, sizeof(v4l2_event));
472 v4l2_event.type = UVC_EVENT_STREAMON;
473 v4l2_event_queue(&uvc->vdev, &v4l2_event);
474
475 uvc->state = UVC_STATE_STREAMING;
476 }
477 return 0;
478
479 case UVC_STATE_STREAMING:
480 if (!alt) {
481 INFO(cdev, "bulk streaming intf not support alt 0\n");
482 return 0;
483 }
484
485 if (uvc->video.ep &&
486 uvc->video.ep->enabled) {
487 ret = usb_ep_disable(uvc->video.ep);
488 if (ret)
489 return ret;
490 }
491
492 memset(&v4l2_event, 0, sizeof(v4l2_event));
493 v4l2_event.type = UVC_EVENT_STREAMOFF;
494 v4l2_event_queue(&uvc->vdev, &v4l2_event);
495 uvc->state = UVC_STATE_CONNECTED;
496 return 0;
497
498 default:
499 return -EINVAL;
500 }
501 }
502 }
503
504 static void
uvc_function_disable(struct usb_function * f)505 uvc_function_disable(struct usb_function *f)
506 {
507 struct uvc_device *uvc = to_uvc(f);
508 struct v4l2_event v4l2_event;
509
510 uvcg_info(f, "%s()\n", __func__);
511
512 memset(&v4l2_event, 0, sizeof(v4l2_event));
513 v4l2_event.type = UVC_EVENT_DISCONNECT;
514 v4l2_event_queue(&uvc->vdev, &v4l2_event);
515
516 uvc->state = UVC_STATE_DISCONNECTED;
517
518 usb_ep_disable(uvc->video.ep);
519 usb_ep_disable(uvc->control_ep);
520 }
521
uvc_function_suspend(struct usb_function * f)522 static void uvc_function_suspend(struct usb_function *f)
523 {
524 struct uvc_device *uvc = to_uvc(f);
525 struct v4l2_event v4l2_event;
526
527 memset(&v4l2_event, 0, sizeof(v4l2_event));
528 v4l2_event.type = UVC_EVENT_SUSPEND;
529 v4l2_event_queue(&uvc->vdev, &v4l2_event);
530 uvc->event_suspend = 1;
531 uvc_trace(UVC_TRACE_SUSPEND, "send UVC_EVENT_SUSPEND\n");
532 }
533
uvc_function_resume(struct usb_function * f)534 static void uvc_function_resume(struct usb_function *f)
535 {
536 struct uvc_device *uvc = to_uvc(f);
537 struct v4l2_event v4l2_event;
538
539 memset(&v4l2_event, 0, sizeof(v4l2_event));
540 v4l2_event.type = UVC_EVENT_RESUME;
541 v4l2_event_queue(&uvc->vdev, &v4l2_event);
542 uvc->event_suspend = 0;
543 uvc_trace(UVC_TRACE_SUSPEND, "send UVC_EVENT_RESUME\n");
544 }
545
546 /* --------------------------------------------------------------------------
547 * Connection / disconnection
548 */
549
550 void
uvc_function_connect(struct uvc_device * uvc)551 uvc_function_connect(struct uvc_device *uvc)
552 {
553 int ret;
554
555 if ((ret = usb_function_activate(&uvc->func)) < 0)
556 uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);
557 }
558
559 void
uvc_function_disconnect(struct uvc_device * uvc)560 uvc_function_disconnect(struct uvc_device *uvc)
561 {
562 int ret;
563
564 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
565 uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);
566 }
567
568 /* --------------------------------------------------------------------------
569 * USB probe and disconnect
570 */
571
function_name_show(struct device * dev,struct device_attribute * attr,char * buf)572 static ssize_t function_name_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574 {
575 struct uvc_device *uvc = dev_get_drvdata(dev);
576
577 return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);
578 }
579
580 static DEVICE_ATTR_RO(function_name);
581
582 static int
uvc_register_video(struct uvc_device * uvc)583 uvc_register_video(struct uvc_device *uvc)
584 {
585 struct usb_composite_dev *cdev = uvc->func.config->cdev;
586 int ret;
587
588 /* TODO reference counting. */
589 uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
590 uvc->vdev.fops = &uvc_v4l2_fops;
591 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
592 uvc->vdev.release = video_device_release_empty;
593 uvc->vdev.vfl_dir = VFL_DIR_TX;
594 uvc->vdev.lock = &uvc->video.mutex;
595 uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
596 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
597
598 video_set_drvdata(&uvc->vdev, uvc);
599
600 ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1);
601 if (ret < 0)
602 return ret;
603
604 ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
605 if (ret < 0) {
606 video_unregister_device(&uvc->vdev);
607 return ret;
608 }
609
610 return 0;
611 }
612
613 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
614 do { \
615 memcpy(mem, desc, (desc)->bLength); \
616 *(dst)++ = mem; \
617 mem += (desc)->bLength; \
618 } while (0);
619
620 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
621 do { \
622 const struct usb_descriptor_header * const *__src; \
623 for (__src = src; *__src; ++__src) { \
624 memcpy(mem, *__src, (*__src)->bLength); \
625 *dst++ = mem; \
626 mem += (*__src)->bLength; \
627 } \
628 } while (0)
629
630 static struct usb_descriptor_header **
uvc_copy_descriptors(struct uvc_device * uvc,enum usb_device_speed speed)631 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
632 {
633 struct uvc_input_header_descriptor *uvc_streaming_header;
634 struct uvc_header_descriptor *uvc_control_header;
635 const struct uvc_descriptor_header * const *uvc_control_desc;
636 const struct uvc_descriptor_header * const *uvc_streaming_cls;
637 const struct usb_descriptor_header * const *uvc_streaming_std;
638 const struct usb_descriptor_header * const *src;
639 struct usb_interface_descriptor *streaming_intf_alt0;
640 struct usb_descriptor_header **dst;
641 struct usb_descriptor_header **hdr;
642 struct f_uvc_opts *opts;
643 unsigned int control_size;
644 unsigned int streaming_size;
645 unsigned int n_desc;
646 unsigned int bytes;
647 void *mem;
648
649 opts = fi_to_f_uvc_opts(uvc->func.fi);
650
651 switch (speed) {
652 case USB_SPEED_SUPER:
653 uvc_control_desc = uvc->desc.ss_control;
654 uvc_streaming_cls = uvc->desc.ss_streaming;
655 if (!opts->streaming_bulk)
656 uvc_streaming_std = uvc_ss_streaming;
657 else
658 uvc_streaming_std = uvc_ss_bulk_streaming;
659 break;
660
661 case USB_SPEED_HIGH:
662 uvc_control_desc = uvc->desc.fs_control;
663 uvc_streaming_cls = uvc->desc.hs_streaming;
664 if (!opts->streaming_bulk)
665 uvc_streaming_std = uvc_hs_streaming;
666 else
667 uvc_streaming_std = uvc_hs_bulk_streaming;
668 break;
669
670 case USB_SPEED_FULL:
671 default:
672 uvc_control_desc = uvc->desc.fs_control;
673 uvc_streaming_cls = uvc->desc.fs_streaming;
674 if (!opts->streaming_bulk)
675 uvc_streaming_std = uvc_fs_streaming;
676 else
677 uvc_streaming_std = uvc_fs_bulk_streaming;
678 break;
679 }
680
681 if (!uvc_control_desc || !uvc_streaming_cls)
682 return ERR_PTR(-ENODEV);
683
684 /* Descriptors layout
685 *
686 * uvc_iad
687 * uvc_control_intf
688 * Class-specific UVC control descriptors
689 * uvc_control_ep
690 * uvc_control_cs_ep
691 * uvc_ss_control_comp (for SS only)
692 * uvc_streaming_intf_alt0
693 * Class-specific UVC streaming descriptors
694 * uvc_{fs|hs}_streaming
695 */
696
697 if (!opts->streaming_bulk)
698 streaming_intf_alt0 = &uvc_streaming_intf_alt0;
699 else
700 streaming_intf_alt0 = &uvc_bulk_streaming_intf_alt0;
701
702 /* Count descriptors and compute their size. */
703 control_size = 0;
704 streaming_size = 0;
705 bytes = uvc_iad.bLength + uvc_control_intf.bLength
706 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
707 + streaming_intf_alt0->bLength;
708
709 if (speed == USB_SPEED_SUPER) {
710 bytes += uvc_ss_control_comp.bLength;
711 n_desc = 6;
712 } else {
713 n_desc = 5;
714 }
715
716 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
717 *src; ++src) {
718 control_size += (*src)->bLength;
719 bytes += (*src)->bLength;
720 n_desc++;
721 }
722 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
723 *src; ++src) {
724 streaming_size += (*src)->bLength;
725 bytes += (*src)->bLength;
726 n_desc++;
727 }
728 for (src = uvc_streaming_std; *src; ++src) {
729 bytes += (*src)->bLength;
730 n_desc++;
731 }
732
733 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
734 if (mem == NULL)
735 return NULL;
736
737 hdr = mem;
738 dst = mem;
739 mem += (n_desc + 1) * sizeof(*src);
740
741 /* Copy the descriptors. */
742 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
743 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
744
745 uvc_control_header = mem;
746 UVC_COPY_DESCRIPTORS(mem, dst,
747 (const struct usb_descriptor_header **)uvc_control_desc);
748 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
749 uvc_control_header->bInCollection = 1;
750 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
751
752 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
753 if (speed == USB_SPEED_SUPER)
754 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
755
756 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
757 UVC_COPY_DESCRIPTOR(mem, dst, streaming_intf_alt0);
758
759 uvc_streaming_header = mem;
760 UVC_COPY_DESCRIPTORS(mem, dst,
761 (const struct usb_descriptor_header**)uvc_streaming_cls);
762 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
763 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
764
765 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
766
767 *dst = NULL;
768 return hdr;
769 }
770
771 static int
uvc_function_bind(struct usb_configuration * c,struct usb_function * f)772 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
773 {
774 struct usb_composite_dev *cdev = c->cdev;
775 struct uvc_device *uvc = to_uvc(f);
776 struct usb_string *us;
777 unsigned int max_packet_mult;
778 unsigned int max_packet_size;
779 struct usb_ep *ep;
780 struct f_uvc_opts *opts;
781 int ret = -EINVAL;
782 u8 address;
783
784 uvcg_info(f, "%s()\n", __func__);
785
786 opts = fi_to_f_uvc_opts(f->fi);
787 /* Sanity check the streaming endpoint module parameters.
788 */
789 if (!opts->streaming_bulk) {
790 opts->streaming_interval = clamp(opts->streaming_interval,
791 1U, 16U);
792 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket,
793 1U, 3072U);
794 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
795 } else {
796 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket,
797 1U, 1024U);
798 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
799 }
800
801 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
802 if (opts->streaming_maxburst &&
803 (opts->streaming_maxpacket % 1024) != 0) {
804 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
805 uvcg_info(f, "overriding streaming_maxpacket to %d\n",
806 opts->streaming_maxpacket);
807 }
808
809 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
810 * module parameters.
811 *
812 * NOTE: We assume that the user knows what they are doing and won't
813 * give parameters that their UDC doesn't support.
814 */
815 if (opts->streaming_maxpacket <= 1024) {
816 max_packet_mult = 1;
817 max_packet_size = opts->streaming_maxpacket;
818 } else if (opts->streaming_maxpacket <= 2048) {
819 max_packet_mult = 2;
820 max_packet_size = opts->streaming_maxpacket / 2;
821 } else {
822 max_packet_mult = 3;
823 max_packet_size = opts->streaming_maxpacket / 3;
824 }
825
826 if (!opts->streaming_bulk) {
827 uvc_fs_streaming_ep.wMaxPacketSize =
828 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
829 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
830
831 uvc_hs_streaming_ep.wMaxPacketSize =
832 cpu_to_le16(max_packet_size |
833 ((max_packet_mult - 1) << 11));
834
835 /* A high-bandwidth endpoint must specify a bInterval value of 1 */
836 if (max_packet_mult > 1)
837 uvc_hs_streaming_ep.bInterval = 1;
838 else
839 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
840
841 uvc_ss_streaming_ep.wMaxPacketSize =
842 cpu_to_le16(max_packet_size);
843 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
844 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
845 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
846 uvc_ss_streaming_comp.wBytesPerInterval =
847 cpu_to_le16(max_packet_size * max_packet_mult *
848 (opts->streaming_maxburst + 1));
849 } else {
850 uvc_fs_bulk_streaming_ep.wMaxPacketSize =
851 cpu_to_le16(min(opts->streaming_maxpacket, 64U));
852
853 uvc_hs_bulk_streaming_ep.wMaxPacketSize =
854 cpu_to_le16(min(opts->streaming_maxpacket, 512U));
855
856 uvc_ss_bulk_streaming_ep.wMaxPacketSize =
857 cpu_to_le16(max_packet_size);
858 uvc_ss_bulk_streaming_comp.bMaxBurst = opts->streaming_maxburst;
859 /*
860 * As per USB 3.1 spec "Table 9-26. SuperSpeed Endpoint
861 * Companion Descriptor", the wBytesPerInterval must be
862 * set to zero for bulk endpoints.
863 */
864 uvc_ss_bulk_streaming_comp.wBytesPerInterval = 0;
865 }
866
867 /* Allocate endpoints. */
868 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
869 if (!ep) {
870 uvcg_info(f, "Unable to allocate control EP\n");
871 goto error;
872 }
873 uvc->control_ep = ep;
874
875 if (gadget_is_superspeed(c->cdev->gadget)) {
876 if (!opts->streaming_bulk)
877 ep = usb_ep_autoconfig_ss(cdev->gadget,
878 &uvc_ss_streaming_ep,
879 &uvc_ss_streaming_comp);
880 else
881 ep = usb_ep_autoconfig_ss(cdev->gadget,
882 &uvc_ss_bulk_streaming_ep,
883 &uvc_ss_bulk_streaming_comp);
884 } else if (gadget_is_dualspeed(cdev->gadget)) {
885 if (!opts->streaming_bulk) {
886 ep = usb_ep_autoconfig(cdev->gadget,
887 &uvc_hs_streaming_ep);
888 } else {
889 ep = usb_ep_autoconfig(cdev->gadget,
890 &uvc_hs_bulk_streaming_ep);
891 /*
892 * In ep_matches(), it will set wMaxPacketSize to 64
893 * bytes if ep is Bulk and ep_comp is NULL for hs/fs
894 * bulk maxpacket. So we need to set hs bulk maxpacket
895 * 512 bytes again here.
896 */
897 uvc_hs_bulk_streaming_ep.wMaxPacketSize =
898 cpu_to_le16(min(opts->streaming_maxpacket,
899 512U));
900 }
901 } else {
902 if (!opts->streaming_bulk)
903 ep = usb_ep_autoconfig(cdev->gadget,
904 &uvc_fs_streaming_ep);
905 else
906 ep = usb_ep_autoconfig(cdev->gadget,
907 &uvc_fs_bulk_streaming_ep);
908 }
909
910 if (!ep) {
911 uvcg_info(f, "Unable to allocate streaming EP\n");
912 goto error;
913 }
914 uvc->video.ep = ep;
915 address = uvc->video.ep->address;
916
917 if (!opts->streaming_bulk) {
918 uvc_fs_streaming_ep.bEndpointAddress = address;
919 uvc_hs_streaming_ep.bEndpointAddress = address;
920 uvc_ss_streaming_ep.bEndpointAddress = address;
921 } else {
922 uvc_fs_bulk_streaming_ep.bEndpointAddress = address;
923 uvc_hs_bulk_streaming_ep.bEndpointAddress = address;
924 uvc_ss_bulk_streaming_ep.bEndpointAddress = address;
925 }
926
927 #if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
928 if (opts->device_name)
929 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->device_name;
930 #endif
931
932 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name;
933 us = usb_gstrings_attach(cdev, uvc_function_strings,
934 ARRAY_SIZE(uvc_en_us_strings));
935 if (IS_ERR(us)) {
936 ret = PTR_ERR(us);
937 goto error;
938 }
939 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
940 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
941 ret = us[UVC_STRING_STREAMING_IDX].id;
942 if (!opts->streaming_bulk) {
943 uvc_streaming_intf_alt0.iInterface = ret;
944 uvc_streaming_intf_alt1.iInterface = ret;
945 } else {
946 uvc_bulk_streaming_intf_alt0.iInterface = ret;
947 }
948
949 /* Allocate interface IDs. */
950 if ((ret = usb_interface_id(c, f)) < 0)
951 goto error;
952 uvc_iad.bFirstInterface = ret;
953 uvc_control_intf.bInterfaceNumber = ret;
954 uvc->control_intf = ret;
955 opts->control_interface = ret;
956
957 if ((ret = usb_interface_id(c, f)) < 0)
958 goto error;
959
960 if (!opts->streaming_bulk) {
961 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
962 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
963 } else {
964 uvc_bulk_streaming_intf_alt0.bInterfaceNumber = ret;
965 }
966
967 uvc->streaming_intf = ret;
968 opts->streaming_interface = ret;
969
970 /* Copy descriptors */
971 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
972 if (IS_ERR(f->fs_descriptors)) {
973 ret = PTR_ERR(f->fs_descriptors);
974 f->fs_descriptors = NULL;
975 goto error;
976 }
977 if (gadget_is_dualspeed(cdev->gadget)) {
978 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
979 if (IS_ERR(f->hs_descriptors)) {
980 ret = PTR_ERR(f->hs_descriptors);
981 f->hs_descriptors = NULL;
982 goto error;
983 }
984 }
985 if (gadget_is_superspeed(c->cdev->gadget)) {
986 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
987 if (IS_ERR(f->ss_descriptors)) {
988 ret = PTR_ERR(f->ss_descriptors);
989 f->ss_descriptors = NULL;
990 goto error;
991 }
992 }
993
994 /* Preallocate control endpoint request. */
995 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
996 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
997 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
998 ret = -ENOMEM;
999 goto error;
1000 }
1001
1002 uvc->control_req->buf = uvc->control_buf;
1003 uvc->control_req->complete = uvc_function_ep0_complete;
1004 uvc->control_req->context = uvc;
1005
1006 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
1007 uvcg_err(f, "failed to register V4L2 device\n");
1008 goto error;
1009 }
1010
1011 /* Initialise video. */
1012 ret = uvcg_video_init(&uvc->video, uvc);
1013 if (ret < 0)
1014 goto v4l2_error;
1015
1016 if (opts->streaming_bulk)
1017 uvc->video.max_payload_size = uvc->video.imagesize;
1018 /* Register a V4L2 device. */
1019 ret = uvc_register_video(uvc);
1020 if (ret < 0) {
1021 uvcg_err(f, "failed to register video device\n");
1022 goto v4l2_error;
1023 }
1024
1025 return 0;
1026
1027 v4l2_error:
1028 v4l2_device_unregister(&uvc->v4l2_dev);
1029 error:
1030 if (uvc->control_req)
1031 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
1032 kfree(uvc->control_buf);
1033
1034 usb_free_all_descriptors(f);
1035 return ret;
1036 }
1037
1038 /* --------------------------------------------------------------------------
1039 * USB gadget function
1040 */
1041
uvc_free_inst(struct usb_function_instance * f)1042 static void uvc_free_inst(struct usb_function_instance *f)
1043 {
1044 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
1045
1046 mutex_destroy(&opts->lock);
1047
1048 #if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
1049 if (opts->device_name_allocated) {
1050 opts->device_name_allocated = false;
1051 kfree(opts->device_name);
1052 opts->device_name = NULL;
1053 }
1054 #endif
1055
1056 kfree(opts);
1057 }
1058
uvc_alloc_inst(void)1059 static struct usb_function_instance *uvc_alloc_inst(void)
1060 {
1061 struct f_uvc_opts *opts;
1062 struct uvc_camera_terminal_descriptor *cd;
1063 struct uvc_processing_unit_descriptor *pd;
1064 struct uvc_output_terminal_descriptor *od;
1065 struct UVC_EXTENSION_UNIT_DESCRIPTOR(1, 1) *ed;
1066 struct uvc_color_matching_descriptor *md;
1067 struct uvc_descriptor_header **ctl_cls;
1068 int ret;
1069
1070 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1071 if (!opts)
1072 return ERR_PTR(-ENOMEM);
1073 opts->func_inst.free_func_inst = uvc_free_inst;
1074 mutex_init(&opts->lock);
1075
1076 cd = &opts->uvc_camera_terminal;
1077 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
1078 cd->bDescriptorType = USB_DT_CS_INTERFACE;
1079 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
1080 cd->bTerminalID = 1;
1081 cd->wTerminalType = cpu_to_le16(0x0201);
1082 cd->bAssocTerminal = 0;
1083 cd->iTerminal = 0;
1084 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
1085 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
1086 cd->wOcularFocalLength = cpu_to_le16(0);
1087 cd->bControlSize = 3;
1088 cd->bmControls[0] = 2;
1089 cd->bmControls[1] = 0;
1090 cd->bmControls[2] = 0;
1091
1092 pd = &opts->uvc_processing;
1093 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
1094 pd->bDescriptorType = USB_DT_CS_INTERFACE;
1095 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
1096 pd->bUnitID = 2;
1097 pd->bSourceID = 1;
1098 pd->wMaxMultiplier = cpu_to_le16(16*1024);
1099 pd->bControlSize = 2;
1100 pd->bmControls[0] = 1;
1101 pd->bmControls[1] = 0;
1102 pd->iProcessing = 0;
1103 pd->bmVideoStandards = 0;
1104
1105 od = &opts->uvc_output_terminal;
1106 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
1107 od->bDescriptorType = USB_DT_CS_INTERFACE;
1108 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
1109 od->bTerminalID = 3;
1110 od->wTerminalType = cpu_to_le16(0x0101);
1111 od->bAssocTerminal = 0;
1112 od->bSourceID = 2;
1113 od->iTerminal = 0;
1114
1115 ed = &opts->uvc_extension;
1116 ed->bLength = UVC_DT_EXTENSION_UNIT_SIZE(1, 1);
1117 ed->bDescriptorType = USB_DT_CS_INTERFACE;
1118 ed->bDescriptorSubType = UVC_VC_EXTENSION_UNIT;
1119 ed->bUnitID = 6;
1120 ed->guidExtensionCode[0] = 0xa2;
1121 ed->guidExtensionCode[1] = 0x9e;
1122 ed->guidExtensionCode[2] = 0x76;
1123 ed->guidExtensionCode[3] = 0x41;
1124 ed->guidExtensionCode[4] = 0xde;
1125 ed->guidExtensionCode[5] = 0x04;
1126 ed->guidExtensionCode[6] = 0x47;
1127 ed->guidExtensionCode[7] = 0xe3;
1128 ed->guidExtensionCode[8] = 0x8b;
1129 ed->guidExtensionCode[9] = 0x2b;
1130 ed->guidExtensionCode[10] = 0xf4;
1131 ed->guidExtensionCode[11] = 0x34;
1132 ed->guidExtensionCode[12] = 0x1a;
1133 ed->guidExtensionCode[13] = 0xff;
1134 ed->guidExtensionCode[14] = 0x00;
1135 ed->guidExtensionCode[15] = 0x3b;
1136 ed->bNumControls = 3;
1137 ed->bNrInPins = 1;
1138 ed->baSourceID[0] = 2;
1139 ed->bControlSize = 1;
1140 ed->bmControls[0] = 7;
1141 ed->iExtension = 0;
1142
1143 md = &opts->uvc_color_matching;
1144 md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
1145 md->bDescriptorType = USB_DT_CS_INTERFACE;
1146 md->bDescriptorSubType = UVC_VS_COLORFORMAT;
1147 md->bColorPrimaries = 1;
1148 md->bTransferCharacteristics = 1;
1149 md->bMatrixCoefficients = 4;
1150
1151 /* Prepare fs control class descriptors for configfs-based gadgets */
1152 ctl_cls = opts->uvc_fs_control_cls;
1153 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
1154 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
1155 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
1156 ctl_cls[3] = (struct uvc_descriptor_header *)od;
1157 ctl_cls[4] = (struct uvc_descriptor_header *)ed;
1158 ctl_cls[5] = NULL; /* NULL-terminate */
1159 opts->fs_control =
1160 (const struct uvc_descriptor_header * const *)ctl_cls;
1161
1162 /* Prepare hs control class descriptors for configfs-based gadgets */
1163 ctl_cls = opts->uvc_ss_control_cls;
1164 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
1165 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
1166 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
1167 ctl_cls[3] = (struct uvc_descriptor_header *)od;
1168 ctl_cls[4] = (struct uvc_descriptor_header *)ed;
1169 ctl_cls[5] = NULL; /* NULL-terminate */
1170 opts->ss_control =
1171 (const struct uvc_descriptor_header * const *)ctl_cls;
1172
1173 opts->streaming_interval = 1;
1174 opts->streaming_maxpacket = 1024;
1175 opts->pm_qos_latency = 0;
1176 snprintf(opts->function_name, sizeof(opts->function_name), "UVC Camera");
1177
1178 ret = uvcg_attach_configfs(opts);
1179 if (ret < 0) {
1180 kfree(opts);
1181 return ERR_PTR(ret);
1182 }
1183
1184 return &opts->func_inst;
1185 }
1186
uvc_free(struct usb_function * f)1187 static void uvc_free(struct usb_function *f)
1188 {
1189 struct uvc_device *uvc = to_uvc(f);
1190 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
1191 func_inst);
1192 --opts->refcnt;
1193 kfree(uvc);
1194 }
1195
uvc_function_unbind(struct usb_configuration * c,struct usb_function * f)1196 static void uvc_function_unbind(struct usb_configuration *c,
1197 struct usb_function *f)
1198 {
1199 struct usb_composite_dev *cdev = c->cdev;
1200 struct uvc_device *uvc = to_uvc(f);
1201 long wait_ret = 1;
1202
1203 uvcg_info(f, "%s()\n", __func__);
1204
1205 /* If we know we're connected via v4l2, then there should be a cleanup
1206 * of the device from userspace either via UVC_EVENT_DISCONNECT or
1207 * though the video device removal uevent. Allow some time for the
1208 * application to close out before things get deleted.
1209 */
1210 if (uvc->func_connected) {
1211 uvcg_dbg(f, "waiting for clean disconnect\n");
1212 wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1213 uvc->func_connected == false, msecs_to_jiffies(500));
1214 uvcg_dbg(f, "done waiting with ret: %ld\n", wait_ret);
1215 }
1216
1217 device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
1218 video_unregister_device(&uvc->vdev);
1219 v4l2_device_unregister(&uvc->v4l2_dev);
1220
1221 if (uvc->func_connected) {
1222 /* Wait for the release to occur to ensure there are no longer any
1223 * pending operations that may cause panics when resources are cleaned
1224 * up.
1225 */
1226 uvcg_warn(f, "%s no clean disconnect, wait for release\n", __func__);
1227 wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1228 uvc->func_connected == false, msecs_to_jiffies(1000));
1229 uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret);
1230 }
1231
1232 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
1233 kfree(uvc->control_buf);
1234
1235 usb_free_all_descriptors(f);
1236 }
1237
uvc_alloc(struct usb_function_instance * fi)1238 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
1239 {
1240 struct uvc_device *uvc;
1241 struct f_uvc_opts *opts;
1242 struct uvc_descriptor_header **strm_cls;
1243
1244 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
1245 if (uvc == NULL)
1246 return ERR_PTR(-ENOMEM);
1247
1248 mutex_init(&uvc->video.mutex);
1249 uvc->state = UVC_STATE_DISCONNECTED;
1250 init_waitqueue_head(&uvc->func_connected_queue);
1251 opts = fi_to_f_uvc_opts(fi);
1252
1253 mutex_lock(&opts->lock);
1254 if (opts->uvc_fs_streaming_cls) {
1255 strm_cls = opts->uvc_fs_streaming_cls;
1256 opts->fs_streaming =
1257 (const struct uvc_descriptor_header * const *)strm_cls;
1258 }
1259 if (opts->uvc_hs_streaming_cls) {
1260 strm_cls = opts->uvc_hs_streaming_cls;
1261 opts->hs_streaming =
1262 (const struct uvc_descriptor_header * const *)strm_cls;
1263 }
1264 if (opts->uvc_ss_streaming_cls) {
1265 strm_cls = opts->uvc_ss_streaming_cls;
1266 opts->ss_streaming =
1267 (const struct uvc_descriptor_header * const *)strm_cls;
1268 }
1269
1270 uvc->desc.fs_control = opts->fs_control;
1271 uvc->desc.ss_control = opts->ss_control;
1272 uvc->desc.fs_streaming = opts->fs_streaming;
1273 uvc->desc.hs_streaming = opts->hs_streaming;
1274 uvc->desc.ss_streaming = opts->ss_streaming;
1275 ++opts->refcnt;
1276 mutex_unlock(&opts->lock);
1277
1278 /* Register the function. */
1279 uvc->func.name = "uvc";
1280 uvc->func.bind = uvc_function_bind;
1281 uvc->func.unbind = uvc_function_unbind;
1282 uvc->func.get_alt = uvc_function_get_alt;
1283 uvc->func.set_alt = uvc_function_set_alt;
1284 uvc->func.disable = uvc_function_disable;
1285 uvc->func.setup = uvc_function_setup;
1286 uvc->func.free_func = uvc_free;
1287 uvc->func.suspend = uvc_function_suspend;
1288 uvc->func.resume = uvc_function_resume;
1289 uvc->func.bind_deactivated = true;
1290
1291 return &uvc->func;
1292 }
1293
1294 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
1295 MODULE_LICENSE("GPL");
1296 MODULE_AUTHOR("Laurent Pinchart");
1297