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