54f04d3218296a0da9090c39c6ccaa035d66b2bf
[linux-block.git] / drivers / usb / gadget / function / f_uvc.c
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
35 /* --------------------------------------------------------------------------
36  * Function descriptors
37  */
38
39 /* string IDs are assigned dynamically */
40
41 #define UVC_STRING_CONTROL_IDX                  0
42 #define UVC_STRING_STREAMING_IDX                1
43
44 static struct usb_string uvc_en_us_strings[] = {
45         [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
46         [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
47         {  }
48 };
49
50 static struct usb_gadget_strings uvc_stringtab = {
51         .language = 0x0409,     /* en-us */
52         .strings = uvc_en_us_strings,
53 };
54
55 static struct usb_gadget_strings *uvc_function_strings[] = {
56         &uvc_stringtab,
57         NULL,
58 };
59
60 #define UVC_INTF_VIDEO_CONTROL                  0
61 #define UVC_INTF_VIDEO_STREAMING                1
62
63 #define UVC_STATUS_MAX_PACKET_SIZE              16      /* 16 bytes status */
64
65 static struct usb_interface_assoc_descriptor uvc_iad = {
66         .bLength                = sizeof(uvc_iad),
67         .bDescriptorType        = USB_DT_INTERFACE_ASSOCIATION,
68         .bFirstInterface        = 0,
69         .bInterfaceCount        = 2,
70         .bFunctionClass         = USB_CLASS_VIDEO,
71         .bFunctionSubClass      = UVC_SC_VIDEO_INTERFACE_COLLECTION,
72         .bFunctionProtocol      = 0x00,
73         .iFunction              = 0,
74 };
75
76 static struct usb_interface_descriptor uvc_control_intf = {
77         .bLength                = USB_DT_INTERFACE_SIZE,
78         .bDescriptorType        = USB_DT_INTERFACE,
79         .bInterfaceNumber       = UVC_INTF_VIDEO_CONTROL,
80         .bAlternateSetting      = 0,
81         .bNumEndpoints          = 1,
82         .bInterfaceClass        = USB_CLASS_VIDEO,
83         .bInterfaceSubClass     = UVC_SC_VIDEOCONTROL,
84         .bInterfaceProtocol     = 0x00,
85         .iInterface             = 0,
86 };
87
88 static struct usb_endpoint_descriptor uvc_control_ep = {
89         .bLength                = USB_DT_ENDPOINT_SIZE,
90         .bDescriptorType        = USB_DT_ENDPOINT,
91         .bEndpointAddress       = USB_DIR_IN,
92         .bmAttributes           = USB_ENDPOINT_XFER_INT,
93         .wMaxPacketSize         = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
94         .bInterval              = 8,
95 };
96
97 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
98         .bLength                = sizeof(uvc_ss_control_comp),
99         .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
100         /* The following 3 values can be tweaked if necessary. */
101         .bMaxBurst              = 0,
102         .bmAttributes           = 0,
103         .wBytesPerInterval      = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
104 };
105
106 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
107         .bLength                = UVC_DT_CONTROL_ENDPOINT_SIZE,
108         .bDescriptorType        = USB_DT_CS_ENDPOINT,
109         .bDescriptorSubType     = UVC_EP_INTERRUPT,
110         .wMaxTransferSize       = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
111 };
112
113 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
114         .bLength                = USB_DT_INTERFACE_SIZE,
115         .bDescriptorType        = USB_DT_INTERFACE,
116         .bInterfaceNumber       = UVC_INTF_VIDEO_STREAMING,
117         .bAlternateSetting      = 0,
118         .bNumEndpoints          = 0,
119         .bInterfaceClass        = USB_CLASS_VIDEO,
120         .bInterfaceSubClass     = UVC_SC_VIDEOSTREAMING,
121         .bInterfaceProtocol     = 0x00,
122         .iInterface             = 0,
123 };
124
125 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
126         .bLength                = USB_DT_INTERFACE_SIZE,
127         .bDescriptorType        = USB_DT_INTERFACE,
128         .bInterfaceNumber       = UVC_INTF_VIDEO_STREAMING,
129         .bAlternateSetting      = 1,
130         .bNumEndpoints          = 1,
131         .bInterfaceClass        = USB_CLASS_VIDEO,
132         .bInterfaceSubClass     = UVC_SC_VIDEOSTREAMING,
133         .bInterfaceProtocol     = 0x00,
134         .iInterface             = 0,
135 };
136
137 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
138         .bLength                = USB_DT_ENDPOINT_SIZE,
139         .bDescriptorType        = USB_DT_ENDPOINT,
140         .bEndpointAddress       = USB_DIR_IN,
141         .bmAttributes           = USB_ENDPOINT_SYNC_ASYNC
142                                 | USB_ENDPOINT_XFER_ISOC,
143         /* The wMaxPacketSize and bInterval values will be initialized from
144          * module parameters.
145          */
146 };
147
148 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
149         .bLength                = USB_DT_ENDPOINT_SIZE,
150         .bDescriptorType        = USB_DT_ENDPOINT,
151         .bEndpointAddress       = USB_DIR_IN,
152         .bmAttributes           = USB_ENDPOINT_SYNC_ASYNC
153                                 | USB_ENDPOINT_XFER_ISOC,
154         /* The wMaxPacketSize and bInterval values will be initialized from
155          * module parameters.
156          */
157 };
158
159 static 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         /* The wMaxPacketSize and bInterval values will be initialized from
167          * module parameters.
168          */
169 };
170
171 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
172         .bLength                = sizeof(uvc_ss_streaming_comp),
173         .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
174         /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
175          * initialized from module parameters.
176          */
177 };
178
179 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
180         (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
181         (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
182         NULL,
183 };
184
185 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
186         (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
187         (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
188         NULL,
189 };
190
191 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
192         (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
193         (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
194         (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
195         NULL,
196 };
197
198 void uvc_set_trace_param(unsigned int trace)
199 {
200         uvc_gadget_trace_param = trace;
201 }
202 EXPORT_SYMBOL(uvc_set_trace_param);
203
204 /* --------------------------------------------------------------------------
205  * Control requests
206  */
207
208 static void
209 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
210 {
211         struct uvc_device *uvc = req->context;
212         struct v4l2_event v4l2_event;
213         struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
214
215         if (uvc->event_setup_out) {
216                 uvc->event_setup_out = 0;
217
218                 memset(&v4l2_event, 0, sizeof(v4l2_event));
219                 v4l2_event.type = UVC_EVENT_DATA;
220                 uvc_event->data.length = req->actual;
221                 memcpy(&uvc_event->data.data, req->buf, req->actual);
222                 v4l2_event_queue(&uvc->vdev, &v4l2_event);
223         }
224 }
225
226 static int
227 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
228 {
229         struct uvc_device *uvc = to_uvc(f);
230         struct v4l2_event v4l2_event;
231         struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
232
233         /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
234          *      ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
235          *      le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
236          */
237
238         if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
239                 INFO(f->config->cdev, "invalid request type\n");
240                 return -EINVAL;
241         }
242
243         /* Stall too big requests. */
244         if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
245                 return -EINVAL;
246
247         /* Tell the complete callback to generate an event for the next request
248          * that will be enqueued by UVCIOC_SEND_RESPONSE.
249          */
250         uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
251         uvc->event_length = le16_to_cpu(ctrl->wLength);
252
253         memset(&v4l2_event, 0, sizeof(v4l2_event));
254         v4l2_event.type = UVC_EVENT_SETUP;
255         memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
256         v4l2_event_queue(&uvc->vdev, &v4l2_event);
257
258         return 0;
259 }
260
261 void uvc_function_setup_continue(struct uvc_device *uvc)
262 {
263         struct usb_composite_dev *cdev = uvc->func.config->cdev;
264
265         usb_composite_setup_continue(cdev);
266 }
267
268 static int
269 uvc_function_get_alt(struct usb_function *f, unsigned interface)
270 {
271         struct uvc_device *uvc = to_uvc(f);
272
273         INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
274
275         if (interface == uvc->control_intf)
276                 return 0;
277         else if (interface != uvc->streaming_intf)
278                 return -EINVAL;
279         else
280                 return uvc->video.ep->enabled ? 1 : 0;
281 }
282
283 static int
284 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
285 {
286         struct uvc_device *uvc = to_uvc(f);
287         struct usb_composite_dev *cdev = f->config->cdev;
288         struct v4l2_event v4l2_event;
289         struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
290         int ret;
291
292         INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
293
294         if (interface == uvc->control_intf) {
295                 if (alt)
296                         return -EINVAL;
297
298                 INFO(cdev, "reset UVC Control\n");
299                 usb_ep_disable(uvc->control_ep);
300
301                 if (!uvc->control_ep->desc)
302                         if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
303                                 return -EINVAL;
304
305                 usb_ep_enable(uvc->control_ep);
306
307                 if (uvc->state == UVC_STATE_DISCONNECTED) {
308                         memset(&v4l2_event, 0, sizeof(v4l2_event));
309                         v4l2_event.type = UVC_EVENT_CONNECT;
310                         uvc_event->speed = cdev->gadget->speed;
311                         v4l2_event_queue(&uvc->vdev, &v4l2_event);
312
313                         uvc->state = UVC_STATE_CONNECTED;
314                 }
315
316                 return 0;
317         }
318
319         if (interface != uvc->streaming_intf)
320                 return -EINVAL;
321
322         /* TODO
323         if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
324                 return alt ? -EINVAL : 0;
325         */
326
327         switch (alt) {
328         case 0:
329                 if (uvc->state != UVC_STATE_STREAMING)
330                         return 0;
331
332                 if (uvc->video.ep)
333                         usb_ep_disable(uvc->video.ep);
334
335                 memset(&v4l2_event, 0, sizeof(v4l2_event));
336                 v4l2_event.type = UVC_EVENT_STREAMOFF;
337                 v4l2_event_queue(&uvc->vdev, &v4l2_event);
338
339                 uvc->state = UVC_STATE_CONNECTED;
340                 return 0;
341
342         case 1:
343                 if (uvc->state != UVC_STATE_CONNECTED)
344                         return 0;
345
346                 if (!uvc->video.ep)
347                         return -EINVAL;
348
349                 INFO(cdev, "reset UVC\n");
350                 usb_ep_disable(uvc->video.ep);
351
352                 ret = config_ep_by_speed(f->config->cdev->gadget,
353                                 &(uvc->func), uvc->video.ep);
354                 if (ret)
355                         return ret;
356                 usb_ep_enable(uvc->video.ep);
357
358                 memset(&v4l2_event, 0, sizeof(v4l2_event));
359                 v4l2_event.type = UVC_EVENT_STREAMON;
360                 v4l2_event_queue(&uvc->vdev, &v4l2_event);
361                 return USB_GADGET_DELAYED_STATUS;
362
363         default:
364                 return -EINVAL;
365         }
366 }
367
368 static void
369 uvc_function_disable(struct usb_function *f)
370 {
371         struct uvc_device *uvc = to_uvc(f);
372         struct v4l2_event v4l2_event;
373
374         INFO(f->config->cdev, "uvc_function_disable\n");
375
376         memset(&v4l2_event, 0, sizeof(v4l2_event));
377         v4l2_event.type = UVC_EVENT_DISCONNECT;
378         v4l2_event_queue(&uvc->vdev, &v4l2_event);
379
380         uvc->state = UVC_STATE_DISCONNECTED;
381
382         usb_ep_disable(uvc->video.ep);
383         usb_ep_disable(uvc->control_ep);
384 }
385
386 /* --------------------------------------------------------------------------
387  * Connection / disconnection
388  */
389
390 void
391 uvc_function_connect(struct uvc_device *uvc)
392 {
393         struct usb_composite_dev *cdev = uvc->func.config->cdev;
394         int ret;
395
396         if ((ret = usb_function_activate(&uvc->func)) < 0)
397                 INFO(cdev, "UVC connect failed with %d\n", ret);
398 }
399
400 void
401 uvc_function_disconnect(struct uvc_device *uvc)
402 {
403         struct usb_composite_dev *cdev = uvc->func.config->cdev;
404         int ret;
405
406         if ((ret = usb_function_deactivate(&uvc->func)) < 0)
407                 INFO(cdev, "UVC disconnect failed with %d\n", ret);
408 }
409
410 /* --------------------------------------------------------------------------
411  * USB probe and disconnect
412  */
413
414 static int
415 uvc_register_video(struct uvc_device *uvc)
416 {
417         struct usb_composite_dev *cdev = uvc->func.config->cdev;
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         strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
427
428         video_set_drvdata(&uvc->vdev, uvc);
429
430         return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
431 }
432
433 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
434         do { \
435                 memcpy(mem, desc, (desc)->bLength); \
436                 *(dst)++ = mem; \
437                 mem += (desc)->bLength; \
438         } while (0);
439
440 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
441         do { \
442                 const struct usb_descriptor_header * const *__src; \
443                 for (__src = src; *__src; ++__src) { \
444                         memcpy(mem, *__src, (*__src)->bLength); \
445                         *dst++ = mem; \
446                         mem += (*__src)->bLength; \
447                 } \
448         } while (0)
449
450 static struct usb_descriptor_header **
451 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
452 {
453         struct uvc_input_header_descriptor *uvc_streaming_header;
454         struct uvc_header_descriptor *uvc_control_header;
455         const struct uvc_descriptor_header * const *uvc_control_desc;
456         const struct uvc_descriptor_header * const *uvc_streaming_cls;
457         const struct usb_descriptor_header * const *uvc_streaming_std;
458         const struct usb_descriptor_header * const *src;
459         struct usb_descriptor_header **dst;
460         struct usb_descriptor_header **hdr;
461         unsigned int control_size;
462         unsigned int streaming_size;
463         unsigned int n_desc;
464         unsigned int bytes;
465         void *mem;
466
467         switch (speed) {
468         case USB_SPEED_SUPER:
469                 uvc_control_desc = uvc->desc.ss_control;
470                 uvc_streaming_cls = uvc->desc.ss_streaming;
471                 uvc_streaming_std = uvc_ss_streaming;
472                 break;
473
474         case USB_SPEED_HIGH:
475                 uvc_control_desc = uvc->desc.fs_control;
476                 uvc_streaming_cls = uvc->desc.hs_streaming;
477                 uvc_streaming_std = uvc_hs_streaming;
478                 break;
479
480         case USB_SPEED_FULL:
481         default:
482                 uvc_control_desc = uvc->desc.fs_control;
483                 uvc_streaming_cls = uvc->desc.fs_streaming;
484                 uvc_streaming_std = uvc_fs_streaming;
485                 break;
486         }
487
488         if (!uvc_control_desc || !uvc_streaming_cls)
489                 return ERR_PTR(-ENODEV);
490
491         /* Descriptors layout
492          *
493          * uvc_iad
494          * uvc_control_intf
495          * Class-specific UVC control descriptors
496          * uvc_control_ep
497          * uvc_control_cs_ep
498          * uvc_ss_control_comp (for SS only)
499          * uvc_streaming_intf_alt0
500          * Class-specific UVC streaming descriptors
501          * uvc_{fs|hs}_streaming
502          */
503
504         /* Count descriptors and compute their size. */
505         control_size = 0;
506         streaming_size = 0;
507         bytes = uvc_iad.bLength + uvc_control_intf.bLength
508               + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
509               + uvc_streaming_intf_alt0.bLength;
510
511         if (speed == USB_SPEED_SUPER) {
512                 bytes += uvc_ss_control_comp.bLength;
513                 n_desc = 6;
514         } else {
515                 n_desc = 5;
516         }
517
518         for (src = (const struct usb_descriptor_header **)uvc_control_desc;
519              *src; ++src) {
520                 control_size += (*src)->bLength;
521                 bytes += (*src)->bLength;
522                 n_desc++;
523         }
524         for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
525              *src; ++src) {
526                 streaming_size += (*src)->bLength;
527                 bytes += (*src)->bLength;
528                 n_desc++;
529         }
530         for (src = uvc_streaming_std; *src; ++src) {
531                 bytes += (*src)->bLength;
532                 n_desc++;
533         }
534
535         mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
536         if (mem == NULL)
537                 return NULL;
538
539         hdr = mem;
540         dst = mem;
541         mem += (n_desc + 1) * sizeof(*src);
542
543         /* Copy the descriptors. */
544         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
545         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
546
547         uvc_control_header = mem;
548         UVC_COPY_DESCRIPTORS(mem, dst,
549                 (const struct usb_descriptor_header **)uvc_control_desc);
550         uvc_control_header->wTotalLength = cpu_to_le16(control_size);
551         uvc_control_header->bInCollection = 1;
552         uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
553
554         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
555         if (speed == USB_SPEED_SUPER)
556                 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
557
558         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
559         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
560
561         uvc_streaming_header = mem;
562         UVC_COPY_DESCRIPTORS(mem, dst,
563                 (const struct usb_descriptor_header**)uvc_streaming_cls);
564         uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
565         uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
566
567         UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
568
569         *dst = NULL;
570         return hdr;
571 }
572
573 static int
574 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
575 {
576         struct usb_composite_dev *cdev = c->cdev;
577         struct uvc_device *uvc = to_uvc(f);
578         struct usb_string *us;
579         unsigned int max_packet_mult;
580         unsigned int max_packet_size;
581         struct usb_ep *ep;
582         struct f_uvc_opts *opts;
583         int ret = -EINVAL;
584
585         INFO(cdev, "uvc_function_bind\n");
586
587         opts = fi_to_f_uvc_opts(f->fi);
588         /* Sanity check the streaming endpoint module parameters.
589          */
590         opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
591         opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
592         opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
593
594         /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
595         if (opts->streaming_maxburst &&
596             (opts->streaming_maxpacket % 1024) != 0) {
597                 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
598                 INFO(cdev, "overriding streaming_maxpacket to %d\n",
599                      opts->streaming_maxpacket);
600         }
601
602         /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
603          * module parameters.
604          *
605          * NOTE: We assume that the user knows what they are doing and won't
606          * give parameters that their UDC doesn't support.
607          */
608         if (opts->streaming_maxpacket <= 1024) {
609                 max_packet_mult = 1;
610                 max_packet_size = opts->streaming_maxpacket;
611         } else if (opts->streaming_maxpacket <= 2048) {
612                 max_packet_mult = 2;
613                 max_packet_size = opts->streaming_maxpacket / 2;
614         } else {
615                 max_packet_mult = 3;
616                 max_packet_size = opts->streaming_maxpacket / 3;
617         }
618
619         uvc_fs_streaming_ep.wMaxPacketSize =
620                 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
621         uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
622
623         uvc_hs_streaming_ep.wMaxPacketSize =
624                 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
625         uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
626
627         uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
628         uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
629         uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
630         uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
631         uvc_ss_streaming_comp.wBytesPerInterval =
632                 cpu_to_le16(max_packet_size * max_packet_mult *
633                             (opts->streaming_maxburst + 1));
634
635         /* Allocate endpoints. */
636         ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
637         if (!ep) {
638                 INFO(cdev, "Unable to allocate control EP\n");
639                 goto error;
640         }
641         uvc->control_ep = ep;
642
643         if (gadget_is_superspeed(c->cdev->gadget))
644                 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
645                                           &uvc_ss_streaming_comp);
646         else if (gadget_is_dualspeed(cdev->gadget))
647                 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
648         else
649                 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
650
651         if (!ep) {
652                 INFO(cdev, "Unable to allocate streaming EP\n");
653                 goto error;
654         }
655         uvc->video.ep = ep;
656
657         uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
658         uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
659         uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
660
661         us = usb_gstrings_attach(cdev, uvc_function_strings,
662                                  ARRAY_SIZE(uvc_en_us_strings));
663         if (IS_ERR(us)) {
664                 ret = PTR_ERR(us);
665                 goto error;
666         }
667         uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
668         uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
669         ret = us[UVC_STRING_STREAMING_IDX].id;
670         uvc_streaming_intf_alt0.iInterface = ret;
671         uvc_streaming_intf_alt1.iInterface = ret;
672
673         /* Allocate interface IDs. */
674         if ((ret = usb_interface_id(c, f)) < 0)
675                 goto error;
676         uvc_iad.bFirstInterface = ret;
677         uvc_control_intf.bInterfaceNumber = ret;
678         uvc->control_intf = ret;
679
680         if ((ret = usb_interface_id(c, f)) < 0)
681                 goto error;
682         uvc_streaming_intf_alt0.bInterfaceNumber = ret;
683         uvc_streaming_intf_alt1.bInterfaceNumber = ret;
684         uvc->streaming_intf = ret;
685
686         /* Copy descriptors */
687         f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
688         if (IS_ERR(f->fs_descriptors)) {
689                 ret = PTR_ERR(f->fs_descriptors);
690                 f->fs_descriptors = NULL;
691                 goto error;
692         }
693         if (gadget_is_dualspeed(cdev->gadget)) {
694                 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
695                 if (IS_ERR(f->hs_descriptors)) {
696                         ret = PTR_ERR(f->hs_descriptors);
697                         f->hs_descriptors = NULL;
698                         goto error;
699                 }
700         }
701         if (gadget_is_superspeed(c->cdev->gadget)) {
702                 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
703                 if (IS_ERR(f->ss_descriptors)) {
704                         ret = PTR_ERR(f->ss_descriptors);
705                         f->ss_descriptors = NULL;
706                         goto error;
707                 }
708         }
709
710         /* Preallocate control endpoint request. */
711         uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
712         uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
713         if (uvc->control_req == NULL || uvc->control_buf == NULL) {
714                 ret = -ENOMEM;
715                 goto error;
716         }
717
718         uvc->control_req->buf = uvc->control_buf;
719         uvc->control_req->complete = uvc_function_ep0_complete;
720         uvc->control_req->context = uvc;
721
722         if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
723                 printk(KERN_INFO "v4l2_device_register failed\n");
724                 goto error;
725         }
726
727         /* Initialise video. */
728         ret = uvcg_video_init(&uvc->video);
729         if (ret < 0)
730                 goto error;
731
732         /* Register a V4L2 device. */
733         ret = uvc_register_video(uvc);
734         if (ret < 0) {
735                 printk(KERN_INFO "Unable to register video device\n");
736                 goto error;
737         }
738
739         return 0;
740
741 error:
742         v4l2_device_unregister(&uvc->v4l2_dev);
743
744         if (uvc->control_req)
745                 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
746         kfree(uvc->control_buf);
747
748         usb_free_all_descriptors(f);
749         return ret;
750 }
751
752 /* --------------------------------------------------------------------------
753  * USB gadget function
754  */
755
756 static void uvc_free_inst(struct usb_function_instance *f)
757 {
758         struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
759
760         mutex_destroy(&opts->lock);
761         kfree(opts);
762 }
763
764 static struct usb_function_instance *uvc_alloc_inst(void)
765 {
766         struct f_uvc_opts *opts;
767         struct uvc_camera_terminal_descriptor *cd;
768         struct uvc_processing_unit_descriptor *pd;
769         struct uvc_output_terminal_descriptor *od;
770         struct uvc_color_matching_descriptor *md;
771         struct uvc_descriptor_header **ctl_cls;
772
773         opts = kzalloc(sizeof(*opts), GFP_KERNEL);
774         if (!opts)
775                 return ERR_PTR(-ENOMEM);
776         opts->func_inst.free_func_inst = uvc_free_inst;
777         mutex_init(&opts->lock);
778
779         cd = &opts->uvc_camera_terminal;
780         cd->bLength                     = UVC_DT_CAMERA_TERMINAL_SIZE(3);
781         cd->bDescriptorType             = USB_DT_CS_INTERFACE;
782         cd->bDescriptorSubType          = UVC_VC_INPUT_TERMINAL;
783         cd->bTerminalID                 = 1;
784         cd->wTerminalType               = cpu_to_le16(0x0201);
785         cd->bAssocTerminal              = 0;
786         cd->iTerminal                   = 0;
787         cd->wObjectiveFocalLengthMin    = cpu_to_le16(0);
788         cd->wObjectiveFocalLengthMax    = cpu_to_le16(0);
789         cd->wOcularFocalLength          = cpu_to_le16(0);
790         cd->bControlSize                = 3;
791         cd->bmControls[0]               = 2;
792         cd->bmControls[1]               = 0;
793         cd->bmControls[2]               = 0;
794
795         pd = &opts->uvc_processing;
796         pd->bLength                     = UVC_DT_PROCESSING_UNIT_SIZE(2);
797         pd->bDescriptorType             = USB_DT_CS_INTERFACE;
798         pd->bDescriptorSubType          = UVC_VC_PROCESSING_UNIT;
799         pd->bUnitID                     = 2;
800         pd->bSourceID                   = 1;
801         pd->wMaxMultiplier              = cpu_to_le16(16*1024);
802         pd->bControlSize                = 2;
803         pd->bmControls[0]               = 1;
804         pd->bmControls[1]               = 0;
805         pd->iProcessing                 = 0;
806
807         od = &opts->uvc_output_terminal;
808         od->bLength                     = UVC_DT_OUTPUT_TERMINAL_SIZE;
809         od->bDescriptorType             = USB_DT_CS_INTERFACE;
810         od->bDescriptorSubType          = UVC_VC_OUTPUT_TERMINAL;
811         od->bTerminalID                 = 3;
812         od->wTerminalType               = cpu_to_le16(0x0101);
813         od->bAssocTerminal              = 0;
814         od->bSourceID                   = 2;
815         od->iTerminal                   = 0;
816
817         md = &opts->uvc_color_matching;
818         md->bLength                     = UVC_DT_COLOR_MATCHING_SIZE;
819         md->bDescriptorType             = USB_DT_CS_INTERFACE;
820         md->bDescriptorSubType          = UVC_VS_COLORFORMAT;
821         md->bColorPrimaries             = 1;
822         md->bTransferCharacteristics    = 1;
823         md->bMatrixCoefficients         = 4;
824
825         /* Prepare fs control class descriptors for configfs-based gadgets */
826         ctl_cls = opts->uvc_fs_control_cls;
827         ctl_cls[0] = NULL;      /* assigned elsewhere by configfs */
828         ctl_cls[1] = (struct uvc_descriptor_header *)cd;
829         ctl_cls[2] = (struct uvc_descriptor_header *)pd;
830         ctl_cls[3] = (struct uvc_descriptor_header *)od;
831         ctl_cls[4] = NULL;      /* NULL-terminate */
832         opts->fs_control =
833                 (const struct uvc_descriptor_header * const *)ctl_cls;
834
835         /* Prepare hs control class descriptors for configfs-based gadgets */
836         ctl_cls = opts->uvc_ss_control_cls;
837         ctl_cls[0] = NULL;      /* assigned elsewhere by configfs */
838         ctl_cls[1] = (struct uvc_descriptor_header *)cd;
839         ctl_cls[2] = (struct uvc_descriptor_header *)pd;
840         ctl_cls[3] = (struct uvc_descriptor_header *)od;
841         ctl_cls[4] = NULL;      /* NULL-terminate */
842         opts->ss_control =
843                 (const struct uvc_descriptor_header * const *)ctl_cls;
844
845         opts->streaming_interval = 1;
846         opts->streaming_maxpacket = 1024;
847
848         uvcg_attach_configfs(opts);
849         return &opts->func_inst;
850 }
851
852 static void uvc_free(struct usb_function *f)
853 {
854         struct uvc_device *uvc = to_uvc(f);
855         struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
856                                                func_inst);
857         --opts->refcnt;
858         kfree(uvc);
859 }
860
861 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
862 {
863         struct usb_composite_dev *cdev = c->cdev;
864         struct uvc_device *uvc = to_uvc(f);
865
866         INFO(cdev, "%s\n", __func__);
867
868         video_unregister_device(&uvc->vdev);
869         v4l2_device_unregister(&uvc->v4l2_dev);
870
871         usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
872         kfree(uvc->control_buf);
873
874         usb_free_all_descriptors(f);
875 }
876
877 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
878 {
879         struct uvc_device *uvc;
880         struct f_uvc_opts *opts;
881         struct uvc_descriptor_header **strm_cls;
882
883         uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
884         if (uvc == NULL)
885                 return ERR_PTR(-ENOMEM);
886
887         mutex_init(&uvc->video.mutex);
888         uvc->state = UVC_STATE_DISCONNECTED;
889         opts = fi_to_f_uvc_opts(fi);
890
891         mutex_lock(&opts->lock);
892         if (opts->uvc_fs_streaming_cls) {
893                 strm_cls = opts->uvc_fs_streaming_cls;
894                 opts->fs_streaming =
895                         (const struct uvc_descriptor_header * const *)strm_cls;
896         }
897         if (opts->uvc_hs_streaming_cls) {
898                 strm_cls = opts->uvc_hs_streaming_cls;
899                 opts->hs_streaming =
900                         (const struct uvc_descriptor_header * const *)strm_cls;
901         }
902         if (opts->uvc_ss_streaming_cls) {
903                 strm_cls = opts->uvc_ss_streaming_cls;
904                 opts->ss_streaming =
905                         (const struct uvc_descriptor_header * const *)strm_cls;
906         }
907
908         uvc->desc.fs_control = opts->fs_control;
909         uvc->desc.ss_control = opts->ss_control;
910         uvc->desc.fs_streaming = opts->fs_streaming;
911         uvc->desc.hs_streaming = opts->hs_streaming;
912         uvc->desc.ss_streaming = opts->ss_streaming;
913         ++opts->refcnt;
914         mutex_unlock(&opts->lock);
915
916         /* Register the function. */
917         uvc->func.name = "uvc";
918         uvc->func.bind = uvc_function_bind;
919         uvc->func.unbind = uvc_unbind;
920         uvc->func.get_alt = uvc_function_get_alt;
921         uvc->func.set_alt = uvc_function_set_alt;
922         uvc->func.disable = uvc_function_disable;
923         uvc->func.setup = uvc_function_setup;
924         uvc->func.free_func = uvc_free;
925         uvc->func.bind_deactivated = true;
926
927         return &uvc->func;
928 }
929
930 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
931 MODULE_LICENSE("GPL");
932 MODULE_AUTHOR("Laurent Pinchart");