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[] = {
199 { 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 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
596 v4l2_ctrl->id = mapping->id;
597 v4l2_ctrl->type = mapping->v4l2_type;
598 strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
599 v4l2_ctrl->flags = 0;
601 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
602 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
604 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
605 if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
606 video->dev->intfnum, ctrl->info->selector,
607 &data, ctrl->info->size)) < 0)
609 v4l2_ctrl->default_value = uvc_get_le_value(data, mapping);
612 switch (mapping->v4l2_type) {
613 case V4L2_CTRL_TYPE_MENU:
614 v4l2_ctrl->minimum = 0;
615 v4l2_ctrl->maximum = mapping->menu_count - 1;
618 menu = mapping->menu_info;
619 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
620 if (menu->value == v4l2_ctrl->default_value) {
621 v4l2_ctrl->default_value = i;
628 case V4L2_CTRL_TYPE_BOOLEAN:
629 v4l2_ctrl->minimum = 0;
630 v4l2_ctrl->maximum = 1;
638 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
639 if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
640 video->dev->intfnum, ctrl->info->selector,
641 &data, ctrl->info->size)) < 0)
643 v4l2_ctrl->minimum = uvc_get_le_value(data, mapping);
645 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
646 if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
647 video->dev->intfnum, ctrl->info->selector,
648 &data, ctrl->info->size)) < 0)
650 v4l2_ctrl->maximum = uvc_get_le_value(data, mapping);
652 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
653 if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
654 video->dev->intfnum, ctrl->info->selector,
655 &data, ctrl->info->size)) < 0)
657 v4l2_ctrl->step = uvc_get_le_value(data, mapping);
664 /* --------------------------------------------------------------------------
665 * Control transactions
667 * To make extended set operations as atomic as the hardware allows, controls
668 * are handled using begin/commit/rollback operations.
670 * At the beginning of a set request, uvc_ctrl_begin should be called to
671 * initialize the request. This function acquires the control lock.
673 * When setting a control, the new value is stored in the control data field
674 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
675 * later processing. If the UVC and V4L2 control sizes differ, the current
676 * value is loaded from the hardware before storing the new value in the data
679 * After processing all controls in the transaction, uvc_ctrl_commit or
680 * uvc_ctrl_rollback must be called to apply the pending changes to the
681 * hardware or revert them. When applying changes, all controls marked as
682 * dirty will be modified in the UVC device, and the dirty flag will be
683 * cleared. When reverting controls, the control data field
684 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
685 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
688 int uvc_ctrl_begin(struct uvc_video_device *video)
690 return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
693 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
694 struct uvc_entity *entity, int rollback)
696 struct uvc_control *ctrl;
703 for (i = 0; i < entity->ncontrols; ++i) {
704 ctrl = &entity->controls[i];
705 if (ctrl->info == NULL || !ctrl->dirty)
709 ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
710 dev->intfnum, ctrl->info->selector,
711 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
716 if (rollback || ret < 0)
717 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
718 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
721 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
733 int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
735 struct uvc_entity *entity;
738 /* Find the control. */
739 ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
743 list_for_each_entry(entity, &video->iterms, chain) {
744 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
749 list_for_each_entry(entity, &video->extensions, chain) {
750 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
756 mutex_unlock(&video->ctrl_mutex);
760 int uvc_ctrl_get(struct uvc_video_device *video,
761 struct v4l2_ext_control *xctrl)
763 struct uvc_control *ctrl;
764 struct uvc_control_mapping *mapping;
765 struct uvc_menu_info *menu;
769 ctrl = uvc_find_control(video, xctrl->id, &mapping);
770 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
774 ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
775 video->dev->intfnum, ctrl->info->selector,
776 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
781 if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
785 xctrl->value = uvc_get_le_value(
786 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
788 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
789 menu = mapping->menu_info;
790 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
791 if (menu->value == xctrl->value) {
801 int uvc_ctrl_set(struct uvc_video_device *video,
802 struct v4l2_ext_control *xctrl)
804 struct uvc_control *ctrl;
805 struct uvc_control_mapping *mapping;
806 s32 value = xctrl->value;
809 ctrl = uvc_find_control(video, xctrl->id, &mapping);
810 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
813 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
814 if (value < 0 || value >= mapping->menu_count)
816 value = mapping->menu_info[value].value;
819 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
820 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
821 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
822 0, ctrl->info->size);
824 ret = uvc_query_ctrl(video->dev, GET_CUR,
825 ctrl->entity->id, video->dev->intfnum,
826 ctrl->info->selector,
827 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
833 if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
838 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
839 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
843 uvc_set_le_value(value,
844 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
851 /* --------------------------------------------------------------------------
855 int uvc_xu_ctrl_query(struct uvc_video_device *video,
856 struct uvc_xu_control *xctrl, int set)
858 struct uvc_entity *entity;
859 struct uvc_control *ctrl = NULL;
860 unsigned int i, found = 0;
864 /* Find the extension unit. */
865 list_for_each_entry(entity, &video->extensions, chain) {
866 if (entity->id == xctrl->unit)
870 if (entity->id != xctrl->unit) {
871 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
876 /* Find the control. */
877 for (i = 0; i < entity->ncontrols; ++i) {
878 ctrl = &entity->controls[i];
879 if (ctrl->info == NULL)
882 if (ctrl->info->selector == xctrl->selector) {
889 uvc_trace(UVC_TRACE_CONTROL,
890 "Control " UVC_GUID_FORMAT "/%u not found.\n",
891 UVC_GUID_ARGS(entity->extension.guidExtensionCode),
896 /* Validate control data size. */
897 if (ctrl->info->size != xctrl->size)
900 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
901 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
904 if (mutex_lock_interruptible(&video->ctrl_mutex))
907 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
908 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
910 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
912 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
917 ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
918 video->dev->intfnum, xctrl->selector, data,
923 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
930 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
931 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
934 mutex_unlock(&video->ctrl_mutex);
938 /* --------------------------------------------------------------------------
943 * Restore control values after resume, skipping controls that haven't been
947 * - Don't restore modified controls that are back to their default value.
948 * - Handle restore order (Auto-Exposure Mode should be restored before
951 int uvc_ctrl_resume_device(struct uvc_device *dev)
953 struct uvc_control *ctrl;
954 struct uvc_entity *entity;
958 /* Walk the entities list and restore controls when possible. */
959 list_for_each_entry(entity, &dev->entities, list) {
961 for (i = 0; i < entity->ncontrols; ++i) {
962 ctrl = &entity->controls[i];
964 if (ctrl->info == NULL || !ctrl->modified ||
965 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
968 printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
969 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
970 ctrl->info->index, ctrl->info->selector);
974 ret = uvc_ctrl_commit_entity(dev, entity, 0);
982 /* --------------------------------------------------------------------------
983 * Control and mapping handling
986 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
987 struct uvc_control_info *info)
989 struct uvc_entity *entity;
990 struct uvc_control *ctrl = NULL;
994 list_for_each_entry(entity, &dev->entities, list) {
995 if (!uvc_entity_match_guid(entity, info->entity))
998 for (i = 0; i < entity->ncontrols; ++i) {
999 ctrl = &entity->controls[i];
1000 if (ctrl->index == info->index) {
1013 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1014 /* Check if the device control information and length match
1015 * the user supplied information.
1021 if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
1022 dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
1023 uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1024 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1025 UVC_GUID_ARGS(info->entity), info->selector,
1030 if (info->size != le16_to_cpu(size)) {
1031 uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1032 "/%u size doesn't match user supplied "
1033 "value.\n", UVC_GUID_ARGS(info->entity),
1038 if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
1039 dev->intfnum, info->selector, &inf, 1)) < 0) {
1040 uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1041 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1042 UVC_GUID_ARGS(info->entity), info->selector,
1047 flags = info->flags;
1048 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1049 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1050 uvc_trace(UVC_TRACE_CONTROL, "Control "
1051 UVC_GUID_FORMAT "/%u flags don't match "
1052 "supported operations.\n",
1053 UVC_GUID_ARGS(info->entity), info->selector);
1059 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1060 uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1061 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1062 ctrl->info->selector, dev->udev->devpath, entity->id);
1066 * Add an item to the UVC control information list, and instantiate a control
1067 * structure for each device that supports the control.
1069 int uvc_ctrl_add_info(struct uvc_control_info *info)
1071 struct uvc_control_info *ctrl;
1072 struct uvc_device *dev;
1075 /* Find matching controls by walking the devices, entities and
1078 mutex_lock(&uvc_driver.ctrl_mutex);
1080 /* First check if the list contains a control matching the new one.
1081 * Bail out if it does.
1083 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1084 if (memcmp(ctrl->entity, info->entity, 16))
1087 if (ctrl->selector == info->selector) {
1088 uvc_trace(UVC_TRACE_CONTROL, "Control "
1089 UVC_GUID_FORMAT "/%u is already defined.\n",
1090 UVC_GUID_ARGS(info->entity), info->selector);
1094 if (ctrl->index == info->index) {
1095 uvc_trace(UVC_TRACE_CONTROL, "Control "
1096 UVC_GUID_FORMAT "/%u would overwrite index "
1097 "%d.\n", UVC_GUID_ARGS(info->entity),
1098 info->selector, info->index);
1104 list_for_each_entry(dev, &uvc_driver.devices, list)
1105 uvc_ctrl_add_ctrl(dev, info);
1107 INIT_LIST_HEAD(&info->mappings);
1108 list_add_tail(&info->list, &uvc_driver.controls);
1110 mutex_unlock(&uvc_driver.ctrl_mutex);
1114 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1116 struct uvc_control_info *info;
1117 struct uvc_control_mapping *map;
1120 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1121 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1122 "invalid control id 0x%08x\n", mapping->name,
1127 mutex_lock(&uvc_driver.ctrl_mutex);
1128 list_for_each_entry(info, &uvc_driver.controls, list) {
1129 if (memcmp(info->entity, mapping->entity, 16) ||
1130 info->selector != mapping->selector)
1133 if (info->size * 8 < mapping->size + mapping->offset) {
1134 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1135 "overflow control " UVC_GUID_FORMAT "/%u\n",
1136 mapping->name, UVC_GUID_ARGS(info->entity),
1142 /* Check if the list contains a mapping matching the new one.
1143 * Bail out if it does.
1145 list_for_each_entry(map, &info->mappings, list) {
1146 if (map->id == mapping->id) {
1147 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1148 "already defined.\n", mapping->name);
1154 mapping->ctrl = info;
1155 list_add_tail(&mapping->list, &info->mappings);
1156 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1157 UVC_GUID_FORMAT "/%u.\n", mapping->name,
1158 UVC_GUID_ARGS(info->entity), info->selector);
1164 mutex_unlock(&uvc_driver.ctrl_mutex);
1169 * Initialize device controls.
1171 int uvc_ctrl_init_device(struct uvc_device *dev)
1173 struct uvc_control_info *info;
1174 struct uvc_control *ctrl;
1175 struct uvc_entity *entity;
1178 /* Walk the entities list and instantiate controls */
1179 list_for_each_entry(entity, &dev->entities, list) {
1180 unsigned int bControlSize = 0, ncontrols = 0;
1181 __u8 *bmControls = NULL;
1183 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1184 bmControls = entity->extension.bmControls;
1185 bControlSize = entity->extension.bControlSize;
1186 } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
1187 bmControls = entity->processing.bmControls;
1188 bControlSize = entity->processing.bControlSize;
1189 } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
1190 bmControls = entity->camera.bmControls;
1191 bControlSize = entity->camera.bControlSize;
1194 for (i = 0; i < bControlSize; ++i)
1195 ncontrols += hweight8(bmControls[i]);
1200 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1201 if (entity->controls == NULL)
1204 entity->ncontrols = ncontrols;
1206 ctrl = entity->controls;
1207 for (i = 0; i < bControlSize * 8; ++i) {
1208 if (uvc_get_bit(bmControls, i) == 0)
1211 ctrl->entity = entity;
1217 /* Walk the controls info list and associate them with the device
1218 * controls, then add the device to the global device list. This has
1219 * to be done while holding the controls lock, to make sure
1220 * uvc_ctrl_add_info() will not get called in-between.
1222 mutex_lock(&uvc_driver.ctrl_mutex);
1223 list_for_each_entry(info, &uvc_driver.controls, list)
1224 uvc_ctrl_add_ctrl(dev, info);
1226 list_add_tail(&dev->list, &uvc_driver.devices);
1227 mutex_unlock(&uvc_driver.ctrl_mutex);
1233 * Cleanup device controls.
1235 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1237 struct uvc_entity *entity;
1240 /* Remove the device from the global devices list */
1241 mutex_lock(&uvc_driver.ctrl_mutex);
1242 if (dev->list.next != NULL)
1243 list_del(&dev->list);
1244 mutex_unlock(&uvc_driver.ctrl_mutex);
1246 list_for_each_entry(entity, &dev->entities, list) {
1247 for (i = 0; i < entity->ncontrols; ++i)
1248 kfree(entity->controls[i].data);
1250 kfree(entity->controls);
1254 void uvc_ctrl_init(void)
1256 struct uvc_control_info *ctrl = uvc_ctrls;
1257 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1258 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1259 struct uvc_control_mapping *mend =
1260 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1262 for (; ctrl < cend; ++ctrl)
1263 uvc_ctrl_add_info(ctrl);
1265 for (; mapping < mend; ++mapping)
1266 uvc_ctrl_add_mapping(mapping);