2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <linux/workqueue.h>
24 #include <linux/atomic.h>
25 #include <media/v4l2-ctrls.h>
29 #define UVC_CTRL_DATA_CURRENT 0
30 #define UVC_CTRL_DATA_BACKUP 1
31 #define UVC_CTRL_DATA_MIN 2
32 #define UVC_CTRL_DATA_MAX 3
33 #define UVC_CTRL_DATA_RES 4
34 #define UVC_CTRL_DATA_DEF 5
35 #define UVC_CTRL_DATA_LAST 6
37 /* ------------------------------------------------------------------------
41 static struct uvc_control_info uvc_ctrls[] = {
43 .entity = UVC_GUID_UVC_PROCESSING,
44 .selector = UVC_PU_BRIGHTNESS_CONTROL,
47 .flags = UVC_CTRL_FLAG_SET_CUR
48 | UVC_CTRL_FLAG_GET_RANGE
49 | UVC_CTRL_FLAG_RESTORE,
52 .entity = UVC_GUID_UVC_PROCESSING,
53 .selector = UVC_PU_CONTRAST_CONTROL,
56 .flags = UVC_CTRL_FLAG_SET_CUR
57 | UVC_CTRL_FLAG_GET_RANGE
58 | UVC_CTRL_FLAG_RESTORE,
61 .entity = UVC_GUID_UVC_PROCESSING,
62 .selector = UVC_PU_HUE_CONTROL,
65 .flags = UVC_CTRL_FLAG_SET_CUR
66 | UVC_CTRL_FLAG_GET_RANGE
67 | UVC_CTRL_FLAG_RESTORE
68 | UVC_CTRL_FLAG_AUTO_UPDATE,
71 .entity = UVC_GUID_UVC_PROCESSING,
72 .selector = UVC_PU_SATURATION_CONTROL,
75 .flags = UVC_CTRL_FLAG_SET_CUR
76 | UVC_CTRL_FLAG_GET_RANGE
77 | UVC_CTRL_FLAG_RESTORE,
80 .entity = UVC_GUID_UVC_PROCESSING,
81 .selector = UVC_PU_SHARPNESS_CONTROL,
84 .flags = UVC_CTRL_FLAG_SET_CUR
85 | UVC_CTRL_FLAG_GET_RANGE
86 | UVC_CTRL_FLAG_RESTORE,
89 .entity = UVC_GUID_UVC_PROCESSING,
90 .selector = UVC_PU_GAMMA_CONTROL,
93 .flags = UVC_CTRL_FLAG_SET_CUR
94 | UVC_CTRL_FLAG_GET_RANGE
95 | UVC_CTRL_FLAG_RESTORE,
98 .entity = UVC_GUID_UVC_PROCESSING,
99 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
102 .flags = UVC_CTRL_FLAG_SET_CUR
103 | UVC_CTRL_FLAG_GET_RANGE
104 | UVC_CTRL_FLAG_RESTORE
105 | UVC_CTRL_FLAG_AUTO_UPDATE,
108 .entity = UVC_GUID_UVC_PROCESSING,
109 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
112 .flags = UVC_CTRL_FLAG_SET_CUR
113 | UVC_CTRL_FLAG_GET_RANGE
114 | UVC_CTRL_FLAG_RESTORE
115 | UVC_CTRL_FLAG_AUTO_UPDATE,
118 .entity = UVC_GUID_UVC_PROCESSING,
119 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
122 .flags = UVC_CTRL_FLAG_SET_CUR
123 | UVC_CTRL_FLAG_GET_RANGE
124 | UVC_CTRL_FLAG_RESTORE,
127 .entity = UVC_GUID_UVC_PROCESSING,
128 .selector = UVC_PU_GAIN_CONTROL,
131 .flags = UVC_CTRL_FLAG_SET_CUR
132 | UVC_CTRL_FLAG_GET_RANGE
133 | UVC_CTRL_FLAG_RESTORE,
136 .entity = UVC_GUID_UVC_PROCESSING,
137 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
140 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
141 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
144 .entity = UVC_GUID_UVC_PROCESSING,
145 .selector = UVC_PU_HUE_AUTO_CONTROL,
148 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
149 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
152 .entity = UVC_GUID_UVC_PROCESSING,
153 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
156 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
157 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
160 .entity = UVC_GUID_UVC_PROCESSING,
161 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
164 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
165 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
168 .entity = UVC_GUID_UVC_PROCESSING,
169 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
172 .flags = UVC_CTRL_FLAG_SET_CUR
173 | UVC_CTRL_FLAG_GET_RANGE
174 | UVC_CTRL_FLAG_RESTORE,
177 .entity = UVC_GUID_UVC_PROCESSING,
178 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
181 .flags = UVC_CTRL_FLAG_SET_CUR
182 | UVC_CTRL_FLAG_GET_RANGE
183 | UVC_CTRL_FLAG_RESTORE,
186 .entity = UVC_GUID_UVC_PROCESSING,
187 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
190 .flags = UVC_CTRL_FLAG_GET_CUR,
193 .entity = UVC_GUID_UVC_PROCESSING,
194 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
197 .flags = UVC_CTRL_FLAG_GET_CUR,
200 .entity = UVC_GUID_UVC_CAMERA,
201 .selector = UVC_CT_SCANNING_MODE_CONTROL,
204 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
205 | UVC_CTRL_FLAG_RESTORE,
208 .entity = UVC_GUID_UVC_CAMERA,
209 .selector = UVC_CT_AE_MODE_CONTROL,
212 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
213 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
214 | UVC_CTRL_FLAG_RESTORE,
217 .entity = UVC_GUID_UVC_CAMERA,
218 .selector = UVC_CT_AE_PRIORITY_CONTROL,
221 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
222 | UVC_CTRL_FLAG_RESTORE,
225 .entity = UVC_GUID_UVC_CAMERA,
226 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
229 .flags = UVC_CTRL_FLAG_SET_CUR
230 | UVC_CTRL_FLAG_GET_RANGE
231 | UVC_CTRL_FLAG_RESTORE
232 | UVC_CTRL_FLAG_AUTO_UPDATE,
235 .entity = UVC_GUID_UVC_CAMERA,
236 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
239 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
242 .entity = UVC_GUID_UVC_CAMERA,
243 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
246 .flags = UVC_CTRL_FLAG_SET_CUR
247 | UVC_CTRL_FLAG_GET_RANGE
248 | UVC_CTRL_FLAG_RESTORE
249 | UVC_CTRL_FLAG_AUTO_UPDATE,
252 .entity = UVC_GUID_UVC_CAMERA,
253 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
256 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
257 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
258 | UVC_CTRL_FLAG_GET_DEF
259 | UVC_CTRL_FLAG_AUTO_UPDATE,
262 .entity = UVC_GUID_UVC_CAMERA,
263 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
266 .flags = UVC_CTRL_FLAG_SET_CUR
267 | UVC_CTRL_FLAG_GET_RANGE
268 | UVC_CTRL_FLAG_RESTORE
269 | UVC_CTRL_FLAG_AUTO_UPDATE,
272 .entity = UVC_GUID_UVC_CAMERA,
273 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
276 .flags = UVC_CTRL_FLAG_SET_CUR
277 | UVC_CTRL_FLAG_AUTO_UPDATE,
280 .entity = UVC_GUID_UVC_CAMERA,
281 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
284 .flags = UVC_CTRL_FLAG_SET_CUR
285 | UVC_CTRL_FLAG_GET_RANGE
286 | UVC_CTRL_FLAG_RESTORE
287 | UVC_CTRL_FLAG_AUTO_UPDATE,
290 .entity = UVC_GUID_UVC_CAMERA,
291 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
294 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
295 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
296 | UVC_CTRL_FLAG_GET_DEF
297 | UVC_CTRL_FLAG_AUTO_UPDATE,
300 .entity = UVC_GUID_UVC_CAMERA,
301 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
304 .flags = UVC_CTRL_FLAG_SET_CUR
305 | UVC_CTRL_FLAG_GET_RANGE
306 | UVC_CTRL_FLAG_RESTORE
307 | UVC_CTRL_FLAG_AUTO_UPDATE,
310 .entity = UVC_GUID_UVC_CAMERA,
311 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
314 .flags = UVC_CTRL_FLAG_SET_CUR
315 | UVC_CTRL_FLAG_GET_RANGE
316 | UVC_CTRL_FLAG_AUTO_UPDATE,
319 .entity = UVC_GUID_UVC_CAMERA,
320 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
323 .flags = UVC_CTRL_FLAG_SET_CUR
324 | UVC_CTRL_FLAG_GET_RANGE
325 | UVC_CTRL_FLAG_RESTORE
326 | UVC_CTRL_FLAG_AUTO_UPDATE,
329 .entity = UVC_GUID_UVC_CAMERA,
330 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
333 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
334 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
335 | UVC_CTRL_FLAG_GET_DEF
336 | UVC_CTRL_FLAG_AUTO_UPDATE,
339 .entity = UVC_GUID_UVC_CAMERA,
340 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
343 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
344 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
347 .entity = UVC_GUID_UVC_CAMERA,
348 .selector = UVC_CT_PRIVACY_CONTROL,
351 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
352 | UVC_CTRL_FLAG_RESTORE
353 | UVC_CTRL_FLAG_AUTO_UPDATE,
357 static struct uvc_menu_info power_line_frequency_controls[] = {
363 static struct uvc_menu_info exposure_auto_controls[] = {
365 { 1, "Manual Mode" },
366 { 4, "Shutter Priority Mode" },
367 { 8, "Aperture Priority Mode" },
370 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
371 u8 query, const u8 *data)
373 s8 zoom = (s8)data[0];
377 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
388 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
391 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
392 data[2] = min((int)abs(value), 0xff);
395 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
396 u8 query, const u8 *data)
398 unsigned int first = mapping->offset / 8;
399 s8 rel = (s8)data[first];
403 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
406 return -data[first+1];
411 return data[first+1];
415 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
418 unsigned int first = mapping->offset / 8;
420 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
421 data[first+1] = min_t(int, abs(value), 0xff);
424 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
426 .id = V4L2_CID_BRIGHTNESS,
427 .name = "Brightness",
428 .entity = UVC_GUID_UVC_PROCESSING,
429 .selector = UVC_PU_BRIGHTNESS_CONTROL,
432 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
433 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
436 .id = V4L2_CID_CONTRAST,
438 .entity = UVC_GUID_UVC_PROCESSING,
439 .selector = UVC_PU_CONTRAST_CONTROL,
442 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
443 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
448 .entity = UVC_GUID_UVC_PROCESSING,
449 .selector = UVC_PU_HUE_CONTROL,
452 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
453 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
454 .master_id = V4L2_CID_HUE_AUTO,
458 .id = V4L2_CID_SATURATION,
459 .name = "Saturation",
460 .entity = UVC_GUID_UVC_PROCESSING,
461 .selector = UVC_PU_SATURATION_CONTROL,
464 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
465 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
468 .id = V4L2_CID_SHARPNESS,
470 .entity = UVC_GUID_UVC_PROCESSING,
471 .selector = UVC_PU_SHARPNESS_CONTROL,
474 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
475 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
478 .id = V4L2_CID_GAMMA,
480 .entity = UVC_GUID_UVC_PROCESSING,
481 .selector = UVC_PU_GAMMA_CONTROL,
484 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
485 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
488 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
489 .name = "Backlight Compensation",
490 .entity = UVC_GUID_UVC_PROCESSING,
491 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
494 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
495 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
500 .entity = UVC_GUID_UVC_PROCESSING,
501 .selector = UVC_PU_GAIN_CONTROL,
504 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
505 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
508 .id = V4L2_CID_POWER_LINE_FREQUENCY,
509 .name = "Power Line Frequency",
510 .entity = UVC_GUID_UVC_PROCESSING,
511 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
514 .v4l2_type = V4L2_CTRL_TYPE_MENU,
515 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
516 .menu_info = power_line_frequency_controls,
517 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
520 .id = V4L2_CID_HUE_AUTO,
522 .entity = UVC_GUID_UVC_PROCESSING,
523 .selector = UVC_PU_HUE_AUTO_CONTROL,
526 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
527 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
528 .slave_ids = { V4L2_CID_HUE, },
531 .id = V4L2_CID_EXPOSURE_AUTO,
532 .name = "Exposure, Auto",
533 .entity = UVC_GUID_UVC_CAMERA,
534 .selector = UVC_CT_AE_MODE_CONTROL,
537 .v4l2_type = V4L2_CTRL_TYPE_MENU,
538 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
539 .menu_info = exposure_auto_controls,
540 .menu_count = ARRAY_SIZE(exposure_auto_controls),
541 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
544 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
545 .name = "Exposure, Auto Priority",
546 .entity = UVC_GUID_UVC_CAMERA,
547 .selector = UVC_CT_AE_PRIORITY_CONTROL,
550 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
551 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
554 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
555 .name = "Exposure (Absolute)",
556 .entity = UVC_GUID_UVC_CAMERA,
557 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
560 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
561 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
562 .master_id = V4L2_CID_EXPOSURE_AUTO,
563 .master_manual = V4L2_EXPOSURE_MANUAL,
566 .id = V4L2_CID_AUTO_WHITE_BALANCE,
567 .name = "White Balance Temperature, Auto",
568 .entity = UVC_GUID_UVC_PROCESSING,
569 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
572 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
573 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
574 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
577 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
578 .name = "White Balance Temperature",
579 .entity = UVC_GUID_UVC_PROCESSING,
580 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
583 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
584 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
585 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
589 .id = V4L2_CID_AUTO_WHITE_BALANCE,
590 .name = "White Balance Component, Auto",
591 .entity = UVC_GUID_UVC_PROCESSING,
592 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
595 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
596 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
597 .slave_ids = { V4L2_CID_BLUE_BALANCE,
598 V4L2_CID_RED_BALANCE },
601 .id = V4L2_CID_BLUE_BALANCE,
602 .name = "White Balance Blue Component",
603 .entity = UVC_GUID_UVC_PROCESSING,
604 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
607 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
608 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
609 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
613 .id = V4L2_CID_RED_BALANCE,
614 .name = "White Balance Red Component",
615 .entity = UVC_GUID_UVC_PROCESSING,
616 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
619 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
620 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
621 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
625 .id = V4L2_CID_FOCUS_ABSOLUTE,
626 .name = "Focus (absolute)",
627 .entity = UVC_GUID_UVC_CAMERA,
628 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
631 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
632 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
633 .master_id = V4L2_CID_FOCUS_AUTO,
637 .id = V4L2_CID_FOCUS_AUTO,
638 .name = "Focus, Auto",
639 .entity = UVC_GUID_UVC_CAMERA,
640 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
643 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
644 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
645 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
648 .id = V4L2_CID_IRIS_ABSOLUTE,
649 .name = "Iris, Absolute",
650 .entity = UVC_GUID_UVC_CAMERA,
651 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
654 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
655 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
658 .id = V4L2_CID_IRIS_RELATIVE,
659 .name = "Iris, Relative",
660 .entity = UVC_GUID_UVC_CAMERA,
661 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
664 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
665 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
668 .id = V4L2_CID_ZOOM_ABSOLUTE,
669 .name = "Zoom, Absolute",
670 .entity = UVC_GUID_UVC_CAMERA,
671 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
674 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
675 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
678 .id = V4L2_CID_ZOOM_CONTINUOUS,
679 .name = "Zoom, Continuous",
680 .entity = UVC_GUID_UVC_CAMERA,
681 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
684 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
685 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
686 .get = uvc_ctrl_get_zoom,
687 .set = uvc_ctrl_set_zoom,
690 .id = V4L2_CID_PAN_ABSOLUTE,
691 .name = "Pan (Absolute)",
692 .entity = UVC_GUID_UVC_CAMERA,
693 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
696 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
697 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
700 .id = V4L2_CID_TILT_ABSOLUTE,
701 .name = "Tilt (Absolute)",
702 .entity = UVC_GUID_UVC_CAMERA,
703 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
706 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
707 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
710 .id = V4L2_CID_PAN_SPEED,
711 .name = "Pan (Speed)",
712 .entity = UVC_GUID_UVC_CAMERA,
713 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
716 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
717 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
718 .get = uvc_ctrl_get_rel_speed,
719 .set = uvc_ctrl_set_rel_speed,
722 .id = V4L2_CID_TILT_SPEED,
723 .name = "Tilt (Speed)",
724 .entity = UVC_GUID_UVC_CAMERA,
725 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
728 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
729 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
730 .get = uvc_ctrl_get_rel_speed,
731 .set = uvc_ctrl_set_rel_speed,
734 .id = V4L2_CID_PRIVACY,
736 .entity = UVC_GUID_UVC_CAMERA,
737 .selector = UVC_CT_PRIVACY_CONTROL,
740 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
741 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
745 /* ------------------------------------------------------------------------
749 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
751 return ctrl->uvc_data + id * ctrl->info.size;
754 static inline int uvc_test_bit(const u8 *data, int bit)
756 return (data[bit >> 3] >> (bit & 7)) & 1;
759 static inline void uvc_clear_bit(u8 *data, int bit)
761 data[bit >> 3] &= ~(1 << (bit & 7));
764 /* Extract the bit string specified by mapping->offset and mapping->size
765 * from the little-endian data stored at 'data' and return the result as
766 * a signed 32bit integer. Sign extension will be performed if the mapping
767 * references a signed data type.
769 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
770 u8 query, const u8 *data)
772 int bits = mapping->size;
773 int offset = mapping->offset;
779 mask = ((1LL << bits) - 1) << offset;
782 u8 byte = *data & mask;
783 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
784 bits -= 8 - (offset > 0 ? offset : 0);
789 mask = (1 << bits) - 1;
793 /* Sign-extend the value if needed. */
794 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
795 value |= -(value & (1 << (mapping->size - 1)));
800 /* Set the bit string specified by mapping->offset and mapping->size
801 * in the little-endian data stored at 'data' to the value 'value'.
803 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
806 int bits = mapping->size;
807 int offset = mapping->offset;
810 /* According to the v4l2 spec, writing any value to a button control
811 * should result in the action belonging to the button control being
812 * triggered. UVC devices however want to see a 1 written -> override
815 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
821 for (; bits > 0; data++) {
822 mask = ((1LL << bits) - 1) << offset;
823 *data = (*data & ~mask) | ((value << offset) & mask);
824 value >>= offset ? offset : 8;
830 /* ------------------------------------------------------------------------
831 * Terminal and unit management
834 static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
835 static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
836 static const u8 uvc_media_transport_input_guid[16] =
837 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
839 static int uvc_entity_match_guid(const struct uvc_entity *entity,
842 switch (UVC_ENTITY_TYPE(entity)) {
844 return memcmp(uvc_camera_guid, guid, 16) == 0;
846 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
847 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
849 case UVC_VC_PROCESSING_UNIT:
850 return memcmp(uvc_processing_guid, guid, 16) == 0;
852 case UVC_VC_EXTENSION_UNIT:
853 return memcmp(entity->extension.guidExtensionCode,
861 /* ------------------------------------------------------------------------
865 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
866 struct uvc_control_mapping **mapping, struct uvc_control **control,
869 struct uvc_control *ctrl;
870 struct uvc_control_mapping *map;
876 for (i = 0; i < entity->ncontrols; ++i) {
877 ctrl = &entity->controls[i];
878 if (!ctrl->initialized)
881 list_for_each_entry(map, &ctrl->info.mappings, list) {
882 if ((map->id == v4l2_id) && !next) {
888 if ((*mapping == NULL || (*mapping)->id > map->id) &&
889 (map->id > v4l2_id) && next) {
897 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
898 u32 v4l2_id, struct uvc_control_mapping **mapping)
900 struct uvc_control *ctrl = NULL;
901 struct uvc_entity *entity;
902 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
906 /* Mask the query flags. */
907 v4l2_id &= V4L2_CTRL_ID_MASK;
909 /* Find the control. */
910 list_for_each_entry(entity, &chain->entities, chain) {
911 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
916 if (ctrl == NULL && !next)
917 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
923 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
924 struct uvc_control *ctrl)
928 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
929 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
930 chain->dev->intfnum, ctrl->info.selector,
931 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
937 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
938 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
939 chain->dev->intfnum, ctrl->info.selector,
940 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
945 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
946 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
947 chain->dev->intfnum, ctrl->info.selector,
948 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
953 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
954 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
955 chain->dev->intfnum, ctrl->info.selector,
956 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
959 if (UVC_ENTITY_TYPE(ctrl->entity) !=
960 UVC_VC_EXTENSION_UNIT)
963 /* GET_RES is mandatory for XU controls, but some
964 * cameras still choke on it. Ignore errors and set the
965 * resolution value to zero.
967 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
968 "UVC non compliance - GET_RES failed on "
969 "an XU control. Enabling workaround.\n");
970 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
979 static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
982 s32 value = mapping->get(mapping, UVC_GET_CUR, data);
984 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
985 struct uvc_menu_info *menu = mapping->menu_info;
988 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
989 if (menu->value == value) {
999 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1000 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
1005 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1008 if (!ctrl->loaded) {
1009 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1010 chain->dev->intfnum, ctrl->info.selector,
1011 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1019 *value = __uvc_ctrl_get_value(mapping,
1020 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1025 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1026 struct uvc_control *ctrl,
1027 struct uvc_control_mapping *mapping,
1028 struct v4l2_queryctrl *v4l2_ctrl)
1030 struct uvc_control_mapping *master_map = NULL;
1031 struct uvc_control *master_ctrl = NULL;
1032 struct uvc_menu_info *menu;
1035 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1036 v4l2_ctrl->id = mapping->id;
1037 v4l2_ctrl->type = mapping->v4l2_type;
1038 strlcpy(v4l2_ctrl->name, mapping->name, sizeof(v4l2_ctrl->name));
1039 v4l2_ctrl->flags = 0;
1041 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1042 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1043 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1044 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1046 if (mapping->master_id)
1047 __uvc_find_control(ctrl->entity, mapping->master_id,
1048 &master_map, &master_ctrl, 0);
1049 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1051 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1055 if (val != mapping->master_manual)
1056 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1059 if (!ctrl->cached) {
1060 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1065 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1066 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1067 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1070 switch (mapping->v4l2_type) {
1071 case V4L2_CTRL_TYPE_MENU:
1072 v4l2_ctrl->minimum = 0;
1073 v4l2_ctrl->maximum = mapping->menu_count - 1;
1074 v4l2_ctrl->step = 1;
1076 menu = mapping->menu_info;
1077 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1078 if (menu->value == v4l2_ctrl->default_value) {
1079 v4l2_ctrl->default_value = i;
1086 case V4L2_CTRL_TYPE_BOOLEAN:
1087 v4l2_ctrl->minimum = 0;
1088 v4l2_ctrl->maximum = 1;
1089 v4l2_ctrl->step = 1;
1092 case V4L2_CTRL_TYPE_BUTTON:
1093 v4l2_ctrl->minimum = 0;
1094 v4l2_ctrl->maximum = 0;
1095 v4l2_ctrl->step = 0;
1102 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1103 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1104 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1106 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1107 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1108 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1110 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1111 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1112 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1117 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1118 struct v4l2_queryctrl *v4l2_ctrl)
1120 struct uvc_control *ctrl;
1121 struct uvc_control_mapping *mapping;
1124 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1126 return -ERESTARTSYS;
1128 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1134 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1136 mutex_unlock(&chain->ctrl_mutex);
1141 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1142 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1143 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1144 * Balance V4L2 controls use the White Balance Component UVC control) or
1145 * otherwise translated. The approach we take here is to use a translation
1146 * table for the controls that can be mapped directly, and handle the others
1149 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1150 struct v4l2_querymenu *query_menu)
1152 struct uvc_menu_info *menu_info;
1153 struct uvc_control_mapping *mapping;
1154 struct uvc_control *ctrl;
1155 u32 index = query_menu->index;
1156 u32 id = query_menu->id;
1159 memset(query_menu, 0, sizeof(*query_menu));
1160 query_menu->id = id;
1161 query_menu->index = index;
1163 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1165 return -ERESTARTSYS;
1167 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1168 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1173 if (query_menu->index >= mapping->menu_count) {
1178 menu_info = &mapping->menu_info[query_menu->index];
1180 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1181 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1184 if (!ctrl->cached) {
1185 ret = uvc_ctrl_populate_cache(chain, ctrl);
1190 bitmap = mapping->get(mapping, UVC_GET_RES,
1191 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1192 if (!(bitmap & menu_info->value)) {
1198 strlcpy(query_menu->name, menu_info->name, sizeof(query_menu->name));
1201 mutex_unlock(&chain->ctrl_mutex);
1205 /* --------------------------------------------------------------------------
1206 * Ctrl event handling
1209 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1210 struct v4l2_event *ev,
1211 struct uvc_control *ctrl,
1212 struct uvc_control_mapping *mapping,
1213 s32 value, u32 changes)
1215 struct v4l2_queryctrl v4l2_ctrl;
1217 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1219 memset(ev, 0, sizeof(*ev));
1220 ev->type = V4L2_EVENT_CTRL;
1221 ev->id = v4l2_ctrl.id;
1222 ev->u.ctrl.value = value;
1223 ev->u.ctrl.changes = changes;
1224 ev->u.ctrl.type = v4l2_ctrl.type;
1225 ev->u.ctrl.flags = v4l2_ctrl.flags;
1226 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1227 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1228 ev->u.ctrl.step = v4l2_ctrl.step;
1229 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1233 * Send control change events to all subscribers for the @ctrl control. By
1234 * default the subscriber that generated the event, as identified by @handle,
1235 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1236 * @handle can be NULL for asynchronous events related to auto-update controls,
1237 * in which case all subscribers are notified.
1239 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1240 struct uvc_fh *handle, struct uvc_control *ctrl,
1241 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1243 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1244 struct v4l2_subscribed_event *sev;
1245 struct v4l2_event ev;
1247 if (list_empty(&mapping->ev_subs))
1250 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1252 list_for_each_entry(sev, &mapping->ev_subs, node) {
1253 if (sev->fh != originator ||
1254 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1255 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1256 v4l2_event_queue_fh(sev->fh, &ev);
1261 * Send control change events for the slave of the @master control identified
1262 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1263 * generated the event and may be NULL for auto-update events.
1265 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1266 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1268 struct uvc_control_mapping *mapping = NULL;
1269 struct uvc_control *ctrl = NULL;
1270 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1273 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1277 if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1278 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1280 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1283 static void uvc_ctrl_status_event_work(struct work_struct *work)
1285 struct uvc_device *dev = container_of(work, struct uvc_device,
1287 struct uvc_ctrl_work *w = &dev->async_ctrl;
1288 struct uvc_video_chain *chain = w->chain;
1289 struct uvc_control_mapping *mapping;
1290 struct uvc_control *ctrl = w->ctrl;
1291 struct uvc_fh *handle;
1295 mutex_lock(&chain->ctrl_mutex);
1297 handle = ctrl->handle;
1298 ctrl->handle = NULL;
1300 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1301 s32 value = __uvc_ctrl_get_value(mapping, w->data);
1304 * handle may be NULL here if the device sends auto-update
1305 * events without a prior related control set from userspace.
1307 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1308 if (!mapping->slave_ids[i])
1311 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1312 mapping->slave_ids[i]);
1315 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1316 V4L2_EVENT_CTRL_CH_VALUE);
1319 mutex_unlock(&chain->ctrl_mutex);
1321 /* Resubmit the URB. */
1322 w->urb->interval = dev->int_ep->desc.bInterval;
1323 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1325 uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
1329 bool uvc_ctrl_status_event(struct urb *urb, struct uvc_video_chain *chain,
1330 struct uvc_control *ctrl, const u8 *data)
1332 struct uvc_device *dev = chain->dev;
1333 struct uvc_ctrl_work *w = &dev->async_ctrl;
1335 if (list_empty(&ctrl->info.mappings)) {
1336 ctrl->handle = NULL;
1345 schedule_work(&w->work);
1350 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1351 unsigned int xctrls_count, u32 id)
1355 for (i = 0; i < xctrls_count; ++i) {
1356 if (xctrls[i].id == id)
1363 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1364 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1366 struct uvc_control_mapping *mapping;
1367 struct uvc_control *ctrl;
1368 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1372 for (i = 0; i < xctrls_count; ++i) {
1373 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1375 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1376 /* Notification will be sent from an Interrupt event. */
1379 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1380 u32 slave_id = mapping->slave_ids[j];
1386 * We can skip sending an event for the slave if the
1387 * slave is being modified in the same transaction.
1389 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1393 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1398 * If the master is being modified in the same transaction
1399 * flags may change too.
1401 if (mapping->master_id &&
1402 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1403 mapping->master_id))
1404 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1406 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1407 xctrls[i].value, changes);
1411 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1413 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1414 struct uvc_control_mapping *mapping;
1415 struct uvc_control *ctrl;
1418 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1420 return -ERESTARTSYS;
1422 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1428 list_add_tail(&sev->node, &mapping->ev_subs);
1429 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1430 struct v4l2_event ev;
1431 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1434 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1435 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1437 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1439 /* Mark the queue as active, allowing this initial
1440 event to be accepted. */
1442 v4l2_event_queue_fh(sev->fh, &ev);
1446 mutex_unlock(&handle->chain->ctrl_mutex);
1450 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1452 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1454 mutex_lock(&handle->chain->ctrl_mutex);
1455 list_del(&sev->node);
1456 mutex_unlock(&handle->chain->ctrl_mutex);
1459 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1460 .add = uvc_ctrl_add_event,
1461 .del = uvc_ctrl_del_event,
1462 .replace = v4l2_ctrl_replace,
1463 .merge = v4l2_ctrl_merge,
1466 /* --------------------------------------------------------------------------
1467 * Control transactions
1469 * To make extended set operations as atomic as the hardware allows, controls
1470 * are handled using begin/commit/rollback operations.
1472 * At the beginning of a set request, uvc_ctrl_begin should be called to
1473 * initialize the request. This function acquires the control lock.
1475 * When setting a control, the new value is stored in the control data field
1476 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1477 * later processing. If the UVC and V4L2 control sizes differ, the current
1478 * value is loaded from the hardware before storing the new value in the data
1481 * After processing all controls in the transaction, uvc_ctrl_commit or
1482 * uvc_ctrl_rollback must be called to apply the pending changes to the
1483 * hardware or revert them. When applying changes, all controls marked as
1484 * dirty will be modified in the UVC device, and the dirty flag will be
1485 * cleared. When reverting controls, the control data field
1486 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1487 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1490 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1492 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1495 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1496 struct uvc_entity *entity, int rollback)
1498 struct uvc_control *ctrl;
1505 for (i = 0; i < entity->ncontrols; ++i) {
1506 ctrl = &entity->controls[i];
1507 if (!ctrl->initialized)
1510 /* Reset the loaded flag for auto-update controls that were
1511 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1512 * uvc_ctrl_get from using the cached value, and for write-only
1513 * controls to prevent uvc_ctrl_set from setting bits not
1514 * explicitly set by the user.
1516 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1517 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1524 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1525 dev->intfnum, ctrl->info.selector,
1526 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1531 if (rollback || ret < 0)
1532 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1533 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1545 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1546 const struct v4l2_ext_control *xctrls,
1547 unsigned int xctrls_count)
1549 struct uvc_video_chain *chain = handle->chain;
1550 struct uvc_entity *entity;
1553 /* Find the control. */
1554 list_for_each_entry(entity, &chain->entities, chain) {
1555 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1561 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1563 mutex_unlock(&chain->ctrl_mutex);
1567 int uvc_ctrl_get(struct uvc_video_chain *chain,
1568 struct v4l2_ext_control *xctrl)
1570 struct uvc_control *ctrl;
1571 struct uvc_control_mapping *mapping;
1573 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1577 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1580 int uvc_ctrl_set(struct uvc_fh *handle,
1581 struct v4l2_ext_control *xctrl)
1583 struct uvc_video_chain *chain = handle->chain;
1584 struct uvc_control *ctrl;
1585 struct uvc_control_mapping *mapping;
1592 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1595 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1598 /* Clamp out of range values. */
1599 switch (mapping->v4l2_type) {
1600 case V4L2_CTRL_TYPE_INTEGER:
1601 if (!ctrl->cached) {
1602 ret = uvc_ctrl_populate_cache(chain, ctrl);
1607 min = mapping->get(mapping, UVC_GET_MIN,
1608 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1609 max = mapping->get(mapping, UVC_GET_MAX,
1610 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1611 step = mapping->get(mapping, UVC_GET_RES,
1612 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1616 xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
1618 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1619 xctrl->value = clamp(xctrl->value, min, max);
1621 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1622 value = xctrl->value;
1625 case V4L2_CTRL_TYPE_BOOLEAN:
1626 xctrl->value = clamp(xctrl->value, 0, 1);
1627 value = xctrl->value;
1630 case V4L2_CTRL_TYPE_MENU:
1631 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1633 value = mapping->menu_info[xctrl->value].value;
1635 /* Valid menu indices are reported by the GET_RES request for
1636 * UVC controls that support it.
1638 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1639 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1640 if (!ctrl->cached) {
1641 ret = uvc_ctrl_populate_cache(chain, ctrl);
1646 step = mapping->get(mapping, UVC_GET_RES,
1647 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1648 if (!(step & value))
1655 value = xctrl->value;
1659 /* If the mapping doesn't span the whole UVC control, the current value
1660 * needs to be loaded from the device to perform the read-modify-write
1663 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1664 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1665 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1666 0, ctrl->info.size);
1668 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1669 ctrl->entity->id, chain->dev->intfnum,
1670 ctrl->info.selector,
1671 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1680 /* Backup the current value in case we need to rollback later. */
1682 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1683 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1687 mapping->set(mapping, value,
1688 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1690 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1691 ctrl->handle = handle;
1698 /* --------------------------------------------------------------------------
1703 * Retrieve flags for a given control
1705 static int uvc_ctrl_get_flags(struct uvc_device *dev,
1706 const struct uvc_control *ctrl,
1707 struct uvc_control_info *info)
1712 data = kmalloc(1, GFP_KERNEL);
1716 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1717 info->selector, data, 1);
1719 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
1720 UVC_CTRL_FLAG_GET_CUR : 0)
1721 | (data[0] & UVC_CONTROL_CAP_SET ?
1722 UVC_CTRL_FLAG_SET_CUR : 0)
1723 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1724 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
1725 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
1726 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
1732 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1733 const struct uvc_control *ctrl, struct uvc_control_info *info)
1735 struct uvc_ctrl_fixup {
1736 struct usb_device_id id;
1742 static const struct uvc_ctrl_fixup fixups[] = {
1743 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1744 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1745 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1746 UVC_CTRL_FLAG_AUTO_UPDATE },
1747 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1748 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1749 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1750 UVC_CTRL_FLAG_AUTO_UPDATE },
1751 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1752 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1753 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1754 UVC_CTRL_FLAG_AUTO_UPDATE },
1759 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1760 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1763 if (fixups[i].entity == ctrl->entity->id &&
1764 fixups[i].selector == info->selector) {
1765 info->flags = fixups[i].flags;
1772 * Query control information (size and flags) for XU controls.
1774 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1775 const struct uvc_control *ctrl, struct uvc_control_info *info)
1780 data = kmalloc(2, GFP_KERNEL);
1784 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1785 sizeof(info->entity));
1786 info->index = ctrl->index;
1787 info->selector = ctrl->index + 1;
1789 /* Query and verify the control length (GET_LEN) */
1790 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1791 info->selector, data, 2);
1793 uvc_trace(UVC_TRACE_CONTROL,
1794 "GET_LEN failed on control %pUl/%u (%d).\n",
1795 info->entity, info->selector, ret);
1799 info->size = le16_to_cpup((__le16 *)data);
1801 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1802 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
1804 ret = uvc_ctrl_get_flags(dev, ctrl, info);
1806 uvc_trace(UVC_TRACE_CONTROL,
1807 "Failed to get flags for control %pUl/%u (%d).\n",
1808 info->entity, info->selector, ret);
1812 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1814 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1815 "flags { get %u set %u auto %u }.\n",
1816 info->entity, info->selector, info->size,
1817 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1818 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1819 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1826 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1827 const struct uvc_control_info *info);
1829 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1830 struct uvc_control *ctrl)
1832 struct uvc_control_info info;
1835 if (ctrl->initialized)
1838 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1842 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1844 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1845 "%pUl/%u on device %s entity %u\n", info.entity,
1846 info.selector, dev->udev->devpath, ctrl->entity->id);
1851 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1852 struct uvc_xu_control_query *xqry)
1854 struct uvc_entity *entity;
1855 struct uvc_control *ctrl;
1863 /* Find the extension unit. */
1865 list_for_each_entry(entity, &chain->entities, chain) {
1866 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1867 entity->id == xqry->unit) {
1874 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1879 /* Find the control and perform delayed initialization if needed. */
1881 for (i = 0; i < entity->ncontrols; ++i) {
1882 ctrl = &entity->controls[i];
1883 if (ctrl->index == xqry->selector - 1) {
1890 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1891 entity->extension.guidExtensionCode, xqry->selector);
1895 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1896 return -ERESTARTSYS;
1898 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1904 /* Validate the required buffer size and flags for the request */
1906 size = ctrl->info.size;
1908 switch (xqry->query) {
1910 reqflags = UVC_CTRL_FLAG_GET_CUR;
1913 reqflags = UVC_CTRL_FLAG_GET_MIN;
1916 reqflags = UVC_CTRL_FLAG_GET_MAX;
1919 reqflags = UVC_CTRL_FLAG_GET_DEF;
1922 reqflags = UVC_CTRL_FLAG_GET_RES;
1925 reqflags = UVC_CTRL_FLAG_SET_CUR;
1938 if (size != xqry->size) {
1943 if (reqflags && !(ctrl->info.flags & reqflags)) {
1948 data = kmalloc(size, GFP_KERNEL);
1954 if (xqry->query == UVC_SET_CUR &&
1955 copy_from_user(data, xqry->data, size)) {
1960 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1961 chain->dev->intfnum, xqry->selector, data, size);
1965 if (xqry->query != UVC_SET_CUR &&
1966 copy_to_user(xqry->data, data, size))
1970 mutex_unlock(&chain->ctrl_mutex);
1974 /* --------------------------------------------------------------------------
1979 * Restore control values after resume, skipping controls that haven't been
1983 * - Don't restore modified controls that are back to their default value.
1984 * - Handle restore order (Auto-Exposure Mode should be restored before
1987 int uvc_ctrl_restore_values(struct uvc_device *dev)
1989 struct uvc_control *ctrl;
1990 struct uvc_entity *entity;
1994 /* Walk the entities list and restore controls when possible. */
1995 list_for_each_entry(entity, &dev->entities, list) {
1997 for (i = 0; i < entity->ncontrols; ++i) {
1998 ctrl = &entity->controls[i];
2000 if (!ctrl->initialized || !ctrl->modified ||
2001 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2004 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
2005 ctrl->info.entity, ctrl->info.index,
2006 ctrl->info.selector);
2010 ret = uvc_ctrl_commit_entity(dev, entity, 0);
2018 /* --------------------------------------------------------------------------
2019 * Control and mapping handling
2023 * Add control information to a given control.
2025 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2026 const struct uvc_control_info *info)
2031 INIT_LIST_HEAD(&ctrl->info.mappings);
2033 /* Allocate an array to save control values (cur, def, max, etc.) */
2034 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2036 if (ctrl->uvc_data == NULL) {
2041 ctrl->initialized = 1;
2043 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
2044 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
2045 dev->udev->devpath, ctrl->entity->id);
2049 kfree(ctrl->uvc_data);
2054 * Add a control mapping to a given control.
2056 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
2057 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2059 struct uvc_control_mapping *map;
2062 /* Most mappings come from static kernel data and need to be duplicated.
2063 * Mappings that come from userspace will be unnecessarily duplicated,
2064 * this could be optimized.
2066 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2070 INIT_LIST_HEAD(&map->ev_subs);
2072 size = sizeof(*mapping->menu_info) * mapping->menu_count;
2073 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
2074 if (map->menu_info == NULL) {
2079 if (map->get == NULL)
2080 map->get = uvc_get_le_value;
2081 if (map->set == NULL)
2082 map->set = uvc_set_le_value;
2084 list_add_tail(&map->list, &ctrl->info.mappings);
2085 uvc_trace(UVC_TRACE_CONTROL,
2086 "Adding mapping '%s' to control %pUl/%u.\n",
2087 map->name, ctrl->info.entity, ctrl->info.selector);
2092 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2093 const struct uvc_control_mapping *mapping)
2095 struct uvc_device *dev = chain->dev;
2096 struct uvc_control_mapping *map;
2097 struct uvc_entity *entity;
2098 struct uvc_control *ctrl;
2102 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2103 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
2104 "id 0x%08x is invalid.\n", mapping->name,
2109 /* Search for the matching (GUID/CS) control on the current chain */
2110 list_for_each_entry(entity, &chain->entities, chain) {
2113 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2114 !uvc_entity_match_guid(entity, mapping->entity))
2117 for (i = 0; i < entity->ncontrols; ++i) {
2118 ctrl = &entity->controls[i];
2119 if (ctrl->index == mapping->selector - 1) {
2131 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2132 return -ERESTARTSYS;
2134 /* Perform delayed initialization of XU controls */
2135 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2141 /* Validate the user-provided bit-size and offset */
2142 if (mapping->size > 32 ||
2143 mapping->offset + mapping->size > ctrl->info.size * 8) {
2148 list_for_each_entry(map, &ctrl->info.mappings, list) {
2149 if (mapping->id == map->id) {
2150 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
2151 "control id 0x%08x already exists.\n",
2152 mapping->name, mapping->id);
2158 /* Prevent excess memory consumption */
2159 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2160 atomic_dec(&dev->nmappings);
2161 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
2162 "mappings count (%u) exceeded.\n", mapping->name,
2163 UVC_MAX_CONTROL_MAPPINGS);
2168 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2170 atomic_dec(&dev->nmappings);
2173 mutex_unlock(&chain->ctrl_mutex);
2178 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2179 * are currently the ones that crash the camera or unconditionally return an
2180 * error when queried.
2182 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2183 struct uvc_entity *entity)
2185 struct uvc_ctrl_blacklist {
2186 struct usb_device_id id;
2190 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2191 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2192 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2193 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2195 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2196 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2199 const struct uvc_ctrl_blacklist *blacklist;
2205 switch (UVC_ENTITY_TYPE(entity)) {
2206 case UVC_VC_PROCESSING_UNIT:
2207 blacklist = processing_blacklist;
2208 count = ARRAY_SIZE(processing_blacklist);
2209 controls = entity->processing.bmControls;
2210 size = entity->processing.bControlSize;
2213 case UVC_ITT_CAMERA:
2214 blacklist = camera_blacklist;
2215 count = ARRAY_SIZE(camera_blacklist);
2216 controls = entity->camera.bmControls;
2217 size = entity->camera.bControlSize;
2224 for (i = 0; i < count; ++i) {
2225 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2228 if (blacklist[i].index >= 8 * size ||
2229 !uvc_test_bit(controls, blacklist[i].index))
2232 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
2233 "removing it.\n", entity->id, blacklist[i].index);
2235 uvc_clear_bit(controls, blacklist[i].index);
2240 * Add control information and hardcoded stock control mappings to the given
2243 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2245 const struct uvc_control_info *info = uvc_ctrls;
2246 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2247 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2248 const struct uvc_control_mapping *mend =
2249 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2251 /* XU controls initialization requires querying the device for control
2252 * information. As some buggy UVC devices will crash when queried
2253 * repeatedly in a tight loop, delay XU controls initialization until
2256 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2259 for (; info < iend; ++info) {
2260 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2261 ctrl->index == info->index) {
2262 uvc_ctrl_add_info(dev, ctrl, info);
2264 * Retrieve control flags from the device. Ignore errors
2265 * and work with default flag values from the uvc_ctrl
2266 * array when the device doesn't properly implement
2267 * GET_INFO on standard controls.
2269 uvc_ctrl_get_flags(dev, ctrl, &ctrl->info);
2274 if (!ctrl->initialized)
2277 for (; mapping < mend; ++mapping) {
2278 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2279 ctrl->info.selector == mapping->selector)
2280 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2285 * Initialize device controls.
2287 int uvc_ctrl_init_device(struct uvc_device *dev)
2289 struct uvc_entity *entity;
2292 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2294 /* Walk the entities list and instantiate controls */
2295 list_for_each_entry(entity, &dev->entities, list) {
2296 struct uvc_control *ctrl;
2297 unsigned int bControlSize = 0, ncontrols;
2298 u8 *bmControls = NULL;
2300 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2301 bmControls = entity->extension.bmControls;
2302 bControlSize = entity->extension.bControlSize;
2303 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2304 bmControls = entity->processing.bmControls;
2305 bControlSize = entity->processing.bControlSize;
2306 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2307 bmControls = entity->camera.bmControls;
2308 bControlSize = entity->camera.bControlSize;
2311 /* Remove bogus/blacklisted controls */
2312 uvc_ctrl_prune_entity(dev, entity);
2314 /* Count supported controls and allocate the controls array */
2315 ncontrols = memweight(bmControls, bControlSize);
2319 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2321 if (entity->controls == NULL)
2323 entity->ncontrols = ncontrols;
2325 /* Initialize all supported controls */
2326 ctrl = entity->controls;
2327 for (i = 0; i < bControlSize * 8; ++i) {
2328 if (uvc_test_bit(bmControls, i) == 0)
2331 ctrl->entity = entity;
2334 uvc_ctrl_init_ctrl(dev, ctrl);
2343 * Cleanup device controls.
2345 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2346 struct uvc_control *ctrl)
2348 struct uvc_control_mapping *mapping, *nm;
2350 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2351 list_del(&mapping->list);
2352 kfree(mapping->menu_info);
2357 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2359 struct uvc_entity *entity;
2362 /* Can be uninitialized if we are aborting on probe error. */
2363 if (dev->async_ctrl.work.func)
2364 cancel_work_sync(&dev->async_ctrl.work);
2366 /* Free controls and control mappings for all entities. */
2367 list_for_each_entry(entity, &dev->entities, list) {
2368 for (i = 0; i < entity->ncontrols; ++i) {
2369 struct uvc_control *ctrl = &entity->controls[i];
2371 if (!ctrl->initialized)
2374 uvc_ctrl_cleanup_mappings(dev, ctrl);
2375 kfree(ctrl->uvc_data);
2378 kfree(entity->controls);