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,
233 .entity = UVC_GUID_UVC_CAMERA,
234 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
237 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
240 .entity = UVC_GUID_UVC_CAMERA,
241 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
244 .flags = UVC_CTRL_FLAG_SET_CUR
245 | UVC_CTRL_FLAG_GET_RANGE
246 | UVC_CTRL_FLAG_RESTORE
247 | UVC_CTRL_FLAG_AUTO_UPDATE,
250 .entity = UVC_GUID_UVC_CAMERA,
251 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
254 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
255 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
256 | UVC_CTRL_FLAG_GET_DEF
257 | UVC_CTRL_FLAG_AUTO_UPDATE,
260 .entity = UVC_GUID_UVC_CAMERA,
261 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
264 .flags = UVC_CTRL_FLAG_SET_CUR
265 | UVC_CTRL_FLAG_GET_RANGE
266 | UVC_CTRL_FLAG_RESTORE
267 | UVC_CTRL_FLAG_AUTO_UPDATE,
270 .entity = UVC_GUID_UVC_CAMERA,
271 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
274 .flags = UVC_CTRL_FLAG_SET_CUR
275 | UVC_CTRL_FLAG_AUTO_UPDATE,
278 .entity = UVC_GUID_UVC_CAMERA,
279 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
282 .flags = UVC_CTRL_FLAG_SET_CUR
283 | UVC_CTRL_FLAG_GET_RANGE
284 | UVC_CTRL_FLAG_RESTORE
285 | UVC_CTRL_FLAG_AUTO_UPDATE,
288 .entity = UVC_GUID_UVC_CAMERA,
289 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
292 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
293 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
294 | UVC_CTRL_FLAG_GET_DEF
295 | UVC_CTRL_FLAG_AUTO_UPDATE,
298 .entity = UVC_GUID_UVC_CAMERA,
299 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
302 .flags = UVC_CTRL_FLAG_SET_CUR
303 | UVC_CTRL_FLAG_GET_RANGE
304 | UVC_CTRL_FLAG_RESTORE
305 | UVC_CTRL_FLAG_AUTO_UPDATE,
308 .entity = UVC_GUID_UVC_CAMERA,
309 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
312 .flags = UVC_CTRL_FLAG_SET_CUR
313 | UVC_CTRL_FLAG_GET_RANGE
314 | UVC_CTRL_FLAG_AUTO_UPDATE,
317 .entity = UVC_GUID_UVC_CAMERA,
318 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
321 .flags = UVC_CTRL_FLAG_SET_CUR
322 | UVC_CTRL_FLAG_GET_RANGE
323 | UVC_CTRL_FLAG_RESTORE
324 | UVC_CTRL_FLAG_AUTO_UPDATE,
327 .entity = UVC_GUID_UVC_CAMERA,
328 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
331 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
332 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
333 | UVC_CTRL_FLAG_GET_DEF
334 | UVC_CTRL_FLAG_AUTO_UPDATE,
337 .entity = UVC_GUID_UVC_CAMERA,
338 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
341 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
342 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
345 .entity = UVC_GUID_UVC_CAMERA,
346 .selector = UVC_CT_PRIVACY_CONTROL,
349 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
350 | UVC_CTRL_FLAG_RESTORE
351 | UVC_CTRL_FLAG_AUTO_UPDATE,
355 static struct uvc_menu_info power_line_frequency_controls[] = {
361 static struct uvc_menu_info exposure_auto_controls[] = {
363 { 1, "Manual Mode" },
364 { 4, "Shutter Priority Mode" },
365 { 8, "Aperture Priority Mode" },
368 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
369 __u8 query, const __u8 *data)
371 __s8 zoom = (__s8)data[0];
375 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
386 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
387 __s32 value, __u8 *data)
389 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
390 data[2] = min((int)abs(value), 0xff);
393 static __s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
394 __u8 query, const __u8 *data)
396 unsigned int first = mapping->offset / 8;
397 __s8 rel = (__s8)data[first];
401 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
404 return -data[first+1];
409 return data[first+1];
413 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
414 __s32 value, __u8 *data)
416 unsigned int first = mapping->offset / 8;
418 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
419 data[first+1] = min_t(int, abs(value), 0xff);
422 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
424 .id = V4L2_CID_BRIGHTNESS,
425 .name = "Brightness",
426 .entity = UVC_GUID_UVC_PROCESSING,
427 .selector = UVC_PU_BRIGHTNESS_CONTROL,
430 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
431 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
434 .id = V4L2_CID_CONTRAST,
436 .entity = UVC_GUID_UVC_PROCESSING,
437 .selector = UVC_PU_CONTRAST_CONTROL,
440 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
441 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
446 .entity = UVC_GUID_UVC_PROCESSING,
447 .selector = UVC_PU_HUE_CONTROL,
450 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
451 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
452 .master_id = V4L2_CID_HUE_AUTO,
456 .id = V4L2_CID_SATURATION,
457 .name = "Saturation",
458 .entity = UVC_GUID_UVC_PROCESSING,
459 .selector = UVC_PU_SATURATION_CONTROL,
462 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
463 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
466 .id = V4L2_CID_SHARPNESS,
468 .entity = UVC_GUID_UVC_PROCESSING,
469 .selector = UVC_PU_SHARPNESS_CONTROL,
472 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
473 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
476 .id = V4L2_CID_GAMMA,
478 .entity = UVC_GUID_UVC_PROCESSING,
479 .selector = UVC_PU_GAMMA_CONTROL,
482 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
483 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
486 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
487 .name = "Backlight Compensation",
488 .entity = UVC_GUID_UVC_PROCESSING,
489 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
492 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
493 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
498 .entity = UVC_GUID_UVC_PROCESSING,
499 .selector = UVC_PU_GAIN_CONTROL,
502 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
503 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
506 .id = V4L2_CID_POWER_LINE_FREQUENCY,
507 .name = "Power Line Frequency",
508 .entity = UVC_GUID_UVC_PROCESSING,
509 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
512 .v4l2_type = V4L2_CTRL_TYPE_MENU,
513 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
514 .menu_info = power_line_frequency_controls,
515 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
518 .id = V4L2_CID_HUE_AUTO,
520 .entity = UVC_GUID_UVC_PROCESSING,
521 .selector = UVC_PU_HUE_AUTO_CONTROL,
524 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
525 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
526 .slave_ids = { V4L2_CID_HUE, },
529 .id = V4L2_CID_EXPOSURE_AUTO,
530 .name = "Exposure, Auto",
531 .entity = UVC_GUID_UVC_CAMERA,
532 .selector = UVC_CT_AE_MODE_CONTROL,
535 .v4l2_type = V4L2_CTRL_TYPE_MENU,
536 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
537 .menu_info = exposure_auto_controls,
538 .menu_count = ARRAY_SIZE(exposure_auto_controls),
539 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
542 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
543 .name = "Exposure, Auto Priority",
544 .entity = UVC_GUID_UVC_CAMERA,
545 .selector = UVC_CT_AE_PRIORITY_CONTROL,
548 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
549 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
552 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
553 .name = "Exposure (Absolute)",
554 .entity = UVC_GUID_UVC_CAMERA,
555 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
558 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
559 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
560 .master_id = V4L2_CID_EXPOSURE_AUTO,
561 .master_manual = V4L2_EXPOSURE_MANUAL,
564 .id = V4L2_CID_AUTO_WHITE_BALANCE,
565 .name = "White Balance Temperature, Auto",
566 .entity = UVC_GUID_UVC_PROCESSING,
567 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
570 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
571 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
572 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
575 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
576 .name = "White Balance Temperature",
577 .entity = UVC_GUID_UVC_PROCESSING,
578 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
581 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
582 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
583 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
587 .id = V4L2_CID_AUTO_WHITE_BALANCE,
588 .name = "White Balance Component, Auto",
589 .entity = UVC_GUID_UVC_PROCESSING,
590 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
593 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
594 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
595 .slave_ids = { V4L2_CID_BLUE_BALANCE,
596 V4L2_CID_RED_BALANCE },
599 .id = V4L2_CID_BLUE_BALANCE,
600 .name = "White Balance Blue Component",
601 .entity = UVC_GUID_UVC_PROCESSING,
602 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
605 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
606 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
607 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
611 .id = V4L2_CID_RED_BALANCE,
612 .name = "White Balance Red Component",
613 .entity = UVC_GUID_UVC_PROCESSING,
614 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
617 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
618 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
619 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
623 .id = V4L2_CID_FOCUS_ABSOLUTE,
624 .name = "Focus (absolute)",
625 .entity = UVC_GUID_UVC_CAMERA,
626 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
629 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
630 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
631 .master_id = V4L2_CID_FOCUS_AUTO,
635 .id = V4L2_CID_FOCUS_AUTO,
636 .name = "Focus, Auto",
637 .entity = UVC_GUID_UVC_CAMERA,
638 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
641 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
642 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
643 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
646 .id = V4L2_CID_IRIS_ABSOLUTE,
647 .name = "Iris, Absolute",
648 .entity = UVC_GUID_UVC_CAMERA,
649 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
652 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
653 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
656 .id = V4L2_CID_IRIS_RELATIVE,
657 .name = "Iris, Relative",
658 .entity = UVC_GUID_UVC_CAMERA,
659 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
662 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
663 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
666 .id = V4L2_CID_ZOOM_ABSOLUTE,
667 .name = "Zoom, Absolute",
668 .entity = UVC_GUID_UVC_CAMERA,
669 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
672 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
673 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
676 .id = V4L2_CID_ZOOM_CONTINUOUS,
677 .name = "Zoom, Continuous",
678 .entity = UVC_GUID_UVC_CAMERA,
679 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
682 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
683 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
684 .get = uvc_ctrl_get_zoom,
685 .set = uvc_ctrl_set_zoom,
688 .id = V4L2_CID_PAN_ABSOLUTE,
689 .name = "Pan (Absolute)",
690 .entity = UVC_GUID_UVC_CAMERA,
691 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
694 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
695 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
698 .id = V4L2_CID_TILT_ABSOLUTE,
699 .name = "Tilt (Absolute)",
700 .entity = UVC_GUID_UVC_CAMERA,
701 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
704 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
705 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
708 .id = V4L2_CID_PAN_SPEED,
709 .name = "Pan (Speed)",
710 .entity = UVC_GUID_UVC_CAMERA,
711 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
714 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
715 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
716 .get = uvc_ctrl_get_rel_speed,
717 .set = uvc_ctrl_set_rel_speed,
720 .id = V4L2_CID_TILT_SPEED,
721 .name = "Tilt (Speed)",
722 .entity = UVC_GUID_UVC_CAMERA,
723 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
726 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
727 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
728 .get = uvc_ctrl_get_rel_speed,
729 .set = uvc_ctrl_set_rel_speed,
732 .id = V4L2_CID_PRIVACY,
734 .entity = UVC_GUID_UVC_CAMERA,
735 .selector = UVC_CT_PRIVACY_CONTROL,
738 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
739 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
743 /* ------------------------------------------------------------------------
747 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
749 return ctrl->uvc_data + id * ctrl->info.size;
752 static inline int uvc_test_bit(const __u8 *data, int bit)
754 return (data[bit >> 3] >> (bit & 7)) & 1;
757 static inline void uvc_clear_bit(__u8 *data, int bit)
759 data[bit >> 3] &= ~(1 << (bit & 7));
762 /* Extract the bit string specified by mapping->offset and mapping->size
763 * from the little-endian data stored at 'data' and return the result as
764 * a signed 32bit integer. Sign extension will be performed if the mapping
765 * references a signed data type.
767 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
768 __u8 query, const __u8 *data)
770 int bits = mapping->size;
771 int offset = mapping->offset;
777 mask = ((1LL << bits) - 1) << offset;
779 for (; bits > 0; data++) {
780 __u8 byte = *data & mask;
781 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
782 bits -= 8 - (offset > 0 ? offset : 0);
784 mask = (1 << bits) - 1;
787 /* Sign-extend the value if needed. */
788 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
789 value |= -(value & (1 << (mapping->size - 1)));
794 /* Set the bit string specified by mapping->offset and mapping->size
795 * in the little-endian data stored at 'data' to the value 'value'.
797 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
798 __s32 value, __u8 *data)
800 int bits = mapping->size;
801 int offset = mapping->offset;
804 /* According to the v4l2 spec, writing any value to a button control
805 * should result in the action belonging to the button control being
806 * triggered. UVC devices however want to see a 1 written -> override
809 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
815 for (; bits > 0; data++) {
816 mask = ((1LL << bits) - 1) << offset;
817 *data = (*data & ~mask) | ((value << offset) & mask);
818 value >>= offset ? offset : 8;
824 /* ------------------------------------------------------------------------
825 * Terminal and unit management
828 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
829 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
830 static const __u8 uvc_media_transport_input_guid[16] =
831 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
833 static int uvc_entity_match_guid(const struct uvc_entity *entity,
836 switch (UVC_ENTITY_TYPE(entity)) {
838 return memcmp(uvc_camera_guid, guid, 16) == 0;
840 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
841 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
843 case UVC_VC_PROCESSING_UNIT:
844 return memcmp(uvc_processing_guid, guid, 16) == 0;
846 case UVC_VC_EXTENSION_UNIT:
847 return memcmp(entity->extension.guidExtensionCode,
855 /* ------------------------------------------------------------------------
859 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
860 struct uvc_control_mapping **mapping, struct uvc_control **control,
863 struct uvc_control *ctrl;
864 struct uvc_control_mapping *map;
870 for (i = 0; i < entity->ncontrols; ++i) {
871 ctrl = &entity->controls[i];
872 if (!ctrl->initialized)
875 list_for_each_entry(map, &ctrl->info.mappings, list) {
876 if ((map->id == v4l2_id) && !next) {
882 if ((*mapping == NULL || (*mapping)->id > map->id) &&
883 (map->id > v4l2_id) && next) {
891 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
892 __u32 v4l2_id, struct uvc_control_mapping **mapping)
894 struct uvc_control *ctrl = NULL;
895 struct uvc_entity *entity;
896 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
900 /* Mask the query flags. */
901 v4l2_id &= V4L2_CTRL_ID_MASK;
903 /* Find the control. */
904 list_for_each_entry(entity, &chain->entities, chain) {
905 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
910 if (ctrl == NULL && !next)
911 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
917 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
918 struct uvc_control *ctrl)
922 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
923 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
924 chain->dev->intfnum, ctrl->info.selector,
925 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
931 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
932 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
933 chain->dev->intfnum, ctrl->info.selector,
934 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
939 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
940 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
941 chain->dev->intfnum, ctrl->info.selector,
942 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
947 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
948 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
949 chain->dev->intfnum, ctrl->info.selector,
950 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
953 if (UVC_ENTITY_TYPE(ctrl->entity) !=
954 UVC_VC_EXTENSION_UNIT)
957 /* GET_RES is mandatory for XU controls, but some
958 * cameras still choke on it. Ignore errors and set the
959 * resolution value to zero.
961 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
962 "UVC non compliance - GET_RES failed on "
963 "an XU control. Enabling workaround.\n");
964 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
973 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
974 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
977 struct uvc_menu_info *menu;
981 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
985 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
986 chain->dev->intfnum, ctrl->info.selector,
987 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
995 *value = mapping->get(mapping, UVC_GET_CUR,
996 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
998 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
999 menu = mapping->menu_info;
1000 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1001 if (menu->value == *value) {
1011 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1012 struct uvc_control *ctrl,
1013 struct uvc_control_mapping *mapping,
1014 struct v4l2_queryctrl *v4l2_ctrl)
1016 struct uvc_control_mapping *master_map = NULL;
1017 struct uvc_control *master_ctrl = NULL;
1018 struct uvc_menu_info *menu;
1021 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
1022 v4l2_ctrl->id = mapping->id;
1023 v4l2_ctrl->type = mapping->v4l2_type;
1024 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
1025 v4l2_ctrl->flags = 0;
1027 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1028 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1029 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1030 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1032 if (mapping->master_id)
1033 __uvc_find_control(ctrl->entity, mapping->master_id,
1034 &master_map, &master_ctrl, 0);
1035 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1037 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1041 if (val != mapping->master_manual)
1042 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1045 if (!ctrl->cached) {
1046 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1051 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1052 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1053 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1056 switch (mapping->v4l2_type) {
1057 case V4L2_CTRL_TYPE_MENU:
1058 v4l2_ctrl->minimum = 0;
1059 v4l2_ctrl->maximum = mapping->menu_count - 1;
1060 v4l2_ctrl->step = 1;
1062 menu = mapping->menu_info;
1063 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1064 if (menu->value == v4l2_ctrl->default_value) {
1065 v4l2_ctrl->default_value = i;
1072 case V4L2_CTRL_TYPE_BOOLEAN:
1073 v4l2_ctrl->minimum = 0;
1074 v4l2_ctrl->maximum = 1;
1075 v4l2_ctrl->step = 1;
1078 case V4L2_CTRL_TYPE_BUTTON:
1079 v4l2_ctrl->minimum = 0;
1080 v4l2_ctrl->maximum = 0;
1081 v4l2_ctrl->step = 0;
1088 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1089 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1090 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1092 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1093 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1094 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1096 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1097 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1098 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1103 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1104 struct v4l2_queryctrl *v4l2_ctrl)
1106 struct uvc_control *ctrl;
1107 struct uvc_control_mapping *mapping;
1110 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1112 return -ERESTARTSYS;
1114 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1120 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1122 mutex_unlock(&chain->ctrl_mutex);
1127 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
1128 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1129 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1130 * Balance V4L2 controls use the White Balance Component UVC control) or
1131 * otherwise translated. The approach we take here is to use a translation
1132 * table for the controls that can be mapped directly, and handle the others
1135 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1136 struct v4l2_querymenu *query_menu)
1138 struct uvc_menu_info *menu_info;
1139 struct uvc_control_mapping *mapping;
1140 struct uvc_control *ctrl;
1141 u32 index = query_menu->index;
1142 u32 id = query_menu->id;
1145 memset(query_menu, 0, sizeof(*query_menu));
1146 query_menu->id = id;
1147 query_menu->index = index;
1149 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1151 return -ERESTARTSYS;
1153 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1154 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1159 if (query_menu->index >= mapping->menu_count) {
1164 menu_info = &mapping->menu_info[query_menu->index];
1166 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1167 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1170 if (!ctrl->cached) {
1171 ret = uvc_ctrl_populate_cache(chain, ctrl);
1176 bitmap = mapping->get(mapping, UVC_GET_RES,
1177 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1178 if (!(bitmap & menu_info->value)) {
1184 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
1187 mutex_unlock(&chain->ctrl_mutex);
1191 /* --------------------------------------------------------------------------
1192 * Ctrl event handling
1195 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1196 struct v4l2_event *ev,
1197 struct uvc_control *ctrl,
1198 struct uvc_control_mapping *mapping,
1199 s32 value, u32 changes)
1201 struct v4l2_queryctrl v4l2_ctrl;
1203 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1205 memset(ev->reserved, 0, sizeof(ev->reserved));
1206 ev->type = V4L2_EVENT_CTRL;
1207 ev->id = v4l2_ctrl.id;
1208 ev->u.ctrl.value = value;
1209 ev->u.ctrl.changes = changes;
1210 ev->u.ctrl.type = v4l2_ctrl.type;
1211 ev->u.ctrl.flags = v4l2_ctrl.flags;
1212 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1213 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1214 ev->u.ctrl.step = v4l2_ctrl.step;
1215 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1218 static void uvc_ctrl_send_event(struct uvc_fh *handle,
1219 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
1220 s32 value, u32 changes)
1222 struct v4l2_subscribed_event *sev;
1223 struct v4l2_event ev;
1225 if (list_empty(&mapping->ev_subs))
1228 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes);
1230 list_for_each_entry(sev, &mapping->ev_subs, node) {
1231 if (sev->fh && (sev->fh != &handle->vfh ||
1232 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1233 (changes & V4L2_EVENT_CTRL_CH_FLAGS)))
1234 v4l2_event_queue_fh(sev->fh, &ev);
1238 static void uvc_ctrl_send_slave_event(struct uvc_fh *handle,
1239 struct uvc_control *master, u32 slave_id,
1240 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1242 struct uvc_control_mapping *mapping = NULL;
1243 struct uvc_control *ctrl = NULL;
1244 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1249 * We can skip sending an event for the slave if the slave
1250 * is being modified in the same transaction.
1252 for (i = 0; i < xctrls_count; i++) {
1253 if (xctrls[i].id == slave_id)
1257 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1261 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1262 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1264 uvc_ctrl_send_event(handle, ctrl, mapping, val, changes);
1267 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1268 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1270 struct uvc_control_mapping *mapping;
1271 struct uvc_control *ctrl;
1272 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1276 for (i = 0; i < xctrls_count; ++i) {
1277 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1279 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1280 if (!mapping->slave_ids[j])
1282 uvc_ctrl_send_slave_event(handle, ctrl,
1283 mapping->slave_ids[j],
1284 xctrls, xctrls_count);
1288 * If the master is being modified in the same transaction
1289 * flags may change too.
1291 if (mapping->master_id) {
1292 for (j = 0; j < xctrls_count; j++) {
1293 if (xctrls[j].id == mapping->master_id) {
1294 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1300 uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
1305 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1307 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1308 struct uvc_control_mapping *mapping;
1309 struct uvc_control *ctrl;
1312 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1314 return -ERESTARTSYS;
1316 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1322 list_add_tail(&sev->node, &mapping->ev_subs);
1323 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1324 struct v4l2_event ev;
1325 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1328 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1329 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1331 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1333 /* Mark the queue as active, allowing this initial
1334 event to be accepted. */
1336 v4l2_event_queue_fh(sev->fh, &ev);
1340 mutex_unlock(&handle->chain->ctrl_mutex);
1344 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1346 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1348 mutex_lock(&handle->chain->ctrl_mutex);
1349 list_del(&sev->node);
1350 mutex_unlock(&handle->chain->ctrl_mutex);
1353 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1354 .add = uvc_ctrl_add_event,
1355 .del = uvc_ctrl_del_event,
1356 .replace = v4l2_ctrl_replace,
1357 .merge = v4l2_ctrl_merge,
1360 /* --------------------------------------------------------------------------
1361 * Control transactions
1363 * To make extended set operations as atomic as the hardware allows, controls
1364 * are handled using begin/commit/rollback operations.
1366 * At the beginning of a set request, uvc_ctrl_begin should be called to
1367 * initialize the request. This function acquires the control lock.
1369 * When setting a control, the new value is stored in the control data field
1370 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1371 * later processing. If the UVC and V4L2 control sizes differ, the current
1372 * value is loaded from the hardware before storing the new value in the data
1375 * After processing all controls in the transaction, uvc_ctrl_commit or
1376 * uvc_ctrl_rollback must be called to apply the pending changes to the
1377 * hardware or revert them. When applying changes, all controls marked as
1378 * dirty will be modified in the UVC device, and the dirty flag will be
1379 * cleared. When reverting controls, the control data field
1380 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1381 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1384 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1386 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1389 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1390 struct uvc_entity *entity, int rollback)
1392 struct uvc_control *ctrl;
1399 for (i = 0; i < entity->ncontrols; ++i) {
1400 ctrl = &entity->controls[i];
1401 if (!ctrl->initialized)
1404 /* Reset the loaded flag for auto-update controls that were
1405 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1406 * uvc_ctrl_get from using the cached value, and for write-only
1407 * controls to prevent uvc_ctrl_set from setting bits not
1408 * explicitly set by the user.
1410 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1411 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1418 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1419 dev->intfnum, ctrl->info.selector,
1420 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1425 if (rollback || ret < 0)
1426 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1427 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1439 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1440 const struct v4l2_ext_control *xctrls,
1441 unsigned int xctrls_count)
1443 struct uvc_video_chain *chain = handle->chain;
1444 struct uvc_entity *entity;
1447 /* Find the control. */
1448 list_for_each_entry(entity, &chain->entities, chain) {
1449 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1455 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1457 mutex_unlock(&chain->ctrl_mutex);
1461 int uvc_ctrl_get(struct uvc_video_chain *chain,
1462 struct v4l2_ext_control *xctrl)
1464 struct uvc_control *ctrl;
1465 struct uvc_control_mapping *mapping;
1467 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1471 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1474 int uvc_ctrl_set(struct uvc_video_chain *chain,
1475 struct v4l2_ext_control *xctrl)
1477 struct uvc_control *ctrl;
1478 struct uvc_control_mapping *mapping;
1485 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1488 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1491 /* Clamp out of range values. */
1492 switch (mapping->v4l2_type) {
1493 case V4L2_CTRL_TYPE_INTEGER:
1494 if (!ctrl->cached) {
1495 ret = uvc_ctrl_populate_cache(chain, ctrl);
1500 min = mapping->get(mapping, UVC_GET_MIN,
1501 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1502 max = mapping->get(mapping, UVC_GET_MAX,
1503 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1504 step = mapping->get(mapping, UVC_GET_RES,
1505 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1509 xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
1511 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1512 xctrl->value = clamp(xctrl->value, min, max);
1514 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1515 value = xctrl->value;
1518 case V4L2_CTRL_TYPE_BOOLEAN:
1519 xctrl->value = clamp(xctrl->value, 0, 1);
1520 value = xctrl->value;
1523 case V4L2_CTRL_TYPE_MENU:
1524 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1526 value = mapping->menu_info[xctrl->value].value;
1528 /* Valid menu indices are reported by the GET_RES request for
1529 * UVC controls that support it.
1531 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1532 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1533 if (!ctrl->cached) {
1534 ret = uvc_ctrl_populate_cache(chain, ctrl);
1539 step = mapping->get(mapping, UVC_GET_RES,
1540 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1541 if (!(step & value))
1548 value = xctrl->value;
1552 /* If the mapping doesn't span the whole UVC control, the current value
1553 * needs to be loaded from the device to perform the read-modify-write
1556 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1557 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1558 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1559 0, ctrl->info.size);
1561 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1562 ctrl->entity->id, chain->dev->intfnum,
1563 ctrl->info.selector,
1564 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1573 /* Backup the current value in case we need to rollback later. */
1575 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1576 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1580 mapping->set(mapping, value,
1581 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1588 /* --------------------------------------------------------------------------
1592 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1593 const struct uvc_control *ctrl, struct uvc_control_info *info)
1595 struct uvc_ctrl_fixup {
1596 struct usb_device_id id;
1602 static const struct uvc_ctrl_fixup fixups[] = {
1603 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1604 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1605 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1606 UVC_CTRL_FLAG_AUTO_UPDATE },
1607 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1608 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1609 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1610 UVC_CTRL_FLAG_AUTO_UPDATE },
1611 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1612 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1613 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1614 UVC_CTRL_FLAG_AUTO_UPDATE },
1619 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1620 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1623 if (fixups[i].entity == ctrl->entity->id &&
1624 fixups[i].selector == info->selector) {
1625 info->flags = fixups[i].flags;
1632 * Query control information (size and flags) for XU controls.
1634 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1635 const struct uvc_control *ctrl, struct uvc_control_info *info)
1640 data = kmalloc(2, GFP_KERNEL);
1644 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1645 sizeof(info->entity));
1646 info->index = ctrl->index;
1647 info->selector = ctrl->index + 1;
1649 /* Query and verify the control length (GET_LEN) */
1650 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1651 info->selector, data, 2);
1653 uvc_trace(UVC_TRACE_CONTROL,
1654 "GET_LEN failed on control %pUl/%u (%d).\n",
1655 info->entity, info->selector, ret);
1659 info->size = le16_to_cpup((__le16 *)data);
1661 /* Query the control information (GET_INFO) */
1662 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1663 info->selector, data, 1);
1665 uvc_trace(UVC_TRACE_CONTROL,
1666 "GET_INFO failed on control %pUl/%u (%d).\n",
1667 info->entity, info->selector, ret);
1671 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1672 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1673 | (data[0] & UVC_CONTROL_CAP_GET ?
1674 UVC_CTRL_FLAG_GET_CUR : 0)
1675 | (data[0] & UVC_CONTROL_CAP_SET ?
1676 UVC_CTRL_FLAG_SET_CUR : 0)
1677 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1678 UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1680 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1682 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1683 "flags { get %u set %u auto %u }.\n",
1684 info->entity, info->selector, info->size,
1685 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1686 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1687 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1694 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1695 const struct uvc_control_info *info);
1697 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1698 struct uvc_control *ctrl)
1700 struct uvc_control_info info;
1703 if (ctrl->initialized)
1706 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1710 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1712 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1713 "%pUl/%u on device %s entity %u\n", info.entity,
1714 info.selector, dev->udev->devpath, ctrl->entity->id);
1719 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1720 struct uvc_xu_control_query *xqry)
1722 struct uvc_entity *entity;
1723 struct uvc_control *ctrl;
1724 unsigned int i, found = 0;
1730 /* Find the extension unit. */
1731 list_for_each_entry(entity, &chain->entities, chain) {
1732 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1733 entity->id == xqry->unit)
1737 if (entity->id != xqry->unit) {
1738 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1743 /* Find the control and perform delayed initialization if needed. */
1744 for (i = 0; i < entity->ncontrols; ++i) {
1745 ctrl = &entity->controls[i];
1746 if (ctrl->index == xqry->selector - 1) {
1753 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1754 entity->extension.guidExtensionCode, xqry->selector);
1758 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1759 return -ERESTARTSYS;
1761 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1767 /* Validate the required buffer size and flags for the request */
1769 size = ctrl->info.size;
1771 switch (xqry->query) {
1773 reqflags = UVC_CTRL_FLAG_GET_CUR;
1776 reqflags = UVC_CTRL_FLAG_GET_MIN;
1779 reqflags = UVC_CTRL_FLAG_GET_MAX;
1782 reqflags = UVC_CTRL_FLAG_GET_DEF;
1785 reqflags = UVC_CTRL_FLAG_GET_RES;
1788 reqflags = UVC_CTRL_FLAG_SET_CUR;
1801 if (size != xqry->size) {
1806 if (reqflags && !(ctrl->info.flags & reqflags)) {
1811 data = kmalloc(size, GFP_KERNEL);
1817 if (xqry->query == UVC_SET_CUR &&
1818 copy_from_user(data, xqry->data, size)) {
1823 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1824 chain->dev->intfnum, xqry->selector, data, size);
1828 if (xqry->query != UVC_SET_CUR &&
1829 copy_to_user(xqry->data, data, size))
1833 mutex_unlock(&chain->ctrl_mutex);
1837 /* --------------------------------------------------------------------------
1842 * Restore control values after resume, skipping controls that haven't been
1846 * - Don't restore modified controls that are back to their default value.
1847 * - Handle restore order (Auto-Exposure Mode should be restored before
1850 int uvc_ctrl_restore_values(struct uvc_device *dev)
1852 struct uvc_control *ctrl;
1853 struct uvc_entity *entity;
1857 /* Walk the entities list and restore controls when possible. */
1858 list_for_each_entry(entity, &dev->entities, list) {
1860 for (i = 0; i < entity->ncontrols; ++i) {
1861 ctrl = &entity->controls[i];
1863 if (!ctrl->initialized || !ctrl->modified ||
1864 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1867 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1868 ctrl->info.entity, ctrl->info.index,
1869 ctrl->info.selector);
1873 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1881 /* --------------------------------------------------------------------------
1882 * Control and mapping handling
1886 * Add control information to a given control.
1888 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1889 const struct uvc_control_info *info)
1894 INIT_LIST_HEAD(&ctrl->info.mappings);
1896 /* Allocate an array to save control values (cur, def, max, etc.) */
1897 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1899 if (ctrl->uvc_data == NULL) {
1904 ctrl->initialized = 1;
1906 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1907 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
1908 dev->udev->devpath, ctrl->entity->id);
1912 kfree(ctrl->uvc_data);
1917 * Add a control mapping to a given control.
1919 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1920 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1922 struct uvc_control_mapping *map;
1925 /* Most mappings come from static kernel data and need to be duplicated.
1926 * Mappings that come from userspace will be unnecessarily duplicated,
1927 * this could be optimized.
1929 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1933 INIT_LIST_HEAD(&map->ev_subs);
1935 size = sizeof(*mapping->menu_info) * mapping->menu_count;
1936 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1937 if (map->menu_info == NULL) {
1942 if (map->get == NULL)
1943 map->get = uvc_get_le_value;
1944 if (map->set == NULL)
1945 map->set = uvc_set_le_value;
1947 list_add_tail(&map->list, &ctrl->info.mappings);
1948 uvc_trace(UVC_TRACE_CONTROL,
1949 "Adding mapping '%s' to control %pUl/%u.\n",
1950 map->name, ctrl->info.entity, ctrl->info.selector);
1955 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1956 const struct uvc_control_mapping *mapping)
1958 struct uvc_device *dev = chain->dev;
1959 struct uvc_control_mapping *map;
1960 struct uvc_entity *entity;
1961 struct uvc_control *ctrl;
1965 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1966 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1967 "id 0x%08x is invalid.\n", mapping->name,
1972 /* Search for the matching (GUID/CS) control on the current chain */
1973 list_for_each_entry(entity, &chain->entities, chain) {
1976 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1977 !uvc_entity_match_guid(entity, mapping->entity))
1980 for (i = 0; i < entity->ncontrols; ++i) {
1981 ctrl = &entity->controls[i];
1982 if (ctrl->index == mapping->selector - 1) {
1994 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1995 return -ERESTARTSYS;
1997 /* Perform delayed initialization of XU controls */
1998 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2004 list_for_each_entry(map, &ctrl->info.mappings, list) {
2005 if (mapping->id == map->id) {
2006 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
2007 "control id 0x%08x already exists.\n",
2008 mapping->name, mapping->id);
2014 /* Prevent excess memory consumption */
2015 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2016 atomic_dec(&dev->nmappings);
2017 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
2018 "mappings count (%u) exceeded.\n", mapping->name,
2019 UVC_MAX_CONTROL_MAPPINGS);
2024 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2026 atomic_dec(&dev->nmappings);
2029 mutex_unlock(&chain->ctrl_mutex);
2034 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2035 * are currently the ones that crash the camera or unconditionally return an
2036 * error when queried.
2038 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2039 struct uvc_entity *entity)
2041 struct uvc_ctrl_blacklist {
2042 struct usb_device_id id;
2046 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2047 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2048 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2049 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2051 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2052 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2055 const struct uvc_ctrl_blacklist *blacklist;
2061 switch (UVC_ENTITY_TYPE(entity)) {
2062 case UVC_VC_PROCESSING_UNIT:
2063 blacklist = processing_blacklist;
2064 count = ARRAY_SIZE(processing_blacklist);
2065 controls = entity->processing.bmControls;
2066 size = entity->processing.bControlSize;
2069 case UVC_ITT_CAMERA:
2070 blacklist = camera_blacklist;
2071 count = ARRAY_SIZE(camera_blacklist);
2072 controls = entity->camera.bmControls;
2073 size = entity->camera.bControlSize;
2080 for (i = 0; i < count; ++i) {
2081 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2084 if (blacklist[i].index >= 8 * size ||
2085 !uvc_test_bit(controls, blacklist[i].index))
2088 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
2089 "removing it.\n", entity->id, blacklist[i].index);
2091 uvc_clear_bit(controls, blacklist[i].index);
2096 * Add control information and hardcoded stock control mappings to the given
2099 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2101 const struct uvc_control_info *info = uvc_ctrls;
2102 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2103 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2104 const struct uvc_control_mapping *mend =
2105 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2107 /* XU controls initialization requires querying the device for control
2108 * information. As some buggy UVC devices will crash when queried
2109 * repeatedly in a tight loop, delay XU controls initialization until
2112 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2115 for (; info < iend; ++info) {
2116 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2117 ctrl->index == info->index) {
2118 uvc_ctrl_add_info(dev, ctrl, info);
2123 if (!ctrl->initialized)
2126 for (; mapping < mend; ++mapping) {
2127 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2128 ctrl->info.selector == mapping->selector)
2129 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2134 * Initialize device controls.
2136 int uvc_ctrl_init_device(struct uvc_device *dev)
2138 struct uvc_entity *entity;
2141 /* Walk the entities list and instantiate controls */
2142 list_for_each_entry(entity, &dev->entities, list) {
2143 struct uvc_control *ctrl;
2144 unsigned int bControlSize = 0, ncontrols;
2145 __u8 *bmControls = NULL;
2147 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2148 bmControls = entity->extension.bmControls;
2149 bControlSize = entity->extension.bControlSize;
2150 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2151 bmControls = entity->processing.bmControls;
2152 bControlSize = entity->processing.bControlSize;
2153 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2154 bmControls = entity->camera.bmControls;
2155 bControlSize = entity->camera.bControlSize;
2158 /* Remove bogus/blacklisted controls */
2159 uvc_ctrl_prune_entity(dev, entity);
2161 /* Count supported controls and allocate the controls array */
2162 ncontrols = memweight(bmControls, bControlSize);
2166 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2168 if (entity->controls == NULL)
2170 entity->ncontrols = ncontrols;
2172 /* Initialize all supported controls */
2173 ctrl = entity->controls;
2174 for (i = 0; i < bControlSize * 8; ++i) {
2175 if (uvc_test_bit(bmControls, i) == 0)
2178 ctrl->entity = entity;
2181 uvc_ctrl_init_ctrl(dev, ctrl);
2190 * Cleanup device controls.
2192 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2193 struct uvc_control *ctrl)
2195 struct uvc_control_mapping *mapping, *nm;
2197 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2198 list_del(&mapping->list);
2199 kfree(mapping->menu_info);
2204 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2206 struct uvc_entity *entity;
2209 /* Free controls and control mappings for all entities. */
2210 list_for_each_entry(entity, &dev->entities, list) {
2211 for (i = 0; i < entity->ncontrols; ++i) {
2212 struct uvc_control *ctrl = &entity->controls[i];
2214 if (!ctrl->initialized)
2217 uvc_ctrl_cleanup_mappings(dev, ctrl);
2218 kfree(ctrl->uvc_data);
2221 kfree(entity->controls);