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