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