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