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