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_get_bit(const __u8
*data
, int bit
)
548 return (data
[bit
>> 3] >> (bit
& 7)) & 1;
551 /* Extract the bit string specified by mapping->offset and mapping->size
552 * from the little-endian data stored at 'data' and return the result as
553 * a signed 32bit integer. Sign extension will be performed if the mapping
554 * references a signed data type.
556 static __s32
uvc_get_le_value(const __u8
*data
,
557 struct uvc_control_mapping
*mapping
)
559 int bits
= mapping
->size
;
560 int offset
= mapping
->offset
;
566 mask
= ((1LL << bits
) - 1) << offset
;
568 for (; bits
> 0; data
++) {
569 __u8 byte
= *data
& mask
;
570 value
|= offset
> 0 ? (byte
>> offset
) : (byte
<< (-offset
));
571 bits
-= 8 - (offset
> 0 ? offset
: 0);
573 mask
= (1 << bits
) - 1;
576 /* Sign-extend the value if needed */
577 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
578 value
|= -(value
& (1 << (mapping
->size
- 1)));
583 /* Set the bit string specified by mapping->offset and mapping->size
584 * in the little-endian data stored at 'data' to the value 'value'.
586 static void uvc_set_le_value(__s32 value
, __u8
*data
,
587 struct uvc_control_mapping
*mapping
)
589 int bits
= mapping
->size
;
590 int offset
= mapping
->offset
;
596 for (; bits
> 0; data
++) {
597 mask
= ((1LL << bits
) - 1) << offset
;
598 *data
= (*data
& ~mask
) | ((value
<< offset
) & mask
);
599 value
>>= offset
? offset
: 8;
605 /* ------------------------------------------------------------------------
606 * Terminal and unit management
609 static const __u8 uvc_processing_guid
[16] = UVC_GUID_UVC_PROCESSING
;
610 static const __u8 uvc_camera_guid
[16] = UVC_GUID_UVC_CAMERA
;
611 static const __u8 uvc_media_transport_input_guid
[16] =
612 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT
;
614 static int uvc_entity_match_guid(struct uvc_entity
*entity
, __u8 guid
[16])
616 switch (UVC_ENTITY_TYPE(entity
)) {
618 return memcmp(uvc_camera_guid
, guid
, 16) == 0;
620 case ITT_MEDIA_TRANSPORT_INPUT
:
621 return memcmp(uvc_media_transport_input_guid
, guid
, 16) == 0;
623 case VC_PROCESSING_UNIT
:
624 return memcmp(uvc_processing_guid
, guid
, 16) == 0;
626 case VC_EXTENSION_UNIT
:
627 return memcmp(entity
->extension
.guidExtensionCode
,
635 /* ------------------------------------------------------------------------
639 static void __uvc_find_control(struct uvc_entity
*entity
, __u32 v4l2_id
,
640 struct uvc_control_mapping
**mapping
, struct uvc_control
**control
,
643 struct uvc_control
*ctrl
;
644 struct uvc_control_mapping
*map
;
650 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
651 ctrl
= &entity
->controls
[i
];
652 if (ctrl
->info
== NULL
)
655 list_for_each_entry(map
, &ctrl
->info
->mappings
, list
) {
656 if ((map
->id
== v4l2_id
) && !next
) {
662 if ((*mapping
== NULL
|| (*mapping
)->id
> map
->id
) &&
663 (map
->id
> v4l2_id
) && next
) {
671 struct uvc_control
*uvc_find_control(struct uvc_video_device
*video
,
672 __u32 v4l2_id
, struct uvc_control_mapping
**mapping
)
674 struct uvc_control
*ctrl
= NULL
;
675 struct uvc_entity
*entity
;
676 int next
= v4l2_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
680 /* Mask the query flags. */
681 v4l2_id
&= V4L2_CTRL_ID_MASK
;
683 /* Find the control. */
684 __uvc_find_control(video
->processing
, v4l2_id
, mapping
, &ctrl
, next
);
688 list_for_each_entry(entity
, &video
->iterms
, chain
) {
689 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
694 list_for_each_entry(entity
, &video
->extensions
, chain
) {
695 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
700 if (ctrl
== NULL
&& !next
)
701 uvc_trace(UVC_TRACE_CONTROL
, "Control 0x%08x not found.\n",
707 int uvc_query_v4l2_ctrl(struct uvc_video_device
*video
,
708 struct v4l2_queryctrl
*v4l2_ctrl
)
710 struct uvc_control
*ctrl
;
711 struct uvc_control_mapping
*mapping
;
712 struct uvc_menu_info
*menu
;
717 ctrl
= uvc_find_control(video
, v4l2_ctrl
->id
, &mapping
);
721 data
= kmalloc(ctrl
->info
->size
, GFP_KERNEL
);
725 memset(v4l2_ctrl
, 0, sizeof *v4l2_ctrl
);
726 v4l2_ctrl
->id
= mapping
->id
;
727 v4l2_ctrl
->type
= mapping
->v4l2_type
;
728 strncpy(v4l2_ctrl
->name
, mapping
->name
, sizeof v4l2_ctrl
->name
);
729 v4l2_ctrl
->flags
= 0;
731 if (!(ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
))
732 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
734 if (ctrl
->info
->flags
& UVC_CONTROL_GET_DEF
) {
735 if ((ret
= uvc_query_ctrl(video
->dev
, GET_DEF
, ctrl
->entity
->id
,
736 video
->dev
->intfnum
, ctrl
->info
->selector
,
737 data
, ctrl
->info
->size
)) < 0)
739 v4l2_ctrl
->default_value
= uvc_get_le_value(data
, mapping
);
742 switch (mapping
->v4l2_type
) {
743 case V4L2_CTRL_TYPE_MENU
:
744 v4l2_ctrl
->minimum
= 0;
745 v4l2_ctrl
->maximum
= mapping
->menu_count
- 1;
748 menu
= mapping
->menu_info
;
749 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
750 if (menu
->value
== v4l2_ctrl
->default_value
) {
751 v4l2_ctrl
->default_value
= i
;
759 case V4L2_CTRL_TYPE_BOOLEAN
:
760 v4l2_ctrl
->minimum
= 0;
761 v4l2_ctrl
->maximum
= 1;
770 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MIN
) {
771 if ((ret
= uvc_query_ctrl(video
->dev
, GET_MIN
, ctrl
->entity
->id
,
772 video
->dev
->intfnum
, ctrl
->info
->selector
,
773 data
, ctrl
->info
->size
)) < 0)
775 v4l2_ctrl
->minimum
= uvc_get_le_value(data
, mapping
);
777 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MAX
) {
778 if ((ret
= uvc_query_ctrl(video
->dev
, GET_MAX
, ctrl
->entity
->id
,
779 video
->dev
->intfnum
, ctrl
->info
->selector
,
780 data
, ctrl
->info
->size
)) < 0)
782 v4l2_ctrl
->maximum
= uvc_get_le_value(data
, mapping
);
784 if (ctrl
->info
->flags
& UVC_CONTROL_GET_RES
) {
785 if ((ret
= uvc_query_ctrl(video
->dev
, GET_RES
, ctrl
->entity
->id
,
786 video
->dev
->intfnum
, ctrl
->info
->selector
,
787 data
, ctrl
->info
->size
)) < 0)
789 v4l2_ctrl
->step
= uvc_get_le_value(data
, mapping
);
799 /* --------------------------------------------------------------------------
800 * Control transactions
802 * To make extended set operations as atomic as the hardware allows, controls
803 * are handled using begin/commit/rollback operations.
805 * At the beginning of a set request, uvc_ctrl_begin should be called to
806 * initialize the request. This function acquires the control lock.
808 * When setting a control, the new value is stored in the control data field
809 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
810 * later processing. If the UVC and V4L2 control sizes differ, the current
811 * value is loaded from the hardware before storing the new value in the data
814 * After processing all controls in the transaction, uvc_ctrl_commit or
815 * uvc_ctrl_rollback must be called to apply the pending changes to the
816 * hardware or revert them. When applying changes, all controls marked as
817 * dirty will be modified in the UVC device, and the dirty flag will be
818 * cleared. When reverting controls, the control data field
819 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
820 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
823 int uvc_ctrl_begin(struct uvc_video_device
*video
)
825 return mutex_lock_interruptible(&video
->ctrl_mutex
) ? -ERESTARTSYS
: 0;
828 static int uvc_ctrl_commit_entity(struct uvc_device
*dev
,
829 struct uvc_entity
*entity
, int rollback
)
831 struct uvc_control
*ctrl
;
838 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
839 ctrl
= &entity
->controls
[i
];
840 if (ctrl
->info
== NULL
)
843 /* Reset the loaded flag for auto-update controls that were
844 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
845 * uvc_ctrl_get from using the cached value.
847 if (ctrl
->info
->flags
& UVC_CONTROL_AUTO_UPDATE
)
854 ret
= uvc_query_ctrl(dev
, SET_CUR
, ctrl
->entity
->id
,
855 dev
->intfnum
, ctrl
->info
->selector
,
856 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
861 if (rollback
|| ret
< 0)
862 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
863 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
875 int __uvc_ctrl_commit(struct uvc_video_device
*video
, int rollback
)
877 struct uvc_entity
*entity
;
880 /* Find the control. */
881 ret
= uvc_ctrl_commit_entity(video
->dev
, video
->processing
, rollback
);
885 list_for_each_entry(entity
, &video
->iterms
, chain
) {
886 ret
= uvc_ctrl_commit_entity(video
->dev
, entity
, rollback
);
891 list_for_each_entry(entity
, &video
->extensions
, chain
) {
892 ret
= uvc_ctrl_commit_entity(video
->dev
, entity
, rollback
);
898 mutex_unlock(&video
->ctrl_mutex
);
902 int uvc_ctrl_get(struct uvc_video_device
*video
,
903 struct v4l2_ext_control
*xctrl
)
905 struct uvc_control
*ctrl
;
906 struct uvc_control_mapping
*mapping
;
907 struct uvc_menu_info
*menu
;
911 ctrl
= uvc_find_control(video
, xctrl
->id
, &mapping
);
912 if (ctrl
== NULL
|| (ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
) == 0)
916 ret
= uvc_query_ctrl(video
->dev
, GET_CUR
, ctrl
->entity
->id
,
917 video
->dev
->intfnum
, ctrl
->info
->selector
,
918 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
926 xctrl
->value
= uvc_get_le_value(
927 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
), mapping
);
929 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
930 menu
= mapping
->menu_info
;
931 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
932 if (menu
->value
== xctrl
->value
) {
942 int uvc_ctrl_set(struct uvc_video_device
*video
,
943 struct v4l2_ext_control
*xctrl
)
945 struct uvc_control
*ctrl
;
946 struct uvc_control_mapping
*mapping
;
947 s32 value
= xctrl
->value
;
950 ctrl
= uvc_find_control(video
, xctrl
->id
, &mapping
);
951 if (ctrl
== NULL
|| (ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
) == 0)
954 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
955 if (value
< 0 || value
>= mapping
->menu_count
)
957 value
= mapping
->menu_info
[value
].value
;
960 if (!ctrl
->loaded
&& (ctrl
->info
->size
* 8) != mapping
->size
) {
961 if ((ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
) == 0) {
962 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
963 0, ctrl
->info
->size
);
965 ret
= uvc_query_ctrl(video
->dev
, GET_CUR
,
966 ctrl
->entity
->id
, video
->dev
->intfnum
,
967 ctrl
->info
->selector
,
968 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
978 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
979 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
983 uvc_set_le_value(value
,
984 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
), mapping
);
991 /* --------------------------------------------------------------------------
995 int uvc_xu_ctrl_query(struct uvc_video_device
*video
,
996 struct uvc_xu_control
*xctrl
, int set
)
998 struct uvc_entity
*entity
;
999 struct uvc_control
*ctrl
= NULL
;
1000 unsigned int i
, found
= 0;
1004 /* Find the extension unit. */
1005 list_for_each_entry(entity
, &video
->extensions
, chain
) {
1006 if (entity
->id
== xctrl
->unit
)
1010 if (entity
->id
!= xctrl
->unit
) {
1011 uvc_trace(UVC_TRACE_CONTROL
, "Extension unit %u not found.\n",
1016 /* Find the control. */
1017 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1018 ctrl
= &entity
->controls
[i
];
1019 if (ctrl
->info
== NULL
)
1022 if (ctrl
->info
->selector
== xctrl
->selector
) {
1029 uvc_trace(UVC_TRACE_CONTROL
,
1030 "Control " UVC_GUID_FORMAT
"/%u not found.\n",
1031 UVC_GUID_ARGS(entity
->extension
.guidExtensionCode
),
1036 /* Validate control data size. */
1037 if (ctrl
->info
->size
!= xctrl
->size
)
1040 if ((set
&& !(ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
)) ||
1041 (!set
&& !(ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
)))
1044 if (mutex_lock_interruptible(&video
->ctrl_mutex
))
1045 return -ERESTARTSYS
;
1047 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1048 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1050 data
= uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
);
1052 if (set
&& copy_from_user(data
, xctrl
->data
, xctrl
->size
)) {
1057 ret
= uvc_query_ctrl(video
->dev
, set
? SET_CUR
: GET_CUR
, xctrl
->unit
,
1058 video
->dev
->intfnum
, xctrl
->selector
, data
,
1063 if (!set
&& copy_to_user(xctrl
->data
, data
, xctrl
->size
)) {
1070 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1071 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1074 mutex_unlock(&video
->ctrl_mutex
);
1078 /* --------------------------------------------------------------------------
1083 * Restore control values after resume, skipping controls that haven't been
1087 * - Don't restore modified controls that are back to their default value.
1088 * - Handle restore order (Auto-Exposure Mode should be restored before
1091 int uvc_ctrl_resume_device(struct uvc_device
*dev
)
1093 struct uvc_control
*ctrl
;
1094 struct uvc_entity
*entity
;
1098 /* Walk the entities list and restore controls when possible. */
1099 list_for_each_entry(entity
, &dev
->entities
, list
) {
1101 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1102 ctrl
= &entity
->controls
[i
];
1104 if (ctrl
->info
== NULL
|| !ctrl
->modified
||
1105 (ctrl
->info
->flags
& UVC_CONTROL_RESTORE
) == 0)
1108 printk(KERN_INFO
"restoring control " UVC_GUID_FORMAT
1109 "/%u/%u\n", UVC_GUID_ARGS(ctrl
->info
->entity
),
1110 ctrl
->info
->index
, ctrl
->info
->selector
);
1114 ret
= uvc_ctrl_commit_entity(dev
, entity
, 0);
1122 /* --------------------------------------------------------------------------
1123 * Control and mapping handling
1126 static void uvc_ctrl_add_ctrl(struct uvc_device
*dev
,
1127 struct uvc_control_info
*info
)
1129 struct uvc_entity
*entity
;
1130 struct uvc_control
*ctrl
= NULL
;
1134 list_for_each_entry(entity
, &dev
->entities
, list
) {
1135 if (!uvc_entity_match_guid(entity
, info
->entity
))
1138 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1139 ctrl
= &entity
->controls
[i
];
1140 if (ctrl
->index
== info
->index
) {
1153 if (UVC_ENTITY_TYPE(entity
) == VC_EXTENSION_UNIT
) {
1154 /* Check if the device control information and length match
1155 * the user supplied information.
1161 if ((ret
= uvc_query_ctrl(dev
, GET_LEN
, ctrl
->entity
->id
,
1162 dev
->intfnum
, info
->selector
, (__u8
*)&size
, 2)) < 0) {
1163 uvc_trace(UVC_TRACE_CONTROL
, "GET_LEN failed on "
1164 "control " UVC_GUID_FORMAT
"/%u (%d).\n",
1165 UVC_GUID_ARGS(info
->entity
), info
->selector
,
1170 if (info
->size
!= le16_to_cpu(size
)) {
1171 uvc_trace(UVC_TRACE_CONTROL
, "Control " UVC_GUID_FORMAT
1172 "/%u size doesn't match user supplied "
1173 "value.\n", UVC_GUID_ARGS(info
->entity
),
1178 if ((ret
= uvc_query_ctrl(dev
, GET_INFO
, ctrl
->entity
->id
,
1179 dev
->intfnum
, info
->selector
, &inf
, 1)) < 0) {
1180 uvc_trace(UVC_TRACE_CONTROL
, "GET_INFO failed on "
1181 "control " UVC_GUID_FORMAT
"/%u (%d).\n",
1182 UVC_GUID_ARGS(info
->entity
), info
->selector
,
1187 flags
= info
->flags
;
1188 if (((flags
& UVC_CONTROL_GET_CUR
) && !(inf
& (1 << 0))) ||
1189 ((flags
& UVC_CONTROL_SET_CUR
) && !(inf
& (1 << 1)))) {
1190 uvc_trace(UVC_TRACE_CONTROL
, "Control "
1191 UVC_GUID_FORMAT
"/%u flags don't match "
1192 "supported operations.\n",
1193 UVC_GUID_ARGS(info
->entity
), info
->selector
);
1199 ctrl
->data
= kmalloc(ctrl
->info
->size
* UVC_CTRL_NDATA
, GFP_KERNEL
);
1200 uvc_trace(UVC_TRACE_CONTROL
, "Added control " UVC_GUID_FORMAT
"/%u "
1201 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl
->info
->entity
),
1202 ctrl
->info
->selector
, dev
->udev
->devpath
, entity
->id
);
1206 * Add an item to the UVC control information list, and instantiate a control
1207 * structure for each device that supports the control.
1209 int uvc_ctrl_add_info(struct uvc_control_info
*info
)
1211 struct uvc_control_info
*ctrl
;
1212 struct uvc_device
*dev
;
1215 /* Find matching controls by walking the devices, entities and
1218 mutex_lock(&uvc_driver
.ctrl_mutex
);
1220 /* First check if the list contains a control matching the new one.
1221 * Bail out if it does.
1223 list_for_each_entry(ctrl
, &uvc_driver
.controls
, list
) {
1224 if (memcmp(ctrl
->entity
, info
->entity
, 16))
1227 if (ctrl
->selector
== info
->selector
) {
1228 uvc_trace(UVC_TRACE_CONTROL
, "Control "
1229 UVC_GUID_FORMAT
"/%u is already defined.\n",
1230 UVC_GUID_ARGS(info
->entity
), info
->selector
);
1234 if (ctrl
->index
== info
->index
) {
1235 uvc_trace(UVC_TRACE_CONTROL
, "Control "
1236 UVC_GUID_FORMAT
"/%u would overwrite index "
1237 "%d.\n", UVC_GUID_ARGS(info
->entity
),
1238 info
->selector
, info
->index
);
1244 list_for_each_entry(dev
, &uvc_driver
.devices
, list
)
1245 uvc_ctrl_add_ctrl(dev
, info
);
1247 INIT_LIST_HEAD(&info
->mappings
);
1248 list_add_tail(&info
->list
, &uvc_driver
.controls
);
1250 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1254 int uvc_ctrl_add_mapping(struct uvc_control_mapping
*mapping
)
1256 struct uvc_control_info
*info
;
1257 struct uvc_control_mapping
*map
;
1260 if (mapping
->id
& ~V4L2_CTRL_ID_MASK
) {
1261 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s' with "
1262 "invalid control id 0x%08x\n", mapping
->name
,
1267 mutex_lock(&uvc_driver
.ctrl_mutex
);
1268 list_for_each_entry(info
, &uvc_driver
.controls
, list
) {
1269 if (memcmp(info
->entity
, mapping
->entity
, 16) ||
1270 info
->selector
!= mapping
->selector
)
1273 if (info
->size
* 8 < mapping
->size
+ mapping
->offset
) {
1274 uvc_trace(UVC_TRACE_CONTROL
, "Mapping '%s' would "
1275 "overflow control " UVC_GUID_FORMAT
"/%u\n",
1276 mapping
->name
, UVC_GUID_ARGS(info
->entity
),
1282 /* Check if the list contains a mapping matching the new one.
1283 * Bail out if it does.
1285 list_for_each_entry(map
, &info
->mappings
, list
) {
1286 if (map
->id
== mapping
->id
) {
1287 uvc_trace(UVC_TRACE_CONTROL
, "Mapping '%s' is "
1288 "already defined.\n", mapping
->name
);
1294 mapping
->ctrl
= info
;
1295 list_add_tail(&mapping
->list
, &info
->mappings
);
1296 uvc_trace(UVC_TRACE_CONTROL
, "Adding mapping %s to control "
1297 UVC_GUID_FORMAT
"/%u.\n", mapping
->name
,
1298 UVC_GUID_ARGS(info
->entity
), info
->selector
);
1304 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1309 * Initialize device controls.
1311 int uvc_ctrl_init_device(struct uvc_device
*dev
)
1313 struct uvc_control_info
*info
;
1314 struct uvc_control
*ctrl
;
1315 struct uvc_entity
*entity
;
1318 /* Walk the entities list and instantiate controls */
1319 list_for_each_entry(entity
, &dev
->entities
, list
) {
1320 unsigned int bControlSize
= 0, ncontrols
= 0;
1321 __u8
*bmControls
= NULL
;
1323 if (UVC_ENTITY_TYPE(entity
) == VC_EXTENSION_UNIT
) {
1324 bmControls
= entity
->extension
.bmControls
;
1325 bControlSize
= entity
->extension
.bControlSize
;
1326 } else if (UVC_ENTITY_TYPE(entity
) == VC_PROCESSING_UNIT
) {
1327 bmControls
= entity
->processing
.bmControls
;
1328 bControlSize
= entity
->processing
.bControlSize
;
1329 } else if (UVC_ENTITY_TYPE(entity
) == ITT_CAMERA
) {
1330 bmControls
= entity
->camera
.bmControls
;
1331 bControlSize
= entity
->camera
.bControlSize
;
1334 for (i
= 0; i
< bControlSize
; ++i
)
1335 ncontrols
+= hweight8(bmControls
[i
]);
1340 entity
->controls
= kzalloc(ncontrols
*sizeof *ctrl
, GFP_KERNEL
);
1341 if (entity
->controls
== NULL
)
1344 entity
->ncontrols
= ncontrols
;
1346 ctrl
= entity
->controls
;
1347 for (i
= 0; i
< bControlSize
* 8; ++i
) {
1348 if (uvc_get_bit(bmControls
, i
) == 0)
1351 ctrl
->entity
= entity
;
1357 /* Walk the controls info list and associate them with the device
1358 * controls, then add the device to the global device list. This has
1359 * to be done while holding the controls lock, to make sure
1360 * uvc_ctrl_add_info() will not get called in-between.
1362 mutex_lock(&uvc_driver
.ctrl_mutex
);
1363 list_for_each_entry(info
, &uvc_driver
.controls
, list
)
1364 uvc_ctrl_add_ctrl(dev
, info
);
1366 list_add_tail(&dev
->list
, &uvc_driver
.devices
);
1367 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1373 * Cleanup device controls.
1375 void uvc_ctrl_cleanup_device(struct uvc_device
*dev
)
1377 struct uvc_entity
*entity
;
1380 /* Remove the device from the global devices list */
1381 mutex_lock(&uvc_driver
.ctrl_mutex
);
1382 if (dev
->list
.next
!= NULL
)
1383 list_del(&dev
->list
);
1384 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1386 list_for_each_entry(entity
, &dev
->entities
, list
) {
1387 for (i
= 0; i
< entity
->ncontrols
; ++i
)
1388 kfree(entity
->controls
[i
].data
);
1390 kfree(entity
->controls
);
1394 void uvc_ctrl_init(void)
1396 struct uvc_control_info
*ctrl
= uvc_ctrls
;
1397 struct uvc_control_info
*cend
= ctrl
+ ARRAY_SIZE(uvc_ctrls
);
1398 struct uvc_control_mapping
*mapping
= uvc_ctrl_mappings
;
1399 struct uvc_control_mapping
*mend
=
1400 mapping
+ ARRAY_SIZE(uvc_ctrl_mappings
);
1402 for (; ctrl
< cend
; ++ctrl
)
1403 uvc_ctrl_add_info(ctrl
);
1405 for (; mapping
< mend
; ++mapping
)
1406 uvc_ctrl_add_mapping(mapping
);