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