Commit | Line | Data |
---|---|---|
c0efd232 LP |
1 | /* |
2 | * uvc_driver.c -- USB Video Class driver | |
3 | * | |
2c2d264b | 4 | * Copyright (C) 2005-2009 |
c0efd232 LP |
5 | * Laurent Pinchart (laurent.pinchart@skynet.be) |
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 | /* | |
ff924203 LP |
15 | * This driver aims to support video input and ouput devices compliant with the |
16 | * 'USB Video Class' specification. | |
c0efd232 LP |
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> | |
c0efd232 LP |
27 | #include <linux/list.h> |
28 | #include <linux/module.h> | |
29 | #include <linux/usb.h> | |
30 | #include <linux/videodev2.h> | |
31 | #include <linux/vmalloc.h> | |
32 | #include <linux/wait.h> | |
33 | #include <asm/atomic.h> | |
9bc6218d | 34 | #include <asm/unaligned.h> |
c0efd232 LP |
35 | |
36 | #include <media/v4l2-common.h> | |
37 | ||
38 | #include "uvcvideo.h" | |
39 | ||
40 | #define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>" | |
41 | #define DRIVER_DESC "USB Video Class driver" | |
42 | #ifndef DRIVER_VERSION | |
43 | #define DRIVER_VERSION "v0.1.0" | |
44 | #endif | |
45 | ||
0fbd8ee6 | 46 | unsigned int uvc_no_drop_param; |
c0efd232 LP |
47 | static unsigned int uvc_quirks_param; |
48 | unsigned int uvc_trace_param; | |
49 | ||
50 | /* ------------------------------------------------------------------------ | |
2c2d264b | 51 | * Video formats |
c0efd232 LP |
52 | */ |
53 | ||
54 | static struct uvc_format_desc uvc_fmts[] = { | |
55 | { | |
56 | .name = "YUV 4:2:2 (YUYV)", | |
57 | .guid = UVC_GUID_FORMAT_YUY2, | |
58 | .fcc = V4L2_PIX_FMT_YUYV, | |
59 | }, | |
60 | { | |
61 | .name = "YUV 4:2:0 (NV12)", | |
62 | .guid = UVC_GUID_FORMAT_NV12, | |
63 | .fcc = V4L2_PIX_FMT_NV12, | |
64 | }, | |
65 | { | |
66 | .name = "MJPEG", | |
67 | .guid = UVC_GUID_FORMAT_MJPEG, | |
68 | .fcc = V4L2_PIX_FMT_MJPEG, | |
69 | }, | |
70 | { | |
71 | .name = "YVU 4:2:0 (YV12)", | |
72 | .guid = UVC_GUID_FORMAT_YV12, | |
73 | .fcc = V4L2_PIX_FMT_YVU420, | |
74 | }, | |
75 | { | |
76 | .name = "YUV 4:2:0 (I420)", | |
77 | .guid = UVC_GUID_FORMAT_I420, | |
78 | .fcc = V4L2_PIX_FMT_YUV420, | |
79 | }, | |
80 | { | |
81 | .name = "YUV 4:2:2 (UYVY)", | |
82 | .guid = UVC_GUID_FORMAT_UYVY, | |
83 | .fcc = V4L2_PIX_FMT_UYVY, | |
84 | }, | |
85 | { | |
86 | .name = "Greyscale", | |
87 | .guid = UVC_GUID_FORMAT_Y800, | |
88 | .fcc = V4L2_PIX_FMT_GREY, | |
89 | }, | |
90 | { | |
91 | .name = "RGB Bayer", | |
92 | .guid = UVC_GUID_FORMAT_BY8, | |
93 | .fcc = V4L2_PIX_FMT_SBGGR8, | |
94 | }, | |
95 | }; | |
96 | ||
97 | /* ------------------------------------------------------------------------ | |
98 | * Utility functions | |
99 | */ | |
100 | ||
101 | struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts, | |
102 | __u8 epaddr) | |
103 | { | |
104 | struct usb_host_endpoint *ep; | |
105 | unsigned int i; | |
106 | ||
107 | for (i = 0; i < alts->desc.bNumEndpoints; ++i) { | |
108 | ep = &alts->endpoint[i]; | |
109 | if (ep->desc.bEndpointAddress == epaddr) | |
110 | return ep; | |
111 | } | |
112 | ||
113 | return NULL; | |
114 | } | |
115 | ||
116 | static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16]) | |
117 | { | |
118 | unsigned int len = ARRAY_SIZE(uvc_fmts); | |
119 | unsigned int i; | |
120 | ||
121 | for (i = 0; i < len; ++i) { | |
122 | if (memcmp(guid, uvc_fmts[i].guid, 16) == 0) | |
123 | return &uvc_fmts[i]; | |
124 | } | |
125 | ||
126 | return NULL; | |
127 | } | |
128 | ||
129 | static __u32 uvc_colorspace(const __u8 primaries) | |
130 | { | |
131 | static const __u8 colorprimaries[] = { | |
132 | 0, | |
133 | V4L2_COLORSPACE_SRGB, | |
134 | V4L2_COLORSPACE_470_SYSTEM_M, | |
135 | V4L2_COLORSPACE_470_SYSTEM_BG, | |
136 | V4L2_COLORSPACE_SMPTE170M, | |
137 | V4L2_COLORSPACE_SMPTE240M, | |
138 | }; | |
139 | ||
140 | if (primaries < ARRAY_SIZE(colorprimaries)) | |
141 | return colorprimaries[primaries]; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | /* Simplify a fraction using a simple continued fraction decomposition. The | |
147 | * idea here is to convert fractions such as 333333/10000000 to 1/30 using | |
148 | * 32 bit arithmetic only. The algorithm is not perfect and relies upon two | |
149 | * arbitrary parameters to remove non-significative terms from the simple | |
150 | * continued fraction decomposition. Using 8 and 333 for n_terms and threshold | |
151 | * respectively seems to give nice results. | |
152 | */ | |
153 | void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator, | |
154 | unsigned int n_terms, unsigned int threshold) | |
155 | { | |
156 | uint32_t *an; | |
157 | uint32_t x, y, r; | |
158 | unsigned int i, n; | |
159 | ||
160 | an = kmalloc(n_terms * sizeof *an, GFP_KERNEL); | |
161 | if (an == NULL) | |
162 | return; | |
163 | ||
164 | /* Convert the fraction to a simple continued fraction. See | |
165 | * http://mathforum.org/dr.math/faq/faq.fractions.html | |
166 | * Stop if the current term is bigger than or equal to the given | |
167 | * threshold. | |
168 | */ | |
169 | x = *numerator; | |
170 | y = *denominator; | |
171 | ||
172 | for (n = 0; n < n_terms && y != 0; ++n) { | |
173 | an[n] = x / y; | |
174 | if (an[n] >= threshold) { | |
175 | if (n < 2) | |
176 | n++; | |
177 | break; | |
178 | } | |
179 | ||
180 | r = x - an[n] * y; | |
181 | x = y; | |
182 | y = r; | |
183 | } | |
184 | ||
185 | /* Expand the simple continued fraction back to an integer fraction. */ | |
186 | x = 0; | |
187 | y = 1; | |
188 | ||
189 | for (i = n; i > 0; --i) { | |
190 | r = y; | |
191 | y = an[i-1] * y + x; | |
192 | x = r; | |
193 | } | |
194 | ||
195 | *numerator = y; | |
196 | *denominator = x; | |
197 | kfree(an); | |
198 | } | |
199 | ||
200 | /* Convert a fraction to a frame interval in 100ns multiples. The idea here is | |
201 | * to compute numerator / denominator * 10000000 using 32 bit fixed point | |
202 | * arithmetic only. | |
203 | */ | |
204 | uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator) | |
205 | { | |
206 | uint32_t multiplier; | |
207 | ||
208 | /* Saturate the result if the operation would overflow. */ | |
209 | if (denominator == 0 || | |
210 | numerator/denominator >= ((uint32_t)-1)/10000000) | |
211 | return (uint32_t)-1; | |
212 | ||
213 | /* Divide both the denominator and the multiplier by two until | |
214 | * numerator * multiplier doesn't overflow. If anyone knows a better | |
215 | * algorithm please let me know. | |
216 | */ | |
217 | multiplier = 10000000; | |
218 | while (numerator > ((uint32_t)-1)/multiplier) { | |
219 | multiplier /= 2; | |
220 | denominator /= 2; | |
221 | } | |
222 | ||
223 | return denominator ? numerator * multiplier / denominator : 0; | |
224 | } | |
225 | ||
226 | /* ------------------------------------------------------------------------ | |
227 | * Terminal and unit management | |
228 | */ | |
229 | ||
230 | static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id) | |
231 | { | |
232 | struct uvc_entity *entity; | |
233 | ||
234 | list_for_each_entry(entity, &dev->entities, list) { | |
235 | if (entity->id == id) | |
236 | return entity; | |
237 | } | |
238 | ||
239 | return NULL; | |
240 | } | |
241 | ||
242 | static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev, | |
243 | int id, struct uvc_entity *entity) | |
244 | { | |
245 | unsigned int i; | |
246 | ||
247 | if (entity == NULL) | |
248 | entity = list_entry(&dev->entities, struct uvc_entity, list); | |
249 | ||
250 | list_for_each_entry_continue(entity, &dev->entities, list) { | |
251 | switch (UVC_ENTITY_TYPE(entity)) { | |
252 | case TT_STREAMING: | |
253 | if (entity->output.bSourceID == id) | |
254 | return entity; | |
255 | break; | |
256 | ||
257 | case VC_PROCESSING_UNIT: | |
258 | if (entity->processing.bSourceID == id) | |
259 | return entity; | |
260 | break; | |
261 | ||
262 | case VC_SELECTOR_UNIT: | |
263 | for (i = 0; i < entity->selector.bNrInPins; ++i) | |
264 | if (entity->selector.baSourceID[i] == id) | |
265 | return entity; | |
266 | break; | |
267 | ||
268 | case VC_EXTENSION_UNIT: | |
269 | for (i = 0; i < entity->extension.bNrInPins; ++i) | |
270 | if (entity->extension.baSourceID[i] == id) | |
271 | return entity; | |
272 | break; | |
273 | } | |
274 | } | |
275 | ||
276 | return NULL; | |
277 | } | |
278 | ||
279 | /* ------------------------------------------------------------------------ | |
280 | * Descriptors handling | |
281 | */ | |
282 | ||
283 | static int uvc_parse_format(struct uvc_device *dev, | |
284 | struct uvc_streaming *streaming, struct uvc_format *format, | |
285 | __u32 **intervals, unsigned char *buffer, int buflen) | |
286 | { | |
287 | struct usb_interface *intf = streaming->intf; | |
288 | struct usb_host_interface *alts = intf->cur_altsetting; | |
289 | struct uvc_format_desc *fmtdesc; | |
290 | struct uvc_frame *frame; | |
291 | const unsigned char *start = buffer; | |
292 | unsigned int interval; | |
293 | unsigned int i, n; | |
294 | __u8 ftype; | |
295 | ||
296 | format->type = buffer[2]; | |
297 | format->index = buffer[3]; | |
298 | ||
299 | switch (buffer[2]) { | |
300 | case VS_FORMAT_UNCOMPRESSED: | |
301 | case VS_FORMAT_FRAME_BASED: | |
233548a2 LP |
302 | n = buffer[2] == VS_FORMAT_UNCOMPRESSED ? 27 : 28; |
303 | if (buflen < n) { | |
b2d9cc42 | 304 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " |
c0efd232 LP |
305 | "interface %d FORMAT error\n", |
306 | dev->udev->devnum, | |
307 | alts->desc.bInterfaceNumber); | |
308 | return -EINVAL; | |
309 | } | |
310 | ||
311 | /* Find the format descriptor from its GUID. */ | |
312 | fmtdesc = uvc_format_by_guid(&buffer[5]); | |
313 | ||
314 | if (fmtdesc != NULL) { | |
d0ebf307 | 315 | strlcpy(format->name, fmtdesc->name, |
c0efd232 LP |
316 | sizeof format->name); |
317 | format->fcc = fmtdesc->fcc; | |
318 | } else { | |
319 | uvc_printk(KERN_INFO, "Unknown video format " | |
320 | UVC_GUID_FORMAT "\n", | |
321 | UVC_GUID_ARGS(&buffer[5])); | |
322 | snprintf(format->name, sizeof format->name, | |
323 | UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5])); | |
324 | format->fcc = 0; | |
325 | } | |
326 | ||
327 | format->bpp = buffer[21]; | |
328 | if (buffer[2] == VS_FORMAT_UNCOMPRESSED) { | |
329 | ftype = VS_FRAME_UNCOMPRESSED; | |
330 | } else { | |
331 | ftype = VS_FRAME_FRAME_BASED; | |
332 | if (buffer[27]) | |
333 | format->flags = UVC_FMT_FLAG_COMPRESSED; | |
334 | } | |
335 | break; | |
336 | ||
337 | case VS_FORMAT_MJPEG: | |
338 | if (buflen < 11) { | |
b2d9cc42 | 339 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " |
c0efd232 LP |
340 | "interface %d FORMAT error\n", |
341 | dev->udev->devnum, | |
342 | alts->desc.bInterfaceNumber); | |
343 | return -EINVAL; | |
344 | } | |
345 | ||
d0ebf307 | 346 | strlcpy(format->name, "MJPEG", sizeof format->name); |
c0efd232 LP |
347 | format->fcc = V4L2_PIX_FMT_MJPEG; |
348 | format->flags = UVC_FMT_FLAG_COMPRESSED; | |
349 | format->bpp = 0; | |
350 | ftype = VS_FRAME_MJPEG; | |
351 | break; | |
352 | ||
353 | case VS_FORMAT_DV: | |
354 | if (buflen < 9) { | |
b2d9cc42 | 355 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " |
c0efd232 LP |
356 | "interface %d FORMAT error\n", |
357 | dev->udev->devnum, | |
358 | alts->desc.bInterfaceNumber); | |
359 | return -EINVAL; | |
360 | } | |
361 | ||
362 | switch (buffer[8] & 0x7f) { | |
363 | case 0: | |
d0ebf307 | 364 | strlcpy(format->name, "SD-DV", sizeof format->name); |
c0efd232 LP |
365 | break; |
366 | case 1: | |
d0ebf307 | 367 | strlcpy(format->name, "SDL-DV", sizeof format->name); |
c0efd232 LP |
368 | break; |
369 | case 2: | |
d0ebf307 | 370 | strlcpy(format->name, "HD-DV", sizeof format->name); |
c0efd232 LP |
371 | break; |
372 | default: | |
b2d9cc42 | 373 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " |
c0efd232 LP |
374 | "interface %d: unknown DV format %u\n", |
375 | dev->udev->devnum, | |
376 | alts->desc.bInterfaceNumber, buffer[8]); | |
377 | return -EINVAL; | |
378 | } | |
379 | ||
d0ebf307 | 380 | strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz", |
c0efd232 LP |
381 | sizeof format->name); |
382 | ||
383 | format->fcc = V4L2_PIX_FMT_DV; | |
384 | format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM; | |
385 | format->bpp = 0; | |
386 | ftype = 0; | |
387 | ||
388 | /* Create a dummy frame descriptor. */ | |
389 | frame = &format->frame[0]; | |
390 | memset(&format->frame[0], 0, sizeof format->frame[0]); | |
391 | frame->bFrameIntervalType = 1; | |
392 | frame->dwDefaultFrameInterval = 1; | |
393 | frame->dwFrameInterval = *intervals; | |
394 | *(*intervals)++ = 1; | |
395 | format->nframes = 1; | |
396 | break; | |
397 | ||
398 | case VS_FORMAT_MPEG2TS: | |
399 | case VS_FORMAT_STREAM_BASED: | |
400 | /* Not supported yet. */ | |
401 | default: | |
b2d9cc42 | 402 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " |
c0efd232 LP |
403 | "interface %d unsupported format %u\n", |
404 | dev->udev->devnum, alts->desc.bInterfaceNumber, | |
405 | buffer[2]); | |
406 | return -EINVAL; | |
407 | } | |
408 | ||
409 | uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name); | |
410 | ||
411 | buflen -= buffer[0]; | |
412 | buffer += buffer[0]; | |
413 | ||
414 | /* Parse the frame descriptors. Only uncompressed, MJPEG and frame | |
415 | * based formats have frame descriptors. | |
416 | */ | |
417 | while (buflen > 2 && buffer[2] == ftype) { | |
078f8947 | 418 | frame = &format->frame[format->nframes]; |
c0efd232 LP |
419 | if (ftype != VS_FRAME_FRAME_BASED) |
420 | n = buflen > 25 ? buffer[25] : 0; | |
421 | else | |
422 | n = buflen > 21 ? buffer[21] : 0; | |
423 | ||
424 | n = n ? n : 3; | |
425 | ||
426 | if (buflen < 26 + 4*n) { | |
b2d9cc42 | 427 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " |
c0efd232 LP |
428 | "interface %d FRAME error\n", dev->udev->devnum, |
429 | alts->desc.bInterfaceNumber); | |
430 | return -EINVAL; | |
431 | } | |
432 | ||
433 | frame->bFrameIndex = buffer[3]; | |
434 | frame->bmCapabilities = buffer[4]; | |
9bc6218d MH |
435 | frame->wWidth = get_unaligned_le16(&buffer[5]); |
436 | frame->wHeight = get_unaligned_le16(&buffer[7]); | |
437 | frame->dwMinBitRate = get_unaligned_le32(&buffer[9]); | |
438 | frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]); | |
c0efd232 LP |
439 | if (ftype != VS_FRAME_FRAME_BASED) { |
440 | frame->dwMaxVideoFrameBufferSize = | |
9bc6218d | 441 | get_unaligned_le32(&buffer[17]); |
c0efd232 | 442 | frame->dwDefaultFrameInterval = |
9bc6218d | 443 | get_unaligned_le32(&buffer[21]); |
c0efd232 LP |
444 | frame->bFrameIntervalType = buffer[25]; |
445 | } else { | |
446 | frame->dwMaxVideoFrameBufferSize = 0; | |
447 | frame->dwDefaultFrameInterval = | |
9bc6218d | 448 | get_unaligned_le32(&buffer[17]); |
c0efd232 LP |
449 | frame->bFrameIntervalType = buffer[21]; |
450 | } | |
451 | frame->dwFrameInterval = *intervals; | |
452 | ||
453 | /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize | |
454 | * completely. Observed behaviours range from setting the | |
2c2d264b | 455 | * value to 1.1x the actual frame size to hardwiring the |
c0efd232 LP |
456 | * 16 low bits to 0. This results in a higher than necessary |
457 | * memory usage as well as a wrong image size information. For | |
458 | * uncompressed formats this can be fixed by computing the | |
459 | * value from the frame size. | |
460 | */ | |
461 | if (!(format->flags & UVC_FMT_FLAG_COMPRESSED)) | |
462 | frame->dwMaxVideoFrameBufferSize = format->bpp | |
463 | * frame->wWidth * frame->wHeight / 8; | |
464 | ||
465 | /* Some bogus devices report dwMinFrameInterval equal to | |
466 | * dwMaxFrameInterval and have dwFrameIntervalStep set to | |
467 | * zero. Setting all null intervals to 1 fixes the problem and | |
2c2d264b | 468 | * some other divisions by zero that could happen. |
c0efd232 LP |
469 | */ |
470 | for (i = 0; i < n; ++i) { | |
9bc6218d | 471 | interval = get_unaligned_le32(&buffer[26+4*i]); |
c0efd232 LP |
472 | *(*intervals)++ = interval ? interval : 1; |
473 | } | |
474 | ||
475 | /* Make sure that the default frame interval stays between | |
476 | * the boundaries. | |
477 | */ | |
478 | n -= frame->bFrameIntervalType ? 1 : 2; | |
479 | frame->dwDefaultFrameInterval = | |
480 | min(frame->dwFrameInterval[n], | |
481 | max(frame->dwFrameInterval[0], | |
482 | frame->dwDefaultFrameInterval)); | |
483 | ||
484 | uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n", | |
485 | frame->wWidth, frame->wHeight, | |
486 | 10000000/frame->dwDefaultFrameInterval, | |
487 | (100000000/frame->dwDefaultFrameInterval)%10); | |
488 | ||
078f8947 | 489 | format->nframes++; |
c0efd232 LP |
490 | buflen -= buffer[0]; |
491 | buffer += buffer[0]; | |
492 | } | |
493 | ||
494 | if (buflen > 2 && buffer[2] == VS_STILL_IMAGE_FRAME) { | |
495 | buflen -= buffer[0]; | |
496 | buffer += buffer[0]; | |
497 | } | |
498 | ||
499 | if (buflen > 2 && buffer[2] == VS_COLORFORMAT) { | |
500 | if (buflen < 6) { | |
b2d9cc42 | 501 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " |
c0efd232 LP |
502 | "interface %d COLORFORMAT error\n", |
503 | dev->udev->devnum, | |
504 | alts->desc.bInterfaceNumber); | |
505 | return -EINVAL; | |
506 | } | |
507 | ||
508 | format->colorspace = uvc_colorspace(buffer[3]); | |
509 | ||
510 | buflen -= buffer[0]; | |
511 | buffer += buffer[0]; | |
512 | } | |
513 | ||
514 | return buffer - start; | |
515 | } | |
516 | ||
517 | static int uvc_parse_streaming(struct uvc_device *dev, | |
518 | struct usb_interface *intf) | |
519 | { | |
520 | struct uvc_streaming *streaming = NULL; | |
521 | struct uvc_format *format; | |
522 | struct uvc_frame *frame; | |
523 | struct usb_host_interface *alts = &intf->altsetting[0]; | |
524 | unsigned char *_buffer, *buffer = alts->extra; | |
525 | int _buflen, buflen = alts->extralen; | |
526 | unsigned int nformats = 0, nframes = 0, nintervals = 0; | |
527 | unsigned int size, i, n, p; | |
528 | __u32 *interval; | |
529 | __u16 psize; | |
530 | int ret = -EINVAL; | |
531 | ||
532 | if (intf->cur_altsetting->desc.bInterfaceSubClass | |
533 | != SC_VIDEOSTREAMING) { | |
534 | uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a " | |
535 | "video streaming interface\n", dev->udev->devnum, | |
536 | intf->altsetting[0].desc.bInterfaceNumber); | |
537 | return -EINVAL; | |
538 | } | |
539 | ||
540 | if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) { | |
541 | uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already " | |
542 | "claimed\n", dev->udev->devnum, | |
543 | intf->altsetting[0].desc.bInterfaceNumber); | |
544 | return -EINVAL; | |
545 | } | |
546 | ||
547 | streaming = kzalloc(sizeof *streaming, GFP_KERNEL); | |
548 | if (streaming == NULL) { | |
549 | usb_driver_release_interface(&uvc_driver.driver, intf); | |
550 | return -EINVAL; | |
551 | } | |
552 | ||
553 | mutex_init(&streaming->mutex); | |
554 | streaming->intf = usb_get_intf(intf); | |
555 | streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber; | |
556 | ||
557 | /* The Pico iMage webcam has its class-specific interface descriptors | |
558 | * after the endpoint descriptors. | |
559 | */ | |
560 | if (buflen == 0) { | |
561 | for (i = 0; i < alts->desc.bNumEndpoints; ++i) { | |
562 | struct usb_host_endpoint *ep = &alts->endpoint[i]; | |
563 | ||
564 | if (ep->extralen == 0) | |
565 | continue; | |
566 | ||
567 | if (ep->extralen > 2 && | |
568 | ep->extra[1] == USB_DT_CS_INTERFACE) { | |
569 | uvc_trace(UVC_TRACE_DESCR, "trying extra data " | |
570 | "from endpoint %u.\n", i); | |
571 | buffer = alts->endpoint[i].extra; | |
572 | buflen = alts->endpoint[i].extralen; | |
573 | break; | |
574 | } | |
575 | } | |
576 | } | |
577 | ||
578 | /* Skip the standard interface descriptors. */ | |
579 | while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) { | |
580 | buflen -= buffer[0]; | |
581 | buffer += buffer[0]; | |
582 | } | |
583 | ||
584 | if (buflen <= 2) { | |
585 | uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming " | |
586 | "interface descriptors found.\n"); | |
587 | goto error; | |
588 | } | |
589 | ||
590 | /* Parse the header descriptor. */ | |
ff924203 LP |
591 | switch (buffer[2]) { |
592 | case VS_OUTPUT_HEADER: | |
593 | streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
594 | size = 9; | |
595 | break; | |
596 | ||
597 | case VS_INPUT_HEADER: | |
598 | streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
599 | size = 13; | |
600 | break; | |
601 | ||
602 | default: | |
c0efd232 | 603 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface " |
ff924203 LP |
604 | "%d HEADER descriptor not found.\n", dev->udev->devnum, |
605 | alts->desc.bInterfaceNumber); | |
c0efd232 | 606 | goto error; |
ff924203 | 607 | } |
c0efd232 | 608 | |
ff924203 LP |
609 | p = buflen >= 4 ? buffer[3] : 0; |
610 | n = buflen >= size ? buffer[size-1] : 0; | |
611 | ||
612 | if (buflen < size + p*n) { | |
613 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " | |
614 | "interface %d HEADER descriptor is invalid.\n", | |
615 | dev->udev->devnum, alts->desc.bInterfaceNumber); | |
616 | goto error; | |
617 | } | |
c0efd232 | 618 | |
ff924203 LP |
619 | streaming->header.bNumFormats = p; |
620 | streaming->header.bEndpointAddress = buffer[6]; | |
621 | if (buffer[2] == VS_INPUT_HEADER) { | |
c0efd232 LP |
622 | streaming->header.bmInfo = buffer[7]; |
623 | streaming->header.bTerminalLink = buffer[8]; | |
624 | streaming->header.bStillCaptureMethod = buffer[9]; | |
625 | streaming->header.bTriggerSupport = buffer[10]; | |
626 | streaming->header.bTriggerUsage = buffer[11]; | |
c0efd232 | 627 | } else { |
ff924203 LP |
628 | streaming->header.bTerminalLink = buffer[7]; |
629 | } | |
630 | streaming->header.bControlSize = n; | |
631 | ||
632 | streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL); | |
633 | if (streaming->header.bmaControls == NULL) { | |
634 | ret = -ENOMEM; | |
c0efd232 LP |
635 | goto error; |
636 | } | |
637 | ||
ff924203 LP |
638 | memcpy(streaming->header.bmaControls, &buffer[size], p*n); |
639 | ||
c0efd232 LP |
640 | buflen -= buffer[0]; |
641 | buffer += buffer[0]; | |
642 | ||
643 | _buffer = buffer; | |
644 | _buflen = buflen; | |
645 | ||
646 | /* Count the format and frame descriptors. */ | |
647 | while (_buflen > 2) { | |
648 | switch (_buffer[2]) { | |
649 | case VS_FORMAT_UNCOMPRESSED: | |
650 | case VS_FORMAT_MJPEG: | |
651 | case VS_FORMAT_FRAME_BASED: | |
652 | nformats++; | |
653 | break; | |
654 | ||
655 | case VS_FORMAT_DV: | |
656 | /* DV format has no frame descriptor. We will create a | |
657 | * dummy frame descriptor with a dummy frame interval. | |
658 | */ | |
659 | nformats++; | |
660 | nframes++; | |
661 | nintervals++; | |
662 | break; | |
663 | ||
664 | case VS_FORMAT_MPEG2TS: | |
665 | case VS_FORMAT_STREAM_BASED: | |
666 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " | |
667 | "interface %d FORMAT %u is not supported.\n", | |
668 | dev->udev->devnum, | |
669 | alts->desc.bInterfaceNumber, _buffer[2]); | |
670 | break; | |
671 | ||
672 | case VS_FRAME_UNCOMPRESSED: | |
673 | case VS_FRAME_MJPEG: | |
674 | nframes++; | |
675 | if (_buflen > 25) | |
676 | nintervals += _buffer[25] ? _buffer[25] : 3; | |
677 | break; | |
678 | ||
679 | case VS_FRAME_FRAME_BASED: | |
680 | nframes++; | |
681 | if (_buflen > 21) | |
682 | nintervals += _buffer[21] ? _buffer[21] : 3; | |
683 | break; | |
684 | } | |
685 | ||
686 | _buflen -= _buffer[0]; | |
687 | _buffer += _buffer[0]; | |
688 | } | |
689 | ||
690 | if (nformats == 0) { | |
691 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface " | |
692 | "%d has no supported formats defined.\n", | |
693 | dev->udev->devnum, alts->desc.bInterfaceNumber); | |
694 | goto error; | |
695 | } | |
696 | ||
697 | size = nformats * sizeof *format + nframes * sizeof *frame | |
698 | + nintervals * sizeof *interval; | |
699 | format = kzalloc(size, GFP_KERNEL); | |
700 | if (format == NULL) { | |
701 | ret = -ENOMEM; | |
702 | goto error; | |
703 | } | |
704 | ||
705 | frame = (struct uvc_frame *)&format[nformats]; | |
706 | interval = (__u32 *)&frame[nframes]; | |
707 | ||
708 | streaming->format = format; | |
709 | streaming->nformats = nformats; | |
710 | ||
711 | /* Parse the format descriptors. */ | |
712 | while (buflen > 2) { | |
713 | switch (buffer[2]) { | |
714 | case VS_FORMAT_UNCOMPRESSED: | |
715 | case VS_FORMAT_MJPEG: | |
716 | case VS_FORMAT_DV: | |
717 | case VS_FORMAT_FRAME_BASED: | |
718 | format->frame = frame; | |
719 | ret = uvc_parse_format(dev, streaming, format, | |
720 | &interval, buffer, buflen); | |
721 | if (ret < 0) | |
722 | goto error; | |
723 | ||
724 | frame += format->nframes; | |
725 | format++; | |
726 | ||
727 | buflen -= ret; | |
728 | buffer += ret; | |
729 | continue; | |
730 | ||
731 | default: | |
732 | break; | |
733 | } | |
734 | ||
735 | buflen -= buffer[0]; | |
736 | buffer += buffer[0]; | |
737 | } | |
738 | ||
739 | /* Parse the alternate settings to find the maximum bandwidth. */ | |
740 | for (i = 0; i < intf->num_altsetting; ++i) { | |
741 | struct usb_host_endpoint *ep; | |
742 | alts = &intf->altsetting[i]; | |
743 | ep = uvc_find_endpoint(alts, | |
744 | streaming->header.bEndpointAddress); | |
745 | if (ep == NULL) | |
746 | continue; | |
747 | ||
748 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); | |
749 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); | |
750 | if (psize > streaming->maxpsize) | |
751 | streaming->maxpsize = psize; | |
752 | } | |
753 | ||
754 | list_add_tail(&streaming->list, &dev->streaming); | |
755 | return 0; | |
756 | ||
757 | error: | |
758 | usb_driver_release_interface(&uvc_driver.driver, intf); | |
759 | usb_put_intf(intf); | |
760 | kfree(streaming->format); | |
761 | kfree(streaming->header.bmaControls); | |
762 | kfree(streaming); | |
763 | return ret; | |
764 | } | |
765 | ||
766 | /* Parse vendor-specific extensions. */ | |
767 | static int uvc_parse_vendor_control(struct uvc_device *dev, | |
768 | const unsigned char *buffer, int buflen) | |
769 | { | |
770 | struct usb_device *udev = dev->udev; | |
771 | struct usb_host_interface *alts = dev->intf->cur_altsetting; | |
772 | struct uvc_entity *unit; | |
773 | unsigned int n, p; | |
774 | int handled = 0; | |
775 | ||
776 | switch (le16_to_cpu(dev->udev->descriptor.idVendor)) { | |
777 | case 0x046d: /* Logitech */ | |
778 | if (buffer[1] != 0x41 || buffer[2] != 0x01) | |
779 | break; | |
780 | ||
781 | /* Logitech implements several vendor specific functions | |
782 | * through vendor specific extension units (LXU). | |
783 | * | |
784 | * The LXU descriptors are similar to XU descriptors | |
785 | * (see "USB Device Video Class for Video Devices", section | |
786 | * 3.7.2.6 "Extension Unit Descriptor") with the following | |
787 | * differences: | |
788 | * | |
789 | * ---------------------------------------------------------- | |
790 | * 0 bLength 1 Number | |
791 | * Size of this descriptor, in bytes: 24+p+n*2 | |
792 | * ---------------------------------------------------------- | |
793 | * 23+p+n bmControlsType N Bitmap | |
794 | * Individual bits in the set are defined: | |
795 | * 0: Absolute | |
796 | * 1: Relative | |
797 | * | |
798 | * This bitset is mapped exactly the same as bmControls. | |
799 | * ---------------------------------------------------------- | |
800 | * 23+p+n*2 bReserved 1 Boolean | |
801 | * ---------------------------------------------------------- | |
802 | * 24+p+n*2 iExtension 1 Index | |
803 | * Index of a string descriptor that describes this | |
804 | * extension unit. | |
805 | * ---------------------------------------------------------- | |
806 | */ | |
807 | p = buflen >= 22 ? buffer[21] : 0; | |
808 | n = buflen >= 25 + p ? buffer[22+p] : 0; | |
809 | ||
810 | if (buflen < 25 + p + 2*n) { | |
811 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
812 | "interface %d EXTENSION_UNIT error\n", | |
813 | udev->devnum, alts->desc.bInterfaceNumber); | |
814 | break; | |
815 | } | |
816 | ||
817 | unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL); | |
818 | if (unit == NULL) | |
819 | return -ENOMEM; | |
820 | ||
821 | unit->id = buffer[3]; | |
822 | unit->type = VC_EXTENSION_UNIT; | |
823 | memcpy(unit->extension.guidExtensionCode, &buffer[4], 16); | |
824 | unit->extension.bNumControls = buffer[20]; | |
9bc6218d | 825 | unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]); |
c0efd232 LP |
826 | unit->extension.baSourceID = (__u8 *)unit + sizeof *unit; |
827 | memcpy(unit->extension.baSourceID, &buffer[22], p); | |
828 | unit->extension.bControlSize = buffer[22+p]; | |
829 | unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p; | |
830 | unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit | |
831 | + p + n; | |
832 | memcpy(unit->extension.bmControls, &buffer[23+p], 2*n); | |
833 | ||
834 | if (buffer[24+p+2*n] != 0) | |
835 | usb_string(udev, buffer[24+p+2*n], unit->name, | |
836 | sizeof unit->name); | |
837 | else | |
838 | sprintf(unit->name, "Extension %u", buffer[3]); | |
839 | ||
840 | list_add_tail(&unit->list, &dev->entities); | |
841 | handled = 1; | |
842 | break; | |
843 | } | |
844 | ||
845 | return handled; | |
846 | } | |
847 | ||
848 | static int uvc_parse_standard_control(struct uvc_device *dev, | |
849 | const unsigned char *buffer, int buflen) | |
850 | { | |
851 | struct usb_device *udev = dev->udev; | |
852 | struct uvc_entity *unit, *term; | |
853 | struct usb_interface *intf; | |
854 | struct usb_host_interface *alts = dev->intf->cur_altsetting; | |
855 | unsigned int i, n, p, len; | |
856 | __u16 type; | |
857 | ||
858 | switch (buffer[2]) { | |
859 | case VC_HEADER: | |
860 | n = buflen >= 12 ? buffer[11] : 0; | |
861 | ||
862 | if (buflen < 12 || buflen < 12 + n) { | |
863 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
864 | "interface %d HEADER error\n", udev->devnum, | |
865 | alts->desc.bInterfaceNumber); | |
866 | return -EINVAL; | |
867 | } | |
868 | ||
9bc6218d MH |
869 | dev->uvc_version = get_unaligned_le16(&buffer[3]); |
870 | dev->clock_frequency = get_unaligned_le32(&buffer[7]); | |
c0efd232 LP |
871 | |
872 | /* Parse all USB Video Streaming interfaces. */ | |
873 | for (i = 0; i < n; ++i) { | |
874 | intf = usb_ifnum_to_if(udev, buffer[12+i]); | |
875 | if (intf == NULL) { | |
876 | uvc_trace(UVC_TRACE_DESCR, "device %d " | |
877 | "interface %d doesn't exists\n", | |
878 | udev->devnum, i); | |
879 | continue; | |
880 | } | |
881 | ||
882 | uvc_parse_streaming(dev, intf); | |
883 | } | |
884 | break; | |
885 | ||
886 | case VC_INPUT_TERMINAL: | |
887 | if (buflen < 8) { | |
888 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
889 | "interface %d INPUT_TERMINAL error\n", | |
890 | udev->devnum, alts->desc.bInterfaceNumber); | |
891 | return -EINVAL; | |
892 | } | |
893 | ||
894 | /* Make sure the terminal type MSB is not null, otherwise it | |
895 | * could be confused with a unit. | |
896 | */ | |
9bc6218d | 897 | type = get_unaligned_le16(&buffer[4]); |
c0efd232 LP |
898 | if ((type & 0xff00) == 0) { |
899 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
900 | "interface %d INPUT_TERMINAL %d has invalid " | |
901 | "type 0x%04x, skipping\n", udev->devnum, | |
902 | alts->desc.bInterfaceNumber, | |
903 | buffer[3], type); | |
904 | return 0; | |
905 | } | |
906 | ||
907 | n = 0; | |
908 | p = 0; | |
909 | len = 8; | |
910 | ||
911 | if (type == ITT_CAMERA) { | |
912 | n = buflen >= 15 ? buffer[14] : 0; | |
913 | len = 15; | |
914 | ||
915 | } else if (type == ITT_MEDIA_TRANSPORT_INPUT) { | |
916 | n = buflen >= 9 ? buffer[8] : 0; | |
917 | p = buflen >= 10 + n ? buffer[9+n] : 0; | |
918 | len = 10; | |
919 | } | |
920 | ||
921 | if (buflen < len + n + p) { | |
922 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
923 | "interface %d INPUT_TERMINAL error\n", | |
924 | udev->devnum, alts->desc.bInterfaceNumber); | |
925 | return -EINVAL; | |
926 | } | |
927 | ||
928 | term = kzalloc(sizeof *term + n + p, GFP_KERNEL); | |
929 | if (term == NULL) | |
930 | return -ENOMEM; | |
931 | ||
932 | term->id = buffer[3]; | |
933 | term->type = type | UVC_TERM_INPUT; | |
934 | ||
935 | if (UVC_ENTITY_TYPE(term) == ITT_CAMERA) { | |
936 | term->camera.bControlSize = n; | |
937 | term->camera.bmControls = (__u8 *)term + sizeof *term; | |
938 | term->camera.wObjectiveFocalLengthMin = | |
9bc6218d | 939 | get_unaligned_le16(&buffer[8]); |
c0efd232 | 940 | term->camera.wObjectiveFocalLengthMax = |
9bc6218d | 941 | get_unaligned_le16(&buffer[10]); |
c0efd232 | 942 | term->camera.wOcularFocalLength = |
9bc6218d | 943 | get_unaligned_le16(&buffer[12]); |
c0efd232 LP |
944 | memcpy(term->camera.bmControls, &buffer[15], n); |
945 | } else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT) { | |
946 | term->media.bControlSize = n; | |
947 | term->media.bmControls = (__u8 *)term + sizeof *term; | |
948 | term->media.bTransportModeSize = p; | |
949 | term->media.bmTransportModes = (__u8 *)term | |
950 | + sizeof *term + n; | |
951 | memcpy(term->media.bmControls, &buffer[9], n); | |
952 | memcpy(term->media.bmTransportModes, &buffer[10+n], p); | |
953 | } | |
954 | ||
955 | if (buffer[7] != 0) | |
956 | usb_string(udev, buffer[7], term->name, | |
957 | sizeof term->name); | |
958 | else if (UVC_ENTITY_TYPE(term) == ITT_CAMERA) | |
959 | sprintf(term->name, "Camera %u", buffer[3]); | |
960 | else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT) | |
961 | sprintf(term->name, "Media %u", buffer[3]); | |
962 | else | |
963 | sprintf(term->name, "Input %u", buffer[3]); | |
964 | ||
965 | list_add_tail(&term->list, &dev->entities); | |
966 | break; | |
967 | ||
968 | case VC_OUTPUT_TERMINAL: | |
969 | if (buflen < 9) { | |
970 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
971 | "interface %d OUTPUT_TERMINAL error\n", | |
972 | udev->devnum, alts->desc.bInterfaceNumber); | |
973 | return -EINVAL; | |
974 | } | |
975 | ||
976 | /* Make sure the terminal type MSB is not null, otherwise it | |
977 | * could be confused with a unit. | |
978 | */ | |
9bc6218d | 979 | type = get_unaligned_le16(&buffer[4]); |
c0efd232 LP |
980 | if ((type & 0xff00) == 0) { |
981 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
982 | "interface %d OUTPUT_TERMINAL %d has invalid " | |
983 | "type 0x%04x, skipping\n", udev->devnum, | |
984 | alts->desc.bInterfaceNumber, buffer[3], type); | |
985 | return 0; | |
986 | } | |
987 | ||
988 | term = kzalloc(sizeof *term, GFP_KERNEL); | |
989 | if (term == NULL) | |
990 | return -ENOMEM; | |
991 | ||
992 | term->id = buffer[3]; | |
993 | term->type = type | UVC_TERM_OUTPUT; | |
994 | term->output.bSourceID = buffer[7]; | |
995 | ||
996 | if (buffer[8] != 0) | |
997 | usb_string(udev, buffer[8], term->name, | |
998 | sizeof term->name); | |
999 | else | |
1000 | sprintf(term->name, "Output %u", buffer[3]); | |
1001 | ||
1002 | list_add_tail(&term->list, &dev->entities); | |
1003 | break; | |
1004 | ||
1005 | case VC_SELECTOR_UNIT: | |
1006 | p = buflen >= 5 ? buffer[4] : 0; | |
1007 | ||
1008 | if (buflen < 5 || buflen < 6 + p) { | |
1009 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
1010 | "interface %d SELECTOR_UNIT error\n", | |
1011 | udev->devnum, alts->desc.bInterfaceNumber); | |
1012 | return -EINVAL; | |
1013 | } | |
1014 | ||
1015 | unit = kzalloc(sizeof *unit + p, GFP_KERNEL); | |
1016 | if (unit == NULL) | |
1017 | return -ENOMEM; | |
1018 | ||
1019 | unit->id = buffer[3]; | |
1020 | unit->type = buffer[2]; | |
1021 | unit->selector.bNrInPins = buffer[4]; | |
1022 | unit->selector.baSourceID = (__u8 *)unit + sizeof *unit; | |
1023 | memcpy(unit->selector.baSourceID, &buffer[5], p); | |
1024 | ||
1025 | if (buffer[5+p] != 0) | |
1026 | usb_string(udev, buffer[5+p], unit->name, | |
1027 | sizeof unit->name); | |
1028 | else | |
1029 | sprintf(unit->name, "Selector %u", buffer[3]); | |
1030 | ||
1031 | list_add_tail(&unit->list, &dev->entities); | |
1032 | break; | |
1033 | ||
1034 | case VC_PROCESSING_UNIT: | |
1035 | n = buflen >= 8 ? buffer[7] : 0; | |
1036 | p = dev->uvc_version >= 0x0110 ? 10 : 9; | |
1037 | ||
1038 | if (buflen < p + n) { | |
1039 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
1040 | "interface %d PROCESSING_UNIT error\n", | |
1041 | udev->devnum, alts->desc.bInterfaceNumber); | |
1042 | return -EINVAL; | |
1043 | } | |
1044 | ||
1045 | unit = kzalloc(sizeof *unit + n, GFP_KERNEL); | |
1046 | if (unit == NULL) | |
1047 | return -ENOMEM; | |
1048 | ||
1049 | unit->id = buffer[3]; | |
1050 | unit->type = buffer[2]; | |
1051 | unit->processing.bSourceID = buffer[4]; | |
1052 | unit->processing.wMaxMultiplier = | |
9bc6218d | 1053 | get_unaligned_le16(&buffer[5]); |
c0efd232 LP |
1054 | unit->processing.bControlSize = buffer[7]; |
1055 | unit->processing.bmControls = (__u8 *)unit + sizeof *unit; | |
1056 | memcpy(unit->processing.bmControls, &buffer[8], n); | |
1057 | if (dev->uvc_version >= 0x0110) | |
1058 | unit->processing.bmVideoStandards = buffer[9+n]; | |
1059 | ||
1060 | if (buffer[8+n] != 0) | |
1061 | usb_string(udev, buffer[8+n], unit->name, | |
1062 | sizeof unit->name); | |
1063 | else | |
1064 | sprintf(unit->name, "Processing %u", buffer[3]); | |
1065 | ||
1066 | list_add_tail(&unit->list, &dev->entities); | |
1067 | break; | |
1068 | ||
1069 | case VC_EXTENSION_UNIT: | |
1070 | p = buflen >= 22 ? buffer[21] : 0; | |
1071 | n = buflen >= 24 + p ? buffer[22+p] : 0; | |
1072 | ||
1073 | if (buflen < 24 + p + n) { | |
1074 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
1075 | "interface %d EXTENSION_UNIT error\n", | |
1076 | udev->devnum, alts->desc.bInterfaceNumber); | |
1077 | return -EINVAL; | |
1078 | } | |
1079 | ||
1080 | unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL); | |
1081 | if (unit == NULL) | |
1082 | return -ENOMEM; | |
1083 | ||
1084 | unit->id = buffer[3]; | |
1085 | unit->type = buffer[2]; | |
1086 | memcpy(unit->extension.guidExtensionCode, &buffer[4], 16); | |
1087 | unit->extension.bNumControls = buffer[20]; | |
9bc6218d | 1088 | unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]); |
c0efd232 LP |
1089 | unit->extension.baSourceID = (__u8 *)unit + sizeof *unit; |
1090 | memcpy(unit->extension.baSourceID, &buffer[22], p); | |
1091 | unit->extension.bControlSize = buffer[22+p]; | |
1092 | unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p; | |
1093 | memcpy(unit->extension.bmControls, &buffer[23+p], n); | |
1094 | ||
1095 | if (buffer[23+p+n] != 0) | |
1096 | usb_string(udev, buffer[23+p+n], unit->name, | |
1097 | sizeof unit->name); | |
1098 | else | |
1099 | sprintf(unit->name, "Extension %u", buffer[3]); | |
1100 | ||
1101 | list_add_tail(&unit->list, &dev->entities); | |
1102 | break; | |
1103 | ||
1104 | default: | |
1105 | uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE " | |
1106 | "descriptor (%u)\n", buffer[2]); | |
1107 | break; | |
1108 | } | |
1109 | ||
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | static int uvc_parse_control(struct uvc_device *dev) | |
1114 | { | |
1115 | struct usb_host_interface *alts = dev->intf->cur_altsetting; | |
1116 | unsigned char *buffer = alts->extra; | |
1117 | int buflen = alts->extralen; | |
1118 | int ret; | |
1119 | ||
1120 | /* Parse the default alternate setting only, as the UVC specification | |
1121 | * defines a single alternate setting, the default alternate setting | |
1122 | * zero. | |
1123 | */ | |
1124 | ||
1125 | while (buflen > 2) { | |
1126 | if (uvc_parse_vendor_control(dev, buffer, buflen) || | |
1127 | buffer[1] != USB_DT_CS_INTERFACE) | |
1128 | goto next_descriptor; | |
1129 | ||
1130 | if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0) | |
1131 | return ret; | |
1132 | ||
1133 | next_descriptor: | |
1134 | buflen -= buffer[0]; | |
1135 | buffer += buffer[0]; | |
1136 | } | |
1137 | ||
538e7a00 LP |
1138 | /* Check if the optional status endpoint is present. Built-in iSight |
1139 | * webcams have an interrupt endpoint but spit proprietary data that | |
1140 | * don't conform to the UVC status endpoint messages. Don't try to | |
1141 | * handle the interrupt endpoint for those cameras. | |
1142 | */ | |
1143 | if (alts->desc.bNumEndpoints == 1 && | |
1144 | !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) { | |
c0efd232 LP |
1145 | struct usb_host_endpoint *ep = &alts->endpoint[0]; |
1146 | struct usb_endpoint_descriptor *desc = &ep->desc; | |
1147 | ||
1148 | if (usb_endpoint_is_int_in(desc) && | |
1149 | le16_to_cpu(desc->wMaxPacketSize) >= 8 && | |
1150 | desc->bInterval != 0) { | |
1151 | uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint " | |
1152 | "(addr %02x).\n", desc->bEndpointAddress); | |
1153 | dev->int_ep = ep; | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | return 0; | |
1158 | } | |
1159 | ||
1160 | /* ------------------------------------------------------------------------ | |
1161 | * USB probe and disconnect | |
1162 | */ | |
1163 | ||
1164 | /* | |
1165 | * Unregister the video devices. | |
1166 | */ | |
1167 | static void uvc_unregister_video(struct uvc_device *dev) | |
1168 | { | |
1169 | if (dev->video.vdev) { | |
1170 | if (dev->video.vdev->minor == -1) | |
1171 | video_device_release(dev->video.vdev); | |
1172 | else | |
1173 | video_unregister_device(dev->video.vdev); | |
1174 | dev->video.vdev = NULL; | |
1175 | } | |
1176 | } | |
1177 | ||
1178 | /* | |
1179 | * Scan the UVC descriptors to locate a chain starting at an Output Terminal | |
1180 | * and containing the following units: | |
1181 | * | |
2c2d264b | 1182 | * - one Output Terminal (USB Streaming or Display) |
c0efd232 LP |
1183 | * - zero or one Processing Unit |
1184 | * - zero, one or mode single-input Selector Units | |
1185 | * - zero or one multiple-input Selector Units, provided all inputs are | |
1186 | * connected to input terminals | |
1187 | * - zero, one or mode single-input Extension Units | |
2c2d264b | 1188 | * - one or more Input Terminals (Camera, External or USB Streaming) |
c0efd232 LP |
1189 | * |
1190 | * A side forward scan is made on each detected entity to check for additional | |
1191 | * extension units. | |
1192 | */ | |
1193 | static int uvc_scan_chain_entity(struct uvc_video_device *video, | |
1194 | struct uvc_entity *entity) | |
1195 | { | |
1196 | switch (UVC_ENTITY_TYPE(entity)) { | |
1197 | case VC_EXTENSION_UNIT: | |
1198 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1199 | printk(" <- XU %d", entity->id); | |
1200 | ||
1201 | if (entity->extension.bNrInPins != 1) { | |
1202 | uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more " | |
1203 | "than 1 input pin.\n", entity->id); | |
1204 | return -1; | |
1205 | } | |
1206 | ||
1207 | list_add_tail(&entity->chain, &video->extensions); | |
1208 | break; | |
1209 | ||
1210 | case VC_PROCESSING_UNIT: | |
1211 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1212 | printk(" <- PU %d", entity->id); | |
1213 | ||
1214 | if (video->processing != NULL) { | |
1215 | uvc_trace(UVC_TRACE_DESCR, "Found multiple " | |
1216 | "Processing Units in chain.\n"); | |
1217 | return -1; | |
1218 | } | |
1219 | ||
1220 | video->processing = entity; | |
1221 | break; | |
1222 | ||
1223 | case VC_SELECTOR_UNIT: | |
1224 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1225 | printk(" <- SU %d", entity->id); | |
1226 | ||
1227 | /* Single-input selector units are ignored. */ | |
1228 | if (entity->selector.bNrInPins == 1) | |
1229 | break; | |
1230 | ||
1231 | if (video->selector != NULL) { | |
1232 | uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector " | |
1233 | "Units in chain.\n"); | |
1234 | return -1; | |
1235 | } | |
1236 | ||
1237 | video->selector = entity; | |
1238 | break; | |
1239 | ||
1240 | case ITT_VENDOR_SPECIFIC: | |
1241 | case ITT_CAMERA: | |
1242 | case ITT_MEDIA_TRANSPORT_INPUT: | |
1243 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1244 | printk(" <- IT %d\n", entity->id); | |
1245 | ||
1246 | list_add_tail(&entity->chain, &video->iterms); | |
1247 | break; | |
1248 | ||
ff924203 LP |
1249 | case TT_STREAMING: |
1250 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1251 | printk(" <- IT %d\n", entity->id); | |
1252 | ||
1253 | if (!UVC_ENTITY_IS_ITERM(entity)) { | |
1254 | uvc_trace(UVC_TRACE_DESCR, "Unsupported input " | |
1255 | "terminal %u.\n", entity->id); | |
1256 | return -1; | |
1257 | } | |
1258 | ||
1259 | if (video->sterm != NULL) { | |
1260 | uvc_trace(UVC_TRACE_DESCR, "Found multiple streaming " | |
1261 | "entities in chain.\n"); | |
1262 | return -1; | |
1263 | } | |
1264 | ||
1265 | list_add_tail(&entity->chain, &video->iterms); | |
1266 | video->sterm = entity; | |
1267 | break; | |
1268 | ||
c0efd232 LP |
1269 | default: |
1270 | uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type " | |
1271 | "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity)); | |
1272 | return -1; | |
1273 | } | |
1274 | ||
1275 | return 0; | |
1276 | } | |
1277 | ||
1278 | static int uvc_scan_chain_forward(struct uvc_video_device *video, | |
1279 | struct uvc_entity *entity, struct uvc_entity *prev) | |
1280 | { | |
1281 | struct uvc_entity *forward; | |
1282 | int found; | |
1283 | ||
1284 | /* Forward scan */ | |
1285 | forward = NULL; | |
1286 | found = 0; | |
1287 | ||
1288 | while (1) { | |
1289 | forward = uvc_entity_by_reference(video->dev, entity->id, | |
1290 | forward); | |
1291 | if (forward == NULL) | |
1292 | break; | |
1293 | ||
1294 | if (UVC_ENTITY_TYPE(forward) != VC_EXTENSION_UNIT || | |
1295 | forward == prev) | |
1296 | continue; | |
1297 | ||
1298 | if (forward->extension.bNrInPins != 1) { | |
b2d9cc42 | 1299 | uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has " |
c0efd232 LP |
1300 | "more than 1 input pin.\n", entity->id); |
1301 | return -1; | |
1302 | } | |
1303 | ||
1304 | list_add_tail(&forward->chain, &video->extensions); | |
1305 | if (uvc_trace_param & UVC_TRACE_PROBE) { | |
1306 | if (!found) | |
1307 | printk(" (-> XU"); | |
1308 | ||
1309 | printk(" %d", forward->id); | |
1310 | found = 1; | |
1311 | } | |
1312 | } | |
1313 | if (found) | |
1314 | printk(")"); | |
1315 | ||
1316 | return 0; | |
1317 | } | |
1318 | ||
1319 | static int uvc_scan_chain_backward(struct uvc_video_device *video, | |
1320 | struct uvc_entity *entity) | |
1321 | { | |
1322 | struct uvc_entity *term; | |
1323 | int id = -1, i; | |
1324 | ||
1325 | switch (UVC_ENTITY_TYPE(entity)) { | |
1326 | case VC_EXTENSION_UNIT: | |
1327 | id = entity->extension.baSourceID[0]; | |
1328 | break; | |
1329 | ||
1330 | case VC_PROCESSING_UNIT: | |
1331 | id = entity->processing.bSourceID; | |
1332 | break; | |
1333 | ||
1334 | case VC_SELECTOR_UNIT: | |
1335 | /* Single-input selector units are ignored. */ | |
1336 | if (entity->selector.bNrInPins == 1) { | |
1337 | id = entity->selector.baSourceID[0]; | |
1338 | break; | |
1339 | } | |
1340 | ||
1341 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1342 | printk(" <- IT"); | |
1343 | ||
1344 | video->selector = entity; | |
1345 | for (i = 0; i < entity->selector.bNrInPins; ++i) { | |
1346 | id = entity->selector.baSourceID[i]; | |
1347 | term = uvc_entity_by_id(video->dev, id); | |
1348 | if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) { | |
1349 | uvc_trace(UVC_TRACE_DESCR, "Selector unit %d " | |
1350 | "input %d isn't connected to an " | |
1351 | "input terminal\n", entity->id, i); | |
1352 | return -1; | |
1353 | } | |
1354 | ||
1355 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1356 | printk(" %d", term->id); | |
1357 | ||
1358 | list_add_tail(&term->chain, &video->iterms); | |
1359 | uvc_scan_chain_forward(video, term, entity); | |
1360 | } | |
1361 | ||
1362 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1363 | printk("\n"); | |
1364 | ||
1365 | id = 0; | |
1366 | break; | |
1367 | } | |
1368 | ||
1369 | return id; | |
1370 | } | |
1371 | ||
1372 | static int uvc_scan_chain(struct uvc_video_device *video) | |
1373 | { | |
1374 | struct uvc_entity *entity, *prev; | |
1375 | int id; | |
1376 | ||
1377 | entity = video->oterm; | |
1378 | uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id); | |
ff924203 LP |
1379 | |
1380 | if (UVC_ENTITY_TYPE(entity) == TT_STREAMING) | |
1381 | video->sterm = entity; | |
1382 | ||
c0efd232 LP |
1383 | id = entity->output.bSourceID; |
1384 | while (id != 0) { | |
1385 | prev = entity; | |
1386 | entity = uvc_entity_by_id(video->dev, id); | |
1387 | if (entity == NULL) { | |
1388 | uvc_trace(UVC_TRACE_DESCR, "Found reference to " | |
1389 | "unknown entity %d.\n", id); | |
1390 | return -1; | |
1391 | } | |
1392 | ||
1393 | /* Process entity */ | |
1394 | if (uvc_scan_chain_entity(video, entity) < 0) | |
1395 | return -1; | |
1396 | ||
1397 | /* Forward scan */ | |
1398 | if (uvc_scan_chain_forward(video, entity, prev) < 0) | |
1399 | return -1; | |
1400 | ||
1401 | /* Stop when a terminal is found. */ | |
1402 | if (!UVC_ENTITY_IS_UNIT(entity)) | |
1403 | break; | |
1404 | ||
1405 | /* Backward scan */ | |
1406 | id = uvc_scan_chain_backward(video, entity); | |
1407 | if (id < 0) | |
1408 | return id; | |
1409 | } | |
1410 | ||
ff924203 LP |
1411 | if (video->sterm == NULL) { |
1412 | uvc_trace(UVC_TRACE_DESCR, "No streaming entity found in " | |
1413 | "chain.\n"); | |
1414 | return -1; | |
1415 | } | |
c0efd232 LP |
1416 | |
1417 | return 0; | |
1418 | } | |
1419 | ||
1420 | /* | |
1421 | * Register the video devices. | |
1422 | * | |
1423 | * The driver currently supports a single video device per control interface | |
1424 | * only. The terminal and units must match the following structure: | |
1425 | * | |
ff924203 LP |
1426 | * ITT_* -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING |
1427 | * TT_STREAMING -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> OTT_* | |
c0efd232 LP |
1428 | * |
1429 | * The Extension Units, if present, must have a single input pin. The | |
1430 | * Processing Unit and Extension Units can be in any order. Additional | |
1431 | * Extension Units connected to the main chain as single-unit branches are | |
1432 | * also supported. | |
1433 | */ | |
1434 | static int uvc_register_video(struct uvc_device *dev) | |
1435 | { | |
1436 | struct video_device *vdev; | |
1437 | struct uvc_entity *term; | |
1438 | int found = 0, ret; | |
1439 | ||
1440 | /* Check if the control interface matches the structure we expect. */ | |
1441 | list_for_each_entry(term, &dev->entities, list) { | |
1442 | struct uvc_streaming *streaming; | |
1443 | ||
ff924203 | 1444 | if (!UVC_ENTITY_IS_TERM(term) || !UVC_ENTITY_IS_OTERM(term)) |
c0efd232 LP |
1445 | continue; |
1446 | ||
1447 | memset(&dev->video, 0, sizeof dev->video); | |
1448 | mutex_init(&dev->video.ctrl_mutex); | |
1449 | INIT_LIST_HEAD(&dev->video.iterms); | |
1450 | INIT_LIST_HEAD(&dev->video.extensions); | |
1451 | dev->video.oterm = term; | |
1452 | dev->video.dev = dev; | |
1453 | if (uvc_scan_chain(&dev->video) < 0) | |
1454 | continue; | |
1455 | ||
1456 | list_for_each_entry(streaming, &dev->streaming, list) { | |
ff924203 LP |
1457 | if (streaming->header.bTerminalLink == |
1458 | dev->video.sterm->id) { | |
c0efd232 LP |
1459 | dev->video.streaming = streaming; |
1460 | found = 1; | |
1461 | break; | |
1462 | } | |
1463 | } | |
1464 | ||
1465 | if (found) | |
1466 | break; | |
1467 | } | |
1468 | ||
1469 | if (!found) { | |
1470 | uvc_printk(KERN_INFO, "No valid video chain found.\n"); | |
1471 | return -1; | |
1472 | } | |
1473 | ||
1474 | if (uvc_trace_param & UVC_TRACE_PROBE) { | |
1475 | uvc_printk(KERN_INFO, "Found a valid video chain ("); | |
1476 | list_for_each_entry(term, &dev->video.iterms, chain) { | |
1477 | printk("%d", term->id); | |
1478 | if (term->chain.next != &dev->video.iterms) | |
1479 | printk(","); | |
1480 | } | |
1481 | printk(" -> %d).\n", dev->video.oterm->id); | |
1482 | } | |
1483 | ||
ff924203 LP |
1484 | /* Initialize the video buffers queue. */ |
1485 | uvc_queue_init(&dev->video.queue, dev->video.streaming->type); | |
1486 | ||
c0efd232 LP |
1487 | /* Initialize the streaming interface with default streaming |
1488 | * parameters. | |
1489 | */ | |
1490 | if ((ret = uvc_video_init(&dev->video)) < 0) { | |
1491 | uvc_printk(KERN_ERR, "Failed to initialize the device " | |
1492 | "(%d).\n", ret); | |
1493 | return ret; | |
1494 | } | |
1495 | ||
1496 | /* Register the device with V4L. */ | |
1497 | vdev = video_device_alloc(); | |
1498 | if (vdev == NULL) | |
1499 | return -1; | |
1500 | ||
1501 | /* We already hold a reference to dev->udev. The video device will be | |
1502 | * unregistered before the reference is released, so we don't need to | |
1503 | * get another one. | |
1504 | */ | |
5e85e732 | 1505 | vdev->parent = &dev->intf->dev; |
c0efd232 LP |
1506 | vdev->minor = -1; |
1507 | vdev->fops = &uvc_fops; | |
1508 | vdev->release = video_device_release; | |
d0ebf307 | 1509 | strlcpy(vdev->name, dev->name, sizeof vdev->name); |
c0efd232 LP |
1510 | |
1511 | /* Set the driver data before calling video_register_device, otherwise | |
1512 | * uvc_v4l2_open might race us. | |
c0efd232 LP |
1513 | */ |
1514 | dev->video.vdev = vdev; | |
1515 | video_set_drvdata(vdev, &dev->video); | |
1516 | ||
1517 | if (video_register_device(vdev, VFL_TYPE_GRABBER, -1) < 0) { | |
1518 | dev->video.vdev = NULL; | |
1519 | video_device_release(vdev); | |
1520 | return -1; | |
1521 | } | |
1522 | ||
1523 | return 0; | |
1524 | } | |
1525 | ||
1526 | /* | |
1527 | * Delete the UVC device. | |
1528 | * | |
1529 | * Called by the kernel when the last reference to the uvc_device structure | |
1530 | * is released. | |
1531 | * | |
1532 | * Unregistering the video devices is done here because every opened instance | |
1533 | * must be closed before the device can be unregistered. An alternative would | |
1534 | * have been to use another reference count for uvc_v4l2_open/uvc_release, and | |
1535 | * unregister the video devices on disconnect when that reference count drops | |
1536 | * to zero. | |
1537 | * | |
1538 | * As this function is called after or during disconnect(), all URBs have | |
1539 | * already been canceled by the USB core. There is no need to kill the | |
1540 | * interrupt URB manually. | |
1541 | */ | |
1542 | void uvc_delete(struct kref *kref) | |
1543 | { | |
1544 | struct uvc_device *dev = container_of(kref, struct uvc_device, kref); | |
1545 | struct list_head *p, *n; | |
1546 | ||
2c2d264b | 1547 | /* Unregister the video device. */ |
c0efd232 LP |
1548 | uvc_unregister_video(dev); |
1549 | usb_put_intf(dev->intf); | |
1550 | usb_put_dev(dev->udev); | |
1551 | ||
1552 | uvc_status_cleanup(dev); | |
1553 | uvc_ctrl_cleanup_device(dev); | |
1554 | ||
1555 | list_for_each_safe(p, n, &dev->entities) { | |
1556 | struct uvc_entity *entity; | |
1557 | entity = list_entry(p, struct uvc_entity, list); | |
1558 | kfree(entity); | |
1559 | } | |
1560 | ||
1561 | list_for_each_safe(p, n, &dev->streaming) { | |
1562 | struct uvc_streaming *streaming; | |
1563 | streaming = list_entry(p, struct uvc_streaming, list); | |
1564 | usb_driver_release_interface(&uvc_driver.driver, | |
1565 | streaming->intf); | |
1566 | usb_put_intf(streaming->intf); | |
1567 | kfree(streaming->format); | |
1568 | kfree(streaming->header.bmaControls); | |
1569 | kfree(streaming); | |
1570 | } | |
1571 | ||
1572 | kfree(dev); | |
1573 | } | |
1574 | ||
1575 | static int uvc_probe(struct usb_interface *intf, | |
1576 | const struct usb_device_id *id) | |
1577 | { | |
1578 | struct usb_device *udev = interface_to_usbdev(intf); | |
1579 | struct uvc_device *dev; | |
1580 | int ret; | |
1581 | ||
1582 | if (id->idVendor && id->idProduct) | |
1583 | uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s " | |
1584 | "(%04x:%04x)\n", udev->devpath, id->idVendor, | |
1585 | id->idProduct); | |
1586 | else | |
1587 | uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n", | |
1588 | udev->devpath); | |
1589 | ||
2c2d264b | 1590 | /* Allocate memory for the device and initialize it. */ |
c0efd232 LP |
1591 | if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL) |
1592 | return -ENOMEM; | |
1593 | ||
1594 | INIT_LIST_HEAD(&dev->entities); | |
1595 | INIT_LIST_HEAD(&dev->streaming); | |
1596 | kref_init(&dev->kref); | |
04a37e0f | 1597 | atomic_set(&dev->users, 0); |
c0efd232 LP |
1598 | |
1599 | dev->udev = usb_get_dev(udev); | |
1600 | dev->intf = usb_get_intf(intf); | |
1601 | dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber; | |
1602 | dev->quirks = id->driver_info | uvc_quirks_param; | |
1603 | ||
1604 | if (udev->product != NULL) | |
d0ebf307 | 1605 | strlcpy(dev->name, udev->product, sizeof dev->name); |
c0efd232 LP |
1606 | else |
1607 | snprintf(dev->name, sizeof dev->name, | |
1608 | "UVC Camera (%04x:%04x)", | |
1609 | le16_to_cpu(udev->descriptor.idVendor), | |
1610 | le16_to_cpu(udev->descriptor.idProduct)); | |
1611 | ||
2c2d264b | 1612 | /* Parse the Video Class control descriptor. */ |
c0efd232 LP |
1613 | if (uvc_parse_control(dev) < 0) { |
1614 | uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC " | |
1615 | "descriptors.\n"); | |
1616 | goto error; | |
1617 | } | |
1618 | ||
fba4578e | 1619 | uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n", |
c0efd232 LP |
1620 | dev->uvc_version >> 8, dev->uvc_version & 0xff, |
1621 | udev->product ? udev->product : "<unnamed>", | |
1622 | le16_to_cpu(udev->descriptor.idVendor), | |
1623 | le16_to_cpu(udev->descriptor.idProduct)); | |
1624 | ||
1625 | if (uvc_quirks_param != 0) { | |
1626 | uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module " | |
1627 | "parameter for testing purpose.\n", uvc_quirks_param); | |
1628 | uvc_printk(KERN_INFO, "Please report required quirks to the " | |
1629 | "linux-uvc-devel mailing list.\n"); | |
1630 | } | |
1631 | ||
2c2d264b | 1632 | /* Initialize controls. */ |
c0efd232 LP |
1633 | if (uvc_ctrl_init_device(dev) < 0) |
1634 | goto error; | |
1635 | ||
2c2d264b | 1636 | /* Register the video devices. */ |
c0efd232 LP |
1637 | if (uvc_register_video(dev) < 0) |
1638 | goto error; | |
1639 | ||
2c2d264b | 1640 | /* Save our data pointer in the interface data. */ |
c0efd232 LP |
1641 | usb_set_intfdata(intf, dev); |
1642 | ||
2c2d264b | 1643 | /* Initialize the interrupt URB. */ |
c0efd232 LP |
1644 | if ((ret = uvc_status_init(dev)) < 0) { |
1645 | uvc_printk(KERN_INFO, "Unable to initialize the status " | |
1646 | "endpoint (%d), status interrupt will not be " | |
1647 | "supported.\n", ret); | |
1648 | } | |
1649 | ||
1650 | uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n"); | |
1651 | return 0; | |
1652 | ||
1653 | error: | |
1654 | kref_put(&dev->kref, uvc_delete); | |
1655 | return -ENODEV; | |
1656 | } | |
1657 | ||
1658 | static void uvc_disconnect(struct usb_interface *intf) | |
1659 | { | |
1660 | struct uvc_device *dev = usb_get_intfdata(intf); | |
1661 | ||
1662 | /* Set the USB interface data to NULL. This can be done outside the | |
1663 | * lock, as there's no other reader. | |
1664 | */ | |
1665 | usb_set_intfdata(intf, NULL); | |
1666 | ||
1667 | if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOSTREAMING) | |
1668 | return; | |
1669 | ||
1670 | /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide | |
1671 | * lock is needed to prevent uvc_disconnect from releasing its | |
1672 | * reference to the uvc_device instance after uvc_v4l2_open() received | |
1673 | * the pointer to the device (video_devdata) but before it got the | |
1674 | * chance to increase the reference count (kref_get). | |
a9e28585 LP |
1675 | * |
1676 | * Note that the reference can't be released with the lock held, | |
1677 | * otherwise a AB-BA deadlock can occur with videodev_lock that | |
1678 | * videodev acquires in videodev_open() and video_unregister_device(). | |
c0efd232 LP |
1679 | */ |
1680 | mutex_lock(&uvc_driver.open_mutex); | |
c0efd232 | 1681 | dev->state |= UVC_DEV_DISCONNECTED; |
c0efd232 | 1682 | mutex_unlock(&uvc_driver.open_mutex); |
a9e28585 LP |
1683 | |
1684 | kref_put(&dev->kref, uvc_delete); | |
c0efd232 LP |
1685 | } |
1686 | ||
1687 | static int uvc_suspend(struct usb_interface *intf, pm_message_t message) | |
1688 | { | |
1689 | struct uvc_device *dev = usb_get_intfdata(intf); | |
1690 | ||
1691 | uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n", | |
1692 | intf->cur_altsetting->desc.bInterfaceNumber); | |
1693 | ||
1694 | /* Controls are cached on the fly so they don't need to be saved. */ | |
1695 | if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL) | |
1696 | return uvc_status_suspend(dev); | |
1697 | ||
1698 | if (dev->video.streaming->intf != intf) { | |
1699 | uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB " | |
1700 | "interface mismatch.\n"); | |
1701 | return -EINVAL; | |
1702 | } | |
1703 | ||
1704 | return uvc_video_suspend(&dev->video); | |
1705 | } | |
1706 | ||
9b0ae867 | 1707 | static int __uvc_resume(struct usb_interface *intf, int reset) |
c0efd232 LP |
1708 | { |
1709 | struct uvc_device *dev = usb_get_intfdata(intf); | |
c0efd232 LP |
1710 | |
1711 | uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n", | |
1712 | intf->cur_altsetting->desc.bInterfaceNumber); | |
1713 | ||
1714 | if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL) { | |
7564f67d HV |
1715 | if (reset) { |
1716 | int ret = uvc_ctrl_resume_device(dev); | |
1717 | ||
1718 | if (ret < 0) | |
1719 | return ret; | |
1720 | } | |
c0efd232 LP |
1721 | |
1722 | return uvc_status_resume(dev); | |
1723 | } | |
1724 | ||
1725 | if (dev->video.streaming->intf != intf) { | |
1726 | uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB " | |
1727 | "interface mismatch.\n"); | |
1728 | return -EINVAL; | |
1729 | } | |
1730 | ||
1731 | return uvc_video_resume(&dev->video); | |
1732 | } | |
1733 | ||
9b0ae867 LP |
1734 | static int uvc_resume(struct usb_interface *intf) |
1735 | { | |
1736 | return __uvc_resume(intf, 0); | |
1737 | } | |
1738 | ||
1739 | static int uvc_reset_resume(struct usb_interface *intf) | |
1740 | { | |
1741 | return __uvc_resume(intf, 1); | |
1742 | } | |
1743 | ||
c0efd232 LP |
1744 | /* ------------------------------------------------------------------------ |
1745 | * Driver initialization and cleanup | |
1746 | */ | |
1747 | ||
1748 | /* | |
1749 | * The Logitech cameras listed below have their interface class set to | |
1750 | * VENDOR_SPEC because they don't announce themselves as UVC devices, even | |
1751 | * though they are compliant. | |
1752 | */ | |
1753 | static struct usb_device_id uvc_ids[] = { | |
c0efd232 LP |
1754 | /* Microsoft Lifecam NX-6000 */ |
1755 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1756 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1757 | .idVendor = 0x045e, | |
1758 | .idProduct = 0x00f8, | |
1759 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1760 | .bInterfaceSubClass = 1, | |
1761 | .bInterfaceProtocol = 0, | |
1762 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
1763 | /* Microsoft Lifecam VX-7000 */ | |
1764 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1765 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1766 | .idVendor = 0x045e, | |
1767 | .idProduct = 0x0723, | |
1768 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1769 | .bInterfaceSubClass = 1, | |
1770 | .bInterfaceProtocol = 0, | |
1771 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
1772 | /* Logitech Quickcam Fusion */ | |
1773 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1774 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1775 | .idVendor = 0x046d, | |
1776 | .idProduct = 0x08c1, | |
1777 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1778 | .bInterfaceSubClass = 1, | |
1779 | .bInterfaceProtocol = 0 }, | |
1780 | /* Logitech Quickcam Orbit MP */ | |
1781 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1782 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1783 | .idVendor = 0x046d, | |
1784 | .idProduct = 0x08c2, | |
1785 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1786 | .bInterfaceSubClass = 1, | |
1787 | .bInterfaceProtocol = 0 }, | |
1788 | /* Logitech Quickcam Pro for Notebook */ | |
1789 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1790 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1791 | .idVendor = 0x046d, | |
1792 | .idProduct = 0x08c3, | |
1793 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1794 | .bInterfaceSubClass = 1, | |
1795 | .bInterfaceProtocol = 0 }, | |
1796 | /* Logitech Quickcam Pro 5000 */ | |
1797 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1798 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1799 | .idVendor = 0x046d, | |
1800 | .idProduct = 0x08c5, | |
1801 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1802 | .bInterfaceSubClass = 1, | |
1803 | .bInterfaceProtocol = 0 }, | |
1804 | /* Logitech Quickcam OEM Dell Notebook */ | |
1805 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1806 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1807 | .idVendor = 0x046d, | |
1808 | .idProduct = 0x08c6, | |
1809 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1810 | .bInterfaceSubClass = 1, | |
1811 | .bInterfaceProtocol = 0 }, | |
1812 | /* Logitech Quickcam OEM Cisco VT Camera II */ | |
1813 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1814 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1815 | .idVendor = 0x046d, | |
1816 | .idProduct = 0x08c7, | |
1817 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1818 | .bInterfaceSubClass = 1, | |
1819 | .bInterfaceProtocol = 0 }, | |
f61d1d8a LP |
1820 | /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */ |
1821 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1822 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1823 | .idVendor = 0x058f, | |
1824 | .idProduct = 0x3820, | |
1825 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1826 | .bInterfaceSubClass = 1, | |
1827 | .bInterfaceProtocol = 0, | |
1828 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
c0efd232 | 1829 | /* Apple Built-In iSight */ |
2c2d264b | 1830 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
c0efd232 LP |
1831 | | USB_DEVICE_ID_MATCH_INT_INFO, |
1832 | .idVendor = 0x05ac, | |
1833 | .idProduct = 0x8501, | |
2c2d264b LP |
1834 | .bInterfaceClass = USB_CLASS_VIDEO, |
1835 | .bInterfaceSubClass = 1, | |
1836 | .bInterfaceProtocol = 0, | |
c0efd232 LP |
1837 | .driver_info = UVC_QUIRK_PROBE_MINMAX |
1838 | | UVC_QUIRK_BUILTIN_ISIGHT }, | |
1839 | /* Genesys Logic USB 2.0 PC Camera */ | |
2c2d264b | 1840 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
c0efd232 | 1841 | | USB_DEVICE_ID_MATCH_INT_INFO, |
2c2d264b LP |
1842 | .idVendor = 0x05e3, |
1843 | .idProduct = 0x0505, | |
1844 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1845 | .bInterfaceSubClass = 1, | |
1846 | .bInterfaceProtocol = 0, | |
1847 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
50144aee LP |
1848 | /* ViMicro */ |
1849 | { .match_flags = USB_DEVICE_ID_MATCH_VENDOR | |
1850 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1851 | .idVendor = 0x0ac8, | |
1852 | .idProduct = 0x0000, | |
1853 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1854 | .bInterfaceSubClass = 1, | |
1855 | .bInterfaceProtocol = 0, | |
1856 | .driver_info = UVC_QUIRK_FIX_BANDWIDTH }, | |
c0efd232 LP |
1857 | /* MT6227 */ |
1858 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1859 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1860 | .idVendor = 0x0e8d, | |
1861 | .idProduct = 0x0004, | |
1862 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1863 | .bInterfaceSubClass = 1, | |
1864 | .bInterfaceProtocol = 0, | |
1865 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
1866 | /* Syntek (HP Spartan) */ | |
1867 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1868 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1869 | .idVendor = 0x174f, | |
1870 | .idProduct = 0x5212, | |
1871 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1872 | .bInterfaceSubClass = 1, | |
1873 | .bInterfaceProtocol = 0, | |
25e69850 | 1874 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, |
562f0fed LP |
1875 | /* Syntek (Samsung Q310) */ |
1876 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1877 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1878 | .idVendor = 0x174f, | |
1879 | .idProduct = 0x5931, | |
1880 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1881 | .bInterfaceSubClass = 1, | |
1882 | .bInterfaceProtocol = 0, | |
1883 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
f61d1d8a | 1884 | /* Syntek (Asus F9SG) */ |
25e69850 LP |
1885 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1886 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1887 | .idVendor = 0x174f, | |
1888 | .idProduct = 0x8a31, | |
1889 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1890 | .bInterfaceSubClass = 1, | |
1891 | .bInterfaceProtocol = 0, | |
c0efd232 LP |
1892 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, |
1893 | /* Syntek (Asus U3S) */ | |
1894 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1895 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1896 | .idVendor = 0x174f, | |
1897 | .idProduct = 0x8a33, | |
1898 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1899 | .bInterfaceSubClass = 1, | |
1900 | .bInterfaceProtocol = 0, | |
1901 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
0ce566da LP |
1902 | /* Syntek (JAOtech Smart Terminal) */ |
1903 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1904 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1905 | .idVendor = 0x174f, | |
1906 | .idProduct = 0x8a34, | |
1907 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1908 | .bInterfaceSubClass = 1, | |
1909 | .bInterfaceProtocol = 0, | |
1910 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
849a3aba | 1911 | /* Lenovo Thinkpad SL400/SL500 */ |
2f38483b LP |
1912 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1913 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1914 | .idVendor = 0x17ef, | |
1915 | .idProduct = 0x480b, | |
1916 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1917 | .bInterfaceSubClass = 1, | |
1918 | .bInterfaceProtocol = 0, | |
1919 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
c0efd232 LP |
1920 | /* Ecamm Pico iMage */ |
1921 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1922 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1923 | .idVendor = 0x18cd, | |
1924 | .idProduct = 0xcafe, | |
1925 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1926 | .bInterfaceSubClass = 1, | |
1927 | .bInterfaceProtocol = 0, | |
1928 | .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS }, | |
1929 | /* Bodelin ProScopeHR */ | |
1930 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1931 | | USB_DEVICE_ID_MATCH_DEV_HI | |
1932 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1933 | .idVendor = 0x19ab, | |
1934 | .idProduct = 0x1000, | |
1935 | .bcdDevice_hi = 0x0126, | |
1936 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1937 | .bInterfaceSubClass = 1, | |
1938 | .bInterfaceProtocol = 0, | |
1939 | .driver_info = UVC_QUIRK_STATUS_INTERVAL }, | |
1940 | /* SiGma Micro USB Web Camera */ | |
1941 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1942 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1943 | .idVendor = 0x1c4f, | |
1944 | .idProduct = 0x3000, | |
1945 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1946 | .bInterfaceSubClass = 1, | |
1947 | .bInterfaceProtocol = 0, | |
1948 | .driver_info = UVC_QUIRK_PROBE_MINMAX | |
d732c44c | 1949 | | UVC_QUIRK_IGNORE_SELECTOR_UNIT }, |
c0efd232 LP |
1950 | /* Generic USB Video Class */ |
1951 | { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) }, | |
1952 | {} | |
1953 | }; | |
1954 | ||
1955 | MODULE_DEVICE_TABLE(usb, uvc_ids); | |
1956 | ||
1957 | struct uvc_driver uvc_driver = { | |
1958 | .driver = { | |
1959 | .name = "uvcvideo", | |
1960 | .probe = uvc_probe, | |
1961 | .disconnect = uvc_disconnect, | |
1962 | .suspend = uvc_suspend, | |
1963 | .resume = uvc_resume, | |
9b0ae867 | 1964 | .reset_resume = uvc_reset_resume, |
c0efd232 LP |
1965 | .id_table = uvc_ids, |
1966 | .supports_autosuspend = 1, | |
1967 | }, | |
1968 | }; | |
1969 | ||
1970 | static int __init uvc_init(void) | |
1971 | { | |
1972 | int result; | |
1973 | ||
1974 | INIT_LIST_HEAD(&uvc_driver.devices); | |
1975 | INIT_LIST_HEAD(&uvc_driver.controls); | |
1976 | mutex_init(&uvc_driver.open_mutex); | |
1977 | mutex_init(&uvc_driver.ctrl_mutex); | |
1978 | ||
1979 | uvc_ctrl_init(); | |
1980 | ||
1981 | result = usb_register(&uvc_driver.driver); | |
1982 | if (result == 0) | |
1983 | printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n"); | |
1984 | return result; | |
1985 | } | |
1986 | ||
1987 | static void __exit uvc_cleanup(void) | |
1988 | { | |
1989 | usb_deregister(&uvc_driver.driver); | |
1990 | } | |
1991 | ||
1992 | module_init(uvc_init); | |
1993 | module_exit(uvc_cleanup); | |
1994 | ||
0fbd8ee6 LP |
1995 | module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR); |
1996 | MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames"); | |
c0efd232 LP |
1997 | module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR); |
1998 | MODULE_PARM_DESC(quirks, "Forced device quirks"); | |
1999 | module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR); | |
2000 | MODULE_PARM_DESC(trace, "Trace level bitmask"); | |
2001 | ||
2002 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
2003 | MODULE_DESCRIPTION(DRIVER_DESC); | |
2004 | MODULE_LICENSE("GPL"); | |
2005 | MODULE_VERSION(DRIVER_VERSION); | |
f87086e3 | 2006 |