]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/uvc/uvc_driver.c
18a61928f4f6c804476c5071fdb651d6c244b14f
[net-next-2.6.git] / drivers / media / video / uvc / uvc_driver.c
1 /*
2  *      uvc_driver.c  --  USB Video Class driver
3  *
4  *      Copyright (C) 2005-2008
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  */
13
14 /*
15  * This driver aims to support video input devices compliant with the 'USB
16  * Video Class' specification.
17  *
18  * The driver doesn't support the deprecated v4l1 interface. It implements the
19  * mmap capture method only, and doesn't do any image format conversion in
20  * software. If your user-space application doesn't support YUYV or MJPEG, fix
21  * it :-). Please note that the MJPEG data have been stripped from their
22  * Huffman tables (DHT marker), you will need to add it back if your JPEG
23  * codec can't handle MJPEG data.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/version.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/usb.h>
31 #include <linux/videodev2.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <asm/atomic.h>
35 #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         if (buffer[2] == VS_OUTPUT_HEADER) {
613                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
614                         "%d OUTPUT HEADER descriptor is not supported.\n",
615                         dev->udev->devnum, alts->desc.bInterfaceNumber);
616                 goto error;
617         } else if (buffer[2] == VS_INPUT_HEADER) {
618                 p = buflen >= 5 ? buffer[3] : 0;
619                 n = buflen >= 12 ? buffer[12] : 0;
620
621                 if (buflen < 13 + p*n || buffer[2] != VS_INPUT_HEADER) {
622                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
623                                 "interface %d INPUT HEADER descriptor is "
624                                 "invalid.\n", dev->udev->devnum,
625                                 alts->desc.bInterfaceNumber);
626                         goto error;
627                 }
628
629                 streaming->header.bNumFormats = p;
630                 streaming->header.bEndpointAddress = buffer[6];
631                 streaming->header.bmInfo = buffer[7];
632                 streaming->header.bTerminalLink = buffer[8];
633                 streaming->header.bStillCaptureMethod = buffer[9];
634                 streaming->header.bTriggerSupport = buffer[10];
635                 streaming->header.bTriggerUsage = buffer[11];
636                 streaming->header.bControlSize = n;
637
638                 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
639                 if (streaming->header.bmaControls == NULL) {
640                         ret = -ENOMEM;
641                         goto error;
642                 }
643
644                 memcpy(streaming->header.bmaControls, &buffer[13], p*n);
645         } else {
646                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
647                         "%d HEADER descriptor not found.\n", dev->udev->devnum,
648                         alts->desc.bInterfaceNumber);
649                 goto error;
650         }
651
652         buflen -= buffer[0];
653         buffer += buffer[0];
654
655         _buffer = buffer;
656         _buflen = buflen;
657
658         /* Count the format and frame descriptors. */
659         while (_buflen > 2) {
660                 switch (_buffer[2]) {
661                 case VS_FORMAT_UNCOMPRESSED:
662                 case VS_FORMAT_MJPEG:
663                 case VS_FORMAT_FRAME_BASED:
664                         nformats++;
665                         break;
666
667                 case VS_FORMAT_DV:
668                         /* DV format has no frame descriptor. We will create a
669                          * dummy frame descriptor with a dummy frame interval.
670                          */
671                         nformats++;
672                         nframes++;
673                         nintervals++;
674                         break;
675
676                 case VS_FORMAT_MPEG2TS:
677                 case VS_FORMAT_STREAM_BASED:
678                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
679                                 "interface %d FORMAT %u is not supported.\n",
680                                 dev->udev->devnum,
681                                 alts->desc.bInterfaceNumber, _buffer[2]);
682                         break;
683
684                 case VS_FRAME_UNCOMPRESSED:
685                 case VS_FRAME_MJPEG:
686                         nframes++;
687                         if (_buflen > 25)
688                                 nintervals += _buffer[25] ? _buffer[25] : 3;
689                         break;
690
691                 case VS_FRAME_FRAME_BASED:
692                         nframes++;
693                         if (_buflen > 21)
694                                 nintervals += _buffer[21] ? _buffer[21] : 3;
695                         break;
696                 }
697
698                 _buflen -= _buffer[0];
699                 _buffer += _buffer[0];
700         }
701
702         if (nformats == 0) {
703                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
704                         "%d has no supported formats defined.\n",
705                         dev->udev->devnum, alts->desc.bInterfaceNumber);
706                 goto error;
707         }
708
709         size = nformats * sizeof *format + nframes * sizeof *frame
710              + nintervals * sizeof *interval;
711         format = kzalloc(size, GFP_KERNEL);
712         if (format == NULL) {
713                 ret = -ENOMEM;
714                 goto error;
715         }
716
717         frame = (struct uvc_frame *)&format[nformats];
718         interval = (__u32 *)&frame[nframes];
719
720         streaming->format = format;
721         streaming->nformats = nformats;
722
723         /* Parse the format descriptors. */
724         while (buflen > 2) {
725                 switch (buffer[2]) {
726                 case VS_FORMAT_UNCOMPRESSED:
727                 case VS_FORMAT_MJPEG:
728                 case VS_FORMAT_DV:
729                 case VS_FORMAT_FRAME_BASED:
730                         format->frame = frame;
731                         ret = uvc_parse_format(dev, streaming, format,
732                                 &interval, buffer, buflen);
733                         if (ret < 0)
734                                 goto error;
735
736                         frame += format->nframes;
737                         format++;
738
739                         buflen -= ret;
740                         buffer += ret;
741                         continue;
742
743                 default:
744                         break;
745                 }
746
747                 buflen -= buffer[0];
748                 buffer += buffer[0];
749         }
750
751         /* Parse the alternate settings to find the maximum bandwidth. */
752         for (i = 0; i < intf->num_altsetting; ++i) {
753                 struct usb_host_endpoint *ep;
754                 alts = &intf->altsetting[i];
755                 ep = uvc_find_endpoint(alts,
756                                 streaming->header.bEndpointAddress);
757                 if (ep == NULL)
758                         continue;
759
760                 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
761                 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
762                 if (psize > streaming->maxpsize)
763                         streaming->maxpsize = psize;
764         }
765
766         list_add_tail(&streaming->list, &dev->streaming);
767         return 0;
768
769 error:
770         usb_driver_release_interface(&uvc_driver.driver, intf);
771         usb_put_intf(intf);
772         kfree(streaming->format);
773         kfree(streaming->header.bmaControls);
774         kfree(streaming);
775         return ret;
776 }
777
778 /* Parse vendor-specific extensions. */
779 static int uvc_parse_vendor_control(struct uvc_device *dev,
780         const unsigned char *buffer, int buflen)
781 {
782         struct usb_device *udev = dev->udev;
783         struct usb_host_interface *alts = dev->intf->cur_altsetting;
784         struct uvc_entity *unit;
785         unsigned int n, p;
786         int handled = 0;
787
788         switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
789         case 0x046d:            /* Logitech */
790                 if (buffer[1] != 0x41 || buffer[2] != 0x01)
791                         break;
792
793                 /* Logitech implements several vendor specific functions
794                  * through vendor specific extension units (LXU).
795                  *
796                  * The LXU descriptors are similar to XU descriptors
797                  * (see "USB Device Video Class for Video Devices", section
798                  * 3.7.2.6 "Extension Unit Descriptor") with the following
799                  * differences:
800                  *
801                  * ----------------------------------------------------------
802                  * 0            bLength         1        Number
803                  *      Size of this descriptor, in bytes: 24+p+n*2
804                  * ----------------------------------------------------------
805                  * 23+p+n       bmControlsType  N       Bitmap
806                  *      Individual bits in the set are defined:
807                  *      0: Absolute
808                  *      1: Relative
809                  *
810                  *      This bitset is mapped exactly the same as bmControls.
811                  * ----------------------------------------------------------
812                  * 23+p+n*2     bReserved       1       Boolean
813                  * ----------------------------------------------------------
814                  * 24+p+n*2     iExtension      1       Index
815                  *      Index of a string descriptor that describes this
816                  *      extension unit.
817                  * ----------------------------------------------------------
818                  */
819                 p = buflen >= 22 ? buffer[21] : 0;
820                 n = buflen >= 25 + p ? buffer[22+p] : 0;
821
822                 if (buflen < 25 + p + 2*n) {
823                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
824                                 "interface %d EXTENSION_UNIT error\n",
825                                 udev->devnum, alts->desc.bInterfaceNumber);
826                         break;
827                 }
828
829                 unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
830                 if (unit == NULL)
831                         return -ENOMEM;
832
833                 unit->id = buffer[3];
834                 unit->type = VC_EXTENSION_UNIT;
835                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
836                 unit->extension.bNumControls = buffer[20];
837                 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
838                 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
839                 memcpy(unit->extension.baSourceID, &buffer[22], p);
840                 unit->extension.bControlSize = buffer[22+p];
841                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
842                 unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
843                                                + p + n;
844                 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
845
846                 if (buffer[24+p+2*n] != 0)
847                         usb_string(udev, buffer[24+p+2*n], unit->name,
848                                    sizeof unit->name);
849                 else
850                         sprintf(unit->name, "Extension %u", buffer[3]);
851
852                 list_add_tail(&unit->list, &dev->entities);
853                 handled = 1;
854                 break;
855         }
856
857         return handled;
858 }
859
860 static int uvc_parse_standard_control(struct uvc_device *dev,
861         const unsigned char *buffer, int buflen)
862 {
863         struct usb_device *udev = dev->udev;
864         struct uvc_entity *unit, *term;
865         struct usb_interface *intf;
866         struct usb_host_interface *alts = dev->intf->cur_altsetting;
867         unsigned int i, n, p, len;
868         __u16 type;
869
870         switch (buffer[2]) {
871         case VC_HEADER:
872                 n = buflen >= 12 ? buffer[11] : 0;
873
874                 if (buflen < 12 || buflen < 12 + n) {
875                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
876                                 "interface %d HEADER error\n", udev->devnum,
877                                 alts->desc.bInterfaceNumber);
878                         return -EINVAL;
879                 }
880
881                 dev->uvc_version = get_unaligned_le16(&buffer[3]);
882                 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
883
884                 /* Parse all USB Video Streaming interfaces. */
885                 for (i = 0; i < n; ++i) {
886                         intf = usb_ifnum_to_if(udev, buffer[12+i]);
887                         if (intf == NULL) {
888                                 uvc_trace(UVC_TRACE_DESCR, "device %d "
889                                         "interface %d doesn't exists\n",
890                                         udev->devnum, i);
891                                 continue;
892                         }
893
894                         uvc_parse_streaming(dev, intf);
895                 }
896                 break;
897
898         case VC_INPUT_TERMINAL:
899                 if (buflen < 8) {
900                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
901                                 "interface %d INPUT_TERMINAL error\n",
902                                 udev->devnum, alts->desc.bInterfaceNumber);
903                         return -EINVAL;
904                 }
905
906                 /* Make sure the terminal type MSB is not null, otherwise it
907                  * could be confused with a unit.
908                  */
909                 type = get_unaligned_le16(&buffer[4]);
910                 if ((type & 0xff00) == 0) {
911                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
912                                 "interface %d INPUT_TERMINAL %d has invalid "
913                                 "type 0x%04x, skipping\n", udev->devnum,
914                                 alts->desc.bInterfaceNumber,
915                                 buffer[3], type);
916                         return 0;
917                 }
918
919                 n = 0;
920                 p = 0;
921                 len = 8;
922
923                 if (type == ITT_CAMERA) {
924                         n = buflen >= 15 ? buffer[14] : 0;
925                         len = 15;
926
927                 } else if (type == ITT_MEDIA_TRANSPORT_INPUT) {
928                         n = buflen >= 9 ? buffer[8] : 0;
929                         p = buflen >= 10 + n ? buffer[9+n] : 0;
930                         len = 10;
931                 }
932
933                 if (buflen < len + n + p) {
934                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
935                                 "interface %d INPUT_TERMINAL error\n",
936                                 udev->devnum, alts->desc.bInterfaceNumber);
937                         return -EINVAL;
938                 }
939
940                 term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
941                 if (term == NULL)
942                         return -ENOMEM;
943
944                 term->id = buffer[3];
945                 term->type = type | UVC_TERM_INPUT;
946
947                 if (UVC_ENTITY_TYPE(term) == ITT_CAMERA) {
948                         term->camera.bControlSize = n;
949                         term->camera.bmControls = (__u8 *)term + sizeof *term;
950                         term->camera.wObjectiveFocalLengthMin =
951                                 get_unaligned_le16(&buffer[8]);
952                         term->camera.wObjectiveFocalLengthMax =
953                                 get_unaligned_le16(&buffer[10]);
954                         term->camera.wOcularFocalLength =
955                                 get_unaligned_le16(&buffer[12]);
956                         memcpy(term->camera.bmControls, &buffer[15], n);
957                 } else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT) {
958                         term->media.bControlSize = n;
959                         term->media.bmControls = (__u8 *)term + sizeof *term;
960                         term->media.bTransportModeSize = p;
961                         term->media.bmTransportModes = (__u8 *)term
962                                                      + sizeof *term + n;
963                         memcpy(term->media.bmControls, &buffer[9], n);
964                         memcpy(term->media.bmTransportModes, &buffer[10+n], p);
965                 }
966
967                 if (buffer[7] != 0)
968                         usb_string(udev, buffer[7], term->name,
969                                    sizeof term->name);
970                 else if (UVC_ENTITY_TYPE(term) == ITT_CAMERA)
971                         sprintf(term->name, "Camera %u", buffer[3]);
972                 else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT)
973                         sprintf(term->name, "Media %u", buffer[3]);
974                 else
975                         sprintf(term->name, "Input %u", buffer[3]);
976
977                 list_add_tail(&term->list, &dev->entities);
978                 break;
979
980         case VC_OUTPUT_TERMINAL:
981                 if (buflen < 9) {
982                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
983                                 "interface %d OUTPUT_TERMINAL error\n",
984                                 udev->devnum, alts->desc.bInterfaceNumber);
985                         return -EINVAL;
986                 }
987
988                 /* Make sure the terminal type MSB is not null, otherwise it
989                  * could be confused with a unit.
990                  */
991                 type = get_unaligned_le16(&buffer[4]);
992                 if ((type & 0xff00) == 0) {
993                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
994                                 "interface %d OUTPUT_TERMINAL %d has invalid "
995                                 "type 0x%04x, skipping\n", udev->devnum,
996                                 alts->desc.bInterfaceNumber, buffer[3], type);
997                         return 0;
998                 }
999
1000                 term = kzalloc(sizeof *term, GFP_KERNEL);
1001                 if (term == NULL)
1002                         return -ENOMEM;
1003
1004                 term->id = buffer[3];
1005                 term->type = type | UVC_TERM_OUTPUT;
1006                 term->output.bSourceID = buffer[7];
1007
1008                 if (buffer[8] != 0)
1009                         usb_string(udev, buffer[8], term->name,
1010                                    sizeof term->name);
1011                 else
1012                         sprintf(term->name, "Output %u", buffer[3]);
1013
1014                 list_add_tail(&term->list, &dev->entities);
1015                 break;
1016
1017         case VC_SELECTOR_UNIT:
1018                 p = buflen >= 5 ? buffer[4] : 0;
1019
1020                 if (buflen < 5 || buflen < 6 + p) {
1021                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1022                                 "interface %d SELECTOR_UNIT error\n",
1023                                 udev->devnum, alts->desc.bInterfaceNumber);
1024                         return -EINVAL;
1025                 }
1026
1027                 unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1028                 if (unit == NULL)
1029                         return -ENOMEM;
1030
1031                 unit->id = buffer[3];
1032                 unit->type = buffer[2];
1033                 unit->selector.bNrInPins = buffer[4];
1034                 unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1035                 memcpy(unit->selector.baSourceID, &buffer[5], p);
1036
1037                 if (buffer[5+p] != 0)
1038                         usb_string(udev, buffer[5+p], unit->name,
1039                                    sizeof unit->name);
1040                 else
1041                         sprintf(unit->name, "Selector %u", buffer[3]);
1042
1043                 list_add_tail(&unit->list, &dev->entities);
1044                 break;
1045
1046         case VC_PROCESSING_UNIT:
1047                 n = buflen >= 8 ? buffer[7] : 0;
1048                 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1049
1050                 if (buflen < p + n) {
1051                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1052                                 "interface %d PROCESSING_UNIT error\n",
1053                                 udev->devnum, alts->desc.bInterfaceNumber);
1054                         return -EINVAL;
1055                 }
1056
1057                 unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1058                 if (unit == NULL)
1059                         return -ENOMEM;
1060
1061                 unit->id = buffer[3];
1062                 unit->type = buffer[2];
1063                 unit->processing.bSourceID = buffer[4];
1064                 unit->processing.wMaxMultiplier =
1065                         get_unaligned_le16(&buffer[5]);
1066                 unit->processing.bControlSize = buffer[7];
1067                 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1068                 memcpy(unit->processing.bmControls, &buffer[8], n);
1069                 if (dev->uvc_version >= 0x0110)
1070                         unit->processing.bmVideoStandards = buffer[9+n];
1071
1072                 if (buffer[8+n] != 0)
1073                         usb_string(udev, buffer[8+n], unit->name,
1074                                    sizeof unit->name);
1075                 else
1076                         sprintf(unit->name, "Processing %u", buffer[3]);
1077
1078                 list_add_tail(&unit->list, &dev->entities);
1079                 break;
1080
1081         case VC_EXTENSION_UNIT:
1082                 p = buflen >= 22 ? buffer[21] : 0;
1083                 n = buflen >= 24 + p ? buffer[22+p] : 0;
1084
1085                 if (buflen < 24 + p + n) {
1086                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1087                                 "interface %d EXTENSION_UNIT error\n",
1088                                 udev->devnum, alts->desc.bInterfaceNumber);
1089                         return -EINVAL;
1090                 }
1091
1092                 unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1093                 if (unit == NULL)
1094                         return -ENOMEM;
1095
1096                 unit->id = buffer[3];
1097                 unit->type = buffer[2];
1098                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1099                 unit->extension.bNumControls = buffer[20];
1100                 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
1101                 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1102                 memcpy(unit->extension.baSourceID, &buffer[22], p);
1103                 unit->extension.bControlSize = buffer[22+p];
1104                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1105                 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1106
1107                 if (buffer[23+p+n] != 0)
1108                         usb_string(udev, buffer[23+p+n], unit->name,
1109                                    sizeof unit->name);
1110                 else
1111                         sprintf(unit->name, "Extension %u", buffer[3]);
1112
1113                 list_add_tail(&unit->list, &dev->entities);
1114                 break;
1115
1116         default:
1117                 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1118                         "descriptor (%u)\n", buffer[2]);
1119                 break;
1120         }
1121
1122         return 0;
1123 }
1124
1125 static int uvc_parse_control(struct uvc_device *dev)
1126 {
1127         struct usb_host_interface *alts = dev->intf->cur_altsetting;
1128         unsigned char *buffer = alts->extra;
1129         int buflen = alts->extralen;
1130         int ret;
1131
1132         /* Parse the default alternate setting only, as the UVC specification
1133          * defines a single alternate setting, the default alternate setting
1134          * zero.
1135          */
1136
1137         while (buflen > 2) {
1138                 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1139                     buffer[1] != USB_DT_CS_INTERFACE)
1140                         goto next_descriptor;
1141
1142                 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1143                         return ret;
1144
1145 next_descriptor:
1146                 buflen -= buffer[0];
1147                 buffer += buffer[0];
1148         }
1149
1150         /* Check if the optional status endpoint is present. Built-in iSight
1151          * webcams have an interrupt endpoint but spit proprietary data that
1152          * don't conform to the UVC status endpoint messages. Don't try to
1153          * handle the interrupt endpoint for those cameras.
1154          */
1155         if (alts->desc.bNumEndpoints == 1 &&
1156             !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1157                 struct usb_host_endpoint *ep = &alts->endpoint[0];
1158                 struct usb_endpoint_descriptor *desc = &ep->desc;
1159
1160                 if (usb_endpoint_is_int_in(desc) &&
1161                     le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1162                     desc->bInterval != 0) {
1163                         uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1164                                 "(addr %02x).\n", desc->bEndpointAddress);
1165                         dev->int_ep = ep;
1166                 }
1167         }
1168
1169         return 0;
1170 }
1171
1172 /* ------------------------------------------------------------------------
1173  * USB probe and disconnect
1174  */
1175
1176 /*
1177  * Unregister the video devices.
1178  */
1179 static void uvc_unregister_video(struct uvc_device *dev)
1180 {
1181         if (dev->video.vdev) {
1182                 if (dev->video.vdev->minor == -1)
1183                         video_device_release(dev->video.vdev);
1184                 else
1185                         video_unregister_device(dev->video.vdev);
1186                 dev->video.vdev = NULL;
1187         }
1188 }
1189
1190 /*
1191  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1192  * and containing the following units:
1193  *
1194  * - a USB Streaming Output Terminal
1195  * - zero or one Processing Unit
1196  * - zero, one or mode single-input Selector Units
1197  * - zero or one multiple-input Selector Units, provided all inputs are
1198  *   connected to input terminals
1199  * - zero, one or mode single-input Extension Units
1200  * - one Camera Input Terminal, or one or more External terminals.
1201  *
1202  * A side forward scan is made on each detected entity to check for additional
1203  * extension units.
1204  */
1205 static int uvc_scan_chain_entity(struct uvc_video_device *video,
1206         struct uvc_entity *entity)
1207 {
1208         switch (UVC_ENTITY_TYPE(entity)) {
1209         case VC_EXTENSION_UNIT:
1210                 if (uvc_trace_param & UVC_TRACE_PROBE)
1211                         printk(" <- XU %d", entity->id);
1212
1213                 if (entity->extension.bNrInPins != 1) {
1214                         uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1215                                 "than 1 input pin.\n", entity->id);
1216                         return -1;
1217                 }
1218
1219                 list_add_tail(&entity->chain, &video->extensions);
1220                 break;
1221
1222         case VC_PROCESSING_UNIT:
1223                 if (uvc_trace_param & UVC_TRACE_PROBE)
1224                         printk(" <- PU %d", entity->id);
1225
1226                 if (video->processing != NULL) {
1227                         uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1228                                 "Processing Units in chain.\n");
1229                         return -1;
1230                 }
1231
1232                 video->processing = entity;
1233                 break;
1234
1235         case VC_SELECTOR_UNIT:
1236                 if (uvc_trace_param & UVC_TRACE_PROBE)
1237                         printk(" <- SU %d", entity->id);
1238
1239                 /* Single-input selector units are ignored. */
1240                 if (entity->selector.bNrInPins == 1)
1241                         break;
1242
1243                 if (video->selector != NULL) {
1244                         uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1245                                 "Units in chain.\n");
1246                         return -1;
1247                 }
1248
1249                 video->selector = entity;
1250                 break;
1251
1252         case ITT_VENDOR_SPECIFIC:
1253         case ITT_CAMERA:
1254         case ITT_MEDIA_TRANSPORT_INPUT:
1255                 if (uvc_trace_param & UVC_TRACE_PROBE)
1256                         printk(" <- IT %d\n", entity->id);
1257
1258                 list_add_tail(&entity->chain, &video->iterms);
1259                 break;
1260
1261         default:
1262                 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1263                         "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1264                 return -1;
1265         }
1266
1267         return 0;
1268 }
1269
1270 static int uvc_scan_chain_forward(struct uvc_video_device *video,
1271         struct uvc_entity *entity, struct uvc_entity *prev)
1272 {
1273         struct uvc_entity *forward;
1274         int found;
1275
1276         /* Forward scan */
1277         forward = NULL;
1278         found = 0;
1279
1280         while (1) {
1281                 forward = uvc_entity_by_reference(video->dev, entity->id,
1282                         forward);
1283                 if (forward == NULL)
1284                         break;
1285
1286                 if (UVC_ENTITY_TYPE(forward) != VC_EXTENSION_UNIT ||
1287                     forward == prev)
1288                         continue;
1289
1290                 if (forward->extension.bNrInPins != 1) {
1291                         uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has"
1292                                 "more than 1 input pin.\n", entity->id);
1293                         return -1;
1294                 }
1295
1296                 list_add_tail(&forward->chain, &video->extensions);
1297                 if (uvc_trace_param & UVC_TRACE_PROBE) {
1298                         if (!found)
1299                                 printk(" (-> XU");
1300
1301                         printk(" %d", forward->id);
1302                         found = 1;
1303                 }
1304         }
1305         if (found)
1306                 printk(")");
1307
1308         return 0;
1309 }
1310
1311 static int uvc_scan_chain_backward(struct uvc_video_device *video,
1312         struct uvc_entity *entity)
1313 {
1314         struct uvc_entity *term;
1315         int id = -1, i;
1316
1317         switch (UVC_ENTITY_TYPE(entity)) {
1318         case VC_EXTENSION_UNIT:
1319                 id = entity->extension.baSourceID[0];
1320                 break;
1321
1322         case VC_PROCESSING_UNIT:
1323                 id = entity->processing.bSourceID;
1324                 break;
1325
1326         case VC_SELECTOR_UNIT:
1327                 /* Single-input selector units are ignored. */
1328                 if (entity->selector.bNrInPins == 1) {
1329                         id = entity->selector.baSourceID[0];
1330                         break;
1331                 }
1332
1333                 if (uvc_trace_param & UVC_TRACE_PROBE)
1334                         printk(" <- IT");
1335
1336                 video->selector = entity;
1337                 for (i = 0; i < entity->selector.bNrInPins; ++i) {
1338                         id = entity->selector.baSourceID[i];
1339                         term = uvc_entity_by_id(video->dev, id);
1340                         if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1341                                 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1342                                         "input %d isn't connected to an "
1343                                         "input terminal\n", entity->id, i);
1344                                 return -1;
1345                         }
1346
1347                         if (uvc_trace_param & UVC_TRACE_PROBE)
1348                                 printk(" %d", term->id);
1349
1350                         list_add_tail(&term->chain, &video->iterms);
1351                         uvc_scan_chain_forward(video, term, entity);
1352                 }
1353
1354                 if (uvc_trace_param & UVC_TRACE_PROBE)
1355                         printk("\n");
1356
1357                 id = 0;
1358                 break;
1359         }
1360
1361         return id;
1362 }
1363
1364 static int uvc_scan_chain(struct uvc_video_device *video)
1365 {
1366         struct uvc_entity *entity, *prev;
1367         int id;
1368
1369         entity = video->oterm;
1370         uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
1371         id = entity->output.bSourceID;
1372         while (id != 0) {
1373                 prev = entity;
1374                 entity = uvc_entity_by_id(video->dev, id);
1375                 if (entity == NULL) {
1376                         uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1377                                 "unknown entity %d.\n", id);
1378                         return -1;
1379                 }
1380
1381                 /* Process entity */
1382                 if (uvc_scan_chain_entity(video, entity) < 0)
1383                         return -1;
1384
1385                 /* Forward scan */
1386                 if (uvc_scan_chain_forward(video, entity, prev) < 0)
1387                         return -1;
1388
1389                 /* Stop when a terminal is found. */
1390                 if (!UVC_ENTITY_IS_UNIT(entity))
1391                         break;
1392
1393                 /* Backward scan */
1394                 id = uvc_scan_chain_backward(video, entity);
1395                 if (id < 0)
1396                         return id;
1397         }
1398
1399         /* Initialize the video buffers queue. */
1400         uvc_queue_init(&video->queue);
1401
1402         return 0;
1403 }
1404
1405 /*
1406  * Register the video devices.
1407  *
1408  * The driver currently supports a single video device per control interface
1409  * only. The terminal and units must match the following structure:
1410  *
1411  * ITT_CAMERA -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING
1412  *
1413  * The Extension Units, if present, must have a single input pin. The
1414  * Processing Unit and Extension Units can be in any order. Additional
1415  * Extension Units connected to the main chain as single-unit branches are
1416  * also supported.
1417  */
1418 static int uvc_register_video(struct uvc_device *dev)
1419 {
1420         struct video_device *vdev;
1421         struct uvc_entity *term;
1422         int found = 0, ret;
1423
1424         /* Check if the control interface matches the structure we expect. */
1425         list_for_each_entry(term, &dev->entities, list) {
1426                 struct uvc_streaming *streaming;
1427
1428                 if (UVC_ENTITY_TYPE(term) != TT_STREAMING)
1429                         continue;
1430
1431                 memset(&dev->video, 0, sizeof dev->video);
1432                 mutex_init(&dev->video.ctrl_mutex);
1433                 INIT_LIST_HEAD(&dev->video.iterms);
1434                 INIT_LIST_HEAD(&dev->video.extensions);
1435                 dev->video.oterm = term;
1436                 dev->video.dev = dev;
1437                 if (uvc_scan_chain(&dev->video) < 0)
1438                         continue;
1439
1440                 list_for_each_entry(streaming, &dev->streaming, list) {
1441                         if (streaming->header.bTerminalLink == term->id) {
1442                                 dev->video.streaming = streaming;
1443                                 found = 1;
1444                                 break;
1445                         }
1446                 }
1447
1448                 if (found)
1449                         break;
1450         }
1451
1452         if (!found) {
1453                 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1454                 return -1;
1455         }
1456
1457         if (uvc_trace_param & UVC_TRACE_PROBE) {
1458                 uvc_printk(KERN_INFO, "Found a valid video chain (");
1459                 list_for_each_entry(term, &dev->video.iterms, chain) {
1460                         printk("%d", term->id);
1461                         if (term->chain.next != &dev->video.iterms)
1462                                 printk(",");
1463                 }
1464                 printk(" -> %d).\n", dev->video.oterm->id);
1465         }
1466
1467         /* Initialize the streaming interface with default streaming
1468          * parameters.
1469          */
1470         if ((ret = uvc_video_init(&dev->video)) < 0) {
1471                 uvc_printk(KERN_ERR, "Failed to initialize the device "
1472                         "(%d).\n", ret);
1473                 return ret;
1474         }
1475
1476         /* Register the device with V4L. */
1477         vdev = video_device_alloc();
1478         if (vdev == NULL)
1479                 return -1;
1480
1481         /* We already hold a reference to dev->udev. The video device will be
1482          * unregistered before the reference is released, so we don't need to
1483          * get another one.
1484          */
1485         vdev->parent = &dev->intf->dev;
1486         vdev->minor = -1;
1487         vdev->fops = &uvc_fops;
1488         vdev->release = video_device_release;
1489         strncpy(vdev->name, dev->name, sizeof vdev->name);
1490
1491         /* Set the driver data before calling video_register_device, otherwise
1492          * uvc_v4l2_open might race us.
1493          *
1494          * FIXME: usb_set_intfdata hasn't been called so far. Is that a
1495          *        problem ? Does any function which could be called here get
1496          *        a pointer to the usb_interface ?
1497          */
1498         dev->video.vdev = vdev;
1499         video_set_drvdata(vdev, &dev->video);
1500
1501         if (video_register_device(vdev, VFL_TYPE_GRABBER, -1) < 0) {
1502                 dev->video.vdev = NULL;
1503                 video_device_release(vdev);
1504                 return -1;
1505         }
1506
1507         return 0;
1508 }
1509
1510 /*
1511  * Delete the UVC device.
1512  *
1513  * Called by the kernel when the last reference to the uvc_device structure
1514  * is released.
1515  *
1516  * Unregistering the video devices is done here because every opened instance
1517  * must be closed before the device can be unregistered. An alternative would
1518  * have been to use another reference count for uvc_v4l2_open/uvc_release, and
1519  * unregister the video devices on disconnect when that reference count drops
1520  * to zero.
1521  *
1522  * As this function is called after or during disconnect(), all URBs have
1523  * already been canceled by the USB core. There is no need to kill the
1524  * interrupt URB manually.
1525  */
1526 void uvc_delete(struct kref *kref)
1527 {
1528         struct uvc_device *dev = container_of(kref, struct uvc_device, kref);
1529         struct list_head *p, *n;
1530
1531         /* Unregister the video device */
1532         uvc_unregister_video(dev);
1533         usb_put_intf(dev->intf);
1534         usb_put_dev(dev->udev);
1535
1536         uvc_status_cleanup(dev);
1537         uvc_ctrl_cleanup_device(dev);
1538
1539         list_for_each_safe(p, n, &dev->entities) {
1540                 struct uvc_entity *entity;
1541                 entity = list_entry(p, struct uvc_entity, list);
1542                 kfree(entity);
1543         }
1544
1545         list_for_each_safe(p, n, &dev->streaming) {
1546                 struct uvc_streaming *streaming;
1547                 streaming = list_entry(p, struct uvc_streaming, list);
1548                 usb_driver_release_interface(&uvc_driver.driver,
1549                         streaming->intf);
1550                 usb_put_intf(streaming->intf);
1551                 kfree(streaming->format);
1552                 kfree(streaming->header.bmaControls);
1553                 kfree(streaming);
1554         }
1555
1556         kfree(dev);
1557 }
1558
1559 static int uvc_probe(struct usb_interface *intf,
1560                      const struct usb_device_id *id)
1561 {
1562         struct usb_device *udev = interface_to_usbdev(intf);
1563         struct uvc_device *dev;
1564         int ret;
1565
1566         if (id->idVendor && id->idProduct)
1567                 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1568                                 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1569                                 id->idProduct);
1570         else
1571                 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1572                                 udev->devpath);
1573
1574         /* Allocate memory for the device and initialize it */
1575         if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1576                 return -ENOMEM;
1577
1578         INIT_LIST_HEAD(&dev->entities);
1579         INIT_LIST_HEAD(&dev->streaming);
1580         kref_init(&dev->kref);
1581
1582         dev->udev = usb_get_dev(udev);
1583         dev->intf = usb_get_intf(intf);
1584         dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1585         dev->quirks = id->driver_info | uvc_quirks_param;
1586
1587         if (udev->product != NULL)
1588                 strncpy(dev->name, udev->product, sizeof dev->name);
1589         else
1590                 snprintf(dev->name, sizeof dev->name,
1591                         "UVC Camera (%04x:%04x)",
1592                         le16_to_cpu(udev->descriptor.idVendor),
1593                         le16_to_cpu(udev->descriptor.idProduct));
1594
1595         /* Parse the Video Class control descriptor */
1596         if (uvc_parse_control(dev) < 0) {
1597                 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1598                         "descriptors.\n");
1599                 goto error;
1600         }
1601
1602         uvc_printk(KERN_INFO, "Found UVC %u.%02u device %s (%04x:%04x)\n",
1603                 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1604                 udev->product ? udev->product : "<unnamed>",
1605                 le16_to_cpu(udev->descriptor.idVendor),
1606                 le16_to_cpu(udev->descriptor.idProduct));
1607
1608         if (uvc_quirks_param != 0) {
1609                 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1610                         "parameter for testing purpose.\n", uvc_quirks_param);
1611                 uvc_printk(KERN_INFO, "Please report required quirks to the "
1612                         "linux-uvc-devel mailing list.\n");
1613         }
1614
1615         /* Initialize controls */
1616         if (uvc_ctrl_init_device(dev) < 0)
1617                 goto error;
1618
1619         /* Register the video devices */
1620         if (uvc_register_video(dev) < 0)
1621                 goto error;
1622
1623         /* Save our data pointer in the interface data */
1624         usb_set_intfdata(intf, dev);
1625
1626         /* Initialize the interrupt URB */
1627         if ((ret = uvc_status_init(dev)) < 0) {
1628                 uvc_printk(KERN_INFO, "Unable to initialize the status "
1629                         "endpoint (%d), status interrupt will not be "
1630                         "supported.\n", ret);
1631         }
1632
1633         uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1634         return 0;
1635
1636 error:
1637         kref_put(&dev->kref, uvc_delete);
1638         return -ENODEV;
1639 }
1640
1641 static void uvc_disconnect(struct usb_interface *intf)
1642 {
1643         struct uvc_device *dev = usb_get_intfdata(intf);
1644
1645         /* Set the USB interface data to NULL. This can be done outside the
1646          * lock, as there's no other reader.
1647          */
1648         usb_set_intfdata(intf, NULL);
1649
1650         if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOSTREAMING)
1651                 return;
1652
1653         /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1654          * lock is needed to prevent uvc_disconnect from releasing its
1655          * reference to the uvc_device instance after uvc_v4l2_open() received
1656          * the pointer to the device (video_devdata) but before it got the
1657          * chance to increase the reference count (kref_get).
1658          *
1659          * Note that the reference can't be released with the lock held,
1660          * otherwise a AB-BA deadlock can occur with videodev_lock that
1661          * videodev acquires in videodev_open() and video_unregister_device().
1662          */
1663         mutex_lock(&uvc_driver.open_mutex);
1664         dev->state |= UVC_DEV_DISCONNECTED;
1665         mutex_unlock(&uvc_driver.open_mutex);
1666
1667         kref_put(&dev->kref, uvc_delete);
1668 }
1669
1670 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1671 {
1672         struct uvc_device *dev = usb_get_intfdata(intf);
1673
1674         uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1675                 intf->cur_altsetting->desc.bInterfaceNumber);
1676
1677         /* Controls are cached on the fly so they don't need to be saved. */
1678         if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL)
1679                 return uvc_status_suspend(dev);
1680
1681         if (dev->video.streaming->intf != intf) {
1682                 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB "
1683                                 "interface mismatch.\n");
1684                 return -EINVAL;
1685         }
1686
1687         return uvc_video_suspend(&dev->video);
1688 }
1689
1690 static int __uvc_resume(struct usb_interface *intf, int reset)
1691 {
1692         struct uvc_device *dev = usb_get_intfdata(intf);
1693         int ret;
1694
1695         uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1696                 intf->cur_altsetting->desc.bInterfaceNumber);
1697
1698         if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL) {
1699                 if (reset && (ret = uvc_ctrl_resume_device(dev)) < 0)
1700                         return ret;
1701
1702                 return uvc_status_resume(dev);
1703         }
1704
1705         if (dev->video.streaming->intf != intf) {
1706                 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB "
1707                                 "interface mismatch.\n");
1708                 return -EINVAL;
1709         }
1710
1711         return uvc_video_resume(&dev->video);
1712 }
1713
1714 static int uvc_resume(struct usb_interface *intf)
1715 {
1716         return __uvc_resume(intf, 0);
1717 }
1718
1719 static int uvc_reset_resume(struct usb_interface *intf)
1720 {
1721         return __uvc_resume(intf, 1);
1722 }
1723
1724 /* ------------------------------------------------------------------------
1725  * Driver initialization and cleanup
1726  */
1727
1728 /*
1729  * The Logitech cameras listed below have their interface class set to
1730  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1731  * though they are compliant.
1732  */
1733 static struct usb_device_id uvc_ids[] = {
1734         /* Microsoft Lifecam NX-6000 */
1735         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1736                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1737           .idVendor             = 0x045e,
1738           .idProduct            = 0x00f8,
1739           .bInterfaceClass      = USB_CLASS_VIDEO,
1740           .bInterfaceSubClass   = 1,
1741           .bInterfaceProtocol   = 0,
1742           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1743         /* Microsoft Lifecam VX-7000 */
1744         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1745                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1746           .idVendor             = 0x045e,
1747           .idProduct            = 0x0723,
1748           .bInterfaceClass      = USB_CLASS_VIDEO,
1749           .bInterfaceSubClass   = 1,
1750           .bInterfaceProtocol   = 0,
1751           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1752         /* Logitech Quickcam Fusion */
1753         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1754                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1755           .idVendor             = 0x046d,
1756           .idProduct            = 0x08c1,
1757           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1758           .bInterfaceSubClass   = 1,
1759           .bInterfaceProtocol   = 0 },
1760         /* Logitech Quickcam Orbit MP */
1761         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1762                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1763           .idVendor             = 0x046d,
1764           .idProduct            = 0x08c2,
1765           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1766           .bInterfaceSubClass   = 1,
1767           .bInterfaceProtocol   = 0 },
1768         /* Logitech Quickcam Pro for Notebook */
1769         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1770                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1771           .idVendor             = 0x046d,
1772           .idProduct            = 0x08c3,
1773           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1774           .bInterfaceSubClass   = 1,
1775           .bInterfaceProtocol   = 0 },
1776         /* Logitech Quickcam Pro 5000 */
1777         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1778                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1779           .idVendor             = 0x046d,
1780           .idProduct            = 0x08c5,
1781           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1782           .bInterfaceSubClass   = 1,
1783           .bInterfaceProtocol   = 0 },
1784         /* Logitech Quickcam OEM Dell Notebook */
1785         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1786                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1787           .idVendor             = 0x046d,
1788           .idProduct            = 0x08c6,
1789           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1790           .bInterfaceSubClass   = 1,
1791           .bInterfaceProtocol   = 0 },
1792         /* Logitech Quickcam OEM Cisco VT Camera II */
1793         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1794                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1795           .idVendor             = 0x046d,
1796           .idProduct            = 0x08c7,
1797           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1798           .bInterfaceSubClass   = 1,
1799           .bInterfaceProtocol   = 0 },
1800         /* Apple Built-In iSight */
1801         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1802                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1803           .idVendor             = 0x05ac,
1804           .idProduct            = 0x8501,
1805           .bInterfaceClass      = USB_CLASS_VIDEO,
1806           .bInterfaceSubClass   = 1,
1807           .bInterfaceProtocol   = 0,
1808           .driver_info          = UVC_QUIRK_PROBE_MINMAX
1809                                 | UVC_QUIRK_BUILTIN_ISIGHT },
1810         /* Genesys Logic USB 2.0 PC Camera */
1811         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1812                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1813           .idVendor             = 0x05e3,
1814           .idProduct            = 0x0505,
1815           .bInterfaceClass      = USB_CLASS_VIDEO,
1816           .bInterfaceSubClass   = 1,
1817           .bInterfaceProtocol   = 0,
1818           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1819         /* MT6227 */
1820         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1821                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1822           .idVendor             = 0x0e8d,
1823           .idProduct            = 0x0004,
1824           .bInterfaceClass      = USB_CLASS_VIDEO,
1825           .bInterfaceSubClass   = 1,
1826           .bInterfaceProtocol   = 0,
1827           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1828         /* Syntek (HP Spartan) */
1829         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1830                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1831           .idVendor             = 0x174f,
1832           .idProduct            = 0x5212,
1833           .bInterfaceClass      = USB_CLASS_VIDEO,
1834           .bInterfaceSubClass   = 1,
1835           .bInterfaceProtocol   = 0,
1836           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1837         /* Syntek (Samsung Q310) */
1838         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1839                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1840           .idVendor             = 0x174f,
1841           .idProduct            = 0x5931,
1842           .bInterfaceClass      = USB_CLASS_VIDEO,
1843           .bInterfaceSubClass   = 1,
1844           .bInterfaceProtocol   = 0,
1845           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1846         /* Asus F9SG */
1847         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1848                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1849           .idVendor             = 0x174f,
1850           .idProduct            = 0x8a31,
1851           .bInterfaceClass      = USB_CLASS_VIDEO,
1852           .bInterfaceSubClass   = 1,
1853           .bInterfaceProtocol   = 0,
1854           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1855         /* Syntek (Asus U3S) */
1856         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1857                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1858           .idVendor             = 0x174f,
1859           .idProduct            = 0x8a33,
1860           .bInterfaceClass      = USB_CLASS_VIDEO,
1861           .bInterfaceSubClass   = 1,
1862           .bInterfaceProtocol   = 0,
1863           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1864         /* Lenovo Thinkpad SL500 */
1865         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1866                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1867           .idVendor             = 0x17ef,
1868           .idProduct            = 0x480b,
1869           .bInterfaceClass      = USB_CLASS_VIDEO,
1870           .bInterfaceSubClass   = 1,
1871           .bInterfaceProtocol   = 0,
1872           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1873         /* Ecamm Pico iMage */
1874         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1875                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1876           .idVendor             = 0x18cd,
1877           .idProduct            = 0xcafe,
1878           .bInterfaceClass      = USB_CLASS_VIDEO,
1879           .bInterfaceSubClass   = 1,
1880           .bInterfaceProtocol   = 0,
1881           .driver_info          = UVC_QUIRK_PROBE_EXTRAFIELDS },
1882         /* Bodelin ProScopeHR */
1883         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1884                                 | USB_DEVICE_ID_MATCH_DEV_HI
1885                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1886           .idVendor             = 0x19ab,
1887           .idProduct            = 0x1000,
1888           .bcdDevice_hi         = 0x0126,
1889           .bInterfaceClass      = USB_CLASS_VIDEO,
1890           .bInterfaceSubClass   = 1,
1891           .bInterfaceProtocol   = 0,
1892           .driver_info          = UVC_QUIRK_STATUS_INTERVAL },
1893         /* SiGma Micro USB Web Camera */
1894         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1895                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1896           .idVendor             = 0x1c4f,
1897           .idProduct            = 0x3000,
1898           .bInterfaceClass      = USB_CLASS_VIDEO,
1899           .bInterfaceSubClass   = 1,
1900           .bInterfaceProtocol   = 0,
1901           .driver_info          = UVC_QUIRK_PROBE_MINMAX
1902                                 | UVC_QUIRK_IGNORE_SELECTOR_UNIT
1903                                 | UVC_QUIRK_PRUNE_CONTROLS },
1904         /* Generic USB Video Class */
1905         { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
1906         {}
1907 };
1908
1909 MODULE_DEVICE_TABLE(usb, uvc_ids);
1910
1911 struct uvc_driver uvc_driver = {
1912         .driver = {
1913                 .name           = "uvcvideo",
1914                 .probe          = uvc_probe,
1915                 .disconnect     = uvc_disconnect,
1916                 .suspend        = uvc_suspend,
1917                 .resume         = uvc_resume,
1918                 .reset_resume   = uvc_reset_resume,
1919                 .id_table       = uvc_ids,
1920                 .supports_autosuspend = 1,
1921         },
1922 };
1923
1924 static int __init uvc_init(void)
1925 {
1926         int result;
1927
1928         INIT_LIST_HEAD(&uvc_driver.devices);
1929         INIT_LIST_HEAD(&uvc_driver.controls);
1930         mutex_init(&uvc_driver.open_mutex);
1931         mutex_init(&uvc_driver.ctrl_mutex);
1932
1933         uvc_ctrl_init();
1934
1935         result = usb_register(&uvc_driver.driver);
1936         if (result == 0)
1937                 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
1938         return result;
1939 }
1940
1941 static void __exit uvc_cleanup(void)
1942 {
1943         usb_deregister(&uvc_driver.driver);
1944 }
1945
1946 module_init(uvc_init);
1947 module_exit(uvc_cleanup);
1948
1949 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1950 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1951 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1952 MODULE_PARM_DESC(quirks, "Forced device quirks");
1953 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1954 MODULE_PARM_DESC(trace, "Trace level bitmask");
1955
1956 MODULE_AUTHOR(DRIVER_AUTHOR);
1957 MODULE_DESCRIPTION(DRIVER_DESC);
1958 MODULE_LICENSE("GPL");
1959 MODULE_VERSION(DRIVER_VERSION);
1960