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