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