aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/uvc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/uvc')
-rw-r--r--drivers/media/video/uvc/Kconfig17
-rw-r--r--drivers/media/video/uvc/Makefile6
-rw-r--r--drivers/media/video/uvc/uvc_ctrl.c1919
-rw-r--r--drivers/media/video/uvc/uvc_driver.c2406
-rw-r--r--drivers/media/video/uvc/uvc_entity.c126
-rw-r--r--drivers/media/video/uvc/uvc_isight.c137
-rw-r--r--drivers/media/video/uvc/uvc_queue.c634
-rw-r--r--drivers/media/video/uvc/uvc_status.c237
-rw-r--r--drivers/media/video/uvc/uvc_v4l2.c1143
-rw-r--r--drivers/media/video/uvc/uvc_video.c1273
-rw-r--r--drivers/media/video/uvc/uvcvideo.h703
11 files changed, 8601 insertions, 0 deletions
diff --git a/drivers/media/video/uvc/Kconfig b/drivers/media/video/uvc/Kconfig
new file mode 100644
index 00000000000..2956a763721
--- /dev/null
+++ b/drivers/media/video/uvc/Kconfig
@@ -0,0 +1,17 @@
1config USB_VIDEO_CLASS
2 tristate "USB Video Class (UVC)"
3 ---help---
4 Support for the USB Video Class (UVC). Currently only video
5 input devices, such as webcams, are supported.
6
7 For more information see: <http://linux-uvc.berlios.de/>
8
9config USB_VIDEO_CLASS_INPUT_EVDEV
10 bool "UVC input events device support"
11 default y
12 depends on USB_VIDEO_CLASS=INPUT || INPUT=y
13 ---help---
14 This option makes USB Video Class devices register an input device
15 to report button events.
16
17 If you are in doubt, say Y.
diff --git a/drivers/media/video/uvc/Makefile b/drivers/media/video/uvc/Makefile
new file mode 100644
index 00000000000..2071ca8a2f0
--- /dev/null
+++ b/drivers/media/video/uvc/Makefile
@@ -0,0 +1,6 @@
1uvcvideo-objs := uvc_driver.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_ctrl.o \
2 uvc_status.o uvc_isight.o
3ifeq ($(CONFIG_MEDIA_CONTROLLER),y)
4uvcvideo-objs += uvc_entity.o
5endif
6obj-$(CONFIG_USB_VIDEO_CLASS) += uvcvideo.o
diff --git a/drivers/media/video/uvc/uvc_ctrl.c b/drivers/media/video/uvc/uvc_ctrl.c
new file mode 100644
index 00000000000..10c2364f3e8
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_ctrl.c
@@ -0,0 +1,1919 @@
1/*
2 * uvc_ctrl.c -- USB Video Class driver - Controls
3 *
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19#include <linux/usb.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/wait.h>
23#include <linux/atomic.h>
24
25#include "uvcvideo.h"
26
27#define UVC_CTRL_DATA_CURRENT 0
28#define UVC_CTRL_DATA_BACKUP 1
29#define UVC_CTRL_DATA_MIN 2
30#define UVC_CTRL_DATA_MAX 3
31#define UVC_CTRL_DATA_RES 4
32#define UVC_CTRL_DATA_DEF 5
33#define UVC_CTRL_DATA_LAST 6
34
35/* ------------------------------------------------------------------------
36 * Controls
37 */
38
39static struct uvc_control_info uvc_ctrls[] = {
40 {
41 .entity = UVC_GUID_UVC_PROCESSING,
42 .selector = UVC_PU_BRIGHTNESS_CONTROL,
43 .index = 0,
44 .size = 2,
45 .flags = UVC_CTRL_FLAG_SET_CUR
46 | UVC_CTRL_FLAG_GET_RANGE
47 | UVC_CTRL_FLAG_RESTORE,
48 },
49 {
50 .entity = UVC_GUID_UVC_PROCESSING,
51 .selector = UVC_PU_CONTRAST_CONTROL,
52 .index = 1,
53 .size = 2,
54 .flags = UVC_CTRL_FLAG_SET_CUR
55 | UVC_CTRL_FLAG_GET_RANGE
56 | UVC_CTRL_FLAG_RESTORE,
57 },
58 {
59 .entity = UVC_GUID_UVC_PROCESSING,
60 .selector = UVC_PU_HUE_CONTROL,
61 .index = 2,
62 .size = 2,
63 .flags = UVC_CTRL_FLAG_SET_CUR
64 | UVC_CTRL_FLAG_GET_RANGE
65 | UVC_CTRL_FLAG_RESTORE
66 | UVC_CTRL_FLAG_AUTO_UPDATE,
67 },
68 {
69 .entity = UVC_GUID_UVC_PROCESSING,
70 .selector = UVC_PU_SATURATION_CONTROL,
71 .index = 3,
72 .size = 2,
73 .flags = UVC_CTRL_FLAG_SET_CUR
74 | UVC_CTRL_FLAG_GET_RANGE
75 | UVC_CTRL_FLAG_RESTORE,
76 },
77 {
78 .entity = UVC_GUID_UVC_PROCESSING,
79 .selector = UVC_PU_SHARPNESS_CONTROL,
80 .index = 4,
81 .size = 2,
82 .flags = UVC_CTRL_FLAG_SET_CUR
83 | UVC_CTRL_FLAG_GET_RANGE
84 | UVC_CTRL_FLAG_RESTORE,
85 },
86 {
87 .entity = UVC_GUID_UVC_PROCESSING,
88 .selector = UVC_PU_GAMMA_CONTROL,
89 .index = 5,
90 .size = 2,
91 .flags = UVC_CTRL_FLAG_SET_CUR
92 | UVC_CTRL_FLAG_GET_RANGE
93 | UVC_CTRL_FLAG_RESTORE,
94 },
95 {
96 .entity = UVC_GUID_UVC_PROCESSING,
97 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
98 .index = 6,
99 .size = 2,
100 .flags = UVC_CTRL_FLAG_SET_CUR
101 | UVC_CTRL_FLAG_GET_RANGE
102 | UVC_CTRL_FLAG_RESTORE
103 | UVC_CTRL_FLAG_AUTO_UPDATE,
104 },
105 {
106 .entity = UVC_GUID_UVC_PROCESSING,
107 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
108 .index = 7,
109 .size = 4,
110 .flags = UVC_CTRL_FLAG_SET_CUR
111 | UVC_CTRL_FLAG_GET_RANGE
112 | UVC_CTRL_FLAG_RESTORE
113 | UVC_CTRL_FLAG_AUTO_UPDATE,
114 },
115 {
116 .entity = UVC_GUID_UVC_PROCESSING,
117 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
118 .index = 8,
119 .size = 2,
120 .flags = UVC_CTRL_FLAG_SET_CUR
121 | UVC_CTRL_FLAG_GET_RANGE
122 | UVC_CTRL_FLAG_RESTORE,
123 },
124 {
125 .entity = UVC_GUID_UVC_PROCESSING,
126 .selector = UVC_PU_GAIN_CONTROL,
127 .index = 9,
128 .size = 2,
129 .flags = UVC_CTRL_FLAG_SET_CUR
130 | UVC_CTRL_FLAG_GET_RANGE
131 | UVC_CTRL_FLAG_RESTORE,
132 },
133 {
134 .entity = UVC_GUID_UVC_PROCESSING,
135 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
136 .index = 10,
137 .size = 1,
138 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
139 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
140 },
141 {
142 .entity = UVC_GUID_UVC_PROCESSING,
143 .selector = UVC_PU_HUE_AUTO_CONTROL,
144 .index = 11,
145 .size = 1,
146 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
147 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
148 },
149 {
150 .entity = UVC_GUID_UVC_PROCESSING,
151 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
152 .index = 12,
153 .size = 1,
154 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
155 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
156 },
157 {
158 .entity = UVC_GUID_UVC_PROCESSING,
159 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
160 .index = 13,
161 .size = 1,
162 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
163 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
164 },
165 {
166 .entity = UVC_GUID_UVC_PROCESSING,
167 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
168 .index = 14,
169 .size = 2,
170 .flags = UVC_CTRL_FLAG_SET_CUR
171 | UVC_CTRL_FLAG_GET_RANGE
172 | UVC_CTRL_FLAG_RESTORE,
173 },
174 {
175 .entity = UVC_GUID_UVC_PROCESSING,
176 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
177 .index = 15,
178 .size = 2,
179 .flags = UVC_CTRL_FLAG_SET_CUR
180 | UVC_CTRL_FLAG_GET_RANGE
181 | UVC_CTRL_FLAG_RESTORE,
182 },
183 {
184 .entity = UVC_GUID_UVC_PROCESSING,
185 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
186 .index = 16,
187 .size = 1,
188 .flags = UVC_CTRL_FLAG_GET_CUR,
189 },
190 {
191 .entity = UVC_GUID_UVC_PROCESSING,
192 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
193 .index = 17,
194 .size = 1,
195 .flags = UVC_CTRL_FLAG_GET_CUR,
196 },
197 {
198 .entity = UVC_GUID_UVC_CAMERA,
199 .selector = UVC_CT_SCANNING_MODE_CONTROL,
200 .index = 0,
201 .size = 1,
202 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
203 | UVC_CTRL_FLAG_RESTORE,
204 },
205 {
206 .entity = UVC_GUID_UVC_CAMERA,
207 .selector = UVC_CT_AE_MODE_CONTROL,
208 .index = 1,
209 .size = 1,
210 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
211 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
212 | UVC_CTRL_FLAG_RESTORE,
213 },
214 {
215 .entity = UVC_GUID_UVC_CAMERA,
216 .selector = UVC_CT_AE_PRIORITY_CONTROL,
217 .index = 2,
218 .size = 1,
219 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
220 | UVC_CTRL_FLAG_RESTORE,
221 },
222 {
223 .entity = UVC_GUID_UVC_CAMERA,
224 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
225 .index = 3,
226 .size = 4,
227 .flags = UVC_CTRL_FLAG_SET_CUR
228 | UVC_CTRL_FLAG_GET_RANGE
229 | UVC_CTRL_FLAG_RESTORE,
230 },
231 {
232 .entity = UVC_GUID_UVC_CAMERA,
233 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
234 .index = 4,
235 .size = 1,
236 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
237 },
238 {
239 .entity = UVC_GUID_UVC_CAMERA,
240 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
241 .index = 5,
242 .size = 2,
243 .flags = UVC_CTRL_FLAG_SET_CUR
244 | UVC_CTRL_FLAG_GET_RANGE
245 | UVC_CTRL_FLAG_RESTORE
246 | UVC_CTRL_FLAG_AUTO_UPDATE,
247 },
248 {
249 .entity = UVC_GUID_UVC_CAMERA,
250 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
251 .index = 6,
252 .size = 2,
253 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
254 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
255 | UVC_CTRL_FLAG_GET_DEF
256 | UVC_CTRL_FLAG_AUTO_UPDATE,
257 },
258 {
259 .entity = UVC_GUID_UVC_CAMERA,
260 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
261 .index = 7,
262 .size = 2,
263 .flags = UVC_CTRL_FLAG_SET_CUR
264 | UVC_CTRL_FLAG_GET_RANGE
265 | UVC_CTRL_FLAG_RESTORE
266 | UVC_CTRL_FLAG_AUTO_UPDATE,
267 },
268 {
269 .entity = UVC_GUID_UVC_CAMERA,
270 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
271 .index = 8,
272 .size = 1,
273 .flags = UVC_CTRL_FLAG_SET_CUR
274 | UVC_CTRL_FLAG_AUTO_UPDATE,
275 },
276 {
277 .entity = UVC_GUID_UVC_CAMERA,
278 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
279 .index = 9,
280 .size = 2,
281 .flags = UVC_CTRL_FLAG_SET_CUR
282 | UVC_CTRL_FLAG_GET_RANGE
283 | UVC_CTRL_FLAG_RESTORE
284 | UVC_CTRL_FLAG_AUTO_UPDATE,
285 },
286 {
287 .entity = UVC_GUID_UVC_CAMERA,
288 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
289 .index = 10,
290 .size = 3,
291 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
292 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
293 | UVC_CTRL_FLAG_GET_DEF
294 | UVC_CTRL_FLAG_AUTO_UPDATE,
295 },
296 {
297 .entity = UVC_GUID_UVC_CAMERA,
298 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
299 .index = 11,
300 .size = 8,
301 .flags = UVC_CTRL_FLAG_SET_CUR
302 | UVC_CTRL_FLAG_GET_RANGE
303 | UVC_CTRL_FLAG_RESTORE
304 | UVC_CTRL_FLAG_AUTO_UPDATE,
305 },
306 {
307 .entity = UVC_GUID_UVC_CAMERA,
308 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
309 .index = 12,
310 .size = 4,
311 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
312 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
313 | UVC_CTRL_FLAG_GET_DEF
314 | UVC_CTRL_FLAG_AUTO_UPDATE,
315 },
316 {
317 .entity = UVC_GUID_UVC_CAMERA,
318 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
319 .index = 13,
320 .size = 2,
321 .flags = UVC_CTRL_FLAG_SET_CUR
322 | UVC_CTRL_FLAG_GET_RANGE
323 | UVC_CTRL_FLAG_RESTORE
324 | UVC_CTRL_FLAG_AUTO_UPDATE,
325 },
326 {
327 .entity = UVC_GUID_UVC_CAMERA,
328 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
329 .index = 14,
330 .size = 2,
331 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
332 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
333 | UVC_CTRL_FLAG_GET_DEF
334 | UVC_CTRL_FLAG_AUTO_UPDATE,
335 },
336 {
337 .entity = UVC_GUID_UVC_CAMERA,
338 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
339 .index = 17,
340 .size = 1,
341 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
342 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
343 },
344 {
345 .entity = UVC_GUID_UVC_CAMERA,
346 .selector = UVC_CT_PRIVACY_CONTROL,
347 .index = 18,
348 .size = 1,
349 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
350 | UVC_CTRL_FLAG_RESTORE
351 | UVC_CTRL_FLAG_AUTO_UPDATE,
352 },
353};
354
355static struct uvc_menu_info power_line_frequency_controls[] = {
356 { 0, "Disabled" },
357 { 1, "50 Hz" },
358 { 2, "60 Hz" },
359};
360
361static struct uvc_menu_info exposure_auto_controls[] = {
362 { 2, "Auto Mode" },
363 { 1, "Manual Mode" },
364 { 4, "Shutter Priority Mode" },
365 { 8, "Aperture Priority Mode" },
366};
367
368static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
369 __u8 query, const __u8 *data)
370{
371 __s8 zoom = (__s8)data[0];
372
373 switch (query) {
374 case UVC_GET_CUR:
375 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
376
377 case UVC_GET_MIN:
378 case UVC_GET_MAX:
379 case UVC_GET_RES:
380 case UVC_GET_DEF:
381 default:
382 return data[2];
383 }
384}
385
386static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
387 __s32 value, __u8 *data)
388{
389 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
390 data[2] = min((int)abs(value), 0xff);
391}
392
393static struct uvc_control_mapping uvc_ctrl_mappings[] = {
394 {
395 .id = V4L2_CID_BRIGHTNESS,
396 .name = "Brightness",
397 .entity = UVC_GUID_UVC_PROCESSING,
398 .selector = UVC_PU_BRIGHTNESS_CONTROL,
399 .size = 16,
400 .offset = 0,
401 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
402 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
403 },
404 {
405 .id = V4L2_CID_CONTRAST,
406 .name = "Contrast",
407 .entity = UVC_GUID_UVC_PROCESSING,
408 .selector = UVC_PU_CONTRAST_CONTROL,
409 .size = 16,
410 .offset = 0,
411 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
412 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
413 },
414 {
415 .id = V4L2_CID_HUE,
416 .name = "Hue",
417 .entity = UVC_GUID_UVC_PROCESSING,
418 .selector = UVC_PU_HUE_CONTROL,
419 .size = 16,
420 .offset = 0,
421 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
422 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
423 },
424 {
425 .id = V4L2_CID_SATURATION,
426 .name = "Saturation",
427 .entity = UVC_GUID_UVC_PROCESSING,
428 .selector = UVC_PU_SATURATION_CONTROL,
429 .size = 16,
430 .offset = 0,
431 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
432 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
433 },
434 {
435 .id = V4L2_CID_SHARPNESS,
436 .name = "Sharpness",
437 .entity = UVC_GUID_UVC_PROCESSING,
438 .selector = UVC_PU_SHARPNESS_CONTROL,
439 .size = 16,
440 .offset = 0,
441 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
442 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
443 },
444 {
445 .id = V4L2_CID_GAMMA,
446 .name = "Gamma",
447 .entity = UVC_GUID_UVC_PROCESSING,
448 .selector = UVC_PU_GAMMA_CONTROL,
449 .size = 16,
450 .offset = 0,
451 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
452 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
453 },
454 {
455 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
456 .name = "Backlight Compensation",
457 .entity = UVC_GUID_UVC_PROCESSING,
458 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
459 .size = 16,
460 .offset = 0,
461 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
462 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
463 },
464 {
465 .id = V4L2_CID_GAIN,
466 .name = "Gain",
467 .entity = UVC_GUID_UVC_PROCESSING,
468 .selector = UVC_PU_GAIN_CONTROL,
469 .size = 16,
470 .offset = 0,
471 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
472 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
473 },
474 {
475 .id = V4L2_CID_POWER_LINE_FREQUENCY,
476 .name = "Power Line Frequency",
477 .entity = UVC_GUID_UVC_PROCESSING,
478 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
479 .size = 2,
480 .offset = 0,
481 .v4l2_type = V4L2_CTRL_TYPE_MENU,
482 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
483 .menu_info = power_line_frequency_controls,
484 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
485 },
486 {
487 .id = V4L2_CID_HUE_AUTO,
488 .name = "Hue, Auto",
489 .entity = UVC_GUID_UVC_PROCESSING,
490 .selector = UVC_PU_HUE_AUTO_CONTROL,
491 .size = 1,
492 .offset = 0,
493 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
494 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
495 },
496 {
497 .id = V4L2_CID_EXPOSURE_AUTO,
498 .name = "Exposure, Auto",
499 .entity = UVC_GUID_UVC_CAMERA,
500 .selector = UVC_CT_AE_MODE_CONTROL,
501 .size = 4,
502 .offset = 0,
503 .v4l2_type = V4L2_CTRL_TYPE_MENU,
504 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
505 .menu_info = exposure_auto_controls,
506 .menu_count = ARRAY_SIZE(exposure_auto_controls),
507 },
508 {
509 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
510 .name = "Exposure, Auto Priority",
511 .entity = UVC_GUID_UVC_CAMERA,
512 .selector = UVC_CT_AE_PRIORITY_CONTROL,
513 .size = 1,
514 .offset = 0,
515 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
516 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
517 },
518 {
519 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
520 .name = "Exposure (Absolute)",
521 .entity = UVC_GUID_UVC_CAMERA,
522 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
523 .size = 32,
524 .offset = 0,
525 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
526 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
527 },
528 {
529 .id = V4L2_CID_AUTO_WHITE_BALANCE,
530 .name = "White Balance Temperature, Auto",
531 .entity = UVC_GUID_UVC_PROCESSING,
532 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
533 .size = 1,
534 .offset = 0,
535 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
536 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
537 },
538 {
539 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
540 .name = "White Balance Temperature",
541 .entity = UVC_GUID_UVC_PROCESSING,
542 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
543 .size = 16,
544 .offset = 0,
545 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
546 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
547 },
548 {
549 .id = V4L2_CID_AUTO_WHITE_BALANCE,
550 .name = "White Balance Component, Auto",
551 .entity = UVC_GUID_UVC_PROCESSING,
552 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
553 .size = 1,
554 .offset = 0,
555 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
556 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
557 },
558 {
559 .id = V4L2_CID_BLUE_BALANCE,
560 .name = "White Balance Blue Component",
561 .entity = UVC_GUID_UVC_PROCESSING,
562 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
563 .size = 16,
564 .offset = 0,
565 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
566 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
567 },
568 {
569 .id = V4L2_CID_RED_BALANCE,
570 .name = "White Balance Red Component",
571 .entity = UVC_GUID_UVC_PROCESSING,
572 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
573 .size = 16,
574 .offset = 16,
575 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
576 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
577 },
578 {
579 .id = V4L2_CID_FOCUS_ABSOLUTE,
580 .name = "Focus (absolute)",
581 .entity = UVC_GUID_UVC_CAMERA,
582 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
583 .size = 16,
584 .offset = 0,
585 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
586 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
587 },
588 {
589 .id = V4L2_CID_FOCUS_AUTO,
590 .name = "Focus, Auto",
591 .entity = UVC_GUID_UVC_CAMERA,
592 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
593 .size = 1,
594 .offset = 0,
595 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
596 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
597 },
598 {
599 .id = V4L2_CID_IRIS_ABSOLUTE,
600 .name = "Iris, Absolute",
601 .entity = UVC_GUID_UVC_CAMERA,
602 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
603 .size = 16,
604 .offset = 0,
605 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
606 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
607 },
608 {
609 .id = V4L2_CID_IRIS_RELATIVE,
610 .name = "Iris, Relative",
611 .entity = UVC_GUID_UVC_CAMERA,
612 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
613 .size = 8,
614 .offset = 0,
615 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
616 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
617 },
618 {
619 .id = V4L2_CID_ZOOM_ABSOLUTE,
620 .name = "Zoom, Absolute",
621 .entity = UVC_GUID_UVC_CAMERA,
622 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
623 .size = 16,
624 .offset = 0,
625 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
626 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
627 },
628 {
629 .id = V4L2_CID_ZOOM_CONTINUOUS,
630 .name = "Zoom, Continuous",
631 .entity = UVC_GUID_UVC_CAMERA,
632 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
633 .size = 0,
634 .offset = 0,
635 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
636 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
637 .get = uvc_ctrl_get_zoom,
638 .set = uvc_ctrl_set_zoom,
639 },
640 {
641 .id = V4L2_CID_PAN_ABSOLUTE,
642 .name = "Pan (Absolute)",
643 .entity = UVC_GUID_UVC_CAMERA,
644 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
645 .size = 32,
646 .offset = 0,
647 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
648 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
649 },
650 {
651 .id = V4L2_CID_TILT_ABSOLUTE,
652 .name = "Tilt (Absolute)",
653 .entity = UVC_GUID_UVC_CAMERA,
654 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
655 .size = 32,
656 .offset = 32,
657 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
658 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
659 },
660 {
661 .id = V4L2_CID_PRIVACY,
662 .name = "Privacy",
663 .entity = UVC_GUID_UVC_CAMERA,
664 .selector = UVC_CT_PRIVACY_CONTROL,
665 .size = 1,
666 .offset = 0,
667 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
668 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
669 },
670};
671
672/* ------------------------------------------------------------------------
673 * Utility functions
674 */
675
676static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
677{
678 return ctrl->uvc_data + id * ctrl->info.size;
679}
680
681static inline int uvc_test_bit(const __u8 *data, int bit)
682{
683 return (data[bit >> 3] >> (bit & 7)) & 1;
684}
685
686static inline void uvc_clear_bit(__u8 *data, int bit)
687{
688 data[bit >> 3] &= ~(1 << (bit & 7));
689}
690
691/* Extract the bit string specified by mapping->offset and mapping->size
692 * from the little-endian data stored at 'data' and return the result as
693 * a signed 32bit integer. Sign extension will be performed if the mapping
694 * references a signed data type.
695 */
696static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
697 __u8 query, const __u8 *data)
698{
699 int bits = mapping->size;
700 int offset = mapping->offset;
701 __s32 value = 0;
702 __u8 mask;
703
704 data += offset / 8;
705 offset &= 7;
706 mask = ((1LL << bits) - 1) << offset;
707
708 for (; bits > 0; data++) {
709 __u8 byte = *data & mask;
710 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
711 bits -= 8 - (offset > 0 ? offset : 0);
712 offset -= 8;
713 mask = (1 << bits) - 1;
714 }
715
716 /* Sign-extend the value if needed. */
717 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
718 value |= -(value & (1 << (mapping->size - 1)));
719
720 return value;
721}
722
723/* Set the bit string specified by mapping->offset and mapping->size
724 * in the little-endian data stored at 'data' to the value 'value'.
725 */
726static void uvc_set_le_value(struct uvc_control_mapping *mapping,
727 __s32 value, __u8 *data)
728{
729 int bits = mapping->size;
730 int offset = mapping->offset;
731 __u8 mask;
732
733 /* According to the v4l2 spec, writing any value to a button control
734 * should result in the action belonging to the button control being
735 * triggered. UVC devices however want to see a 1 written -> override
736 * value.
737 */
738 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
739 value = -1;
740
741 data += offset / 8;
742 offset &= 7;
743
744 for (; bits > 0; data++) {
745 mask = ((1LL << bits) - 1) << offset;
746 *data = (*data & ~mask) | ((value << offset) & mask);
747 value >>= offset ? offset : 8;
748 bits -= 8 - offset;
749 offset = 0;
750 }
751}
752
753/* ------------------------------------------------------------------------
754 * Terminal and unit management
755 */
756
757static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
758static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
759static const __u8 uvc_media_transport_input_guid[16] =
760 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
761
762static int uvc_entity_match_guid(const struct uvc_entity *entity,
763 const __u8 guid[16])
764{
765 switch (UVC_ENTITY_TYPE(entity)) {
766 case UVC_ITT_CAMERA:
767 return memcmp(uvc_camera_guid, guid, 16) == 0;
768
769 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
770 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
771
772 case UVC_VC_PROCESSING_UNIT:
773 return memcmp(uvc_processing_guid, guid, 16) == 0;
774
775 case UVC_VC_EXTENSION_UNIT:
776 return memcmp(entity->extension.guidExtensionCode,
777 guid, 16) == 0;
778
779 default:
780 return 0;
781 }
782}
783
784/* ------------------------------------------------------------------------
785 * UVC Controls
786 */
787
788static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
789 struct uvc_control_mapping **mapping, struct uvc_control **control,
790 int next)
791{
792 struct uvc_control *ctrl;
793 struct uvc_control_mapping *map;
794 unsigned int i;
795
796 if (entity == NULL)
797 return;
798
799 for (i = 0; i < entity->ncontrols; ++i) {
800 ctrl = &entity->controls[i];
801 if (!ctrl->initialized)
802 continue;
803
804 list_for_each_entry(map, &ctrl->info.mappings, list) {
805 if ((map->id == v4l2_id) && !next) {
806 *control = ctrl;
807 *mapping = map;
808 return;
809 }
810
811 if ((*mapping == NULL || (*mapping)->id > map->id) &&
812 (map->id > v4l2_id) && next) {
813 *control = ctrl;
814 *mapping = map;
815 }
816 }
817 }
818}
819
820static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
821 __u32 v4l2_id, struct uvc_control_mapping **mapping)
822{
823 struct uvc_control *ctrl = NULL;
824 struct uvc_entity *entity;
825 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
826
827 *mapping = NULL;
828
829 /* Mask the query flags. */
830 v4l2_id &= V4L2_CTRL_ID_MASK;
831
832 /* Find the control. */
833 list_for_each_entry(entity, &chain->entities, chain) {
834 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
835 if (ctrl && !next)
836 return ctrl;
837 }
838
839 if (ctrl == NULL && !next)
840 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
841 v4l2_id);
842
843 return ctrl;
844}
845
846static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
847 struct uvc_control *ctrl)
848{
849 int ret;
850
851 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
852 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
853 chain->dev->intfnum, ctrl->info.selector,
854 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
855 ctrl->info.size);
856 if (ret < 0)
857 return ret;
858 }
859
860 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
861 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
862 chain->dev->intfnum, ctrl->info.selector,
863 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
864 ctrl->info.size);
865 if (ret < 0)
866 return ret;
867 }
868 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
869 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
870 chain->dev->intfnum, ctrl->info.selector,
871 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
872 ctrl->info.size);
873 if (ret < 0)
874 return ret;
875 }
876 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
877 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
878 chain->dev->intfnum, ctrl->info.selector,
879 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
880 ctrl->info.size);
881 if (ret < 0)
882 return ret;
883 }
884
885 ctrl->cached = 1;
886 return 0;
887}
888
889int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
890 struct v4l2_queryctrl *v4l2_ctrl)
891{
892 struct uvc_control *ctrl;
893 struct uvc_control_mapping *mapping;
894 struct uvc_menu_info *menu;
895 unsigned int i;
896 int ret;
897
898 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
899 if (ret < 0)
900 return -ERESTARTSYS;
901
902 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
903 if (ctrl == NULL) {
904 ret = -EINVAL;
905 goto done;
906 }
907
908 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
909 v4l2_ctrl->id = mapping->id;
910 v4l2_ctrl->type = mapping->v4l2_type;
911 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
912 v4l2_ctrl->flags = 0;
913
914 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
915 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
916 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
917 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
918
919 if (!ctrl->cached) {
920 ret = uvc_ctrl_populate_cache(chain, ctrl);
921 if (ret < 0)
922 goto done;
923 }
924
925 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
926 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
927 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
928 }
929
930 switch (mapping->v4l2_type) {
931 case V4L2_CTRL_TYPE_MENU:
932 v4l2_ctrl->minimum = 0;
933 v4l2_ctrl->maximum = mapping->menu_count - 1;
934 v4l2_ctrl->step = 1;
935
936 menu = mapping->menu_info;
937 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
938 if (menu->value == v4l2_ctrl->default_value) {
939 v4l2_ctrl->default_value = i;
940 break;
941 }
942 }
943
944 goto done;
945
946 case V4L2_CTRL_TYPE_BOOLEAN:
947 v4l2_ctrl->minimum = 0;
948 v4l2_ctrl->maximum = 1;
949 v4l2_ctrl->step = 1;
950 goto done;
951
952 case V4L2_CTRL_TYPE_BUTTON:
953 v4l2_ctrl->minimum = 0;
954 v4l2_ctrl->maximum = 0;
955 v4l2_ctrl->step = 0;
956 goto done;
957
958 default:
959 break;
960 }
961
962 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
963 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
964 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
965
966 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
967 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
968 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
969
970 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
971 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
972 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
973
974done:
975 mutex_unlock(&chain->ctrl_mutex);
976 return ret;
977}
978
979/*
980 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
981 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
982 * must be grouped (for instance the Red Balance, Blue Balance and Do White
983 * Balance V4L2 controls use the White Balance Component UVC control) or
984 * otherwise translated. The approach we take here is to use a translation
985 * table for the controls that can be mapped directly, and handle the others
986 * manually.
987 */
988int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
989 struct v4l2_querymenu *query_menu)
990{
991 struct uvc_menu_info *menu_info;
992 struct uvc_control_mapping *mapping;
993 struct uvc_control *ctrl;
994 u32 index = query_menu->index;
995 u32 id = query_menu->id;
996 int ret;
997
998 memset(query_menu, 0, sizeof(*query_menu));
999 query_menu->id = id;
1000 query_menu->index = index;
1001
1002 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1003 if (ret < 0)
1004 return -ERESTARTSYS;
1005
1006 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1007 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1008 ret = -EINVAL;
1009 goto done;
1010 }
1011
1012 if (query_menu->index >= mapping->menu_count) {
1013 ret = -EINVAL;
1014 goto done;
1015 }
1016
1017 menu_info = &mapping->menu_info[query_menu->index];
1018
1019 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
1020 s32 bitmap;
1021
1022 if (!ctrl->cached) {
1023 ret = uvc_ctrl_populate_cache(chain, ctrl);
1024 if (ret < 0)
1025 goto done;
1026 }
1027
1028 bitmap = mapping->get(mapping, UVC_GET_RES,
1029 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1030 if (!(bitmap & menu_info->value)) {
1031 ret = -EINVAL;
1032 goto done;
1033 }
1034 }
1035
1036 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
1037
1038done:
1039 mutex_unlock(&chain->ctrl_mutex);
1040 return ret;
1041}
1042
1043
1044/* --------------------------------------------------------------------------
1045 * Control transactions
1046 *
1047 * To make extended set operations as atomic as the hardware allows, controls
1048 * are handled using begin/commit/rollback operations.
1049 *
1050 * At the beginning of a set request, uvc_ctrl_begin should be called to
1051 * initialize the request. This function acquires the control lock.
1052 *
1053 * When setting a control, the new value is stored in the control data field
1054 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1055 * later processing. If the UVC and V4L2 control sizes differ, the current
1056 * value is loaded from the hardware before storing the new value in the data
1057 * field.
1058 *
1059 * After processing all controls in the transaction, uvc_ctrl_commit or
1060 * uvc_ctrl_rollback must be called to apply the pending changes to the
1061 * hardware or revert them. When applying changes, all controls marked as
1062 * dirty will be modified in the UVC device, and the dirty flag will be
1063 * cleared. When reverting controls, the control data field
1064 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1065 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1066 * control lock.
1067 */
1068int uvc_ctrl_begin(struct uvc_video_chain *chain)
1069{
1070 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1071}
1072
1073static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1074 struct uvc_entity *entity, int rollback)
1075{
1076 struct uvc_control *ctrl;
1077 unsigned int i;
1078 int ret;
1079
1080 if (entity == NULL)
1081 return 0;
1082
1083 for (i = 0; i < entity->ncontrols; ++i) {
1084 ctrl = &entity->controls[i];
1085 if (!ctrl->initialized)
1086 continue;
1087
1088 /* Reset the loaded flag for auto-update controls that were
1089 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1090 * uvc_ctrl_get from using the cached value.
1091 */
1092 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE)
1093 ctrl->loaded = 0;
1094
1095 if (!ctrl->dirty)
1096 continue;
1097
1098 if (!rollback)
1099 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1100 dev->intfnum, ctrl->info.selector,
1101 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1102 ctrl->info.size);
1103 else
1104 ret = 0;
1105
1106 if (rollback || ret < 0)
1107 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1108 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1109 ctrl->info.size);
1110
1111 ctrl->dirty = 0;
1112
1113 if (ret < 0)
1114 return ret;
1115 }
1116
1117 return 0;
1118}
1119
1120int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
1121{
1122 struct uvc_entity *entity;
1123 int ret = 0;
1124
1125 /* Find the control. */
1126 list_for_each_entry(entity, &chain->entities, chain) {
1127 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1128 if (ret < 0)
1129 goto done;
1130 }
1131
1132done:
1133 mutex_unlock(&chain->ctrl_mutex);
1134 return ret;
1135}
1136
1137int uvc_ctrl_get(struct uvc_video_chain *chain,
1138 struct v4l2_ext_control *xctrl)
1139{
1140 struct uvc_control *ctrl;
1141 struct uvc_control_mapping *mapping;
1142 struct uvc_menu_info *menu;
1143 unsigned int i;
1144 int ret;
1145
1146 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1147 if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1148 return -EINVAL;
1149
1150 if (!ctrl->loaded) {
1151 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1152 chain->dev->intfnum, ctrl->info.selector,
1153 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1154 ctrl->info.size);
1155 if (ret < 0)
1156 return ret;
1157
1158 ctrl->loaded = 1;
1159 }
1160
1161 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1162 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1163
1164 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1165 menu = mapping->menu_info;
1166 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1167 if (menu->value == xctrl->value) {
1168 xctrl->value = i;
1169 break;
1170 }
1171 }
1172 }
1173
1174 return 0;
1175}
1176
1177int uvc_ctrl_set(struct uvc_video_chain *chain,
1178 struct v4l2_ext_control *xctrl)
1179{
1180 struct uvc_control *ctrl;
1181 struct uvc_control_mapping *mapping;
1182 s32 value;
1183 u32 step;
1184 s32 min;
1185 s32 max;
1186 int ret;
1187
1188 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1189 if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
1190 return -EINVAL;
1191
1192 /* Clamp out of range values. */
1193 switch (mapping->v4l2_type) {
1194 case V4L2_CTRL_TYPE_INTEGER:
1195 if (!ctrl->cached) {
1196 ret = uvc_ctrl_populate_cache(chain, ctrl);
1197 if (ret < 0)
1198 return ret;
1199 }
1200
1201 min = mapping->get(mapping, UVC_GET_MIN,
1202 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1203 max = mapping->get(mapping, UVC_GET_MAX,
1204 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1205 step = mapping->get(mapping, UVC_GET_RES,
1206 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1207 if (step == 0)
1208 step = 1;
1209
1210 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1211 xctrl->value = clamp(xctrl->value, min, max);
1212 value = xctrl->value;
1213 break;
1214
1215 case V4L2_CTRL_TYPE_BOOLEAN:
1216 xctrl->value = clamp(xctrl->value, 0, 1);
1217 value = xctrl->value;
1218 break;
1219
1220 case V4L2_CTRL_TYPE_MENU:
1221 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1222 return -ERANGE;
1223 value = mapping->menu_info[xctrl->value].value;
1224
1225 /* Valid menu indices are reported by the GET_RES request for
1226 * UVC controls that support it.
1227 */
1228 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
1229 if (!ctrl->cached) {
1230 ret = uvc_ctrl_populate_cache(chain, ctrl);
1231 if (ret < 0)
1232 return ret;
1233 }
1234
1235 step = mapping->get(mapping, UVC_GET_RES,
1236 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1237 if (!(step & value))
1238 return -ERANGE;
1239 }
1240
1241 break;
1242
1243 default:
1244 value = xctrl->value;
1245 break;
1246 }
1247
1248 /* If the mapping doesn't span the whole UVC control, the current value
1249 * needs to be loaded from the device to perform the read-modify-write
1250 * operation.
1251 */
1252 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1253 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1254 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1255 0, ctrl->info.size);
1256 } else {
1257 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1258 ctrl->entity->id, chain->dev->intfnum,
1259 ctrl->info.selector,
1260 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1261 ctrl->info.size);
1262 if (ret < 0)
1263 return ret;
1264 }
1265
1266 ctrl->loaded = 1;
1267 }
1268
1269 /* Backup the current value in case we need to rollback later. */
1270 if (!ctrl->dirty) {
1271 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1272 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1273 ctrl->info.size);
1274 }
1275
1276 mapping->set(mapping, value,
1277 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1278
1279 ctrl->dirty = 1;
1280 ctrl->modified = 1;
1281 return 0;
1282}
1283
1284/* --------------------------------------------------------------------------
1285 * Dynamic controls
1286 */
1287
1288static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1289 const struct uvc_control *ctrl, struct uvc_control_info *info)
1290{
1291 struct uvc_ctrl_fixup {
1292 struct usb_device_id id;
1293 u8 entity;
1294 u8 selector;
1295 u8 flags;
1296 };
1297
1298 static const struct uvc_ctrl_fixup fixups[] = {
1299 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1300 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1301 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1302 UVC_CTRL_FLAG_AUTO_UPDATE },
1303 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1304 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1305 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1306 UVC_CTRL_FLAG_AUTO_UPDATE },
1307 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1308 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1309 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1310 UVC_CTRL_FLAG_AUTO_UPDATE },
1311 };
1312
1313 unsigned int i;
1314
1315 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1316 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1317 continue;
1318
1319 if (fixups[i].entity == ctrl->entity->id &&
1320 fixups[i].selector == info->selector) {
1321 info->flags = fixups[i].flags;
1322 return;
1323 }
1324 }
1325}
1326
1327/*
1328 * Query control information (size and flags) for XU controls.
1329 */
1330static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1331 const struct uvc_control *ctrl, struct uvc_control_info *info)
1332{
1333 u8 *data;
1334 int ret;
1335
1336 data = kmalloc(2, GFP_KERNEL);
1337 if (data == NULL)
1338 return -ENOMEM;
1339
1340 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1341 sizeof(info->entity));
1342 info->index = ctrl->index;
1343 info->selector = ctrl->index + 1;
1344
1345 /* Query and verify the control length (GET_LEN) */
1346 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1347 info->selector, data, 2);
1348 if (ret < 0) {
1349 uvc_trace(UVC_TRACE_CONTROL,
1350 "GET_LEN failed on control %pUl/%u (%d).\n",
1351 info->entity, info->selector, ret);
1352 goto done;
1353 }
1354
1355 info->size = le16_to_cpup((__le16 *)data);
1356
1357 /* Query the control information (GET_INFO) */
1358 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1359 info->selector, data, 1);
1360 if (ret < 0) {
1361 uvc_trace(UVC_TRACE_CONTROL,
1362 "GET_INFO failed on control %pUl/%u (%d).\n",
1363 info->entity, info->selector, ret);
1364 goto done;
1365 }
1366
1367 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1368 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1369 | (data[0] & UVC_CONTROL_CAP_GET ?
1370 UVC_CTRL_FLAG_GET_CUR : 0)
1371 | (data[0] & UVC_CONTROL_CAP_SET ?
1372 UVC_CTRL_FLAG_SET_CUR : 0)
1373 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1374 UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1375
1376 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1377
1378 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1379 "flags { get %u set %u auto %u }.\n",
1380 info->entity, info->selector, info->size,
1381 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1382 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1383 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1384
1385done:
1386 kfree(data);
1387 return ret;
1388}
1389
1390static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1391 const struct uvc_control_info *info);
1392
1393static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1394 struct uvc_control *ctrl)
1395{
1396 struct uvc_control_info info;
1397 int ret;
1398
1399 if (ctrl->initialized)
1400 return 0;
1401
1402 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1403 if (ret < 0)
1404 return ret;
1405
1406 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1407 if (ret < 0)
1408 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1409 "%pUl/%u on device %s entity %u\n", info.entity,
1410 info.selector, dev->udev->devpath, ctrl->entity->id);
1411
1412 return ret;
1413}
1414
1415int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1416 struct uvc_xu_control_query *xqry)
1417{
1418 struct uvc_entity *entity;
1419 struct uvc_control *ctrl;
1420 unsigned int i, found = 0;
1421 __u32 reqflags;
1422 __u16 size;
1423 __u8 *data = NULL;
1424 int ret;
1425
1426 /* Find the extension unit. */
1427 list_for_each_entry(entity, &chain->entities, chain) {
1428 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1429 entity->id == xqry->unit)
1430 break;
1431 }
1432
1433 if (entity->id != xqry->unit) {
1434 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1435 xqry->unit);
1436 return -ENOENT;
1437 }
1438
1439 /* Find the control and perform delayed initialization if needed. */
1440 for (i = 0; i < entity->ncontrols; ++i) {
1441 ctrl = &entity->controls[i];
1442 if (ctrl->index == xqry->selector - 1) {
1443 found = 1;
1444 break;
1445 }
1446 }
1447
1448 if (!found) {
1449 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1450 entity->extension.guidExtensionCode, xqry->selector);
1451 return -ENOENT;
1452 }
1453
1454 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1455 return -ERESTARTSYS;
1456
1457 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1458 if (ret < 0) {
1459 ret = -ENOENT;
1460 goto done;
1461 }
1462
1463 /* Validate the required buffer size and flags for the request */
1464 reqflags = 0;
1465 size = ctrl->info.size;
1466
1467 switch (xqry->query) {
1468 case UVC_GET_CUR:
1469 reqflags = UVC_CTRL_FLAG_GET_CUR;
1470 break;
1471 case UVC_GET_MIN:
1472 reqflags = UVC_CTRL_FLAG_GET_MIN;
1473 break;
1474 case UVC_GET_MAX:
1475 reqflags = UVC_CTRL_FLAG_GET_MAX;
1476 break;
1477 case UVC_GET_DEF:
1478 reqflags = UVC_CTRL_FLAG_GET_DEF;
1479 break;
1480 case UVC_GET_RES:
1481 reqflags = UVC_CTRL_FLAG_GET_RES;
1482 break;
1483 case UVC_SET_CUR:
1484 reqflags = UVC_CTRL_FLAG_SET_CUR;
1485 break;
1486 case UVC_GET_LEN:
1487 size = 2;
1488 break;
1489 case UVC_GET_INFO:
1490 size = 1;
1491 break;
1492 default:
1493 ret = -EINVAL;
1494 goto done;
1495 }
1496
1497 if (size != xqry->size) {
1498 ret = -ENOBUFS;
1499 goto done;
1500 }
1501
1502 if (reqflags && !(ctrl->info.flags & reqflags)) {
1503 ret = -EBADRQC;
1504 goto done;
1505 }
1506
1507 data = kmalloc(size, GFP_KERNEL);
1508 if (data == NULL) {
1509 ret = -ENOMEM;
1510 goto done;
1511 }
1512
1513 if (xqry->query == UVC_SET_CUR &&
1514 copy_from_user(data, xqry->data, size)) {
1515 ret = -EFAULT;
1516 goto done;
1517 }
1518
1519 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1520 chain->dev->intfnum, xqry->selector, data, size);
1521 if (ret < 0)
1522 goto done;
1523
1524 if (xqry->query != UVC_SET_CUR &&
1525 copy_to_user(xqry->data, data, size))
1526 ret = -EFAULT;
1527done:
1528 kfree(data);
1529 mutex_unlock(&chain->ctrl_mutex);
1530 return ret;
1531}
1532
1533/* --------------------------------------------------------------------------
1534 * Suspend/resume
1535 */
1536
1537/*
1538 * Restore control values after resume, skipping controls that haven't been
1539 * changed.
1540 *
1541 * TODO
1542 * - Don't restore modified controls that are back to their default value.
1543 * - Handle restore order (Auto-Exposure Mode should be restored before
1544 * Exposure Time).
1545 */
1546int uvc_ctrl_resume_device(struct uvc_device *dev)
1547{
1548 struct uvc_control *ctrl;
1549 struct uvc_entity *entity;
1550 unsigned int i;
1551 int ret;
1552
1553 /* Walk the entities list and restore controls when possible. */
1554 list_for_each_entry(entity, &dev->entities, list) {
1555
1556 for (i = 0; i < entity->ncontrols; ++i) {
1557 ctrl = &entity->controls[i];
1558
1559 if (!ctrl->initialized || !ctrl->modified ||
1560 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1561 continue;
1562
1563 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1564 ctrl->info.entity, ctrl->info.index,
1565 ctrl->info.selector);
1566 ctrl->dirty = 1;
1567 }
1568
1569 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1570 if (ret < 0)
1571 return ret;
1572 }
1573
1574 return 0;
1575}
1576
1577/* --------------------------------------------------------------------------
1578 * Control and mapping handling
1579 */
1580
1581/*
1582 * Add control information to a given control.
1583 */
1584static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1585 const struct uvc_control_info *info)
1586{
1587 int ret = 0;
1588
1589 memcpy(&ctrl->info, info, sizeof(*info));
1590 INIT_LIST_HEAD(&ctrl->info.mappings);
1591
1592 /* Allocate an array to save control values (cur, def, max, etc.) */
1593 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1594 GFP_KERNEL);
1595 if (ctrl->uvc_data == NULL) {
1596 ret = -ENOMEM;
1597 goto done;
1598 }
1599
1600 ctrl->initialized = 1;
1601
1602 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1603 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
1604 dev->udev->devpath, ctrl->entity->id);
1605
1606done:
1607 if (ret < 0)
1608 kfree(ctrl->uvc_data);
1609 return ret;
1610}
1611
1612/*
1613 * Add a control mapping to a given control.
1614 */
1615static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1616 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1617{
1618 struct uvc_control_mapping *map;
1619 unsigned int size;
1620
1621 /* Most mappings come from static kernel data and need to be duplicated.
1622 * Mappings that come from userspace will be unnecessarily duplicated,
1623 * this could be optimized.
1624 */
1625 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1626 if (map == NULL)
1627 return -ENOMEM;
1628
1629 size = sizeof(*mapping->menu_info) * mapping->menu_count;
1630 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1631 if (map->menu_info == NULL) {
1632 kfree(map);
1633 return -ENOMEM;
1634 }
1635
1636 if (map->get == NULL)
1637 map->get = uvc_get_le_value;
1638 if (map->set == NULL)
1639 map->set = uvc_set_le_value;
1640
1641 map->ctrl = &ctrl->info;
1642 list_add_tail(&map->list, &ctrl->info.mappings);
1643 uvc_trace(UVC_TRACE_CONTROL,
1644 "Adding mapping '%s' to control %pUl/%u.\n",
1645 map->name, ctrl->info.entity, ctrl->info.selector);
1646
1647 return 0;
1648}
1649
1650int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1651 const struct uvc_control_mapping *mapping)
1652{
1653 struct uvc_device *dev = chain->dev;
1654 struct uvc_control_mapping *map;
1655 struct uvc_entity *entity;
1656 struct uvc_control *ctrl;
1657 int found = 0;
1658 int ret;
1659
1660 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1661 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1662 "id 0x%08x is invalid.\n", mapping->name,
1663 mapping->id);
1664 return -EINVAL;
1665 }
1666
1667 /* Search for the matching (GUID/CS) control on the current chain */
1668 list_for_each_entry(entity, &chain->entities, chain) {
1669 unsigned int i;
1670
1671 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1672 !uvc_entity_match_guid(entity, mapping->entity))
1673 continue;
1674
1675 for (i = 0; i < entity->ncontrols; ++i) {
1676 ctrl = &entity->controls[i];
1677 if (ctrl->index == mapping->selector - 1) {
1678 found = 1;
1679 break;
1680 }
1681 }
1682
1683 if (found)
1684 break;
1685 }
1686 if (!found)
1687 return -ENOENT;
1688
1689 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1690 return -ERESTARTSYS;
1691
1692 /* Perform delayed initialization of XU controls */
1693 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
1694 if (ret < 0) {
1695 ret = -ENOENT;
1696 goto done;
1697 }
1698
1699 list_for_each_entry(map, &ctrl->info.mappings, list) {
1700 if (mapping->id == map->id) {
1701 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
1702 "control id 0x%08x already exists.\n",
1703 mapping->name, mapping->id);
1704 ret = -EEXIST;
1705 goto done;
1706 }
1707 }
1708
1709 /* Prevent excess memory consumption */
1710 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
1711 atomic_dec(&dev->nmappings);
1712 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
1713 "mappings count (%u) exceeded.\n", mapping->name,
1714 UVC_MAX_CONTROL_MAPPINGS);
1715 ret = -ENOMEM;
1716 goto done;
1717 }
1718
1719 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1720 if (ret < 0)
1721 atomic_dec(&dev->nmappings);
1722
1723done:
1724 mutex_unlock(&chain->ctrl_mutex);
1725 return ret;
1726}
1727
1728/*
1729 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1730 * are currently the ones that crash the camera or unconditionally return an
1731 * error when queried.
1732 */
1733static void uvc_ctrl_prune_entity(struct uvc_device *dev,
1734 struct uvc_entity *entity)
1735{
1736 struct uvc_ctrl_blacklist {
1737 struct usb_device_id id;
1738 u8 index;
1739 };
1740
1741 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1742 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1743 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1744 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1745 };
1746 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
1747 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
1748 };
1749
1750 const struct uvc_ctrl_blacklist *blacklist;
1751 unsigned int size;
1752 unsigned int count;
1753 unsigned int i;
1754 u8 *controls;
1755
1756 switch (UVC_ENTITY_TYPE(entity)) {
1757 case UVC_VC_PROCESSING_UNIT:
1758 blacklist = processing_blacklist;
1759 count = ARRAY_SIZE(processing_blacklist);
1760 controls = entity->processing.bmControls;
1761 size = entity->processing.bControlSize;
1762 break;
1763
1764 case UVC_ITT_CAMERA:
1765 blacklist = camera_blacklist;
1766 count = ARRAY_SIZE(camera_blacklist);
1767 controls = entity->camera.bmControls;
1768 size = entity->camera.bControlSize;
1769 break;
1770
1771 default:
1772 return;
1773 }
1774
1775 for (i = 0; i < count; ++i) {
1776 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1777 continue;
1778
1779 if (blacklist[i].index >= 8 * size ||
1780 !uvc_test_bit(controls, blacklist[i].index))
1781 continue;
1782
1783 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1784 "removing it.\n", entity->id, blacklist[i].index);
1785
1786 uvc_clear_bit(controls, blacklist[i].index);
1787 }
1788}
1789
1790/*
1791 * Add control information and hardcoded stock control mappings to the given
1792 * device.
1793 */
1794static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
1795{
1796 const struct uvc_control_info *info = uvc_ctrls;
1797 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
1798 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1799 const struct uvc_control_mapping *mend =
1800 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1801
1802 /* XU controls initialization requires querying the device for control
1803 * information. As some buggy UVC devices will crash when queried
1804 * repeatedly in a tight loop, delay XU controls initialization until
1805 * first use.
1806 */
1807 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
1808 return;
1809
1810 for (; info < iend; ++info) {
1811 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
1812 ctrl->index == info->index) {
1813 uvc_ctrl_add_info(dev, ctrl, info);
1814 break;
1815 }
1816 }
1817
1818 if (!ctrl->initialized)
1819 return;
1820
1821 for (; mapping < mend; ++mapping) {
1822 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
1823 ctrl->info.selector == mapping->selector)
1824 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1825 }
1826}
1827
1828/*
1829 * Initialize device controls.
1830 */
1831int uvc_ctrl_init_device(struct uvc_device *dev)
1832{
1833 struct uvc_entity *entity;
1834 unsigned int i;
1835
1836 /* Walk the entities list and instantiate controls */
1837 list_for_each_entry(entity, &dev->entities, list) {
1838 struct uvc_control *ctrl;
1839 unsigned int bControlSize = 0, ncontrols = 0;
1840 __u8 *bmControls = NULL;
1841
1842 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1843 bmControls = entity->extension.bmControls;
1844 bControlSize = entity->extension.bControlSize;
1845 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1846 bmControls = entity->processing.bmControls;
1847 bControlSize = entity->processing.bControlSize;
1848 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1849 bmControls = entity->camera.bmControls;
1850 bControlSize = entity->camera.bControlSize;
1851 }
1852
1853 /* Remove bogus/blacklisted controls */
1854 uvc_ctrl_prune_entity(dev, entity);
1855
1856 /* Count supported controls and allocate the controls array */
1857 for (i = 0; i < bControlSize; ++i)
1858 ncontrols += hweight8(bmControls[i]);
1859 if (ncontrols == 0)
1860 continue;
1861
1862 entity->controls = kzalloc(ncontrols * sizeof(*ctrl),
1863 GFP_KERNEL);
1864 if (entity->controls == NULL)
1865 return -ENOMEM;
1866 entity->ncontrols = ncontrols;
1867
1868 /* Initialize all supported controls */
1869 ctrl = entity->controls;
1870 for (i = 0; i < bControlSize * 8; ++i) {
1871 if (uvc_test_bit(bmControls, i) == 0)
1872 continue;
1873
1874 ctrl->entity = entity;
1875 ctrl->index = i;
1876
1877 uvc_ctrl_init_ctrl(dev, ctrl);
1878 ctrl++;
1879 }
1880 }
1881
1882 return 0;
1883}
1884
1885/*
1886 * Cleanup device controls.
1887 */
1888static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
1889 struct uvc_control *ctrl)
1890{
1891 struct uvc_control_mapping *mapping, *nm;
1892
1893 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
1894 list_del(&mapping->list);
1895 kfree(mapping->menu_info);
1896 kfree(mapping);
1897 }
1898}
1899
1900void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1901{
1902 struct uvc_entity *entity;
1903 unsigned int i;
1904
1905 /* Free controls and control mappings for all entities. */
1906 list_for_each_entry(entity, &dev->entities, list) {
1907 for (i = 0; i < entity->ncontrols; ++i) {
1908 struct uvc_control *ctrl = &entity->controls[i];
1909
1910 if (!ctrl->initialized)
1911 continue;
1912
1913 uvc_ctrl_cleanup_mappings(dev, ctrl);
1914 kfree(ctrl->uvc_data);
1915 }
1916
1917 kfree(entity->controls);
1918 }
1919}
diff --git a/drivers/media/video/uvc/uvc_driver.c b/drivers/media/video/uvc/uvc_driver.c
new file mode 100644
index 00000000000..e4100b1f68d
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_driver.c
@@ -0,0 +1,2406 @@
1/*
2 * uvc_driver.c -- USB Video Class driver
3 *
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14/*
15 * This driver aims to support video input and ouput devices compliant with the
16 * 'USB Video Class' specification.
17 *
18 * The driver doesn't support the deprecated v4l1 interface. It implements the
19 * mmap capture method only, and doesn't do any image format conversion in
20 * software. If your user-space application doesn't support YUYV or MJPEG, fix
21 * it :-). Please note that the MJPEG data have been stripped from their
22 * Huffman tables (DHT marker), you will need to add it back if your JPEG
23 * codec can't handle MJPEG data.
24 */
25
26#include <linux/kernel.h>
27#include <linux/list.h>
28#include <linux/module.h>
29#include <linux/slab.h>
30#include <linux/usb.h>
31#include <linux/videodev2.h>
32#include <linux/vmalloc.h>
33#include <linux/wait.h>
34#include <linux/version.h>
35#include <asm/atomic.h>
36#include <asm/unaligned.h>
37
38#include <media/v4l2-common.h>
39
40#include "uvcvideo.h"
41
42#define DRIVER_AUTHOR "Laurent Pinchart " \
43 "<laurent.pinchart@ideasonboard.com>"
44#define DRIVER_DESC "USB Video Class driver"
45
46unsigned int uvc_clock_param = CLOCK_MONOTONIC;
47unsigned int uvc_no_drop_param;
48static unsigned int uvc_quirks_param = -1;
49unsigned int uvc_trace_param;
50unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
51
52/* ------------------------------------------------------------------------
53 * Video formats
54 */
55
56static struct uvc_format_desc uvc_fmts[] = {
57 {
58 .name = "YUV 4:2:2 (YUYV)",
59 .guid = UVC_GUID_FORMAT_YUY2,
60 .fcc = V4L2_PIX_FMT_YUYV,
61 },
62 {
63 .name = "YUV 4:2:2 (YUYV)",
64 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
65 .fcc = V4L2_PIX_FMT_YUYV,
66 },
67 {
68 .name = "YUV 4:2:0 (NV12)",
69 .guid = UVC_GUID_FORMAT_NV12,
70 .fcc = V4L2_PIX_FMT_NV12,
71 },
72 {
73 .name = "MJPEG",
74 .guid = UVC_GUID_FORMAT_MJPEG,
75 .fcc = V4L2_PIX_FMT_MJPEG,
76 },
77 {
78 .name = "YVU 4:2:0 (YV12)",
79 .guid = UVC_GUID_FORMAT_YV12,
80 .fcc = V4L2_PIX_FMT_YVU420,
81 },
82 {
83 .name = "YUV 4:2:0 (I420)",
84 .guid = UVC_GUID_FORMAT_I420,
85 .fcc = V4L2_PIX_FMT_YUV420,
86 },
87 {
88 .name = "YUV 4:2:0 (M420)",
89 .guid = UVC_GUID_FORMAT_M420,
90 .fcc = V4L2_PIX_FMT_M420,
91 },
92 {
93 .name = "YUV 4:2:2 (UYVY)",
94 .guid = UVC_GUID_FORMAT_UYVY,
95 .fcc = V4L2_PIX_FMT_UYVY,
96 },
97 {
98 .name = "Greyscale (8-bit)",
99 .guid = UVC_GUID_FORMAT_Y800,
100 .fcc = V4L2_PIX_FMT_GREY,
101 },
102 {
103 .name = "Greyscale (16-bit)",
104 .guid = UVC_GUID_FORMAT_Y16,
105 .fcc = V4L2_PIX_FMT_Y16,
106 },
107 {
108 .name = "RGB Bayer",
109 .guid = UVC_GUID_FORMAT_BY8,
110 .fcc = V4L2_PIX_FMT_SBGGR8,
111 },
112 {
113 .name = "RGB565",
114 .guid = UVC_GUID_FORMAT_RGBP,
115 .fcc = V4L2_PIX_FMT_RGB565,
116 },
117};
118
119/* ------------------------------------------------------------------------
120 * Utility functions
121 */
122
123struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
124 __u8 epaddr)
125{
126 struct usb_host_endpoint *ep;
127 unsigned int i;
128
129 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
130 ep = &alts->endpoint[i];
131 if (ep->desc.bEndpointAddress == epaddr)
132 return ep;
133 }
134
135 return NULL;
136}
137
138static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
139{
140 unsigned int len = ARRAY_SIZE(uvc_fmts);
141 unsigned int i;
142
143 for (i = 0; i < len; ++i) {
144 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
145 return &uvc_fmts[i];
146 }
147
148 return NULL;
149}
150
151static __u32 uvc_colorspace(const __u8 primaries)
152{
153 static const __u8 colorprimaries[] = {
154 0,
155 V4L2_COLORSPACE_SRGB,
156 V4L2_COLORSPACE_470_SYSTEM_M,
157 V4L2_COLORSPACE_470_SYSTEM_BG,
158 V4L2_COLORSPACE_SMPTE170M,
159 V4L2_COLORSPACE_SMPTE240M,
160 };
161
162 if (primaries < ARRAY_SIZE(colorprimaries))
163 return colorprimaries[primaries];
164
165 return 0;
166}
167
168/* Simplify a fraction using a simple continued fraction decomposition. The
169 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
170 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
171 * arbitrary parameters to remove non-significative terms from the simple
172 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
173 * respectively seems to give nice results.
174 */
175void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
176 unsigned int n_terms, unsigned int threshold)
177{
178 uint32_t *an;
179 uint32_t x, y, r;
180 unsigned int i, n;
181
182 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
183 if (an == NULL)
184 return;
185
186 /* Convert the fraction to a simple continued fraction. See
187 * http://mathforum.org/dr.math/faq/faq.fractions.html
188 * Stop if the current term is bigger than or equal to the given
189 * threshold.
190 */
191 x = *numerator;
192 y = *denominator;
193
194 for (n = 0; n < n_terms && y != 0; ++n) {
195 an[n] = x / y;
196 if (an[n] >= threshold) {
197 if (n < 2)
198 n++;
199 break;
200 }
201
202 r = x - an[n] * y;
203 x = y;
204 y = r;
205 }
206
207 /* Expand the simple continued fraction back to an integer fraction. */
208 x = 0;
209 y = 1;
210
211 for (i = n; i > 0; --i) {
212 r = y;
213 y = an[i-1] * y + x;
214 x = r;
215 }
216
217 *numerator = y;
218 *denominator = x;
219 kfree(an);
220}
221
222/* Convert a fraction to a frame interval in 100ns multiples. The idea here is
223 * to compute numerator / denominator * 10000000 using 32 bit fixed point
224 * arithmetic only.
225 */
226uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
227{
228 uint32_t multiplier;
229
230 /* Saturate the result if the operation would overflow. */
231 if (denominator == 0 ||
232 numerator/denominator >= ((uint32_t)-1)/10000000)
233 return (uint32_t)-1;
234
235 /* Divide both the denominator and the multiplier by two until
236 * numerator * multiplier doesn't overflow. If anyone knows a better
237 * algorithm please let me know.
238 */
239 multiplier = 10000000;
240 while (numerator > ((uint32_t)-1)/multiplier) {
241 multiplier /= 2;
242 denominator /= 2;
243 }
244
245 return denominator ? numerator * multiplier / denominator : 0;
246}
247
248/* ------------------------------------------------------------------------
249 * Terminal and unit management
250 */
251
252struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
253{
254 struct uvc_entity *entity;
255
256 list_for_each_entry(entity, &dev->entities, list) {
257 if (entity->id == id)
258 return entity;
259 }
260
261 return NULL;
262}
263
264static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
265 int id, struct uvc_entity *entity)
266{
267 unsigned int i;
268
269 if (entity == NULL)
270 entity = list_entry(&dev->entities, struct uvc_entity, list);
271
272 list_for_each_entry_continue(entity, &dev->entities, list) {
273 for (i = 0; i < entity->bNrInPins; ++i)
274 if (entity->baSourceID[i] == id)
275 return entity;
276 }
277
278 return NULL;
279}
280
281static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
282{
283 struct uvc_streaming *stream;
284
285 list_for_each_entry(stream, &dev->streams, list) {
286 if (stream->header.bTerminalLink == id)
287 return stream;
288 }
289
290 return NULL;
291}
292
293/* ------------------------------------------------------------------------
294 * Descriptors parsing
295 */
296
297static int uvc_parse_format(struct uvc_device *dev,
298 struct uvc_streaming *streaming, struct uvc_format *format,
299 __u32 **intervals, unsigned char *buffer, int buflen)
300{
301 struct usb_interface *intf = streaming->intf;
302 struct usb_host_interface *alts = intf->cur_altsetting;
303 struct uvc_format_desc *fmtdesc;
304 struct uvc_frame *frame;
305 const unsigned char *start = buffer;
306 unsigned int interval;
307 unsigned int i, n;
308 __u8 ftype;
309
310 format->type = buffer[2];
311 format->index = buffer[3];
312
313 switch (buffer[2]) {
314 case UVC_VS_FORMAT_UNCOMPRESSED:
315 case UVC_VS_FORMAT_FRAME_BASED:
316 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
317 if (buflen < n) {
318 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
319 "interface %d FORMAT error\n",
320 dev->udev->devnum,
321 alts->desc.bInterfaceNumber);
322 return -EINVAL;
323 }
324
325 /* Find the format descriptor from its GUID. */
326 fmtdesc = uvc_format_by_guid(&buffer[5]);
327
328 if (fmtdesc != NULL) {
329 strlcpy(format->name, fmtdesc->name,
330 sizeof format->name);
331 format->fcc = fmtdesc->fcc;
332 } else {
333 uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
334 &buffer[5]);
335 snprintf(format->name, sizeof(format->name), "%pUl\n",
336 &buffer[5]);
337 format->fcc = 0;
338 }
339
340 format->bpp = buffer[21];
341 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
342 ftype = UVC_VS_FRAME_UNCOMPRESSED;
343 } else {
344 ftype = UVC_VS_FRAME_FRAME_BASED;
345 if (buffer[27])
346 format->flags = UVC_FMT_FLAG_COMPRESSED;
347 }
348 break;
349
350 case UVC_VS_FORMAT_MJPEG:
351 if (buflen < 11) {
352 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
353 "interface %d FORMAT error\n",
354 dev->udev->devnum,
355 alts->desc.bInterfaceNumber);
356 return -EINVAL;
357 }
358
359 strlcpy(format->name, "MJPEG", sizeof format->name);
360 format->fcc = V4L2_PIX_FMT_MJPEG;
361 format->flags = UVC_FMT_FLAG_COMPRESSED;
362 format->bpp = 0;
363 ftype = UVC_VS_FRAME_MJPEG;
364 break;
365
366 case UVC_VS_FORMAT_DV:
367 if (buflen < 9) {
368 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
369 "interface %d FORMAT error\n",
370 dev->udev->devnum,
371 alts->desc.bInterfaceNumber);
372 return -EINVAL;
373 }
374
375 switch (buffer[8] & 0x7f) {
376 case 0:
377 strlcpy(format->name, "SD-DV", sizeof format->name);
378 break;
379 case 1:
380 strlcpy(format->name, "SDL-DV", sizeof format->name);
381 break;
382 case 2:
383 strlcpy(format->name, "HD-DV", sizeof format->name);
384 break;
385 default:
386 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
387 "interface %d: unknown DV format %u\n",
388 dev->udev->devnum,
389 alts->desc.bInterfaceNumber, buffer[8]);
390 return -EINVAL;
391 }
392
393 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
394 sizeof format->name);
395
396 format->fcc = V4L2_PIX_FMT_DV;
397 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
398 format->bpp = 0;
399 ftype = 0;
400
401 /* Create a dummy frame descriptor. */
402 frame = &format->frame[0];
403 memset(&format->frame[0], 0, sizeof format->frame[0]);
404 frame->bFrameIntervalType = 1;
405 frame->dwDefaultFrameInterval = 1;
406 frame->dwFrameInterval = *intervals;
407 *(*intervals)++ = 1;
408 format->nframes = 1;
409 break;
410
411 case UVC_VS_FORMAT_MPEG2TS:
412 case UVC_VS_FORMAT_STREAM_BASED:
413 /* Not supported yet. */
414 default:
415 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
416 "interface %d unsupported format %u\n",
417 dev->udev->devnum, alts->desc.bInterfaceNumber,
418 buffer[2]);
419 return -EINVAL;
420 }
421
422 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
423
424 buflen -= buffer[0];
425 buffer += buffer[0];
426
427 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
428 * based formats have frame descriptors.
429 */
430 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
431 buffer[2] == ftype) {
432 frame = &format->frame[format->nframes];
433 if (ftype != UVC_VS_FRAME_FRAME_BASED)
434 n = buflen > 25 ? buffer[25] : 0;
435 else
436 n = buflen > 21 ? buffer[21] : 0;
437
438 n = n ? n : 3;
439
440 if (buflen < 26 + 4*n) {
441 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
442 "interface %d FRAME error\n", dev->udev->devnum,
443 alts->desc.bInterfaceNumber);
444 return -EINVAL;
445 }
446
447 frame->bFrameIndex = buffer[3];
448 frame->bmCapabilities = buffer[4];
449 frame->wWidth = get_unaligned_le16(&buffer[5]);
450 frame->wHeight = get_unaligned_le16(&buffer[7]);
451 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
452 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
453 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
454 frame->dwMaxVideoFrameBufferSize =
455 get_unaligned_le32(&buffer[17]);
456 frame->dwDefaultFrameInterval =
457 get_unaligned_le32(&buffer[21]);
458 frame->bFrameIntervalType = buffer[25];
459 } else {
460 frame->dwMaxVideoFrameBufferSize = 0;
461 frame->dwDefaultFrameInterval =
462 get_unaligned_le32(&buffer[17]);
463 frame->bFrameIntervalType = buffer[21];
464 }
465 frame->dwFrameInterval = *intervals;
466
467 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
468 * completely. Observed behaviours range from setting the
469 * value to 1.1x the actual frame size to hardwiring the
470 * 16 low bits to 0. This results in a higher than necessary
471 * memory usage as well as a wrong image size information. For
472 * uncompressed formats this can be fixed by computing the
473 * value from the frame size.
474 */
475 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
476 frame->dwMaxVideoFrameBufferSize = format->bpp
477 * frame->wWidth * frame->wHeight / 8;
478
479 /* Some bogus devices report dwMinFrameInterval equal to
480 * dwMaxFrameInterval and have dwFrameIntervalStep set to
481 * zero. Setting all null intervals to 1 fixes the problem and
482 * some other divisions by zero that could happen.
483 */
484 for (i = 0; i < n; ++i) {
485 interval = get_unaligned_le32(&buffer[26+4*i]);
486 *(*intervals)++ = interval ? interval : 1;
487 }
488
489 /* Make sure that the default frame interval stays between
490 * the boundaries.
491 */
492 n -= frame->bFrameIntervalType ? 1 : 2;
493 frame->dwDefaultFrameInterval =
494 min(frame->dwFrameInterval[n],
495 max(frame->dwFrameInterval[0],
496 frame->dwDefaultFrameInterval));
497
498 if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) {
499 frame->bFrameIntervalType = 1;
500 frame->dwFrameInterval[0] =
501 frame->dwDefaultFrameInterval;
502 }
503
504 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
505 frame->wWidth, frame->wHeight,
506 10000000/frame->dwDefaultFrameInterval,
507 (100000000/frame->dwDefaultFrameInterval)%10);
508
509 format->nframes++;
510 buflen -= buffer[0];
511 buffer += buffer[0];
512 }
513
514 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
515 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
516 buflen -= buffer[0];
517 buffer += buffer[0];
518 }
519
520 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
521 buffer[2] == UVC_VS_COLORFORMAT) {
522 if (buflen < 6) {
523 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
524 "interface %d COLORFORMAT error\n",
525 dev->udev->devnum,
526 alts->desc.bInterfaceNumber);
527 return -EINVAL;
528 }
529
530 format->colorspace = uvc_colorspace(buffer[3]);
531
532 buflen -= buffer[0];
533 buffer += buffer[0];
534 }
535
536 return buffer - start;
537}
538
539static int uvc_parse_streaming(struct uvc_device *dev,
540 struct usb_interface *intf)
541{
542 struct uvc_streaming *streaming = NULL;
543 struct uvc_format *format;
544 struct uvc_frame *frame;
545 struct usb_host_interface *alts = &intf->altsetting[0];
546 unsigned char *_buffer, *buffer = alts->extra;
547 int _buflen, buflen = alts->extralen;
548 unsigned int nformats = 0, nframes = 0, nintervals = 0;
549 unsigned int size, i, n, p;
550 __u32 *interval;
551 __u16 psize;
552 int ret = -EINVAL;
553
554 if (intf->cur_altsetting->desc.bInterfaceSubClass
555 != UVC_SC_VIDEOSTREAMING) {
556 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
557 "video streaming interface\n", dev->udev->devnum,
558 intf->altsetting[0].desc.bInterfaceNumber);
559 return -EINVAL;
560 }
561
562 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
563 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
564 "claimed\n", dev->udev->devnum,
565 intf->altsetting[0].desc.bInterfaceNumber);
566 return -EINVAL;
567 }
568
569 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
570 if (streaming == NULL) {
571 usb_driver_release_interface(&uvc_driver.driver, intf);
572 return -EINVAL;
573 }
574
575 mutex_init(&streaming->mutex);
576 streaming->dev = dev;
577 streaming->intf = usb_get_intf(intf);
578 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
579
580 /* The Pico iMage webcam has its class-specific interface descriptors
581 * after the endpoint descriptors.
582 */
583 if (buflen == 0) {
584 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
585 struct usb_host_endpoint *ep = &alts->endpoint[i];
586
587 if (ep->extralen == 0)
588 continue;
589
590 if (ep->extralen > 2 &&
591 ep->extra[1] == USB_DT_CS_INTERFACE) {
592 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
593 "from endpoint %u.\n", i);
594 buffer = alts->endpoint[i].extra;
595 buflen = alts->endpoint[i].extralen;
596 break;
597 }
598 }
599 }
600
601 /* Skip the standard interface descriptors. */
602 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
603 buflen -= buffer[0];
604 buffer += buffer[0];
605 }
606
607 if (buflen <= 2) {
608 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
609 "interface descriptors found.\n");
610 goto error;
611 }
612
613 /* Parse the header descriptor. */
614 switch (buffer[2]) {
615 case UVC_VS_OUTPUT_HEADER:
616 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
617 size = 9;
618 break;
619
620 case UVC_VS_INPUT_HEADER:
621 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
622 size = 13;
623 break;
624
625 default:
626 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
627 "%d HEADER descriptor not found.\n", dev->udev->devnum,
628 alts->desc.bInterfaceNumber);
629 goto error;
630 }
631
632 p = buflen >= 4 ? buffer[3] : 0;
633 n = buflen >= size ? buffer[size-1] : 0;
634
635 if (buflen < size + p*n) {
636 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
637 "interface %d HEADER descriptor is invalid.\n",
638 dev->udev->devnum, alts->desc.bInterfaceNumber);
639 goto error;
640 }
641
642 streaming->header.bNumFormats = p;
643 streaming->header.bEndpointAddress = buffer[6];
644 if (buffer[2] == UVC_VS_INPUT_HEADER) {
645 streaming->header.bmInfo = buffer[7];
646 streaming->header.bTerminalLink = buffer[8];
647 streaming->header.bStillCaptureMethod = buffer[9];
648 streaming->header.bTriggerSupport = buffer[10];
649 streaming->header.bTriggerUsage = buffer[11];
650 } else {
651 streaming->header.bTerminalLink = buffer[7];
652 }
653 streaming->header.bControlSize = n;
654
655 streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
656 GFP_KERNEL);
657 if (streaming->header.bmaControls == NULL) {
658 ret = -ENOMEM;
659 goto error;
660 }
661
662 buflen -= buffer[0];
663 buffer += buffer[0];
664
665 _buffer = buffer;
666 _buflen = buflen;
667
668 /* Count the format and frame descriptors. */
669 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
670 switch (_buffer[2]) {
671 case UVC_VS_FORMAT_UNCOMPRESSED:
672 case UVC_VS_FORMAT_MJPEG:
673 case UVC_VS_FORMAT_FRAME_BASED:
674 nformats++;
675 break;
676
677 case UVC_VS_FORMAT_DV:
678 /* DV format has no frame descriptor. We will create a
679 * dummy frame descriptor with a dummy frame interval.
680 */
681 nformats++;
682 nframes++;
683 nintervals++;
684 break;
685
686 case UVC_VS_FORMAT_MPEG2TS:
687 case UVC_VS_FORMAT_STREAM_BASED:
688 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
689 "interface %d FORMAT %u is not supported.\n",
690 dev->udev->devnum,
691 alts->desc.bInterfaceNumber, _buffer[2]);
692 break;
693
694 case UVC_VS_FRAME_UNCOMPRESSED:
695 case UVC_VS_FRAME_MJPEG:
696 nframes++;
697 if (_buflen > 25)
698 nintervals += _buffer[25] ? _buffer[25] : 3;
699 break;
700
701 case UVC_VS_FRAME_FRAME_BASED:
702 nframes++;
703 if (_buflen > 21)
704 nintervals += _buffer[21] ? _buffer[21] : 3;
705 break;
706 }
707
708 _buflen -= _buffer[0];
709 _buffer += _buffer[0];
710 }
711
712 if (nformats == 0) {
713 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
714 "%d has no supported formats defined.\n",
715 dev->udev->devnum, alts->desc.bInterfaceNumber);
716 goto error;
717 }
718
719 size = nformats * sizeof *format + nframes * sizeof *frame
720 + nintervals * sizeof *interval;
721 format = kzalloc(size, GFP_KERNEL);
722 if (format == NULL) {
723 ret = -ENOMEM;
724 goto error;
725 }
726
727 frame = (struct uvc_frame *)&format[nformats];
728 interval = (__u32 *)&frame[nframes];
729
730 streaming->format = format;
731 streaming->nformats = nformats;
732
733 /* Parse the format descriptors. */
734 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
735 switch (buffer[2]) {
736 case UVC_VS_FORMAT_UNCOMPRESSED:
737 case UVC_VS_FORMAT_MJPEG:
738 case UVC_VS_FORMAT_DV:
739 case UVC_VS_FORMAT_FRAME_BASED:
740 format->frame = frame;
741 ret = uvc_parse_format(dev, streaming, format,
742 &interval, buffer, buflen);
743 if (ret < 0)
744 goto error;
745
746 frame += format->nframes;
747 format++;
748
749 buflen -= ret;
750 buffer += ret;
751 continue;
752
753 default:
754 break;
755 }
756
757 buflen -= buffer[0];
758 buffer += buffer[0];
759 }
760
761 if (buflen)
762 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
763 "%d has %u bytes of trailing descriptor garbage.\n",
764 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
765
766 /* Parse the alternate settings to find the maximum bandwidth. */
767 for (i = 0; i < intf->num_altsetting; ++i) {
768 struct usb_host_endpoint *ep;
769 alts = &intf->altsetting[i];
770 ep = uvc_find_endpoint(alts,
771 streaming->header.bEndpointAddress);
772 if (ep == NULL)
773 continue;
774
775 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
776 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
777 if (psize > streaming->maxpsize)
778 streaming->maxpsize = psize;
779 }
780
781 list_add_tail(&streaming->list, &dev->streams);
782 return 0;
783
784error:
785 usb_driver_release_interface(&uvc_driver.driver, intf);
786 usb_put_intf(intf);
787 kfree(streaming->format);
788 kfree(streaming->header.bmaControls);
789 kfree(streaming);
790 return ret;
791}
792
793static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
794 unsigned int num_pads, unsigned int extra_size)
795{
796 struct uvc_entity *entity;
797 unsigned int num_inputs;
798 unsigned int size;
799 unsigned int i;
800
801 extra_size = ALIGN(extra_size, sizeof(*entity->pads));
802 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
803 size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
804 + num_inputs;
805 entity = kzalloc(size, GFP_KERNEL);
806 if (entity == NULL)
807 return NULL;
808
809 entity->id = id;
810 entity->type = type;
811
812 entity->num_links = 0;
813 entity->num_pads = num_pads;
814 entity->pads = ((void *)(entity + 1)) + extra_size;
815
816 for (i = 0; i < num_inputs; ++i)
817 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
818 if (!UVC_ENTITY_IS_OTERM(entity))
819 entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE;
820
821 entity->bNrInPins = num_inputs;
822 entity->baSourceID = (__u8 *)(&entity->pads[num_pads]);
823
824 return entity;
825}
826
827/* Parse vendor-specific extensions. */
828static int uvc_parse_vendor_control(struct uvc_device *dev,
829 const unsigned char *buffer, int buflen)
830{
831 struct usb_device *udev = dev->udev;
832 struct usb_host_interface *alts = dev->intf->cur_altsetting;
833 struct uvc_entity *unit;
834 unsigned int n, p;
835 int handled = 0;
836
837 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
838 case 0x046d: /* Logitech */
839 if (buffer[1] != 0x41 || buffer[2] != 0x01)
840 break;
841
842 /* Logitech implements several vendor specific functions
843 * through vendor specific extension units (LXU).
844 *
845 * The LXU descriptors are similar to XU descriptors
846 * (see "USB Device Video Class for Video Devices", section
847 * 3.7.2.6 "Extension Unit Descriptor") with the following
848 * differences:
849 *
850 * ----------------------------------------------------------
851 * 0 bLength 1 Number
852 * Size of this descriptor, in bytes: 24+p+n*2
853 * ----------------------------------------------------------
854 * 23+p+n bmControlsType N Bitmap
855 * Individual bits in the set are defined:
856 * 0: Absolute
857 * 1: Relative
858 *
859 * This bitset is mapped exactly the same as bmControls.
860 * ----------------------------------------------------------
861 * 23+p+n*2 bReserved 1 Boolean
862 * ----------------------------------------------------------
863 * 24+p+n*2 iExtension 1 Index
864 * Index of a string descriptor that describes this
865 * extension unit.
866 * ----------------------------------------------------------
867 */
868 p = buflen >= 22 ? buffer[21] : 0;
869 n = buflen >= 25 + p ? buffer[22+p] : 0;
870
871 if (buflen < 25 + p + 2*n) {
872 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
873 "interface %d EXTENSION_UNIT error\n",
874 udev->devnum, alts->desc.bInterfaceNumber);
875 break;
876 }
877
878 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
879 p + 1, 2*n);
880 if (unit == NULL)
881 return -ENOMEM;
882
883 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
884 unit->extension.bNumControls = buffer[20];
885 memcpy(unit->baSourceID, &buffer[22], p);
886 unit->extension.bControlSize = buffer[22+p];
887 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
888 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
889 + n;
890 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
891
892 if (buffer[24+p+2*n] != 0)
893 usb_string(udev, buffer[24+p+2*n], unit->name,
894 sizeof unit->name);
895 else
896 sprintf(unit->name, "Extension %u", buffer[3]);
897
898 list_add_tail(&unit->list, &dev->entities);
899 handled = 1;
900 break;
901 }
902
903 return handled;
904}
905
906static int uvc_parse_standard_control(struct uvc_device *dev,
907 const unsigned char *buffer, int buflen)
908{
909 struct usb_device *udev = dev->udev;
910 struct uvc_entity *unit, *term;
911 struct usb_interface *intf;
912 struct usb_host_interface *alts = dev->intf->cur_altsetting;
913 unsigned int i, n, p, len;
914 __u16 type;
915
916 switch (buffer[2]) {
917 case UVC_VC_HEADER:
918 n = buflen >= 12 ? buffer[11] : 0;
919
920 if (buflen < 12 || buflen < 12 + n) {
921 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
922 "interface %d HEADER error\n", udev->devnum,
923 alts->desc.bInterfaceNumber);
924 return -EINVAL;
925 }
926
927 dev->uvc_version = get_unaligned_le16(&buffer[3]);
928 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
929
930 /* Parse all USB Video Streaming interfaces. */
931 for (i = 0; i < n; ++i) {
932 intf = usb_ifnum_to_if(udev, buffer[12+i]);
933 if (intf == NULL) {
934 uvc_trace(UVC_TRACE_DESCR, "device %d "
935 "interface %d doesn't exists\n",
936 udev->devnum, i);
937 continue;
938 }
939
940 uvc_parse_streaming(dev, intf);
941 }
942 break;
943
944 case UVC_VC_INPUT_TERMINAL:
945 if (buflen < 8) {
946 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
947 "interface %d INPUT_TERMINAL error\n",
948 udev->devnum, alts->desc.bInterfaceNumber);
949 return -EINVAL;
950 }
951
952 /* Make sure the terminal type MSB is not null, otherwise it
953 * could be confused with a unit.
954 */
955 type = get_unaligned_le16(&buffer[4]);
956 if ((type & 0xff00) == 0) {
957 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
958 "interface %d INPUT_TERMINAL %d has invalid "
959 "type 0x%04x, skipping\n", udev->devnum,
960 alts->desc.bInterfaceNumber,
961 buffer[3], type);
962 return 0;
963 }
964
965 n = 0;
966 p = 0;
967 len = 8;
968
969 if (type == UVC_ITT_CAMERA) {
970 n = buflen >= 15 ? buffer[14] : 0;
971 len = 15;
972
973 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
974 n = buflen >= 9 ? buffer[8] : 0;
975 p = buflen >= 10 + n ? buffer[9+n] : 0;
976 len = 10;
977 }
978
979 if (buflen < len + n + p) {
980 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
981 "interface %d INPUT_TERMINAL error\n",
982 udev->devnum, alts->desc.bInterfaceNumber);
983 return -EINVAL;
984 }
985
986 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
987 1, n + p);
988 if (term == NULL)
989 return -ENOMEM;
990
991 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
992 term->camera.bControlSize = n;
993 term->camera.bmControls = (__u8 *)term + sizeof *term;
994 term->camera.wObjectiveFocalLengthMin =
995 get_unaligned_le16(&buffer[8]);
996 term->camera.wObjectiveFocalLengthMax =
997 get_unaligned_le16(&buffer[10]);
998 term->camera.wOcularFocalLength =
999 get_unaligned_le16(&buffer[12]);
1000 memcpy(term->camera.bmControls, &buffer[15], n);
1001 } else if (UVC_ENTITY_TYPE(term) ==
1002 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
1003 term->media.bControlSize = n;
1004 term->media.bmControls = (__u8 *)term + sizeof *term;
1005 term->media.bTransportModeSize = p;
1006 term->media.bmTransportModes = (__u8 *)term
1007 + sizeof *term + n;
1008 memcpy(term->media.bmControls, &buffer[9], n);
1009 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
1010 }
1011
1012 if (buffer[7] != 0)
1013 usb_string(udev, buffer[7], term->name,
1014 sizeof term->name);
1015 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
1016 sprintf(term->name, "Camera %u", buffer[3]);
1017 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
1018 sprintf(term->name, "Media %u", buffer[3]);
1019 else
1020 sprintf(term->name, "Input %u", buffer[3]);
1021
1022 list_add_tail(&term->list, &dev->entities);
1023 break;
1024
1025 case UVC_VC_OUTPUT_TERMINAL:
1026 if (buflen < 9) {
1027 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1028 "interface %d OUTPUT_TERMINAL error\n",
1029 udev->devnum, alts->desc.bInterfaceNumber);
1030 return -EINVAL;
1031 }
1032
1033 /* Make sure the terminal type MSB is not null, otherwise it
1034 * could be confused with a unit.
1035 */
1036 type = get_unaligned_le16(&buffer[4]);
1037 if ((type & 0xff00) == 0) {
1038 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1039 "interface %d OUTPUT_TERMINAL %d has invalid "
1040 "type 0x%04x, skipping\n", udev->devnum,
1041 alts->desc.bInterfaceNumber, buffer[3], type);
1042 return 0;
1043 }
1044
1045 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1046 1, 0);
1047 if (term == NULL)
1048 return -ENOMEM;
1049
1050 memcpy(term->baSourceID, &buffer[7], 1);
1051
1052 if (buffer[8] != 0)
1053 usb_string(udev, buffer[8], term->name,
1054 sizeof term->name);
1055 else
1056 sprintf(term->name, "Output %u", buffer[3]);
1057
1058 list_add_tail(&term->list, &dev->entities);
1059 break;
1060
1061 case UVC_VC_SELECTOR_UNIT:
1062 p = buflen >= 5 ? buffer[4] : 0;
1063
1064 if (buflen < 5 || buflen < 6 + p) {
1065 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1066 "interface %d SELECTOR_UNIT error\n",
1067 udev->devnum, alts->desc.bInterfaceNumber);
1068 return -EINVAL;
1069 }
1070
1071 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1072 if (unit == NULL)
1073 return -ENOMEM;
1074
1075 memcpy(unit->baSourceID, &buffer[5], p);
1076
1077 if (buffer[5+p] != 0)
1078 usb_string(udev, buffer[5+p], unit->name,
1079 sizeof unit->name);
1080 else
1081 sprintf(unit->name, "Selector %u", buffer[3]);
1082
1083 list_add_tail(&unit->list, &dev->entities);
1084 break;
1085
1086 case UVC_VC_PROCESSING_UNIT:
1087 n = buflen >= 8 ? buffer[7] : 0;
1088 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1089
1090 if (buflen < p + n) {
1091 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1092 "interface %d PROCESSING_UNIT error\n",
1093 udev->devnum, alts->desc.bInterfaceNumber);
1094 return -EINVAL;
1095 }
1096
1097 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1098 if (unit == NULL)
1099 return -ENOMEM;
1100
1101 memcpy(unit->baSourceID, &buffer[4], 1);
1102 unit->processing.wMaxMultiplier =
1103 get_unaligned_le16(&buffer[5]);
1104 unit->processing.bControlSize = buffer[7];
1105 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1106 memcpy(unit->processing.bmControls, &buffer[8], n);
1107 if (dev->uvc_version >= 0x0110)
1108 unit->processing.bmVideoStandards = buffer[9+n];
1109
1110 if (buffer[8+n] != 0)
1111 usb_string(udev, buffer[8+n], unit->name,
1112 sizeof unit->name);
1113 else
1114 sprintf(unit->name, "Processing %u", buffer[3]);
1115
1116 list_add_tail(&unit->list, &dev->entities);
1117 break;
1118
1119 case UVC_VC_EXTENSION_UNIT:
1120 p = buflen >= 22 ? buffer[21] : 0;
1121 n = buflen >= 24 + p ? buffer[22+p] : 0;
1122
1123 if (buflen < 24 + p + n) {
1124 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1125 "interface %d EXTENSION_UNIT error\n",
1126 udev->devnum, alts->desc.bInterfaceNumber);
1127 return -EINVAL;
1128 }
1129
1130 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1131 if (unit == NULL)
1132 return -ENOMEM;
1133
1134 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1135 unit->extension.bNumControls = buffer[20];
1136 memcpy(unit->baSourceID, &buffer[22], p);
1137 unit->extension.bControlSize = buffer[22+p];
1138 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1139 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1140
1141 if (buffer[23+p+n] != 0)
1142 usb_string(udev, buffer[23+p+n], unit->name,
1143 sizeof unit->name);
1144 else
1145 sprintf(unit->name, "Extension %u", buffer[3]);
1146
1147 list_add_tail(&unit->list, &dev->entities);
1148 break;
1149
1150 default:
1151 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1152 "descriptor (%u)\n", buffer[2]);
1153 break;
1154 }
1155
1156 return 0;
1157}
1158
1159static int uvc_parse_control(struct uvc_device *dev)
1160{
1161 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1162 unsigned char *buffer = alts->extra;
1163 int buflen = alts->extralen;
1164 int ret;
1165
1166 /* Parse the default alternate setting only, as the UVC specification
1167 * defines a single alternate setting, the default alternate setting
1168 * zero.
1169 */
1170
1171 while (buflen > 2) {
1172 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1173 buffer[1] != USB_DT_CS_INTERFACE)
1174 goto next_descriptor;
1175
1176 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1177 return ret;
1178
1179next_descriptor:
1180 buflen -= buffer[0];
1181 buffer += buffer[0];
1182 }
1183
1184 /* Check if the optional status endpoint is present. Built-in iSight
1185 * webcams have an interrupt endpoint but spit proprietary data that
1186 * don't conform to the UVC status endpoint messages. Don't try to
1187 * handle the interrupt endpoint for those cameras.
1188 */
1189 if (alts->desc.bNumEndpoints == 1 &&
1190 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1191 struct usb_host_endpoint *ep = &alts->endpoint[0];
1192 struct usb_endpoint_descriptor *desc = &ep->desc;
1193
1194 if (usb_endpoint_is_int_in(desc) &&
1195 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1196 desc->bInterval != 0) {
1197 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1198 "(addr %02x).\n", desc->bEndpointAddress);
1199 dev->int_ep = ep;
1200 }
1201 }
1202
1203 return 0;
1204}
1205
1206/* ------------------------------------------------------------------------
1207 * UVC device scan
1208 */
1209
1210/*
1211 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1212 * and containing the following units:
1213 *
1214 * - one or more Output Terminals (USB Streaming or Display)
1215 * - zero or one Processing Unit
1216 * - zero, one or more single-input Selector Units
1217 * - zero or one multiple-input Selector Units, provided all inputs are
1218 * connected to input terminals
1219 * - zero, one or mode single-input Extension Units
1220 * - one or more Input Terminals (Camera, External or USB Streaming)
1221 *
1222 * The terminal and units must match on of the following structures:
1223 *
1224 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1225 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1226 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1227 *
1228 * +---------+ +---------+ -> OTT_*(0)
1229 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1230 * +---------+ +---------+ -> OTT_*(n)
1231 *
1232 * The Processing Unit and Extension Units can be in any order. Additional
1233 * Extension Units connected to the main chain as single-unit branches are
1234 * also supported. Single-input Selector Units are ignored.
1235 */
1236static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1237 struct uvc_entity *entity)
1238{
1239 switch (UVC_ENTITY_TYPE(entity)) {
1240 case UVC_VC_EXTENSION_UNIT:
1241 if (uvc_trace_param & UVC_TRACE_PROBE)
1242 printk(" <- XU %d", entity->id);
1243
1244 if (entity->bNrInPins != 1) {
1245 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1246 "than 1 input pin.\n", entity->id);
1247 return -1;
1248 }
1249
1250 break;
1251
1252 case UVC_VC_PROCESSING_UNIT:
1253 if (uvc_trace_param & UVC_TRACE_PROBE)
1254 printk(" <- PU %d", entity->id);
1255
1256 if (chain->processing != NULL) {
1257 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1258 "Processing Units in chain.\n");
1259 return -1;
1260 }
1261
1262 chain->processing = entity;
1263 break;
1264
1265 case UVC_VC_SELECTOR_UNIT:
1266 if (uvc_trace_param & UVC_TRACE_PROBE)
1267 printk(" <- SU %d", entity->id);
1268
1269 /* Single-input selector units are ignored. */
1270 if (entity->bNrInPins == 1)
1271 break;
1272
1273 if (chain->selector != NULL) {
1274 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1275 "Units in chain.\n");
1276 return -1;
1277 }
1278
1279 chain->selector = entity;
1280 break;
1281
1282 case UVC_ITT_VENDOR_SPECIFIC:
1283 case UVC_ITT_CAMERA:
1284 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1285 if (uvc_trace_param & UVC_TRACE_PROBE)
1286 printk(" <- IT %d\n", entity->id);
1287
1288 break;
1289
1290 case UVC_OTT_VENDOR_SPECIFIC:
1291 case UVC_OTT_DISPLAY:
1292 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1293 if (uvc_trace_param & UVC_TRACE_PROBE)
1294 printk(" OT %d", entity->id);
1295
1296 break;
1297
1298 case UVC_TT_STREAMING:
1299 if (UVC_ENTITY_IS_ITERM(entity)) {
1300 if (uvc_trace_param & UVC_TRACE_PROBE)
1301 printk(" <- IT %d\n", entity->id);
1302 } else {
1303 if (uvc_trace_param & UVC_TRACE_PROBE)
1304 printk(" OT %d", entity->id);
1305 }
1306
1307 break;
1308
1309 default:
1310 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1311 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1312 return -1;
1313 }
1314
1315 list_add_tail(&entity->chain, &chain->entities);
1316 return 0;
1317}
1318
1319static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1320 struct uvc_entity *entity, struct uvc_entity *prev)
1321{
1322 struct uvc_entity *forward;
1323 int found;
1324
1325 /* Forward scan */
1326 forward = NULL;
1327 found = 0;
1328
1329 while (1) {
1330 forward = uvc_entity_by_reference(chain->dev, entity->id,
1331 forward);
1332 if (forward == NULL)
1333 break;
1334 if (forward == prev)
1335 continue;
1336
1337 switch (UVC_ENTITY_TYPE(forward)) {
1338 case UVC_VC_EXTENSION_UNIT:
1339 if (forward->bNrInPins != 1) {
1340 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1341 "has more than 1 input pin.\n",
1342 entity->id);
1343 return -EINVAL;
1344 }
1345
1346 list_add_tail(&forward->chain, &chain->entities);
1347 if (uvc_trace_param & UVC_TRACE_PROBE) {
1348 if (!found)
1349 printk(" (->");
1350
1351 printk(" XU %d", forward->id);
1352 found = 1;
1353 }
1354 break;
1355
1356 case UVC_OTT_VENDOR_SPECIFIC:
1357 case UVC_OTT_DISPLAY:
1358 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1359 case UVC_TT_STREAMING:
1360 if (UVC_ENTITY_IS_ITERM(forward)) {
1361 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1362 "terminal %u.\n", forward->id);
1363 return -EINVAL;
1364 }
1365
1366 list_add_tail(&forward->chain, &chain->entities);
1367 if (uvc_trace_param & UVC_TRACE_PROBE) {
1368 if (!found)
1369 printk(" (->");
1370
1371 printk(" OT %d", forward->id);
1372 found = 1;
1373 }
1374 break;
1375 }
1376 }
1377 if (found)
1378 printk(")");
1379
1380 return 0;
1381}
1382
1383static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1384 struct uvc_entity **_entity)
1385{
1386 struct uvc_entity *entity = *_entity;
1387 struct uvc_entity *term;
1388 int id = -EINVAL, i;
1389
1390 switch (UVC_ENTITY_TYPE(entity)) {
1391 case UVC_VC_EXTENSION_UNIT:
1392 case UVC_VC_PROCESSING_UNIT:
1393 id = entity->baSourceID[0];
1394 break;
1395
1396 case UVC_VC_SELECTOR_UNIT:
1397 /* Single-input selector units are ignored. */
1398 if (entity->bNrInPins == 1) {
1399 id = entity->baSourceID[0];
1400 break;
1401 }
1402
1403 if (uvc_trace_param & UVC_TRACE_PROBE)
1404 printk(" <- IT");
1405
1406 chain->selector = entity;
1407 for (i = 0; i < entity->bNrInPins; ++i) {
1408 id = entity->baSourceID[i];
1409 term = uvc_entity_by_id(chain->dev, id);
1410 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1411 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1412 "input %d isn't connected to an "
1413 "input terminal\n", entity->id, i);
1414 return -1;
1415 }
1416
1417 if (uvc_trace_param & UVC_TRACE_PROBE)
1418 printk(" %d", term->id);
1419
1420 list_add_tail(&term->chain, &chain->entities);
1421 uvc_scan_chain_forward(chain, term, entity);
1422 }
1423
1424 if (uvc_trace_param & UVC_TRACE_PROBE)
1425 printk("\n");
1426
1427 id = 0;
1428 break;
1429
1430 case UVC_ITT_VENDOR_SPECIFIC:
1431 case UVC_ITT_CAMERA:
1432 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1433 case UVC_OTT_VENDOR_SPECIFIC:
1434 case UVC_OTT_DISPLAY:
1435 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1436 case UVC_TT_STREAMING:
1437 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1438 break;
1439 }
1440
1441 if (id <= 0) {
1442 *_entity = NULL;
1443 return id;
1444 }
1445
1446 entity = uvc_entity_by_id(chain->dev, id);
1447 if (entity == NULL) {
1448 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1449 "unknown entity %d.\n", id);
1450 return -EINVAL;
1451 }
1452
1453 *_entity = entity;
1454 return 0;
1455}
1456
1457static int uvc_scan_chain(struct uvc_video_chain *chain,
1458 struct uvc_entity *term)
1459{
1460 struct uvc_entity *entity, *prev;
1461
1462 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1463
1464 entity = term;
1465 prev = NULL;
1466
1467 while (entity != NULL) {
1468 /* Entity must not be part of an existing chain */
1469 if (entity->chain.next || entity->chain.prev) {
1470 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1471 "entity %d already in chain.\n", entity->id);
1472 return -EINVAL;
1473 }
1474
1475 /* Process entity */
1476 if (uvc_scan_chain_entity(chain, entity) < 0)
1477 return -EINVAL;
1478
1479 /* Forward scan */
1480 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1481 return -EINVAL;
1482
1483 /* Backward scan */
1484 prev = entity;
1485 if (uvc_scan_chain_backward(chain, &entity) < 0)
1486 return -EINVAL;
1487 }
1488
1489 return 0;
1490}
1491
1492static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1493 char *buffer)
1494{
1495 struct uvc_entity *term;
1496 unsigned int nterms = 0;
1497 char *p = buffer;
1498
1499 list_for_each_entry(term, terms, chain) {
1500 if (!UVC_ENTITY_IS_TERM(term) ||
1501 UVC_TERM_DIRECTION(term) != dir)
1502 continue;
1503
1504 if (nterms)
1505 p += sprintf(p, ",");
1506 if (++nterms >= 4) {
1507 p += sprintf(p, "...");
1508 break;
1509 }
1510 p += sprintf(p, "%u", term->id);
1511 }
1512
1513 return p - buffer;
1514}
1515
1516static const char *uvc_print_chain(struct uvc_video_chain *chain)
1517{
1518 static char buffer[43];
1519 char *p = buffer;
1520
1521 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1522 p += sprintf(p, " -> ");
1523 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1524
1525 return buffer;
1526}
1527
1528/*
1529 * Scan the device for video chains and register video devices.
1530 *
1531 * Chains are scanned starting at their output terminals and walked backwards.
1532 */
1533static int uvc_scan_device(struct uvc_device *dev)
1534{
1535 struct uvc_video_chain *chain;
1536 struct uvc_entity *term;
1537
1538 list_for_each_entry(term, &dev->entities, list) {
1539 if (!UVC_ENTITY_IS_OTERM(term))
1540 continue;
1541
1542 /* If the terminal is already included in a chain, skip it.
1543 * This can happen for chains that have multiple output
1544 * terminals, where all output terminals beside the first one
1545 * will be inserted in the chain in forward scans.
1546 */
1547 if (term->chain.next || term->chain.prev)
1548 continue;
1549
1550 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1551 if (chain == NULL)
1552 return -ENOMEM;
1553
1554 INIT_LIST_HEAD(&chain->entities);
1555 mutex_init(&chain->ctrl_mutex);
1556 chain->dev = dev;
1557
1558 if (uvc_scan_chain(chain, term) < 0) {
1559 kfree(chain);
1560 continue;
1561 }
1562
1563 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1564 uvc_print_chain(chain));
1565
1566 list_add_tail(&chain->list, &dev->chains);
1567 }
1568
1569 if (list_empty(&dev->chains)) {
1570 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1571 return -1;
1572 }
1573
1574 return 0;
1575}
1576
1577/* ------------------------------------------------------------------------
1578 * Video device registration and unregistration
1579 */
1580
1581/*
1582 * Delete the UVC device.
1583 *
1584 * Called by the kernel when the last reference to the uvc_device structure
1585 * is released.
1586 *
1587 * As this function is called after or during disconnect(), all URBs have
1588 * already been canceled by the USB core. There is no need to kill the
1589 * interrupt URB manually.
1590 */
1591static void uvc_delete(struct uvc_device *dev)
1592{
1593 struct list_head *p, *n;
1594
1595 usb_put_intf(dev->intf);
1596 usb_put_dev(dev->udev);
1597
1598 uvc_status_cleanup(dev);
1599 uvc_ctrl_cleanup_device(dev);
1600
1601 if (dev->vdev.dev)
1602 v4l2_device_unregister(&dev->vdev);
1603#ifdef CONFIG_MEDIA_CONTROLLER
1604 if (media_devnode_is_registered(&dev->mdev.devnode))
1605 media_device_unregister(&dev->mdev);
1606#endif
1607
1608 list_for_each_safe(p, n, &dev->chains) {
1609 struct uvc_video_chain *chain;
1610 chain = list_entry(p, struct uvc_video_chain, list);
1611 kfree(chain);
1612 }
1613
1614 list_for_each_safe(p, n, &dev->entities) {
1615 struct uvc_entity *entity;
1616 entity = list_entry(p, struct uvc_entity, list);
1617#ifdef CONFIG_MEDIA_CONTROLLER
1618 uvc_mc_cleanup_entity(entity);
1619#endif
1620 if (entity->vdev) {
1621 video_device_release(entity->vdev);
1622 entity->vdev = NULL;
1623 }
1624 kfree(entity);
1625 }
1626
1627 list_for_each_safe(p, n, &dev->streams) {
1628 struct uvc_streaming *streaming;
1629 streaming = list_entry(p, struct uvc_streaming, list);
1630 usb_driver_release_interface(&uvc_driver.driver,
1631 streaming->intf);
1632 usb_put_intf(streaming->intf);
1633 kfree(streaming->format);
1634 kfree(streaming->header.bmaControls);
1635 kfree(streaming);
1636 }
1637
1638 kfree(dev);
1639}
1640
1641static void uvc_release(struct video_device *vdev)
1642{
1643 struct uvc_streaming *stream = video_get_drvdata(vdev);
1644 struct uvc_device *dev = stream->dev;
1645
1646 /* Decrement the registered streams count and delete the device when it
1647 * reaches zero.
1648 */
1649 if (atomic_dec_and_test(&dev->nstreams))
1650 uvc_delete(dev);
1651}
1652
1653/*
1654 * Unregister the video devices.
1655 */
1656static void uvc_unregister_video(struct uvc_device *dev)
1657{
1658 struct uvc_streaming *stream;
1659
1660 /* Unregistering all video devices might result in uvc_delete() being
1661 * called from inside the loop if there's no open file handle. To avoid
1662 * that, increment the stream count before iterating over the streams
1663 * and decrement it when done.
1664 */
1665 atomic_inc(&dev->nstreams);
1666
1667 list_for_each_entry(stream, &dev->streams, list) {
1668 if (stream->vdev == NULL)
1669 continue;
1670
1671 video_unregister_device(stream->vdev);
1672 stream->vdev = NULL;
1673 }
1674
1675 /* Decrement the stream count and call uvc_delete explicitly if there
1676 * are no stream left.
1677 */
1678 if (atomic_dec_and_test(&dev->nstreams))
1679 uvc_delete(dev);
1680}
1681
1682static int uvc_register_video(struct uvc_device *dev,
1683 struct uvc_streaming *stream)
1684{
1685 struct video_device *vdev;
1686 int ret;
1687
1688 /* Initialize the streaming interface with default streaming
1689 * parameters.
1690 */
1691 ret = uvc_video_init(stream);
1692 if (ret < 0) {
1693 uvc_printk(KERN_ERR, "Failed to initialize the device "
1694 "(%d).\n", ret);
1695 return ret;
1696 }
1697
1698 /* Register the device with V4L. */
1699 vdev = video_device_alloc();
1700 if (vdev == NULL) {
1701 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1702 ret);
1703 return -ENOMEM;
1704 }
1705
1706 /* We already hold a reference to dev->udev. The video device will be
1707 * unregistered before the reference is released, so we don't need to
1708 * get another one.
1709 */
1710 vdev->v4l2_dev = &dev->vdev;
1711 vdev->fops = &uvc_fops;
1712 vdev->release = uvc_release;
1713 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1714
1715 /* Set the driver data before calling video_register_device, otherwise
1716 * uvc_v4l2_open might race us.
1717 */
1718 stream->vdev = vdev;
1719 video_set_drvdata(vdev, stream);
1720
1721 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1722 if (ret < 0) {
1723 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1724 ret);
1725 stream->vdev = NULL;
1726 video_device_release(vdev);
1727 return ret;
1728 }
1729
1730 atomic_inc(&dev->nstreams);
1731 return 0;
1732}
1733
1734/*
1735 * Register all video devices in all chains.
1736 */
1737static int uvc_register_terms(struct uvc_device *dev,
1738 struct uvc_video_chain *chain)
1739{
1740 struct uvc_streaming *stream;
1741 struct uvc_entity *term;
1742 int ret;
1743
1744 list_for_each_entry(term, &chain->entities, chain) {
1745 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1746 continue;
1747
1748 stream = uvc_stream_by_id(dev, term->id);
1749 if (stream == NULL) {
1750 uvc_printk(KERN_INFO, "No streaming interface found "
1751 "for terminal %u.", term->id);
1752 continue;
1753 }
1754
1755 stream->chain = chain;
1756 ret = uvc_register_video(dev, stream);
1757 if (ret < 0)
1758 return ret;
1759
1760 term->vdev = stream->vdev;
1761 }
1762
1763 return 0;
1764}
1765
1766static int uvc_register_chains(struct uvc_device *dev)
1767{
1768 struct uvc_video_chain *chain;
1769 int ret;
1770
1771 list_for_each_entry(chain, &dev->chains, list) {
1772 ret = uvc_register_terms(dev, chain);
1773 if (ret < 0)
1774 return ret;
1775
1776#ifdef CONFIG_MEDIA_CONTROLLER
1777 ret = uvc_mc_register_entities(chain);
1778 if (ret < 0) {
1779 uvc_printk(KERN_INFO, "Failed to register entites "
1780 "(%d).\n", ret);
1781 }
1782#endif
1783 }
1784
1785 return 0;
1786}
1787
1788/* ------------------------------------------------------------------------
1789 * USB probe, disconnect, suspend and resume
1790 */
1791
1792static int uvc_probe(struct usb_interface *intf,
1793 const struct usb_device_id *id)
1794{
1795 struct usb_device *udev = interface_to_usbdev(intf);
1796 struct uvc_device *dev;
1797 int ret;
1798
1799 if (id->idVendor && id->idProduct)
1800 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1801 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1802 id->idProduct);
1803 else
1804 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1805 udev->devpath);
1806
1807 /* Allocate memory for the device and initialize it. */
1808 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1809 return -ENOMEM;
1810
1811 INIT_LIST_HEAD(&dev->entities);
1812 INIT_LIST_HEAD(&dev->chains);
1813 INIT_LIST_HEAD(&dev->streams);
1814 atomic_set(&dev->nstreams, 0);
1815 atomic_set(&dev->users, 0);
1816 atomic_set(&dev->nmappings, 0);
1817
1818 dev->udev = usb_get_dev(udev);
1819 dev->intf = usb_get_intf(intf);
1820 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1821 dev->quirks = (uvc_quirks_param == -1)
1822 ? id->driver_info : uvc_quirks_param;
1823
1824 if (udev->product != NULL)
1825 strlcpy(dev->name, udev->product, sizeof dev->name);
1826 else
1827 snprintf(dev->name, sizeof dev->name,
1828 "UVC Camera (%04x:%04x)",
1829 le16_to_cpu(udev->descriptor.idVendor),
1830 le16_to_cpu(udev->descriptor.idProduct));
1831
1832 /* Parse the Video Class control descriptor. */
1833 if (uvc_parse_control(dev) < 0) {
1834 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1835 "descriptors.\n");
1836 goto error;
1837 }
1838
1839 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1840 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1841 udev->product ? udev->product : "<unnamed>",
1842 le16_to_cpu(udev->descriptor.idVendor),
1843 le16_to_cpu(udev->descriptor.idProduct));
1844
1845 if (dev->quirks != id->driver_info) {
1846 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1847 "parameter for testing purpose.\n", dev->quirks);
1848 uvc_printk(KERN_INFO, "Please report required quirks to the "
1849 "linux-uvc-devel mailing list.\n");
1850 }
1851
1852 /* Register the media and V4L2 devices. */
1853#ifdef CONFIG_MEDIA_CONTROLLER
1854 dev->mdev.dev = &intf->dev;
1855 strlcpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
1856 if (udev->serial)
1857 strlcpy(dev->mdev.serial, udev->serial,
1858 sizeof(dev->mdev.serial));
1859 strcpy(dev->mdev.bus_info, udev->devpath);
1860 dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
1861 dev->mdev.driver_version = LINUX_VERSION_CODE;
1862 if (media_device_register(&dev->mdev) < 0)
1863 goto error;
1864
1865 dev->vdev.mdev = &dev->mdev;
1866#endif
1867 if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
1868 goto error;
1869
1870 /* Initialize controls. */
1871 if (uvc_ctrl_init_device(dev) < 0)
1872 goto error;
1873
1874 /* Scan the device for video chains. */
1875 if (uvc_scan_device(dev) < 0)
1876 goto error;
1877
1878 /* Register video device nodes. */
1879 if (uvc_register_chains(dev) < 0)
1880 goto error;
1881
1882 /* Save our data pointer in the interface data. */
1883 usb_set_intfdata(intf, dev);
1884
1885 /* Initialize the interrupt URB. */
1886 if ((ret = uvc_status_init(dev)) < 0) {
1887 uvc_printk(KERN_INFO, "Unable to initialize the status "
1888 "endpoint (%d), status interrupt will not be "
1889 "supported.\n", ret);
1890 }
1891
1892 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1893 usb_enable_autosuspend(udev);
1894 return 0;
1895
1896error:
1897 uvc_unregister_video(dev);
1898 return -ENODEV;
1899}
1900
1901static void uvc_disconnect(struct usb_interface *intf)
1902{
1903 struct uvc_device *dev = usb_get_intfdata(intf);
1904
1905 /* Set the USB interface data to NULL. This can be done outside the
1906 * lock, as there's no other reader.
1907 */
1908 usb_set_intfdata(intf, NULL);
1909
1910 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1911 UVC_SC_VIDEOSTREAMING)
1912 return;
1913
1914 dev->state |= UVC_DEV_DISCONNECTED;
1915
1916 uvc_unregister_video(dev);
1917}
1918
1919static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1920{
1921 struct uvc_device *dev = usb_get_intfdata(intf);
1922 struct uvc_streaming *stream;
1923
1924 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1925 intf->cur_altsetting->desc.bInterfaceNumber);
1926
1927 /* Controls are cached on the fly so they don't need to be saved. */
1928 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1929 UVC_SC_VIDEOCONTROL)
1930 return uvc_status_suspend(dev);
1931
1932 list_for_each_entry(stream, &dev->streams, list) {
1933 if (stream->intf == intf)
1934 return uvc_video_suspend(stream);
1935 }
1936
1937 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1938 "mismatch.\n");
1939 return -EINVAL;
1940}
1941
1942static int __uvc_resume(struct usb_interface *intf, int reset)
1943{
1944 struct uvc_device *dev = usb_get_intfdata(intf);
1945 struct uvc_streaming *stream;
1946
1947 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1948 intf->cur_altsetting->desc.bInterfaceNumber);
1949
1950 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1951 UVC_SC_VIDEOCONTROL) {
1952 if (reset) {
1953 int ret = uvc_ctrl_resume_device(dev);
1954
1955 if (ret < 0)
1956 return ret;
1957 }
1958
1959 return uvc_status_resume(dev);
1960 }
1961
1962 list_for_each_entry(stream, &dev->streams, list) {
1963 if (stream->intf == intf)
1964 return uvc_video_resume(stream, reset);
1965 }
1966
1967 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1968 "mismatch.\n");
1969 return -EINVAL;
1970}
1971
1972static int uvc_resume(struct usb_interface *intf)
1973{
1974 return __uvc_resume(intf, 0);
1975}
1976
1977static int uvc_reset_resume(struct usb_interface *intf)
1978{
1979 return __uvc_resume(intf, 1);
1980}
1981
1982/* ------------------------------------------------------------------------
1983 * Module parameters
1984 */
1985
1986static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1987{
1988 if (uvc_clock_param == CLOCK_MONOTONIC)
1989 return sprintf(buffer, "CLOCK_MONOTONIC");
1990 else
1991 return sprintf(buffer, "CLOCK_REALTIME");
1992}
1993
1994static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1995{
1996 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1997 val += strlen("clock_");
1998
1999 if (strcasecmp(val, "monotonic") == 0)
2000 uvc_clock_param = CLOCK_MONOTONIC;
2001 else if (strcasecmp(val, "realtime") == 0)
2002 uvc_clock_param = CLOCK_REALTIME;
2003 else
2004 return -EINVAL;
2005
2006 return 0;
2007}
2008
2009module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
2010 &uvc_clock_param, S_IRUGO|S_IWUSR);
2011MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
2012module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2013MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2014module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2015MODULE_PARM_DESC(quirks, "Forced device quirks");
2016module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2017MODULE_PARM_DESC(trace, "Trace level bitmask");
2018module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2019MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2020
2021/* ------------------------------------------------------------------------
2022 * Driver initialization and cleanup
2023 */
2024
2025/*
2026 * The Logitech cameras listed below have their interface class set to
2027 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
2028 * though they are compliant.
2029 */
2030static struct usb_device_id uvc_ids[] = {
2031 /* Genius eFace 2025 */
2032 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2033 | USB_DEVICE_ID_MATCH_INT_INFO,
2034 .idVendor = 0x0458,
2035 .idProduct = 0x706e,
2036 .bInterfaceClass = USB_CLASS_VIDEO,
2037 .bInterfaceSubClass = 1,
2038 .bInterfaceProtocol = 0,
2039 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2040 /* Microsoft Lifecam NX-6000 */
2041 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2042 | USB_DEVICE_ID_MATCH_INT_INFO,
2043 .idVendor = 0x045e,
2044 .idProduct = 0x00f8,
2045 .bInterfaceClass = USB_CLASS_VIDEO,
2046 .bInterfaceSubClass = 1,
2047 .bInterfaceProtocol = 0,
2048 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2049 /* Microsoft Lifecam VX-7000 */
2050 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2051 | USB_DEVICE_ID_MATCH_INT_INFO,
2052 .idVendor = 0x045e,
2053 .idProduct = 0x0723,
2054 .bInterfaceClass = USB_CLASS_VIDEO,
2055 .bInterfaceSubClass = 1,
2056 .bInterfaceProtocol = 0,
2057 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2058 /* Logitech Quickcam Fusion */
2059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2060 | USB_DEVICE_ID_MATCH_INT_INFO,
2061 .idVendor = 0x046d,
2062 .idProduct = 0x08c1,
2063 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2064 .bInterfaceSubClass = 1,
2065 .bInterfaceProtocol = 0 },
2066 /* Logitech Quickcam Orbit MP */
2067 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2068 | USB_DEVICE_ID_MATCH_INT_INFO,
2069 .idVendor = 0x046d,
2070 .idProduct = 0x08c2,
2071 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2072 .bInterfaceSubClass = 1,
2073 .bInterfaceProtocol = 0 },
2074 /* Logitech Quickcam Pro for Notebook */
2075 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2076 | USB_DEVICE_ID_MATCH_INT_INFO,
2077 .idVendor = 0x046d,
2078 .idProduct = 0x08c3,
2079 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2080 .bInterfaceSubClass = 1,
2081 .bInterfaceProtocol = 0 },
2082 /* Logitech Quickcam Pro 5000 */
2083 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2084 | USB_DEVICE_ID_MATCH_INT_INFO,
2085 .idVendor = 0x046d,
2086 .idProduct = 0x08c5,
2087 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2088 .bInterfaceSubClass = 1,
2089 .bInterfaceProtocol = 0 },
2090 /* Logitech Quickcam OEM Dell Notebook */
2091 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2092 | USB_DEVICE_ID_MATCH_INT_INFO,
2093 .idVendor = 0x046d,
2094 .idProduct = 0x08c6,
2095 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2096 .bInterfaceSubClass = 1,
2097 .bInterfaceProtocol = 0 },
2098 /* Logitech Quickcam OEM Cisco VT Camera II */
2099 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2100 | USB_DEVICE_ID_MATCH_INT_INFO,
2101 .idVendor = 0x046d,
2102 .idProduct = 0x08c7,
2103 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2104 .bInterfaceSubClass = 1,
2105 .bInterfaceProtocol = 0 },
2106 /* Chicony CNF7129 (Asus EEE 100HE) */
2107 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2108 | USB_DEVICE_ID_MATCH_INT_INFO,
2109 .idVendor = 0x04f2,
2110 .idProduct = 0xb071,
2111 .bInterfaceClass = USB_CLASS_VIDEO,
2112 .bInterfaceSubClass = 1,
2113 .bInterfaceProtocol = 0,
2114 .driver_info = UVC_QUIRK_RESTRICT_FRAME_RATE },
2115 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2116 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2117 | USB_DEVICE_ID_MATCH_INT_INFO,
2118 .idVendor = 0x058f,
2119 .idProduct = 0x3820,
2120 .bInterfaceClass = USB_CLASS_VIDEO,
2121 .bInterfaceSubClass = 1,
2122 .bInterfaceProtocol = 0,
2123 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2124 /* Apple Built-In iSight */
2125 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2126 | USB_DEVICE_ID_MATCH_INT_INFO,
2127 .idVendor = 0x05ac,
2128 .idProduct = 0x8501,
2129 .bInterfaceClass = USB_CLASS_VIDEO,
2130 .bInterfaceSubClass = 1,
2131 .bInterfaceProtocol = 0,
2132 .driver_info = UVC_QUIRK_PROBE_MINMAX
2133 | UVC_QUIRK_BUILTIN_ISIGHT },
2134 /* Foxlink ("HP Webcam" on HP Mini 5103) */
2135 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2136 | USB_DEVICE_ID_MATCH_INT_INFO,
2137 .idVendor = 0x05c8,
2138 .idProduct = 0x0403,
2139 .bInterfaceClass = USB_CLASS_VIDEO,
2140 .bInterfaceSubClass = 1,
2141 .bInterfaceProtocol = 0,
2142 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2143 /* Genesys Logic USB 2.0 PC Camera */
2144 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2145 | USB_DEVICE_ID_MATCH_INT_INFO,
2146 .idVendor = 0x05e3,
2147 .idProduct = 0x0505,
2148 .bInterfaceClass = USB_CLASS_VIDEO,
2149 .bInterfaceSubClass = 1,
2150 .bInterfaceProtocol = 0,
2151 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2152 /* Hercules Classic Silver */
2153 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2154 | USB_DEVICE_ID_MATCH_INT_INFO,
2155 .idVendor = 0x06f8,
2156 .idProduct = 0x300c,
2157 .bInterfaceClass = USB_CLASS_VIDEO,
2158 .bInterfaceSubClass = 1,
2159 .bInterfaceProtocol = 0,
2160 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2161 /* ViMicro Vega */
2162 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2163 | USB_DEVICE_ID_MATCH_INT_INFO,
2164 .idVendor = 0x0ac8,
2165 .idProduct = 0x332d,
2166 .bInterfaceClass = USB_CLASS_VIDEO,
2167 .bInterfaceSubClass = 1,
2168 .bInterfaceProtocol = 0,
2169 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2170 /* ViMicro - Minoru3D */
2171 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2172 | USB_DEVICE_ID_MATCH_INT_INFO,
2173 .idVendor = 0x0ac8,
2174 .idProduct = 0x3410,
2175 .bInterfaceClass = USB_CLASS_VIDEO,
2176 .bInterfaceSubClass = 1,
2177 .bInterfaceProtocol = 0,
2178 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2179 /* ViMicro Venus - Minoru3D */
2180 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2181 | USB_DEVICE_ID_MATCH_INT_INFO,
2182 .idVendor = 0x0ac8,
2183 .idProduct = 0x3420,
2184 .bInterfaceClass = USB_CLASS_VIDEO,
2185 .bInterfaceSubClass = 1,
2186 .bInterfaceProtocol = 0,
2187 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2188 /* MT6227 */
2189 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2190 | USB_DEVICE_ID_MATCH_INT_INFO,
2191 .idVendor = 0x0e8d,
2192 .idProduct = 0x0004,
2193 .bInterfaceClass = USB_CLASS_VIDEO,
2194 .bInterfaceSubClass = 1,
2195 .bInterfaceProtocol = 0,
2196 .driver_info = UVC_QUIRK_PROBE_MINMAX
2197 | UVC_QUIRK_PROBE_DEF },
2198 /* IMC Networks (Medion Akoya) */
2199 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2200 | USB_DEVICE_ID_MATCH_INT_INFO,
2201 .idVendor = 0x13d3,
2202 .idProduct = 0x5103,
2203 .bInterfaceClass = USB_CLASS_VIDEO,
2204 .bInterfaceSubClass = 1,
2205 .bInterfaceProtocol = 0,
2206 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2207 /* JMicron USB2.0 XGA WebCam */
2208 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2209 | USB_DEVICE_ID_MATCH_INT_INFO,
2210 .idVendor = 0x152d,
2211 .idProduct = 0x0310,
2212 .bInterfaceClass = USB_CLASS_VIDEO,
2213 .bInterfaceSubClass = 1,
2214 .bInterfaceProtocol = 0,
2215 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2216 /* Syntek (HP Spartan) */
2217 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2218 | USB_DEVICE_ID_MATCH_INT_INFO,
2219 .idVendor = 0x174f,
2220 .idProduct = 0x5212,
2221 .bInterfaceClass = USB_CLASS_VIDEO,
2222 .bInterfaceSubClass = 1,
2223 .bInterfaceProtocol = 0,
2224 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2225 /* Syntek (Samsung Q310) */
2226 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2227 | USB_DEVICE_ID_MATCH_INT_INFO,
2228 .idVendor = 0x174f,
2229 .idProduct = 0x5931,
2230 .bInterfaceClass = USB_CLASS_VIDEO,
2231 .bInterfaceSubClass = 1,
2232 .bInterfaceProtocol = 0,
2233 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2234 /* Syntek (Packard Bell EasyNote MX52 */
2235 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2236 | USB_DEVICE_ID_MATCH_INT_INFO,
2237 .idVendor = 0x174f,
2238 .idProduct = 0x8a12,
2239 .bInterfaceClass = USB_CLASS_VIDEO,
2240 .bInterfaceSubClass = 1,
2241 .bInterfaceProtocol = 0,
2242 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2243 /* Syntek (Asus F9SG) */
2244 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2245 | USB_DEVICE_ID_MATCH_INT_INFO,
2246 .idVendor = 0x174f,
2247 .idProduct = 0x8a31,
2248 .bInterfaceClass = USB_CLASS_VIDEO,
2249 .bInterfaceSubClass = 1,
2250 .bInterfaceProtocol = 0,
2251 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2252 /* Syntek (Asus U3S) */
2253 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2254 | USB_DEVICE_ID_MATCH_INT_INFO,
2255 .idVendor = 0x174f,
2256 .idProduct = 0x8a33,
2257 .bInterfaceClass = USB_CLASS_VIDEO,
2258 .bInterfaceSubClass = 1,
2259 .bInterfaceProtocol = 0,
2260 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2261 /* Syntek (JAOtech Smart Terminal) */
2262 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2263 | USB_DEVICE_ID_MATCH_INT_INFO,
2264 .idVendor = 0x174f,
2265 .idProduct = 0x8a34,
2266 .bInterfaceClass = USB_CLASS_VIDEO,
2267 .bInterfaceSubClass = 1,
2268 .bInterfaceProtocol = 0,
2269 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2270 /* Miricle 307K */
2271 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2272 | USB_DEVICE_ID_MATCH_INT_INFO,
2273 .idVendor = 0x17dc,
2274 .idProduct = 0x0202,
2275 .bInterfaceClass = USB_CLASS_VIDEO,
2276 .bInterfaceSubClass = 1,
2277 .bInterfaceProtocol = 0,
2278 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2279 /* Lenovo Thinkpad SL400/SL500 */
2280 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2281 | USB_DEVICE_ID_MATCH_INT_INFO,
2282 .idVendor = 0x17ef,
2283 .idProduct = 0x480b,
2284 .bInterfaceClass = USB_CLASS_VIDEO,
2285 .bInterfaceSubClass = 1,
2286 .bInterfaceProtocol = 0,
2287 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2288 /* Aveo Technology USB 2.0 Camera */
2289 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2290 | USB_DEVICE_ID_MATCH_INT_INFO,
2291 .idVendor = 0x1871,
2292 .idProduct = 0x0306,
2293 .bInterfaceClass = USB_CLASS_VIDEO,
2294 .bInterfaceSubClass = 1,
2295 .bInterfaceProtocol = 0,
2296 .driver_info = UVC_QUIRK_PROBE_MINMAX
2297 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2298 /* Ecamm Pico iMage */
2299 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2300 | USB_DEVICE_ID_MATCH_INT_INFO,
2301 .idVendor = 0x18cd,
2302 .idProduct = 0xcafe,
2303 .bInterfaceClass = USB_CLASS_VIDEO,
2304 .bInterfaceSubClass = 1,
2305 .bInterfaceProtocol = 0,
2306 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2307 /* Manta MM-353 Plako */
2308 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2309 | USB_DEVICE_ID_MATCH_INT_INFO,
2310 .idVendor = 0x18ec,
2311 .idProduct = 0x3188,
2312 .bInterfaceClass = USB_CLASS_VIDEO,
2313 .bInterfaceSubClass = 1,
2314 .bInterfaceProtocol = 0,
2315 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2316 /* FSC WebCam V30S */
2317 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2318 | USB_DEVICE_ID_MATCH_INT_INFO,
2319 .idVendor = 0x18ec,
2320 .idProduct = 0x3288,
2321 .bInterfaceClass = USB_CLASS_VIDEO,
2322 .bInterfaceSubClass = 1,
2323 .bInterfaceProtocol = 0,
2324 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2325 /* Arkmicro unbranded */
2326 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2327 | USB_DEVICE_ID_MATCH_INT_INFO,
2328 .idVendor = 0x18ec,
2329 .idProduct = 0x3290,
2330 .bInterfaceClass = USB_CLASS_VIDEO,
2331 .bInterfaceSubClass = 1,
2332 .bInterfaceProtocol = 0,
2333 .driver_info = UVC_QUIRK_PROBE_DEF },
2334 /* Bodelin ProScopeHR */
2335 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2336 | USB_DEVICE_ID_MATCH_DEV_HI
2337 | USB_DEVICE_ID_MATCH_INT_INFO,
2338 .idVendor = 0x19ab,
2339 .idProduct = 0x1000,
2340 .bcdDevice_hi = 0x0126,
2341 .bInterfaceClass = USB_CLASS_VIDEO,
2342 .bInterfaceSubClass = 1,
2343 .bInterfaceProtocol = 0,
2344 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2345 /* MSI StarCam 370i */
2346 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2347 | USB_DEVICE_ID_MATCH_INT_INFO,
2348 .idVendor = 0x1b3b,
2349 .idProduct = 0x2951,
2350 .bInterfaceClass = USB_CLASS_VIDEO,
2351 .bInterfaceSubClass = 1,
2352 .bInterfaceProtocol = 0,
2353 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2354 /* SiGma Micro USB Web Camera */
2355 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2356 | USB_DEVICE_ID_MATCH_INT_INFO,
2357 .idVendor = 0x1c4f,
2358 .idProduct = 0x3000,
2359 .bInterfaceClass = USB_CLASS_VIDEO,
2360 .bInterfaceSubClass = 1,
2361 .bInterfaceProtocol = 0,
2362 .driver_info = UVC_QUIRK_PROBE_MINMAX
2363 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2364 /* Generic USB Video Class */
2365 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2366 {}
2367};
2368
2369MODULE_DEVICE_TABLE(usb, uvc_ids);
2370
2371struct uvc_driver uvc_driver = {
2372 .driver = {
2373 .name = "uvcvideo",
2374 .probe = uvc_probe,
2375 .disconnect = uvc_disconnect,
2376 .suspend = uvc_suspend,
2377 .resume = uvc_resume,
2378 .reset_resume = uvc_reset_resume,
2379 .id_table = uvc_ids,
2380 .supports_autosuspend = 1,
2381 },
2382};
2383
2384static int __init uvc_init(void)
2385{
2386 int result;
2387
2388 result = usb_register(&uvc_driver.driver);
2389 if (result == 0)
2390 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2391 return result;
2392}
2393
2394static void __exit uvc_cleanup(void)
2395{
2396 usb_deregister(&uvc_driver.driver);
2397}
2398
2399module_init(uvc_init);
2400module_exit(uvc_cleanup);
2401
2402MODULE_AUTHOR(DRIVER_AUTHOR);
2403MODULE_DESCRIPTION(DRIVER_DESC);
2404MODULE_LICENSE("GPL");
2405MODULE_VERSION(DRIVER_VERSION);
2406
diff --git a/drivers/media/video/uvc/uvc_entity.c b/drivers/media/video/uvc/uvc_entity.c
new file mode 100644
index 00000000000..29e239911d0
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_entity.c
@@ -0,0 +1,126 @@
1/*
2 * uvc_entity.c -- USB Video Class driver
3 *
4 * Copyright (C) 2005-2011
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/videodev2.h>
17
18#include <media/v4l2-common.h>
19
20#include "uvcvideo.h"
21
22/* ------------------------------------------------------------------------
23 * Video subdevices registration and unregistration
24 */
25
26static int uvc_mc_register_entity(struct uvc_video_chain *chain,
27 struct uvc_entity *entity)
28{
29 const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
30 struct media_entity *sink;
31 unsigned int i;
32 int ret;
33
34 sink = (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
35 ? (entity->vdev ? &entity->vdev->entity : NULL)
36 : &entity->subdev.entity;
37 if (sink == NULL)
38 return 0;
39
40 for (i = 0; i < entity->num_pads; ++i) {
41 struct media_entity *source;
42 struct uvc_entity *remote;
43 u8 remote_pad;
44
45 if (!(entity->pads[i].flags & MEDIA_PAD_FL_SINK))
46 continue;
47
48 remote = uvc_entity_by_id(chain->dev, entity->baSourceID[i]);
49 if (remote == NULL)
50 return -EINVAL;
51
52 source = (UVC_ENTITY_TYPE(remote) == UVC_TT_STREAMING)
53 ? (remote->vdev ? &remote->vdev->entity : NULL)
54 : &remote->subdev.entity;
55 if (source == NULL)
56 continue;
57
58 remote_pad = remote->num_pads - 1;
59 ret = media_entity_create_link(source, remote_pad,
60 sink, i, flags);
61 if (ret < 0)
62 return ret;
63 }
64
65 if (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
66 return 0;
67
68 return v4l2_device_register_subdev(&chain->dev->vdev, &entity->subdev);
69}
70
71static struct v4l2_subdev_ops uvc_subdev_ops = {
72};
73
74void uvc_mc_cleanup_entity(struct uvc_entity *entity)
75{
76 if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING)
77 media_entity_cleanup(&entity->subdev.entity);
78 else if (entity->vdev != NULL)
79 media_entity_cleanup(&entity->vdev->entity);
80}
81
82static int uvc_mc_init_entity(struct uvc_entity *entity)
83{
84 int ret;
85
86 if (UVC_ENTITY_TYPE(entity) != UVC_TT_STREAMING) {
87 v4l2_subdev_init(&entity->subdev, &uvc_subdev_ops);
88 strlcpy(entity->subdev.name, entity->name,
89 sizeof(entity->subdev.name));
90
91 ret = media_entity_init(&entity->subdev.entity,
92 entity->num_pads, entity->pads, 0);
93 } else if (entity->vdev != NULL) {
94 ret = media_entity_init(&entity->vdev->entity,
95 entity->num_pads, entity->pads, 0);
96 } else
97 ret = 0;
98
99 return ret;
100}
101
102int uvc_mc_register_entities(struct uvc_video_chain *chain)
103{
104 struct uvc_entity *entity;
105 int ret;
106
107 list_for_each_entry(entity, &chain->entities, chain) {
108 ret = uvc_mc_init_entity(entity);
109 if (ret < 0) {
110 uvc_printk(KERN_INFO, "Failed to initialize entity for "
111 "entity %u\n", entity->id);
112 return ret;
113 }
114 }
115
116 list_for_each_entry(entity, &chain->entities, chain) {
117 ret = uvc_mc_register_entity(chain, entity);
118 if (ret < 0) {
119 uvc_printk(KERN_INFO, "Failed to register entity for "
120 "entity %u\n", entity->id);
121 return ret;
122 }
123 }
124
125 return 0;
126}
diff --git a/drivers/media/video/uvc/uvc_isight.c b/drivers/media/video/uvc/uvc_isight.c
new file mode 100644
index 00000000000..74bbe8f18f3
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_isight.c
@@ -0,0 +1,137 @@
1/*
2 * uvc_isight.c -- USB Video Class driver - iSight support
3 *
4 * Copyright (C) 2006-2007
5 * Ivan N. Zlatev <contact@i-nz.net>
6 * Copyright (C) 2008-2009
7 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 */
15
16#include <linux/usb.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19
20#include "uvcvideo.h"
21
22/* Built-in iSight webcams implements most of UVC 1.0 except a
23 * different packet format. Instead of sending a header at the
24 * beginning of each isochronous transfer payload, the webcam sends a
25 * single header per image (on its own in a packet), followed by
26 * packets containing data only.
27 *
28 * Offset Size (bytes) Description
29 * ------------------------------------------------------------------
30 * 0x00 1 Header length
31 * 0x01 1 Flags (UVC-compliant)
32 * 0x02 4 Always equal to '11223344'
33 * 0x06 8 Always equal to 'deadbeefdeadface'
34 * 0x0e 16 Unknown
35 *
36 * The header can be prefixed by an optional, unknown-purpose byte.
37 */
38
39static int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
40 const __u8 *data, unsigned int len)
41{
42 static const __u8 hdr[] = {
43 0x11, 0x22, 0x33, 0x44,
44 0xde, 0xad, 0xbe, 0xef,
45 0xde, 0xad, 0xfa, 0xce
46 };
47
48 unsigned int maxlen, nbytes;
49 __u8 *mem;
50 int is_header = 0;
51
52 if (buf == NULL)
53 return 0;
54
55 if ((len >= 14 && memcmp(&data[2], hdr, 12) == 0) ||
56 (len >= 15 && memcmp(&data[3], hdr, 12) == 0)) {
57 uvc_trace(UVC_TRACE_FRAME, "iSight header found\n");
58 is_header = 1;
59 }
60
61 /* Synchronize to the input stream by waiting for a header packet. */
62 if (buf->state != UVC_BUF_STATE_ACTIVE) {
63 if (!is_header) {
64 uvc_trace(UVC_TRACE_FRAME, "Dropping packet (out of "
65 "sync).\n");
66 return 0;
67 }
68
69 buf->state = UVC_BUF_STATE_ACTIVE;
70 }
71
72 /* Mark the buffer as done if we're at the beginning of a new frame.
73 *
74 * Empty buffers (bytesused == 0) don't trigger end of frame detection
75 * as it doesn't make sense to return an empty buffer.
76 */
77 if (is_header && buf->buf.bytesused != 0) {
78 buf->state = UVC_BUF_STATE_DONE;
79 return -EAGAIN;
80 }
81
82 /* Copy the video data to the buffer. Skip header packets, as they
83 * contain no data.
84 */
85 if (!is_header) {
86 maxlen = buf->buf.length - buf->buf.bytesused;
87 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
88 nbytes = min(len, maxlen);
89 memcpy(mem, data, nbytes);
90 buf->buf.bytesused += nbytes;
91
92 if (len > maxlen || buf->buf.bytesused == buf->buf.length) {
93 uvc_trace(UVC_TRACE_FRAME, "Frame complete "
94 "(overflow).\n");
95 buf->state = UVC_BUF_STATE_DONE;
96 }
97 }
98
99 return 0;
100}
101
102void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
103 struct uvc_buffer *buf)
104{
105 int ret, i;
106
107 for (i = 0; i < urb->number_of_packets; ++i) {
108 if (urb->iso_frame_desc[i].status < 0) {
109 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
110 "lost (%d).\n",
111 urb->iso_frame_desc[i].status);
112 }
113
114 /* Decode the payload packet.
115 * uvc_video_decode is entered twice when a frame transition
116 * has been detected because the end of frame can only be
117 * reliably detected when the first packet of the new frame
118 * is processed. The first pass detects the transition and
119 * closes the previous frame's buffer, the second pass
120 * processes the data of the first payload of the new frame.
121 */
122 do {
123 ret = isight_decode(&stream->queue, buf,
124 urb->transfer_buffer +
125 urb->iso_frame_desc[i].offset,
126 urb->iso_frame_desc[i].actual_length);
127
128 if (buf == NULL)
129 break;
130
131 if (buf->state == UVC_BUF_STATE_DONE ||
132 buf->state == UVC_BUF_STATE_ERROR)
133 buf = uvc_queue_next_buffer(&stream->queue,
134 buf);
135 } while (ret == -EAGAIN);
136 }
137}
diff --git a/drivers/media/video/uvc/uvc_queue.c b/drivers/media/video/uvc/uvc_queue.c
new file mode 100644
index 00000000000..677691c4450
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_queue.c
@@ -0,0 +1,634 @@
1/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/mm.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/wait.h>
22#include <linux/atomic.h>
23
24#include "uvcvideo.h"
25
26/* ------------------------------------------------------------------------
27 * Video buffers queue management.
28 *
29 * Video queues is initialized by uvc_queue_init(). The function performs
30 * basic initialization of the uvc_video_queue struct and never fails.
31 *
32 * Video buffer allocation and freeing are performed by uvc_alloc_buffers and
33 * uvc_free_buffers respectively. The former acquires the video queue lock,
34 * while the later must be called with the lock held (so that allocation can
35 * free previously allocated buffers). Trying to free buffers that are mapped
36 * to user space will return -EBUSY.
37 *
38 * Video buffers are managed using two queues. However, unlike most USB video
39 * drivers that use an in queue and an out queue, we use a main queue to hold
40 * all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
41 * hold empty buffers. This design (copied from video-buf) minimizes locking
42 * in interrupt, as only one queue is shared between interrupt and user
43 * contexts.
44 *
45 * Use cases
46 * ---------
47 *
48 * Unless stated otherwise, all operations that modify the irq buffers queue
49 * are protected by the irq spinlock.
50 *
51 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
52 *
53 * The buffers are added to the main and irq queues. Both operations are
54 * protected by the queue lock, and the later is protected by the irq
55 * spinlock as well.
56 *
57 * The completion handler fetches a buffer from the irq queue and fills it
58 * with video data. If no buffer is available (irq queue empty), the handler
59 * returns immediately.
60 *
61 * When the buffer is full, the completion handler removes it from the irq
62 * queue, marks it as done (UVC_BUF_STATE_DONE) and wakes its wait queue.
63 * At that point, any process waiting on the buffer will be woken up. If a
64 * process tries to dequeue a buffer after it has been marked done, the
65 * dequeing will succeed immediately.
66 *
67 * 2. Buffers are queued, user is waiting on a buffer and the device gets
68 * disconnected.
69 *
70 * When the device is disconnected, the kernel calls the completion handler
71 * with an appropriate status code. The handler marks all buffers in the
72 * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
73 * that any process waiting on a buffer gets woken up.
74 *
75 * Waking up up the first buffer on the irq list is not enough, as the
76 * process waiting on the buffer might restart the dequeue operation
77 * immediately.
78 *
79 */
80
81void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
82 int drop_corrupted)
83{
84 mutex_init(&queue->mutex);
85 spin_lock_init(&queue->irqlock);
86 INIT_LIST_HEAD(&queue->mainqueue);
87 INIT_LIST_HEAD(&queue->irqqueue);
88 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
89 queue->type = type;
90}
91
92/*
93 * Free the video buffers.
94 *
95 * This function must be called with the queue lock held.
96 */
97static int __uvc_free_buffers(struct uvc_video_queue *queue)
98{
99 unsigned int i;
100
101 for (i = 0; i < queue->count; ++i) {
102 if (queue->buffer[i].vma_use_count != 0)
103 return -EBUSY;
104 }
105
106 if (queue->count) {
107 uvc_queue_cancel(queue, 0);
108 INIT_LIST_HEAD(&queue->mainqueue);
109 vfree(queue->mem);
110 queue->count = 0;
111 }
112
113 return 0;
114}
115
116int uvc_free_buffers(struct uvc_video_queue *queue)
117{
118 int ret;
119
120 mutex_lock(&queue->mutex);
121 ret = __uvc_free_buffers(queue);
122 mutex_unlock(&queue->mutex);
123
124 return ret;
125}
126
127/*
128 * Allocate the video buffers.
129 *
130 * Pages are reserved to make sure they will not be swapped, as they will be
131 * filled in the URB completion handler.
132 *
133 * Buffers will be individually mapped, so they must all be page aligned.
134 */
135int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
136 unsigned int buflength)
137{
138 unsigned int bufsize = PAGE_ALIGN(buflength);
139 unsigned int i;
140 void *mem = NULL;
141 int ret;
142
143 if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
144 nbuffers = UVC_MAX_VIDEO_BUFFERS;
145
146 mutex_lock(&queue->mutex);
147
148 if ((ret = __uvc_free_buffers(queue)) < 0)
149 goto done;
150
151 /* Bail out if no buffers should be allocated. */
152 if (nbuffers == 0)
153 goto done;
154
155 /* Decrement the number of buffers until allocation succeeds. */
156 for (; nbuffers > 0; --nbuffers) {
157 mem = vmalloc_32(nbuffers * bufsize);
158 if (mem != NULL)
159 break;
160 }
161
162 if (mem == NULL) {
163 ret = -ENOMEM;
164 goto done;
165 }
166
167 for (i = 0; i < nbuffers; ++i) {
168 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
169 queue->buffer[i].buf.index = i;
170 queue->buffer[i].buf.m.offset = i * bufsize;
171 queue->buffer[i].buf.length = buflength;
172 queue->buffer[i].buf.type = queue->type;
173 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
174 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
175 queue->buffer[i].buf.flags = 0;
176 init_waitqueue_head(&queue->buffer[i].wait);
177 }
178
179 queue->mem = mem;
180 queue->count = nbuffers;
181 queue->buf_size = bufsize;
182 ret = nbuffers;
183
184done:
185 mutex_unlock(&queue->mutex);
186 return ret;
187}
188
189/*
190 * Check if buffers have been allocated.
191 */
192int uvc_queue_allocated(struct uvc_video_queue *queue)
193{
194 int allocated;
195
196 mutex_lock(&queue->mutex);
197 allocated = queue->count != 0;
198 mutex_unlock(&queue->mutex);
199
200 return allocated;
201}
202
203static void __uvc_query_buffer(struct uvc_buffer *buf,
204 struct v4l2_buffer *v4l2_buf)
205{
206 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
207
208 if (buf->vma_use_count)
209 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
210
211 switch (buf->state) {
212 case UVC_BUF_STATE_ERROR:
213 case UVC_BUF_STATE_DONE:
214 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
215 break;
216 case UVC_BUF_STATE_QUEUED:
217 case UVC_BUF_STATE_ACTIVE:
218 case UVC_BUF_STATE_READY:
219 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
220 break;
221 case UVC_BUF_STATE_IDLE:
222 default:
223 break;
224 }
225}
226
227int uvc_query_buffer(struct uvc_video_queue *queue,
228 struct v4l2_buffer *v4l2_buf)
229{
230 int ret = 0;
231
232 mutex_lock(&queue->mutex);
233 if (v4l2_buf->index >= queue->count) {
234 ret = -EINVAL;
235 goto done;
236 }
237
238 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
239
240done:
241 mutex_unlock(&queue->mutex);
242 return ret;
243}
244
245/*
246 * Queue a video buffer. Attempting to queue a buffer that has already been
247 * queued will return -EINVAL.
248 */
249int uvc_queue_buffer(struct uvc_video_queue *queue,
250 struct v4l2_buffer *v4l2_buf)
251{
252 struct uvc_buffer *buf;
253 unsigned long flags;
254 int ret = 0;
255
256 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
257
258 if (v4l2_buf->type != queue->type ||
259 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
260 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
261 "and/or memory (%u).\n", v4l2_buf->type,
262 v4l2_buf->memory);
263 return -EINVAL;
264 }
265
266 mutex_lock(&queue->mutex);
267 if (v4l2_buf->index >= queue->count) {
268 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
269 ret = -EINVAL;
270 goto done;
271 }
272
273 buf = &queue->buffer[v4l2_buf->index];
274 if (buf->state != UVC_BUF_STATE_IDLE) {
275 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
276 "(%u).\n", buf->state);
277 ret = -EINVAL;
278 goto done;
279 }
280
281 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
282 v4l2_buf->bytesused > buf->buf.length) {
283 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
284 ret = -EINVAL;
285 goto done;
286 }
287
288 spin_lock_irqsave(&queue->irqlock, flags);
289 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
290 spin_unlock_irqrestore(&queue->irqlock, flags);
291 ret = -ENODEV;
292 goto done;
293 }
294 buf->state = UVC_BUF_STATE_QUEUED;
295 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
296 buf->buf.bytesused = 0;
297 else
298 buf->buf.bytesused = v4l2_buf->bytesused;
299
300 list_add_tail(&buf->stream, &queue->mainqueue);
301 list_add_tail(&buf->queue, &queue->irqqueue);
302 spin_unlock_irqrestore(&queue->irqlock, flags);
303
304done:
305 mutex_unlock(&queue->mutex);
306 return ret;
307}
308
309static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
310{
311 if (nonblocking) {
312 return (buf->state != UVC_BUF_STATE_QUEUED &&
313 buf->state != UVC_BUF_STATE_ACTIVE &&
314 buf->state != UVC_BUF_STATE_READY)
315 ? 0 : -EAGAIN;
316 }
317
318 return wait_event_interruptible(buf->wait,
319 buf->state != UVC_BUF_STATE_QUEUED &&
320 buf->state != UVC_BUF_STATE_ACTIVE &&
321 buf->state != UVC_BUF_STATE_READY);
322}
323
324/*
325 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
326 * available.
327 */
328int uvc_dequeue_buffer(struct uvc_video_queue *queue,
329 struct v4l2_buffer *v4l2_buf, int nonblocking)
330{
331 struct uvc_buffer *buf;
332 int ret = 0;
333
334 if (v4l2_buf->type != queue->type ||
335 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
336 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
337 "and/or memory (%u).\n", v4l2_buf->type,
338 v4l2_buf->memory);
339 return -EINVAL;
340 }
341
342 mutex_lock(&queue->mutex);
343 if (list_empty(&queue->mainqueue)) {
344 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
345 ret = -EINVAL;
346 goto done;
347 }
348
349 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
350 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
351 goto done;
352
353 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
354 buf->buf.index, buf->state, buf->buf.bytesused);
355
356 switch (buf->state) {
357 case UVC_BUF_STATE_ERROR:
358 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
359 "(transmission error).\n");
360 ret = -EIO;
361 case UVC_BUF_STATE_DONE:
362 buf->state = UVC_BUF_STATE_IDLE;
363 break;
364
365 case UVC_BUF_STATE_IDLE:
366 case UVC_BUF_STATE_QUEUED:
367 case UVC_BUF_STATE_ACTIVE:
368 case UVC_BUF_STATE_READY:
369 default:
370 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
371 "(driver bug?).\n", buf->state);
372 ret = -EINVAL;
373 goto done;
374 }
375
376 list_del(&buf->stream);
377 __uvc_query_buffer(buf, v4l2_buf);
378
379done:
380 mutex_unlock(&queue->mutex);
381 return ret;
382}
383
384/*
385 * VMA operations.
386 */
387static void uvc_vm_open(struct vm_area_struct *vma)
388{
389 struct uvc_buffer *buffer = vma->vm_private_data;
390 buffer->vma_use_count++;
391}
392
393static void uvc_vm_close(struct vm_area_struct *vma)
394{
395 struct uvc_buffer *buffer = vma->vm_private_data;
396 buffer->vma_use_count--;
397}
398
399static const struct vm_operations_struct uvc_vm_ops = {
400 .open = uvc_vm_open,
401 .close = uvc_vm_close,
402};
403
404/*
405 * Memory-map a video buffer.
406 *
407 * This function implements video buffers memory mapping and is intended to be
408 * used by the device mmap handler.
409 */
410int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
411{
412 struct uvc_buffer *uninitialized_var(buffer);
413 struct page *page;
414 unsigned long addr, start, size;
415 unsigned int i;
416 int ret = 0;
417
418 start = vma->vm_start;
419 size = vma->vm_end - vma->vm_start;
420
421 mutex_lock(&queue->mutex);
422
423 for (i = 0; i < queue->count; ++i) {
424 buffer = &queue->buffer[i];
425 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
426 break;
427 }
428
429 if (i == queue->count || PAGE_ALIGN(size) != queue->buf_size) {
430 ret = -EINVAL;
431 goto done;
432 }
433
434 /*
435 * VM_IO marks the area as being an mmaped region for I/O to a
436 * device. It also prevents the region from being core dumped.
437 */
438 vma->vm_flags |= VM_IO;
439
440 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
441#ifdef CONFIG_MMU
442 while (size > 0) {
443 page = vmalloc_to_page((void *)addr);
444 if ((ret = vm_insert_page(vma, start, page)) < 0)
445 goto done;
446
447 start += PAGE_SIZE;
448 addr += PAGE_SIZE;
449 size -= PAGE_SIZE;
450 }
451#endif
452
453 vma->vm_ops = &uvc_vm_ops;
454 vma->vm_private_data = buffer;
455 uvc_vm_open(vma);
456
457done:
458 mutex_unlock(&queue->mutex);
459 return ret;
460}
461
462/*
463 * Poll the video queue.
464 *
465 * This function implements video queue polling and is intended to be used by
466 * the device poll handler.
467 */
468unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
469 poll_table *wait)
470{
471 struct uvc_buffer *buf;
472 unsigned int mask = 0;
473
474 mutex_lock(&queue->mutex);
475 if (list_empty(&queue->mainqueue)) {
476 mask |= POLLERR;
477 goto done;
478 }
479 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
480
481 poll_wait(file, &buf->wait, wait);
482 if (buf->state == UVC_BUF_STATE_DONE ||
483 buf->state == UVC_BUF_STATE_ERROR) {
484 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
485 mask |= POLLIN | POLLRDNORM;
486 else
487 mask |= POLLOUT | POLLWRNORM;
488 }
489
490done:
491 mutex_unlock(&queue->mutex);
492 return mask;
493}
494
495#ifndef CONFIG_MMU
496/*
497 * Get unmapped area.
498 *
499 * NO-MMU arch need this function to make mmap() work correctly.
500 */
501unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
502 unsigned long pgoff)
503{
504 struct uvc_buffer *buffer;
505 unsigned int i;
506 unsigned long ret;
507
508 mutex_lock(&queue->mutex);
509 for (i = 0; i < queue->count; ++i) {
510 buffer = &queue->buffer[i];
511 if ((buffer->buf.m.offset >> PAGE_SHIFT) == pgoff)
512 break;
513 }
514 if (i == queue->count) {
515 ret = -EINVAL;
516 goto done;
517 }
518 ret = (unsigned long)queue->mem + buffer->buf.m.offset;
519done:
520 mutex_unlock(&queue->mutex);
521 return ret;
522}
523#endif
524
525/*
526 * Enable or disable the video buffers queue.
527 *
528 * The queue must be enabled before starting video acquisition and must be
529 * disabled after stopping it. This ensures that the video buffers queue
530 * state can be properly initialized before buffers are accessed from the
531 * interrupt handler.
532 *
533 * Enabling the video queue returns -EBUSY if the queue is already enabled.
534 *
535 * Disabling the video queue cancels the queue and removes all buffers from
536 * the main queue.
537 *
538 * This function can't be called from interrupt context. Use
539 * uvc_queue_cancel() instead.
540 */
541int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
542{
543 unsigned int i;
544 int ret = 0;
545
546 mutex_lock(&queue->mutex);
547 if (enable) {
548 if (uvc_queue_streaming(queue)) {
549 ret = -EBUSY;
550 goto done;
551 }
552 queue->flags |= UVC_QUEUE_STREAMING;
553 queue->buf_used = 0;
554 } else {
555 uvc_queue_cancel(queue, 0);
556 INIT_LIST_HEAD(&queue->mainqueue);
557
558 for (i = 0; i < queue->count; ++i) {
559 queue->buffer[i].error = 0;
560 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
561 }
562
563 queue->flags &= ~UVC_QUEUE_STREAMING;
564 }
565
566done:
567 mutex_unlock(&queue->mutex);
568 return ret;
569}
570
571/*
572 * Cancel the video buffers queue.
573 *
574 * Cancelling the queue marks all buffers on the irq queue as erroneous,
575 * wakes them up and removes them from the queue.
576 *
577 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
578 * fail with -ENODEV.
579 *
580 * This function acquires the irq spinlock and can be called from interrupt
581 * context.
582 */
583void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
584{
585 struct uvc_buffer *buf;
586 unsigned long flags;
587
588 spin_lock_irqsave(&queue->irqlock, flags);
589 while (!list_empty(&queue->irqqueue)) {
590 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
591 queue);
592 list_del(&buf->queue);
593 buf->state = UVC_BUF_STATE_ERROR;
594 wake_up(&buf->wait);
595 }
596 /* This must be protected by the irqlock spinlock to avoid race
597 * conditions between uvc_queue_buffer and the disconnection event that
598 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
599 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
600 * state outside the queue code.
601 */
602 if (disconnect)
603 queue->flags |= UVC_QUEUE_DISCONNECTED;
604 spin_unlock_irqrestore(&queue->irqlock, flags);
605}
606
607struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
608 struct uvc_buffer *buf)
609{
610 struct uvc_buffer *nextbuf;
611 unsigned long flags;
612
613 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
614 buf->error = 0;
615 buf->state = UVC_BUF_STATE_QUEUED;
616 buf->buf.bytesused = 0;
617 return buf;
618 }
619
620 spin_lock_irqsave(&queue->irqlock, flags);
621 list_del(&buf->queue);
622 buf->error = 0;
623 buf->state = UVC_BUF_STATE_DONE;
624 if (!list_empty(&queue->irqqueue))
625 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
626 queue);
627 else
628 nextbuf = NULL;
629 spin_unlock_irqrestore(&queue->irqlock, flags);
630
631 wake_up(&buf->wait);
632 return nextbuf;
633}
634
diff --git a/drivers/media/video/uvc/uvc_status.c b/drivers/media/video/uvc/uvc_status.c
new file mode 100644
index 00000000000..b7492775e6a
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_status.c
@@ -0,0 +1,237 @@
1/*
2 * uvc_status.c -- USB Video Class driver - Status endpoint
3 *
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/input.h>
16#include <linux/slab.h>
17#include <linux/usb.h>
18#include <linux/usb/input.h>
19
20#include "uvcvideo.h"
21
22/* --------------------------------------------------------------------------
23 * Input device
24 */
25#ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
26static int uvc_input_init(struct uvc_device *dev)
27{
28 struct input_dev *input;
29 int ret;
30
31 input = input_allocate_device();
32 if (input == NULL)
33 return -ENOMEM;
34
35 usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
36 strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
37
38 input->name = dev->name;
39 input->phys = dev->input_phys;
40 usb_to_input_id(dev->udev, &input->id);
41 input->dev.parent = &dev->intf->dev;
42
43 __set_bit(EV_KEY, input->evbit);
44 __set_bit(KEY_CAMERA, input->keybit);
45
46 if ((ret = input_register_device(input)) < 0)
47 goto error;
48
49 dev->input = input;
50 return 0;
51
52error:
53 input_free_device(input);
54 return ret;
55}
56
57static void uvc_input_cleanup(struct uvc_device *dev)
58{
59 if (dev->input)
60 input_unregister_device(dev->input);
61}
62
63static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
64 int value)
65{
66 if (dev->input) {
67 input_report_key(dev->input, code, value);
68 input_sync(dev->input);
69 }
70}
71
72#else
73#define uvc_input_init(dev)
74#define uvc_input_cleanup(dev)
75#define uvc_input_report_key(dev, code, value)
76#endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
77
78/* --------------------------------------------------------------------------
79 * Status interrupt endpoint
80 */
81static void uvc_event_streaming(struct uvc_device *dev, __u8 *data, int len)
82{
83 if (len < 3) {
84 uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event "
85 "received.\n");
86 return;
87 }
88
89 if (data[2] == 0) {
90 if (len < 4)
91 return;
92 uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n",
93 data[1], data[3] ? "pressed" : "released", len);
94 uvc_input_report_key(dev, KEY_CAMERA, data[3]);
95 } else {
96 uvc_trace(UVC_TRACE_STATUS, "Stream %u error event %02x %02x "
97 "len %d.\n", data[1], data[2], data[3], len);
98 }
99}
100
101static void uvc_event_control(struct uvc_device *dev, __u8 *data, int len)
102{
103 char *attrs[3] = { "value", "info", "failure" };
104
105 if (len < 6 || data[2] != 0 || data[4] > 2) {
106 uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
107 "received.\n");
108 return;
109 }
110
111 uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
112 data[1], data[3], attrs[data[4]], len);
113}
114
115static void uvc_status_complete(struct urb *urb)
116{
117 struct uvc_device *dev = urb->context;
118 int len, ret;
119
120 switch (urb->status) {
121 case 0:
122 break;
123
124 case -ENOENT: /* usb_kill_urb() called. */
125 case -ECONNRESET: /* usb_unlink_urb() called. */
126 case -ESHUTDOWN: /* The endpoint is being disabled. */
127 case -EPROTO: /* Device is disconnected (reported by some
128 * host controller). */
129 return;
130
131 default:
132 uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
133 "completion handler.\n", urb->status);
134 return;
135 }
136
137 len = urb->actual_length;
138 if (len > 0) {
139 switch (dev->status[0] & 0x0f) {
140 case UVC_STATUS_TYPE_CONTROL:
141 uvc_event_control(dev, dev->status, len);
142 break;
143
144 case UVC_STATUS_TYPE_STREAMING:
145 uvc_event_streaming(dev, dev->status, len);
146 break;
147
148 default:
149 uvc_trace(UVC_TRACE_STATUS, "Unknown status event "
150 "type %u.\n", dev->status[0]);
151 break;
152 }
153 }
154
155 /* Resubmit the URB. */
156 urb->interval = dev->int_ep->desc.bInterval;
157 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
158 uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
159 ret);
160 }
161}
162
163int uvc_status_init(struct uvc_device *dev)
164{
165 struct usb_host_endpoint *ep = dev->int_ep;
166 unsigned int pipe;
167 int interval;
168
169 if (ep == NULL)
170 return 0;
171
172 uvc_input_init(dev);
173
174 dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
175 if (dev->status == NULL)
176 return -ENOMEM;
177
178 dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
179 if (dev->int_urb == NULL) {
180 kfree(dev->status);
181 return -ENOMEM;
182 }
183
184 pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
185
186 /* For high-speed interrupt endpoints, the bInterval value is used as
187 * an exponent of two. Some developers forgot about it.
188 */
189 interval = ep->desc.bInterval;
190 if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
191 (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
192 interval = fls(interval) - 1;
193
194 usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
195 dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
196 dev, interval);
197
198 return 0;
199}
200
201void uvc_status_cleanup(struct uvc_device *dev)
202{
203 usb_kill_urb(dev->int_urb);
204 usb_free_urb(dev->int_urb);
205 kfree(dev->status);
206 uvc_input_cleanup(dev);
207}
208
209int uvc_status_start(struct uvc_device *dev)
210{
211 if (dev->int_urb == NULL)
212 return 0;
213
214 return usb_submit_urb(dev->int_urb, GFP_KERNEL);
215}
216
217void uvc_status_stop(struct uvc_device *dev)
218{
219 usb_kill_urb(dev->int_urb);
220}
221
222int uvc_status_suspend(struct uvc_device *dev)
223{
224 if (atomic_read(&dev->users))
225 usb_kill_urb(dev->int_urb);
226
227 return 0;
228}
229
230int uvc_status_resume(struct uvc_device *dev)
231{
232 if (dev->int_urb == NULL || atomic_read(&dev->users) == 0)
233 return 0;
234
235 return usb_submit_urb(dev->int_urb, GFP_NOIO);
236}
237
diff --git a/drivers/media/video/uvc/uvc_v4l2.c b/drivers/media/video/uvc/uvc_v4l2.c
new file mode 100644
index 00000000000..ea71d5f1f6d
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_v4l2.c
@@ -0,0 +1,1143 @@
1/*
2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
3 *
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/version.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/usb.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
23#include <linux/wait.h>
24#include <linux/atomic.h>
25
26#include <media/v4l2-common.h>
27#include <media/v4l2-ioctl.h>
28
29#include "uvcvideo.h"
30
31/* ------------------------------------------------------------------------
32 * UVC ioctls
33 */
34static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
35 struct uvc_xu_control_mapping *xmap, int old)
36{
37 struct uvc_control_mapping *map;
38 unsigned int size;
39 int ret;
40
41 map = kzalloc(sizeof *map, GFP_KERNEL);
42 if (map == NULL)
43 return -ENOMEM;
44
45 map->id = xmap->id;
46 memcpy(map->name, xmap->name, sizeof map->name);
47 memcpy(map->entity, xmap->entity, sizeof map->entity);
48 map->selector = xmap->selector;
49 map->size = xmap->size;
50 map->offset = xmap->offset;
51 map->v4l2_type = xmap->v4l2_type;
52 map->data_type = xmap->data_type;
53
54 switch (xmap->v4l2_type) {
55 case V4L2_CTRL_TYPE_INTEGER:
56 case V4L2_CTRL_TYPE_BOOLEAN:
57 case V4L2_CTRL_TYPE_BUTTON:
58 break;
59
60 case V4L2_CTRL_TYPE_MENU:
61 if (old) {
62 uvc_trace(UVC_TRACE_CONTROL, "V4L2_CTRL_TYPE_MENU not "
63 "supported for UVCIOC_CTRL_MAP_OLD.\n");
64 ret = -EINVAL;
65 goto done;
66 }
67
68 size = xmap->menu_count * sizeof(*map->menu_info);
69 map->menu_info = kmalloc(size, GFP_KERNEL);
70 if (map->menu_info == NULL) {
71 ret = -ENOMEM;
72 goto done;
73 }
74
75 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
76 ret = -EFAULT;
77 goto done;
78 }
79
80 map->menu_count = xmap->menu_count;
81 break;
82
83 default:
84 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
85 "%u.\n", xmap->v4l2_type);
86 ret = -ENOTTY;
87 goto done;
88 }
89
90 ret = uvc_ctrl_add_mapping(chain, map);
91
92done:
93 kfree(map->menu_info);
94 kfree(map);
95
96 return ret;
97}
98
99/* ------------------------------------------------------------------------
100 * V4L2 interface
101 */
102
103/*
104 * Find the frame interval closest to the requested frame interval for the
105 * given frame format and size. This should be done by the device as part of
106 * the Video Probe and Commit negotiation, but some hardware don't implement
107 * that feature.
108 */
109static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
110{
111 unsigned int i;
112
113 if (frame->bFrameIntervalType) {
114 __u32 best = -1, dist;
115
116 for (i = 0; i < frame->bFrameIntervalType; ++i) {
117 dist = interval > frame->dwFrameInterval[i]
118 ? interval - frame->dwFrameInterval[i]
119 : frame->dwFrameInterval[i] - interval;
120
121 if (dist > best)
122 break;
123
124 best = dist;
125 }
126
127 interval = frame->dwFrameInterval[i-1];
128 } else {
129 const __u32 min = frame->dwFrameInterval[0];
130 const __u32 max = frame->dwFrameInterval[1];
131 const __u32 step = frame->dwFrameInterval[2];
132
133 interval = min + (interval - min + step/2) / step * step;
134 if (interval > max)
135 interval = max;
136 }
137
138 return interval;
139}
140
141static int uvc_v4l2_try_format(struct uvc_streaming *stream,
142 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
143 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
144{
145 struct uvc_format *format = NULL;
146 struct uvc_frame *frame = NULL;
147 __u16 rw, rh;
148 unsigned int d, maxd;
149 unsigned int i;
150 __u32 interval;
151 int ret = 0;
152 __u8 *fcc;
153
154 if (fmt->type != stream->type)
155 return -EINVAL;
156
157 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
158 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
159 fmt->fmt.pix.pixelformat,
160 fcc[0], fcc[1], fcc[2], fcc[3],
161 fmt->fmt.pix.width, fmt->fmt.pix.height);
162
163 /* Check if the hardware supports the requested format. */
164 for (i = 0; i < stream->nformats; ++i) {
165 format = &stream->format[i];
166 if (format->fcc == fmt->fmt.pix.pixelformat)
167 break;
168 }
169
170 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
171 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
172 fmt->fmt.pix.pixelformat);
173 return -EINVAL;
174 }
175
176 /* Find the closest image size. The distance between image sizes is
177 * the size in pixels of the non-overlapping regions between the
178 * requested size and the frame-specified size.
179 */
180 rw = fmt->fmt.pix.width;
181 rh = fmt->fmt.pix.height;
182 maxd = (unsigned int)-1;
183
184 for (i = 0; i < format->nframes; ++i) {
185 __u16 w = format->frame[i].wWidth;
186 __u16 h = format->frame[i].wHeight;
187
188 d = min(w, rw) * min(h, rh);
189 d = w*h + rw*rh - 2*d;
190 if (d < maxd) {
191 maxd = d;
192 frame = &format->frame[i];
193 }
194
195 if (maxd == 0)
196 break;
197 }
198
199 if (frame == NULL) {
200 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
201 fmt->fmt.pix.width, fmt->fmt.pix.height);
202 return -EINVAL;
203 }
204
205 /* Use the default frame interval. */
206 interval = frame->dwDefaultFrameInterval;
207 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
208 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
209 (100000000/interval)%10);
210
211 /* Set the format index, frame index and frame interval. */
212 memset(probe, 0, sizeof *probe);
213 probe->bmHint = 1; /* dwFrameInterval */
214 probe->bFormatIndex = format->index;
215 probe->bFrameIndex = frame->bFrameIndex;
216 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
217 /* Some webcams stall the probe control set request when the
218 * dwMaxVideoFrameSize field is set to zero. The UVC specification
219 * clearly states that the field is read-only from the host, so this
220 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
221 * the webcam to work around the problem.
222 *
223 * The workaround could probably be enabled for all webcams, so the
224 * quirk can be removed if needed. It's currently useful to detect
225 * webcam bugs and fix them before they hit the market (providing
226 * developers test their webcams with the Linux driver as well as with
227 * the Windows driver).
228 */
229 mutex_lock(&stream->mutex);
230 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
231 probe->dwMaxVideoFrameSize =
232 stream->ctrl.dwMaxVideoFrameSize;
233
234 /* Probe the device. */
235 ret = uvc_probe_video(stream, probe);
236 mutex_unlock(&stream->mutex);
237 if (ret < 0)
238 goto done;
239
240 fmt->fmt.pix.width = frame->wWidth;
241 fmt->fmt.pix.height = frame->wHeight;
242 fmt->fmt.pix.field = V4L2_FIELD_NONE;
243 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
244 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
245 fmt->fmt.pix.colorspace = format->colorspace;
246 fmt->fmt.pix.priv = 0;
247
248 if (uvc_format != NULL)
249 *uvc_format = format;
250 if (uvc_frame != NULL)
251 *uvc_frame = frame;
252
253done:
254 return ret;
255}
256
257static int uvc_v4l2_get_format(struct uvc_streaming *stream,
258 struct v4l2_format *fmt)
259{
260 struct uvc_format *format;
261 struct uvc_frame *frame;
262 int ret = 0;
263
264 if (fmt->type != stream->type)
265 return -EINVAL;
266
267 mutex_lock(&stream->mutex);
268 format = stream->cur_format;
269 frame = stream->cur_frame;
270
271 if (format == NULL || frame == NULL) {
272 ret = -EINVAL;
273 goto done;
274 }
275
276 fmt->fmt.pix.pixelformat = format->fcc;
277 fmt->fmt.pix.width = frame->wWidth;
278 fmt->fmt.pix.height = frame->wHeight;
279 fmt->fmt.pix.field = V4L2_FIELD_NONE;
280 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
281 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
282 fmt->fmt.pix.colorspace = format->colorspace;
283 fmt->fmt.pix.priv = 0;
284
285done:
286 mutex_unlock(&stream->mutex);
287 return ret;
288}
289
290static int uvc_v4l2_set_format(struct uvc_streaming *stream,
291 struct v4l2_format *fmt)
292{
293 struct uvc_streaming_control probe;
294 struct uvc_format *format;
295 struct uvc_frame *frame;
296 int ret;
297
298 if (fmt->type != stream->type)
299 return -EINVAL;
300
301 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
302 if (ret < 0)
303 return ret;
304
305 mutex_lock(&stream->mutex);
306
307 if (uvc_queue_allocated(&stream->queue)) {
308 ret = -EBUSY;
309 goto done;
310 }
311
312 memcpy(&stream->ctrl, &probe, sizeof probe);
313 stream->cur_format = format;
314 stream->cur_frame = frame;
315
316done:
317 mutex_unlock(&stream->mutex);
318 return ret;
319}
320
321static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
322 struct v4l2_streamparm *parm)
323{
324 uint32_t numerator, denominator;
325
326 if (parm->type != stream->type)
327 return -EINVAL;
328
329 mutex_lock(&stream->mutex);
330 numerator = stream->ctrl.dwFrameInterval;
331 mutex_unlock(&stream->mutex);
332
333 denominator = 10000000;
334 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
335
336 memset(parm, 0, sizeof *parm);
337 parm->type = stream->type;
338
339 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
340 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
341 parm->parm.capture.capturemode = 0;
342 parm->parm.capture.timeperframe.numerator = numerator;
343 parm->parm.capture.timeperframe.denominator = denominator;
344 parm->parm.capture.extendedmode = 0;
345 parm->parm.capture.readbuffers = 0;
346 } else {
347 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
348 parm->parm.output.outputmode = 0;
349 parm->parm.output.timeperframe.numerator = numerator;
350 parm->parm.output.timeperframe.denominator = denominator;
351 }
352
353 return 0;
354}
355
356static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
357 struct v4l2_streamparm *parm)
358{
359 struct uvc_streaming_control probe;
360 struct v4l2_fract timeperframe;
361 uint32_t interval;
362 int ret;
363
364 if (parm->type != stream->type)
365 return -EINVAL;
366
367 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
368 timeperframe = parm->parm.capture.timeperframe;
369 else
370 timeperframe = parm->parm.output.timeperframe;
371
372 interval = uvc_fraction_to_interval(timeperframe.numerator,
373 timeperframe.denominator);
374 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
375 timeperframe.numerator, timeperframe.denominator, interval);
376
377 mutex_lock(&stream->mutex);
378
379 if (uvc_queue_streaming(&stream->queue)) {
380 mutex_unlock(&stream->mutex);
381 return -EBUSY;
382 }
383
384 memcpy(&probe, &stream->ctrl, sizeof probe);
385 probe.dwFrameInterval =
386 uvc_try_frame_interval(stream->cur_frame, interval);
387
388 /* Probe the device with the new settings. */
389 ret = uvc_probe_video(stream, &probe);
390 if (ret < 0) {
391 mutex_unlock(&stream->mutex);
392 return ret;
393 }
394
395 memcpy(&stream->ctrl, &probe, sizeof probe);
396 mutex_unlock(&stream->mutex);
397
398 /* Return the actual frame period. */
399 timeperframe.numerator = probe.dwFrameInterval;
400 timeperframe.denominator = 10000000;
401 uvc_simplify_fraction(&timeperframe.numerator,
402 &timeperframe.denominator, 8, 333);
403
404 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
405 parm->parm.capture.timeperframe = timeperframe;
406 else
407 parm->parm.output.timeperframe = timeperframe;
408
409 return 0;
410}
411
412/* ------------------------------------------------------------------------
413 * Privilege management
414 */
415
416/*
417 * Privilege management is the multiple-open implementation basis. The current
418 * implementation is completely transparent for the end-user and doesn't
419 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
420 * Those ioctls enable finer control on the device (by making possible for a
421 * user to request exclusive access to a device), but are not mature yet.
422 * Switching to the V4L2 priority mechanism might be considered in the future
423 * if this situation changes.
424 *
425 * Each open instance of a UVC device can either be in a privileged or
426 * unprivileged state. Only a single instance can be in a privileged state at
427 * a given time. Trying to perform an operation that requires privileges will
428 * automatically acquire the required privileges if possible, or return -EBUSY
429 * otherwise. Privileges are dismissed when closing the instance or when
430 * freeing the video buffers using VIDIOC_REQBUFS.
431 *
432 * Operations that require privileges are:
433 *
434 * - VIDIOC_S_INPUT
435 * - VIDIOC_S_PARM
436 * - VIDIOC_S_FMT
437 * - VIDIOC_REQBUFS
438 */
439static int uvc_acquire_privileges(struct uvc_fh *handle)
440{
441 /* Always succeed if the handle is already privileged. */
442 if (handle->state == UVC_HANDLE_ACTIVE)
443 return 0;
444
445 /* Check if the device already has a privileged handle. */
446 if (atomic_inc_return(&handle->stream->active) != 1) {
447 atomic_dec(&handle->stream->active);
448 return -EBUSY;
449 }
450
451 handle->state = UVC_HANDLE_ACTIVE;
452 return 0;
453}
454
455static void uvc_dismiss_privileges(struct uvc_fh *handle)
456{
457 if (handle->state == UVC_HANDLE_ACTIVE)
458 atomic_dec(&handle->stream->active);
459
460 handle->state = UVC_HANDLE_PASSIVE;
461}
462
463static int uvc_has_privileges(struct uvc_fh *handle)
464{
465 return handle->state == UVC_HANDLE_ACTIVE;
466}
467
468/* ------------------------------------------------------------------------
469 * V4L2 file operations
470 */
471
472static int uvc_v4l2_open(struct file *file)
473{
474 struct uvc_streaming *stream;
475 struct uvc_fh *handle;
476 int ret = 0;
477
478 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
479 stream = video_drvdata(file);
480
481 if (stream->dev->state & UVC_DEV_DISCONNECTED)
482 return -ENODEV;
483
484 ret = usb_autopm_get_interface(stream->dev->intf);
485 if (ret < 0)
486 return ret;
487
488 /* Create the device handle. */
489 handle = kzalloc(sizeof *handle, GFP_KERNEL);
490 if (handle == NULL) {
491 usb_autopm_put_interface(stream->dev->intf);
492 return -ENOMEM;
493 }
494
495 if (atomic_inc_return(&stream->dev->users) == 1) {
496 ret = uvc_status_start(stream->dev);
497 if (ret < 0) {
498 usb_autopm_put_interface(stream->dev->intf);
499 atomic_dec(&stream->dev->users);
500 kfree(handle);
501 return ret;
502 }
503 }
504
505 handle->chain = stream->chain;
506 handle->stream = stream;
507 handle->state = UVC_HANDLE_PASSIVE;
508 file->private_data = handle;
509
510 return 0;
511}
512
513static int uvc_v4l2_release(struct file *file)
514{
515 struct uvc_fh *handle = file->private_data;
516 struct uvc_streaming *stream = handle->stream;
517
518 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
519
520 /* Only free resources if this is a privileged handle. */
521 if (uvc_has_privileges(handle)) {
522 uvc_video_enable(stream, 0);
523
524 if (uvc_free_buffers(&stream->queue) < 0)
525 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
526 "free buffers.\n");
527 }
528
529 /* Release the file handle. */
530 uvc_dismiss_privileges(handle);
531 kfree(handle);
532 file->private_data = NULL;
533
534 if (atomic_dec_return(&stream->dev->users) == 0)
535 uvc_status_stop(stream->dev);
536
537 usb_autopm_put_interface(stream->dev->intf);
538 return 0;
539}
540
541static void uvc_v4l2_ioctl_warn(void)
542{
543 static int warned;
544
545 if (warned)
546 return;
547
548 uvc_printk(KERN_INFO, "Deprecated UVCIOC_CTRL_{ADD,MAP_OLD,GET,SET} "
549 "ioctls will be removed in 2.6.42.\n");
550 uvc_printk(KERN_INFO, "See http://www.ideasonboard.org/uvc/upgrade/ "
551 "for upgrade instructions.\n");
552 warned = 1;
553}
554
555static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
556{
557 struct video_device *vdev = video_devdata(file);
558 struct uvc_fh *handle = file->private_data;
559 struct uvc_video_chain *chain = handle->chain;
560 struct uvc_streaming *stream = handle->stream;
561 long ret = 0;
562
563 switch (cmd) {
564 /* Query capabilities */
565 case VIDIOC_QUERYCAP:
566 {
567 struct v4l2_capability *cap = arg;
568
569 memset(cap, 0, sizeof *cap);
570 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
571 strlcpy(cap->card, vdev->name, sizeof cap->card);
572 usb_make_path(stream->dev->udev,
573 cap->bus_info, sizeof(cap->bus_info));
574 cap->version = LINUX_VERSION_CODE;
575 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
576 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
577 | V4L2_CAP_STREAMING;
578 else
579 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
580 | V4L2_CAP_STREAMING;
581 break;
582 }
583
584 /* Get, Set & Query control */
585 case VIDIOC_QUERYCTRL:
586 return uvc_query_v4l2_ctrl(chain, arg);
587
588 case VIDIOC_G_CTRL:
589 {
590 struct v4l2_control *ctrl = arg;
591 struct v4l2_ext_control xctrl;
592
593 memset(&xctrl, 0, sizeof xctrl);
594 xctrl.id = ctrl->id;
595
596 ret = uvc_ctrl_begin(chain);
597 if (ret < 0)
598 return ret;
599
600 ret = uvc_ctrl_get(chain, &xctrl);
601 uvc_ctrl_rollback(chain);
602 if (ret >= 0)
603 ctrl->value = xctrl.value;
604 break;
605 }
606
607 case VIDIOC_S_CTRL:
608 {
609 struct v4l2_control *ctrl = arg;
610 struct v4l2_ext_control xctrl;
611
612 memset(&xctrl, 0, sizeof xctrl);
613 xctrl.id = ctrl->id;
614 xctrl.value = ctrl->value;
615
616 ret = uvc_ctrl_begin(chain);
617 if (ret < 0)
618 return ret;
619
620 ret = uvc_ctrl_set(chain, &xctrl);
621 if (ret < 0) {
622 uvc_ctrl_rollback(chain);
623 return ret;
624 }
625 ret = uvc_ctrl_commit(chain);
626 if (ret == 0)
627 ctrl->value = xctrl.value;
628 break;
629 }
630
631 case VIDIOC_QUERYMENU:
632 return uvc_query_v4l2_menu(chain, arg);
633
634 case VIDIOC_G_EXT_CTRLS:
635 {
636 struct v4l2_ext_controls *ctrls = arg;
637 struct v4l2_ext_control *ctrl = ctrls->controls;
638 unsigned int i;
639
640 ret = uvc_ctrl_begin(chain);
641 if (ret < 0)
642 return ret;
643
644 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
645 ret = uvc_ctrl_get(chain, ctrl);
646 if (ret < 0) {
647 uvc_ctrl_rollback(chain);
648 ctrls->error_idx = i;
649 return ret;
650 }
651 }
652 ctrls->error_idx = 0;
653 ret = uvc_ctrl_rollback(chain);
654 break;
655 }
656
657 case VIDIOC_S_EXT_CTRLS:
658 case VIDIOC_TRY_EXT_CTRLS:
659 {
660 struct v4l2_ext_controls *ctrls = arg;
661 struct v4l2_ext_control *ctrl = ctrls->controls;
662 unsigned int i;
663
664 ret = uvc_ctrl_begin(chain);
665 if (ret < 0)
666 return ret;
667
668 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
669 ret = uvc_ctrl_set(chain, ctrl);
670 if (ret < 0) {
671 uvc_ctrl_rollback(chain);
672 ctrls->error_idx = i;
673 return ret;
674 }
675 }
676
677 ctrls->error_idx = 0;
678
679 if (cmd == VIDIOC_S_EXT_CTRLS)
680 ret = uvc_ctrl_commit(chain);
681 else
682 ret = uvc_ctrl_rollback(chain);
683 break;
684 }
685
686 /* Get, Set & Enum input */
687 case VIDIOC_ENUMINPUT:
688 {
689 const struct uvc_entity *selector = chain->selector;
690 struct v4l2_input *input = arg;
691 struct uvc_entity *iterm = NULL;
692 u32 index = input->index;
693 int pin = 0;
694
695 if (selector == NULL ||
696 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
697 if (index != 0)
698 return -EINVAL;
699 list_for_each_entry(iterm, &chain->entities, chain) {
700 if (UVC_ENTITY_IS_ITERM(iterm))
701 break;
702 }
703 pin = iterm->id;
704 } else if (pin < selector->bNrInPins) {
705 pin = selector->baSourceID[index];
706 list_for_each_entry(iterm, &chain->entities, chain) {
707 if (!UVC_ENTITY_IS_ITERM(iterm))
708 continue;
709 if (iterm->id == pin)
710 break;
711 }
712 }
713
714 if (iterm == NULL || iterm->id != pin)
715 return -EINVAL;
716
717 memset(input, 0, sizeof *input);
718 input->index = index;
719 strlcpy(input->name, iterm->name, sizeof input->name);
720 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
721 input->type = V4L2_INPUT_TYPE_CAMERA;
722 break;
723 }
724
725 case VIDIOC_G_INPUT:
726 {
727 u8 input;
728
729 if (chain->selector == NULL ||
730 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
731 *(int *)arg = 0;
732 break;
733 }
734
735 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
736 chain->selector->id, chain->dev->intfnum,
737 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
738 if (ret < 0)
739 return ret;
740
741 *(int *)arg = input - 1;
742 break;
743 }
744
745 case VIDIOC_S_INPUT:
746 {
747 u32 input = *(u32 *)arg + 1;
748
749 if ((ret = uvc_acquire_privileges(handle)) < 0)
750 return ret;
751
752 if (chain->selector == NULL ||
753 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
754 if (input != 1)
755 return -EINVAL;
756 break;
757 }
758
759 if (input == 0 || input > chain->selector->bNrInPins)
760 return -EINVAL;
761
762 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
763 chain->selector->id, chain->dev->intfnum,
764 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
765 }
766
767 /* Try, Get, Set & Enum format */
768 case VIDIOC_ENUM_FMT:
769 {
770 struct v4l2_fmtdesc *fmt = arg;
771 struct uvc_format *format;
772 enum v4l2_buf_type type = fmt->type;
773 __u32 index = fmt->index;
774
775 if (fmt->type != stream->type ||
776 fmt->index >= stream->nformats)
777 return -EINVAL;
778
779 memset(fmt, 0, sizeof(*fmt));
780 fmt->index = index;
781 fmt->type = type;
782
783 format = &stream->format[fmt->index];
784 fmt->flags = 0;
785 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
786 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
787 strlcpy(fmt->description, format->name,
788 sizeof fmt->description);
789 fmt->description[sizeof fmt->description - 1] = 0;
790 fmt->pixelformat = format->fcc;
791 break;
792 }
793
794 case VIDIOC_TRY_FMT:
795 {
796 struct uvc_streaming_control probe;
797
798 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
799 }
800
801 case VIDIOC_S_FMT:
802 if ((ret = uvc_acquire_privileges(handle)) < 0)
803 return ret;
804
805 return uvc_v4l2_set_format(stream, arg);
806
807 case VIDIOC_G_FMT:
808 return uvc_v4l2_get_format(stream, arg);
809
810 /* Frame size enumeration */
811 case VIDIOC_ENUM_FRAMESIZES:
812 {
813 struct v4l2_frmsizeenum *fsize = arg;
814 struct uvc_format *format = NULL;
815 struct uvc_frame *frame;
816 int i;
817
818 /* Look for the given pixel format */
819 for (i = 0; i < stream->nformats; i++) {
820 if (stream->format[i].fcc ==
821 fsize->pixel_format) {
822 format = &stream->format[i];
823 break;
824 }
825 }
826 if (format == NULL)
827 return -EINVAL;
828
829 if (fsize->index >= format->nframes)
830 return -EINVAL;
831
832 frame = &format->frame[fsize->index];
833 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
834 fsize->discrete.width = frame->wWidth;
835 fsize->discrete.height = frame->wHeight;
836 break;
837 }
838
839 /* Frame interval enumeration */
840 case VIDIOC_ENUM_FRAMEINTERVALS:
841 {
842 struct v4l2_frmivalenum *fival = arg;
843 struct uvc_format *format = NULL;
844 struct uvc_frame *frame = NULL;
845 int i;
846
847 /* Look for the given pixel format and frame size */
848 for (i = 0; i < stream->nformats; i++) {
849 if (stream->format[i].fcc ==
850 fival->pixel_format) {
851 format = &stream->format[i];
852 break;
853 }
854 }
855 if (format == NULL)
856 return -EINVAL;
857
858 for (i = 0; i < format->nframes; i++) {
859 if (format->frame[i].wWidth == fival->width &&
860 format->frame[i].wHeight == fival->height) {
861 frame = &format->frame[i];
862 break;
863 }
864 }
865 if (frame == NULL)
866 return -EINVAL;
867
868 if (frame->bFrameIntervalType) {
869 if (fival->index >= frame->bFrameIntervalType)
870 return -EINVAL;
871
872 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
873 fival->discrete.numerator =
874 frame->dwFrameInterval[fival->index];
875 fival->discrete.denominator = 10000000;
876 uvc_simplify_fraction(&fival->discrete.numerator,
877 &fival->discrete.denominator, 8, 333);
878 } else {
879 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
880 fival->stepwise.min.numerator =
881 frame->dwFrameInterval[0];
882 fival->stepwise.min.denominator = 10000000;
883 fival->stepwise.max.numerator =
884 frame->dwFrameInterval[1];
885 fival->stepwise.max.denominator = 10000000;
886 fival->stepwise.step.numerator =
887 frame->dwFrameInterval[2];
888 fival->stepwise.step.denominator = 10000000;
889 uvc_simplify_fraction(&fival->stepwise.min.numerator,
890 &fival->stepwise.min.denominator, 8, 333);
891 uvc_simplify_fraction(&fival->stepwise.max.numerator,
892 &fival->stepwise.max.denominator, 8, 333);
893 uvc_simplify_fraction(&fival->stepwise.step.numerator,
894 &fival->stepwise.step.denominator, 8, 333);
895 }
896 break;
897 }
898
899 /* Get & Set streaming parameters */
900 case VIDIOC_G_PARM:
901 return uvc_v4l2_get_streamparm(stream, arg);
902
903 case VIDIOC_S_PARM:
904 if ((ret = uvc_acquire_privileges(handle)) < 0)
905 return ret;
906
907 return uvc_v4l2_set_streamparm(stream, arg);
908
909 /* Cropping and scaling */
910 case VIDIOC_CROPCAP:
911 {
912 struct v4l2_cropcap *ccap = arg;
913
914 if (ccap->type != stream->type)
915 return -EINVAL;
916
917 ccap->bounds.left = 0;
918 ccap->bounds.top = 0;
919
920 mutex_lock(&stream->mutex);
921 ccap->bounds.width = stream->cur_frame->wWidth;
922 ccap->bounds.height = stream->cur_frame->wHeight;
923 mutex_unlock(&stream->mutex);
924
925 ccap->defrect = ccap->bounds;
926
927 ccap->pixelaspect.numerator = 1;
928 ccap->pixelaspect.denominator = 1;
929 break;
930 }
931
932 case VIDIOC_G_CROP:
933 case VIDIOC_S_CROP:
934 return -EINVAL;
935
936 /* Buffers & streaming */
937 case VIDIOC_REQBUFS:
938 {
939 struct v4l2_requestbuffers *rb = arg;
940
941 if (rb->type != stream->type ||
942 rb->memory != V4L2_MEMORY_MMAP)
943 return -EINVAL;
944
945 if ((ret = uvc_acquire_privileges(handle)) < 0)
946 return ret;
947
948 mutex_lock(&stream->mutex);
949 ret = uvc_alloc_buffers(&stream->queue, rb->count,
950 stream->ctrl.dwMaxVideoFrameSize);
951 mutex_unlock(&stream->mutex);
952 if (ret < 0)
953 return ret;
954
955 if (ret == 0)
956 uvc_dismiss_privileges(handle);
957
958 rb->count = ret;
959 ret = 0;
960 break;
961 }
962
963 case VIDIOC_QUERYBUF:
964 {
965 struct v4l2_buffer *buf = arg;
966
967 if (buf->type != stream->type)
968 return -EINVAL;
969
970 if (!uvc_has_privileges(handle))
971 return -EBUSY;
972
973 return uvc_query_buffer(&stream->queue, buf);
974 }
975
976 case VIDIOC_QBUF:
977 if (!uvc_has_privileges(handle))
978 return -EBUSY;
979
980 return uvc_queue_buffer(&stream->queue, arg);
981
982 case VIDIOC_DQBUF:
983 if (!uvc_has_privileges(handle))
984 return -EBUSY;
985
986 return uvc_dequeue_buffer(&stream->queue, arg,
987 file->f_flags & O_NONBLOCK);
988
989 case VIDIOC_STREAMON:
990 {
991 int *type = arg;
992
993 if (*type != stream->type)
994 return -EINVAL;
995
996 if (!uvc_has_privileges(handle))
997 return -EBUSY;
998
999 mutex_lock(&stream->mutex);
1000 ret = uvc_video_enable(stream, 1);
1001 mutex_unlock(&stream->mutex);
1002 if (ret < 0)
1003 return ret;
1004 break;
1005 }
1006
1007 case VIDIOC_STREAMOFF:
1008 {
1009 int *type = arg;
1010
1011 if (*type != stream->type)
1012 return -EINVAL;
1013
1014 if (!uvc_has_privileges(handle))
1015 return -EBUSY;
1016
1017 return uvc_video_enable(stream, 0);
1018 }
1019
1020 /* Analog video standards make no sense for digital cameras. */
1021 case VIDIOC_ENUMSTD:
1022 case VIDIOC_QUERYSTD:
1023 case VIDIOC_G_STD:
1024 case VIDIOC_S_STD:
1025
1026 case VIDIOC_OVERLAY:
1027
1028 case VIDIOC_ENUMAUDIO:
1029 case VIDIOC_ENUMAUDOUT:
1030
1031 case VIDIOC_ENUMOUTPUT:
1032 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1033 return -EINVAL;
1034
1035 /* Dynamic controls. UVCIOC_CTRL_ADD, UVCIOC_CTRL_MAP_OLD,
1036 * UVCIOC_CTRL_GET and UVCIOC_CTRL_SET are deprecated and scheduled for
1037 * removal in 2.6.42.
1038 */
1039 case __UVCIOC_CTRL_ADD:
1040 uvc_v4l2_ioctl_warn();
1041 return -EEXIST;
1042
1043 case __UVCIOC_CTRL_MAP_OLD:
1044 uvc_v4l2_ioctl_warn();
1045 case __UVCIOC_CTRL_MAP:
1046 case UVCIOC_CTRL_MAP:
1047 return uvc_ioctl_ctrl_map(chain, arg,
1048 cmd == __UVCIOC_CTRL_MAP_OLD);
1049
1050 case __UVCIOC_CTRL_GET:
1051 case __UVCIOC_CTRL_SET:
1052 {
1053 struct uvc_xu_control *xctrl = arg;
1054 struct uvc_xu_control_query xqry = {
1055 .unit = xctrl->unit,
1056 .selector = xctrl->selector,
1057 .query = cmd == __UVCIOC_CTRL_GET
1058 ? UVC_GET_CUR : UVC_SET_CUR,
1059 .size = xctrl->size,
1060 .data = xctrl->data,
1061 };
1062
1063 uvc_v4l2_ioctl_warn();
1064 return uvc_xu_ctrl_query(chain, &xqry);
1065 }
1066
1067 case UVCIOC_CTRL_QUERY:
1068 return uvc_xu_ctrl_query(chain, arg);
1069
1070 default:
1071 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1072 return -EINVAL;
1073 }
1074
1075 return ret;
1076}
1077
1078static long uvc_v4l2_ioctl(struct file *file,
1079 unsigned int cmd, unsigned long arg)
1080{
1081 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1082 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1083 v4l_printk_ioctl(cmd);
1084 printk(")\n");
1085 }
1086
1087 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1088}
1089
1090static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1091 size_t count, loff_t *ppos)
1092{
1093 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1094 return -EINVAL;
1095}
1096
1097static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1098{
1099 struct uvc_fh *handle = file->private_data;
1100 struct uvc_streaming *stream = handle->stream;
1101
1102 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1103
1104 return uvc_queue_mmap(&stream->queue, vma);
1105}
1106
1107static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1108{
1109 struct uvc_fh *handle = file->private_data;
1110 struct uvc_streaming *stream = handle->stream;
1111
1112 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1113
1114 return uvc_queue_poll(&stream->queue, file, wait);
1115}
1116
1117#ifndef CONFIG_MMU
1118static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1119 unsigned long addr, unsigned long len, unsigned long pgoff,
1120 unsigned long flags)
1121{
1122 struct uvc_fh *handle = file->private_data;
1123 struct uvc_streaming *stream = handle->stream;
1124
1125 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1126
1127 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1128}
1129#endif
1130
1131const struct v4l2_file_operations uvc_fops = {
1132 .owner = THIS_MODULE,
1133 .open = uvc_v4l2_open,
1134 .release = uvc_v4l2_release,
1135 .unlocked_ioctl = uvc_v4l2_ioctl,
1136 .read = uvc_v4l2_read,
1137 .mmap = uvc_v4l2_mmap,
1138 .poll = uvc_v4l2_poll,
1139#ifndef CONFIG_MMU
1140 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1141#endif
1142};
1143
diff --git a/drivers/media/video/uvc/uvc_video.c b/drivers/media/video/uvc/uvc_video.c
new file mode 100644
index 00000000000..ffd1158628b
--- /dev/null
+++ b/drivers/media/video/uvc/uvc_video.c
@@ -0,0 +1,1273 @@
1/*
2 * uvc_video.c -- USB Video Class driver - Video handling
3 *
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/wait.h>
22#include <linux/atomic.h>
23#include <asm/unaligned.h>
24
25#include <media/v4l2-common.h>
26
27#include "uvcvideo.h"
28
29/* ------------------------------------------------------------------------
30 * UVC Controls
31 */
32
33static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
34 __u8 intfnum, __u8 cs, void *data, __u16 size,
35 int timeout)
36{
37 __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
38 unsigned int pipe;
39
40 pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
41 : usb_sndctrlpipe(dev->udev, 0);
42 type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
43
44 return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
45 unit << 8 | intfnum, data, size, timeout);
46}
47
48static const char *uvc_query_name(__u8 query)
49{
50 switch (query) {
51 case UVC_SET_CUR:
52 return "SET_CUR";
53 case UVC_GET_CUR:
54 return "GET_CUR";
55 case UVC_GET_MIN:
56 return "GET_MIN";
57 case UVC_GET_MAX:
58 return "GET_MAX";
59 case UVC_GET_RES:
60 return "GET_RES";
61 case UVC_GET_LEN:
62 return "GET_LEN";
63 case UVC_GET_INFO:
64 return "GET_INFO";
65 case UVC_GET_DEF:
66 return "GET_DEF";
67 default:
68 return "<invalid>";
69 }
70}
71
72int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
73 __u8 intfnum, __u8 cs, void *data, __u16 size)
74{
75 int ret;
76
77 ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
78 UVC_CTRL_CONTROL_TIMEOUT);
79 if (ret != size) {
80 uvc_printk(KERN_ERR, "Failed to query (%s) UVC control %u on "
81 "unit %u: %d (exp. %u).\n", uvc_query_name(query), cs,
82 unit, ret, size);
83 return -EIO;
84 }
85
86 return 0;
87}
88
89static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
90 struct uvc_streaming_control *ctrl)
91{
92 struct uvc_format *format = NULL;
93 struct uvc_frame *frame = NULL;
94 unsigned int i;
95
96 for (i = 0; i < stream->nformats; ++i) {
97 if (stream->format[i].index == ctrl->bFormatIndex) {
98 format = &stream->format[i];
99 break;
100 }
101 }
102
103 if (format == NULL)
104 return;
105
106 for (i = 0; i < format->nframes; ++i) {
107 if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
108 frame = &format->frame[i];
109 break;
110 }
111 }
112
113 if (frame == NULL)
114 return;
115
116 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
117 (ctrl->dwMaxVideoFrameSize == 0 &&
118 stream->dev->uvc_version < 0x0110))
119 ctrl->dwMaxVideoFrameSize =
120 frame->dwMaxVideoFrameBufferSize;
121
122 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
123 stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
124 stream->intf->num_altsetting > 1) {
125 u32 interval;
126 u32 bandwidth;
127
128 interval = (ctrl->dwFrameInterval > 100000)
129 ? ctrl->dwFrameInterval
130 : frame->dwFrameInterval[0];
131
132 /* Compute a bandwidth estimation by multiplying the frame
133 * size by the number of video frames per second, divide the
134 * result by the number of USB frames (or micro-frames for
135 * high-speed devices) per second and add the UVC header size
136 * (assumed to be 12 bytes long).
137 */
138 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
139 bandwidth *= 10000000 / interval + 1;
140 bandwidth /= 1000;
141 if (stream->dev->udev->speed == USB_SPEED_HIGH)
142 bandwidth /= 8;
143 bandwidth += 12;
144
145 /* The bandwidth estimate is too low for many cameras. Don't use
146 * maximum packet sizes lower than 1024 bytes to try and work
147 * around the problem. According to measurements done on two
148 * different camera models, the value is high enough to get most
149 * resolutions working while not preventing two simultaneous
150 * VGA streams at 15 fps.
151 */
152 bandwidth = max_t(u32, bandwidth, 1024);
153
154 ctrl->dwMaxPayloadTransferSize = bandwidth;
155 }
156}
157
158static int uvc_get_video_ctrl(struct uvc_streaming *stream,
159 struct uvc_streaming_control *ctrl, int probe, __u8 query)
160{
161 __u8 *data;
162 __u16 size;
163 int ret;
164
165 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
166 if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
167 query == UVC_GET_DEF)
168 return -EIO;
169
170 data = kmalloc(size, GFP_KERNEL);
171 if (data == NULL)
172 return -ENOMEM;
173
174 ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
175 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
176 size, uvc_timeout_param);
177
178 if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
179 /* Some cameras, mostly based on Bison Electronics chipsets,
180 * answer a GET_MIN or GET_MAX request with the wCompQuality
181 * field only.
182 */
183 uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
184 "compliance - GET_MIN/MAX(PROBE) incorrectly "
185 "supported. Enabling workaround.\n");
186 memset(ctrl, 0, sizeof *ctrl);
187 ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
188 ret = 0;
189 goto out;
190 } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
191 /* Many cameras don't support the GET_DEF request on their
192 * video probe control. Warn once and return, the caller will
193 * fall back to GET_CUR.
194 */
195 uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
196 "compliance - GET_DEF(PROBE) not supported. "
197 "Enabling workaround.\n");
198 ret = -EIO;
199 goto out;
200 } else if (ret != size) {
201 uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
202 "%d (exp. %u).\n", query, probe ? "probe" : "commit",
203 ret, size);
204 ret = -EIO;
205 goto out;
206 }
207
208 ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
209 ctrl->bFormatIndex = data[2];
210 ctrl->bFrameIndex = data[3];
211 ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
212 ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
213 ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
214 ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
215 ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
216 ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
217 ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
218 ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
219
220 if (size == 34) {
221 ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
222 ctrl->bmFramingInfo = data[30];
223 ctrl->bPreferedVersion = data[31];
224 ctrl->bMinVersion = data[32];
225 ctrl->bMaxVersion = data[33];
226 } else {
227 ctrl->dwClockFrequency = stream->dev->clock_frequency;
228 ctrl->bmFramingInfo = 0;
229 ctrl->bPreferedVersion = 0;
230 ctrl->bMinVersion = 0;
231 ctrl->bMaxVersion = 0;
232 }
233
234 /* Some broken devices return null or wrong dwMaxVideoFrameSize and
235 * dwMaxPayloadTransferSize fields. Try to get the value from the
236 * format and frame descriptors.
237 */
238 uvc_fixup_video_ctrl(stream, ctrl);
239 ret = 0;
240
241out:
242 kfree(data);
243 return ret;
244}
245
246static int uvc_set_video_ctrl(struct uvc_streaming *stream,
247 struct uvc_streaming_control *ctrl, int probe)
248{
249 __u8 *data;
250 __u16 size;
251 int ret;
252
253 size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
254 data = kzalloc(size, GFP_KERNEL);
255 if (data == NULL)
256 return -ENOMEM;
257
258 *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
259 data[2] = ctrl->bFormatIndex;
260 data[3] = ctrl->bFrameIndex;
261 *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
262 *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
263 *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
264 *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
265 *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
266 *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
267 put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
268 put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
269
270 if (size == 34) {
271 put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
272 data[30] = ctrl->bmFramingInfo;
273 data[31] = ctrl->bPreferedVersion;
274 data[32] = ctrl->bMinVersion;
275 data[33] = ctrl->bMaxVersion;
276 }
277
278 ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
279 probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
280 size, uvc_timeout_param);
281 if (ret != size) {
282 uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
283 "%d (exp. %u).\n", probe ? "probe" : "commit",
284 ret, size);
285 ret = -EIO;
286 }
287
288 kfree(data);
289 return ret;
290}
291
292int uvc_probe_video(struct uvc_streaming *stream,
293 struct uvc_streaming_control *probe)
294{
295 struct uvc_streaming_control probe_min, probe_max;
296 __u16 bandwidth;
297 unsigned int i;
298 int ret;
299
300 /* Perform probing. The device should adjust the requested values
301 * according to its capabilities. However, some devices, namely the
302 * first generation UVC Logitech webcams, don't implement the Video
303 * Probe control properly, and just return the needed bandwidth. For
304 * that reason, if the needed bandwidth exceeds the maximum available
305 * bandwidth, try to lower the quality.
306 */
307 ret = uvc_set_video_ctrl(stream, probe, 1);
308 if (ret < 0)
309 goto done;
310
311 /* Get the minimum and maximum values for compression settings. */
312 if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
313 ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
314 if (ret < 0)
315 goto done;
316 ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
317 if (ret < 0)
318 goto done;
319
320 probe->wCompQuality = probe_max.wCompQuality;
321 }
322
323 for (i = 0; i < 2; ++i) {
324 ret = uvc_set_video_ctrl(stream, probe, 1);
325 if (ret < 0)
326 goto done;
327 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
328 if (ret < 0)
329 goto done;
330
331 if (stream->intf->num_altsetting == 1)
332 break;
333
334 bandwidth = probe->dwMaxPayloadTransferSize;
335 if (bandwidth <= stream->maxpsize)
336 break;
337
338 if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
339 ret = -ENOSPC;
340 goto done;
341 }
342
343 /* TODO: negotiate compression parameters */
344 probe->wKeyFrameRate = probe_min.wKeyFrameRate;
345 probe->wPFrameRate = probe_min.wPFrameRate;
346 probe->wCompQuality = probe_max.wCompQuality;
347 probe->wCompWindowSize = probe_min.wCompWindowSize;
348 }
349
350done:
351 return ret;
352}
353
354int uvc_commit_video(struct uvc_streaming *stream,
355 struct uvc_streaming_control *probe)
356{
357 return uvc_set_video_ctrl(stream, probe, 0);
358}
359
360/* ------------------------------------------------------------------------
361 * Video codecs
362 */
363
364/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
365#define UVC_STREAM_EOH (1 << 7)
366#define UVC_STREAM_ERR (1 << 6)
367#define UVC_STREAM_STI (1 << 5)
368#define UVC_STREAM_RES (1 << 4)
369#define UVC_STREAM_SCR (1 << 3)
370#define UVC_STREAM_PTS (1 << 2)
371#define UVC_STREAM_EOF (1 << 1)
372#define UVC_STREAM_FID (1 << 0)
373
374/* Video payload decoding is handled by uvc_video_decode_start(),
375 * uvc_video_decode_data() and uvc_video_decode_end().
376 *
377 * uvc_video_decode_start is called with URB data at the start of a bulk or
378 * isochronous payload. It processes header data and returns the header size
379 * in bytes if successful. If an error occurs, it returns a negative error
380 * code. The following error codes have special meanings.
381 *
382 * - EAGAIN informs the caller that the current video buffer should be marked
383 * as done, and that the function should be called again with the same data
384 * and a new video buffer. This is used when end of frame conditions can be
385 * reliably detected at the beginning of the next frame only.
386 *
387 * If an error other than -EAGAIN is returned, the caller will drop the current
388 * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
389 * made until the next payload. -ENODATA can be used to drop the current
390 * payload if no other error code is appropriate.
391 *
392 * uvc_video_decode_data is called for every URB with URB data. It copies the
393 * data to the video buffer.
394 *
395 * uvc_video_decode_end is called with header data at the end of a bulk or
396 * isochronous payload. It performs any additional header data processing and
397 * returns 0 or a negative error code if an error occurred. As header data have
398 * already been processed by uvc_video_decode_start, this functions isn't
399 * required to perform sanity checks a second time.
400 *
401 * For isochronous transfers where a payload is always transferred in a single
402 * URB, the three functions will be called in a row.
403 *
404 * To let the decoder process header data and update its internal state even
405 * when no video buffer is available, uvc_video_decode_start must be prepared
406 * to be called with a NULL buf parameter. uvc_video_decode_data and
407 * uvc_video_decode_end will never be called with a NULL buffer.
408 */
409static int uvc_video_decode_start(struct uvc_streaming *stream,
410 struct uvc_buffer *buf, const __u8 *data, int len)
411{
412 __u8 fid;
413
414 /* Sanity checks:
415 * - packet must be at least 2 bytes long
416 * - bHeaderLength value must be at least 2 bytes (see above)
417 * - bHeaderLength value can't be larger than the packet size.
418 */
419 if (len < 2 || data[0] < 2 || data[0] > len)
420 return -EINVAL;
421
422 /* Skip payloads marked with the error bit ("error frames"). */
423 if (data[1] & UVC_STREAM_ERR) {
424 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
425 "set).\n");
426 return -ENODATA;
427 }
428
429 fid = data[1] & UVC_STREAM_FID;
430
431 /* Increase the sequence number regardless of any buffer states, so
432 * that discontinuous sequence numbers always indicate lost frames.
433 */
434 if (stream->last_fid != fid)
435 stream->sequence++;
436
437 /* Store the payload FID bit and return immediately when the buffer is
438 * NULL.
439 */
440 if (buf == NULL) {
441 stream->last_fid = fid;
442 return -ENODATA;
443 }
444
445 /* Synchronize to the input stream by waiting for the FID bit to be
446 * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
447 * stream->last_fid is initialized to -1, so the first isochronous
448 * frame will always be in sync.
449 *
450 * If the device doesn't toggle the FID bit, invert stream->last_fid
451 * when the EOF bit is set to force synchronisation on the next packet.
452 */
453 if (buf->state != UVC_BUF_STATE_ACTIVE) {
454 struct timespec ts;
455
456 if (fid == stream->last_fid) {
457 uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
458 "sync).\n");
459 if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
460 (data[1] & UVC_STREAM_EOF))
461 stream->last_fid ^= UVC_STREAM_FID;
462 return -ENODATA;
463 }
464
465 if (uvc_clock_param == CLOCK_MONOTONIC)
466 ktime_get_ts(&ts);
467 else
468 ktime_get_real_ts(&ts);
469
470 buf->buf.sequence = stream->sequence;
471 buf->buf.timestamp.tv_sec = ts.tv_sec;
472 buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
473
474 /* TODO: Handle PTS and SCR. */
475 buf->state = UVC_BUF_STATE_ACTIVE;
476 }
477
478 /* Mark the buffer as done if we're at the beginning of a new frame.
479 * End of frame detection is better implemented by checking the EOF
480 * bit (FID bit toggling is delayed by one frame compared to the EOF
481 * bit), but some devices don't set the bit at end of frame (and the
482 * last payload can be lost anyway). We thus must check if the FID has
483 * been toggled.
484 *
485 * stream->last_fid is initialized to -1, so the first isochronous
486 * frame will never trigger an end of frame detection.
487 *
488 * Empty buffers (bytesused == 0) don't trigger end of frame detection
489 * as it doesn't make sense to return an empty buffer. This also
490 * avoids detecting end of frame conditions at FID toggling if the
491 * previous payload had the EOF bit set.
492 */
493 if (fid != stream->last_fid && buf->buf.bytesused != 0) {
494 uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
495 "toggled).\n");
496 buf->state = UVC_BUF_STATE_READY;
497 return -EAGAIN;
498 }
499
500 stream->last_fid = fid;
501
502 return data[0];
503}
504
505static void uvc_video_decode_data(struct uvc_streaming *stream,
506 struct uvc_buffer *buf, const __u8 *data, int len)
507{
508 struct uvc_video_queue *queue = &stream->queue;
509 unsigned int maxlen, nbytes;
510 void *mem;
511
512 if (len <= 0)
513 return;
514
515 /* Copy the video data to the buffer. */
516 maxlen = buf->buf.length - buf->buf.bytesused;
517 mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
518 nbytes = min((unsigned int)len, maxlen);
519 memcpy(mem, data, nbytes);
520 buf->buf.bytesused += nbytes;
521
522 /* Complete the current frame if the buffer size was exceeded. */
523 if (len > maxlen) {
524 uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
525 buf->state = UVC_BUF_STATE_READY;
526 }
527}
528
529static void uvc_video_decode_end(struct uvc_streaming *stream,
530 struct uvc_buffer *buf, const __u8 *data, int len)
531{
532 /* Mark the buffer as done if the EOF marker is set. */
533 if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
534 uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
535 if (data[0] == len)
536 uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
537 buf->state = UVC_BUF_STATE_READY;
538 if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
539 stream->last_fid ^= UVC_STREAM_FID;
540 }
541}
542
543/* Video payload encoding is handled by uvc_video_encode_header() and
544 * uvc_video_encode_data(). Only bulk transfers are currently supported.
545 *
546 * uvc_video_encode_header is called at the start of a payload. It adds header
547 * data to the transfer buffer and returns the header size. As the only known
548 * UVC output device transfers a whole frame in a single payload, the EOF bit
549 * is always set in the header.
550 *
551 * uvc_video_encode_data is called for every URB and copies the data from the
552 * video buffer to the transfer buffer.
553 */
554static int uvc_video_encode_header(struct uvc_streaming *stream,
555 struct uvc_buffer *buf, __u8 *data, int len)
556{
557 data[0] = 2; /* Header length */
558 data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
559 | (stream->last_fid & UVC_STREAM_FID);
560 return 2;
561}
562
563static int uvc_video_encode_data(struct uvc_streaming *stream,
564 struct uvc_buffer *buf, __u8 *data, int len)
565{
566 struct uvc_video_queue *queue = &stream->queue;
567 unsigned int nbytes;
568 void *mem;
569
570 /* Copy video data to the URB buffer. */
571 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
572 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
573 nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
574 nbytes);
575 memcpy(data, mem, nbytes);
576
577 queue->buf_used += nbytes;
578
579 return nbytes;
580}
581
582/* ------------------------------------------------------------------------
583 * URB handling
584 */
585
586/*
587 * Completion handler for video URBs.
588 */
589static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
590 struct uvc_buffer *buf)
591{
592 u8 *mem;
593 int ret, i;
594
595 for (i = 0; i < urb->number_of_packets; ++i) {
596 if (urb->iso_frame_desc[i].status < 0) {
597 uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
598 "lost (%d).\n", urb->iso_frame_desc[i].status);
599 /* Mark the buffer as faulty. */
600 if (buf != NULL)
601 buf->error = 1;
602 continue;
603 }
604
605 /* Decode the payload header. */
606 mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
607 do {
608 ret = uvc_video_decode_start(stream, buf, mem,
609 urb->iso_frame_desc[i].actual_length);
610 if (ret == -EAGAIN)
611 buf = uvc_queue_next_buffer(&stream->queue,
612 buf);
613 } while (ret == -EAGAIN);
614
615 if (ret < 0)
616 continue;
617
618 /* Decode the payload data. */
619 uvc_video_decode_data(stream, buf, mem + ret,
620 urb->iso_frame_desc[i].actual_length - ret);
621
622 /* Process the header again. */
623 uvc_video_decode_end(stream, buf, mem,
624 urb->iso_frame_desc[i].actual_length);
625
626 if (buf->state == UVC_BUF_STATE_READY) {
627 if (buf->buf.length != buf->buf.bytesused &&
628 !(stream->cur_format->flags &
629 UVC_FMT_FLAG_COMPRESSED))
630 buf->error = 1;
631
632 buf = uvc_queue_next_buffer(&stream->queue, buf);
633 }
634 }
635}
636
637static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
638 struct uvc_buffer *buf)
639{
640 u8 *mem;
641 int len, ret;
642
643 if (urb->actual_length == 0)
644 return;
645
646 mem = urb->transfer_buffer;
647 len = urb->actual_length;
648 stream->bulk.payload_size += len;
649
650 /* If the URB is the first of its payload, decode and save the
651 * header.
652 */
653 if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
654 do {
655 ret = uvc_video_decode_start(stream, buf, mem, len);
656 if (ret == -EAGAIN)
657 buf = uvc_queue_next_buffer(&stream->queue,
658 buf);
659 } while (ret == -EAGAIN);
660
661 /* If an error occurred skip the rest of the payload. */
662 if (ret < 0 || buf == NULL) {
663 stream->bulk.skip_payload = 1;
664 } else {
665 memcpy(stream->bulk.header, mem, ret);
666 stream->bulk.header_size = ret;
667
668 mem += ret;
669 len -= ret;
670 }
671 }
672
673 /* The buffer queue might have been cancelled while a bulk transfer
674 * was in progress, so we can reach here with buf equal to NULL. Make
675 * sure buf is never dereferenced if NULL.
676 */
677
678 /* Process video data. */
679 if (!stream->bulk.skip_payload && buf != NULL)
680 uvc_video_decode_data(stream, buf, mem, len);
681
682 /* Detect the payload end by a URB smaller than the maximum size (or
683 * a payload size equal to the maximum) and process the header again.
684 */
685 if (urb->actual_length < urb->transfer_buffer_length ||
686 stream->bulk.payload_size >= stream->bulk.max_payload_size) {
687 if (!stream->bulk.skip_payload && buf != NULL) {
688 uvc_video_decode_end(stream, buf, stream->bulk.header,
689 stream->bulk.payload_size);
690 if (buf->state == UVC_BUF_STATE_READY)
691 buf = uvc_queue_next_buffer(&stream->queue,
692 buf);
693 }
694
695 stream->bulk.header_size = 0;
696 stream->bulk.skip_payload = 0;
697 stream->bulk.payload_size = 0;
698 }
699}
700
701static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
702 struct uvc_buffer *buf)
703{
704 u8 *mem = urb->transfer_buffer;
705 int len = stream->urb_size, ret;
706
707 if (buf == NULL) {
708 urb->transfer_buffer_length = 0;
709 return;
710 }
711
712 /* If the URB is the first of its payload, add the header. */
713 if (stream->bulk.header_size == 0) {
714 ret = uvc_video_encode_header(stream, buf, mem, len);
715 stream->bulk.header_size = ret;
716 stream->bulk.payload_size += ret;
717 mem += ret;
718 len -= ret;
719 }
720
721 /* Process video data. */
722 ret = uvc_video_encode_data(stream, buf, mem, len);
723
724 stream->bulk.payload_size += ret;
725 len -= ret;
726
727 if (buf->buf.bytesused == stream->queue.buf_used ||
728 stream->bulk.payload_size == stream->bulk.max_payload_size) {
729 if (buf->buf.bytesused == stream->queue.buf_used) {
730 stream->queue.buf_used = 0;
731 buf->state = UVC_BUF_STATE_READY;
732 buf->buf.sequence = ++stream->sequence;
733 uvc_queue_next_buffer(&stream->queue, buf);
734 stream->last_fid ^= UVC_STREAM_FID;
735 }
736
737 stream->bulk.header_size = 0;
738 stream->bulk.payload_size = 0;
739 }
740
741 urb->transfer_buffer_length = stream->urb_size - len;
742}
743
744static void uvc_video_complete(struct urb *urb)
745{
746 struct uvc_streaming *stream = urb->context;
747 struct uvc_video_queue *queue = &stream->queue;
748 struct uvc_buffer *buf = NULL;
749 unsigned long flags;
750 int ret;
751
752 switch (urb->status) {
753 case 0:
754 break;
755
756 default:
757 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
758 "completion handler.\n", urb->status);
759
760 case -ENOENT: /* usb_kill_urb() called. */
761 if (stream->frozen)
762 return;
763
764 case -ECONNRESET: /* usb_unlink_urb() called. */
765 case -ESHUTDOWN: /* The endpoint is being disabled. */
766 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
767 return;
768 }
769
770 spin_lock_irqsave(&queue->irqlock, flags);
771 if (!list_empty(&queue->irqqueue))
772 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
773 queue);
774 spin_unlock_irqrestore(&queue->irqlock, flags);
775
776 stream->decode(urb, stream, buf);
777
778 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
779 uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
780 ret);
781 }
782}
783
784/*
785 * Free transfer buffers.
786 */
787static void uvc_free_urb_buffers(struct uvc_streaming *stream)
788{
789 unsigned int i;
790
791 for (i = 0; i < UVC_URBS; ++i) {
792 if (stream->urb_buffer[i]) {
793 usb_free_coherent(stream->dev->udev, stream->urb_size,
794 stream->urb_buffer[i], stream->urb_dma[i]);
795 stream->urb_buffer[i] = NULL;
796 }
797 }
798
799 stream->urb_size = 0;
800}
801
802/*
803 * Allocate transfer buffers. This function can be called with buffers
804 * already allocated when resuming from suspend, in which case it will
805 * return without touching the buffers.
806 *
807 * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
808 * system is too low on memory try successively smaller numbers of packets
809 * until allocation succeeds.
810 *
811 * Return the number of allocated packets on success or 0 when out of memory.
812 */
813static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
814 unsigned int size, unsigned int psize, gfp_t gfp_flags)
815{
816 unsigned int npackets;
817 unsigned int i;
818
819 /* Buffers are already allocated, bail out. */
820 if (stream->urb_size)
821 return stream->urb_size / psize;
822
823 /* Compute the number of packets. Bulk endpoints might transfer UVC
824 * payloads across multiple URBs.
825 */
826 npackets = DIV_ROUND_UP(size, psize);
827 if (npackets > UVC_MAX_PACKETS)
828 npackets = UVC_MAX_PACKETS;
829
830 /* Retry allocations until one succeed. */
831 for (; npackets > 1; npackets /= 2) {
832 for (i = 0; i < UVC_URBS; ++i) {
833 stream->urb_size = psize * npackets;
834 stream->urb_buffer[i] = usb_alloc_coherent(
835 stream->dev->udev, stream->urb_size,
836 gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
837 if (!stream->urb_buffer[i]) {
838 uvc_free_urb_buffers(stream);
839 break;
840 }
841 }
842
843 if (i == UVC_URBS) {
844 uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
845 "of %ux%u bytes each.\n", UVC_URBS, npackets,
846 psize);
847 return npackets;
848 }
849 }
850
851 uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
852 "per packet).\n", psize);
853 return 0;
854}
855
856/*
857 * Uninitialize isochronous/bulk URBs and free transfer buffers.
858 */
859static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
860{
861 struct urb *urb;
862 unsigned int i;
863
864 for (i = 0; i < UVC_URBS; ++i) {
865 urb = stream->urb[i];
866 if (urb == NULL)
867 continue;
868
869 usb_kill_urb(urb);
870 usb_free_urb(urb);
871 stream->urb[i] = NULL;
872 }
873
874 if (free_buffers)
875 uvc_free_urb_buffers(stream);
876}
877
878/*
879 * Initialize isochronous URBs and allocate transfer buffers. The packet size
880 * is given by the endpoint.
881 */
882static int uvc_init_video_isoc(struct uvc_streaming *stream,
883 struct usb_host_endpoint *ep, gfp_t gfp_flags)
884{
885 struct urb *urb;
886 unsigned int npackets, i, j;
887 u16 psize;
888 u32 size;
889
890 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
891 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
892 size = stream->ctrl.dwMaxVideoFrameSize;
893
894 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
895 if (npackets == 0)
896 return -ENOMEM;
897
898 size = npackets * psize;
899
900 for (i = 0; i < UVC_URBS; ++i) {
901 urb = usb_alloc_urb(npackets, gfp_flags);
902 if (urb == NULL) {
903 uvc_uninit_video(stream, 1);
904 return -ENOMEM;
905 }
906
907 urb->dev = stream->dev->udev;
908 urb->context = stream;
909 urb->pipe = usb_rcvisocpipe(stream->dev->udev,
910 ep->desc.bEndpointAddress);
911 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
912 urb->interval = ep->desc.bInterval;
913 urb->transfer_buffer = stream->urb_buffer[i];
914 urb->transfer_dma = stream->urb_dma[i];
915 urb->complete = uvc_video_complete;
916 urb->number_of_packets = npackets;
917 urb->transfer_buffer_length = size;
918
919 for (j = 0; j < npackets; ++j) {
920 urb->iso_frame_desc[j].offset = j * psize;
921 urb->iso_frame_desc[j].length = psize;
922 }
923
924 stream->urb[i] = urb;
925 }
926
927 return 0;
928}
929
930/*
931 * Initialize bulk URBs and allocate transfer buffers. The packet size is
932 * given by the endpoint.
933 */
934static int uvc_init_video_bulk(struct uvc_streaming *stream,
935 struct usb_host_endpoint *ep, gfp_t gfp_flags)
936{
937 struct urb *urb;
938 unsigned int npackets, pipe, i;
939 u16 psize;
940 u32 size;
941
942 psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
943 size = stream->ctrl.dwMaxPayloadTransferSize;
944 stream->bulk.max_payload_size = size;
945
946 npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
947 if (npackets == 0)
948 return -ENOMEM;
949
950 size = npackets * psize;
951
952 if (usb_endpoint_dir_in(&ep->desc))
953 pipe = usb_rcvbulkpipe(stream->dev->udev,
954 ep->desc.bEndpointAddress);
955 else
956 pipe = usb_sndbulkpipe(stream->dev->udev,
957 ep->desc.bEndpointAddress);
958
959 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
960 size = 0;
961
962 for (i = 0; i < UVC_URBS; ++i) {
963 urb = usb_alloc_urb(0, gfp_flags);
964 if (urb == NULL) {
965 uvc_uninit_video(stream, 1);
966 return -ENOMEM;
967 }
968
969 usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
970 stream->urb_buffer[i], size, uvc_video_complete,
971 stream);
972 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
973 urb->transfer_dma = stream->urb_dma[i];
974
975 stream->urb[i] = urb;
976 }
977
978 return 0;
979}
980
981/*
982 * Initialize isochronous/bulk URBs and allocate transfer buffers.
983 */
984static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
985{
986 struct usb_interface *intf = stream->intf;
987 struct usb_host_endpoint *ep;
988 unsigned int i;
989 int ret;
990
991 stream->sequence = -1;
992 stream->last_fid = -1;
993 stream->bulk.header_size = 0;
994 stream->bulk.skip_payload = 0;
995 stream->bulk.payload_size = 0;
996
997 if (intf->num_altsetting > 1) {
998 struct usb_host_endpoint *best_ep = NULL;
999 unsigned int best_psize = 3 * 1024;
1000 unsigned int bandwidth;
1001 unsigned int uninitialized_var(altsetting);
1002 int intfnum = stream->intfnum;
1003
1004 /* Isochronous endpoint, select the alternate setting. */
1005 bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
1006
1007 if (bandwidth == 0) {
1008 uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1009 "bandwidth, defaulting to lowest.\n");
1010 bandwidth = 1;
1011 } else {
1012 uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1013 "B/frame bandwidth.\n", bandwidth);
1014 }
1015
1016 for (i = 0; i < intf->num_altsetting; ++i) {
1017 struct usb_host_interface *alts;
1018 unsigned int psize;
1019
1020 alts = &intf->altsetting[i];
1021 ep = uvc_find_endpoint(alts,
1022 stream->header.bEndpointAddress);
1023 if (ep == NULL)
1024 continue;
1025
1026 /* Check if the bandwidth is high enough. */
1027 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
1028 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
1029 if (psize >= bandwidth && psize <= best_psize) {
1030 altsetting = i;
1031 best_psize = psize;
1032 best_ep = ep;
1033 }
1034 }
1035
1036 if (best_ep == NULL) {
1037 uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1038 "for requested bandwidth.\n");
1039 return -EIO;
1040 }
1041
1042 uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1043 "(%u B/frame bandwidth).\n", altsetting, best_psize);
1044
1045 ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
1046 if (ret < 0)
1047 return ret;
1048
1049 ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
1050 } else {
1051 /* Bulk endpoint, proceed to URB initialization. */
1052 ep = uvc_find_endpoint(&intf->altsetting[0],
1053 stream->header.bEndpointAddress);
1054 if (ep == NULL)
1055 return -EIO;
1056
1057 ret = uvc_init_video_bulk(stream, ep, gfp_flags);
1058 }
1059
1060 if (ret < 0)
1061 return ret;
1062
1063 /* Submit the URBs. */
1064 for (i = 0; i < UVC_URBS; ++i) {
1065 ret = usb_submit_urb(stream->urb[i], gfp_flags);
1066 if (ret < 0) {
1067 uvc_printk(KERN_ERR, "Failed to submit URB %u "
1068 "(%d).\n", i, ret);
1069 uvc_uninit_video(stream, 1);
1070 return ret;
1071 }
1072 }
1073
1074 return 0;
1075}
1076
1077/* --------------------------------------------------------------------------
1078 * Suspend/resume
1079 */
1080
1081/*
1082 * Stop streaming without disabling the video queue.
1083 *
1084 * To let userspace applications resume without trouble, we must not touch the
1085 * video buffers in any way. We mark the device as frozen to make sure the URB
1086 * completion handler won't try to cancel the queue when we kill the URBs.
1087 */
1088int uvc_video_suspend(struct uvc_streaming *stream)
1089{
1090 if (!uvc_queue_streaming(&stream->queue))
1091 return 0;
1092
1093 stream->frozen = 1;
1094 uvc_uninit_video(stream, 0);
1095 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1096 return 0;
1097}
1098
1099/*
1100 * Reconfigure the video interface and restart streaming if it was enabled
1101 * before suspend.
1102 *
1103 * If an error occurs, disable the video queue. This will wake all pending
1104 * buffers, making sure userspace applications are notified of the problem
1105 * instead of waiting forever.
1106 */
1107int uvc_video_resume(struct uvc_streaming *stream, int reset)
1108{
1109 int ret;
1110
1111 /* If the bus has been reset on resume, set the alternate setting to 0.
1112 * This should be the default value, but some devices crash or otherwise
1113 * misbehave if they don't receive a SET_INTERFACE request before any
1114 * other video control request.
1115 */
1116 if (reset)
1117 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1118
1119 stream->frozen = 0;
1120
1121 ret = uvc_commit_video(stream, &stream->ctrl);
1122 if (ret < 0) {
1123 uvc_queue_enable(&stream->queue, 0);
1124 return ret;
1125 }
1126
1127 if (!uvc_queue_streaming(&stream->queue))
1128 return 0;
1129
1130 ret = uvc_init_video(stream, GFP_NOIO);
1131 if (ret < 0)
1132 uvc_queue_enable(&stream->queue, 0);
1133
1134 return ret;
1135}
1136
1137/* ------------------------------------------------------------------------
1138 * Video device
1139 */
1140
1141/*
1142 * Initialize the UVC video device by switching to alternate setting 0 and
1143 * retrieve the default format.
1144 *
1145 * Some cameras (namely the Fuji Finepix) set the format and frame
1146 * indexes to zero. The UVC standard doesn't clearly make this a spec
1147 * violation, so try to silently fix the values if possible.
1148 *
1149 * This function is called before registering the device with V4L.
1150 */
1151int uvc_video_init(struct uvc_streaming *stream)
1152{
1153 struct uvc_streaming_control *probe = &stream->ctrl;
1154 struct uvc_format *format = NULL;
1155 struct uvc_frame *frame = NULL;
1156 unsigned int i;
1157 int ret;
1158
1159 if (stream->nformats == 0) {
1160 uvc_printk(KERN_INFO, "No supported video formats found.\n");
1161 return -EINVAL;
1162 }
1163
1164 atomic_set(&stream->active, 0);
1165
1166 /* Initialize the video buffers queue. */
1167 uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
1168
1169 /* Alternate setting 0 should be the default, yet the XBox Live Vision
1170 * Cam (and possibly other devices) crash or otherwise misbehave if
1171 * they don't receive a SET_INTERFACE request before any other video
1172 * control request.
1173 */
1174 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1175
1176 /* Set the streaming probe control with default streaming parameters
1177 * retrieved from the device. Webcams that don't suport GET_DEF
1178 * requests on the probe control will just keep their current streaming
1179 * parameters.
1180 */
1181 if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
1182 uvc_set_video_ctrl(stream, probe, 1);
1183
1184 /* Initialize the streaming parameters with the probe control current
1185 * value. This makes sure SET_CUR requests on the streaming commit
1186 * control will always use values retrieved from a successful GET_CUR
1187 * request on the probe control, as required by the UVC specification.
1188 */
1189 ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
1190 if (ret < 0)
1191 return ret;
1192
1193 /* Check if the default format descriptor exists. Use the first
1194 * available format otherwise.
1195 */
1196 for (i = stream->nformats; i > 0; --i) {
1197 format = &stream->format[i-1];
1198 if (format->index == probe->bFormatIndex)
1199 break;
1200 }
1201
1202 if (format->nframes == 0) {
1203 uvc_printk(KERN_INFO, "No frame descriptor found for the "
1204 "default format.\n");
1205 return -EINVAL;
1206 }
1207
1208 /* Zero bFrameIndex might be correct. Stream-based formats (including
1209 * MPEG-2 TS and DV) do not support frames but have a dummy frame
1210 * descriptor with bFrameIndex set to zero. If the default frame
1211 * descriptor is not found, use the first available frame.
1212 */
1213 for (i = format->nframes; i > 0; --i) {
1214 frame = &format->frame[i-1];
1215 if (frame->bFrameIndex == probe->bFrameIndex)
1216 break;
1217 }
1218
1219 probe->bFormatIndex = format->index;
1220 probe->bFrameIndex = frame->bFrameIndex;
1221
1222 stream->cur_format = format;
1223 stream->cur_frame = frame;
1224
1225 /* Select the video decoding function */
1226 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1227 if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
1228 stream->decode = uvc_video_decode_isight;
1229 else if (stream->intf->num_altsetting > 1)
1230 stream->decode = uvc_video_decode_isoc;
1231 else
1232 stream->decode = uvc_video_decode_bulk;
1233 } else {
1234 if (stream->intf->num_altsetting == 1)
1235 stream->decode = uvc_video_encode_bulk;
1236 else {
1237 uvc_printk(KERN_INFO, "Isochronous endpoints are not "
1238 "supported for video output devices.\n");
1239 return -EINVAL;
1240 }
1241 }
1242
1243 return 0;
1244}
1245
1246/*
1247 * Enable or disable the video stream.
1248 */
1249int uvc_video_enable(struct uvc_streaming *stream, int enable)
1250{
1251 int ret;
1252
1253 if (!enable) {
1254 uvc_uninit_video(stream, 1);
1255 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1256 uvc_queue_enable(&stream->queue, 0);
1257 return 0;
1258 }
1259
1260 ret = uvc_queue_enable(&stream->queue, 1);
1261 if (ret < 0)
1262 return ret;
1263
1264 /* Commit the streaming parameters. */
1265 ret = uvc_commit_video(stream, &stream->ctrl);
1266 if (ret < 0) {
1267 uvc_queue_enable(&stream->queue, 0);
1268 return ret;
1269 }
1270
1271 return uvc_init_video(stream, GFP_KERNEL);
1272}
1273
diff --git a/drivers/media/video/uvc/uvcvideo.h b/drivers/media/video/uvc/uvcvideo.h
new file mode 100644
index 00000000000..cbdd49bf8b6
--- /dev/null
+++ b/drivers/media/video/uvc/uvcvideo.h
@@ -0,0 +1,703 @@
1#ifndef _USB_VIDEO_H_
2#define _USB_VIDEO_H_
3
4#include <linux/kernel.h>
5#include <linux/videodev2.h>
6
7#ifndef __KERNEL__
8/*
9 * This header provides binary compatibility with applications using the private
10 * uvcvideo API. This API is deprecated and will be removed in 2.6.42.
11 * Applications should be recompiled against the public linux/uvcvideo.h header.
12 */
13#warn "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
14
15/*
16 * Dynamic controls
17 */
18
19/* Data types for UVC control data */
20#define UVC_CTRL_DATA_TYPE_RAW 0
21#define UVC_CTRL_DATA_TYPE_SIGNED 1
22#define UVC_CTRL_DATA_TYPE_UNSIGNED 2
23#define UVC_CTRL_DATA_TYPE_BOOLEAN 3
24#define UVC_CTRL_DATA_TYPE_ENUM 4
25#define UVC_CTRL_DATA_TYPE_BITMASK 5
26
27/* Control flags */
28#define UVC_CONTROL_SET_CUR (1 << 0)
29#define UVC_CONTROL_GET_CUR (1 << 1)
30#define UVC_CONTROL_GET_MIN (1 << 2)
31#define UVC_CONTROL_GET_MAX (1 << 3)
32#define UVC_CONTROL_GET_RES (1 << 4)
33#define UVC_CONTROL_GET_DEF (1 << 5)
34#define UVC_CONTROL_RESTORE (1 << 6)
35#define UVC_CONTROL_AUTO_UPDATE (1 << 7)
36
37#define UVC_CONTROL_GET_RANGE (UVC_CONTROL_GET_CUR | UVC_CONTROL_GET_MIN | \
38 UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES | \
39 UVC_CONTROL_GET_DEF)
40
41struct uvc_menu_info {
42 __u32 value;
43 __u8 name[32];
44};
45
46struct uvc_xu_control_mapping {
47 __u32 id;
48 __u8 name[32];
49 __u8 entity[16];
50 __u8 selector;
51
52 __u8 size;
53 __u8 offset;
54 __u32 v4l2_type;
55 __u32 data_type;
56
57 struct uvc_menu_info __user *menu_info;
58 __u32 menu_count;
59
60 __u32 reserved[4];
61};
62
63#endif
64
65struct uvc_xu_control_info {
66 __u8 entity[16];
67 __u8 index;
68 __u8 selector;
69 __u16 size;
70 __u32 flags;
71};
72
73struct uvc_xu_control_mapping_old {
74 __u8 reserved[64];
75};
76
77struct uvc_xu_control {
78 __u8 unit;
79 __u8 selector;
80 __u16 size;
81 __u8 __user *data;
82};
83
84#ifndef __KERNEL__
85#define UVCIOC_CTRL_ADD _IOW('U', 1, struct uvc_xu_control_info)
86#define UVCIOC_CTRL_MAP_OLD _IOWR('U', 2, struct uvc_xu_control_mapping_old)
87#define UVCIOC_CTRL_MAP _IOWR('U', 2, struct uvc_xu_control_mapping)
88#define UVCIOC_CTRL_GET _IOWR('U', 3, struct uvc_xu_control)
89#define UVCIOC_CTRL_SET _IOW('U', 4, struct uvc_xu_control)
90#else
91#define __UVCIOC_CTRL_ADD _IOW('U', 1, struct uvc_xu_control_info)
92#define __UVCIOC_CTRL_MAP_OLD _IOWR('U', 2, struct uvc_xu_control_mapping_old)
93#define __UVCIOC_CTRL_MAP _IOWR('U', 2, struct uvc_xu_control_mapping)
94#define __UVCIOC_CTRL_GET _IOWR('U', 3, struct uvc_xu_control)
95#define __UVCIOC_CTRL_SET _IOW('U', 4, struct uvc_xu_control)
96#endif
97
98#ifdef __KERNEL__
99
100#include <linux/poll.h>
101#include <linux/usb.h>
102#include <linux/usb/video.h>
103#include <linux/uvcvideo.h>
104#include <media/media-device.h>
105#include <media/v4l2-device.h>
106
107/* --------------------------------------------------------------------------
108 * UVC constants
109 */
110
111#define UVC_TERM_INPUT 0x0000
112#define UVC_TERM_OUTPUT 0x8000
113#define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
114
115#define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
116#define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
117#define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
118#define UVC_ENTITY_IS_ITERM(entity) \
119 (UVC_ENTITY_IS_TERM(entity) && \
120 ((entity)->type & 0x8000) == UVC_TERM_INPUT)
121#define UVC_ENTITY_IS_OTERM(entity) \
122 (UVC_ENTITY_IS_TERM(entity) && \
123 ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
124
125
126/* ------------------------------------------------------------------------
127 * GUIDs
128 */
129#define UVC_GUID_UVC_CAMERA \
130 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
132#define UVC_GUID_UVC_OUTPUT \
133 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
135#define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
136 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
138#define UVC_GUID_UVC_PROCESSING \
139 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
141#define UVC_GUID_UVC_SELECTOR \
142 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
144
145#define UVC_GUID_FORMAT_MJPEG \
146 { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
147 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
148#define UVC_GUID_FORMAT_YUY2 \
149 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
150 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
151#define UVC_GUID_FORMAT_YUY2_ISIGHT \
152 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
153 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
154#define UVC_GUID_FORMAT_NV12 \
155 { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
156 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
157#define UVC_GUID_FORMAT_YV12 \
158 { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
159 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
160#define UVC_GUID_FORMAT_I420 \
161 { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
162 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
163#define UVC_GUID_FORMAT_UYVY \
164 { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
165 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
166#define UVC_GUID_FORMAT_Y800 \
167 { 'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, \
168 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
169#define UVC_GUID_FORMAT_Y16 \
170 { 'Y', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, \
171 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
172#define UVC_GUID_FORMAT_BY8 \
173 { 'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, \
174 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
175#define UVC_GUID_FORMAT_RGBP \
176 { 'R', 'G', 'B', 'P', 0x00, 0x00, 0x10, 0x00, \
177 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
178#define UVC_GUID_FORMAT_M420 \
179 { 'M', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
180 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
181
182/* ------------------------------------------------------------------------
183 * Driver specific constants.
184 */
185
186#define DRIVER_VERSION "1.1.1"
187
188/* Number of isochronous URBs. */
189#define UVC_URBS 5
190/* Maximum number of packets per URB. */
191#define UVC_MAX_PACKETS 32
192/* Maximum number of video buffers. */
193#define UVC_MAX_VIDEO_BUFFERS 32
194/* Maximum status buffer size in bytes of interrupt URB. */
195#define UVC_MAX_STATUS_SIZE 16
196
197#define UVC_CTRL_CONTROL_TIMEOUT 300
198#define UVC_CTRL_STREAMING_TIMEOUT 5000
199
200/* Maximum allowed number of control mappings per device */
201#define UVC_MAX_CONTROL_MAPPINGS 1024
202
203/* Devices quirks */
204#define UVC_QUIRK_STATUS_INTERVAL 0x00000001
205#define UVC_QUIRK_PROBE_MINMAX 0x00000002
206#define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
207#define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
208#define UVC_QUIRK_STREAM_NO_FID 0x00000010
209#define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
210#define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
211#define UVC_QUIRK_PROBE_DEF 0x00000100
212#define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
213
214/* Format flags */
215#define UVC_FMT_FLAG_COMPRESSED 0x00000001
216#define UVC_FMT_FLAG_STREAM 0x00000002
217
218/* ------------------------------------------------------------------------
219 * Structures.
220 */
221
222struct uvc_device;
223
224/* TODO: Put the most frequently accessed fields at the beginning of
225 * structures to maximize cache efficiency.
226 */
227struct uvc_control_info {
228 struct list_head mappings;
229
230 __u8 entity[16];
231 __u8 index; /* Bit index in bmControls */
232 __u8 selector;
233
234 __u16 size;
235 __u32 flags;
236};
237
238struct uvc_control_mapping {
239 struct list_head list;
240
241 struct uvc_control_info *ctrl;
242
243 __u32 id;
244 __u8 name[32];
245 __u8 entity[16];
246 __u8 selector;
247
248 __u8 size;
249 __u8 offset;
250 enum v4l2_ctrl_type v4l2_type;
251 __u32 data_type;
252
253 struct uvc_menu_info *menu_info;
254 __u32 menu_count;
255
256 __s32 (*get) (struct uvc_control_mapping *mapping, __u8 query,
257 const __u8 *data);
258 void (*set) (struct uvc_control_mapping *mapping, __s32 value,
259 __u8 *data);
260};
261
262struct uvc_control {
263 struct uvc_entity *entity;
264 struct uvc_control_info info;
265
266 __u8 index; /* Used to match the uvc_control entry with a
267 uvc_control_info. */
268 __u8 dirty:1,
269 loaded:1,
270 modified:1,
271 cached:1,
272 initialized:1;
273
274 __u8 *uvc_data;
275};
276
277struct uvc_format_desc {
278 char *name;
279 __u8 guid[16];
280 __u32 fcc;
281};
282
283/* The term 'entity' refers to both UVC units and UVC terminals.
284 *
285 * The type field is either the terminal type (wTerminalType in the terminal
286 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
287 * As the bDescriptorSubtype field is one byte long, the type value will
288 * always have a null MSB for units. All terminal types defined by the UVC
289 * specification have a non-null MSB, so it is safe to use the MSB to
290 * differentiate between units and terminals as long as the descriptor parsing
291 * code makes sure terminal types have a non-null MSB.
292 *
293 * For terminals, the type's most significant bit stores the terminal
294 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
295 * always be accessed with the UVC_ENTITY_* macros and never directly.
296 */
297
298struct uvc_entity {
299 struct list_head list; /* Entity as part of a UVC device. */
300 struct list_head chain; /* Entity as part of a video device
301 * chain. */
302 __u8 id;
303 __u16 type;
304 char name[64];
305
306 /* Media controller-related fields. */
307 struct video_device *vdev;
308 struct v4l2_subdev subdev;
309 unsigned int num_pads;
310 unsigned int num_links;
311 struct media_pad *pads;
312
313 union {
314 struct {
315 __u16 wObjectiveFocalLengthMin;
316 __u16 wObjectiveFocalLengthMax;
317 __u16 wOcularFocalLength;
318 __u8 bControlSize;
319 __u8 *bmControls;
320 } camera;
321
322 struct {
323 __u8 bControlSize;
324 __u8 *bmControls;
325 __u8 bTransportModeSize;
326 __u8 *bmTransportModes;
327 } media;
328
329 struct {
330 } output;
331
332 struct {
333 __u16 wMaxMultiplier;
334 __u8 bControlSize;
335 __u8 *bmControls;
336 __u8 bmVideoStandards;
337 } processing;
338
339 struct {
340 } selector;
341
342 struct {
343 __u8 guidExtensionCode[16];
344 __u8 bNumControls;
345 __u8 bControlSize;
346 __u8 *bmControls;
347 __u8 *bmControlsType;
348 } extension;
349 };
350
351 __u8 bNrInPins;
352 __u8 *baSourceID;
353
354 unsigned int ncontrols;
355 struct uvc_control *controls;
356};
357
358struct uvc_frame {
359 __u8 bFrameIndex;
360 __u8 bmCapabilities;
361 __u16 wWidth;
362 __u16 wHeight;
363 __u32 dwMinBitRate;
364 __u32 dwMaxBitRate;
365 __u32 dwMaxVideoFrameBufferSize;
366 __u8 bFrameIntervalType;
367 __u32 dwDefaultFrameInterval;
368 __u32 *dwFrameInterval;
369};
370
371struct uvc_format {
372 __u8 type;
373 __u8 index;
374 __u8 bpp;
375 __u8 colorspace;
376 __u32 fcc;
377 __u32 flags;
378
379 char name[32];
380
381 unsigned int nframes;
382 struct uvc_frame *frame;
383};
384
385struct uvc_streaming_header {
386 __u8 bNumFormats;
387 __u8 bEndpointAddress;
388 __u8 bTerminalLink;
389 __u8 bControlSize;
390 __u8 *bmaControls;
391 /* The following fields are used by input headers only. */
392 __u8 bmInfo;
393 __u8 bStillCaptureMethod;
394 __u8 bTriggerSupport;
395 __u8 bTriggerUsage;
396};
397
398enum uvc_buffer_state {
399 UVC_BUF_STATE_IDLE = 0,
400 UVC_BUF_STATE_QUEUED = 1,
401 UVC_BUF_STATE_ACTIVE = 2,
402 UVC_BUF_STATE_READY = 3,
403 UVC_BUF_STATE_DONE = 4,
404 UVC_BUF_STATE_ERROR = 5,
405};
406
407struct uvc_buffer {
408 unsigned long vma_use_count;
409 struct list_head stream;
410
411 /* Touched by interrupt handler. */
412 struct v4l2_buffer buf;
413 struct list_head queue;
414 wait_queue_head_t wait;
415 enum uvc_buffer_state state;
416 unsigned int error;
417};
418
419#define UVC_QUEUE_STREAMING (1 << 0)
420#define UVC_QUEUE_DISCONNECTED (1 << 1)
421#define UVC_QUEUE_DROP_CORRUPTED (1 << 2)
422
423struct uvc_video_queue {
424 enum v4l2_buf_type type;
425
426 void *mem;
427 unsigned int flags;
428
429 unsigned int count;
430 unsigned int buf_size;
431 unsigned int buf_used;
432 struct uvc_buffer buffer[UVC_MAX_VIDEO_BUFFERS];
433 struct mutex mutex; /* protects buffers and mainqueue */
434 spinlock_t irqlock; /* protects irqqueue */
435
436 struct list_head mainqueue;
437 struct list_head irqqueue;
438};
439
440struct uvc_video_chain {
441 struct uvc_device *dev;
442 struct list_head list;
443
444 struct list_head entities; /* All entities */
445 struct uvc_entity *processing; /* Processing unit */
446 struct uvc_entity *selector; /* Selector unit */
447
448 struct mutex ctrl_mutex; /* Protects ctrl.info */
449};
450
451struct uvc_streaming {
452 struct list_head list;
453 struct uvc_device *dev;
454 struct video_device *vdev;
455 struct uvc_video_chain *chain;
456 atomic_t active;
457
458 struct usb_interface *intf;
459 int intfnum;
460 __u16 maxpsize;
461
462 struct uvc_streaming_header header;
463 enum v4l2_buf_type type;
464
465 unsigned int nformats;
466 struct uvc_format *format;
467
468 struct uvc_streaming_control ctrl;
469 struct uvc_format *cur_format;
470 struct uvc_frame *cur_frame;
471 /* Protect access to ctrl, cur_format, cur_frame and hardware video
472 * probe control.
473 */
474 struct mutex mutex;
475
476 unsigned int frozen : 1;
477 struct uvc_video_queue queue;
478 void (*decode) (struct urb *urb, struct uvc_streaming *video,
479 struct uvc_buffer *buf);
480
481 /* Context data used by the bulk completion handler. */
482 struct {
483 __u8 header[256];
484 unsigned int header_size;
485 int skip_payload;
486 __u32 payload_size;
487 __u32 max_payload_size;
488 } bulk;
489
490 struct urb *urb[UVC_URBS];
491 char *urb_buffer[UVC_URBS];
492 dma_addr_t urb_dma[UVC_URBS];
493 unsigned int urb_size;
494
495 __u32 sequence;
496 __u8 last_fid;
497};
498
499enum uvc_device_state {
500 UVC_DEV_DISCONNECTED = 1,
501};
502
503struct uvc_device {
504 struct usb_device *udev;
505 struct usb_interface *intf;
506 unsigned long warnings;
507 __u32 quirks;
508 int intfnum;
509 char name[32];
510
511 enum uvc_device_state state;
512 atomic_t users;
513 atomic_t nmappings;
514
515 /* Video control interface */
516#ifdef CONFIG_MEDIA_CONTROLLER
517 struct media_device mdev;
518#endif
519 struct v4l2_device vdev;
520 __u16 uvc_version;
521 __u32 clock_frequency;
522
523 struct list_head entities;
524 struct list_head chains;
525
526 /* Video Streaming interfaces */
527 struct list_head streams;
528 atomic_t nstreams;
529
530 /* Status Interrupt Endpoint */
531 struct usb_host_endpoint *int_ep;
532 struct urb *int_urb;
533 __u8 *status;
534 struct input_dev *input;
535 char input_phys[64];
536};
537
538enum uvc_handle_state {
539 UVC_HANDLE_PASSIVE = 0,
540 UVC_HANDLE_ACTIVE = 1,
541};
542
543struct uvc_fh {
544 struct uvc_video_chain *chain;
545 struct uvc_streaming *stream;
546 enum uvc_handle_state state;
547};
548
549struct uvc_driver {
550 struct usb_driver driver;
551};
552
553/* ------------------------------------------------------------------------
554 * Debugging, printing and logging
555 */
556
557#define UVC_TRACE_PROBE (1 << 0)
558#define UVC_TRACE_DESCR (1 << 1)
559#define UVC_TRACE_CONTROL (1 << 2)
560#define UVC_TRACE_FORMAT (1 << 3)
561#define UVC_TRACE_CAPTURE (1 << 4)
562#define UVC_TRACE_CALLS (1 << 5)
563#define UVC_TRACE_IOCTL (1 << 6)
564#define UVC_TRACE_FRAME (1 << 7)
565#define UVC_TRACE_SUSPEND (1 << 8)
566#define UVC_TRACE_STATUS (1 << 9)
567#define UVC_TRACE_VIDEO (1 << 10)
568
569#define UVC_WARN_MINMAX 0
570#define UVC_WARN_PROBE_DEF 1
571
572extern unsigned int uvc_clock_param;
573extern unsigned int uvc_no_drop_param;
574extern unsigned int uvc_trace_param;
575extern unsigned int uvc_timeout_param;
576
577#define uvc_trace(flag, msg...) \
578 do { \
579 if (uvc_trace_param & flag) \
580 printk(KERN_DEBUG "uvcvideo: " msg); \
581 } while (0)
582
583#define uvc_warn_once(dev, warn, msg...) \
584 do { \
585 if (!test_and_set_bit(warn, &dev->warnings)) \
586 printk(KERN_INFO "uvcvideo: " msg); \
587 } while (0)
588
589#define uvc_printk(level, msg...) \
590 printk(level "uvcvideo: " msg)
591
592/* --------------------------------------------------------------------------
593 * Internal functions.
594 */
595
596/* Core driver */
597extern struct uvc_driver uvc_driver;
598
599extern struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
600
601/* Video buffers queue management. */
602extern void uvc_queue_init(struct uvc_video_queue *queue,
603 enum v4l2_buf_type type, int drop_corrupted);
604extern int uvc_alloc_buffers(struct uvc_video_queue *queue,
605 unsigned int nbuffers, unsigned int buflength);
606extern int uvc_free_buffers(struct uvc_video_queue *queue);
607extern int uvc_query_buffer(struct uvc_video_queue *queue,
608 struct v4l2_buffer *v4l2_buf);
609extern int uvc_queue_buffer(struct uvc_video_queue *queue,
610 struct v4l2_buffer *v4l2_buf);
611extern int uvc_dequeue_buffer(struct uvc_video_queue *queue,
612 struct v4l2_buffer *v4l2_buf, int nonblocking);
613extern int uvc_queue_enable(struct uvc_video_queue *queue, int enable);
614extern void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
615extern struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
616 struct uvc_buffer *buf);
617extern int uvc_queue_mmap(struct uvc_video_queue *queue,
618 struct vm_area_struct *vma);
619extern unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
620 struct file *file, poll_table *wait);
621#ifndef CONFIG_MMU
622extern unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
623 unsigned long pgoff);
624#endif
625extern int uvc_queue_allocated(struct uvc_video_queue *queue);
626static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
627{
628 return queue->flags & UVC_QUEUE_STREAMING;
629}
630
631/* V4L2 interface */
632extern const struct v4l2_file_operations uvc_fops;
633
634/* Media controller */
635extern int uvc_mc_register_entities(struct uvc_video_chain *chain);
636extern void uvc_mc_cleanup_entity(struct uvc_entity *entity);
637
638/* Video */
639extern int uvc_video_init(struct uvc_streaming *stream);
640extern int uvc_video_suspend(struct uvc_streaming *stream);
641extern int uvc_video_resume(struct uvc_streaming *stream, int reset);
642extern int uvc_video_enable(struct uvc_streaming *stream, int enable);
643extern int uvc_probe_video(struct uvc_streaming *stream,
644 struct uvc_streaming_control *probe);
645extern int uvc_commit_video(struct uvc_streaming *stream,
646 struct uvc_streaming_control *ctrl);
647extern int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
648 __u8 intfnum, __u8 cs, void *data, __u16 size);
649
650/* Status */
651extern int uvc_status_init(struct uvc_device *dev);
652extern void uvc_status_cleanup(struct uvc_device *dev);
653extern int uvc_status_start(struct uvc_device *dev);
654extern void uvc_status_stop(struct uvc_device *dev);
655extern int uvc_status_suspend(struct uvc_device *dev);
656extern int uvc_status_resume(struct uvc_device *dev);
657
658/* Controls */
659extern int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
660 struct v4l2_queryctrl *v4l2_ctrl);
661extern int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
662 struct v4l2_querymenu *query_menu);
663
664extern int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
665 const struct uvc_control_mapping *mapping);
666extern int uvc_ctrl_init_device(struct uvc_device *dev);
667extern void uvc_ctrl_cleanup_device(struct uvc_device *dev);
668extern int uvc_ctrl_resume_device(struct uvc_device *dev);
669
670extern int uvc_ctrl_begin(struct uvc_video_chain *chain);
671extern int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback);
672static inline int uvc_ctrl_commit(struct uvc_video_chain *chain)
673{
674 return __uvc_ctrl_commit(chain, 0);
675}
676static inline int uvc_ctrl_rollback(struct uvc_video_chain *chain)
677{
678 return __uvc_ctrl_commit(chain, 1);
679}
680
681extern int uvc_ctrl_get(struct uvc_video_chain *chain,
682 struct v4l2_ext_control *xctrl);
683extern int uvc_ctrl_set(struct uvc_video_chain *chain,
684 struct v4l2_ext_control *xctrl);
685
686extern int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
687 struct uvc_xu_control_query *xqry);
688
689/* Utility functions */
690extern void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
691 unsigned int n_terms, unsigned int threshold);
692extern uint32_t uvc_fraction_to_interval(uint32_t numerator,
693 uint32_t denominator);
694extern struct usb_host_endpoint *uvc_find_endpoint(
695 struct usb_host_interface *alts, __u8 epaddr);
696
697/* Quirks support */
698void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
699 struct uvc_buffer *buf);
700
701#endif /* __KERNEL__ */
702
703#endif