1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * uvc_ctrl.c -- USB Video Class driver - Controls
5 * Copyright (C) 2005-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 #include <asm/barrier.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/uaccess.h>
15 #include <linux/usb.h>
16 #include <linux/videodev2.h>
17 #include <linux/vmalloc.h>
18 #include <linux/wait.h>
19 #include <linux/workqueue.h>
20 #include <linux/atomic.h>
21 #include <media/v4l2-ctrls.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,
353 static const struct uvc_menu_info power_line_frequency_controls[] = {
359 static const struct uvc_menu_info exposure_auto_controls[] = {
361 { 1, "Manual Mode" },
362 { 4, "Shutter Priority Mode" },
363 { 8, "Aperture Priority Mode" },
366 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
367 u8 query, const u8 *data)
369 s8 zoom = (s8)data[0];
373 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
384 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
387 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
388 data[2] = min((int)abs(value), 0xff);
391 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
392 u8 query, const u8 *data)
394 unsigned int first = mapping->offset / 8;
395 s8 rel = (s8)data[first];
399 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
402 return -data[first+1];
407 return data[first+1];
411 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
414 unsigned int first = mapping->offset / 8;
416 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
417 data[first+1] = min_t(int, abs(value), 0xff);
420 static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
422 .id = V4L2_CID_BRIGHTNESS,
423 .name = "Brightness",
424 .entity = UVC_GUID_UVC_PROCESSING,
425 .selector = UVC_PU_BRIGHTNESS_CONTROL,
428 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
429 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
432 .id = V4L2_CID_CONTRAST,
434 .entity = UVC_GUID_UVC_PROCESSING,
435 .selector = UVC_PU_CONTRAST_CONTROL,
438 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
439 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
444 .entity = UVC_GUID_UVC_PROCESSING,
445 .selector = UVC_PU_HUE_CONTROL,
448 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
449 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
450 .master_id = V4L2_CID_HUE_AUTO,
454 .id = V4L2_CID_SATURATION,
455 .name = "Saturation",
456 .entity = UVC_GUID_UVC_PROCESSING,
457 .selector = UVC_PU_SATURATION_CONTROL,
460 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
461 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
464 .id = V4L2_CID_SHARPNESS,
466 .entity = UVC_GUID_UVC_PROCESSING,
467 .selector = UVC_PU_SHARPNESS_CONTROL,
470 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
471 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
474 .id = V4L2_CID_GAMMA,
476 .entity = UVC_GUID_UVC_PROCESSING,
477 .selector = UVC_PU_GAMMA_CONTROL,
480 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
481 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
484 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
485 .name = "Backlight Compensation",
486 .entity = UVC_GUID_UVC_PROCESSING,
487 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
490 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
491 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
496 .entity = UVC_GUID_UVC_PROCESSING,
497 .selector = UVC_PU_GAIN_CONTROL,
500 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
501 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
504 .id = V4L2_CID_POWER_LINE_FREQUENCY,
505 .name = "Power Line Frequency",
506 .entity = UVC_GUID_UVC_PROCESSING,
507 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
510 .v4l2_type = V4L2_CTRL_TYPE_MENU,
511 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
512 .menu_info = power_line_frequency_controls,
513 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
516 .id = V4L2_CID_HUE_AUTO,
518 .entity = UVC_GUID_UVC_PROCESSING,
519 .selector = UVC_PU_HUE_AUTO_CONTROL,
522 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
523 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
524 .slave_ids = { V4L2_CID_HUE, },
527 .id = V4L2_CID_EXPOSURE_AUTO,
528 .name = "Exposure, Auto",
529 .entity = UVC_GUID_UVC_CAMERA,
530 .selector = UVC_CT_AE_MODE_CONTROL,
533 .v4l2_type = V4L2_CTRL_TYPE_MENU,
534 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
535 .menu_info = exposure_auto_controls,
536 .menu_count = ARRAY_SIZE(exposure_auto_controls),
537 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
540 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
541 .name = "Exposure, Auto Priority",
542 .entity = UVC_GUID_UVC_CAMERA,
543 .selector = UVC_CT_AE_PRIORITY_CONTROL,
546 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
547 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
550 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
551 .name = "Exposure (Absolute)",
552 .entity = UVC_GUID_UVC_CAMERA,
553 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
556 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
557 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
558 .master_id = V4L2_CID_EXPOSURE_AUTO,
559 .master_manual = V4L2_EXPOSURE_MANUAL,
562 .id = V4L2_CID_AUTO_WHITE_BALANCE,
563 .name = "White Balance Temperature, Auto",
564 .entity = UVC_GUID_UVC_PROCESSING,
565 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
568 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
569 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
570 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
573 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
574 .name = "White Balance Temperature",
575 .entity = UVC_GUID_UVC_PROCESSING,
576 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
579 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
580 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
581 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
585 .id = V4L2_CID_AUTO_WHITE_BALANCE,
586 .name = "White Balance Component, Auto",
587 .entity = UVC_GUID_UVC_PROCESSING,
588 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
591 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
592 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
593 .slave_ids = { V4L2_CID_BLUE_BALANCE,
594 V4L2_CID_RED_BALANCE },
597 .id = V4L2_CID_BLUE_BALANCE,
598 .name = "White Balance Blue Component",
599 .entity = UVC_GUID_UVC_PROCESSING,
600 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
603 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
604 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
605 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
609 .id = V4L2_CID_RED_BALANCE,
610 .name = "White Balance Red Component",
611 .entity = UVC_GUID_UVC_PROCESSING,
612 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
615 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
616 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
617 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
621 .id = V4L2_CID_FOCUS_ABSOLUTE,
622 .name = "Focus (absolute)",
623 .entity = UVC_GUID_UVC_CAMERA,
624 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
627 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
628 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
629 .master_id = V4L2_CID_FOCUS_AUTO,
633 .id = V4L2_CID_FOCUS_AUTO,
634 .name = "Focus, Auto",
635 .entity = UVC_GUID_UVC_CAMERA,
636 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
639 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
640 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
641 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
644 .id = V4L2_CID_IRIS_ABSOLUTE,
645 .name = "Iris, Absolute",
646 .entity = UVC_GUID_UVC_CAMERA,
647 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
650 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
651 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
654 .id = V4L2_CID_IRIS_RELATIVE,
655 .name = "Iris, Relative",
656 .entity = UVC_GUID_UVC_CAMERA,
657 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
660 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
661 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
664 .id = V4L2_CID_ZOOM_ABSOLUTE,
665 .name = "Zoom, Absolute",
666 .entity = UVC_GUID_UVC_CAMERA,
667 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
670 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
671 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
674 .id = V4L2_CID_ZOOM_CONTINUOUS,
675 .name = "Zoom, Continuous",
676 .entity = UVC_GUID_UVC_CAMERA,
677 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
680 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
681 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
682 .get = uvc_ctrl_get_zoom,
683 .set = uvc_ctrl_set_zoom,
686 .id = V4L2_CID_PAN_ABSOLUTE,
687 .name = "Pan (Absolute)",
688 .entity = UVC_GUID_UVC_CAMERA,
689 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
692 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
693 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
696 .id = V4L2_CID_TILT_ABSOLUTE,
697 .name = "Tilt (Absolute)",
698 .entity = UVC_GUID_UVC_CAMERA,
699 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
702 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
703 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
706 .id = V4L2_CID_PAN_SPEED,
707 .name = "Pan (Speed)",
708 .entity = UVC_GUID_UVC_CAMERA,
709 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
712 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
713 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
714 .get = uvc_ctrl_get_rel_speed,
715 .set = uvc_ctrl_set_rel_speed,
718 .id = V4L2_CID_TILT_SPEED,
719 .name = "Tilt (Speed)",
720 .entity = UVC_GUID_UVC_CAMERA,
721 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
724 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
725 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
726 .get = uvc_ctrl_get_rel_speed,
727 .set = uvc_ctrl_set_rel_speed,
730 .id = V4L2_CID_PRIVACY,
732 .entity = UVC_GUID_UVC_CAMERA,
733 .selector = UVC_CT_PRIVACY_CONTROL,
736 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
737 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
741 /* ------------------------------------------------------------------------
745 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
747 return ctrl->uvc_data + id * ctrl->info.size;
750 static inline int uvc_test_bit(const u8 *data, int bit)
752 return (data[bit >> 3] >> (bit & 7)) & 1;
755 static inline void uvc_clear_bit(u8 *data, int bit)
757 data[bit >> 3] &= ~(1 << (bit & 7));
760 /* Extract the bit string specified by mapping->offset and mapping->size
761 * from the little-endian data stored at 'data' and return the result as
762 * a signed 32bit integer. Sign extension will be performed if the mapping
763 * references a signed data type.
765 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
766 u8 query, const u8 *data)
768 int bits = mapping->size;
769 int offset = mapping->offset;
775 mask = ((1LL << bits) - 1) << offset;
778 u8 byte = *data & mask;
779 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
780 bits -= 8 - (offset > 0 ? offset : 0);
785 mask = (1 << bits) - 1;
789 /* Sign-extend the value if needed. */
790 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
791 value |= -(value & (1 << (mapping->size - 1)));
796 /* Set the bit string specified by mapping->offset and mapping->size
797 * in the little-endian data stored at 'data' to the value 'value'.
799 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
802 int bits = mapping->size;
803 int offset = mapping->offset;
806 /* According to the v4l2 spec, writing any value to a button control
807 * should result in the action belonging to the button control being
808 * triggered. UVC devices however want to see a 1 written -> override
811 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
817 for (; bits > 0; data++) {
818 mask = ((1LL << bits) - 1) << offset;
819 *data = (*data & ~mask) | ((value << offset) & mask);
820 value >>= offset ? offset : 8;
826 /* ------------------------------------------------------------------------
827 * Terminal and unit management
830 static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
831 static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
832 static const u8 uvc_media_transport_input_guid[16] =
833 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
835 static int uvc_entity_match_guid(const struct uvc_entity *entity,
838 switch (UVC_ENTITY_TYPE(entity)) {
840 return memcmp(uvc_camera_guid, guid, 16) == 0;
842 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
843 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
845 case UVC_VC_PROCESSING_UNIT:
846 return memcmp(uvc_processing_guid, guid, 16) == 0;
848 case UVC_VC_EXTENSION_UNIT:
849 return memcmp(entity->extension.guidExtensionCode,
857 /* ------------------------------------------------------------------------
861 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
862 struct uvc_control_mapping **mapping, struct uvc_control **control,
865 struct uvc_control *ctrl;
866 struct uvc_control_mapping *map;
872 for (i = 0; i < entity->ncontrols; ++i) {
873 ctrl = &entity->controls[i];
874 if (!ctrl->initialized)
877 list_for_each_entry(map, &ctrl->info.mappings, list) {
878 if ((map->id == v4l2_id) && !next) {
884 if ((*mapping == NULL || (*mapping)->id > map->id) &&
885 (map->id > v4l2_id) && next) {
893 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
894 u32 v4l2_id, struct uvc_control_mapping **mapping)
896 struct uvc_control *ctrl = NULL;
897 struct uvc_entity *entity;
898 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
902 /* Mask the query flags. */
903 v4l2_id &= V4L2_CTRL_ID_MASK;
905 /* Find the control. */
906 list_for_each_entry(entity, &chain->entities, chain) {
907 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
912 if (ctrl == NULL && !next)
913 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
919 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
920 struct uvc_control *ctrl)
924 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
925 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
926 chain->dev->intfnum, ctrl->info.selector,
927 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
933 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
934 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
935 chain->dev->intfnum, ctrl->info.selector,
936 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
941 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
942 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
943 chain->dev->intfnum, ctrl->info.selector,
944 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
949 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
950 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
951 chain->dev->intfnum, ctrl->info.selector,
952 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
955 if (UVC_ENTITY_TYPE(ctrl->entity) !=
956 UVC_VC_EXTENSION_UNIT)
959 /* GET_RES is mandatory for XU controls, but some
960 * cameras still choke on it. Ignore errors and set the
961 * resolution value to zero.
963 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
964 "UVC non compliance - GET_RES failed on "
965 "an XU control. Enabling workaround.\n");
966 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
975 static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
978 s32 value = mapping->get(mapping, UVC_GET_CUR, data);
980 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
981 const struct uvc_menu_info *menu = mapping->menu_info;
984 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
985 if (menu->value == value) {
995 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
996 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
1001 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1004 if (!ctrl->loaded) {
1005 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1006 chain->dev->intfnum, ctrl->info.selector,
1007 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1015 *value = __uvc_ctrl_get_value(mapping,
1016 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1021 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1022 struct uvc_control *ctrl,
1023 struct uvc_control_mapping *mapping,
1024 struct v4l2_queryctrl *v4l2_ctrl)
1026 struct uvc_control_mapping *master_map = NULL;
1027 struct uvc_control *master_ctrl = NULL;
1028 const struct uvc_menu_info *menu;
1031 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1032 v4l2_ctrl->id = mapping->id;
1033 v4l2_ctrl->type = mapping->v4l2_type;
1034 strscpy(v4l2_ctrl->name, mapping->name, sizeof(v4l2_ctrl->name));
1035 v4l2_ctrl->flags = 0;
1037 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1038 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1039 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1040 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1042 if (mapping->master_id)
1043 __uvc_find_control(ctrl->entity, mapping->master_id,
1044 &master_map, &master_ctrl, 0);
1045 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1047 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1051 if (val != mapping->master_manual)
1052 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1055 if (!ctrl->cached) {
1056 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1061 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1062 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1063 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1066 switch (mapping->v4l2_type) {
1067 case V4L2_CTRL_TYPE_MENU:
1068 v4l2_ctrl->minimum = 0;
1069 v4l2_ctrl->maximum = mapping->menu_count - 1;
1070 v4l2_ctrl->step = 1;
1072 menu = mapping->menu_info;
1073 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1074 if (menu->value == v4l2_ctrl->default_value) {
1075 v4l2_ctrl->default_value = i;
1082 case V4L2_CTRL_TYPE_BOOLEAN:
1083 v4l2_ctrl->minimum = 0;
1084 v4l2_ctrl->maximum = 1;
1085 v4l2_ctrl->step = 1;
1088 case V4L2_CTRL_TYPE_BUTTON:
1089 v4l2_ctrl->minimum = 0;
1090 v4l2_ctrl->maximum = 0;
1091 v4l2_ctrl->step = 0;
1098 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1099 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1100 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1102 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1103 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1104 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1106 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1107 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1108 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1113 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1114 struct v4l2_queryctrl *v4l2_ctrl)
1116 struct uvc_control *ctrl;
1117 struct uvc_control_mapping *mapping;
1120 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1122 return -ERESTARTSYS;
1124 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1130 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1132 mutex_unlock(&chain->ctrl_mutex);
1137 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1138 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1139 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1140 * Balance V4L2 controls use the White Balance Component UVC control) or
1141 * otherwise translated. The approach we take here is to use a translation
1142 * table for the controls that can be mapped directly, and handle the others
1145 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1146 struct v4l2_querymenu *query_menu)
1148 const struct uvc_menu_info *menu_info;
1149 struct uvc_control_mapping *mapping;
1150 struct uvc_control *ctrl;
1151 u32 index = query_menu->index;
1152 u32 id = query_menu->id;
1155 memset(query_menu, 0, sizeof(*query_menu));
1156 query_menu->id = id;
1157 query_menu->index = index;
1159 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1161 return -ERESTARTSYS;
1163 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1164 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1169 if (query_menu->index >= mapping->menu_count) {
1174 menu_info = &mapping->menu_info[query_menu->index];
1176 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1177 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1180 if (!ctrl->cached) {
1181 ret = uvc_ctrl_populate_cache(chain, ctrl);
1186 bitmap = mapping->get(mapping, UVC_GET_RES,
1187 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1188 if (!(bitmap & menu_info->value)) {
1194 strscpy(query_menu->name, menu_info->name, sizeof(query_menu->name));
1197 mutex_unlock(&chain->ctrl_mutex);
1201 /* --------------------------------------------------------------------------
1202 * Ctrl event handling
1205 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1206 struct v4l2_event *ev,
1207 struct uvc_control *ctrl,
1208 struct uvc_control_mapping *mapping,
1209 s32 value, u32 changes)
1211 struct v4l2_queryctrl v4l2_ctrl;
1213 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1215 memset(ev, 0, sizeof(*ev));
1216 ev->type = V4L2_EVENT_CTRL;
1217 ev->id = v4l2_ctrl.id;
1218 ev->u.ctrl.value = value;
1219 ev->u.ctrl.changes = changes;
1220 ev->u.ctrl.type = v4l2_ctrl.type;
1221 ev->u.ctrl.flags = v4l2_ctrl.flags;
1222 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1223 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1224 ev->u.ctrl.step = v4l2_ctrl.step;
1225 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1229 * Send control change events to all subscribers for the @ctrl control. By
1230 * default the subscriber that generated the event, as identified by @handle,
1231 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1232 * @handle can be NULL for asynchronous events related to auto-update controls,
1233 * in which case all subscribers are notified.
1235 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1236 struct uvc_fh *handle, struct uvc_control *ctrl,
1237 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1239 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1240 struct v4l2_subscribed_event *sev;
1241 struct v4l2_event ev;
1243 if (list_empty(&mapping->ev_subs))
1246 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1248 list_for_each_entry(sev, &mapping->ev_subs, node) {
1249 if (sev->fh != originator ||
1250 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1251 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1252 v4l2_event_queue_fh(sev->fh, &ev);
1257 * Send control change events for the slave of the @master control identified
1258 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1259 * generated the event and may be NULL for auto-update events.
1261 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1262 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1264 struct uvc_control_mapping *mapping = NULL;
1265 struct uvc_control *ctrl = NULL;
1266 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1269 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1273 if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1274 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1276 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1279 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1280 struct uvc_control *ctrl, const u8 *data)
1282 struct uvc_control_mapping *mapping;
1283 struct uvc_fh *handle;
1286 mutex_lock(&chain->ctrl_mutex);
1288 handle = ctrl->handle;
1289 ctrl->handle = NULL;
1291 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1292 s32 value = __uvc_ctrl_get_value(mapping, data);
1295 * handle may be NULL here if the device sends auto-update
1296 * events without a prior related control set from userspace.
1298 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1299 if (!mapping->slave_ids[i])
1302 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1303 mapping->slave_ids[i]);
1306 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1307 V4L2_EVENT_CTRL_CH_VALUE);
1310 mutex_unlock(&chain->ctrl_mutex);
1313 static void uvc_ctrl_status_event_work(struct work_struct *work)
1315 struct uvc_device *dev = container_of(work, struct uvc_device,
1317 struct uvc_ctrl_work *w = &dev->async_ctrl;
1320 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1322 /* The barrier is needed to synchronize with uvc_status_stop(). */
1323 if (smp_load_acquire(&dev->flush_status))
1326 /* Resubmit the URB. */
1327 w->urb->interval = dev->int_ep->desc.bInterval;
1328 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1330 uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
1334 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1335 struct uvc_control *ctrl, const u8 *data)
1337 struct uvc_device *dev = chain->dev;
1338 struct uvc_ctrl_work *w = &dev->async_ctrl;
1340 if (list_empty(&ctrl->info.mappings)) {
1341 ctrl->handle = NULL;
1350 schedule_work(&w->work);
1355 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1356 unsigned int xctrls_count, u32 id)
1360 for (i = 0; i < xctrls_count; ++i) {
1361 if (xctrls[i].id == id)
1368 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1369 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1371 struct uvc_control_mapping *mapping;
1372 struct uvc_control *ctrl;
1373 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1377 for (i = 0; i < xctrls_count; ++i) {
1378 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1380 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1381 /* Notification will be sent from an Interrupt event. */
1384 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1385 u32 slave_id = mapping->slave_ids[j];
1391 * We can skip sending an event for the slave if the
1392 * slave is being modified in the same transaction.
1394 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1398 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1403 * If the master is being modified in the same transaction
1404 * flags may change too.
1406 if (mapping->master_id &&
1407 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1408 mapping->master_id))
1409 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1411 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1412 xctrls[i].value, changes);
1416 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1418 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1419 struct uvc_control_mapping *mapping;
1420 struct uvc_control *ctrl;
1423 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1425 return -ERESTARTSYS;
1427 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1433 list_add_tail(&sev->node, &mapping->ev_subs);
1434 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1435 struct v4l2_event ev;
1436 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1439 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1440 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1442 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1444 /* Mark the queue as active, allowing this initial
1445 event to be accepted. */
1447 v4l2_event_queue_fh(sev->fh, &ev);
1451 mutex_unlock(&handle->chain->ctrl_mutex);
1455 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1457 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1459 mutex_lock(&handle->chain->ctrl_mutex);
1460 list_del(&sev->node);
1461 mutex_unlock(&handle->chain->ctrl_mutex);
1464 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1465 .add = uvc_ctrl_add_event,
1466 .del = uvc_ctrl_del_event,
1467 .replace = v4l2_ctrl_replace,
1468 .merge = v4l2_ctrl_merge,
1471 /* --------------------------------------------------------------------------
1472 * Control transactions
1474 * To make extended set operations as atomic as the hardware allows, controls
1475 * are handled using begin/commit/rollback operations.
1477 * At the beginning of a set request, uvc_ctrl_begin should be called to
1478 * initialize the request. This function acquires the control lock.
1480 * When setting a control, the new value is stored in the control data field
1481 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1482 * later processing. If the UVC and V4L2 control sizes differ, the current
1483 * value is loaded from the hardware before storing the new value in the data
1486 * After processing all controls in the transaction, uvc_ctrl_commit or
1487 * uvc_ctrl_rollback must be called to apply the pending changes to the
1488 * hardware or revert them. When applying changes, all controls marked as
1489 * dirty will be modified in the UVC device, and the dirty flag will be
1490 * cleared. When reverting controls, the control data field
1491 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1492 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1495 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1497 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1500 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1501 struct uvc_entity *entity, int rollback)
1503 struct uvc_control *ctrl;
1510 for (i = 0; i < entity->ncontrols; ++i) {
1511 ctrl = &entity->controls[i];
1512 if (!ctrl->initialized)
1515 /* Reset the loaded flag for auto-update controls that were
1516 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1517 * uvc_ctrl_get from using the cached value, and for write-only
1518 * controls to prevent uvc_ctrl_set from setting bits not
1519 * explicitly set by the user.
1521 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1522 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1529 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1530 dev->intfnum, ctrl->info.selector,
1531 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1536 if (rollback || ret < 0)
1537 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1538 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1550 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1551 const struct v4l2_ext_control *xctrls,
1552 unsigned int xctrls_count)
1554 struct uvc_video_chain *chain = handle->chain;
1555 struct uvc_entity *entity;
1558 /* Find the control. */
1559 list_for_each_entry(entity, &chain->entities, chain) {
1560 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1566 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1568 mutex_unlock(&chain->ctrl_mutex);
1572 int uvc_ctrl_get(struct uvc_video_chain *chain,
1573 struct v4l2_ext_control *xctrl)
1575 struct uvc_control *ctrl;
1576 struct uvc_control_mapping *mapping;
1578 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1582 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1585 int uvc_ctrl_set(struct uvc_fh *handle,
1586 struct v4l2_ext_control *xctrl)
1588 struct uvc_video_chain *chain = handle->chain;
1589 struct uvc_control *ctrl;
1590 struct uvc_control_mapping *mapping;
1597 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1600 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1603 /* Clamp out of range values. */
1604 switch (mapping->v4l2_type) {
1605 case V4L2_CTRL_TYPE_INTEGER:
1606 if (!ctrl->cached) {
1607 ret = uvc_ctrl_populate_cache(chain, ctrl);
1612 min = mapping->get(mapping, UVC_GET_MIN,
1613 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1614 max = mapping->get(mapping, UVC_GET_MAX,
1615 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1616 step = mapping->get(mapping, UVC_GET_RES,
1617 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1621 xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
1623 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1624 xctrl->value = clamp(xctrl->value, min, max);
1626 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1627 value = xctrl->value;
1630 case V4L2_CTRL_TYPE_BOOLEAN:
1631 xctrl->value = clamp(xctrl->value, 0, 1);
1632 value = xctrl->value;
1635 case V4L2_CTRL_TYPE_MENU:
1636 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1638 value = mapping->menu_info[xctrl->value].value;
1640 /* Valid menu indices are reported by the GET_RES request for
1641 * UVC controls that support it.
1643 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1644 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1645 if (!ctrl->cached) {
1646 ret = uvc_ctrl_populate_cache(chain, ctrl);
1651 step = mapping->get(mapping, UVC_GET_RES,
1652 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1653 if (!(step & value))
1660 value = xctrl->value;
1664 /* If the mapping doesn't span the whole UVC control, the current value
1665 * needs to be loaded from the device to perform the read-modify-write
1668 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1669 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1670 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1671 0, ctrl->info.size);
1673 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1674 ctrl->entity->id, chain->dev->intfnum,
1675 ctrl->info.selector,
1676 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1685 /* Backup the current value in case we need to rollback later. */
1687 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1688 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1692 mapping->set(mapping, value,
1693 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1695 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1696 ctrl->handle = handle;
1703 /* --------------------------------------------------------------------------
1708 * Retrieve flags for a given control
1710 static int uvc_ctrl_get_flags(struct uvc_device *dev,
1711 const struct uvc_control *ctrl,
1712 struct uvc_control_info *info)
1717 data = kmalloc(1, GFP_KERNEL);
1721 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1722 info->selector, data, 1);
1724 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
1725 UVC_CTRL_FLAG_GET_CUR : 0)
1726 | (data[0] & UVC_CONTROL_CAP_SET ?
1727 UVC_CTRL_FLAG_SET_CUR : 0)
1728 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1729 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
1730 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
1731 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
1737 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1738 const struct uvc_control *ctrl, struct uvc_control_info *info)
1740 struct uvc_ctrl_fixup {
1741 struct usb_device_id id;
1747 static const struct uvc_ctrl_fixup fixups[] = {
1748 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1749 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1750 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1751 UVC_CTRL_FLAG_AUTO_UPDATE },
1752 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1753 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1754 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1755 UVC_CTRL_FLAG_AUTO_UPDATE },
1756 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1757 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1758 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1759 UVC_CTRL_FLAG_AUTO_UPDATE },
1764 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1765 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1768 if (fixups[i].entity == ctrl->entity->id &&
1769 fixups[i].selector == info->selector) {
1770 info->flags = fixups[i].flags;
1777 * Query control information (size and flags) for XU controls.
1779 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1780 const struct uvc_control *ctrl, struct uvc_control_info *info)
1785 data = kmalloc(2, GFP_KERNEL);
1789 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1790 sizeof(info->entity));
1791 info->index = ctrl->index;
1792 info->selector = ctrl->index + 1;
1794 /* Query and verify the control length (GET_LEN) */
1795 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1796 info->selector, data, 2);
1798 uvc_trace(UVC_TRACE_CONTROL,
1799 "GET_LEN failed on control %pUl/%u (%d).\n",
1800 info->entity, info->selector, ret);
1804 info->size = le16_to_cpup((__le16 *)data);
1806 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1807 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
1809 ret = uvc_ctrl_get_flags(dev, ctrl, info);
1811 uvc_trace(UVC_TRACE_CONTROL,
1812 "Failed to get flags for control %pUl/%u (%d).\n",
1813 info->entity, info->selector, ret);
1817 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1819 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1820 "flags { get %u set %u auto %u }.\n",
1821 info->entity, info->selector, info->size,
1822 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1823 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1824 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1831 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1832 const struct uvc_control_info *info);
1834 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1835 struct uvc_control *ctrl)
1837 struct uvc_control_info info;
1840 if (ctrl->initialized)
1843 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1847 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1849 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1850 "%pUl/%u on device %s entity %u\n", info.entity,
1851 info.selector, dev->udev->devpath, ctrl->entity->id);
1856 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1857 struct uvc_xu_control_query *xqry)
1859 struct uvc_entity *entity;
1860 struct uvc_control *ctrl;
1868 /* Find the extension unit. */
1870 list_for_each_entry(entity, &chain->entities, chain) {
1871 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1872 entity->id == xqry->unit) {
1879 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1884 /* Find the control and perform delayed initialization if needed. */
1886 for (i = 0; i < entity->ncontrols; ++i) {
1887 ctrl = &entity->controls[i];
1888 if (ctrl->index == xqry->selector - 1) {
1895 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1896 entity->extension.guidExtensionCode, xqry->selector);
1900 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1901 return -ERESTARTSYS;
1903 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1909 /* Validate the required buffer size and flags for the request */
1911 size = ctrl->info.size;
1913 switch (xqry->query) {
1915 reqflags = UVC_CTRL_FLAG_GET_CUR;
1918 reqflags = UVC_CTRL_FLAG_GET_MIN;
1921 reqflags = UVC_CTRL_FLAG_GET_MAX;
1924 reqflags = UVC_CTRL_FLAG_GET_DEF;
1927 reqflags = UVC_CTRL_FLAG_GET_RES;
1930 reqflags = UVC_CTRL_FLAG_SET_CUR;
1943 if (size != xqry->size) {
1948 if (reqflags && !(ctrl->info.flags & reqflags)) {
1953 data = kmalloc(size, GFP_KERNEL);
1959 if (xqry->query == UVC_SET_CUR &&
1960 copy_from_user(data, xqry->data, size)) {
1965 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1966 chain->dev->intfnum, xqry->selector, data, size);
1970 if (xqry->query != UVC_SET_CUR &&
1971 copy_to_user(xqry->data, data, size))
1975 mutex_unlock(&chain->ctrl_mutex);
1979 /* --------------------------------------------------------------------------
1984 * Restore control values after resume, skipping controls that haven't been
1988 * - Don't restore modified controls that are back to their default value.
1989 * - Handle restore order (Auto-Exposure Mode should be restored before
1992 int uvc_ctrl_restore_values(struct uvc_device *dev)
1994 struct uvc_control *ctrl;
1995 struct uvc_entity *entity;
1999 /* Walk the entities list and restore controls when possible. */
2000 list_for_each_entry(entity, &dev->entities, list) {
2002 for (i = 0; i < entity->ncontrols; ++i) {
2003 ctrl = &entity->controls[i];
2005 if (!ctrl->initialized || !ctrl->modified ||
2006 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2009 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
2010 ctrl->info.entity, ctrl->info.index,
2011 ctrl->info.selector);
2015 ret = uvc_ctrl_commit_entity(dev, entity, 0);
2023 /* --------------------------------------------------------------------------
2024 * Control and mapping handling
2028 * Add control information to a given control.
2030 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2031 const struct uvc_control_info *info)
2034 INIT_LIST_HEAD(&ctrl->info.mappings);
2036 /* Allocate an array to save control values (cur, def, max, etc.) */
2037 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2039 if (!ctrl->uvc_data)
2042 ctrl->initialized = 1;
2044 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
2045 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
2046 dev->udev->devpath, ctrl->entity->id);
2052 * Add a control mapping to a given control.
2054 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
2055 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2057 struct uvc_control_mapping *map;
2060 /* Most mappings come from static kernel data and need to be duplicated.
2061 * Mappings that come from userspace will be unnecessarily duplicated,
2062 * this could be optimized.
2064 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2068 INIT_LIST_HEAD(&map->ev_subs);
2070 size = sizeof(*mapping->menu_info) * mapping->menu_count;
2071 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
2072 if (map->menu_info == NULL) {
2077 if (map->get == NULL)
2078 map->get = uvc_get_le_value;
2079 if (map->set == NULL)
2080 map->set = uvc_set_le_value;
2082 list_add_tail(&map->list, &ctrl->info.mappings);
2083 uvc_trace(UVC_TRACE_CONTROL,
2084 "Adding mapping '%s' to control %pUl/%u.\n",
2085 map->name, ctrl->info.entity, ctrl->info.selector);
2090 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2091 const struct uvc_control_mapping *mapping)
2093 struct uvc_device *dev = chain->dev;
2094 struct uvc_control_mapping *map;
2095 struct uvc_entity *entity;
2096 struct uvc_control *ctrl;
2100 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2101 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
2102 "id 0x%08x is invalid.\n", mapping->name,
2107 /* Search for the matching (GUID/CS) control on the current chain */
2108 list_for_each_entry(entity, &chain->entities, chain) {
2111 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2112 !uvc_entity_match_guid(entity, mapping->entity))
2115 for (i = 0; i < entity->ncontrols; ++i) {
2116 ctrl = &entity->controls[i];
2117 if (ctrl->index == mapping->selector - 1) {
2129 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2130 return -ERESTARTSYS;
2132 /* Perform delayed initialization of XU controls */
2133 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2139 /* Validate the user-provided bit-size and offset */
2140 if (mapping->size > 32 ||
2141 mapping->offset + mapping->size > ctrl->info.size * 8) {
2146 list_for_each_entry(map, &ctrl->info.mappings, list) {
2147 if (mapping->id == map->id) {
2148 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
2149 "control id 0x%08x already exists.\n",
2150 mapping->name, mapping->id);
2156 /* Prevent excess memory consumption */
2157 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2158 atomic_dec(&dev->nmappings);
2159 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
2160 "mappings count (%u) exceeded.\n", mapping->name,
2161 UVC_MAX_CONTROL_MAPPINGS);
2166 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2168 atomic_dec(&dev->nmappings);
2171 mutex_unlock(&chain->ctrl_mutex);
2176 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2177 * are currently the ones that crash the camera or unconditionally return an
2178 * error when queried.
2180 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2181 struct uvc_entity *entity)
2183 struct uvc_ctrl_blacklist {
2184 struct usb_device_id id;
2188 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2189 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2190 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2191 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2193 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2194 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2197 const struct uvc_ctrl_blacklist *blacklist;
2203 switch (UVC_ENTITY_TYPE(entity)) {
2204 case UVC_VC_PROCESSING_UNIT:
2205 blacklist = processing_blacklist;
2206 count = ARRAY_SIZE(processing_blacklist);
2207 controls = entity->processing.bmControls;
2208 size = entity->processing.bControlSize;
2211 case UVC_ITT_CAMERA:
2212 blacklist = camera_blacklist;
2213 count = ARRAY_SIZE(camera_blacklist);
2214 controls = entity->camera.bmControls;
2215 size = entity->camera.bControlSize;
2222 for (i = 0; i < count; ++i) {
2223 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2226 if (blacklist[i].index >= 8 * size ||
2227 !uvc_test_bit(controls, blacklist[i].index))
2230 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
2231 "removing it.\n", entity->id, blacklist[i].index);
2233 uvc_clear_bit(controls, blacklist[i].index);
2238 * Add control information and hardcoded stock control mappings to the given
2241 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2243 const struct uvc_control_info *info = uvc_ctrls;
2244 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2245 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2246 const struct uvc_control_mapping *mend =
2247 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2249 /* XU controls initialization requires querying the device for control
2250 * information. As some buggy UVC devices will crash when queried
2251 * repeatedly in a tight loop, delay XU controls initialization until
2254 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2257 for (; info < iend; ++info) {
2258 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2259 ctrl->index == info->index) {
2260 uvc_ctrl_add_info(dev, ctrl, info);
2262 * Retrieve control flags from the device. Ignore errors
2263 * and work with default flag values from the uvc_ctrl
2264 * array when the device doesn't properly implement
2265 * GET_INFO on standard controls.
2267 uvc_ctrl_get_flags(dev, ctrl, &ctrl->info);
2272 if (!ctrl->initialized)
2275 for (; mapping < mend; ++mapping) {
2276 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2277 ctrl->info.selector == mapping->selector)
2278 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2283 * Initialize device controls.
2285 int uvc_ctrl_init_device(struct uvc_device *dev)
2287 struct uvc_entity *entity;
2290 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2292 /* Walk the entities list and instantiate controls */
2293 list_for_each_entry(entity, &dev->entities, list) {
2294 struct uvc_control *ctrl;
2295 unsigned int bControlSize = 0, ncontrols;
2296 u8 *bmControls = NULL;
2298 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2299 bmControls = entity->extension.bmControls;
2300 bControlSize = entity->extension.bControlSize;
2301 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2302 bmControls = entity->processing.bmControls;
2303 bControlSize = entity->processing.bControlSize;
2304 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2305 bmControls = entity->camera.bmControls;
2306 bControlSize = entity->camera.bControlSize;
2309 /* Remove bogus/blacklisted controls */
2310 uvc_ctrl_prune_entity(dev, entity);
2312 /* Count supported controls and allocate the controls array */
2313 ncontrols = memweight(bmControls, bControlSize);
2317 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2319 if (entity->controls == NULL)
2321 entity->ncontrols = ncontrols;
2323 /* Initialize all supported controls */
2324 ctrl = entity->controls;
2325 for (i = 0; i < bControlSize * 8; ++i) {
2326 if (uvc_test_bit(bmControls, i) == 0)
2329 ctrl->entity = entity;
2332 uvc_ctrl_init_ctrl(dev, ctrl);
2341 * Cleanup device controls.
2343 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2344 struct uvc_control *ctrl)
2346 struct uvc_control_mapping *mapping, *nm;
2348 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2349 list_del(&mapping->list);
2350 kfree(mapping->menu_info);
2355 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2357 struct uvc_entity *entity;
2360 /* Can be uninitialized if we are aborting on probe error. */
2361 if (dev->async_ctrl.work.func)
2362 cancel_work_sync(&dev->async_ctrl.work);
2364 /* Free controls and control mappings for all entities. */
2365 list_for_each_entry(entity, &dev->entities, list) {
2366 for (i = 0; i < entity->ncontrols; ++i) {
2367 struct uvc_control *ctrl = &entity->controls[i];
2369 if (!ctrl->initialized)
2372 uvc_ctrl_cleanup_mappings(dev, ctrl);
2373 kfree(ctrl->uvc_data);
2376 kfree(entity->controls);