Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / uvc / uvc_ctrl.c
blob3b2e7800d56f9a2f134abcbe29670d7b53b3a189
1 /*
2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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.
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/uaccess.h>
18 #include <linux/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
21 #include <linux/wait.h>
22 #include <asm/atomic.h>
24 #include "uvcvideo.h"
26 #define UVC_CTRL_DATA_CURRENT 0
27 #define UVC_CTRL_DATA_BACKUP 1
28 #define UVC_CTRL_DATA_MIN 2
29 #define UVC_CTRL_DATA_MAX 3
30 #define UVC_CTRL_DATA_RES 4
31 #define UVC_CTRL_DATA_DEF 5
32 #define UVC_CTRL_DATA_LAST 6
34 /* ------------------------------------------------------------------------
35 * Controls
38 static struct uvc_control_info uvc_ctrls[] = {
40 .entity = UVC_GUID_UVC_PROCESSING,
41 .selector = UVC_PU_BRIGHTNESS_CONTROL,
42 .index = 0,
43 .size = 2,
44 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
45 | UVC_CONTROL_RESTORE,
48 .entity = UVC_GUID_UVC_PROCESSING,
49 .selector = UVC_PU_CONTRAST_CONTROL,
50 .index = 1,
51 .size = 2,
52 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
53 | UVC_CONTROL_RESTORE,
56 .entity = UVC_GUID_UVC_PROCESSING,
57 .selector = UVC_PU_HUE_CONTROL,
58 .index = 2,
59 .size = 2,
60 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
61 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
64 .entity = UVC_GUID_UVC_PROCESSING,
65 .selector = UVC_PU_SATURATION_CONTROL,
66 .index = 3,
67 .size = 2,
68 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
69 | UVC_CONTROL_RESTORE,
72 .entity = UVC_GUID_UVC_PROCESSING,
73 .selector = UVC_PU_SHARPNESS_CONTROL,
74 .index = 4,
75 .size = 2,
76 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
77 | UVC_CONTROL_RESTORE,
80 .entity = UVC_GUID_UVC_PROCESSING,
81 .selector = UVC_PU_GAMMA_CONTROL,
82 .index = 5,
83 .size = 2,
84 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
85 | UVC_CONTROL_RESTORE,
88 .entity = UVC_GUID_UVC_PROCESSING,
89 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
90 .index = 6,
91 .size = 2,
92 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
93 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
96 .entity = UVC_GUID_UVC_PROCESSING,
97 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
98 .index = 7,
99 .size = 4,
100 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
101 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
104 .entity = UVC_GUID_UVC_PROCESSING,
105 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
106 .index = 8,
107 .size = 2,
108 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
109 | UVC_CONTROL_RESTORE,
112 .entity = UVC_GUID_UVC_PROCESSING,
113 .selector = UVC_PU_GAIN_CONTROL,
114 .index = 9,
115 .size = 2,
116 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
117 | UVC_CONTROL_RESTORE,
120 .entity = UVC_GUID_UVC_PROCESSING,
121 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
122 .index = 10,
123 .size = 1,
124 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
125 | UVC_CONTROL_RESTORE,
128 .entity = UVC_GUID_UVC_PROCESSING,
129 .selector = UVC_PU_HUE_AUTO_CONTROL,
130 .index = 11,
131 .size = 1,
132 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
133 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
136 .entity = UVC_GUID_UVC_PROCESSING,
137 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
138 .index = 12,
139 .size = 1,
140 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
141 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
144 .entity = UVC_GUID_UVC_PROCESSING,
145 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
146 .index = 13,
147 .size = 1,
148 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
149 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
152 .entity = UVC_GUID_UVC_PROCESSING,
153 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
154 .index = 14,
155 .size = 2,
156 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
157 | UVC_CONTROL_RESTORE,
160 .entity = UVC_GUID_UVC_PROCESSING,
161 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
162 .index = 15,
163 .size = 2,
164 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
165 | UVC_CONTROL_RESTORE,
168 .entity = UVC_GUID_UVC_PROCESSING,
169 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
170 .index = 16,
171 .size = 1,
172 .flags = UVC_CONTROL_GET_CUR,
175 .entity = UVC_GUID_UVC_PROCESSING,
176 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
177 .index = 17,
178 .size = 1,
179 .flags = UVC_CONTROL_GET_CUR,
182 .entity = UVC_GUID_UVC_CAMERA,
183 .selector = UVC_CT_SCANNING_MODE_CONTROL,
184 .index = 0,
185 .size = 1,
186 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
187 | UVC_CONTROL_RESTORE,
190 .entity = UVC_GUID_UVC_CAMERA,
191 .selector = UVC_CT_AE_MODE_CONTROL,
192 .index = 1,
193 .size = 1,
194 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
195 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
196 | UVC_CONTROL_RESTORE,
199 .entity = UVC_GUID_UVC_CAMERA,
200 .selector = UVC_CT_AE_PRIORITY_CONTROL,
201 .index = 2,
202 .size = 1,
203 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
204 | UVC_CONTROL_RESTORE,
207 .entity = UVC_GUID_UVC_CAMERA,
208 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
209 .index = 3,
210 .size = 4,
211 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
212 | UVC_CONTROL_RESTORE,
215 .entity = UVC_GUID_UVC_CAMERA,
216 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
217 .index = 4,
218 .size = 1,
219 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
220 | UVC_CONTROL_RESTORE,
223 .entity = UVC_GUID_UVC_CAMERA,
224 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
225 .index = 5,
226 .size = 2,
227 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
228 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
231 .entity = UVC_GUID_UVC_CAMERA,
232 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
233 .index = 6,
234 .size = 2,
235 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
236 | UVC_CONTROL_AUTO_UPDATE,
239 .entity = UVC_GUID_UVC_CAMERA,
240 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
241 .index = 7,
242 .size = 2,
243 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
244 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
247 .entity = UVC_GUID_UVC_CAMERA,
248 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
249 .index = 8,
250 .size = 1,
251 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
252 | UVC_CONTROL_AUTO_UPDATE,
255 .entity = UVC_GUID_UVC_CAMERA,
256 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
257 .index = 9,
258 .size = 2,
259 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
260 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
263 .entity = UVC_GUID_UVC_CAMERA,
264 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
265 .index = 10,
266 .size = 3,
267 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
268 | UVC_CONTROL_AUTO_UPDATE,
271 .entity = UVC_GUID_UVC_CAMERA,
272 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
273 .index = 11,
274 .size = 8,
275 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
276 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
279 .entity = UVC_GUID_UVC_CAMERA,
280 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
281 .index = 12,
282 .size = 4,
283 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
284 | UVC_CONTROL_AUTO_UPDATE,
287 .entity = UVC_GUID_UVC_CAMERA,
288 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
289 .index = 13,
290 .size = 2,
291 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
292 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
295 .entity = UVC_GUID_UVC_CAMERA,
296 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
297 .index = 14,
298 .size = 2,
299 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
300 | UVC_CONTROL_AUTO_UPDATE,
303 .entity = UVC_GUID_UVC_CAMERA,
304 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
305 .index = 17,
306 .size = 1,
307 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
308 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
311 .entity = UVC_GUID_UVC_CAMERA,
312 .selector = UVC_CT_PRIVACY_CONTROL,
313 .index = 18,
314 .size = 1,
315 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
316 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
320 static struct uvc_menu_info power_line_frequency_controls[] = {
321 { 0, "Disabled" },
322 { 1, "50 Hz" },
323 { 2, "60 Hz" },
326 static struct uvc_menu_info exposure_auto_controls[] = {
327 { 2, "Auto Mode" },
328 { 1, "Manual Mode" },
329 { 4, "Shutter Priority Mode" },
330 { 8, "Aperture Priority Mode" },
333 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
334 __u8 query, const __u8 *data)
336 __s8 zoom = (__s8)data[0];
338 switch (query) {
339 case UVC_GET_CUR:
340 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
342 case UVC_GET_MIN:
343 case UVC_GET_MAX:
344 case UVC_GET_RES:
345 case UVC_GET_DEF:
346 default:
347 return data[2];
351 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
352 __s32 value, __u8 *data)
354 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
355 data[2] = min((int)abs(value), 0xff);
358 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
360 .id = V4L2_CID_BRIGHTNESS,
361 .name = "Brightness",
362 .entity = UVC_GUID_UVC_PROCESSING,
363 .selector = UVC_PU_BRIGHTNESS_CONTROL,
364 .size = 16,
365 .offset = 0,
366 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
367 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
370 .id = V4L2_CID_CONTRAST,
371 .name = "Contrast",
372 .entity = UVC_GUID_UVC_PROCESSING,
373 .selector = UVC_PU_CONTRAST_CONTROL,
374 .size = 16,
375 .offset = 0,
376 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
377 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
380 .id = V4L2_CID_HUE,
381 .name = "Hue",
382 .entity = UVC_GUID_UVC_PROCESSING,
383 .selector = UVC_PU_HUE_CONTROL,
384 .size = 16,
385 .offset = 0,
386 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
387 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
390 .id = V4L2_CID_SATURATION,
391 .name = "Saturation",
392 .entity = UVC_GUID_UVC_PROCESSING,
393 .selector = UVC_PU_SATURATION_CONTROL,
394 .size = 16,
395 .offset = 0,
396 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
397 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
400 .id = V4L2_CID_SHARPNESS,
401 .name = "Sharpness",
402 .entity = UVC_GUID_UVC_PROCESSING,
403 .selector = UVC_PU_SHARPNESS_CONTROL,
404 .size = 16,
405 .offset = 0,
406 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
407 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
410 .id = V4L2_CID_GAMMA,
411 .name = "Gamma",
412 .entity = UVC_GUID_UVC_PROCESSING,
413 .selector = UVC_PU_GAMMA_CONTROL,
414 .size = 16,
415 .offset = 0,
416 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
417 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
420 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
421 .name = "Backlight Compensation",
422 .entity = UVC_GUID_UVC_PROCESSING,
423 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
424 .size = 16,
425 .offset = 0,
426 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
427 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
430 .id = V4L2_CID_GAIN,
431 .name = "Gain",
432 .entity = UVC_GUID_UVC_PROCESSING,
433 .selector = UVC_PU_GAIN_CONTROL,
434 .size = 16,
435 .offset = 0,
436 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
437 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
440 .id = V4L2_CID_POWER_LINE_FREQUENCY,
441 .name = "Power Line Frequency",
442 .entity = UVC_GUID_UVC_PROCESSING,
443 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
444 .size = 2,
445 .offset = 0,
446 .v4l2_type = V4L2_CTRL_TYPE_MENU,
447 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
448 .menu_info = power_line_frequency_controls,
449 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
452 .id = V4L2_CID_HUE_AUTO,
453 .name = "Hue, Auto",
454 .entity = UVC_GUID_UVC_PROCESSING,
455 .selector = UVC_PU_HUE_AUTO_CONTROL,
456 .size = 1,
457 .offset = 0,
458 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
459 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
462 .id = V4L2_CID_EXPOSURE_AUTO,
463 .name = "Exposure, Auto",
464 .entity = UVC_GUID_UVC_CAMERA,
465 .selector = UVC_CT_AE_MODE_CONTROL,
466 .size = 4,
467 .offset = 0,
468 .v4l2_type = V4L2_CTRL_TYPE_MENU,
469 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
470 .menu_info = exposure_auto_controls,
471 .menu_count = ARRAY_SIZE(exposure_auto_controls),
474 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
475 .name = "Exposure, Auto Priority",
476 .entity = UVC_GUID_UVC_CAMERA,
477 .selector = UVC_CT_AE_PRIORITY_CONTROL,
478 .size = 1,
479 .offset = 0,
480 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
481 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
484 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
485 .name = "Exposure (Absolute)",
486 .entity = UVC_GUID_UVC_CAMERA,
487 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
488 .size = 32,
489 .offset = 0,
490 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
491 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
494 .id = V4L2_CID_AUTO_WHITE_BALANCE,
495 .name = "White Balance Temperature, Auto",
496 .entity = UVC_GUID_UVC_PROCESSING,
497 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
498 .size = 1,
499 .offset = 0,
500 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
501 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
504 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
505 .name = "White Balance Temperature",
506 .entity = UVC_GUID_UVC_PROCESSING,
507 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
508 .size = 16,
509 .offset = 0,
510 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
511 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
514 .id = V4L2_CID_AUTO_WHITE_BALANCE,
515 .name = "White Balance Component, Auto",
516 .entity = UVC_GUID_UVC_PROCESSING,
517 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
518 .size = 1,
519 .offset = 0,
520 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
521 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
524 .id = V4L2_CID_BLUE_BALANCE,
525 .name = "White Balance Blue Component",
526 .entity = UVC_GUID_UVC_PROCESSING,
527 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
528 .size = 16,
529 .offset = 0,
530 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
531 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
534 .id = V4L2_CID_RED_BALANCE,
535 .name = "White Balance Red Component",
536 .entity = UVC_GUID_UVC_PROCESSING,
537 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
538 .size = 16,
539 .offset = 16,
540 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
541 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
544 .id = V4L2_CID_FOCUS_ABSOLUTE,
545 .name = "Focus (absolute)",
546 .entity = UVC_GUID_UVC_CAMERA,
547 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
548 .size = 16,
549 .offset = 0,
550 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
551 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
554 .id = V4L2_CID_FOCUS_AUTO,
555 .name = "Focus, Auto",
556 .entity = UVC_GUID_UVC_CAMERA,
557 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
558 .size = 1,
559 .offset = 0,
560 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
561 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
564 .id = V4L2_CID_ZOOM_ABSOLUTE,
565 .name = "Zoom, Absolute",
566 .entity = UVC_GUID_UVC_CAMERA,
567 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
568 .size = 16,
569 .offset = 0,
570 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
571 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
574 .id = V4L2_CID_ZOOM_CONTINUOUS,
575 .name = "Zoom, Continuous",
576 .entity = UVC_GUID_UVC_CAMERA,
577 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
578 .size = 0,
579 .offset = 0,
580 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
581 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
582 .get = uvc_ctrl_get_zoom,
583 .set = uvc_ctrl_set_zoom,
586 .id = V4L2_CID_PRIVACY,
587 .name = "Privacy",
588 .entity = UVC_GUID_UVC_CAMERA,
589 .selector = UVC_CT_PRIVACY_CONTROL,
590 .size = 1,
591 .offset = 0,
592 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
593 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
597 /* ------------------------------------------------------------------------
598 * Utility functions
601 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
603 return ctrl->data + id * ctrl->info->size;
606 static inline int uvc_test_bit(const __u8 *data, int bit)
608 return (data[bit >> 3] >> (bit & 7)) & 1;
611 static inline void uvc_clear_bit(__u8 *data, int bit)
613 data[bit >> 3] &= ~(1 << (bit & 7));
616 /* Extract the bit string specified by mapping->offset and mapping->size
617 * from the little-endian data stored at 'data' and return the result as
618 * a signed 32bit integer. Sign extension will be performed if the mapping
619 * references a signed data type.
621 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
622 __u8 query, const __u8 *data)
624 int bits = mapping->size;
625 int offset = mapping->offset;
626 __s32 value = 0;
627 __u8 mask;
629 data += offset / 8;
630 offset &= 7;
631 mask = ((1LL << bits) - 1) << offset;
633 for (; bits > 0; data++) {
634 __u8 byte = *data & mask;
635 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
636 bits -= 8 - (offset > 0 ? offset : 0);
637 offset -= 8;
638 mask = (1 << bits) - 1;
641 /* Sign-extend the value if needed. */
642 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
643 value |= -(value & (1 << (mapping->size - 1)));
645 return value;
648 /* Set the bit string specified by mapping->offset and mapping->size
649 * in the little-endian data stored at 'data' to the value 'value'.
651 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
652 __s32 value, __u8 *data)
654 int bits = mapping->size;
655 int offset = mapping->offset;
656 __u8 mask;
658 data += offset / 8;
659 offset &= 7;
661 for (; bits > 0; data++) {
662 mask = ((1LL << bits) - 1) << offset;
663 *data = (*data & ~mask) | ((value << offset) & mask);
664 value >>= offset ? offset : 8;
665 bits -= 8 - offset;
666 offset = 0;
670 /* ------------------------------------------------------------------------
671 * Terminal and unit management
674 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
675 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
676 static const __u8 uvc_media_transport_input_guid[16] =
677 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
679 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
681 switch (UVC_ENTITY_TYPE(entity)) {
682 case UVC_ITT_CAMERA:
683 return memcmp(uvc_camera_guid, guid, 16) == 0;
685 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
686 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
688 case UVC_VC_PROCESSING_UNIT:
689 return memcmp(uvc_processing_guid, guid, 16) == 0;
691 case UVC_VC_EXTENSION_UNIT:
692 return memcmp(entity->extension.guidExtensionCode,
693 guid, 16) == 0;
695 default:
696 return 0;
700 /* ------------------------------------------------------------------------
701 * UVC Controls
704 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
705 struct uvc_control_mapping **mapping, struct uvc_control **control,
706 int next)
708 struct uvc_control *ctrl;
709 struct uvc_control_mapping *map;
710 unsigned int i;
712 if (entity == NULL)
713 return;
715 for (i = 0; i < entity->ncontrols; ++i) {
716 ctrl = &entity->controls[i];
717 if (ctrl->info == NULL)
718 continue;
720 list_for_each_entry(map, &ctrl->info->mappings, list) {
721 if ((map->id == v4l2_id) && !next) {
722 *control = ctrl;
723 *mapping = map;
724 return;
727 if ((*mapping == NULL || (*mapping)->id > map->id) &&
728 (map->id > v4l2_id) && next) {
729 *control = ctrl;
730 *mapping = map;
736 struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
737 __u32 v4l2_id, struct uvc_control_mapping **mapping)
739 struct uvc_control *ctrl = NULL;
740 struct uvc_entity *entity;
741 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
743 *mapping = NULL;
745 /* Mask the query flags. */
746 v4l2_id &= V4L2_CTRL_ID_MASK;
748 /* Find the control. */
749 list_for_each_entry(entity, &chain->entities, chain) {
750 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
751 if (ctrl && !next)
752 return ctrl;
755 if (ctrl == NULL && !next)
756 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
757 v4l2_id);
759 return ctrl;
762 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
763 struct uvc_control *ctrl)
765 int ret;
767 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
768 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
769 chain->dev->intfnum, ctrl->info->selector,
770 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
771 ctrl->info->size);
772 if (ret < 0)
773 return ret;
776 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
777 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
778 chain->dev->intfnum, ctrl->info->selector,
779 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
780 ctrl->info->size);
781 if (ret < 0)
782 return ret;
784 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
785 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
786 chain->dev->intfnum, ctrl->info->selector,
787 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
788 ctrl->info->size);
789 if (ret < 0)
790 return ret;
792 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
793 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
794 chain->dev->intfnum, ctrl->info->selector,
795 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
796 ctrl->info->size);
797 if (ret < 0)
798 return ret;
801 ctrl->cached = 1;
802 return 0;
805 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
806 struct v4l2_queryctrl *v4l2_ctrl)
808 struct uvc_control *ctrl;
809 struct uvc_control_mapping *mapping;
810 struct uvc_menu_info *menu;
811 unsigned int i;
812 int ret;
814 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
815 if (ctrl == NULL)
816 return -EINVAL;
818 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
819 v4l2_ctrl->id = mapping->id;
820 v4l2_ctrl->type = mapping->v4l2_type;
821 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
822 v4l2_ctrl->flags = 0;
824 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
825 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
827 if (!ctrl->cached) {
828 ret = uvc_ctrl_populate_cache(chain, ctrl);
829 if (ret < 0)
830 return ret;
833 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
834 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
835 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
838 switch (mapping->v4l2_type) {
839 case V4L2_CTRL_TYPE_MENU:
840 v4l2_ctrl->minimum = 0;
841 v4l2_ctrl->maximum = mapping->menu_count - 1;
842 v4l2_ctrl->step = 1;
844 menu = mapping->menu_info;
845 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
846 if (menu->value == v4l2_ctrl->default_value) {
847 v4l2_ctrl->default_value = i;
848 break;
852 return 0;
854 case V4L2_CTRL_TYPE_BOOLEAN:
855 v4l2_ctrl->minimum = 0;
856 v4l2_ctrl->maximum = 1;
857 v4l2_ctrl->step = 1;
858 return 0;
860 case V4L2_CTRL_TYPE_BUTTON:
861 v4l2_ctrl->minimum = 0;
862 v4l2_ctrl->maximum = 0;
863 v4l2_ctrl->step = 0;
864 return 0;
866 default:
867 break;
870 if (ctrl->info->flags & UVC_CONTROL_GET_MIN)
871 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
872 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
874 if (ctrl->info->flags & UVC_CONTROL_GET_MAX)
875 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
876 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
878 if (ctrl->info->flags & UVC_CONTROL_GET_RES)
879 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
880 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
882 return 0;
886 /* --------------------------------------------------------------------------
887 * Control transactions
889 * To make extended set operations as atomic as the hardware allows, controls
890 * are handled using begin/commit/rollback operations.
892 * At the beginning of a set request, uvc_ctrl_begin should be called to
893 * initialize the request. This function acquires the control lock.
895 * When setting a control, the new value is stored in the control data field
896 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
897 * later processing. If the UVC and V4L2 control sizes differ, the current
898 * value is loaded from the hardware before storing the new value in the data
899 * field.
901 * After processing all controls in the transaction, uvc_ctrl_commit or
902 * uvc_ctrl_rollback must be called to apply the pending changes to the
903 * hardware or revert them. When applying changes, all controls marked as
904 * dirty will be modified in the UVC device, and the dirty flag will be
905 * cleared. When reverting controls, the control data field
906 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
907 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
908 * control lock.
910 int uvc_ctrl_begin(struct uvc_video_chain *chain)
912 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
915 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
916 struct uvc_entity *entity, int rollback)
918 struct uvc_control *ctrl;
919 unsigned int i;
920 int ret;
922 if (entity == NULL)
923 return 0;
925 for (i = 0; i < entity->ncontrols; ++i) {
926 ctrl = &entity->controls[i];
927 if (ctrl->info == NULL)
928 continue;
930 /* Reset the loaded flag for auto-update controls that were
931 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
932 * uvc_ctrl_get from using the cached value.
934 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
935 ctrl->loaded = 0;
937 if (!ctrl->dirty)
938 continue;
940 if (!rollback)
941 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
942 dev->intfnum, ctrl->info->selector,
943 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
944 ctrl->info->size);
945 else
946 ret = 0;
948 if (rollback || ret < 0)
949 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
950 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
951 ctrl->info->size);
953 ctrl->dirty = 0;
955 if (ret < 0)
956 return ret;
959 return 0;
962 int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
964 struct uvc_entity *entity;
965 int ret = 0;
967 /* Find the control. */
968 list_for_each_entry(entity, &chain->entities, chain) {
969 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
970 if (ret < 0)
971 goto done;
974 done:
975 mutex_unlock(&chain->ctrl_mutex);
976 return ret;
979 int uvc_ctrl_get(struct uvc_video_chain *chain,
980 struct v4l2_ext_control *xctrl)
982 struct uvc_control *ctrl;
983 struct uvc_control_mapping *mapping;
984 struct uvc_menu_info *menu;
985 unsigned int i;
986 int ret;
988 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
989 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
990 return -EINVAL;
992 if (!ctrl->loaded) {
993 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
994 chain->dev->intfnum, ctrl->info->selector,
995 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
996 ctrl->info->size);
997 if (ret < 0)
998 return ret;
1000 ctrl->loaded = 1;
1003 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1004 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1006 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1007 menu = mapping->menu_info;
1008 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1009 if (menu->value == xctrl->value) {
1010 xctrl->value = i;
1011 break;
1016 return 0;
1019 int uvc_ctrl_set(struct uvc_video_chain *chain,
1020 struct v4l2_ext_control *xctrl)
1022 struct uvc_control *ctrl;
1023 struct uvc_control_mapping *mapping;
1024 s32 value;
1025 u32 step;
1026 s32 min;
1027 s32 max;
1028 int ret;
1030 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1031 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1032 return -EINVAL;
1034 /* Clamp out of range values. */
1035 switch (mapping->v4l2_type) {
1036 case V4L2_CTRL_TYPE_INTEGER:
1037 if (!ctrl->cached) {
1038 ret = uvc_ctrl_populate_cache(chain, ctrl);
1039 if (ret < 0)
1040 return ret;
1043 min = mapping->get(mapping, UVC_GET_MIN,
1044 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1045 max = mapping->get(mapping, UVC_GET_MAX,
1046 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1047 step = mapping->get(mapping, UVC_GET_RES,
1048 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1050 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1051 xctrl->value = clamp(xctrl->value, min, max);
1052 value = xctrl->value;
1053 break;
1055 case V4L2_CTRL_TYPE_BOOLEAN:
1056 xctrl->value = clamp(xctrl->value, 0, 1);
1057 value = xctrl->value;
1058 break;
1060 case V4L2_CTRL_TYPE_MENU:
1061 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1062 return -ERANGE;
1063 value = mapping->menu_info[xctrl->value].value;
1064 break;
1066 default:
1067 value = xctrl->value;
1068 break;
1071 /* If the mapping doesn't span the whole UVC control, the current value
1072 * needs to be loaded from the device to perform the read-modify-write
1073 * operation.
1075 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1076 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1077 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1078 0, ctrl->info->size);
1079 } else {
1080 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1081 ctrl->entity->id, chain->dev->intfnum,
1082 ctrl->info->selector,
1083 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1084 ctrl->info->size);
1085 if (ret < 0)
1086 return ret;
1089 ctrl->loaded = 1;
1092 /* Backup the current value in case we need to rollback later. */
1093 if (!ctrl->dirty) {
1094 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1095 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1096 ctrl->info->size);
1099 mapping->set(mapping, value,
1100 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1102 ctrl->dirty = 1;
1103 ctrl->modified = 1;
1104 return 0;
1107 /* --------------------------------------------------------------------------
1108 * Dynamic controls
1111 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1112 struct uvc_xu_control *xctrl, int set)
1114 struct uvc_entity *entity;
1115 struct uvc_control *ctrl = NULL;
1116 unsigned int i, found = 0;
1117 __u8 *data;
1118 int ret;
1120 /* Find the extension unit. */
1121 list_for_each_entry(entity, &chain->entities, chain) {
1122 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1123 entity->id == xctrl->unit)
1124 break;
1127 if (entity->id != xctrl->unit) {
1128 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1129 xctrl->unit);
1130 return -EINVAL;
1133 /* Find the control. */
1134 for (i = 0; i < entity->ncontrols; ++i) {
1135 ctrl = &entity->controls[i];
1136 if (ctrl->info == NULL)
1137 continue;
1139 if (ctrl->info->selector == xctrl->selector) {
1140 found = 1;
1141 break;
1145 if (!found) {
1146 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1147 entity->extension.guidExtensionCode, xctrl->selector);
1148 return -EINVAL;
1151 /* Validate control data size. */
1152 if (ctrl->info->size != xctrl->size)
1153 return -EINVAL;
1155 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1156 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1157 return -EINVAL;
1159 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1160 return -ERESTARTSYS;
1162 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1163 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1164 xctrl->size);
1165 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1167 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1168 ret = -EFAULT;
1169 goto out;
1172 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1173 xctrl->unit, chain->dev->intfnum, xctrl->selector,
1174 data, xctrl->size);
1175 if (ret < 0)
1176 goto out;
1178 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1179 ret = -EFAULT;
1180 goto out;
1183 out:
1184 if (ret)
1185 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1186 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1187 xctrl->size);
1189 mutex_unlock(&chain->ctrl_mutex);
1190 return ret;
1193 /* --------------------------------------------------------------------------
1194 * Suspend/resume
1198 * Restore control values after resume, skipping controls that haven't been
1199 * changed.
1201 * TODO
1202 * - Don't restore modified controls that are back to their default value.
1203 * - Handle restore order (Auto-Exposure Mode should be restored before
1204 * Exposure Time).
1206 int uvc_ctrl_resume_device(struct uvc_device *dev)
1208 struct uvc_control *ctrl;
1209 struct uvc_entity *entity;
1210 unsigned int i;
1211 int ret;
1213 /* Walk the entities list and restore controls when possible. */
1214 list_for_each_entry(entity, &dev->entities, list) {
1216 for (i = 0; i < entity->ncontrols; ++i) {
1217 ctrl = &entity->controls[i];
1219 if (ctrl->info == NULL || !ctrl->modified ||
1220 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1221 continue;
1223 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1224 ctrl->info->entity, ctrl->info->index,
1225 ctrl->info->selector);
1226 ctrl->dirty = 1;
1229 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1230 if (ret < 0)
1231 return ret;
1234 return 0;
1237 /* --------------------------------------------------------------------------
1238 * Control and mapping handling
1241 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1242 struct uvc_control_info *info)
1244 struct uvc_entity *entity;
1245 struct uvc_control *ctrl = NULL;
1246 int ret, found = 0;
1247 unsigned int i;
1249 list_for_each_entry(entity, &dev->entities, list) {
1250 if (!uvc_entity_match_guid(entity, info->entity))
1251 continue;
1253 for (i = 0; i < entity->ncontrols; ++i) {
1254 ctrl = &entity->controls[i];
1255 if (ctrl->index == info->index) {
1256 found = 1;
1257 break;
1261 if (found)
1262 break;
1265 if (!found)
1266 return;
1268 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1269 /* Check if the device control information and length match
1270 * the user supplied information.
1272 __u32 flags;
1273 __le16 size;
1274 __u8 inf;
1276 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1277 dev->intfnum, info->selector, (__u8 *)&size, 2);
1278 if (ret < 0) {
1279 uvc_trace(UVC_TRACE_CONTROL,
1280 "GET_LEN failed on control %pUl/%u (%d).\n",
1281 info->entity, info->selector, ret);
1282 return;
1285 if (info->size != le16_to_cpu(size)) {
1286 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1287 "doesn't match user supplied value.\n",
1288 info->entity, info->selector);
1289 return;
1292 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1293 dev->intfnum, info->selector, &inf, 1);
1294 if (ret < 0) {
1295 uvc_trace(UVC_TRACE_CONTROL,
1296 "GET_INFO failed on control %pUl/%u (%d).\n",
1297 info->entity, info->selector, ret);
1298 return;
1301 flags = info->flags;
1302 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1303 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1304 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1305 "don't match supported operations.\n",
1306 info->entity, info->selector);
1307 return;
1311 ctrl->info = info;
1312 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
1313 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1314 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1315 dev->udev->devpath, entity->id);
1319 * Add an item to the UVC control information list, and instantiate a control
1320 * structure for each device that supports the control.
1322 int uvc_ctrl_add_info(struct uvc_control_info *info)
1324 struct uvc_control_info *ctrl;
1325 struct uvc_device *dev;
1326 int ret = 0;
1328 /* Find matching controls by walking the devices, entities and
1329 * controls list.
1331 mutex_lock(&uvc_driver.ctrl_mutex);
1333 /* First check if the list contains a control matching the new one.
1334 * Bail out if it does.
1336 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1337 if (memcmp(ctrl->entity, info->entity, 16))
1338 continue;
1340 if (ctrl->selector == info->selector) {
1341 uvc_trace(UVC_TRACE_CONTROL,
1342 "Control %pUl/%u is already defined.\n",
1343 info->entity, info->selector);
1344 ret = -EEXIST;
1345 goto end;
1347 if (ctrl->index == info->index) {
1348 uvc_trace(UVC_TRACE_CONTROL,
1349 "Control %pUl/%u would overwrite index %d.\n",
1350 info->entity, info->selector, info->index);
1351 ret = -EEXIST;
1352 goto end;
1356 list_for_each_entry(dev, &uvc_driver.devices, list)
1357 uvc_ctrl_add_ctrl(dev, info);
1359 INIT_LIST_HEAD(&info->mappings);
1360 list_add_tail(&info->list, &uvc_driver.controls);
1361 end:
1362 mutex_unlock(&uvc_driver.ctrl_mutex);
1363 return ret;
1366 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1368 struct uvc_control_info *info;
1369 struct uvc_control_mapping *map;
1370 int ret = -EINVAL;
1372 if (mapping->get == NULL)
1373 mapping->get = uvc_get_le_value;
1374 if (mapping->set == NULL)
1375 mapping->set = uvc_set_le_value;
1377 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1378 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1379 "invalid control id 0x%08x\n", mapping->name,
1380 mapping->id);
1381 return -EINVAL;
1384 mutex_lock(&uvc_driver.ctrl_mutex);
1385 list_for_each_entry(info, &uvc_driver.controls, list) {
1386 if (memcmp(info->entity, mapping->entity, 16) ||
1387 info->selector != mapping->selector)
1388 continue;
1390 if (info->size * 8 < mapping->size + mapping->offset) {
1391 uvc_trace(UVC_TRACE_CONTROL,
1392 "Mapping '%s' would overflow control %pUl/%u\n",
1393 mapping->name, info->entity, info->selector);
1394 ret = -EOVERFLOW;
1395 goto end;
1398 /* Check if the list contains a mapping matching the new one.
1399 * Bail out if it does.
1401 list_for_each_entry(map, &info->mappings, list) {
1402 if (map->id == mapping->id) {
1403 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1404 "already defined.\n", mapping->name);
1405 ret = -EEXIST;
1406 goto end;
1410 mapping->ctrl = info;
1411 list_add_tail(&mapping->list, &info->mappings);
1412 uvc_trace(UVC_TRACE_CONTROL,
1413 "Adding mapping %s to control %pUl/%u.\n",
1414 mapping->name, info->entity, info->selector);
1416 ret = 0;
1417 break;
1419 end:
1420 mutex_unlock(&uvc_driver.ctrl_mutex);
1421 return ret;
1425 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1426 * are currently the ones that crash the camera or unconditionally return an
1427 * error when queried.
1429 static void
1430 uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
1432 static const struct {
1433 struct usb_device_id id;
1434 u8 index;
1435 } blacklist[] = {
1436 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1437 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1438 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1441 u8 *controls;
1442 unsigned int size;
1443 unsigned int i;
1445 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
1446 return;
1448 controls = entity->processing.bmControls;
1449 size = entity->processing.bControlSize;
1451 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
1452 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1453 continue;
1455 if (blacklist[i].index >= 8 * size ||
1456 !uvc_test_bit(controls, blacklist[i].index))
1457 continue;
1459 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1460 "removing it.\n", entity->id, blacklist[i].index);
1462 uvc_clear_bit(controls, blacklist[i].index);
1467 * Initialize device controls.
1469 int uvc_ctrl_init_device(struct uvc_device *dev)
1471 struct uvc_control_info *info;
1472 struct uvc_control *ctrl;
1473 struct uvc_entity *entity;
1474 unsigned int i;
1476 /* Walk the entities list and instantiate controls */
1477 list_for_each_entry(entity, &dev->entities, list) {
1478 unsigned int bControlSize = 0, ncontrols = 0;
1479 __u8 *bmControls = NULL;
1481 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1482 bmControls = entity->extension.bmControls;
1483 bControlSize = entity->extension.bControlSize;
1484 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1485 bmControls = entity->processing.bmControls;
1486 bControlSize = entity->processing.bControlSize;
1487 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1488 bmControls = entity->camera.bmControls;
1489 bControlSize = entity->camera.bControlSize;
1492 uvc_ctrl_prune_entity(dev, entity);
1494 for (i = 0; i < bControlSize; ++i)
1495 ncontrols += hweight8(bmControls[i]);
1497 if (ncontrols == 0)
1498 continue;
1500 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1501 if (entity->controls == NULL)
1502 return -ENOMEM;
1504 entity->ncontrols = ncontrols;
1506 ctrl = entity->controls;
1507 for (i = 0; i < bControlSize * 8; ++i) {
1508 if (uvc_test_bit(bmControls, i) == 0)
1509 continue;
1511 ctrl->entity = entity;
1512 ctrl->index = i;
1513 ctrl++;
1517 /* Walk the controls info list and associate them with the device
1518 * controls, then add the device to the global device list. This has
1519 * to be done while holding the controls lock, to make sure
1520 * uvc_ctrl_add_info() will not get called in-between.
1522 mutex_lock(&uvc_driver.ctrl_mutex);
1523 list_for_each_entry(info, &uvc_driver.controls, list)
1524 uvc_ctrl_add_ctrl(dev, info);
1526 list_add_tail(&dev->list, &uvc_driver.devices);
1527 mutex_unlock(&uvc_driver.ctrl_mutex);
1529 return 0;
1533 * Cleanup device controls.
1535 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1537 struct uvc_entity *entity;
1538 unsigned int i;
1540 /* Remove the device from the global devices list */
1541 mutex_lock(&uvc_driver.ctrl_mutex);
1542 if (dev->list.next != NULL)
1543 list_del(&dev->list);
1544 mutex_unlock(&uvc_driver.ctrl_mutex);
1546 list_for_each_entry(entity, &dev->entities, list) {
1547 for (i = 0; i < entity->ncontrols; ++i)
1548 kfree(entity->controls[i].data);
1550 kfree(entity->controls);
1554 void uvc_ctrl_init(void)
1556 struct uvc_control_info *ctrl = uvc_ctrls;
1557 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1558 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1559 struct uvc_control_mapping *mend =
1560 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1562 for (; ctrl < cend; ++ctrl)
1563 uvc_ctrl_add_info(ctrl);
1565 for (; mapping < mend; ++mapping)
1566 uvc_ctrl_add_mapping(mapping);