xref: /openbmc/linux/drivers/media/usb/uvc/uvc_driver.c (revision 8ee90c5c)
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 kref *kref)
1806 {
1807 	struct uvc_device *dev = container_of(kref, struct uvc_device, ref);
1808 	struct list_head *p, *n;
1809 
1810 	uvc_status_cleanup(dev);
1811 	uvc_ctrl_cleanup_device(dev);
1812 
1813 	usb_put_intf(dev->intf);
1814 	usb_put_dev(dev->udev);
1815 
1816 	if (dev->vdev.dev)
1817 		v4l2_device_unregister(&dev->vdev);
1818 #ifdef CONFIG_MEDIA_CONTROLLER
1819 	if (media_devnode_is_registered(dev->mdev.devnode))
1820 		media_device_unregister(&dev->mdev);
1821 	media_device_cleanup(&dev->mdev);
1822 #endif
1823 
1824 	list_for_each_safe(p, n, &dev->chains) {
1825 		struct uvc_video_chain *chain;
1826 		chain = list_entry(p, struct uvc_video_chain, list);
1827 		kfree(chain);
1828 	}
1829 
1830 	list_for_each_safe(p, n, &dev->entities) {
1831 		struct uvc_entity *entity;
1832 		entity = list_entry(p, struct uvc_entity, list);
1833 #ifdef CONFIG_MEDIA_CONTROLLER
1834 		uvc_mc_cleanup_entity(entity);
1835 #endif
1836 		kfree(entity);
1837 	}
1838 
1839 	list_for_each_safe(p, n, &dev->streams) {
1840 		struct uvc_streaming *streaming;
1841 		streaming = list_entry(p, struct uvc_streaming, list);
1842 		usb_driver_release_interface(&uvc_driver.driver,
1843 			streaming->intf);
1844 		usb_put_intf(streaming->intf);
1845 		kfree(streaming->format);
1846 		kfree(streaming->header.bmaControls);
1847 		kfree(streaming);
1848 	}
1849 
1850 	kfree(dev);
1851 }
1852 
1853 static void uvc_release(struct video_device *vdev)
1854 {
1855 	struct uvc_streaming *stream = video_get_drvdata(vdev);
1856 	struct uvc_device *dev = stream->dev;
1857 
1858 	kref_put(&dev->ref, uvc_delete);
1859 }
1860 
1861 /*
1862  * Unregister the video devices.
1863  */
1864 static void uvc_unregister_video(struct uvc_device *dev)
1865 {
1866 	struct uvc_streaming *stream;
1867 
1868 	/* Unregistering all video devices might result in uvc_delete() being
1869 	 * called from inside the loop if there's no open file handle. To avoid
1870 	 * that, increment the refcount before iterating over the streams and
1871 	 * decrement it when done.
1872 	 */
1873 	kref_get(&dev->ref);
1874 
1875 	list_for_each_entry(stream, &dev->streams, list) {
1876 		if (!video_is_registered(&stream->vdev))
1877 			continue;
1878 
1879 		video_unregister_device(&stream->vdev);
1880 
1881 		uvc_debugfs_cleanup_stream(stream);
1882 	}
1883 
1884 	kref_put(&dev->ref, uvc_delete);
1885 }
1886 
1887 static int uvc_register_video(struct uvc_device *dev,
1888 		struct uvc_streaming *stream)
1889 {
1890 	struct video_device *vdev = &stream->vdev;
1891 	int ret;
1892 
1893 	/* Initialize the video buffers queue. */
1894 	ret = uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
1895 	if (ret)
1896 		return ret;
1897 
1898 	/* Initialize the streaming interface with default streaming
1899 	 * parameters.
1900 	 */
1901 	ret = uvc_video_init(stream);
1902 	if (ret < 0) {
1903 		uvc_printk(KERN_ERR, "Failed to initialize the device "
1904 			"(%d).\n", ret);
1905 		return ret;
1906 	}
1907 
1908 	uvc_debugfs_init_stream(stream);
1909 
1910 	/* Register the device with V4L. */
1911 
1912 	/* We already hold a reference to dev->udev. The video device will be
1913 	 * unregistered before the reference is released, so we don't need to
1914 	 * get another one.
1915 	 */
1916 	vdev->v4l2_dev = &dev->vdev;
1917 	vdev->fops = &uvc_fops;
1918 	vdev->ioctl_ops = &uvc_ioctl_ops;
1919 	vdev->release = uvc_release;
1920 	vdev->prio = &stream->chain->prio;
1921 	if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1922 		vdev->vfl_dir = VFL_DIR_TX;
1923 	strlcpy(vdev->name, dev->name, sizeof vdev->name);
1924 
1925 	/* Set the driver data before calling video_register_device, otherwise
1926 	 * uvc_v4l2_open might race us.
1927 	 */
1928 	video_set_drvdata(vdev, stream);
1929 
1930 	ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1931 	if (ret < 0) {
1932 		uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1933 			   ret);
1934 		return ret;
1935 	}
1936 
1937 	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1938 		stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE;
1939 	else
1940 		stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT;
1941 
1942 	kref_get(&dev->ref);
1943 	return 0;
1944 }
1945 
1946 /*
1947  * Register all video devices in all chains.
1948  */
1949 static int uvc_register_terms(struct uvc_device *dev,
1950 	struct uvc_video_chain *chain)
1951 {
1952 	struct uvc_streaming *stream;
1953 	struct uvc_entity *term;
1954 	int ret;
1955 
1956 	list_for_each_entry(term, &chain->entities, chain) {
1957 		if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1958 			continue;
1959 
1960 		stream = uvc_stream_by_id(dev, term->id);
1961 		if (stream == NULL) {
1962 			uvc_printk(KERN_INFO, "No streaming interface found "
1963 				   "for terminal %u.", term->id);
1964 			continue;
1965 		}
1966 
1967 		stream->chain = chain;
1968 		ret = uvc_register_video(dev, stream);
1969 		if (ret < 0)
1970 			return ret;
1971 
1972 		term->vdev = &stream->vdev;
1973 	}
1974 
1975 	return 0;
1976 }
1977 
1978 static int uvc_register_chains(struct uvc_device *dev)
1979 {
1980 	struct uvc_video_chain *chain;
1981 	int ret;
1982 
1983 	list_for_each_entry(chain, &dev->chains, list) {
1984 		ret = uvc_register_terms(dev, chain);
1985 		if (ret < 0)
1986 			return ret;
1987 
1988 #ifdef CONFIG_MEDIA_CONTROLLER
1989 		ret = uvc_mc_register_entities(chain);
1990 		if (ret < 0) {
1991 			uvc_printk(KERN_INFO, "Failed to register entites "
1992 				"(%d).\n", ret);
1993 		}
1994 #endif
1995 	}
1996 
1997 	return 0;
1998 }
1999 
2000 /* ------------------------------------------------------------------------
2001  * USB probe, disconnect, suspend and resume
2002  */
2003 
2004 static int uvc_probe(struct usb_interface *intf,
2005 		     const struct usb_device_id *id)
2006 {
2007 	struct usb_device *udev = interface_to_usbdev(intf);
2008 	struct uvc_device *dev;
2009 	int function;
2010 	int ret;
2011 
2012 	if (id->idVendor && id->idProduct)
2013 		uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
2014 				"(%04x:%04x)\n", udev->devpath, id->idVendor,
2015 				id->idProduct);
2016 	else
2017 		uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
2018 				udev->devpath);
2019 
2020 	/* Allocate memory for the device and initialize it. */
2021 	if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
2022 		return -ENOMEM;
2023 
2024 	INIT_LIST_HEAD(&dev->entities);
2025 	INIT_LIST_HEAD(&dev->chains);
2026 	INIT_LIST_HEAD(&dev->streams);
2027 	kref_init(&dev->ref);
2028 	atomic_set(&dev->nmappings, 0);
2029 	mutex_init(&dev->lock);
2030 
2031 	dev->udev = usb_get_dev(udev);
2032 	dev->intf = usb_get_intf(intf);
2033 	dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
2034 	dev->quirks = (uvc_quirks_param == -1)
2035 		    ? id->driver_info : uvc_quirks_param;
2036 
2037 	if (udev->product != NULL)
2038 		strlcpy(dev->name, udev->product, sizeof dev->name);
2039 	else
2040 		snprintf(dev->name, sizeof dev->name,
2041 			 "UVC Camera (%04x:%04x)",
2042 			 le16_to_cpu(udev->descriptor.idVendor),
2043 			 le16_to_cpu(udev->descriptor.idProduct));
2044 
2045 	/*
2046 	 * Add iFunction or iInterface to names when available as additional
2047 	 * distinguishers between interfaces. iFunction is prioritized over
2048 	 * iInterface which matches Windows behavior at the point of writing.
2049 	 */
2050 	if (intf->intf_assoc && intf->intf_assoc->iFunction != 0)
2051 		function = intf->intf_assoc->iFunction;
2052 	else
2053 		function = intf->cur_altsetting->desc.iInterface;
2054 	if (function != 0) {
2055 		size_t len;
2056 
2057 		strlcat(dev->name, ": ", sizeof(dev->name));
2058 		len = strlen(dev->name);
2059 		usb_string(udev, function, dev->name + len,
2060 			   sizeof(dev->name) - len);
2061 	}
2062 
2063 	/* Parse the Video Class control descriptor. */
2064 	if (uvc_parse_control(dev) < 0) {
2065 		uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
2066 			"descriptors.\n");
2067 		goto error;
2068 	}
2069 
2070 	uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
2071 		dev->uvc_version >> 8, dev->uvc_version & 0xff,
2072 		udev->product ? udev->product : "<unnamed>",
2073 		le16_to_cpu(udev->descriptor.idVendor),
2074 		le16_to_cpu(udev->descriptor.idProduct));
2075 
2076 	if (dev->quirks != id->driver_info) {
2077 		uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
2078 			"parameter for testing purpose.\n", dev->quirks);
2079 		uvc_printk(KERN_INFO, "Please report required quirks to the "
2080 			"linux-uvc-devel mailing list.\n");
2081 	}
2082 
2083 	/* Initialize the media device and register the V4L2 device. */
2084 #ifdef CONFIG_MEDIA_CONTROLLER
2085 	dev->mdev.dev = &intf->dev;
2086 	strlcpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
2087 	if (udev->serial)
2088 		strlcpy(dev->mdev.serial, udev->serial,
2089 			sizeof(dev->mdev.serial));
2090 	strcpy(dev->mdev.bus_info, udev->devpath);
2091 	dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
2092 	media_device_init(&dev->mdev);
2093 
2094 	dev->vdev.mdev = &dev->mdev;
2095 #endif
2096 	if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
2097 		goto error;
2098 
2099 	/* Initialize controls. */
2100 	if (uvc_ctrl_init_device(dev) < 0)
2101 		goto error;
2102 
2103 	/* Scan the device for video chains. */
2104 	if (uvc_scan_device(dev) < 0)
2105 		goto error;
2106 
2107 	/* Register video device nodes. */
2108 	if (uvc_register_chains(dev) < 0)
2109 		goto error;
2110 
2111 #ifdef CONFIG_MEDIA_CONTROLLER
2112 	/* Register the media device node */
2113 	if (media_device_register(&dev->mdev) < 0)
2114 		goto error;
2115 #endif
2116 	/* Save our data pointer in the interface data. */
2117 	usb_set_intfdata(intf, dev);
2118 
2119 	/* Initialize the interrupt URB. */
2120 	if ((ret = uvc_status_init(dev)) < 0) {
2121 		uvc_printk(KERN_INFO, "Unable to initialize the status "
2122 			"endpoint (%d), status interrupt will not be "
2123 			"supported.\n", ret);
2124 	}
2125 
2126 	uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
2127 	usb_enable_autosuspend(udev);
2128 	return 0;
2129 
2130 error:
2131 	uvc_unregister_video(dev);
2132 	return -ENODEV;
2133 }
2134 
2135 static void uvc_disconnect(struct usb_interface *intf)
2136 {
2137 	struct uvc_device *dev = usb_get_intfdata(intf);
2138 
2139 	/* Set the USB interface data to NULL. This can be done outside the
2140 	 * lock, as there's no other reader.
2141 	 */
2142 	usb_set_intfdata(intf, NULL);
2143 
2144 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2145 	    UVC_SC_VIDEOSTREAMING)
2146 		return;
2147 
2148 	uvc_unregister_video(dev);
2149 }
2150 
2151 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
2152 {
2153 	struct uvc_device *dev = usb_get_intfdata(intf);
2154 	struct uvc_streaming *stream;
2155 
2156 	uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
2157 		intf->cur_altsetting->desc.bInterfaceNumber);
2158 
2159 	/* Controls are cached on the fly so they don't need to be saved. */
2160 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2161 	    UVC_SC_VIDEOCONTROL) {
2162 		mutex_lock(&dev->lock);
2163 		if (dev->users)
2164 			uvc_status_stop(dev);
2165 		mutex_unlock(&dev->lock);
2166 		return 0;
2167 	}
2168 
2169 	list_for_each_entry(stream, &dev->streams, list) {
2170 		if (stream->intf == intf)
2171 			return uvc_video_suspend(stream);
2172 	}
2173 
2174 	uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
2175 			"mismatch.\n");
2176 	return -EINVAL;
2177 }
2178 
2179 static int __uvc_resume(struct usb_interface *intf, int reset)
2180 {
2181 	struct uvc_device *dev = usb_get_intfdata(intf);
2182 	struct uvc_streaming *stream;
2183 	int ret = 0;
2184 
2185 	uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
2186 		intf->cur_altsetting->desc.bInterfaceNumber);
2187 
2188 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2189 	    UVC_SC_VIDEOCONTROL) {
2190 		if (reset) {
2191 			ret = uvc_ctrl_restore_values(dev);
2192 			if (ret < 0)
2193 				return ret;
2194 		}
2195 
2196 		mutex_lock(&dev->lock);
2197 		if (dev->users)
2198 			ret = uvc_status_start(dev, GFP_NOIO);
2199 		mutex_unlock(&dev->lock);
2200 
2201 		return ret;
2202 	}
2203 
2204 	list_for_each_entry(stream, &dev->streams, list) {
2205 		if (stream->intf == intf) {
2206 			ret = uvc_video_resume(stream, reset);
2207 			if (ret < 0)
2208 				uvc_queue_streamoff(&stream->queue,
2209 						    stream->queue.queue.type);
2210 			return ret;
2211 		}
2212 	}
2213 
2214 	uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
2215 			"mismatch.\n");
2216 	return -EINVAL;
2217 }
2218 
2219 static int uvc_resume(struct usb_interface *intf)
2220 {
2221 	return __uvc_resume(intf, 0);
2222 }
2223 
2224 static int uvc_reset_resume(struct usb_interface *intf)
2225 {
2226 	return __uvc_resume(intf, 1);
2227 }
2228 
2229 /* ------------------------------------------------------------------------
2230  * Module parameters
2231  */
2232 
2233 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
2234 {
2235 	if (uvc_clock_param == CLOCK_MONOTONIC)
2236 		return sprintf(buffer, "CLOCK_MONOTONIC");
2237 	else
2238 		return sprintf(buffer, "CLOCK_REALTIME");
2239 }
2240 
2241 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
2242 {
2243 	if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
2244 		val += strlen("clock_");
2245 
2246 	if (strcasecmp(val, "monotonic") == 0)
2247 		uvc_clock_param = CLOCK_MONOTONIC;
2248 	else if (strcasecmp(val, "realtime") == 0)
2249 		uvc_clock_param = CLOCK_REALTIME;
2250 	else
2251 		return -EINVAL;
2252 
2253 	return 0;
2254 }
2255 
2256 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
2257 		  &uvc_clock_param, S_IRUGO|S_IWUSR);
2258 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
2259 module_param_named(hwtimestamps, uvc_hw_timestamps_param, uint, S_IRUGO|S_IWUSR);
2260 MODULE_PARM_DESC(hwtimestamps, "Use hardware timestamps");
2261 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2262 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2263 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2264 MODULE_PARM_DESC(quirks, "Forced device quirks");
2265 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2266 MODULE_PARM_DESC(trace, "Trace level bitmask");
2267 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2268 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2269 
2270 /* ------------------------------------------------------------------------
2271  * Driver initialization and cleanup
2272  */
2273 
2274 /*
2275  * The Logitech cameras listed below have their interface class set to
2276  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
2277  * though they are compliant.
2278  */
2279 static const struct usb_device_id uvc_ids[] = {
2280 	/* LogiLink Wireless Webcam */
2281 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2282 				| USB_DEVICE_ID_MATCH_INT_INFO,
2283 	  .idVendor		= 0x0416,
2284 	  .idProduct		= 0xa91a,
2285 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2286 	  .bInterfaceSubClass	= 1,
2287 	  .bInterfaceProtocol	= 0,
2288 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2289 	/* Genius eFace 2025 */
2290 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2291 				| USB_DEVICE_ID_MATCH_INT_INFO,
2292 	  .idVendor		= 0x0458,
2293 	  .idProduct		= 0x706e,
2294 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2295 	  .bInterfaceSubClass	= 1,
2296 	  .bInterfaceProtocol	= 0,
2297 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2298 	/* Microsoft Lifecam NX-6000 */
2299 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2300 				| USB_DEVICE_ID_MATCH_INT_INFO,
2301 	  .idVendor		= 0x045e,
2302 	  .idProduct		= 0x00f8,
2303 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2304 	  .bInterfaceSubClass	= 1,
2305 	  .bInterfaceProtocol	= 0,
2306 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2307 	/* Microsoft Lifecam NX-3000 */
2308 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2309 				| USB_DEVICE_ID_MATCH_INT_INFO,
2310 	  .idVendor		= 0x045e,
2311 	  .idProduct		= 0x0721,
2312 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2313 	  .bInterfaceSubClass	= 1,
2314 	  .bInterfaceProtocol	= 0,
2315 	  .driver_info		= UVC_QUIRK_PROBE_DEF },
2316 	/* Microsoft Lifecam VX-7000 */
2317 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2318 				| USB_DEVICE_ID_MATCH_INT_INFO,
2319 	  .idVendor		= 0x045e,
2320 	  .idProduct		= 0x0723,
2321 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2322 	  .bInterfaceSubClass	= 1,
2323 	  .bInterfaceProtocol	= 0,
2324 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2325 	/* Logitech Quickcam Fusion */
2326 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2327 				| USB_DEVICE_ID_MATCH_INT_INFO,
2328 	  .idVendor		= 0x046d,
2329 	  .idProduct		= 0x08c1,
2330 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2331 	  .bInterfaceSubClass	= 1,
2332 	  .bInterfaceProtocol	= 0 },
2333 	/* Logitech Quickcam Orbit MP */
2334 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2335 				| USB_DEVICE_ID_MATCH_INT_INFO,
2336 	  .idVendor		= 0x046d,
2337 	  .idProduct		= 0x08c2,
2338 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2339 	  .bInterfaceSubClass	= 1,
2340 	  .bInterfaceProtocol	= 0 },
2341 	/* Logitech Quickcam Pro for Notebook */
2342 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2343 				| USB_DEVICE_ID_MATCH_INT_INFO,
2344 	  .idVendor		= 0x046d,
2345 	  .idProduct		= 0x08c3,
2346 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2347 	  .bInterfaceSubClass	= 1,
2348 	  .bInterfaceProtocol	= 0 },
2349 	/* Logitech Quickcam Pro 5000 */
2350 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2351 				| USB_DEVICE_ID_MATCH_INT_INFO,
2352 	  .idVendor		= 0x046d,
2353 	  .idProduct		= 0x08c5,
2354 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2355 	  .bInterfaceSubClass	= 1,
2356 	  .bInterfaceProtocol	= 0 },
2357 	/* Logitech Quickcam OEM Dell Notebook */
2358 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2359 				| USB_DEVICE_ID_MATCH_INT_INFO,
2360 	  .idVendor		= 0x046d,
2361 	  .idProduct		= 0x08c6,
2362 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2363 	  .bInterfaceSubClass	= 1,
2364 	  .bInterfaceProtocol	= 0 },
2365 	/* Logitech Quickcam OEM Cisco VT Camera II */
2366 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2367 				| USB_DEVICE_ID_MATCH_INT_INFO,
2368 	  .idVendor		= 0x046d,
2369 	  .idProduct		= 0x08c7,
2370 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2371 	  .bInterfaceSubClass	= 1,
2372 	  .bInterfaceProtocol	= 0 },
2373 	/* Logitech HD Pro Webcam C920 */
2374 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2375 				| USB_DEVICE_ID_MATCH_INT_INFO,
2376 	  .idVendor		= 0x046d,
2377 	  .idProduct		= 0x082d,
2378 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2379 	  .bInterfaceSubClass	= 1,
2380 	  .bInterfaceProtocol	= 0,
2381 	  .driver_info		= UVC_QUIRK_RESTORE_CTRLS_ON_INIT },
2382 	/* Chicony CNF7129 (Asus EEE 100HE) */
2383 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2384 				| USB_DEVICE_ID_MATCH_INT_INFO,
2385 	  .idVendor		= 0x04f2,
2386 	  .idProduct		= 0xb071,
2387 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2388 	  .bInterfaceSubClass	= 1,
2389 	  .bInterfaceProtocol	= 0,
2390 	  .driver_info		= UVC_QUIRK_RESTRICT_FRAME_RATE },
2391 	/* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2392 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2393 				| USB_DEVICE_ID_MATCH_INT_INFO,
2394 	  .idVendor		= 0x058f,
2395 	  .idProduct		= 0x3820,
2396 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2397 	  .bInterfaceSubClass	= 1,
2398 	  .bInterfaceProtocol	= 0,
2399 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2400 	/* Dell XPS m1530 */
2401 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2402 				| USB_DEVICE_ID_MATCH_INT_INFO,
2403 	  .idVendor		= 0x05a9,
2404 	  .idProduct		= 0x2640,
2405 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2406 	  .bInterfaceSubClass	= 1,
2407 	  .bInterfaceProtocol	= 0,
2408 	  .driver_info 		= UVC_QUIRK_PROBE_DEF },
2409 	/* Dell SP2008WFP Monitor */
2410 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2411 				| USB_DEVICE_ID_MATCH_INT_INFO,
2412 	  .idVendor		= 0x05a9,
2413 	  .idProduct		= 0x2641,
2414 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2415 	  .bInterfaceSubClass	= 1,
2416 	  .bInterfaceProtocol	= 0,
2417 	  .driver_info 		= UVC_QUIRK_PROBE_DEF },
2418 	/* Dell Alienware X51 */
2419 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2420 				| USB_DEVICE_ID_MATCH_INT_INFO,
2421 	  .idVendor		= 0x05a9,
2422 	  .idProduct		= 0x2643,
2423 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2424 	  .bInterfaceSubClass	= 1,
2425 	  .bInterfaceProtocol	= 0,
2426 	  .driver_info	= UVC_QUIRK_PROBE_DEF },
2427 	/* Dell Studio Hybrid 140g (OmniVision webcam) */
2428 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2429 				| USB_DEVICE_ID_MATCH_INT_INFO,
2430 	  .idVendor		= 0x05a9,
2431 	  .idProduct		= 0x264a,
2432 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2433 	  .bInterfaceSubClass	= 1,
2434 	  .bInterfaceProtocol	= 0,
2435 	  .driver_info		= UVC_QUIRK_PROBE_DEF },
2436 	/* Dell XPS M1330 (OmniVision OV7670 webcam) */
2437 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2438 				| USB_DEVICE_ID_MATCH_INT_INFO,
2439 	  .idVendor		= 0x05a9,
2440 	  .idProduct		= 0x7670,
2441 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2442 	  .bInterfaceSubClass	= 1,
2443 	  .bInterfaceProtocol	= 0,
2444 	  .driver_info		= UVC_QUIRK_PROBE_DEF },
2445 	/* Apple Built-In iSight */
2446 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2447 				| USB_DEVICE_ID_MATCH_INT_INFO,
2448 	  .idVendor		= 0x05ac,
2449 	  .idProduct		= 0x8501,
2450 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2451 	  .bInterfaceSubClass	= 1,
2452 	  .bInterfaceProtocol	= 0,
2453 	  .driver_info 		= UVC_QUIRK_PROBE_MINMAX
2454 				| UVC_QUIRK_BUILTIN_ISIGHT },
2455 	/* Apple Built-In iSight via iBridge */
2456 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2457 				| USB_DEVICE_ID_MATCH_INT_INFO,
2458 	  .idVendor		= 0x05ac,
2459 	  .idProduct		= 0x8600,
2460 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2461 	  .bInterfaceSubClass	= 1,
2462 	  .bInterfaceProtocol	= 0,
2463 	  .driver_info		= UVC_QUIRK_PROBE_DEF },
2464 	/* Foxlink ("HP Webcam" on HP Mini 5103) */
2465 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2466 				| USB_DEVICE_ID_MATCH_INT_INFO,
2467 	  .idVendor		= 0x05c8,
2468 	  .idProduct		= 0x0403,
2469 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2470 	  .bInterfaceSubClass	= 1,
2471 	  .bInterfaceProtocol	= 0,
2472 	  .driver_info		= UVC_QUIRK_FIX_BANDWIDTH },
2473 	/* Genesys Logic USB 2.0 PC Camera */
2474 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2475 				| USB_DEVICE_ID_MATCH_INT_INFO,
2476 	  .idVendor		= 0x05e3,
2477 	  .idProduct		= 0x0505,
2478 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2479 	  .bInterfaceSubClass	= 1,
2480 	  .bInterfaceProtocol	= 0,
2481 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2482 	/* Hercules Classic Silver */
2483 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2484 				| USB_DEVICE_ID_MATCH_INT_INFO,
2485 	  .idVendor		= 0x06f8,
2486 	  .idProduct		= 0x300c,
2487 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2488 	  .bInterfaceSubClass	= 1,
2489 	  .bInterfaceProtocol	= 0,
2490 	  .driver_info		= UVC_QUIRK_FIX_BANDWIDTH },
2491 	/* ViMicro Vega */
2492 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2493 				| USB_DEVICE_ID_MATCH_INT_INFO,
2494 	  .idVendor		= 0x0ac8,
2495 	  .idProduct		= 0x332d,
2496 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2497 	  .bInterfaceSubClass	= 1,
2498 	  .bInterfaceProtocol	= 0,
2499 	  .driver_info		= UVC_QUIRK_FIX_BANDWIDTH },
2500 	/* ViMicro - Minoru3D */
2501 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2502 				| USB_DEVICE_ID_MATCH_INT_INFO,
2503 	  .idVendor		= 0x0ac8,
2504 	  .idProduct		= 0x3410,
2505 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2506 	  .bInterfaceSubClass	= 1,
2507 	  .bInterfaceProtocol	= 0,
2508 	  .driver_info		= UVC_QUIRK_FIX_BANDWIDTH },
2509 	/* ViMicro Venus - Minoru3D */
2510 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2511 				| USB_DEVICE_ID_MATCH_INT_INFO,
2512 	  .idVendor		= 0x0ac8,
2513 	  .idProduct		= 0x3420,
2514 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2515 	  .bInterfaceSubClass	= 1,
2516 	  .bInterfaceProtocol	= 0,
2517 	  .driver_info		= UVC_QUIRK_FIX_BANDWIDTH },
2518 	/* Ophir Optronics - SPCAM 620U */
2519 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2520 				| USB_DEVICE_ID_MATCH_INT_INFO,
2521 	  .idVendor		= 0x0bd3,
2522 	  .idProduct		= 0x0555,
2523 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2524 	  .bInterfaceSubClass	= 1,
2525 	  .bInterfaceProtocol	= 0,
2526 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2527 	/* MT6227 */
2528 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2529 				| USB_DEVICE_ID_MATCH_INT_INFO,
2530 	  .idVendor		= 0x0e8d,
2531 	  .idProduct		= 0x0004,
2532 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2533 	  .bInterfaceSubClass	= 1,
2534 	  .bInterfaceProtocol	= 0,
2535 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX
2536 				| UVC_QUIRK_PROBE_DEF },
2537 	/* IMC Networks (Medion Akoya) */
2538 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2539 				| USB_DEVICE_ID_MATCH_INT_INFO,
2540 	  .idVendor		= 0x13d3,
2541 	  .idProduct		= 0x5103,
2542 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2543 	  .bInterfaceSubClass	= 1,
2544 	  .bInterfaceProtocol	= 0,
2545 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2546 	/* JMicron USB2.0 XGA WebCam */
2547 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2548 				| USB_DEVICE_ID_MATCH_INT_INFO,
2549 	  .idVendor		= 0x152d,
2550 	  .idProduct		= 0x0310,
2551 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2552 	  .bInterfaceSubClass	= 1,
2553 	  .bInterfaceProtocol	= 0,
2554 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2555 	/* Syntek (HP Spartan) */
2556 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2557 				| USB_DEVICE_ID_MATCH_INT_INFO,
2558 	  .idVendor		= 0x174f,
2559 	  .idProduct		= 0x5212,
2560 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2561 	  .bInterfaceSubClass	= 1,
2562 	  .bInterfaceProtocol	= 0,
2563 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2564 	/* Syntek (Samsung Q310) */
2565 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2566 				| USB_DEVICE_ID_MATCH_INT_INFO,
2567 	  .idVendor		= 0x174f,
2568 	  .idProduct		= 0x5931,
2569 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2570 	  .bInterfaceSubClass	= 1,
2571 	  .bInterfaceProtocol	= 0,
2572 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2573 	/* Syntek (Packard Bell EasyNote MX52 */
2574 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2575 				| USB_DEVICE_ID_MATCH_INT_INFO,
2576 	  .idVendor		= 0x174f,
2577 	  .idProduct		= 0x8a12,
2578 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2579 	  .bInterfaceSubClass	= 1,
2580 	  .bInterfaceProtocol	= 0,
2581 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2582 	/* Syntek (Asus F9SG) */
2583 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2584 				| USB_DEVICE_ID_MATCH_INT_INFO,
2585 	  .idVendor		= 0x174f,
2586 	  .idProduct		= 0x8a31,
2587 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2588 	  .bInterfaceSubClass	= 1,
2589 	  .bInterfaceProtocol	= 0,
2590 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2591 	/* Syntek (Asus U3S) */
2592 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2593 				| USB_DEVICE_ID_MATCH_INT_INFO,
2594 	  .idVendor		= 0x174f,
2595 	  .idProduct		= 0x8a33,
2596 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2597 	  .bInterfaceSubClass	= 1,
2598 	  .bInterfaceProtocol	= 0,
2599 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2600 	/* Syntek (JAOtech Smart Terminal) */
2601 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2602 				| USB_DEVICE_ID_MATCH_INT_INFO,
2603 	  .idVendor		= 0x174f,
2604 	  .idProduct		= 0x8a34,
2605 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2606 	  .bInterfaceSubClass	= 1,
2607 	  .bInterfaceProtocol	= 0,
2608 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2609 	/* Miricle 307K */
2610 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2611 				| USB_DEVICE_ID_MATCH_INT_INFO,
2612 	  .idVendor		= 0x17dc,
2613 	  .idProduct		= 0x0202,
2614 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2615 	  .bInterfaceSubClass	= 1,
2616 	  .bInterfaceProtocol	= 0,
2617 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2618 	/* Lenovo Thinkpad SL400/SL500 */
2619 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2620 				| USB_DEVICE_ID_MATCH_INT_INFO,
2621 	  .idVendor		= 0x17ef,
2622 	  .idProduct		= 0x480b,
2623 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2624 	  .bInterfaceSubClass	= 1,
2625 	  .bInterfaceProtocol	= 0,
2626 	  .driver_info		= UVC_QUIRK_STREAM_NO_FID },
2627 	/* Aveo Technology USB 2.0 Camera */
2628 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2629 				| USB_DEVICE_ID_MATCH_INT_INFO,
2630 	  .idVendor		= 0x1871,
2631 	  .idProduct		= 0x0306,
2632 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2633 	  .bInterfaceSubClass	= 1,
2634 	  .bInterfaceProtocol	= 0,
2635 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX
2636 				| UVC_QUIRK_PROBE_EXTRAFIELDS },
2637 	/* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */
2638 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2639 				| USB_DEVICE_ID_MATCH_INT_INFO,
2640 	  .idVendor		= 0x1871,
2641 	  .idProduct		= 0x0516,
2642 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2643 	  .bInterfaceSubClass	= 1,
2644 	  .bInterfaceProtocol	= 0 },
2645 	/* Ecamm Pico iMage */
2646 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2647 				| USB_DEVICE_ID_MATCH_INT_INFO,
2648 	  .idVendor		= 0x18cd,
2649 	  .idProduct		= 0xcafe,
2650 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2651 	  .bInterfaceSubClass	= 1,
2652 	  .bInterfaceProtocol	= 0,
2653 	  .driver_info		= UVC_QUIRK_PROBE_EXTRAFIELDS },
2654 	/* Manta MM-353 Plako */
2655 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2656 				| USB_DEVICE_ID_MATCH_INT_INFO,
2657 	  .idVendor		= 0x18ec,
2658 	  .idProduct		= 0x3188,
2659 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2660 	  .bInterfaceSubClass	= 1,
2661 	  .bInterfaceProtocol	= 0,
2662 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2663 	/* FSC WebCam V30S */
2664 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2665 				| USB_DEVICE_ID_MATCH_INT_INFO,
2666 	  .idVendor		= 0x18ec,
2667 	  .idProduct		= 0x3288,
2668 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2669 	  .bInterfaceSubClass	= 1,
2670 	  .bInterfaceProtocol	= 0,
2671 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2672 	/* Arkmicro unbranded */
2673 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2674 				| USB_DEVICE_ID_MATCH_INT_INFO,
2675 	  .idVendor		= 0x18ec,
2676 	  .idProduct		= 0x3290,
2677 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2678 	  .bInterfaceSubClass	= 1,
2679 	  .bInterfaceProtocol	= 0,
2680 	  .driver_info		= UVC_QUIRK_PROBE_DEF },
2681 	/* The Imaging Source USB CCD cameras */
2682 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2683 				| USB_DEVICE_ID_MATCH_INT_INFO,
2684 	  .idVendor		= 0x199e,
2685 	  .idProduct		= 0x8102,
2686 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2687 	  .bInterfaceSubClass	= 1,
2688 	  .bInterfaceProtocol	= 0 },
2689 	/* Bodelin ProScopeHR */
2690 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2691 				| USB_DEVICE_ID_MATCH_DEV_HI
2692 				| USB_DEVICE_ID_MATCH_INT_INFO,
2693 	  .idVendor		= 0x19ab,
2694 	  .idProduct		= 0x1000,
2695 	  .bcdDevice_hi		= 0x0126,
2696 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2697 	  .bInterfaceSubClass	= 1,
2698 	  .bInterfaceProtocol	= 0,
2699 	  .driver_info		= UVC_QUIRK_STATUS_INTERVAL },
2700 	/* MSI StarCam 370i */
2701 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2702 				| USB_DEVICE_ID_MATCH_INT_INFO,
2703 	  .idVendor		= 0x1b3b,
2704 	  .idProduct		= 0x2951,
2705 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2706 	  .bInterfaceSubClass	= 1,
2707 	  .bInterfaceProtocol	= 0,
2708 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX },
2709 	/* SiGma Micro USB Web Camera */
2710 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2711 				| USB_DEVICE_ID_MATCH_INT_INFO,
2712 	  .idVendor		= 0x1c4f,
2713 	  .idProduct		= 0x3000,
2714 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2715 	  .bInterfaceSubClass	= 1,
2716 	  .bInterfaceProtocol	= 0,
2717 	  .driver_info		= UVC_QUIRK_PROBE_MINMAX
2718 				| UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2719 	/* Oculus VR Positional Tracker DK2 */
2720 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2721 				| USB_DEVICE_ID_MATCH_INT_INFO,
2722 	  .idVendor		= 0x2833,
2723 	  .idProduct		= 0x0201,
2724 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2725 	  .bInterfaceSubClass	= 1,
2726 	  .bInterfaceProtocol	= 0,
2727 	  .driver_info		= UVC_QUIRK_FORCE_Y8 },
2728 	/* Oculus VR Rift Sensor */
2729 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2730 				| USB_DEVICE_ID_MATCH_INT_INFO,
2731 	  .idVendor		= 0x2833,
2732 	  .idProduct		= 0x0211,
2733 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2734 	  .bInterfaceSubClass	= 1,
2735 	  .bInterfaceProtocol	= 0,
2736 	  .driver_info		= UVC_QUIRK_FORCE_Y8 },
2737 	/* Generic USB Video Class */
2738 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_UNDEFINED) },
2739 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_15) },
2740 	{}
2741 };
2742 
2743 MODULE_DEVICE_TABLE(usb, uvc_ids);
2744 
2745 struct uvc_driver uvc_driver = {
2746 	.driver = {
2747 		.name		= "uvcvideo",
2748 		.probe		= uvc_probe,
2749 		.disconnect	= uvc_disconnect,
2750 		.suspend	= uvc_suspend,
2751 		.resume		= uvc_resume,
2752 		.reset_resume	= uvc_reset_resume,
2753 		.id_table	= uvc_ids,
2754 		.supports_autosuspend = 1,
2755 	},
2756 };
2757 
2758 static int __init uvc_init(void)
2759 {
2760 	int ret;
2761 
2762 	uvc_debugfs_init();
2763 
2764 	ret = usb_register(&uvc_driver.driver);
2765 	if (ret < 0) {
2766 		uvc_debugfs_cleanup();
2767 		return ret;
2768 	}
2769 
2770 	printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2771 	return 0;
2772 }
2773 
2774 static void __exit uvc_cleanup(void)
2775 {
2776 	usb_deregister(&uvc_driver.driver);
2777 	uvc_debugfs_cleanup();
2778 }
2779 
2780 module_init(uvc_init);
2781 module_exit(uvc_cleanup);
2782 
2783 MODULE_AUTHOR(DRIVER_AUTHOR);
2784 MODULE_DESCRIPTION(DRIVER_DESC);
2785 MODULE_LICENSE("GPL");
2786 MODULE_VERSION(DRIVER_VERSION);
2787 
2788