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_BACKLIGHT_COMPENSATION_CONTROL,
89 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
90 | UVC_CONTROL_RESTORE,
93 .entity = UVC_GUID_UVC_PROCESSING,
94 .selector = PU_GAIN_CONTROL,
97 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
98 | UVC_CONTROL_RESTORE,
101 .entity = UVC_GUID_UVC_PROCESSING,
102 .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
105 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
106 | UVC_CONTROL_RESTORE,
109 .entity = UVC_GUID_UVC_PROCESSING,
110 .selector = PU_HUE_AUTO_CONTROL,
113 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
114 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
117 .entity = UVC_GUID_UVC_CAMERA,
118 .selector = CT_AE_MODE_CONTROL,
121 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
122 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
123 | UVC_CONTROL_RESTORE,
126 .entity = UVC_GUID_UVC_CAMERA,
127 .selector = CT_AE_PRIORITY_CONTROL,
130 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
131 | UVC_CONTROL_RESTORE,
134 .entity = UVC_GUID_UVC_CAMERA,
135 .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
138 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
139 | UVC_CONTROL_RESTORE,
142 .entity = UVC_GUID_UVC_CAMERA,
143 .selector = CT_FOCUS_ABSOLUTE_CONTROL,
146 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
147 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
150 .entity = UVC_GUID_UVC_CAMERA,
151 .selector = CT_FOCUS_AUTO_CONTROL,
154 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
155 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
158 .entity = UVC_GUID_UVC_PROCESSING,
159 .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
162 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
163 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
166 .entity = UVC_GUID_UVC_PROCESSING,
167 .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
170 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
171 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
174 .entity = UVC_GUID_UVC_PROCESSING,
175 .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
178 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
179 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
182 .entity = UVC_GUID_UVC_PROCESSING,
183 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
186 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
187 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
191 static struct uvc_menu_info power_line_frequency_controls[] = {
197 static struct uvc_menu_info exposure_auto_controls[] = {
198 { 1, "Manual Mode" },
200 { 4, "Shutter Priority Mode" },
201 { 8, "Aperture Priority Mode" },
204 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
206 .id = V4L2_CID_BRIGHTNESS,
207 .name = "Brightness",
208 .entity = UVC_GUID_UVC_PROCESSING,
209 .selector = PU_BRIGHTNESS_CONTROL,
212 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
213 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
216 .id = V4L2_CID_CONTRAST,
218 .entity = UVC_GUID_UVC_PROCESSING,
219 .selector = PU_CONTRAST_CONTROL,
222 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
223 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
228 .entity = UVC_GUID_UVC_PROCESSING,
229 .selector = PU_HUE_CONTROL,
232 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
233 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
236 .id = V4L2_CID_SATURATION,
237 .name = "Saturation",
238 .entity = UVC_GUID_UVC_PROCESSING,
239 .selector = PU_SATURATION_CONTROL,
242 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
243 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
246 .id = V4L2_CID_SHARPNESS,
248 .entity = UVC_GUID_UVC_PROCESSING,
249 .selector = PU_SHARPNESS_CONTROL,
252 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
253 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
256 .id = V4L2_CID_GAMMA,
258 .entity = UVC_GUID_UVC_PROCESSING,
259 .selector = PU_GAMMA_CONTROL,
262 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
263 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
266 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
267 .name = "Backlight Compensation",
268 .entity = UVC_GUID_UVC_PROCESSING,
269 .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
272 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
273 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
278 .entity = UVC_GUID_UVC_PROCESSING,
279 .selector = PU_GAIN_CONTROL,
282 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
283 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
286 .id = V4L2_CID_POWER_LINE_FREQUENCY,
287 .name = "Power Line Frequency",
288 .entity = UVC_GUID_UVC_PROCESSING,
289 .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
292 .v4l2_type = V4L2_CTRL_TYPE_MENU,
293 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
294 .menu_info = power_line_frequency_controls,
295 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
298 .id = V4L2_CID_HUE_AUTO,
300 .entity = UVC_GUID_UVC_PROCESSING,
301 .selector = PU_HUE_AUTO_CONTROL,
304 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
305 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
308 .id = V4L2_CID_EXPOSURE_AUTO,
309 .name = "Exposure, Auto",
310 .entity = UVC_GUID_UVC_CAMERA,
311 .selector = CT_AE_MODE_CONTROL,
314 .v4l2_type = V4L2_CTRL_TYPE_MENU,
315 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
316 .menu_info = exposure_auto_controls,
317 .menu_count = ARRAY_SIZE(exposure_auto_controls),
320 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
321 .name = "Exposure, Auto Priority",
322 .entity = UVC_GUID_UVC_CAMERA,
323 .selector = CT_AE_PRIORITY_CONTROL,
326 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
327 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
330 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
331 .name = "Exposure (Absolute)",
332 .entity = UVC_GUID_UVC_CAMERA,
333 .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
336 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
337 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
340 .id = V4L2_CID_AUTO_WHITE_BALANCE,
341 .name = "White Balance Temperature, Auto",
342 .entity = UVC_GUID_UVC_PROCESSING,
343 .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
346 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
347 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
350 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
351 .name = "White Balance Temperature",
352 .entity = UVC_GUID_UVC_PROCESSING,
353 .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
356 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
357 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
360 .id = V4L2_CID_AUTO_WHITE_BALANCE,
361 .name = "White Balance Component, Auto",
362 .entity = UVC_GUID_UVC_PROCESSING,
363 .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
366 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
367 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
370 .id = V4L2_CID_BLUE_BALANCE,
371 .name = "White Balance Blue Component",
372 .entity = UVC_GUID_UVC_PROCESSING,
373 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
376 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
377 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
380 .id = V4L2_CID_RED_BALANCE,
381 .name = "White Balance Red Component",
382 .entity = UVC_GUID_UVC_PROCESSING,
383 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
386 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
387 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
390 .id = V4L2_CID_FOCUS_ABSOLUTE,
391 .name = "Focus (absolute)",
392 .entity = UVC_GUID_UVC_CAMERA,
393 .selector = CT_FOCUS_ABSOLUTE_CONTROL,
396 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
397 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
400 .id = V4L2_CID_FOCUS_AUTO,
401 .name = "Focus, Auto",
402 .entity = UVC_GUID_UVC_CAMERA,
403 .selector = CT_FOCUS_AUTO_CONTROL,
406 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
407 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
411 /* ------------------------------------------------------------------------
415 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
417 return ctrl->data + id * ctrl->info->size;
420 static inline int uvc_get_bit(const __u8 *data, int bit)
422 return (data[bit >> 3] >> (bit & 7)) & 1;
425 /* Extract the bit string specified by mapping->offset and mapping->size
426 * from the little-endian data stored at 'data' and return the result as
427 * a signed 32bit integer. Sign extension will be performed if the mapping
428 * references a signed data type.
430 static __s32 uvc_get_le_value(const __u8 *data,
431 struct uvc_control_mapping *mapping)
433 int bits = mapping->size;
434 int offset = mapping->offset;
440 mask = ((1LL << bits) - 1) << offset;
442 for (; bits > 0; data++) {
443 __u8 byte = *data & mask;
444 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
445 bits -= 8 - (offset > 0 ? offset : 0);
447 mask = (1 << bits) - 1;
450 /* Sign-extend the value if needed */
451 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
452 value |= -(value & (1 << (mapping->size - 1)));
457 /* Set the bit string specified by mapping->offset and mapping->size
458 * in the little-endian data stored at 'data' to the value 'value'.
460 static void uvc_set_le_value(__s32 value, __u8 *data,
461 struct uvc_control_mapping *mapping)
463 int bits = mapping->size;
464 int offset = mapping->offset;
470 for (; bits > 0; data++) {
471 mask = ((1LL << bits) - 1) << offset;
472 *data = (*data & ~mask) | ((value << offset) & mask);
473 value >>= offset ? offset : 8;
479 /* ------------------------------------------------------------------------
480 * Terminal and unit management
483 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
484 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
485 static const __u8 uvc_media_transport_input_guid[16] =
486 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
488 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
490 switch (UVC_ENTITY_TYPE(entity)) {
492 return memcmp(uvc_camera_guid, guid, 16) == 0;
494 case ITT_MEDIA_TRANSPORT_INPUT:
495 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
497 case VC_PROCESSING_UNIT:
498 return memcmp(uvc_processing_guid, guid, 16) == 0;
500 case VC_EXTENSION_UNIT:
501 return memcmp(entity->extension.guidExtensionCode,
509 /* ------------------------------------------------------------------------
513 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
514 struct uvc_control_mapping **mapping, struct uvc_control **control,
517 struct uvc_control *ctrl;
518 struct uvc_control_mapping *map;
524 for (i = 0; i < entity->ncontrols; ++i) {
525 ctrl = &entity->controls[i];
526 if (ctrl->info == NULL)
529 list_for_each_entry(map, &ctrl->info->mappings, list) {
530 if ((map->id == v4l2_id) && !next) {
536 if ((*mapping == NULL || (*mapping)->id > map->id) &&
537 (map->id > v4l2_id) && next) {
545 struct uvc_control *uvc_find_control(struct uvc_video_device *video,
546 __u32 v4l2_id, struct uvc_control_mapping **mapping)
548 struct uvc_control *ctrl = NULL;
549 struct uvc_entity *entity;
550 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
554 /* Mask the query flags. */
555 v4l2_id &= V4L2_CTRL_ID_MASK;
557 /* Find the control. */
558 __uvc_find_control(video->processing, v4l2_id, mapping, &ctrl, next);
562 list_for_each_entry(entity, &video->iterms, chain) {
563 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
568 list_for_each_entry(entity, &video->extensions, chain) {
569 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
574 if (ctrl == NULL && !next)
575 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
581 int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
582 struct v4l2_queryctrl *v4l2_ctrl)
584 struct uvc_control *ctrl;
585 struct uvc_control_mapping *mapping;
586 struct uvc_menu_info *menu;
591 ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
595 v4l2_ctrl->id = mapping->id;
596 v4l2_ctrl->type = mapping->v4l2_type;
597 strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
598 v4l2_ctrl->flags = 0;
600 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
601 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
603 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
604 if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
605 video->dev->intfnum, ctrl->info->selector,
606 &data, ctrl->info->size)) < 0)
608 v4l2_ctrl->default_value = uvc_get_le_value(data, mapping);
611 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
612 v4l2_ctrl->minimum = 0;
613 v4l2_ctrl->maximum = mapping->menu_count - 1;
616 menu = mapping->menu_info;
617 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
618 if (menu->value == v4l2_ctrl->default_value) {
619 v4l2_ctrl->default_value = i;
627 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
628 if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
629 video->dev->intfnum, ctrl->info->selector,
630 &data, ctrl->info->size)) < 0)
632 v4l2_ctrl->minimum = uvc_get_le_value(data, mapping);
634 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
635 if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
636 video->dev->intfnum, ctrl->info->selector,
637 &data, ctrl->info->size)) < 0)
639 v4l2_ctrl->maximum = uvc_get_le_value(data, mapping);
641 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
642 if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
643 video->dev->intfnum, ctrl->info->selector,
644 &data, ctrl->info->size)) < 0)
646 v4l2_ctrl->step = uvc_get_le_value(data, mapping);
653 /* --------------------------------------------------------------------------
654 * Control transactions
656 * To make extended set operations as atomic as the hardware allows, controls
657 * are handled using begin/commit/rollback operations.
659 * At the beginning of a set request, uvc_ctrl_begin should be called to
660 * initialize the request. This function acquires the control lock.
662 * When setting a control, the new value is stored in the control data field
663 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
664 * later processing. If the UVC and V4L2 control sizes differ, the current
665 * value is loaded from the hardware before storing the new value in the data
668 * After processing all controls in the transaction, uvc_ctrl_commit or
669 * uvc_ctrl_rollback must be called to apply the pending changes to the
670 * hardware or revert them. When applying changes, all controls marked as
671 * dirty will be modified in the UVC device, and the dirty flag will be
672 * cleared. When reverting controls, the control data field
673 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
674 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
677 int uvc_ctrl_begin(struct uvc_video_device *video)
679 return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
682 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
683 struct uvc_entity *entity, int rollback)
685 struct uvc_control *ctrl;
692 for (i = 0; i < entity->ncontrols; ++i) {
693 ctrl = &entity->controls[i];
694 if (ctrl->info == NULL || !ctrl->dirty)
698 ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
699 dev->intfnum, ctrl->info->selector,
700 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
705 if (rollback || ret < 0)
706 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
707 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
710 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
722 int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
724 struct uvc_entity *entity;
727 /* Find the control. */
728 ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
732 list_for_each_entry(entity, &video->iterms, chain) {
733 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
738 list_for_each_entry(entity, &video->extensions, chain) {
739 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
745 mutex_unlock(&video->ctrl_mutex);
749 int uvc_ctrl_get(struct uvc_video_device *video,
750 struct v4l2_ext_control *xctrl)
752 struct uvc_control *ctrl;
753 struct uvc_control_mapping *mapping;
754 struct uvc_menu_info *menu;
758 ctrl = uvc_find_control(video, xctrl->id, &mapping);
759 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
763 ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
764 video->dev->intfnum, ctrl->info->selector,
765 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
770 if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
774 xctrl->value = uvc_get_le_value(
775 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
777 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
778 menu = mapping->menu_info;
779 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
780 if (menu->value == xctrl->value) {
790 int uvc_ctrl_set(struct uvc_video_device *video,
791 struct v4l2_ext_control *xctrl)
793 struct uvc_control *ctrl;
794 struct uvc_control_mapping *mapping;
795 s32 value = xctrl->value;
798 ctrl = uvc_find_control(video, xctrl->id, &mapping);
799 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
802 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
803 if (value < 0 || value >= mapping->menu_count)
805 value = mapping->menu_info[value].value;
808 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
809 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
810 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
811 0, ctrl->info->size);
813 ret = uvc_query_ctrl(video->dev, GET_CUR,
814 ctrl->entity->id, video->dev->intfnum,
815 ctrl->info->selector,
816 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
822 if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
827 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
828 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
832 uvc_set_le_value(value,
833 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
840 /* --------------------------------------------------------------------------
844 int uvc_xu_ctrl_query(struct uvc_video_device *video,
845 struct uvc_xu_control *xctrl, int set)
847 struct uvc_entity *entity;
848 struct uvc_control *ctrl = NULL;
849 unsigned int i, found = 0;
853 /* Find the extension unit. */
854 list_for_each_entry(entity, &video->extensions, chain) {
855 if (entity->id == xctrl->unit)
859 if (entity->id != xctrl->unit) {
860 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
865 /* Find the control. */
866 for (i = 0; i < entity->ncontrols; ++i) {
867 ctrl = &entity->controls[i];
868 if (ctrl->info == NULL)
871 if (ctrl->info->selector == xctrl->selector) {
878 uvc_trace(UVC_TRACE_CONTROL,
879 "Control " UVC_GUID_FORMAT "/%u not found.\n",
880 UVC_GUID_ARGS(entity->extension.guidExtensionCode),
885 /* Validate control data size. */
886 if (ctrl->info->size != xctrl->size)
889 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
890 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
893 if (mutex_lock_interruptible(&video->ctrl_mutex))
896 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
897 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
899 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
901 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
906 ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
907 video->dev->intfnum, xctrl->selector, data,
912 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
919 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
920 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
923 mutex_unlock(&video->ctrl_mutex);
927 /* --------------------------------------------------------------------------
932 * Restore control values after resume, skipping controls that haven't been
936 * - Don't restore modified controls that are back to their default value.
937 * - Handle restore order (Auto-Exposure Mode should be restored before
940 int uvc_ctrl_resume_device(struct uvc_device *dev)
942 struct uvc_control *ctrl;
943 struct uvc_entity *entity;
947 /* Walk the entities list and restore controls when possible. */
948 list_for_each_entry(entity, &dev->entities, list) {
950 for (i = 0; i < entity->ncontrols; ++i) {
951 ctrl = &entity->controls[i];
953 if (ctrl->info == NULL || !ctrl->modified ||
954 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
957 printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
958 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
959 ctrl->info->index, ctrl->info->selector);
963 ret = uvc_ctrl_commit_entity(dev, entity, 0);
971 /* --------------------------------------------------------------------------
972 * Control and mapping handling
975 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
976 struct uvc_control_info *info)
978 struct uvc_entity *entity;
979 struct uvc_control *ctrl = NULL;
983 list_for_each_entry(entity, &dev->entities, list) {
984 if (!uvc_entity_match_guid(entity, info->entity))
987 for (i = 0; i < entity->ncontrols; ++i) {
988 ctrl = &entity->controls[i];
989 if (ctrl->index == info->index) {
1002 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1003 /* Check if the device control information and length match
1004 * the user supplied information.
1010 if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
1011 dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
1012 uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1013 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1014 UVC_GUID_ARGS(info->entity), info->selector,
1019 if (info->size != le16_to_cpu(size)) {
1020 uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1021 "/%u size doesn't match user supplied "
1022 "value.\n", UVC_GUID_ARGS(info->entity),
1027 if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
1028 dev->intfnum, info->selector, &inf, 1)) < 0) {
1029 uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1030 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1031 UVC_GUID_ARGS(info->entity), info->selector,
1036 flags = info->flags;
1037 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1038 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1039 uvc_trace(UVC_TRACE_CONTROL, "Control "
1040 UVC_GUID_FORMAT "/%u flags don't match "
1041 "supported operations.\n",
1042 UVC_GUID_ARGS(info->entity), info->selector);
1048 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1049 uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1050 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1051 ctrl->info->selector, dev->udev->devpath, entity->id);
1055 * Add an item to the UVC control information list, and instantiate a control
1056 * structure for each device that supports the control.
1058 int uvc_ctrl_add_info(struct uvc_control_info *info)
1060 struct uvc_control_info *ctrl;
1061 struct uvc_device *dev;
1064 /* Find matching controls by walking the devices, entities and
1067 mutex_lock(&uvc_driver.ctrl_mutex);
1069 /* First check if the list contains a control matching the new one.
1070 * Bail out if it does.
1072 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1073 if (memcmp(ctrl->entity, info->entity, 16))
1076 if (ctrl->selector == info->selector) {
1077 uvc_trace(UVC_TRACE_CONTROL, "Control "
1078 UVC_GUID_FORMAT "/%u is already defined.\n",
1079 UVC_GUID_ARGS(info->entity), info->selector);
1083 if (ctrl->index == info->index) {
1084 uvc_trace(UVC_TRACE_CONTROL, "Control "
1085 UVC_GUID_FORMAT "/%u would overwrite index "
1086 "%d.\n", UVC_GUID_ARGS(info->entity),
1087 info->selector, info->index);
1093 list_for_each_entry(dev, &uvc_driver.devices, list)
1094 uvc_ctrl_add_ctrl(dev, info);
1096 INIT_LIST_HEAD(&info->mappings);
1097 list_add_tail(&info->list, &uvc_driver.controls);
1099 mutex_unlock(&uvc_driver.ctrl_mutex);
1103 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1105 struct uvc_control_info *info;
1106 struct uvc_control_mapping *map;
1109 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1110 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1111 "invalid control id 0x%08x\n", mapping->name,
1116 mutex_lock(&uvc_driver.ctrl_mutex);
1117 list_for_each_entry(info, &uvc_driver.controls, list) {
1118 if (memcmp(info->entity, mapping->entity, 16) ||
1119 info->selector != mapping->selector)
1122 if (info->size * 8 < mapping->size + mapping->offset) {
1123 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1124 "overflow control " UVC_GUID_FORMAT "/%u\n",
1125 mapping->name, UVC_GUID_ARGS(info->entity),
1131 /* Check if the list contains a mapping matching the new one.
1132 * Bail out if it does.
1134 list_for_each_entry(map, &info->mappings, list) {
1135 if (map->id == mapping->id) {
1136 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1137 "already defined.\n", mapping->name);
1143 mapping->ctrl = info;
1144 list_add_tail(&mapping->list, &info->mappings);
1145 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1146 UVC_GUID_FORMAT "/%u.\n", mapping->name,
1147 UVC_GUID_ARGS(info->entity), info->selector);
1153 mutex_unlock(&uvc_driver.ctrl_mutex);
1158 * Initialize device controls.
1160 int uvc_ctrl_init_device(struct uvc_device *dev)
1162 struct uvc_control_info *info;
1163 struct uvc_control *ctrl;
1164 struct uvc_entity *entity;
1167 /* Walk the entities list and instantiate controls */
1168 list_for_each_entry(entity, &dev->entities, list) {
1169 unsigned int bControlSize = 0, ncontrols = 0;
1170 __u8 *bmControls = NULL;
1172 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1173 bmControls = entity->extension.bmControls;
1174 bControlSize = entity->extension.bControlSize;
1175 } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
1176 bmControls = entity->processing.bmControls;
1177 bControlSize = entity->processing.bControlSize;
1178 } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
1179 bmControls = entity->camera.bmControls;
1180 bControlSize = entity->camera.bControlSize;
1183 for (i = 0; i < bControlSize; ++i)
1184 ncontrols += hweight8(bmControls[i]);
1189 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1190 if (entity->controls == NULL)
1193 entity->ncontrols = ncontrols;
1195 ctrl = entity->controls;
1196 for (i = 0; i < bControlSize * 8; ++i) {
1197 if (uvc_get_bit(bmControls, i) == 0)
1200 ctrl->entity = entity;
1206 /* Walk the controls info list and associate them with the device
1207 * controls, then add the device to the global device list. This has
1208 * to be done while holding the controls lock, to make sure
1209 * uvc_ctrl_add_info() will not get called in-between.
1211 mutex_lock(&uvc_driver.ctrl_mutex);
1212 list_for_each_entry(info, &uvc_driver.controls, list)
1213 uvc_ctrl_add_ctrl(dev, info);
1215 list_add_tail(&dev->list, &uvc_driver.devices);
1216 mutex_unlock(&uvc_driver.ctrl_mutex);
1222 * Cleanup device controls.
1224 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1226 struct uvc_entity *entity;
1229 /* Remove the device from the global devices list */
1230 mutex_lock(&uvc_driver.ctrl_mutex);
1231 if (dev->list.next != NULL)
1232 list_del(&dev->list);
1233 mutex_unlock(&uvc_driver.ctrl_mutex);
1235 list_for_each_entry(entity, &dev->entities, list) {
1236 for (i = 0; i < entity->ncontrols; ++i)
1237 kfree(entity->controls[i].data);
1239 kfree(entity->controls);
1243 void uvc_ctrl_init(void)
1245 struct uvc_control_info *ctrl = uvc_ctrls;
1246 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1247 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1248 struct uvc_control_mapping *mend =
1249 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1251 for (; ctrl < cend; ++ctrl)
1252 uvc_ctrl_add_info(ctrl);
1254 for (; mapping < mend; ++mapping)
1255 uvc_ctrl_add_mapping(mapping);