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