]>
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; | |
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) { | |
d0ebf307 | 317 | strlcpy(format->name, fmtdesc->name, |
c0efd232 LP |
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 | ||
d0ebf307 | 348 | strlcpy(format->name, "MJPEG", sizeof format->name); |
c0efd232 LP |
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: | |
d0ebf307 | 366 | strlcpy(format->name, "SD-DV", sizeof format->name); |
c0efd232 LP |
367 | break; |
368 | case 1: | |
d0ebf307 | 369 | strlcpy(format->name, "SDL-DV", sizeof format->name); |
c0efd232 LP |
370 | break; |
371 | case 2: | |
d0ebf307 | 372 | strlcpy(format->name, "HD-DV", sizeof format->name); |
c0efd232 LP |
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 | ||
d0ebf307 | 382 | strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz", |
c0efd232 LP |
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 | |
2c2d264b | 476 | * value to 1.1x the actual frame size to hardwiring the |
c0efd232 LP |
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 | |
2c2d264b | 489 | * some other divisions by zero that could happen. |
c0efd232 LP |
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. */ | |
ff924203 LP |
611 | switch (buffer[2]) { |
612 | case VS_OUTPUT_HEADER: | |
613 | streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
614 | size = 9; | |
615 | break; | |
616 | ||
617 | case VS_INPUT_HEADER: | |
618 | streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
619 | size = 13; | |
620 | break; | |
621 | ||
622 | default: | |
c0efd232 | 623 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface " |
ff924203 LP |
624 | "%d HEADER descriptor not found.\n", dev->udev->devnum, |
625 | alts->desc.bInterfaceNumber); | |
c0efd232 | 626 | goto error; |
ff924203 | 627 | } |
c0efd232 | 628 | |
ff924203 LP |
629 | p = buflen >= 4 ? buffer[3] : 0; |
630 | n = buflen >= size ? buffer[size-1] : 0; | |
631 | ||
632 | if (buflen < size + p*n) { | |
633 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " | |
634 | "interface %d HEADER descriptor is invalid.\n", | |
635 | dev->udev->devnum, alts->desc.bInterfaceNumber); | |
636 | goto error; | |
637 | } | |
c0efd232 | 638 | |
ff924203 LP |
639 | streaming->header.bNumFormats = p; |
640 | streaming->header.bEndpointAddress = buffer[6]; | |
641 | if (buffer[2] == VS_INPUT_HEADER) { | |
c0efd232 LP |
642 | streaming->header.bmInfo = buffer[7]; |
643 | streaming->header.bTerminalLink = buffer[8]; | |
644 | streaming->header.bStillCaptureMethod = buffer[9]; | |
645 | streaming->header.bTriggerSupport = buffer[10]; | |
646 | streaming->header.bTriggerUsage = buffer[11]; | |
c0efd232 | 647 | } else { |
ff924203 LP |
648 | streaming->header.bTerminalLink = buffer[7]; |
649 | } | |
650 | streaming->header.bControlSize = n; | |
651 | ||
652 | streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL); | |
653 | if (streaming->header.bmaControls == NULL) { | |
654 | ret = -ENOMEM; | |
c0efd232 LP |
655 | goto error; |
656 | } | |
657 | ||
ff924203 LP |
658 | memcpy(streaming->header.bmaControls, &buffer[size], p*n); |
659 | ||
c0efd232 LP |
660 | buflen -= buffer[0]; |
661 | buffer += buffer[0]; | |
662 | ||
663 | _buffer = buffer; | |
664 | _buflen = buflen; | |
665 | ||
666 | /* Count the format and frame descriptors. */ | |
667 | while (_buflen > 2) { | |
668 | switch (_buffer[2]) { | |
669 | case VS_FORMAT_UNCOMPRESSED: | |
670 | case VS_FORMAT_MJPEG: | |
671 | case VS_FORMAT_FRAME_BASED: | |
672 | nformats++; | |
673 | break; | |
674 | ||
675 | case VS_FORMAT_DV: | |
676 | /* DV format has no frame descriptor. We will create a | |
677 | * dummy frame descriptor with a dummy frame interval. | |
678 | */ | |
679 | nformats++; | |
680 | nframes++; | |
681 | nintervals++; | |
682 | break; | |
683 | ||
684 | case VS_FORMAT_MPEG2TS: | |
685 | case VS_FORMAT_STREAM_BASED: | |
686 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " | |
687 | "interface %d FORMAT %u is not supported.\n", | |
688 | dev->udev->devnum, | |
689 | alts->desc.bInterfaceNumber, _buffer[2]); | |
690 | break; | |
691 | ||
692 | case VS_FRAME_UNCOMPRESSED: | |
693 | case VS_FRAME_MJPEG: | |
694 | nframes++; | |
695 | if (_buflen > 25) | |
696 | nintervals += _buffer[25] ? _buffer[25] : 3; | |
697 | break; | |
698 | ||
699 | case VS_FRAME_FRAME_BASED: | |
700 | nframes++; | |
701 | if (_buflen > 21) | |
702 | nintervals += _buffer[21] ? _buffer[21] : 3; | |
703 | break; | |
704 | } | |
705 | ||
706 | _buflen -= _buffer[0]; | |
707 | _buffer += _buffer[0]; | |
708 | } | |
709 | ||
710 | if (nformats == 0) { | |
711 | uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface " | |
712 | "%d has no supported formats defined.\n", | |
713 | dev->udev->devnum, alts->desc.bInterfaceNumber); | |
714 | goto error; | |
715 | } | |
716 | ||
717 | size = nformats * sizeof *format + nframes * sizeof *frame | |
718 | + nintervals * sizeof *interval; | |
719 | format = kzalloc(size, GFP_KERNEL); | |
720 | if (format == NULL) { | |
721 | ret = -ENOMEM; | |
722 | goto error; | |
723 | } | |
724 | ||
725 | frame = (struct uvc_frame *)&format[nformats]; | |
726 | interval = (__u32 *)&frame[nframes]; | |
727 | ||
728 | streaming->format = format; | |
729 | streaming->nformats = nformats; | |
730 | ||
731 | /* Parse the format descriptors. */ | |
732 | while (buflen > 2) { | |
733 | switch (buffer[2]) { | |
734 | case VS_FORMAT_UNCOMPRESSED: | |
735 | case VS_FORMAT_MJPEG: | |
736 | case VS_FORMAT_DV: | |
737 | case VS_FORMAT_FRAME_BASED: | |
738 | format->frame = frame; | |
739 | ret = uvc_parse_format(dev, streaming, format, | |
740 | &interval, buffer, buflen); | |
741 | if (ret < 0) | |
742 | goto error; | |
743 | ||
744 | frame += format->nframes; | |
745 | format++; | |
746 | ||
747 | buflen -= ret; | |
748 | buffer += ret; | |
749 | continue; | |
750 | ||
751 | default: | |
752 | break; | |
753 | } | |
754 | ||
755 | buflen -= buffer[0]; | |
756 | buffer += buffer[0]; | |
757 | } | |
758 | ||
759 | /* Parse the alternate settings to find the maximum bandwidth. */ | |
760 | for (i = 0; i < intf->num_altsetting; ++i) { | |
761 | struct usb_host_endpoint *ep; | |
762 | alts = &intf->altsetting[i]; | |
763 | ep = uvc_find_endpoint(alts, | |
764 | streaming->header.bEndpointAddress); | |
765 | if (ep == NULL) | |
766 | continue; | |
767 | ||
768 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); | |
769 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); | |
770 | if (psize > streaming->maxpsize) | |
771 | streaming->maxpsize = psize; | |
772 | } | |
773 | ||
774 | list_add_tail(&streaming->list, &dev->streaming); | |
775 | return 0; | |
776 | ||
777 | error: | |
778 | usb_driver_release_interface(&uvc_driver.driver, intf); | |
779 | usb_put_intf(intf); | |
780 | kfree(streaming->format); | |
781 | kfree(streaming->header.bmaControls); | |
782 | kfree(streaming); | |
783 | return ret; | |
784 | } | |
785 | ||
786 | /* Parse vendor-specific extensions. */ | |
787 | static int uvc_parse_vendor_control(struct uvc_device *dev, | |
788 | const unsigned char *buffer, int buflen) | |
789 | { | |
790 | struct usb_device *udev = dev->udev; | |
791 | struct usb_host_interface *alts = dev->intf->cur_altsetting; | |
792 | struct uvc_entity *unit; | |
793 | unsigned int n, p; | |
794 | int handled = 0; | |
795 | ||
796 | switch (le16_to_cpu(dev->udev->descriptor.idVendor)) { | |
797 | case 0x046d: /* Logitech */ | |
798 | if (buffer[1] != 0x41 || buffer[2] != 0x01) | |
799 | break; | |
800 | ||
801 | /* Logitech implements several vendor specific functions | |
802 | * through vendor specific extension units (LXU). | |
803 | * | |
804 | * The LXU descriptors are similar to XU descriptors | |
805 | * (see "USB Device Video Class for Video Devices", section | |
806 | * 3.7.2.6 "Extension Unit Descriptor") with the following | |
807 | * differences: | |
808 | * | |
809 | * ---------------------------------------------------------- | |
810 | * 0 bLength 1 Number | |
811 | * Size of this descriptor, in bytes: 24+p+n*2 | |
812 | * ---------------------------------------------------------- | |
813 | * 23+p+n bmControlsType N Bitmap | |
814 | * Individual bits in the set are defined: | |
815 | * 0: Absolute | |
816 | * 1: Relative | |
817 | * | |
818 | * This bitset is mapped exactly the same as bmControls. | |
819 | * ---------------------------------------------------------- | |
820 | * 23+p+n*2 bReserved 1 Boolean | |
821 | * ---------------------------------------------------------- | |
822 | * 24+p+n*2 iExtension 1 Index | |
823 | * Index of a string descriptor that describes this | |
824 | * extension unit. | |
825 | * ---------------------------------------------------------- | |
826 | */ | |
827 | p = buflen >= 22 ? buffer[21] : 0; | |
828 | n = buflen >= 25 + p ? buffer[22+p] : 0; | |
829 | ||
830 | if (buflen < 25 + p + 2*n) { | |
831 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
832 | "interface %d EXTENSION_UNIT error\n", | |
833 | udev->devnum, alts->desc.bInterfaceNumber); | |
834 | break; | |
835 | } | |
836 | ||
837 | unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL); | |
838 | if (unit == NULL) | |
839 | return -ENOMEM; | |
840 | ||
841 | unit->id = buffer[3]; | |
842 | unit->type = VC_EXTENSION_UNIT; | |
843 | memcpy(unit->extension.guidExtensionCode, &buffer[4], 16); | |
844 | unit->extension.bNumControls = buffer[20]; | |
9bc6218d | 845 | unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]); |
c0efd232 LP |
846 | unit->extension.baSourceID = (__u8 *)unit + sizeof *unit; |
847 | memcpy(unit->extension.baSourceID, &buffer[22], p); | |
848 | unit->extension.bControlSize = buffer[22+p]; | |
849 | unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p; | |
850 | unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit | |
851 | + p + n; | |
852 | memcpy(unit->extension.bmControls, &buffer[23+p], 2*n); | |
853 | ||
854 | if (buffer[24+p+2*n] != 0) | |
855 | usb_string(udev, buffer[24+p+2*n], unit->name, | |
856 | sizeof unit->name); | |
857 | else | |
858 | sprintf(unit->name, "Extension %u", buffer[3]); | |
859 | ||
860 | list_add_tail(&unit->list, &dev->entities); | |
861 | handled = 1; | |
862 | break; | |
863 | } | |
864 | ||
865 | return handled; | |
866 | } | |
867 | ||
868 | static int uvc_parse_standard_control(struct uvc_device *dev, | |
869 | const unsigned char *buffer, int buflen) | |
870 | { | |
871 | struct usb_device *udev = dev->udev; | |
872 | struct uvc_entity *unit, *term; | |
873 | struct usb_interface *intf; | |
874 | struct usb_host_interface *alts = dev->intf->cur_altsetting; | |
875 | unsigned int i, n, p, len; | |
876 | __u16 type; | |
877 | ||
878 | switch (buffer[2]) { | |
879 | case VC_HEADER: | |
880 | n = buflen >= 12 ? buffer[11] : 0; | |
881 | ||
882 | if (buflen < 12 || buflen < 12 + n) { | |
883 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
884 | "interface %d HEADER error\n", udev->devnum, | |
885 | alts->desc.bInterfaceNumber); | |
886 | return -EINVAL; | |
887 | } | |
888 | ||
9bc6218d MH |
889 | dev->uvc_version = get_unaligned_le16(&buffer[3]); |
890 | dev->clock_frequency = get_unaligned_le32(&buffer[7]); | |
c0efd232 LP |
891 | |
892 | /* Parse all USB Video Streaming interfaces. */ | |
893 | for (i = 0; i < n; ++i) { | |
894 | intf = usb_ifnum_to_if(udev, buffer[12+i]); | |
895 | if (intf == NULL) { | |
896 | uvc_trace(UVC_TRACE_DESCR, "device %d " | |
897 | "interface %d doesn't exists\n", | |
898 | udev->devnum, i); | |
899 | continue; | |
900 | } | |
901 | ||
902 | uvc_parse_streaming(dev, intf); | |
903 | } | |
904 | break; | |
905 | ||
906 | case VC_INPUT_TERMINAL: | |
907 | if (buflen < 8) { | |
908 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
909 | "interface %d INPUT_TERMINAL error\n", | |
910 | udev->devnum, alts->desc.bInterfaceNumber); | |
911 | return -EINVAL; | |
912 | } | |
913 | ||
914 | /* Make sure the terminal type MSB is not null, otherwise it | |
915 | * could be confused with a unit. | |
916 | */ | |
9bc6218d | 917 | type = get_unaligned_le16(&buffer[4]); |
c0efd232 LP |
918 | if ((type & 0xff00) == 0) { |
919 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
920 | "interface %d INPUT_TERMINAL %d has invalid " | |
921 | "type 0x%04x, skipping\n", udev->devnum, | |
922 | alts->desc.bInterfaceNumber, | |
923 | buffer[3], type); | |
924 | return 0; | |
925 | } | |
926 | ||
927 | n = 0; | |
928 | p = 0; | |
929 | len = 8; | |
930 | ||
931 | if (type == ITT_CAMERA) { | |
932 | n = buflen >= 15 ? buffer[14] : 0; | |
933 | len = 15; | |
934 | ||
935 | } else if (type == ITT_MEDIA_TRANSPORT_INPUT) { | |
936 | n = buflen >= 9 ? buffer[8] : 0; | |
937 | p = buflen >= 10 + n ? buffer[9+n] : 0; | |
938 | len = 10; | |
939 | } | |
940 | ||
941 | if (buflen < len + n + p) { | |
942 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
943 | "interface %d INPUT_TERMINAL error\n", | |
944 | udev->devnum, alts->desc.bInterfaceNumber); | |
945 | return -EINVAL; | |
946 | } | |
947 | ||
948 | term = kzalloc(sizeof *term + n + p, GFP_KERNEL); | |
949 | if (term == NULL) | |
950 | return -ENOMEM; | |
951 | ||
952 | term->id = buffer[3]; | |
953 | term->type = type | UVC_TERM_INPUT; | |
954 | ||
955 | if (UVC_ENTITY_TYPE(term) == ITT_CAMERA) { | |
956 | term->camera.bControlSize = n; | |
957 | term->camera.bmControls = (__u8 *)term + sizeof *term; | |
958 | term->camera.wObjectiveFocalLengthMin = | |
9bc6218d | 959 | get_unaligned_le16(&buffer[8]); |
c0efd232 | 960 | term->camera.wObjectiveFocalLengthMax = |
9bc6218d | 961 | get_unaligned_le16(&buffer[10]); |
c0efd232 | 962 | term->camera.wOcularFocalLength = |
9bc6218d | 963 | get_unaligned_le16(&buffer[12]); |
c0efd232 LP |
964 | memcpy(term->camera.bmControls, &buffer[15], n); |
965 | } else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT) { | |
966 | term->media.bControlSize = n; | |
967 | term->media.bmControls = (__u8 *)term + sizeof *term; | |
968 | term->media.bTransportModeSize = p; | |
969 | term->media.bmTransportModes = (__u8 *)term | |
970 | + sizeof *term + n; | |
971 | memcpy(term->media.bmControls, &buffer[9], n); | |
972 | memcpy(term->media.bmTransportModes, &buffer[10+n], p); | |
973 | } | |
974 | ||
975 | if (buffer[7] != 0) | |
976 | usb_string(udev, buffer[7], term->name, | |
977 | sizeof term->name); | |
978 | else if (UVC_ENTITY_TYPE(term) == ITT_CAMERA) | |
979 | sprintf(term->name, "Camera %u", buffer[3]); | |
980 | else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT) | |
981 | sprintf(term->name, "Media %u", buffer[3]); | |
982 | else | |
983 | sprintf(term->name, "Input %u", buffer[3]); | |
984 | ||
985 | list_add_tail(&term->list, &dev->entities); | |
986 | break; | |
987 | ||
988 | case VC_OUTPUT_TERMINAL: | |
989 | if (buflen < 9) { | |
990 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
991 | "interface %d OUTPUT_TERMINAL error\n", | |
992 | udev->devnum, alts->desc.bInterfaceNumber); | |
993 | return -EINVAL; | |
994 | } | |
995 | ||
996 | /* Make sure the terminal type MSB is not null, otherwise it | |
997 | * could be confused with a unit. | |
998 | */ | |
9bc6218d | 999 | type = get_unaligned_le16(&buffer[4]); |
c0efd232 LP |
1000 | if ((type & 0xff00) == 0) { |
1001 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
1002 | "interface %d OUTPUT_TERMINAL %d has invalid " | |
1003 | "type 0x%04x, skipping\n", udev->devnum, | |
1004 | alts->desc.bInterfaceNumber, buffer[3], type); | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | term = kzalloc(sizeof *term, GFP_KERNEL); | |
1009 | if (term == NULL) | |
1010 | return -ENOMEM; | |
1011 | ||
1012 | term->id = buffer[3]; | |
1013 | term->type = type | UVC_TERM_OUTPUT; | |
1014 | term->output.bSourceID = buffer[7]; | |
1015 | ||
1016 | if (buffer[8] != 0) | |
1017 | usb_string(udev, buffer[8], term->name, | |
1018 | sizeof term->name); | |
1019 | else | |
1020 | sprintf(term->name, "Output %u", buffer[3]); | |
1021 | ||
1022 | list_add_tail(&term->list, &dev->entities); | |
1023 | break; | |
1024 | ||
1025 | case VC_SELECTOR_UNIT: | |
1026 | p = buflen >= 5 ? buffer[4] : 0; | |
1027 | ||
1028 | if (buflen < 5 || buflen < 6 + p) { | |
1029 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
1030 | "interface %d SELECTOR_UNIT error\n", | |
1031 | udev->devnum, alts->desc.bInterfaceNumber); | |
1032 | return -EINVAL; | |
1033 | } | |
1034 | ||
1035 | unit = kzalloc(sizeof *unit + p, GFP_KERNEL); | |
1036 | if (unit == NULL) | |
1037 | return -ENOMEM; | |
1038 | ||
1039 | unit->id = buffer[3]; | |
1040 | unit->type = buffer[2]; | |
1041 | unit->selector.bNrInPins = buffer[4]; | |
1042 | unit->selector.baSourceID = (__u8 *)unit + sizeof *unit; | |
1043 | memcpy(unit->selector.baSourceID, &buffer[5], p); | |
1044 | ||
1045 | if (buffer[5+p] != 0) | |
1046 | usb_string(udev, buffer[5+p], unit->name, | |
1047 | sizeof unit->name); | |
1048 | else | |
1049 | sprintf(unit->name, "Selector %u", buffer[3]); | |
1050 | ||
1051 | list_add_tail(&unit->list, &dev->entities); | |
1052 | break; | |
1053 | ||
1054 | case VC_PROCESSING_UNIT: | |
1055 | n = buflen >= 8 ? buffer[7] : 0; | |
1056 | p = dev->uvc_version >= 0x0110 ? 10 : 9; | |
1057 | ||
1058 | if (buflen < p + n) { | |
1059 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
1060 | "interface %d PROCESSING_UNIT error\n", | |
1061 | udev->devnum, alts->desc.bInterfaceNumber); | |
1062 | return -EINVAL; | |
1063 | } | |
1064 | ||
1065 | unit = kzalloc(sizeof *unit + n, GFP_KERNEL); | |
1066 | if (unit == NULL) | |
1067 | return -ENOMEM; | |
1068 | ||
1069 | unit->id = buffer[3]; | |
1070 | unit->type = buffer[2]; | |
1071 | unit->processing.bSourceID = buffer[4]; | |
1072 | unit->processing.wMaxMultiplier = | |
9bc6218d | 1073 | get_unaligned_le16(&buffer[5]); |
c0efd232 LP |
1074 | unit->processing.bControlSize = buffer[7]; |
1075 | unit->processing.bmControls = (__u8 *)unit + sizeof *unit; | |
1076 | memcpy(unit->processing.bmControls, &buffer[8], n); | |
1077 | if (dev->uvc_version >= 0x0110) | |
1078 | unit->processing.bmVideoStandards = buffer[9+n]; | |
1079 | ||
1080 | if (buffer[8+n] != 0) | |
1081 | usb_string(udev, buffer[8+n], unit->name, | |
1082 | sizeof unit->name); | |
1083 | else | |
1084 | sprintf(unit->name, "Processing %u", buffer[3]); | |
1085 | ||
1086 | list_add_tail(&unit->list, &dev->entities); | |
1087 | break; | |
1088 | ||
1089 | case VC_EXTENSION_UNIT: | |
1090 | p = buflen >= 22 ? buffer[21] : 0; | |
1091 | n = buflen >= 24 + p ? buffer[22+p] : 0; | |
1092 | ||
1093 | if (buflen < 24 + p + n) { | |
1094 | uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " | |
1095 | "interface %d EXTENSION_UNIT error\n", | |
1096 | udev->devnum, alts->desc.bInterfaceNumber); | |
1097 | return -EINVAL; | |
1098 | } | |
1099 | ||
1100 | unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL); | |
1101 | if (unit == NULL) | |
1102 | return -ENOMEM; | |
1103 | ||
1104 | unit->id = buffer[3]; | |
1105 | unit->type = buffer[2]; | |
1106 | memcpy(unit->extension.guidExtensionCode, &buffer[4], 16); | |
1107 | unit->extension.bNumControls = buffer[20]; | |
9bc6218d | 1108 | unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]); |
c0efd232 LP |
1109 | unit->extension.baSourceID = (__u8 *)unit + sizeof *unit; |
1110 | memcpy(unit->extension.baSourceID, &buffer[22], p); | |
1111 | unit->extension.bControlSize = buffer[22+p]; | |
1112 | unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p; | |
1113 | memcpy(unit->extension.bmControls, &buffer[23+p], n); | |
1114 | ||
1115 | if (buffer[23+p+n] != 0) | |
1116 | usb_string(udev, buffer[23+p+n], unit->name, | |
1117 | sizeof unit->name); | |
1118 | else | |
1119 | sprintf(unit->name, "Extension %u", buffer[3]); | |
1120 | ||
1121 | list_add_tail(&unit->list, &dev->entities); | |
1122 | break; | |
1123 | ||
1124 | default: | |
1125 | uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE " | |
1126 | "descriptor (%u)\n", buffer[2]); | |
1127 | break; | |
1128 | } | |
1129 | ||
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | static int uvc_parse_control(struct uvc_device *dev) | |
1134 | { | |
1135 | struct usb_host_interface *alts = dev->intf->cur_altsetting; | |
1136 | unsigned char *buffer = alts->extra; | |
1137 | int buflen = alts->extralen; | |
1138 | int ret; | |
1139 | ||
1140 | /* Parse the default alternate setting only, as the UVC specification | |
1141 | * defines a single alternate setting, the default alternate setting | |
1142 | * zero. | |
1143 | */ | |
1144 | ||
1145 | while (buflen > 2) { | |
1146 | if (uvc_parse_vendor_control(dev, buffer, buflen) || | |
1147 | buffer[1] != USB_DT_CS_INTERFACE) | |
1148 | goto next_descriptor; | |
1149 | ||
1150 | if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0) | |
1151 | return ret; | |
1152 | ||
1153 | next_descriptor: | |
1154 | buflen -= buffer[0]; | |
1155 | buffer += buffer[0]; | |
1156 | } | |
1157 | ||
538e7a00 LP |
1158 | /* Check if the optional status endpoint is present. Built-in iSight |
1159 | * webcams have an interrupt endpoint but spit proprietary data that | |
1160 | * don't conform to the UVC status endpoint messages. Don't try to | |
1161 | * handle the interrupt endpoint for those cameras. | |
1162 | */ | |
1163 | if (alts->desc.bNumEndpoints == 1 && | |
1164 | !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) { | |
c0efd232 LP |
1165 | struct usb_host_endpoint *ep = &alts->endpoint[0]; |
1166 | struct usb_endpoint_descriptor *desc = &ep->desc; | |
1167 | ||
1168 | if (usb_endpoint_is_int_in(desc) && | |
1169 | le16_to_cpu(desc->wMaxPacketSize) >= 8 && | |
1170 | desc->bInterval != 0) { | |
1171 | uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint " | |
1172 | "(addr %02x).\n", desc->bEndpointAddress); | |
1173 | dev->int_ep = ep; | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | /* ------------------------------------------------------------------------ | |
1181 | * USB probe and disconnect | |
1182 | */ | |
1183 | ||
1184 | /* | |
1185 | * Unregister the video devices. | |
1186 | */ | |
1187 | static void uvc_unregister_video(struct uvc_device *dev) | |
1188 | { | |
1189 | if (dev->video.vdev) { | |
1190 | if (dev->video.vdev->minor == -1) | |
1191 | video_device_release(dev->video.vdev); | |
1192 | else | |
1193 | video_unregister_device(dev->video.vdev); | |
1194 | dev->video.vdev = NULL; | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | /* | |
1199 | * Scan the UVC descriptors to locate a chain starting at an Output Terminal | |
1200 | * and containing the following units: | |
1201 | * | |
2c2d264b | 1202 | * - one Output Terminal (USB Streaming or Display) |
c0efd232 LP |
1203 | * - zero or one Processing Unit |
1204 | * - zero, one or mode single-input Selector Units | |
1205 | * - zero or one multiple-input Selector Units, provided all inputs are | |
1206 | * connected to input terminals | |
1207 | * - zero, one or mode single-input Extension Units | |
2c2d264b | 1208 | * - one or more Input Terminals (Camera, External or USB Streaming) |
c0efd232 LP |
1209 | * |
1210 | * A side forward scan is made on each detected entity to check for additional | |
1211 | * extension units. | |
1212 | */ | |
1213 | static int uvc_scan_chain_entity(struct uvc_video_device *video, | |
1214 | struct uvc_entity *entity) | |
1215 | { | |
1216 | switch (UVC_ENTITY_TYPE(entity)) { | |
1217 | case VC_EXTENSION_UNIT: | |
1218 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1219 | printk(" <- XU %d", entity->id); | |
1220 | ||
1221 | if (entity->extension.bNrInPins != 1) { | |
1222 | uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more " | |
1223 | "than 1 input pin.\n", entity->id); | |
1224 | return -1; | |
1225 | } | |
1226 | ||
1227 | list_add_tail(&entity->chain, &video->extensions); | |
1228 | break; | |
1229 | ||
1230 | case VC_PROCESSING_UNIT: | |
1231 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1232 | printk(" <- PU %d", entity->id); | |
1233 | ||
1234 | if (video->processing != NULL) { | |
1235 | uvc_trace(UVC_TRACE_DESCR, "Found multiple " | |
1236 | "Processing Units in chain.\n"); | |
1237 | return -1; | |
1238 | } | |
1239 | ||
1240 | video->processing = entity; | |
1241 | break; | |
1242 | ||
1243 | case VC_SELECTOR_UNIT: | |
1244 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1245 | printk(" <- SU %d", entity->id); | |
1246 | ||
1247 | /* Single-input selector units are ignored. */ | |
1248 | if (entity->selector.bNrInPins == 1) | |
1249 | break; | |
1250 | ||
1251 | if (video->selector != NULL) { | |
1252 | uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector " | |
1253 | "Units in chain.\n"); | |
1254 | return -1; | |
1255 | } | |
1256 | ||
1257 | video->selector = entity; | |
1258 | break; | |
1259 | ||
1260 | case ITT_VENDOR_SPECIFIC: | |
1261 | case ITT_CAMERA: | |
1262 | case ITT_MEDIA_TRANSPORT_INPUT: | |
1263 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1264 | printk(" <- IT %d\n", entity->id); | |
1265 | ||
1266 | list_add_tail(&entity->chain, &video->iterms); | |
1267 | break; | |
1268 | ||
ff924203 LP |
1269 | case TT_STREAMING: |
1270 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1271 | printk(" <- IT %d\n", entity->id); | |
1272 | ||
1273 | if (!UVC_ENTITY_IS_ITERM(entity)) { | |
1274 | uvc_trace(UVC_TRACE_DESCR, "Unsupported input " | |
1275 | "terminal %u.\n", entity->id); | |
1276 | return -1; | |
1277 | } | |
1278 | ||
1279 | if (video->sterm != NULL) { | |
1280 | uvc_trace(UVC_TRACE_DESCR, "Found multiple streaming " | |
1281 | "entities in chain.\n"); | |
1282 | return -1; | |
1283 | } | |
1284 | ||
1285 | list_add_tail(&entity->chain, &video->iterms); | |
1286 | video->sterm = entity; | |
1287 | break; | |
1288 | ||
c0efd232 LP |
1289 | default: |
1290 | uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type " | |
1291 | "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity)); | |
1292 | return -1; | |
1293 | } | |
1294 | ||
1295 | return 0; | |
1296 | } | |
1297 | ||
1298 | static int uvc_scan_chain_forward(struct uvc_video_device *video, | |
1299 | struct uvc_entity *entity, struct uvc_entity *prev) | |
1300 | { | |
1301 | struct uvc_entity *forward; | |
1302 | int found; | |
1303 | ||
1304 | /* Forward scan */ | |
1305 | forward = NULL; | |
1306 | found = 0; | |
1307 | ||
1308 | while (1) { | |
1309 | forward = uvc_entity_by_reference(video->dev, entity->id, | |
1310 | forward); | |
1311 | if (forward == NULL) | |
1312 | break; | |
1313 | ||
1314 | if (UVC_ENTITY_TYPE(forward) != VC_EXTENSION_UNIT || | |
1315 | forward == prev) | |
1316 | continue; | |
1317 | ||
1318 | if (forward->extension.bNrInPins != 1) { | |
1319 | uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has" | |
1320 | "more than 1 input pin.\n", entity->id); | |
1321 | return -1; | |
1322 | } | |
1323 | ||
1324 | list_add_tail(&forward->chain, &video->extensions); | |
1325 | if (uvc_trace_param & UVC_TRACE_PROBE) { | |
1326 | if (!found) | |
1327 | printk(" (-> XU"); | |
1328 | ||
1329 | printk(" %d", forward->id); | |
1330 | found = 1; | |
1331 | } | |
1332 | } | |
1333 | if (found) | |
1334 | printk(")"); | |
1335 | ||
1336 | return 0; | |
1337 | } | |
1338 | ||
1339 | static int uvc_scan_chain_backward(struct uvc_video_device *video, | |
1340 | struct uvc_entity *entity) | |
1341 | { | |
1342 | struct uvc_entity *term; | |
1343 | int id = -1, i; | |
1344 | ||
1345 | switch (UVC_ENTITY_TYPE(entity)) { | |
1346 | case VC_EXTENSION_UNIT: | |
1347 | id = entity->extension.baSourceID[0]; | |
1348 | break; | |
1349 | ||
1350 | case VC_PROCESSING_UNIT: | |
1351 | id = entity->processing.bSourceID; | |
1352 | break; | |
1353 | ||
1354 | case VC_SELECTOR_UNIT: | |
1355 | /* Single-input selector units are ignored. */ | |
1356 | if (entity->selector.bNrInPins == 1) { | |
1357 | id = entity->selector.baSourceID[0]; | |
1358 | break; | |
1359 | } | |
1360 | ||
1361 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1362 | printk(" <- IT"); | |
1363 | ||
1364 | video->selector = entity; | |
1365 | for (i = 0; i < entity->selector.bNrInPins; ++i) { | |
1366 | id = entity->selector.baSourceID[i]; | |
1367 | term = uvc_entity_by_id(video->dev, id); | |
1368 | if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) { | |
1369 | uvc_trace(UVC_TRACE_DESCR, "Selector unit %d " | |
1370 | "input %d isn't connected to an " | |
1371 | "input terminal\n", entity->id, i); | |
1372 | return -1; | |
1373 | } | |
1374 | ||
1375 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1376 | printk(" %d", term->id); | |
1377 | ||
1378 | list_add_tail(&term->chain, &video->iterms); | |
1379 | uvc_scan_chain_forward(video, term, entity); | |
1380 | } | |
1381 | ||
1382 | if (uvc_trace_param & UVC_TRACE_PROBE) | |
1383 | printk("\n"); | |
1384 | ||
1385 | id = 0; | |
1386 | break; | |
1387 | } | |
1388 | ||
1389 | return id; | |
1390 | } | |
1391 | ||
1392 | static int uvc_scan_chain(struct uvc_video_device *video) | |
1393 | { | |
1394 | struct uvc_entity *entity, *prev; | |
1395 | int id; | |
1396 | ||
1397 | entity = video->oterm; | |
1398 | uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id); | |
ff924203 LP |
1399 | |
1400 | if (UVC_ENTITY_TYPE(entity) == TT_STREAMING) | |
1401 | video->sterm = entity; | |
1402 | ||
c0efd232 LP |
1403 | id = entity->output.bSourceID; |
1404 | while (id != 0) { | |
1405 | prev = entity; | |
1406 | entity = uvc_entity_by_id(video->dev, id); | |
1407 | if (entity == NULL) { | |
1408 | uvc_trace(UVC_TRACE_DESCR, "Found reference to " | |
1409 | "unknown entity %d.\n", id); | |
1410 | return -1; | |
1411 | } | |
1412 | ||
1413 | /* Process entity */ | |
1414 | if (uvc_scan_chain_entity(video, entity) < 0) | |
1415 | return -1; | |
1416 | ||
1417 | /* Forward scan */ | |
1418 | if (uvc_scan_chain_forward(video, entity, prev) < 0) | |
1419 | return -1; | |
1420 | ||
1421 | /* Stop when a terminal is found. */ | |
1422 | if (!UVC_ENTITY_IS_UNIT(entity)) | |
1423 | break; | |
1424 | ||
1425 | /* Backward scan */ | |
1426 | id = uvc_scan_chain_backward(video, entity); | |
1427 | if (id < 0) | |
1428 | return id; | |
1429 | } | |
1430 | ||
ff924203 LP |
1431 | if (video->sterm == NULL) { |
1432 | uvc_trace(UVC_TRACE_DESCR, "No streaming entity found in " | |
1433 | "chain.\n"); | |
1434 | return -1; | |
1435 | } | |
c0efd232 LP |
1436 | |
1437 | return 0; | |
1438 | } | |
1439 | ||
1440 | /* | |
1441 | * Register the video devices. | |
1442 | * | |
1443 | * The driver currently supports a single video device per control interface | |
1444 | * only. The terminal and units must match the following structure: | |
1445 | * | |
ff924203 LP |
1446 | * ITT_* -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING |
1447 | * TT_STREAMING -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> OTT_* | |
c0efd232 LP |
1448 | * |
1449 | * The Extension Units, if present, must have a single input pin. The | |
1450 | * Processing Unit and Extension Units can be in any order. Additional | |
1451 | * Extension Units connected to the main chain as single-unit branches are | |
1452 | * also supported. | |
1453 | */ | |
1454 | static int uvc_register_video(struct uvc_device *dev) | |
1455 | { | |
1456 | struct video_device *vdev; | |
1457 | struct uvc_entity *term; | |
1458 | int found = 0, ret; | |
1459 | ||
1460 | /* Check if the control interface matches the structure we expect. */ | |
1461 | list_for_each_entry(term, &dev->entities, list) { | |
1462 | struct uvc_streaming *streaming; | |
1463 | ||
ff924203 | 1464 | if (!UVC_ENTITY_IS_TERM(term) || !UVC_ENTITY_IS_OTERM(term)) |
c0efd232 LP |
1465 | continue; |
1466 | ||
1467 | memset(&dev->video, 0, sizeof dev->video); | |
1468 | mutex_init(&dev->video.ctrl_mutex); | |
1469 | INIT_LIST_HEAD(&dev->video.iterms); | |
1470 | INIT_LIST_HEAD(&dev->video.extensions); | |
1471 | dev->video.oterm = term; | |
1472 | dev->video.dev = dev; | |
1473 | if (uvc_scan_chain(&dev->video) < 0) | |
1474 | continue; | |
1475 | ||
1476 | list_for_each_entry(streaming, &dev->streaming, list) { | |
ff924203 LP |
1477 | if (streaming->header.bTerminalLink == |
1478 | dev->video.sterm->id) { | |
c0efd232 LP |
1479 | dev->video.streaming = streaming; |
1480 | found = 1; | |
1481 | break; | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | if (found) | |
1486 | break; | |
1487 | } | |
1488 | ||
1489 | if (!found) { | |
1490 | uvc_printk(KERN_INFO, "No valid video chain found.\n"); | |
1491 | return -1; | |
1492 | } | |
1493 | ||
1494 | if (uvc_trace_param & UVC_TRACE_PROBE) { | |
1495 | uvc_printk(KERN_INFO, "Found a valid video chain ("); | |
1496 | list_for_each_entry(term, &dev->video.iterms, chain) { | |
1497 | printk("%d", term->id); | |
1498 | if (term->chain.next != &dev->video.iterms) | |
1499 | printk(","); | |
1500 | } | |
1501 | printk(" -> %d).\n", dev->video.oterm->id); | |
1502 | } | |
1503 | ||
ff924203 LP |
1504 | /* Initialize the video buffers queue. */ |
1505 | uvc_queue_init(&dev->video.queue, dev->video.streaming->type); | |
1506 | ||
c0efd232 LP |
1507 | /* Initialize the streaming interface with default streaming |
1508 | * parameters. | |
1509 | */ | |
1510 | if ((ret = uvc_video_init(&dev->video)) < 0) { | |
1511 | uvc_printk(KERN_ERR, "Failed to initialize the device " | |
1512 | "(%d).\n", ret); | |
1513 | return ret; | |
1514 | } | |
1515 | ||
1516 | /* Register the device with V4L. */ | |
1517 | vdev = video_device_alloc(); | |
1518 | if (vdev == NULL) | |
1519 | return -1; | |
1520 | ||
1521 | /* We already hold a reference to dev->udev. The video device will be | |
1522 | * unregistered before the reference is released, so we don't need to | |
1523 | * get another one. | |
1524 | */ | |
5e85e732 | 1525 | vdev->parent = &dev->intf->dev; |
c0efd232 LP |
1526 | vdev->minor = -1; |
1527 | vdev->fops = &uvc_fops; | |
1528 | vdev->release = video_device_release; | |
d0ebf307 | 1529 | strlcpy(vdev->name, dev->name, sizeof vdev->name); |
c0efd232 LP |
1530 | |
1531 | /* Set the driver data before calling video_register_device, otherwise | |
1532 | * uvc_v4l2_open might race us. | |
c0efd232 LP |
1533 | */ |
1534 | dev->video.vdev = vdev; | |
1535 | video_set_drvdata(vdev, &dev->video); | |
1536 | ||
1537 | if (video_register_device(vdev, VFL_TYPE_GRABBER, -1) < 0) { | |
1538 | dev->video.vdev = NULL; | |
1539 | video_device_release(vdev); | |
1540 | return -1; | |
1541 | } | |
1542 | ||
1543 | return 0; | |
1544 | } | |
1545 | ||
1546 | /* | |
1547 | * Delete the UVC device. | |
1548 | * | |
1549 | * Called by the kernel when the last reference to the uvc_device structure | |
1550 | * is released. | |
1551 | * | |
1552 | * Unregistering the video devices is done here because every opened instance | |
1553 | * must be closed before the device can be unregistered. An alternative would | |
1554 | * have been to use another reference count for uvc_v4l2_open/uvc_release, and | |
1555 | * unregister the video devices on disconnect when that reference count drops | |
1556 | * to zero. | |
1557 | * | |
1558 | * As this function is called after or during disconnect(), all URBs have | |
1559 | * already been canceled by the USB core. There is no need to kill the | |
1560 | * interrupt URB manually. | |
1561 | */ | |
1562 | void uvc_delete(struct kref *kref) | |
1563 | { | |
1564 | struct uvc_device *dev = container_of(kref, struct uvc_device, kref); | |
1565 | struct list_head *p, *n; | |
1566 | ||
2c2d264b | 1567 | /* Unregister the video device. */ |
c0efd232 LP |
1568 | uvc_unregister_video(dev); |
1569 | usb_put_intf(dev->intf); | |
1570 | usb_put_dev(dev->udev); | |
1571 | ||
1572 | uvc_status_cleanup(dev); | |
1573 | uvc_ctrl_cleanup_device(dev); | |
1574 | ||
1575 | list_for_each_safe(p, n, &dev->entities) { | |
1576 | struct uvc_entity *entity; | |
1577 | entity = list_entry(p, struct uvc_entity, list); | |
1578 | kfree(entity); | |
1579 | } | |
1580 | ||
1581 | list_for_each_safe(p, n, &dev->streaming) { | |
1582 | struct uvc_streaming *streaming; | |
1583 | streaming = list_entry(p, struct uvc_streaming, list); | |
1584 | usb_driver_release_interface(&uvc_driver.driver, | |
1585 | streaming->intf); | |
1586 | usb_put_intf(streaming->intf); | |
1587 | kfree(streaming->format); | |
1588 | kfree(streaming->header.bmaControls); | |
1589 | kfree(streaming); | |
1590 | } | |
1591 | ||
1592 | kfree(dev); | |
1593 | } | |
1594 | ||
1595 | static int uvc_probe(struct usb_interface *intf, | |
1596 | const struct usb_device_id *id) | |
1597 | { | |
1598 | struct usb_device *udev = interface_to_usbdev(intf); | |
1599 | struct uvc_device *dev; | |
1600 | int ret; | |
1601 | ||
1602 | if (id->idVendor && id->idProduct) | |
1603 | uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s " | |
1604 | "(%04x:%04x)\n", udev->devpath, id->idVendor, | |
1605 | id->idProduct); | |
1606 | else | |
1607 | uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n", | |
1608 | udev->devpath); | |
1609 | ||
2c2d264b | 1610 | /* Allocate memory for the device and initialize it. */ |
c0efd232 LP |
1611 | if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL) |
1612 | return -ENOMEM; | |
1613 | ||
1614 | INIT_LIST_HEAD(&dev->entities); | |
1615 | INIT_LIST_HEAD(&dev->streaming); | |
1616 | kref_init(&dev->kref); | |
1617 | ||
1618 | dev->udev = usb_get_dev(udev); | |
1619 | dev->intf = usb_get_intf(intf); | |
1620 | dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber; | |
1621 | dev->quirks = id->driver_info | uvc_quirks_param; | |
1622 | ||
1623 | if (udev->product != NULL) | |
d0ebf307 | 1624 | strlcpy(dev->name, udev->product, sizeof dev->name); |
c0efd232 LP |
1625 | else |
1626 | snprintf(dev->name, sizeof dev->name, | |
1627 | "UVC Camera (%04x:%04x)", | |
1628 | le16_to_cpu(udev->descriptor.idVendor), | |
1629 | le16_to_cpu(udev->descriptor.idProduct)); | |
1630 | ||
2c2d264b | 1631 | /* Parse the Video Class control descriptor. */ |
c0efd232 LP |
1632 | if (uvc_parse_control(dev) < 0) { |
1633 | uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC " | |
1634 | "descriptors.\n"); | |
1635 | goto error; | |
1636 | } | |
1637 | ||
fba4578e | 1638 | uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n", |
c0efd232 LP |
1639 | dev->uvc_version >> 8, dev->uvc_version & 0xff, |
1640 | udev->product ? udev->product : "<unnamed>", | |
1641 | le16_to_cpu(udev->descriptor.idVendor), | |
1642 | le16_to_cpu(udev->descriptor.idProduct)); | |
1643 | ||
1644 | if (uvc_quirks_param != 0) { | |
1645 | uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module " | |
1646 | "parameter for testing purpose.\n", uvc_quirks_param); | |
1647 | uvc_printk(KERN_INFO, "Please report required quirks to the " | |
1648 | "linux-uvc-devel mailing list.\n"); | |
1649 | } | |
1650 | ||
2c2d264b | 1651 | /* Initialize controls. */ |
c0efd232 LP |
1652 | if (uvc_ctrl_init_device(dev) < 0) |
1653 | goto error; | |
1654 | ||
2c2d264b | 1655 | /* Register the video devices. */ |
c0efd232 LP |
1656 | if (uvc_register_video(dev) < 0) |
1657 | goto error; | |
1658 | ||
2c2d264b | 1659 | /* Save our data pointer in the interface data. */ |
c0efd232 LP |
1660 | usb_set_intfdata(intf, dev); |
1661 | ||
2c2d264b | 1662 | /* Initialize the interrupt URB. */ |
c0efd232 LP |
1663 | if ((ret = uvc_status_init(dev)) < 0) { |
1664 | uvc_printk(KERN_INFO, "Unable to initialize the status " | |
1665 | "endpoint (%d), status interrupt will not be " | |
1666 | "supported.\n", ret); | |
1667 | } | |
1668 | ||
1669 | uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n"); | |
1670 | return 0; | |
1671 | ||
1672 | error: | |
1673 | kref_put(&dev->kref, uvc_delete); | |
1674 | return -ENODEV; | |
1675 | } | |
1676 | ||
1677 | static void uvc_disconnect(struct usb_interface *intf) | |
1678 | { | |
1679 | struct uvc_device *dev = usb_get_intfdata(intf); | |
1680 | ||
1681 | /* Set the USB interface data to NULL. This can be done outside the | |
1682 | * lock, as there's no other reader. | |
1683 | */ | |
1684 | usb_set_intfdata(intf, NULL); | |
1685 | ||
1686 | if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOSTREAMING) | |
1687 | return; | |
1688 | ||
1689 | /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide | |
1690 | * lock is needed to prevent uvc_disconnect from releasing its | |
1691 | * reference to the uvc_device instance after uvc_v4l2_open() received | |
1692 | * the pointer to the device (video_devdata) but before it got the | |
1693 | * chance to increase the reference count (kref_get). | |
a9e28585 LP |
1694 | * |
1695 | * Note that the reference can't be released with the lock held, | |
1696 | * otherwise a AB-BA deadlock can occur with videodev_lock that | |
1697 | * videodev acquires in videodev_open() and video_unregister_device(). | |
c0efd232 LP |
1698 | */ |
1699 | mutex_lock(&uvc_driver.open_mutex); | |
c0efd232 | 1700 | dev->state |= UVC_DEV_DISCONNECTED; |
c0efd232 | 1701 | mutex_unlock(&uvc_driver.open_mutex); |
a9e28585 LP |
1702 | |
1703 | kref_put(&dev->kref, uvc_delete); | |
c0efd232 LP |
1704 | } |
1705 | ||
1706 | static int uvc_suspend(struct usb_interface *intf, pm_message_t message) | |
1707 | { | |
1708 | struct uvc_device *dev = usb_get_intfdata(intf); | |
1709 | ||
1710 | uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n", | |
1711 | intf->cur_altsetting->desc.bInterfaceNumber); | |
1712 | ||
1713 | /* Controls are cached on the fly so they don't need to be saved. */ | |
1714 | if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL) | |
1715 | return uvc_status_suspend(dev); | |
1716 | ||
1717 | if (dev->video.streaming->intf != intf) { | |
1718 | uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB " | |
1719 | "interface mismatch.\n"); | |
1720 | return -EINVAL; | |
1721 | } | |
1722 | ||
1723 | return uvc_video_suspend(&dev->video); | |
1724 | } | |
1725 | ||
9b0ae867 | 1726 | static int __uvc_resume(struct usb_interface *intf, int reset) |
c0efd232 LP |
1727 | { |
1728 | struct uvc_device *dev = usb_get_intfdata(intf); | |
1729 | int ret; | |
1730 | ||
1731 | uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n", | |
1732 | intf->cur_altsetting->desc.bInterfaceNumber); | |
1733 | ||
1734 | if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL) { | |
9b0ae867 | 1735 | if (reset && (ret = uvc_ctrl_resume_device(dev)) < 0) |
c0efd232 LP |
1736 | return ret; |
1737 | ||
1738 | return uvc_status_resume(dev); | |
1739 | } | |
1740 | ||
1741 | if (dev->video.streaming->intf != intf) { | |
1742 | uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB " | |
1743 | "interface mismatch.\n"); | |
1744 | return -EINVAL; | |
1745 | } | |
1746 | ||
1747 | return uvc_video_resume(&dev->video); | |
1748 | } | |
1749 | ||
9b0ae867 LP |
1750 | static int uvc_resume(struct usb_interface *intf) |
1751 | { | |
1752 | return __uvc_resume(intf, 0); | |
1753 | } | |
1754 | ||
1755 | static int uvc_reset_resume(struct usb_interface *intf) | |
1756 | { | |
1757 | return __uvc_resume(intf, 1); | |
1758 | } | |
1759 | ||
c0efd232 LP |
1760 | /* ------------------------------------------------------------------------ |
1761 | * Driver initialization and cleanup | |
1762 | */ | |
1763 | ||
1764 | /* | |
1765 | * The Logitech cameras listed below have their interface class set to | |
1766 | * VENDOR_SPEC because they don't announce themselves as UVC devices, even | |
1767 | * though they are compliant. | |
1768 | */ | |
1769 | static struct usb_device_id uvc_ids[] = { | |
c0efd232 LP |
1770 | /* Microsoft Lifecam NX-6000 */ |
1771 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1772 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1773 | .idVendor = 0x045e, | |
1774 | .idProduct = 0x00f8, | |
1775 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1776 | .bInterfaceSubClass = 1, | |
1777 | .bInterfaceProtocol = 0, | |
1778 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
1779 | /* Microsoft Lifecam VX-7000 */ | |
1780 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1781 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1782 | .idVendor = 0x045e, | |
1783 | .idProduct = 0x0723, | |
1784 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1785 | .bInterfaceSubClass = 1, | |
1786 | .bInterfaceProtocol = 0, | |
1787 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
1788 | /* Logitech Quickcam Fusion */ | |
1789 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1790 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1791 | .idVendor = 0x046d, | |
1792 | .idProduct = 0x08c1, | |
1793 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1794 | .bInterfaceSubClass = 1, | |
1795 | .bInterfaceProtocol = 0 }, | |
1796 | /* Logitech Quickcam Orbit MP */ | |
1797 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1798 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1799 | .idVendor = 0x046d, | |
1800 | .idProduct = 0x08c2, | |
1801 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1802 | .bInterfaceSubClass = 1, | |
1803 | .bInterfaceProtocol = 0 }, | |
1804 | /* Logitech Quickcam Pro for Notebook */ | |
1805 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1806 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1807 | .idVendor = 0x046d, | |
1808 | .idProduct = 0x08c3, | |
1809 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1810 | .bInterfaceSubClass = 1, | |
1811 | .bInterfaceProtocol = 0 }, | |
1812 | /* Logitech Quickcam Pro 5000 */ | |
1813 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1814 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1815 | .idVendor = 0x046d, | |
1816 | .idProduct = 0x08c5, | |
1817 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1818 | .bInterfaceSubClass = 1, | |
1819 | .bInterfaceProtocol = 0 }, | |
1820 | /* Logitech Quickcam OEM Dell Notebook */ | |
1821 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1822 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1823 | .idVendor = 0x046d, | |
1824 | .idProduct = 0x08c6, | |
1825 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1826 | .bInterfaceSubClass = 1, | |
1827 | .bInterfaceProtocol = 0 }, | |
1828 | /* Logitech Quickcam OEM Cisco VT Camera II */ | |
1829 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1830 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1831 | .idVendor = 0x046d, | |
1832 | .idProduct = 0x08c7, | |
1833 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
1834 | .bInterfaceSubClass = 1, | |
1835 | .bInterfaceProtocol = 0 }, | |
f61d1d8a LP |
1836 | /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */ |
1837 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1838 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1839 | .idVendor = 0x058f, | |
1840 | .idProduct = 0x3820, | |
1841 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1842 | .bInterfaceSubClass = 1, | |
1843 | .bInterfaceProtocol = 0, | |
1844 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
c0efd232 | 1845 | /* Apple Built-In iSight */ |
2c2d264b | 1846 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
c0efd232 LP |
1847 | | USB_DEVICE_ID_MATCH_INT_INFO, |
1848 | .idVendor = 0x05ac, | |
1849 | .idProduct = 0x8501, | |
2c2d264b LP |
1850 | .bInterfaceClass = USB_CLASS_VIDEO, |
1851 | .bInterfaceSubClass = 1, | |
1852 | .bInterfaceProtocol = 0, | |
c0efd232 LP |
1853 | .driver_info = UVC_QUIRK_PROBE_MINMAX |
1854 | | UVC_QUIRK_BUILTIN_ISIGHT }, | |
1855 | /* Genesys Logic USB 2.0 PC Camera */ | |
2c2d264b | 1856 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
c0efd232 | 1857 | | USB_DEVICE_ID_MATCH_INT_INFO, |
2c2d264b LP |
1858 | .idVendor = 0x05e3, |
1859 | .idProduct = 0x0505, | |
1860 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1861 | .bInterfaceSubClass = 1, | |
1862 | .bInterfaceProtocol = 0, | |
1863 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
50144aee LP |
1864 | /* ViMicro */ |
1865 | { .match_flags = USB_DEVICE_ID_MATCH_VENDOR | |
1866 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1867 | .idVendor = 0x0ac8, | |
1868 | .idProduct = 0x0000, | |
1869 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1870 | .bInterfaceSubClass = 1, | |
1871 | .bInterfaceProtocol = 0, | |
1872 | .driver_info = UVC_QUIRK_FIX_BANDWIDTH }, | |
c0efd232 LP |
1873 | /* MT6227 */ |
1874 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1875 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1876 | .idVendor = 0x0e8d, | |
1877 | .idProduct = 0x0004, | |
1878 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1879 | .bInterfaceSubClass = 1, | |
1880 | .bInterfaceProtocol = 0, | |
1881 | .driver_info = UVC_QUIRK_PROBE_MINMAX }, | |
1882 | /* Syntek (HP Spartan) */ | |
1883 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1884 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1885 | .idVendor = 0x174f, | |
1886 | .idProduct = 0x5212, | |
1887 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1888 | .bInterfaceSubClass = 1, | |
1889 | .bInterfaceProtocol = 0, | |
25e69850 | 1890 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, |
562f0fed LP |
1891 | /* Syntek (Samsung Q310) */ |
1892 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1893 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1894 | .idVendor = 0x174f, | |
1895 | .idProduct = 0x5931, | |
1896 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1897 | .bInterfaceSubClass = 1, | |
1898 | .bInterfaceProtocol = 0, | |
1899 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
f61d1d8a | 1900 | /* Syntek (Asus F9SG) */ |
25e69850 LP |
1901 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1902 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1903 | .idVendor = 0x174f, | |
1904 | .idProduct = 0x8a31, | |
1905 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1906 | .bInterfaceSubClass = 1, | |
1907 | .bInterfaceProtocol = 0, | |
c0efd232 LP |
1908 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, |
1909 | /* Syntek (Asus U3S) */ | |
1910 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1911 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1912 | .idVendor = 0x174f, | |
1913 | .idProduct = 0x8a33, | |
1914 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1915 | .bInterfaceSubClass = 1, | |
1916 | .bInterfaceProtocol = 0, | |
1917 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
2f38483b LP |
1918 | /* Lenovo Thinkpad SL500 */ |
1919 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1920 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1921 | .idVendor = 0x17ef, | |
1922 | .idProduct = 0x480b, | |
1923 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1924 | .bInterfaceSubClass = 1, | |
1925 | .bInterfaceProtocol = 0, | |
1926 | .driver_info = UVC_QUIRK_STREAM_NO_FID }, | |
c0efd232 LP |
1927 | /* Ecamm Pico iMage */ |
1928 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1929 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1930 | .idVendor = 0x18cd, | |
1931 | .idProduct = 0xcafe, | |
1932 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1933 | .bInterfaceSubClass = 1, | |
1934 | .bInterfaceProtocol = 0, | |
1935 | .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS }, | |
1936 | /* Bodelin ProScopeHR */ | |
1937 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1938 | | USB_DEVICE_ID_MATCH_DEV_HI | |
1939 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1940 | .idVendor = 0x19ab, | |
1941 | .idProduct = 0x1000, | |
1942 | .bcdDevice_hi = 0x0126, | |
1943 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1944 | .bInterfaceSubClass = 1, | |
1945 | .bInterfaceProtocol = 0, | |
1946 | .driver_info = UVC_QUIRK_STATUS_INTERVAL }, | |
1947 | /* SiGma Micro USB Web Camera */ | |
1948 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
1949 | | USB_DEVICE_ID_MATCH_INT_INFO, | |
1950 | .idVendor = 0x1c4f, | |
1951 | .idProduct = 0x3000, | |
1952 | .bInterfaceClass = USB_CLASS_VIDEO, | |
1953 | .bInterfaceSubClass = 1, | |
1954 | .bInterfaceProtocol = 0, | |
1955 | .driver_info = UVC_QUIRK_PROBE_MINMAX | |
2bdd29cf LP |
1956 | | UVC_QUIRK_IGNORE_SELECTOR_UNIT |
1957 | | UVC_QUIRK_PRUNE_CONTROLS }, | |
c0efd232 LP |
1958 | /* Generic USB Video Class */ |
1959 | { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) }, | |
1960 | {} | |
1961 | }; | |
1962 | ||
1963 | MODULE_DEVICE_TABLE(usb, uvc_ids); | |
1964 | ||
1965 | struct uvc_driver uvc_driver = { | |
1966 | .driver = { | |
1967 | .name = "uvcvideo", | |
1968 | .probe = uvc_probe, | |
1969 | .disconnect = uvc_disconnect, | |
1970 | .suspend = uvc_suspend, | |
1971 | .resume = uvc_resume, | |
9b0ae867 | 1972 | .reset_resume = uvc_reset_resume, |
c0efd232 LP |
1973 | .id_table = uvc_ids, |
1974 | .supports_autosuspend = 1, | |
1975 | }, | |
1976 | }; | |
1977 | ||
1978 | static int __init uvc_init(void) | |
1979 | { | |
1980 | int result; | |
1981 | ||
1982 | INIT_LIST_HEAD(&uvc_driver.devices); | |
1983 | INIT_LIST_HEAD(&uvc_driver.controls); | |
1984 | mutex_init(&uvc_driver.open_mutex); | |
1985 | mutex_init(&uvc_driver.ctrl_mutex); | |
1986 | ||
1987 | uvc_ctrl_init(); | |
1988 | ||
1989 | result = usb_register(&uvc_driver.driver); | |
1990 | if (result == 0) | |
1991 | printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n"); | |
1992 | return result; | |
1993 | } | |
1994 | ||
1995 | static void __exit uvc_cleanup(void) | |
1996 | { | |
1997 | usb_deregister(&uvc_driver.driver); | |
1998 | } | |
1999 | ||
2000 | module_init(uvc_init); | |
2001 | module_exit(uvc_cleanup); | |
2002 | ||
0fbd8ee6 LP |
2003 | module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR); |
2004 | MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames"); | |
c0efd232 LP |
2005 | module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR); |
2006 | MODULE_PARM_DESC(quirks, "Forced device quirks"); | |
2007 | module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR); | |
2008 | MODULE_PARM_DESC(trace, "Trace level bitmask"); | |
2009 | ||
2010 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
2011 | MODULE_DESCRIPTION(DRIVER_DESC); | |
2012 | MODULE_LICENSE("GPL"); | |
2013 | MODULE_VERSION(DRIVER_VERSION); | |
f87086e3 | 2014 |