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>
27 #define UVC_CTRL_DATA_CURRENT 0
28 #define UVC_CTRL_DATA_BACKUP 1
29 #define UVC_CTRL_DATA_MIN 2
30 #define UVC_CTRL_DATA_MAX 3
31 #define UVC_CTRL_DATA_RES 4
32 #define UVC_CTRL_DATA_DEF 5
33 #define UVC_CTRL_DATA_LAST 6
35 /* ------------------------------------------------------------------------
39 static struct uvc_control_info uvc_ctrls[] = {
41 .entity = UVC_GUID_UVC_PROCESSING,
42 .selector = UVC_PU_BRIGHTNESS_CONTROL,
45 .flags = UVC_CTRL_FLAG_SET_CUR
46 | UVC_CTRL_FLAG_GET_RANGE
47 | UVC_CTRL_FLAG_RESTORE,
50 .entity = UVC_GUID_UVC_PROCESSING,
51 .selector = UVC_PU_CONTRAST_CONTROL,
54 .flags = UVC_CTRL_FLAG_SET_CUR
55 | UVC_CTRL_FLAG_GET_RANGE
56 | UVC_CTRL_FLAG_RESTORE,
59 .entity = UVC_GUID_UVC_PROCESSING,
60 .selector = UVC_PU_HUE_CONTROL,
63 .flags = UVC_CTRL_FLAG_SET_CUR
64 | UVC_CTRL_FLAG_GET_RANGE
65 | UVC_CTRL_FLAG_RESTORE
66 | UVC_CTRL_FLAG_AUTO_UPDATE,
69 .entity = UVC_GUID_UVC_PROCESSING,
70 .selector = UVC_PU_SATURATION_CONTROL,
73 .flags = UVC_CTRL_FLAG_SET_CUR
74 | UVC_CTRL_FLAG_GET_RANGE
75 | UVC_CTRL_FLAG_RESTORE,
78 .entity = UVC_GUID_UVC_PROCESSING,
79 .selector = UVC_PU_SHARPNESS_CONTROL,
82 .flags = UVC_CTRL_FLAG_SET_CUR
83 | UVC_CTRL_FLAG_GET_RANGE
84 | UVC_CTRL_FLAG_RESTORE,
87 .entity = UVC_GUID_UVC_PROCESSING,
88 .selector = UVC_PU_GAMMA_CONTROL,
91 .flags = UVC_CTRL_FLAG_SET_CUR
92 | UVC_CTRL_FLAG_GET_RANGE
93 | UVC_CTRL_FLAG_RESTORE,
96 .entity = UVC_GUID_UVC_PROCESSING,
97 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
100 .flags = UVC_CTRL_FLAG_SET_CUR
101 | UVC_CTRL_FLAG_GET_RANGE
102 | UVC_CTRL_FLAG_RESTORE
103 | UVC_CTRL_FLAG_AUTO_UPDATE,
106 .entity = UVC_GUID_UVC_PROCESSING,
107 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
110 .flags = UVC_CTRL_FLAG_SET_CUR
111 | UVC_CTRL_FLAG_GET_RANGE
112 | UVC_CTRL_FLAG_RESTORE
113 | UVC_CTRL_FLAG_AUTO_UPDATE,
116 .entity = UVC_GUID_UVC_PROCESSING,
117 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
120 .flags = UVC_CTRL_FLAG_SET_CUR
121 | UVC_CTRL_FLAG_GET_RANGE
122 | UVC_CTRL_FLAG_RESTORE,
125 .entity = UVC_GUID_UVC_PROCESSING,
126 .selector = UVC_PU_GAIN_CONTROL,
129 .flags = UVC_CTRL_FLAG_SET_CUR
130 | UVC_CTRL_FLAG_GET_RANGE
131 | UVC_CTRL_FLAG_RESTORE,
134 .entity = UVC_GUID_UVC_PROCESSING,
135 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
138 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
139 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
142 .entity = UVC_GUID_UVC_PROCESSING,
143 .selector = UVC_PU_HUE_AUTO_CONTROL,
146 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
147 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
150 .entity = UVC_GUID_UVC_PROCESSING,
151 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
154 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
155 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
158 .entity = UVC_GUID_UVC_PROCESSING,
159 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
162 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
163 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
166 .entity = UVC_GUID_UVC_PROCESSING,
167 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
170 .flags = UVC_CTRL_FLAG_SET_CUR
171 | UVC_CTRL_FLAG_GET_RANGE
172 | UVC_CTRL_FLAG_RESTORE,
175 .entity = UVC_GUID_UVC_PROCESSING,
176 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
179 .flags = UVC_CTRL_FLAG_SET_CUR
180 | UVC_CTRL_FLAG_GET_RANGE
181 | UVC_CTRL_FLAG_RESTORE,
184 .entity = UVC_GUID_UVC_PROCESSING,
185 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
188 .flags = UVC_CTRL_FLAG_GET_CUR,
191 .entity = UVC_GUID_UVC_PROCESSING,
192 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
195 .flags = UVC_CTRL_FLAG_GET_CUR,
198 .entity = UVC_GUID_UVC_CAMERA,
199 .selector = UVC_CT_SCANNING_MODE_CONTROL,
202 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
203 | UVC_CTRL_FLAG_RESTORE,
206 .entity = UVC_GUID_UVC_CAMERA,
207 .selector = UVC_CT_AE_MODE_CONTROL,
210 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
211 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
212 | UVC_CTRL_FLAG_RESTORE,
215 .entity = UVC_GUID_UVC_CAMERA,
216 .selector = UVC_CT_AE_PRIORITY_CONTROL,
219 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
220 | UVC_CTRL_FLAG_RESTORE,
223 .entity = UVC_GUID_UVC_CAMERA,
224 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
227 .flags = UVC_CTRL_FLAG_SET_CUR
228 | UVC_CTRL_FLAG_GET_RANGE
229 | UVC_CTRL_FLAG_RESTORE,
232 .entity = UVC_GUID_UVC_CAMERA,
233 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
236 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
239 .entity = UVC_GUID_UVC_CAMERA,
240 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
243 .flags = UVC_CTRL_FLAG_SET_CUR
244 | UVC_CTRL_FLAG_GET_RANGE
245 | UVC_CTRL_FLAG_RESTORE
246 | UVC_CTRL_FLAG_AUTO_UPDATE,
249 .entity = UVC_GUID_UVC_CAMERA,
250 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
253 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
254 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
255 | UVC_CTRL_FLAG_GET_DEF
256 | UVC_CTRL_FLAG_AUTO_UPDATE,
259 .entity = UVC_GUID_UVC_CAMERA,
260 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
263 .flags = UVC_CTRL_FLAG_SET_CUR
264 | UVC_CTRL_FLAG_GET_RANGE
265 | UVC_CTRL_FLAG_RESTORE
266 | UVC_CTRL_FLAG_AUTO_UPDATE,
269 .entity = UVC_GUID_UVC_CAMERA,
270 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
273 .flags = UVC_CTRL_FLAG_SET_CUR
274 | UVC_CTRL_FLAG_AUTO_UPDATE,
277 .entity = UVC_GUID_UVC_CAMERA,
278 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
281 .flags = UVC_CTRL_FLAG_SET_CUR
282 | UVC_CTRL_FLAG_GET_RANGE
283 | UVC_CTRL_FLAG_RESTORE
284 | UVC_CTRL_FLAG_AUTO_UPDATE,
287 .entity = UVC_GUID_UVC_CAMERA,
288 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
291 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
292 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
293 | UVC_CTRL_FLAG_GET_DEF
294 | UVC_CTRL_FLAG_AUTO_UPDATE,
297 .entity = UVC_GUID_UVC_CAMERA,
298 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
301 .flags = UVC_CTRL_FLAG_SET_CUR
302 | UVC_CTRL_FLAG_GET_RANGE
303 | UVC_CTRL_FLAG_RESTORE
304 | UVC_CTRL_FLAG_AUTO_UPDATE,
307 .entity = UVC_GUID_UVC_CAMERA,
308 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
311 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
312 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
313 | UVC_CTRL_FLAG_GET_DEF
314 | UVC_CTRL_FLAG_AUTO_UPDATE,
317 .entity = UVC_GUID_UVC_CAMERA,
318 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
321 .flags = UVC_CTRL_FLAG_SET_CUR
322 | UVC_CTRL_FLAG_GET_RANGE
323 | UVC_CTRL_FLAG_RESTORE
324 | UVC_CTRL_FLAG_AUTO_UPDATE,
327 .entity = UVC_GUID_UVC_CAMERA,
328 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
331 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
332 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
333 | UVC_CTRL_FLAG_GET_DEF
334 | UVC_CTRL_FLAG_AUTO_UPDATE,
337 .entity = UVC_GUID_UVC_CAMERA,
338 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
341 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
342 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
345 .entity = UVC_GUID_UVC_CAMERA,
346 .selector = UVC_CT_PRIVACY_CONTROL,
349 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
350 | UVC_CTRL_FLAG_RESTORE
351 | UVC_CTRL_FLAG_AUTO_UPDATE,
355 static struct uvc_menu_info power_line_frequency_controls[] = {
361 static struct uvc_menu_info exposure_auto_controls[] = {
363 { 1, "Manual Mode" },
364 { 4, "Shutter Priority Mode" },
365 { 8, "Aperture Priority Mode" },
368 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
369 __u8 query, const __u8 *data)
371 __s8 zoom = (__s8)data[0];
375 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
386 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
387 __s32 value, __u8 *data)
389 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
390 data[2] = min((int)abs(value), 0xff);
393 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
395 .id = V4L2_CID_BRIGHTNESS,
396 .name = "Brightness",
397 .entity = UVC_GUID_UVC_PROCESSING,
398 .selector = UVC_PU_BRIGHTNESS_CONTROL,
401 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
402 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
405 .id = V4L2_CID_CONTRAST,
407 .entity = UVC_GUID_UVC_PROCESSING,
408 .selector = UVC_PU_CONTRAST_CONTROL,
411 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
412 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
417 .entity = UVC_GUID_UVC_PROCESSING,
418 .selector = UVC_PU_HUE_CONTROL,
421 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
422 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
425 .id = V4L2_CID_SATURATION,
426 .name = "Saturation",
427 .entity = UVC_GUID_UVC_PROCESSING,
428 .selector = UVC_PU_SATURATION_CONTROL,
431 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
432 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
435 .id = V4L2_CID_SHARPNESS,
437 .entity = UVC_GUID_UVC_PROCESSING,
438 .selector = UVC_PU_SHARPNESS_CONTROL,
441 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
442 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
445 .id = V4L2_CID_GAMMA,
447 .entity = UVC_GUID_UVC_PROCESSING,
448 .selector = UVC_PU_GAMMA_CONTROL,
451 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
452 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
455 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
456 .name = "Backlight Compensation",
457 .entity = UVC_GUID_UVC_PROCESSING,
458 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
461 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
462 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
467 .entity = UVC_GUID_UVC_PROCESSING,
468 .selector = UVC_PU_GAIN_CONTROL,
471 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
472 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
475 .id = V4L2_CID_POWER_LINE_FREQUENCY,
476 .name = "Power Line Frequency",
477 .entity = UVC_GUID_UVC_PROCESSING,
478 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
481 .v4l2_type = V4L2_CTRL_TYPE_MENU,
482 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
483 .menu_info = power_line_frequency_controls,
484 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
487 .id = V4L2_CID_HUE_AUTO,
489 .entity = UVC_GUID_UVC_PROCESSING,
490 .selector = UVC_PU_HUE_AUTO_CONTROL,
493 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
494 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
497 .id = V4L2_CID_EXPOSURE_AUTO,
498 .name = "Exposure, Auto",
499 .entity = UVC_GUID_UVC_CAMERA,
500 .selector = UVC_CT_AE_MODE_CONTROL,
503 .v4l2_type = V4L2_CTRL_TYPE_MENU,
504 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
505 .menu_info = exposure_auto_controls,
506 .menu_count = ARRAY_SIZE(exposure_auto_controls),
509 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
510 .name = "Exposure, Auto Priority",
511 .entity = UVC_GUID_UVC_CAMERA,
512 .selector = UVC_CT_AE_PRIORITY_CONTROL,
515 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
516 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
519 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
520 .name = "Exposure (Absolute)",
521 .entity = UVC_GUID_UVC_CAMERA,
522 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
525 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
526 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
529 .id = V4L2_CID_AUTO_WHITE_BALANCE,
530 .name = "White Balance Temperature, Auto",
531 .entity = UVC_GUID_UVC_PROCESSING,
532 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
535 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
536 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
539 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
540 .name = "White Balance Temperature",
541 .entity = UVC_GUID_UVC_PROCESSING,
542 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
545 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
546 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
549 .id = V4L2_CID_AUTO_WHITE_BALANCE,
550 .name = "White Balance Component, Auto",
551 .entity = UVC_GUID_UVC_PROCESSING,
552 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
555 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
556 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
559 .id = V4L2_CID_BLUE_BALANCE,
560 .name = "White Balance Blue Component",
561 .entity = UVC_GUID_UVC_PROCESSING,
562 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
565 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
566 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
569 .id = V4L2_CID_RED_BALANCE,
570 .name = "White Balance Red Component",
571 .entity = UVC_GUID_UVC_PROCESSING,
572 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
575 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
576 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
579 .id = V4L2_CID_FOCUS_ABSOLUTE,
580 .name = "Focus (absolute)",
581 .entity = UVC_GUID_UVC_CAMERA,
582 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
585 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
586 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
589 .id = V4L2_CID_FOCUS_AUTO,
590 .name = "Focus, Auto",
591 .entity = UVC_GUID_UVC_CAMERA,
592 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
595 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
596 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
599 .id = V4L2_CID_IRIS_ABSOLUTE,
600 .name = "Iris, Absolute",
601 .entity = UVC_GUID_UVC_CAMERA,
602 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
605 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
606 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
609 .id = V4L2_CID_IRIS_RELATIVE,
610 .name = "Iris, Relative",
611 .entity = UVC_GUID_UVC_CAMERA,
612 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
615 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
616 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
619 .id = V4L2_CID_ZOOM_ABSOLUTE,
620 .name = "Zoom, Absolute",
621 .entity = UVC_GUID_UVC_CAMERA,
622 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
625 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
626 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
629 .id = V4L2_CID_ZOOM_CONTINUOUS,
630 .name = "Zoom, Continuous",
631 .entity = UVC_GUID_UVC_CAMERA,
632 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
635 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
636 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
637 .get = uvc_ctrl_get_zoom,
638 .set = uvc_ctrl_set_zoom,
641 .id = V4L2_CID_PAN_ABSOLUTE,
642 .name = "Pan (Absolute)",
643 .entity = UVC_GUID_UVC_CAMERA,
644 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
647 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
648 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
651 .id = V4L2_CID_TILT_ABSOLUTE,
652 .name = "Tilt (Absolute)",
653 .entity = UVC_GUID_UVC_CAMERA,
654 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
657 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
658 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
661 .id = V4L2_CID_PRIVACY,
663 .entity = UVC_GUID_UVC_CAMERA,
664 .selector = UVC_CT_PRIVACY_CONTROL,
667 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
668 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
672 /* ------------------------------------------------------------------------
676 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
678 return ctrl->uvc_data + id * ctrl->info.size;
681 static inline int uvc_test_bit(const __u8 *data, int bit)
683 return (data[bit >> 3] >> (bit & 7)) & 1;
686 static inline void uvc_clear_bit(__u8 *data, int bit)
688 data[bit >> 3] &= ~(1 << (bit & 7));
691 /* Extract the bit string specified by mapping->offset and mapping->size
692 * from the little-endian data stored at 'data' and return the result as
693 * a signed 32bit integer. Sign extension will be performed if the mapping
694 * references a signed data type.
696 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
697 __u8 query, const __u8 *data)
699 int bits = mapping->size;
700 int offset = mapping->offset;
706 mask = ((1LL << bits) - 1) << offset;
708 for (; bits > 0; data++) {
709 __u8 byte = *data & mask;
710 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
711 bits -= 8 - (offset > 0 ? offset : 0);
713 mask = (1 << bits) - 1;
716 /* Sign-extend the value if needed. */
717 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
718 value |= -(value & (1 << (mapping->size - 1)));
723 /* Set the bit string specified by mapping->offset and mapping->size
724 * in the little-endian data stored at 'data' to the value 'value'.
726 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
727 __s32 value, __u8 *data)
729 int bits = mapping->size;
730 int offset = mapping->offset;
733 /* According to the v4l2 spec, writing any value to a button control
734 * should result in the action belonging to the button control being
735 * triggered. UVC devices however want to see a 1 written -> override
738 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
744 for (; bits > 0; data++) {
745 mask = ((1LL << bits) - 1) << offset;
746 *data = (*data & ~mask) | ((value << offset) & mask);
747 value >>= offset ? offset : 8;
753 /* ------------------------------------------------------------------------
754 * Terminal and unit management
757 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
758 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
759 static const __u8 uvc_media_transport_input_guid[16] =
760 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
762 static int uvc_entity_match_guid(const struct uvc_entity *entity,
765 switch (UVC_ENTITY_TYPE(entity)) {
767 return memcmp(uvc_camera_guid, guid, 16) == 0;
769 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
770 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
772 case UVC_VC_PROCESSING_UNIT:
773 return memcmp(uvc_processing_guid, guid, 16) == 0;
775 case UVC_VC_EXTENSION_UNIT:
776 return memcmp(entity->extension.guidExtensionCode,
784 /* ------------------------------------------------------------------------
788 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
789 struct uvc_control_mapping **mapping, struct uvc_control **control,
792 struct uvc_control *ctrl;
793 struct uvc_control_mapping *map;
799 for (i = 0; i < entity->ncontrols; ++i) {
800 ctrl = &entity->controls[i];
801 if (!ctrl->initialized)
804 list_for_each_entry(map, &ctrl->info.mappings, list) {
805 if ((map->id == v4l2_id) && !next) {
811 if ((*mapping == NULL || (*mapping)->id > map->id) &&
812 (map->id > v4l2_id) && next) {
820 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
821 __u32 v4l2_id, struct uvc_control_mapping **mapping)
823 struct uvc_control *ctrl = NULL;
824 struct uvc_entity *entity;
825 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
829 /* Mask the query flags. */
830 v4l2_id &= V4L2_CTRL_ID_MASK;
832 /* Find the control. */
833 list_for_each_entry(entity, &chain->entities, chain) {
834 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
839 if (ctrl == NULL && !next)
840 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
846 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
847 struct uvc_control *ctrl)
851 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
852 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
853 chain->dev->intfnum, ctrl->info.selector,
854 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
860 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
861 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
862 chain->dev->intfnum, ctrl->info.selector,
863 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
868 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
869 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
870 chain->dev->intfnum, ctrl->info.selector,
871 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
876 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
877 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
878 chain->dev->intfnum, ctrl->info.selector,
879 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
882 if (UVC_ENTITY_TYPE(ctrl->entity) !=
883 UVC_VC_EXTENSION_UNIT)
886 /* GET_RES is mandatory for XU controls, but some
887 * cameras still choke on it. Ignore errors and set the
888 * resolution value to zero.
890 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
891 "UVC non compliance - GET_RES failed on "
892 "an XU control. Enabling workaround.\n");
893 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
902 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
903 struct v4l2_queryctrl *v4l2_ctrl)
905 struct uvc_control *ctrl;
906 struct uvc_control_mapping *mapping;
907 struct uvc_menu_info *menu;
911 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
915 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
921 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
922 v4l2_ctrl->id = mapping->id;
923 v4l2_ctrl->type = mapping->v4l2_type;
924 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
925 v4l2_ctrl->flags = 0;
927 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
928 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
929 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
930 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
933 ret = uvc_ctrl_populate_cache(chain, ctrl);
938 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
939 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
940 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
943 switch (mapping->v4l2_type) {
944 case V4L2_CTRL_TYPE_MENU:
945 v4l2_ctrl->minimum = 0;
946 v4l2_ctrl->maximum = mapping->menu_count - 1;
949 menu = mapping->menu_info;
950 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
951 if (menu->value == v4l2_ctrl->default_value) {
952 v4l2_ctrl->default_value = i;
959 case V4L2_CTRL_TYPE_BOOLEAN:
960 v4l2_ctrl->minimum = 0;
961 v4l2_ctrl->maximum = 1;
965 case V4L2_CTRL_TYPE_BUTTON:
966 v4l2_ctrl->minimum = 0;
967 v4l2_ctrl->maximum = 0;
975 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
976 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
977 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
979 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
980 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
981 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
983 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
984 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
985 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
988 mutex_unlock(&chain->ctrl_mutex);
993 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
994 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
995 * must be grouped (for instance the Red Balance, Blue Balance and Do White
996 * Balance V4L2 controls use the White Balance Component UVC control) or
997 * otherwise translated. The approach we take here is to use a translation
998 * table for the controls that can be mapped directly, and handle the others
1001 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1002 struct v4l2_querymenu *query_menu)
1004 struct uvc_menu_info *menu_info;
1005 struct uvc_control_mapping *mapping;
1006 struct uvc_control *ctrl;
1007 u32 index = query_menu->index;
1008 u32 id = query_menu->id;
1011 memset(query_menu, 0, sizeof(*query_menu));
1012 query_menu->id = id;
1013 query_menu->index = index;
1015 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1017 return -ERESTARTSYS;
1019 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1020 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1025 if (query_menu->index >= mapping->menu_count) {
1030 menu_info = &mapping->menu_info[query_menu->index];
1032 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1033 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1036 if (!ctrl->cached) {
1037 ret = uvc_ctrl_populate_cache(chain, ctrl);
1042 bitmap = mapping->get(mapping, UVC_GET_RES,
1043 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1044 if (!(bitmap & menu_info->value)) {
1050 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
1053 mutex_unlock(&chain->ctrl_mutex);
1058 /* --------------------------------------------------------------------------
1059 * Control transactions
1061 * To make extended set operations as atomic as the hardware allows, controls
1062 * are handled using begin/commit/rollback operations.
1064 * At the beginning of a set request, uvc_ctrl_begin should be called to
1065 * initialize the request. This function acquires the control lock.
1067 * When setting a control, the new value is stored in the control data field
1068 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1069 * later processing. If the UVC and V4L2 control sizes differ, the current
1070 * value is loaded from the hardware before storing the new value in the data
1073 * After processing all controls in the transaction, uvc_ctrl_commit or
1074 * uvc_ctrl_rollback must be called to apply the pending changes to the
1075 * hardware or revert them. When applying changes, all controls marked as
1076 * dirty will be modified in the UVC device, and the dirty flag will be
1077 * cleared. When reverting controls, the control data field
1078 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1079 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1082 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1084 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1087 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1088 struct uvc_entity *entity, int rollback)
1090 struct uvc_control *ctrl;
1097 for (i = 0; i < entity->ncontrols; ++i) {
1098 ctrl = &entity->controls[i];
1099 if (!ctrl->initialized)
1102 /* Reset the loaded flag for auto-update controls that were
1103 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1104 * uvc_ctrl_get from using the cached value.
1106 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE)
1113 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1114 dev->intfnum, ctrl->info.selector,
1115 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1120 if (rollback || ret < 0)
1121 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1122 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1134 int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
1136 struct uvc_entity *entity;
1139 /* Find the control. */
1140 list_for_each_entry(entity, &chain->entities, chain) {
1141 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1147 mutex_unlock(&chain->ctrl_mutex);
1151 int uvc_ctrl_get(struct uvc_video_chain *chain,
1152 struct v4l2_ext_control *xctrl)
1154 struct uvc_control *ctrl;
1155 struct uvc_control_mapping *mapping;
1156 struct uvc_menu_info *menu;
1160 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1161 if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1164 if (!ctrl->loaded) {
1165 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1166 chain->dev->intfnum, ctrl->info.selector,
1167 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1175 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1176 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1178 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1179 menu = mapping->menu_info;
1180 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1181 if (menu->value == xctrl->value) {
1191 int uvc_ctrl_set(struct uvc_video_chain *chain,
1192 struct v4l2_ext_control *xctrl)
1194 struct uvc_control *ctrl;
1195 struct uvc_control_mapping *mapping;
1202 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1203 if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
1206 /* Clamp out of range values. */
1207 switch (mapping->v4l2_type) {
1208 case V4L2_CTRL_TYPE_INTEGER:
1209 if (!ctrl->cached) {
1210 ret = uvc_ctrl_populate_cache(chain, ctrl);
1215 min = mapping->get(mapping, UVC_GET_MIN,
1216 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1217 max = mapping->get(mapping, UVC_GET_MAX,
1218 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1219 step = mapping->get(mapping, UVC_GET_RES,
1220 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1224 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1225 xctrl->value = clamp(xctrl->value, min, max);
1226 value = xctrl->value;
1229 case V4L2_CTRL_TYPE_BOOLEAN:
1230 xctrl->value = clamp(xctrl->value, 0, 1);
1231 value = xctrl->value;
1234 case V4L2_CTRL_TYPE_MENU:
1235 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1237 value = mapping->menu_info[xctrl->value].value;
1239 /* Valid menu indices are reported by the GET_RES request for
1240 * UVC controls that support it.
1242 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1243 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1244 if (!ctrl->cached) {
1245 ret = uvc_ctrl_populate_cache(chain, ctrl);
1250 step = mapping->get(mapping, UVC_GET_RES,
1251 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1252 if (!(step & value))
1259 value = xctrl->value;
1263 /* If the mapping doesn't span the whole UVC control, the current value
1264 * needs to be loaded from the device to perform the read-modify-write
1267 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1268 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1269 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1270 0, ctrl->info.size);
1272 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1273 ctrl->entity->id, chain->dev->intfnum,
1274 ctrl->info.selector,
1275 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1284 /* Backup the current value in case we need to rollback later. */
1286 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1287 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1291 mapping->set(mapping, value,
1292 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1299 /* --------------------------------------------------------------------------
1303 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1304 const struct uvc_control *ctrl, struct uvc_control_info *info)
1306 struct uvc_ctrl_fixup {
1307 struct usb_device_id id;
1313 static const struct uvc_ctrl_fixup fixups[] = {
1314 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1315 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1316 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1317 UVC_CTRL_FLAG_AUTO_UPDATE },
1318 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1319 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1320 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1321 UVC_CTRL_FLAG_AUTO_UPDATE },
1322 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1323 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1324 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1325 UVC_CTRL_FLAG_AUTO_UPDATE },
1330 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1331 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1334 if (fixups[i].entity == ctrl->entity->id &&
1335 fixups[i].selector == info->selector) {
1336 info->flags = fixups[i].flags;
1343 * Query control information (size and flags) for XU controls.
1345 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1346 const struct uvc_control *ctrl, struct uvc_control_info *info)
1351 data = kmalloc(2, GFP_KERNEL);
1355 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1356 sizeof(info->entity));
1357 info->index = ctrl->index;
1358 info->selector = ctrl->index + 1;
1360 /* Query and verify the control length (GET_LEN) */
1361 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1362 info->selector, data, 2);
1364 uvc_trace(UVC_TRACE_CONTROL,
1365 "GET_LEN failed on control %pUl/%u (%d).\n",
1366 info->entity, info->selector, ret);
1370 info->size = le16_to_cpup((__le16 *)data);
1372 /* Query the control information (GET_INFO) */
1373 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1374 info->selector, data, 1);
1376 uvc_trace(UVC_TRACE_CONTROL,
1377 "GET_INFO failed on control %pUl/%u (%d).\n",
1378 info->entity, info->selector, ret);
1382 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1383 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1384 | (data[0] & UVC_CONTROL_CAP_GET ?
1385 UVC_CTRL_FLAG_GET_CUR : 0)
1386 | (data[0] & UVC_CONTROL_CAP_SET ?
1387 UVC_CTRL_FLAG_SET_CUR : 0)
1388 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1389 UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1391 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1393 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1394 "flags { get %u set %u auto %u }.\n",
1395 info->entity, info->selector, info->size,
1396 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1397 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1398 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1405 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1406 const struct uvc_control_info *info);
1408 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1409 struct uvc_control *ctrl)
1411 struct uvc_control_info info;
1414 if (ctrl->initialized)
1417 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1421 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1423 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1424 "%pUl/%u on device %s entity %u\n", info.entity,
1425 info.selector, dev->udev->devpath, ctrl->entity->id);
1430 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1431 struct uvc_xu_control_query *xqry)
1433 struct uvc_entity *entity;
1434 struct uvc_control *ctrl;
1435 unsigned int i, found = 0;
1441 /* Find the extension unit. */
1442 list_for_each_entry(entity, &chain->entities, chain) {
1443 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1444 entity->id == xqry->unit)
1448 if (entity->id != xqry->unit) {
1449 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1454 /* Find the control and perform delayed initialization if needed. */
1455 for (i = 0; i < entity->ncontrols; ++i) {
1456 ctrl = &entity->controls[i];
1457 if (ctrl->index == xqry->selector - 1) {
1464 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1465 entity->extension.guidExtensionCode, xqry->selector);
1469 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1470 return -ERESTARTSYS;
1472 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1478 /* Validate the required buffer size and flags for the request */
1480 size = ctrl->info.size;
1482 switch (xqry->query) {
1484 reqflags = UVC_CTRL_FLAG_GET_CUR;
1487 reqflags = UVC_CTRL_FLAG_GET_MIN;
1490 reqflags = UVC_CTRL_FLAG_GET_MAX;
1493 reqflags = UVC_CTRL_FLAG_GET_DEF;
1496 reqflags = UVC_CTRL_FLAG_GET_RES;
1499 reqflags = UVC_CTRL_FLAG_SET_CUR;
1512 if (size != xqry->size) {
1517 if (reqflags && !(ctrl->info.flags & reqflags)) {
1522 data = kmalloc(size, GFP_KERNEL);
1528 if (xqry->query == UVC_SET_CUR &&
1529 copy_from_user(data, xqry->data, size)) {
1534 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1535 chain->dev->intfnum, xqry->selector, data, size);
1539 if (xqry->query != UVC_SET_CUR &&
1540 copy_to_user(xqry->data, data, size))
1544 mutex_unlock(&chain->ctrl_mutex);
1548 /* --------------------------------------------------------------------------
1553 * Restore control values after resume, skipping controls that haven't been
1557 * - Don't restore modified controls that are back to their default value.
1558 * - Handle restore order (Auto-Exposure Mode should be restored before
1561 int uvc_ctrl_resume_device(struct uvc_device *dev)
1563 struct uvc_control *ctrl;
1564 struct uvc_entity *entity;
1568 /* Walk the entities list and restore controls when possible. */
1569 list_for_each_entry(entity, &dev->entities, list) {
1571 for (i = 0; i < entity->ncontrols; ++i) {
1572 ctrl = &entity->controls[i];
1574 if (!ctrl->initialized || !ctrl->modified ||
1575 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1578 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1579 ctrl->info.entity, ctrl->info.index,
1580 ctrl->info.selector);
1584 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1592 /* --------------------------------------------------------------------------
1593 * Control and mapping handling
1597 * Add control information to a given control.
1599 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1600 const struct uvc_control_info *info)
1604 memcpy(&ctrl->info, info, sizeof(*info));
1605 INIT_LIST_HEAD(&ctrl->info.mappings);
1607 /* Allocate an array to save control values (cur, def, max, etc.) */
1608 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1610 if (ctrl->uvc_data == NULL) {
1615 ctrl->initialized = 1;
1617 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1618 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
1619 dev->udev->devpath, ctrl->entity->id);
1623 kfree(ctrl->uvc_data);
1628 * Add a control mapping to a given control.
1630 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1631 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1633 struct uvc_control_mapping *map;
1636 /* Most mappings come from static kernel data and need to be duplicated.
1637 * Mappings that come from userspace will be unnecessarily duplicated,
1638 * this could be optimized.
1640 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1644 size = sizeof(*mapping->menu_info) * mapping->menu_count;
1645 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1646 if (map->menu_info == NULL) {
1651 if (map->get == NULL)
1652 map->get = uvc_get_le_value;
1653 if (map->set == NULL)
1654 map->set = uvc_set_le_value;
1656 map->ctrl = &ctrl->info;
1657 list_add_tail(&map->list, &ctrl->info.mappings);
1658 uvc_trace(UVC_TRACE_CONTROL,
1659 "Adding mapping '%s' to control %pUl/%u.\n",
1660 map->name, ctrl->info.entity, ctrl->info.selector);
1665 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1666 const struct uvc_control_mapping *mapping)
1668 struct uvc_device *dev = chain->dev;
1669 struct uvc_control_mapping *map;
1670 struct uvc_entity *entity;
1671 struct uvc_control *ctrl;
1675 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1676 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1677 "id 0x%08x is invalid.\n", mapping->name,
1682 /* Search for the matching (GUID/CS) control on the current chain */
1683 list_for_each_entry(entity, &chain->entities, chain) {
1686 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1687 !uvc_entity_match_guid(entity, mapping->entity))
1690 for (i = 0; i < entity->ncontrols; ++i) {
1691 ctrl = &entity->controls[i];
1692 if (ctrl->index == mapping->selector - 1) {
1704 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1705 return -ERESTARTSYS;
1707 /* Perform delayed initialization of XU controls */
1708 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
1714 list_for_each_entry(map, &ctrl->info.mappings, list) {
1715 if (mapping->id == map->id) {
1716 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
1717 "control id 0x%08x already exists.\n",
1718 mapping->name, mapping->id);
1724 /* Prevent excess memory consumption */
1725 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
1726 atomic_dec(&dev->nmappings);
1727 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
1728 "mappings count (%u) exceeded.\n", mapping->name,
1729 UVC_MAX_CONTROL_MAPPINGS);
1734 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1736 atomic_dec(&dev->nmappings);
1739 mutex_unlock(&chain->ctrl_mutex);
1744 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1745 * are currently the ones that crash the camera or unconditionally return an
1746 * error when queried.
1748 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
1749 struct uvc_entity *entity)
1751 struct uvc_ctrl_blacklist {
1752 struct usb_device_id id;
1756 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1757 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1758 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1759 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1761 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
1762 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
1765 const struct uvc_ctrl_blacklist *blacklist;
1771 switch (UVC_ENTITY_TYPE(entity)) {
1772 case UVC_VC_PROCESSING_UNIT:
1773 blacklist = processing_blacklist;
1774 count = ARRAY_SIZE(processing_blacklist);
1775 controls = entity->processing.bmControls;
1776 size = entity->processing.bControlSize;
1779 case UVC_ITT_CAMERA:
1780 blacklist = camera_blacklist;
1781 count = ARRAY_SIZE(camera_blacklist);
1782 controls = entity->camera.bmControls;
1783 size = entity->camera.bControlSize;
1790 for (i = 0; i < count; ++i) {
1791 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1794 if (blacklist[i].index >= 8 * size ||
1795 !uvc_test_bit(controls, blacklist[i].index))
1798 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1799 "removing it.\n", entity->id, blacklist[i].index);
1801 uvc_clear_bit(controls, blacklist[i].index);
1806 * Add control information and hardcoded stock control mappings to the given
1809 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
1811 const struct uvc_control_info *info = uvc_ctrls;
1812 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
1813 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1814 const struct uvc_control_mapping *mend =
1815 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1817 /* XU controls initialization requires querying the device for control
1818 * information. As some buggy UVC devices will crash when queried
1819 * repeatedly in a tight loop, delay XU controls initialization until
1822 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
1825 for (; info < iend; ++info) {
1826 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
1827 ctrl->index == info->index) {
1828 uvc_ctrl_add_info(dev, ctrl, info);
1833 if (!ctrl->initialized)
1836 for (; mapping < mend; ++mapping) {
1837 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
1838 ctrl->info.selector == mapping->selector)
1839 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1844 * Initialize device controls.
1846 int uvc_ctrl_init_device(struct uvc_device *dev)
1848 struct uvc_entity *entity;
1851 /* Walk the entities list and instantiate controls */
1852 list_for_each_entry(entity, &dev->entities, list) {
1853 struct uvc_control *ctrl;
1854 unsigned int bControlSize = 0, ncontrols = 0;
1855 __u8 *bmControls = NULL;
1857 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1858 bmControls = entity->extension.bmControls;
1859 bControlSize = entity->extension.bControlSize;
1860 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1861 bmControls = entity->processing.bmControls;
1862 bControlSize = entity->processing.bControlSize;
1863 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1864 bmControls = entity->camera.bmControls;
1865 bControlSize = entity->camera.bControlSize;
1868 /* Remove bogus/blacklisted controls */
1869 uvc_ctrl_prune_entity(dev, entity);
1871 /* Count supported controls and allocate the controls array */
1872 for (i = 0; i < bControlSize; ++i)
1873 ncontrols += hweight8(bmControls[i]);
1877 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
1879 if (entity->controls == NULL)
1881 entity->ncontrols = ncontrols;
1883 /* Initialize all supported controls */
1884 ctrl = entity->controls;
1885 for (i = 0; i < bControlSize * 8; ++i) {
1886 if (uvc_test_bit(bmControls, i) == 0)
1889 ctrl->entity = entity;
1892 uvc_ctrl_init_ctrl(dev, ctrl);
1901 * Cleanup device controls.
1903 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
1904 struct uvc_control *ctrl)
1906 struct uvc_control_mapping *mapping, *nm;
1908 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
1909 list_del(&mapping->list);
1910 kfree(mapping->menu_info);
1915 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1917 struct uvc_entity *entity;
1920 /* Free controls and control mappings for all entities. */
1921 list_for_each_entry(entity, &dev->entities, list) {
1922 for (i = 0; i < entity->ncontrols; ++i) {
1923 struct uvc_control *ctrl = &entity->controls[i];
1925 if (!ctrl->initialized)
1928 uvc_ctrl_cleanup_mappings(dev, ctrl);
1929 kfree(ctrl->uvc_data);
1932 kfree(entity->controls);