Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uvc_driver.c
Go to the documentation of this file.
1 /*
2  * uvc_driver.c -- USB Video Class driver
3  *
4  * Copyright (C) 2005-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 
14 #include <linux/atomic.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <linux/version.h>
24 #include <asm/unaligned.h>
25 
26 #include <media/v4l2-common.h>
27 
28 #include "uvcvideo.h"
29 
30 #define DRIVER_AUTHOR "Laurent Pinchart " \
32 #define DRIVER_DESC "USB Video Class driver"
33 
35 unsigned int uvc_no_drop_param;
36 static unsigned int uvc_quirks_param = -1;
37 unsigned int uvc_trace_param;
39 
40 /* ------------------------------------------------------------------------
41  * Video formats
42  */
43 
44 static struct uvc_format_desc uvc_fmts[] = {
45  {
46  .name = "YUV 4:2:2 (YUYV)",
47  .guid = UVC_GUID_FORMAT_YUY2,
48  .fcc = V4L2_PIX_FMT_YUYV,
49  },
50  {
51  .name = "YUV 4:2:2 (YUYV)",
53  .fcc = V4L2_PIX_FMT_YUYV,
54  },
55  {
56  .name = "YUV 4:2:0 (NV12)",
57  .guid = UVC_GUID_FORMAT_NV12,
58  .fcc = V4L2_PIX_FMT_NV12,
59  },
60  {
61  .name = "MJPEG",
62  .guid = UVC_GUID_FORMAT_MJPEG,
63  .fcc = V4L2_PIX_FMT_MJPEG,
64  },
65  {
66  .name = "YVU 4:2:0 (YV12)",
67  .guid = UVC_GUID_FORMAT_YV12,
68  .fcc = V4L2_PIX_FMT_YVU420,
69  },
70  {
71  .name = "YUV 4:2:0 (I420)",
72  .guid = UVC_GUID_FORMAT_I420,
73  .fcc = V4L2_PIX_FMT_YUV420,
74  },
75  {
76  .name = "YUV 4:2:0 (M420)",
77  .guid = UVC_GUID_FORMAT_M420,
78  .fcc = V4L2_PIX_FMT_M420,
79  },
80  {
81  .name = "YUV 4:2:2 (UYVY)",
82  .guid = UVC_GUID_FORMAT_UYVY,
83  .fcc = V4L2_PIX_FMT_UYVY,
84  },
85  {
86  .name = "Greyscale 8-bit (Y800)",
87  .guid = UVC_GUID_FORMAT_Y800,
88  .fcc = V4L2_PIX_FMT_GREY,
89  },
90  {
91  .name = "Greyscale 8-bit (Y8 )",
92  .guid = UVC_GUID_FORMAT_Y8,
93  .fcc = V4L2_PIX_FMT_GREY,
94  },
95  {
96  .name = "Greyscale 10-bit (Y10 )",
97  .guid = UVC_GUID_FORMAT_Y10,
98  .fcc = V4L2_PIX_FMT_Y10,
99  },
100  {
101  .name = "Greyscale 12-bit (Y12 )",
102  .guid = UVC_GUID_FORMAT_Y12,
103  .fcc = V4L2_PIX_FMT_Y12,
104  },
105  {
106  .name = "Greyscale 16-bit (Y16 )",
107  .guid = UVC_GUID_FORMAT_Y16,
108  .fcc = V4L2_PIX_FMT_Y16,
109  },
110  {
111  .name = "RGB Bayer",
112  .guid = UVC_GUID_FORMAT_BY8,
113  .fcc = V4L2_PIX_FMT_SBGGR8,
114  },
115  {
116  .name = "RGB565",
117  .guid = UVC_GUID_FORMAT_RGBP,
118  .fcc = V4L2_PIX_FMT_RGB565,
119  },
120  {
121  .name = "H.264",
122  .guid = UVC_GUID_FORMAT_H264,
123  .fcc = V4L2_PIX_FMT_H264,
124  },
125 };
126 
127 /* ------------------------------------------------------------------------
128  * Utility functions
129  */
130 
131 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
132  __u8 epaddr)
133 {
134  struct usb_host_endpoint *ep;
135  unsigned int i;
136 
137  for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
138  ep = &alts->endpoint[i];
139  if (ep->desc.bEndpointAddress == epaddr)
140  return ep;
141  }
142 
143  return NULL;
144 }
145 
146 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
147 {
148  unsigned int len = ARRAY_SIZE(uvc_fmts);
149  unsigned int i;
150 
151  for (i = 0; i < len; ++i) {
152  if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
153  return &uvc_fmts[i];
154  }
155 
156  return NULL;
157 }
158 
159 static __u32 uvc_colorspace(const __u8 primaries)
160 {
161  static const __u8 colorprimaries[] = {
162  0,
168  };
169 
170  if (primaries < ARRAY_SIZE(colorprimaries))
171  return colorprimaries[primaries];
172 
173  return 0;
174 }
175 
176 /* Simplify a fraction using a simple continued fraction decomposition. The
177  * idea here is to convert fractions such as 333333/10000000 to 1/30 using
178  * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
179  * arbitrary parameters to remove non-significative terms from the simple
180  * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
181  * respectively seems to give nice results.
182  */
183 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
184  unsigned int n_terms, unsigned int threshold)
185 {
186  uint32_t *an;
187  uint32_t x, y, r;
188  unsigned int i, n;
189 
190  an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
191  if (an == NULL)
192  return;
193 
194  /* Convert the fraction to a simple continued fraction. See
195  * http://mathforum.org/dr.math/faq/faq.fractions.html
196  * Stop if the current term is bigger than or equal to the given
197  * threshold.
198  */
199  x = *numerator;
200  y = *denominator;
201 
202  for (n = 0; n < n_terms && y != 0; ++n) {
203  an[n] = x / y;
204  if (an[n] >= threshold) {
205  if (n < 2)
206  n++;
207  break;
208  }
209 
210  r = x - an[n] * y;
211  x = y;
212  y = r;
213  }
214 
215  /* Expand the simple continued fraction back to an integer fraction. */
216  x = 0;
217  y = 1;
218 
219  for (i = n; i > 0; --i) {
220  r = y;
221  y = an[i-1] * y + x;
222  x = r;
223  }
224 
225  *numerator = y;
226  *denominator = x;
227  kfree(an);
228 }
229 
230 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
231  * to compute numerator / denominator * 10000000 using 32 bit fixed point
232  * arithmetic only.
233  */
235 {
236  uint32_t multiplier;
237 
238  /* Saturate the result if the operation would overflow. */
239  if (denominator == 0 ||
240  numerator/denominator >= ((uint32_t)-1)/10000000)
241  return (uint32_t)-1;
242 
243  /* Divide both the denominator and the multiplier by two until
244  * numerator * multiplier doesn't overflow. If anyone knows a better
245  * algorithm please let me know.
246  */
247  multiplier = 10000000;
248  while (numerator > ((uint32_t)-1)/multiplier) {
249  multiplier /= 2;
250  denominator /= 2;
251  }
252 
253  return denominator ? numerator * multiplier / denominator : 0;
254 }
255 
256 /* ------------------------------------------------------------------------
257  * Terminal and unit management
258  */
259 
260 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
261 {
262  struct uvc_entity *entity;
263 
264  list_for_each_entry(entity, &dev->entities, list) {
265  if (entity->id == id)
266  return entity;
267  }
268 
269  return NULL;
270 }
271 
272 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
273  int id, struct uvc_entity *entity)
274 {
275  unsigned int i;
276 
277  if (entity == NULL)
278  entity = list_entry(&dev->entities, struct uvc_entity, list);
279 
280  list_for_each_entry_continue(entity, &dev->entities, list) {
281  for (i = 0; i < entity->bNrInPins; ++i)
282  if (entity->baSourceID[i] == id)
283  return entity;
284  }
285 
286  return NULL;
287 }
288 
289 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
290 {
291  struct uvc_streaming *stream;
292 
293  list_for_each_entry(stream, &dev->streams, list) {
294  if (stream->header.bTerminalLink == id)
295  return stream;
296  }
297 
298  return NULL;
299 }
300 
301 /* ------------------------------------------------------------------------
302  * Descriptors parsing
303  */
304 
305 static int uvc_parse_format(struct uvc_device *dev,
306  struct uvc_streaming *streaming, struct uvc_format *format,
307  __u32 **intervals, unsigned char *buffer, int buflen)
308 {
309  struct usb_interface *intf = streaming->intf;
310  struct usb_host_interface *alts = intf->cur_altsetting;
311  struct uvc_format_desc *fmtdesc;
312  struct uvc_frame *frame;
313  const unsigned char *start = buffer;
314  unsigned int interval;
315  unsigned int i, n;
316  __u8 ftype;
317 
318  format->type = buffer[2];
319  format->index = buffer[3];
320 
321  switch (buffer[2]) {
324  n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
325  if (buflen < n) {
326  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
327  "interface %d FORMAT error\n",
328  dev->udev->devnum,
329  alts->desc.bInterfaceNumber);
330  return -EINVAL;
331  }
332 
333  /* Find the format descriptor from its GUID. */
334  fmtdesc = uvc_format_by_guid(&buffer[5]);
335 
336  if (fmtdesc != NULL) {
337  strlcpy(format->name, fmtdesc->name,
338  sizeof format->name);
339  format->fcc = fmtdesc->fcc;
340  } else {
341  uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
342  &buffer[5]);
343  snprintf(format->name, sizeof(format->name), "%pUl\n",
344  &buffer[5]);
345  format->fcc = 0;
346  }
347 
348  format->bpp = buffer[21];
349  if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
351  } else {
352  ftype = UVC_VS_FRAME_FRAME_BASED;
353  if (buffer[27])
354  format->flags = UVC_FMT_FLAG_COMPRESSED;
355  }
356  break;
357 
358  case UVC_VS_FORMAT_MJPEG:
359  if (buflen < 11) {
360  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
361  "interface %d FORMAT error\n",
362  dev->udev->devnum,
363  alts->desc.bInterfaceNumber);
364  return -EINVAL;
365  }
366 
367  strlcpy(format->name, "MJPEG", sizeof format->name);
368  format->fcc = V4L2_PIX_FMT_MJPEG;
369  format->flags = UVC_FMT_FLAG_COMPRESSED;
370  format->bpp = 0;
371  ftype = UVC_VS_FRAME_MJPEG;
372  break;
373 
374  case UVC_VS_FORMAT_DV:
375  if (buflen < 9) {
376  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
377  "interface %d FORMAT error\n",
378  dev->udev->devnum,
379  alts->desc.bInterfaceNumber);
380  return -EINVAL;
381  }
382 
383  switch (buffer[8] & 0x7f) {
384  case 0:
385  strlcpy(format->name, "SD-DV", sizeof format->name);
386  break;
387  case 1:
388  strlcpy(format->name, "SDL-DV", sizeof format->name);
389  break;
390  case 2:
391  strlcpy(format->name, "HD-DV", sizeof format->name);
392  break;
393  default:
394  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
395  "interface %d: unknown DV format %u\n",
396  dev->udev->devnum,
397  alts->desc.bInterfaceNumber, buffer[8]);
398  return -EINVAL;
399  }
400 
401  strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
402  sizeof format->name);
403 
404  format->fcc = V4L2_PIX_FMT_DV;
406  format->bpp = 0;
407  ftype = 0;
408 
409  /* Create a dummy frame descriptor. */
410  frame = &format->frame[0];
411  memset(&format->frame[0], 0, sizeof format->frame[0]);
412  frame->bFrameIntervalType = 1;
413  frame->dwDefaultFrameInterval = 1;
414  frame->dwFrameInterval = *intervals;
415  *(*intervals)++ = 1;
416  format->nframes = 1;
417  break;
418 
421  /* Not supported yet. */
422  default:
423  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
424  "interface %d unsupported format %u\n",
425  dev->udev->devnum, alts->desc.bInterfaceNumber,
426  buffer[2]);
427  return -EINVAL;
428  }
429 
430  uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
431 
432  buflen -= buffer[0];
433  buffer += buffer[0];
434 
435  /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
436  * based formats have frame descriptors.
437  */
438  while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
439  buffer[2] == ftype) {
440  frame = &format->frame[format->nframes];
441  if (ftype != UVC_VS_FRAME_FRAME_BASED)
442  n = buflen > 25 ? buffer[25] : 0;
443  else
444  n = buflen > 21 ? buffer[21] : 0;
445 
446  n = n ? n : 3;
447 
448  if (buflen < 26 + 4*n) {
449  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
450  "interface %d FRAME error\n", dev->udev->devnum,
451  alts->desc.bInterfaceNumber);
452  return -EINVAL;
453  }
454 
455  frame->bFrameIndex = buffer[3];
456  frame->bmCapabilities = buffer[4];
457  frame->wWidth = get_unaligned_le16(&buffer[5]);
458  frame->wHeight = get_unaligned_le16(&buffer[7]);
459  frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
460  frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
461  if (ftype != UVC_VS_FRAME_FRAME_BASED) {
463  get_unaligned_le32(&buffer[17]);
464  frame->dwDefaultFrameInterval =
465  get_unaligned_le32(&buffer[21]);
466  frame->bFrameIntervalType = buffer[25];
467  } else {
468  frame->dwMaxVideoFrameBufferSize = 0;
469  frame->dwDefaultFrameInterval =
470  get_unaligned_le32(&buffer[17]);
471  frame->bFrameIntervalType = buffer[21];
472  }
473  frame->dwFrameInterval = *intervals;
474 
475  /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
476  * completely. Observed behaviours range from setting the
477  * value to 1.1x the actual frame size to hardwiring the
478  * 16 low bits to 0. This results in a higher than necessary
479  * memory usage as well as a wrong image size information. For
480  * uncompressed formats this can be fixed by computing the
481  * value from the frame size.
482  */
483  if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
484  frame->dwMaxVideoFrameBufferSize = format->bpp
485  * frame->wWidth * frame->wHeight / 8;
486 
487  /* Some bogus devices report dwMinFrameInterval equal to
488  * dwMaxFrameInterval and have dwFrameIntervalStep set to
489  * zero. Setting all null intervals to 1 fixes the problem and
490  * some other divisions by zero that could happen.
491  */
492  for (i = 0; i < n; ++i) {
493  interval = get_unaligned_le32(&buffer[26+4*i]);
494  *(*intervals)++ = interval ? interval : 1;
495  }
496 
497  /* Make sure that the default frame interval stays between
498  * the boundaries.
499  */
500  n -= frame->bFrameIntervalType ? 1 : 2;
501  frame->dwDefaultFrameInterval =
502  min(frame->dwFrameInterval[n],
503  max(frame->dwFrameInterval[0],
504  frame->dwDefaultFrameInterval));
505 
507  frame->bFrameIntervalType = 1;
508  frame->dwFrameInterval[0] =
509  frame->dwDefaultFrameInterval;
510  }
511 
512  uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
513  frame->wWidth, frame->wHeight,
514  10000000/frame->dwDefaultFrameInterval,
515  (100000000/frame->dwDefaultFrameInterval)%10);
516 
517  format->nframes++;
518  buflen -= buffer[0];
519  buffer += buffer[0];
520  }
521 
522  if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
523  buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
524  buflen -= buffer[0];
525  buffer += buffer[0];
526  }
527 
528  if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
529  buffer[2] == UVC_VS_COLORFORMAT) {
530  if (buflen < 6) {
531  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
532  "interface %d COLORFORMAT error\n",
533  dev->udev->devnum,
534  alts->desc.bInterfaceNumber);
535  return -EINVAL;
536  }
537 
538  format->colorspace = uvc_colorspace(buffer[3]);
539 
540  buflen -= buffer[0];
541  buffer += buffer[0];
542  }
543 
544  return buffer - start;
545 }
546 
547 static int uvc_parse_streaming(struct uvc_device *dev,
548  struct usb_interface *intf)
549 {
550  struct uvc_streaming *streaming = NULL;
551  struct uvc_format *format;
552  struct uvc_frame *frame;
553  struct usb_host_interface *alts = &intf->altsetting[0];
554  unsigned char *_buffer, *buffer = alts->extra;
555  int _buflen, buflen = alts->extralen;
556  unsigned int nformats = 0, nframes = 0, nintervals = 0;
557  unsigned int size, i, n, p;
558  __u32 *interval;
559  __u16 psize;
560  int ret = -EINVAL;
561 
562  if (intf->cur_altsetting->desc.bInterfaceSubClass
564  uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
565  "video streaming interface\n", dev->udev->devnum,
566  intf->altsetting[0].desc.bInterfaceNumber);
567  return -EINVAL;
568  }
569 
570  if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
571  uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
572  "claimed\n", dev->udev->devnum,
573  intf->altsetting[0].desc.bInterfaceNumber);
574  return -EINVAL;
575  }
576 
577  streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
578  if (streaming == NULL) {
580  return -EINVAL;
581  }
582 
583  mutex_init(&streaming->mutex);
584  streaming->dev = dev;
585  streaming->intf = usb_get_intf(intf);
586  streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
587 
588  /* The Pico iMage webcam has its class-specific interface descriptors
589  * after the endpoint descriptors.
590  */
591  if (buflen == 0) {
592  for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
593  struct usb_host_endpoint *ep = &alts->endpoint[i];
594 
595  if (ep->extralen == 0)
596  continue;
597 
598  if (ep->extralen > 2 &&
599  ep->extra[1] == USB_DT_CS_INTERFACE) {
600  uvc_trace(UVC_TRACE_DESCR, "trying extra data "
601  "from endpoint %u.\n", i);
602  buffer = alts->endpoint[i].extra;
603  buflen = alts->endpoint[i].extralen;
604  break;
605  }
606  }
607  }
608 
609  /* Skip the standard interface descriptors. */
610  while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
611  buflen -= buffer[0];
612  buffer += buffer[0];
613  }
614 
615  if (buflen <= 2) {
616  uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
617  "interface descriptors found.\n");
618  goto error;
619  }
620 
621  /* Parse the header descriptor. */
622  switch (buffer[2]) {
624  streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
625  size = 9;
626  break;
627 
628  case UVC_VS_INPUT_HEADER:
629  streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
630  size = 13;
631  break;
632 
633  default:
634  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
635  "%d HEADER descriptor not found.\n", dev->udev->devnum,
636  alts->desc.bInterfaceNumber);
637  goto error;
638  }
639 
640  p = buflen >= 4 ? buffer[3] : 0;
641  n = buflen >= size ? buffer[size-1] : 0;
642 
643  if (buflen < size + p*n) {
644  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
645  "interface %d HEADER descriptor is invalid.\n",
646  dev->udev->devnum, alts->desc.bInterfaceNumber);
647  goto error;
648  }
649 
650  streaming->header.bNumFormats = p;
651  streaming->header.bEndpointAddress = buffer[6];
652  if (buffer[2] == UVC_VS_INPUT_HEADER) {
653  streaming->header.bmInfo = buffer[7];
654  streaming->header.bTerminalLink = buffer[8];
655  streaming->header.bStillCaptureMethod = buffer[9];
656  streaming->header.bTriggerSupport = buffer[10];
657  streaming->header.bTriggerUsage = buffer[11];
658  } else {
659  streaming->header.bTerminalLink = buffer[7];
660  }
661  streaming->header.bControlSize = n;
662 
663  streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
664  GFP_KERNEL);
665  if (streaming->header.bmaControls == NULL) {
666  ret = -ENOMEM;
667  goto error;
668  }
669 
670  buflen -= buffer[0];
671  buffer += buffer[0];
672 
673  _buffer = buffer;
674  _buflen = buflen;
675 
676  /* Count the format and frame descriptors. */
677  while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
678  switch (_buffer[2]) {
680  case UVC_VS_FORMAT_MJPEG:
682  nformats++;
683  break;
684 
685  case UVC_VS_FORMAT_DV:
686  /* DV format has no frame descriptor. We will create a
687  * dummy frame descriptor with a dummy frame interval.
688  */
689  nformats++;
690  nframes++;
691  nintervals++;
692  break;
693 
696  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
697  "interface %d FORMAT %u is not supported.\n",
698  dev->udev->devnum,
699  alts->desc.bInterfaceNumber, _buffer[2]);
700  break;
701 
703  case UVC_VS_FRAME_MJPEG:
704  nframes++;
705  if (_buflen > 25)
706  nintervals += _buffer[25] ? _buffer[25] : 3;
707  break;
708 
710  nframes++;
711  if (_buflen > 21)
712  nintervals += _buffer[21] ? _buffer[21] : 3;
713  break;
714  }
715 
716  _buflen -= _buffer[0];
717  _buffer += _buffer[0];
718  }
719 
720  if (nformats == 0) {
721  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
722  "%d has no supported formats defined.\n",
723  dev->udev->devnum, alts->desc.bInterfaceNumber);
724  goto error;
725  }
726 
727  size = nformats * sizeof *format + nframes * sizeof *frame
728  + nintervals * sizeof *interval;
729  format = kzalloc(size, GFP_KERNEL);
730  if (format == NULL) {
731  ret = -ENOMEM;
732  goto error;
733  }
734 
735  frame = (struct uvc_frame *)&format[nformats];
736  interval = (__u32 *)&frame[nframes];
737 
738  streaming->format = format;
739  streaming->nformats = nformats;
740 
741  /* Parse the format descriptors. */
742  while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
743  switch (buffer[2]) {
745  case UVC_VS_FORMAT_MJPEG:
746  case UVC_VS_FORMAT_DV:
748  format->frame = frame;
749  ret = uvc_parse_format(dev, streaming, format,
750  &interval, buffer, buflen);
751  if (ret < 0)
752  goto error;
753 
754  frame += format->nframes;
755  format++;
756 
757  buflen -= ret;
758  buffer += ret;
759  continue;
760 
761  default:
762  break;
763  }
764 
765  buflen -= buffer[0];
766  buffer += buffer[0];
767  }
768 
769  if (buflen)
770  uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
771  "%d has %u bytes of trailing descriptor garbage.\n",
772  dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
773 
774  /* Parse the alternate settings to find the maximum bandwidth. */
775  for (i = 0; i < intf->num_altsetting; ++i) {
776  struct usb_host_endpoint *ep;
777  alts = &intf->altsetting[i];
778  ep = uvc_find_endpoint(alts,
779  streaming->header.bEndpointAddress);
780  if (ep == NULL)
781  continue;
782 
783  psize = le16_to_cpu(ep->desc.wMaxPacketSize);
784  psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
785  if (psize > streaming->maxpsize)
786  streaming->maxpsize = psize;
787  }
788 
789  list_add_tail(&streaming->list, &dev->streams);
790  return 0;
791 
792 error:
794  usb_put_intf(intf);
795  kfree(streaming->format);
796  kfree(streaming->header.bmaControls);
797  kfree(streaming);
798  return ret;
799 }
800 
801 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
802  unsigned int num_pads, unsigned int extra_size)
803 {
804  struct uvc_entity *entity;
805  unsigned int num_inputs;
806  unsigned int size;
807  unsigned int i;
808 
809  extra_size = ALIGN(extra_size, sizeof(*entity->pads));
810  num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
811  size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
812  + num_inputs;
813  entity = kzalloc(size, GFP_KERNEL);
814  if (entity == NULL)
815  return NULL;
816 
817  entity->id = id;
818  entity->type = type;
819 
820  entity->num_links = 0;
821  entity->num_pads = num_pads;
822  entity->pads = ((void *)(entity + 1)) + extra_size;
823 
824  for (i = 0; i < num_inputs; ++i)
825  entity->pads[i].flags = MEDIA_PAD_FL_SINK;
826  if (!UVC_ENTITY_IS_OTERM(entity))
827  entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE;
828 
829  entity->bNrInPins = num_inputs;
830  entity->baSourceID = (__u8 *)(&entity->pads[num_pads]);
831 
832  return entity;
833 }
834 
835 /* Parse vendor-specific extensions. */
836 static int uvc_parse_vendor_control(struct uvc_device *dev,
837  const unsigned char *buffer, int buflen)
838 {
839  struct usb_device *udev = dev->udev;
840  struct usb_host_interface *alts = dev->intf->cur_altsetting;
841  struct uvc_entity *unit;
842  unsigned int n, p;
843  int handled = 0;
844 
845  switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
846  case 0x046d: /* Logitech */
847  if (buffer[1] != 0x41 || buffer[2] != 0x01)
848  break;
849 
850  /* Logitech implements several vendor specific functions
851  * through vendor specific extension units (LXU).
852  *
853  * The LXU descriptors are similar to XU descriptors
854  * (see "USB Device Video Class for Video Devices", section
855  * 3.7.2.6 "Extension Unit Descriptor") with the following
856  * differences:
857  *
858  * ----------------------------------------------------------
859  * 0 bLength 1 Number
860  * Size of this descriptor, in bytes: 24+p+n*2
861  * ----------------------------------------------------------
862  * 23+p+n bmControlsType N Bitmap
863  * Individual bits in the set are defined:
864  * 0: Absolute
865  * 1: Relative
866  *
867  * This bitset is mapped exactly the same as bmControls.
868  * ----------------------------------------------------------
869  * 23+p+n*2 bReserved 1 Boolean
870  * ----------------------------------------------------------
871  * 24+p+n*2 iExtension 1 Index
872  * Index of a string descriptor that describes this
873  * extension unit.
874  * ----------------------------------------------------------
875  */
876  p = buflen >= 22 ? buffer[21] : 0;
877  n = buflen >= 25 + p ? buffer[22+p] : 0;
878 
879  if (buflen < 25 + p + 2*n) {
880  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
881  "interface %d EXTENSION_UNIT error\n",
882  udev->devnum, alts->desc.bInterfaceNumber);
883  break;
884  }
885 
886  unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
887  p + 1, 2*n);
888  if (unit == NULL)
889  return -ENOMEM;
890 
891  memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
892  unit->extension.bNumControls = buffer[20];
893  memcpy(unit->baSourceID, &buffer[22], p);
894  unit->extension.bControlSize = buffer[22+p];
895  unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
896  unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
897  + n;
898  memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
899 
900  if (buffer[24+p+2*n] != 0)
901  usb_string(udev, buffer[24+p+2*n], unit->name,
902  sizeof unit->name);
903  else
904  sprintf(unit->name, "Extension %u", buffer[3]);
905 
906  list_add_tail(&unit->list, &dev->entities);
907  handled = 1;
908  break;
909  }
910 
911  return handled;
912 }
913 
914 static int uvc_parse_standard_control(struct uvc_device *dev,
915  const unsigned char *buffer, int buflen)
916 {
917  struct usb_device *udev = dev->udev;
918  struct uvc_entity *unit, *term;
919  struct usb_interface *intf;
920  struct usb_host_interface *alts = dev->intf->cur_altsetting;
921  unsigned int i, n, p, len;
922  __u16 type;
923 
924  switch (buffer[2]) {
925  case UVC_VC_HEADER:
926  n = buflen >= 12 ? buffer[11] : 0;
927 
928  if (buflen < 12 || buflen < 12 + n) {
929  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
930  "interface %d HEADER error\n", udev->devnum,
931  alts->desc.bInterfaceNumber);
932  return -EINVAL;
933  }
934 
935  dev->uvc_version = get_unaligned_le16(&buffer[3]);
936  dev->clock_frequency = get_unaligned_le32(&buffer[7]);
937 
938  /* Parse all USB Video Streaming interfaces. */
939  for (i = 0; i < n; ++i) {
940  intf = usb_ifnum_to_if(udev, buffer[12+i]);
941  if (intf == NULL) {
942  uvc_trace(UVC_TRACE_DESCR, "device %d "
943  "interface %d doesn't exists\n",
944  udev->devnum, i);
945  continue;
946  }
947 
948  uvc_parse_streaming(dev, intf);
949  }
950  break;
951 
953  if (buflen < 8) {
954  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
955  "interface %d INPUT_TERMINAL error\n",
956  udev->devnum, alts->desc.bInterfaceNumber);
957  return -EINVAL;
958  }
959 
960  /* Make sure the terminal type MSB is not null, otherwise it
961  * could be confused with a unit.
962  */
963  type = get_unaligned_le16(&buffer[4]);
964  if ((type & 0xff00) == 0) {
965  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
966  "interface %d INPUT_TERMINAL %d has invalid "
967  "type 0x%04x, skipping\n", udev->devnum,
968  alts->desc.bInterfaceNumber,
969  buffer[3], type);
970  return 0;
971  }
972 
973  n = 0;
974  p = 0;
975  len = 8;
976 
977  if (type == UVC_ITT_CAMERA) {
978  n = buflen >= 15 ? buffer[14] : 0;
979  len = 15;
980 
981  } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
982  n = buflen >= 9 ? buffer[8] : 0;
983  p = buflen >= 10 + n ? buffer[9+n] : 0;
984  len = 10;
985  }
986 
987  if (buflen < len + n + p) {
988  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
989  "interface %d INPUT_TERMINAL error\n",
990  udev->devnum, alts->desc.bInterfaceNumber);
991  return -EINVAL;
992  }
993 
994  term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
995  1, n + p);
996  if (term == NULL)
997  return -ENOMEM;
998 
999  if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
1000  term->camera.bControlSize = n;
1001  term->camera.bmControls = (__u8 *)term + sizeof *term;
1002  term->camera.wObjectiveFocalLengthMin =
1003  get_unaligned_le16(&buffer[8]);
1004  term->camera.wObjectiveFocalLengthMax =
1005  get_unaligned_le16(&buffer[10]);
1006  term->camera.wOcularFocalLength =
1007  get_unaligned_le16(&buffer[12]);
1008  memcpy(term->camera.bmControls, &buffer[15], n);
1009  } else if (UVC_ENTITY_TYPE(term) ==
1011  term->media.bControlSize = n;
1012  term->media.bmControls = (__u8 *)term + sizeof *term;
1013  term->media.bTransportModeSize = p;
1014  term->media.bmTransportModes = (__u8 *)term
1015  + sizeof *term + n;
1016  memcpy(term->media.bmControls, &buffer[9], n);
1017  memcpy(term->media.bmTransportModes, &buffer[10+n], p);
1018  }
1019 
1020  if (buffer[7] != 0)
1021  usb_string(udev, buffer[7], term->name,
1022  sizeof term->name);
1023  else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
1024  sprintf(term->name, "Camera %u", buffer[3]);
1026  sprintf(term->name, "Media %u", buffer[3]);
1027  else
1028  sprintf(term->name, "Input %u", buffer[3]);
1029 
1030  list_add_tail(&term->list, &dev->entities);
1031  break;
1032 
1034  if (buflen < 9) {
1035  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1036  "interface %d OUTPUT_TERMINAL error\n",
1037  udev->devnum, alts->desc.bInterfaceNumber);
1038  return -EINVAL;
1039  }
1040 
1041  /* Make sure the terminal type MSB is not null, otherwise it
1042  * could be confused with a unit.
1043  */
1044  type = get_unaligned_le16(&buffer[4]);
1045  if ((type & 0xff00) == 0) {
1046  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1047  "interface %d OUTPUT_TERMINAL %d has invalid "
1048  "type 0x%04x, skipping\n", udev->devnum,
1049  alts->desc.bInterfaceNumber, buffer[3], type);
1050  return 0;
1051  }
1052 
1053  term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1054  1, 0);
1055  if (term == NULL)
1056  return -ENOMEM;
1057 
1058  memcpy(term->baSourceID, &buffer[7], 1);
1059 
1060  if (buffer[8] != 0)
1061  usb_string(udev, buffer[8], term->name,
1062  sizeof term->name);
1063  else
1064  sprintf(term->name, "Output %u", buffer[3]);
1065 
1066  list_add_tail(&term->list, &dev->entities);
1067  break;
1068 
1069  case UVC_VC_SELECTOR_UNIT:
1070  p = buflen >= 5 ? buffer[4] : 0;
1071 
1072  if (buflen < 5 || buflen < 6 + p) {
1073  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1074  "interface %d SELECTOR_UNIT error\n",
1075  udev->devnum, alts->desc.bInterfaceNumber);
1076  return -EINVAL;
1077  }
1078 
1079  unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1080  if (unit == NULL)
1081  return -ENOMEM;
1082 
1083  memcpy(unit->baSourceID, &buffer[5], p);
1084 
1085  if (buffer[5+p] != 0)
1086  usb_string(udev, buffer[5+p], unit->name,
1087  sizeof unit->name);
1088  else
1089  sprintf(unit->name, "Selector %u", buffer[3]);
1090 
1091  list_add_tail(&unit->list, &dev->entities);
1092  break;
1093 
1095  n = buflen >= 8 ? buffer[7] : 0;
1096  p = dev->uvc_version >= 0x0110 ? 10 : 9;
1097 
1098  if (buflen < p + n) {
1099  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1100  "interface %d PROCESSING_UNIT error\n",
1101  udev->devnum, alts->desc.bInterfaceNumber);
1102  return -EINVAL;
1103  }
1104 
1105  unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1106  if (unit == NULL)
1107  return -ENOMEM;
1108 
1109  memcpy(unit->baSourceID, &buffer[4], 1);
1110  unit->processing.wMaxMultiplier =
1111  get_unaligned_le16(&buffer[5]);
1112  unit->processing.bControlSize = buffer[7];
1113  unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1114  memcpy(unit->processing.bmControls, &buffer[8], n);
1115  if (dev->uvc_version >= 0x0110)
1116  unit->processing.bmVideoStandards = buffer[9+n];
1117 
1118  if (buffer[8+n] != 0)
1119  usb_string(udev, buffer[8+n], unit->name,
1120  sizeof unit->name);
1121  else
1122  sprintf(unit->name, "Processing %u", buffer[3]);
1123 
1124  list_add_tail(&unit->list, &dev->entities);
1125  break;
1126 
1127  case UVC_VC_EXTENSION_UNIT:
1128  p = buflen >= 22 ? buffer[21] : 0;
1129  n = buflen >= 24 + p ? buffer[22+p] : 0;
1130 
1131  if (buflen < 24 + p + n) {
1132  uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1133  "interface %d EXTENSION_UNIT error\n",
1134  udev->devnum, alts->desc.bInterfaceNumber);
1135  return -EINVAL;
1136  }
1137 
1138  unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1139  if (unit == NULL)
1140  return -ENOMEM;
1141 
1142  memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1143  unit->extension.bNumControls = buffer[20];
1144  memcpy(unit->baSourceID, &buffer[22], p);
1145  unit->extension.bControlSize = buffer[22+p];
1146  unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1147  memcpy(unit->extension.bmControls, &buffer[23+p], n);
1148 
1149  if (buffer[23+p+n] != 0)
1150  usb_string(udev, buffer[23+p+n], unit->name,
1151  sizeof unit->name);
1152  else
1153  sprintf(unit->name, "Extension %u", buffer[3]);
1154 
1155  list_add_tail(&unit->list, &dev->entities);
1156  break;
1157 
1158  default:
1159  uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1160  "descriptor (%u)\n", buffer[2]);
1161  break;
1162  }
1163 
1164  return 0;
1165 }
1166 
1167 static int uvc_parse_control(struct uvc_device *dev)
1168 {
1169  struct usb_host_interface *alts = dev->intf->cur_altsetting;
1170  unsigned char *buffer = alts->extra;
1171  int buflen = alts->extralen;
1172  int ret;
1173 
1174  /* Parse the default alternate setting only, as the UVC specification
1175  * defines a single alternate setting, the default alternate setting
1176  * zero.
1177  */
1178 
1179  while (buflen > 2) {
1180  if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1181  buffer[1] != USB_DT_CS_INTERFACE)
1182  goto next_descriptor;
1183 
1184  if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1185  return ret;
1186 
1187 next_descriptor:
1188  buflen -= buffer[0];
1189  buffer += buffer[0];
1190  }
1191 
1192  /* Check if the optional status endpoint is present. Built-in iSight
1193  * webcams have an interrupt endpoint but spit proprietary data that
1194  * don't conform to the UVC status endpoint messages. Don't try to
1195  * handle the interrupt endpoint for those cameras.
1196  */
1197  if (alts->desc.bNumEndpoints == 1 &&
1198  !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1199  struct usb_host_endpoint *ep = &alts->endpoint[0];
1200  struct usb_endpoint_descriptor *desc = &ep->desc;
1201 
1202  if (usb_endpoint_is_int_in(desc) &&
1203  le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1204  desc->bInterval != 0) {
1205  uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1206  "(addr %02x).\n", desc->bEndpointAddress);
1207  dev->int_ep = ep;
1208  }
1209  }
1210 
1211  return 0;
1212 }
1213 
1214 /* ------------------------------------------------------------------------
1215  * UVC device scan
1216  */
1217 
1218 /*
1219  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1220  * and containing the following units:
1221  *
1222  * - one or more Output Terminals (USB Streaming or Display)
1223  * - zero or one Processing Unit
1224  * - zero, one or more single-input Selector Units
1225  * - zero or one multiple-input Selector Units, provided all inputs are
1226  * connected to input terminals
1227  * - zero, one or mode single-input Extension Units
1228  * - one or more Input Terminals (Camera, External or USB Streaming)
1229  *
1230  * The terminal and units must match on of the following structures:
1231  *
1232  * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1233  * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1234  * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1235  *
1236  * +---------+ +---------+ -> OTT_*(0)
1237  * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1238  * +---------+ +---------+ -> OTT_*(n)
1239  *
1240  * The Processing Unit and Extension Units can be in any order. Additional
1241  * Extension Units connected to the main chain as single-unit branches are
1242  * also supported. Single-input Selector Units are ignored.
1243  */
1244 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1245  struct uvc_entity *entity)
1246 {
1247  switch (UVC_ENTITY_TYPE(entity)) {
1248  case UVC_VC_EXTENSION_UNIT:
1250  printk(" <- XU %d", entity->id);
1251 
1252  if (entity->bNrInPins != 1) {
1253  uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1254  "than 1 input pin.\n", entity->id);
1255  return -1;
1256  }
1257 
1258  break;
1259 
1261  if (uvc_trace_param & UVC_TRACE_PROBE)
1262  printk(" <- PU %d", entity->id);
1263 
1264  if (chain->processing != NULL) {
1265  uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1266  "Processing Units in chain.\n");
1267  return -1;
1268  }
1269 
1270  chain->processing = entity;
1271  break;
1272 
1273  case UVC_VC_SELECTOR_UNIT:
1274  if (uvc_trace_param & UVC_TRACE_PROBE)
1275  printk(" <- SU %d", entity->id);
1276 
1277  /* Single-input selector units are ignored. */
1278  if (entity->bNrInPins == 1)
1279  break;
1280 
1281  if (chain->selector != NULL) {
1282  uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1283  "Units in chain.\n");
1284  return -1;
1285  }
1286 
1287  chain->selector = entity;
1288  break;
1289 
1291  case UVC_ITT_CAMERA:
1293  if (uvc_trace_param & UVC_TRACE_PROBE)
1294  printk(" <- IT %d\n", entity->id);
1295 
1296  break;
1297 
1299  case UVC_OTT_DISPLAY:
1301  if (uvc_trace_param & UVC_TRACE_PROBE)
1302  printk(" OT %d", entity->id);
1303 
1304  break;
1305 
1306  case UVC_TT_STREAMING:
1307  if (UVC_ENTITY_IS_ITERM(entity)) {
1308  if (uvc_trace_param & UVC_TRACE_PROBE)
1309  printk(" <- IT %d\n", entity->id);
1310  } else {
1311  if (uvc_trace_param & UVC_TRACE_PROBE)
1312  printk(" OT %d", entity->id);
1313  }
1314 
1315  break;
1316 
1317  default:
1318  uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1319  "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1320  return -1;
1321  }
1322 
1323  list_add_tail(&entity->chain, &chain->entities);
1324  return 0;
1325 }
1326 
1327 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1328  struct uvc_entity *entity, struct uvc_entity *prev)
1329 {
1330  struct uvc_entity *forward;
1331  int found;
1332 
1333  /* Forward scan */
1334  forward = NULL;
1335  found = 0;
1336 
1337  while (1) {
1338  forward = uvc_entity_by_reference(chain->dev, entity->id,
1339  forward);
1340  if (forward == NULL)
1341  break;
1342  if (forward == prev)
1343  continue;
1344 
1345  switch (UVC_ENTITY_TYPE(forward)) {
1346  case UVC_VC_EXTENSION_UNIT:
1347  if (forward->bNrInPins != 1) {
1348  uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1349  "has more than 1 input pin.\n",
1350  entity->id);
1351  return -EINVAL;
1352  }
1353 
1354  list_add_tail(&forward->chain, &chain->entities);
1355  if (uvc_trace_param & UVC_TRACE_PROBE) {
1356  if (!found)
1357  printk(" (->");
1358 
1359  printk(" XU %d", forward->id);
1360  found = 1;
1361  }
1362  break;
1363 
1365  case UVC_OTT_DISPLAY:
1367  case UVC_TT_STREAMING:
1368  if (UVC_ENTITY_IS_ITERM(forward)) {
1369  uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1370  "terminal %u.\n", forward->id);
1371  return -EINVAL;
1372  }
1373 
1374  list_add_tail(&forward->chain, &chain->entities);
1375  if (uvc_trace_param & UVC_TRACE_PROBE) {
1376  if (!found)
1377  printk(" (->");
1378 
1379  printk(" OT %d", forward->id);
1380  found = 1;
1381  }
1382  break;
1383  }
1384  }
1385  if (found)
1386  printk(")");
1387 
1388  return 0;
1389 }
1390 
1391 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1392  struct uvc_entity **_entity)
1393 {
1394  struct uvc_entity *entity = *_entity;
1395  struct uvc_entity *term;
1396  int id = -EINVAL, i;
1397 
1398  switch (UVC_ENTITY_TYPE(entity)) {
1399  case UVC_VC_EXTENSION_UNIT:
1401  id = entity->baSourceID[0];
1402  break;
1403 
1404  case UVC_VC_SELECTOR_UNIT:
1405  /* Single-input selector units are ignored. */
1406  if (entity->bNrInPins == 1) {
1407  id = entity->baSourceID[0];
1408  break;
1409  }
1410 
1411  if (uvc_trace_param & UVC_TRACE_PROBE)
1412  printk(" <- IT");
1413 
1414  chain->selector = entity;
1415  for (i = 0; i < entity->bNrInPins; ++i) {
1416  id = entity->baSourceID[i];
1417  term = uvc_entity_by_id(chain->dev, id);
1418  if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1419  uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1420  "input %d isn't connected to an "
1421  "input terminal\n", entity->id, i);
1422  return -1;
1423  }
1424 
1425  if (uvc_trace_param & UVC_TRACE_PROBE)
1426  printk(" %d", term->id);
1427 
1428  list_add_tail(&term->chain, &chain->entities);
1429  uvc_scan_chain_forward(chain, term, entity);
1430  }
1431 
1432  if (uvc_trace_param & UVC_TRACE_PROBE)
1433  printk("\n");
1434 
1435  id = 0;
1436  break;
1437 
1439  case UVC_ITT_CAMERA:
1442  case UVC_OTT_DISPLAY:
1444  case UVC_TT_STREAMING:
1445  id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1446  break;
1447  }
1448 
1449  if (id <= 0) {
1450  *_entity = NULL;
1451  return id;
1452  }
1453 
1454  entity = uvc_entity_by_id(chain->dev, id);
1455  if (entity == NULL) {
1456  uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1457  "unknown entity %d.\n", id);
1458  return -EINVAL;
1459  }
1460 
1461  *_entity = entity;
1462  return 0;
1463 }
1464 
1465 static int uvc_scan_chain(struct uvc_video_chain *chain,
1466  struct uvc_entity *term)
1467 {
1468  struct uvc_entity *entity, *prev;
1469 
1470  uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1471 
1472  entity = term;
1473  prev = NULL;
1474 
1475  while (entity != NULL) {
1476  /* Entity must not be part of an existing chain */
1477  if (entity->chain.next || entity->chain.prev) {
1478  uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1479  "entity %d already in chain.\n", entity->id);
1480  return -EINVAL;
1481  }
1482 
1483  /* Process entity */
1484  if (uvc_scan_chain_entity(chain, entity) < 0)
1485  return -EINVAL;
1486 
1487  /* Forward scan */
1488  if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1489  return -EINVAL;
1490 
1491  /* Backward scan */
1492  prev = entity;
1493  if (uvc_scan_chain_backward(chain, &entity) < 0)
1494  return -EINVAL;
1495  }
1496 
1497  return 0;
1498 }
1499 
1500 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1501  char *buffer)
1502 {
1503  struct uvc_entity *term;
1504  unsigned int nterms = 0;
1505  char *p = buffer;
1506 
1507  list_for_each_entry(term, terms, chain) {
1508  if (!UVC_ENTITY_IS_TERM(term) ||
1509  UVC_TERM_DIRECTION(term) != dir)
1510  continue;
1511 
1512  if (nterms)
1513  p += sprintf(p, ",");
1514  if (++nterms >= 4) {
1515  p += sprintf(p, "...");
1516  break;
1517  }
1518  p += sprintf(p, "%u", term->id);
1519  }
1520 
1521  return p - buffer;
1522 }
1523 
1524 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1525 {
1526  static char buffer[43];
1527  char *p = buffer;
1528 
1529  p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1530  p += sprintf(p, " -> ");
1531  uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1532 
1533  return buffer;
1534 }
1535 
1536 /*
1537  * Scan the device for video chains and register video devices.
1538  *
1539  * Chains are scanned starting at their output terminals and walked backwards.
1540  */
1541 static int uvc_scan_device(struct uvc_device *dev)
1542 {
1543  struct uvc_video_chain *chain;
1544  struct uvc_entity *term;
1545 
1546  list_for_each_entry(term, &dev->entities, list) {
1547  if (!UVC_ENTITY_IS_OTERM(term))
1548  continue;
1549 
1550  /* If the terminal is already included in a chain, skip it.
1551  * This can happen for chains that have multiple output
1552  * terminals, where all output terminals beside the first one
1553  * will be inserted in the chain in forward scans.
1554  */
1555  if (term->chain.next || term->chain.prev)
1556  continue;
1557 
1558  chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1559  if (chain == NULL)
1560  return -ENOMEM;
1561 
1562  INIT_LIST_HEAD(&chain->entities);
1563  mutex_init(&chain->ctrl_mutex);
1564  chain->dev = dev;
1565 
1566  if (uvc_scan_chain(chain, term) < 0) {
1567  kfree(chain);
1568  continue;
1569  }
1570 
1571  uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1572  uvc_print_chain(chain));
1573 
1574  list_add_tail(&chain->list, &dev->chains);
1575  }
1576 
1577  if (list_empty(&dev->chains)) {
1578  uvc_printk(KERN_INFO, "No valid video chain found.\n");
1579  return -1;
1580  }
1581 
1582  return 0;
1583 }
1584 
1585 /* ------------------------------------------------------------------------
1586  * Video device registration and unregistration
1587  */
1588 
1589 /*
1590  * Delete the UVC device.
1591  *
1592  * Called by the kernel when the last reference to the uvc_device structure
1593  * is released.
1594  *
1595  * As this function is called after or during disconnect(), all URBs have
1596  * already been canceled by the USB core. There is no need to kill the
1597  * interrupt URB manually.
1598  */
1599 static void uvc_delete(struct uvc_device *dev)
1600 {
1601  struct list_head *p, *n;
1602 
1603  usb_put_intf(dev->intf);
1604  usb_put_dev(dev->udev);
1605 
1606  uvc_status_cleanup(dev);
1608 
1609  if (dev->vdev.dev)
1611 #ifdef CONFIG_MEDIA_CONTROLLER
1612  if (media_devnode_is_registered(&dev->mdev.devnode))
1613  media_device_unregister(&dev->mdev);
1614 #endif
1615 
1616  list_for_each_safe(p, n, &dev->chains) {
1617  struct uvc_video_chain *chain;
1618  chain = list_entry(p, struct uvc_video_chain, list);
1619  kfree(chain);
1620  }
1621 
1622  list_for_each_safe(p, n, &dev->entities) {
1623  struct uvc_entity *entity;
1624  entity = list_entry(p, struct uvc_entity, list);
1625 #ifdef CONFIG_MEDIA_CONTROLLER
1626  uvc_mc_cleanup_entity(entity);
1627 #endif
1628  if (entity->vdev) {
1629  video_device_release(entity->vdev);
1630  entity->vdev = NULL;
1631  }
1632  kfree(entity);
1633  }
1634 
1635  list_for_each_safe(p, n, &dev->streams) {
1636  struct uvc_streaming *streaming;
1637  streaming = list_entry(p, struct uvc_streaming, list);
1639  streaming->intf);
1640  usb_put_intf(streaming->intf);
1641  kfree(streaming->format);
1642  kfree(streaming->header.bmaControls);
1643  kfree(streaming);
1644  }
1645 
1646  kfree(dev);
1647 }
1648 
1649 static void uvc_release(struct video_device *vdev)
1650 {
1651  struct uvc_streaming *stream = video_get_drvdata(vdev);
1652  struct uvc_device *dev = stream->dev;
1653 
1654  /* Decrement the registered streams count and delete the device when it
1655  * reaches zero.
1656  */
1657  if (atomic_dec_and_test(&dev->nstreams))
1658  uvc_delete(dev);
1659 }
1660 
1661 /*
1662  * Unregister the video devices.
1663  */
1664 static void uvc_unregister_video(struct uvc_device *dev)
1665 {
1666  struct uvc_streaming *stream;
1667 
1668  /* Unregistering all video devices might result in uvc_delete() being
1669  * called from inside the loop if there's no open file handle. To avoid
1670  * that, increment the stream count before iterating over the streams
1671  * and decrement it when done.
1672  */
1673  atomic_inc(&dev->nstreams);
1674 
1675  list_for_each_entry(stream, &dev->streams, list) {
1676  if (stream->vdev == NULL)
1677  continue;
1678 
1679  video_unregister_device(stream->vdev);
1680  stream->vdev = NULL;
1681 
1683  }
1684 
1685  /* Decrement the stream count and call uvc_delete explicitly if there
1686  * are no stream left.
1687  */
1688  if (atomic_dec_and_test(&dev->nstreams))
1689  uvc_delete(dev);
1690 }
1691 
1692 static int uvc_register_video(struct uvc_device *dev,
1693  struct uvc_streaming *stream)
1694 {
1695  struct video_device *vdev;
1696  int ret;
1697 
1698  /* Initialize the streaming interface with default streaming
1699  * parameters.
1700  */
1701  ret = uvc_video_init(stream);
1702  if (ret < 0) {
1703  uvc_printk(KERN_ERR, "Failed to initialize the device "
1704  "(%d).\n", ret);
1705  return ret;
1706  }
1707 
1708  uvc_debugfs_init_stream(stream);
1709 
1710  /* Register the device with V4L. */
1711  vdev = video_device_alloc();
1712  if (vdev == NULL) {
1713  uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1714  ret);
1715  return -ENOMEM;
1716  }
1717 
1718  /* We already hold a reference to dev->udev. The video device will be
1719  * unregistered before the reference is released, so we don't need to
1720  * get another one.
1721  */
1722  vdev->v4l2_dev = &dev->vdev;
1723  vdev->fops = &uvc_fops;
1724  vdev->release = uvc_release;
1725  if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1726  vdev->vfl_dir = VFL_DIR_TX;
1727  strlcpy(vdev->name, dev->name, sizeof vdev->name);
1728 
1729  /* Set the driver data before calling video_register_device, otherwise
1730  * uvc_v4l2_open might race us.
1731  */
1732  stream->vdev = vdev;
1733  video_set_drvdata(vdev, stream);
1734 
1735  ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1736  if (ret < 0) {
1737  uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1738  ret);
1739  stream->vdev = NULL;
1740  video_device_release(vdev);
1741  return ret;
1742  }
1743 
1744  atomic_inc(&dev->nstreams);
1745  return 0;
1746 }
1747 
1748 /*
1749  * Register all video devices in all chains.
1750  */
1751 static int uvc_register_terms(struct uvc_device *dev,
1752  struct uvc_video_chain *chain)
1753 {
1754  struct uvc_streaming *stream;
1755  struct uvc_entity *term;
1756  int ret;
1757 
1758  list_for_each_entry(term, &chain->entities, chain) {
1759  if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1760  continue;
1761 
1762  stream = uvc_stream_by_id(dev, term->id);
1763  if (stream == NULL) {
1764  uvc_printk(KERN_INFO, "No streaming interface found "
1765  "for terminal %u.", term->id);
1766  continue;
1767  }
1768 
1769  stream->chain = chain;
1770  ret = uvc_register_video(dev, stream);
1771  if (ret < 0)
1772  return ret;
1773 
1774  term->vdev = stream->vdev;
1775  }
1776 
1777  return 0;
1778 }
1779 
1780 static int uvc_register_chains(struct uvc_device *dev)
1781 {
1782  struct uvc_video_chain *chain;
1783  int ret;
1784 
1785  list_for_each_entry(chain, &dev->chains, list) {
1786  ret = uvc_register_terms(dev, chain);
1787  if (ret < 0)
1788  return ret;
1789 
1790 #ifdef CONFIG_MEDIA_CONTROLLER
1791  ret = uvc_mc_register_entities(chain);
1792  if (ret < 0) {
1793  uvc_printk(KERN_INFO, "Failed to register entites "
1794  "(%d).\n", ret);
1795  }
1796 #endif
1797  }
1798 
1799  return 0;
1800 }
1801 
1802 /* ------------------------------------------------------------------------
1803  * USB probe, disconnect, suspend and resume
1804  */
1805 
1806 static int uvc_probe(struct usb_interface *intf,
1807  const struct usb_device_id *id)
1808 {
1809  struct usb_device *udev = interface_to_usbdev(intf);
1810  struct uvc_device *dev;
1811  int ret;
1812 
1813  if (id->idVendor && id->idProduct)
1814  uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1815  "(%04x:%04x)\n", udev->devpath, id->idVendor,
1816  id->idProduct);
1817  else
1818  uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1819  udev->devpath);
1820 
1821  /* Allocate memory for the device and initialize it. */
1822  if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1823  return -ENOMEM;
1824 
1825  INIT_LIST_HEAD(&dev->entities);
1826  INIT_LIST_HEAD(&dev->chains);
1827  INIT_LIST_HEAD(&dev->streams);
1828  atomic_set(&dev->nstreams, 0);
1829  atomic_set(&dev->users, 0);
1830  atomic_set(&dev->nmappings, 0);
1831 
1832  dev->udev = usb_get_dev(udev);
1833  dev->intf = usb_get_intf(intf);
1834  dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1835  dev->quirks = (uvc_quirks_param == -1)
1836  ? id->driver_info : uvc_quirks_param;
1837 
1838  if (udev->product != NULL)
1839  strlcpy(dev->name, udev->product, sizeof dev->name);
1840  else
1841  snprintf(dev->name, sizeof dev->name,
1842  "UVC Camera (%04x:%04x)",
1843  le16_to_cpu(udev->descriptor.idVendor),
1844  le16_to_cpu(udev->descriptor.idProduct));
1845 
1846  /* Parse the Video Class control descriptor. */
1847  if (uvc_parse_control(dev) < 0) {
1848  uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1849  "descriptors.\n");
1850  goto error;
1851  }
1852 
1853  uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1854  dev->uvc_version >> 8, dev->uvc_version & 0xff,
1855  udev->product ? udev->product : "<unnamed>",
1856  le16_to_cpu(udev->descriptor.idVendor),
1857  le16_to_cpu(udev->descriptor.idProduct));
1858 
1859  if (dev->quirks != id->driver_info) {
1860  uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1861  "parameter for testing purpose.\n", dev->quirks);
1862  uvc_printk(KERN_INFO, "Please report required quirks to the "
1863  "linux-uvc-devel mailing list.\n");
1864  }
1865 
1866  /* Register the media and V4L2 devices. */
1867 #ifdef CONFIG_MEDIA_CONTROLLER
1868  dev->mdev.dev = &intf->dev;
1869  strlcpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
1870  if (udev->serial)
1871  strlcpy(dev->mdev.serial, udev->serial,
1872  sizeof(dev->mdev.serial));
1873  strcpy(dev->mdev.bus_info, udev->devpath);
1874  dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
1875  dev->mdev.driver_version = LINUX_VERSION_CODE;
1876  if (media_device_register(&dev->mdev) < 0)
1877  goto error;
1878 
1879  dev->vdev.mdev = &dev->mdev;
1880 #endif
1881  if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
1882  goto error;
1883 
1884  /* Initialize controls. */
1885  if (uvc_ctrl_init_device(dev) < 0)
1886  goto error;
1887 
1888  /* Scan the device for video chains. */
1889  if (uvc_scan_device(dev) < 0)
1890  goto error;
1891 
1892  /* Register video device nodes. */
1893  if (uvc_register_chains(dev) < 0)
1894  goto error;
1895 
1896  /* Save our data pointer in the interface data. */
1897  usb_set_intfdata(intf, dev);
1898 
1899  /* Initialize the interrupt URB. */
1900  if ((ret = uvc_status_init(dev)) < 0) {
1901  uvc_printk(KERN_INFO, "Unable to initialize the status "
1902  "endpoint (%d), status interrupt will not be "
1903  "supported.\n", ret);
1904  }
1905 
1906  uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1907  usb_enable_autosuspend(udev);
1908  return 0;
1909 
1910 error:
1911  uvc_unregister_video(dev);
1912  return -ENODEV;
1913 }
1914 
1915 static void uvc_disconnect(struct usb_interface *intf)
1916 {
1917  struct uvc_device *dev = usb_get_intfdata(intf);
1918 
1919  /* Set the USB interface data to NULL. This can be done outside the
1920  * lock, as there's no other reader.
1921  */
1922  usb_set_intfdata(intf, NULL);
1923 
1924  if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1926  return;
1927 
1928  dev->state |= UVC_DEV_DISCONNECTED;
1929 
1930  uvc_unregister_video(dev);
1931 }
1932 
1933 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1934 {
1935  struct uvc_device *dev = usb_get_intfdata(intf);
1936  struct uvc_streaming *stream;
1937 
1938  uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1939  intf->cur_altsetting->desc.bInterfaceNumber);
1940 
1941  /* Controls are cached on the fly so they don't need to be saved. */
1942  if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1944  return uvc_status_suspend(dev);
1945 
1946  list_for_each_entry(stream, &dev->streams, list) {
1947  if (stream->intf == intf)
1948  return uvc_video_suspend(stream);
1949  }
1950 
1951  uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1952  "mismatch.\n");
1953  return -EINVAL;
1954 }
1955 
1956 static int __uvc_resume(struct usb_interface *intf, int reset)
1957 {
1958  struct uvc_device *dev = usb_get_intfdata(intf);
1959  struct uvc_streaming *stream;
1960 
1961  uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1962  intf->cur_altsetting->desc.bInterfaceNumber);
1963 
1964  if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1966  if (reset) {
1967  int ret = uvc_ctrl_resume_device(dev);
1968 
1969  if (ret < 0)
1970  return ret;
1971  }
1972 
1973  return uvc_status_resume(dev);
1974  }
1975 
1976  list_for_each_entry(stream, &dev->streams, list) {
1977  if (stream->intf == intf)
1978  return uvc_video_resume(stream, reset);
1979  }
1980 
1981  uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1982  "mismatch.\n");
1983  return -EINVAL;
1984 }
1985 
1986 static int uvc_resume(struct usb_interface *intf)
1987 {
1988  return __uvc_resume(intf, 0);
1989 }
1990 
1991 static int uvc_reset_resume(struct usb_interface *intf)
1992 {
1993  return __uvc_resume(intf, 1);
1994 }
1995 
1996 /* ------------------------------------------------------------------------
1997  * Module parameters
1998  */
1999 
2000 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
2001 {
2003  return sprintf(buffer, "CLOCK_MONOTONIC");
2004  else
2005  return sprintf(buffer, "CLOCK_REALTIME");
2006 }
2007 
2008 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
2009 {
2010  if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
2011  val += strlen("clock_");
2012 
2013  if (strcasecmp(val, "monotonic") == 0)
2015  else if (strcasecmp(val, "realtime") == 0)
2017  else
2018  return -EINVAL;
2019 
2020  return 0;
2021 }
2022 
2023 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
2025 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
2027 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2028 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2029 MODULE_PARM_DESC(quirks, "Forced device quirks");
2031 MODULE_PARM_DESC(trace, "Trace level bitmask");
2033 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2034 
2035 /* ------------------------------------------------------------------------
2036  * Driver initialization and cleanup
2037  */
2038 
2039 /*
2040  * The Logitech cameras listed below have their interface class set to
2041  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
2042  * though they are compliant.
2043  */
2044 static struct usb_device_id uvc_ids[] = {
2045  /* LogiLink Wireless Webcam */
2046  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2047  | USB_DEVICE_ID_MATCH_INT_INFO,
2048  .idVendor = 0x0416,
2049  .idProduct = 0xa91a,
2050  .bInterfaceClass = USB_CLASS_VIDEO,
2051  .bInterfaceSubClass = 1,
2052  .bInterfaceProtocol = 0,
2053  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2054  /* Genius eFace 2025 */
2055  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2056  | USB_DEVICE_ID_MATCH_INT_INFO,
2057  .idVendor = 0x0458,
2058  .idProduct = 0x706e,
2059  .bInterfaceClass = USB_CLASS_VIDEO,
2060  .bInterfaceSubClass = 1,
2061  .bInterfaceProtocol = 0,
2062  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2063  /* Microsoft Lifecam NX-6000 */
2064  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2065  | USB_DEVICE_ID_MATCH_INT_INFO,
2066  .idVendor = 0x045e,
2067  .idProduct = 0x00f8,
2068  .bInterfaceClass = USB_CLASS_VIDEO,
2069  .bInterfaceSubClass = 1,
2070  .bInterfaceProtocol = 0,
2071  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2072  /* Microsoft Lifecam VX-7000 */
2073  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2074  | USB_DEVICE_ID_MATCH_INT_INFO,
2075  .idVendor = 0x045e,
2076  .idProduct = 0x0723,
2077  .bInterfaceClass = USB_CLASS_VIDEO,
2078  .bInterfaceSubClass = 1,
2079  .bInterfaceProtocol = 0,
2080  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2081  /* Logitech Quickcam Fusion */
2082  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2083  | USB_DEVICE_ID_MATCH_INT_INFO,
2084  .idVendor = 0x046d,
2085  .idProduct = 0x08c1,
2086  .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2087  .bInterfaceSubClass = 1,
2088  .bInterfaceProtocol = 0 },
2089  /* Logitech Quickcam Orbit MP */
2090  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2091  | USB_DEVICE_ID_MATCH_INT_INFO,
2092  .idVendor = 0x046d,
2093  .idProduct = 0x08c2,
2094  .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2095  .bInterfaceSubClass = 1,
2096  .bInterfaceProtocol = 0 },
2097  /* Logitech Quickcam Pro for Notebook */
2098  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2099  | USB_DEVICE_ID_MATCH_INT_INFO,
2100  .idVendor = 0x046d,
2101  .idProduct = 0x08c3,
2102  .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2103  .bInterfaceSubClass = 1,
2104  .bInterfaceProtocol = 0 },
2105  /* Logitech Quickcam Pro 5000 */
2106  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2107  | USB_DEVICE_ID_MATCH_INT_INFO,
2108  .idVendor = 0x046d,
2109  .idProduct = 0x08c5,
2110  .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2111  .bInterfaceSubClass = 1,
2112  .bInterfaceProtocol = 0 },
2113  /* Logitech Quickcam OEM Dell Notebook */
2114  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2115  | USB_DEVICE_ID_MATCH_INT_INFO,
2116  .idVendor = 0x046d,
2117  .idProduct = 0x08c6,
2118  .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2119  .bInterfaceSubClass = 1,
2120  .bInterfaceProtocol = 0 },
2121  /* Logitech Quickcam OEM Cisco VT Camera II */
2122  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2123  | USB_DEVICE_ID_MATCH_INT_INFO,
2124  .idVendor = 0x046d,
2125  .idProduct = 0x08c7,
2126  .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2127  .bInterfaceSubClass = 1,
2128  .bInterfaceProtocol = 0 },
2129  /* Chicony CNF7129 (Asus EEE 100HE) */
2130  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2131  | USB_DEVICE_ID_MATCH_INT_INFO,
2132  .idVendor = 0x04f2,
2133  .idProduct = 0xb071,
2134  .bInterfaceClass = USB_CLASS_VIDEO,
2135  .bInterfaceSubClass = 1,
2136  .bInterfaceProtocol = 0,
2137  .driver_info = UVC_QUIRK_RESTRICT_FRAME_RATE },
2138  /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2139  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2140  | USB_DEVICE_ID_MATCH_INT_INFO,
2141  .idVendor = 0x058f,
2142  .idProduct = 0x3820,
2143  .bInterfaceClass = USB_CLASS_VIDEO,
2144  .bInterfaceSubClass = 1,
2145  .bInterfaceProtocol = 0,
2146  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2147  /* Dell XPS m1530 */
2148  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2149  | USB_DEVICE_ID_MATCH_INT_INFO,
2150  .idVendor = 0x05a9,
2151  .idProduct = 0x2640,
2152  .bInterfaceClass = USB_CLASS_VIDEO,
2153  .bInterfaceSubClass = 1,
2154  .bInterfaceProtocol = 0,
2155  .driver_info = UVC_QUIRK_PROBE_DEF },
2156  /* Apple Built-In iSight */
2157  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2158  | USB_DEVICE_ID_MATCH_INT_INFO,
2159  .idVendor = 0x05ac,
2160  .idProduct = 0x8501,
2161  .bInterfaceClass = USB_CLASS_VIDEO,
2162  .bInterfaceSubClass = 1,
2163  .bInterfaceProtocol = 0,
2164  .driver_info = UVC_QUIRK_PROBE_MINMAX
2166  /* Foxlink ("HP Webcam" on HP Mini 5103) */
2167  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2168  | USB_DEVICE_ID_MATCH_INT_INFO,
2169  .idVendor = 0x05c8,
2170  .idProduct = 0x0403,
2171  .bInterfaceClass = USB_CLASS_VIDEO,
2172  .bInterfaceSubClass = 1,
2173  .bInterfaceProtocol = 0,
2174  .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2175  /* Genesys Logic USB 2.0 PC Camera */
2176  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2177  | USB_DEVICE_ID_MATCH_INT_INFO,
2178  .idVendor = 0x05e3,
2179  .idProduct = 0x0505,
2180  .bInterfaceClass = USB_CLASS_VIDEO,
2181  .bInterfaceSubClass = 1,
2182  .bInterfaceProtocol = 0,
2183  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2184  /* Hercules Classic Silver */
2185  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2186  | USB_DEVICE_ID_MATCH_INT_INFO,
2187  .idVendor = 0x06f8,
2188  .idProduct = 0x300c,
2189  .bInterfaceClass = USB_CLASS_VIDEO,
2190  .bInterfaceSubClass = 1,
2191  .bInterfaceProtocol = 0,
2192  .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2193  /* ViMicro Vega */
2194  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2195  | USB_DEVICE_ID_MATCH_INT_INFO,
2196  .idVendor = 0x0ac8,
2197  .idProduct = 0x332d,
2198  .bInterfaceClass = USB_CLASS_VIDEO,
2199  .bInterfaceSubClass = 1,
2200  .bInterfaceProtocol = 0,
2201  .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2202  /* ViMicro - Minoru3D */
2203  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2204  | USB_DEVICE_ID_MATCH_INT_INFO,
2205  .idVendor = 0x0ac8,
2206  .idProduct = 0x3410,
2207  .bInterfaceClass = USB_CLASS_VIDEO,
2208  .bInterfaceSubClass = 1,
2209  .bInterfaceProtocol = 0,
2210  .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2211  /* ViMicro Venus - Minoru3D */
2212  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2213  | USB_DEVICE_ID_MATCH_INT_INFO,
2214  .idVendor = 0x0ac8,
2215  .idProduct = 0x3420,
2216  .bInterfaceClass = USB_CLASS_VIDEO,
2217  .bInterfaceSubClass = 1,
2218  .bInterfaceProtocol = 0,
2219  .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2220  /* Ophir Optronics - SPCAM 620U */
2221  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2222  | USB_DEVICE_ID_MATCH_INT_INFO,
2223  .idVendor = 0x0bd3,
2224  .idProduct = 0x0555,
2225  .bInterfaceClass = USB_CLASS_VIDEO,
2226  .bInterfaceSubClass = 1,
2227  .bInterfaceProtocol = 0,
2228  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2229  /* MT6227 */
2230  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2231  | USB_DEVICE_ID_MATCH_INT_INFO,
2232  .idVendor = 0x0e8d,
2233  .idProduct = 0x0004,
2234  .bInterfaceClass = USB_CLASS_VIDEO,
2235  .bInterfaceSubClass = 1,
2236  .bInterfaceProtocol = 0,
2237  .driver_info = UVC_QUIRK_PROBE_MINMAX
2238  | UVC_QUIRK_PROBE_DEF },
2239  /* IMC Networks (Medion Akoya) */
2240  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2241  | USB_DEVICE_ID_MATCH_INT_INFO,
2242  .idVendor = 0x13d3,
2243  .idProduct = 0x5103,
2244  .bInterfaceClass = USB_CLASS_VIDEO,
2245  .bInterfaceSubClass = 1,
2246  .bInterfaceProtocol = 0,
2247  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2248  /* JMicron USB2.0 XGA WebCam */
2249  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2250  | USB_DEVICE_ID_MATCH_INT_INFO,
2251  .idVendor = 0x152d,
2252  .idProduct = 0x0310,
2253  .bInterfaceClass = USB_CLASS_VIDEO,
2254  .bInterfaceSubClass = 1,
2255  .bInterfaceProtocol = 0,
2256  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2257  /* Syntek (HP Spartan) */
2258  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2259  | USB_DEVICE_ID_MATCH_INT_INFO,
2260  .idVendor = 0x174f,
2261  .idProduct = 0x5212,
2262  .bInterfaceClass = USB_CLASS_VIDEO,
2263  .bInterfaceSubClass = 1,
2264  .bInterfaceProtocol = 0,
2265  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2266  /* Syntek (Samsung Q310) */
2267  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2268  | USB_DEVICE_ID_MATCH_INT_INFO,
2269  .idVendor = 0x174f,
2270  .idProduct = 0x5931,
2271  .bInterfaceClass = USB_CLASS_VIDEO,
2272  .bInterfaceSubClass = 1,
2273  .bInterfaceProtocol = 0,
2274  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2275  /* Syntek (Packard Bell EasyNote MX52 */
2276  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2277  | USB_DEVICE_ID_MATCH_INT_INFO,
2278  .idVendor = 0x174f,
2279  .idProduct = 0x8a12,
2280  .bInterfaceClass = USB_CLASS_VIDEO,
2281  .bInterfaceSubClass = 1,
2282  .bInterfaceProtocol = 0,
2283  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2284  /* Syntek (Asus F9SG) */
2285  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2286  | USB_DEVICE_ID_MATCH_INT_INFO,
2287  .idVendor = 0x174f,
2288  .idProduct = 0x8a31,
2289  .bInterfaceClass = USB_CLASS_VIDEO,
2290  .bInterfaceSubClass = 1,
2291  .bInterfaceProtocol = 0,
2292  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2293  /* Syntek (Asus U3S) */
2294  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2295  | USB_DEVICE_ID_MATCH_INT_INFO,
2296  .idVendor = 0x174f,
2297  .idProduct = 0x8a33,
2298  .bInterfaceClass = USB_CLASS_VIDEO,
2299  .bInterfaceSubClass = 1,
2300  .bInterfaceProtocol = 0,
2301  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2302  /* Syntek (JAOtech Smart Terminal) */
2303  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2304  | USB_DEVICE_ID_MATCH_INT_INFO,
2305  .idVendor = 0x174f,
2306  .idProduct = 0x8a34,
2307  .bInterfaceClass = USB_CLASS_VIDEO,
2308  .bInterfaceSubClass = 1,
2309  .bInterfaceProtocol = 0,
2310  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2311  /* Miricle 307K */
2312  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2313  | USB_DEVICE_ID_MATCH_INT_INFO,
2314  .idVendor = 0x17dc,
2315  .idProduct = 0x0202,
2316  .bInterfaceClass = USB_CLASS_VIDEO,
2317  .bInterfaceSubClass = 1,
2318  .bInterfaceProtocol = 0,
2319  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2320  /* Lenovo Thinkpad SL400/SL500 */
2321  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2322  | USB_DEVICE_ID_MATCH_INT_INFO,
2323  .idVendor = 0x17ef,
2324  .idProduct = 0x480b,
2325  .bInterfaceClass = USB_CLASS_VIDEO,
2326  .bInterfaceSubClass = 1,
2327  .bInterfaceProtocol = 0,
2328  .driver_info = UVC_QUIRK_STREAM_NO_FID },
2329  /* Aveo Technology USB 2.0 Camera */
2330  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2331  | USB_DEVICE_ID_MATCH_INT_INFO,
2332  .idVendor = 0x1871,
2333  .idProduct = 0x0306,
2334  .bInterfaceClass = USB_CLASS_VIDEO,
2335  .bInterfaceSubClass = 1,
2336  .bInterfaceProtocol = 0,
2337  .driver_info = UVC_QUIRK_PROBE_MINMAX
2339  /* Ecamm Pico iMage */
2340  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2341  | USB_DEVICE_ID_MATCH_INT_INFO,
2342  .idVendor = 0x18cd,
2343  .idProduct = 0xcafe,
2344  .bInterfaceClass = USB_CLASS_VIDEO,
2345  .bInterfaceSubClass = 1,
2346  .bInterfaceProtocol = 0,
2347  .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2348  /* Manta MM-353 Plako */
2349  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2350  | USB_DEVICE_ID_MATCH_INT_INFO,
2351  .idVendor = 0x18ec,
2352  .idProduct = 0x3188,
2353  .bInterfaceClass = USB_CLASS_VIDEO,
2354  .bInterfaceSubClass = 1,
2355  .bInterfaceProtocol = 0,
2356  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2357  /* FSC WebCam V30S */
2358  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2359  | USB_DEVICE_ID_MATCH_INT_INFO,
2360  .idVendor = 0x18ec,
2361  .idProduct = 0x3288,
2362  .bInterfaceClass = USB_CLASS_VIDEO,
2363  .bInterfaceSubClass = 1,
2364  .bInterfaceProtocol = 0,
2365  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2366  /* Arkmicro unbranded */
2367  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2368  | USB_DEVICE_ID_MATCH_INT_INFO,
2369  .idVendor = 0x18ec,
2370  .idProduct = 0x3290,
2371  .bInterfaceClass = USB_CLASS_VIDEO,
2372  .bInterfaceSubClass = 1,
2373  .bInterfaceProtocol = 0,
2374  .driver_info = UVC_QUIRK_PROBE_DEF },
2375  /* The Imaging Source USB CCD cameras */
2376  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2377  | USB_DEVICE_ID_MATCH_INT_INFO,
2378  .idVendor = 0x199e,
2379  .idProduct = 0x8102,
2380  .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2381  .bInterfaceSubClass = 1,
2382  .bInterfaceProtocol = 0 },
2383  /* Bodelin ProScopeHR */
2384  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2386  | USB_DEVICE_ID_MATCH_INT_INFO,
2387  .idVendor = 0x19ab,
2388  .idProduct = 0x1000,
2389  .bcdDevice_hi = 0x0126,
2390  .bInterfaceClass = USB_CLASS_VIDEO,
2391  .bInterfaceSubClass = 1,
2392  .bInterfaceProtocol = 0,
2393  .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2394  /* MSI StarCam 370i */
2395  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2396  | USB_DEVICE_ID_MATCH_INT_INFO,
2397  .idVendor = 0x1b3b,
2398  .idProduct = 0x2951,
2399  .bInterfaceClass = USB_CLASS_VIDEO,
2400  .bInterfaceSubClass = 1,
2401  .bInterfaceProtocol = 0,
2402  .driver_info = UVC_QUIRK_PROBE_MINMAX },
2403  /* SiGma Micro USB Web Camera */
2404  { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2405  | USB_DEVICE_ID_MATCH_INT_INFO,
2406  .idVendor = 0x1c4f,
2407  .idProduct = 0x3000,
2408  .bInterfaceClass = USB_CLASS_VIDEO,
2409  .bInterfaceSubClass = 1,
2410  .bInterfaceProtocol = 0,
2411  .driver_info = UVC_QUIRK_PROBE_MINMAX
2413  /* Generic USB Video Class */
2414  { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2415  {}
2416 };
2417 
2418 MODULE_DEVICE_TABLE(usb, uvc_ids);
2419 
2421  .driver = {
2422  .name = "uvcvideo",
2423  .probe = uvc_probe,
2424  .disconnect = uvc_disconnect,
2425  .suspend = uvc_suspend,
2426  .resume = uvc_resume,
2427  .reset_resume = uvc_reset_resume,
2428  .id_table = uvc_ids,
2429  .supports_autosuspend = 1,
2430  },
2431 };
2432 
2433 static int __init uvc_init(void)
2434 {
2435  int ret;
2436 
2437  uvc_debugfs_init();
2438 
2439  ret = usb_register(&uvc_driver.driver);
2440  if (ret < 0) {
2442  return ret;
2443  }
2444 
2446  return 0;
2447 }
2448 
2449 static void __exit uvc_cleanup(void)
2450 {
2451  usb_deregister(&uvc_driver.driver);
2453 }
2454 
2455 module_init(uvc_init);
2456 module_exit(uvc_cleanup);
2457 
2460 MODULE_LICENSE("GPL");
2462