xref: /linux/drivers/media/usb/uvc/uvc_driver.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
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 		vb2_video_unregister_device(&stream->vdev);
1965 		vb2_video_unregister_device(&stream->meta.vdev);
1966 
1967 		/*
1968 		 * Now both vdevs are not streaming and all the ioctls will
1969 		 * return -ENODEV.
1970 		 */
1971 
1972 		uvc_debugfs_cleanup_stream(stream);
1973 	}
1974 
1975 	uvc_status_unregister(dev);
1976 
1977 	if (dev->vdev.dev)
1978 		v4l2_device_unregister(&dev->vdev);
1979 #ifdef CONFIG_MEDIA_CONTROLLER
1980 	if (media_devnode_is_registered(dev->mdev.devnode))
1981 		media_device_unregister(&dev->mdev);
1982 #endif
1983 }
1984 
1985 int uvc_register_video_device(struct uvc_device *dev,
1986 			      struct uvc_streaming *stream,
1987 			      struct video_device *vdev,
1988 			      struct uvc_video_queue *queue,
1989 			      enum v4l2_buf_type type,
1990 			      const struct v4l2_file_operations *fops,
1991 			      const struct v4l2_ioctl_ops *ioctl_ops)
1992 {
1993 	int ret;
1994 
1995 	/* Initialize the video buffers queue. */
1996 	ret = uvc_queue_init(queue, type);
1997 	if (ret)
1998 		return ret;
1999 
2000 	/* Register the device with V4L. */
2001 
2002 	/*
2003 	 * We already hold a reference to dev->udev. The video device will be
2004 	 * unregistered before the reference is released, so we don't need to
2005 	 * get another one.
2006 	 */
2007 	vdev->v4l2_dev = &dev->vdev;
2008 	vdev->fops = fops;
2009 	vdev->ioctl_ops = ioctl_ops;
2010 	vdev->release = uvc_release;
2011 	vdev->prio = &stream->chain->prio;
2012 	vdev->queue = &queue->queue;
2013 	vdev->lock = &queue->mutex;
2014 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2015 		vdev->vfl_dir = VFL_DIR_TX;
2016 	else
2017 		vdev->vfl_dir = VFL_DIR_RX;
2018 
2019 	switch (type) {
2020 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2021 	default:
2022 		vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
2023 		break;
2024 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2025 		vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
2026 		break;
2027 	case V4L2_BUF_TYPE_META_CAPTURE:
2028 		vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
2029 		break;
2030 	}
2031 
2032 	strscpy(vdev->name, dev->name, sizeof(vdev->name));
2033 
2034 	/*
2035 	 * Set the driver data before calling video_register_device, otherwise
2036 	 * the file open() handler might race us.
2037 	 */
2038 	video_set_drvdata(vdev, stream);
2039 
2040 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2041 	if (ret < 0) {
2042 		dev_err(&stream->intf->dev,
2043 			"Failed to register %s device (%d).\n",
2044 			v4l2_type_names[type], ret);
2045 		return ret;
2046 	}
2047 
2048 	kref_get(&dev->ref);
2049 	return 0;
2050 }
2051 
2052 static int uvc_register_video(struct uvc_device *dev,
2053 		struct uvc_streaming *stream)
2054 {
2055 	int ret;
2056 
2057 	/* Initialize the streaming interface with default parameters. */
2058 	ret = uvc_video_init(stream);
2059 	if (ret < 0) {
2060 		dev_err(&stream->intf->dev,
2061 			"Failed to initialize the device (%d).\n", ret);
2062 		return ret;
2063 	}
2064 
2065 	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2066 		stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE
2067 			| V4L2_CAP_META_CAPTURE;
2068 	else
2069 		stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT;
2070 
2071 	uvc_debugfs_init_stream(stream);
2072 
2073 	/* Register the device with V4L. */
2074 	return uvc_register_video_device(dev, stream, &stream->vdev,
2075 					 &stream->queue, stream->type,
2076 					 &uvc_fops, &uvc_ioctl_ops);
2077 }
2078 
2079 /*
2080  * Register all video devices in all chains.
2081  */
2082 static int uvc_register_terms(struct uvc_device *dev,
2083 	struct uvc_video_chain *chain)
2084 {
2085 	struct uvc_streaming *stream;
2086 	struct uvc_entity *term;
2087 	int ret;
2088 
2089 	list_for_each_entry(term, &chain->entities, chain) {
2090 		if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
2091 			continue;
2092 
2093 		stream = uvc_stream_by_id(dev, term->id);
2094 		if (stream == NULL) {
2095 			dev_info(&dev->udev->dev,
2096 				 "No streaming interface found for terminal %u.",
2097 				 term->id);
2098 			continue;
2099 		}
2100 
2101 		stream->chain = chain;
2102 		ret = uvc_register_video(dev, stream);
2103 		if (ret < 0)
2104 			return ret;
2105 
2106 		/*
2107 		 * Register a metadata node, but ignore a possible failure,
2108 		 * complete registration of video nodes anyway.
2109 		 */
2110 		uvc_meta_register(stream);
2111 
2112 		term->vdev = &stream->vdev;
2113 	}
2114 
2115 	return 0;
2116 }
2117 
2118 static int uvc_register_chains(struct uvc_device *dev)
2119 {
2120 	struct uvc_video_chain *chain;
2121 	int ret;
2122 
2123 	list_for_each_entry(chain, &dev->chains, list) {
2124 		ret = uvc_register_terms(dev, chain);
2125 		if (ret < 0)
2126 			return ret;
2127 
2128 #ifdef CONFIG_MEDIA_CONTROLLER
2129 		ret = uvc_mc_register_entities(chain);
2130 		if (ret < 0)
2131 			dev_info(&dev->udev->dev,
2132 				 "Failed to register entities (%d).\n", ret);
2133 #endif
2134 	}
2135 
2136 	return 0;
2137 }
2138 
2139 /* ------------------------------------------------------------------------
2140  * USB probe, disconnect, suspend and resume
2141  */
2142 
2143 static const struct uvc_device_info uvc_quirk_none = { 0 };
2144 
2145 static int uvc_probe(struct usb_interface *intf,
2146 		     const struct usb_device_id *id)
2147 {
2148 	struct usb_device *udev = interface_to_usbdev(intf);
2149 	struct uvc_device *dev;
2150 	const struct uvc_device_info *info =
2151 		(const struct uvc_device_info *)id->driver_info;
2152 	int function;
2153 	int ret;
2154 
2155 	/* Allocate memory for the device and initialize it. */
2156 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2157 	if (dev == NULL)
2158 		return -ENOMEM;
2159 
2160 	INIT_LIST_HEAD(&dev->entities);
2161 	INIT_LIST_HEAD(&dev->chains);
2162 	INIT_LIST_HEAD(&dev->streams);
2163 	kref_init(&dev->ref);
2164 	atomic_set(&dev->nmappings, 0);
2165 
2166 	dev->udev = usb_get_dev(udev);
2167 	dev->intf = usb_get_intf(intf);
2168 	dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
2169 	dev->info = info ? info : &uvc_quirk_none;
2170 	dev->quirks = uvc_quirks_param == -1
2171 		    ? dev->info->quirks : uvc_quirks_param;
2172 
2173 	if (id->idVendor && id->idProduct)
2174 		uvc_dbg(dev, PROBE, "Probing known UVC device %s (%04x:%04x)\n",
2175 			udev->devpath, id->idVendor, id->idProduct);
2176 	else
2177 		uvc_dbg(dev, PROBE, "Probing generic UVC device %s\n",
2178 			udev->devpath);
2179 
2180 	if (udev->product != NULL)
2181 		strscpy(dev->name, udev->product, sizeof(dev->name));
2182 	else
2183 		snprintf(dev->name, sizeof(dev->name),
2184 			 "UVC Camera (%04x:%04x)",
2185 			 le16_to_cpu(udev->descriptor.idVendor),
2186 			 le16_to_cpu(udev->descriptor.idProduct));
2187 
2188 	/*
2189 	 * Add iFunction or iInterface to names when available as additional
2190 	 * distinguishers between interfaces. iFunction is prioritized over
2191 	 * iInterface which matches Windows behavior at the point of writing.
2192 	 */
2193 	if (intf->intf_assoc && intf->intf_assoc->iFunction != 0)
2194 		function = intf->intf_assoc->iFunction;
2195 	else
2196 		function = intf->cur_altsetting->desc.iInterface;
2197 	if (function != 0) {
2198 		size_t len;
2199 
2200 		strlcat(dev->name, ": ", sizeof(dev->name));
2201 		len = strlen(dev->name);
2202 		usb_string(udev, function, dev->name + len,
2203 			   sizeof(dev->name) - len);
2204 	}
2205 
2206 	/* Initialize the media device. */
2207 #ifdef CONFIG_MEDIA_CONTROLLER
2208 	dev->mdev.dev = &intf->dev;
2209 	strscpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
2210 	if (udev->serial)
2211 		strscpy(dev->mdev.serial, udev->serial,
2212 			sizeof(dev->mdev.serial));
2213 	usb_make_path(udev, dev->mdev.bus_info, sizeof(dev->mdev.bus_info));
2214 	dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
2215 	media_device_init(&dev->mdev);
2216 
2217 	dev->vdev.mdev = &dev->mdev;
2218 #endif
2219 
2220 	/* Parse the Video Class control descriptor. */
2221 	ret = uvc_parse_control(dev);
2222 	if (ret < 0) {
2223 		uvc_dbg(dev, PROBE, "Unable to parse UVC descriptors\n");
2224 		goto error;
2225 	}
2226 
2227 	/* Parse the associated GPIOs. */
2228 	ret = uvc_gpio_parse(dev);
2229 	if (ret < 0)
2230 		goto error;
2231 
2232 	dev_info(&dev->udev->dev, "Found UVC %u.%02x device %s (%04x:%04x)\n",
2233 		 dev->uvc_version >> 8, dev->uvc_version & 0xff,
2234 		 udev->product ? udev->product : "<unnamed>",
2235 		 le16_to_cpu(udev->descriptor.idVendor),
2236 		 le16_to_cpu(udev->descriptor.idProduct));
2237 
2238 	if (dev->quirks != dev->info->quirks) {
2239 		dev_info(&dev->udev->dev,
2240 			 "Forcing device quirks to 0x%x by module parameter for testing purpose.\n",
2241 			 dev->quirks);
2242 		dev_info(&dev->udev->dev,
2243 			 "Please report required quirks to the linux-media mailing list.\n");
2244 	}
2245 
2246 	if (dev->info->uvc_version) {
2247 		dev->uvc_version = dev->info->uvc_version;
2248 		dev_info(&dev->udev->dev, "Forcing UVC version to %u.%02x\n",
2249 			 dev->uvc_version >> 8, dev->uvc_version & 0xff);
2250 	}
2251 
2252 	/* Register the V4L2 device. */
2253 	ret = v4l2_device_register(&intf->dev, &dev->vdev);
2254 	if (ret < 0)
2255 		goto error;
2256 
2257 	/* Scan the device for video chains. */
2258 	ret = uvc_scan_device(dev);
2259 	if (ret < 0)
2260 		goto error;
2261 
2262 	/* Initialize controls. */
2263 	ret = uvc_ctrl_init_device(dev);
2264 	if (ret < 0)
2265 		goto error;
2266 
2267 	/* Register video device nodes. */
2268 	ret = uvc_register_chains(dev);
2269 	if (ret < 0)
2270 		goto error;
2271 
2272 #ifdef CONFIG_MEDIA_CONTROLLER
2273 	/* Register the media device node */
2274 	ret = media_device_register(&dev->mdev);
2275 	if (ret < 0)
2276 		goto error;
2277 #endif
2278 	/* Save our data pointer in the interface data. */
2279 	usb_set_intfdata(intf, dev);
2280 
2281 	/* Initialize the interrupt URB. */
2282 	ret = uvc_status_init(dev);
2283 	if (ret < 0) {
2284 		dev_info(&dev->udev->dev,
2285 			 "Unable to initialize the status endpoint (%d), status interrupt will not be supported.\n",
2286 			 ret);
2287 	}
2288 
2289 	ret = uvc_gpio_init_irq(dev);
2290 	if (ret < 0) {
2291 		dev_err(&dev->udev->dev,
2292 			"Unable to request privacy GPIO IRQ (%d)\n", ret);
2293 		goto error;
2294 	}
2295 
2296 	ret = uvc_meta_init(dev);
2297 	if (ret < 0) {
2298 		dev_err(&dev->udev->dev,
2299 			"Error initializing the metadata formats (%d)\n", ret);
2300 		goto error;
2301 	}
2302 
2303 	if (dev->quirks & UVC_QUIRK_NO_RESET_RESUME)
2304 		udev->quirks &= ~USB_QUIRK_RESET_RESUME;
2305 
2306 	if (!(dev->quirks & UVC_QUIRK_DISABLE_AUTOSUSPEND))
2307 		usb_enable_autosuspend(udev);
2308 
2309 	uvc_dbg(dev, PROBE, "UVC device initialized\n");
2310 
2311 	return 0;
2312 
2313 error:
2314 	uvc_unregister_video(dev);
2315 	kref_put(&dev->ref, uvc_delete);
2316 	return ret;
2317 }
2318 
2319 static void uvc_disconnect(struct usb_interface *intf)
2320 {
2321 	struct uvc_device *dev = usb_get_intfdata(intf);
2322 
2323 	/*
2324 	 * Set the USB interface data to NULL. This can be done outside the
2325 	 * lock, as there's no other reader.
2326 	 */
2327 	usb_set_intfdata(intf, NULL);
2328 
2329 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2330 	    UVC_SC_VIDEOSTREAMING)
2331 		return;
2332 
2333 	uvc_unregister_video(dev);
2334 	kref_put(&dev->ref, uvc_delete);
2335 }
2336 
2337 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
2338 {
2339 	struct uvc_device *dev = usb_get_intfdata(intf);
2340 	struct uvc_streaming *stream;
2341 
2342 	uvc_dbg(dev, SUSPEND, "Suspending interface %u\n",
2343 		intf->cur_altsetting->desc.bInterfaceNumber);
2344 
2345 	/* Controls are cached on the fly so they don't need to be saved. */
2346 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2347 	    UVC_SC_VIDEOCONTROL) {
2348 		uvc_status_suspend(dev);
2349 		return 0;
2350 	}
2351 
2352 	list_for_each_entry(stream, &dev->streams, list) {
2353 		if (stream->intf == intf)
2354 			return uvc_video_suspend(stream);
2355 	}
2356 
2357 	uvc_dbg(dev, SUSPEND,
2358 		"Suspend: video streaming USB interface mismatch\n");
2359 	return -EINVAL;
2360 }
2361 
2362 static int __uvc_resume(struct usb_interface *intf, int reset)
2363 {
2364 	struct uvc_device *dev = usb_get_intfdata(intf);
2365 	struct uvc_streaming *stream;
2366 	int ret = 0;
2367 
2368 	uvc_dbg(dev, SUSPEND, "Resuming interface %u\n",
2369 		intf->cur_altsetting->desc.bInterfaceNumber);
2370 
2371 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2372 	    UVC_SC_VIDEOCONTROL) {
2373 		if (reset) {
2374 			ret = uvc_ctrl_restore_values(dev);
2375 			if (ret < 0)
2376 				return ret;
2377 		}
2378 
2379 		return uvc_status_resume(dev);
2380 	}
2381 
2382 	list_for_each_entry(stream, &dev->streams, list) {
2383 		if (stream->intf == intf) {
2384 			ret = uvc_video_resume(stream, reset);
2385 			if (ret < 0) {
2386 				mutex_lock(&stream->queue.mutex);
2387 				vb2_streamoff(&stream->queue.queue,
2388 					      stream->queue.queue.type);
2389 				mutex_unlock(&stream->queue.mutex);
2390 			}
2391 			return ret;
2392 		}
2393 	}
2394 
2395 	uvc_dbg(dev, SUSPEND,
2396 		"Resume: video streaming USB interface mismatch\n");
2397 	return -EINVAL;
2398 }
2399 
2400 static int uvc_resume(struct usb_interface *intf)
2401 {
2402 	return __uvc_resume(intf, 0);
2403 }
2404 
2405 static int uvc_reset_resume(struct usb_interface *intf)
2406 {
2407 	return __uvc_resume(intf, 1);
2408 }
2409 
2410 /* ------------------------------------------------------------------------
2411  * Module parameters
2412  */
2413 
2414 static int uvc_clock_param_get(char *buffer, const struct kernel_param *kp)
2415 {
2416 	if (uvc_clock_param == CLOCK_MONOTONIC)
2417 		return sprintf(buffer, "CLOCK_MONOTONIC");
2418 	else
2419 		return sprintf(buffer, "CLOCK_REALTIME");
2420 }
2421 
2422 static int uvc_clock_param_set(const char *val, const struct kernel_param *kp)
2423 {
2424 	if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
2425 		val += strlen("clock_");
2426 
2427 	if (strcasecmp(val, "monotonic") == 0)
2428 		uvc_clock_param = CLOCK_MONOTONIC;
2429 	else if (strcasecmp(val, "realtime") == 0)
2430 		uvc_clock_param = CLOCK_REALTIME;
2431 	else
2432 		return -EINVAL;
2433 
2434 	return 0;
2435 }
2436 
2437 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
2438 		  &uvc_clock_param, 0644);
2439 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
2440 module_param_named(hwtimestamps, uvc_hw_timestamps_param, uint, 0644);
2441 MODULE_PARM_DESC(hwtimestamps, "Use hardware timestamps");
2442 
2443 static int param_set_nodrop(const char *val, const struct kernel_param *kp)
2444 {
2445 	pr_warn_once("uvcvideo: "
2446 		     DEPRECATED
2447 		     "nodrop parameter will be eventually removed.\n");
2448 	return param_set_bool(val, kp);
2449 }
2450 
2451 static const struct kernel_param_ops param_ops_nodrop = {
2452 	.set = param_set_nodrop,
2453 	.get = param_get_uint,
2454 };
2455 
2456 param_check_uint(nodrop, &uvc_no_drop_param);
2457 module_param_cb(nodrop, &param_ops_nodrop, &uvc_no_drop_param, 0644);
2458 __MODULE_PARM_TYPE(nodrop, "uint");
2459 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2460 
2461 module_param_named(quirks, uvc_quirks_param, uint, 0644);
2462 MODULE_PARM_DESC(quirks, "Forced device quirks");
2463 module_param_named(trace, uvc_dbg_param, uint, 0644);
2464 MODULE_PARM_DESC(trace, "Trace level bitmask");
2465 module_param_named(timeout, uvc_timeout_param, uint, 0644);
2466 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2467 
2468 /* ------------------------------------------------------------------------
2469  * Driver initialization and cleanup
2470  */
2471 
2472 static const struct uvc_device_info uvc_quirk_probe_minmax = {
2473 	.quirks = UVC_QUIRK_PROBE_MINMAX,
2474 };
2475 
2476 static const struct uvc_device_info uvc_quirk_fix_bandwidth = {
2477 	.quirks = UVC_QUIRK_FIX_BANDWIDTH,
2478 };
2479 
2480 static const struct uvc_device_info uvc_quirk_probe_def = {
2481 	.quirks = UVC_QUIRK_PROBE_DEF,
2482 };
2483 
2484 static const struct uvc_device_info uvc_quirk_stream_no_fid = {
2485 	.quirks = UVC_QUIRK_STREAM_NO_FID,
2486 };
2487 
2488 static const struct uvc_device_info uvc_quirk_force_y8 = {
2489 	.quirks = UVC_QUIRK_FORCE_Y8,
2490 };
2491 
2492 #define UVC_INFO_QUIRK(q) (kernel_ulong_t)&(struct uvc_device_info){.quirks = q}
2493 #define UVC_INFO_META(m) (kernel_ulong_t)&(struct uvc_device_info) \
2494 	{.meta_format = m}
2495 
2496 /*
2497  * The Logitech cameras listed below have their interface class set to
2498  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
2499  * though they are compliant.
2500  *
2501  * Sort these by vendor/product ID.
2502  */
2503 static const struct usb_device_id uvc_ids[] = {
2504 	/* HP Webcam HD 2300 */
2505 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2506 				| USB_DEVICE_ID_MATCH_INT_INFO,
2507 	  .idVendor		= 0x03f0,
2508 	  .idProduct		= 0xe207,
2509 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2510 	  .bInterfaceSubClass	= 1,
2511 	  .bInterfaceProtocol	= 0,
2512 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2513 	/* Quanta ACER HD User Facing */
2514 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2515 				| USB_DEVICE_ID_MATCH_INT_INFO,
2516 	  .idVendor		= 0x0408,
2517 	  .idProduct		= 0x4033,
2518 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2519 	  .bInterfaceSubClass	= 1,
2520 	  .bInterfaceProtocol	= UVC_PC_PROTOCOL_15,
2521 	  .driver_info		= (kernel_ulong_t)&(const struct uvc_device_info){
2522 		.uvc_version = 0x010a,
2523 	  } },
2524 	/* Quanta ACER HD User Facing */
2525 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2526 				| USB_DEVICE_ID_MATCH_INT_INFO,
2527 	  .idVendor		= 0x0408,
2528 	  .idProduct		= 0x4035,
2529 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2530 	  .bInterfaceSubClass	= 1,
2531 	  .bInterfaceProtocol	= UVC_PC_PROTOCOL_15,
2532 	  .driver_info		= (kernel_ulong_t)&(const struct uvc_device_info){
2533 		.uvc_version = 0x010a,
2534 	  } },
2535 	/* LogiLink Wireless Webcam */
2536 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2537 				| USB_DEVICE_ID_MATCH_INT_INFO,
2538 	  .idVendor		= 0x0416,
2539 	  .idProduct		= 0xa91a,
2540 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2541 	  .bInterfaceSubClass	= 1,
2542 	  .bInterfaceProtocol	= 0,
2543 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2544 	/* Genius eFace 2025 */
2545 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2546 				| USB_DEVICE_ID_MATCH_INT_INFO,
2547 	  .idVendor		= 0x0458,
2548 	  .idProduct		= 0x706e,
2549 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2550 	  .bInterfaceSubClass	= 1,
2551 	  .bInterfaceProtocol	= 0,
2552 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2553 	/* Microsoft Lifecam NX-6000 */
2554 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2555 				| USB_DEVICE_ID_MATCH_INT_INFO,
2556 	  .idVendor		= 0x045e,
2557 	  .idProduct		= 0x00f8,
2558 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2559 	  .bInterfaceSubClass	= 1,
2560 	  .bInterfaceProtocol	= 0,
2561 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2562 	/* Microsoft Lifecam NX-3000 */
2563 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2564 				| USB_DEVICE_ID_MATCH_INT_INFO,
2565 	  .idVendor		= 0x045e,
2566 	  .idProduct		= 0x0721,
2567 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2568 	  .bInterfaceSubClass	= 1,
2569 	  .bInterfaceProtocol	= 0,
2570 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2571 	/* Microsoft Lifecam VX-7000 */
2572 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2573 				| USB_DEVICE_ID_MATCH_INT_INFO,
2574 	  .idVendor		= 0x045e,
2575 	  .idProduct		= 0x0723,
2576 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2577 	  .bInterfaceSubClass	= 1,
2578 	  .bInterfaceProtocol	= 0,
2579 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2580 	/* Logitech, Webcam C910 */
2581 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2582 				| USB_DEVICE_ID_MATCH_INT_INFO,
2583 	  .idVendor		= 0x046d,
2584 	  .idProduct		= 0x0821,
2585 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2586 	  .bInterfaceSubClass	= 1,
2587 	  .bInterfaceProtocol	= 0,
2588 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_WAKE_AUTOSUSPEND)},
2589 	/* Logitech, Webcam B910 */
2590 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2591 				| USB_DEVICE_ID_MATCH_INT_INFO,
2592 	  .idVendor		= 0x046d,
2593 	  .idProduct		= 0x0823,
2594 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2595 	  .bInterfaceSubClass	= 1,
2596 	  .bInterfaceProtocol	= 0,
2597 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_WAKE_AUTOSUSPEND)},
2598 	/* Logitech Quickcam Fusion */
2599 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2600 				| USB_DEVICE_ID_MATCH_INT_INFO,
2601 	  .idVendor		= 0x046d,
2602 	  .idProduct		= 0x08c1,
2603 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2604 	  .bInterfaceSubClass	= 1,
2605 	  .bInterfaceProtocol	= 0 },
2606 	/* Logitech Quickcam Orbit MP */
2607 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2608 				| USB_DEVICE_ID_MATCH_INT_INFO,
2609 	  .idVendor		= 0x046d,
2610 	  .idProduct		= 0x08c2,
2611 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2612 	  .bInterfaceSubClass	= 1,
2613 	  .bInterfaceProtocol	= 0 },
2614 	/* Logitech Quickcam Pro for Notebook */
2615 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2616 				| USB_DEVICE_ID_MATCH_INT_INFO,
2617 	  .idVendor		= 0x046d,
2618 	  .idProduct		= 0x08c3,
2619 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2620 	  .bInterfaceSubClass	= 1,
2621 	  .bInterfaceProtocol	= 0 },
2622 	/* Logitech Quickcam Pro 5000 */
2623 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2624 				| USB_DEVICE_ID_MATCH_INT_INFO,
2625 	  .idVendor		= 0x046d,
2626 	  .idProduct		= 0x08c5,
2627 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2628 	  .bInterfaceSubClass	= 1,
2629 	  .bInterfaceProtocol	= 0 },
2630 	/* Logitech Quickcam OEM Dell Notebook */
2631 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2632 				| USB_DEVICE_ID_MATCH_INT_INFO,
2633 	  .idVendor		= 0x046d,
2634 	  .idProduct		= 0x08c6,
2635 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2636 	  .bInterfaceSubClass	= 1,
2637 	  .bInterfaceProtocol	= 0 },
2638 	/* Logitech Quickcam OEM Cisco VT Camera II */
2639 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2640 				| USB_DEVICE_ID_MATCH_INT_INFO,
2641 	  .idVendor		= 0x046d,
2642 	  .idProduct		= 0x08c7,
2643 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2644 	  .bInterfaceSubClass	= 1,
2645 	  .bInterfaceProtocol	= 0 },
2646 	/* Logitech HD Pro Webcam C920 */
2647 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2648 				| USB_DEVICE_ID_MATCH_INT_INFO,
2649 	  .idVendor		= 0x046d,
2650 	  .idProduct		= 0x082d,
2651 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2652 	  .bInterfaceSubClass	= 1,
2653 	  .bInterfaceProtocol	= 0,
2654 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT
2655 					       | UVC_QUIRK_INVALID_DEVICE_SOF) },
2656 	/* Logitech HD Pro Webcam C922 */
2657 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2658 				| USB_DEVICE_ID_MATCH_INT_INFO,
2659 	  .idVendor		= 0x046d,
2660 	  .idProduct		= 0x085c,
2661 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2662 	  .bInterfaceSubClass	= 1,
2663 	  .bInterfaceProtocol	= 0,
2664 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_INVALID_DEVICE_SOF) },
2665 	/* Logitech Rally Bar Huddle */
2666 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2667 				| USB_DEVICE_ID_MATCH_INT_INFO,
2668 	  .idVendor		= 0x046d,
2669 	  .idProduct		= 0x087c,
2670 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2671 	  .bInterfaceSubClass	= 1,
2672 	  .bInterfaceProtocol	= 0,
2673 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_NO_RESET_RESUME) },
2674 	/* Logitech Rally Bar */
2675 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2676 				| USB_DEVICE_ID_MATCH_INT_INFO,
2677 	  .idVendor		= 0x046d,
2678 	  .idProduct		= 0x089b,
2679 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2680 	  .bInterfaceSubClass	= 1,
2681 	  .bInterfaceProtocol	= 0,
2682 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_NO_RESET_RESUME) },
2683 	/* Logitech Rally Bar Mini */
2684 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2685 				| USB_DEVICE_ID_MATCH_INT_INFO,
2686 	  .idVendor		= 0x046d,
2687 	  .idProduct		= 0x08d3,
2688 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2689 	  .bInterfaceSubClass	= 1,
2690 	  .bInterfaceProtocol	= 0,
2691 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_NO_RESET_RESUME) },
2692 	/* Chicony CNF7129 (Asus EEE 100HE) */
2693 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2694 				| USB_DEVICE_ID_MATCH_INT_INFO,
2695 	  .idVendor		= 0x04f2,
2696 	  .idProduct		= 0xb071,
2697 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2698 	  .bInterfaceSubClass	= 1,
2699 	  .bInterfaceProtocol	= 0,
2700 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_RESTRICT_FRAME_RATE) },
2701 	/* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2702 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2703 				| USB_DEVICE_ID_MATCH_INT_INFO,
2704 	  .idVendor		= 0x058f,
2705 	  .idProduct		= 0x3820,
2706 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2707 	  .bInterfaceSubClass	= 1,
2708 	  .bInterfaceProtocol	= 0,
2709 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2710 	/* Dell XPS m1530 */
2711 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2712 				| USB_DEVICE_ID_MATCH_INT_INFO,
2713 	  .idVendor		= 0x05a9,
2714 	  .idProduct		= 0x2640,
2715 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2716 	  .bInterfaceSubClass	= 1,
2717 	  .bInterfaceProtocol	= 0,
2718 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2719 	/* Dell SP2008WFP Monitor */
2720 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2721 				| USB_DEVICE_ID_MATCH_INT_INFO,
2722 	  .idVendor		= 0x05a9,
2723 	  .idProduct		= 0x2641,
2724 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2725 	  .bInterfaceSubClass	= 1,
2726 	  .bInterfaceProtocol	= 0,
2727 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2728 	/* Dell Alienware X51 */
2729 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2730 				| USB_DEVICE_ID_MATCH_INT_INFO,
2731 	  .idVendor		= 0x05a9,
2732 	  .idProduct		= 0x2643,
2733 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2734 	  .bInterfaceSubClass	= 1,
2735 	  .bInterfaceProtocol	= 0,
2736 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2737 	/* Dell Studio Hybrid 140g (OmniVision webcam) */
2738 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2739 				| USB_DEVICE_ID_MATCH_INT_INFO,
2740 	  .idVendor		= 0x05a9,
2741 	  .idProduct		= 0x264a,
2742 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2743 	  .bInterfaceSubClass	= 1,
2744 	  .bInterfaceProtocol	= 0,
2745 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2746 	/* Dell XPS M1330 (OmniVision OV7670 webcam) */
2747 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2748 				| USB_DEVICE_ID_MATCH_INT_INFO,
2749 	  .idVendor		= 0x05a9,
2750 	  .idProduct		= 0x7670,
2751 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2752 	  .bInterfaceSubClass	= 1,
2753 	  .bInterfaceProtocol	= 0,
2754 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2755 	/* Apple Built-In iSight */
2756 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2757 				| USB_DEVICE_ID_MATCH_INT_INFO,
2758 	  .idVendor		= 0x05ac,
2759 	  .idProduct		= 0x8501,
2760 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2761 	  .bInterfaceSubClass	= 1,
2762 	  .bInterfaceProtocol	= 0,
2763 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
2764 					| UVC_QUIRK_BUILTIN_ISIGHT) },
2765 	/* Apple FaceTime HD Camera (Built-In) */
2766 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2767 				| USB_DEVICE_ID_MATCH_INT_INFO,
2768 	  .idVendor		= 0x05ac,
2769 	  .idProduct		= 0x8514,
2770 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2771 	  .bInterfaceSubClass	= 1,
2772 	  .bInterfaceProtocol	= 0,
2773 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2774 	/* Apple Built-In iSight via iBridge */
2775 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2776 				| USB_DEVICE_ID_MATCH_INT_INFO,
2777 	  .idVendor		= 0x05ac,
2778 	  .idProduct		= 0x8600,
2779 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2780 	  .bInterfaceSubClass	= 1,
2781 	  .bInterfaceProtocol	= 0,
2782 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2783 	/* Foxlink ("HP Webcam" on HP Mini 5103) */
2784 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2785 				| USB_DEVICE_ID_MATCH_INT_INFO,
2786 	  .idVendor		= 0x05c8,
2787 	  .idProduct		= 0x0403,
2788 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2789 	  .bInterfaceSubClass	= 1,
2790 	  .bInterfaceProtocol	= 0,
2791 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2792 	/* Genesys Logic USB 2.0 PC Camera */
2793 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2794 				| USB_DEVICE_ID_MATCH_INT_INFO,
2795 	  .idVendor		= 0x05e3,
2796 	  .idProduct		= 0x0505,
2797 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2798 	  .bInterfaceSubClass	= 1,
2799 	  .bInterfaceProtocol	= 0,
2800 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2801 	/* Hercules Classic Silver */
2802 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2803 				| USB_DEVICE_ID_MATCH_INT_INFO,
2804 	  .idVendor		= 0x06f8,
2805 	  .idProduct		= 0x300c,
2806 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2807 	  .bInterfaceSubClass	= 1,
2808 	  .bInterfaceProtocol	= 0,
2809 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2810 	/* ViMicro Vega */
2811 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2812 				| USB_DEVICE_ID_MATCH_INT_INFO,
2813 	  .idVendor		= 0x0ac8,
2814 	  .idProduct		= 0x332d,
2815 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2816 	  .bInterfaceSubClass	= 1,
2817 	  .bInterfaceProtocol	= 0,
2818 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2819 	/* ViMicro - Minoru3D */
2820 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2821 				| USB_DEVICE_ID_MATCH_INT_INFO,
2822 	  .idVendor		= 0x0ac8,
2823 	  .idProduct		= 0x3410,
2824 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2825 	  .bInterfaceSubClass	= 1,
2826 	  .bInterfaceProtocol	= 0,
2827 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2828 	/* ViMicro Venus - Minoru3D */
2829 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2830 				| USB_DEVICE_ID_MATCH_INT_INFO,
2831 	  .idVendor		= 0x0ac8,
2832 	  .idProduct		= 0x3420,
2833 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2834 	  .bInterfaceSubClass	= 1,
2835 	  .bInterfaceProtocol	= 0,
2836 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2837 	/* Ophir Optronics - SPCAM 620U */
2838 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2839 				| USB_DEVICE_ID_MATCH_INT_INFO,
2840 	  .idVendor		= 0x0bd3,
2841 	  .idProduct		= 0x0555,
2842 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2843 	  .bInterfaceSubClass	= 1,
2844 	  .bInterfaceProtocol	= 0,
2845 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2846 	/* Sonix Technology Co. Ltd. - 292A IPC AR0330 */
2847 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2848 				| USB_DEVICE_ID_MATCH_INT_INFO,
2849 	  .idVendor		= 0x0c45,
2850 	  .idProduct		= 0x6366,
2851 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2852 	  .bInterfaceSubClass	= 1,
2853 	  .bInterfaceProtocol	= 0,
2854 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_MJPEG_NO_EOF) },
2855 	/* MT6227 */
2856 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2857 				| USB_DEVICE_ID_MATCH_INT_INFO,
2858 	  .idVendor		= 0x0e8d,
2859 	  .idProduct		= 0x0004,
2860 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2861 	  .bInterfaceSubClass	= 1,
2862 	  .bInterfaceProtocol	= 0,
2863 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
2864 					| UVC_QUIRK_PROBE_DEF) },
2865 	/* IMC Networks (Medion Akoya) */
2866 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2867 				| USB_DEVICE_ID_MATCH_INT_INFO,
2868 	  .idVendor		= 0x13d3,
2869 	  .idProduct		= 0x5103,
2870 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2871 	  .bInterfaceSubClass	= 1,
2872 	  .bInterfaceProtocol	= 0,
2873 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2874 	/* JMicron USB2.0 XGA WebCam */
2875 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2876 				| USB_DEVICE_ID_MATCH_INT_INFO,
2877 	  .idVendor		= 0x152d,
2878 	  .idProduct		= 0x0310,
2879 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2880 	  .bInterfaceSubClass	= 1,
2881 	  .bInterfaceProtocol	= 0,
2882 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2883 	/* Kurokesu C1 PRO */
2884 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2885 				| USB_DEVICE_ID_MATCH_INT_INFO,
2886 	  .idVendor		= 0x16d0,
2887 	  .idProduct		= 0x0ed1,
2888 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2889 	  .bInterfaceSubClass	= 1,
2890 	  .bInterfaceProtocol	= 0,
2891 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_MJPEG_NO_EOF) },
2892 	/* Syntek (HP Spartan) */
2893 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2894 				| USB_DEVICE_ID_MATCH_INT_INFO,
2895 	  .idVendor		= 0x174f,
2896 	  .idProduct		= 0x5212,
2897 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2898 	  .bInterfaceSubClass	= 1,
2899 	  .bInterfaceProtocol	= 0,
2900 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2901 	/* Syntek (Samsung Q310) */
2902 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2903 				| USB_DEVICE_ID_MATCH_INT_INFO,
2904 	  .idVendor		= 0x174f,
2905 	  .idProduct		= 0x5931,
2906 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2907 	  .bInterfaceSubClass	= 1,
2908 	  .bInterfaceProtocol	= 0,
2909 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2910 	/* Syntek (Packard Bell EasyNote MX52 */
2911 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2912 				| USB_DEVICE_ID_MATCH_INT_INFO,
2913 	  .idVendor		= 0x174f,
2914 	  .idProduct		= 0x8a12,
2915 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2916 	  .bInterfaceSubClass	= 1,
2917 	  .bInterfaceProtocol	= 0,
2918 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2919 	/* Syntek (Asus F9SG) */
2920 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2921 				| USB_DEVICE_ID_MATCH_INT_INFO,
2922 	  .idVendor		= 0x174f,
2923 	  .idProduct		= 0x8a31,
2924 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2925 	  .bInterfaceSubClass	= 1,
2926 	  .bInterfaceProtocol	= 0,
2927 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2928 	/* Syntek (Asus U3S) */
2929 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2930 				| USB_DEVICE_ID_MATCH_INT_INFO,
2931 	  .idVendor		= 0x174f,
2932 	  .idProduct		= 0x8a33,
2933 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2934 	  .bInterfaceSubClass	= 1,
2935 	  .bInterfaceProtocol	= 0,
2936 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2937 	/* Syntek (JAOtech Smart Terminal) */
2938 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2939 				| USB_DEVICE_ID_MATCH_INT_INFO,
2940 	  .idVendor		= 0x174f,
2941 	  .idProduct		= 0x8a34,
2942 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2943 	  .bInterfaceSubClass	= 1,
2944 	  .bInterfaceProtocol	= 0,
2945 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2946 	/* Miricle 307K */
2947 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2948 				| USB_DEVICE_ID_MATCH_INT_INFO,
2949 	  .idVendor		= 0x17dc,
2950 	  .idProduct		= 0x0202,
2951 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2952 	  .bInterfaceSubClass	= 1,
2953 	  .bInterfaceProtocol	= 0,
2954 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2955 	/* Lenovo Thinkpad SL400/SL500 */
2956 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2957 				| USB_DEVICE_ID_MATCH_INT_INFO,
2958 	  .idVendor		= 0x17ef,
2959 	  .idProduct		= 0x480b,
2960 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2961 	  .bInterfaceSubClass	= 1,
2962 	  .bInterfaceProtocol	= 0,
2963 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2964 	/* Aveo Technology USB 2.0 Camera */
2965 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2966 				| USB_DEVICE_ID_MATCH_INT_INFO,
2967 	  .idVendor		= 0x1871,
2968 	  .idProduct		= 0x0306,
2969 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2970 	  .bInterfaceSubClass	= 1,
2971 	  .bInterfaceProtocol	= 0,
2972 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
2973 					| UVC_QUIRK_PROBE_EXTRAFIELDS) },
2974 	/* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */
2975 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2976 				| USB_DEVICE_ID_MATCH_INT_INFO,
2977 	  .idVendor		= 0x1871,
2978 	  .idProduct		= 0x0516,
2979 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
2980 	  .bInterfaceSubClass	= 1,
2981 	  .bInterfaceProtocol	= 0 },
2982 	/* Ecamm Pico iMage */
2983 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2984 				| USB_DEVICE_ID_MATCH_INT_INFO,
2985 	  .idVendor		= 0x18cd,
2986 	  .idProduct		= 0xcafe,
2987 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2988 	  .bInterfaceSubClass	= 1,
2989 	  .bInterfaceProtocol	= 0,
2990 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_EXTRAFIELDS) },
2991 	/* Manta MM-353 Plako */
2992 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2993 				| USB_DEVICE_ID_MATCH_INT_INFO,
2994 	  .idVendor		= 0x18ec,
2995 	  .idProduct		= 0x3188,
2996 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2997 	  .bInterfaceSubClass	= 1,
2998 	  .bInterfaceProtocol	= 0,
2999 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3000 	/* FSC WebCam V30S */
3001 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3002 				| USB_DEVICE_ID_MATCH_INT_INFO,
3003 	  .idVendor		= 0x18ec,
3004 	  .idProduct		= 0x3288,
3005 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3006 	  .bInterfaceSubClass	= 1,
3007 	  .bInterfaceProtocol	= 0,
3008 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3009 	/* Arkmicro unbranded */
3010 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3011 				| USB_DEVICE_ID_MATCH_INT_INFO,
3012 	  .idVendor		= 0x18ec,
3013 	  .idProduct		= 0x3290,
3014 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3015 	  .bInterfaceSubClass	= 1,
3016 	  .bInterfaceProtocol	= 0,
3017 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
3018 	/* The Imaging Source USB CCD cameras */
3019 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3020 				| USB_DEVICE_ID_MATCH_INT_INFO,
3021 	  .idVendor		= 0x199e,
3022 	  .idProduct		= 0x8102,
3023 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
3024 	  .bInterfaceSubClass	= 1,
3025 	  .bInterfaceProtocol	= 0 },
3026 	/* Bodelin ProScopeHR */
3027 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3028 				| USB_DEVICE_ID_MATCH_DEV_HI
3029 				| USB_DEVICE_ID_MATCH_INT_INFO,
3030 	  .idVendor		= 0x19ab,
3031 	  .idProduct		= 0x1000,
3032 	  .bcdDevice_hi		= 0x0126,
3033 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3034 	  .bInterfaceSubClass	= 1,
3035 	  .bInterfaceProtocol	= 0,
3036 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_STATUS_INTERVAL) },
3037 	/* MSI StarCam 370i */
3038 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3039 				| USB_DEVICE_ID_MATCH_INT_INFO,
3040 	  .idVendor		= 0x1b3b,
3041 	  .idProduct		= 0x2951,
3042 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3043 	  .bInterfaceSubClass	= 1,
3044 	  .bInterfaceProtocol	= 0,
3045 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3046 	/* Generalplus Technology Inc. 808 Camera */
3047 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3048 				| USB_DEVICE_ID_MATCH_INT_INFO,
3049 	  .idVendor		= 0x1b3f,
3050 	  .idProduct		= 0x2002,
3051 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3052 	  .bInterfaceSubClass	= 1,
3053 	  .bInterfaceProtocol	= 0,
3054 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3055 	/* Shenzhen Aoni Electronic Co.,Ltd 2K FHD camera */
3056 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3057 				| USB_DEVICE_ID_MATCH_INT_INFO,
3058 	  .idVendor		= 0x1bcf,
3059 	  .idProduct		= 0x0b40,
3060 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3061 	  .bInterfaceSubClass	= 1,
3062 	  .bInterfaceProtocol	= 0,
3063 	  .driver_info		= (kernel_ulong_t)&(const struct uvc_device_info){
3064 		.uvc_version = 0x010a,
3065 	  } },
3066 	/* SiGma Micro USB Web Camera */
3067 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3068 				| USB_DEVICE_ID_MATCH_INT_INFO,
3069 	  .idVendor		= 0x1c4f,
3070 	  .idProduct		= 0x3000,
3071 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3072 	  .bInterfaceSubClass	= 1,
3073 	  .bInterfaceProtocol	= 0,
3074 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
3075 					| UVC_QUIRK_IGNORE_SELECTOR_UNIT) },
3076 	/* Actions Microelectronics Co. Display capture-UVC05 */
3077 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3078 				| USB_DEVICE_ID_MATCH_INT_INFO,
3079 	  .idVendor		= 0x1de1,
3080 	  .idProduct		= 0xf105,
3081 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3082 	  .bInterfaceSubClass	= 1,
3083 	  .bInterfaceProtocol	= 0,
3084 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_DISABLE_AUTOSUSPEND) },
3085 	/* NXP Semiconductors IR VIDEO */
3086 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3087 				| USB_DEVICE_ID_MATCH_INT_INFO,
3088 	  .idVendor		= 0x1fc9,
3089 	  .idProduct		= 0x009b,
3090 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3091 	  .bInterfaceSubClass	= 1,
3092 	  .bInterfaceProtocol	= 0,
3093 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3094 	/* Oculus VR Positional Tracker DK2 */
3095 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3096 				| USB_DEVICE_ID_MATCH_INT_INFO,
3097 	  .idVendor		= 0x2833,
3098 	  .idProduct		= 0x0201,
3099 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3100 	  .bInterfaceSubClass	= 1,
3101 	  .bInterfaceProtocol	= 0,
3102 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_force_y8 },
3103 	/* Oculus VR Rift Sensor */
3104 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3105 				| USB_DEVICE_ID_MATCH_INT_INFO,
3106 	  .idVendor		= 0x2833,
3107 	  .idProduct		= 0x0211,
3108 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
3109 	  .bInterfaceSubClass	= 1,
3110 	  .bInterfaceProtocol	= 0,
3111 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_force_y8 },
3112 	/* GEO Semiconductor GC6500 */
3113 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3114 				| USB_DEVICE_ID_MATCH_INT_INFO,
3115 	  .idVendor		= 0x29fe,
3116 	  .idProduct		= 0x4d53,
3117 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3118 	  .bInterfaceSubClass	= 1,
3119 	  .bInterfaceProtocol	= 0,
3120 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_FORCE_BPP) },
3121 	/* Insta360 Link */
3122 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3123 				| USB_DEVICE_ID_MATCH_INT_INFO,
3124 	  .idVendor		= 0x2e1a,
3125 	  .idProduct		= 0x4c01,
3126 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3127 	  .bInterfaceSubClass	= 1,
3128 	  .bInterfaceProtocol	= 0,
3129 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_DISABLE_AUTOSUSPEND) },
3130 	/* Intel D410/ASR depth camera */
3131 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3132 				| USB_DEVICE_ID_MATCH_INT_INFO,
3133 	  .idVendor		= 0x8086,
3134 	  .idProduct		= 0x0ad2,
3135 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3136 	  .bInterfaceSubClass	= 1,
3137 	  .bInterfaceProtocol	= 0,
3138 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3139 	/* Intel D415/ASRC depth camera */
3140 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3141 				| USB_DEVICE_ID_MATCH_INT_INFO,
3142 	  .idVendor		= 0x8086,
3143 	  .idProduct		= 0x0ad3,
3144 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3145 	  .bInterfaceSubClass	= 1,
3146 	  .bInterfaceProtocol	= 0,
3147 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3148 	/* Intel D430/AWG depth camera */
3149 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3150 				| USB_DEVICE_ID_MATCH_INT_INFO,
3151 	  .idVendor		= 0x8086,
3152 	  .idProduct		= 0x0ad4,
3153 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3154 	  .bInterfaceSubClass	= 1,
3155 	  .bInterfaceProtocol	= 0,
3156 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3157 	/* Intel RealSense D4M */
3158 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3159 				| USB_DEVICE_ID_MATCH_INT_INFO,
3160 	  .idVendor		= 0x8086,
3161 	  .idProduct		= 0x0b03,
3162 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3163 	  .bInterfaceSubClass	= 1,
3164 	  .bInterfaceProtocol	= 0,
3165 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3166 	/* Intel D435/AWGC depth camera */
3167 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3168 				| USB_DEVICE_ID_MATCH_INT_INFO,
3169 	  .idVendor		= 0x8086,
3170 	  .idProduct		= 0x0b07,
3171 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3172 	  .bInterfaceSubClass	= 1,
3173 	  .bInterfaceProtocol	= 0,
3174 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3175 	/* Intel D435i depth camera */
3176 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3177 				| USB_DEVICE_ID_MATCH_INT_INFO,
3178 	  .idVendor		= 0x8086,
3179 	  .idProduct		= 0x0b3a,
3180 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3181 	  .bInterfaceSubClass	= 1,
3182 	  .bInterfaceProtocol	= 0,
3183 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3184 	/* Intel D405 Depth Camera */
3185 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3186 				| USB_DEVICE_ID_MATCH_INT_INFO,
3187 	  .idVendor		= 0x8086,
3188 	  .idProduct		= 0x0b5b,
3189 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3190 	  .bInterfaceSubClass	= 1,
3191 	  .bInterfaceProtocol	= 0,
3192 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3193 	/* Intel D455 Depth Camera */
3194 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3195 				| USB_DEVICE_ID_MATCH_INT_INFO,
3196 	  .idVendor		= 0x8086,
3197 	  .idProduct		= 0x0b5c,
3198 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3199 	  .bInterfaceSubClass	= 1,
3200 	  .bInterfaceProtocol	= 0,
3201 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3202 	/* Intel D421 Depth Module */
3203 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3204 				| USB_DEVICE_ID_MATCH_INT_INFO,
3205 	  .idVendor		= 0x8086,
3206 	  .idProduct		= 0x1155,
3207 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3208 	  .bInterfaceSubClass	= 1,
3209 	  .bInterfaceProtocol	= 0,
3210 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3211 	/* Generic USB Video Class */
3212 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_UNDEFINED) },
3213 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_15) },
3214 	{}
3215 };
3216 
3217 MODULE_DEVICE_TABLE(usb, uvc_ids);
3218 
3219 static struct usb_driver uvc_driver = {
3220 	.name		= "uvcvideo",
3221 	.probe		= uvc_probe,
3222 	.disconnect	= uvc_disconnect,
3223 	.suspend	= uvc_suspend,
3224 	.resume		= uvc_resume,
3225 	.reset_resume	= uvc_reset_resume,
3226 	.id_table	= uvc_ids,
3227 	.supports_autosuspend = 1,
3228 };
3229 
3230 static int __init uvc_init(void)
3231 {
3232 	int ret;
3233 
3234 	uvc_debugfs_init();
3235 
3236 	ret = usb_register(&uvc_driver);
3237 	if (ret < 0) {
3238 		uvc_debugfs_cleanup();
3239 		return ret;
3240 	}
3241 
3242 	return 0;
3243 }
3244 
3245 static void __exit uvc_cleanup(void)
3246 {
3247 	usb_deregister(&uvc_driver);
3248 	uvc_debugfs_cleanup();
3249 }
3250 
3251 module_init(uvc_init);
3252 module_exit(uvc_cleanup);
3253 
3254 MODULE_AUTHOR(DRIVER_AUTHOR);
3255 MODULE_DESCRIPTION(DRIVER_DESC);
3256 MODULE_LICENSE("GPL");
3257 MODULE_VERSION(DRIVER_VERSION);
3258 
3259