1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * uvc_ctrl.c -- USB Video Class driver - Controls
5 * Copyright (C) 2005-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 #include <asm/barrier.h>
10 #include <linux/bitops.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/usb.h>
17 #include <linux/usb/uvc.h>
18 #include <linux/videodev2.h>
19 #include <linux/vmalloc.h>
20 #include <linux/wait.h>
21 #include <linux/workqueue.h>
22 #include <linux/atomic.h>
23 #include <media/v4l2-ctrls.h>
27 #define UVC_CTRL_DATA_CURRENT 0
28 #define UVC_CTRL_DATA_BACKUP 1
29 #define UVC_CTRL_DATA_MIN 2
30 #define UVC_CTRL_DATA_MAX 3
31 #define UVC_CTRL_DATA_RES 4
32 #define UVC_CTRL_DATA_DEF 5
33 #define UVC_CTRL_DATA_LAST 6
35 /* ------------------------------------------------------------------------
39 static const struct uvc_control_info uvc_ctrls[] = {
41 .entity = UVC_GUID_UVC_PROCESSING,
42 .selector = UVC_PU_BRIGHTNESS_CONTROL,
45 .flags = UVC_CTRL_FLAG_SET_CUR
46 | UVC_CTRL_FLAG_GET_RANGE
47 | UVC_CTRL_FLAG_RESTORE,
50 .entity = UVC_GUID_UVC_PROCESSING,
51 .selector = UVC_PU_CONTRAST_CONTROL,
54 .flags = UVC_CTRL_FLAG_SET_CUR
55 | UVC_CTRL_FLAG_GET_RANGE
56 | UVC_CTRL_FLAG_RESTORE,
59 .entity = UVC_GUID_UVC_PROCESSING,
60 .selector = UVC_PU_HUE_CONTROL,
63 .flags = UVC_CTRL_FLAG_SET_CUR
64 | UVC_CTRL_FLAG_GET_RANGE
65 | UVC_CTRL_FLAG_RESTORE
66 | UVC_CTRL_FLAG_AUTO_UPDATE,
69 .entity = UVC_GUID_UVC_PROCESSING,
70 .selector = UVC_PU_SATURATION_CONTROL,
73 .flags = UVC_CTRL_FLAG_SET_CUR
74 | UVC_CTRL_FLAG_GET_RANGE
75 | UVC_CTRL_FLAG_RESTORE,
78 .entity = UVC_GUID_UVC_PROCESSING,
79 .selector = UVC_PU_SHARPNESS_CONTROL,
82 .flags = UVC_CTRL_FLAG_SET_CUR
83 | UVC_CTRL_FLAG_GET_RANGE
84 | UVC_CTRL_FLAG_RESTORE,
87 .entity = UVC_GUID_UVC_PROCESSING,
88 .selector = UVC_PU_GAMMA_CONTROL,
91 .flags = UVC_CTRL_FLAG_SET_CUR
92 | UVC_CTRL_FLAG_GET_RANGE
93 | UVC_CTRL_FLAG_RESTORE,
96 .entity = UVC_GUID_UVC_PROCESSING,
97 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
100 .flags = UVC_CTRL_FLAG_SET_CUR
101 | UVC_CTRL_FLAG_GET_RANGE
102 | UVC_CTRL_FLAG_RESTORE
103 | UVC_CTRL_FLAG_AUTO_UPDATE,
106 .entity = UVC_GUID_UVC_PROCESSING,
107 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
110 .flags = UVC_CTRL_FLAG_SET_CUR
111 | UVC_CTRL_FLAG_GET_RANGE
112 | UVC_CTRL_FLAG_RESTORE
113 | UVC_CTRL_FLAG_AUTO_UPDATE,
116 .entity = UVC_GUID_UVC_PROCESSING,
117 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
120 .flags = UVC_CTRL_FLAG_SET_CUR
121 | UVC_CTRL_FLAG_GET_RANGE
122 | UVC_CTRL_FLAG_RESTORE,
125 .entity = UVC_GUID_UVC_PROCESSING,
126 .selector = UVC_PU_GAIN_CONTROL,
129 .flags = UVC_CTRL_FLAG_SET_CUR
130 | UVC_CTRL_FLAG_GET_RANGE
131 | UVC_CTRL_FLAG_RESTORE,
134 .entity = UVC_GUID_UVC_PROCESSING,
135 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
138 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
139 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
142 .entity = UVC_GUID_UVC_PROCESSING,
143 .selector = UVC_PU_HUE_AUTO_CONTROL,
146 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
147 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
150 .entity = UVC_GUID_UVC_PROCESSING,
151 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
154 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
155 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
158 .entity = UVC_GUID_UVC_PROCESSING,
159 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
162 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
163 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
166 .entity = UVC_GUID_UVC_PROCESSING,
167 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
170 .flags = UVC_CTRL_FLAG_SET_CUR
171 | UVC_CTRL_FLAG_GET_RANGE
172 | UVC_CTRL_FLAG_RESTORE,
175 .entity = UVC_GUID_UVC_PROCESSING,
176 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
179 .flags = UVC_CTRL_FLAG_SET_CUR
180 | UVC_CTRL_FLAG_GET_RANGE
181 | UVC_CTRL_FLAG_RESTORE,
184 .entity = UVC_GUID_UVC_PROCESSING,
185 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
188 .flags = UVC_CTRL_FLAG_GET_CUR,
191 .entity = UVC_GUID_UVC_PROCESSING,
192 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
195 .flags = UVC_CTRL_FLAG_GET_CUR,
198 .entity = UVC_GUID_UVC_CAMERA,
199 .selector = UVC_CT_SCANNING_MODE_CONTROL,
202 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
203 | UVC_CTRL_FLAG_RESTORE,
206 .entity = UVC_GUID_UVC_CAMERA,
207 .selector = UVC_CT_AE_MODE_CONTROL,
210 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
211 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
212 | UVC_CTRL_FLAG_RESTORE,
215 .entity = UVC_GUID_UVC_CAMERA,
216 .selector = UVC_CT_AE_PRIORITY_CONTROL,
219 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
220 | UVC_CTRL_FLAG_RESTORE,
223 .entity = UVC_GUID_UVC_CAMERA,
224 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
227 .flags = UVC_CTRL_FLAG_SET_CUR
228 | UVC_CTRL_FLAG_GET_RANGE
229 | UVC_CTRL_FLAG_RESTORE
230 | UVC_CTRL_FLAG_AUTO_UPDATE,
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,
354 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
355 .selector = UVC_CT_PRIVACY_CONTROL,
358 .flags = UVC_CTRL_FLAG_GET_CUR
359 | UVC_CTRL_FLAG_AUTO_UPDATE,
363 static const u32 uvc_control_classes[] = {
364 V4L2_CID_CAMERA_CLASS,
368 static const int exposure_auto_mapping[] = { 2, 1, 4, 8 };
371 * This function translates the V4L2 menu index @idx, as exposed to userspace as
372 * the V4L2 control value, to the corresponding UVC control value used by the
373 * device. The custom menu_mapping in the control @mapping is used when
374 * available, otherwise the function assumes that the V4L2 and UVC values are
377 * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is
378 * expressed as a bitmask and is thus guaranteed to have a single bit set.
380 * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the
381 * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM
382 * or UVC_CTRL_DATA_TYPE_BITMASK.
384 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
387 if (!test_bit(idx, &mapping->menu_mask))
390 if (mapping->menu_mapping)
391 return mapping->menu_mapping[idx];
397 uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx)
399 if (!test_bit(idx, &mapping->menu_mask))
402 if (mapping->menu_names)
403 return mapping->menu_names[idx];
405 return v4l2_ctrl_get_menu(mapping->id)[idx];
408 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
409 u8 query, const u8 *data)
411 s8 zoom = (s8)data[0];
415 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
426 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
429 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
430 data[2] = min((int)abs(value), 0xff);
433 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
434 u8 query, const u8 *data)
436 unsigned int first = mapping->offset / 8;
437 s8 rel = (s8)data[first];
441 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
444 return -data[first+1];
449 return data[first+1];
453 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
456 unsigned int first = mapping->offset / 8;
458 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
459 data[first+1] = min_t(int, abs(value), 0xff);
462 static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
464 .id = V4L2_CID_BRIGHTNESS,
465 .entity = UVC_GUID_UVC_PROCESSING,
466 .selector = UVC_PU_BRIGHTNESS_CONTROL,
469 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
470 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
473 .id = V4L2_CID_CONTRAST,
474 .entity = UVC_GUID_UVC_PROCESSING,
475 .selector = UVC_PU_CONTRAST_CONTROL,
478 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
479 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
483 .entity = UVC_GUID_UVC_PROCESSING,
484 .selector = UVC_PU_HUE_CONTROL,
487 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
488 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
489 .master_id = V4L2_CID_HUE_AUTO,
493 .id = V4L2_CID_SATURATION,
494 .entity = UVC_GUID_UVC_PROCESSING,
495 .selector = UVC_PU_SATURATION_CONTROL,
498 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
499 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
502 .id = V4L2_CID_SHARPNESS,
503 .entity = UVC_GUID_UVC_PROCESSING,
504 .selector = UVC_PU_SHARPNESS_CONTROL,
507 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
508 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
511 .id = V4L2_CID_GAMMA,
512 .entity = UVC_GUID_UVC_PROCESSING,
513 .selector = UVC_PU_GAMMA_CONTROL,
516 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
517 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
520 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
521 .entity = UVC_GUID_UVC_PROCESSING,
522 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
525 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
526 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
530 .entity = UVC_GUID_UVC_PROCESSING,
531 .selector = UVC_PU_GAIN_CONTROL,
534 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
535 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
538 .id = V4L2_CID_HUE_AUTO,
539 .entity = UVC_GUID_UVC_PROCESSING,
540 .selector = UVC_PU_HUE_AUTO_CONTROL,
543 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
544 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
545 .slave_ids = { V4L2_CID_HUE, },
548 .id = V4L2_CID_EXPOSURE_AUTO,
549 .entity = UVC_GUID_UVC_CAMERA,
550 .selector = UVC_CT_AE_MODE_CONTROL,
553 .v4l2_type = V4L2_CTRL_TYPE_MENU,
554 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
555 .menu_mapping = exposure_auto_mapping,
556 .menu_mask = GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY,
558 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
561 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
562 .entity = UVC_GUID_UVC_CAMERA,
563 .selector = UVC_CT_AE_PRIORITY_CONTROL,
566 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
567 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
570 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
571 .entity = UVC_GUID_UVC_CAMERA,
572 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
575 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
576 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
577 .master_id = V4L2_CID_EXPOSURE_AUTO,
578 .master_manual = V4L2_EXPOSURE_MANUAL,
581 .id = V4L2_CID_AUTO_WHITE_BALANCE,
582 .entity = UVC_GUID_UVC_PROCESSING,
583 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
586 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
587 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
588 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
591 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
592 .entity = UVC_GUID_UVC_PROCESSING,
593 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
596 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
597 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
598 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
602 .id = V4L2_CID_AUTO_WHITE_BALANCE,
603 .entity = UVC_GUID_UVC_PROCESSING,
604 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
607 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
608 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
609 .slave_ids = { V4L2_CID_BLUE_BALANCE,
610 V4L2_CID_RED_BALANCE },
613 .id = V4L2_CID_BLUE_BALANCE,
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_RED_BALANCE,
625 .entity = UVC_GUID_UVC_PROCESSING,
626 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
629 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
630 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
631 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
635 .id = V4L2_CID_FOCUS_ABSOLUTE,
636 .entity = UVC_GUID_UVC_CAMERA,
637 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
640 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
641 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
642 .master_id = V4L2_CID_FOCUS_AUTO,
646 .id = V4L2_CID_FOCUS_AUTO,
647 .entity = UVC_GUID_UVC_CAMERA,
648 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
651 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
652 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
653 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
656 .id = V4L2_CID_IRIS_ABSOLUTE,
657 .entity = UVC_GUID_UVC_CAMERA,
658 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
661 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
662 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
665 .id = V4L2_CID_IRIS_RELATIVE,
666 .entity = UVC_GUID_UVC_CAMERA,
667 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
670 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
671 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
674 .id = V4L2_CID_ZOOM_ABSOLUTE,
675 .entity = UVC_GUID_UVC_CAMERA,
676 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
679 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
680 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
683 .id = V4L2_CID_ZOOM_CONTINUOUS,
684 .entity = UVC_GUID_UVC_CAMERA,
685 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
688 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
689 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
690 .get = uvc_ctrl_get_zoom,
691 .set = uvc_ctrl_set_zoom,
694 .id = V4L2_CID_PAN_ABSOLUTE,
695 .entity = UVC_GUID_UVC_CAMERA,
696 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
699 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
700 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
703 .id = V4L2_CID_TILT_ABSOLUTE,
704 .entity = UVC_GUID_UVC_CAMERA,
705 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
708 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
709 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
712 .id = V4L2_CID_PAN_SPEED,
713 .entity = UVC_GUID_UVC_CAMERA,
714 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
717 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
718 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
719 .get = uvc_ctrl_get_rel_speed,
720 .set = uvc_ctrl_set_rel_speed,
723 .id = V4L2_CID_TILT_SPEED,
724 .entity = UVC_GUID_UVC_CAMERA,
725 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
728 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
729 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
730 .get = uvc_ctrl_get_rel_speed,
731 .set = uvc_ctrl_set_rel_speed,
734 .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,
743 .id = V4L2_CID_PRIVACY,
744 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
745 .selector = UVC_CT_PRIVACY_CONTROL,
748 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
749 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
753 const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = {
754 .id = V4L2_CID_POWER_LINE_FREQUENCY,
755 .entity = UVC_GUID_UVC_PROCESSING,
756 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
759 .v4l2_type = V4L2_CTRL_TYPE_MENU,
760 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
761 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
762 V4L2_CID_POWER_LINE_FREQUENCY_50HZ),
765 const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
766 .id = V4L2_CID_POWER_LINE_FREQUENCY,
767 .entity = UVC_GUID_UVC_PROCESSING,
768 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
771 .v4l2_type = V4L2_CTRL_TYPE_MENU,
772 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
773 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
774 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
777 static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc11[] = {
778 &uvc_ctrl_power_line_mapping_uvc11,
782 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
783 .id = V4L2_CID_POWER_LINE_FREQUENCY,
784 .entity = UVC_GUID_UVC_PROCESSING,
785 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
788 .v4l2_type = V4L2_CTRL_TYPE_MENU,
789 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
790 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
791 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
794 static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc15[] = {
795 &uvc_ctrl_power_line_mapping_uvc15,
799 /* ------------------------------------------------------------------------
803 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
805 return ctrl->uvc_data + id * ctrl->info.size;
808 static inline int uvc_test_bit(const u8 *data, int bit)
810 return (data[bit >> 3] >> (bit & 7)) & 1;
813 static inline void uvc_clear_bit(u8 *data, int bit)
815 data[bit >> 3] &= ~(1 << (bit & 7));
819 * Extract the bit string specified by mapping->offset and mapping->size
820 * from the little-endian data stored at 'data' and return the result as
821 * a signed 32bit integer. Sign extension will be performed if the mapping
822 * references a signed data type.
824 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
825 u8 query, const u8 *data)
827 int bits = mapping->size;
828 int offset = mapping->offset;
834 mask = ((1LL << bits) - 1) << offset;
837 u8 byte = *data & mask;
838 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
839 bits -= 8 - (offset > 0 ? offset : 0);
844 mask = (1 << bits) - 1;
848 /* Sign-extend the value if needed. */
849 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
850 value |= -(value & (1 << (mapping->size - 1)));
856 * Set the bit string specified by mapping->offset and mapping->size
857 * in the little-endian data stored at 'data' to the value 'value'.
859 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
862 int bits = mapping->size;
863 int offset = mapping->offset;
867 * According to the v4l2 spec, writing any value to a button control
868 * should result in the action belonging to the button control being
869 * triggered. UVC devices however want to see a 1 written -> override
872 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
878 for (; bits > 0; data++) {
879 mask = ((1LL << bits) - 1) << offset;
880 *data = (*data & ~mask) | ((value << offset) & mask);
881 value >>= offset ? offset : 8;
887 /* ------------------------------------------------------------------------
888 * Terminal and unit management
891 static int uvc_entity_match_guid(const struct uvc_entity *entity,
894 return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
897 /* ------------------------------------------------------------------------
901 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
902 struct uvc_control_mapping **mapping, struct uvc_control **control,
905 struct uvc_control *ctrl;
906 struct uvc_control_mapping *map;
912 for (i = 0; i < entity->ncontrols; ++i) {
913 ctrl = &entity->controls[i];
914 if (!ctrl->initialized)
917 list_for_each_entry(map, &ctrl->info.mappings, list) {
918 if ((map->id == v4l2_id) && !next) {
924 if ((*mapping == NULL || (*mapping)->id > map->id) &&
925 (map->id > v4l2_id) && next) {
933 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
934 u32 v4l2_id, struct uvc_control_mapping **mapping)
936 struct uvc_control *ctrl = NULL;
937 struct uvc_entity *entity;
938 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
942 /* Mask the query flags. */
943 v4l2_id &= V4L2_CTRL_ID_MASK;
945 /* Find the control. */
946 list_for_each_entry(entity, &chain->entities, chain) {
947 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
952 if (ctrl == NULL && !next)
953 uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
959 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
960 struct uvc_control *ctrl)
964 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
965 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
966 chain->dev->intfnum, ctrl->info.selector,
967 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
973 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
974 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
975 chain->dev->intfnum, ctrl->info.selector,
976 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
981 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
982 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
983 chain->dev->intfnum, ctrl->info.selector,
984 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
989 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
990 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
991 chain->dev->intfnum, ctrl->info.selector,
992 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
995 if (UVC_ENTITY_TYPE(ctrl->entity) !=
996 UVC_VC_EXTENSION_UNIT)
1000 * GET_RES is mandatory for XU controls, but some
1001 * cameras still choke on it. Ignore errors and set the
1002 * resolution value to zero.
1004 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
1005 "UVC non compliance - GET_RES failed on "
1006 "an XU control. Enabling workaround.\n");
1007 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
1016 static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
1019 s32 value = mapping->get(mapping, UVC_GET_CUR, data);
1021 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1024 for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
1027 if (!test_bit(i, &mapping->menu_mask))
1030 menu_value = uvc_mapping_get_menu_value(mapping, i);
1032 if (menu_value == value) {
1042 static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
1043 struct uvc_control *ctrl)
1051 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1053 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1054 memset(data, 0, ctrl->info.size);
1060 if (ctrl->entity->get_cur)
1061 ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
1062 ctrl->info.selector, data,
1065 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1066 ctrl->entity->id, chain->dev->intfnum,
1067 ctrl->info.selector, data,
1078 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1079 struct uvc_control *ctrl,
1080 struct uvc_control_mapping *mapping,
1085 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1088 ret = __uvc_ctrl_load_cur(chain, ctrl);
1092 *value = __uvc_ctrl_get_value(mapping,
1093 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1098 static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1101 bool find_next = req_id & V4L2_CTRL_FLAG_NEXT_CTRL;
1104 req_id &= V4L2_CTRL_ID_MASK;
1106 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
1107 if (!(chain->ctrl_class_bitmap & BIT(i)))
1110 if (uvc_control_classes[i] == req_id)
1114 if (uvc_control_classes[i] > req_id &&
1115 uvc_control_classes[i] < found_id)
1122 static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1123 u32 found_id, struct v4l2_queryctrl *v4l2_ctrl)
1127 idx = __uvc_query_v4l2_class(chain, req_id, found_id);
1131 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1132 v4l2_ctrl->id = uvc_control_classes[idx];
1133 strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
1134 sizeof(v4l2_ctrl->name));
1135 v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
1136 v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
1137 | V4L2_CTRL_FLAG_READ_ONLY;
1142 * Check if control @v4l2_id can be accessed by the given control @ioctl
1143 * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
1145 * For set operations on slave controls, check if the master's value is set to
1146 * manual, either in the others controls set in the same ioctl call, or from
1147 * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
1148 * both the master and slave control, such as for instance setting
1149 * auto_exposure=1, exposure_time_absolute=251.
1151 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
1152 const struct v4l2_ext_controls *ctrls,
1153 unsigned long ioctl)
1155 struct uvc_control_mapping *master_map = NULL;
1156 struct uvc_control *master_ctrl = NULL;
1157 struct uvc_control_mapping *mapping;
1158 struct uvc_control *ctrl;
1159 bool read = ioctl == VIDIOC_G_EXT_CTRLS;
1164 if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
1167 ctrl = uvc_find_control(chain, v4l2_id, &mapping);
1171 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read)
1174 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read)
1177 if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id)
1181 * Iterate backwards in cases where the master control is accessed
1182 * multiple times in the same ioctl. We want the last value.
1184 for (i = ctrls->count - 1; i >= 0; i--) {
1185 if (ctrls->controls[i].id == mapping->master_id)
1186 return ctrls->controls[i].value ==
1187 mapping->master_manual ? 0 : -EACCES;
1190 __uvc_find_control(ctrl->entity, mapping->master_id, &master_map,
1193 if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1196 ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1197 if (ret >= 0 && val != mapping->master_manual)
1203 static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
1210 name = v4l2_ctrl_get_name(map->id);
1214 return "Unknown Control";
1217 static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl,
1218 struct uvc_control_mapping *mapping)
1221 * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
1222 * the number of bits supported. Those controls do not list GET_MAX
1225 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1226 return mapping->get(mapping, UVC_GET_RES,
1227 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1229 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1230 return mapping->get(mapping, UVC_GET_MAX,
1231 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1236 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1237 struct uvc_control *ctrl,
1238 struct uvc_control_mapping *mapping,
1239 struct v4l2_queryctrl *v4l2_ctrl)
1241 struct uvc_control_mapping *master_map = NULL;
1242 struct uvc_control *master_ctrl = NULL;
1245 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1246 v4l2_ctrl->id = mapping->id;
1247 v4l2_ctrl->type = mapping->v4l2_type;
1248 strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
1249 sizeof(v4l2_ctrl->name));
1250 v4l2_ctrl->flags = 0;
1252 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1253 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1254 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1255 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1257 if (mapping->master_id)
1258 __uvc_find_control(ctrl->entity, mapping->master_id,
1259 &master_map, &master_ctrl, 0);
1260 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1262 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1266 if (val != mapping->master_manual)
1267 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1270 if (!ctrl->cached) {
1271 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1276 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1277 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1278 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1281 switch (mapping->v4l2_type) {
1282 case V4L2_CTRL_TYPE_MENU:
1283 v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1;
1284 v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1;
1285 v4l2_ctrl->step = 1;
1287 for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
1290 if (!test_bit(i, &mapping->menu_mask))
1293 menu_value = uvc_mapping_get_menu_value(mapping, i);
1295 if (menu_value == v4l2_ctrl->default_value) {
1296 v4l2_ctrl->default_value = i;
1303 case V4L2_CTRL_TYPE_BOOLEAN:
1304 v4l2_ctrl->minimum = 0;
1305 v4l2_ctrl->maximum = 1;
1306 v4l2_ctrl->step = 1;
1309 case V4L2_CTRL_TYPE_BUTTON:
1310 v4l2_ctrl->minimum = 0;
1311 v4l2_ctrl->maximum = 0;
1312 v4l2_ctrl->step = 0;
1315 case V4L2_CTRL_TYPE_BITMASK:
1316 v4l2_ctrl->minimum = 0;
1317 v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping);
1318 v4l2_ctrl->step = 0;
1325 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1326 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1327 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1329 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1330 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1331 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1333 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1334 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1335 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1340 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1341 struct v4l2_queryctrl *v4l2_ctrl)
1343 struct uvc_control *ctrl;
1344 struct uvc_control_mapping *mapping;
1347 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1349 return -ERESTARTSYS;
1351 /* Check if the ctrl is a know class */
1352 if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
1353 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
1358 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1365 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
1366 * a class should be inserted between the previous control and the one
1367 * we have just found.
1369 if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1370 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
1376 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1378 mutex_unlock(&chain->ctrl_mutex);
1383 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1384 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1385 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1386 * Balance V4L2 controls use the White Balance Component UVC control) or
1387 * otherwise translated. The approach we take here is to use a translation
1388 * table for the controls that can be mapped directly, and handle the others
1391 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1392 struct v4l2_querymenu *query_menu)
1394 struct uvc_control_mapping *mapping;
1395 struct uvc_control *ctrl;
1396 u32 index = query_menu->index;
1397 u32 id = query_menu->id;
1401 memset(query_menu, 0, sizeof(*query_menu));
1402 query_menu->id = id;
1403 query_menu->index = index;
1405 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1407 return -ERESTARTSYS;
1409 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1410 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1415 if (!test_bit(query_menu->index, &mapping->menu_mask)) {
1420 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
1423 if (!ctrl->cached) {
1424 ret = uvc_ctrl_populate_cache(chain, ctrl);
1429 mask = uvc_mapping_get_menu_value(mapping, query_menu->index);
1435 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) {
1441 name = uvc_mapping_get_menu_name(mapping, query_menu->index);
1447 strscpy(query_menu->name, name, sizeof(query_menu->name));
1450 mutex_unlock(&chain->ctrl_mutex);
1454 /* --------------------------------------------------------------------------
1455 * Ctrl event handling
1458 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1459 struct v4l2_event *ev,
1460 struct uvc_control *ctrl,
1461 struct uvc_control_mapping *mapping,
1462 s32 value, u32 changes)
1464 struct v4l2_queryctrl v4l2_ctrl;
1466 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1468 memset(ev, 0, sizeof(*ev));
1469 ev->type = V4L2_EVENT_CTRL;
1470 ev->id = v4l2_ctrl.id;
1471 ev->u.ctrl.value = value;
1472 ev->u.ctrl.changes = changes;
1473 ev->u.ctrl.type = v4l2_ctrl.type;
1474 ev->u.ctrl.flags = v4l2_ctrl.flags;
1475 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1476 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1477 ev->u.ctrl.step = v4l2_ctrl.step;
1478 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1482 * Send control change events to all subscribers for the @ctrl control. By
1483 * default the subscriber that generated the event, as identified by @handle,
1484 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1485 * @handle can be NULL for asynchronous events related to auto-update controls,
1486 * in which case all subscribers are notified.
1488 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1489 struct uvc_fh *handle, struct uvc_control *ctrl,
1490 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1492 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1493 struct v4l2_subscribed_event *sev;
1494 struct v4l2_event ev;
1496 if (list_empty(&mapping->ev_subs))
1499 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1501 list_for_each_entry(sev, &mapping->ev_subs, node) {
1502 if (sev->fh != originator ||
1503 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1504 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1505 v4l2_event_queue_fh(sev->fh, &ev);
1510 * Send control change events for the slave of the @master control identified
1511 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1512 * generated the event and may be NULL for auto-update events.
1514 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1515 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1517 struct uvc_control_mapping *mapping = NULL;
1518 struct uvc_control *ctrl = NULL;
1519 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1522 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1526 if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1527 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1529 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1532 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1533 struct uvc_control *ctrl, const u8 *data)
1535 struct uvc_control_mapping *mapping;
1536 struct uvc_fh *handle;
1539 mutex_lock(&chain->ctrl_mutex);
1541 handle = ctrl->handle;
1542 ctrl->handle = NULL;
1544 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1545 s32 value = __uvc_ctrl_get_value(mapping, data);
1548 * handle may be NULL here if the device sends auto-update
1549 * events without a prior related control set from userspace.
1551 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1552 if (!mapping->slave_ids[i])
1555 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1556 mapping->slave_ids[i]);
1559 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1560 V4L2_EVENT_CTRL_CH_VALUE);
1563 mutex_unlock(&chain->ctrl_mutex);
1566 static void uvc_ctrl_status_event_work(struct work_struct *work)
1568 struct uvc_device *dev = container_of(work, struct uvc_device,
1570 struct uvc_ctrl_work *w = &dev->async_ctrl;
1573 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1575 /* The barrier is needed to synchronize with uvc_status_stop(). */
1576 if (smp_load_acquire(&dev->flush_status))
1579 /* Resubmit the URB. */
1580 w->urb->interval = dev->int_ep->desc.bInterval;
1581 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1583 dev_err(&dev->udev->dev,
1584 "Failed to resubmit status URB (%d).\n", ret);
1587 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1588 struct uvc_control *ctrl, const u8 *data)
1590 struct uvc_device *dev = chain->dev;
1591 struct uvc_ctrl_work *w = &dev->async_ctrl;
1593 if (list_empty(&ctrl->info.mappings)) {
1594 ctrl->handle = NULL;
1603 schedule_work(&w->work);
1608 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1609 unsigned int xctrls_count, u32 id)
1613 for (i = 0; i < xctrls_count; ++i) {
1614 if (xctrls[i].id == id)
1621 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1622 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1624 struct uvc_control_mapping *mapping;
1625 struct uvc_control *ctrl;
1626 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1630 for (i = 0; i < xctrls_count; ++i) {
1631 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1633 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1634 /* Notification will be sent from an Interrupt event. */
1637 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1638 u32 slave_id = mapping->slave_ids[j];
1644 * We can skip sending an event for the slave if the
1645 * slave is being modified in the same transaction.
1647 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1651 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1656 * If the master is being modified in the same transaction
1657 * flags may change too.
1659 if (mapping->master_id &&
1660 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1661 mapping->master_id))
1662 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1664 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1665 xctrls[i].value, changes);
1669 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1671 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1672 struct uvc_control_mapping *mapping;
1673 struct uvc_control *ctrl;
1676 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1678 return -ERESTARTSYS;
1680 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
1685 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1691 list_add_tail(&sev->node, &mapping->ev_subs);
1692 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1693 struct v4l2_event ev;
1694 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1697 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1698 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1700 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1703 * Mark the queue as active, allowing this initial event to be
1707 v4l2_event_queue_fh(sev->fh, &ev);
1711 mutex_unlock(&handle->chain->ctrl_mutex);
1715 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1717 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1719 mutex_lock(&handle->chain->ctrl_mutex);
1720 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
1722 list_del(&sev->node);
1724 mutex_unlock(&handle->chain->ctrl_mutex);
1727 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1728 .add = uvc_ctrl_add_event,
1729 .del = uvc_ctrl_del_event,
1730 .replace = v4l2_ctrl_replace,
1731 .merge = v4l2_ctrl_merge,
1734 /* --------------------------------------------------------------------------
1735 * Control transactions
1737 * To make extended set operations as atomic as the hardware allows, controls
1738 * are handled using begin/commit/rollback operations.
1740 * At the beginning of a set request, uvc_ctrl_begin should be called to
1741 * initialize the request. This function acquires the control lock.
1743 * When setting a control, the new value is stored in the control data field
1744 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1745 * later processing. If the UVC and V4L2 control sizes differ, the current
1746 * value is loaded from the hardware before storing the new value in the data
1749 * After processing all controls in the transaction, uvc_ctrl_commit or
1750 * uvc_ctrl_rollback must be called to apply the pending changes to the
1751 * hardware or revert them. When applying changes, all controls marked as
1752 * dirty will be modified in the UVC device, and the dirty flag will be
1753 * cleared. When reverting controls, the control data field
1754 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1755 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1758 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1760 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1763 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1764 struct uvc_entity *entity, int rollback, struct uvc_control **err_ctrl)
1766 struct uvc_control *ctrl;
1773 for (i = 0; i < entity->ncontrols; ++i) {
1774 ctrl = &entity->controls[i];
1775 if (!ctrl->initialized)
1779 * Reset the loaded flag for auto-update controls that were
1780 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1781 * uvc_ctrl_get from using the cached value, and for write-only
1782 * controls to prevent uvc_ctrl_set from setting bits not
1783 * explicitly set by the user.
1785 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1786 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1793 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1794 dev->intfnum, ctrl->info.selector,
1795 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1800 if (rollback || ret < 0)
1801 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1802 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1817 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
1818 struct v4l2_ext_controls *ctrls,
1819 struct uvc_control *uvc_control)
1821 struct uvc_control_mapping *mapping = NULL;
1822 struct uvc_control *ctrl_found = NULL;
1826 return ctrls->count;
1828 for (i = 0; i < ctrls->count; i++) {
1829 __uvc_find_control(entity, ctrls->controls[i].id, &mapping,
1831 if (uvc_control == ctrl_found)
1835 return ctrls->count;
1838 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1839 struct v4l2_ext_controls *ctrls)
1841 struct uvc_video_chain *chain = handle->chain;
1842 struct uvc_control *err_ctrl;
1843 struct uvc_entity *entity;
1846 /* Find the control. */
1847 list_for_each_entry(entity, &chain->entities, chain) {
1848 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback,
1855 uvc_ctrl_send_events(handle, ctrls->controls, ctrls->count);
1857 if (ret < 0 && ctrls)
1858 ctrls->error_idx = uvc_ctrl_find_ctrl_idx(entity, ctrls,
1860 mutex_unlock(&chain->ctrl_mutex);
1864 int uvc_ctrl_get(struct uvc_video_chain *chain,
1865 struct v4l2_ext_control *xctrl)
1867 struct uvc_control *ctrl;
1868 struct uvc_control_mapping *mapping;
1870 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1873 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1877 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1880 int uvc_ctrl_set(struct uvc_fh *handle,
1881 struct v4l2_ext_control *xctrl)
1883 struct uvc_video_chain *chain = handle->chain;
1884 struct uvc_control *ctrl;
1885 struct uvc_control_mapping *mapping;
1892 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1895 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1898 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1901 /* Clamp out of range values. */
1902 switch (mapping->v4l2_type) {
1903 case V4L2_CTRL_TYPE_INTEGER:
1904 if (!ctrl->cached) {
1905 ret = uvc_ctrl_populate_cache(chain, ctrl);
1910 min = mapping->get(mapping, UVC_GET_MIN,
1911 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1912 max = mapping->get(mapping, UVC_GET_MAX,
1913 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1914 step = mapping->get(mapping, UVC_GET_RES,
1915 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1919 xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
1921 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1922 xctrl->value = clamp(xctrl->value, min, max);
1924 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1925 value = xctrl->value;
1928 case V4L2_CTRL_TYPE_BITMASK:
1929 if (!ctrl->cached) {
1930 ret = uvc_ctrl_populate_cache(chain, ctrl);
1935 xctrl->value &= uvc_get_ctrl_bitmap(ctrl, mapping);
1936 value = xctrl->value;
1939 case V4L2_CTRL_TYPE_BOOLEAN:
1940 xctrl->value = clamp(xctrl->value, 0, 1);
1941 value = xctrl->value;
1944 case V4L2_CTRL_TYPE_MENU:
1945 if (xctrl->value < (ffs(mapping->menu_mask) - 1) ||
1946 xctrl->value > (fls(mapping->menu_mask) - 1))
1949 if (!test_bit(xctrl->value, &mapping->menu_mask))
1952 value = uvc_mapping_get_menu_value(mapping, xctrl->value);
1955 * Valid menu indices are reported by the GET_RES request for
1956 * UVC controls that support it.
1958 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
1959 if (!ctrl->cached) {
1960 ret = uvc_ctrl_populate_cache(chain, ctrl);
1965 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & value))
1972 value = xctrl->value;
1977 * If the mapping doesn't span the whole UVC control, the current value
1978 * needs to be loaded from the device to perform the read-modify-write
1981 if ((ctrl->info.size * 8) != mapping->size) {
1982 ret = __uvc_ctrl_load_cur(chain, ctrl);
1987 /* Backup the current value in case we need to rollback later. */
1989 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1990 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1994 mapping->set(mapping, value,
1995 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1997 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1998 ctrl->handle = handle;
2005 /* --------------------------------------------------------------------------
2010 * Retrieve flags for a given control
2012 static int uvc_ctrl_get_flags(struct uvc_device *dev,
2013 const struct uvc_control *ctrl,
2014 struct uvc_control_info *info)
2019 data = kmalloc(1, GFP_KERNEL);
2023 if (ctrl->entity->get_info)
2024 ret = ctrl->entity->get_info(dev, ctrl->entity,
2025 ctrl->info.selector, data);
2027 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
2028 dev->intfnum, info->selector, data, 1);
2030 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
2031 UVC_CTRL_FLAG_GET_CUR : 0)
2032 | (data[0] & UVC_CONTROL_CAP_SET ?
2033 UVC_CTRL_FLAG_SET_CUR : 0)
2034 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
2035 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
2036 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
2037 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
2043 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
2044 const struct uvc_control *ctrl, struct uvc_control_info *info)
2046 struct uvc_ctrl_fixup {
2047 struct usb_device_id id;
2053 static const struct uvc_ctrl_fixup fixups[] = {
2054 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2055 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2056 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2057 UVC_CTRL_FLAG_AUTO_UPDATE },
2058 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2059 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2060 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2061 UVC_CTRL_FLAG_AUTO_UPDATE },
2062 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2063 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2064 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2065 UVC_CTRL_FLAG_AUTO_UPDATE },
2070 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
2071 if (!usb_match_one_id(dev->intf, &fixups[i].id))
2074 if (fixups[i].entity == ctrl->entity->id &&
2075 fixups[i].selector == info->selector) {
2076 info->flags = fixups[i].flags;
2083 * Query control information (size and flags) for XU controls.
2085 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
2086 const struct uvc_control *ctrl, struct uvc_control_info *info)
2091 data = kmalloc(2, GFP_KERNEL);
2095 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
2096 info->index = ctrl->index;
2097 info->selector = ctrl->index + 1;
2099 /* Query and verify the control length (GET_LEN) */
2100 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
2101 info->selector, data, 2);
2103 uvc_dbg(dev, CONTROL,
2104 "GET_LEN failed on control %pUl/%u (%d)\n",
2105 info->entity, info->selector, ret);
2109 info->size = le16_to_cpup((__le16 *)data);
2111 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
2112 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
2114 ret = uvc_ctrl_get_flags(dev, ctrl, info);
2116 uvc_dbg(dev, CONTROL,
2117 "Failed to get flags for control %pUl/%u (%d)\n",
2118 info->entity, info->selector, ret);
2122 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
2124 uvc_dbg(dev, CONTROL,
2125 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2126 info->entity, info->selector, info->size,
2127 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
2128 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
2129 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
2136 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2137 const struct uvc_control_info *info);
2139 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
2140 struct uvc_control *ctrl)
2142 struct uvc_control_info info;
2145 if (ctrl->initialized)
2148 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2152 ret = uvc_ctrl_add_info(dev, ctrl, &info);
2154 uvc_dbg(dev, CONTROL,
2155 "Failed to initialize control %pUl/%u on device %s entity %u\n",
2156 info.entity, info.selector, dev->udev->devpath,
2162 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2163 struct uvc_xu_control_query *xqry)
2165 struct uvc_entity *entity;
2166 struct uvc_control *ctrl;
2174 /* Find the extension unit. */
2176 list_for_each_entry(entity, &chain->entities, chain) {
2177 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
2178 entity->id == xqry->unit) {
2185 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2190 /* Find the control and perform delayed initialization if needed. */
2192 for (i = 0; i < entity->ncontrols; ++i) {
2193 ctrl = &entity->controls[i];
2194 if (ctrl->index == xqry->selector - 1) {
2201 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2202 entity->guid, xqry->selector);
2206 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2207 return -ERESTARTSYS;
2209 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2215 /* Validate the required buffer size and flags for the request */
2217 size = ctrl->info.size;
2219 switch (xqry->query) {
2221 reqflags = UVC_CTRL_FLAG_GET_CUR;
2224 reqflags = UVC_CTRL_FLAG_GET_MIN;
2227 reqflags = UVC_CTRL_FLAG_GET_MAX;
2230 reqflags = UVC_CTRL_FLAG_GET_DEF;
2233 reqflags = UVC_CTRL_FLAG_GET_RES;
2236 reqflags = UVC_CTRL_FLAG_SET_CUR;
2249 if (size != xqry->size) {
2254 if (reqflags && !(ctrl->info.flags & reqflags)) {
2259 data = kmalloc(size, GFP_KERNEL);
2265 if (xqry->query == UVC_SET_CUR &&
2266 copy_from_user(data, xqry->data, size)) {
2271 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2272 chain->dev->intfnum, xqry->selector, data, size);
2276 if (xqry->query != UVC_SET_CUR &&
2277 copy_to_user(xqry->data, data, size))
2281 mutex_unlock(&chain->ctrl_mutex);
2285 /* --------------------------------------------------------------------------
2290 * Restore control values after resume, skipping controls that haven't been
2294 * - Don't restore modified controls that are back to their default value.
2295 * - Handle restore order (Auto-Exposure Mode should be restored before
2298 int uvc_ctrl_restore_values(struct uvc_device *dev)
2300 struct uvc_control *ctrl;
2301 struct uvc_entity *entity;
2305 /* Walk the entities list and restore controls when possible. */
2306 list_for_each_entry(entity, &dev->entities, list) {
2308 for (i = 0; i < entity->ncontrols; ++i) {
2309 ctrl = &entity->controls[i];
2311 if (!ctrl->initialized || !ctrl->modified ||
2312 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2314 dev_dbg(&dev->udev->dev,
2315 "restoring control %pUl/%u/%u\n",
2316 ctrl->info.entity, ctrl->info.index,
2317 ctrl->info.selector);
2321 ret = uvc_ctrl_commit_entity(dev, entity, 0, NULL);
2329 /* --------------------------------------------------------------------------
2330 * Control and mapping handling
2334 * Add control information to a given control.
2336 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2337 const struct uvc_control_info *info)
2340 INIT_LIST_HEAD(&ctrl->info.mappings);
2342 /* Allocate an array to save control values (cur, def, max, etc.) */
2343 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2345 if (!ctrl->uvc_data)
2348 ctrl->initialized = 1;
2350 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2351 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2358 * Add a control mapping to a given control.
2360 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2361 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2363 struct uvc_control_mapping *map;
2368 * Most mappings come from static kernel data, and need to be duplicated.
2369 * Mappings that come from userspace will be unnecessarily duplicated,
2370 * this could be optimized.
2372 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2377 map->menu_names = NULL;
2378 map->menu_mapping = NULL;
2380 /* For UVCIOC_CTRL_MAP custom control */
2381 if (mapping->name) {
2382 map->name = kstrdup(mapping->name, GFP_KERNEL);
2387 INIT_LIST_HEAD(&map->ev_subs);
2389 if (mapping->menu_mapping && mapping->menu_mask) {
2390 size = sizeof(mapping->menu_mapping[0])
2391 * fls(mapping->menu_mask);
2392 map->menu_mapping = kmemdup(mapping->menu_mapping, size,
2394 if (!map->menu_mapping)
2397 if (mapping->menu_names && mapping->menu_mask) {
2398 size = sizeof(mapping->menu_names[0])
2399 * fls(mapping->menu_mask);
2400 map->menu_names = kmemdup(mapping->menu_names, size,
2402 if (!map->menu_names)
2406 if (map->get == NULL)
2407 map->get = uvc_get_le_value;
2408 if (map->set == NULL)
2409 map->set = uvc_set_le_value;
2411 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
2412 if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
2413 V4L2_CTRL_ID2WHICH(map->id)) {
2414 chain->ctrl_class_bitmap |= BIT(i);
2419 list_add_tail(&map->list, &ctrl->info.mappings);
2420 uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2421 uvc_map_get_name(map), ctrl->info.entity,
2422 ctrl->info.selector);
2427 kfree(map->menu_names);
2428 kfree(map->menu_mapping);
2434 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2435 const struct uvc_control_mapping *mapping)
2437 struct uvc_device *dev = chain->dev;
2438 struct uvc_control_mapping *map;
2439 struct uvc_entity *entity;
2440 struct uvc_control *ctrl;
2444 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2445 uvc_dbg(dev, CONTROL,
2446 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2447 uvc_map_get_name(mapping), mapping->id);
2451 /* Search for the matching (GUID/CS) control on the current chain */
2452 list_for_each_entry(entity, &chain->entities, chain) {
2455 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2456 !uvc_entity_match_guid(entity, mapping->entity))
2459 for (i = 0; i < entity->ncontrols; ++i) {
2460 ctrl = &entity->controls[i];
2461 if (ctrl->index == mapping->selector - 1) {
2473 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2474 return -ERESTARTSYS;
2476 /* Perform delayed initialization of XU controls */
2477 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2483 /* Validate the user-provided bit-size and offset */
2484 if (mapping->size > 32 ||
2485 mapping->offset + mapping->size > ctrl->info.size * 8) {
2490 list_for_each_entry(map, &ctrl->info.mappings, list) {
2491 if (mapping->id == map->id) {
2492 uvc_dbg(dev, CONTROL,
2493 "Can't add mapping '%s', control id 0x%08x already exists\n",
2494 uvc_map_get_name(mapping), mapping->id);
2500 /* Prevent excess memory consumption */
2501 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2502 atomic_dec(&dev->nmappings);
2503 uvc_dbg(dev, CONTROL,
2504 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
2505 uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
2510 ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2512 atomic_dec(&dev->nmappings);
2515 mutex_unlock(&chain->ctrl_mutex);
2520 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2521 * are currently the ones that crash the camera or unconditionally return an
2522 * error when queried.
2524 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2525 struct uvc_entity *entity)
2527 struct uvc_ctrl_blacklist {
2528 struct usb_device_id id;
2532 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2533 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2534 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2535 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2537 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2538 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2541 const struct uvc_ctrl_blacklist *blacklist;
2547 switch (UVC_ENTITY_TYPE(entity)) {
2548 case UVC_VC_PROCESSING_UNIT:
2549 blacklist = processing_blacklist;
2550 count = ARRAY_SIZE(processing_blacklist);
2551 controls = entity->processing.bmControls;
2552 size = entity->processing.bControlSize;
2555 case UVC_ITT_CAMERA:
2556 blacklist = camera_blacklist;
2557 count = ARRAY_SIZE(camera_blacklist);
2558 controls = entity->camera.bmControls;
2559 size = entity->camera.bControlSize;
2566 for (i = 0; i < count; ++i) {
2567 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2570 if (blacklist[i].index >= 8 * size ||
2571 !uvc_test_bit(controls, blacklist[i].index))
2574 uvc_dbg(dev, CONTROL,
2575 "%u/%u control is black listed, removing it\n",
2576 entity->id, blacklist[i].index);
2578 uvc_clear_bit(controls, blacklist[i].index);
2583 * Add control information and hardcoded stock control mappings to the given
2586 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
2587 struct uvc_control *ctrl)
2589 const struct uvc_control_mapping **mappings;
2593 * XU controls initialization requires querying the device for control
2594 * information. As some buggy UVC devices will crash when queried
2595 * repeatedly in a tight loop, delay XU controls initialization until
2598 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2601 for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
2602 const struct uvc_control_info *info = &uvc_ctrls[i];
2604 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2605 ctrl->index == info->index) {
2606 uvc_ctrl_add_info(chain->dev, ctrl, info);
2608 * Retrieve control flags from the device. Ignore errors
2609 * and work with default flag values from the uvc_ctrl
2610 * array when the device doesn't properly implement
2611 * GET_INFO on standard controls.
2613 uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
2618 if (!ctrl->initialized)
2622 * First check if the device provides a custom mapping for this control,
2623 * used to override standard mappings for non-conformant devices. Don't
2624 * process standard mappings if a custom mapping is found. This
2625 * mechanism doesn't support combining standard and custom mappings for
2628 if (chain->dev->info->mappings) {
2629 bool custom = false;
2631 for (i = 0; chain->dev->info->mappings[i]; ++i) {
2632 const struct uvc_control_mapping *mapping =
2633 chain->dev->info->mappings[i];
2635 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2636 ctrl->info.selector == mapping->selector) {
2637 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2646 /* Process common mappings next. */
2647 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
2648 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
2650 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2651 ctrl->info.selector == mapping->selector)
2652 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2655 /* Finally process version-specific mappings. */
2656 mappings = chain->dev->uvc_version < 0x0150
2657 ? uvc_ctrl_mappings_uvc11 : uvc_ctrl_mappings_uvc15;
2659 for (i = 0; mappings[i]; ++i) {
2660 const struct uvc_control_mapping *mapping = mappings[i];
2662 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2663 ctrl->info.selector == mapping->selector)
2664 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2669 * Initialize device controls.
2671 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
2673 struct uvc_entity *entity;
2676 /* Walk the entities list and instantiate controls */
2677 list_for_each_entry(entity, &chain->entities, chain) {
2678 struct uvc_control *ctrl;
2679 unsigned int bControlSize = 0, ncontrols;
2680 u8 *bmControls = NULL;
2682 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2683 bmControls = entity->extension.bmControls;
2684 bControlSize = entity->extension.bControlSize;
2685 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2686 bmControls = entity->processing.bmControls;
2687 bControlSize = entity->processing.bControlSize;
2688 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2689 bmControls = entity->camera.bmControls;
2690 bControlSize = entity->camera.bControlSize;
2691 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
2692 bmControls = entity->gpio.bmControls;
2693 bControlSize = entity->gpio.bControlSize;
2696 /* Remove bogus/blacklisted controls */
2697 uvc_ctrl_prune_entity(chain->dev, entity);
2699 /* Count supported controls and allocate the controls array */
2700 ncontrols = memweight(bmControls, bControlSize);
2704 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2706 if (entity->controls == NULL)
2708 entity->ncontrols = ncontrols;
2710 /* Initialize all supported controls */
2711 ctrl = entity->controls;
2712 for (i = 0; i < bControlSize * 8; ++i) {
2713 if (uvc_test_bit(bmControls, i) == 0)
2716 ctrl->entity = entity;
2719 uvc_ctrl_init_ctrl(chain, ctrl);
2727 int uvc_ctrl_init_device(struct uvc_device *dev)
2729 struct uvc_video_chain *chain;
2732 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2734 list_for_each_entry(chain, &dev->chains, list) {
2735 ret = uvc_ctrl_init_chain(chain);
2744 * Cleanup device controls.
2746 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2747 struct uvc_control *ctrl)
2749 struct uvc_control_mapping *mapping, *nm;
2751 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2752 list_del(&mapping->list);
2753 kfree(mapping->menu_names);
2754 kfree(mapping->menu_mapping);
2755 kfree(mapping->name);
2760 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2762 struct uvc_entity *entity;
2765 /* Can be uninitialized if we are aborting on probe error. */
2766 if (dev->async_ctrl.work.func)
2767 cancel_work_sync(&dev->async_ctrl.work);
2769 /* Free controls and control mappings for all entities. */
2770 list_for_each_entry(entity, &dev->entities, list) {
2771 for (i = 0; i < entity->ncontrols; ++i) {
2772 struct uvc_control *ctrl = &entity->controls[i];
2774 if (!ctrl->initialized)
2777 uvc_ctrl_cleanup_mappings(dev, ctrl);
2778 kfree(ctrl->uvc_data);
2781 kfree(entity->controls);