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