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