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