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