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