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