2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2008
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/version.h>
16 #include <linux/list.h>
17 #include <linux/module.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 <asm/atomic.h>
27 #define UVC_CTRL_NDATA 2
28 #define UVC_CTRL_DATA_CURRENT 0
29 #define UVC_CTRL_DATA_BACKUP 1
31 /* ------------------------------------------------------------------------
32 * Control, formats, ...
35 static struct uvc_control_info uvc_ctrls[] = {
37 .entity = UVC_GUID_UVC_PROCESSING,
38 .selector = PU_BRIGHTNESS_CONTROL,
41 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
42 | UVC_CONTROL_RESTORE,
45 .entity = UVC_GUID_UVC_PROCESSING,
46 .selector = PU_CONTRAST_CONTROL,
49 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
50 | UVC_CONTROL_RESTORE,
53 .entity = UVC_GUID_UVC_PROCESSING,
54 .selector = PU_HUE_CONTROL,
57 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
58 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
61 .entity = UVC_GUID_UVC_PROCESSING,
62 .selector = PU_SATURATION_CONTROL,
65 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
66 | UVC_CONTROL_RESTORE,
69 .entity = UVC_GUID_UVC_PROCESSING,
70 .selector = PU_SHARPNESS_CONTROL,
73 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
74 | UVC_CONTROL_RESTORE,
77 .entity = UVC_GUID_UVC_PROCESSING,
78 .selector = PU_GAMMA_CONTROL,
81 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
82 | UVC_CONTROL_RESTORE,
85 .entity = UVC_GUID_UVC_PROCESSING,
86 .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
89 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
90 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
93 .entity = UVC_GUID_UVC_PROCESSING,
94 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
97 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
98 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
101 .entity = UVC_GUID_UVC_PROCESSING,
102 .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
105 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
106 | UVC_CONTROL_RESTORE,
109 .entity = UVC_GUID_UVC_PROCESSING,
110 .selector = PU_GAIN_CONTROL,
113 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
114 | UVC_CONTROL_RESTORE,
117 .entity = UVC_GUID_UVC_PROCESSING,
118 .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
121 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
122 | UVC_CONTROL_RESTORE,
125 .entity = UVC_GUID_UVC_PROCESSING,
126 .selector = PU_HUE_AUTO_CONTROL,
129 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
130 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
133 .entity = UVC_GUID_UVC_PROCESSING,
134 .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
137 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
138 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
141 .entity = UVC_GUID_UVC_PROCESSING,
142 .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
145 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
146 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
149 .entity = UVC_GUID_UVC_PROCESSING,
150 .selector = PU_DIGITAL_MULTIPLIER_CONTROL,
153 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
154 | UVC_CONTROL_RESTORE,
157 .entity = UVC_GUID_UVC_PROCESSING,
158 .selector = PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
161 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
162 | UVC_CONTROL_RESTORE,
165 .entity = UVC_GUID_UVC_PROCESSING,
166 .selector = PU_ANALOG_VIDEO_STANDARD_CONTROL,
169 .flags = UVC_CONTROL_GET_CUR,
172 .entity = UVC_GUID_UVC_PROCESSING,
173 .selector = PU_ANALOG_LOCK_STATUS_CONTROL,
176 .flags = UVC_CONTROL_GET_CUR,
179 .entity = UVC_GUID_UVC_CAMERA,
180 .selector = CT_SCANNING_MODE_CONTROL,
183 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
184 | UVC_CONTROL_RESTORE,
187 .entity = UVC_GUID_UVC_CAMERA,
188 .selector = CT_AE_MODE_CONTROL,
191 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
192 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
193 | UVC_CONTROL_RESTORE,
196 .entity = UVC_GUID_UVC_CAMERA,
197 .selector = CT_AE_PRIORITY_CONTROL,
200 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
201 | UVC_CONTROL_RESTORE,
204 .entity = UVC_GUID_UVC_CAMERA,
205 .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
208 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
209 | UVC_CONTROL_RESTORE,
212 .entity = UVC_GUID_UVC_CAMERA,
213 .selector = CT_EXPOSURE_TIME_RELATIVE_CONTROL,
216 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
217 | UVC_CONTROL_RESTORE,
220 .entity = UVC_GUID_UVC_CAMERA,
221 .selector = CT_FOCUS_ABSOLUTE_CONTROL,
224 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
225 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
228 .entity = UVC_GUID_UVC_CAMERA,
229 .selector = CT_FOCUS_RELATIVE_CONTROL,
232 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
233 | UVC_CONTROL_AUTO_UPDATE,
236 .entity = UVC_GUID_UVC_CAMERA,
237 .selector = CT_IRIS_ABSOLUTE_CONTROL,
240 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
241 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
244 .entity = UVC_GUID_UVC_CAMERA,
245 .selector = CT_IRIS_RELATIVE_CONTROL,
248 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
249 | UVC_CONTROL_AUTO_UPDATE,
252 .entity = UVC_GUID_UVC_CAMERA,
253 .selector = CT_ZOOM_ABSOLUTE_CONTROL,
256 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
257 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
260 .entity = UVC_GUID_UVC_CAMERA,
261 .selector = CT_ZOOM_RELATIVE_CONTROL,
264 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
265 | UVC_CONTROL_AUTO_UPDATE,
268 .entity = UVC_GUID_UVC_CAMERA,
269 .selector = CT_PANTILT_ABSOLUTE_CONTROL,
272 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
273 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
276 .entity = UVC_GUID_UVC_CAMERA,
277 .selector = CT_PANTILT_RELATIVE_CONTROL,
280 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
281 | UVC_CONTROL_AUTO_UPDATE,
284 .entity = UVC_GUID_UVC_CAMERA,
285 .selector = CT_ROLL_ABSOLUTE_CONTROL,
288 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
289 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
292 .entity = UVC_GUID_UVC_CAMERA,
293 .selector = CT_ROLL_RELATIVE_CONTROL,
296 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
297 | UVC_CONTROL_AUTO_UPDATE,
300 .entity = UVC_GUID_UVC_CAMERA,
301 .selector = CT_FOCUS_AUTO_CONTROL,
304 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
305 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
308 .entity = UVC_GUID_UVC_CAMERA,
309 .selector = CT_PRIVACY_CONTROL,
312 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
313 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
317 static struct uvc_menu_info power_line_frequency_controls[] = {
323 static struct uvc_menu_info exposure_auto_controls[] = {
325 { 1, "Manual Mode" },
326 { 4, "Shutter Priority Mode" },
327 { 8, "Aperture Priority Mode" },
330 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
331 __u8 query, const __u8 *data)
333 __s8 zoom = (__s8)data[0];
337 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
348 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
349 __s32 value, __u8 *data)
351 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
352 data[2] = min(abs(value), 0xff);
355 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
357 .id = V4L2_CID_BRIGHTNESS,
358 .name = "Brightness",
359 .entity = UVC_GUID_UVC_PROCESSING,
360 .selector = PU_BRIGHTNESS_CONTROL,
363 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
364 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
367 .id = V4L2_CID_CONTRAST,
369 .entity = UVC_GUID_UVC_PROCESSING,
370 .selector = PU_CONTRAST_CONTROL,
373 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
374 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
379 .entity = UVC_GUID_UVC_PROCESSING,
380 .selector = PU_HUE_CONTROL,
383 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
384 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
387 .id = V4L2_CID_SATURATION,
388 .name = "Saturation",
389 .entity = UVC_GUID_UVC_PROCESSING,
390 .selector = PU_SATURATION_CONTROL,
393 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
394 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
397 .id = V4L2_CID_SHARPNESS,
399 .entity = UVC_GUID_UVC_PROCESSING,
400 .selector = PU_SHARPNESS_CONTROL,
403 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
404 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
407 .id = V4L2_CID_GAMMA,
409 .entity = UVC_GUID_UVC_PROCESSING,
410 .selector = PU_GAMMA_CONTROL,
413 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
414 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
417 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
418 .name = "Backlight Compensation",
419 .entity = UVC_GUID_UVC_PROCESSING,
420 .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
423 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
424 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
429 .entity = UVC_GUID_UVC_PROCESSING,
430 .selector = PU_GAIN_CONTROL,
433 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
434 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
437 .id = V4L2_CID_POWER_LINE_FREQUENCY,
438 .name = "Power Line Frequency",
439 .entity = UVC_GUID_UVC_PROCESSING,
440 .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
443 .v4l2_type = V4L2_CTRL_TYPE_MENU,
444 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
445 .menu_info = power_line_frequency_controls,
446 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
449 .id = V4L2_CID_HUE_AUTO,
451 .entity = UVC_GUID_UVC_PROCESSING,
452 .selector = PU_HUE_AUTO_CONTROL,
455 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
456 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
459 .id = V4L2_CID_EXPOSURE_AUTO,
460 .name = "Exposure, Auto",
461 .entity = UVC_GUID_UVC_CAMERA,
462 .selector = CT_AE_MODE_CONTROL,
465 .v4l2_type = V4L2_CTRL_TYPE_MENU,
466 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
467 .menu_info = exposure_auto_controls,
468 .menu_count = ARRAY_SIZE(exposure_auto_controls),
471 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
472 .name = "Exposure, Auto Priority",
473 .entity = UVC_GUID_UVC_CAMERA,
474 .selector = CT_AE_PRIORITY_CONTROL,
477 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
478 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
481 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
482 .name = "Exposure (Absolute)",
483 .entity = UVC_GUID_UVC_CAMERA,
484 .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
487 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
488 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
491 .id = V4L2_CID_AUTO_WHITE_BALANCE,
492 .name = "White Balance Temperature, Auto",
493 .entity = UVC_GUID_UVC_PROCESSING,
494 .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
497 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
498 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
501 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
502 .name = "White Balance Temperature",
503 .entity = UVC_GUID_UVC_PROCESSING,
504 .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
507 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
508 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
511 .id = V4L2_CID_AUTO_WHITE_BALANCE,
512 .name = "White Balance Component, Auto",
513 .entity = UVC_GUID_UVC_PROCESSING,
514 .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
517 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
518 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
521 .id = V4L2_CID_BLUE_BALANCE,
522 .name = "White Balance Blue Component",
523 .entity = UVC_GUID_UVC_PROCESSING,
524 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
527 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
528 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
531 .id = V4L2_CID_RED_BALANCE,
532 .name = "White Balance Red Component",
533 .entity = UVC_GUID_UVC_PROCESSING,
534 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
537 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
538 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
541 .id = V4L2_CID_FOCUS_ABSOLUTE,
542 .name = "Focus (absolute)",
543 .entity = UVC_GUID_UVC_CAMERA,
544 .selector = CT_FOCUS_ABSOLUTE_CONTROL,
547 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
548 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
551 .id = V4L2_CID_FOCUS_AUTO,
552 .name = "Focus, Auto",
553 .entity = UVC_GUID_UVC_CAMERA,
554 .selector = CT_FOCUS_AUTO_CONTROL,
557 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
558 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
561 .id = V4L2_CID_ZOOM_ABSOLUTE,
562 .name = "Zoom, Absolute",
563 .entity = UVC_GUID_UVC_CAMERA,
564 .selector = CT_ZOOM_ABSOLUTE_CONTROL,
567 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
568 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
571 .id = V4L2_CID_ZOOM_CONTINUOUS,
572 .name = "Zoom, Continuous",
573 .entity = UVC_GUID_UVC_CAMERA,
574 .selector = CT_ZOOM_RELATIVE_CONTROL,
577 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
578 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
579 .get = uvc_ctrl_get_zoom,
580 .set = uvc_ctrl_set_zoom,
583 .id = V4L2_CID_PRIVACY,
585 .entity = UVC_GUID_UVC_CAMERA,
586 .selector = CT_PRIVACY_CONTROL,
589 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
590 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
594 /* ------------------------------------------------------------------------
598 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
600 return ctrl->data + id * ctrl->info->size;
603 static inline int uvc_test_bit(const __u8 *data, int bit)
605 return (data[bit >> 3] >> (bit & 7)) & 1;
608 static inline void uvc_clear_bit(__u8 *data, int bit)
610 data[bit >> 3] &= ~(1 << (bit & 7));
613 /* Extract the bit string specified by mapping->offset and mapping->size
614 * from the little-endian data stored at 'data' and return the result as
615 * a signed 32bit integer. Sign extension will be performed if the mapping
616 * references a signed data type.
618 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
619 __u8 query, const __u8 *data)
621 int bits = mapping->size;
622 int offset = mapping->offset;
628 mask = ((1LL << bits) - 1) << offset;
630 for (; bits > 0; data++) {
631 __u8 byte = *data & mask;
632 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
633 bits -= 8 - (offset > 0 ? offset : 0);
635 mask = (1 << bits) - 1;
638 /* Sign-extend the value if needed */
639 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
640 value |= -(value & (1 << (mapping->size - 1)));
645 /* Set the bit string specified by mapping->offset and mapping->size
646 * in the little-endian data stored at 'data' to the value 'value'.
648 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
649 __s32 value, __u8 *data)
651 int bits = mapping->size;
652 int offset = mapping->offset;
658 for (; bits > 0; data++) {
659 mask = ((1LL << bits) - 1) << offset;
660 *data = (*data & ~mask) | ((value << offset) & mask);
661 value >>= offset ? offset : 8;
667 /* ------------------------------------------------------------------------
668 * Terminal and unit management
671 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
672 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
673 static const __u8 uvc_media_transport_input_guid[16] =
674 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
676 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
678 switch (UVC_ENTITY_TYPE(entity)) {
680 return memcmp(uvc_camera_guid, guid, 16) == 0;
682 case ITT_MEDIA_TRANSPORT_INPUT:
683 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
685 case VC_PROCESSING_UNIT:
686 return memcmp(uvc_processing_guid, guid, 16) == 0;
688 case VC_EXTENSION_UNIT:
689 return memcmp(entity->extension.guidExtensionCode,
697 /* ------------------------------------------------------------------------
701 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
702 struct uvc_control_mapping **mapping, struct uvc_control **control,
705 struct uvc_control *ctrl;
706 struct uvc_control_mapping *map;
712 for (i = 0; i < entity->ncontrols; ++i) {
713 ctrl = &entity->controls[i];
714 if (ctrl->info == NULL)
717 list_for_each_entry(map, &ctrl->info->mappings, list) {
718 if ((map->id == v4l2_id) && !next) {
724 if ((*mapping == NULL || (*mapping)->id > map->id) &&
725 (map->id > v4l2_id) && next) {
733 struct uvc_control *uvc_find_control(struct uvc_video_device *video,
734 __u32 v4l2_id, struct uvc_control_mapping **mapping)
736 struct uvc_control *ctrl = NULL;
737 struct uvc_entity *entity;
738 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
742 /* Mask the query flags. */
743 v4l2_id &= V4L2_CTRL_ID_MASK;
745 /* Find the control. */
746 __uvc_find_control(video->processing, v4l2_id, mapping, &ctrl, next);
750 list_for_each_entry(entity, &video->iterms, chain) {
751 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
756 list_for_each_entry(entity, &video->extensions, chain) {
757 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
762 if (ctrl == NULL && !next)
763 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
769 int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
770 struct v4l2_queryctrl *v4l2_ctrl)
772 struct uvc_control *ctrl;
773 struct uvc_control_mapping *mapping;
774 struct uvc_menu_info *menu;
779 ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
783 data = kmalloc(ctrl->info->size, GFP_KERNEL);
787 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
788 v4l2_ctrl->id = mapping->id;
789 v4l2_ctrl->type = mapping->v4l2_type;
790 strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
791 v4l2_ctrl->flags = 0;
793 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
794 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
796 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
797 if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
798 video->dev->intfnum, ctrl->info->selector,
799 data, ctrl->info->size)) < 0)
801 v4l2_ctrl->default_value = mapping->get(mapping, GET_DEF, data);
804 switch (mapping->v4l2_type) {
805 case V4L2_CTRL_TYPE_MENU:
806 v4l2_ctrl->minimum = 0;
807 v4l2_ctrl->maximum = mapping->menu_count - 1;
810 menu = mapping->menu_info;
811 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
812 if (menu->value == v4l2_ctrl->default_value) {
813 v4l2_ctrl->default_value = i;
821 case V4L2_CTRL_TYPE_BOOLEAN:
822 v4l2_ctrl->minimum = 0;
823 v4l2_ctrl->maximum = 1;
832 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
833 if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
834 video->dev->intfnum, ctrl->info->selector,
835 data, ctrl->info->size)) < 0)
837 v4l2_ctrl->minimum = mapping->get(mapping, GET_MIN, data);
839 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
840 if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
841 video->dev->intfnum, ctrl->info->selector,
842 data, ctrl->info->size)) < 0)
844 v4l2_ctrl->maximum = mapping->get(mapping, GET_MAX, data);
846 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
847 if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
848 video->dev->intfnum, ctrl->info->selector,
849 data, ctrl->info->size)) < 0)
851 v4l2_ctrl->step = mapping->get(mapping, GET_RES, data);
861 /* --------------------------------------------------------------------------
862 * Control transactions
864 * To make extended set operations as atomic as the hardware allows, controls
865 * are handled using begin/commit/rollback operations.
867 * At the beginning of a set request, uvc_ctrl_begin should be called to
868 * initialize the request. This function acquires the control lock.
870 * When setting a control, the new value is stored in the control data field
871 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
872 * later processing. If the UVC and V4L2 control sizes differ, the current
873 * value is loaded from the hardware before storing the new value in the data
876 * After processing all controls in the transaction, uvc_ctrl_commit or
877 * uvc_ctrl_rollback must be called to apply the pending changes to the
878 * hardware or revert them. When applying changes, all controls marked as
879 * dirty will be modified in the UVC device, and the dirty flag will be
880 * cleared. When reverting controls, the control data field
881 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
882 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
885 int uvc_ctrl_begin(struct uvc_video_device *video)
887 return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
890 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
891 struct uvc_entity *entity, int rollback)
893 struct uvc_control *ctrl;
900 for (i = 0; i < entity->ncontrols; ++i) {
901 ctrl = &entity->controls[i];
902 if (ctrl->info == NULL)
905 /* Reset the loaded flag for auto-update controls that were
906 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
907 * uvc_ctrl_get from using the cached value.
909 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
916 ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
917 dev->intfnum, ctrl->info->selector,
918 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
923 if (rollback || ret < 0)
924 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
925 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
937 int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
939 struct uvc_entity *entity;
942 /* Find the control. */
943 ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
947 list_for_each_entry(entity, &video->iterms, chain) {
948 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
953 list_for_each_entry(entity, &video->extensions, chain) {
954 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
960 mutex_unlock(&video->ctrl_mutex);
964 int uvc_ctrl_get(struct uvc_video_device *video,
965 struct v4l2_ext_control *xctrl)
967 struct uvc_control *ctrl;
968 struct uvc_control_mapping *mapping;
969 struct uvc_menu_info *menu;
973 ctrl = uvc_find_control(video, xctrl->id, &mapping);
974 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
978 ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
979 video->dev->intfnum, ctrl->info->selector,
980 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
988 xctrl->value = mapping->get(mapping, GET_CUR,
989 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
991 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
992 menu = mapping->menu_info;
993 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
994 if (menu->value == xctrl->value) {
1004 int uvc_ctrl_set(struct uvc_video_device *video,
1005 struct v4l2_ext_control *xctrl)
1007 struct uvc_control *ctrl;
1008 struct uvc_control_mapping *mapping;
1009 s32 value = xctrl->value;
1012 ctrl = uvc_find_control(video, xctrl->id, &mapping);
1013 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1016 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1017 if (value < 0 || value >= mapping->menu_count)
1019 value = mapping->menu_info[value].value;
1022 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1023 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1024 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1025 0, ctrl->info->size);
1027 ret = uvc_query_ctrl(video->dev, GET_CUR,
1028 ctrl->entity->id, video->dev->intfnum,
1029 ctrl->info->selector,
1030 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1040 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1041 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1045 mapping->set(mapping, value,
1046 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1053 /* --------------------------------------------------------------------------
1057 int uvc_xu_ctrl_query(struct uvc_video_device *video,
1058 struct uvc_xu_control *xctrl, int set)
1060 struct uvc_entity *entity;
1061 struct uvc_control *ctrl = NULL;
1062 unsigned int i, found = 0;
1066 /* Find the extension unit. */
1067 list_for_each_entry(entity, &video->extensions, chain) {
1068 if (entity->id == xctrl->unit)
1072 if (entity->id != xctrl->unit) {
1073 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1078 /* Find the control. */
1079 for (i = 0; i < entity->ncontrols; ++i) {
1080 ctrl = &entity->controls[i];
1081 if (ctrl->info == NULL)
1084 if (ctrl->info->selector == xctrl->selector) {
1091 uvc_trace(UVC_TRACE_CONTROL,
1092 "Control " UVC_GUID_FORMAT "/%u not found.\n",
1093 UVC_GUID_ARGS(entity->extension.guidExtensionCode),
1098 /* Validate control data size. */
1099 if (ctrl->info->size != xctrl->size)
1102 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1103 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1106 if (mutex_lock_interruptible(&video->ctrl_mutex))
1107 return -ERESTARTSYS;
1109 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1110 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1112 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1114 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1119 ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
1120 video->dev->intfnum, xctrl->selector, data,
1125 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1132 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1133 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1136 mutex_unlock(&video->ctrl_mutex);
1140 /* --------------------------------------------------------------------------
1145 * Restore control values after resume, skipping controls that haven't been
1149 * - Don't restore modified controls that are back to their default value.
1150 * - Handle restore order (Auto-Exposure Mode should be restored before
1153 int uvc_ctrl_resume_device(struct uvc_device *dev)
1155 struct uvc_control *ctrl;
1156 struct uvc_entity *entity;
1160 /* Walk the entities list and restore controls when possible. */
1161 list_for_each_entry(entity, &dev->entities, list) {
1163 for (i = 0; i < entity->ncontrols; ++i) {
1164 ctrl = &entity->controls[i];
1166 if (ctrl->info == NULL || !ctrl->modified ||
1167 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1170 printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
1171 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
1172 ctrl->info->index, ctrl->info->selector);
1176 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1184 /* --------------------------------------------------------------------------
1185 * Control and mapping handling
1188 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1189 struct uvc_control_info *info)
1191 struct uvc_entity *entity;
1192 struct uvc_control *ctrl = NULL;
1196 list_for_each_entry(entity, &dev->entities, list) {
1197 if (!uvc_entity_match_guid(entity, info->entity))
1200 for (i = 0; i < entity->ncontrols; ++i) {
1201 ctrl = &entity->controls[i];
1202 if (ctrl->index == info->index) {
1215 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1216 /* Check if the device control information and length match
1217 * the user supplied information.
1223 if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
1224 dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
1225 uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1226 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1227 UVC_GUID_ARGS(info->entity), info->selector,
1232 if (info->size != le16_to_cpu(size)) {
1233 uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1234 "/%u size doesn't match user supplied "
1235 "value.\n", UVC_GUID_ARGS(info->entity),
1240 if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
1241 dev->intfnum, info->selector, &inf, 1)) < 0) {
1242 uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1243 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1244 UVC_GUID_ARGS(info->entity), info->selector,
1249 flags = info->flags;
1250 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1251 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1252 uvc_trace(UVC_TRACE_CONTROL, "Control "
1253 UVC_GUID_FORMAT "/%u flags don't match "
1254 "supported operations.\n",
1255 UVC_GUID_ARGS(info->entity), info->selector);
1261 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1262 uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1263 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1264 ctrl->info->selector, dev->udev->devpath, entity->id);
1268 * Add an item to the UVC control information list, and instantiate a control
1269 * structure for each device that supports the control.
1271 int uvc_ctrl_add_info(struct uvc_control_info *info)
1273 struct uvc_control_info *ctrl;
1274 struct uvc_device *dev;
1277 /* Find matching controls by walking the devices, entities and
1280 mutex_lock(&uvc_driver.ctrl_mutex);
1282 /* First check if the list contains a control matching the new one.
1283 * Bail out if it does.
1285 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1286 if (memcmp(ctrl->entity, info->entity, 16))
1289 if (ctrl->selector == info->selector) {
1290 uvc_trace(UVC_TRACE_CONTROL, "Control "
1291 UVC_GUID_FORMAT "/%u is already defined.\n",
1292 UVC_GUID_ARGS(info->entity), info->selector);
1296 if (ctrl->index == info->index) {
1297 uvc_trace(UVC_TRACE_CONTROL, "Control "
1298 UVC_GUID_FORMAT "/%u would overwrite index "
1299 "%d.\n", UVC_GUID_ARGS(info->entity),
1300 info->selector, info->index);
1306 list_for_each_entry(dev, &uvc_driver.devices, list)
1307 uvc_ctrl_add_ctrl(dev, info);
1309 INIT_LIST_HEAD(&info->mappings);
1310 list_add_tail(&info->list, &uvc_driver.controls);
1312 mutex_unlock(&uvc_driver.ctrl_mutex);
1316 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1318 struct uvc_control_info *info;
1319 struct uvc_control_mapping *map;
1322 if (mapping->get == NULL)
1323 mapping->get = uvc_get_le_value;
1324 if (mapping->set == NULL)
1325 mapping->set = uvc_set_le_value;
1327 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1328 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1329 "invalid control id 0x%08x\n", mapping->name,
1334 mutex_lock(&uvc_driver.ctrl_mutex);
1335 list_for_each_entry(info, &uvc_driver.controls, list) {
1336 if (memcmp(info->entity, mapping->entity, 16) ||
1337 info->selector != mapping->selector)
1340 if (info->size * 8 < mapping->size + mapping->offset) {
1341 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1342 "overflow control " UVC_GUID_FORMAT "/%u\n",
1343 mapping->name, UVC_GUID_ARGS(info->entity),
1349 /* Check if the list contains a mapping matching the new one.
1350 * Bail out if it does.
1352 list_for_each_entry(map, &info->mappings, list) {
1353 if (map->id == mapping->id) {
1354 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1355 "already defined.\n", mapping->name);
1361 mapping->ctrl = info;
1362 list_add_tail(&mapping->list, &info->mappings);
1363 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1364 UVC_GUID_FORMAT "/%u.\n", mapping->name,
1365 UVC_GUID_ARGS(info->entity), info->selector);
1371 mutex_unlock(&uvc_driver.ctrl_mutex);
1376 * Prune an entity of its bogus controls. This currently includes processing
1377 * unit auto controls for which no corresponding manual control is available.
1378 * Such auto controls make little sense if any, and are known to crash at
1379 * least the SiGma Micro webcam.
1382 uvc_ctrl_prune_entity(struct uvc_entity *entity)
1384 static const struct {
1388 { 2, 11 }, /* Hue */
1389 { 6, 12 }, /* White Balance Temperature */
1390 { 7, 13 }, /* White Balance Component */
1397 if (UVC_ENTITY_TYPE(entity) != VC_PROCESSING_UNIT)
1400 controls = entity->processing.bmControls;
1401 size = entity->processing.bControlSize;
1403 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
1404 if (blacklist[i].idx_auto >= 8 * size ||
1405 blacklist[i].idx_manual >= 8 * size)
1408 if (!uvc_test_bit(controls, blacklist[i].idx_auto) ||
1409 uvc_test_bit(controls, blacklist[i].idx_manual))
1412 uvc_trace(UVC_TRACE_CONTROL, "Auto control %u/%u has no "
1413 "matching manual control, removing it.\n", entity->id,
1414 blacklist[i].idx_auto);
1416 uvc_clear_bit(controls, blacklist[i].idx_auto);
1421 * Initialize device controls.
1423 int uvc_ctrl_init_device(struct uvc_device *dev)
1425 struct uvc_control_info *info;
1426 struct uvc_control *ctrl;
1427 struct uvc_entity *entity;
1430 /* Walk the entities list and instantiate controls */
1431 list_for_each_entry(entity, &dev->entities, list) {
1432 unsigned int bControlSize = 0, ncontrols = 0;
1433 __u8 *bmControls = NULL;
1435 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1436 bmControls = entity->extension.bmControls;
1437 bControlSize = entity->extension.bControlSize;
1438 } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
1439 bmControls = entity->processing.bmControls;
1440 bControlSize = entity->processing.bControlSize;
1441 } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
1442 bmControls = entity->camera.bmControls;
1443 bControlSize = entity->camera.bControlSize;
1446 if (dev->quirks & UVC_QUIRK_PRUNE_CONTROLS)
1447 uvc_ctrl_prune_entity(entity);
1449 for (i = 0; i < bControlSize; ++i)
1450 ncontrols += hweight8(bmControls[i]);
1455 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1456 if (entity->controls == NULL)
1459 entity->ncontrols = ncontrols;
1461 ctrl = entity->controls;
1462 for (i = 0; i < bControlSize * 8; ++i) {
1463 if (uvc_test_bit(bmControls, i) == 0)
1466 ctrl->entity = entity;
1472 /* Walk the controls info list and associate them with the device
1473 * controls, then add the device to the global device list. This has
1474 * to be done while holding the controls lock, to make sure
1475 * uvc_ctrl_add_info() will not get called in-between.
1477 mutex_lock(&uvc_driver.ctrl_mutex);
1478 list_for_each_entry(info, &uvc_driver.controls, list)
1479 uvc_ctrl_add_ctrl(dev, info);
1481 list_add_tail(&dev->list, &uvc_driver.devices);
1482 mutex_unlock(&uvc_driver.ctrl_mutex);
1488 * Cleanup device controls.
1490 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1492 struct uvc_entity *entity;
1495 /* Remove the device from the global devices list */
1496 mutex_lock(&uvc_driver.ctrl_mutex);
1497 if (dev->list.next != NULL)
1498 list_del(&dev->list);
1499 mutex_unlock(&uvc_driver.ctrl_mutex);
1501 list_for_each_entry(entity, &dev->entities, list) {
1502 for (i = 0; i < entity->ncontrols; ++i)
1503 kfree(entity->controls[i].data);
1505 kfree(entity->controls);
1509 void uvc_ctrl_init(void)
1511 struct uvc_control_info *ctrl = uvc_ctrls;
1512 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1513 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1514 struct uvc_control_mapping *mend =
1515 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1517 for (; ctrl < cend; ++ctrl)
1518 uvc_ctrl_add_info(ctrl);
1520 for (; mapping < mend; ++mapping)
1521 uvc_ctrl_add_mapping(mapping);