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