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>
24 #define UVC_CTRL_DATA_CURRENT 0
25 #define UVC_CTRL_DATA_BACKUP 1
26 #define UVC_CTRL_DATA_MIN 2
27 #define UVC_CTRL_DATA_MAX 3
28 #define UVC_CTRL_DATA_RES 4
29 #define UVC_CTRL_DATA_DEF 5
30 #define UVC_CTRL_DATA_LAST 6
32 /* ------------------------------------------------------------------------
36 static const struct uvc_control_info uvc_ctrls[] = {
38 .entity = UVC_GUID_UVC_PROCESSING,
39 .selector = UVC_PU_BRIGHTNESS_CONTROL,
42 .flags = UVC_CTRL_FLAG_SET_CUR
43 | UVC_CTRL_FLAG_GET_RANGE
44 | UVC_CTRL_FLAG_RESTORE,
47 .entity = UVC_GUID_UVC_PROCESSING,
48 .selector = UVC_PU_CONTRAST_CONTROL,
51 .flags = UVC_CTRL_FLAG_SET_CUR
52 | UVC_CTRL_FLAG_GET_RANGE
53 | UVC_CTRL_FLAG_RESTORE,
56 .entity = UVC_GUID_UVC_PROCESSING,
57 .selector = UVC_PU_HUE_CONTROL,
60 .flags = UVC_CTRL_FLAG_SET_CUR
61 | UVC_CTRL_FLAG_GET_RANGE
62 | UVC_CTRL_FLAG_RESTORE
63 | UVC_CTRL_FLAG_AUTO_UPDATE,
66 .entity = UVC_GUID_UVC_PROCESSING,
67 .selector = UVC_PU_SATURATION_CONTROL,
70 .flags = UVC_CTRL_FLAG_SET_CUR
71 | UVC_CTRL_FLAG_GET_RANGE
72 | UVC_CTRL_FLAG_RESTORE,
75 .entity = UVC_GUID_UVC_PROCESSING,
76 .selector = UVC_PU_SHARPNESS_CONTROL,
79 .flags = UVC_CTRL_FLAG_SET_CUR
80 | UVC_CTRL_FLAG_GET_RANGE
81 | UVC_CTRL_FLAG_RESTORE,
84 .entity = UVC_GUID_UVC_PROCESSING,
85 .selector = UVC_PU_GAMMA_CONTROL,
88 .flags = UVC_CTRL_FLAG_SET_CUR
89 | UVC_CTRL_FLAG_GET_RANGE
90 | UVC_CTRL_FLAG_RESTORE,
93 .entity = UVC_GUID_UVC_PROCESSING,
94 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
97 .flags = UVC_CTRL_FLAG_SET_CUR
98 | UVC_CTRL_FLAG_GET_RANGE
99 | UVC_CTRL_FLAG_RESTORE
100 | UVC_CTRL_FLAG_AUTO_UPDATE,
103 .entity = UVC_GUID_UVC_PROCESSING,
104 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
107 .flags = UVC_CTRL_FLAG_SET_CUR
108 | UVC_CTRL_FLAG_GET_RANGE
109 | UVC_CTRL_FLAG_RESTORE
110 | UVC_CTRL_FLAG_AUTO_UPDATE,
113 .entity = UVC_GUID_UVC_PROCESSING,
114 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
117 .flags = UVC_CTRL_FLAG_SET_CUR
118 | UVC_CTRL_FLAG_GET_RANGE
119 | UVC_CTRL_FLAG_RESTORE,
122 .entity = UVC_GUID_UVC_PROCESSING,
123 .selector = UVC_PU_GAIN_CONTROL,
126 .flags = UVC_CTRL_FLAG_SET_CUR
127 | UVC_CTRL_FLAG_GET_RANGE
128 | UVC_CTRL_FLAG_RESTORE,
131 .entity = UVC_GUID_UVC_PROCESSING,
132 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
135 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
136 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
139 .entity = UVC_GUID_UVC_PROCESSING,
140 .selector = UVC_PU_HUE_AUTO_CONTROL,
143 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
144 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
147 .entity = UVC_GUID_UVC_PROCESSING,
148 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
151 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
152 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
155 .entity = UVC_GUID_UVC_PROCESSING,
156 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
159 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
160 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
163 .entity = UVC_GUID_UVC_PROCESSING,
164 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
167 .flags = UVC_CTRL_FLAG_SET_CUR
168 | UVC_CTRL_FLAG_GET_RANGE
169 | UVC_CTRL_FLAG_RESTORE,
172 .entity = UVC_GUID_UVC_PROCESSING,
173 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
176 .flags = UVC_CTRL_FLAG_SET_CUR
177 | UVC_CTRL_FLAG_GET_RANGE
178 | UVC_CTRL_FLAG_RESTORE,
181 .entity = UVC_GUID_UVC_PROCESSING,
182 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
185 .flags = UVC_CTRL_FLAG_GET_CUR,
188 .entity = UVC_GUID_UVC_PROCESSING,
189 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
192 .flags = UVC_CTRL_FLAG_GET_CUR,
195 .entity = UVC_GUID_UVC_CAMERA,
196 .selector = UVC_CT_SCANNING_MODE_CONTROL,
199 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
200 | UVC_CTRL_FLAG_RESTORE,
203 .entity = UVC_GUID_UVC_CAMERA,
204 .selector = UVC_CT_AE_MODE_CONTROL,
207 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
208 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
209 | UVC_CTRL_FLAG_RESTORE,
212 .entity = UVC_GUID_UVC_CAMERA,
213 .selector = UVC_CT_AE_PRIORITY_CONTROL,
216 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
217 | UVC_CTRL_FLAG_RESTORE,
220 .entity = UVC_GUID_UVC_CAMERA,
221 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
224 .flags = UVC_CTRL_FLAG_SET_CUR
225 | UVC_CTRL_FLAG_GET_RANGE
226 | UVC_CTRL_FLAG_RESTORE
227 | UVC_CTRL_FLAG_AUTO_UPDATE,
230 .entity = UVC_GUID_UVC_CAMERA,
231 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
234 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
237 .entity = UVC_GUID_UVC_CAMERA,
238 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
241 .flags = UVC_CTRL_FLAG_SET_CUR
242 | UVC_CTRL_FLAG_GET_RANGE
243 | UVC_CTRL_FLAG_RESTORE
244 | UVC_CTRL_FLAG_AUTO_UPDATE,
247 .entity = UVC_GUID_UVC_CAMERA,
248 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
251 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
252 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
253 | UVC_CTRL_FLAG_GET_DEF
254 | UVC_CTRL_FLAG_AUTO_UPDATE,
257 .entity = UVC_GUID_UVC_CAMERA,
258 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
261 .flags = UVC_CTRL_FLAG_SET_CUR
262 | UVC_CTRL_FLAG_GET_RANGE
263 | UVC_CTRL_FLAG_RESTORE
264 | UVC_CTRL_FLAG_AUTO_UPDATE,
267 .entity = UVC_GUID_UVC_CAMERA,
268 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
271 .flags = UVC_CTRL_FLAG_SET_CUR
272 | UVC_CTRL_FLAG_AUTO_UPDATE,
275 .entity = UVC_GUID_UVC_CAMERA,
276 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
279 .flags = UVC_CTRL_FLAG_SET_CUR
280 | UVC_CTRL_FLAG_GET_RANGE
281 | UVC_CTRL_FLAG_RESTORE
282 | UVC_CTRL_FLAG_AUTO_UPDATE,
285 .entity = UVC_GUID_UVC_CAMERA,
286 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
289 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
290 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
291 | UVC_CTRL_FLAG_GET_DEF
292 | UVC_CTRL_FLAG_AUTO_UPDATE,
295 .entity = UVC_GUID_UVC_CAMERA,
296 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
299 .flags = UVC_CTRL_FLAG_SET_CUR
300 | UVC_CTRL_FLAG_GET_RANGE
301 | UVC_CTRL_FLAG_RESTORE
302 | UVC_CTRL_FLAG_AUTO_UPDATE,
305 .entity = UVC_GUID_UVC_CAMERA,
306 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
309 .flags = UVC_CTRL_FLAG_SET_CUR
310 | UVC_CTRL_FLAG_GET_RANGE
311 | UVC_CTRL_FLAG_AUTO_UPDATE,
314 .entity = UVC_GUID_UVC_CAMERA,
315 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
318 .flags = UVC_CTRL_FLAG_SET_CUR
319 | UVC_CTRL_FLAG_GET_RANGE
320 | UVC_CTRL_FLAG_RESTORE
321 | UVC_CTRL_FLAG_AUTO_UPDATE,
324 .entity = UVC_GUID_UVC_CAMERA,
325 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
328 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
329 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
330 | UVC_CTRL_FLAG_GET_DEF
331 | UVC_CTRL_FLAG_AUTO_UPDATE,
334 .entity = UVC_GUID_UVC_CAMERA,
335 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
338 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
339 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
342 .entity = UVC_GUID_UVC_CAMERA,
343 .selector = UVC_CT_PRIVACY_CONTROL,
346 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
347 | UVC_CTRL_FLAG_RESTORE
348 | UVC_CTRL_FLAG_AUTO_UPDATE,
351 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
352 .selector = UVC_CT_PRIVACY_CONTROL,
355 .flags = UVC_CTRL_FLAG_GET_CUR
356 | UVC_CTRL_FLAG_AUTO_UPDATE,
360 static const struct uvc_menu_info power_line_frequency_controls[] = {
366 static const struct uvc_menu_info exposure_auto_controls[] = {
368 { 1, "Manual Mode" },
369 { 4, "Shutter Priority Mode" },
370 { 8, "Aperture Priority Mode" },
373 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
374 u8 query, const u8 *data)
376 s8 zoom = (s8)data[0];
380 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
391 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
394 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
395 data[2] = min((int)abs(value), 0xff);
398 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
399 u8 query, const u8 *data)
401 unsigned int first = mapping->offset / 8;
402 s8 rel = (s8)data[first];
406 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
409 return -data[first+1];
414 return data[first+1];
418 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
421 unsigned int first = mapping->offset / 8;
423 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
424 data[first+1] = min_t(int, abs(value), 0xff);
427 static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
429 .id = V4L2_CID_BRIGHTNESS,
430 .name = "Brightness",
431 .entity = UVC_GUID_UVC_PROCESSING,
432 .selector = UVC_PU_BRIGHTNESS_CONTROL,
435 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
436 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
439 .id = V4L2_CID_CONTRAST,
441 .entity = UVC_GUID_UVC_PROCESSING,
442 .selector = UVC_PU_CONTRAST_CONTROL,
445 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
446 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
451 .entity = UVC_GUID_UVC_PROCESSING,
452 .selector = UVC_PU_HUE_CONTROL,
455 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
456 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
457 .master_id = V4L2_CID_HUE_AUTO,
461 .id = V4L2_CID_SATURATION,
462 .name = "Saturation",
463 .entity = UVC_GUID_UVC_PROCESSING,
464 .selector = UVC_PU_SATURATION_CONTROL,
467 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
468 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
471 .id = V4L2_CID_SHARPNESS,
473 .entity = UVC_GUID_UVC_PROCESSING,
474 .selector = UVC_PU_SHARPNESS_CONTROL,
477 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
478 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
481 .id = V4L2_CID_GAMMA,
483 .entity = UVC_GUID_UVC_PROCESSING,
484 .selector = UVC_PU_GAMMA_CONTROL,
487 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
488 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
491 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
492 .name = "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,
503 .entity = UVC_GUID_UVC_PROCESSING,
504 .selector = UVC_PU_GAIN_CONTROL,
507 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
508 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
511 .id = V4L2_CID_POWER_LINE_FREQUENCY,
512 .name = "Power Line Frequency",
513 .entity = UVC_GUID_UVC_PROCESSING,
514 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
517 .v4l2_type = V4L2_CTRL_TYPE_MENU,
518 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
519 .menu_info = power_line_frequency_controls,
520 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
523 .id = V4L2_CID_HUE_AUTO,
525 .entity = UVC_GUID_UVC_PROCESSING,
526 .selector = UVC_PU_HUE_AUTO_CONTROL,
529 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
530 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
531 .slave_ids = { V4L2_CID_HUE, },
534 .id = V4L2_CID_EXPOSURE_AUTO,
535 .name = "Exposure, Auto",
536 .entity = UVC_GUID_UVC_CAMERA,
537 .selector = UVC_CT_AE_MODE_CONTROL,
540 .v4l2_type = V4L2_CTRL_TYPE_MENU,
541 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
542 .menu_info = exposure_auto_controls,
543 .menu_count = ARRAY_SIZE(exposure_auto_controls),
544 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
547 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
548 .name = "Exposure, Auto Priority",
549 .entity = UVC_GUID_UVC_CAMERA,
550 .selector = UVC_CT_AE_PRIORITY_CONTROL,
553 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
554 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
557 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
558 .name = "Exposure (Absolute)",
559 .entity = UVC_GUID_UVC_CAMERA,
560 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
563 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
564 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
565 .master_id = V4L2_CID_EXPOSURE_AUTO,
566 .master_manual = V4L2_EXPOSURE_MANUAL,
569 .id = V4L2_CID_AUTO_WHITE_BALANCE,
570 .name = "White Balance Temperature, Auto",
571 .entity = UVC_GUID_UVC_PROCESSING,
572 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
575 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
576 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
577 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
580 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
581 .name = "White Balance Temperature",
582 .entity = UVC_GUID_UVC_PROCESSING,
583 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
586 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
587 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
588 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
592 .id = V4L2_CID_AUTO_WHITE_BALANCE,
593 .name = "White Balance Component, Auto",
594 .entity = UVC_GUID_UVC_PROCESSING,
595 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
598 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
599 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
600 .slave_ids = { V4L2_CID_BLUE_BALANCE,
601 V4L2_CID_RED_BALANCE },
604 .id = V4L2_CID_BLUE_BALANCE,
605 .name = "White Balance Blue Component",
606 .entity = UVC_GUID_UVC_PROCESSING,
607 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
610 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
611 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
612 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
616 .id = V4L2_CID_RED_BALANCE,
617 .name = "White Balance Red Component",
618 .entity = UVC_GUID_UVC_PROCESSING,
619 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
622 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
623 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
624 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
628 .id = V4L2_CID_FOCUS_ABSOLUTE,
629 .name = "Focus (absolute)",
630 .entity = UVC_GUID_UVC_CAMERA,
631 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
634 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
635 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
636 .master_id = V4L2_CID_FOCUS_AUTO,
640 .id = V4L2_CID_FOCUS_AUTO,
641 .name = "Focus, Auto",
642 .entity = UVC_GUID_UVC_CAMERA,
643 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
646 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
647 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
648 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
651 .id = V4L2_CID_IRIS_ABSOLUTE,
652 .name = "Iris, Absolute",
653 .entity = UVC_GUID_UVC_CAMERA,
654 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
657 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
658 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
661 .id = V4L2_CID_IRIS_RELATIVE,
662 .name = "Iris, Relative",
663 .entity = UVC_GUID_UVC_CAMERA,
664 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
667 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
668 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
671 .id = V4L2_CID_ZOOM_ABSOLUTE,
672 .name = "Zoom, Absolute",
673 .entity = UVC_GUID_UVC_CAMERA,
674 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
677 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
678 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
681 .id = V4L2_CID_ZOOM_CONTINUOUS,
682 .name = "Zoom, Continuous",
683 .entity = UVC_GUID_UVC_CAMERA,
684 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
687 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
688 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
689 .get = uvc_ctrl_get_zoom,
690 .set = uvc_ctrl_set_zoom,
693 .id = V4L2_CID_PAN_ABSOLUTE,
694 .name = "Pan (Absolute)",
695 .entity = UVC_GUID_UVC_CAMERA,
696 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
699 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
700 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
703 .id = V4L2_CID_TILT_ABSOLUTE,
704 .name = "Tilt (Absolute)",
705 .entity = UVC_GUID_UVC_CAMERA,
706 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
709 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
710 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
713 .id = V4L2_CID_PAN_SPEED,
714 .name = "Pan (Speed)",
715 .entity = UVC_GUID_UVC_CAMERA,
716 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
719 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
720 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
721 .get = uvc_ctrl_get_rel_speed,
722 .set = uvc_ctrl_set_rel_speed,
725 .id = V4L2_CID_TILT_SPEED,
726 .name = "Tilt (Speed)",
727 .entity = UVC_GUID_UVC_CAMERA,
728 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
731 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
732 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
733 .get = uvc_ctrl_get_rel_speed,
734 .set = uvc_ctrl_set_rel_speed,
737 .id = V4L2_CID_PRIVACY,
739 .entity = UVC_GUID_UVC_CAMERA,
740 .selector = UVC_CT_PRIVACY_CONTROL,
743 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
744 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
747 .id = V4L2_CID_PRIVACY,
749 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
750 .selector = UVC_CT_PRIVACY_CONTROL,
753 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
754 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
758 /* ------------------------------------------------------------------------
762 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
764 return ctrl->uvc_data + id * ctrl->info.size;
767 static inline int uvc_test_bit(const u8 *data, int bit)
769 return (data[bit >> 3] >> (bit & 7)) & 1;
772 static inline void uvc_clear_bit(u8 *data, int bit)
774 data[bit >> 3] &= ~(1 << (bit & 7));
777 /* Extract the bit string specified by mapping->offset and mapping->size
778 * from the little-endian data stored at 'data' and return the result as
779 * a signed 32bit integer. Sign extension will be performed if the mapping
780 * references a signed data type.
782 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
783 u8 query, const u8 *data)
785 int bits = mapping->size;
786 int offset = mapping->offset;
792 mask = ((1LL << bits) - 1) << offset;
795 u8 byte = *data & mask;
796 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
797 bits -= 8 - (offset > 0 ? offset : 0);
802 mask = (1 << bits) - 1;
806 /* Sign-extend the value if needed. */
807 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
808 value |= -(value & (1 << (mapping->size - 1)));
813 /* Set the bit string specified by mapping->offset and mapping->size
814 * in the little-endian data stored at 'data' to the value 'value'.
816 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
819 int bits = mapping->size;
820 int offset = mapping->offset;
823 /* According to the v4l2 spec, writing any value to a button control
824 * should result in the action belonging to the button control being
825 * triggered. UVC devices however want to see a 1 written -> override
828 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
834 for (; bits > 0; data++) {
835 mask = ((1LL << bits) - 1) << offset;
836 *data = (*data & ~mask) | ((value << offset) & mask);
837 value >>= offset ? offset : 8;
843 /* ------------------------------------------------------------------------
844 * Terminal and unit management
847 static int uvc_entity_match_guid(const struct uvc_entity *entity,
850 return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
853 /* ------------------------------------------------------------------------
857 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
858 struct uvc_control_mapping **mapping, struct uvc_control **control,
861 struct uvc_control *ctrl;
862 struct uvc_control_mapping *map;
868 for (i = 0; i < entity->ncontrols; ++i) {
869 ctrl = &entity->controls[i];
870 if (!ctrl->initialized)
873 list_for_each_entry(map, &ctrl->info.mappings, list) {
874 if ((map->id == v4l2_id) && !next) {
880 if ((*mapping == NULL || (*mapping)->id > map->id) &&
881 (map->id > v4l2_id) && next) {
889 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
890 u32 v4l2_id, struct uvc_control_mapping **mapping)
892 struct uvc_control *ctrl = NULL;
893 struct uvc_entity *entity;
894 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
898 /* Mask the query flags. */
899 v4l2_id &= V4L2_CTRL_ID_MASK;
901 /* Find the control. */
902 list_for_each_entry(entity, &chain->entities, chain) {
903 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
908 if (ctrl == NULL && !next)
909 uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
915 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
916 struct uvc_control *ctrl)
920 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
921 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
922 chain->dev->intfnum, ctrl->info.selector,
923 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
929 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
930 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
931 chain->dev->intfnum, ctrl->info.selector,
932 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
937 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
938 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
939 chain->dev->intfnum, ctrl->info.selector,
940 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
945 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
946 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
947 chain->dev->intfnum, ctrl->info.selector,
948 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
951 if (UVC_ENTITY_TYPE(ctrl->entity) !=
952 UVC_VC_EXTENSION_UNIT)
955 /* GET_RES is mandatory for XU controls, but some
956 * cameras still choke on it. Ignore errors and set the
957 * resolution value to zero.
959 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
960 "UVC non compliance - GET_RES failed on "
961 "an XU control. Enabling workaround.\n");
962 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
971 static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
974 s32 value = mapping->get(mapping, UVC_GET_CUR, data);
976 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
977 const struct uvc_menu_info *menu = mapping->menu_info;
980 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
981 if (menu->value == value) {
991 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
992 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
997 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1000 if (!ctrl->loaded) {
1001 if (ctrl->entity->get_cur) {
1002 ret = ctrl->entity->get_cur(chain->dev,
1004 ctrl->info.selector,
1005 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1008 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1010 chain->dev->intfnum,
1011 ctrl->info.selector,
1012 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1021 *value = __uvc_ctrl_get_value(mapping,
1022 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1027 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1028 struct uvc_control *ctrl,
1029 struct uvc_control_mapping *mapping,
1030 struct v4l2_queryctrl *v4l2_ctrl)
1032 struct uvc_control_mapping *master_map = NULL;
1033 struct uvc_control *master_ctrl = NULL;
1034 const struct uvc_menu_info *menu;
1037 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1038 v4l2_ctrl->id = mapping->id;
1039 v4l2_ctrl->type = mapping->v4l2_type;
1040 strscpy(v4l2_ctrl->name, mapping->name, sizeof(v4l2_ctrl->name));
1041 v4l2_ctrl->flags = 0;
1043 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1044 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1045 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1046 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1048 if (mapping->master_id)
1049 __uvc_find_control(ctrl->entity, mapping->master_id,
1050 &master_map, &master_ctrl, 0);
1051 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1053 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1057 if (val != mapping->master_manual)
1058 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1061 if (!ctrl->cached) {
1062 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1067 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1068 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1069 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1072 switch (mapping->v4l2_type) {
1073 case V4L2_CTRL_TYPE_MENU:
1074 v4l2_ctrl->minimum = 0;
1075 v4l2_ctrl->maximum = mapping->menu_count - 1;
1076 v4l2_ctrl->step = 1;
1078 menu = mapping->menu_info;
1079 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1080 if (menu->value == v4l2_ctrl->default_value) {
1081 v4l2_ctrl->default_value = i;
1088 case V4L2_CTRL_TYPE_BOOLEAN:
1089 v4l2_ctrl->minimum = 0;
1090 v4l2_ctrl->maximum = 1;
1091 v4l2_ctrl->step = 1;
1094 case V4L2_CTRL_TYPE_BUTTON:
1095 v4l2_ctrl->minimum = 0;
1096 v4l2_ctrl->maximum = 0;
1097 v4l2_ctrl->step = 0;
1104 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1105 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1106 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1108 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1109 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1110 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1112 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1113 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1114 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1119 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1120 struct v4l2_queryctrl *v4l2_ctrl)
1122 struct uvc_control *ctrl;
1123 struct uvc_control_mapping *mapping;
1126 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1128 return -ERESTARTSYS;
1130 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1136 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1138 mutex_unlock(&chain->ctrl_mutex);
1143 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1144 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1145 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1146 * Balance V4L2 controls use the White Balance Component UVC control) or
1147 * otherwise translated. The approach we take here is to use a translation
1148 * table for the controls that can be mapped directly, and handle the others
1151 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1152 struct v4l2_querymenu *query_menu)
1154 const struct uvc_menu_info *menu_info;
1155 struct uvc_control_mapping *mapping;
1156 struct uvc_control *ctrl;
1157 u32 index = query_menu->index;
1158 u32 id = query_menu->id;
1161 memset(query_menu, 0, sizeof(*query_menu));
1162 query_menu->id = id;
1163 query_menu->index = index;
1165 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1167 return -ERESTARTSYS;
1169 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1170 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1175 if (query_menu->index >= mapping->menu_count) {
1180 menu_info = &mapping->menu_info[query_menu->index];
1182 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1183 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1186 if (!ctrl->cached) {
1187 ret = uvc_ctrl_populate_cache(chain, ctrl);
1192 bitmap = mapping->get(mapping, UVC_GET_RES,
1193 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1194 if (!(bitmap & menu_info->value)) {
1200 strscpy(query_menu->name, menu_info->name, sizeof(query_menu->name));
1203 mutex_unlock(&chain->ctrl_mutex);
1207 /* --------------------------------------------------------------------------
1208 * Ctrl event handling
1211 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1212 struct v4l2_event *ev,
1213 struct uvc_control *ctrl,
1214 struct uvc_control_mapping *mapping,
1215 s32 value, u32 changes)
1217 struct v4l2_queryctrl v4l2_ctrl;
1219 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1221 memset(ev, 0, sizeof(*ev));
1222 ev->type = V4L2_EVENT_CTRL;
1223 ev->id = v4l2_ctrl.id;
1224 ev->u.ctrl.value = value;
1225 ev->u.ctrl.changes = changes;
1226 ev->u.ctrl.type = v4l2_ctrl.type;
1227 ev->u.ctrl.flags = v4l2_ctrl.flags;
1228 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1229 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1230 ev->u.ctrl.step = v4l2_ctrl.step;
1231 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1235 * Send control change events to all subscribers for the @ctrl control. By
1236 * default the subscriber that generated the event, as identified by @handle,
1237 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1238 * @handle can be NULL for asynchronous events related to auto-update controls,
1239 * in which case all subscribers are notified.
1241 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1242 struct uvc_fh *handle, struct uvc_control *ctrl,
1243 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1245 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1246 struct v4l2_subscribed_event *sev;
1247 struct v4l2_event ev;
1249 if (list_empty(&mapping->ev_subs))
1252 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1254 list_for_each_entry(sev, &mapping->ev_subs, node) {
1255 if (sev->fh != originator ||
1256 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1257 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1258 v4l2_event_queue_fh(sev->fh, &ev);
1263 * Send control change events for the slave of the @master control identified
1264 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1265 * generated the event and may be NULL for auto-update events.
1267 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1268 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1270 struct uvc_control_mapping *mapping = NULL;
1271 struct uvc_control *ctrl = NULL;
1272 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1275 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1279 if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1280 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1282 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1285 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1286 struct uvc_control *ctrl, const u8 *data)
1288 struct uvc_control_mapping *mapping;
1289 struct uvc_fh *handle;
1292 mutex_lock(&chain->ctrl_mutex);
1294 handle = ctrl->handle;
1295 ctrl->handle = NULL;
1297 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1298 s32 value = __uvc_ctrl_get_value(mapping, data);
1301 * handle may be NULL here if the device sends auto-update
1302 * events without a prior related control set from userspace.
1304 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1305 if (!mapping->slave_ids[i])
1308 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1309 mapping->slave_ids[i]);
1312 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1313 V4L2_EVENT_CTRL_CH_VALUE);
1316 mutex_unlock(&chain->ctrl_mutex);
1319 static void uvc_ctrl_status_event_work(struct work_struct *work)
1321 struct uvc_device *dev = container_of(work, struct uvc_device,
1323 struct uvc_ctrl_work *w = &dev->async_ctrl;
1326 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1328 /* Resubmit the URB. */
1329 w->urb->interval = dev->int_ep->desc.bInterval;
1330 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1332 dev_err(&dev->udev->dev,
1333 "Failed to resubmit status URB (%d).\n", ret);
1336 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1337 struct uvc_control *ctrl, const u8 *data)
1339 struct uvc_device *dev = chain->dev;
1340 struct uvc_ctrl_work *w = &dev->async_ctrl;
1342 if (list_empty(&ctrl->info.mappings)) {
1343 ctrl->handle = NULL;
1352 schedule_work(&w->work);
1357 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1358 unsigned int xctrls_count, u32 id)
1362 for (i = 0; i < xctrls_count; ++i) {
1363 if (xctrls[i].id == id)
1370 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1371 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1373 struct uvc_control_mapping *mapping;
1374 struct uvc_control *ctrl;
1375 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1379 for (i = 0; i < xctrls_count; ++i) {
1380 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1382 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1383 /* Notification will be sent from an Interrupt event. */
1386 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1387 u32 slave_id = mapping->slave_ids[j];
1393 * We can skip sending an event for the slave if the
1394 * slave is being modified in the same transaction.
1396 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1400 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1405 * If the master is being modified in the same transaction
1406 * flags may change too.
1408 if (mapping->master_id &&
1409 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1410 mapping->master_id))
1411 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1413 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1414 xctrls[i].value, changes);
1418 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1420 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1421 struct uvc_control_mapping *mapping;
1422 struct uvc_control *ctrl;
1425 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1427 return -ERESTARTSYS;
1429 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1435 list_add_tail(&sev->node, &mapping->ev_subs);
1436 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1437 struct v4l2_event ev;
1438 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1441 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1442 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1444 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1446 /* Mark the queue as active, allowing this initial
1447 event to be accepted. */
1449 v4l2_event_queue_fh(sev->fh, &ev);
1453 mutex_unlock(&handle->chain->ctrl_mutex);
1457 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1459 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1461 mutex_lock(&handle->chain->ctrl_mutex);
1462 list_del(&sev->node);
1463 mutex_unlock(&handle->chain->ctrl_mutex);
1466 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1467 .add = uvc_ctrl_add_event,
1468 .del = uvc_ctrl_del_event,
1469 .replace = v4l2_ctrl_replace,
1470 .merge = v4l2_ctrl_merge,
1473 /* --------------------------------------------------------------------------
1474 * Control transactions
1476 * To make extended set operations as atomic as the hardware allows, controls
1477 * are handled using begin/commit/rollback operations.
1479 * At the beginning of a set request, uvc_ctrl_begin should be called to
1480 * initialize the request. This function acquires the control lock.
1482 * When setting a control, the new value is stored in the control data field
1483 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1484 * later processing. If the UVC and V4L2 control sizes differ, the current
1485 * value is loaded from the hardware before storing the new value in the data
1488 * After processing all controls in the transaction, uvc_ctrl_commit or
1489 * uvc_ctrl_rollback must be called to apply the pending changes to the
1490 * hardware or revert them. When applying changes, all controls marked as
1491 * dirty will be modified in the UVC device, and the dirty flag will be
1492 * cleared. When reverting controls, the control data field
1493 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1494 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1497 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1499 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1502 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1503 struct uvc_entity *entity, int rollback)
1505 struct uvc_control *ctrl;
1512 for (i = 0; i < entity->ncontrols; ++i) {
1513 ctrl = &entity->controls[i];
1514 if (!ctrl->initialized)
1517 /* Reset the loaded flag for auto-update controls that were
1518 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1519 * uvc_ctrl_get from using the cached value, and for write-only
1520 * controls to prevent uvc_ctrl_set from setting bits not
1521 * explicitly set by the user.
1523 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1524 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1531 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1532 dev->intfnum, ctrl->info.selector,
1533 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1538 if (rollback || ret < 0)
1539 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1540 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1552 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1553 const struct v4l2_ext_control *xctrls,
1554 unsigned int xctrls_count)
1556 struct uvc_video_chain *chain = handle->chain;
1557 struct uvc_entity *entity;
1560 /* Find the control. */
1561 list_for_each_entry(entity, &chain->entities, chain) {
1562 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1568 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1570 mutex_unlock(&chain->ctrl_mutex);
1574 int uvc_ctrl_get(struct uvc_video_chain *chain,
1575 struct v4l2_ext_control *xctrl)
1577 struct uvc_control *ctrl;
1578 struct uvc_control_mapping *mapping;
1580 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1584 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1587 int uvc_ctrl_set(struct uvc_fh *handle,
1588 struct v4l2_ext_control *xctrl)
1590 struct uvc_video_chain *chain = handle->chain;
1591 struct uvc_control *ctrl;
1592 struct uvc_control_mapping *mapping;
1599 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1602 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1605 /* Clamp out of range values. */
1606 switch (mapping->v4l2_type) {
1607 case V4L2_CTRL_TYPE_INTEGER:
1608 if (!ctrl->cached) {
1609 ret = uvc_ctrl_populate_cache(chain, ctrl);
1614 min = mapping->get(mapping, UVC_GET_MIN,
1615 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1616 max = mapping->get(mapping, UVC_GET_MAX,
1617 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1618 step = mapping->get(mapping, UVC_GET_RES,
1619 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1623 xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
1625 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1626 xctrl->value = clamp(xctrl->value, min, max);
1628 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1629 value = xctrl->value;
1632 case V4L2_CTRL_TYPE_BOOLEAN:
1633 xctrl->value = clamp(xctrl->value, 0, 1);
1634 value = xctrl->value;
1637 case V4L2_CTRL_TYPE_MENU:
1638 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1640 value = mapping->menu_info[xctrl->value].value;
1642 /* Valid menu indices are reported by the GET_RES request for
1643 * UVC controls that support it.
1645 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1646 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1647 if (!ctrl->cached) {
1648 ret = uvc_ctrl_populate_cache(chain, ctrl);
1653 step = mapping->get(mapping, UVC_GET_RES,
1654 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1655 if (!(step & value))
1662 value = xctrl->value;
1666 /* If the mapping doesn't span the whole UVC control, the current value
1667 * needs to be loaded from the device to perform the read-modify-write
1670 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1671 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1672 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1673 0, ctrl->info.size);
1675 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1676 ctrl->entity->id, chain->dev->intfnum,
1677 ctrl->info.selector,
1678 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1687 /* Backup the current value in case we need to rollback later. */
1689 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1690 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1694 mapping->set(mapping, value,
1695 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1697 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1698 ctrl->handle = handle;
1705 /* --------------------------------------------------------------------------
1710 * Retrieve flags for a given control
1712 static int uvc_ctrl_get_flags(struct uvc_device *dev,
1713 const struct uvc_control *ctrl,
1714 struct uvc_control_info *info)
1719 data = kmalloc(1, GFP_KERNEL);
1723 if (ctrl->entity->get_info)
1724 ret = ctrl->entity->get_info(dev, ctrl->entity,
1725 ctrl->info.selector, data);
1727 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1728 dev->intfnum, info->selector, data, 1);
1730 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
1731 UVC_CTRL_FLAG_GET_CUR : 0)
1732 | (data[0] & UVC_CONTROL_CAP_SET ?
1733 UVC_CTRL_FLAG_SET_CUR : 0)
1734 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1735 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
1736 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
1737 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
1743 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1744 const struct uvc_control *ctrl, struct uvc_control_info *info)
1746 struct uvc_ctrl_fixup {
1747 struct usb_device_id id;
1753 static const struct uvc_ctrl_fixup fixups[] = {
1754 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1755 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1756 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1757 UVC_CTRL_FLAG_AUTO_UPDATE },
1758 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1759 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1760 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1761 UVC_CTRL_FLAG_AUTO_UPDATE },
1762 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1763 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1764 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1765 UVC_CTRL_FLAG_AUTO_UPDATE },
1770 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1771 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1774 if (fixups[i].entity == ctrl->entity->id &&
1775 fixups[i].selector == info->selector) {
1776 info->flags = fixups[i].flags;
1783 * Query control information (size and flags) for XU controls.
1785 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1786 const struct uvc_control *ctrl, struct uvc_control_info *info)
1791 data = kmalloc(2, GFP_KERNEL);
1795 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
1796 info->index = ctrl->index;
1797 info->selector = ctrl->index + 1;
1799 /* Query and verify the control length (GET_LEN) */
1800 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1801 info->selector, data, 2);
1803 uvc_dbg(dev, CONTROL,
1804 "GET_LEN failed on control %pUl/%u (%d)\n",
1805 info->entity, info->selector, ret);
1809 info->size = le16_to_cpup((__le16 *)data);
1811 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1812 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
1814 ret = uvc_ctrl_get_flags(dev, ctrl, info);
1816 uvc_dbg(dev, CONTROL,
1817 "Failed to get flags for control %pUl/%u (%d)\n",
1818 info->entity, info->selector, ret);
1822 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1824 uvc_dbg(dev, CONTROL,
1825 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
1826 info->entity, info->selector, info->size,
1827 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1828 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1829 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1836 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1837 const struct uvc_control_info *info);
1839 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1840 struct uvc_control *ctrl)
1842 struct uvc_control_info info;
1845 if (ctrl->initialized)
1848 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1852 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1854 uvc_dbg(dev, CONTROL,
1855 "Failed to initialize control %pUl/%u on device %s entity %u\n",
1856 info.entity, info.selector, dev->udev->devpath,
1862 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1863 struct uvc_xu_control_query *xqry)
1865 struct uvc_entity *entity;
1866 struct uvc_control *ctrl;
1874 /* Find the extension unit. */
1876 list_for_each_entry(entity, &chain->entities, chain) {
1877 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1878 entity->id == xqry->unit) {
1885 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
1890 /* Find the control and perform delayed initialization if needed. */
1892 for (i = 0; i < entity->ncontrols; ++i) {
1893 ctrl = &entity->controls[i];
1894 if (ctrl->index == xqry->selector - 1) {
1901 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
1902 entity->guid, xqry->selector);
1906 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1907 return -ERESTARTSYS;
1909 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1915 /* Validate the required buffer size and flags for the request */
1917 size = ctrl->info.size;
1919 switch (xqry->query) {
1921 reqflags = UVC_CTRL_FLAG_GET_CUR;
1924 reqflags = UVC_CTRL_FLAG_GET_MIN;
1927 reqflags = UVC_CTRL_FLAG_GET_MAX;
1930 reqflags = UVC_CTRL_FLAG_GET_DEF;
1933 reqflags = UVC_CTRL_FLAG_GET_RES;
1936 reqflags = UVC_CTRL_FLAG_SET_CUR;
1949 if (size != xqry->size) {
1954 if (reqflags && !(ctrl->info.flags & reqflags)) {
1959 data = kmalloc(size, GFP_KERNEL);
1965 if (xqry->query == UVC_SET_CUR &&
1966 copy_from_user(data, xqry->data, size)) {
1971 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1972 chain->dev->intfnum, xqry->selector, data, size);
1976 if (xqry->query != UVC_SET_CUR &&
1977 copy_to_user(xqry->data, data, size))
1981 mutex_unlock(&chain->ctrl_mutex);
1985 /* --------------------------------------------------------------------------
1990 * Restore control values after resume, skipping controls that haven't been
1994 * - Don't restore modified controls that are back to their default value.
1995 * - Handle restore order (Auto-Exposure Mode should be restored before
1998 int uvc_ctrl_restore_values(struct uvc_device *dev)
2000 struct uvc_control *ctrl;
2001 struct uvc_entity *entity;
2005 /* Walk the entities list and restore controls when possible. */
2006 list_for_each_entry(entity, &dev->entities, list) {
2008 for (i = 0; i < entity->ncontrols; ++i) {
2009 ctrl = &entity->controls[i];
2011 if (!ctrl->initialized || !ctrl->modified ||
2012 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2014 dev_info(&dev->udev->dev,
2015 "restoring control %pUl/%u/%u\n",
2016 ctrl->info.entity, ctrl->info.index,
2017 ctrl->info.selector);
2021 ret = uvc_ctrl_commit_entity(dev, entity, 0);
2029 /* --------------------------------------------------------------------------
2030 * Control and mapping handling
2034 * Add control information to a given control.
2036 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2037 const struct uvc_control_info *info)
2040 INIT_LIST_HEAD(&ctrl->info.mappings);
2042 /* Allocate an array to save control values (cur, def, max, etc.) */
2043 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2045 if (!ctrl->uvc_data)
2048 ctrl->initialized = 1;
2050 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2051 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2058 * Add a control mapping to a given control.
2060 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
2061 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2063 struct uvc_control_mapping *map;
2066 /* Most mappings come from static kernel data and need to be duplicated.
2067 * Mappings that come from userspace will be unnecessarily duplicated,
2068 * this could be optimized.
2070 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2074 INIT_LIST_HEAD(&map->ev_subs);
2076 size = sizeof(*mapping->menu_info) * mapping->menu_count;
2077 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
2078 if (map->menu_info == NULL) {
2083 if (map->get == NULL)
2084 map->get = uvc_get_le_value;
2085 if (map->set == NULL)
2086 map->set = uvc_set_le_value;
2088 list_add_tail(&map->list, &ctrl->info.mappings);
2089 uvc_dbg(dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2090 map->name, ctrl->info.entity, ctrl->info.selector);
2095 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2096 const struct uvc_control_mapping *mapping)
2098 struct uvc_device *dev = chain->dev;
2099 struct uvc_control_mapping *map;
2100 struct uvc_entity *entity;
2101 struct uvc_control *ctrl;
2105 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2106 uvc_dbg(dev, CONTROL,
2107 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2108 mapping->name, mapping->id);
2112 /* Search for the matching (GUID/CS) control on the current chain */
2113 list_for_each_entry(entity, &chain->entities, chain) {
2116 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2117 !uvc_entity_match_guid(entity, mapping->entity))
2120 for (i = 0; i < entity->ncontrols; ++i) {
2121 ctrl = &entity->controls[i];
2122 if (ctrl->index == mapping->selector - 1) {
2134 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2135 return -ERESTARTSYS;
2137 /* Perform delayed initialization of XU controls */
2138 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2144 /* Validate the user-provided bit-size and offset */
2145 if (mapping->size > 32 ||
2146 mapping->offset + mapping->size > ctrl->info.size * 8) {
2151 list_for_each_entry(map, &ctrl->info.mappings, list) {
2152 if (mapping->id == map->id) {
2153 uvc_dbg(dev, CONTROL,
2154 "Can't add mapping '%s', control id 0x%08x already exists\n",
2155 mapping->name, mapping->id);
2161 /* Prevent excess memory consumption */
2162 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2163 atomic_dec(&dev->nmappings);
2164 uvc_dbg(dev, CONTROL,
2165 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
2166 mapping->name, UVC_MAX_CONTROL_MAPPINGS);
2171 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2173 atomic_dec(&dev->nmappings);
2176 mutex_unlock(&chain->ctrl_mutex);
2181 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2182 * are currently the ones that crash the camera or unconditionally return an
2183 * error when queried.
2185 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2186 struct uvc_entity *entity)
2188 struct uvc_ctrl_blacklist {
2189 struct usb_device_id id;
2193 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2194 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2195 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2196 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2198 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2199 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2202 const struct uvc_ctrl_blacklist *blacklist;
2208 switch (UVC_ENTITY_TYPE(entity)) {
2209 case UVC_VC_PROCESSING_UNIT:
2210 blacklist = processing_blacklist;
2211 count = ARRAY_SIZE(processing_blacklist);
2212 controls = entity->processing.bmControls;
2213 size = entity->processing.bControlSize;
2216 case UVC_ITT_CAMERA:
2217 blacklist = camera_blacklist;
2218 count = ARRAY_SIZE(camera_blacklist);
2219 controls = entity->camera.bmControls;
2220 size = entity->camera.bControlSize;
2227 for (i = 0; i < count; ++i) {
2228 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2231 if (blacklist[i].index >= 8 * size ||
2232 !uvc_test_bit(controls, blacklist[i].index))
2235 uvc_dbg(dev, CONTROL,
2236 "%u/%u control is black listed, removing it\n",
2237 entity->id, blacklist[i].index);
2239 uvc_clear_bit(controls, blacklist[i].index);
2244 * Add control information and hardcoded stock control mappings to the given
2247 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2249 const struct uvc_control_info *info = uvc_ctrls;
2250 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2251 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2252 const struct uvc_control_mapping *mend =
2253 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2255 /* XU controls initialization requires querying the device for control
2256 * information. As some buggy UVC devices will crash when queried
2257 * repeatedly in a tight loop, delay XU controls initialization until
2260 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2263 for (; info < iend; ++info) {
2264 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2265 ctrl->index == info->index) {
2266 uvc_ctrl_add_info(dev, ctrl, info);
2268 * Retrieve control flags from the device. Ignore errors
2269 * and work with default flag values from the uvc_ctrl
2270 * array when the device doesn't properly implement
2271 * GET_INFO on standard controls.
2273 uvc_ctrl_get_flags(dev, ctrl, &ctrl->info);
2278 if (!ctrl->initialized)
2281 for (; mapping < mend; ++mapping) {
2282 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2283 ctrl->info.selector == mapping->selector)
2284 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2289 * Initialize device controls.
2291 int uvc_ctrl_init_device(struct uvc_device *dev)
2293 struct uvc_entity *entity;
2296 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2298 /* Walk the entities list and instantiate controls */
2299 list_for_each_entry(entity, &dev->entities, list) {
2300 struct uvc_control *ctrl;
2301 unsigned int bControlSize = 0, ncontrols;
2302 u8 *bmControls = NULL;
2304 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2305 bmControls = entity->extension.bmControls;
2306 bControlSize = entity->extension.bControlSize;
2307 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2308 bmControls = entity->processing.bmControls;
2309 bControlSize = entity->processing.bControlSize;
2310 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2311 bmControls = entity->camera.bmControls;
2312 bControlSize = entity->camera.bControlSize;
2313 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
2314 bmControls = entity->gpio.bmControls;
2315 bControlSize = entity->gpio.bControlSize;
2318 /* Remove bogus/blacklisted controls */
2319 uvc_ctrl_prune_entity(dev, entity);
2321 /* Count supported controls and allocate the controls array */
2322 ncontrols = memweight(bmControls, bControlSize);
2326 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2328 if (entity->controls == NULL)
2330 entity->ncontrols = ncontrols;
2332 /* Initialize all supported controls */
2333 ctrl = entity->controls;
2334 for (i = 0; i < bControlSize * 8; ++i) {
2335 if (uvc_test_bit(bmControls, i) == 0)
2338 ctrl->entity = entity;
2341 uvc_ctrl_init_ctrl(dev, ctrl);
2350 * Cleanup device controls.
2352 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2353 struct uvc_control *ctrl)
2355 struct uvc_control_mapping *mapping, *nm;
2357 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2358 list_del(&mapping->list);
2359 kfree(mapping->menu_info);
2364 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2366 struct uvc_entity *entity;
2369 /* Can be uninitialized if we are aborting on probe error. */
2370 if (dev->async_ctrl.work.func)
2371 cancel_work_sync(&dev->async_ctrl.work);
2373 /* Free controls and control mappings for all entities. */
2374 list_for_each_entry(entity, &dev->entities, list) {
2375 for (i = 0; i < entity->ncontrols; ++i) {
2376 struct uvc_control *ctrl = &entity->controls[i];
2378 if (!ctrl->initialized)
2381 uvc_ctrl_cleanup_mappings(dev, ctrl);
2382 kfree(ctrl->uvc_data);
2385 kfree(entity->controls);