79d6821c4741cb37411f5f826830115690cfc46f
[linux-3.10.git] / drivers / media / video / uvc / uvc_driver.c
1 /*
2  *      uvc_driver.c  --  USB Video Class driver
3  *
4  *      Copyright (C) 2005-2008
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
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 /*
15  * This driver aims to support video input devices compliant with the 'USB
16  * Video Class' specification.
17  *
18  * The driver doesn't support the deprecated v4l1 interface. It implements the
19  * mmap capture method only, and doesn't do any image format conversion in
20  * software. If your user-space application doesn't support YUYV or MJPEG, fix
21  * it :-). Please note that the MJPEG data have been stripped from their
22  * Huffman tables (DHT marker), you will need to add it back if your JPEG
23  * codec can't handle MJPEG data.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/version.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/usb.h>
31 #include <linux/videodev2.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <asm/atomic.h>
35
36 #include <media/v4l2-common.h>
37
38 #include "uvcvideo.h"
39
40 #define DRIVER_AUTHOR           "Laurent Pinchart <laurent.pinchart@skynet.be>"
41 #define DRIVER_DESC             "USB Video Class driver"
42 #ifndef DRIVER_VERSION
43 #define DRIVER_VERSION          "v0.1.0"
44 #endif
45
46 static unsigned int uvc_quirks_param;
47 unsigned int uvc_trace_param;
48
49 /* ------------------------------------------------------------------------
50  * Control, formats, ...
51  */
52
53 static struct uvc_format_desc uvc_fmts[] = {
54         {
55                 .name           = "YUV 4:2:2 (YUYV)",
56                 .guid           = UVC_GUID_FORMAT_YUY2,
57                 .fcc            = V4L2_PIX_FMT_YUYV,
58         },
59         {
60                 .name           = "YUV 4:2:0 (NV12)",
61                 .guid           = UVC_GUID_FORMAT_NV12,
62                 .fcc            = V4L2_PIX_FMT_NV12,
63         },
64         {
65                 .name           = "MJPEG",
66                 .guid           = UVC_GUID_FORMAT_MJPEG,
67                 .fcc            = V4L2_PIX_FMT_MJPEG,
68         },
69         {
70                 .name           = "YVU 4:2:0 (YV12)",
71                 .guid           = UVC_GUID_FORMAT_YV12,
72                 .fcc            = V4L2_PIX_FMT_YVU420,
73         },
74         {
75                 .name           = "YUV 4:2:0 (I420)",
76                 .guid           = UVC_GUID_FORMAT_I420,
77                 .fcc            = V4L2_PIX_FMT_YUV420,
78         },
79         {
80                 .name           = "YUV 4:2:2 (UYVY)",
81                 .guid           = UVC_GUID_FORMAT_UYVY,
82                 .fcc            = V4L2_PIX_FMT_UYVY,
83         },
84         {
85                 .name           = "Greyscale",
86                 .guid           = UVC_GUID_FORMAT_Y800,
87                 .fcc            = V4L2_PIX_FMT_GREY,
88         },
89         {
90                 .name           = "RGB Bayer",
91                 .guid           = UVC_GUID_FORMAT_BY8,
92                 .fcc            = V4L2_PIX_FMT_SBGGR8,
93         },
94 };
95
96 /* ------------------------------------------------------------------------
97  * Utility functions
98  */
99
100 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
101                 __u8 epaddr)
102 {
103         struct usb_host_endpoint *ep;
104         unsigned int i;
105
106         for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
107                 ep = &alts->endpoint[i];
108                 if (ep->desc.bEndpointAddress == epaddr)
109                         return ep;
110         }
111
112         return NULL;
113 }
114
115 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
116 {
117         unsigned int len = ARRAY_SIZE(uvc_fmts);
118         unsigned int i;
119
120         for (i = 0; i < len; ++i) {
121                 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
122                         return &uvc_fmts[i];
123         }
124
125         return NULL;
126 }
127
128 static __u32 uvc_colorspace(const __u8 primaries)
129 {
130         static const __u8 colorprimaries[] = {
131                 0,
132                 V4L2_COLORSPACE_SRGB,
133                 V4L2_COLORSPACE_470_SYSTEM_M,
134                 V4L2_COLORSPACE_470_SYSTEM_BG,
135                 V4L2_COLORSPACE_SMPTE170M,
136                 V4L2_COLORSPACE_SMPTE240M,
137         };
138
139         if (primaries < ARRAY_SIZE(colorprimaries))
140                 return colorprimaries[primaries];
141
142         return 0;
143 }
144
145 /* Simplify a fraction using a simple continued fraction decomposition. The
146  * idea here is to convert fractions such as 333333/10000000 to 1/30 using
147  * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
148  * arbitrary parameters to remove non-significative terms from the simple
149  * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
150  * respectively seems to give nice results.
151  */
152 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
153                 unsigned int n_terms, unsigned int threshold)
154 {
155         uint32_t *an;
156         uint32_t x, y, r;
157         unsigned int i, n;
158
159         an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
160         if (an == NULL)
161                 return;
162
163         /* Convert the fraction to a simple continued fraction. See
164          * http://mathforum.org/dr.math/faq/faq.fractions.html
165          * Stop if the current term is bigger than or equal to the given
166          * threshold.
167          */
168         x = *numerator;
169         y = *denominator;
170
171         for (n = 0; n < n_terms && y != 0; ++n) {
172                 an[n] = x / y;
173                 if (an[n] >= threshold) {
174                         if (n < 2)
175                                 n++;
176                         break;
177                 }
178
179                 r = x - an[n] * y;
180                 x = y;
181                 y = r;
182         }
183
184         /* Expand the simple continued fraction back to an integer fraction. */
185         x = 0;
186         y = 1;
187
188         for (i = n; i > 0; --i) {
189                 r = y;
190                 y = an[i-1] * y + x;
191                 x = r;
192         }
193
194         *numerator = y;
195         *denominator = x;
196         kfree(an);
197 }
198
199 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
200  * to compute numerator / denominator * 10000000 using 32 bit fixed point
201  * arithmetic only.
202  */
203 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
204 {
205         uint32_t multiplier;
206
207         /* Saturate the result if the operation would overflow. */
208         if (denominator == 0 ||
209             numerator/denominator >= ((uint32_t)-1)/10000000)
210                 return (uint32_t)-1;
211
212         /* Divide both the denominator and the multiplier by two until
213          * numerator * multiplier doesn't overflow. If anyone knows a better
214          * algorithm please let me know.
215          */
216         multiplier = 10000000;
217         while (numerator > ((uint32_t)-1)/multiplier) {
218                 multiplier /= 2;
219                 denominator /= 2;
220         }
221
222         return denominator ? numerator * multiplier / denominator : 0;
223 }
224
225 /* ------------------------------------------------------------------------
226  * Terminal and unit management
227  */
228
229 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
230 {
231         struct uvc_entity *entity;
232
233         list_for_each_entry(entity, &dev->entities, list) {
234                 if (entity->id == id)
235                         return entity;
236         }
237
238         return NULL;
239 }
240
241 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
242         int id, struct uvc_entity *entity)
243 {
244         unsigned int i;
245
246         if (entity == NULL)
247                 entity = list_entry(&dev->entities, struct uvc_entity, list);
248
249         list_for_each_entry_continue(entity, &dev->entities, list) {
250                 switch (UVC_ENTITY_TYPE(entity)) {
251                 case TT_STREAMING:
252                         if (entity->output.bSourceID == id)
253                                 return entity;
254                         break;
255
256                 case VC_PROCESSING_UNIT:
257                         if (entity->processing.bSourceID == id)
258                                 return entity;
259                         break;
260
261                 case VC_SELECTOR_UNIT:
262                         for (i = 0; i < entity->selector.bNrInPins; ++i)
263                                 if (entity->selector.baSourceID[i] == id)
264                                         return entity;
265                         break;
266
267                 case VC_EXTENSION_UNIT:
268                         for (i = 0; i < entity->extension.bNrInPins; ++i)
269                                 if (entity->extension.baSourceID[i] == id)
270                                         return entity;
271                         break;
272                 }
273         }
274
275         return NULL;
276 }
277
278 /* ------------------------------------------------------------------------
279  * Descriptors handling
280  */
281
282 static int uvc_parse_format(struct uvc_device *dev,
283         struct uvc_streaming *streaming, struct uvc_format *format,
284         __u32 **intervals, unsigned char *buffer, int buflen)
285 {
286         struct usb_interface *intf = streaming->intf;
287         struct usb_host_interface *alts = intf->cur_altsetting;
288         struct uvc_format_desc *fmtdesc;
289         struct uvc_frame *frame;
290         const unsigned char *start = buffer;
291         unsigned int interval;
292         unsigned int i, n;
293         __u8 ftype;
294
295         format->type = buffer[2];
296         format->index = buffer[3];
297
298         switch (buffer[2]) {
299         case VS_FORMAT_UNCOMPRESSED:
300         case VS_FORMAT_FRAME_BASED:
301                 n = buffer[2] == VS_FORMAT_UNCOMPRESSED ? 27 : 28;
302                 if (buflen < n) {
303                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
304                                "interface %d FORMAT error\n",
305                                dev->udev->devnum,
306                                alts->desc.bInterfaceNumber);
307                         return -EINVAL;
308                 }
309
310                 /* Find the format descriptor from its GUID. */
311                 fmtdesc = uvc_format_by_guid(&buffer[5]);
312
313                 if (fmtdesc != NULL) {
314                         strncpy(format->name, fmtdesc->name,
315                                 sizeof format->name);
316                         format->fcc = fmtdesc->fcc;
317                 } else {
318                         uvc_printk(KERN_INFO, "Unknown video format "
319                                 UVC_GUID_FORMAT "\n",
320                                 UVC_GUID_ARGS(&buffer[5]));
321                         snprintf(format->name, sizeof format->name,
322                                 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
323                         format->fcc = 0;
324                 }
325
326                 format->bpp = buffer[21];
327                 if (buffer[2] == VS_FORMAT_UNCOMPRESSED) {
328                         ftype = VS_FRAME_UNCOMPRESSED;
329                 } else {
330                         ftype = VS_FRAME_FRAME_BASED;
331                         if (buffer[27])
332                                 format->flags = UVC_FMT_FLAG_COMPRESSED;
333                 }
334                 break;
335
336         case VS_FORMAT_MJPEG:
337                 if (buflen < 11) {
338                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
339                                "interface %d FORMAT error\n",
340                                dev->udev->devnum,
341                                alts->desc.bInterfaceNumber);
342                         return -EINVAL;
343                 }
344
345                 strncpy(format->name, "MJPEG", sizeof format->name);
346                 format->fcc = V4L2_PIX_FMT_MJPEG;
347                 format->flags = UVC_FMT_FLAG_COMPRESSED;
348                 format->bpp = 0;
349                 ftype = VS_FRAME_MJPEG;
350                 break;
351
352         case VS_FORMAT_DV:
353                 if (buflen < 9) {
354                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
355                                "interface %d FORMAT error\n",
356                                dev->udev->devnum,
357                                alts->desc.bInterfaceNumber);
358                         return -EINVAL;
359                 }
360
361                 switch (buffer[8] & 0x7f) {
362                 case 0:
363                         strncpy(format->name, "SD-DV", sizeof format->name);
364                         break;
365                 case 1:
366                         strncpy(format->name, "SDL-DV", sizeof format->name);
367                         break;
368                 case 2:
369                         strncpy(format->name, "HD-DV", sizeof format->name);
370                         break;
371                 default:
372                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
373                                "interface %d: unknown DV format %u\n",
374                                dev->udev->devnum,
375                                alts->desc.bInterfaceNumber, buffer[8]);
376                         return -EINVAL;
377                 }
378
379                 strncat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
380                         sizeof format->name);
381
382                 format->fcc = V4L2_PIX_FMT_DV;
383                 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
384                 format->bpp = 0;
385                 ftype = 0;
386
387                 /* Create a dummy frame descriptor. */
388                 frame = &format->frame[0];
389                 memset(&format->frame[0], 0, sizeof format->frame[0]);
390                 frame->bFrameIntervalType = 1;
391                 frame->dwDefaultFrameInterval = 1;
392                 frame->dwFrameInterval = *intervals;
393                 *(*intervals)++ = 1;
394                 format->nframes = 1;
395                 break;
396
397         case VS_FORMAT_MPEG2TS:
398         case VS_FORMAT_STREAM_BASED:
399                 /* Not supported yet. */
400         default:
401                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
402                        "interface %d unsupported format %u\n",
403                        dev->udev->devnum, alts->desc.bInterfaceNumber,
404                        buffer[2]);
405                 return -EINVAL;
406         }
407
408         uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
409
410         buflen -= buffer[0];
411         buffer += buffer[0];
412
413         /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
414          * based formats have frame descriptors.
415          */
416         while (buflen > 2 && buffer[2] == ftype) {
417                 frame = &format->frame[format->nframes];
418
419                 if (ftype != VS_FRAME_FRAME_BASED)
420                         n = buflen > 25 ? buffer[25] : 0;
421                 else
422                         n = buflen > 21 ? buffer[21] : 0;
423
424                 n = n ? n : 3;
425
426                 if (buflen < 26 + 4*n) {
427                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
428                                "interface %d FRAME error\n", dev->udev->devnum,
429                                alts->desc.bInterfaceNumber);
430                         return -EINVAL;
431                 }
432
433                 frame->bFrameIndex = buffer[3];
434                 frame->bmCapabilities = buffer[4];
435                 frame->wWidth = le16_to_cpup((__le16 *)&buffer[5]);
436                 frame->wHeight = le16_to_cpup((__le16 *)&buffer[7]);
437                 frame->dwMinBitRate = le32_to_cpup((__le32 *)&buffer[9]);
438                 frame->dwMaxBitRate = le32_to_cpup((__le32 *)&buffer[13]);
439                 if (ftype != VS_FRAME_FRAME_BASED) {
440                         frame->dwMaxVideoFrameBufferSize =
441                                 le32_to_cpup((__le32 *)&buffer[17]);
442                         frame->dwDefaultFrameInterval =
443                                 le32_to_cpup((__le32 *)&buffer[21]);
444                         frame->bFrameIntervalType = buffer[25];
445                 } else {
446                         frame->dwMaxVideoFrameBufferSize = 0;
447                         frame->dwDefaultFrameInterval =
448                                 le32_to_cpup((__le32 *)&buffer[17]);
449                         frame->bFrameIntervalType = buffer[21];
450                 }
451                 frame->dwFrameInterval = *intervals;
452
453                 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
454                  * completely. Observed behaviours range from setting the
455                  * value to 1.1x the actual frame size of hardwiring the
456                  * 16 low bits to 0. This results in a higher than necessary
457                  * memory usage as well as a wrong image size information. For
458                  * uncompressed formats this can be fixed by computing the
459                  * value from the frame size.
460                  */
461                 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
462                         frame->dwMaxVideoFrameBufferSize = format->bpp
463                                 * frame->wWidth * frame->wHeight / 8;
464
465                 /* Some bogus devices report dwMinFrameInterval equal to
466                  * dwMaxFrameInterval and have dwFrameIntervalStep set to
467                  * zero. Setting all null intervals to 1 fixes the problem and
468                  * some other divisions by zero which could happen.
469                  */
470                 for (i = 0; i < n; ++i) {
471                         interval = le32_to_cpup((__le32 *)&buffer[26+4*i]);
472                         *(*intervals)++ = interval ? interval : 1;
473                 }
474
475                 /* Make sure that the default frame interval stays between
476                  * the boundaries.
477                  */
478                 n -= frame->bFrameIntervalType ? 1 : 2;
479                 frame->dwDefaultFrameInterval =
480                         min(frame->dwFrameInterval[n],
481                             max(frame->dwFrameInterval[0],
482                                 frame->dwDefaultFrameInterval));
483
484                 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
485                         frame->wWidth, frame->wHeight,
486                         10000000/frame->dwDefaultFrameInterval,
487                         (100000000/frame->dwDefaultFrameInterval)%10);
488
489                 format->nframes++;
490                 buflen -= buffer[0];
491                 buffer += buffer[0];
492         }
493
494         if (buflen > 2 && buffer[2] == VS_STILL_IMAGE_FRAME) {
495                 buflen -= buffer[0];
496                 buffer += buffer[0];
497         }
498
499         if (buflen > 2 && buffer[2] == VS_COLORFORMAT) {
500                 if (buflen < 6) {
501                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
502                                "interface %d COLORFORMAT error\n",
503                                dev->udev->devnum,
504                                alts->desc.bInterfaceNumber);
505                         return -EINVAL;
506                 }
507
508                 format->colorspace = uvc_colorspace(buffer[3]);
509
510                 buflen -= buffer[0];
511                 buffer += buffer[0];
512         }
513
514         return buffer - start;
515 }
516
517 static int uvc_parse_streaming(struct uvc_device *dev,
518         struct usb_interface *intf)
519 {
520         struct uvc_streaming *streaming = NULL;
521         struct uvc_format *format;
522         struct uvc_frame *frame;
523         struct usb_host_interface *alts = &intf->altsetting[0];
524         unsigned char *_buffer, *buffer = alts->extra;
525         int _buflen, buflen = alts->extralen;
526         unsigned int nformats = 0, nframes = 0, nintervals = 0;
527         unsigned int size, i, n, p;
528         __u32 *interval;
529         __u16 psize;
530         int ret = -EINVAL;
531
532         if (intf->cur_altsetting->desc.bInterfaceSubClass
533                 != SC_VIDEOSTREAMING) {
534                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
535                         "video streaming interface\n", dev->udev->devnum,
536                         intf->altsetting[0].desc.bInterfaceNumber);
537                 return -EINVAL;
538         }
539
540         if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
541                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
542                         "claimed\n", dev->udev->devnum,
543                         intf->altsetting[0].desc.bInterfaceNumber);
544                 return -EINVAL;
545         }
546
547         streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
548         if (streaming == NULL) {
549                 usb_driver_release_interface(&uvc_driver.driver, intf);
550                 return -EINVAL;
551         }
552
553         mutex_init(&streaming->mutex);
554         streaming->intf = usb_get_intf(intf);
555         streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
556
557         /* The Pico iMage webcam has its class-specific interface descriptors
558          * after the endpoint descriptors.
559          */
560         if (buflen == 0) {
561                 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
562                         struct usb_host_endpoint *ep = &alts->endpoint[i];
563
564                         if (ep->extralen == 0)
565                                 continue;
566
567                         if (ep->extralen > 2 &&
568                             ep->extra[1] == USB_DT_CS_INTERFACE) {
569                                 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
570                                         "from endpoint %u.\n", i);
571                                 buffer = alts->endpoint[i].extra;
572                                 buflen = alts->endpoint[i].extralen;
573                                 break;
574                         }
575                 }
576         }
577
578         /* Skip the standard interface descriptors. */
579         while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
580                 buflen -= buffer[0];
581                 buffer += buffer[0];
582         }
583
584         if (buflen <= 2) {
585                 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
586                         "interface descriptors found.\n");
587                 goto error;
588         }
589
590         /* Parse the header descriptor. */
591         if (buffer[2] == VS_OUTPUT_HEADER) {
592                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
593                         "%d OUTPUT HEADER descriptor is not supported.\n",
594                         dev->udev->devnum, alts->desc.bInterfaceNumber);
595                 goto error;
596         } else if (buffer[2] == VS_INPUT_HEADER) {
597                 p = buflen >= 5 ? buffer[3] : 0;
598                 n = buflen >= 12 ? buffer[12] : 0;
599
600                 if (buflen < 13 + p*n || buffer[2] != VS_INPUT_HEADER) {
601                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
602                                 "interface %d INPUT HEADER descriptor is "
603                                 "invalid.\n", dev->udev->devnum,
604                                 alts->desc.bInterfaceNumber);
605                         goto error;
606                 }
607
608                 streaming->header.bNumFormats = p;
609                 streaming->header.bEndpointAddress = buffer[6];
610                 streaming->header.bmInfo = buffer[7];
611                 streaming->header.bTerminalLink = buffer[8];
612                 streaming->header.bStillCaptureMethod = buffer[9];
613                 streaming->header.bTriggerSupport = buffer[10];
614                 streaming->header.bTriggerUsage = buffer[11];
615                 streaming->header.bControlSize = n;
616
617                 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
618                 if (streaming->header.bmaControls == NULL) {
619                         ret = -ENOMEM;
620                         goto error;
621                 }
622
623                 memcpy(streaming->header.bmaControls, &buffer[13], p*n);
624         } else {
625                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
626                         "%d HEADER descriptor not found.\n", dev->udev->devnum,
627                         alts->desc.bInterfaceNumber);
628                 goto error;
629         }
630
631         buflen -= buffer[0];
632         buffer += buffer[0];
633
634         _buffer = buffer;
635         _buflen = buflen;
636
637         /* Count the format and frame descriptors. */
638         while (_buflen > 2) {
639                 switch (_buffer[2]) {
640                 case VS_FORMAT_UNCOMPRESSED:
641                 case VS_FORMAT_MJPEG:
642                 case VS_FORMAT_FRAME_BASED:
643                         nformats++;
644                         break;
645
646                 case VS_FORMAT_DV:
647                         /* DV format has no frame descriptor. We will create a
648                          * dummy frame descriptor with a dummy frame interval.
649                          */
650                         nformats++;
651                         nframes++;
652                         nintervals++;
653                         break;
654
655                 case VS_FORMAT_MPEG2TS:
656                 case VS_FORMAT_STREAM_BASED:
657                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
658                                 "interface %d FORMAT %u is not supported.\n",
659                                 dev->udev->devnum,
660                                 alts->desc.bInterfaceNumber, _buffer[2]);
661                         break;
662
663                 case VS_FRAME_UNCOMPRESSED:
664                 case VS_FRAME_MJPEG:
665                         nframes++;
666                         if (_buflen > 25)
667                                 nintervals += _buffer[25] ? _buffer[25] : 3;
668                         break;
669
670                 case VS_FRAME_FRAME_BASED:
671                         nframes++;
672                         if (_buflen > 21)
673                                 nintervals += _buffer[21] ? _buffer[21] : 3;
674                         break;
675                 }
676
677                 _buflen -= _buffer[0];
678                 _buffer += _buffer[0];
679         }
680
681         if (nformats == 0) {
682                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
683                         "%d has no supported formats defined.\n",
684                         dev->udev->devnum, alts->desc.bInterfaceNumber);
685                 goto error;
686         }
687
688         size = nformats * sizeof *format + nframes * sizeof *frame
689              + nintervals * sizeof *interval;
690         format = kzalloc(size, GFP_KERNEL);
691         if (format == NULL) {
692                 ret = -ENOMEM;
693                 goto error;
694         }
695
696         frame = (struct uvc_frame *)&format[nformats];
697         interval = (__u32 *)&frame[nframes];
698
699         streaming->format = format;
700         streaming->nformats = nformats;
701
702         /* Parse the format descriptors. */
703         while (buflen > 2) {
704                 switch (buffer[2]) {
705                 case VS_FORMAT_UNCOMPRESSED:
706                 case VS_FORMAT_MJPEG:
707                 case VS_FORMAT_DV:
708                 case VS_FORMAT_FRAME_BASED:
709                         format->frame = frame;
710                         ret = uvc_parse_format(dev, streaming, format,
711                                 &interval, buffer, buflen);
712                         if (ret < 0)
713                                 goto error;
714
715                         frame += format->nframes;
716                         format++;
717
718                         buflen -= ret;
719                         buffer += ret;
720                         continue;
721
722                 default:
723                         break;
724                 }
725
726                 buflen -= buffer[0];
727                 buffer += buffer[0];
728         }
729
730         /* Parse the alternate settings to find the maximum bandwidth. */
731         for (i = 0; i < intf->num_altsetting; ++i) {
732                 struct usb_host_endpoint *ep;
733                 alts = &intf->altsetting[i];
734                 ep = uvc_find_endpoint(alts,
735                                 streaming->header.bEndpointAddress);
736                 if (ep == NULL)
737                         continue;
738
739                 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
740                 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
741                 if (psize > streaming->maxpsize)
742                         streaming->maxpsize = psize;
743         }
744
745         list_add_tail(&streaming->list, &dev->streaming);
746         return 0;
747
748 error:
749         usb_driver_release_interface(&uvc_driver.driver, intf);
750         usb_put_intf(intf);
751         kfree(streaming->format);
752         kfree(streaming->header.bmaControls);
753         kfree(streaming);
754         return ret;
755 }
756
757 /* Parse vendor-specific extensions. */
758 static int uvc_parse_vendor_control(struct uvc_device *dev,
759         const unsigned char *buffer, int buflen)
760 {
761         struct usb_device *udev = dev->udev;
762         struct usb_host_interface *alts = dev->intf->cur_altsetting;
763         struct uvc_entity *unit;
764         unsigned int n, p;
765         int handled = 0;
766
767         switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
768         case 0x046d:            /* Logitech */
769                 if (buffer[1] != 0x41 || buffer[2] != 0x01)
770                         break;
771
772                 /* Logitech implements several vendor specific functions
773                  * through vendor specific extension units (LXU).
774                  *
775                  * The LXU descriptors are similar to XU descriptors
776                  * (see "USB Device Video Class for Video Devices", section
777                  * 3.7.2.6 "Extension Unit Descriptor") with the following
778                  * differences:
779                  *
780                  * ----------------------------------------------------------
781                  * 0            bLength         1        Number
782                  *      Size of this descriptor, in bytes: 24+p+n*2
783                  * ----------------------------------------------------------
784                  * 23+p+n       bmControlsType  N       Bitmap
785                  *      Individual bits in the set are defined:
786                  *      0: Absolute
787                  *      1: Relative
788                  *
789                  *      This bitset is mapped exactly the same as bmControls.
790                  * ----------------------------------------------------------
791                  * 23+p+n*2     bReserved       1       Boolean
792                  * ----------------------------------------------------------
793                  * 24+p+n*2     iExtension      1       Index
794                  *      Index of a string descriptor that describes this
795                  *      extension unit.
796                  * ----------------------------------------------------------
797                  */
798                 p = buflen >= 22 ? buffer[21] : 0;
799                 n = buflen >= 25 + p ? buffer[22+p] : 0;
800
801                 if (buflen < 25 + p + 2*n) {
802                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
803                                 "interface %d EXTENSION_UNIT error\n",
804                                 udev->devnum, alts->desc.bInterfaceNumber);
805                         break;
806                 }
807
808                 unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
809                 if (unit == NULL)
810                         return -ENOMEM;
811
812                 unit->id = buffer[3];
813                 unit->type = VC_EXTENSION_UNIT;
814                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
815                 unit->extension.bNumControls = buffer[20];
816                 unit->extension.bNrInPins =
817                         le16_to_cpup((__le16 *)&buffer[21]);
818                 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
819                 memcpy(unit->extension.baSourceID, &buffer[22], p);
820                 unit->extension.bControlSize = buffer[22+p];
821                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
822                 unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
823                                                + p + n;
824                 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
825
826                 if (buffer[24+p+2*n] != 0)
827                         usb_string(udev, buffer[24+p+2*n], unit->name,
828                                    sizeof unit->name);
829                 else
830                         sprintf(unit->name, "Extension %u", buffer[3]);
831
832                 list_add_tail(&unit->list, &dev->entities);
833                 handled = 1;
834                 break;
835         }
836
837         return handled;
838 }
839
840 static int uvc_parse_standard_control(struct uvc_device *dev,
841         const unsigned char *buffer, int buflen)
842 {
843         struct usb_device *udev = dev->udev;
844         struct uvc_entity *unit, *term;
845         struct usb_interface *intf;
846         struct usb_host_interface *alts = dev->intf->cur_altsetting;
847         unsigned int i, n, p, len;
848         __u16 type;
849
850         switch (buffer[2]) {
851         case VC_HEADER:
852                 n = buflen >= 12 ? buffer[11] : 0;
853
854                 if (buflen < 12 || buflen < 12 + n) {
855                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
856                                 "interface %d HEADER error\n", udev->devnum,
857                                 alts->desc.bInterfaceNumber);
858                         return -EINVAL;
859                 }
860
861                 dev->uvc_version = le16_to_cpup((__le16 *)&buffer[3]);
862                 dev->clock_frequency = le32_to_cpup((__le32 *)&buffer[7]);
863
864                 /* Parse all USB Video Streaming interfaces. */
865                 for (i = 0; i < n; ++i) {
866                         intf = usb_ifnum_to_if(udev, buffer[12+i]);
867                         if (intf == NULL) {
868                                 uvc_trace(UVC_TRACE_DESCR, "device %d "
869                                         "interface %d doesn't exists\n",
870                                         udev->devnum, i);
871                                 continue;
872                         }
873
874                         uvc_parse_streaming(dev, intf);
875                 }
876                 break;
877
878         case VC_INPUT_TERMINAL:
879                 if (buflen < 8) {
880                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
881                                 "interface %d INPUT_TERMINAL error\n",
882                                 udev->devnum, alts->desc.bInterfaceNumber);
883                         return -EINVAL;
884                 }
885
886                 /* Make sure the terminal type MSB is not null, otherwise it
887                  * could be confused with a unit.
888                  */
889                 type = le16_to_cpup((__le16 *)&buffer[4]);
890                 if ((type & 0xff00) == 0) {
891                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
892                                 "interface %d INPUT_TERMINAL %d has invalid "
893                                 "type 0x%04x, skipping\n", udev->devnum,
894                                 alts->desc.bInterfaceNumber,
895                                 buffer[3], type);
896                         return 0;
897                 }
898
899                 n = 0;
900                 p = 0;
901                 len = 8;
902
903                 if (type == ITT_CAMERA) {
904                         n = buflen >= 15 ? buffer[14] : 0;
905                         len = 15;
906
907                 } else if (type == ITT_MEDIA_TRANSPORT_INPUT) {
908                         n = buflen >= 9 ? buffer[8] : 0;
909                         p = buflen >= 10 + n ? buffer[9+n] : 0;
910                         len = 10;
911                 }
912
913                 if (buflen < len + n + p) {
914                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
915                                 "interface %d INPUT_TERMINAL error\n",
916                                 udev->devnum, alts->desc.bInterfaceNumber);
917                         return -EINVAL;
918                 }
919
920                 term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
921                 if (term == NULL)
922                         return -ENOMEM;
923
924                 term->id = buffer[3];
925                 term->type = type | UVC_TERM_INPUT;
926
927                 if (UVC_ENTITY_TYPE(term) == ITT_CAMERA) {
928                         term->camera.bControlSize = n;
929                         term->camera.bmControls = (__u8 *)term + sizeof *term;
930                         term->camera.wObjectiveFocalLengthMin =
931                                 le16_to_cpup((__le16 *)&buffer[8]);
932                         term->camera.wObjectiveFocalLengthMax =
933                                 le16_to_cpup((__le16 *)&buffer[10]);
934                         term->camera.wOcularFocalLength =
935                                 le16_to_cpup((__le16 *)&buffer[12]);
936                         memcpy(term->camera.bmControls, &buffer[15], n);
937                 } else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT) {
938                         term->media.bControlSize = n;
939                         term->media.bmControls = (__u8 *)term + sizeof *term;
940                         term->media.bTransportModeSize = p;
941                         term->media.bmTransportModes = (__u8 *)term
942                                                      + sizeof *term + n;
943                         memcpy(term->media.bmControls, &buffer[9], n);
944                         memcpy(term->media.bmTransportModes, &buffer[10+n], p);
945                 }
946
947                 if (buffer[7] != 0)
948                         usb_string(udev, buffer[7], term->name,
949                                    sizeof term->name);
950                 else if (UVC_ENTITY_TYPE(term) == ITT_CAMERA)
951                         sprintf(term->name, "Camera %u", buffer[3]);
952                 else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT)
953                         sprintf(term->name, "Media %u", buffer[3]);
954                 else
955                         sprintf(term->name, "Input %u", buffer[3]);
956
957                 list_add_tail(&term->list, &dev->entities);
958                 break;
959
960         case VC_OUTPUT_TERMINAL:
961                 if (buflen < 9) {
962                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
963                                 "interface %d OUTPUT_TERMINAL error\n",
964                                 udev->devnum, alts->desc.bInterfaceNumber);
965                         return -EINVAL;
966                 }
967
968                 /* Make sure the terminal type MSB is not null, otherwise it
969                  * could be confused with a unit.
970                  */
971                 type = le16_to_cpup((__le16 *)&buffer[4]);
972                 if ((type & 0xff00) == 0) {
973                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
974                                 "interface %d OUTPUT_TERMINAL %d has invalid "
975                                 "type 0x%04x, skipping\n", udev->devnum,
976                                 alts->desc.bInterfaceNumber, buffer[3], type);
977                         return 0;
978                 }
979
980                 term = kzalloc(sizeof *term, GFP_KERNEL);
981                 if (term == NULL)
982                         return -ENOMEM;
983
984                 term->id = buffer[3];
985                 term->type = type | UVC_TERM_OUTPUT;
986                 term->output.bSourceID = buffer[7];
987
988                 if (buffer[8] != 0)
989                         usb_string(udev, buffer[8], term->name,
990                                    sizeof term->name);
991                 else
992                         sprintf(term->name, "Output %u", buffer[3]);
993
994                 list_add_tail(&term->list, &dev->entities);
995                 break;
996
997         case VC_SELECTOR_UNIT:
998                 p = buflen >= 5 ? buffer[4] : 0;
999
1000                 if (buflen < 5 || buflen < 6 + p) {
1001                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1002                                 "interface %d SELECTOR_UNIT error\n",
1003                                 udev->devnum, alts->desc.bInterfaceNumber);
1004                         return -EINVAL;
1005                 }
1006
1007                 unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1008                 if (unit == NULL)
1009                         return -ENOMEM;
1010
1011                 unit->id = buffer[3];
1012                 unit->type = buffer[2];
1013                 unit->selector.bNrInPins = buffer[4];
1014                 unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1015                 memcpy(unit->selector.baSourceID, &buffer[5], p);
1016
1017                 if (buffer[5+p] != 0)
1018                         usb_string(udev, buffer[5+p], unit->name,
1019                                    sizeof unit->name);
1020                 else
1021                         sprintf(unit->name, "Selector %u", buffer[3]);
1022
1023                 list_add_tail(&unit->list, &dev->entities);
1024                 break;
1025
1026         case VC_PROCESSING_UNIT:
1027                 n = buflen >= 8 ? buffer[7] : 0;
1028                 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1029
1030                 if (buflen < p + n) {
1031                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1032                                 "interface %d PROCESSING_UNIT error\n",
1033                                 udev->devnum, alts->desc.bInterfaceNumber);
1034                         return -EINVAL;
1035                 }
1036
1037                 unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1038                 if (unit == NULL)
1039                         return -ENOMEM;
1040
1041                 unit->id = buffer[3];
1042                 unit->type = buffer[2];
1043                 unit->processing.bSourceID = buffer[4];
1044                 unit->processing.wMaxMultiplier =
1045                         le16_to_cpup((__le16 *)&buffer[5]);
1046                 unit->processing.bControlSize = buffer[7];
1047                 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1048                 memcpy(unit->processing.bmControls, &buffer[8], n);
1049                 if (dev->uvc_version >= 0x0110)
1050                         unit->processing.bmVideoStandards = buffer[9+n];
1051
1052                 if (buffer[8+n] != 0)
1053                         usb_string(udev, buffer[8+n], unit->name,
1054                                    sizeof unit->name);
1055                 else
1056                         sprintf(unit->name, "Processing %u", buffer[3]);
1057
1058                 list_add_tail(&unit->list, &dev->entities);
1059                 break;
1060
1061         case VC_EXTENSION_UNIT:
1062                 p = buflen >= 22 ? buffer[21] : 0;
1063                 n = buflen >= 24 + p ? buffer[22+p] : 0;
1064
1065                 if (buflen < 24 + p + n) {
1066                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1067                                 "interface %d EXTENSION_UNIT error\n",
1068                                 udev->devnum, alts->desc.bInterfaceNumber);
1069                         return -EINVAL;
1070                 }
1071
1072                 unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1073                 if (unit == NULL)
1074                         return -ENOMEM;
1075
1076                 unit->id = buffer[3];
1077                 unit->type = buffer[2];
1078                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1079                 unit->extension.bNumControls = buffer[20];
1080                 unit->extension.bNrInPins =
1081                         le16_to_cpup((__le16 *)&buffer[21]);
1082                 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1083                 memcpy(unit->extension.baSourceID, &buffer[22], p);
1084                 unit->extension.bControlSize = buffer[22+p];
1085                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1086                 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1087
1088                 if (buffer[23+p+n] != 0)
1089                         usb_string(udev, buffer[23+p+n], unit->name,
1090                                    sizeof unit->name);
1091                 else
1092                         sprintf(unit->name, "Extension %u", buffer[3]);
1093
1094                 list_add_tail(&unit->list, &dev->entities);
1095                 break;
1096
1097         default:
1098                 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1099                         "descriptor (%u)\n", buffer[2]);
1100                 break;
1101         }
1102
1103         return 0;
1104 }
1105
1106 static int uvc_parse_control(struct uvc_device *dev)
1107 {
1108         struct usb_host_interface *alts = dev->intf->cur_altsetting;
1109         unsigned char *buffer = alts->extra;
1110         int buflen = alts->extralen;
1111         int ret;
1112
1113         /* Parse the default alternate setting only, as the UVC specification
1114          * defines a single alternate setting, the default alternate setting
1115          * zero.
1116          */
1117
1118         while (buflen > 2) {
1119                 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1120                     buffer[1] != USB_DT_CS_INTERFACE)
1121                         goto next_descriptor;
1122
1123                 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1124                         return ret;
1125
1126 next_descriptor:
1127                 buflen -= buffer[0];
1128                 buffer += buffer[0];
1129         }
1130
1131         /* Check if the optional status endpoint is present. */
1132         if (alts->desc.bNumEndpoints == 1) {
1133                 struct usb_host_endpoint *ep = &alts->endpoint[0];
1134                 struct usb_endpoint_descriptor *desc = &ep->desc;
1135
1136                 if (usb_endpoint_is_int_in(desc) &&
1137                     le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1138                     desc->bInterval != 0) {
1139                         uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1140                                 "(addr %02x).\n", desc->bEndpointAddress);
1141                         dev->int_ep = ep;
1142                 }
1143         }
1144
1145         return 0;
1146 }
1147
1148 /* ------------------------------------------------------------------------
1149  * USB probe and disconnect
1150  */
1151
1152 /*
1153  * Unregister the video devices.
1154  */
1155 static void uvc_unregister_video(struct uvc_device *dev)
1156 {
1157         if (dev->video.vdev) {
1158                 if (dev->video.vdev->minor == -1)
1159                         video_device_release(dev->video.vdev);
1160                 else
1161                         video_unregister_device(dev->video.vdev);
1162                 dev->video.vdev = NULL;
1163         }
1164 }
1165
1166 /*
1167  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1168  * and containing the following units:
1169  *
1170  * - a USB Streaming Output Terminal
1171  * - zero or one Processing Unit
1172  * - zero, one or mode single-input Selector Units
1173  * - zero or one multiple-input Selector Units, provided all inputs are
1174  *   connected to input terminals
1175  * - zero, one or mode single-input Extension Units
1176  * - one Camera Input Terminal, or one or more External terminals.
1177  *
1178  * A side forward scan is made on each detected entity to check for additional
1179  * extension units.
1180  */
1181 static int uvc_scan_chain_entity(struct uvc_video_device *video,
1182         struct uvc_entity *entity)
1183 {
1184         switch (UVC_ENTITY_TYPE(entity)) {
1185         case VC_EXTENSION_UNIT:
1186                 if (uvc_trace_param & UVC_TRACE_PROBE)
1187                         printk(" <- XU %d", entity->id);
1188
1189                 if (entity->extension.bNrInPins != 1) {
1190                         uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1191                                 "than 1 input pin.\n", entity->id);
1192                         return -1;
1193                 }
1194
1195                 list_add_tail(&entity->chain, &video->extensions);
1196                 break;
1197
1198         case VC_PROCESSING_UNIT:
1199                 if (uvc_trace_param & UVC_TRACE_PROBE)
1200                         printk(" <- PU %d", entity->id);
1201
1202                 if (video->processing != NULL) {
1203                         uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1204                                 "Processing Units in chain.\n");
1205                         return -1;
1206                 }
1207
1208                 video->processing = entity;
1209                 break;
1210
1211         case VC_SELECTOR_UNIT:
1212                 if (uvc_trace_param & UVC_TRACE_PROBE)
1213                         printk(" <- SU %d", entity->id);
1214
1215                 /* Single-input selector units are ignored. */
1216                 if (entity->selector.bNrInPins == 1)
1217                         break;
1218
1219                 if (video->selector != NULL) {
1220                         uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1221                                 "Units in chain.\n");
1222                         return -1;
1223                 }
1224
1225                 video->selector = entity;
1226                 break;
1227
1228         case ITT_VENDOR_SPECIFIC:
1229         case ITT_CAMERA:
1230         case ITT_MEDIA_TRANSPORT_INPUT:
1231                 if (uvc_trace_param & UVC_TRACE_PROBE)
1232                         printk(" <- IT %d\n", entity->id);
1233
1234                 list_add_tail(&entity->chain, &video->iterms);
1235                 break;
1236
1237         default:
1238                 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1239                         "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1240                 return -1;
1241         }
1242
1243         return 0;
1244 }
1245
1246 static int uvc_scan_chain_forward(struct uvc_video_device *video,
1247         struct uvc_entity *entity, struct uvc_entity *prev)
1248 {
1249         struct uvc_entity *forward;
1250         int found;
1251
1252         /* Forward scan */
1253         forward = NULL;
1254         found = 0;
1255
1256         while (1) {
1257                 forward = uvc_entity_by_reference(video->dev, entity->id,
1258                         forward);
1259                 if (forward == NULL)
1260                         break;
1261
1262                 if (UVC_ENTITY_TYPE(forward) != VC_EXTENSION_UNIT ||
1263                     forward == prev)
1264                         continue;
1265
1266                 if (forward->extension.bNrInPins != 1) {
1267                         uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has"
1268                                 "more than 1 input pin.\n", entity->id);
1269                         return -1;
1270                 }
1271
1272                 list_add_tail(&forward->chain, &video->extensions);
1273                 if (uvc_trace_param & UVC_TRACE_PROBE) {
1274                         if (!found)
1275                                 printk(" (-> XU");
1276
1277                         printk(" %d", forward->id);
1278                         found = 1;
1279                 }
1280         }
1281         if (found)
1282                 printk(")");
1283
1284         return 0;
1285 }
1286
1287 static int uvc_scan_chain_backward(struct uvc_video_device *video,
1288         struct uvc_entity *entity)
1289 {
1290         struct uvc_entity *term;
1291         int id = -1, i;
1292
1293         switch (UVC_ENTITY_TYPE(entity)) {
1294         case VC_EXTENSION_UNIT:
1295                 id = entity->extension.baSourceID[0];
1296                 break;
1297
1298         case VC_PROCESSING_UNIT:
1299                 id = entity->processing.bSourceID;
1300                 break;
1301
1302         case VC_SELECTOR_UNIT:
1303                 /* Single-input selector units are ignored. */
1304                 if (entity->selector.bNrInPins == 1) {
1305                         id = entity->selector.baSourceID[0];
1306                         break;
1307                 }
1308
1309                 if (uvc_trace_param & UVC_TRACE_PROBE)
1310                         printk(" <- IT");
1311
1312                 video->selector = entity;
1313                 for (i = 0; i < entity->selector.bNrInPins; ++i) {
1314                         id = entity->selector.baSourceID[i];
1315                         term = uvc_entity_by_id(video->dev, id);
1316                         if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1317                                 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1318                                         "input %d isn't connected to an "
1319                                         "input terminal\n", entity->id, i);
1320                                 return -1;
1321                         }
1322
1323                         if (uvc_trace_param & UVC_TRACE_PROBE)
1324                                 printk(" %d", term->id);
1325
1326                         list_add_tail(&term->chain, &video->iterms);
1327                         uvc_scan_chain_forward(video, term, entity);
1328                 }
1329
1330                 if (uvc_trace_param & UVC_TRACE_PROBE)
1331                         printk("\n");
1332
1333                 id = 0;
1334                 break;
1335         }
1336
1337         return id;
1338 }
1339
1340 static int uvc_scan_chain(struct uvc_video_device *video)
1341 {
1342         struct uvc_entity *entity, *prev;
1343         int id;
1344
1345         entity = video->oterm;
1346         uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
1347         id = entity->output.bSourceID;
1348         while (id != 0) {
1349                 prev = entity;
1350                 entity = uvc_entity_by_id(video->dev, id);
1351                 if (entity == NULL) {
1352                         uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1353                                 "unknown entity %d.\n", id);
1354                         return -1;
1355                 }
1356
1357                 /* Process entity */
1358                 if (uvc_scan_chain_entity(video, entity) < 0)
1359                         return -1;
1360
1361                 /* Forward scan */
1362                 if (uvc_scan_chain_forward(video, entity, prev) < 0)
1363                         return -1;
1364
1365                 /* Stop when a terminal is found. */
1366                 if (!UVC_ENTITY_IS_UNIT(entity))
1367                         break;
1368
1369                 /* Backward scan */
1370                 id = uvc_scan_chain_backward(video, entity);
1371                 if (id < 0)
1372                         return id;
1373         }
1374
1375         /* Initialize the video buffers queue. */
1376         uvc_queue_init(&video->queue);
1377
1378         return 0;
1379 }
1380
1381 /*
1382  * Register the video devices.
1383  *
1384  * The driver currently supports a single video device per control interface
1385  * only. The terminal and units must match the following structure:
1386  *
1387  * ITT_CAMERA -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING
1388  *
1389  * The Extension Units, if present, must have a single input pin. The
1390  * Processing Unit and Extension Units can be in any order. Additional
1391  * Extension Units connected to the main chain as single-unit branches are
1392  * also supported.
1393  */
1394 static int uvc_register_video(struct uvc_device *dev)
1395 {
1396         struct video_device *vdev;
1397         struct uvc_entity *term;
1398         int found = 0, ret;
1399
1400         /* Check if the control interface matches the structure we expect. */
1401         list_for_each_entry(term, &dev->entities, list) {
1402                 struct uvc_streaming *streaming;
1403
1404                 if (UVC_ENTITY_TYPE(term) != TT_STREAMING)
1405                         continue;
1406
1407                 memset(&dev->video, 0, sizeof dev->video);
1408                 mutex_init(&dev->video.ctrl_mutex);
1409                 INIT_LIST_HEAD(&dev->video.iterms);
1410                 INIT_LIST_HEAD(&dev->video.extensions);
1411                 dev->video.oterm = term;
1412                 dev->video.dev = dev;
1413                 if (uvc_scan_chain(&dev->video) < 0)
1414                         continue;
1415
1416                 list_for_each_entry(streaming, &dev->streaming, list) {
1417                         if (streaming->header.bTerminalLink == term->id) {
1418                                 dev->video.streaming = streaming;
1419                                 found = 1;
1420                                 break;
1421                         }
1422                 }
1423
1424                 if (found)
1425                         break;
1426         }
1427
1428         if (!found) {
1429                 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1430                 return -1;
1431         }
1432
1433         if (uvc_trace_param & UVC_TRACE_PROBE) {
1434                 uvc_printk(KERN_INFO, "Found a valid video chain (");
1435                 list_for_each_entry(term, &dev->video.iterms, chain) {
1436                         printk("%d", term->id);
1437                         if (term->chain.next != &dev->video.iterms)
1438                                 printk(",");
1439                 }
1440                 printk(" -> %d).\n", dev->video.oterm->id);
1441         }
1442
1443         /* Initialize the streaming interface with default streaming
1444          * parameters.
1445          */
1446         if ((ret = uvc_video_init(&dev->video)) < 0) {
1447                 uvc_printk(KERN_ERR, "Failed to initialize the device "
1448                         "(%d).\n", ret);
1449                 return ret;
1450         }
1451
1452         /* Register the device with V4L. */
1453         vdev = video_device_alloc();
1454         if (vdev == NULL)
1455                 return -1;
1456
1457         /* We already hold a reference to dev->udev. The video device will be
1458          * unregistered before the reference is released, so we don't need to
1459          * get another one.
1460          */
1461         vdev->parent = &dev->intf->dev;
1462         vdev->type = 0;
1463         vdev->type2 = 0;
1464         vdev->minor = -1;
1465         vdev->fops = &uvc_fops;
1466         vdev->release = video_device_release;
1467         strncpy(vdev->name, dev->name, sizeof vdev->name);
1468
1469         /* Set the driver data before calling video_register_device, otherwise
1470          * uvc_v4l2_open might race us.
1471          *
1472          * FIXME: usb_set_intfdata hasn't been called so far. Is that a
1473          *        problem ? Does any function which could be called here get
1474          *        a pointer to the usb_interface ?
1475          */
1476         dev->video.vdev = vdev;
1477         video_set_drvdata(vdev, &dev->video);
1478
1479         if (video_register_device(vdev, VFL_TYPE_GRABBER, -1) < 0) {
1480                 dev->video.vdev = NULL;
1481                 video_device_release(vdev);
1482                 return -1;
1483         }
1484
1485         return 0;
1486 }
1487
1488 /*
1489  * Delete the UVC device.
1490  *
1491  * Called by the kernel when the last reference to the uvc_device structure
1492  * is released.
1493  *
1494  * Unregistering the video devices is done here because every opened instance
1495  * must be closed before the device can be unregistered. An alternative would
1496  * have been to use another reference count for uvc_v4l2_open/uvc_release, and
1497  * unregister the video devices on disconnect when that reference count drops
1498  * to zero.
1499  *
1500  * As this function is called after or during disconnect(), all URBs have
1501  * already been canceled by the USB core. There is no need to kill the
1502  * interrupt URB manually.
1503  */
1504 void uvc_delete(struct kref *kref)
1505 {
1506         struct uvc_device *dev = container_of(kref, struct uvc_device, kref);
1507         struct list_head *p, *n;
1508
1509         /* Unregister the video device */
1510         uvc_unregister_video(dev);
1511         usb_put_intf(dev->intf);
1512         usb_put_dev(dev->udev);
1513
1514         uvc_status_cleanup(dev);
1515         uvc_ctrl_cleanup_device(dev);
1516
1517         list_for_each_safe(p, n, &dev->entities) {
1518                 struct uvc_entity *entity;
1519                 entity = list_entry(p, struct uvc_entity, list);
1520                 kfree(entity);
1521         }
1522
1523         list_for_each_safe(p, n, &dev->streaming) {
1524                 struct uvc_streaming *streaming;
1525                 streaming = list_entry(p, struct uvc_streaming, list);
1526                 usb_driver_release_interface(&uvc_driver.driver,
1527                         streaming->intf);
1528                 usb_put_intf(streaming->intf);
1529                 kfree(streaming->format);
1530                 kfree(streaming->header.bmaControls);
1531                 kfree(streaming);
1532         }
1533
1534         kfree(dev);
1535 }
1536
1537 static int uvc_probe(struct usb_interface *intf,
1538                      const struct usb_device_id *id)
1539 {
1540         struct usb_device *udev = interface_to_usbdev(intf);
1541         struct uvc_device *dev;
1542         int ret;
1543
1544         if (id->idVendor && id->idProduct)
1545                 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1546                                 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1547                                 id->idProduct);
1548         else
1549                 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1550                                 udev->devpath);
1551
1552         /* Allocate memory for the device and initialize it */
1553         if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1554                 return -ENOMEM;
1555
1556         INIT_LIST_HEAD(&dev->entities);
1557         INIT_LIST_HEAD(&dev->streaming);
1558         kref_init(&dev->kref);
1559
1560         dev->udev = usb_get_dev(udev);
1561         dev->intf = usb_get_intf(intf);
1562         dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1563         dev->quirks = id->driver_info | uvc_quirks_param;
1564
1565         if (udev->product != NULL)
1566                 strncpy(dev->name, udev->product, sizeof dev->name);
1567         else
1568                 snprintf(dev->name, sizeof dev->name,
1569                         "UVC Camera (%04x:%04x)",
1570                         le16_to_cpu(udev->descriptor.idVendor),
1571                         le16_to_cpu(udev->descriptor.idProduct));
1572
1573         /* Parse the Video Class control descriptor */
1574         if (uvc_parse_control(dev) < 0) {
1575                 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1576                         "descriptors.\n");
1577                 goto error;
1578         }
1579
1580         uvc_printk(KERN_INFO, "Found UVC %u.%02u device %s (%04x:%04x)\n",
1581                 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1582                 udev->product ? udev->product : "<unnamed>",
1583                 le16_to_cpu(udev->descriptor.idVendor),
1584                 le16_to_cpu(udev->descriptor.idProduct));
1585
1586         if (uvc_quirks_param != 0) {
1587                 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1588                         "parameter for testing purpose.\n", uvc_quirks_param);
1589                 uvc_printk(KERN_INFO, "Please report required quirks to the "
1590                         "linux-uvc-devel mailing list.\n");
1591         }
1592
1593         /* Initialize controls */
1594         if (uvc_ctrl_init_device(dev) < 0)
1595                 goto error;
1596
1597         /* Register the video devices */
1598         if (uvc_register_video(dev) < 0)
1599                 goto error;
1600
1601         /* Save our data pointer in the interface data */
1602         usb_set_intfdata(intf, dev);
1603
1604         /* Initialize the interrupt URB */
1605         if ((ret = uvc_status_init(dev)) < 0) {
1606                 uvc_printk(KERN_INFO, "Unable to initialize the status "
1607                         "endpoint (%d), status interrupt will not be "
1608                         "supported.\n", ret);
1609         }
1610
1611         uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1612         return 0;
1613
1614 error:
1615         kref_put(&dev->kref, uvc_delete);
1616         return -ENODEV;
1617 }
1618
1619 static void uvc_disconnect(struct usb_interface *intf)
1620 {
1621         struct uvc_device *dev = usb_get_intfdata(intf);
1622
1623         /* Set the USB interface data to NULL. This can be done outside the
1624          * lock, as there's no other reader.
1625          */
1626         usb_set_intfdata(intf, NULL);
1627
1628         if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOSTREAMING)
1629                 return;
1630
1631         /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1632          * lock is needed to prevent uvc_disconnect from releasing its
1633          * reference to the uvc_device instance after uvc_v4l2_open() received
1634          * the pointer to the device (video_devdata) but before it got the
1635          * chance to increase the reference count (kref_get).
1636          *
1637          * Note that the reference can't be released with the lock held,
1638          * otherwise a AB-BA deadlock can occur with videodev_lock that
1639          * videodev acquires in videodev_open() and video_unregister_device().
1640          */
1641         mutex_lock(&uvc_driver.open_mutex);
1642         dev->state |= UVC_DEV_DISCONNECTED;
1643         mutex_unlock(&uvc_driver.open_mutex);
1644
1645         kref_put(&dev->kref, uvc_delete);
1646 }
1647
1648 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1649 {
1650         struct uvc_device *dev = usb_get_intfdata(intf);
1651
1652         uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1653                 intf->cur_altsetting->desc.bInterfaceNumber);
1654
1655         /* Controls are cached on the fly so they don't need to be saved. */
1656         if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL)
1657                 return uvc_status_suspend(dev);
1658
1659         if (dev->video.streaming->intf != intf) {
1660                 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB "
1661                                 "interface mismatch.\n");
1662                 return -EINVAL;
1663         }
1664
1665         return uvc_video_suspend(&dev->video);
1666 }
1667
1668 static int uvc_resume(struct usb_interface *intf)
1669 {
1670         struct uvc_device *dev = usb_get_intfdata(intf);
1671         int ret;
1672
1673         uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1674                 intf->cur_altsetting->desc.bInterfaceNumber);
1675
1676         if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL) {
1677                 if ((ret = uvc_ctrl_resume_device(dev)) < 0)
1678                         return ret;
1679
1680                 return uvc_status_resume(dev);
1681         }
1682
1683         if (dev->video.streaming->intf != intf) {
1684                 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB "
1685                                 "interface mismatch.\n");
1686                 return -EINVAL;
1687         }
1688
1689         return uvc_video_resume(&dev->video);
1690 }
1691
1692 /* ------------------------------------------------------------------------
1693  * Driver initialization and cleanup
1694  */
1695
1696 /*
1697  * The Logitech cameras listed below have their interface class set to
1698  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1699  * though they are compliant.
1700  */
1701 static struct usb_device_id uvc_ids[] = {
1702         /* ALi M5606 (Clevo M540SR) */
1703         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1704                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1705           .idVendor             = 0x0402,
1706           .idProduct            = 0x5606,
1707           .bInterfaceClass      = USB_CLASS_VIDEO,
1708           .bInterfaceSubClass   = 1,
1709           .bInterfaceProtocol   = 0,
1710           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1711         /* Creative Live! Optia */
1712         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1713                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1714           .idVendor             = 0x041e,
1715           .idProduct            = 0x4057,
1716           .bInterfaceClass      = USB_CLASS_VIDEO,
1717           .bInterfaceSubClass   = 1,
1718           .bInterfaceProtocol   = 0,
1719           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1720         /* Microsoft Lifecam NX-6000 */
1721         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1722                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1723           .idVendor             = 0x045e,
1724           .idProduct            = 0x00f8,
1725           .bInterfaceClass      = USB_CLASS_VIDEO,
1726           .bInterfaceSubClass   = 1,
1727           .bInterfaceProtocol   = 0,
1728           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1729         /* Microsoft Lifecam VX-7000 */
1730         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1731                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1732           .idVendor             = 0x045e,
1733           .idProduct            = 0x0723,
1734           .bInterfaceClass      = USB_CLASS_VIDEO,
1735           .bInterfaceSubClass   = 1,
1736           .bInterfaceProtocol   = 0,
1737           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1738         /* Logitech Quickcam Fusion */
1739         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1740                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1741           .idVendor             = 0x046d,
1742           .idProduct            = 0x08c1,
1743           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1744           .bInterfaceSubClass   = 1,
1745           .bInterfaceProtocol   = 0 },
1746         /* Logitech Quickcam Orbit MP */
1747         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1748                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1749           .idVendor             = 0x046d,
1750           .idProduct            = 0x08c2,
1751           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1752           .bInterfaceSubClass   = 1,
1753           .bInterfaceProtocol   = 0 },
1754         /* Logitech Quickcam Pro for Notebook */
1755         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1756                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1757           .idVendor             = 0x046d,
1758           .idProduct            = 0x08c3,
1759           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1760           .bInterfaceSubClass   = 1,
1761           .bInterfaceProtocol   = 0 },
1762         /* Logitech Quickcam Pro 5000 */
1763         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1764                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1765           .idVendor             = 0x046d,
1766           .idProduct            = 0x08c5,
1767           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1768           .bInterfaceSubClass   = 1,
1769           .bInterfaceProtocol   = 0 },
1770         /* Logitech Quickcam OEM Dell Notebook */
1771         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1772                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1773           .idVendor             = 0x046d,
1774           .idProduct            = 0x08c6,
1775           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1776           .bInterfaceSubClass   = 1,
1777           .bInterfaceProtocol   = 0 },
1778         /* Logitech Quickcam OEM Cisco VT Camera II */
1779         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1780                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1781           .idVendor             = 0x046d,
1782           .idProduct            = 0x08c7,
1783           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1784           .bInterfaceSubClass   = 1,
1785           .bInterfaceProtocol   = 0 },
1786         /* Apple Built-In iSight */
1787         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1788                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1789           .idVendor             = 0x05ac,
1790           .idProduct            = 0x8501,
1791           .bInterfaceClass      = USB_CLASS_VIDEO,
1792           .bInterfaceSubClass   = 1,
1793           .bInterfaceProtocol   = 0,
1794           .driver_info          = UVC_QUIRK_PROBE_MINMAX
1795                                 | UVC_QUIRK_BUILTIN_ISIGHT },
1796         /* Genesys Logic USB 2.0 PC Camera */
1797         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1798                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1799           .idVendor             = 0x05e3,
1800           .idProduct            = 0x0505,
1801           .bInterfaceClass      = USB_CLASS_VIDEO,
1802           .bInterfaceSubClass   = 1,
1803           .bInterfaceProtocol   = 0,
1804           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1805         /* Silicon Motion SM371 */
1806         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1807                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1808           .idVendor             = 0x090c,
1809           .idProduct            = 0xb371,
1810           .bInterfaceClass      = USB_CLASS_VIDEO,
1811           .bInterfaceSubClass   = 1,
1812           .bInterfaceProtocol   = 0,
1813           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1814         /* MT6227 */
1815         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1816                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1817           .idVendor             = 0x0e8d,
1818           .idProduct            = 0x0004,
1819           .bInterfaceClass      = USB_CLASS_VIDEO,
1820           .bInterfaceSubClass   = 1,
1821           .bInterfaceProtocol   = 0,
1822           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1823         /* Syntek (HP Spartan) */
1824         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1825                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1826           .idVendor             = 0x174f,
1827           .idProduct            = 0x5212,
1828           .bInterfaceClass      = USB_CLASS_VIDEO,
1829           .bInterfaceSubClass   = 1,
1830           .bInterfaceProtocol   = 0,
1831           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1832         /* Asus F9SG */
1833         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1834                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1835           .idVendor             = 0x174f,
1836           .idProduct            = 0x8a31,
1837           .bInterfaceClass      = USB_CLASS_VIDEO,
1838           .bInterfaceSubClass   = 1,
1839           .bInterfaceProtocol   = 0,
1840           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1841         /* Syntek (Asus U3S) */
1842         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1843                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1844           .idVendor             = 0x174f,
1845           .idProduct            = 0x8a33,
1846           .bInterfaceClass      = USB_CLASS_VIDEO,
1847           .bInterfaceSubClass   = 1,
1848           .bInterfaceProtocol   = 0,
1849           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1850         /* Ecamm Pico iMage */
1851         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1852                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1853           .idVendor             = 0x18cd,
1854           .idProduct            = 0xcafe,
1855           .bInterfaceClass      = USB_CLASS_VIDEO,
1856           .bInterfaceSubClass   = 1,
1857           .bInterfaceProtocol   = 0,
1858           .driver_info          = UVC_QUIRK_PROBE_EXTRAFIELDS },
1859         /* Bodelin ProScopeHR */
1860         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1861                                 | USB_DEVICE_ID_MATCH_DEV_HI
1862                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1863           .idVendor             = 0x19ab,
1864           .idProduct            = 0x1000,
1865           .bcdDevice_hi         = 0x0126,
1866           .bInterfaceClass      = USB_CLASS_VIDEO,
1867           .bInterfaceSubClass   = 1,
1868           .bInterfaceProtocol   = 0,
1869           .driver_info          = UVC_QUIRK_STATUS_INTERVAL },
1870         /* SiGma Micro USB Web Camera */
1871         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1872                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1873           .idVendor             = 0x1c4f,
1874           .idProduct            = 0x3000,
1875           .bInterfaceClass      = USB_CLASS_VIDEO,
1876           .bInterfaceSubClass   = 1,
1877           .bInterfaceProtocol   = 0,
1878           .driver_info          = UVC_QUIRK_PROBE_MINMAX
1879                                 | UVC_QUIRK_IGNORE_SELECTOR_UNIT},
1880         /* Acer OEM Webcam - Unknown vendor */
1881         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1882                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1883           .idVendor             = 0x5986,
1884           .idProduct            = 0x0100,
1885           .bInterfaceClass      = USB_CLASS_VIDEO,
1886           .bInterfaceSubClass   = 1,
1887           .bInterfaceProtocol   = 0,
1888           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1889         /* Packard Bell OEM Webcam */
1890         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1891                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1892           .idVendor             = 0x5986,
1893           .idProduct            = 0x0101,
1894           .bInterfaceClass      = USB_CLASS_VIDEO,
1895           .bInterfaceSubClass   = 1,
1896           .bInterfaceProtocol   = 0,
1897           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1898         /* Acer Crystal Eye webcam */
1899         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1900                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1901           .idVendor             = 0x5986,
1902           .idProduct            = 0x0102,
1903           .bInterfaceClass      = USB_CLASS_VIDEO,
1904           .bInterfaceSubClass   = 1,
1905           .bInterfaceProtocol   = 0,
1906           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1907         /* Medion Akoya Mini E1210 */
1908         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1909                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1910           .idVendor             = 0x5986,
1911           .idProduct            = 0x0141,
1912           .bInterfaceClass      = USB_CLASS_VIDEO,
1913           .bInterfaceSubClass   = 1,
1914           .bInterfaceProtocol   = 0,
1915           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1916         /* Acer OrbiCam - Unknown vendor */
1917         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1918                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1919           .idVendor             = 0x5986,
1920           .idProduct            = 0x0200,
1921           .bInterfaceClass      = USB_CLASS_VIDEO,
1922           .bInterfaceSubClass   = 1,
1923           .bInterfaceProtocol   = 0,
1924           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1925         /* Generic USB Video Class */
1926         { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
1927         {}
1928 };
1929
1930 MODULE_DEVICE_TABLE(usb, uvc_ids);
1931
1932 struct uvc_driver uvc_driver = {
1933         .driver = {
1934                 .name           = "uvcvideo",
1935                 .probe          = uvc_probe,
1936                 .disconnect     = uvc_disconnect,
1937                 .suspend        = uvc_suspend,
1938                 .resume         = uvc_resume,
1939                 .id_table       = uvc_ids,
1940                 .supports_autosuspend = 1,
1941         },
1942 };
1943
1944 static int __init uvc_init(void)
1945 {
1946         int result;
1947
1948         INIT_LIST_HEAD(&uvc_driver.devices);
1949         INIT_LIST_HEAD(&uvc_driver.controls);
1950         mutex_init(&uvc_driver.open_mutex);
1951         mutex_init(&uvc_driver.ctrl_mutex);
1952
1953         uvc_ctrl_init();
1954
1955         result = usb_register(&uvc_driver.driver);
1956         if (result == 0)
1957                 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
1958         return result;
1959 }
1960
1961 static void __exit uvc_cleanup(void)
1962 {
1963         usb_deregister(&uvc_driver.driver);
1964 }
1965
1966 module_init(uvc_init);
1967 module_exit(uvc_cleanup);
1968
1969 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1970 MODULE_PARM_DESC(quirks, "Forced device quirks");
1971 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1972 MODULE_PARM_DESC(trace, "Trace level bitmask");
1973
1974 MODULE_AUTHOR(DRIVER_AUTHOR);
1975 MODULE_DESCRIPTION(DRIVER_DESC);
1976 MODULE_LICENSE("GPL");
1977 MODULE_VERSION(DRIVER_VERSION);
1978