2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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/uaccess.h>
18 #include <linux/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
21 #include <linux/wait.h>
22 #include <asm/atomic.h>
26 #define UVC_CTRL_NDATA 2
27 #define UVC_CTRL_DATA_CURRENT 0
28 #define UVC_CTRL_DATA_BACKUP 1
30 /* ------------------------------------------------------------------------
34 static struct uvc_control_info uvc_ctrls[] = {
36 .entity = UVC_GUID_UVC_PROCESSING,
37 .selector = UVC_PU_BRIGHTNESS_CONTROL,
40 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
41 | UVC_CONTROL_RESTORE,
44 .entity = UVC_GUID_UVC_PROCESSING,
45 .selector = UVC_PU_CONTRAST_CONTROL,
48 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
49 | UVC_CONTROL_RESTORE,
52 .entity = UVC_GUID_UVC_PROCESSING,
53 .selector = UVC_PU_HUE_CONTROL,
56 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
57 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
60 .entity = UVC_GUID_UVC_PROCESSING,
61 .selector = UVC_PU_SATURATION_CONTROL,
64 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
65 | UVC_CONTROL_RESTORE,
68 .entity = UVC_GUID_UVC_PROCESSING,
69 .selector = UVC_PU_SHARPNESS_CONTROL,
72 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
73 | UVC_CONTROL_RESTORE,
76 .entity = UVC_GUID_UVC_PROCESSING,
77 .selector = UVC_PU_GAMMA_CONTROL,
80 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
81 | UVC_CONTROL_RESTORE,
84 .entity = UVC_GUID_UVC_PROCESSING,
85 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
88 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
89 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
92 .entity = UVC_GUID_UVC_PROCESSING,
93 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
96 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
97 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
100 .entity = UVC_GUID_UVC_PROCESSING,
101 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
104 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
105 | UVC_CONTROL_RESTORE,
108 .entity = UVC_GUID_UVC_PROCESSING,
109 .selector = UVC_PU_GAIN_CONTROL,
112 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
113 | UVC_CONTROL_RESTORE,
116 .entity = UVC_GUID_UVC_PROCESSING,
117 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
120 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
121 | UVC_CONTROL_RESTORE,
124 .entity = UVC_GUID_UVC_PROCESSING,
125 .selector = UVC_PU_HUE_AUTO_CONTROL,
128 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
129 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
132 .entity = UVC_GUID_UVC_PROCESSING,
133 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
136 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
137 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
140 .entity = UVC_GUID_UVC_PROCESSING,
141 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
144 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
145 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
148 .entity = UVC_GUID_UVC_PROCESSING,
149 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
152 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
153 | UVC_CONTROL_RESTORE,
156 .entity = UVC_GUID_UVC_PROCESSING,
157 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
160 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
161 | UVC_CONTROL_RESTORE,
164 .entity = UVC_GUID_UVC_PROCESSING,
165 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
168 .flags = UVC_CONTROL_GET_CUR,
171 .entity = UVC_GUID_UVC_PROCESSING,
172 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
175 .flags = UVC_CONTROL_GET_CUR,
178 .entity = UVC_GUID_UVC_CAMERA,
179 .selector = UVC_CT_SCANNING_MODE_CONTROL,
182 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
183 | UVC_CONTROL_RESTORE,
186 .entity = UVC_GUID_UVC_CAMERA,
187 .selector = UVC_CT_AE_MODE_CONTROL,
190 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
191 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
192 | UVC_CONTROL_RESTORE,
195 .entity = UVC_GUID_UVC_CAMERA,
196 .selector = UVC_CT_AE_PRIORITY_CONTROL,
199 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
200 | UVC_CONTROL_RESTORE,
203 .entity = UVC_GUID_UVC_CAMERA,
204 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
207 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
208 | UVC_CONTROL_RESTORE,
211 .entity = UVC_GUID_UVC_CAMERA,
212 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
215 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
216 | UVC_CONTROL_RESTORE,
219 .entity = UVC_GUID_UVC_CAMERA,
220 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
223 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
224 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
227 .entity = UVC_GUID_UVC_CAMERA,
228 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
231 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
232 | UVC_CONTROL_AUTO_UPDATE,
235 .entity = UVC_GUID_UVC_CAMERA,
236 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
239 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
240 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
243 .entity = UVC_GUID_UVC_CAMERA,
244 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
247 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
248 | UVC_CONTROL_AUTO_UPDATE,
251 .entity = UVC_GUID_UVC_CAMERA,
252 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
255 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
256 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
259 .entity = UVC_GUID_UVC_CAMERA,
260 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
263 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
264 | UVC_CONTROL_AUTO_UPDATE,
267 .entity = UVC_GUID_UVC_CAMERA,
268 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
271 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
272 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
275 .entity = UVC_GUID_UVC_CAMERA,
276 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
279 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
280 | UVC_CONTROL_AUTO_UPDATE,
283 .entity = UVC_GUID_UVC_CAMERA,
284 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
287 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
288 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
291 .entity = UVC_GUID_UVC_CAMERA,
292 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
295 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
296 | UVC_CONTROL_AUTO_UPDATE,
299 .entity = UVC_GUID_UVC_CAMERA,
300 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
303 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
304 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
307 .entity = UVC_GUID_UVC_CAMERA,
308 .selector = UVC_CT_PRIVACY_CONTROL,
311 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
312 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
316 static struct uvc_menu_info power_line_frequency_controls[] = {
322 static struct uvc_menu_info exposure_auto_controls[] = {
324 { 1, "Manual Mode" },
325 { 4, "Shutter Priority Mode" },
326 { 8, "Aperture Priority Mode" },
329 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
330 __u8 query, const __u8 *data)
332 __s8 zoom = (__s8)data[0];
336 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
347 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
348 __s32 value, __u8 *data)
350 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
351 data[2] = min((int)abs(value), 0xff);
354 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
356 .id = V4L2_CID_BRIGHTNESS,
357 .name = "Brightness",
358 .entity = UVC_GUID_UVC_PROCESSING,
359 .selector = UVC_PU_BRIGHTNESS_CONTROL,
362 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
363 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
366 .id = V4L2_CID_CONTRAST,
368 .entity = UVC_GUID_UVC_PROCESSING,
369 .selector = UVC_PU_CONTRAST_CONTROL,
372 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
373 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
378 .entity = UVC_GUID_UVC_PROCESSING,
379 .selector = UVC_PU_HUE_CONTROL,
382 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
383 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
386 .id = V4L2_CID_SATURATION,
387 .name = "Saturation",
388 .entity = UVC_GUID_UVC_PROCESSING,
389 .selector = UVC_PU_SATURATION_CONTROL,
392 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
393 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
396 .id = V4L2_CID_SHARPNESS,
398 .entity = UVC_GUID_UVC_PROCESSING,
399 .selector = UVC_PU_SHARPNESS_CONTROL,
402 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
403 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
406 .id = V4L2_CID_GAMMA,
408 .entity = UVC_GUID_UVC_PROCESSING,
409 .selector = UVC_PU_GAMMA_CONTROL,
412 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
413 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
416 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
417 .name = "Backlight Compensation",
418 .entity = UVC_GUID_UVC_PROCESSING,
419 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
422 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
423 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
428 .entity = UVC_GUID_UVC_PROCESSING,
429 .selector = UVC_PU_GAIN_CONTROL,
432 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
433 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
436 .id = V4L2_CID_POWER_LINE_FREQUENCY,
437 .name = "Power Line Frequency",
438 .entity = UVC_GUID_UVC_PROCESSING,
439 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
442 .v4l2_type = V4L2_CTRL_TYPE_MENU,
443 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
444 .menu_info = power_line_frequency_controls,
445 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
448 .id = V4L2_CID_HUE_AUTO,
450 .entity = UVC_GUID_UVC_PROCESSING,
451 .selector = UVC_PU_HUE_AUTO_CONTROL,
454 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
455 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
458 .id = V4L2_CID_EXPOSURE_AUTO,
459 .name = "Exposure, Auto",
460 .entity = UVC_GUID_UVC_CAMERA,
461 .selector = UVC_CT_AE_MODE_CONTROL,
464 .v4l2_type = V4L2_CTRL_TYPE_MENU,
465 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
466 .menu_info = exposure_auto_controls,
467 .menu_count = ARRAY_SIZE(exposure_auto_controls),
470 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
471 .name = "Exposure, Auto Priority",
472 .entity = UVC_GUID_UVC_CAMERA,
473 .selector = UVC_CT_AE_PRIORITY_CONTROL,
476 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
477 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
480 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
481 .name = "Exposure (Absolute)",
482 .entity = UVC_GUID_UVC_CAMERA,
483 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
486 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
487 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
490 .id = V4L2_CID_AUTO_WHITE_BALANCE,
491 .name = "White Balance Temperature, Auto",
492 .entity = UVC_GUID_UVC_PROCESSING,
493 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
496 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
497 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
500 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
501 .name = "White Balance Temperature",
502 .entity = UVC_GUID_UVC_PROCESSING,
503 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
506 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
507 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
510 .id = V4L2_CID_AUTO_WHITE_BALANCE,
511 .name = "White Balance Component, Auto",
512 .entity = UVC_GUID_UVC_PROCESSING,
513 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
516 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
517 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
520 .id = V4L2_CID_BLUE_BALANCE,
521 .name = "White Balance Blue Component",
522 .entity = UVC_GUID_UVC_PROCESSING,
523 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
526 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
527 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
530 .id = V4L2_CID_RED_BALANCE,
531 .name = "White Balance Red Component",
532 .entity = UVC_GUID_UVC_PROCESSING,
533 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
536 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
537 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
540 .id = V4L2_CID_FOCUS_ABSOLUTE,
541 .name = "Focus (absolute)",
542 .entity = UVC_GUID_UVC_CAMERA,
543 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
546 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
547 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
550 .id = V4L2_CID_FOCUS_AUTO,
551 .name = "Focus, Auto",
552 .entity = UVC_GUID_UVC_CAMERA,
553 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
556 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
557 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
560 .id = V4L2_CID_ZOOM_ABSOLUTE,
561 .name = "Zoom, Absolute",
562 .entity = UVC_GUID_UVC_CAMERA,
563 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
566 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
567 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
570 .id = V4L2_CID_ZOOM_CONTINUOUS,
571 .name = "Zoom, Continuous",
572 .entity = UVC_GUID_UVC_CAMERA,
573 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
576 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
577 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
578 .get = uvc_ctrl_get_zoom,
579 .set = uvc_ctrl_set_zoom,
582 .id = V4L2_CID_PRIVACY,
584 .entity = UVC_GUID_UVC_CAMERA,
585 .selector = UVC_CT_PRIVACY_CONTROL,
588 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
589 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
593 /* ------------------------------------------------------------------------
597 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
599 return ctrl->data + id * ctrl->info->size;
602 static inline int uvc_test_bit(const __u8 *data, int bit)
604 return (data[bit >> 3] >> (bit & 7)) & 1;
607 static inline void uvc_clear_bit(__u8 *data, int bit)
609 data[bit >> 3] &= ~(1 << (bit & 7));
612 /* Extract the bit string specified by mapping->offset and mapping->size
613 * from the little-endian data stored at 'data' and return the result as
614 * a signed 32bit integer. Sign extension will be performed if the mapping
615 * references a signed data type.
617 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
618 __u8 query, const __u8 *data)
620 int bits = mapping->size;
621 int offset = mapping->offset;
627 mask = ((1LL << bits) - 1) << offset;
629 for (; bits > 0; data++) {
630 __u8 byte = *data & mask;
631 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
632 bits -= 8 - (offset > 0 ? offset : 0);
634 mask = (1 << bits) - 1;
637 /* Sign-extend the value if needed. */
638 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
639 value |= -(value & (1 << (mapping->size - 1)));
644 /* Set the bit string specified by mapping->offset and mapping->size
645 * in the little-endian data stored at 'data' to the value 'value'.
647 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
648 __s32 value, __u8 *data)
650 int bits = mapping->size;
651 int offset = mapping->offset;
657 for (; bits > 0; data++) {
658 mask = ((1LL << bits) - 1) << offset;
659 *data = (*data & ~mask) | ((value << offset) & mask);
660 value >>= offset ? offset : 8;
666 /* ------------------------------------------------------------------------
667 * Terminal and unit management
670 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
671 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
672 static const __u8 uvc_media_transport_input_guid[16] =
673 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
675 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
677 switch (UVC_ENTITY_TYPE(entity)) {
679 return memcmp(uvc_camera_guid, guid, 16) == 0;
681 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
682 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
684 case UVC_VC_PROCESSING_UNIT:
685 return memcmp(uvc_processing_guid, guid, 16) == 0;
687 case UVC_VC_EXTENSION_UNIT:
688 return memcmp(entity->extension.guidExtensionCode,
696 /* ------------------------------------------------------------------------
700 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
701 struct uvc_control_mapping **mapping, struct uvc_control **control,
704 struct uvc_control *ctrl;
705 struct uvc_control_mapping *map;
711 for (i = 0; i < entity->ncontrols; ++i) {
712 ctrl = &entity->controls[i];
713 if (ctrl->info == NULL)
716 list_for_each_entry(map, &ctrl->info->mappings, list) {
717 if ((map->id == v4l2_id) && !next) {
723 if ((*mapping == NULL || (*mapping)->id > map->id) &&
724 (map->id > v4l2_id) && next) {
732 struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
733 __u32 v4l2_id, struct uvc_control_mapping **mapping)
735 struct uvc_control *ctrl = NULL;
736 struct uvc_entity *entity;
737 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
741 /* Mask the query flags. */
742 v4l2_id &= V4L2_CTRL_ID_MASK;
744 /* Find the control. */
745 list_for_each_entry(entity, &chain->entities, chain) {
746 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
751 if (ctrl == NULL && !next)
752 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
758 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
759 struct v4l2_queryctrl *v4l2_ctrl)
761 struct uvc_control *ctrl;
762 struct uvc_control_mapping *mapping;
763 struct uvc_menu_info *menu;
768 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
772 data = kmalloc(ctrl->info->size, GFP_KERNEL);
776 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
777 v4l2_ctrl->id = mapping->id;
778 v4l2_ctrl->type = mapping->v4l2_type;
779 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
780 v4l2_ctrl->flags = 0;
782 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
783 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
785 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
786 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
787 chain->dev->intfnum, ctrl->info->selector,
788 data, ctrl->info->size);
791 v4l2_ctrl->default_value =
792 mapping->get(mapping, UVC_GET_DEF, data);
795 switch (mapping->v4l2_type) {
796 case V4L2_CTRL_TYPE_MENU:
797 v4l2_ctrl->minimum = 0;
798 v4l2_ctrl->maximum = mapping->menu_count - 1;
801 menu = mapping->menu_info;
802 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
803 if (menu->value == v4l2_ctrl->default_value) {
804 v4l2_ctrl->default_value = i;
812 case V4L2_CTRL_TYPE_BOOLEAN:
813 v4l2_ctrl->minimum = 0;
814 v4l2_ctrl->maximum = 1;
819 case V4L2_CTRL_TYPE_BUTTON:
820 v4l2_ctrl->minimum = 0;
821 v4l2_ctrl->maximum = 0;
830 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
831 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
832 chain->dev->intfnum, ctrl->info->selector,
833 data, ctrl->info->size);
836 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, data);
838 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
839 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
840 chain->dev->intfnum, ctrl->info->selector,
841 data, ctrl->info->size);
844 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, data);
846 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
847 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
848 chain->dev->intfnum, ctrl->info->selector,
849 data, ctrl->info->size);
852 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, data);
862 /* --------------------------------------------------------------------------
863 * Control transactions
865 * To make extended set operations as atomic as the hardware allows, controls
866 * are handled using begin/commit/rollback operations.
868 * At the beginning of a set request, uvc_ctrl_begin should be called to
869 * initialize the request. This function acquires the control lock.
871 * When setting a control, the new value is stored in the control data field
872 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
873 * later processing. If the UVC and V4L2 control sizes differ, the current
874 * value is loaded from the hardware before storing the new value in the data
877 * After processing all controls in the transaction, uvc_ctrl_commit or
878 * uvc_ctrl_rollback must be called to apply the pending changes to the
879 * hardware or revert them. When applying changes, all controls marked as
880 * dirty will be modified in the UVC device, and the dirty flag will be
881 * cleared. When reverting controls, the control data field
882 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
883 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
886 int uvc_ctrl_begin(struct uvc_video_chain *chain)
888 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
891 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
892 struct uvc_entity *entity, int rollback)
894 struct uvc_control *ctrl;
901 for (i = 0; i < entity->ncontrols; ++i) {
902 ctrl = &entity->controls[i];
903 if (ctrl->info == NULL)
906 /* Reset the loaded flag for auto-update controls that were
907 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
908 * uvc_ctrl_get from using the cached value.
910 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
917 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
918 dev->intfnum, ctrl->info->selector,
919 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
924 if (rollback || ret < 0)
925 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
926 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
938 int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
940 struct uvc_entity *entity;
943 /* Find the control. */
944 list_for_each_entry(entity, &chain->entities, chain) {
945 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
951 mutex_unlock(&chain->ctrl_mutex);
955 int uvc_ctrl_get(struct uvc_video_chain *chain,
956 struct v4l2_ext_control *xctrl)
958 struct uvc_control *ctrl;
959 struct uvc_control_mapping *mapping;
960 struct uvc_menu_info *menu;
964 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
965 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
969 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
970 chain->dev->intfnum, ctrl->info->selector,
971 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
979 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
980 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
982 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
983 menu = mapping->menu_info;
984 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
985 if (menu->value == xctrl->value) {
995 int uvc_ctrl_set(struct uvc_video_chain *chain,
996 struct v4l2_ext_control *xctrl)
998 struct uvc_control *ctrl;
999 struct uvc_control_mapping *mapping;
1000 s32 value = xctrl->value;
1003 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1004 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1007 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1008 if (value < 0 || value >= mapping->menu_count)
1010 value = mapping->menu_info[value].value;
1013 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1014 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1015 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1016 0, ctrl->info->size);
1018 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1019 ctrl->entity->id, chain->dev->intfnum,
1020 ctrl->info->selector,
1021 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1031 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1032 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1036 mapping->set(mapping, value,
1037 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1044 /* --------------------------------------------------------------------------
1048 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1049 struct uvc_xu_control *xctrl, int set)
1051 struct uvc_entity *entity;
1052 struct uvc_control *ctrl = NULL;
1053 unsigned int i, found = 0;
1057 /* Find the extension unit. */
1058 list_for_each_entry(entity, &chain->entities, chain) {
1059 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1060 entity->id == xctrl->unit)
1064 if (entity->id != xctrl->unit) {
1065 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1070 /* Find the control. */
1071 for (i = 0; i < entity->ncontrols; ++i) {
1072 ctrl = &entity->controls[i];
1073 if (ctrl->info == NULL)
1076 if (ctrl->info->selector == xctrl->selector) {
1083 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1084 entity->extension.guidExtensionCode, xctrl->selector);
1088 /* Validate control data size. */
1089 if (ctrl->info->size != xctrl->size)
1092 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1093 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1096 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1097 return -ERESTARTSYS;
1099 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1100 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1102 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1104 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1109 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1110 xctrl->unit, chain->dev->intfnum, xctrl->selector,
1115 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1122 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1123 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1126 mutex_unlock(&chain->ctrl_mutex);
1130 /* --------------------------------------------------------------------------
1135 * Restore control values after resume, skipping controls that haven't been
1139 * - Don't restore modified controls that are back to their default value.
1140 * - Handle restore order (Auto-Exposure Mode should be restored before
1143 int uvc_ctrl_resume_device(struct uvc_device *dev)
1145 struct uvc_control *ctrl;
1146 struct uvc_entity *entity;
1150 /* Walk the entities list and restore controls when possible. */
1151 list_for_each_entry(entity, &dev->entities, list) {
1153 for (i = 0; i < entity->ncontrols; ++i) {
1154 ctrl = &entity->controls[i];
1156 if (ctrl->info == NULL || !ctrl->modified ||
1157 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1160 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1161 ctrl->info->entity, ctrl->info->index,
1162 ctrl->info->selector);
1166 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1174 /* --------------------------------------------------------------------------
1175 * Control and mapping handling
1178 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1179 struct uvc_control_info *info)
1181 struct uvc_entity *entity;
1182 struct uvc_control *ctrl = NULL;
1186 list_for_each_entry(entity, &dev->entities, list) {
1187 if (!uvc_entity_match_guid(entity, info->entity))
1190 for (i = 0; i < entity->ncontrols; ++i) {
1191 ctrl = &entity->controls[i];
1192 if (ctrl->index == info->index) {
1205 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1206 /* Check if the device control information and length match
1207 * the user supplied information.
1213 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1214 dev->intfnum, info->selector, (__u8 *)&size, 2);
1216 uvc_trace(UVC_TRACE_CONTROL,
1217 "GET_LEN failed on control %pUl/%u (%d).\n",
1218 info->entity, info->selector, ret);
1222 if (info->size != le16_to_cpu(size)) {
1223 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1224 "doesn't match user supplied value.\n",
1225 info->entity, info->selector);
1229 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1230 dev->intfnum, info->selector, &inf, 1);
1232 uvc_trace(UVC_TRACE_CONTROL,
1233 "GET_INFO failed on control %pUl/%u (%d).\n",
1234 info->entity, info->selector, ret);
1238 flags = info->flags;
1239 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1240 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1241 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1242 "don't match supported operations.\n",
1243 info->entity, info->selector);
1249 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1250 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1251 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1252 dev->udev->devpath, entity->id);
1256 * Add an item to the UVC control information list, and instantiate a control
1257 * structure for each device that supports the control.
1259 int uvc_ctrl_add_info(struct uvc_control_info *info)
1261 struct uvc_control_info *ctrl;
1262 struct uvc_device *dev;
1265 /* Find matching controls by walking the devices, entities and
1268 mutex_lock(&uvc_driver.ctrl_mutex);
1270 /* First check if the list contains a control matching the new one.
1271 * Bail out if it does.
1273 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1274 if (memcmp(ctrl->entity, info->entity, 16))
1277 if (ctrl->selector == info->selector) {
1278 uvc_trace(UVC_TRACE_CONTROL,
1279 "Control %pUl/%u is already defined.\n",
1280 info->entity, info->selector);
1284 if (ctrl->index == info->index) {
1285 uvc_trace(UVC_TRACE_CONTROL,
1286 "Control %pUl/%u would overwrite index %d.\n",
1287 info->entity, info->selector, info->index);
1293 list_for_each_entry(dev, &uvc_driver.devices, list)
1294 uvc_ctrl_add_ctrl(dev, info);
1296 INIT_LIST_HEAD(&info->mappings);
1297 list_add_tail(&info->list, &uvc_driver.controls);
1299 mutex_unlock(&uvc_driver.ctrl_mutex);
1303 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1305 struct uvc_control_info *info;
1306 struct uvc_control_mapping *map;
1309 if (mapping->get == NULL)
1310 mapping->get = uvc_get_le_value;
1311 if (mapping->set == NULL)
1312 mapping->set = uvc_set_le_value;
1314 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1315 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1316 "invalid control id 0x%08x\n", mapping->name,
1321 mutex_lock(&uvc_driver.ctrl_mutex);
1322 list_for_each_entry(info, &uvc_driver.controls, list) {
1323 if (memcmp(info->entity, mapping->entity, 16) ||
1324 info->selector != mapping->selector)
1327 if (info->size * 8 < mapping->size + mapping->offset) {
1328 uvc_trace(UVC_TRACE_CONTROL,
1329 "Mapping '%s' would overflow control %pUl/%u\n",
1330 mapping->name, info->entity, info->selector);
1335 /* Check if the list contains a mapping matching the new one.
1336 * Bail out if it does.
1338 list_for_each_entry(map, &info->mappings, list) {
1339 if (map->id == mapping->id) {
1340 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1341 "already defined.\n", mapping->name);
1347 mapping->ctrl = info;
1348 list_add_tail(&mapping->list, &info->mappings);
1349 uvc_trace(UVC_TRACE_CONTROL,
1350 "Adding mapping %s to control %pUl/%u.\n",
1351 mapping->name, info->entity, info->selector);
1357 mutex_unlock(&uvc_driver.ctrl_mutex);
1362 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1363 * are currently the ones that crash the camera or unconditionally return an
1364 * error when queried.
1367 uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
1369 static const struct {
1370 struct usb_device_id id;
1373 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1374 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1381 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
1384 controls = entity->processing.bmControls;
1385 size = entity->processing.bControlSize;
1387 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
1388 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1391 if (blacklist[i].index >= 8 * size ||
1392 !uvc_test_bit(controls, blacklist[i].index))
1395 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1396 "removing it.\n", entity->id, blacklist[i].index);
1398 uvc_clear_bit(controls, blacklist[i].index);
1403 * Initialize device controls.
1405 int uvc_ctrl_init_device(struct uvc_device *dev)
1407 struct uvc_control_info *info;
1408 struct uvc_control *ctrl;
1409 struct uvc_entity *entity;
1412 /* Walk the entities list and instantiate controls */
1413 list_for_each_entry(entity, &dev->entities, list) {
1414 unsigned int bControlSize = 0, ncontrols = 0;
1415 __u8 *bmControls = NULL;
1417 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1418 bmControls = entity->extension.bmControls;
1419 bControlSize = entity->extension.bControlSize;
1420 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1421 bmControls = entity->processing.bmControls;
1422 bControlSize = entity->processing.bControlSize;
1423 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1424 bmControls = entity->camera.bmControls;
1425 bControlSize = entity->camera.bControlSize;
1428 uvc_ctrl_prune_entity(dev, entity);
1430 for (i = 0; i < bControlSize; ++i)
1431 ncontrols += hweight8(bmControls[i]);
1436 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1437 if (entity->controls == NULL)
1440 entity->ncontrols = ncontrols;
1442 ctrl = entity->controls;
1443 for (i = 0; i < bControlSize * 8; ++i) {
1444 if (uvc_test_bit(bmControls, i) == 0)
1447 ctrl->entity = entity;
1453 /* Walk the controls info list and associate them with the device
1454 * controls, then add the device to the global device list. This has
1455 * to be done while holding the controls lock, to make sure
1456 * uvc_ctrl_add_info() will not get called in-between.
1458 mutex_lock(&uvc_driver.ctrl_mutex);
1459 list_for_each_entry(info, &uvc_driver.controls, list)
1460 uvc_ctrl_add_ctrl(dev, info);
1462 list_add_tail(&dev->list, &uvc_driver.devices);
1463 mutex_unlock(&uvc_driver.ctrl_mutex);
1469 * Cleanup device controls.
1471 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1473 struct uvc_entity *entity;
1476 /* Remove the device from the global devices list */
1477 mutex_lock(&uvc_driver.ctrl_mutex);
1478 if (dev->list.next != NULL)
1479 list_del(&dev->list);
1480 mutex_unlock(&uvc_driver.ctrl_mutex);
1482 list_for_each_entry(entity, &dev->entities, list) {
1483 for (i = 0; i < entity->ncontrols; ++i)
1484 kfree(entity->controls[i].data);
1486 kfree(entity->controls);
1490 void uvc_ctrl_init(void)
1492 struct uvc_control_info *ctrl = uvc_ctrls;
1493 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1494 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1495 struct uvc_control_mapping *mend =
1496 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1498 for (; ctrl < cend; ++ctrl)
1499 uvc_ctrl_add_info(ctrl);
1501 for (; mapping < mend; ++mapping)
1502 uvc_ctrl_add_mapping(mapping);