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_power_line_mapping_limited
= {
463 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
464 .entity
= UVC_GUID_UVC_PROCESSING
,
465 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
468 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
469 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
470 .menu_mask
= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ
,
471 V4L2_CID_POWER_LINE_FREQUENCY_50HZ
),
474 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11
= {
475 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
476 .entity
= UVC_GUID_UVC_PROCESSING
,
477 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
480 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
481 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
482 .menu_mask
= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ
,
483 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
),
486 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15
= {
487 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
488 .entity
= UVC_GUID_UVC_PROCESSING
,
489 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
492 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
493 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
494 .menu_mask
= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO
,
495 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
),
498 static const struct uvc_control_mapping
*uvc_ctrl_filter_plf_mapping(
499 struct uvc_video_chain
*chain
, struct uvc_control
*ctrl
)
501 const struct uvc_control_mapping
*out_mapping
=
502 &uvc_ctrl_power_line_mapping_uvc11
;
503 u8
*buf
__free(kfree
) = NULL
;
507 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
511 /* Save the current PLF value, so we can restore it. */
512 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
, ctrl
->entity
->id
,
513 chain
->dev
->intfnum
, ctrl
->info
.selector
,
515 /* If we cannot read the control skip it. */
520 /* If PLF value cannot be set to off, it is limited. */
521 *buf
= V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
;
522 ret
= uvc_query_ctrl(chain
->dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
523 chain
->dev
->intfnum
, ctrl
->info
.selector
,
526 return &uvc_ctrl_power_line_mapping_limited
;
528 /* UVC 1.1 does not define auto, we can exit. */
529 if (chain
->dev
->uvc_version
< 0x150)
532 /* Check if the device supports auto. */
533 *buf
= V4L2_CID_POWER_LINE_FREQUENCY_AUTO
;
534 ret
= uvc_query_ctrl(chain
->dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
535 chain
->dev
->intfnum
, ctrl
->info
.selector
,
538 out_mapping
= &uvc_ctrl_power_line_mapping_uvc15
;
541 /* Restore initial value and add mapping. */
543 uvc_query_ctrl(chain
->dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
544 chain
->dev
->intfnum
, ctrl
->info
.selector
,
550 static const struct uvc_control_mapping uvc_ctrl_mappings
[] = {
552 .id
= V4L2_CID_BRIGHTNESS
,
553 .entity
= UVC_GUID_UVC_PROCESSING
,
554 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
557 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
558 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
561 .id
= V4L2_CID_CONTRAST
,
562 .entity
= UVC_GUID_UVC_PROCESSING
,
563 .selector
= UVC_PU_CONTRAST_CONTROL
,
566 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
567 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
571 .entity
= UVC_GUID_UVC_PROCESSING
,
572 .selector
= UVC_PU_HUE_CONTROL
,
575 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
576 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
577 .master_id
= V4L2_CID_HUE_AUTO
,
581 .id
= V4L2_CID_SATURATION
,
582 .entity
= UVC_GUID_UVC_PROCESSING
,
583 .selector
= UVC_PU_SATURATION_CONTROL
,
586 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
587 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
590 .id
= V4L2_CID_SHARPNESS
,
591 .entity
= UVC_GUID_UVC_PROCESSING
,
592 .selector
= UVC_PU_SHARPNESS_CONTROL
,
595 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
596 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
599 .id
= V4L2_CID_GAMMA
,
600 .entity
= UVC_GUID_UVC_PROCESSING
,
601 .selector
= UVC_PU_GAMMA_CONTROL
,
604 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
605 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
608 .id
= V4L2_CID_BACKLIGHT_COMPENSATION
,
609 .entity
= UVC_GUID_UVC_PROCESSING
,
610 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
613 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
614 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
618 .entity
= UVC_GUID_UVC_PROCESSING
,
619 .selector
= UVC_PU_GAIN_CONTROL
,
622 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
623 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
626 .id
= V4L2_CID_HUE_AUTO
,
627 .entity
= UVC_GUID_UVC_PROCESSING
,
628 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
631 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
632 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
633 .slave_ids
= { V4L2_CID_HUE
, },
636 .id
= V4L2_CID_EXPOSURE_AUTO
,
637 .entity
= UVC_GUID_UVC_CAMERA
,
638 .selector
= UVC_CT_AE_MODE_CONTROL
,
641 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
642 .data_type
= UVC_CTRL_DATA_TYPE_BITMASK
,
643 .menu_mapping
= exposure_auto_mapping
,
644 .menu_mask
= GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY
,
646 .slave_ids
= { V4L2_CID_EXPOSURE_ABSOLUTE
, },
649 .id
= V4L2_CID_EXPOSURE_AUTO_PRIORITY
,
650 .entity
= UVC_GUID_UVC_CAMERA
,
651 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
654 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
655 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
658 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
659 .entity
= UVC_GUID_UVC_CAMERA
,
660 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
663 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
664 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
665 .master_id
= V4L2_CID_EXPOSURE_AUTO
,
666 .master_manual
= V4L2_EXPOSURE_MANUAL
,
669 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
670 .entity
= UVC_GUID_UVC_PROCESSING
,
671 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
674 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
675 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
676 .slave_ids
= { V4L2_CID_WHITE_BALANCE_TEMPERATURE
, },
679 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
680 .entity
= UVC_GUID_UVC_PROCESSING
,
681 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
684 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
685 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
686 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
690 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
691 .entity
= UVC_GUID_UVC_PROCESSING
,
692 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
695 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
696 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
697 .slave_ids
= { V4L2_CID_BLUE_BALANCE
,
698 V4L2_CID_RED_BALANCE
},
701 .id
= V4L2_CID_BLUE_BALANCE
,
702 .entity
= UVC_GUID_UVC_PROCESSING
,
703 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
706 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
707 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
708 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
712 .id
= V4L2_CID_RED_BALANCE
,
713 .entity
= UVC_GUID_UVC_PROCESSING
,
714 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
717 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
718 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
719 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
723 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
724 .entity
= UVC_GUID_UVC_CAMERA
,
725 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
728 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
729 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
730 .master_id
= V4L2_CID_FOCUS_AUTO
,
734 .id
= V4L2_CID_FOCUS_AUTO
,
735 .entity
= UVC_GUID_UVC_CAMERA
,
736 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
739 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
740 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
741 .slave_ids
= { V4L2_CID_FOCUS_ABSOLUTE
, },
744 .id
= V4L2_CID_IRIS_ABSOLUTE
,
745 .entity
= UVC_GUID_UVC_CAMERA
,
746 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
749 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
750 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
753 .id
= V4L2_CID_IRIS_RELATIVE
,
754 .entity
= UVC_GUID_UVC_CAMERA
,
755 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
758 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
759 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
762 .id
= V4L2_CID_ZOOM_ABSOLUTE
,
763 .entity
= UVC_GUID_UVC_CAMERA
,
764 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
767 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
768 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
771 .id
= V4L2_CID_ZOOM_CONTINUOUS
,
772 .entity
= UVC_GUID_UVC_CAMERA
,
773 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
776 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
777 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
778 .get
= uvc_ctrl_get_zoom
,
779 .set
= uvc_ctrl_set_zoom
,
782 .id
= V4L2_CID_PAN_ABSOLUTE
,
783 .entity
= UVC_GUID_UVC_CAMERA
,
784 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
787 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
788 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
791 .id
= V4L2_CID_TILT_ABSOLUTE
,
792 .entity
= UVC_GUID_UVC_CAMERA
,
793 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
796 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
797 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
800 .id
= V4L2_CID_PAN_SPEED
,
801 .entity
= UVC_GUID_UVC_CAMERA
,
802 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
805 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
806 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
807 .get
= uvc_ctrl_get_rel_speed
,
808 .set
= uvc_ctrl_set_rel_speed
,
811 .id
= V4L2_CID_TILT_SPEED
,
812 .entity
= UVC_GUID_UVC_CAMERA
,
813 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
816 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
817 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
818 .get
= uvc_ctrl_get_rel_speed
,
819 .set
= uvc_ctrl_set_rel_speed
,
822 .id
= V4L2_CID_PRIVACY
,
823 .entity
= UVC_GUID_UVC_CAMERA
,
824 .selector
= UVC_CT_PRIVACY_CONTROL
,
827 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
828 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
831 .id
= V4L2_CID_PRIVACY
,
832 .entity
= UVC_GUID_EXT_GPIO_CONTROLLER
,
833 .selector
= UVC_CT_PRIVACY_CONTROL
,
836 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
837 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
840 .entity
= UVC_GUID_UVC_PROCESSING
,
841 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
842 .filter_mapping
= uvc_ctrl_filter_plf_mapping
,
846 /* ------------------------------------------------------------------------
850 static inline u8
*uvc_ctrl_data(struct uvc_control
*ctrl
, int id
)
852 return ctrl
->uvc_data
+ id
* ctrl
->info
.size
;
855 static inline int uvc_test_bit(const u8
*data
, int bit
)
857 return (data
[bit
>> 3] >> (bit
& 7)) & 1;
860 static inline void uvc_clear_bit(u8
*data
, int bit
)
862 data
[bit
>> 3] &= ~(1 << (bit
& 7));
866 * Extract the bit string specified by mapping->offset and mapping->size
867 * from the little-endian data stored at 'data' and return the result as
868 * a signed 32bit integer. Sign extension will be performed if the mapping
869 * references a signed data type.
871 static s32
uvc_get_le_value(struct uvc_control_mapping
*mapping
,
872 u8 query
, const u8
*data
)
874 int bits
= mapping
->size
;
875 int offset
= mapping
->offset
;
881 mask
= ((1LL << bits
) - 1) << offset
;
884 u8 byte
= *data
& mask
;
885 value
|= offset
> 0 ? (byte
>> offset
) : (byte
<< (-offset
));
886 bits
-= 8 - max(offset
, 0);
891 mask
= (1 << bits
) - 1;
895 /* Sign-extend the value if needed. */
896 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
897 value
|= -(value
& (1 << (mapping
->size
- 1)));
903 * Set the bit string specified by mapping->offset and mapping->size
904 * in the little-endian data stored at 'data' to the value 'value'.
906 static void uvc_set_le_value(struct uvc_control_mapping
*mapping
,
909 int bits
= mapping
->size
;
910 int offset
= mapping
->offset
;
914 * According to the v4l2 spec, writing any value to a button control
915 * should result in the action belonging to the button control being
916 * triggered. UVC devices however want to see a 1 written -> override
919 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_BUTTON
)
925 for (; bits
> 0; data
++) {
926 mask
= ((1LL << bits
) - 1) << offset
;
927 *data
= (*data
& ~mask
) | ((value
<< offset
) & mask
);
928 value
>>= offset
? offset
: 8;
934 /* ------------------------------------------------------------------------
935 * Terminal and unit management
938 static int uvc_entity_match_guid(const struct uvc_entity
*entity
,
941 return memcmp(entity
->guid
, guid
, sizeof(entity
->guid
)) == 0;
944 /* ------------------------------------------------------------------------
948 static void __uvc_find_control(struct uvc_entity
*entity
, u32 v4l2_id
,
949 struct uvc_control_mapping
**mapping
, struct uvc_control
**control
,
952 struct uvc_control
*ctrl
;
953 struct uvc_control_mapping
*map
;
959 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
960 ctrl
= &entity
->controls
[i
];
961 if (!ctrl
->initialized
)
964 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
965 if ((map
->id
== v4l2_id
) && !next
) {
971 if ((*mapping
== NULL
|| (*mapping
)->id
> map
->id
) &&
972 (map
->id
> v4l2_id
) && next
) {
980 static struct uvc_control
*uvc_find_control(struct uvc_video_chain
*chain
,
981 u32 v4l2_id
, struct uvc_control_mapping
**mapping
)
983 struct uvc_control
*ctrl
= NULL
;
984 struct uvc_entity
*entity
;
985 int next
= v4l2_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
989 /* Mask the query flags. */
990 v4l2_id
&= V4L2_CTRL_ID_MASK
;
992 /* Find the control. */
993 list_for_each_entry(entity
, &chain
->entities
, chain
) {
994 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
999 if (ctrl
== NULL
&& !next
)
1000 uvc_dbg(chain
->dev
, CONTROL
, "Control 0x%08x not found\n",
1006 static int uvc_ctrl_populate_cache(struct uvc_video_chain
*chain
,
1007 struct uvc_control
*ctrl
)
1011 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_DEF
) {
1012 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_DEF
, ctrl
->entity
->id
,
1013 chain
->dev
->intfnum
, ctrl
->info
.selector
,
1014 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
),
1020 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MIN
) {
1021 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MIN
, ctrl
->entity
->id
,
1022 chain
->dev
->intfnum
, ctrl
->info
.selector
,
1023 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
),
1028 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MAX
) {
1029 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MAX
, ctrl
->entity
->id
,
1030 chain
->dev
->intfnum
, ctrl
->info
.selector
,
1031 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
),
1036 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
) {
1037 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_RES
, ctrl
->entity
->id
,
1038 chain
->dev
->intfnum
, ctrl
->info
.selector
,
1039 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
),
1042 if (UVC_ENTITY_TYPE(ctrl
->entity
) !=
1043 UVC_VC_EXTENSION_UNIT
)
1047 * GET_RES is mandatory for XU controls, but some
1048 * cameras still choke on it. Ignore errors and set the
1049 * resolution value to zero.
1051 uvc_warn_once(chain
->dev
, UVC_WARN_XU_GET_RES
,
1052 "UVC non compliance - GET_RES failed on "
1053 "an XU control. Enabling workaround.\n");
1054 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
), 0,
1063 static s32
__uvc_ctrl_get_value(struct uvc_control_mapping
*mapping
,
1066 s32 value
= mapping
->get(mapping
, UVC_GET_CUR
, data
);
1068 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
1071 for (i
= 0; BIT(i
) <= mapping
->menu_mask
; ++i
) {
1074 if (!test_bit(i
, &mapping
->menu_mask
))
1077 menu_value
= uvc_mapping_get_menu_value(mapping
, i
);
1079 if (menu_value
== value
) {
1089 static int __uvc_ctrl_load_cur(struct uvc_video_chain
*chain
,
1090 struct uvc_control
*ctrl
)
1098 data
= uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
);
1100 if ((ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
) == 0) {
1101 memset(data
, 0, ctrl
->info
.size
);
1107 if (ctrl
->entity
->get_cur
)
1108 ret
= ctrl
->entity
->get_cur(chain
->dev
, ctrl
->entity
,
1109 ctrl
->info
.selector
, data
,
1112 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
1113 ctrl
->entity
->id
, chain
->dev
->intfnum
,
1114 ctrl
->info
.selector
, data
,
1125 static int __uvc_ctrl_get(struct uvc_video_chain
*chain
,
1126 struct uvc_control
*ctrl
,
1127 struct uvc_control_mapping
*mapping
,
1132 if ((ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
) == 0)
1135 ret
= __uvc_ctrl_load_cur(chain
, ctrl
);
1139 *value
= __uvc_ctrl_get_value(mapping
,
1140 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1145 static int __uvc_query_v4l2_class(struct uvc_video_chain
*chain
, u32 req_id
,
1148 bool find_next
= req_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
1151 req_id
&= V4L2_CTRL_ID_MASK
;
1153 for (i
= 0; i
< ARRAY_SIZE(uvc_control_classes
); i
++) {
1154 if (!(chain
->ctrl_class_bitmap
& BIT(i
)))
1157 if (uvc_control_classes
[i
] == req_id
)
1161 if (uvc_control_classes
[i
] > req_id
&&
1162 uvc_control_classes
[i
] < found_id
)
1169 static int uvc_query_v4l2_class(struct uvc_video_chain
*chain
, u32 req_id
,
1170 u32 found_id
, struct v4l2_queryctrl
*v4l2_ctrl
)
1174 idx
= __uvc_query_v4l2_class(chain
, req_id
, found_id
);
1178 memset(v4l2_ctrl
, 0, sizeof(*v4l2_ctrl
));
1179 v4l2_ctrl
->id
= uvc_control_classes
[idx
];
1180 strscpy(v4l2_ctrl
->name
, v4l2_ctrl_get_name(v4l2_ctrl
->id
),
1181 sizeof(v4l2_ctrl
->name
));
1182 v4l2_ctrl
->type
= V4L2_CTRL_TYPE_CTRL_CLASS
;
1183 v4l2_ctrl
->flags
= V4L2_CTRL_FLAG_WRITE_ONLY
1184 | V4L2_CTRL_FLAG_READ_ONLY
;
1189 * Check if control @v4l2_id can be accessed by the given control @ioctl
1190 * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
1192 * For set operations on slave controls, check if the master's value is set to
1193 * manual, either in the others controls set in the same ioctl call, or from
1194 * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
1195 * both the master and slave control, such as for instance setting
1196 * auto_exposure=1, exposure_time_absolute=251.
1198 int uvc_ctrl_is_accessible(struct uvc_video_chain
*chain
, u32 v4l2_id
,
1199 const struct v4l2_ext_controls
*ctrls
,
1200 unsigned long ioctl
)
1202 struct uvc_control_mapping
*master_map
= NULL
;
1203 struct uvc_control
*master_ctrl
= NULL
;
1204 struct uvc_control_mapping
*mapping
;
1205 struct uvc_control
*ctrl
;
1206 bool read
= ioctl
== VIDIOC_G_EXT_CTRLS
;
1211 if (__uvc_query_v4l2_class(chain
, v4l2_id
, 0) >= 0)
1214 ctrl
= uvc_find_control(chain
, v4l2_id
, &mapping
);
1218 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
) && read
)
1221 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_SET_CUR
) && !read
)
1224 if (ioctl
!= VIDIOC_S_EXT_CTRLS
|| !mapping
->master_id
)
1228 * Iterate backwards in cases where the master control is accessed
1229 * multiple times in the same ioctl. We want the last value.
1231 for (i
= ctrls
->count
- 1; i
>= 0; i
--) {
1232 if (ctrls
->controls
[i
].id
== mapping
->master_id
)
1233 return ctrls
->controls
[i
].value
==
1234 mapping
->master_manual
? 0 : -EACCES
;
1237 __uvc_find_control(ctrl
->entity
, mapping
->master_id
, &master_map
,
1240 if (!master_ctrl
|| !(master_ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
))
1243 ret
= __uvc_ctrl_get(chain
, master_ctrl
, master_map
, &val
);
1244 if (ret
>= 0 && val
!= mapping
->master_manual
)
1250 static const char *uvc_map_get_name(const struct uvc_control_mapping
*map
)
1257 name
= v4l2_ctrl_get_name(map
->id
);
1261 return "Unknown Control";
1264 static u32
uvc_get_ctrl_bitmap(struct uvc_control
*ctrl
,
1265 struct uvc_control_mapping
*mapping
)
1268 * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
1269 * the number of bits supported. Those controls do not list GET_MAX
1272 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)
1273 return mapping
->get(mapping
, UVC_GET_RES
,
1274 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1276 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MAX
)
1277 return mapping
->get(mapping
, UVC_GET_MAX
,
1278 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1283 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
1284 struct uvc_control
*ctrl
,
1285 struct uvc_control_mapping
*mapping
,
1286 struct v4l2_queryctrl
*v4l2_ctrl
)
1288 struct uvc_control_mapping
*master_map
= NULL
;
1289 struct uvc_control
*master_ctrl
= NULL
;
1292 memset(v4l2_ctrl
, 0, sizeof(*v4l2_ctrl
));
1293 v4l2_ctrl
->id
= mapping
->id
;
1294 v4l2_ctrl
->type
= mapping
->v4l2_type
;
1295 strscpy(v4l2_ctrl
->name
, uvc_map_get_name(mapping
),
1296 sizeof(v4l2_ctrl
->name
));
1297 v4l2_ctrl
->flags
= 0;
1299 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
))
1300 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_WRITE_ONLY
;
1301 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_SET_CUR
))
1302 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1304 if (mapping
->master_id
)
1305 __uvc_find_control(ctrl
->entity
, mapping
->master_id
,
1306 &master_map
, &master_ctrl
, 0);
1307 if (master_ctrl
&& (master_ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
)) {
1309 int ret
= __uvc_ctrl_get(chain
, master_ctrl
, master_map
, &val
);
1313 if (val
!= mapping
->master_manual
)
1314 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
1317 if (!ctrl
->cached
) {
1318 int ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1323 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_DEF
) {
1324 v4l2_ctrl
->default_value
= mapping
->get(mapping
, UVC_GET_DEF
,
1325 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
));
1328 switch (mapping
->v4l2_type
) {
1329 case V4L2_CTRL_TYPE_MENU
:
1330 v4l2_ctrl
->minimum
= ffs(mapping
->menu_mask
) - 1;
1331 v4l2_ctrl
->maximum
= fls(mapping
->menu_mask
) - 1;
1332 v4l2_ctrl
->step
= 1;
1334 for (i
= 0; BIT(i
) <= mapping
->menu_mask
; ++i
) {
1337 if (!test_bit(i
, &mapping
->menu_mask
))
1340 menu_value
= uvc_mapping_get_menu_value(mapping
, i
);
1342 if (menu_value
== v4l2_ctrl
->default_value
) {
1343 v4l2_ctrl
->default_value
= i
;
1350 case V4L2_CTRL_TYPE_BOOLEAN
:
1351 v4l2_ctrl
->minimum
= 0;
1352 v4l2_ctrl
->maximum
= 1;
1353 v4l2_ctrl
->step
= 1;
1356 case V4L2_CTRL_TYPE_BUTTON
:
1357 v4l2_ctrl
->minimum
= 0;
1358 v4l2_ctrl
->maximum
= 0;
1359 v4l2_ctrl
->step
= 0;
1362 case V4L2_CTRL_TYPE_BITMASK
:
1363 v4l2_ctrl
->minimum
= 0;
1364 v4l2_ctrl
->maximum
= uvc_get_ctrl_bitmap(ctrl
, mapping
);
1365 v4l2_ctrl
->step
= 0;
1372 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MIN
)
1373 v4l2_ctrl
->minimum
= mapping
->get(mapping
, UVC_GET_MIN
,
1374 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1376 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MAX
)
1377 v4l2_ctrl
->maximum
= mapping
->get(mapping
, UVC_GET_MAX
,
1378 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1380 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)
1381 v4l2_ctrl
->step
= mapping
->get(mapping
, UVC_GET_RES
,
1382 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1387 int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
1388 struct v4l2_queryctrl
*v4l2_ctrl
)
1390 struct uvc_control
*ctrl
;
1391 struct uvc_control_mapping
*mapping
;
1394 ret
= mutex_lock_interruptible(&chain
->ctrl_mutex
);
1396 return -ERESTARTSYS
;
1398 /* Check if the ctrl is a know class */
1399 if (!(v4l2_ctrl
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
)) {
1400 ret
= uvc_query_v4l2_class(chain
, v4l2_ctrl
->id
, 0, v4l2_ctrl
);
1405 ctrl
= uvc_find_control(chain
, v4l2_ctrl
->id
, &mapping
);
1412 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
1413 * a class should be inserted between the previous control and the one
1414 * we have just found.
1416 if (v4l2_ctrl
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
1417 ret
= uvc_query_v4l2_class(chain
, v4l2_ctrl
->id
, mapping
->id
,
1423 ret
= __uvc_query_v4l2_ctrl(chain
, ctrl
, mapping
, v4l2_ctrl
);
1425 mutex_unlock(&chain
->ctrl_mutex
);
1430 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1431 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1432 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1433 * Balance V4L2 controls use the White Balance Component UVC control) or
1434 * otherwise translated. The approach we take here is to use a translation
1435 * table for the controls that can be mapped directly, and handle the others
1438 int uvc_query_v4l2_menu(struct uvc_video_chain
*chain
,
1439 struct v4l2_querymenu
*query_menu
)
1441 struct uvc_control_mapping
*mapping
;
1442 struct uvc_control
*ctrl
;
1443 u32 index
= query_menu
->index
;
1444 u32 id
= query_menu
->id
;
1448 memset(query_menu
, 0, sizeof(*query_menu
));
1449 query_menu
->id
= id
;
1450 query_menu
->index
= index
;
1452 if (index
>= BITS_PER_TYPE(mapping
->menu_mask
))
1455 ret
= mutex_lock_interruptible(&chain
->ctrl_mutex
);
1457 return -ERESTARTSYS
;
1459 ctrl
= uvc_find_control(chain
, query_menu
->id
, &mapping
);
1460 if (ctrl
== NULL
|| mapping
->v4l2_type
!= V4L2_CTRL_TYPE_MENU
) {
1465 if (!test_bit(query_menu
->index
, &mapping
->menu_mask
)) {
1470 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_BITMASK
) {
1473 if (!ctrl
->cached
) {
1474 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1479 mask
= uvc_mapping_get_menu_value(mapping
, query_menu
->index
);
1485 if (!(uvc_get_ctrl_bitmap(ctrl
, mapping
) & mask
)) {
1491 name
= uvc_mapping_get_menu_name(mapping
, query_menu
->index
);
1497 strscpy(query_menu
->name
, name
, sizeof(query_menu
->name
));
1500 mutex_unlock(&chain
->ctrl_mutex
);
1504 /* --------------------------------------------------------------------------
1505 * Ctrl event handling
1508 static void uvc_ctrl_fill_event(struct uvc_video_chain
*chain
,
1509 struct v4l2_event
*ev
,
1510 struct uvc_control
*ctrl
,
1511 struct uvc_control_mapping
*mapping
,
1512 s32 value
, u32 changes
)
1514 struct v4l2_queryctrl v4l2_ctrl
;
1516 __uvc_query_v4l2_ctrl(chain
, ctrl
, mapping
, &v4l2_ctrl
);
1518 memset(ev
, 0, sizeof(*ev
));
1519 ev
->type
= V4L2_EVENT_CTRL
;
1520 ev
->id
= v4l2_ctrl
.id
;
1521 ev
->u
.ctrl
.value
= value
;
1522 ev
->u
.ctrl
.changes
= changes
;
1523 ev
->u
.ctrl
.type
= v4l2_ctrl
.type
;
1524 ev
->u
.ctrl
.flags
= v4l2_ctrl
.flags
;
1525 ev
->u
.ctrl
.minimum
= v4l2_ctrl
.minimum
;
1526 ev
->u
.ctrl
.maximum
= v4l2_ctrl
.maximum
;
1527 ev
->u
.ctrl
.step
= v4l2_ctrl
.step
;
1528 ev
->u
.ctrl
.default_value
= v4l2_ctrl
.default_value
;
1532 * Send control change events to all subscribers for the @ctrl control. By
1533 * default the subscriber that generated the event, as identified by @handle,
1534 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1535 * @handle can be NULL for asynchronous events related to auto-update controls,
1536 * in which case all subscribers are notified.
1538 static void uvc_ctrl_send_event(struct uvc_video_chain
*chain
,
1539 struct uvc_fh
*handle
, struct uvc_control
*ctrl
,
1540 struct uvc_control_mapping
*mapping
, s32 value
, u32 changes
)
1542 struct v4l2_fh
*originator
= handle
? &handle
->vfh
: NULL
;
1543 struct v4l2_subscribed_event
*sev
;
1544 struct v4l2_event ev
;
1546 if (list_empty(&mapping
->ev_subs
))
1549 uvc_ctrl_fill_event(chain
, &ev
, ctrl
, mapping
, value
, changes
);
1551 list_for_each_entry(sev
, &mapping
->ev_subs
, node
) {
1552 if (sev
->fh
!= originator
||
1553 (sev
->flags
& V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK
) ||
1554 (changes
& V4L2_EVENT_CTRL_CH_FLAGS
))
1555 v4l2_event_queue_fh(sev
->fh
, &ev
);
1560 * Send control change events for the slave of the @master control identified
1561 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1562 * generated the event and may be NULL for auto-update events.
1564 static void uvc_ctrl_send_slave_event(struct uvc_video_chain
*chain
,
1565 struct uvc_fh
*handle
, struct uvc_control
*master
, u32 slave_id
)
1567 struct uvc_control_mapping
*mapping
= NULL
;
1568 struct uvc_control
*ctrl
= NULL
;
1569 u32 changes
= V4L2_EVENT_CTRL_CH_FLAGS
;
1572 __uvc_find_control(master
->entity
, slave_id
, &mapping
, &ctrl
, 0);
1576 if (__uvc_ctrl_get(chain
, ctrl
, mapping
, &val
) == 0)
1577 changes
|= V4L2_EVENT_CTRL_CH_VALUE
;
1579 uvc_ctrl_send_event(chain
, handle
, ctrl
, mapping
, val
, changes
);
1582 void uvc_ctrl_status_event(struct uvc_video_chain
*chain
,
1583 struct uvc_control
*ctrl
, const u8
*data
)
1585 struct uvc_control_mapping
*mapping
;
1586 struct uvc_fh
*handle
;
1589 mutex_lock(&chain
->ctrl_mutex
);
1591 handle
= ctrl
->handle
;
1592 ctrl
->handle
= NULL
;
1594 list_for_each_entry(mapping
, &ctrl
->info
.mappings
, list
) {
1595 s32 value
= __uvc_ctrl_get_value(mapping
, data
);
1598 * handle may be NULL here if the device sends auto-update
1599 * events without a prior related control set from userspace.
1601 for (i
= 0; i
< ARRAY_SIZE(mapping
->slave_ids
); ++i
) {
1602 if (!mapping
->slave_ids
[i
])
1605 uvc_ctrl_send_slave_event(chain
, handle
, ctrl
,
1606 mapping
->slave_ids
[i
]);
1609 uvc_ctrl_send_event(chain
, handle
, ctrl
, mapping
, value
,
1610 V4L2_EVENT_CTRL_CH_VALUE
);
1613 mutex_unlock(&chain
->ctrl_mutex
);
1616 static void uvc_ctrl_status_event_work(struct work_struct
*work
)
1618 struct uvc_device
*dev
= container_of(work
, struct uvc_device
,
1620 struct uvc_ctrl_work
*w
= &dev
->async_ctrl
;
1623 uvc_ctrl_status_event(w
->chain
, w
->ctrl
, w
->data
);
1625 /* The barrier is needed to synchronize with uvc_status_stop(). */
1626 if (smp_load_acquire(&dev
->flush_status
))
1629 /* Resubmit the URB. */
1630 w
->urb
->interval
= dev
->int_ep
->desc
.bInterval
;
1631 ret
= usb_submit_urb(w
->urb
, GFP_KERNEL
);
1633 dev_err(&dev
->udev
->dev
,
1634 "Failed to resubmit status URB (%d).\n", ret
);
1637 bool uvc_ctrl_status_event_async(struct urb
*urb
, struct uvc_video_chain
*chain
,
1638 struct uvc_control
*ctrl
, const u8
*data
)
1640 struct uvc_device
*dev
= chain
->dev
;
1641 struct uvc_ctrl_work
*w
= &dev
->async_ctrl
;
1643 if (list_empty(&ctrl
->info
.mappings
)) {
1644 ctrl
->handle
= NULL
;
1653 schedule_work(&w
->work
);
1658 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control
*xctrls
,
1659 unsigned int xctrls_count
, u32 id
)
1663 for (i
= 0; i
< xctrls_count
; ++i
) {
1664 if (xctrls
[i
].id
== id
)
1671 static void uvc_ctrl_send_events(struct uvc_fh
*handle
,
1672 const struct v4l2_ext_control
*xctrls
, unsigned int xctrls_count
)
1674 struct uvc_control_mapping
*mapping
;
1675 struct uvc_control
*ctrl
;
1676 u32 changes
= V4L2_EVENT_CTRL_CH_VALUE
;
1680 for (i
= 0; i
< xctrls_count
; ++i
) {
1681 ctrl
= uvc_find_control(handle
->chain
, xctrls
[i
].id
, &mapping
);
1683 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_ASYNCHRONOUS
)
1684 /* Notification will be sent from an Interrupt event. */
1687 for (j
= 0; j
< ARRAY_SIZE(mapping
->slave_ids
); ++j
) {
1688 u32 slave_id
= mapping
->slave_ids
[j
];
1694 * We can skip sending an event for the slave if the
1695 * slave is being modified in the same transaction.
1697 if (uvc_ctrl_xctrls_has_control(xctrls
, xctrls_count
,
1701 uvc_ctrl_send_slave_event(handle
->chain
, handle
, ctrl
,
1706 * If the master is being modified in the same transaction
1707 * flags may change too.
1709 if (mapping
->master_id
&&
1710 uvc_ctrl_xctrls_has_control(xctrls
, xctrls_count
,
1711 mapping
->master_id
))
1712 changes
|= V4L2_EVENT_CTRL_CH_FLAGS
;
1714 uvc_ctrl_send_event(handle
->chain
, handle
, ctrl
, mapping
,
1715 xctrls
[i
].value
, changes
);
1719 static int uvc_ctrl_add_event(struct v4l2_subscribed_event
*sev
, unsigned elems
)
1721 struct uvc_fh
*handle
= container_of(sev
->fh
, struct uvc_fh
, vfh
);
1722 struct uvc_control_mapping
*mapping
;
1723 struct uvc_control
*ctrl
;
1726 ret
= mutex_lock_interruptible(&handle
->chain
->ctrl_mutex
);
1728 return -ERESTARTSYS
;
1730 if (__uvc_query_v4l2_class(handle
->chain
, sev
->id
, 0) >= 0) {
1735 ctrl
= uvc_find_control(handle
->chain
, sev
->id
, &mapping
);
1741 list_add_tail(&sev
->node
, &mapping
->ev_subs
);
1742 if (sev
->flags
& V4L2_EVENT_SUB_FL_SEND_INITIAL
) {
1743 struct v4l2_event ev
;
1744 u32 changes
= V4L2_EVENT_CTRL_CH_FLAGS
;
1747 if (__uvc_ctrl_get(handle
->chain
, ctrl
, mapping
, &val
) == 0)
1748 changes
|= V4L2_EVENT_CTRL_CH_VALUE
;
1750 uvc_ctrl_fill_event(handle
->chain
, &ev
, ctrl
, mapping
, val
,
1753 * Mark the queue as active, allowing this initial event to be
1757 v4l2_event_queue_fh(sev
->fh
, &ev
);
1761 mutex_unlock(&handle
->chain
->ctrl_mutex
);
1765 static void uvc_ctrl_del_event(struct v4l2_subscribed_event
*sev
)
1767 struct uvc_fh
*handle
= container_of(sev
->fh
, struct uvc_fh
, vfh
);
1769 mutex_lock(&handle
->chain
->ctrl_mutex
);
1770 if (__uvc_query_v4l2_class(handle
->chain
, sev
->id
, 0) >= 0)
1772 list_del(&sev
->node
);
1774 mutex_unlock(&handle
->chain
->ctrl_mutex
);
1777 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops
= {
1778 .add
= uvc_ctrl_add_event
,
1779 .del
= uvc_ctrl_del_event
,
1780 .replace
= v4l2_ctrl_replace
,
1781 .merge
= v4l2_ctrl_merge
,
1784 /* --------------------------------------------------------------------------
1785 * Control transactions
1787 * To make extended set operations as atomic as the hardware allows, controls
1788 * are handled using begin/commit/rollback operations.
1790 * At the beginning of a set request, uvc_ctrl_begin should be called to
1791 * initialize the request. This function acquires the control lock.
1793 * When setting a control, the new value is stored in the control data field
1794 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1795 * later processing. If the UVC and V4L2 control sizes differ, the current
1796 * value is loaded from the hardware before storing the new value in the data
1799 * After processing all controls in the transaction, uvc_ctrl_commit or
1800 * uvc_ctrl_rollback must be called to apply the pending changes to the
1801 * hardware or revert them. When applying changes, all controls marked as
1802 * dirty will be modified in the UVC device, and the dirty flag will be
1803 * cleared. When reverting controls, the control data field
1804 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1805 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1808 int uvc_ctrl_begin(struct uvc_video_chain
*chain
)
1810 return mutex_lock_interruptible(&chain
->ctrl_mutex
) ? -ERESTARTSYS
: 0;
1813 static int uvc_ctrl_commit_entity(struct uvc_device
*dev
,
1814 struct uvc_entity
*entity
, int rollback
, struct uvc_control
**err_ctrl
)
1816 struct uvc_control
*ctrl
;
1823 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1824 ctrl
= &entity
->controls
[i
];
1825 if (!ctrl
->initialized
)
1829 * Reset the loaded flag for auto-update controls that were
1830 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1831 * uvc_ctrl_get from using the cached value, and for write-only
1832 * controls to prevent uvc_ctrl_set from setting bits not
1833 * explicitly set by the user.
1835 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_AUTO_UPDATE
||
1836 !(ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
))
1843 ret
= uvc_query_ctrl(dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
1844 dev
->intfnum
, ctrl
->info
.selector
,
1845 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1850 if (rollback
|| ret
< 0)
1851 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1852 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1867 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity
*entity
,
1868 struct v4l2_ext_controls
*ctrls
,
1869 struct uvc_control
*uvc_control
)
1871 struct uvc_control_mapping
*mapping
= NULL
;
1872 struct uvc_control
*ctrl_found
= NULL
;
1876 return ctrls
->count
;
1878 for (i
= 0; i
< ctrls
->count
; i
++) {
1879 __uvc_find_control(entity
, ctrls
->controls
[i
].id
, &mapping
,
1881 if (uvc_control
== ctrl_found
)
1885 return ctrls
->count
;
1888 int __uvc_ctrl_commit(struct uvc_fh
*handle
, int rollback
,
1889 struct v4l2_ext_controls
*ctrls
)
1891 struct uvc_video_chain
*chain
= handle
->chain
;
1892 struct uvc_control
*err_ctrl
;
1893 struct uvc_entity
*entity
;
1896 /* Find the control. */
1897 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1898 ret
= uvc_ctrl_commit_entity(chain
->dev
, entity
, rollback
,
1903 uvc_ctrl_find_ctrl_idx(entity
, ctrls
,
1910 uvc_ctrl_send_events(handle
, ctrls
->controls
, ctrls
->count
);
1912 mutex_unlock(&chain
->ctrl_mutex
);
1916 int uvc_ctrl_get(struct uvc_video_chain
*chain
,
1917 struct v4l2_ext_control
*xctrl
)
1919 struct uvc_control
*ctrl
;
1920 struct uvc_control_mapping
*mapping
;
1922 if (__uvc_query_v4l2_class(chain
, xctrl
->id
, 0) >= 0)
1925 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1929 return __uvc_ctrl_get(chain
, ctrl
, mapping
, &xctrl
->value
);
1932 int uvc_ctrl_set(struct uvc_fh
*handle
,
1933 struct v4l2_ext_control
*xctrl
)
1935 struct uvc_video_chain
*chain
= handle
->chain
;
1936 struct uvc_control
*ctrl
;
1937 struct uvc_control_mapping
*mapping
;
1944 if (__uvc_query_v4l2_class(chain
, xctrl
->id
, 0) >= 0)
1947 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1950 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_SET_CUR
))
1953 /* Clamp out of range values. */
1954 switch (mapping
->v4l2_type
) {
1955 case V4L2_CTRL_TYPE_INTEGER
:
1956 if (!ctrl
->cached
) {
1957 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1962 min
= mapping
->get(mapping
, UVC_GET_MIN
,
1963 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1964 max
= mapping
->get(mapping
, UVC_GET_MAX
,
1965 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1966 step
= mapping
->get(mapping
, UVC_GET_RES
,
1967 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1971 xctrl
->value
= min
+ DIV_ROUND_CLOSEST((u32
)(xctrl
->value
- min
),
1973 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
1974 xctrl
->value
= clamp(xctrl
->value
, min
, max
);
1976 xctrl
->value
= clamp_t(u32
, xctrl
->value
, min
, max
);
1977 value
= xctrl
->value
;
1980 case V4L2_CTRL_TYPE_BITMASK
:
1981 if (!ctrl
->cached
) {
1982 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1987 xctrl
->value
&= uvc_get_ctrl_bitmap(ctrl
, mapping
);
1988 value
= xctrl
->value
;
1991 case V4L2_CTRL_TYPE_BOOLEAN
:
1992 xctrl
->value
= clamp(xctrl
->value
, 0, 1);
1993 value
= xctrl
->value
;
1996 case V4L2_CTRL_TYPE_MENU
:
1997 if (xctrl
->value
< (ffs(mapping
->menu_mask
) - 1) ||
1998 xctrl
->value
> (fls(mapping
->menu_mask
) - 1))
2001 if (!test_bit(xctrl
->value
, &mapping
->menu_mask
))
2004 value
= uvc_mapping_get_menu_value(mapping
, xctrl
->value
);
2007 * Valid menu indices are reported by the GET_RES request for
2008 * UVC controls that support it.
2010 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_BITMASK
) {
2011 if (!ctrl
->cached
) {
2012 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
2017 if (!(uvc_get_ctrl_bitmap(ctrl
, mapping
) & value
))
2024 value
= xctrl
->value
;
2029 * If the mapping doesn't span the whole UVC control, the current value
2030 * needs to be loaded from the device to perform the read-modify-write
2033 if ((ctrl
->info
.size
* 8) != mapping
->size
) {
2034 ret
= __uvc_ctrl_load_cur(chain
, ctrl
);
2039 /* Backup the current value in case we need to rollback later. */
2041 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
2042 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
2046 mapping
->set(mapping
, value
,
2047 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
2049 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_ASYNCHRONOUS
)
2050 ctrl
->handle
= handle
;
2057 /* --------------------------------------------------------------------------
2062 * Retrieve flags for a given control
2064 static int uvc_ctrl_get_flags(struct uvc_device
*dev
,
2065 const struct uvc_control
*ctrl
,
2066 struct uvc_control_info
*info
)
2071 data
= kmalloc(1, GFP_KERNEL
);
2075 if (ctrl
->entity
->get_info
)
2076 ret
= ctrl
->entity
->get_info(dev
, ctrl
->entity
,
2077 ctrl
->info
.selector
, data
);
2079 ret
= uvc_query_ctrl(dev
, UVC_GET_INFO
, ctrl
->entity
->id
,
2080 dev
->intfnum
, info
->selector
, data
, 1);
2083 info
->flags
&= ~(UVC_CTRL_FLAG_GET_CUR
|
2084 UVC_CTRL_FLAG_SET_CUR
|
2085 UVC_CTRL_FLAG_AUTO_UPDATE
|
2086 UVC_CTRL_FLAG_ASYNCHRONOUS
);
2088 info
->flags
|= (data
[0] & UVC_CONTROL_CAP_GET
?
2089 UVC_CTRL_FLAG_GET_CUR
: 0)
2090 | (data
[0] & UVC_CONTROL_CAP_SET
?
2091 UVC_CTRL_FLAG_SET_CUR
: 0)
2092 | (data
[0] & UVC_CONTROL_CAP_AUTOUPDATE
?
2093 UVC_CTRL_FLAG_AUTO_UPDATE
: 0)
2094 | (data
[0] & UVC_CONTROL_CAP_ASYNCHRONOUS
?
2095 UVC_CTRL_FLAG_ASYNCHRONOUS
: 0);
2102 static void uvc_ctrl_fixup_xu_info(struct uvc_device
*dev
,
2103 const struct uvc_control
*ctrl
, struct uvc_control_info
*info
)
2105 struct uvc_ctrl_fixup
{
2106 struct usb_device_id id
;
2112 static const struct uvc_ctrl_fixup fixups
[] = {
2113 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2114 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
2115 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
2116 UVC_CTRL_FLAG_AUTO_UPDATE
},
2117 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2118 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
2119 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
2120 UVC_CTRL_FLAG_AUTO_UPDATE
},
2121 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2122 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
2123 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
2124 UVC_CTRL_FLAG_AUTO_UPDATE
},
2129 for (i
= 0; i
< ARRAY_SIZE(fixups
); ++i
) {
2130 if (!usb_match_one_id(dev
->intf
, &fixups
[i
].id
))
2133 if (fixups
[i
].entity
== ctrl
->entity
->id
&&
2134 fixups
[i
].selector
== info
->selector
) {
2135 info
->flags
= fixups
[i
].flags
;
2142 * Query control information (size and flags) for XU controls.
2144 static int uvc_ctrl_fill_xu_info(struct uvc_device
*dev
,
2145 const struct uvc_control
*ctrl
, struct uvc_control_info
*info
)
2150 data
= kmalloc(2, GFP_KERNEL
);
2154 memcpy(info
->entity
, ctrl
->entity
->guid
, sizeof(info
->entity
));
2155 info
->index
= ctrl
->index
;
2156 info
->selector
= ctrl
->index
+ 1;
2158 /* Query and verify the control length (GET_LEN) */
2159 ret
= uvc_query_ctrl(dev
, UVC_GET_LEN
, ctrl
->entity
->id
, dev
->intfnum
,
2160 info
->selector
, data
, 2);
2162 uvc_dbg(dev
, CONTROL
,
2163 "GET_LEN failed on control %pUl/%u (%d)\n",
2164 info
->entity
, info
->selector
, ret
);
2168 info
->size
= le16_to_cpup((__le16
*)data
);
2170 info
->flags
= UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
2171 | UVC_CTRL_FLAG_GET_RES
| UVC_CTRL_FLAG_GET_DEF
;
2173 ret
= uvc_ctrl_get_flags(dev
, ctrl
, info
);
2175 uvc_dbg(dev
, CONTROL
,
2176 "Failed to get flags for control %pUl/%u (%d)\n",
2177 info
->entity
, info
->selector
, ret
);
2181 uvc_ctrl_fixup_xu_info(dev
, ctrl
, info
);
2183 uvc_dbg(dev
, CONTROL
,
2184 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2185 info
->entity
, info
->selector
, info
->size
,
2186 (info
->flags
& UVC_CTRL_FLAG_GET_CUR
) ? 1 : 0,
2187 (info
->flags
& UVC_CTRL_FLAG_SET_CUR
) ? 1 : 0,
2188 (info
->flags
& UVC_CTRL_FLAG_AUTO_UPDATE
) ? 1 : 0);
2195 static int uvc_ctrl_add_info(struct uvc_device
*dev
, struct uvc_control
*ctrl
,
2196 const struct uvc_control_info
*info
);
2198 static int uvc_ctrl_init_xu_ctrl(struct uvc_device
*dev
,
2199 struct uvc_control
*ctrl
)
2201 struct uvc_control_info info
;
2204 if (ctrl
->initialized
)
2207 ret
= uvc_ctrl_fill_xu_info(dev
, ctrl
, &info
);
2211 ret
= uvc_ctrl_add_info(dev
, ctrl
, &info
);
2213 uvc_dbg(dev
, CONTROL
,
2214 "Failed to initialize control %pUl/%u on device %s entity %u\n",
2215 info
.entity
, info
.selector
, dev
->udev
->devpath
,
2221 int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
2222 struct uvc_xu_control_query
*xqry
)
2224 struct uvc_entity
*entity
, *iter
;
2225 struct uvc_control
*ctrl
;
2233 /* Find the extension unit. */
2235 list_for_each_entry(iter
, &chain
->entities
, chain
) {
2236 if (UVC_ENTITY_TYPE(iter
) == UVC_VC_EXTENSION_UNIT
&&
2237 iter
->id
== xqry
->unit
) {
2244 uvc_dbg(chain
->dev
, CONTROL
, "Extension unit %u not found\n",
2249 /* Find the control and perform delayed initialization if needed. */
2251 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
2252 ctrl
= &entity
->controls
[i
];
2253 if (ctrl
->index
== xqry
->selector
- 1) {
2260 uvc_dbg(chain
->dev
, CONTROL
, "Control %pUl/%u not found\n",
2261 entity
->guid
, xqry
->selector
);
2265 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
2266 return -ERESTARTSYS
;
2268 ret
= uvc_ctrl_init_xu_ctrl(chain
->dev
, ctrl
);
2274 /* Validate the required buffer size and flags for the request */
2276 size
= ctrl
->info
.size
;
2278 switch (xqry
->query
) {
2280 reqflags
= UVC_CTRL_FLAG_GET_CUR
;
2283 reqflags
= UVC_CTRL_FLAG_GET_MIN
;
2286 reqflags
= UVC_CTRL_FLAG_GET_MAX
;
2289 reqflags
= UVC_CTRL_FLAG_GET_DEF
;
2292 reqflags
= UVC_CTRL_FLAG_GET_RES
;
2295 reqflags
= UVC_CTRL_FLAG_SET_CUR
;
2308 if (size
!= xqry
->size
) {
2313 if (reqflags
&& !(ctrl
->info
.flags
& reqflags
)) {
2318 data
= kmalloc(size
, GFP_KERNEL
);
2324 if (xqry
->query
== UVC_SET_CUR
&&
2325 copy_from_user(data
, xqry
->data
, size
)) {
2330 ret
= uvc_query_ctrl(chain
->dev
, xqry
->query
, xqry
->unit
,
2331 chain
->dev
->intfnum
, xqry
->selector
, data
, size
);
2335 if (xqry
->query
!= UVC_SET_CUR
&&
2336 copy_to_user(xqry
->data
, data
, size
))
2340 mutex_unlock(&chain
->ctrl_mutex
);
2344 /* --------------------------------------------------------------------------
2349 * Restore control values after resume, skipping controls that haven't been
2353 * - Don't restore modified controls that are back to their default value.
2354 * - Handle restore order (Auto-Exposure Mode should be restored before
2357 int uvc_ctrl_restore_values(struct uvc_device
*dev
)
2359 struct uvc_control
*ctrl
;
2360 struct uvc_entity
*entity
;
2364 /* Walk the entities list and restore controls when possible. */
2365 list_for_each_entry(entity
, &dev
->entities
, list
) {
2367 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
2368 ctrl
= &entity
->controls
[i
];
2370 if (!ctrl
->initialized
|| !ctrl
->modified
||
2371 (ctrl
->info
.flags
& UVC_CTRL_FLAG_RESTORE
) == 0)
2373 dev_dbg(&dev
->udev
->dev
,
2374 "restoring control %pUl/%u/%u\n",
2375 ctrl
->info
.entity
, ctrl
->info
.index
,
2376 ctrl
->info
.selector
);
2380 ret
= uvc_ctrl_commit_entity(dev
, entity
, 0, NULL
);
2388 /* --------------------------------------------------------------------------
2389 * Control and mapping handling
2393 * Add control information to a given control.
2395 static int uvc_ctrl_add_info(struct uvc_device
*dev
, struct uvc_control
*ctrl
,
2396 const struct uvc_control_info
*info
)
2399 INIT_LIST_HEAD(&ctrl
->info
.mappings
);
2401 /* Allocate an array to save control values (cur, def, max, etc.) */
2402 ctrl
->uvc_data
= kzalloc(ctrl
->info
.size
* UVC_CTRL_DATA_LAST
+ 1,
2404 if (!ctrl
->uvc_data
)
2407 ctrl
->initialized
= 1;
2409 uvc_dbg(dev
, CONTROL
, "Added control %pUl/%u to device %s entity %u\n",
2410 ctrl
->info
.entity
, ctrl
->info
.selector
, dev
->udev
->devpath
,
2417 * Add a control mapping to a given control.
2419 static int __uvc_ctrl_add_mapping(struct uvc_video_chain
*chain
,
2420 struct uvc_control
*ctrl
, const struct uvc_control_mapping
*mapping
)
2422 struct uvc_control_mapping
*map
;
2427 * Most mappings come from static kernel data, and need to be duplicated.
2428 * Mappings that come from userspace will be unnecessarily duplicated,
2429 * this could be optimized.
2431 map
= kmemdup(mapping
, sizeof(*mapping
), GFP_KERNEL
);
2436 map
->menu_names
= NULL
;
2437 map
->menu_mapping
= NULL
;
2439 /* For UVCIOC_CTRL_MAP custom control */
2440 if (mapping
->name
) {
2441 map
->name
= kstrdup(mapping
->name
, GFP_KERNEL
);
2446 INIT_LIST_HEAD(&map
->ev_subs
);
2448 if (mapping
->menu_mapping
&& mapping
->menu_mask
) {
2449 size
= sizeof(mapping
->menu_mapping
[0])
2450 * fls(mapping
->menu_mask
);
2451 map
->menu_mapping
= kmemdup(mapping
->menu_mapping
, size
,
2453 if (!map
->menu_mapping
)
2456 if (mapping
->menu_names
&& mapping
->menu_mask
) {
2457 size
= sizeof(mapping
->menu_names
[0])
2458 * fls(mapping
->menu_mask
);
2459 map
->menu_names
= kmemdup(mapping
->menu_names
, size
,
2461 if (!map
->menu_names
)
2465 if (map
->get
== NULL
)
2466 map
->get
= uvc_get_le_value
;
2467 if (map
->set
== NULL
)
2468 map
->set
= uvc_set_le_value
;
2470 for (i
= 0; i
< ARRAY_SIZE(uvc_control_classes
); i
++) {
2471 if (V4L2_CTRL_ID2WHICH(uvc_control_classes
[i
]) ==
2472 V4L2_CTRL_ID2WHICH(map
->id
)) {
2473 chain
->ctrl_class_bitmap
|= BIT(i
);
2478 list_add_tail(&map
->list
, &ctrl
->info
.mappings
);
2479 uvc_dbg(chain
->dev
, CONTROL
, "Adding mapping '%s' to control %pUl/%u\n",
2480 uvc_map_get_name(map
), ctrl
->info
.entity
,
2481 ctrl
->info
.selector
);
2486 kfree(map
->menu_names
);
2487 kfree(map
->menu_mapping
);
2493 int uvc_ctrl_add_mapping(struct uvc_video_chain
*chain
,
2494 const struct uvc_control_mapping
*mapping
)
2496 struct uvc_device
*dev
= chain
->dev
;
2497 struct uvc_control_mapping
*map
;
2498 struct uvc_entity
*entity
;
2499 struct uvc_control
*ctrl
;
2503 if (mapping
->id
& ~V4L2_CTRL_ID_MASK
) {
2504 uvc_dbg(dev
, CONTROL
,
2505 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2506 uvc_map_get_name(mapping
), mapping
->id
);
2510 /* Search for the matching (GUID/CS) control on the current chain */
2511 list_for_each_entry(entity
, &chain
->entities
, chain
) {
2514 if (UVC_ENTITY_TYPE(entity
) != UVC_VC_EXTENSION_UNIT
||
2515 !uvc_entity_match_guid(entity
, mapping
->entity
))
2518 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
2519 ctrl
= &entity
->controls
[i
];
2520 if (ctrl
->index
== mapping
->selector
- 1) {
2532 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
2533 return -ERESTARTSYS
;
2535 /* Perform delayed initialization of XU controls */
2536 ret
= uvc_ctrl_init_xu_ctrl(dev
, ctrl
);
2542 /* Validate the user-provided bit-size and offset */
2543 if (mapping
->size
> 32 ||
2544 mapping
->offset
+ mapping
->size
> ctrl
->info
.size
* 8) {
2549 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
2550 if (mapping
->id
== map
->id
) {
2551 uvc_dbg(dev
, CONTROL
,
2552 "Can't add mapping '%s', control id 0x%08x already exists\n",
2553 uvc_map_get_name(mapping
), mapping
->id
);
2559 /* Prevent excess memory consumption */
2560 if (atomic_inc_return(&dev
->nmappings
) > UVC_MAX_CONTROL_MAPPINGS
) {
2561 atomic_dec(&dev
->nmappings
);
2562 uvc_dbg(dev
, CONTROL
,
2563 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
2564 uvc_map_get_name(mapping
), UVC_MAX_CONTROL_MAPPINGS
);
2569 ret
= __uvc_ctrl_add_mapping(chain
, ctrl
, mapping
);
2571 atomic_dec(&dev
->nmappings
);
2574 mutex_unlock(&chain
->ctrl_mutex
);
2579 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2580 * are currently the ones that crash the camera or unconditionally return an
2581 * error when queried.
2583 static void uvc_ctrl_prune_entity(struct uvc_device
*dev
,
2584 struct uvc_entity
*entity
)
2586 struct uvc_ctrl_blacklist
{
2587 struct usb_device_id id
;
2591 static const struct uvc_ctrl_blacklist processing_blacklist
[] = {
2592 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2593 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2594 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2596 static const struct uvc_ctrl_blacklist camera_blacklist
[] = {
2597 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2600 const struct uvc_ctrl_blacklist
*blacklist
;
2606 switch (UVC_ENTITY_TYPE(entity
)) {
2607 case UVC_VC_PROCESSING_UNIT
:
2608 blacklist
= processing_blacklist
;
2609 count
= ARRAY_SIZE(processing_blacklist
);
2610 controls
= entity
->processing
.bmControls
;
2611 size
= entity
->processing
.bControlSize
;
2614 case UVC_ITT_CAMERA
:
2615 blacklist
= camera_blacklist
;
2616 count
= ARRAY_SIZE(camera_blacklist
);
2617 controls
= entity
->camera
.bmControls
;
2618 size
= entity
->camera
.bControlSize
;
2625 for (i
= 0; i
< count
; ++i
) {
2626 if (!usb_match_one_id(dev
->intf
, &blacklist
[i
].id
))
2629 if (blacklist
[i
].index
>= 8 * size
||
2630 !uvc_test_bit(controls
, blacklist
[i
].index
))
2633 uvc_dbg(dev
, CONTROL
,
2634 "%u/%u control is black listed, removing it\n",
2635 entity
->id
, blacklist
[i
].index
);
2637 uvc_clear_bit(controls
, blacklist
[i
].index
);
2642 * Add control information and hardcoded stock control mappings to the given
2645 static void uvc_ctrl_init_ctrl(struct uvc_video_chain
*chain
,
2646 struct uvc_control
*ctrl
)
2651 * XU controls initialization requires querying the device for control
2652 * information. As some buggy UVC devices will crash when queried
2653 * repeatedly in a tight loop, delay XU controls initialization until
2656 if (UVC_ENTITY_TYPE(ctrl
->entity
) == UVC_VC_EXTENSION_UNIT
)
2659 for (i
= 0; i
< ARRAY_SIZE(uvc_ctrls
); ++i
) {
2660 const struct uvc_control_info
*info
= &uvc_ctrls
[i
];
2662 if (uvc_entity_match_guid(ctrl
->entity
, info
->entity
) &&
2663 ctrl
->index
== info
->index
) {
2664 uvc_ctrl_add_info(chain
->dev
, ctrl
, info
);
2666 * Retrieve control flags from the device. Ignore errors
2667 * and work with default flag values from the uvc_ctrl
2668 * array when the device doesn't properly implement
2669 * GET_INFO on standard controls.
2671 uvc_ctrl_get_flags(chain
->dev
, ctrl
, &ctrl
->info
);
2676 if (!ctrl
->initialized
)
2679 /* Process common mappings. */
2680 for (i
= 0; i
< ARRAY_SIZE(uvc_ctrl_mappings
); ++i
) {
2681 const struct uvc_control_mapping
*mapping
= &uvc_ctrl_mappings
[i
];
2683 if (!uvc_entity_match_guid(ctrl
->entity
, mapping
->entity
) ||
2684 ctrl
->info
.selector
!= mapping
->selector
)
2687 /* Let the device provide a custom mapping. */
2688 if (mapping
->filter_mapping
) {
2689 mapping
= mapping
->filter_mapping(chain
, ctrl
);
2694 __uvc_ctrl_add_mapping(chain
, ctrl
, mapping
);
2699 * Initialize device controls.
2701 static int uvc_ctrl_init_chain(struct uvc_video_chain
*chain
)
2703 struct uvc_entity
*entity
;
2706 /* Walk the entities list and instantiate controls */
2707 list_for_each_entry(entity
, &chain
->entities
, chain
) {
2708 struct uvc_control
*ctrl
;
2709 unsigned int bControlSize
= 0, ncontrols
;
2710 u8
*bmControls
= NULL
;
2712 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
2713 bmControls
= entity
->extension
.bmControls
;
2714 bControlSize
= entity
->extension
.bControlSize
;
2715 } else if (UVC_ENTITY_TYPE(entity
) == UVC_VC_PROCESSING_UNIT
) {
2716 bmControls
= entity
->processing
.bmControls
;
2717 bControlSize
= entity
->processing
.bControlSize
;
2718 } else if (UVC_ENTITY_TYPE(entity
) == UVC_ITT_CAMERA
) {
2719 bmControls
= entity
->camera
.bmControls
;
2720 bControlSize
= entity
->camera
.bControlSize
;
2721 } else if (UVC_ENTITY_TYPE(entity
) == UVC_EXT_GPIO_UNIT
) {
2722 bmControls
= entity
->gpio
.bmControls
;
2723 bControlSize
= entity
->gpio
.bControlSize
;
2726 /* Remove bogus/blacklisted controls */
2727 uvc_ctrl_prune_entity(chain
->dev
, entity
);
2729 /* Count supported controls and allocate the controls array */
2730 ncontrols
= memweight(bmControls
, bControlSize
);
2734 entity
->controls
= kcalloc(ncontrols
, sizeof(*ctrl
),
2736 if (entity
->controls
== NULL
)
2738 entity
->ncontrols
= ncontrols
;
2740 /* Initialize all supported controls */
2741 ctrl
= entity
->controls
;
2742 for (i
= 0; i
< bControlSize
* 8; ++i
) {
2743 if (uvc_test_bit(bmControls
, i
) == 0)
2746 ctrl
->entity
= entity
;
2749 uvc_ctrl_init_ctrl(chain
, ctrl
);
2757 int uvc_ctrl_init_device(struct uvc_device
*dev
)
2759 struct uvc_video_chain
*chain
;
2762 INIT_WORK(&dev
->async_ctrl
.work
, uvc_ctrl_status_event_work
);
2764 list_for_each_entry(chain
, &dev
->chains
, list
) {
2765 ret
= uvc_ctrl_init_chain(chain
);
2774 * Cleanup device controls.
2776 static void uvc_ctrl_cleanup_mappings(struct uvc_device
*dev
,
2777 struct uvc_control
*ctrl
)
2779 struct uvc_control_mapping
*mapping
, *nm
;
2781 list_for_each_entry_safe(mapping
, nm
, &ctrl
->info
.mappings
, list
) {
2782 list_del(&mapping
->list
);
2783 kfree(mapping
->menu_names
);
2784 kfree(mapping
->menu_mapping
);
2785 kfree(mapping
->name
);
2790 void uvc_ctrl_cleanup_device(struct uvc_device
*dev
)
2792 struct uvc_entity
*entity
;
2795 /* Can be uninitialized if we are aborting on probe error. */
2796 if (dev
->async_ctrl
.work
.func
)
2797 cancel_work_sync(&dev
->async_ctrl
.work
);
2799 /* Free controls and control mappings for all entities. */
2800 list_for_each_entry(entity
, &dev
->entities
, list
) {
2801 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
2802 struct uvc_control
*ctrl
= &entity
->controls
[i
];
2804 if (!ctrl
->initialized
)
2807 uvc_ctrl_cleanup_mappings(dev
, ctrl
);
2808 kfree(ctrl
->uvc_data
);
2811 kfree(entity
->controls
);