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