2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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/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>
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
36 /* ------------------------------------------------------------------------
40 static struct uvc_control_info uvc_ctrls[] = {
42 .entity = UVC_GUID_UVC_PROCESSING,
43 .selector = UVC_PU_BRIGHTNESS_CONTROL,
46 .flags = UVC_CTRL_FLAG_SET_CUR
47 | UVC_CTRL_FLAG_GET_RANGE
48 | UVC_CTRL_FLAG_RESTORE,
51 .entity = UVC_GUID_UVC_PROCESSING,
52 .selector = UVC_PU_CONTRAST_CONTROL,
55 .flags = UVC_CTRL_FLAG_SET_CUR
56 | UVC_CTRL_FLAG_GET_RANGE
57 | UVC_CTRL_FLAG_RESTORE,
60 .entity = UVC_GUID_UVC_PROCESSING,
61 .selector = UVC_PU_HUE_CONTROL,
64 .flags = UVC_CTRL_FLAG_SET_CUR
65 | UVC_CTRL_FLAG_GET_RANGE
66 | UVC_CTRL_FLAG_RESTORE
67 | UVC_CTRL_FLAG_AUTO_UPDATE,
70 .entity = UVC_GUID_UVC_PROCESSING,
71 .selector = UVC_PU_SATURATION_CONTROL,
74 .flags = UVC_CTRL_FLAG_SET_CUR
75 | UVC_CTRL_FLAG_GET_RANGE
76 | UVC_CTRL_FLAG_RESTORE,
79 .entity = UVC_GUID_UVC_PROCESSING,
80 .selector = UVC_PU_SHARPNESS_CONTROL,
83 .flags = UVC_CTRL_FLAG_SET_CUR
84 | UVC_CTRL_FLAG_GET_RANGE
85 | UVC_CTRL_FLAG_RESTORE,
88 .entity = UVC_GUID_UVC_PROCESSING,
89 .selector = UVC_PU_GAMMA_CONTROL,
92 .flags = UVC_CTRL_FLAG_SET_CUR
93 | UVC_CTRL_FLAG_GET_RANGE
94 | UVC_CTRL_FLAG_RESTORE,
97 .entity = UVC_GUID_UVC_PROCESSING,
98 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
101 .flags = UVC_CTRL_FLAG_SET_CUR
102 | UVC_CTRL_FLAG_GET_RANGE
103 | UVC_CTRL_FLAG_RESTORE
104 | UVC_CTRL_FLAG_AUTO_UPDATE,
107 .entity = UVC_GUID_UVC_PROCESSING,
108 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
111 .flags = UVC_CTRL_FLAG_SET_CUR
112 | UVC_CTRL_FLAG_GET_RANGE
113 | UVC_CTRL_FLAG_RESTORE
114 | UVC_CTRL_FLAG_AUTO_UPDATE,
117 .entity = UVC_GUID_UVC_PROCESSING,
118 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
121 .flags = UVC_CTRL_FLAG_SET_CUR
122 | UVC_CTRL_FLAG_GET_RANGE
123 | UVC_CTRL_FLAG_RESTORE,
126 .entity = UVC_GUID_UVC_PROCESSING,
127 .selector = UVC_PU_GAIN_CONTROL,
130 .flags = UVC_CTRL_FLAG_SET_CUR
131 | UVC_CTRL_FLAG_GET_RANGE
132 | UVC_CTRL_FLAG_RESTORE,
135 .entity = UVC_GUID_UVC_PROCESSING,
136 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
139 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
140 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
143 .entity = UVC_GUID_UVC_PROCESSING,
144 .selector = UVC_PU_HUE_AUTO_CONTROL,
147 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
148 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
151 .entity = UVC_GUID_UVC_PROCESSING,
152 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
155 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
156 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
159 .entity = UVC_GUID_UVC_PROCESSING,
160 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
163 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
164 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
167 .entity = UVC_GUID_UVC_PROCESSING,
168 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
171 .flags = UVC_CTRL_FLAG_SET_CUR
172 | UVC_CTRL_FLAG_GET_RANGE
173 | UVC_CTRL_FLAG_RESTORE,
176 .entity = UVC_GUID_UVC_PROCESSING,
177 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
180 .flags = UVC_CTRL_FLAG_SET_CUR
181 | UVC_CTRL_FLAG_GET_RANGE
182 | UVC_CTRL_FLAG_RESTORE,
185 .entity = UVC_GUID_UVC_PROCESSING,
186 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
189 .flags = UVC_CTRL_FLAG_GET_CUR,
192 .entity = UVC_GUID_UVC_PROCESSING,
193 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
196 .flags = UVC_CTRL_FLAG_GET_CUR,
199 .entity = UVC_GUID_UVC_CAMERA,
200 .selector = UVC_CT_SCANNING_MODE_CONTROL,
203 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
204 | UVC_CTRL_FLAG_RESTORE,
207 .entity = UVC_GUID_UVC_CAMERA,
208 .selector = UVC_CT_AE_MODE_CONTROL,
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,
216 .entity = UVC_GUID_UVC_CAMERA,
217 .selector = UVC_CT_AE_PRIORITY_CONTROL,
220 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
221 | UVC_CTRL_FLAG_RESTORE,
224 .entity = UVC_GUID_UVC_CAMERA,
225 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
228 .flags = UVC_CTRL_FLAG_SET_CUR
229 | UVC_CTRL_FLAG_GET_RANGE
230 | UVC_CTRL_FLAG_RESTORE
231 | UVC_CTRL_FLAG_AUTO_UPDATE,
234 .entity = UVC_GUID_UVC_CAMERA,
235 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
238 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
241 .entity = UVC_GUID_UVC_CAMERA,
242 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
245 .flags = UVC_CTRL_FLAG_SET_CUR
246 | UVC_CTRL_FLAG_GET_RANGE
247 | UVC_CTRL_FLAG_RESTORE
248 | UVC_CTRL_FLAG_AUTO_UPDATE,
251 .entity = UVC_GUID_UVC_CAMERA,
252 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
255 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
256 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
257 | UVC_CTRL_FLAG_GET_DEF
258 | UVC_CTRL_FLAG_AUTO_UPDATE,
261 .entity = UVC_GUID_UVC_CAMERA,
262 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
265 .flags = UVC_CTRL_FLAG_SET_CUR
266 | UVC_CTRL_FLAG_GET_RANGE
267 | UVC_CTRL_FLAG_RESTORE
268 | UVC_CTRL_FLAG_AUTO_UPDATE,
271 .entity = UVC_GUID_UVC_CAMERA,
272 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
275 .flags = UVC_CTRL_FLAG_SET_CUR
276 | UVC_CTRL_FLAG_AUTO_UPDATE,
279 .entity = UVC_GUID_UVC_CAMERA,
280 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
283 .flags = UVC_CTRL_FLAG_SET_CUR
284 | UVC_CTRL_FLAG_GET_RANGE
285 | UVC_CTRL_FLAG_RESTORE
286 | UVC_CTRL_FLAG_AUTO_UPDATE,
289 .entity = UVC_GUID_UVC_CAMERA,
290 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
293 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
294 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
295 | UVC_CTRL_FLAG_GET_DEF
296 | UVC_CTRL_FLAG_AUTO_UPDATE,
299 .entity = UVC_GUID_UVC_CAMERA,
300 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
303 .flags = UVC_CTRL_FLAG_SET_CUR
304 | UVC_CTRL_FLAG_GET_RANGE
305 | UVC_CTRL_FLAG_RESTORE
306 | UVC_CTRL_FLAG_AUTO_UPDATE,
309 .entity = UVC_GUID_UVC_CAMERA,
310 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
313 .flags = UVC_CTRL_FLAG_SET_CUR
314 | UVC_CTRL_FLAG_GET_RANGE
315 | UVC_CTRL_FLAG_AUTO_UPDATE,
318 .entity = UVC_GUID_UVC_CAMERA,
319 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
322 .flags = UVC_CTRL_FLAG_SET_CUR
323 | UVC_CTRL_FLAG_GET_RANGE
324 | UVC_CTRL_FLAG_RESTORE
325 | UVC_CTRL_FLAG_AUTO_UPDATE,
328 .entity = UVC_GUID_UVC_CAMERA,
329 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
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,
338 .entity = UVC_GUID_UVC_CAMERA,
339 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
342 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
343 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
346 .entity = UVC_GUID_UVC_CAMERA,
347 .selector = UVC_CT_PRIVACY_CONTROL,
350 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
351 | UVC_CTRL_FLAG_RESTORE
352 | UVC_CTRL_FLAG_AUTO_UPDATE,
356 static struct uvc_menu_info power_line_frequency_controls[] = {
362 static struct uvc_menu_info exposure_auto_controls[] = {
364 { 1, "Manual Mode" },
365 { 4, "Shutter Priority Mode" },
366 { 8, "Aperture Priority Mode" },
369 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
370 __u8 query, const __u8 *data)
372 __s8 zoom = (__s8)data[0];
376 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
387 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
388 __s32 value, __u8 *data)
390 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
391 data[2] = min((int)abs(value), 0xff);
394 static __s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
395 __u8 query, const __u8 *data)
397 unsigned int first = mapping->offset / 8;
398 __s8 rel = (__s8)data[first];
402 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
405 return -data[first+1];
410 return data[first+1];
414 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
415 __s32 value, __u8 *data)
417 unsigned int first = mapping->offset / 8;
419 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
420 data[first+1] = min_t(int, abs(value), 0xff);
423 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
425 .id = V4L2_CID_BRIGHTNESS,
426 .name = "Brightness",
427 .entity = UVC_GUID_UVC_PROCESSING,
428 .selector = UVC_PU_BRIGHTNESS_CONTROL,
431 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
432 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
435 .id = V4L2_CID_CONTRAST,
437 .entity = UVC_GUID_UVC_PROCESSING,
438 .selector = UVC_PU_CONTRAST_CONTROL,
441 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
442 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
447 .entity = UVC_GUID_UVC_PROCESSING,
448 .selector = UVC_PU_HUE_CONTROL,
451 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
452 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
453 .master_id = V4L2_CID_HUE_AUTO,
457 .id = V4L2_CID_SATURATION,
458 .name = "Saturation",
459 .entity = UVC_GUID_UVC_PROCESSING,
460 .selector = UVC_PU_SATURATION_CONTROL,
463 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
464 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
467 .id = V4L2_CID_SHARPNESS,
469 .entity = UVC_GUID_UVC_PROCESSING,
470 .selector = UVC_PU_SHARPNESS_CONTROL,
473 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
474 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
477 .id = V4L2_CID_GAMMA,
479 .entity = UVC_GUID_UVC_PROCESSING,
480 .selector = UVC_PU_GAMMA_CONTROL,
483 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
484 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
487 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
488 .name = "Backlight Compensation",
489 .entity = UVC_GUID_UVC_PROCESSING,
490 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
493 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
494 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
499 .entity = UVC_GUID_UVC_PROCESSING,
500 .selector = UVC_PU_GAIN_CONTROL,
503 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
504 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
507 .id = V4L2_CID_POWER_LINE_FREQUENCY,
508 .name = "Power Line Frequency",
509 .entity = UVC_GUID_UVC_PROCESSING,
510 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
513 .v4l2_type = V4L2_CTRL_TYPE_MENU,
514 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
515 .menu_info = power_line_frequency_controls,
516 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
519 .id = V4L2_CID_HUE_AUTO,
521 .entity = UVC_GUID_UVC_PROCESSING,
522 .selector = UVC_PU_HUE_AUTO_CONTROL,
525 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
526 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
527 .slave_ids = { V4L2_CID_HUE, },
530 .id = V4L2_CID_EXPOSURE_AUTO,
531 .name = "Exposure, Auto",
532 .entity = UVC_GUID_UVC_CAMERA,
533 .selector = UVC_CT_AE_MODE_CONTROL,
536 .v4l2_type = V4L2_CTRL_TYPE_MENU,
537 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
538 .menu_info = exposure_auto_controls,
539 .menu_count = ARRAY_SIZE(exposure_auto_controls),
540 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
543 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
544 .name = "Exposure, Auto Priority",
545 .entity = UVC_GUID_UVC_CAMERA,
546 .selector = UVC_CT_AE_PRIORITY_CONTROL,
549 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
550 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
553 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
554 .name = "Exposure (Absolute)",
555 .entity = UVC_GUID_UVC_CAMERA,
556 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
559 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
560 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
561 .master_id = V4L2_CID_EXPOSURE_AUTO,
562 .master_manual = V4L2_EXPOSURE_MANUAL,
565 .id = V4L2_CID_AUTO_WHITE_BALANCE,
566 .name = "White Balance Temperature, Auto",
567 .entity = UVC_GUID_UVC_PROCESSING,
568 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
571 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
572 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
573 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
576 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
577 .name = "White Balance Temperature",
578 .entity = UVC_GUID_UVC_PROCESSING,
579 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
582 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
583 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
584 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
588 .id = V4L2_CID_AUTO_WHITE_BALANCE,
589 .name = "White Balance Component, Auto",
590 .entity = UVC_GUID_UVC_PROCESSING,
591 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
594 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
595 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
596 .slave_ids = { V4L2_CID_BLUE_BALANCE,
597 V4L2_CID_RED_BALANCE },
600 .id = V4L2_CID_BLUE_BALANCE,
601 .name = "White Balance Blue Component",
602 .entity = UVC_GUID_UVC_PROCESSING,
603 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
606 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
607 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
608 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
612 .id = V4L2_CID_RED_BALANCE,
613 .name = "White Balance Red Component",
614 .entity = UVC_GUID_UVC_PROCESSING,
615 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
618 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
619 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
620 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
624 .id = V4L2_CID_FOCUS_ABSOLUTE,
625 .name = "Focus (absolute)",
626 .entity = UVC_GUID_UVC_CAMERA,
627 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
630 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
631 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
632 .master_id = V4L2_CID_FOCUS_AUTO,
636 .id = V4L2_CID_FOCUS_AUTO,
637 .name = "Focus, Auto",
638 .entity = UVC_GUID_UVC_CAMERA,
639 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
642 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
643 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
644 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
647 .id = V4L2_CID_IRIS_ABSOLUTE,
648 .name = "Iris, Absolute",
649 .entity = UVC_GUID_UVC_CAMERA,
650 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
653 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
654 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
657 .id = V4L2_CID_IRIS_RELATIVE,
658 .name = "Iris, Relative",
659 .entity = UVC_GUID_UVC_CAMERA,
660 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
663 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
664 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
667 .id = V4L2_CID_ZOOM_ABSOLUTE,
668 .name = "Zoom, Absolute",
669 .entity = UVC_GUID_UVC_CAMERA,
670 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
673 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
674 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
677 .id = V4L2_CID_ZOOM_CONTINUOUS,
678 .name = "Zoom, Continuous",
679 .entity = UVC_GUID_UVC_CAMERA,
680 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
683 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
684 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
685 .get = uvc_ctrl_get_zoom,
686 .set = uvc_ctrl_set_zoom,
689 .id = V4L2_CID_PAN_ABSOLUTE,
690 .name = "Pan (Absolute)",
691 .entity = UVC_GUID_UVC_CAMERA,
692 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
695 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
696 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
699 .id = V4L2_CID_TILT_ABSOLUTE,
700 .name = "Tilt (Absolute)",
701 .entity = UVC_GUID_UVC_CAMERA,
702 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
705 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
706 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
709 .id = V4L2_CID_PAN_SPEED,
710 .name = "Pan (Speed)",
711 .entity = UVC_GUID_UVC_CAMERA,
712 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
715 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
716 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
717 .get = uvc_ctrl_get_rel_speed,
718 .set = uvc_ctrl_set_rel_speed,
721 .id = V4L2_CID_TILT_SPEED,
722 .name = "Tilt (Speed)",
723 .entity = UVC_GUID_UVC_CAMERA,
724 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
727 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
728 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
729 .get = uvc_ctrl_get_rel_speed,
730 .set = uvc_ctrl_set_rel_speed,
733 .id = V4L2_CID_PRIVACY,
735 .entity = UVC_GUID_UVC_CAMERA,
736 .selector = UVC_CT_PRIVACY_CONTROL,
739 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
740 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
744 /* ------------------------------------------------------------------------
748 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
750 return ctrl->uvc_data + id * ctrl->info.size;
753 static inline int uvc_test_bit(const __u8 *data, int bit)
755 return (data[bit >> 3] >> (bit & 7)) & 1;
758 static inline void uvc_clear_bit(__u8 *data, int bit)
760 data[bit >> 3] &= ~(1 << (bit & 7));
763 /* Extract the bit string specified by mapping->offset and mapping->size
764 * from the little-endian data stored at 'data' and return the result as
765 * a signed 32bit integer. Sign extension will be performed if the mapping
766 * references a signed data type.
768 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
769 __u8 query, const __u8 *data)
771 int bits = mapping->size;
772 int offset = mapping->offset;
778 mask = ((1LL << bits) - 1) << offset;
780 for (; bits > 0; data++) {
781 __u8 byte = *data & mask;
782 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
783 bits -= 8 - (offset > 0 ? offset : 0);
785 mask = (1 << bits) - 1;
788 /* Sign-extend the value if needed. */
789 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
790 value |= -(value & (1 << (mapping->size - 1)));
795 /* Set the bit string specified by mapping->offset and mapping->size
796 * in the little-endian data stored at 'data' to the value 'value'.
798 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
799 __s32 value, __u8 *data)
801 int bits = mapping->size;
802 int offset = mapping->offset;
805 /* According to the v4l2 spec, writing any value to a button control
806 * should result in the action belonging to the button control being
807 * triggered. UVC devices however want to see a 1 written -> override
810 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
816 for (; bits > 0; data++) {
817 mask = ((1LL << bits) - 1) << offset;
818 *data = (*data & ~mask) | ((value << offset) & mask);
819 value >>= offset ? offset : 8;
825 /* ------------------------------------------------------------------------
826 * Terminal and unit management
829 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
830 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
831 static const __u8 uvc_media_transport_input_guid[16] =
832 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
834 static int uvc_entity_match_guid(const struct uvc_entity *entity,
837 switch (UVC_ENTITY_TYPE(entity)) {
839 return memcmp(uvc_camera_guid, guid, 16) == 0;
841 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
842 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
844 case UVC_VC_PROCESSING_UNIT:
845 return memcmp(uvc_processing_guid, guid, 16) == 0;
847 case UVC_VC_EXTENSION_UNIT:
848 return memcmp(entity->extension.guidExtensionCode,
856 /* ------------------------------------------------------------------------
860 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
861 struct uvc_control_mapping **mapping, struct uvc_control **control,
864 struct uvc_control *ctrl;
865 struct uvc_control_mapping *map;
871 for (i = 0; i < entity->ncontrols; ++i) {
872 ctrl = &entity->controls[i];
873 if (!ctrl->initialized)
876 list_for_each_entry(map, &ctrl->info.mappings, list) {
877 if ((map->id == v4l2_id) && !next) {
883 if ((*mapping == NULL || (*mapping)->id > map->id) &&
884 (map->id > v4l2_id) && next) {
892 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
893 __u32 v4l2_id, struct uvc_control_mapping **mapping)
895 struct uvc_control *ctrl = NULL;
896 struct uvc_entity *entity;
897 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
901 /* Mask the query flags. */
902 v4l2_id &= V4L2_CTRL_ID_MASK;
904 /* Find the control. */
905 list_for_each_entry(entity, &chain->entities, chain) {
906 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
911 if (ctrl == NULL && !next)
912 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
918 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
919 struct uvc_control *ctrl)
923 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
924 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
925 chain->dev->intfnum, ctrl->info.selector,
926 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
932 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
933 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
934 chain->dev->intfnum, ctrl->info.selector,
935 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
940 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
941 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
942 chain->dev->intfnum, ctrl->info.selector,
943 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
948 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
949 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
950 chain->dev->intfnum, ctrl->info.selector,
951 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
954 if (UVC_ENTITY_TYPE(ctrl->entity) !=
955 UVC_VC_EXTENSION_UNIT)
958 /* GET_RES is mandatory for XU controls, but some
959 * cameras still choke on it. Ignore errors and set the
960 * resolution value to zero.
962 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
963 "UVC non compliance - GET_RES failed on "
964 "an XU control. Enabling workaround.\n");
965 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
974 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
975 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
978 struct uvc_menu_info *menu;
982 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
986 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
987 chain->dev->intfnum, ctrl->info.selector,
988 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
996 *value = mapping->get(mapping, UVC_GET_CUR,
997 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
999 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1000 menu = mapping->menu_info;
1001 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1002 if (menu->value == *value) {
1012 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1013 struct uvc_control *ctrl,
1014 struct uvc_control_mapping *mapping,
1015 struct v4l2_queryctrl *v4l2_ctrl)
1017 struct uvc_control_mapping *master_map = NULL;
1018 struct uvc_control *master_ctrl = NULL;
1019 struct uvc_menu_info *menu;
1022 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
1023 v4l2_ctrl->id = mapping->id;
1024 v4l2_ctrl->type = mapping->v4l2_type;
1025 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
1026 v4l2_ctrl->flags = 0;
1028 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1029 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1030 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1031 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1033 if (mapping->master_id)
1034 __uvc_find_control(ctrl->entity, mapping->master_id,
1035 &master_map, &master_ctrl, 0);
1036 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1038 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1042 if (val != mapping->master_manual)
1043 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1046 if (!ctrl->cached) {
1047 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1052 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1053 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1054 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1057 switch (mapping->v4l2_type) {
1058 case V4L2_CTRL_TYPE_MENU:
1059 v4l2_ctrl->minimum = 0;
1060 v4l2_ctrl->maximum = mapping->menu_count - 1;
1061 v4l2_ctrl->step = 1;
1063 menu = mapping->menu_info;
1064 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1065 if (menu->value == v4l2_ctrl->default_value) {
1066 v4l2_ctrl->default_value = i;
1073 case V4L2_CTRL_TYPE_BOOLEAN:
1074 v4l2_ctrl->minimum = 0;
1075 v4l2_ctrl->maximum = 1;
1076 v4l2_ctrl->step = 1;
1079 case V4L2_CTRL_TYPE_BUTTON:
1080 v4l2_ctrl->minimum = 0;
1081 v4l2_ctrl->maximum = 0;
1082 v4l2_ctrl->step = 0;
1089 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1090 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1091 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1093 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1094 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1095 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1097 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1098 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1099 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1104 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1105 struct v4l2_queryctrl *v4l2_ctrl)
1107 struct uvc_control *ctrl;
1108 struct uvc_control_mapping *mapping;
1111 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1113 return -ERESTARTSYS;
1115 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1121 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1123 mutex_unlock(&chain->ctrl_mutex);
1128 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
1129 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1130 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1131 * Balance V4L2 controls use the White Balance Component UVC control) or
1132 * otherwise translated. The approach we take here is to use a translation
1133 * table for the controls that can be mapped directly, and handle the others
1136 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1137 struct v4l2_querymenu *query_menu)
1139 struct uvc_menu_info *menu_info;
1140 struct uvc_control_mapping *mapping;
1141 struct uvc_control *ctrl;
1142 u32 index = query_menu->index;
1143 u32 id = query_menu->id;
1146 memset(query_menu, 0, sizeof(*query_menu));
1147 query_menu->id = id;
1148 query_menu->index = index;
1150 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1152 return -ERESTARTSYS;
1154 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1155 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1160 if (query_menu->index >= mapping->menu_count) {
1165 menu_info = &mapping->menu_info[query_menu->index];
1167 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1168 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1171 if (!ctrl->cached) {
1172 ret = uvc_ctrl_populate_cache(chain, ctrl);
1177 bitmap = mapping->get(mapping, UVC_GET_RES,
1178 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1179 if (!(bitmap & menu_info->value)) {
1185 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
1188 mutex_unlock(&chain->ctrl_mutex);
1192 /* --------------------------------------------------------------------------
1193 * Ctrl event handling
1196 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1197 struct v4l2_event *ev,
1198 struct uvc_control *ctrl,
1199 struct uvc_control_mapping *mapping,
1200 s32 value, u32 changes)
1202 struct v4l2_queryctrl v4l2_ctrl;
1204 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1206 memset(ev, 0, sizeof(*ev));
1207 ev->type = V4L2_EVENT_CTRL;
1208 ev->id = v4l2_ctrl.id;
1209 ev->u.ctrl.value = value;
1210 ev->u.ctrl.changes = changes;
1211 ev->u.ctrl.type = v4l2_ctrl.type;
1212 ev->u.ctrl.flags = v4l2_ctrl.flags;
1213 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1214 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1215 ev->u.ctrl.step = v4l2_ctrl.step;
1216 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1219 static void uvc_ctrl_send_event(struct uvc_fh *handle,
1220 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
1221 s32 value, u32 changes)
1223 struct v4l2_subscribed_event *sev;
1224 struct v4l2_event ev;
1226 if (list_empty(&mapping->ev_subs))
1229 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes);
1231 list_for_each_entry(sev, &mapping->ev_subs, node) {
1232 if (sev->fh && (sev->fh != &handle->vfh ||
1233 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1234 (changes & V4L2_EVENT_CTRL_CH_FLAGS)))
1235 v4l2_event_queue_fh(sev->fh, &ev);
1239 static void uvc_ctrl_send_slave_event(struct uvc_fh *handle,
1240 struct uvc_control *master, u32 slave_id,
1241 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1243 struct uvc_control_mapping *mapping = NULL;
1244 struct uvc_control *ctrl = NULL;
1245 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1250 * We can skip sending an event for the slave if the slave
1251 * is being modified in the same transaction.
1253 for (i = 0; i < xctrls_count; i++) {
1254 if (xctrls[i].id == slave_id)
1258 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1262 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1263 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1265 uvc_ctrl_send_event(handle, ctrl, mapping, val, changes);
1268 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1269 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1271 struct uvc_control_mapping *mapping;
1272 struct uvc_control *ctrl;
1273 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1277 for (i = 0; i < xctrls_count; ++i) {
1278 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1280 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1281 if (!mapping->slave_ids[j])
1283 uvc_ctrl_send_slave_event(handle, ctrl,
1284 mapping->slave_ids[j],
1285 xctrls, xctrls_count);
1289 * If the master is being modified in the same transaction
1290 * flags may change too.
1292 if (mapping->master_id) {
1293 for (j = 0; j < xctrls_count; j++) {
1294 if (xctrls[j].id == mapping->master_id) {
1295 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1301 uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
1306 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1308 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1309 struct uvc_control_mapping *mapping;
1310 struct uvc_control *ctrl;
1313 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1315 return -ERESTARTSYS;
1317 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1323 list_add_tail(&sev->node, &mapping->ev_subs);
1324 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1325 struct v4l2_event ev;
1326 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1329 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1330 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1332 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1334 /* Mark the queue as active, allowing this initial
1335 event to be accepted. */
1337 v4l2_event_queue_fh(sev->fh, &ev);
1341 mutex_unlock(&handle->chain->ctrl_mutex);
1345 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1347 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1349 mutex_lock(&handle->chain->ctrl_mutex);
1350 list_del(&sev->node);
1351 mutex_unlock(&handle->chain->ctrl_mutex);
1354 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1355 .add = uvc_ctrl_add_event,
1356 .del = uvc_ctrl_del_event,
1357 .replace = v4l2_ctrl_replace,
1358 .merge = v4l2_ctrl_merge,
1361 /* --------------------------------------------------------------------------
1362 * Control transactions
1364 * To make extended set operations as atomic as the hardware allows, controls
1365 * are handled using begin/commit/rollback operations.
1367 * At the beginning of a set request, uvc_ctrl_begin should be called to
1368 * initialize the request. This function acquires the control lock.
1370 * When setting a control, the new value is stored in the control data field
1371 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1372 * later processing. If the UVC and V4L2 control sizes differ, the current
1373 * value is loaded from the hardware before storing the new value in the data
1376 * After processing all controls in the transaction, uvc_ctrl_commit or
1377 * uvc_ctrl_rollback must be called to apply the pending changes to the
1378 * hardware or revert them. When applying changes, all controls marked as
1379 * dirty will be modified in the UVC device, and the dirty flag will be
1380 * cleared. When reverting controls, the control data field
1381 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1382 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1385 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1387 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1390 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1391 struct uvc_entity *entity, int rollback)
1393 struct uvc_control *ctrl;
1400 for (i = 0; i < entity->ncontrols; ++i) {
1401 ctrl = &entity->controls[i];
1402 if (!ctrl->initialized)
1405 /* Reset the loaded flag for auto-update controls that were
1406 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1407 * uvc_ctrl_get from using the cached value, and for write-only
1408 * controls to prevent uvc_ctrl_set from setting bits not
1409 * explicitly set by the user.
1411 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1412 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1419 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1420 dev->intfnum, ctrl->info.selector,
1421 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1426 if (rollback || ret < 0)
1427 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1428 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1440 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1441 const struct v4l2_ext_control *xctrls,
1442 unsigned int xctrls_count)
1444 struct uvc_video_chain *chain = handle->chain;
1445 struct uvc_entity *entity;
1448 /* Find the control. */
1449 list_for_each_entry(entity, &chain->entities, chain) {
1450 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1456 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1458 mutex_unlock(&chain->ctrl_mutex);
1462 int uvc_ctrl_get(struct uvc_video_chain *chain,
1463 struct v4l2_ext_control *xctrl)
1465 struct uvc_control *ctrl;
1466 struct uvc_control_mapping *mapping;
1468 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1472 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1475 int uvc_ctrl_set(struct uvc_video_chain *chain,
1476 struct v4l2_ext_control *xctrl)
1478 struct uvc_control *ctrl;
1479 struct uvc_control_mapping *mapping;
1486 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1489 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1492 /* Clamp out of range values. */
1493 switch (mapping->v4l2_type) {
1494 case V4L2_CTRL_TYPE_INTEGER:
1495 if (!ctrl->cached) {
1496 ret = uvc_ctrl_populate_cache(chain, ctrl);
1501 min = mapping->get(mapping, UVC_GET_MIN,
1502 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1503 max = mapping->get(mapping, UVC_GET_MAX,
1504 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1505 step = mapping->get(mapping, UVC_GET_RES,
1506 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1510 xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
1512 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1513 xctrl->value = clamp(xctrl->value, min, max);
1515 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1516 value = xctrl->value;
1519 case V4L2_CTRL_TYPE_BOOLEAN:
1520 xctrl->value = clamp(xctrl->value, 0, 1);
1521 value = xctrl->value;
1524 case V4L2_CTRL_TYPE_MENU:
1525 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1527 value = mapping->menu_info[xctrl->value].value;
1529 /* Valid menu indices are reported by the GET_RES request for
1530 * UVC controls that support it.
1532 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1533 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1534 if (!ctrl->cached) {
1535 ret = uvc_ctrl_populate_cache(chain, ctrl);
1540 step = mapping->get(mapping, UVC_GET_RES,
1541 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1542 if (!(step & value))
1549 value = xctrl->value;
1553 /* If the mapping doesn't span the whole UVC control, the current value
1554 * needs to be loaded from the device to perform the read-modify-write
1557 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1558 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1559 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1560 0, ctrl->info.size);
1562 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1563 ctrl->entity->id, chain->dev->intfnum,
1564 ctrl->info.selector,
1565 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1574 /* Backup the current value in case we need to rollback later. */
1576 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1577 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1581 mapping->set(mapping, value,
1582 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1589 /* --------------------------------------------------------------------------
1593 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1594 const struct uvc_control *ctrl, struct uvc_control_info *info)
1596 struct uvc_ctrl_fixup {
1597 struct usb_device_id id;
1603 static const struct uvc_ctrl_fixup fixups[] = {
1604 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1605 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1606 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1607 UVC_CTRL_FLAG_AUTO_UPDATE },
1608 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1609 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1610 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1611 UVC_CTRL_FLAG_AUTO_UPDATE },
1612 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1613 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1614 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1615 UVC_CTRL_FLAG_AUTO_UPDATE },
1620 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1621 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1624 if (fixups[i].entity == ctrl->entity->id &&
1625 fixups[i].selector == info->selector) {
1626 info->flags = fixups[i].flags;
1633 * Query control information (size and flags) for XU controls.
1635 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1636 const struct uvc_control *ctrl, struct uvc_control_info *info)
1641 data = kmalloc(2, GFP_KERNEL);
1645 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1646 sizeof(info->entity));
1647 info->index = ctrl->index;
1648 info->selector = ctrl->index + 1;
1650 /* Query and verify the control length (GET_LEN) */
1651 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1652 info->selector, data, 2);
1654 uvc_trace(UVC_TRACE_CONTROL,
1655 "GET_LEN failed on control %pUl/%u (%d).\n",
1656 info->entity, info->selector, ret);
1660 info->size = le16_to_cpup((__le16 *)data);
1662 /* Query the control information (GET_INFO) */
1663 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1664 info->selector, data, 1);
1666 uvc_trace(UVC_TRACE_CONTROL,
1667 "GET_INFO failed on control %pUl/%u (%d).\n",
1668 info->entity, info->selector, ret);
1672 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1673 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1674 | (data[0] & UVC_CONTROL_CAP_GET ?
1675 UVC_CTRL_FLAG_GET_CUR : 0)
1676 | (data[0] & UVC_CONTROL_CAP_SET ?
1677 UVC_CTRL_FLAG_SET_CUR : 0)
1678 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1679 UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1681 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1683 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1684 "flags { get %u set %u auto %u }.\n",
1685 info->entity, info->selector, info->size,
1686 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1687 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1688 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1695 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1696 const struct uvc_control_info *info);
1698 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1699 struct uvc_control *ctrl)
1701 struct uvc_control_info info;
1704 if (ctrl->initialized)
1707 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1711 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1713 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1714 "%pUl/%u on device %s entity %u\n", info.entity,
1715 info.selector, dev->udev->devpath, ctrl->entity->id);
1720 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1721 struct uvc_xu_control_query *xqry)
1723 struct uvc_entity *entity;
1724 struct uvc_control *ctrl;
1725 unsigned int i, found = 0;
1731 /* Find the extension unit. */
1732 list_for_each_entry(entity, &chain->entities, chain) {
1733 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1734 entity->id == xqry->unit)
1738 if (entity->id != xqry->unit) {
1739 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1744 /* Find the control and perform delayed initialization if needed. */
1745 for (i = 0; i < entity->ncontrols; ++i) {
1746 ctrl = &entity->controls[i];
1747 if (ctrl->index == xqry->selector - 1) {
1754 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1755 entity->extension.guidExtensionCode, xqry->selector);
1759 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1760 return -ERESTARTSYS;
1762 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1768 /* Validate the required buffer size and flags for the request */
1770 size = ctrl->info.size;
1772 switch (xqry->query) {
1774 reqflags = UVC_CTRL_FLAG_GET_CUR;
1777 reqflags = UVC_CTRL_FLAG_GET_MIN;
1780 reqflags = UVC_CTRL_FLAG_GET_MAX;
1783 reqflags = UVC_CTRL_FLAG_GET_DEF;
1786 reqflags = UVC_CTRL_FLAG_GET_RES;
1789 reqflags = UVC_CTRL_FLAG_SET_CUR;
1802 if (size != xqry->size) {
1807 if (reqflags && !(ctrl->info.flags & reqflags)) {
1812 data = kmalloc(size, GFP_KERNEL);
1818 if (xqry->query == UVC_SET_CUR &&
1819 copy_from_user(data, xqry->data, size)) {
1824 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1825 chain->dev->intfnum, xqry->selector, data, size);
1829 if (xqry->query != UVC_SET_CUR &&
1830 copy_to_user(xqry->data, data, size))
1834 mutex_unlock(&chain->ctrl_mutex);
1838 /* --------------------------------------------------------------------------
1843 * Restore control values after resume, skipping controls that haven't been
1847 * - Don't restore modified controls that are back to their default value.
1848 * - Handle restore order (Auto-Exposure Mode should be restored before
1851 int uvc_ctrl_restore_values(struct uvc_device *dev)
1853 struct uvc_control *ctrl;
1854 struct uvc_entity *entity;
1858 /* Walk the entities list and restore controls when possible. */
1859 list_for_each_entry(entity, &dev->entities, list) {
1861 for (i = 0; i < entity->ncontrols; ++i) {
1862 ctrl = &entity->controls[i];
1864 if (!ctrl->initialized || !ctrl->modified ||
1865 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1868 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1869 ctrl->info.entity, ctrl->info.index,
1870 ctrl->info.selector);
1874 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1882 /* --------------------------------------------------------------------------
1883 * Control and mapping handling
1887 * Add control information to a given control.
1889 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1890 const struct uvc_control_info *info)
1895 INIT_LIST_HEAD(&ctrl->info.mappings);
1897 /* Allocate an array to save control values (cur, def, max, etc.) */
1898 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1900 if (ctrl->uvc_data == NULL) {
1905 ctrl->initialized = 1;
1907 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1908 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
1909 dev->udev->devpath, ctrl->entity->id);
1913 kfree(ctrl->uvc_data);
1918 * Add a control mapping to a given control.
1920 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1921 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1923 struct uvc_control_mapping *map;
1926 /* Most mappings come from static kernel data and need to be duplicated.
1927 * Mappings that come from userspace will be unnecessarily duplicated,
1928 * this could be optimized.
1930 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1934 INIT_LIST_HEAD(&map->ev_subs);
1936 size = sizeof(*mapping->menu_info) * mapping->menu_count;
1937 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1938 if (map->menu_info == NULL) {
1943 if (map->get == NULL)
1944 map->get = uvc_get_le_value;
1945 if (map->set == NULL)
1946 map->set = uvc_set_le_value;
1948 list_add_tail(&map->list, &ctrl->info.mappings);
1949 uvc_trace(UVC_TRACE_CONTROL,
1950 "Adding mapping '%s' to control %pUl/%u.\n",
1951 map->name, ctrl->info.entity, ctrl->info.selector);
1956 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1957 const struct uvc_control_mapping *mapping)
1959 struct uvc_device *dev = chain->dev;
1960 struct uvc_control_mapping *map;
1961 struct uvc_entity *entity;
1962 struct uvc_control *ctrl;
1966 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1967 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1968 "id 0x%08x is invalid.\n", mapping->name,
1973 /* Search for the matching (GUID/CS) control on the current chain */
1974 list_for_each_entry(entity, &chain->entities, chain) {
1977 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1978 !uvc_entity_match_guid(entity, mapping->entity))
1981 for (i = 0; i < entity->ncontrols; ++i) {
1982 ctrl = &entity->controls[i];
1983 if (ctrl->index == mapping->selector - 1) {
1995 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1996 return -ERESTARTSYS;
1998 /* Perform delayed initialization of XU controls */
1999 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2005 /* Validate the user-provided bit-size and offset */
2006 if (mapping->size > 32 ||
2007 mapping->offset + mapping->size > ctrl->info.size * 8) {
2012 list_for_each_entry(map, &ctrl->info.mappings, list) {
2013 if (mapping->id == map->id) {
2014 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
2015 "control id 0x%08x already exists.\n",
2016 mapping->name, mapping->id);
2022 /* Prevent excess memory consumption */
2023 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2024 atomic_dec(&dev->nmappings);
2025 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
2026 "mappings count (%u) exceeded.\n", mapping->name,
2027 UVC_MAX_CONTROL_MAPPINGS);
2032 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2034 atomic_dec(&dev->nmappings);
2037 mutex_unlock(&chain->ctrl_mutex);
2042 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2043 * are currently the ones that crash the camera or unconditionally return an
2044 * error when queried.
2046 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2047 struct uvc_entity *entity)
2049 struct uvc_ctrl_blacklist {
2050 struct usb_device_id id;
2054 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2055 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2056 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2057 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2059 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2060 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2063 const struct uvc_ctrl_blacklist *blacklist;
2069 switch (UVC_ENTITY_TYPE(entity)) {
2070 case UVC_VC_PROCESSING_UNIT:
2071 blacklist = processing_blacklist;
2072 count = ARRAY_SIZE(processing_blacklist);
2073 controls = entity->processing.bmControls;
2074 size = entity->processing.bControlSize;
2077 case UVC_ITT_CAMERA:
2078 blacklist = camera_blacklist;
2079 count = ARRAY_SIZE(camera_blacklist);
2080 controls = entity->camera.bmControls;
2081 size = entity->camera.bControlSize;
2088 for (i = 0; i < count; ++i) {
2089 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2092 if (blacklist[i].index >= 8 * size ||
2093 !uvc_test_bit(controls, blacklist[i].index))
2096 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
2097 "removing it.\n", entity->id, blacklist[i].index);
2099 uvc_clear_bit(controls, blacklist[i].index);
2104 * Add control information and hardcoded stock control mappings to the given
2107 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2109 const struct uvc_control_info *info = uvc_ctrls;
2110 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2111 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2112 const struct uvc_control_mapping *mend =
2113 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2115 /* XU controls initialization requires querying the device for control
2116 * information. As some buggy UVC devices will crash when queried
2117 * repeatedly in a tight loop, delay XU controls initialization until
2120 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2123 for (; info < iend; ++info) {
2124 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2125 ctrl->index == info->index) {
2126 uvc_ctrl_add_info(dev, ctrl, info);
2131 if (!ctrl->initialized)
2134 for (; mapping < mend; ++mapping) {
2135 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2136 ctrl->info.selector == mapping->selector)
2137 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2142 * Initialize device controls.
2144 int uvc_ctrl_init_device(struct uvc_device *dev)
2146 struct uvc_entity *entity;
2149 /* Walk the entities list and instantiate controls */
2150 list_for_each_entry(entity, &dev->entities, list) {
2151 struct uvc_control *ctrl;
2152 unsigned int bControlSize = 0, ncontrols;
2153 __u8 *bmControls = NULL;
2155 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2156 bmControls = entity->extension.bmControls;
2157 bControlSize = entity->extension.bControlSize;
2158 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2159 bmControls = entity->processing.bmControls;
2160 bControlSize = entity->processing.bControlSize;
2161 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2162 bmControls = entity->camera.bmControls;
2163 bControlSize = entity->camera.bControlSize;
2166 /* Remove bogus/blacklisted controls */
2167 uvc_ctrl_prune_entity(dev, entity);
2169 /* Count supported controls and allocate the controls array */
2170 ncontrols = memweight(bmControls, bControlSize);
2174 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2176 if (entity->controls == NULL)
2178 entity->ncontrols = ncontrols;
2180 /* Initialize all supported controls */
2181 ctrl = entity->controls;
2182 for (i = 0; i < bControlSize * 8; ++i) {
2183 if (uvc_test_bit(bmControls, i) == 0)
2186 ctrl->entity = entity;
2189 uvc_ctrl_init_ctrl(dev, ctrl);
2198 * Cleanup device controls.
2200 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2201 struct uvc_control *ctrl)
2203 struct uvc_control_mapping *mapping, *nm;
2205 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2206 list_del(&mapping->list);
2207 kfree(mapping->menu_info);
2212 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2214 struct uvc_entity *entity;
2217 /* Free controls and control mappings for all entities. */
2218 list_for_each_entry(entity, &dev->entities, list) {
2219 for (i = 0; i < entity->ncontrols; ++i) {
2220 struct uvc_control *ctrl = &entity->controls[i];
2222 if (!ctrl->initialized)
2225 uvc_ctrl_cleanup_mappings(dev, ctrl);
2226 kfree(ctrl->uvc_data);
2229 kfree(entity->controls);