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 struct uvc_control_mapping uvc_ctrl_mappings[] = {
332 .id = V4L2_CID_BRIGHTNESS,
333 .name = "Brightness",
334 .entity = UVC_GUID_UVC_PROCESSING,
335 .selector = PU_BRIGHTNESS_CONTROL,
338 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
339 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
342 .id = V4L2_CID_CONTRAST,
344 .entity = UVC_GUID_UVC_PROCESSING,
345 .selector = PU_CONTRAST_CONTROL,
348 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
349 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
354 .entity = UVC_GUID_UVC_PROCESSING,
355 .selector = PU_HUE_CONTROL,
358 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
359 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
362 .id = V4L2_CID_SATURATION,
363 .name = "Saturation",
364 .entity = UVC_GUID_UVC_PROCESSING,
365 .selector = PU_SATURATION_CONTROL,
368 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
369 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
372 .id = V4L2_CID_SHARPNESS,
374 .entity = UVC_GUID_UVC_PROCESSING,
375 .selector = PU_SHARPNESS_CONTROL,
378 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
379 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
382 .id = V4L2_CID_GAMMA,
384 .entity = UVC_GUID_UVC_PROCESSING,
385 .selector = PU_GAMMA_CONTROL,
388 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
389 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
392 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
393 .name = "Backlight Compensation",
394 .entity = UVC_GUID_UVC_PROCESSING,
395 .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
398 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
399 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
404 .entity = UVC_GUID_UVC_PROCESSING,
405 .selector = PU_GAIN_CONTROL,
408 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
409 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
412 .id = V4L2_CID_POWER_LINE_FREQUENCY,
413 .name = "Power Line Frequency",
414 .entity = UVC_GUID_UVC_PROCESSING,
415 .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
418 .v4l2_type = V4L2_CTRL_TYPE_MENU,
419 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
420 .menu_info = power_line_frequency_controls,
421 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
424 .id = V4L2_CID_HUE_AUTO,
426 .entity = UVC_GUID_UVC_PROCESSING,
427 .selector = PU_HUE_AUTO_CONTROL,
430 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
431 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
434 .id = V4L2_CID_EXPOSURE_AUTO,
435 .name = "Exposure, Auto",
436 .entity = UVC_GUID_UVC_CAMERA,
437 .selector = CT_AE_MODE_CONTROL,
440 .v4l2_type = V4L2_CTRL_TYPE_MENU,
441 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
442 .menu_info = exposure_auto_controls,
443 .menu_count = ARRAY_SIZE(exposure_auto_controls),
446 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
447 .name = "Exposure, Auto Priority",
448 .entity = UVC_GUID_UVC_CAMERA,
449 .selector = CT_AE_PRIORITY_CONTROL,
452 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
453 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
456 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
457 .name = "Exposure (Absolute)",
458 .entity = UVC_GUID_UVC_CAMERA,
459 .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
462 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
463 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
466 .id = V4L2_CID_AUTO_WHITE_BALANCE,
467 .name = "White Balance Temperature, Auto",
468 .entity = UVC_GUID_UVC_PROCESSING,
469 .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
472 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
473 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
476 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
477 .name = "White Balance Temperature",
478 .entity = UVC_GUID_UVC_PROCESSING,
479 .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
482 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
483 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
486 .id = V4L2_CID_AUTO_WHITE_BALANCE,
487 .name = "White Balance Component, Auto",
488 .entity = UVC_GUID_UVC_PROCESSING,
489 .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
492 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
493 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
496 .id = V4L2_CID_BLUE_BALANCE,
497 .name = "White Balance Blue Component",
498 .entity = UVC_GUID_UVC_PROCESSING,
499 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
502 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
503 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
506 .id = V4L2_CID_RED_BALANCE,
507 .name = "White Balance Red Component",
508 .entity = UVC_GUID_UVC_PROCESSING,
509 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
512 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
513 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
516 .id = V4L2_CID_FOCUS_ABSOLUTE,
517 .name = "Focus (absolute)",
518 .entity = UVC_GUID_UVC_CAMERA,
519 .selector = CT_FOCUS_ABSOLUTE_CONTROL,
522 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
523 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
526 .id = V4L2_CID_FOCUS_AUTO,
527 .name = "Focus, Auto",
528 .entity = UVC_GUID_UVC_CAMERA,
529 .selector = CT_FOCUS_AUTO_CONTROL,
532 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
533 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
537 /* ------------------------------------------------------------------------
541 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
543 return ctrl->data + id * ctrl->info->size;
546 static inline int uvc_test_bit(const __u8 *data, int bit)
548 return (data[bit >> 3] >> (bit & 7)) & 1;
551 static inline void uvc_clear_bit(__u8 *data, int bit)
553 data[bit >> 3] &= ~(1 << (bit & 7));
556 /* Extract the bit string specified by mapping->offset and mapping->size
557 * from the little-endian data stored at 'data' and return the result as
558 * a signed 32bit integer. Sign extension will be performed if the mapping
559 * references a signed data type.
561 static __s32 uvc_get_le_value(const __u8 *data,
562 struct uvc_control_mapping *mapping)
564 int bits = mapping->size;
565 int offset = mapping->offset;
571 mask = ((1LL << bits) - 1) << offset;
573 for (; bits > 0; data++) {
574 __u8 byte = *data & mask;
575 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
576 bits -= 8 - (offset > 0 ? offset : 0);
578 mask = (1 << bits) - 1;
581 /* Sign-extend the value if needed */
582 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
583 value |= -(value & (1 << (mapping->size - 1)));
588 /* Set the bit string specified by mapping->offset and mapping->size
589 * in the little-endian data stored at 'data' to the value 'value'.
591 static void uvc_set_le_value(__s32 value, __u8 *data,
592 struct uvc_control_mapping *mapping)
594 int bits = mapping->size;
595 int offset = mapping->offset;
601 for (; bits > 0; data++) {
602 mask = ((1LL << bits) - 1) << offset;
603 *data = (*data & ~mask) | ((value << offset) & mask);
604 value >>= offset ? offset : 8;
610 /* ------------------------------------------------------------------------
611 * Terminal and unit management
614 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
615 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
616 static const __u8 uvc_media_transport_input_guid[16] =
617 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
619 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
621 switch (UVC_ENTITY_TYPE(entity)) {
623 return memcmp(uvc_camera_guid, guid, 16) == 0;
625 case ITT_MEDIA_TRANSPORT_INPUT:
626 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
628 case VC_PROCESSING_UNIT:
629 return memcmp(uvc_processing_guid, guid, 16) == 0;
631 case VC_EXTENSION_UNIT:
632 return memcmp(entity->extension.guidExtensionCode,
640 /* ------------------------------------------------------------------------
644 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
645 struct uvc_control_mapping **mapping, struct uvc_control **control,
648 struct uvc_control *ctrl;
649 struct uvc_control_mapping *map;
655 for (i = 0; i < entity->ncontrols; ++i) {
656 ctrl = &entity->controls[i];
657 if (ctrl->info == NULL)
660 list_for_each_entry(map, &ctrl->info->mappings, list) {
661 if ((map->id == v4l2_id) && !next) {
667 if ((*mapping == NULL || (*mapping)->id > map->id) &&
668 (map->id > v4l2_id) && next) {
676 struct uvc_control *uvc_find_control(struct uvc_video_device *video,
677 __u32 v4l2_id, struct uvc_control_mapping **mapping)
679 struct uvc_control *ctrl = NULL;
680 struct uvc_entity *entity;
681 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
685 /* Mask the query flags. */
686 v4l2_id &= V4L2_CTRL_ID_MASK;
688 /* Find the control. */
689 __uvc_find_control(video->processing, v4l2_id, mapping, &ctrl, next);
693 list_for_each_entry(entity, &video->iterms, chain) {
694 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
699 list_for_each_entry(entity, &video->extensions, chain) {
700 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
705 if (ctrl == NULL && !next)
706 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
712 int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
713 struct v4l2_queryctrl *v4l2_ctrl)
715 struct uvc_control *ctrl;
716 struct uvc_control_mapping *mapping;
717 struct uvc_menu_info *menu;
722 ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
726 data = kmalloc(ctrl->info->size, GFP_KERNEL);
730 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
731 v4l2_ctrl->id = mapping->id;
732 v4l2_ctrl->type = mapping->v4l2_type;
733 strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
734 v4l2_ctrl->flags = 0;
736 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
737 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
739 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
740 if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
741 video->dev->intfnum, ctrl->info->selector,
742 data, ctrl->info->size)) < 0)
744 v4l2_ctrl->default_value = uvc_get_le_value(data, mapping);
747 switch (mapping->v4l2_type) {
748 case V4L2_CTRL_TYPE_MENU:
749 v4l2_ctrl->minimum = 0;
750 v4l2_ctrl->maximum = mapping->menu_count - 1;
753 menu = mapping->menu_info;
754 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
755 if (menu->value == v4l2_ctrl->default_value) {
756 v4l2_ctrl->default_value = i;
764 case V4L2_CTRL_TYPE_BOOLEAN:
765 v4l2_ctrl->minimum = 0;
766 v4l2_ctrl->maximum = 1;
775 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
776 if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
777 video->dev->intfnum, ctrl->info->selector,
778 data, ctrl->info->size)) < 0)
780 v4l2_ctrl->minimum = uvc_get_le_value(data, mapping);
782 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
783 if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
784 video->dev->intfnum, ctrl->info->selector,
785 data, ctrl->info->size)) < 0)
787 v4l2_ctrl->maximum = uvc_get_le_value(data, mapping);
789 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
790 if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
791 video->dev->intfnum, ctrl->info->selector,
792 data, ctrl->info->size)) < 0)
794 v4l2_ctrl->step = uvc_get_le_value(data, mapping);
804 /* --------------------------------------------------------------------------
805 * Control transactions
807 * To make extended set operations as atomic as the hardware allows, controls
808 * are handled using begin/commit/rollback operations.
810 * At the beginning of a set request, uvc_ctrl_begin should be called to
811 * initialize the request. This function acquires the control lock.
813 * When setting a control, the new value is stored in the control data field
814 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
815 * later processing. If the UVC and V4L2 control sizes differ, the current
816 * value is loaded from the hardware before storing the new value in the data
819 * After processing all controls in the transaction, uvc_ctrl_commit or
820 * uvc_ctrl_rollback must be called to apply the pending changes to the
821 * hardware or revert them. When applying changes, all controls marked as
822 * dirty will be modified in the UVC device, and the dirty flag will be
823 * cleared. When reverting controls, the control data field
824 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
825 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
828 int uvc_ctrl_begin(struct uvc_video_device *video)
830 return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
833 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
834 struct uvc_entity *entity, int rollback)
836 struct uvc_control *ctrl;
843 for (i = 0; i < entity->ncontrols; ++i) {
844 ctrl = &entity->controls[i];
845 if (ctrl->info == NULL)
848 /* Reset the loaded flag for auto-update controls that were
849 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
850 * uvc_ctrl_get from using the cached value.
852 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
859 ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
860 dev->intfnum, ctrl->info->selector,
861 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
866 if (rollback || ret < 0)
867 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
868 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
880 int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
882 struct uvc_entity *entity;
885 /* Find the control. */
886 ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
890 list_for_each_entry(entity, &video->iterms, chain) {
891 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
896 list_for_each_entry(entity, &video->extensions, chain) {
897 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
903 mutex_unlock(&video->ctrl_mutex);
907 int uvc_ctrl_get(struct uvc_video_device *video,
908 struct v4l2_ext_control *xctrl)
910 struct uvc_control *ctrl;
911 struct uvc_control_mapping *mapping;
912 struct uvc_menu_info *menu;
916 ctrl = uvc_find_control(video, xctrl->id, &mapping);
917 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
921 ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
922 video->dev->intfnum, ctrl->info->selector,
923 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
931 xctrl->value = uvc_get_le_value(
932 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
934 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
935 menu = mapping->menu_info;
936 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
937 if (menu->value == xctrl->value) {
947 int uvc_ctrl_set(struct uvc_video_device *video,
948 struct v4l2_ext_control *xctrl)
950 struct uvc_control *ctrl;
951 struct uvc_control_mapping *mapping;
952 s32 value = xctrl->value;
955 ctrl = uvc_find_control(video, xctrl->id, &mapping);
956 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
959 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
960 if (value < 0 || value >= mapping->menu_count)
962 value = mapping->menu_info[value].value;
965 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
966 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
967 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
968 0, ctrl->info->size);
970 ret = uvc_query_ctrl(video->dev, GET_CUR,
971 ctrl->entity->id, video->dev->intfnum,
972 ctrl->info->selector,
973 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
983 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
984 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
988 uvc_set_le_value(value,
989 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
996 /* --------------------------------------------------------------------------
1000 int uvc_xu_ctrl_query(struct uvc_video_device *video,
1001 struct uvc_xu_control *xctrl, int set)
1003 struct uvc_entity *entity;
1004 struct uvc_control *ctrl = NULL;
1005 unsigned int i, found = 0;
1009 /* Find the extension unit. */
1010 list_for_each_entry(entity, &video->extensions, chain) {
1011 if (entity->id == xctrl->unit)
1015 if (entity->id != xctrl->unit) {
1016 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1021 /* Find the control. */
1022 for (i = 0; i < entity->ncontrols; ++i) {
1023 ctrl = &entity->controls[i];
1024 if (ctrl->info == NULL)
1027 if (ctrl->info->selector == xctrl->selector) {
1034 uvc_trace(UVC_TRACE_CONTROL,
1035 "Control " UVC_GUID_FORMAT "/%u not found.\n",
1036 UVC_GUID_ARGS(entity->extension.guidExtensionCode),
1041 /* Validate control data size. */
1042 if (ctrl->info->size != xctrl->size)
1045 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1046 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1049 if (mutex_lock_interruptible(&video->ctrl_mutex))
1050 return -ERESTARTSYS;
1052 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1053 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1055 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1057 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1062 ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
1063 video->dev->intfnum, xctrl->selector, data,
1068 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1075 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1076 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1079 mutex_unlock(&video->ctrl_mutex);
1083 /* --------------------------------------------------------------------------
1088 * Restore control values after resume, skipping controls that haven't been
1092 * - Don't restore modified controls that are back to their default value.
1093 * - Handle restore order (Auto-Exposure Mode should be restored before
1096 int uvc_ctrl_resume_device(struct uvc_device *dev)
1098 struct uvc_control *ctrl;
1099 struct uvc_entity *entity;
1103 /* Walk the entities list and restore controls when possible. */
1104 list_for_each_entry(entity, &dev->entities, list) {
1106 for (i = 0; i < entity->ncontrols; ++i) {
1107 ctrl = &entity->controls[i];
1109 if (ctrl->info == NULL || !ctrl->modified ||
1110 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1113 printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
1114 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
1115 ctrl->info->index, ctrl->info->selector);
1119 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1127 /* --------------------------------------------------------------------------
1128 * Control and mapping handling
1131 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1132 struct uvc_control_info *info)
1134 struct uvc_entity *entity;
1135 struct uvc_control *ctrl = NULL;
1139 list_for_each_entry(entity, &dev->entities, list) {
1140 if (!uvc_entity_match_guid(entity, info->entity))
1143 for (i = 0; i < entity->ncontrols; ++i) {
1144 ctrl = &entity->controls[i];
1145 if (ctrl->index == info->index) {
1158 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1159 /* Check if the device control information and length match
1160 * the user supplied information.
1166 if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
1167 dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
1168 uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1169 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1170 UVC_GUID_ARGS(info->entity), info->selector,
1175 if (info->size != le16_to_cpu(size)) {
1176 uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1177 "/%u size doesn't match user supplied "
1178 "value.\n", UVC_GUID_ARGS(info->entity),
1183 if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
1184 dev->intfnum, info->selector, &inf, 1)) < 0) {
1185 uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1186 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1187 UVC_GUID_ARGS(info->entity), info->selector,
1192 flags = info->flags;
1193 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1194 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1195 uvc_trace(UVC_TRACE_CONTROL, "Control "
1196 UVC_GUID_FORMAT "/%u flags don't match "
1197 "supported operations.\n",
1198 UVC_GUID_ARGS(info->entity), info->selector);
1204 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1205 uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1206 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1207 ctrl->info->selector, dev->udev->devpath, entity->id);
1211 * Add an item to the UVC control information list, and instantiate a control
1212 * structure for each device that supports the control.
1214 int uvc_ctrl_add_info(struct uvc_control_info *info)
1216 struct uvc_control_info *ctrl;
1217 struct uvc_device *dev;
1220 /* Find matching controls by walking the devices, entities and
1223 mutex_lock(&uvc_driver.ctrl_mutex);
1225 /* First check if the list contains a control matching the new one.
1226 * Bail out if it does.
1228 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1229 if (memcmp(ctrl->entity, info->entity, 16))
1232 if (ctrl->selector == info->selector) {
1233 uvc_trace(UVC_TRACE_CONTROL, "Control "
1234 UVC_GUID_FORMAT "/%u is already defined.\n",
1235 UVC_GUID_ARGS(info->entity), info->selector);
1239 if (ctrl->index == info->index) {
1240 uvc_trace(UVC_TRACE_CONTROL, "Control "
1241 UVC_GUID_FORMAT "/%u would overwrite index "
1242 "%d.\n", UVC_GUID_ARGS(info->entity),
1243 info->selector, info->index);
1249 list_for_each_entry(dev, &uvc_driver.devices, list)
1250 uvc_ctrl_add_ctrl(dev, info);
1252 INIT_LIST_HEAD(&info->mappings);
1253 list_add_tail(&info->list, &uvc_driver.controls);
1255 mutex_unlock(&uvc_driver.ctrl_mutex);
1259 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1261 struct uvc_control_info *info;
1262 struct uvc_control_mapping *map;
1265 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1266 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1267 "invalid control id 0x%08x\n", mapping->name,
1272 mutex_lock(&uvc_driver.ctrl_mutex);
1273 list_for_each_entry(info, &uvc_driver.controls, list) {
1274 if (memcmp(info->entity, mapping->entity, 16) ||
1275 info->selector != mapping->selector)
1278 if (info->size * 8 < mapping->size + mapping->offset) {
1279 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1280 "overflow control " UVC_GUID_FORMAT "/%u\n",
1281 mapping->name, UVC_GUID_ARGS(info->entity),
1287 /* Check if the list contains a mapping matching the new one.
1288 * Bail out if it does.
1290 list_for_each_entry(map, &info->mappings, list) {
1291 if (map->id == mapping->id) {
1292 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1293 "already defined.\n", mapping->name);
1299 mapping->ctrl = info;
1300 list_add_tail(&mapping->list, &info->mappings);
1301 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1302 UVC_GUID_FORMAT "/%u.\n", mapping->name,
1303 UVC_GUID_ARGS(info->entity), info->selector);
1309 mutex_unlock(&uvc_driver.ctrl_mutex);
1314 * Prune an entity of its bogus controls. This currently includes processing
1315 * unit auto controls for which no corresponding manual control is available.
1316 * Such auto controls make little sense if any, and are known to crash at
1317 * least the SiGma Micro webcam.
1320 uvc_ctrl_prune_entity(struct uvc_entity *entity)
1322 static const struct {
1326 { 2, 11 }, /* Hue */
1327 { 6, 12 }, /* White Balance Temperature */
1328 { 7, 13 }, /* White Balance Component */
1335 if (UVC_ENTITY_TYPE(entity) != VC_PROCESSING_UNIT)
1338 controls = entity->processing.bmControls;
1339 size = entity->processing.bControlSize;
1341 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
1342 if (blacklist[i].idx_auto >= 8 * size ||
1343 blacklist[i].idx_manual >= 8 * size)
1346 if (!uvc_test_bit(controls, blacklist[i].idx_auto) ||
1347 uvc_test_bit(controls, blacklist[i].idx_manual))
1350 uvc_trace(UVC_TRACE_CONTROL, "Auto control %u/%u has no "
1351 "matching manual control, removing it.\n", entity->id,
1352 blacklist[i].idx_auto);
1354 uvc_clear_bit(controls, blacklist[i].idx_auto);
1359 * Initialize device controls.
1361 int uvc_ctrl_init_device(struct uvc_device *dev)
1363 struct uvc_control_info *info;
1364 struct uvc_control *ctrl;
1365 struct uvc_entity *entity;
1368 /* Walk the entities list and instantiate controls */
1369 list_for_each_entry(entity, &dev->entities, list) {
1370 unsigned int bControlSize = 0, ncontrols = 0;
1371 __u8 *bmControls = NULL;
1373 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1374 bmControls = entity->extension.bmControls;
1375 bControlSize = entity->extension.bControlSize;
1376 } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
1377 bmControls = entity->processing.bmControls;
1378 bControlSize = entity->processing.bControlSize;
1379 } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
1380 bmControls = entity->camera.bmControls;
1381 bControlSize = entity->camera.bControlSize;
1384 if (dev->quirks & UVC_QUIRK_PRUNE_CONTROLS)
1385 uvc_ctrl_prune_entity(entity);
1387 for (i = 0; i < bControlSize; ++i)
1388 ncontrols += hweight8(bmControls[i]);
1393 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1394 if (entity->controls == NULL)
1397 entity->ncontrols = ncontrols;
1399 ctrl = entity->controls;
1400 for (i = 0; i < bControlSize * 8; ++i) {
1401 if (uvc_test_bit(bmControls, i) == 0)
1404 ctrl->entity = entity;
1410 /* Walk the controls info list and associate them with the device
1411 * controls, then add the device to the global device list. This has
1412 * to be done while holding the controls lock, to make sure
1413 * uvc_ctrl_add_info() will not get called in-between.
1415 mutex_lock(&uvc_driver.ctrl_mutex);
1416 list_for_each_entry(info, &uvc_driver.controls, list)
1417 uvc_ctrl_add_ctrl(dev, info);
1419 list_add_tail(&dev->list, &uvc_driver.devices);
1420 mutex_unlock(&uvc_driver.ctrl_mutex);
1426 * Cleanup device controls.
1428 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1430 struct uvc_entity *entity;
1433 /* Remove the device from the global devices list */
1434 mutex_lock(&uvc_driver.ctrl_mutex);
1435 if (dev->list.next != NULL)
1436 list_del(&dev->list);
1437 mutex_unlock(&uvc_driver.ctrl_mutex);
1439 list_for_each_entry(entity, &dev->entities, list) {
1440 for (i = 0; i < entity->ncontrols; ++i)
1441 kfree(entity->controls[i].data);
1443 kfree(entity->controls);
1447 void uvc_ctrl_init(void)
1449 struct uvc_control_info *ctrl = uvc_ctrls;
1450 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1451 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1452 struct uvc_control_mapping *mend =
1453 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1455 for (; ctrl < cend; ++ctrl)
1456 uvc_ctrl_add_info(ctrl);
1458 for (; mapping < mend; ++mapping)
1459 uvc_ctrl_add_mapping(mapping);