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/atomic.h>
24 #include <media/v4l2-ctrls.h>
28 #define UVC_CTRL_DATA_CURRENT 0
29 #define UVC_CTRL_DATA_BACKUP 1
30 #define UVC_CTRL_DATA_MIN 2
31 #define UVC_CTRL_DATA_MAX 3
32 #define UVC_CTRL_DATA_RES 4
33 #define UVC_CTRL_DATA_DEF 5
34 #define UVC_CTRL_DATA_LAST 6
36 /* ------------------------------------------------------------------------
40 static struct uvc_control_info uvc_ctrls[] = {
42 .entity = UVC_GUID_UVC_PROCESSING,
43 .selector = UVC_PU_BRIGHTNESS_CONTROL,
46 .flags = UVC_CTRL_FLAG_SET_CUR
47 | UVC_CTRL_FLAG_GET_RANGE
48 | UVC_CTRL_FLAG_RESTORE,
51 .entity = UVC_GUID_UVC_PROCESSING,
52 .selector = UVC_PU_CONTRAST_CONTROL,
55 .flags = UVC_CTRL_FLAG_SET_CUR
56 | UVC_CTRL_FLAG_GET_RANGE
57 | UVC_CTRL_FLAG_RESTORE,
60 .entity = UVC_GUID_UVC_PROCESSING,
61 .selector = UVC_PU_HUE_CONTROL,
64 .flags = UVC_CTRL_FLAG_SET_CUR
65 | UVC_CTRL_FLAG_GET_RANGE
66 | UVC_CTRL_FLAG_RESTORE
67 | UVC_CTRL_FLAG_AUTO_UPDATE,
70 .entity = UVC_GUID_UVC_PROCESSING,
71 .selector = UVC_PU_SATURATION_CONTROL,
74 .flags = UVC_CTRL_FLAG_SET_CUR
75 | UVC_CTRL_FLAG_GET_RANGE
76 | UVC_CTRL_FLAG_RESTORE,
79 .entity = UVC_GUID_UVC_PROCESSING,
80 .selector = UVC_PU_SHARPNESS_CONTROL,
83 .flags = UVC_CTRL_FLAG_SET_CUR
84 | UVC_CTRL_FLAG_GET_RANGE
85 | UVC_CTRL_FLAG_RESTORE,
88 .entity = UVC_GUID_UVC_PROCESSING,
89 .selector = UVC_PU_GAMMA_CONTROL,
92 .flags = UVC_CTRL_FLAG_SET_CUR
93 | UVC_CTRL_FLAG_GET_RANGE
94 | UVC_CTRL_FLAG_RESTORE,
97 .entity = UVC_GUID_UVC_PROCESSING,
98 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
101 .flags = UVC_CTRL_FLAG_SET_CUR
102 | UVC_CTRL_FLAG_GET_RANGE
103 | UVC_CTRL_FLAG_RESTORE
104 | UVC_CTRL_FLAG_AUTO_UPDATE,
107 .entity = UVC_GUID_UVC_PROCESSING,
108 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
111 .flags = UVC_CTRL_FLAG_SET_CUR
112 | UVC_CTRL_FLAG_GET_RANGE
113 | UVC_CTRL_FLAG_RESTORE
114 | UVC_CTRL_FLAG_AUTO_UPDATE,
117 .entity = UVC_GUID_UVC_PROCESSING,
118 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
121 .flags = UVC_CTRL_FLAG_SET_CUR
122 | UVC_CTRL_FLAG_GET_RANGE
123 | UVC_CTRL_FLAG_RESTORE,
126 .entity = UVC_GUID_UVC_PROCESSING,
127 .selector = UVC_PU_GAIN_CONTROL,
130 .flags = UVC_CTRL_FLAG_SET_CUR
131 | UVC_CTRL_FLAG_GET_RANGE
132 | UVC_CTRL_FLAG_RESTORE,
135 .entity = UVC_GUID_UVC_PROCESSING,
136 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
139 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
140 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
143 .entity = UVC_GUID_UVC_PROCESSING,
144 .selector = UVC_PU_HUE_AUTO_CONTROL,
147 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
148 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
151 .entity = UVC_GUID_UVC_PROCESSING,
152 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
155 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
156 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
159 .entity = UVC_GUID_UVC_PROCESSING,
160 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
163 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
164 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
167 .entity = UVC_GUID_UVC_PROCESSING,
168 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
171 .flags = UVC_CTRL_FLAG_SET_CUR
172 | UVC_CTRL_FLAG_GET_RANGE
173 | UVC_CTRL_FLAG_RESTORE,
176 .entity = UVC_GUID_UVC_PROCESSING,
177 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
180 .flags = UVC_CTRL_FLAG_SET_CUR
181 | UVC_CTRL_FLAG_GET_RANGE
182 | UVC_CTRL_FLAG_RESTORE,
185 .entity = UVC_GUID_UVC_PROCESSING,
186 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
189 .flags = UVC_CTRL_FLAG_GET_CUR,
192 .entity = UVC_GUID_UVC_PROCESSING,
193 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
196 .flags = UVC_CTRL_FLAG_GET_CUR,
199 .entity = UVC_GUID_UVC_CAMERA,
200 .selector = UVC_CT_SCANNING_MODE_CONTROL,
203 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
204 | UVC_CTRL_FLAG_RESTORE,
207 .entity = UVC_GUID_UVC_CAMERA,
208 .selector = UVC_CT_AE_MODE_CONTROL,
211 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
212 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
213 | UVC_CTRL_FLAG_RESTORE,
216 .entity = UVC_GUID_UVC_CAMERA,
217 .selector = UVC_CT_AE_PRIORITY_CONTROL,
220 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
221 | UVC_CTRL_FLAG_RESTORE,
224 .entity = UVC_GUID_UVC_CAMERA,
225 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
228 .flags = UVC_CTRL_FLAG_SET_CUR
229 | UVC_CTRL_FLAG_GET_RANGE
230 | UVC_CTRL_FLAG_RESTORE,
233 .entity = UVC_GUID_UVC_CAMERA,
234 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
237 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
240 .entity = UVC_GUID_UVC_CAMERA,
241 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
244 .flags = UVC_CTRL_FLAG_SET_CUR
245 | UVC_CTRL_FLAG_GET_RANGE
246 | UVC_CTRL_FLAG_RESTORE
247 | UVC_CTRL_FLAG_AUTO_UPDATE,
250 .entity = UVC_GUID_UVC_CAMERA,
251 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
254 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
255 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
256 | UVC_CTRL_FLAG_GET_DEF
257 | UVC_CTRL_FLAG_AUTO_UPDATE,
260 .entity = UVC_GUID_UVC_CAMERA,
261 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
264 .flags = UVC_CTRL_FLAG_SET_CUR
265 | UVC_CTRL_FLAG_GET_RANGE
266 | UVC_CTRL_FLAG_RESTORE
267 | UVC_CTRL_FLAG_AUTO_UPDATE,
270 .entity = UVC_GUID_UVC_CAMERA,
271 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
274 .flags = UVC_CTRL_FLAG_SET_CUR
275 | UVC_CTRL_FLAG_AUTO_UPDATE,
278 .entity = UVC_GUID_UVC_CAMERA,
279 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
282 .flags = UVC_CTRL_FLAG_SET_CUR
283 | UVC_CTRL_FLAG_GET_RANGE
284 | UVC_CTRL_FLAG_RESTORE
285 | UVC_CTRL_FLAG_AUTO_UPDATE,
288 .entity = UVC_GUID_UVC_CAMERA,
289 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
292 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
293 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
294 | UVC_CTRL_FLAG_GET_DEF
295 | UVC_CTRL_FLAG_AUTO_UPDATE,
298 .entity = UVC_GUID_UVC_CAMERA,
299 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
302 .flags = UVC_CTRL_FLAG_SET_CUR
303 | UVC_CTRL_FLAG_GET_RANGE
304 | UVC_CTRL_FLAG_RESTORE
305 | UVC_CTRL_FLAG_AUTO_UPDATE,
308 .entity = UVC_GUID_UVC_CAMERA,
309 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
312 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
313 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
314 | UVC_CTRL_FLAG_GET_DEF
315 | UVC_CTRL_FLAG_AUTO_UPDATE,
318 .entity = UVC_GUID_UVC_CAMERA,
319 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
322 .flags = UVC_CTRL_FLAG_SET_CUR
323 | UVC_CTRL_FLAG_GET_RANGE
324 | UVC_CTRL_FLAG_RESTORE
325 | UVC_CTRL_FLAG_AUTO_UPDATE,
328 .entity = UVC_GUID_UVC_CAMERA,
329 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
332 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
333 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
334 | UVC_CTRL_FLAG_GET_DEF
335 | UVC_CTRL_FLAG_AUTO_UPDATE,
338 .entity = UVC_GUID_UVC_CAMERA,
339 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
342 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
343 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
346 .entity = UVC_GUID_UVC_CAMERA,
347 .selector = UVC_CT_PRIVACY_CONTROL,
350 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
351 | UVC_CTRL_FLAG_RESTORE
352 | UVC_CTRL_FLAG_AUTO_UPDATE,
356 static struct uvc_menu_info power_line_frequency_controls[] = {
362 static struct uvc_menu_info exposure_auto_controls[] = {
364 { 1, "Manual Mode" },
365 { 4, "Shutter Priority Mode" },
366 { 8, "Aperture Priority Mode" },
369 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
370 __u8 query, const __u8 *data)
372 __s8 zoom = (__s8)data[0];
376 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
387 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
388 __s32 value, __u8 *data)
390 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
391 data[2] = min((int)abs(value), 0xff);
394 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
396 .id = V4L2_CID_BRIGHTNESS,
397 .name = "Brightness",
398 .entity = UVC_GUID_UVC_PROCESSING,
399 .selector = UVC_PU_BRIGHTNESS_CONTROL,
402 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
403 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
406 .id = V4L2_CID_CONTRAST,
408 .entity = UVC_GUID_UVC_PROCESSING,
409 .selector = UVC_PU_CONTRAST_CONTROL,
412 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
413 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
418 .entity = UVC_GUID_UVC_PROCESSING,
419 .selector = UVC_PU_HUE_CONTROL,
422 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
423 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
426 .id = V4L2_CID_SATURATION,
427 .name = "Saturation",
428 .entity = UVC_GUID_UVC_PROCESSING,
429 .selector = UVC_PU_SATURATION_CONTROL,
432 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
433 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
436 .id = V4L2_CID_SHARPNESS,
438 .entity = UVC_GUID_UVC_PROCESSING,
439 .selector = UVC_PU_SHARPNESS_CONTROL,
442 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
443 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
446 .id = V4L2_CID_GAMMA,
448 .entity = UVC_GUID_UVC_PROCESSING,
449 .selector = UVC_PU_GAMMA_CONTROL,
452 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
453 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
456 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
457 .name = "Backlight Compensation",
458 .entity = UVC_GUID_UVC_PROCESSING,
459 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
462 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
463 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
468 .entity = UVC_GUID_UVC_PROCESSING,
469 .selector = UVC_PU_GAIN_CONTROL,
472 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
473 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
476 .id = V4L2_CID_POWER_LINE_FREQUENCY,
477 .name = "Power Line Frequency",
478 .entity = UVC_GUID_UVC_PROCESSING,
479 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
482 .v4l2_type = V4L2_CTRL_TYPE_MENU,
483 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
484 .menu_info = power_line_frequency_controls,
485 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
488 .id = V4L2_CID_HUE_AUTO,
490 .entity = UVC_GUID_UVC_PROCESSING,
491 .selector = UVC_PU_HUE_AUTO_CONTROL,
494 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
495 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
498 .id = V4L2_CID_EXPOSURE_AUTO,
499 .name = "Exposure, Auto",
500 .entity = UVC_GUID_UVC_CAMERA,
501 .selector = UVC_CT_AE_MODE_CONTROL,
504 .v4l2_type = V4L2_CTRL_TYPE_MENU,
505 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
506 .menu_info = exposure_auto_controls,
507 .menu_count = ARRAY_SIZE(exposure_auto_controls),
510 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
511 .name = "Exposure, Auto Priority",
512 .entity = UVC_GUID_UVC_CAMERA,
513 .selector = UVC_CT_AE_PRIORITY_CONTROL,
516 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
517 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
520 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
521 .name = "Exposure (Absolute)",
522 .entity = UVC_GUID_UVC_CAMERA,
523 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
526 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
527 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
530 .id = V4L2_CID_AUTO_WHITE_BALANCE,
531 .name = "White Balance Temperature, Auto",
532 .entity = UVC_GUID_UVC_PROCESSING,
533 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
536 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
537 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
540 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
541 .name = "White Balance Temperature",
542 .entity = UVC_GUID_UVC_PROCESSING,
543 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
546 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
547 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
550 .id = V4L2_CID_AUTO_WHITE_BALANCE,
551 .name = "White Balance Component, Auto",
552 .entity = UVC_GUID_UVC_PROCESSING,
553 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
556 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
557 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
560 .id = V4L2_CID_BLUE_BALANCE,
561 .name = "White Balance Blue Component",
562 .entity = UVC_GUID_UVC_PROCESSING,
563 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
566 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
567 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
570 .id = V4L2_CID_RED_BALANCE,
571 .name = "White Balance Red Component",
572 .entity = UVC_GUID_UVC_PROCESSING,
573 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
576 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
577 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
580 .id = V4L2_CID_FOCUS_ABSOLUTE,
581 .name = "Focus (absolute)",
582 .entity = UVC_GUID_UVC_CAMERA,
583 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
586 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
587 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
590 .id = V4L2_CID_FOCUS_AUTO,
591 .name = "Focus, Auto",
592 .entity = UVC_GUID_UVC_CAMERA,
593 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
596 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
597 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
600 .id = V4L2_CID_IRIS_ABSOLUTE,
601 .name = "Iris, Absolute",
602 .entity = UVC_GUID_UVC_CAMERA,
603 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
606 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
607 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
610 .id = V4L2_CID_IRIS_RELATIVE,
611 .name = "Iris, Relative",
612 .entity = UVC_GUID_UVC_CAMERA,
613 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
616 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
617 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
620 .id = V4L2_CID_ZOOM_ABSOLUTE,
621 .name = "Zoom, Absolute",
622 .entity = UVC_GUID_UVC_CAMERA,
623 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
626 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
627 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
630 .id = V4L2_CID_ZOOM_CONTINUOUS,
631 .name = "Zoom, Continuous",
632 .entity = UVC_GUID_UVC_CAMERA,
633 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
636 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
637 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
638 .get = uvc_ctrl_get_zoom,
639 .set = uvc_ctrl_set_zoom,
642 .id = V4L2_CID_PAN_ABSOLUTE,
643 .name = "Pan (Absolute)",
644 .entity = UVC_GUID_UVC_CAMERA,
645 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
648 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
649 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
652 .id = V4L2_CID_TILT_ABSOLUTE,
653 .name = "Tilt (Absolute)",
654 .entity = UVC_GUID_UVC_CAMERA,
655 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
658 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
659 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
662 .id = V4L2_CID_PRIVACY,
664 .entity = UVC_GUID_UVC_CAMERA,
665 .selector = UVC_CT_PRIVACY_CONTROL,
668 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
669 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
673 /* ------------------------------------------------------------------------
677 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
679 return ctrl->uvc_data + id * ctrl->info.size;
682 static inline int uvc_test_bit(const __u8 *data, int bit)
684 return (data[bit >> 3] >> (bit & 7)) & 1;
687 static inline void uvc_clear_bit(__u8 *data, int bit)
689 data[bit >> 3] &= ~(1 << (bit & 7));
692 /* Extract the bit string specified by mapping->offset and mapping->size
693 * from the little-endian data stored at 'data' and return the result as
694 * a signed 32bit integer. Sign extension will be performed if the mapping
695 * references a signed data type.
697 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
698 __u8 query, const __u8 *data)
700 int bits = mapping->size;
701 int offset = mapping->offset;
707 mask = ((1LL << bits) - 1) << offset;
709 for (; bits > 0; data++) {
710 __u8 byte = *data & mask;
711 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
712 bits -= 8 - (offset > 0 ? offset : 0);
714 mask = (1 << bits) - 1;
717 /* Sign-extend the value if needed. */
718 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
719 value |= -(value & (1 << (mapping->size - 1)));
724 /* Set the bit string specified by mapping->offset and mapping->size
725 * in the little-endian data stored at 'data' to the value 'value'.
727 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
728 __s32 value, __u8 *data)
730 int bits = mapping->size;
731 int offset = mapping->offset;
734 /* According to the v4l2 spec, writing any value to a button control
735 * should result in the action belonging to the button control being
736 * triggered. UVC devices however want to see a 1 written -> override
739 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
745 for (; bits > 0; data++) {
746 mask = ((1LL << bits) - 1) << offset;
747 *data = (*data & ~mask) | ((value << offset) & mask);
748 value >>= offset ? offset : 8;
754 /* ------------------------------------------------------------------------
755 * Terminal and unit management
758 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
759 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
760 static const __u8 uvc_media_transport_input_guid[16] =
761 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
763 static int uvc_entity_match_guid(const struct uvc_entity *entity,
766 switch (UVC_ENTITY_TYPE(entity)) {
768 return memcmp(uvc_camera_guid, guid, 16) == 0;
770 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
771 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
773 case UVC_VC_PROCESSING_UNIT:
774 return memcmp(uvc_processing_guid, guid, 16) == 0;
776 case UVC_VC_EXTENSION_UNIT:
777 return memcmp(entity->extension.guidExtensionCode,
785 /* ------------------------------------------------------------------------
789 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
790 struct uvc_control_mapping **mapping, struct uvc_control **control,
793 struct uvc_control *ctrl;
794 struct uvc_control_mapping *map;
800 for (i = 0; i < entity->ncontrols; ++i) {
801 ctrl = &entity->controls[i];
802 if (!ctrl->initialized)
805 list_for_each_entry(map, &ctrl->info.mappings, list) {
806 if ((map->id == v4l2_id) && !next) {
812 if ((*mapping == NULL || (*mapping)->id > map->id) &&
813 (map->id > v4l2_id) && next) {
821 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
822 __u32 v4l2_id, struct uvc_control_mapping **mapping)
824 struct uvc_control *ctrl = NULL;
825 struct uvc_entity *entity;
826 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
830 /* Mask the query flags. */
831 v4l2_id &= V4L2_CTRL_ID_MASK;
833 /* Find the control. */
834 list_for_each_entry(entity, &chain->entities, chain) {
835 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
840 if (ctrl == NULL && !next)
841 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
847 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
848 struct uvc_control *ctrl)
852 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
853 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
854 chain->dev->intfnum, ctrl->info.selector,
855 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
861 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
862 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
863 chain->dev->intfnum, ctrl->info.selector,
864 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
869 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
870 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
871 chain->dev->intfnum, ctrl->info.selector,
872 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
877 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
878 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
879 chain->dev->intfnum, ctrl->info.selector,
880 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
883 if (UVC_ENTITY_TYPE(ctrl->entity) !=
884 UVC_VC_EXTENSION_UNIT)
887 /* GET_RES is mandatory for XU controls, but some
888 * cameras still choke on it. Ignore errors and set the
889 * resolution value to zero.
891 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
892 "UVC non compliance - GET_RES failed on "
893 "an XU control. Enabling workaround.\n");
894 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
903 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
904 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
907 struct uvc_menu_info *menu;
911 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
915 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
916 chain->dev->intfnum, ctrl->info.selector,
917 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
925 *value = mapping->get(mapping, UVC_GET_CUR,
926 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
928 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
929 menu = mapping->menu_info;
930 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
931 if (menu->value == *value) {
941 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
942 struct uvc_control *ctrl,
943 struct uvc_control_mapping *mapping,
944 struct v4l2_queryctrl *v4l2_ctrl)
946 struct uvc_menu_info *menu;
949 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
950 v4l2_ctrl->id = mapping->id;
951 v4l2_ctrl->type = mapping->v4l2_type;
952 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
953 v4l2_ctrl->flags = 0;
955 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
956 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
957 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
958 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
961 int ret = uvc_ctrl_populate_cache(chain, ctrl);
966 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
967 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
968 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
971 switch (mapping->v4l2_type) {
972 case V4L2_CTRL_TYPE_MENU:
973 v4l2_ctrl->minimum = 0;
974 v4l2_ctrl->maximum = mapping->menu_count - 1;
977 menu = mapping->menu_info;
978 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
979 if (menu->value == v4l2_ctrl->default_value) {
980 v4l2_ctrl->default_value = i;
987 case V4L2_CTRL_TYPE_BOOLEAN:
988 v4l2_ctrl->minimum = 0;
989 v4l2_ctrl->maximum = 1;
993 case V4L2_CTRL_TYPE_BUTTON:
994 v4l2_ctrl->minimum = 0;
995 v4l2_ctrl->maximum = 0;
1003 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1004 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1005 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1007 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1008 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1009 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1011 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1012 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1013 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1018 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1019 struct v4l2_queryctrl *v4l2_ctrl)
1021 struct uvc_control *ctrl;
1022 struct uvc_control_mapping *mapping;
1025 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1027 return -ERESTARTSYS;
1029 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1035 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1037 mutex_unlock(&chain->ctrl_mutex);
1042 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
1043 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1044 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1045 * Balance V4L2 controls use the White Balance Component UVC control) or
1046 * otherwise translated. The approach we take here is to use a translation
1047 * table for the controls that can be mapped directly, and handle the others
1050 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1051 struct v4l2_querymenu *query_menu)
1053 struct uvc_menu_info *menu_info;
1054 struct uvc_control_mapping *mapping;
1055 struct uvc_control *ctrl;
1056 u32 index = query_menu->index;
1057 u32 id = query_menu->id;
1060 memset(query_menu, 0, sizeof(*query_menu));
1061 query_menu->id = id;
1062 query_menu->index = index;
1064 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1066 return -ERESTARTSYS;
1068 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1069 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1074 if (query_menu->index >= mapping->menu_count) {
1079 menu_info = &mapping->menu_info[query_menu->index];
1081 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1082 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1085 if (!ctrl->cached) {
1086 ret = uvc_ctrl_populate_cache(chain, ctrl);
1091 bitmap = mapping->get(mapping, UVC_GET_RES,
1092 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1093 if (!(bitmap & menu_info->value)) {
1099 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
1102 mutex_unlock(&chain->ctrl_mutex);
1106 /* --------------------------------------------------------------------------
1107 * Ctrl event handling
1110 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1111 struct v4l2_event *ev,
1112 struct uvc_control *ctrl,
1113 struct uvc_control_mapping *mapping,
1114 s32 value, u32 changes)
1116 struct v4l2_queryctrl v4l2_ctrl;
1118 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1120 memset(ev->reserved, 0, sizeof(ev->reserved));
1121 ev->type = V4L2_EVENT_CTRL;
1122 ev->id = v4l2_ctrl.id;
1123 ev->u.ctrl.value = value;
1124 ev->u.ctrl.changes = changes;
1125 ev->u.ctrl.type = v4l2_ctrl.type;
1126 ev->u.ctrl.flags = v4l2_ctrl.flags;
1127 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1128 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1129 ev->u.ctrl.step = v4l2_ctrl.step;
1130 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1133 static void uvc_ctrl_send_event(struct uvc_fh *handle,
1134 struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
1135 s32 value, u32 changes)
1137 struct v4l2_subscribed_event *sev;
1138 struct v4l2_event ev;
1140 if (list_empty(&mapping->ev_subs))
1143 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes);
1145 list_for_each_entry(sev, &mapping->ev_subs, node) {
1146 if (sev->fh && (sev->fh != &handle->vfh ||
1147 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK)))
1148 v4l2_event_queue_fh(sev->fh, &ev);
1152 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1153 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1155 struct uvc_control_mapping *mapping;
1156 struct uvc_control *ctrl;
1159 for (i = 0; i < xctrls_count; ++i) {
1160 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1161 uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
1162 V4L2_EVENT_CTRL_CH_VALUE);
1166 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev)
1168 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1169 struct uvc_control_mapping *mapping;
1170 struct uvc_control *ctrl;
1173 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1175 return -ERESTARTSYS;
1177 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1183 list_add_tail(&sev->node, &mapping->ev_subs);
1184 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1185 struct v4l2_event ev;
1186 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1189 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1190 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1192 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1194 v4l2_event_queue_fh(sev->fh, &ev);
1198 mutex_unlock(&handle->chain->ctrl_mutex);
1202 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1204 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1206 mutex_lock(&handle->chain->ctrl_mutex);
1207 list_del(&sev->node);
1208 mutex_unlock(&handle->chain->ctrl_mutex);
1211 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1212 .add = uvc_ctrl_add_event,
1213 .del = uvc_ctrl_del_event,
1214 .replace = v4l2_ctrl_replace,
1215 .merge = v4l2_ctrl_merge,
1218 /* --------------------------------------------------------------------------
1219 * Control transactions
1221 * To make extended set operations as atomic as the hardware allows, controls
1222 * are handled using begin/commit/rollback operations.
1224 * At the beginning of a set request, uvc_ctrl_begin should be called to
1225 * initialize the request. This function acquires the control lock.
1227 * When setting a control, the new value is stored in the control data field
1228 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1229 * later processing. If the UVC and V4L2 control sizes differ, the current
1230 * value is loaded from the hardware before storing the new value in the data
1233 * After processing all controls in the transaction, uvc_ctrl_commit or
1234 * uvc_ctrl_rollback must be called to apply the pending changes to the
1235 * hardware or revert them. When applying changes, all controls marked as
1236 * dirty will be modified in the UVC device, and the dirty flag will be
1237 * cleared. When reverting controls, the control data field
1238 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1239 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1242 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1244 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1247 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1248 struct uvc_entity *entity, int rollback)
1250 struct uvc_control *ctrl;
1257 for (i = 0; i < entity->ncontrols; ++i) {
1258 ctrl = &entity->controls[i];
1259 if (!ctrl->initialized)
1262 /* Reset the loaded flag for auto-update controls that were
1263 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1264 * uvc_ctrl_get from using the cached value.
1266 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE)
1273 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1274 dev->intfnum, ctrl->info.selector,
1275 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1280 if (rollback || ret < 0)
1281 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1282 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1294 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1295 const struct v4l2_ext_control *xctrls,
1296 unsigned int xctrls_count)
1298 struct uvc_video_chain *chain = handle->chain;
1299 struct uvc_entity *entity;
1302 /* Find the control. */
1303 list_for_each_entry(entity, &chain->entities, chain) {
1304 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1310 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1312 mutex_unlock(&chain->ctrl_mutex);
1316 int uvc_ctrl_get(struct uvc_video_chain *chain,
1317 struct v4l2_ext_control *xctrl)
1319 struct uvc_control *ctrl;
1320 struct uvc_control_mapping *mapping;
1322 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1326 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1329 int uvc_ctrl_set(struct uvc_video_chain *chain,
1330 struct v4l2_ext_control *xctrl)
1332 struct uvc_control *ctrl;
1333 struct uvc_control_mapping *mapping;
1340 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1341 if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
1344 /* Clamp out of range values. */
1345 switch (mapping->v4l2_type) {
1346 case V4L2_CTRL_TYPE_INTEGER:
1347 if (!ctrl->cached) {
1348 ret = uvc_ctrl_populate_cache(chain, ctrl);
1353 min = mapping->get(mapping, UVC_GET_MIN,
1354 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1355 max = mapping->get(mapping, UVC_GET_MAX,
1356 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1357 step = mapping->get(mapping, UVC_GET_RES,
1358 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1362 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1363 xctrl->value = clamp(xctrl->value, min, max);
1364 value = xctrl->value;
1367 case V4L2_CTRL_TYPE_BOOLEAN:
1368 xctrl->value = clamp(xctrl->value, 0, 1);
1369 value = xctrl->value;
1372 case V4L2_CTRL_TYPE_MENU:
1373 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1375 value = mapping->menu_info[xctrl->value].value;
1377 /* Valid menu indices are reported by the GET_RES request for
1378 * UVC controls that support it.
1380 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1381 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1382 if (!ctrl->cached) {
1383 ret = uvc_ctrl_populate_cache(chain, ctrl);
1388 step = mapping->get(mapping, UVC_GET_RES,
1389 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1390 if (!(step & value))
1397 value = xctrl->value;
1401 /* If the mapping doesn't span the whole UVC control, the current value
1402 * needs to be loaded from the device to perform the read-modify-write
1405 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1406 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1407 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1408 0, ctrl->info.size);
1410 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1411 ctrl->entity->id, chain->dev->intfnum,
1412 ctrl->info.selector,
1413 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1422 /* Backup the current value in case we need to rollback later. */
1424 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1425 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1429 mapping->set(mapping, value,
1430 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1437 /* --------------------------------------------------------------------------
1441 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1442 const struct uvc_control *ctrl, struct uvc_control_info *info)
1444 struct uvc_ctrl_fixup {
1445 struct usb_device_id id;
1451 static const struct uvc_ctrl_fixup fixups[] = {
1452 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1453 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1454 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1455 UVC_CTRL_FLAG_AUTO_UPDATE },
1456 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1457 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1458 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1459 UVC_CTRL_FLAG_AUTO_UPDATE },
1460 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1461 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1462 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1463 UVC_CTRL_FLAG_AUTO_UPDATE },
1468 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1469 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1472 if (fixups[i].entity == ctrl->entity->id &&
1473 fixups[i].selector == info->selector) {
1474 info->flags = fixups[i].flags;
1481 * Query control information (size and flags) for XU controls.
1483 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1484 const struct uvc_control *ctrl, struct uvc_control_info *info)
1489 data = kmalloc(2, GFP_KERNEL);
1493 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1494 sizeof(info->entity));
1495 info->index = ctrl->index;
1496 info->selector = ctrl->index + 1;
1498 /* Query and verify the control length (GET_LEN) */
1499 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1500 info->selector, data, 2);
1502 uvc_trace(UVC_TRACE_CONTROL,
1503 "GET_LEN failed on control %pUl/%u (%d).\n",
1504 info->entity, info->selector, ret);
1508 info->size = le16_to_cpup((__le16 *)data);
1510 /* Query the control information (GET_INFO) */
1511 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1512 info->selector, data, 1);
1514 uvc_trace(UVC_TRACE_CONTROL,
1515 "GET_INFO failed on control %pUl/%u (%d).\n",
1516 info->entity, info->selector, ret);
1520 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1521 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1522 | (data[0] & UVC_CONTROL_CAP_GET ?
1523 UVC_CTRL_FLAG_GET_CUR : 0)
1524 | (data[0] & UVC_CONTROL_CAP_SET ?
1525 UVC_CTRL_FLAG_SET_CUR : 0)
1526 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1527 UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1529 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1531 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1532 "flags { get %u set %u auto %u }.\n",
1533 info->entity, info->selector, info->size,
1534 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1535 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1536 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1543 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1544 const struct uvc_control_info *info);
1546 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1547 struct uvc_control *ctrl)
1549 struct uvc_control_info info;
1552 if (ctrl->initialized)
1555 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1559 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1561 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1562 "%pUl/%u on device %s entity %u\n", info.entity,
1563 info.selector, dev->udev->devpath, ctrl->entity->id);
1568 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1569 struct uvc_xu_control_query *xqry)
1571 struct uvc_entity *entity;
1572 struct uvc_control *ctrl;
1573 unsigned int i, found = 0;
1579 /* Find the extension unit. */
1580 list_for_each_entry(entity, &chain->entities, chain) {
1581 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1582 entity->id == xqry->unit)
1586 if (entity->id != xqry->unit) {
1587 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1592 /* Find the control and perform delayed initialization if needed. */
1593 for (i = 0; i < entity->ncontrols; ++i) {
1594 ctrl = &entity->controls[i];
1595 if (ctrl->index == xqry->selector - 1) {
1602 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1603 entity->extension.guidExtensionCode, xqry->selector);
1607 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1608 return -ERESTARTSYS;
1610 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1616 /* Validate the required buffer size and flags for the request */
1618 size = ctrl->info.size;
1620 switch (xqry->query) {
1622 reqflags = UVC_CTRL_FLAG_GET_CUR;
1625 reqflags = UVC_CTRL_FLAG_GET_MIN;
1628 reqflags = UVC_CTRL_FLAG_GET_MAX;
1631 reqflags = UVC_CTRL_FLAG_GET_DEF;
1634 reqflags = UVC_CTRL_FLAG_GET_RES;
1637 reqflags = UVC_CTRL_FLAG_SET_CUR;
1650 if (size != xqry->size) {
1655 if (reqflags && !(ctrl->info.flags & reqflags)) {
1660 data = kmalloc(size, GFP_KERNEL);
1666 if (xqry->query == UVC_SET_CUR &&
1667 copy_from_user(data, xqry->data, size)) {
1672 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1673 chain->dev->intfnum, xqry->selector, data, size);
1677 if (xqry->query != UVC_SET_CUR &&
1678 copy_to_user(xqry->data, data, size))
1682 mutex_unlock(&chain->ctrl_mutex);
1686 /* --------------------------------------------------------------------------
1691 * Restore control values after resume, skipping controls that haven't been
1695 * - Don't restore modified controls that are back to their default value.
1696 * - Handle restore order (Auto-Exposure Mode should be restored before
1699 int uvc_ctrl_resume_device(struct uvc_device *dev)
1701 struct uvc_control *ctrl;
1702 struct uvc_entity *entity;
1706 /* Walk the entities list and restore controls when possible. */
1707 list_for_each_entry(entity, &dev->entities, list) {
1709 for (i = 0; i < entity->ncontrols; ++i) {
1710 ctrl = &entity->controls[i];
1712 if (!ctrl->initialized || !ctrl->modified ||
1713 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1716 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1717 ctrl->info.entity, ctrl->info.index,
1718 ctrl->info.selector);
1722 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1730 /* --------------------------------------------------------------------------
1731 * Control and mapping handling
1735 * Add control information to a given control.
1737 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1738 const struct uvc_control_info *info)
1742 memcpy(&ctrl->info, info, sizeof(*info));
1743 INIT_LIST_HEAD(&ctrl->info.mappings);
1745 /* Allocate an array to save control values (cur, def, max, etc.) */
1746 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1748 if (ctrl->uvc_data == NULL) {
1753 ctrl->initialized = 1;
1755 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1756 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
1757 dev->udev->devpath, ctrl->entity->id);
1761 kfree(ctrl->uvc_data);
1766 * Add a control mapping to a given control.
1768 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1769 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1771 struct uvc_control_mapping *map;
1774 /* Most mappings come from static kernel data and need to be duplicated.
1775 * Mappings that come from userspace will be unnecessarily duplicated,
1776 * this could be optimized.
1778 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1782 INIT_LIST_HEAD(&map->ev_subs);
1784 size = sizeof(*mapping->menu_info) * mapping->menu_count;
1785 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1786 if (map->menu_info == NULL) {
1791 if (map->get == NULL)
1792 map->get = uvc_get_le_value;
1793 if (map->set == NULL)
1794 map->set = uvc_set_le_value;
1796 map->ctrl = &ctrl->info;
1797 list_add_tail(&map->list, &ctrl->info.mappings);
1798 uvc_trace(UVC_TRACE_CONTROL,
1799 "Adding mapping '%s' to control %pUl/%u.\n",
1800 map->name, ctrl->info.entity, ctrl->info.selector);
1805 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1806 const struct uvc_control_mapping *mapping)
1808 struct uvc_device *dev = chain->dev;
1809 struct uvc_control_mapping *map;
1810 struct uvc_entity *entity;
1811 struct uvc_control *ctrl;
1815 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1816 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1817 "id 0x%08x is invalid.\n", mapping->name,
1822 /* Search for the matching (GUID/CS) control on the current chain */
1823 list_for_each_entry(entity, &chain->entities, chain) {
1826 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1827 !uvc_entity_match_guid(entity, mapping->entity))
1830 for (i = 0; i < entity->ncontrols; ++i) {
1831 ctrl = &entity->controls[i];
1832 if (ctrl->index == mapping->selector - 1) {
1844 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1845 return -ERESTARTSYS;
1847 /* Perform delayed initialization of XU controls */
1848 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
1854 list_for_each_entry(map, &ctrl->info.mappings, list) {
1855 if (mapping->id == map->id) {
1856 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
1857 "control id 0x%08x already exists.\n",
1858 mapping->name, mapping->id);
1864 /* Prevent excess memory consumption */
1865 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
1866 atomic_dec(&dev->nmappings);
1867 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
1868 "mappings count (%u) exceeded.\n", mapping->name,
1869 UVC_MAX_CONTROL_MAPPINGS);
1874 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1876 atomic_dec(&dev->nmappings);
1879 mutex_unlock(&chain->ctrl_mutex);
1884 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1885 * are currently the ones that crash the camera or unconditionally return an
1886 * error when queried.
1888 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
1889 struct uvc_entity *entity)
1891 struct uvc_ctrl_blacklist {
1892 struct usb_device_id id;
1896 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1897 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1898 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1899 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1901 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
1902 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
1905 const struct uvc_ctrl_blacklist *blacklist;
1911 switch (UVC_ENTITY_TYPE(entity)) {
1912 case UVC_VC_PROCESSING_UNIT:
1913 blacklist = processing_blacklist;
1914 count = ARRAY_SIZE(processing_blacklist);
1915 controls = entity->processing.bmControls;
1916 size = entity->processing.bControlSize;
1919 case UVC_ITT_CAMERA:
1920 blacklist = camera_blacklist;
1921 count = ARRAY_SIZE(camera_blacklist);
1922 controls = entity->camera.bmControls;
1923 size = entity->camera.bControlSize;
1930 for (i = 0; i < count; ++i) {
1931 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1934 if (blacklist[i].index >= 8 * size ||
1935 !uvc_test_bit(controls, blacklist[i].index))
1938 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1939 "removing it.\n", entity->id, blacklist[i].index);
1941 uvc_clear_bit(controls, blacklist[i].index);
1946 * Add control information and hardcoded stock control mappings to the given
1949 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
1951 const struct uvc_control_info *info = uvc_ctrls;
1952 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
1953 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1954 const struct uvc_control_mapping *mend =
1955 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1957 /* XU controls initialization requires querying the device for control
1958 * information. As some buggy UVC devices will crash when queried
1959 * repeatedly in a tight loop, delay XU controls initialization until
1962 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
1965 for (; info < iend; ++info) {
1966 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
1967 ctrl->index == info->index) {
1968 uvc_ctrl_add_info(dev, ctrl, info);
1973 if (!ctrl->initialized)
1976 for (; mapping < mend; ++mapping) {
1977 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
1978 ctrl->info.selector == mapping->selector)
1979 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1984 * Initialize device controls.
1986 int uvc_ctrl_init_device(struct uvc_device *dev)
1988 struct uvc_entity *entity;
1991 /* Walk the entities list and instantiate controls */
1992 list_for_each_entry(entity, &dev->entities, list) {
1993 struct uvc_control *ctrl;
1994 unsigned int bControlSize = 0, ncontrols = 0;
1995 __u8 *bmControls = NULL;
1997 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1998 bmControls = entity->extension.bmControls;
1999 bControlSize = entity->extension.bControlSize;
2000 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2001 bmControls = entity->processing.bmControls;
2002 bControlSize = entity->processing.bControlSize;
2003 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2004 bmControls = entity->camera.bmControls;
2005 bControlSize = entity->camera.bControlSize;
2008 /* Remove bogus/blacklisted controls */
2009 uvc_ctrl_prune_entity(dev, entity);
2011 /* Count supported controls and allocate the controls array */
2012 for (i = 0; i < bControlSize; ++i)
2013 ncontrols += hweight8(bmControls[i]);
2017 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2019 if (entity->controls == NULL)
2021 entity->ncontrols = ncontrols;
2023 /* Initialize all supported controls */
2024 ctrl = entity->controls;
2025 for (i = 0; i < bControlSize * 8; ++i) {
2026 if (uvc_test_bit(bmControls, i) == 0)
2029 ctrl->entity = entity;
2032 uvc_ctrl_init_ctrl(dev, ctrl);
2041 * Cleanup device controls.
2043 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2044 struct uvc_control *ctrl)
2046 struct uvc_control_mapping *mapping, *nm;
2048 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2049 list_del(&mapping->list);
2050 kfree(mapping->menu_info);
2055 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2057 struct uvc_entity *entity;
2060 /* Free controls and control mappings for all entities. */
2061 list_for_each_entry(entity, &dev->entities, list) {
2062 for (i = 0; i < entity->ncontrols; ++i) {
2063 struct uvc_control *ctrl = &entity->controls[i];
2065 if (!ctrl->initialized)
2068 uvc_ctrl_cleanup_mappings(dev, ctrl);
2069 kfree(ctrl->uvc_data);
2072 kfree(entity->controls);