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