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