packaging: install license for rpm package instead of license package
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / usb / gadget / f_uvc.c
1 /*
2  *      uvc_gadget.c  --  USB Video Class Gadget driver
3  *
4  *      Copyright (C) 2009-2010
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/fs.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/string.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/video.h>
23 #include <linux/vmalloc.h>
24 #include <linux/wait.h>
25
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-event.h>
28
29 #include "uvc.h"
30
31 unsigned int uvc_gadget_trace_param;
32
33 /*-------------------------------------------------------------------------*/
34
35 /* module parameters specific to the Video streaming endpoint */
36 static unsigned int streaming_interval = 1;
37 module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
38 MODULE_PARM_DESC(streaming_interval, "1 - 16");
39
40 static unsigned int streaming_maxpacket = 1024;
41 module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
42 MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
43
44 static unsigned int streaming_maxburst;
45 module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
46 MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
47
48 /* --------------------------------------------------------------------------
49  * Function descriptors
50  */
51
52 /* string IDs are assigned dynamically */
53
54 #define UVC_STRING_CONTROL_IDX                  0
55 #define UVC_STRING_STREAMING_IDX                1
56
57 static struct usb_string uvc_en_us_strings[] = {
58         [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
59         [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
60         {  }
61 };
62
63 static struct usb_gadget_strings uvc_stringtab = {
64         .language = 0x0409,     /* en-us */
65         .strings = uvc_en_us_strings,
66 };
67
68 static struct usb_gadget_strings *uvc_function_strings[] = {
69         &uvc_stringtab,
70         NULL,
71 };
72
73 #define UVC_INTF_VIDEO_CONTROL                  0
74 #define UVC_INTF_VIDEO_STREAMING                1
75
76 #define UVC_STATUS_MAX_PACKET_SIZE              16      /* 16 bytes status */
77
78 static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
79         .bLength                = sizeof(uvc_iad),
80         .bDescriptorType        = USB_DT_INTERFACE_ASSOCIATION,
81         .bFirstInterface        = 0,
82         .bInterfaceCount        = 2,
83         .bFunctionClass         = USB_CLASS_VIDEO,
84         .bFunctionSubClass      = UVC_SC_VIDEO_INTERFACE_COLLECTION,
85         .bFunctionProtocol      = 0x00,
86         .iFunction              = 0,
87 };
88
89 static struct usb_interface_descriptor uvc_control_intf __initdata = {
90         .bLength                = USB_DT_INTERFACE_SIZE,
91         .bDescriptorType        = USB_DT_INTERFACE,
92         .bInterfaceNumber       = UVC_INTF_VIDEO_CONTROL,
93         .bAlternateSetting      = 0,
94         .bNumEndpoints          = 1,
95         .bInterfaceClass        = USB_CLASS_VIDEO,
96         .bInterfaceSubClass     = UVC_SC_VIDEOCONTROL,
97         .bInterfaceProtocol     = 0x00,
98         .iInterface             = 0,
99 };
100
101 static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
102         .bLength                = USB_DT_ENDPOINT_SIZE,
103         .bDescriptorType        = USB_DT_ENDPOINT,
104         .bEndpointAddress       = USB_DIR_IN,
105         .bmAttributes           = USB_ENDPOINT_XFER_INT,
106         .wMaxPacketSize         = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
107         .bInterval              = 8,
108 };
109
110 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
111         .bLength                = sizeof(uvc_ss_control_comp),
112         .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
113         /* The following 3 values can be tweaked if necessary. */
114         .bMaxBurst              = 0,
115         .bmAttributes           = 0,
116         .wBytesPerInterval      = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
117 };
118
119 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
120         .bLength                = UVC_DT_CONTROL_ENDPOINT_SIZE,
121         .bDescriptorType        = USB_DT_CS_ENDPOINT,
122         .bDescriptorSubType     = UVC_EP_INTERRUPT,
123         .wMaxTransferSize       = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
124 };
125
126 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
127         .bLength                = USB_DT_INTERFACE_SIZE,
128         .bDescriptorType        = USB_DT_INTERFACE,
129         .bInterfaceNumber       = UVC_INTF_VIDEO_STREAMING,
130         .bAlternateSetting      = 0,
131         .bNumEndpoints          = 0,
132         .bInterfaceClass        = USB_CLASS_VIDEO,
133         .bInterfaceSubClass     = UVC_SC_VIDEOSTREAMING,
134         .bInterfaceProtocol     = 0x00,
135         .iInterface             = 0,
136 };
137
138 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
139         .bLength                = USB_DT_INTERFACE_SIZE,
140         .bDescriptorType        = USB_DT_INTERFACE,
141         .bInterfaceNumber       = UVC_INTF_VIDEO_STREAMING,
142         .bAlternateSetting      = 1,
143         .bNumEndpoints          = 1,
144         .bInterfaceClass        = USB_CLASS_VIDEO,
145         .bInterfaceSubClass     = UVC_SC_VIDEOSTREAMING,
146         .bInterfaceProtocol     = 0x00,
147         .iInterface             = 0,
148 };
149
150 static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
151         .bLength                = USB_DT_ENDPOINT_SIZE,
152         .bDescriptorType        = USB_DT_ENDPOINT,
153         .bEndpointAddress       = USB_DIR_IN,
154         .bmAttributes           = USB_ENDPOINT_SYNC_ASYNC
155                                 | USB_ENDPOINT_XFER_ISOC,
156         /* The wMaxPacketSize and bInterval values will be initialized from
157          * module parameters.
158          */
159         .wMaxPacketSize         = 0,
160         .bInterval              = 0,
161 };
162
163 static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
164         .bLength                = USB_DT_ENDPOINT_SIZE,
165         .bDescriptorType        = USB_DT_ENDPOINT,
166         .bEndpointAddress       = USB_DIR_IN,
167         .bmAttributes           = USB_ENDPOINT_SYNC_ASYNC
168                                 | USB_ENDPOINT_XFER_ISOC,
169         /* The wMaxPacketSize and bInterval values will be initialized from
170          * module parameters.
171          */
172         .wMaxPacketSize         = 0,
173         .bInterval              = 0,
174 };
175
176 static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
177         .bLength                = USB_DT_ENDPOINT_SIZE,
178         .bDescriptorType        = USB_DT_ENDPOINT,
179
180         .bEndpointAddress       = USB_DIR_IN,
181         .bmAttributes           = USB_ENDPOINT_SYNC_ASYNC
182                                 | USB_ENDPOINT_XFER_ISOC,
183         /* The wMaxPacketSize and bInterval values will be initialized from
184          * module parameters.
185          */
186         .wMaxPacketSize         = 0,
187         .bInterval              = 0,
188 };
189
190 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
191         .bLength                = sizeof(uvc_ss_streaming_comp),
192         .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
193         /* The following 3 values can be tweaked if necessary. */
194         .bMaxBurst              = 0,
195         .bmAttributes           = 0,
196         .wBytesPerInterval      = cpu_to_le16(1024),
197 };
198
199 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
200         (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
201         (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
202         NULL,
203 };
204
205 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
206         (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
207         (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
208         NULL,
209 };
210
211 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
212         (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
213         (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
214         (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
215         NULL,
216 };
217
218 /* --------------------------------------------------------------------------
219  * Control requests
220  */
221
222 static void
223 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
224 {
225         struct uvc_device *uvc = req->context;
226         struct v4l2_event v4l2_event;
227         struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
228
229         if (uvc->event_setup_out) {
230                 uvc->event_setup_out = 0;
231
232                 memset(&v4l2_event, 0, sizeof(v4l2_event));
233                 v4l2_event.type = UVC_EVENT_DATA;
234                 uvc_event->data.length = req->actual;
235                 memcpy(&uvc_event->data.data, req->buf, req->actual);
236                 v4l2_event_queue(uvc->vdev, &v4l2_event);
237         }
238 }
239
240 static int
241 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
242 {
243         struct uvc_device *uvc = to_uvc(f);
244         struct v4l2_event v4l2_event;
245         struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
246
247         /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
248          *      ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
249          *      le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
250          */
251
252         if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
253                 INFO(f->config->cdev, "invalid request type\n");
254                 return -EINVAL;
255         }
256
257         /* Stall too big requests. */
258         if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
259                 return -EINVAL;
260
261         memset(&v4l2_event, 0, sizeof(v4l2_event));
262         v4l2_event.type = UVC_EVENT_SETUP;
263         memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
264         v4l2_event_queue(uvc->vdev, &v4l2_event);
265
266         return 0;
267 }
268
269 void uvc_function_setup_continue(struct uvc_device *uvc)
270 {
271         struct usb_composite_dev *cdev = uvc->func.config->cdev;
272
273         usb_composite_setup_continue(cdev);
274 }
275
276 static int
277 uvc_function_get_alt(struct usb_function *f, unsigned interface)
278 {
279         struct uvc_device *uvc = to_uvc(f);
280
281         INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
282
283         if (interface == uvc->control_intf)
284                 return 0;
285         else if (interface != uvc->streaming_intf)
286                 return -EINVAL;
287         else
288                 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
289 }
290
291 static int
292 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
293 {
294         struct uvc_device *uvc = to_uvc(f);
295         struct v4l2_event v4l2_event;
296         struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
297         int ret;
298
299         INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
300
301         if (interface == uvc->control_intf) {
302                 if (alt)
303                         return -EINVAL;
304
305                 if (uvc->state == UVC_STATE_DISCONNECTED) {
306                         memset(&v4l2_event, 0, sizeof(v4l2_event));
307                         v4l2_event.type = UVC_EVENT_CONNECT;
308                         uvc_event->speed = f->config->cdev->gadget->speed;
309                         v4l2_event_queue(uvc->vdev, &v4l2_event);
310
311                         uvc->state = UVC_STATE_CONNECTED;
312                 }
313
314                 return 0;
315         }
316
317         if (interface != uvc->streaming_intf)
318                 return -EINVAL;
319
320         /* TODO
321         if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
322                 return alt ? -EINVAL : 0;
323         */
324
325         switch (alt) {
326         case 0:
327                 if (uvc->state != UVC_STATE_STREAMING)
328                         return 0;
329
330                 if (uvc->video.ep)
331                         usb_ep_disable(uvc->video.ep);
332
333                 memset(&v4l2_event, 0, sizeof(v4l2_event));
334                 v4l2_event.type = UVC_EVENT_STREAMOFF;
335                 v4l2_event_queue(uvc->vdev, &v4l2_event);
336
337                 uvc->state = UVC_STATE_CONNECTED;
338                 return 0;
339
340         case 1:
341                 if (uvc->state != UVC_STATE_CONNECTED)
342                         return 0;
343
344                 if (uvc->video.ep) {
345                         ret = config_ep_by_speed(f->config->cdev->gadget,
346                                         &(uvc->func), uvc->video.ep);
347                         if (ret)
348                                 return ret;
349                         usb_ep_enable(uvc->video.ep);
350                 }
351
352                 memset(&v4l2_event, 0, sizeof(v4l2_event));
353                 v4l2_event.type = UVC_EVENT_STREAMON;
354                 v4l2_event_queue(uvc->vdev, &v4l2_event);
355                 return USB_GADGET_DELAYED_STATUS;
356
357         default:
358                 return -EINVAL;
359         }
360 }
361
362 static void
363 uvc_function_disable(struct usb_function *f)
364 {
365         struct uvc_device *uvc = to_uvc(f);
366         struct v4l2_event v4l2_event;
367
368         INFO(f->config->cdev, "uvc_function_disable\n");
369
370         memset(&v4l2_event, 0, sizeof(v4l2_event));
371         v4l2_event.type = UVC_EVENT_DISCONNECT;
372         v4l2_event_queue(uvc->vdev, &v4l2_event);
373
374         uvc->state = UVC_STATE_DISCONNECTED;
375 }
376
377 /* --------------------------------------------------------------------------
378  * Connection / disconnection
379  */
380
381 void
382 uvc_function_connect(struct uvc_device *uvc)
383 {
384         struct usb_composite_dev *cdev = uvc->func.config->cdev;
385         int ret;
386
387         if ((ret = usb_function_activate(&uvc->func)) < 0)
388                 INFO(cdev, "UVC connect failed with %d\n", ret);
389 }
390
391 void
392 uvc_function_disconnect(struct uvc_device *uvc)
393 {
394         struct usb_composite_dev *cdev = uvc->func.config->cdev;
395         int ret;
396
397         if ((ret = usb_function_deactivate(&uvc->func)) < 0)
398                 INFO(cdev, "UVC disconnect failed with %d\n", ret);
399 }
400
401 /* --------------------------------------------------------------------------
402  * USB probe and disconnect
403  */
404
405 static int
406 uvc_register_video(struct uvc_device *uvc)
407 {
408         struct usb_composite_dev *cdev = uvc->func.config->cdev;
409         struct video_device *video;
410
411         /* TODO reference counting. */
412         video = video_device_alloc();
413         if (video == NULL)
414                 return -ENOMEM;
415
416         video->parent = &cdev->gadget->dev;
417         video->fops = &uvc_v4l2_fops;
418         video->release = video_device_release;
419         strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
420
421         uvc->vdev = video;
422         video_set_drvdata(video, uvc);
423
424         return video_register_device(video, VFL_TYPE_GRABBER, -1);
425 }
426
427 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
428         do { \
429                 memcpy(mem, desc, (desc)->bLength); \
430                 *(dst)++ = mem; \
431                 mem += (desc)->bLength; \
432         } while (0);
433
434 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
435         do { \
436                 const struct usb_descriptor_header * const *__src; \
437                 for (__src = src; *__src; ++__src) { \
438                         memcpy(mem, *__src, (*__src)->bLength); \
439                         *dst++ = mem; \
440                         mem += (*__src)->bLength; \
441                 } \
442         } while (0)
443
444 static struct usb_descriptor_header ** __init
445 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
446 {
447         struct uvc_input_header_descriptor *uvc_streaming_header;
448         struct uvc_header_descriptor *uvc_control_header;
449         const struct uvc_descriptor_header * const *uvc_control_desc;
450         const struct uvc_descriptor_header * const *uvc_streaming_cls;
451         const struct usb_descriptor_header * const *uvc_streaming_std;
452         const struct usb_descriptor_header * const *src;
453         struct usb_descriptor_header **dst;
454         struct usb_descriptor_header **hdr;
455         unsigned int control_size;
456         unsigned int streaming_size;
457         unsigned int n_desc;
458         unsigned int bytes;
459         void *mem;
460
461         switch (speed) {
462         case USB_SPEED_SUPER:
463                 uvc_control_desc = uvc->desc.ss_control;
464                 uvc_streaming_cls = uvc->desc.ss_streaming;
465                 uvc_streaming_std = uvc_ss_streaming;
466                 break;
467
468         case USB_SPEED_HIGH:
469                 uvc_control_desc = uvc->desc.fs_control;
470                 uvc_streaming_cls = uvc->desc.hs_streaming;
471                 uvc_streaming_std = uvc_hs_streaming;
472                 break;
473
474         case USB_SPEED_FULL:
475         default:
476                 uvc_control_desc = uvc->desc.fs_control;
477                 uvc_streaming_cls = uvc->desc.fs_streaming;
478                 uvc_streaming_std = uvc_fs_streaming;
479                 break;
480         }
481
482         /* Descriptors layout
483          *
484          * uvc_iad
485          * uvc_control_intf
486          * Class-specific UVC control descriptors
487          * uvc_control_ep
488          * uvc_control_cs_ep
489          * uvc_ss_control_comp (for SS only)
490          * uvc_streaming_intf_alt0
491          * Class-specific UVC streaming descriptors
492          * uvc_{fs|hs}_streaming
493          */
494
495         /* Count descriptors and compute their size. */
496         control_size = 0;
497         streaming_size = 0;
498         bytes = uvc_iad.bLength + uvc_control_intf.bLength
499               + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
500               + uvc_streaming_intf_alt0.bLength;
501
502         if (speed == USB_SPEED_SUPER) {
503                 bytes += uvc_ss_control_comp.bLength;
504                 n_desc = 6;
505         } else {
506                 n_desc = 5;
507         }
508
509         for (src = (const struct usb_descriptor_header **)uvc_control_desc;
510              *src; ++src) {
511                 control_size += (*src)->bLength;
512                 bytes += (*src)->bLength;
513                 n_desc++;
514         }
515         for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
516              *src; ++src) {
517                 streaming_size += (*src)->bLength;
518                 bytes += (*src)->bLength;
519                 n_desc++;
520         }
521         for (src = uvc_streaming_std; *src; ++src) {
522                 bytes += (*src)->bLength;
523                 n_desc++;
524         }
525
526         mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
527         if (mem == NULL)
528                 return NULL;
529
530         hdr = mem;
531         dst = mem;
532         mem += (n_desc + 1) * sizeof(*src);
533
534         /* Copy the descriptors. */
535         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
536         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
537
538         uvc_control_header = mem;
539         UVC_COPY_DESCRIPTORS(mem, dst,
540                 (const struct usb_descriptor_header **)uvc_control_desc);
541         uvc_control_header->wTotalLength = cpu_to_le16(control_size);
542         uvc_control_header->bInCollection = 1;
543         uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
544
545         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
546         if (speed == USB_SPEED_SUPER)
547                 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
548
549         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
550         UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
551
552         uvc_streaming_header = mem;
553         UVC_COPY_DESCRIPTORS(mem, dst,
554                 (const struct usb_descriptor_header**)uvc_streaming_cls);
555         uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
556         uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
557
558         UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
559
560         *dst = NULL;
561         return hdr;
562 }
563
564 static void
565 uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
566 {
567         struct usb_composite_dev *cdev = c->cdev;
568         struct uvc_device *uvc = to_uvc(f);
569
570         INFO(cdev, "uvc_function_unbind\n");
571
572         video_unregister_device(uvc->vdev);
573         uvc->control_ep->driver_data = NULL;
574         uvc->video.ep->driver_data = NULL;
575
576         uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
577         usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
578         kfree(uvc->control_buf);
579
580         usb_free_all_descriptors(f);
581
582         kfree(uvc);
583 }
584
585 static int __init
586 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
587 {
588         struct usb_composite_dev *cdev = c->cdev;
589         struct uvc_device *uvc = to_uvc(f);
590         unsigned int max_packet_mult;
591         unsigned int max_packet_size;
592         struct usb_ep *ep;
593         int ret = -EINVAL;
594
595         INFO(cdev, "uvc_function_bind\n");
596
597         /* Sanity check the streaming endpoint module parameters.
598          */
599         streaming_interval = clamp(streaming_interval, 1U, 16U);
600         streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
601         streaming_maxburst = min(streaming_maxburst, 15U);
602
603         /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
604          * module parameters.
605          *
606          * NOTE: We assume that the user knows what they are doing and won't
607          * give parameters that their UDC doesn't support.
608          */
609         if (streaming_maxpacket <= 1024) {
610                 max_packet_mult = 1;
611                 max_packet_size = streaming_maxpacket;
612         } else if (streaming_maxpacket <= 2048) {
613                 max_packet_mult = 2;
614                 max_packet_size = streaming_maxpacket / 2;
615         } else {
616                 max_packet_mult = 3;
617                 max_packet_size = streaming_maxpacket / 3;
618         }
619
620         uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
621         uvc_fs_streaming_ep.bInterval = streaming_interval;
622
623         uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
624         uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
625         uvc_hs_streaming_ep.bInterval = streaming_interval;
626
627         uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
628         uvc_ss_streaming_ep.bInterval = streaming_interval;
629         uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
630         uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
631         uvc_ss_streaming_comp.wBytesPerInterval =
632                 max_packet_size * max_packet_mult * streaming_maxburst;
633
634         /* Allocate endpoints. */
635         ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
636         if (!ep) {
637                 INFO(cdev, "Unable to allocate control EP\n");
638                 goto error;
639         }
640         uvc->control_ep = ep;
641         ep->driver_data = uvc;
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         ep->driver_data = uvc;
657
658         uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
659         uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
660         uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
661
662         /* Allocate interface IDs. */
663         if ((ret = usb_interface_id(c, f)) < 0)
664                 goto error;
665         uvc_iad.bFirstInterface = ret;
666         uvc_control_intf.bInterfaceNumber = ret;
667         uvc->control_intf = ret;
668
669         if ((ret = usb_interface_id(c, f)) < 0)
670                 goto error;
671         uvc_streaming_intf_alt0.bInterfaceNumber = ret;
672         uvc_streaming_intf_alt1.bInterfaceNumber = ret;
673         uvc->streaming_intf = ret;
674
675         /* Copy descriptors */
676         f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
677         if (gadget_is_dualspeed(cdev->gadget))
678                 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
679         if (gadget_is_superspeed(c->cdev->gadget))
680                 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
681
682         /* Preallocate control endpoint request. */
683         uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
684         uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
685         if (uvc->control_req == NULL || uvc->control_buf == NULL) {
686                 ret = -ENOMEM;
687                 goto error;
688         }
689
690         uvc->control_req->buf = uvc->control_buf;
691         uvc->control_req->complete = uvc_function_ep0_complete;
692         uvc->control_req->context = uvc;
693
694         /* Avoid letting this gadget enumerate until the userspace server is
695          * active.
696          */
697         if ((ret = usb_function_deactivate(f)) < 0)
698                 goto error;
699
700         /* Initialise video. */
701         ret = uvc_video_init(&uvc->video);
702         if (ret < 0)
703                 goto error;
704
705         /* Register a V4L2 device. */
706         ret = uvc_register_video(uvc);
707         if (ret < 0) {
708                 printk(KERN_INFO "Unable to register video device\n");
709                 goto error;
710         }
711
712         return 0;
713
714 error:
715         if (uvc->vdev)
716                 video_device_release(uvc->vdev);
717
718         if (uvc->control_ep)
719                 uvc->control_ep->driver_data = NULL;
720         if (uvc->video.ep)
721                 uvc->video.ep->driver_data = NULL;
722
723         if (uvc->control_req) {
724                 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
725                 kfree(uvc->control_buf);
726         }
727
728         usb_free_all_descriptors(f);
729         return ret;
730 }
731
732 /* --------------------------------------------------------------------------
733  * USB gadget function
734  */
735
736 /**
737  * uvc_bind_config - add a UVC function to a configuration
738  * @c: the configuration to support the UVC instance
739  * Context: single threaded during gadget setup
740  *
741  * Returns zero on success, else negative errno.
742  *
743  * Caller must have called @uvc_setup(). Caller is also responsible for
744  * calling @uvc_cleanup() before module unload.
745  */
746 int __init
747 uvc_bind_config(struct usb_configuration *c,
748                 const struct uvc_descriptor_header * const *fs_control,
749                 const struct uvc_descriptor_header * const *ss_control,
750                 const struct uvc_descriptor_header * const *fs_streaming,
751                 const struct uvc_descriptor_header * const *hs_streaming,
752                 const struct uvc_descriptor_header * const *ss_streaming)
753 {
754         struct uvc_device *uvc;
755         int ret = 0;
756
757         /* TODO Check if the USB device controller supports the required
758          * features.
759          */
760         if (!gadget_is_dualspeed(c->cdev->gadget))
761                 return -EINVAL;
762
763         uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
764         if (uvc == NULL)
765                 return -ENOMEM;
766
767         uvc->state = UVC_STATE_DISCONNECTED;
768
769         /* Validate the descriptors. */
770         if (fs_control == NULL || fs_control[0] == NULL ||
771             fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
772                 goto error;
773
774         if (ss_control == NULL || ss_control[0] == NULL ||
775             ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
776                 goto error;
777
778         if (fs_streaming == NULL || fs_streaming[0] == NULL ||
779             fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
780                 goto error;
781
782         if (hs_streaming == NULL || hs_streaming[0] == NULL ||
783             hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
784                 goto error;
785
786         if (ss_streaming == NULL || ss_streaming[0] == NULL ||
787             ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
788                 goto error;
789
790         uvc->desc.fs_control = fs_control;
791         uvc->desc.ss_control = ss_control;
792         uvc->desc.fs_streaming = fs_streaming;
793         uvc->desc.hs_streaming = hs_streaming;
794         uvc->desc.ss_streaming = ss_streaming;
795
796         /* String descriptors are global, we only need to allocate string IDs
797          * for the first UVC function. UVC functions beyond the first (if any)
798          * will reuse the same IDs.
799          */
800         if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
801                 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
802                 if (ret)
803                         goto error;
804                 uvc_iad.iFunction =
805                         uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
806                 uvc_control_intf.iInterface =
807                         uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
808                 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
809                 uvc_streaming_intf_alt0.iInterface = ret;
810                 uvc_streaming_intf_alt1.iInterface = ret;
811         }
812
813         /* Register the function. */
814         uvc->func.name = "uvc";
815         uvc->func.strings = uvc_function_strings;
816         uvc->func.bind = uvc_function_bind;
817         uvc->func.unbind = uvc_function_unbind;
818         uvc->func.get_alt = uvc_function_get_alt;
819         uvc->func.set_alt = uvc_function_set_alt;
820         uvc->func.disable = uvc_function_disable;
821         uvc->func.setup = uvc_function_setup;
822
823         ret = usb_add_function(c, &uvc->func);
824         if (ret)
825                 kfree(uvc);
826
827         return ret;
828
829 error:
830         kfree(uvc);
831         return ret;
832 }
833
834 module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
835 MODULE_PARM_DESC(trace, "Trace level bitmask");
836