1
2
3
4
5
6
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
39
40
41
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,
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
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
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
146
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
157
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
169
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
177
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
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
235 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
236 return -EINVAL;
237
238
239
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
314
315
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
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
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
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 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
427
428 video_set_drvdata(&uvc->vdev, uvc);
429
430 ret = video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
431 if (ret < 0)
432 return ret;
433
434 ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
435 if (ret < 0) {
436 video_unregister_device(&uvc->vdev);
437 return ret;
438 }
439
440 return 0;
441}
442
443#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
444 do { \
445 memcpy(mem, desc, (desc)->bLength); \
446 *(dst)++ = mem; \
447 mem += (desc)->bLength; \
448 } while (0);
449
450#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
451 do { \
452 const struct usb_descriptor_header * const *__src; \
453 for (__src = src; *__src; ++__src) { \
454 memcpy(mem, *__src, (*__src)->bLength); \
455 *dst++ = mem; \
456 mem += (*__src)->bLength; \
457 } \
458 } while (0)
459
460static struct usb_descriptor_header **
461uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
462{
463 struct uvc_input_header_descriptor *uvc_streaming_header;
464 struct uvc_header_descriptor *uvc_control_header;
465 const struct uvc_descriptor_header * const *uvc_control_desc;
466 const struct uvc_descriptor_header * const *uvc_streaming_cls;
467 const struct usb_descriptor_header * const *uvc_streaming_std;
468 const struct usb_descriptor_header * const *src;
469 struct usb_descriptor_header **dst;
470 struct usb_descriptor_header **hdr;
471 unsigned int control_size;
472 unsigned int streaming_size;
473 unsigned int n_desc;
474 unsigned int bytes;
475 void *mem;
476
477 switch (speed) {
478 case USB_SPEED_SUPER:
479 uvc_control_desc = uvc->desc.ss_control;
480 uvc_streaming_cls = uvc->desc.ss_streaming;
481 uvc_streaming_std = uvc_ss_streaming;
482 break;
483
484 case USB_SPEED_HIGH:
485 uvc_control_desc = uvc->desc.fs_control;
486 uvc_streaming_cls = uvc->desc.hs_streaming;
487 uvc_streaming_std = uvc_hs_streaming;
488 break;
489
490 case USB_SPEED_FULL:
491 default:
492 uvc_control_desc = uvc->desc.fs_control;
493 uvc_streaming_cls = uvc->desc.fs_streaming;
494 uvc_streaming_std = uvc_fs_streaming;
495 break;
496 }
497
498 if (!uvc_control_desc || !uvc_streaming_cls)
499 return ERR_PTR(-ENODEV);
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515 control_size = 0;
516 streaming_size = 0;
517 bytes = uvc_iad.bLength + uvc_control_intf.bLength
518 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
519 + uvc_streaming_intf_alt0.bLength;
520
521 if (speed == USB_SPEED_SUPER) {
522 bytes += uvc_ss_control_comp.bLength;
523 n_desc = 6;
524 } else {
525 n_desc = 5;
526 }
527
528 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
529 *src; ++src) {
530 control_size += (*src)->bLength;
531 bytes += (*src)->bLength;
532 n_desc++;
533 }
534 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
535 *src; ++src) {
536 streaming_size += (*src)->bLength;
537 bytes += (*src)->bLength;
538 n_desc++;
539 }
540 for (src = uvc_streaming_std; *src; ++src) {
541 bytes += (*src)->bLength;
542 n_desc++;
543 }
544
545 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
546 if (mem == NULL)
547 return NULL;
548
549 hdr = mem;
550 dst = mem;
551 mem += (n_desc + 1) * sizeof(*src);
552
553
554 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
555 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
556
557 uvc_control_header = mem;
558 UVC_COPY_DESCRIPTORS(mem, dst,
559 (const struct usb_descriptor_header **)uvc_control_desc);
560 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
561 uvc_control_header->bInCollection = 1;
562 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
563
564 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
565 if (speed == USB_SPEED_SUPER)
566 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
567
568 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
569 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
570
571 uvc_streaming_header = mem;
572 UVC_COPY_DESCRIPTORS(mem, dst,
573 (const struct usb_descriptor_header**)uvc_streaming_cls);
574 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
575 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
576
577 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
578
579 *dst = NULL;
580 return hdr;
581}
582
583static int
584uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
585{
586 struct usb_composite_dev *cdev = c->cdev;
587 struct uvc_device *uvc = to_uvc(f);
588 struct usb_string *us;
589 unsigned int max_packet_mult;
590 unsigned int max_packet_size;
591 struct usb_ep *ep;
592 struct f_uvc_opts *opts;
593 int ret = -EINVAL;
594
595 uvcg_info(f, "%s()\n", __func__);
596
597 opts = fi_to_f_uvc_opts(f->fi);
598
599
600 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
601 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
602 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
603
604
605 if (opts->streaming_maxburst &&
606 (opts->streaming_maxpacket % 1024) != 0) {
607 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
608 uvcg_info(f, "overriding streaming_maxpacket to %d\n",
609 opts->streaming_maxpacket);
610 }
611
612
613
614
615
616
617
618 if (opts->streaming_maxpacket <= 1024) {
619 max_packet_mult = 1;
620 max_packet_size = opts->streaming_maxpacket;
621 } else if (opts->streaming_maxpacket <= 2048) {
622 max_packet_mult = 2;
623 max_packet_size = opts->streaming_maxpacket / 2;
624 } else {
625 max_packet_mult = 3;
626 max_packet_size = opts->streaming_maxpacket / 3;
627 }
628
629 uvc_fs_streaming_ep.wMaxPacketSize =
630 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
631 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
632
633 uvc_hs_streaming_ep.wMaxPacketSize =
634 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
635 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
636
637 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
638 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
639 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
640 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
641 uvc_ss_streaming_comp.wBytesPerInterval =
642 cpu_to_le16(max_packet_size * max_packet_mult *
643 (opts->streaming_maxburst + 1));
644
645
646 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
647 if (!ep) {
648 uvcg_info(f, "Unable to allocate control EP\n");
649 goto error;
650 }
651 uvc->control_ep = ep;
652
653 if (gadget_is_superspeed(c->cdev->gadget))
654 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
655 &uvc_ss_streaming_comp);
656 else if (gadget_is_dualspeed(cdev->gadget))
657 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
658 else
659 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
660
661 if (!ep) {
662 uvcg_info(f, "Unable to allocate streaming EP\n");
663 goto error;
664 }
665 uvc->video.ep = ep;
666
667 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
668 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
669 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
670
671 us = usb_gstrings_attach(cdev, uvc_function_strings,
672 ARRAY_SIZE(uvc_en_us_strings));
673 if (IS_ERR(us)) {
674 ret = PTR_ERR(us);
675 goto error;
676 }
677 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
678 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
679 ret = us[UVC_STRING_STREAMING_IDX].id;
680 uvc_streaming_intf_alt0.iInterface = ret;
681 uvc_streaming_intf_alt1.iInterface = ret;
682
683
684 if ((ret = usb_interface_id(c, f)) < 0)
685 goto error;
686 uvc_iad.bFirstInterface = ret;
687 uvc_control_intf.bInterfaceNumber = ret;
688 uvc->control_intf = ret;
689 opts->control_interface = ret;
690
691 if ((ret = usb_interface_id(c, f)) < 0)
692 goto error;
693 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
694 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
695 uvc->streaming_intf = ret;
696 opts->streaming_interface = ret;
697
698
699 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
700 if (IS_ERR(f->fs_descriptors)) {
701 ret = PTR_ERR(f->fs_descriptors);
702 f->fs_descriptors = NULL;
703 goto error;
704 }
705 if (gadget_is_dualspeed(cdev->gadget)) {
706 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
707 if (IS_ERR(f->hs_descriptors)) {
708 ret = PTR_ERR(f->hs_descriptors);
709 f->hs_descriptors = NULL;
710 goto error;
711 }
712 }
713 if (gadget_is_superspeed(c->cdev->gadget)) {
714 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
715 if (IS_ERR(f->ss_descriptors)) {
716 ret = PTR_ERR(f->ss_descriptors);
717 f->ss_descriptors = NULL;
718 goto error;
719 }
720 }
721
722
723 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
724 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
725 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
726 ret = -ENOMEM;
727 goto error;
728 }
729
730 uvc->control_req->buf = uvc->control_buf;
731 uvc->control_req->complete = uvc_function_ep0_complete;
732 uvc->control_req->context = uvc;
733
734 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
735 uvcg_err(f, "failed to register V4L2 device\n");
736 goto error;
737 }
738
739
740 ret = uvcg_video_init(&uvc->video, uvc);
741 if (ret < 0)
742 goto error;
743
744
745 ret = uvc_register_video(uvc);
746 if (ret < 0) {
747 uvcg_err(f, "failed to register video device\n");
748 goto error;
749 }
750
751 return 0;
752
753error:
754 v4l2_device_unregister(&uvc->v4l2_dev);
755
756 if (uvc->control_req)
757 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
758 kfree(uvc->control_buf);
759
760 usb_free_all_descriptors(f);
761 return ret;
762}
763
764
765
766
767
768static void uvc_free_inst(struct usb_function_instance *f)
769{
770 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
771
772 mutex_destroy(&opts->lock);
773 kfree(opts);
774}
775
776static struct usb_function_instance *uvc_alloc_inst(void)
777{
778 struct f_uvc_opts *opts;
779 struct uvc_camera_terminal_descriptor *cd;
780 struct uvc_processing_unit_descriptor *pd;
781 struct uvc_output_terminal_descriptor *od;
782 struct uvc_color_matching_descriptor *md;
783 struct uvc_descriptor_header **ctl_cls;
784 int ret;
785
786 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
787 if (!opts)
788 return ERR_PTR(-ENOMEM);
789 opts->func_inst.free_func_inst = uvc_free_inst;
790 mutex_init(&opts->lock);
791
792 cd = &opts->uvc_camera_terminal;
793 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
794 cd->bDescriptorType = USB_DT_CS_INTERFACE;
795 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
796 cd->bTerminalID = 1;
797 cd->wTerminalType = cpu_to_le16(0x0201);
798 cd->bAssocTerminal = 0;
799 cd->iTerminal = 0;
800 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
801 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
802 cd->wOcularFocalLength = cpu_to_le16(0);
803 cd->bControlSize = 3;
804 cd->bmControls[0] = 2;
805 cd->bmControls[1] = 0;
806 cd->bmControls[2] = 0;
807
808 pd = &opts->uvc_processing;
809 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
810 pd->bDescriptorType = USB_DT_CS_INTERFACE;
811 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
812 pd->bUnitID = 2;
813 pd->bSourceID = 1;
814 pd->wMaxMultiplier = cpu_to_le16(16*1024);
815 pd->bControlSize = 2;
816 pd->bmControls[0] = 1;
817 pd->bmControls[1] = 0;
818 pd->iProcessing = 0;
819
820 od = &opts->uvc_output_terminal;
821 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
822 od->bDescriptorType = USB_DT_CS_INTERFACE;
823 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
824 od->bTerminalID = 3;
825 od->wTerminalType = cpu_to_le16(0x0101);
826 od->bAssocTerminal = 0;
827 od->bSourceID = 2;
828 od->iTerminal = 0;
829
830 md = &opts->uvc_color_matching;
831 md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
832 md->bDescriptorType = USB_DT_CS_INTERFACE;
833 md->bDescriptorSubType = UVC_VS_COLORFORMAT;
834 md->bColorPrimaries = 1;
835 md->bTransferCharacteristics = 1;
836 md->bMatrixCoefficients = 4;
837
838
839 ctl_cls = opts->uvc_fs_control_cls;
840 ctl_cls[0] = NULL;
841 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
842 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
843 ctl_cls[3] = (struct uvc_descriptor_header *)od;
844 ctl_cls[4] = NULL;
845 opts->fs_control =
846 (const struct uvc_descriptor_header * const *)ctl_cls;
847
848
849 ctl_cls = opts->uvc_ss_control_cls;
850 ctl_cls[0] = NULL;
851 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
852 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
853 ctl_cls[3] = (struct uvc_descriptor_header *)od;
854 ctl_cls[4] = NULL;
855 opts->ss_control =
856 (const struct uvc_descriptor_header * const *)ctl_cls;
857
858 opts->streaming_interval = 1;
859 opts->streaming_maxpacket = 1024;
860
861 ret = uvcg_attach_configfs(opts);
862 if (ret < 0) {
863 kfree(opts);
864 return ERR_PTR(ret);
865 }
866
867 return &opts->func_inst;
868}
869
870static void uvc_free(struct usb_function *f)
871{
872 struct uvc_device *uvc = to_uvc(f);
873 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
874 func_inst);
875 --opts->refcnt;
876 kfree(uvc);
877}
878
879static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
880{
881 struct usb_composite_dev *cdev = c->cdev;
882 struct uvc_device *uvc = to_uvc(f);
883
884 uvcg_info(f, "%s\n", __func__);
885
886 device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
887 video_unregister_device(&uvc->vdev);
888 v4l2_device_unregister(&uvc->v4l2_dev);
889
890 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
891 kfree(uvc->control_buf);
892
893 usb_free_all_descriptors(f);
894}
895
896static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
897{
898 struct uvc_device *uvc;
899 struct f_uvc_opts *opts;
900 struct uvc_descriptor_header **strm_cls;
901
902 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
903 if (uvc == NULL)
904 return ERR_PTR(-ENOMEM);
905
906 mutex_init(&uvc->video.mutex);
907 uvc->state = UVC_STATE_DISCONNECTED;
908 opts = fi_to_f_uvc_opts(fi);
909
910 mutex_lock(&opts->lock);
911 if (opts->uvc_fs_streaming_cls) {
912 strm_cls = opts->uvc_fs_streaming_cls;
913 opts->fs_streaming =
914 (const struct uvc_descriptor_header * const *)strm_cls;
915 }
916 if (opts->uvc_hs_streaming_cls) {
917 strm_cls = opts->uvc_hs_streaming_cls;
918 opts->hs_streaming =
919 (const struct uvc_descriptor_header * const *)strm_cls;
920 }
921 if (opts->uvc_ss_streaming_cls) {
922 strm_cls = opts->uvc_ss_streaming_cls;
923 opts->ss_streaming =
924 (const struct uvc_descriptor_header * const *)strm_cls;
925 }
926
927 uvc->desc.fs_control = opts->fs_control;
928 uvc->desc.ss_control = opts->ss_control;
929 uvc->desc.fs_streaming = opts->fs_streaming;
930 uvc->desc.hs_streaming = opts->hs_streaming;
931 uvc->desc.ss_streaming = opts->ss_streaming;
932 ++opts->refcnt;
933 mutex_unlock(&opts->lock);
934
935
936 uvc->func.name = "uvc";
937 uvc->func.bind = uvc_function_bind;
938 uvc->func.unbind = uvc_unbind;
939 uvc->func.get_alt = uvc_function_get_alt;
940 uvc->func.set_alt = uvc_function_set_alt;
941 uvc->func.disable = uvc_function_disable;
942 uvc->func.setup = uvc_function_setup;
943 uvc->func.free_func = uvc_free;
944 uvc->func.bind_deactivated = true;
945
946 return &uvc->func;
947}
948
949DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
950MODULE_LICENSE("GPL");
951MODULE_AUTHOR("Laurent Pinchart");
952