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