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