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