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