]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/uvc/uvc_driver.c
V4L/DVB (13308): uvcvideo: Add support for MSI StarCam 370i webcams
[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-2009
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  */
13
14 /*
15  * This driver aims to support video input and ouput devices compliant with the
16  * 'USB Video Class' specification.
17  *
18  * The driver doesn't support the deprecated v4l1 interface. It implements the
19  * mmap capture method only, and doesn't do any image format conversion in
20  * software. If your user-space application doesn't support YUYV or MJPEG, fix
21  * it :-). Please note that the MJPEG data have been stripped from their
22  * Huffman tables (DHT marker), you will need to add it back if your JPEG
23  * codec can't handle MJPEG data.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/usb.h>
30 #include <linux/videodev2.h>
31 #include <linux/vmalloc.h>
32 #include <linux/wait.h>
33 #include <asm/atomic.h>
34 #include <asm/unaligned.h>
35
36 #include <media/v4l2-common.h>
37
38 #include "uvcvideo.h"
39
40 #define DRIVER_AUTHOR           "Laurent Pinchart <laurent.pinchart@skynet.be>"
41 #define DRIVER_DESC             "USB Video Class driver"
42 #ifndef DRIVER_VERSION
43 #define DRIVER_VERSION          "v0.1.0"
44 #endif
45
46 unsigned int uvc_no_drop_param;
47 static unsigned int uvc_quirks_param;
48 unsigned int uvc_trace_param;
49 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
50
51 /* ------------------------------------------------------------------------
52  * Video 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 UVC_TT_STREAMING:
254                         if (entity->output.bSourceID == id)
255                                 return entity;
256                         break;
257
258                 case UVC_VC_PROCESSING_UNIT:
259                         if (entity->processing.bSourceID == id)
260                                 return entity;
261                         break;
262
263                 case UVC_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 UVC_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 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
281 {
282         struct uvc_streaming *stream;
283
284         list_for_each_entry(stream, &dev->streams, list) {
285                 if (stream->header.bTerminalLink == id)
286                         return stream;
287         }
288
289         return NULL;
290 }
291
292 /* ------------------------------------------------------------------------
293  * Descriptors parsing
294  */
295
296 static int uvc_parse_format(struct uvc_device *dev,
297         struct uvc_streaming *streaming, struct uvc_format *format,
298         __u32 **intervals, unsigned char *buffer, int buflen)
299 {
300         struct usb_interface *intf = streaming->intf;
301         struct usb_host_interface *alts = intf->cur_altsetting;
302         struct uvc_format_desc *fmtdesc;
303         struct uvc_frame *frame;
304         const unsigned char *start = buffer;
305         unsigned int interval;
306         unsigned int i, n;
307         __u8 ftype;
308
309         format->type = buffer[2];
310         format->index = buffer[3];
311
312         switch (buffer[2]) {
313         case UVC_VS_FORMAT_UNCOMPRESSED:
314         case UVC_VS_FORMAT_FRAME_BASED:
315                 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
316                 if (buflen < n) {
317                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
318                                "interface %d FORMAT error\n",
319                                dev->udev->devnum,
320                                alts->desc.bInterfaceNumber);
321                         return -EINVAL;
322                 }
323
324                 /* Find the format descriptor from its GUID. */
325                 fmtdesc = uvc_format_by_guid(&buffer[5]);
326
327                 if (fmtdesc != NULL) {
328                         strlcpy(format->name, fmtdesc->name,
329                                 sizeof format->name);
330                         format->fcc = fmtdesc->fcc;
331                 } else {
332                         uvc_printk(KERN_INFO, "Unknown video format "
333                                 UVC_GUID_FORMAT "\n",
334                                 UVC_GUID_ARGS(&buffer[5]));
335                         snprintf(format->name, sizeof format->name,
336                                 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
337                         format->fcc = 0;
338                 }
339
340                 format->bpp = buffer[21];
341                 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
342                         ftype = UVC_VS_FRAME_UNCOMPRESSED;
343                 } else {
344                         ftype = UVC_VS_FRAME_FRAME_BASED;
345                         if (buffer[27])
346                                 format->flags = UVC_FMT_FLAG_COMPRESSED;
347                 }
348                 break;
349
350         case UVC_VS_FORMAT_MJPEG:
351                 if (buflen < 11) {
352                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
353                                "interface %d FORMAT error\n",
354                                dev->udev->devnum,
355                                alts->desc.bInterfaceNumber);
356                         return -EINVAL;
357                 }
358
359                 strlcpy(format->name, "MJPEG", sizeof format->name);
360                 format->fcc = V4L2_PIX_FMT_MJPEG;
361                 format->flags = UVC_FMT_FLAG_COMPRESSED;
362                 format->bpp = 0;
363                 ftype = UVC_VS_FRAME_MJPEG;
364                 break;
365
366         case UVC_VS_FORMAT_DV:
367                 if (buflen < 9) {
368                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
369                                "interface %d FORMAT error\n",
370                                dev->udev->devnum,
371                                alts->desc.bInterfaceNumber);
372                         return -EINVAL;
373                 }
374
375                 switch (buffer[8] & 0x7f) {
376                 case 0:
377                         strlcpy(format->name, "SD-DV", sizeof format->name);
378                         break;
379                 case 1:
380                         strlcpy(format->name, "SDL-DV", sizeof format->name);
381                         break;
382                 case 2:
383                         strlcpy(format->name, "HD-DV", sizeof format->name);
384                         break;
385                 default:
386                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
387                                "interface %d: unknown DV format %u\n",
388                                dev->udev->devnum,
389                                alts->desc.bInterfaceNumber, buffer[8]);
390                         return -EINVAL;
391                 }
392
393                 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
394                         sizeof format->name);
395
396                 format->fcc = V4L2_PIX_FMT_DV;
397                 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
398                 format->bpp = 0;
399                 ftype = 0;
400
401                 /* Create a dummy frame descriptor. */
402                 frame = &format->frame[0];
403                 memset(&format->frame[0], 0, sizeof format->frame[0]);
404                 frame->bFrameIntervalType = 1;
405                 frame->dwDefaultFrameInterval = 1;
406                 frame->dwFrameInterval = *intervals;
407                 *(*intervals)++ = 1;
408                 format->nframes = 1;
409                 break;
410
411         case UVC_VS_FORMAT_MPEG2TS:
412         case UVC_VS_FORMAT_STREAM_BASED:
413                 /* Not supported yet. */
414         default:
415                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
416                        "interface %d unsupported format %u\n",
417                        dev->udev->devnum, alts->desc.bInterfaceNumber,
418                        buffer[2]);
419                 return -EINVAL;
420         }
421
422         uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
423
424         buflen -= buffer[0];
425         buffer += buffer[0];
426
427         /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
428          * based formats have frame descriptors.
429          */
430         while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
431                buffer[2] == ftype) {
432                 frame = &format->frame[format->nframes];
433                 if (ftype != UVC_VS_FRAME_FRAME_BASED)
434                         n = buflen > 25 ? buffer[25] : 0;
435                 else
436                         n = buflen > 21 ? buffer[21] : 0;
437
438                 n = n ? n : 3;
439
440                 if (buflen < 26 + 4*n) {
441                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
442                                "interface %d FRAME error\n", dev->udev->devnum,
443                                alts->desc.bInterfaceNumber);
444                         return -EINVAL;
445                 }
446
447                 frame->bFrameIndex = buffer[3];
448                 frame->bmCapabilities = buffer[4];
449                 frame->wWidth = get_unaligned_le16(&buffer[5]);
450                 frame->wHeight = get_unaligned_le16(&buffer[7]);
451                 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
452                 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
453                 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
454                         frame->dwMaxVideoFrameBufferSize =
455                                 get_unaligned_le32(&buffer[17]);
456                         frame->dwDefaultFrameInterval =
457                                 get_unaligned_le32(&buffer[21]);
458                         frame->bFrameIntervalType = buffer[25];
459                 } else {
460                         frame->dwMaxVideoFrameBufferSize = 0;
461                         frame->dwDefaultFrameInterval =
462                                 get_unaligned_le32(&buffer[17]);
463                         frame->bFrameIntervalType = buffer[21];
464                 }
465                 frame->dwFrameInterval = *intervals;
466
467                 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
468                  * completely. Observed behaviours range from setting the
469                  * value to 1.1x the actual frame size to hardwiring the
470                  * 16 low bits to 0. This results in a higher than necessary
471                  * memory usage as well as a wrong image size information. For
472                  * uncompressed formats this can be fixed by computing the
473                  * value from the frame size.
474                  */
475                 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
476                         frame->dwMaxVideoFrameBufferSize = format->bpp
477                                 * frame->wWidth * frame->wHeight / 8;
478
479                 /* Some bogus devices report dwMinFrameInterval equal to
480                  * dwMaxFrameInterval and have dwFrameIntervalStep set to
481                  * zero. Setting all null intervals to 1 fixes the problem and
482                  * some other divisions by zero that could happen.
483                  */
484                 for (i = 0; i < n; ++i) {
485                         interval = get_unaligned_le32(&buffer[26+4*i]);
486                         *(*intervals)++ = interval ? interval : 1;
487                 }
488
489                 /* Make sure that the default frame interval stays between
490                  * the boundaries.
491                  */
492                 n -= frame->bFrameIntervalType ? 1 : 2;
493                 frame->dwDefaultFrameInterval =
494                         min(frame->dwFrameInterval[n],
495                             max(frame->dwFrameInterval[0],
496                                 frame->dwDefaultFrameInterval));
497
498                 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
499                         frame->wWidth, frame->wHeight,
500                         10000000/frame->dwDefaultFrameInterval,
501                         (100000000/frame->dwDefaultFrameInterval)%10);
502
503                 format->nframes++;
504                 buflen -= buffer[0];
505                 buffer += buffer[0];
506         }
507
508         if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
509             buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
510                 buflen -= buffer[0];
511                 buffer += buffer[0];
512         }
513
514         if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
515             buffer[2] == UVC_VS_COLORFORMAT) {
516                 if (buflen < 6) {
517                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
518                                "interface %d COLORFORMAT error\n",
519                                dev->udev->devnum,
520                                alts->desc.bInterfaceNumber);
521                         return -EINVAL;
522                 }
523
524                 format->colorspace = uvc_colorspace(buffer[3]);
525
526                 buflen -= buffer[0];
527                 buffer += buffer[0];
528         }
529
530         return buffer - start;
531 }
532
533 static int uvc_parse_streaming(struct uvc_device *dev,
534         struct usb_interface *intf)
535 {
536         struct uvc_streaming *streaming = NULL;
537         struct uvc_format *format;
538         struct uvc_frame *frame;
539         struct usb_host_interface *alts = &intf->altsetting[0];
540         unsigned char *_buffer, *buffer = alts->extra;
541         int _buflen, buflen = alts->extralen;
542         unsigned int nformats = 0, nframes = 0, nintervals = 0;
543         unsigned int size, i, n, p;
544         __u32 *interval;
545         __u16 psize;
546         int ret = -EINVAL;
547
548         if (intf->cur_altsetting->desc.bInterfaceSubClass
549                 != UVC_SC_VIDEOSTREAMING) {
550                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
551                         "video streaming interface\n", dev->udev->devnum,
552                         intf->altsetting[0].desc.bInterfaceNumber);
553                 return -EINVAL;
554         }
555
556         if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
557                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
558                         "claimed\n", dev->udev->devnum,
559                         intf->altsetting[0].desc.bInterfaceNumber);
560                 return -EINVAL;
561         }
562
563         streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
564         if (streaming == NULL) {
565                 usb_driver_release_interface(&uvc_driver.driver, intf);
566                 return -EINVAL;
567         }
568
569         mutex_init(&streaming->mutex);
570         streaming->dev = dev;
571         streaming->intf = usb_get_intf(intf);
572         streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
573
574         /* The Pico iMage webcam has its class-specific interface descriptors
575          * after the endpoint descriptors.
576          */
577         if (buflen == 0) {
578                 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
579                         struct usb_host_endpoint *ep = &alts->endpoint[i];
580
581                         if (ep->extralen == 0)
582                                 continue;
583
584                         if (ep->extralen > 2 &&
585                             ep->extra[1] == USB_DT_CS_INTERFACE) {
586                                 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
587                                         "from endpoint %u.\n", i);
588                                 buffer = alts->endpoint[i].extra;
589                                 buflen = alts->endpoint[i].extralen;
590                                 break;
591                         }
592                 }
593         }
594
595         /* Skip the standard interface descriptors. */
596         while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
597                 buflen -= buffer[0];
598                 buffer += buffer[0];
599         }
600
601         if (buflen <= 2) {
602                 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
603                         "interface descriptors found.\n");
604                 goto error;
605         }
606
607         /* Parse the header descriptor. */
608         switch (buffer[2]) {
609         case UVC_VS_OUTPUT_HEADER:
610                 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
611                 size = 9;
612                 break;
613
614         case UVC_VS_INPUT_HEADER:
615                 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
616                 size = 13;
617                 break;
618
619         default:
620                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
621                         "%d HEADER descriptor not found.\n", dev->udev->devnum,
622                         alts->desc.bInterfaceNumber);
623                 goto error;
624         }
625
626         p = buflen >= 4 ? buffer[3] : 0;
627         n = buflen >= size ? buffer[size-1] : 0;
628
629         if (buflen < size + p*n) {
630                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
631                         "interface %d HEADER descriptor is invalid.\n",
632                         dev->udev->devnum, alts->desc.bInterfaceNumber);
633                 goto error;
634         }
635
636         streaming->header.bNumFormats = p;
637         streaming->header.bEndpointAddress = buffer[6];
638         if (buffer[2] == UVC_VS_INPUT_HEADER) {
639                 streaming->header.bmInfo = buffer[7];
640                 streaming->header.bTerminalLink = buffer[8];
641                 streaming->header.bStillCaptureMethod = buffer[9];
642                 streaming->header.bTriggerSupport = buffer[10];
643                 streaming->header.bTriggerUsage = buffer[11];
644         } else {
645                 streaming->header.bTerminalLink = buffer[7];
646         }
647         streaming->header.bControlSize = n;
648
649         streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
650         if (streaming->header.bmaControls == NULL) {
651                 ret = -ENOMEM;
652                 goto error;
653         }
654
655         memcpy(streaming->header.bmaControls, &buffer[size], p*n);
656
657         buflen -= buffer[0];
658         buffer += buffer[0];
659
660         _buffer = buffer;
661         _buflen = buflen;
662
663         /* Count the format and frame descriptors. */
664         while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
665                 switch (_buffer[2]) {
666                 case UVC_VS_FORMAT_UNCOMPRESSED:
667                 case UVC_VS_FORMAT_MJPEG:
668                 case UVC_VS_FORMAT_FRAME_BASED:
669                         nformats++;
670                         break;
671
672                 case UVC_VS_FORMAT_DV:
673                         /* DV format has no frame descriptor. We will create a
674                          * dummy frame descriptor with a dummy frame interval.
675                          */
676                         nformats++;
677                         nframes++;
678                         nintervals++;
679                         break;
680
681                 case UVC_VS_FORMAT_MPEG2TS:
682                 case UVC_VS_FORMAT_STREAM_BASED:
683                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
684                                 "interface %d FORMAT %u is not supported.\n",
685                                 dev->udev->devnum,
686                                 alts->desc.bInterfaceNumber, _buffer[2]);
687                         break;
688
689                 case UVC_VS_FRAME_UNCOMPRESSED:
690                 case UVC_VS_FRAME_MJPEG:
691                         nframes++;
692                         if (_buflen > 25)
693                                 nintervals += _buffer[25] ? _buffer[25] : 3;
694                         break;
695
696                 case UVC_VS_FRAME_FRAME_BASED:
697                         nframes++;
698                         if (_buflen > 21)
699                                 nintervals += _buffer[21] ? _buffer[21] : 3;
700                         break;
701                 }
702
703                 _buflen -= _buffer[0];
704                 _buffer += _buffer[0];
705         }
706
707         if (nformats == 0) {
708                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
709                         "%d has no supported formats defined.\n",
710                         dev->udev->devnum, alts->desc.bInterfaceNumber);
711                 goto error;
712         }
713
714         size = nformats * sizeof *format + nframes * sizeof *frame
715              + nintervals * sizeof *interval;
716         format = kzalloc(size, GFP_KERNEL);
717         if (format == NULL) {
718                 ret = -ENOMEM;
719                 goto error;
720         }
721
722         frame = (struct uvc_frame *)&format[nformats];
723         interval = (__u32 *)&frame[nframes];
724
725         streaming->format = format;
726         streaming->nformats = nformats;
727
728         /* Parse the format descriptors. */
729         while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
730                 switch (buffer[2]) {
731                 case UVC_VS_FORMAT_UNCOMPRESSED:
732                 case UVC_VS_FORMAT_MJPEG:
733                 case UVC_VS_FORMAT_DV:
734                 case UVC_VS_FORMAT_FRAME_BASED:
735                         format->frame = frame;
736                         ret = uvc_parse_format(dev, streaming, format,
737                                 &interval, buffer, buflen);
738                         if (ret < 0)
739                                 goto error;
740
741                         frame += format->nframes;
742                         format++;
743
744                         buflen -= ret;
745                         buffer += ret;
746                         continue;
747
748                 default:
749                         break;
750                 }
751
752                 buflen -= buffer[0];
753                 buffer += buffer[0];
754         }
755
756         if (buflen)
757                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
758                         "%d has %u bytes of trailing descriptor garbage.\n",
759                         dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
760
761         /* Parse the alternate settings to find the maximum bandwidth. */
762         for (i = 0; i < intf->num_altsetting; ++i) {
763                 struct usb_host_endpoint *ep;
764                 alts = &intf->altsetting[i];
765                 ep = uvc_find_endpoint(alts,
766                                 streaming->header.bEndpointAddress);
767                 if (ep == NULL)
768                         continue;
769
770                 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
771                 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
772                 if (psize > streaming->maxpsize)
773                         streaming->maxpsize = psize;
774         }
775
776         list_add_tail(&streaming->list, &dev->streams);
777         return 0;
778
779 error:
780         usb_driver_release_interface(&uvc_driver.driver, intf);
781         usb_put_intf(intf);
782         kfree(streaming->format);
783         kfree(streaming->header.bmaControls);
784         kfree(streaming);
785         return ret;
786 }
787
788 /* Parse vendor-specific extensions. */
789 static int uvc_parse_vendor_control(struct uvc_device *dev,
790         const unsigned char *buffer, int buflen)
791 {
792         struct usb_device *udev = dev->udev;
793         struct usb_host_interface *alts = dev->intf->cur_altsetting;
794         struct uvc_entity *unit;
795         unsigned int n, p;
796         int handled = 0;
797
798         switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
799         case 0x046d:            /* Logitech */
800                 if (buffer[1] != 0x41 || buffer[2] != 0x01)
801                         break;
802
803                 /* Logitech implements several vendor specific functions
804                  * through vendor specific extension units (LXU).
805                  *
806                  * The LXU descriptors are similar to XU descriptors
807                  * (see "USB Device Video Class for Video Devices", section
808                  * 3.7.2.6 "Extension Unit Descriptor") with the following
809                  * differences:
810                  *
811                  * ----------------------------------------------------------
812                  * 0            bLength         1        Number
813                  *      Size of this descriptor, in bytes: 24+p+n*2
814                  * ----------------------------------------------------------
815                  * 23+p+n       bmControlsType  N       Bitmap
816                  *      Individual bits in the set are defined:
817                  *      0: Absolute
818                  *      1: Relative
819                  *
820                  *      This bitset is mapped exactly the same as bmControls.
821                  * ----------------------------------------------------------
822                  * 23+p+n*2     bReserved       1       Boolean
823                  * ----------------------------------------------------------
824                  * 24+p+n*2     iExtension      1       Index
825                  *      Index of a string descriptor that describes this
826                  *      extension unit.
827                  * ----------------------------------------------------------
828                  */
829                 p = buflen >= 22 ? buffer[21] : 0;
830                 n = buflen >= 25 + p ? buffer[22+p] : 0;
831
832                 if (buflen < 25 + p + 2*n) {
833                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
834                                 "interface %d EXTENSION_UNIT error\n",
835                                 udev->devnum, alts->desc.bInterfaceNumber);
836                         break;
837                 }
838
839                 unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
840                 if (unit == NULL)
841                         return -ENOMEM;
842
843                 unit->id = buffer[3];
844                 unit->type = UVC_VC_EXTENSION_UNIT;
845                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
846                 unit->extension.bNumControls = buffer[20];
847                 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
848                 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
849                 memcpy(unit->extension.baSourceID, &buffer[22], p);
850                 unit->extension.bControlSize = buffer[22+p];
851                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
852                 unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
853                                                + p + n;
854                 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
855
856                 if (buffer[24+p+2*n] != 0)
857                         usb_string(udev, buffer[24+p+2*n], unit->name,
858                                    sizeof unit->name);
859                 else
860                         sprintf(unit->name, "Extension %u", buffer[3]);
861
862                 list_add_tail(&unit->list, &dev->entities);
863                 handled = 1;
864                 break;
865         }
866
867         return handled;
868 }
869
870 static int uvc_parse_standard_control(struct uvc_device *dev,
871         const unsigned char *buffer, int buflen)
872 {
873         struct usb_device *udev = dev->udev;
874         struct uvc_entity *unit, *term;
875         struct usb_interface *intf;
876         struct usb_host_interface *alts = dev->intf->cur_altsetting;
877         unsigned int i, n, p, len;
878         __u16 type;
879
880         switch (buffer[2]) {
881         case UVC_VC_HEADER:
882                 n = buflen >= 12 ? buffer[11] : 0;
883
884                 if (buflen < 12 || buflen < 12 + n) {
885                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
886                                 "interface %d HEADER error\n", udev->devnum,
887                                 alts->desc.bInterfaceNumber);
888                         return -EINVAL;
889                 }
890
891                 dev->uvc_version = get_unaligned_le16(&buffer[3]);
892                 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
893
894                 /* Parse all USB Video Streaming interfaces. */
895                 for (i = 0; i < n; ++i) {
896                         intf = usb_ifnum_to_if(udev, buffer[12+i]);
897                         if (intf == NULL) {
898                                 uvc_trace(UVC_TRACE_DESCR, "device %d "
899                                         "interface %d doesn't exists\n",
900                                         udev->devnum, i);
901                                 continue;
902                         }
903
904                         uvc_parse_streaming(dev, intf);
905                 }
906                 break;
907
908         case UVC_VC_INPUT_TERMINAL:
909                 if (buflen < 8) {
910                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
911                                 "interface %d INPUT_TERMINAL error\n",
912                                 udev->devnum, alts->desc.bInterfaceNumber);
913                         return -EINVAL;
914                 }
915
916                 /* Make sure the terminal type MSB is not null, otherwise it
917                  * could be confused with a unit.
918                  */
919                 type = get_unaligned_le16(&buffer[4]);
920                 if ((type & 0xff00) == 0) {
921                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
922                                 "interface %d INPUT_TERMINAL %d has invalid "
923                                 "type 0x%04x, skipping\n", udev->devnum,
924                                 alts->desc.bInterfaceNumber,
925                                 buffer[3], type);
926                         return 0;
927                 }
928
929                 n = 0;
930                 p = 0;
931                 len = 8;
932
933                 if (type == UVC_ITT_CAMERA) {
934                         n = buflen >= 15 ? buffer[14] : 0;
935                         len = 15;
936
937                 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
938                         n = buflen >= 9 ? buffer[8] : 0;
939                         p = buflen >= 10 + n ? buffer[9+n] : 0;
940                         len = 10;
941                 }
942
943                 if (buflen < len + n + p) {
944                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
945                                 "interface %d INPUT_TERMINAL error\n",
946                                 udev->devnum, alts->desc.bInterfaceNumber);
947                         return -EINVAL;
948                 }
949
950                 term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
951                 if (term == NULL)
952                         return -ENOMEM;
953
954                 term->id = buffer[3];
955                 term->type = type | UVC_TERM_INPUT;
956
957                 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
958                         term->camera.bControlSize = n;
959                         term->camera.bmControls = (__u8 *)term + sizeof *term;
960                         term->camera.wObjectiveFocalLengthMin =
961                                 get_unaligned_le16(&buffer[8]);
962                         term->camera.wObjectiveFocalLengthMax =
963                                 get_unaligned_le16(&buffer[10]);
964                         term->camera.wOcularFocalLength =
965                                 get_unaligned_le16(&buffer[12]);
966                         memcpy(term->camera.bmControls, &buffer[15], n);
967                 } else if (UVC_ENTITY_TYPE(term) ==
968                            UVC_ITT_MEDIA_TRANSPORT_INPUT) {
969                         term->media.bControlSize = n;
970                         term->media.bmControls = (__u8 *)term + sizeof *term;
971                         term->media.bTransportModeSize = p;
972                         term->media.bmTransportModes = (__u8 *)term
973                                                      + sizeof *term + n;
974                         memcpy(term->media.bmControls, &buffer[9], n);
975                         memcpy(term->media.bmTransportModes, &buffer[10+n], p);
976                 }
977
978                 if (buffer[7] != 0)
979                         usb_string(udev, buffer[7], term->name,
980                                    sizeof term->name);
981                 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
982                         sprintf(term->name, "Camera %u", buffer[3]);
983                 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
984                         sprintf(term->name, "Media %u", buffer[3]);
985                 else
986                         sprintf(term->name, "Input %u", buffer[3]);
987
988                 list_add_tail(&term->list, &dev->entities);
989                 break;
990
991         case UVC_VC_OUTPUT_TERMINAL:
992                 if (buflen < 9) {
993                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
994                                 "interface %d OUTPUT_TERMINAL error\n",
995                                 udev->devnum, alts->desc.bInterfaceNumber);
996                         return -EINVAL;
997                 }
998
999                 /* Make sure the terminal type MSB is not null, otherwise it
1000                  * could be confused with a unit.
1001                  */
1002                 type = get_unaligned_le16(&buffer[4]);
1003                 if ((type & 0xff00) == 0) {
1004                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1005                                 "interface %d OUTPUT_TERMINAL %d has invalid "
1006                                 "type 0x%04x, skipping\n", udev->devnum,
1007                                 alts->desc.bInterfaceNumber, buffer[3], type);
1008                         return 0;
1009                 }
1010
1011                 term = kzalloc(sizeof *term, GFP_KERNEL);
1012                 if (term == NULL)
1013                         return -ENOMEM;
1014
1015                 term->id = buffer[3];
1016                 term->type = type | UVC_TERM_OUTPUT;
1017                 term->output.bSourceID = buffer[7];
1018
1019                 if (buffer[8] != 0)
1020                         usb_string(udev, buffer[8], term->name,
1021                                    sizeof term->name);
1022                 else
1023                         sprintf(term->name, "Output %u", buffer[3]);
1024
1025                 list_add_tail(&term->list, &dev->entities);
1026                 break;
1027
1028         case UVC_VC_SELECTOR_UNIT:
1029                 p = buflen >= 5 ? buffer[4] : 0;
1030
1031                 if (buflen < 5 || buflen < 6 + p) {
1032                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1033                                 "interface %d SELECTOR_UNIT error\n",
1034                                 udev->devnum, alts->desc.bInterfaceNumber);
1035                         return -EINVAL;
1036                 }
1037
1038                 unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1039                 if (unit == NULL)
1040                         return -ENOMEM;
1041
1042                 unit->id = buffer[3];
1043                 unit->type = buffer[2];
1044                 unit->selector.bNrInPins = buffer[4];
1045                 unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1046                 memcpy(unit->selector.baSourceID, &buffer[5], p);
1047
1048                 if (buffer[5+p] != 0)
1049                         usb_string(udev, buffer[5+p], unit->name,
1050                                    sizeof unit->name);
1051                 else
1052                         sprintf(unit->name, "Selector %u", buffer[3]);
1053
1054                 list_add_tail(&unit->list, &dev->entities);
1055                 break;
1056
1057         case UVC_VC_PROCESSING_UNIT:
1058                 n = buflen >= 8 ? buffer[7] : 0;
1059                 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1060
1061                 if (buflen < p + n) {
1062                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1063                                 "interface %d PROCESSING_UNIT error\n",
1064                                 udev->devnum, alts->desc.bInterfaceNumber);
1065                         return -EINVAL;
1066                 }
1067
1068                 unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1069                 if (unit == NULL)
1070                         return -ENOMEM;
1071
1072                 unit->id = buffer[3];
1073                 unit->type = buffer[2];
1074                 unit->processing.bSourceID = buffer[4];
1075                 unit->processing.wMaxMultiplier =
1076                         get_unaligned_le16(&buffer[5]);
1077                 unit->processing.bControlSize = buffer[7];
1078                 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1079                 memcpy(unit->processing.bmControls, &buffer[8], n);
1080                 if (dev->uvc_version >= 0x0110)
1081                         unit->processing.bmVideoStandards = buffer[9+n];
1082
1083                 if (buffer[8+n] != 0)
1084                         usb_string(udev, buffer[8+n], unit->name,
1085                                    sizeof unit->name);
1086                 else
1087                         sprintf(unit->name, "Processing %u", buffer[3]);
1088
1089                 list_add_tail(&unit->list, &dev->entities);
1090                 break;
1091
1092         case UVC_VC_EXTENSION_UNIT:
1093                 p = buflen >= 22 ? buffer[21] : 0;
1094                 n = buflen >= 24 + p ? buffer[22+p] : 0;
1095
1096                 if (buflen < 24 + p + n) {
1097                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1098                                 "interface %d EXTENSION_UNIT error\n",
1099                                 udev->devnum, alts->desc.bInterfaceNumber);
1100                         return -EINVAL;
1101                 }
1102
1103                 unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1104                 if (unit == NULL)
1105                         return -ENOMEM;
1106
1107                 unit->id = buffer[3];
1108                 unit->type = buffer[2];
1109                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1110                 unit->extension.bNumControls = buffer[20];
1111                 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
1112                 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1113                 memcpy(unit->extension.baSourceID, &buffer[22], p);
1114                 unit->extension.bControlSize = buffer[22+p];
1115                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1116                 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1117
1118                 if (buffer[23+p+n] != 0)
1119                         usb_string(udev, buffer[23+p+n], unit->name,
1120                                    sizeof unit->name);
1121                 else
1122                         sprintf(unit->name, "Extension %u", buffer[3]);
1123
1124                 list_add_tail(&unit->list, &dev->entities);
1125                 break;
1126
1127         default:
1128                 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1129                         "descriptor (%u)\n", buffer[2]);
1130                 break;
1131         }
1132
1133         return 0;
1134 }
1135
1136 static int uvc_parse_control(struct uvc_device *dev)
1137 {
1138         struct usb_host_interface *alts = dev->intf->cur_altsetting;
1139         unsigned char *buffer = alts->extra;
1140         int buflen = alts->extralen;
1141         int ret;
1142
1143         /* Parse the default alternate setting only, as the UVC specification
1144          * defines a single alternate setting, the default alternate setting
1145          * zero.
1146          */
1147
1148         while (buflen > 2) {
1149                 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1150                     buffer[1] != USB_DT_CS_INTERFACE)
1151                         goto next_descriptor;
1152
1153                 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1154                         return ret;
1155
1156 next_descriptor:
1157                 buflen -= buffer[0];
1158                 buffer += buffer[0];
1159         }
1160
1161         /* Check if the optional status endpoint is present. Built-in iSight
1162          * webcams have an interrupt endpoint but spit proprietary data that
1163          * don't conform to the UVC status endpoint messages. Don't try to
1164          * handle the interrupt endpoint for those cameras.
1165          */
1166         if (alts->desc.bNumEndpoints == 1 &&
1167             !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1168                 struct usb_host_endpoint *ep = &alts->endpoint[0];
1169                 struct usb_endpoint_descriptor *desc = &ep->desc;
1170
1171                 if (usb_endpoint_is_int_in(desc) &&
1172                     le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1173                     desc->bInterval != 0) {
1174                         uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1175                                 "(addr %02x).\n", desc->bEndpointAddress);
1176                         dev->int_ep = ep;
1177                 }
1178         }
1179
1180         return 0;
1181 }
1182
1183 /* ------------------------------------------------------------------------
1184  * UVC device scan
1185  */
1186
1187 /*
1188  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1189  * and containing the following units:
1190  *
1191  * - one or more Output Terminals (USB Streaming or Display)
1192  * - zero or one Processing Unit
1193  * - zero, one or more single-input Selector Units
1194  * - zero or one multiple-input Selector Units, provided all inputs are
1195  *   connected to input terminals
1196  * - zero, one or mode single-input Extension Units
1197  * - one or more Input Terminals (Camera, External or USB Streaming)
1198  *
1199  * The terminal and units must match on of the following structures:
1200  *
1201  * ITT_*(0) -> +---------+    +---------+    +---------+ -> TT_STREAMING(0)
1202  * ...         | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} |    ...
1203  * ITT_*(n) -> +---------+    +---------+    +---------+ -> TT_STREAMING(n)
1204  *
1205  *                 +---------+    +---------+ -> OTT_*(0)
1206  * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} |    ...
1207  *                 +---------+    +---------+ -> OTT_*(n)
1208  *
1209  * The Processing Unit and Extension Units can be in any order. Additional
1210  * Extension Units connected to the main chain as single-unit branches are
1211  * also supported. Single-input Selector Units are ignored.
1212  */
1213 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1214         struct uvc_entity *entity)
1215 {
1216         switch (UVC_ENTITY_TYPE(entity)) {
1217         case UVC_VC_EXTENSION_UNIT:
1218                 if (uvc_trace_param & UVC_TRACE_PROBE)
1219                         printk(" <- XU %d", entity->id);
1220
1221                 if (entity->extension.bNrInPins != 1) {
1222                         uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1223                                 "than 1 input pin.\n", entity->id);
1224                         return -1;
1225                 }
1226
1227                 list_add_tail(&entity->chain, &chain->extensions);
1228                 break;
1229
1230         case UVC_VC_PROCESSING_UNIT:
1231                 if (uvc_trace_param & UVC_TRACE_PROBE)
1232                         printk(" <- PU %d", entity->id);
1233
1234                 if (chain->processing != NULL) {
1235                         uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1236                                 "Processing Units in chain.\n");
1237                         return -1;
1238                 }
1239
1240                 chain->processing = entity;
1241                 break;
1242
1243         case UVC_VC_SELECTOR_UNIT:
1244                 if (uvc_trace_param & UVC_TRACE_PROBE)
1245                         printk(" <- SU %d", entity->id);
1246
1247                 /* Single-input selector units are ignored. */
1248                 if (entity->selector.bNrInPins == 1)
1249                         break;
1250
1251                 if (chain->selector != NULL) {
1252                         uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1253                                 "Units in chain.\n");
1254                         return -1;
1255                 }
1256
1257                 chain->selector = entity;
1258                 break;
1259
1260         case UVC_ITT_VENDOR_SPECIFIC:
1261         case UVC_ITT_CAMERA:
1262         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1263                 if (uvc_trace_param & UVC_TRACE_PROBE)
1264                         printk(" <- IT %d\n", entity->id);
1265
1266                 list_add_tail(&entity->chain, &chain->iterms);
1267                 break;
1268
1269         case UVC_TT_STREAMING:
1270                 if (uvc_trace_param & UVC_TRACE_PROBE)
1271                         printk(" <- IT %d\n", entity->id);
1272
1273                 if (!UVC_ENTITY_IS_ITERM(entity)) {
1274                         uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1275                                 "terminal %u.\n", entity->id);
1276                         return -1;
1277                 }
1278
1279                 list_add_tail(&entity->chain, &chain->iterms);
1280                 break;
1281
1282         default:
1283                 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1284                         "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1285                 return -1;
1286         }
1287
1288         return 0;
1289 }
1290
1291 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1292         struct uvc_entity *entity, struct uvc_entity *prev)
1293 {
1294         struct uvc_entity *forward;
1295         int found;
1296
1297         /* Forward scan */
1298         forward = NULL;
1299         found = 0;
1300
1301         while (1) {
1302                 forward = uvc_entity_by_reference(chain->dev, entity->id,
1303                         forward);
1304                 if (forward == NULL)
1305                         break;
1306                 if (forward == prev)
1307                         continue;
1308
1309                 switch (UVC_ENTITY_TYPE(forward)) {
1310                 case UVC_VC_EXTENSION_UNIT:
1311                         if (forward->extension.bNrInPins != 1) {
1312                                 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1313                                           "has more than 1 input pin.\n",
1314                                           entity->id);
1315                                 return -EINVAL;
1316                         }
1317
1318                         list_add_tail(&forward->chain, &chain->extensions);
1319                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1320                                 if (!found)
1321                                         printk(" (->");
1322
1323                                 printk(" XU %d", forward->id);
1324                                 found = 1;
1325                         }
1326                         break;
1327
1328                 case UVC_OTT_VENDOR_SPECIFIC:
1329                 case UVC_OTT_DISPLAY:
1330                 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1331                 case UVC_TT_STREAMING:
1332                         if (UVC_ENTITY_IS_ITERM(forward)) {
1333                                 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1334                                         "terminal %u.\n", forward->id);
1335                                 return -EINVAL;
1336                         }
1337
1338                         list_add_tail(&forward->chain, &chain->oterms);
1339                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1340                                 if (!found)
1341                                         printk(" (->");
1342
1343                                 printk(" OT %d", forward->id);
1344                                 found = 1;
1345                         }
1346                         break;
1347                 }
1348         }
1349         if (found)
1350                 printk(")");
1351
1352         return 0;
1353 }
1354
1355 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1356         struct uvc_entity *entity)
1357 {
1358         struct uvc_entity *term;
1359         int id = -1, i;
1360
1361         switch (UVC_ENTITY_TYPE(entity)) {
1362         case UVC_VC_EXTENSION_UNIT:
1363                 id = entity->extension.baSourceID[0];
1364                 break;
1365
1366         case UVC_VC_PROCESSING_UNIT:
1367                 id = entity->processing.bSourceID;
1368                 break;
1369
1370         case UVC_VC_SELECTOR_UNIT:
1371                 /* Single-input selector units are ignored. */
1372                 if (entity->selector.bNrInPins == 1) {
1373                         id = entity->selector.baSourceID[0];
1374                         break;
1375                 }
1376
1377                 if (uvc_trace_param & UVC_TRACE_PROBE)
1378                         printk(" <- IT");
1379
1380                 chain->selector = entity;
1381                 for (i = 0; i < entity->selector.bNrInPins; ++i) {
1382                         id = entity->selector.baSourceID[i];
1383                         term = uvc_entity_by_id(chain->dev, id);
1384                         if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1385                                 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1386                                         "input %d isn't connected to an "
1387                                         "input terminal\n", entity->id, i);
1388                                 return -1;
1389                         }
1390
1391                         if (uvc_trace_param & UVC_TRACE_PROBE)
1392                                 printk(" %d", term->id);
1393
1394                         list_add_tail(&term->chain, &chain->iterms);
1395                         uvc_scan_chain_forward(chain, term, entity);
1396                 }
1397
1398                 if (uvc_trace_param & UVC_TRACE_PROBE)
1399                         printk("\n");
1400
1401                 id = 0;
1402                 break;
1403         }
1404
1405         return id;
1406 }
1407
1408 static int uvc_scan_chain(struct uvc_video_chain *chain,
1409                           struct uvc_entity *oterm)
1410 {
1411         struct uvc_entity *entity, *prev;
1412         int id;
1413
1414         entity = oterm;
1415         list_add_tail(&entity->chain, &chain->oterms);
1416         uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
1417
1418         id = entity->output.bSourceID;
1419         while (id != 0) {
1420                 prev = entity;
1421                 entity = uvc_entity_by_id(chain->dev, id);
1422                 if (entity == NULL) {
1423                         uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1424                                 "unknown entity %d.\n", id);
1425                         return -EINVAL;
1426                 }
1427
1428                 if (entity->chain.next || entity->chain.prev) {
1429                         uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1430                                 "entity %d already in chain.\n", id);
1431                         return -EINVAL;
1432                 }
1433
1434                 /* Process entity */
1435                 if (uvc_scan_chain_entity(chain, entity) < 0)
1436                         return -EINVAL;
1437
1438                 /* Forward scan */
1439                 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1440                         return -EINVAL;
1441
1442                 /* Stop when a terminal is found. */
1443                 if (UVC_ENTITY_IS_TERM(entity))
1444                         break;
1445
1446                 /* Backward scan */
1447                 id = uvc_scan_chain_backward(chain, entity);
1448                 if (id < 0)
1449                         return id;
1450         }
1451
1452         return 0;
1453 }
1454
1455 static unsigned int uvc_print_terms(struct list_head *terms, char *buffer)
1456 {
1457         struct uvc_entity *term;
1458         unsigned int nterms = 0;
1459         char *p = buffer;
1460
1461         list_for_each_entry(term, terms, chain) {
1462                 p += sprintf(p, "%u", term->id);
1463                 if (term->chain.next != terms) {
1464                         p += sprintf(p, ",");
1465                         if (++nterms >= 4) {
1466                                 p += sprintf(p, "...");
1467                                 break;
1468                         }
1469                 }
1470         }
1471
1472         return p - buffer;
1473 }
1474
1475 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1476 {
1477         static char buffer[43];
1478         char *p = buffer;
1479
1480         p += uvc_print_terms(&chain->iterms, p);
1481         p += sprintf(p, " -> ");
1482         uvc_print_terms(&chain->oterms, p);
1483
1484         return buffer;
1485 }
1486
1487 /*
1488  * Scan the device for video chains and register video devices.
1489  *
1490  * Chains are scanned starting at their output terminals and walked backwards.
1491  */
1492 static int uvc_scan_device(struct uvc_device *dev)
1493 {
1494         struct uvc_video_chain *chain;
1495         struct uvc_entity *term;
1496
1497         list_for_each_entry(term, &dev->entities, list) {
1498                 if (!UVC_ENTITY_IS_OTERM(term))
1499                         continue;
1500
1501                 /* If the terminal is already included in a chain, skip it.
1502                  * This can happen for chains that have multiple output
1503                  * terminals, where all output terminals beside the first one
1504                  * will be inserted in the chain in forward scans.
1505                  */
1506                 if (term->chain.next || term->chain.prev)
1507                         continue;
1508
1509                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1510                 if (chain == NULL)
1511                         return -ENOMEM;
1512
1513                 INIT_LIST_HEAD(&chain->iterms);
1514                 INIT_LIST_HEAD(&chain->oterms);
1515                 INIT_LIST_HEAD(&chain->extensions);
1516                 mutex_init(&chain->ctrl_mutex);
1517                 chain->dev = dev;
1518
1519                 if (uvc_scan_chain(chain, term) < 0) {
1520                         kfree(chain);
1521                         continue;
1522                 }
1523
1524                 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1525                           uvc_print_chain(chain));
1526
1527                 list_add_tail(&chain->list, &dev->chains);
1528         }
1529
1530         if (list_empty(&dev->chains)) {
1531                 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1532                 return -1;
1533         }
1534
1535         return 0;
1536 }
1537
1538 /* ------------------------------------------------------------------------
1539  * Video device registration and unregistration
1540  */
1541
1542 /*
1543  * Delete the UVC device.
1544  *
1545  * Called by the kernel when the last reference to the uvc_device structure
1546  * is released.
1547  *
1548  * As this function is called after or during disconnect(), all URBs have
1549  * already been canceled by the USB core. There is no need to kill the
1550  * interrupt URB manually.
1551  */
1552 static void uvc_delete(struct uvc_device *dev)
1553 {
1554         struct list_head *p, *n;
1555
1556         usb_put_intf(dev->intf);
1557         usb_put_dev(dev->udev);
1558
1559         uvc_status_cleanup(dev);
1560         uvc_ctrl_cleanup_device(dev);
1561
1562         list_for_each_safe(p, n, &dev->chains) {
1563                 struct uvc_video_chain *chain;
1564                 chain = list_entry(p, struct uvc_video_chain, list);
1565                 kfree(chain);
1566         }
1567
1568         list_for_each_safe(p, n, &dev->entities) {
1569                 struct uvc_entity *entity;
1570                 entity = list_entry(p, struct uvc_entity, list);
1571                 kfree(entity);
1572         }
1573
1574         list_for_each_safe(p, n, &dev->streams) {
1575                 struct uvc_streaming *streaming;
1576                 streaming = list_entry(p, struct uvc_streaming, list);
1577                 usb_driver_release_interface(&uvc_driver.driver,
1578                         streaming->intf);
1579                 usb_put_intf(streaming->intf);
1580                 kfree(streaming->format);
1581                 kfree(streaming->header.bmaControls);
1582                 kfree(streaming);
1583         }
1584
1585         kfree(dev);
1586 }
1587
1588 static void uvc_release(struct video_device *vdev)
1589 {
1590         struct uvc_streaming *stream = video_get_drvdata(vdev);
1591         struct uvc_device *dev = stream->dev;
1592
1593         video_device_release(vdev);
1594
1595         /* Decrement the registered streams count and delete the device when it
1596          * reaches zero.
1597          */
1598         if (atomic_dec_and_test(&dev->nstreams))
1599                 uvc_delete(dev);
1600 }
1601
1602 /*
1603  * Unregister the video devices.
1604  */
1605 static void uvc_unregister_video(struct uvc_device *dev)
1606 {
1607         struct uvc_streaming *stream;
1608
1609         /* Unregistering all video devices might result in uvc_delete() being
1610          * called from inside the loop if there's no open file handle. To avoid
1611          * that, increment the stream count before iterating over the streams
1612          * and decrement it when done.
1613          */
1614         atomic_inc(&dev->nstreams);
1615
1616         list_for_each_entry(stream, &dev->streams, list) {
1617                 if (stream->vdev == NULL)
1618                         continue;
1619
1620                 video_unregister_device(stream->vdev);
1621                 stream->vdev = NULL;
1622         }
1623
1624         /* Decrement the stream count and call uvc_delete explicitly if there
1625          * are no stream left.
1626          */
1627         if (atomic_dec_and_test(&dev->nstreams))
1628                 uvc_delete(dev);
1629 }
1630
1631 static int uvc_register_video(struct uvc_device *dev,
1632                 struct uvc_streaming *stream)
1633 {
1634         struct video_device *vdev;
1635         int ret;
1636
1637         /* Initialize the streaming interface with default streaming
1638          * parameters.
1639          */
1640         ret = uvc_video_init(stream);
1641         if (ret < 0) {
1642                 uvc_printk(KERN_ERR, "Failed to initialize the device "
1643                         "(%d).\n", ret);
1644                 return ret;
1645         }
1646
1647         /* Register the device with V4L. */
1648         vdev = video_device_alloc();
1649         if (vdev == NULL) {
1650                 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1651                            ret);
1652                 return -ENOMEM;
1653         }
1654
1655         /* We already hold a reference to dev->udev. The video device will be
1656          * unregistered before the reference is released, so we don't need to
1657          * get another one.
1658          */
1659         vdev->parent = &dev->intf->dev;
1660         vdev->minor = -1;
1661         vdev->fops = &uvc_fops;
1662         vdev->release = uvc_release;
1663         strlcpy(vdev->name, dev->name, sizeof vdev->name);
1664
1665         /* Set the driver data before calling video_register_device, otherwise
1666          * uvc_v4l2_open might race us.
1667          */
1668         stream->vdev = vdev;
1669         video_set_drvdata(vdev, stream);
1670
1671         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1672         if (ret < 0) {
1673                 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1674                            ret);
1675                 stream->vdev = NULL;
1676                 video_device_release(vdev);
1677                 return ret;
1678         }
1679
1680         atomic_inc(&dev->nstreams);
1681         return 0;
1682 }
1683
1684 /*
1685  * Register all video devices in all chains.
1686  */
1687 static int uvc_register_terms(struct uvc_device *dev,
1688         struct uvc_video_chain *chain, struct list_head *terms)
1689 {
1690         struct uvc_streaming *stream;
1691         struct uvc_entity *term;
1692         int ret;
1693
1694         list_for_each_entry(term, terms, chain) {
1695                 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1696                         continue;
1697
1698                 stream = uvc_stream_by_id(dev, term->id);
1699                 if (stream == NULL) {
1700                         uvc_printk(KERN_INFO, "No streaming interface found "
1701                                    "for terminal %u.", term->id);
1702                         continue;
1703                 }
1704
1705                 stream->chain = chain;
1706                 ret = uvc_register_video(dev, stream);
1707                 if (ret < 0)
1708                         return ret;
1709         }
1710
1711         return 0;
1712 }
1713
1714 static int uvc_register_chains(struct uvc_device *dev)
1715 {
1716         struct uvc_video_chain *chain;
1717         int ret;
1718
1719         list_for_each_entry(chain, &dev->chains, list) {
1720                 ret = uvc_register_terms(dev, chain, &chain->iterms);
1721                 if (ret < 0)
1722                         return ret;
1723
1724                 ret = uvc_register_terms(dev, chain, &chain->oterms);
1725                 if (ret < 0)
1726                         return ret;
1727         }
1728
1729         return 0;
1730 }
1731
1732 /* ------------------------------------------------------------------------
1733  * USB probe, disconnect, suspend and resume
1734  */
1735
1736 static int uvc_probe(struct usb_interface *intf,
1737                      const struct usb_device_id *id)
1738 {
1739         struct usb_device *udev = interface_to_usbdev(intf);
1740         struct uvc_device *dev;
1741         int ret;
1742
1743         if (id->idVendor && id->idProduct)
1744                 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1745                                 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1746                                 id->idProduct);
1747         else
1748                 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1749                                 udev->devpath);
1750
1751         /* Allocate memory for the device and initialize it. */
1752         if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1753                 return -ENOMEM;
1754
1755         INIT_LIST_HEAD(&dev->entities);
1756         INIT_LIST_HEAD(&dev->chains);
1757         INIT_LIST_HEAD(&dev->streams);
1758         atomic_set(&dev->nstreams, 0);
1759         atomic_set(&dev->users, 0);
1760
1761         dev->udev = usb_get_dev(udev);
1762         dev->intf = usb_get_intf(intf);
1763         dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1764         dev->quirks = id->driver_info | uvc_quirks_param;
1765
1766         if (udev->product != NULL)
1767                 strlcpy(dev->name, udev->product, sizeof dev->name);
1768         else
1769                 snprintf(dev->name, sizeof dev->name,
1770                         "UVC Camera (%04x:%04x)",
1771                         le16_to_cpu(udev->descriptor.idVendor),
1772                         le16_to_cpu(udev->descriptor.idProduct));
1773
1774         /* Parse the Video Class control descriptor. */
1775         if (uvc_parse_control(dev) < 0) {
1776                 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1777                         "descriptors.\n");
1778                 goto error;
1779         }
1780
1781         uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1782                 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1783                 udev->product ? udev->product : "<unnamed>",
1784                 le16_to_cpu(udev->descriptor.idVendor),
1785                 le16_to_cpu(udev->descriptor.idProduct));
1786
1787         if (uvc_quirks_param != 0) {
1788                 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1789                         "parameter for testing purpose.\n", uvc_quirks_param);
1790                 uvc_printk(KERN_INFO, "Please report required quirks to the "
1791                         "linux-uvc-devel mailing list.\n");
1792         }
1793
1794         /* Initialize controls. */
1795         if (uvc_ctrl_init_device(dev) < 0)
1796                 goto error;
1797
1798         /* Scan the device for video chains. */
1799         if (uvc_scan_device(dev) < 0)
1800                 goto error;
1801
1802         /* Register video devices. */
1803         if (uvc_register_chains(dev) < 0)
1804                 goto error;
1805
1806         /* Save our data pointer in the interface data. */
1807         usb_set_intfdata(intf, dev);
1808
1809         /* Initialize the interrupt URB. */
1810         if ((ret = uvc_status_init(dev)) < 0) {
1811                 uvc_printk(KERN_INFO, "Unable to initialize the status "
1812                         "endpoint (%d), status interrupt will not be "
1813                         "supported.\n", ret);
1814         }
1815
1816         uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1817         return 0;
1818
1819 error:
1820         uvc_unregister_video(dev);
1821         return -ENODEV;
1822 }
1823
1824 static void uvc_disconnect(struct usb_interface *intf)
1825 {
1826         struct uvc_device *dev = usb_get_intfdata(intf);
1827
1828         /* Set the USB interface data to NULL. This can be done outside the
1829          * lock, as there's no other reader.
1830          */
1831         usb_set_intfdata(intf, NULL);
1832
1833         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1834             UVC_SC_VIDEOSTREAMING)
1835                 return;
1836
1837         dev->state |= UVC_DEV_DISCONNECTED;
1838
1839         uvc_unregister_video(dev);
1840 }
1841
1842 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1843 {
1844         struct uvc_device *dev = usb_get_intfdata(intf);
1845         struct uvc_streaming *stream;
1846
1847         uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1848                 intf->cur_altsetting->desc.bInterfaceNumber);
1849
1850         /* Controls are cached on the fly so they don't need to be saved. */
1851         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1852             UVC_SC_VIDEOCONTROL)
1853                 return uvc_status_suspend(dev);
1854
1855         list_for_each_entry(stream, &dev->streams, list) {
1856                 if (stream->intf == intf)
1857                         return uvc_video_suspend(stream);
1858         }
1859
1860         uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1861                         "mismatch.\n");
1862         return -EINVAL;
1863 }
1864
1865 static int __uvc_resume(struct usb_interface *intf, int reset)
1866 {
1867         struct uvc_device *dev = usb_get_intfdata(intf);
1868         struct uvc_streaming *stream;
1869
1870         uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1871                 intf->cur_altsetting->desc.bInterfaceNumber);
1872
1873         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1874             UVC_SC_VIDEOCONTROL) {
1875                 if (reset) {
1876                         int ret = uvc_ctrl_resume_device(dev);
1877
1878                         if (ret < 0)
1879                                 return ret;
1880                 }
1881
1882                 return uvc_status_resume(dev);
1883         }
1884
1885         list_for_each_entry(stream, &dev->streams, list) {
1886                 if (stream->intf == intf)
1887                         return uvc_video_resume(stream);
1888         }
1889
1890         uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1891                         "mismatch.\n");
1892         return -EINVAL;
1893 }
1894
1895 static int uvc_resume(struct usb_interface *intf)
1896 {
1897         return __uvc_resume(intf, 0);
1898 }
1899
1900 static int uvc_reset_resume(struct usb_interface *intf)
1901 {
1902         return __uvc_resume(intf, 1);
1903 }
1904
1905 /* ------------------------------------------------------------------------
1906  * Driver initialization and cleanup
1907  */
1908
1909 /*
1910  * The Logitech cameras listed below have their interface class set to
1911  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1912  * though they are compliant.
1913  */
1914 static struct usb_device_id uvc_ids[] = {
1915         /* Microsoft Lifecam NX-6000 */
1916         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1917                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1918           .idVendor             = 0x045e,
1919           .idProduct            = 0x00f8,
1920           .bInterfaceClass      = USB_CLASS_VIDEO,
1921           .bInterfaceSubClass   = 1,
1922           .bInterfaceProtocol   = 0,
1923           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1924         /* Microsoft Lifecam VX-7000 */
1925         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1926                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1927           .idVendor             = 0x045e,
1928           .idProduct            = 0x0723,
1929           .bInterfaceClass      = USB_CLASS_VIDEO,
1930           .bInterfaceSubClass   = 1,
1931           .bInterfaceProtocol   = 0,
1932           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1933         /* Logitech Quickcam Fusion */
1934         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1935                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1936           .idVendor             = 0x046d,
1937           .idProduct            = 0x08c1,
1938           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1939           .bInterfaceSubClass   = 1,
1940           .bInterfaceProtocol   = 0 },
1941         /* Logitech Quickcam Orbit MP */
1942         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1943                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1944           .idVendor             = 0x046d,
1945           .idProduct            = 0x08c2,
1946           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1947           .bInterfaceSubClass   = 1,
1948           .bInterfaceProtocol   = 0 },
1949         /* Logitech Quickcam Pro for Notebook */
1950         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1951                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1952           .idVendor             = 0x046d,
1953           .idProduct            = 0x08c3,
1954           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1955           .bInterfaceSubClass   = 1,
1956           .bInterfaceProtocol   = 0 },
1957         /* Logitech Quickcam Pro 5000 */
1958         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1959                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1960           .idVendor             = 0x046d,
1961           .idProduct            = 0x08c5,
1962           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1963           .bInterfaceSubClass   = 1,
1964           .bInterfaceProtocol   = 0 },
1965         /* Logitech Quickcam OEM Dell Notebook */
1966         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1967                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1968           .idVendor             = 0x046d,
1969           .idProduct            = 0x08c6,
1970           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1971           .bInterfaceSubClass   = 1,
1972           .bInterfaceProtocol   = 0 },
1973         /* Logitech Quickcam OEM Cisco VT Camera II */
1974         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1975                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1976           .idVendor             = 0x046d,
1977           .idProduct            = 0x08c7,
1978           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1979           .bInterfaceSubClass   = 1,
1980           .bInterfaceProtocol   = 0 },
1981         /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1982         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1983                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1984           .idVendor             = 0x058f,
1985           .idProduct            = 0x3820,
1986           .bInterfaceClass      = USB_CLASS_VIDEO,
1987           .bInterfaceSubClass   = 1,
1988           .bInterfaceProtocol   = 0,
1989           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1990         /* Apple Built-In iSight */
1991         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1992                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1993           .idVendor             = 0x05ac,
1994           .idProduct            = 0x8501,
1995           .bInterfaceClass      = USB_CLASS_VIDEO,
1996           .bInterfaceSubClass   = 1,
1997           .bInterfaceProtocol   = 0,
1998           .driver_info          = UVC_QUIRK_PROBE_MINMAX
1999                                 | UVC_QUIRK_BUILTIN_ISIGHT },
2000         /* Genesys Logic USB 2.0 PC Camera */
2001         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2002                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2003           .idVendor             = 0x05e3,
2004           .idProduct            = 0x0505,
2005           .bInterfaceClass      = USB_CLASS_VIDEO,
2006           .bInterfaceSubClass   = 1,
2007           .bInterfaceProtocol   = 0,
2008           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2009         /* ViMicro Vega */
2010         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2011                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2012           .idVendor             = 0x0ac8,
2013           .idProduct            = 0x332d,
2014           .bInterfaceClass      = USB_CLASS_VIDEO,
2015           .bInterfaceSubClass   = 1,
2016           .bInterfaceProtocol   = 0,
2017           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2018         /* ViMicro - Minoru3D */
2019         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2020                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2021           .idVendor             = 0x0ac8,
2022           .idProduct            = 0x3410,
2023           .bInterfaceClass      = USB_CLASS_VIDEO,
2024           .bInterfaceSubClass   = 1,
2025           .bInterfaceProtocol   = 0,
2026           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2027         /* ViMicro Venus - Minoru3D */
2028         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2029                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2030           .idVendor             = 0x0ac8,
2031           .idProduct            = 0x3420,
2032           .bInterfaceClass      = USB_CLASS_VIDEO,
2033           .bInterfaceSubClass   = 1,
2034           .bInterfaceProtocol   = 0,
2035           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2036         /* MT6227 */
2037         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2038                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2039           .idVendor             = 0x0e8d,
2040           .idProduct            = 0x0004,
2041           .bInterfaceClass      = USB_CLASS_VIDEO,
2042           .bInterfaceSubClass   = 1,
2043           .bInterfaceProtocol   = 0,
2044           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2045                                 | UVC_QUIRK_PROBE_DEF },
2046         /* Syntek (HP Spartan) */
2047         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2048                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2049           .idVendor             = 0x174f,
2050           .idProduct            = 0x5212,
2051           .bInterfaceClass      = USB_CLASS_VIDEO,
2052           .bInterfaceSubClass   = 1,
2053           .bInterfaceProtocol   = 0,
2054           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2055         /* Syntek (Samsung Q310) */
2056         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2057                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2058           .idVendor             = 0x174f,
2059           .idProduct            = 0x5931,
2060           .bInterfaceClass      = USB_CLASS_VIDEO,
2061           .bInterfaceSubClass   = 1,
2062           .bInterfaceProtocol   = 0,
2063           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2064         /* Syntek (Asus F9SG) */
2065         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2066                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2067           .idVendor             = 0x174f,
2068           .idProduct            = 0x8a31,
2069           .bInterfaceClass      = USB_CLASS_VIDEO,
2070           .bInterfaceSubClass   = 1,
2071           .bInterfaceProtocol   = 0,
2072           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2073         /* Syntek (Asus U3S) */
2074         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2075                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2076           .idVendor             = 0x174f,
2077           .idProduct            = 0x8a33,
2078           .bInterfaceClass      = USB_CLASS_VIDEO,
2079           .bInterfaceSubClass   = 1,
2080           .bInterfaceProtocol   = 0,
2081           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2082         /* Syntek (JAOtech Smart Terminal) */
2083         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2084                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2085           .idVendor             = 0x174f,
2086           .idProduct            = 0x8a34,
2087           .bInterfaceClass      = USB_CLASS_VIDEO,
2088           .bInterfaceSubClass   = 1,
2089           .bInterfaceProtocol   = 0,
2090           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2091         /* Lenovo Thinkpad SL400/SL500 */
2092         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2093                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2094           .idVendor             = 0x17ef,
2095           .idProduct            = 0x480b,
2096           .bInterfaceClass      = USB_CLASS_VIDEO,
2097           .bInterfaceSubClass   = 1,
2098           .bInterfaceProtocol   = 0,
2099           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2100         /* Aveo Technology USB 2.0 Camera */
2101         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2102                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2103           .idVendor             = 0x1871,
2104           .idProduct            = 0x0306,
2105           .bInterfaceClass      = USB_CLASS_VIDEO,
2106           .bInterfaceSubClass   = 1,
2107           .bInterfaceProtocol   = 0,
2108           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2109                                 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2110         /* Ecamm Pico iMage */
2111         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2112                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2113           .idVendor             = 0x18cd,
2114           .idProduct            = 0xcafe,
2115           .bInterfaceClass      = USB_CLASS_VIDEO,
2116           .bInterfaceSubClass   = 1,
2117           .bInterfaceProtocol   = 0,
2118           .driver_info          = UVC_QUIRK_PROBE_EXTRAFIELDS },
2119         /* FSC WebCam V30S */
2120         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2121                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2122           .idVendor             = 0x18ec,
2123           .idProduct            = 0x3288,
2124           .bInterfaceClass      = USB_CLASS_VIDEO,
2125           .bInterfaceSubClass   = 1,
2126           .bInterfaceProtocol   = 0,
2127           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2128         /* Bodelin ProScopeHR */
2129         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2130                                 | USB_DEVICE_ID_MATCH_DEV_HI
2131                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2132           .idVendor             = 0x19ab,
2133           .idProduct            = 0x1000,
2134           .bcdDevice_hi         = 0x0126,
2135           .bInterfaceClass      = USB_CLASS_VIDEO,
2136           .bInterfaceSubClass   = 1,
2137           .bInterfaceProtocol   = 0,
2138           .driver_info          = UVC_QUIRK_STATUS_INTERVAL },
2139         /* MSI StarCam 370i */
2140         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2141                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2142           .idVendor             = 0x1b3b,
2143           .idProduct            = 0x2951,
2144           .bInterfaceClass      = USB_CLASS_VIDEO,
2145           .bInterfaceSubClass   = 1,
2146           .bInterfaceProtocol   = 0,
2147           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2148         /* SiGma Micro USB Web Camera */
2149         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2150                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2151           .idVendor             = 0x1c4f,
2152           .idProduct            = 0x3000,
2153           .bInterfaceClass      = USB_CLASS_VIDEO,
2154           .bInterfaceSubClass   = 1,
2155           .bInterfaceProtocol   = 0,
2156           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2157                                 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2158         /* Generic USB Video Class */
2159         { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2160         {}
2161 };
2162
2163 MODULE_DEVICE_TABLE(usb, uvc_ids);
2164
2165 struct uvc_driver uvc_driver = {
2166         .driver = {
2167                 .name           = "uvcvideo",
2168                 .probe          = uvc_probe,
2169                 .disconnect     = uvc_disconnect,
2170                 .suspend        = uvc_suspend,
2171                 .resume         = uvc_resume,
2172                 .reset_resume   = uvc_reset_resume,
2173                 .id_table       = uvc_ids,
2174                 .supports_autosuspend = 1,
2175         },
2176 };
2177
2178 static int __init uvc_init(void)
2179 {
2180         int result;
2181
2182         INIT_LIST_HEAD(&uvc_driver.devices);
2183         INIT_LIST_HEAD(&uvc_driver.controls);
2184         mutex_init(&uvc_driver.ctrl_mutex);
2185
2186         uvc_ctrl_init();
2187
2188         result = usb_register(&uvc_driver.driver);
2189         if (result == 0)
2190                 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2191         return result;
2192 }
2193
2194 static void __exit uvc_cleanup(void)
2195 {
2196         usb_deregister(&uvc_driver.driver);
2197 }
2198
2199 module_init(uvc_init);
2200 module_exit(uvc_cleanup);
2201
2202 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2203 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2204 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2205 MODULE_PARM_DESC(quirks, "Forced device quirks");
2206 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2207 MODULE_PARM_DESC(trace, "Trace level bitmask");
2208 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2209 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2210
2211 MODULE_AUTHOR(DRIVER_AUTHOR);
2212 MODULE_DESCRIPTION(DRIVER_DESC);
2213 MODULE_LICENSE("GPL");
2214 MODULE_VERSION(DRIVER_VERSION);
2215