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 <linux/kernel.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <linux/usb.h>
15 #include <linux/videodev2.h>
16 #include <linux/vmalloc.h>
17 #include <linux/wait.h>
18 #include <linux/workqueue.h>
19 #include <linux/atomic.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-uvc.h>
25 #define UVC_CTRL_DATA_CURRENT 0
26 #define UVC_CTRL_DATA_BACKUP 1
27 #define UVC_CTRL_DATA_MIN 2
28 #define UVC_CTRL_DATA_MAX 3
29 #define UVC_CTRL_DATA_RES 4
30 #define UVC_CTRL_DATA_DEF 5
31 #define UVC_CTRL_DATA_LAST 6
33 /* ------------------------------------------------------------------------
37 static const struct uvc_control_info uvc_ctrls[] = {
39 .entity = UVC_GUID_UVC_PROCESSING,
40 .selector = UVC_PU_BRIGHTNESS_CONTROL,
43 .flags = UVC_CTRL_FLAG_SET_CUR
44 | UVC_CTRL_FLAG_GET_RANGE
45 | UVC_CTRL_FLAG_RESTORE,
48 .entity = UVC_GUID_UVC_PROCESSING,
49 .selector = UVC_PU_CONTRAST_CONTROL,
52 .flags = UVC_CTRL_FLAG_SET_CUR
53 | UVC_CTRL_FLAG_GET_RANGE
54 | UVC_CTRL_FLAG_RESTORE,
57 .entity = UVC_GUID_UVC_PROCESSING,
58 .selector = UVC_PU_HUE_CONTROL,
61 .flags = UVC_CTRL_FLAG_SET_CUR
62 | UVC_CTRL_FLAG_GET_RANGE
63 | UVC_CTRL_FLAG_RESTORE
64 | UVC_CTRL_FLAG_AUTO_UPDATE,
67 .entity = UVC_GUID_UVC_PROCESSING,
68 .selector = UVC_PU_SATURATION_CONTROL,
71 .flags = UVC_CTRL_FLAG_SET_CUR
72 | UVC_CTRL_FLAG_GET_RANGE
73 | UVC_CTRL_FLAG_RESTORE,
76 .entity = UVC_GUID_UVC_PROCESSING,
77 .selector = UVC_PU_SHARPNESS_CONTROL,
80 .flags = UVC_CTRL_FLAG_SET_CUR
81 | UVC_CTRL_FLAG_GET_RANGE
82 | UVC_CTRL_FLAG_RESTORE,
85 .entity = UVC_GUID_UVC_PROCESSING,
86 .selector = UVC_PU_GAMMA_CONTROL,
89 .flags = UVC_CTRL_FLAG_SET_CUR
90 | UVC_CTRL_FLAG_GET_RANGE
91 | UVC_CTRL_FLAG_RESTORE,
94 .entity = UVC_GUID_UVC_PROCESSING,
95 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
98 .flags = UVC_CTRL_FLAG_SET_CUR
99 | UVC_CTRL_FLAG_GET_RANGE
100 | UVC_CTRL_FLAG_RESTORE
101 | UVC_CTRL_FLAG_AUTO_UPDATE,
104 .entity = UVC_GUID_UVC_PROCESSING,
105 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
108 .flags = UVC_CTRL_FLAG_SET_CUR
109 | UVC_CTRL_FLAG_GET_RANGE
110 | UVC_CTRL_FLAG_RESTORE
111 | UVC_CTRL_FLAG_AUTO_UPDATE,
114 .entity = UVC_GUID_UVC_PROCESSING,
115 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
118 .flags = UVC_CTRL_FLAG_SET_CUR
119 | UVC_CTRL_FLAG_GET_RANGE
120 | UVC_CTRL_FLAG_RESTORE,
123 .entity = UVC_GUID_UVC_PROCESSING,
124 .selector = UVC_PU_GAIN_CONTROL,
127 .flags = UVC_CTRL_FLAG_SET_CUR
128 | UVC_CTRL_FLAG_GET_RANGE
129 | UVC_CTRL_FLAG_RESTORE,
132 .entity = UVC_GUID_UVC_PROCESSING,
133 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
136 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
137 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
140 .entity = UVC_GUID_UVC_PROCESSING,
141 .selector = UVC_PU_HUE_AUTO_CONTROL,
144 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
145 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
148 .entity = UVC_GUID_UVC_PROCESSING,
149 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
152 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
153 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
156 .entity = UVC_GUID_UVC_PROCESSING,
157 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
160 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
161 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
164 .entity = UVC_GUID_UVC_PROCESSING,
165 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
168 .flags = UVC_CTRL_FLAG_SET_CUR
169 | UVC_CTRL_FLAG_GET_RANGE
170 | UVC_CTRL_FLAG_RESTORE,
173 .entity = UVC_GUID_UVC_PROCESSING,
174 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
177 .flags = UVC_CTRL_FLAG_SET_CUR
178 | UVC_CTRL_FLAG_GET_RANGE
179 | UVC_CTRL_FLAG_RESTORE,
182 .entity = UVC_GUID_UVC_PROCESSING,
183 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
186 .flags = UVC_CTRL_FLAG_GET_CUR,
189 .entity = UVC_GUID_UVC_PROCESSING,
190 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
193 .flags = UVC_CTRL_FLAG_GET_CUR,
196 .entity = UVC_GUID_UVC_CAMERA,
197 .selector = UVC_CT_SCANNING_MODE_CONTROL,
200 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
201 | UVC_CTRL_FLAG_RESTORE,
204 .entity = UVC_GUID_UVC_CAMERA,
205 .selector = UVC_CT_AE_MODE_CONTROL,
208 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
209 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
210 | UVC_CTRL_FLAG_RESTORE,
213 .entity = UVC_GUID_UVC_CAMERA,
214 .selector = UVC_CT_AE_PRIORITY_CONTROL,
217 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
218 | UVC_CTRL_FLAG_RESTORE,
221 .entity = UVC_GUID_UVC_CAMERA,
222 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
225 .flags = UVC_CTRL_FLAG_SET_CUR
226 | UVC_CTRL_FLAG_GET_RANGE
227 | UVC_CTRL_FLAG_RESTORE
228 | UVC_CTRL_FLAG_AUTO_UPDATE,
231 .entity = UVC_GUID_UVC_CAMERA,
232 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
235 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
238 .entity = UVC_GUID_UVC_CAMERA,
239 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
242 .flags = UVC_CTRL_FLAG_SET_CUR
243 | UVC_CTRL_FLAG_GET_RANGE
244 | UVC_CTRL_FLAG_RESTORE
245 | UVC_CTRL_FLAG_AUTO_UPDATE,
248 .entity = UVC_GUID_UVC_CAMERA,
249 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
252 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
253 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
254 | UVC_CTRL_FLAG_GET_DEF
255 | UVC_CTRL_FLAG_AUTO_UPDATE,
258 .entity = UVC_GUID_UVC_CAMERA,
259 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
262 .flags = UVC_CTRL_FLAG_SET_CUR
263 | UVC_CTRL_FLAG_GET_RANGE
264 | UVC_CTRL_FLAG_RESTORE
265 | UVC_CTRL_FLAG_AUTO_UPDATE,
268 .entity = UVC_GUID_UVC_CAMERA,
269 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
272 .flags = UVC_CTRL_FLAG_SET_CUR
273 | UVC_CTRL_FLAG_AUTO_UPDATE,
276 .entity = UVC_GUID_UVC_CAMERA,
277 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
280 .flags = UVC_CTRL_FLAG_SET_CUR
281 | UVC_CTRL_FLAG_GET_RANGE
282 | UVC_CTRL_FLAG_RESTORE
283 | UVC_CTRL_FLAG_AUTO_UPDATE,
286 .entity = UVC_GUID_UVC_CAMERA,
287 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
290 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
291 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
292 | UVC_CTRL_FLAG_GET_DEF
293 | UVC_CTRL_FLAG_AUTO_UPDATE,
296 .entity = UVC_GUID_UVC_CAMERA,
297 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
300 .flags = UVC_CTRL_FLAG_SET_CUR
301 | UVC_CTRL_FLAG_GET_RANGE
302 | UVC_CTRL_FLAG_RESTORE
303 | UVC_CTRL_FLAG_AUTO_UPDATE,
306 .entity = UVC_GUID_UVC_CAMERA,
307 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
310 .flags = UVC_CTRL_FLAG_SET_CUR
311 | UVC_CTRL_FLAG_GET_RANGE
312 | UVC_CTRL_FLAG_AUTO_UPDATE,
315 .entity = UVC_GUID_UVC_CAMERA,
316 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
319 .flags = UVC_CTRL_FLAG_SET_CUR
320 | UVC_CTRL_FLAG_GET_RANGE
321 | UVC_CTRL_FLAG_RESTORE
322 | UVC_CTRL_FLAG_AUTO_UPDATE,
325 .entity = UVC_GUID_UVC_CAMERA,
326 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
329 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
330 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
331 | UVC_CTRL_FLAG_GET_DEF
332 | UVC_CTRL_FLAG_AUTO_UPDATE,
335 .entity = UVC_GUID_UVC_CAMERA,
336 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
339 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
340 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
343 .entity = UVC_GUID_UVC_CAMERA,
344 .selector = UVC_CT_PRIVACY_CONTROL,
347 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
348 | UVC_CTRL_FLAG_RESTORE
349 | UVC_CTRL_FLAG_AUTO_UPDATE,
352 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
353 .selector = UVC_CT_PRIVACY_CONTROL,
356 .flags = UVC_CTRL_FLAG_GET_CUR
357 | UVC_CTRL_FLAG_AUTO_UPDATE,
361 static const u32 uvc_control_classes[] = {
362 V4L2_CID_CAMERA_CLASS,
366 static const struct uvc_menu_info power_line_frequency_controls[] = {
373 static const struct uvc_menu_info exposure_auto_controls[] = {
375 { 1, "Manual Mode" },
376 { 4, "Shutter Priority Mode" },
377 { 8, "Aperture Priority Mode" },
380 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
381 u8 query, const u8 *data)
383 s8 zoom = (s8)data[0];
387 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
398 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
401 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
402 data[2] = min((int)abs(value), 0xff);
405 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
406 u8 query, const u8 *data)
408 unsigned int first = mapping->offset / 8;
409 s8 rel = (s8)data[first];
413 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
416 return -data[first+1];
421 return data[first+1];
425 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
428 unsigned int first = mapping->offset / 8;
430 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
431 data[first+1] = min_t(int, abs(value), 0xff);
434 static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
436 .id = V4L2_CID_BRIGHTNESS,
437 .entity = UVC_GUID_UVC_PROCESSING,
438 .selector = UVC_PU_BRIGHTNESS_CONTROL,
441 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
442 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
445 .id = V4L2_CID_CONTRAST,
446 .entity = UVC_GUID_UVC_PROCESSING,
447 .selector = UVC_PU_CONTRAST_CONTROL,
450 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
451 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
455 .entity = UVC_GUID_UVC_PROCESSING,
456 .selector = UVC_PU_HUE_CONTROL,
459 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
460 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
461 .master_id = V4L2_CID_HUE_AUTO,
465 .id = V4L2_CID_SATURATION,
466 .entity = UVC_GUID_UVC_PROCESSING,
467 .selector = UVC_PU_SATURATION_CONTROL,
470 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
471 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
474 .id = V4L2_CID_SHARPNESS,
475 .entity = UVC_GUID_UVC_PROCESSING,
476 .selector = UVC_PU_SHARPNESS_CONTROL,
479 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
480 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
483 .id = V4L2_CID_GAMMA,
484 .entity = UVC_GUID_UVC_PROCESSING,
485 .selector = UVC_PU_GAMMA_CONTROL,
488 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
489 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
492 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
493 .entity = UVC_GUID_UVC_PROCESSING,
494 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
497 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
498 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
502 .entity = UVC_GUID_UVC_PROCESSING,
503 .selector = UVC_PU_GAIN_CONTROL,
506 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
507 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
510 .id = V4L2_CID_HUE_AUTO,
511 .entity = UVC_GUID_UVC_PROCESSING,
512 .selector = UVC_PU_HUE_AUTO_CONTROL,
515 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
516 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
517 .slave_ids = { V4L2_CID_HUE, },
520 .id = V4L2_CID_EXPOSURE_AUTO,
521 .entity = UVC_GUID_UVC_CAMERA,
522 .selector = UVC_CT_AE_MODE_CONTROL,
525 .v4l2_type = V4L2_CTRL_TYPE_MENU,
526 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
527 .menu_info = exposure_auto_controls,
528 .menu_count = ARRAY_SIZE(exposure_auto_controls),
529 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
532 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
533 .entity = UVC_GUID_UVC_CAMERA,
534 .selector = UVC_CT_AE_PRIORITY_CONTROL,
537 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
538 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
541 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
542 .entity = UVC_GUID_UVC_CAMERA,
543 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
546 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
547 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
548 .master_id = V4L2_CID_EXPOSURE_AUTO,
549 .master_manual = V4L2_EXPOSURE_MANUAL,
552 .id = V4L2_CID_AUTO_WHITE_BALANCE,
553 .entity = UVC_GUID_UVC_PROCESSING,
554 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
557 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
558 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
559 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
562 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
563 .entity = UVC_GUID_UVC_PROCESSING,
564 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
567 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
568 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
569 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
573 .id = V4L2_CID_AUTO_WHITE_BALANCE,
574 .entity = UVC_GUID_UVC_PROCESSING,
575 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
578 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
579 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
580 .slave_ids = { V4L2_CID_BLUE_BALANCE,
581 V4L2_CID_RED_BALANCE },
584 .id = V4L2_CID_BLUE_BALANCE,
585 .entity = UVC_GUID_UVC_PROCESSING,
586 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
589 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
590 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
591 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
595 .id = V4L2_CID_RED_BALANCE,
596 .entity = UVC_GUID_UVC_PROCESSING,
597 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
600 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
601 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
602 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
606 .id = V4L2_CID_FOCUS_ABSOLUTE,
607 .entity = UVC_GUID_UVC_CAMERA,
608 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
611 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
612 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
613 .master_id = V4L2_CID_FOCUS_AUTO,
617 .id = V4L2_CID_FOCUS_AUTO,
618 .entity = UVC_GUID_UVC_CAMERA,
619 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
622 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
623 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
624 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
627 .id = V4L2_CID_IRIS_ABSOLUTE,
628 .entity = UVC_GUID_UVC_CAMERA,
629 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
632 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
633 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
636 .id = V4L2_CID_IRIS_RELATIVE,
637 .entity = UVC_GUID_UVC_CAMERA,
638 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
641 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
642 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
645 .id = V4L2_CID_ZOOM_ABSOLUTE,
646 .entity = UVC_GUID_UVC_CAMERA,
647 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
650 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
651 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
654 .id = V4L2_CID_ZOOM_CONTINUOUS,
655 .entity = UVC_GUID_UVC_CAMERA,
656 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
659 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
660 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
661 .get = uvc_ctrl_get_zoom,
662 .set = uvc_ctrl_set_zoom,
665 .id = V4L2_CID_PAN_ABSOLUTE,
666 .entity = UVC_GUID_UVC_CAMERA,
667 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
670 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
671 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
674 .id = V4L2_CID_TILT_ABSOLUTE,
675 .entity = UVC_GUID_UVC_CAMERA,
676 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
679 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
680 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
683 .id = V4L2_CID_PAN_SPEED,
684 .entity = UVC_GUID_UVC_CAMERA,
685 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
688 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
689 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
690 .get = uvc_ctrl_get_rel_speed,
691 .set = uvc_ctrl_set_rel_speed,
694 .id = V4L2_CID_TILT_SPEED,
695 .entity = UVC_GUID_UVC_CAMERA,
696 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
699 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
700 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
701 .get = uvc_ctrl_get_rel_speed,
702 .set = uvc_ctrl_set_rel_speed,
705 .id = V4L2_CID_PRIVACY,
706 .entity = UVC_GUID_UVC_CAMERA,
707 .selector = UVC_CT_PRIVACY_CONTROL,
710 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
711 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
714 .id = V4L2_CID_PRIVACY,
715 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
716 .selector = UVC_CT_PRIVACY_CONTROL,
719 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
720 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
724 static const struct uvc_control_mapping uvc_ctrl_mappings_uvc11[] = {
726 .id = V4L2_CID_POWER_LINE_FREQUENCY,
727 .entity = UVC_GUID_UVC_PROCESSING,
728 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
731 .v4l2_type = V4L2_CTRL_TYPE_MENU,
732 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
733 .menu_info = power_line_frequency_controls,
734 .menu_count = ARRAY_SIZE(power_line_frequency_controls) - 1,
738 static const struct uvc_control_mapping uvc_ctrl_mappings_uvc15[] = {
740 .id = V4L2_CID_POWER_LINE_FREQUENCY,
741 .entity = UVC_GUID_UVC_PROCESSING,
742 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
745 .v4l2_type = V4L2_CTRL_TYPE_MENU,
746 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
747 .menu_info = power_line_frequency_controls,
748 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
752 /* ------------------------------------------------------------------------
756 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
758 return ctrl->uvc_data + id * ctrl->info.size;
761 static inline int uvc_test_bit(const u8 *data, int bit)
763 return (data[bit >> 3] >> (bit & 7)) & 1;
766 static inline void uvc_clear_bit(u8 *data, int bit)
768 data[bit >> 3] &= ~(1 << (bit & 7));
772 * Extract the bit string specified by mapping->offset and mapping->size
773 * from the little-endian data stored at 'data' and return the result as
774 * a signed 32bit integer. Sign extension will be performed if the mapping
775 * references a signed data type.
777 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
778 u8 query, const u8 *data)
780 int bits = mapping->size;
781 int offset = mapping->offset;
787 mask = ((1LL << bits) - 1) << offset;
790 u8 byte = *data & mask;
791 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
792 bits -= 8 - (offset > 0 ? offset : 0);
797 mask = (1 << bits) - 1;
801 /* Sign-extend the value if needed. */
802 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
803 value |= -(value & (1 << (mapping->size - 1)));
809 * Set the bit string specified by mapping->offset and mapping->size
810 * in the little-endian data stored at 'data' to the value 'value'.
812 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
815 int bits = mapping->size;
816 int offset = mapping->offset;
820 * According to the v4l2 spec, writing any value to a button control
821 * should result in the action belonging to the button control being
822 * triggered. UVC devices however want to see a 1 written -> override
825 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
831 for (; bits > 0; data++) {
832 mask = ((1LL << bits) - 1) << offset;
833 *data = (*data & ~mask) | ((value << offset) & mask);
834 value >>= offset ? offset : 8;
840 /* ------------------------------------------------------------------------
841 * Terminal and unit management
844 static int uvc_entity_match_guid(const struct uvc_entity *entity,
847 return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
850 /* ------------------------------------------------------------------------
854 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
855 struct uvc_control_mapping **mapping, struct uvc_control **control,
858 struct uvc_control *ctrl;
859 struct uvc_control_mapping *map;
865 for (i = 0; i < entity->ncontrols; ++i) {
866 ctrl = &entity->controls[i];
867 if (!ctrl->initialized)
870 list_for_each_entry(map, &ctrl->info.mappings, list) {
871 if ((map->id == v4l2_id) && !next) {
877 if ((*mapping == NULL || (*mapping)->id > map->id) &&
878 (map->id > v4l2_id) && next) {
886 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
887 u32 v4l2_id, struct uvc_control_mapping **mapping)
889 struct uvc_control *ctrl = NULL;
890 struct uvc_entity *entity;
891 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
895 /* Mask the query flags. */
896 v4l2_id &= V4L2_CTRL_ID_MASK;
898 /* Find the control. */
899 list_for_each_entry(entity, &chain->entities, chain) {
900 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
905 if (ctrl == NULL && !next)
906 uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
912 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
913 struct uvc_control *ctrl)
917 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
918 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
919 chain->dev->intfnum, ctrl->info.selector,
920 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
926 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
927 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
928 chain->dev->intfnum, ctrl->info.selector,
929 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
934 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
935 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
936 chain->dev->intfnum, ctrl->info.selector,
937 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
942 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
943 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
944 chain->dev->intfnum, ctrl->info.selector,
945 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
948 if (UVC_ENTITY_TYPE(ctrl->entity) !=
949 UVC_VC_EXTENSION_UNIT)
953 * GET_RES is mandatory for XU controls, but some
954 * cameras still choke on it. Ignore errors and set the
955 * resolution value to zero.
957 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
958 "UVC non compliance - GET_RES failed on "
959 "an XU control. Enabling workaround.\n");
960 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
969 static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
972 s32 value = mapping->get(mapping, UVC_GET_CUR, data);
974 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
975 const struct uvc_menu_info *menu = mapping->menu_info;
978 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
979 if (menu->value == value) {
989 static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
990 struct uvc_control *ctrl)
998 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1000 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1001 memset(data, 0, ctrl->info.size);
1007 if (ctrl->entity->get_cur)
1008 ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
1009 ctrl->info.selector, data,
1012 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1013 ctrl->entity->id, chain->dev->intfnum,
1014 ctrl->info.selector, data,
1025 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1026 struct uvc_control *ctrl,
1027 struct uvc_control_mapping *mapping,
1032 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1035 ret = __uvc_ctrl_load_cur(chain, ctrl);
1039 *value = __uvc_ctrl_get_value(mapping,
1040 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1045 static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1048 bool find_next = req_id & V4L2_CTRL_FLAG_NEXT_CTRL;
1051 req_id &= V4L2_CTRL_ID_MASK;
1053 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
1054 if (!(chain->ctrl_class_bitmap & BIT(i)))
1057 if (uvc_control_classes[i] == req_id)
1061 if (uvc_control_classes[i] > req_id &&
1062 uvc_control_classes[i] < found_id)
1069 static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1070 u32 found_id, struct v4l2_queryctrl *v4l2_ctrl)
1074 idx = __uvc_query_v4l2_class(chain, req_id, found_id);
1078 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1079 v4l2_ctrl->id = uvc_control_classes[idx];
1080 strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
1081 sizeof(v4l2_ctrl->name));
1082 v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
1083 v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
1084 | V4L2_CTRL_FLAG_READ_ONLY;
1088 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
1091 struct uvc_control_mapping *mapping;
1092 struct uvc_control *ctrl;
1094 if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
1097 ctrl = uvc_find_control(chain, v4l2_id, &mapping);
1101 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read)
1104 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read)
1110 static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
1117 name = v4l2_ctrl_get_name(map->id);
1121 return "Unknown Control";
1124 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1125 struct uvc_control *ctrl,
1126 struct uvc_control_mapping *mapping,
1127 struct v4l2_queryctrl *v4l2_ctrl)
1129 struct uvc_control_mapping *master_map = NULL;
1130 struct uvc_control *master_ctrl = NULL;
1131 const struct uvc_menu_info *menu;
1134 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1135 v4l2_ctrl->id = mapping->id;
1136 v4l2_ctrl->type = mapping->v4l2_type;
1137 strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
1138 sizeof(v4l2_ctrl->name));
1139 v4l2_ctrl->flags = 0;
1141 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1142 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1143 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1144 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1146 if (mapping->master_id)
1147 __uvc_find_control(ctrl->entity, mapping->master_id,
1148 &master_map, &master_ctrl, 0);
1149 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1151 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1155 if (val != mapping->master_manual)
1156 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1159 if (!ctrl->cached) {
1160 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1165 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1166 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1167 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1170 switch (mapping->v4l2_type) {
1171 case V4L2_CTRL_TYPE_MENU:
1172 v4l2_ctrl->minimum = 0;
1173 v4l2_ctrl->maximum = mapping->menu_count - 1;
1174 v4l2_ctrl->step = 1;
1176 menu = mapping->menu_info;
1177 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1178 if (menu->value == v4l2_ctrl->default_value) {
1179 v4l2_ctrl->default_value = i;
1186 case V4L2_CTRL_TYPE_BOOLEAN:
1187 v4l2_ctrl->minimum = 0;
1188 v4l2_ctrl->maximum = 1;
1189 v4l2_ctrl->step = 1;
1192 case V4L2_CTRL_TYPE_BUTTON:
1193 v4l2_ctrl->minimum = 0;
1194 v4l2_ctrl->maximum = 0;
1195 v4l2_ctrl->step = 0;
1202 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1203 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1204 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1206 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1207 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1208 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1210 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1211 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1212 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1217 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1218 struct v4l2_queryctrl *v4l2_ctrl)
1220 struct uvc_control *ctrl;
1221 struct uvc_control_mapping *mapping;
1224 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1226 return -ERESTARTSYS;
1228 /* Check if the ctrl is a know class */
1229 if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
1230 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
1235 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1242 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
1243 * a class should be inserted between the previous control and the one
1244 * we have just found.
1246 if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1247 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
1253 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1255 mutex_unlock(&chain->ctrl_mutex);
1260 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1261 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1262 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1263 * Balance V4L2 controls use the White Balance Component UVC control) or
1264 * otherwise translated. The approach we take here is to use a translation
1265 * table for the controls that can be mapped directly, and handle the others
1268 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1269 struct v4l2_querymenu *query_menu)
1271 const struct uvc_menu_info *menu_info;
1272 struct uvc_control_mapping *mapping;
1273 struct uvc_control *ctrl;
1274 u32 index = query_menu->index;
1275 u32 id = query_menu->id;
1278 memset(query_menu, 0, sizeof(*query_menu));
1279 query_menu->id = id;
1280 query_menu->index = index;
1282 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1284 return -ERESTARTSYS;
1286 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1287 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1292 if (query_menu->index >= mapping->menu_count) {
1297 menu_info = &mapping->menu_info[query_menu->index];
1299 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1300 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1303 if (!ctrl->cached) {
1304 ret = uvc_ctrl_populate_cache(chain, ctrl);
1309 bitmap = mapping->get(mapping, UVC_GET_RES,
1310 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1311 if (!(bitmap & menu_info->value)) {
1317 strscpy(query_menu->name, menu_info->name, sizeof(query_menu->name));
1320 mutex_unlock(&chain->ctrl_mutex);
1324 /* --------------------------------------------------------------------------
1325 * Ctrl event handling
1328 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1329 struct v4l2_event *ev,
1330 struct uvc_control *ctrl,
1331 struct uvc_control_mapping *mapping,
1332 s32 value, u32 changes)
1334 struct v4l2_queryctrl v4l2_ctrl;
1336 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1338 memset(ev, 0, sizeof(*ev));
1339 ev->type = V4L2_EVENT_CTRL;
1340 ev->id = v4l2_ctrl.id;
1341 ev->u.ctrl.value = value;
1342 ev->u.ctrl.changes = changes;
1343 ev->u.ctrl.type = v4l2_ctrl.type;
1344 ev->u.ctrl.flags = v4l2_ctrl.flags;
1345 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1346 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1347 ev->u.ctrl.step = v4l2_ctrl.step;
1348 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1352 * Send control change events to all subscribers for the @ctrl control. By
1353 * default the subscriber that generated the event, as identified by @handle,
1354 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1355 * @handle can be NULL for asynchronous events related to auto-update controls,
1356 * in which case all subscribers are notified.
1358 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1359 struct uvc_fh *handle, struct uvc_control *ctrl,
1360 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1362 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1363 struct v4l2_subscribed_event *sev;
1364 struct v4l2_event ev;
1366 if (list_empty(&mapping->ev_subs))
1369 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1371 list_for_each_entry(sev, &mapping->ev_subs, node) {
1372 if (sev->fh != originator ||
1373 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1374 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1375 v4l2_event_queue_fh(sev->fh, &ev);
1380 * Send control change events for the slave of the @master control identified
1381 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1382 * generated the event and may be NULL for auto-update events.
1384 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1385 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1387 struct uvc_control_mapping *mapping = NULL;
1388 struct uvc_control *ctrl = NULL;
1389 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1392 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1396 if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1397 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1399 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1402 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1403 struct uvc_control *ctrl, const u8 *data)
1405 struct uvc_control_mapping *mapping;
1406 struct uvc_fh *handle;
1409 mutex_lock(&chain->ctrl_mutex);
1411 handle = ctrl->handle;
1412 ctrl->handle = NULL;
1414 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1415 s32 value = __uvc_ctrl_get_value(mapping, data);
1418 * handle may be NULL here if the device sends auto-update
1419 * events without a prior related control set from userspace.
1421 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1422 if (!mapping->slave_ids[i])
1425 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1426 mapping->slave_ids[i]);
1429 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1430 V4L2_EVENT_CTRL_CH_VALUE);
1433 mutex_unlock(&chain->ctrl_mutex);
1436 static void uvc_ctrl_status_event_work(struct work_struct *work)
1438 struct uvc_device *dev = container_of(work, struct uvc_device,
1440 struct uvc_ctrl_work *w = &dev->async_ctrl;
1443 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1445 /* Resubmit the URB. */
1446 w->urb->interval = dev->int_ep->desc.bInterval;
1447 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1449 dev_err(&dev->udev->dev,
1450 "Failed to resubmit status URB (%d).\n", ret);
1453 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1454 struct uvc_control *ctrl, const u8 *data)
1456 struct uvc_device *dev = chain->dev;
1457 struct uvc_ctrl_work *w = &dev->async_ctrl;
1459 if (list_empty(&ctrl->info.mappings)) {
1460 ctrl->handle = NULL;
1469 schedule_work(&w->work);
1474 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1475 unsigned int xctrls_count, u32 id)
1479 for (i = 0; i < xctrls_count; ++i) {
1480 if (xctrls[i].id == id)
1487 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1488 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1490 struct uvc_control_mapping *mapping;
1491 struct uvc_control *ctrl;
1492 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1496 for (i = 0; i < xctrls_count; ++i) {
1497 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1499 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1500 /* Notification will be sent from an Interrupt event. */
1503 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1504 u32 slave_id = mapping->slave_ids[j];
1510 * We can skip sending an event for the slave if the
1511 * slave is being modified in the same transaction.
1513 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1517 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1522 * If the master is being modified in the same transaction
1523 * flags may change too.
1525 if (mapping->master_id &&
1526 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1527 mapping->master_id))
1528 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1530 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1531 xctrls[i].value, changes);
1535 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1537 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1538 struct uvc_control_mapping *mapping;
1539 struct uvc_control *ctrl;
1542 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1544 return -ERESTARTSYS;
1546 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
1551 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1557 list_add_tail(&sev->node, &mapping->ev_subs);
1558 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1559 struct v4l2_event ev;
1560 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1563 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1564 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1566 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1569 * Mark the queue as active, allowing this initial event to be
1573 v4l2_event_queue_fh(sev->fh, &ev);
1577 mutex_unlock(&handle->chain->ctrl_mutex);
1581 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1583 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1585 mutex_lock(&handle->chain->ctrl_mutex);
1586 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
1588 list_del(&sev->node);
1590 mutex_unlock(&handle->chain->ctrl_mutex);
1593 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1594 .add = uvc_ctrl_add_event,
1595 .del = uvc_ctrl_del_event,
1596 .replace = v4l2_ctrl_replace,
1597 .merge = v4l2_ctrl_merge,
1600 /* --------------------------------------------------------------------------
1601 * Control transactions
1603 * To make extended set operations as atomic as the hardware allows, controls
1604 * are handled using begin/commit/rollback operations.
1606 * At the beginning of a set request, uvc_ctrl_begin should be called to
1607 * initialize the request. This function acquires the control lock.
1609 * When setting a control, the new value is stored in the control data field
1610 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1611 * later processing. If the UVC and V4L2 control sizes differ, the current
1612 * value is loaded from the hardware before storing the new value in the data
1615 * After processing all controls in the transaction, uvc_ctrl_commit or
1616 * uvc_ctrl_rollback must be called to apply the pending changes to the
1617 * hardware or revert them. When applying changes, all controls marked as
1618 * dirty will be modified in the UVC device, and the dirty flag will be
1619 * cleared. When reverting controls, the control data field
1620 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1621 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1624 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1626 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1629 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1630 struct uvc_entity *entity, int rollback, struct uvc_control **err_ctrl)
1632 struct uvc_control *ctrl;
1639 for (i = 0; i < entity->ncontrols; ++i) {
1640 ctrl = &entity->controls[i];
1641 if (!ctrl->initialized)
1645 * Reset the loaded flag for auto-update controls that were
1646 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1647 * uvc_ctrl_get from using the cached value, and for write-only
1648 * controls to prevent uvc_ctrl_set from setting bits not
1649 * explicitly set by the user.
1651 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1652 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1659 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1660 dev->intfnum, ctrl->info.selector,
1661 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1666 if (rollback || ret < 0)
1667 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1668 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1683 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
1684 struct v4l2_ext_controls *ctrls,
1685 struct uvc_control *uvc_control)
1687 struct uvc_control_mapping *mapping = NULL;
1688 struct uvc_control *ctrl_found = NULL;
1692 return ctrls->count;
1694 for (i = 0; i < ctrls->count; i++) {
1695 __uvc_find_control(entity, ctrls->controls[i].id, &mapping,
1697 if (uvc_control == ctrl_found)
1701 return ctrls->count;
1704 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1705 struct v4l2_ext_controls *ctrls)
1707 struct uvc_video_chain *chain = handle->chain;
1708 struct uvc_control *err_ctrl;
1709 struct uvc_entity *entity;
1712 /* Find the control. */
1713 list_for_each_entry(entity, &chain->entities, chain) {
1714 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback,
1721 uvc_ctrl_send_events(handle, ctrls->controls, ctrls->count);
1723 if (ret < 0 && ctrls)
1724 ctrls->error_idx = uvc_ctrl_find_ctrl_idx(entity, ctrls,
1726 mutex_unlock(&chain->ctrl_mutex);
1730 int uvc_ctrl_get(struct uvc_video_chain *chain,
1731 struct v4l2_ext_control *xctrl)
1733 struct uvc_control *ctrl;
1734 struct uvc_control_mapping *mapping;
1736 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1739 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1743 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1746 int uvc_ctrl_set(struct uvc_fh *handle,
1747 struct v4l2_ext_control *xctrl)
1749 struct uvc_video_chain *chain = handle->chain;
1750 struct uvc_control *ctrl;
1751 struct uvc_control_mapping *mapping;
1758 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1761 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1764 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1767 /* Clamp out of range values. */
1768 switch (mapping->v4l2_type) {
1769 case V4L2_CTRL_TYPE_INTEGER:
1770 if (!ctrl->cached) {
1771 ret = uvc_ctrl_populate_cache(chain, ctrl);
1776 min = mapping->get(mapping, UVC_GET_MIN,
1777 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1778 max = mapping->get(mapping, UVC_GET_MAX,
1779 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1780 step = mapping->get(mapping, UVC_GET_RES,
1781 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1785 xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
1787 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1788 xctrl->value = clamp(xctrl->value, min, max);
1790 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1791 value = xctrl->value;
1794 case V4L2_CTRL_TYPE_BOOLEAN:
1795 xctrl->value = clamp(xctrl->value, 0, 1);
1796 value = xctrl->value;
1799 case V4L2_CTRL_TYPE_MENU:
1800 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1802 value = mapping->menu_info[xctrl->value].value;
1805 * Valid menu indices are reported by the GET_RES request for
1806 * UVC controls that support it.
1808 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1809 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1810 if (!ctrl->cached) {
1811 ret = uvc_ctrl_populate_cache(chain, ctrl);
1816 step = mapping->get(mapping, UVC_GET_RES,
1817 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1818 if (!(step & value))
1825 value = xctrl->value;
1830 * If the mapping doesn't span the whole UVC control, the current value
1831 * needs to be loaded from the device to perform the read-modify-write
1834 if ((ctrl->info.size * 8) != mapping->size) {
1835 ret = __uvc_ctrl_load_cur(chain, ctrl);
1840 /* Backup the current value in case we need to rollback later. */
1842 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1843 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1847 mapping->set(mapping, value,
1848 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1850 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1851 ctrl->handle = handle;
1858 /* --------------------------------------------------------------------------
1863 * Retrieve flags for a given control
1865 static int uvc_ctrl_get_flags(struct uvc_device *dev,
1866 const struct uvc_control *ctrl,
1867 struct uvc_control_info *info)
1872 data = kmalloc(1, GFP_KERNEL);
1876 if (ctrl->entity->get_info)
1877 ret = ctrl->entity->get_info(dev, ctrl->entity,
1878 ctrl->info.selector, data);
1880 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1881 dev->intfnum, info->selector, data, 1);
1883 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
1884 UVC_CTRL_FLAG_GET_CUR : 0)
1885 | (data[0] & UVC_CONTROL_CAP_SET ?
1886 UVC_CTRL_FLAG_SET_CUR : 0)
1887 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1888 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
1889 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
1890 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
1896 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1897 const struct uvc_control *ctrl, struct uvc_control_info *info)
1899 struct uvc_ctrl_fixup {
1900 struct usb_device_id id;
1906 static const struct uvc_ctrl_fixup fixups[] = {
1907 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1908 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1909 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1910 UVC_CTRL_FLAG_AUTO_UPDATE },
1911 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1912 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1913 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1914 UVC_CTRL_FLAG_AUTO_UPDATE },
1915 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1916 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1917 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1918 UVC_CTRL_FLAG_AUTO_UPDATE },
1923 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1924 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1927 if (fixups[i].entity == ctrl->entity->id &&
1928 fixups[i].selector == info->selector) {
1929 info->flags = fixups[i].flags;
1936 * Query control information (size and flags) for XU controls.
1938 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1939 const struct uvc_control *ctrl, struct uvc_control_info *info)
1944 data = kmalloc(2, GFP_KERNEL);
1948 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
1949 info->index = ctrl->index;
1950 info->selector = ctrl->index + 1;
1952 /* Query and verify the control length (GET_LEN) */
1953 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1954 info->selector, data, 2);
1956 uvc_dbg(dev, CONTROL,
1957 "GET_LEN failed on control %pUl/%u (%d)\n",
1958 info->entity, info->selector, ret);
1962 info->size = le16_to_cpup((__le16 *)data);
1964 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1965 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
1967 ret = uvc_ctrl_get_flags(dev, ctrl, info);
1969 uvc_dbg(dev, CONTROL,
1970 "Failed to get flags for control %pUl/%u (%d)\n",
1971 info->entity, info->selector, ret);
1975 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1977 uvc_dbg(dev, CONTROL,
1978 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
1979 info->entity, info->selector, info->size,
1980 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1981 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1982 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1989 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1990 const struct uvc_control_info *info);
1992 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1993 struct uvc_control *ctrl)
1995 struct uvc_control_info info;
1998 if (ctrl->initialized)
2001 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2005 ret = uvc_ctrl_add_info(dev, ctrl, &info);
2007 uvc_dbg(dev, CONTROL,
2008 "Failed to initialize control %pUl/%u on device %s entity %u\n",
2009 info.entity, info.selector, dev->udev->devpath,
2015 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2016 struct uvc_xu_control_query *xqry)
2018 struct uvc_entity *entity;
2019 struct uvc_control *ctrl;
2027 /* Find the extension unit. */
2029 list_for_each_entry(entity, &chain->entities, chain) {
2030 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
2031 entity->id == xqry->unit) {
2038 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2043 /* Find the control and perform delayed initialization if needed. */
2045 for (i = 0; i < entity->ncontrols; ++i) {
2046 ctrl = &entity->controls[i];
2047 if (ctrl->index == xqry->selector - 1) {
2054 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2055 entity->guid, xqry->selector);
2059 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2060 return -ERESTARTSYS;
2062 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2068 /* Validate the required buffer size and flags for the request */
2070 size = ctrl->info.size;
2072 switch (xqry->query) {
2074 reqflags = UVC_CTRL_FLAG_GET_CUR;
2077 reqflags = UVC_CTRL_FLAG_GET_MIN;
2080 reqflags = UVC_CTRL_FLAG_GET_MAX;
2083 reqflags = UVC_CTRL_FLAG_GET_DEF;
2086 reqflags = UVC_CTRL_FLAG_GET_RES;
2089 reqflags = UVC_CTRL_FLAG_SET_CUR;
2102 if (size != xqry->size) {
2107 if (reqflags && !(ctrl->info.flags & reqflags)) {
2112 data = kmalloc(size, GFP_KERNEL);
2118 if (xqry->query == UVC_SET_CUR &&
2119 copy_from_user(data, xqry->data, size)) {
2124 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2125 chain->dev->intfnum, xqry->selector, data, size);
2129 if (xqry->query != UVC_SET_CUR &&
2130 copy_to_user(xqry->data, data, size))
2134 mutex_unlock(&chain->ctrl_mutex);
2138 /* --------------------------------------------------------------------------
2143 * Restore control values after resume, skipping controls that haven't been
2147 * - Don't restore modified controls that are back to their default value.
2148 * - Handle restore order (Auto-Exposure Mode should be restored before
2151 int uvc_ctrl_restore_values(struct uvc_device *dev)
2153 struct uvc_control *ctrl;
2154 struct uvc_entity *entity;
2158 /* Walk the entities list and restore controls when possible. */
2159 list_for_each_entry(entity, &dev->entities, list) {
2161 for (i = 0; i < entity->ncontrols; ++i) {
2162 ctrl = &entity->controls[i];
2164 if (!ctrl->initialized || !ctrl->modified ||
2165 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2167 dev_dbg(&dev->udev->dev,
2168 "restoring control %pUl/%u/%u\n",
2169 ctrl->info.entity, ctrl->info.index,
2170 ctrl->info.selector);
2174 ret = uvc_ctrl_commit_entity(dev, entity, 0, NULL);
2182 /* --------------------------------------------------------------------------
2183 * Control and mapping handling
2187 * Add control information to a given control.
2189 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2190 const struct uvc_control_info *info)
2193 INIT_LIST_HEAD(&ctrl->info.mappings);
2195 /* Allocate an array to save control values (cur, def, max, etc.) */
2196 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2198 if (!ctrl->uvc_data)
2201 ctrl->initialized = 1;
2203 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2204 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2211 * Add a control mapping to a given control.
2213 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2214 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2216 struct uvc_control_mapping *map;
2221 * Most mappings come from static kernel data and need to be duplicated.
2222 * Mappings that come from userspace will be unnecessarily duplicated,
2223 * this could be optimized.
2225 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2229 /* For UVCIOC_CTRL_MAP custom control */
2230 if (mapping->name) {
2231 map->name = kstrdup(mapping->name, GFP_KERNEL);
2238 INIT_LIST_HEAD(&map->ev_subs);
2240 size = sizeof(*mapping->menu_info) * mapping->menu_count;
2241 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
2242 if (map->menu_info == NULL) {
2248 if (map->get == NULL)
2249 map->get = uvc_get_le_value;
2250 if (map->set == NULL)
2251 map->set = uvc_set_le_value;
2253 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
2254 if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
2255 V4L2_CTRL_ID2WHICH(map->id)) {
2256 chain->ctrl_class_bitmap |= BIT(i);
2261 list_add_tail(&map->list, &ctrl->info.mappings);
2262 uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2263 uvc_map_get_name(map), ctrl->info.entity,
2264 ctrl->info.selector);
2269 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2270 const struct uvc_control_mapping *mapping)
2272 struct uvc_device *dev = chain->dev;
2273 struct uvc_control_mapping *map;
2274 struct uvc_entity *entity;
2275 struct uvc_control *ctrl;
2279 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2280 uvc_dbg(dev, CONTROL,
2281 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2282 uvc_map_get_name(mapping), mapping->id);
2286 /* Search for the matching (GUID/CS) control on the current chain */
2287 list_for_each_entry(entity, &chain->entities, chain) {
2290 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2291 !uvc_entity_match_guid(entity, mapping->entity))
2294 for (i = 0; i < entity->ncontrols; ++i) {
2295 ctrl = &entity->controls[i];
2296 if (ctrl->index == mapping->selector - 1) {
2308 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2309 return -ERESTARTSYS;
2311 /* Perform delayed initialization of XU controls */
2312 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2318 /* Validate the user-provided bit-size and offset */
2319 if (mapping->size > 32 ||
2320 mapping->offset + mapping->size > ctrl->info.size * 8) {
2325 list_for_each_entry(map, &ctrl->info.mappings, list) {
2326 if (mapping->id == map->id) {
2327 uvc_dbg(dev, CONTROL,
2328 "Can't add mapping '%s', control id 0x%08x already exists\n",
2329 uvc_map_get_name(mapping), mapping->id);
2335 /* Prevent excess memory consumption */
2336 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2337 atomic_dec(&dev->nmappings);
2338 uvc_dbg(dev, CONTROL,
2339 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
2340 uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
2345 ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2347 atomic_dec(&dev->nmappings);
2350 mutex_unlock(&chain->ctrl_mutex);
2355 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2356 * are currently the ones that crash the camera or unconditionally return an
2357 * error when queried.
2359 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2360 struct uvc_entity *entity)
2362 struct uvc_ctrl_blacklist {
2363 struct usb_device_id id;
2367 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2368 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2369 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2370 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2372 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2373 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2376 const struct uvc_ctrl_blacklist *blacklist;
2382 switch (UVC_ENTITY_TYPE(entity)) {
2383 case UVC_VC_PROCESSING_UNIT:
2384 blacklist = processing_blacklist;
2385 count = ARRAY_SIZE(processing_blacklist);
2386 controls = entity->processing.bmControls;
2387 size = entity->processing.bControlSize;
2390 case UVC_ITT_CAMERA:
2391 blacklist = camera_blacklist;
2392 count = ARRAY_SIZE(camera_blacklist);
2393 controls = entity->camera.bmControls;
2394 size = entity->camera.bControlSize;
2401 for (i = 0; i < count; ++i) {
2402 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2405 if (blacklist[i].index >= 8 * size ||
2406 !uvc_test_bit(controls, blacklist[i].index))
2409 uvc_dbg(dev, CONTROL,
2410 "%u/%u control is black listed, removing it\n",
2411 entity->id, blacklist[i].index);
2413 uvc_clear_bit(controls, blacklist[i].index);
2418 * Add control information and hardcoded stock control mappings to the given
2421 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
2422 struct uvc_control *ctrl)
2424 const struct uvc_control_mapping *mappings;
2425 unsigned int num_mappings;
2429 * XU controls initialization requires querying the device for control
2430 * information. As some buggy UVC devices will crash when queried
2431 * repeatedly in a tight loop, delay XU controls initialization until
2434 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2437 for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
2438 const struct uvc_control_info *info = &uvc_ctrls[i];
2440 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2441 ctrl->index == info->index) {
2442 uvc_ctrl_add_info(chain->dev, ctrl, info);
2444 * Retrieve control flags from the device. Ignore errors
2445 * and work with default flag values from the uvc_ctrl
2446 * array when the device doesn't properly implement
2447 * GET_INFO on standard controls.
2449 uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
2454 if (!ctrl->initialized)
2458 * First check if the device provides a custom mapping for this control,
2459 * used to override standard mappings for non-conformant devices. Don't
2460 * process standard mappings if a custom mapping is found. This
2461 * mechanism doesn't support combining standard and custom mappings for
2464 if (chain->dev->info->mappings) {
2465 bool custom = false;
2467 for (i = 0; chain->dev->info->mappings[i]; ++i) {
2468 const struct uvc_control_mapping *mapping =
2469 chain->dev->info->mappings[i];
2471 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2472 ctrl->info.selector == mapping->selector) {
2473 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2482 /* Process common mappings next. */
2483 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
2484 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
2486 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2487 ctrl->info.selector == mapping->selector)
2488 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2491 /* Finally process version-specific mappings. */
2492 if (chain->dev->uvc_version < 0x0150) {
2493 mappings = uvc_ctrl_mappings_uvc11;
2494 num_mappings = ARRAY_SIZE(uvc_ctrl_mappings_uvc11);
2496 mappings = uvc_ctrl_mappings_uvc15;
2497 num_mappings = ARRAY_SIZE(uvc_ctrl_mappings_uvc15);
2500 for (i = 0; i < num_mappings; ++i) {
2501 const struct uvc_control_mapping *mapping = &mappings[i];
2503 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2504 ctrl->info.selector == mapping->selector)
2505 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2510 * Initialize device controls.
2512 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
2514 struct uvc_entity *entity;
2517 /* Walk the entities list and instantiate controls */
2518 list_for_each_entry(entity, &chain->entities, chain) {
2519 struct uvc_control *ctrl;
2520 unsigned int bControlSize = 0, ncontrols;
2521 u8 *bmControls = NULL;
2523 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2524 bmControls = entity->extension.bmControls;
2525 bControlSize = entity->extension.bControlSize;
2526 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2527 bmControls = entity->processing.bmControls;
2528 bControlSize = entity->processing.bControlSize;
2529 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2530 bmControls = entity->camera.bmControls;
2531 bControlSize = entity->camera.bControlSize;
2532 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
2533 bmControls = entity->gpio.bmControls;
2534 bControlSize = entity->gpio.bControlSize;
2537 /* Remove bogus/blacklisted controls */
2538 uvc_ctrl_prune_entity(chain->dev, entity);
2540 /* Count supported controls and allocate the controls array */
2541 ncontrols = memweight(bmControls, bControlSize);
2545 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2547 if (entity->controls == NULL)
2549 entity->ncontrols = ncontrols;
2551 /* Initialize all supported controls */
2552 ctrl = entity->controls;
2553 for (i = 0; i < bControlSize * 8; ++i) {
2554 if (uvc_test_bit(bmControls, i) == 0)
2557 ctrl->entity = entity;
2560 uvc_ctrl_init_ctrl(chain, ctrl);
2568 int uvc_ctrl_init_device(struct uvc_device *dev)
2570 struct uvc_video_chain *chain;
2573 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2575 list_for_each_entry(chain, &dev->chains, list) {
2576 ret = uvc_ctrl_init_chain(chain);
2585 * Cleanup device controls.
2587 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2588 struct uvc_control *ctrl)
2590 struct uvc_control_mapping *mapping, *nm;
2592 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2593 list_del(&mapping->list);
2594 kfree(mapping->menu_info);
2595 kfree(mapping->name);
2600 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2602 struct uvc_entity *entity;
2605 /* Can be uninitialized if we are aborting on probe error. */
2606 if (dev->async_ctrl.work.func)
2607 cancel_work_sync(&dev->async_ctrl.work);
2609 /* Free controls and control mappings for all entities. */
2610 list_for_each_entry(entity, &dev->entities, list) {
2611 for (i = 0; i < entity->ncontrols; ++i) {
2612 struct uvc_control *ctrl = &entity->controls[i];
2614 if (!ctrl->initialized)
2617 uvc_ctrl_cleanup_mappings(dev, ctrl);
2618 kfree(ctrl->uvc_data);
2621 kfree(entity->controls);