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