2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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/list.h>
16 #include <linux/module.h>
17 #include <linux/slab.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_DATA_CURRENT 0
28 #define UVC_CTRL_DATA_BACKUP 1
29 #define UVC_CTRL_DATA_MIN 2
30 #define UVC_CTRL_DATA_MAX 3
31 #define UVC_CTRL_DATA_RES 4
32 #define UVC_CTRL_DATA_DEF 5
33 #define UVC_CTRL_DATA_LAST 6
35 /* ------------------------------------------------------------------------
39 static struct uvc_control_info uvc_ctrls
[] = {
41 .entity
= UVC_GUID_UVC_PROCESSING
,
42 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
45 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
46 | UVC_CONTROL_RESTORE
,
49 .entity
= UVC_GUID_UVC_PROCESSING
,
50 .selector
= UVC_PU_CONTRAST_CONTROL
,
53 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
54 | UVC_CONTROL_RESTORE
,
57 .entity
= UVC_GUID_UVC_PROCESSING
,
58 .selector
= UVC_PU_HUE_CONTROL
,
61 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
62 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
65 .entity
= UVC_GUID_UVC_PROCESSING
,
66 .selector
= UVC_PU_SATURATION_CONTROL
,
69 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
70 | UVC_CONTROL_RESTORE
,
73 .entity
= UVC_GUID_UVC_PROCESSING
,
74 .selector
= UVC_PU_SHARPNESS_CONTROL
,
77 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
78 | UVC_CONTROL_RESTORE
,
81 .entity
= UVC_GUID_UVC_PROCESSING
,
82 .selector
= UVC_PU_GAMMA_CONTROL
,
85 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
86 | UVC_CONTROL_RESTORE
,
89 .entity
= UVC_GUID_UVC_PROCESSING
,
90 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
93 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
94 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
97 .entity
= UVC_GUID_UVC_PROCESSING
,
98 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
101 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
102 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
105 .entity
= UVC_GUID_UVC_PROCESSING
,
106 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
109 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
110 | UVC_CONTROL_RESTORE
,
113 .entity
= UVC_GUID_UVC_PROCESSING
,
114 .selector
= UVC_PU_GAIN_CONTROL
,
117 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
118 | UVC_CONTROL_RESTORE
,
121 .entity
= UVC_GUID_UVC_PROCESSING
,
122 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
125 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
126 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
129 .entity
= UVC_GUID_UVC_PROCESSING
,
130 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
133 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
134 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
137 .entity
= UVC_GUID_UVC_PROCESSING
,
138 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
141 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
142 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
145 .entity
= UVC_GUID_UVC_PROCESSING
,
146 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
149 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
150 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
153 .entity
= UVC_GUID_UVC_PROCESSING
,
154 .selector
= UVC_PU_DIGITAL_MULTIPLIER_CONTROL
,
157 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
158 | UVC_CONTROL_RESTORE
,
161 .entity
= UVC_GUID_UVC_PROCESSING
,
162 .selector
= UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL
,
165 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
166 | UVC_CONTROL_RESTORE
,
169 .entity
= UVC_GUID_UVC_PROCESSING
,
170 .selector
= UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL
,
173 .flags
= UVC_CONTROL_GET_CUR
,
176 .entity
= UVC_GUID_UVC_PROCESSING
,
177 .selector
= UVC_PU_ANALOG_LOCK_STATUS_CONTROL
,
180 .flags
= UVC_CONTROL_GET_CUR
,
183 .entity
= UVC_GUID_UVC_CAMERA
,
184 .selector
= UVC_CT_SCANNING_MODE_CONTROL
,
187 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
188 | UVC_CONTROL_RESTORE
,
191 .entity
= UVC_GUID_UVC_CAMERA
,
192 .selector
= UVC_CT_AE_MODE_CONTROL
,
195 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
196 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_GET_RES
197 | UVC_CONTROL_RESTORE
,
200 .entity
= UVC_GUID_UVC_CAMERA
,
201 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
204 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
205 | UVC_CONTROL_RESTORE
,
208 .entity
= UVC_GUID_UVC_CAMERA
,
209 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
212 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
213 | UVC_CONTROL_RESTORE
,
216 .entity
= UVC_GUID_UVC_CAMERA
,
217 .selector
= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL
,
220 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_RESTORE
,
223 .entity
= UVC_GUID_UVC_CAMERA
,
224 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
227 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
228 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
231 .entity
= UVC_GUID_UVC_CAMERA
,
232 .selector
= UVC_CT_FOCUS_RELATIVE_CONTROL
,
235 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_MIN
236 | UVC_CONTROL_GET_MAX
| UVC_CONTROL_GET_RES
237 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_AUTO_UPDATE
,
240 .entity
= UVC_GUID_UVC_CAMERA
,
241 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
244 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
245 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
248 .entity
= UVC_GUID_UVC_CAMERA
,
249 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
252 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_AUTO_UPDATE
,
255 .entity
= UVC_GUID_UVC_CAMERA
,
256 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
259 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
260 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
263 .entity
= UVC_GUID_UVC_CAMERA
,
264 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
267 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_MIN
268 | UVC_CONTROL_GET_MAX
| UVC_CONTROL_GET_RES
269 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_AUTO_UPDATE
,
272 .entity
= UVC_GUID_UVC_CAMERA
,
273 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
276 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
277 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
280 .entity
= UVC_GUID_UVC_CAMERA
,
281 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
284 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_MIN
285 | UVC_CONTROL_GET_MAX
| UVC_CONTROL_GET_RES
286 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_AUTO_UPDATE
,
289 .entity
= UVC_GUID_UVC_CAMERA
,
290 .selector
= UVC_CT_ROLL_ABSOLUTE_CONTROL
,
293 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
294 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
297 .entity
= UVC_GUID_UVC_CAMERA
,
298 .selector
= UVC_CT_ROLL_RELATIVE_CONTROL
,
301 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_MIN
302 | UVC_CONTROL_GET_MAX
| UVC_CONTROL_GET_RES
303 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_AUTO_UPDATE
,
306 .entity
= UVC_GUID_UVC_CAMERA
,
307 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
310 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
311 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
314 .entity
= UVC_GUID_UVC_CAMERA
,
315 .selector
= UVC_CT_PRIVACY_CONTROL
,
318 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
319 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
323 static struct uvc_menu_info power_line_frequency_controls
[] = {
329 static struct uvc_menu_info exposure_auto_controls
[] = {
331 { 1, "Manual Mode" },
332 { 4, "Shutter Priority Mode" },
333 { 8, "Aperture Priority Mode" },
336 static __s32
uvc_ctrl_get_zoom(struct uvc_control_mapping
*mapping
,
337 __u8 query
, const __u8
*data
)
339 __s8 zoom
= (__s8
)data
[0];
343 return (zoom
== 0) ? 0 : (zoom
> 0 ? data
[2] : -data
[2]);
354 static void uvc_ctrl_set_zoom(struct uvc_control_mapping
*mapping
,
355 __s32 value
, __u8
*data
)
357 data
[0] = value
== 0 ? 0 : (value
> 0) ? 1 : 0xff;
358 data
[2] = min((int)abs(value
), 0xff);
361 static struct uvc_control_mapping uvc_ctrl_mappings
[] = {
363 .id
= V4L2_CID_BRIGHTNESS
,
364 .name
= "Brightness",
365 .entity
= UVC_GUID_UVC_PROCESSING
,
366 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
369 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
370 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
373 .id
= V4L2_CID_CONTRAST
,
375 .entity
= UVC_GUID_UVC_PROCESSING
,
376 .selector
= UVC_PU_CONTRAST_CONTROL
,
379 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
380 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
385 .entity
= UVC_GUID_UVC_PROCESSING
,
386 .selector
= UVC_PU_HUE_CONTROL
,
389 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
390 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
393 .id
= V4L2_CID_SATURATION
,
394 .name
= "Saturation",
395 .entity
= UVC_GUID_UVC_PROCESSING
,
396 .selector
= UVC_PU_SATURATION_CONTROL
,
399 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
400 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
403 .id
= V4L2_CID_SHARPNESS
,
405 .entity
= UVC_GUID_UVC_PROCESSING
,
406 .selector
= UVC_PU_SHARPNESS_CONTROL
,
409 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
410 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
413 .id
= V4L2_CID_GAMMA
,
415 .entity
= UVC_GUID_UVC_PROCESSING
,
416 .selector
= UVC_PU_GAMMA_CONTROL
,
419 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
420 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
423 .id
= V4L2_CID_BACKLIGHT_COMPENSATION
,
424 .name
= "Backlight Compensation",
425 .entity
= UVC_GUID_UVC_PROCESSING
,
426 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
429 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
430 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
435 .entity
= UVC_GUID_UVC_PROCESSING
,
436 .selector
= UVC_PU_GAIN_CONTROL
,
439 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
440 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
443 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
444 .name
= "Power Line Frequency",
445 .entity
= UVC_GUID_UVC_PROCESSING
,
446 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
449 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
450 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
451 .menu_info
= power_line_frequency_controls
,
452 .menu_count
= ARRAY_SIZE(power_line_frequency_controls
),
455 .id
= V4L2_CID_HUE_AUTO
,
457 .entity
= UVC_GUID_UVC_PROCESSING
,
458 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
461 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
462 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
465 .id
= V4L2_CID_EXPOSURE_AUTO
,
466 .name
= "Exposure, Auto",
467 .entity
= UVC_GUID_UVC_CAMERA
,
468 .selector
= UVC_CT_AE_MODE_CONTROL
,
471 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
472 .data_type
= UVC_CTRL_DATA_TYPE_BITMASK
,
473 .menu_info
= exposure_auto_controls
,
474 .menu_count
= ARRAY_SIZE(exposure_auto_controls
),
477 .id
= V4L2_CID_EXPOSURE_AUTO_PRIORITY
,
478 .name
= "Exposure, Auto Priority",
479 .entity
= UVC_GUID_UVC_CAMERA
,
480 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
483 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
484 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
487 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
488 .name
= "Exposure (Absolute)",
489 .entity
= UVC_GUID_UVC_CAMERA
,
490 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
493 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
494 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
497 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
498 .name
= "White Balance Temperature, Auto",
499 .entity
= UVC_GUID_UVC_PROCESSING
,
500 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
503 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
504 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
507 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
508 .name
= "White Balance Temperature",
509 .entity
= UVC_GUID_UVC_PROCESSING
,
510 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
513 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
514 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
517 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
518 .name
= "White Balance Component, Auto",
519 .entity
= UVC_GUID_UVC_PROCESSING
,
520 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
523 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
524 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
527 .id
= V4L2_CID_BLUE_BALANCE
,
528 .name
= "White Balance Blue Component",
529 .entity
= UVC_GUID_UVC_PROCESSING
,
530 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
533 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
534 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
537 .id
= V4L2_CID_RED_BALANCE
,
538 .name
= "White Balance Red Component",
539 .entity
= UVC_GUID_UVC_PROCESSING
,
540 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
543 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
544 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
547 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
548 .name
= "Focus (absolute)",
549 .entity
= UVC_GUID_UVC_CAMERA
,
550 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
553 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
554 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
557 .id
= V4L2_CID_FOCUS_AUTO
,
558 .name
= "Focus, Auto",
559 .entity
= UVC_GUID_UVC_CAMERA
,
560 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
563 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
564 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
567 .id
= V4L2_CID_IRIS_ABSOLUTE
,
568 .name
= "Iris, Absolute",
569 .entity
= UVC_GUID_UVC_CAMERA
,
570 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
573 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
574 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
577 .id
= V4L2_CID_IRIS_RELATIVE
,
578 .name
= "Iris, Relative",
579 .entity
= UVC_GUID_UVC_CAMERA
,
580 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
583 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
584 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
587 .id
= V4L2_CID_ZOOM_ABSOLUTE
,
588 .name
= "Zoom, Absolute",
589 .entity
= UVC_GUID_UVC_CAMERA
,
590 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
593 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
594 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
597 .id
= V4L2_CID_ZOOM_CONTINUOUS
,
598 .name
= "Zoom, Continuous",
599 .entity
= UVC_GUID_UVC_CAMERA
,
600 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
603 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
604 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
605 .get
= uvc_ctrl_get_zoom
,
606 .set
= uvc_ctrl_set_zoom
,
609 .id
= V4L2_CID_PAN_ABSOLUTE
,
610 .name
= "Pan (Absolute)",
611 .entity
= UVC_GUID_UVC_CAMERA
,
612 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
615 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
616 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
619 .id
= V4L2_CID_TILT_ABSOLUTE
,
620 .name
= "Tilt (Absolute)",
621 .entity
= UVC_GUID_UVC_CAMERA
,
622 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
625 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
626 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
629 .id
= V4L2_CID_PRIVACY
,
631 .entity
= UVC_GUID_UVC_CAMERA
,
632 .selector
= UVC_CT_PRIVACY_CONTROL
,
635 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
636 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
640 /* ------------------------------------------------------------------------
644 static inline __u8
*uvc_ctrl_data(struct uvc_control
*ctrl
, int id
)
646 return ctrl
->uvc_data
+ id
* ctrl
->info
.size
;
649 static inline int uvc_test_bit(const __u8
*data
, int bit
)
651 return (data
[bit
>> 3] >> (bit
& 7)) & 1;
654 static inline void uvc_clear_bit(__u8
*data
, int bit
)
656 data
[bit
>> 3] &= ~(1 << (bit
& 7));
659 /* Extract the bit string specified by mapping->offset and mapping->size
660 * from the little-endian data stored at 'data' and return the result as
661 * a signed 32bit integer. Sign extension will be performed if the mapping
662 * references a signed data type.
664 static __s32
uvc_get_le_value(struct uvc_control_mapping
*mapping
,
665 __u8 query
, const __u8
*data
)
667 int bits
= mapping
->size
;
668 int offset
= mapping
->offset
;
674 mask
= ((1LL << bits
) - 1) << offset
;
676 for (; bits
> 0; data
++) {
677 __u8 byte
= *data
& mask
;
678 value
|= offset
> 0 ? (byte
>> offset
) : (byte
<< (-offset
));
679 bits
-= 8 - (offset
> 0 ? offset
: 0);
681 mask
= (1 << bits
) - 1;
684 /* Sign-extend the value if needed. */
685 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
686 value
|= -(value
& (1 << (mapping
->size
- 1)));
691 /* Set the bit string specified by mapping->offset and mapping->size
692 * in the little-endian data stored at 'data' to the value 'value'.
694 static void uvc_set_le_value(struct uvc_control_mapping
*mapping
,
695 __s32 value
, __u8
*data
)
697 int bits
= mapping
->size
;
698 int offset
= mapping
->offset
;
701 /* According to the v4l2 spec, writing any value to a button control
702 * should result in the action belonging to the button control being
703 * triggered. UVC devices however want to see a 1 written -> override
706 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_BUTTON
)
712 for (; bits
> 0; data
++) {
713 mask
= ((1LL << bits
) - 1) << offset
;
714 *data
= (*data
& ~mask
) | ((value
<< offset
) & mask
);
715 value
>>= offset
? offset
: 8;
721 /* ------------------------------------------------------------------------
722 * Terminal and unit management
725 static const __u8 uvc_processing_guid
[16] = UVC_GUID_UVC_PROCESSING
;
726 static const __u8 uvc_camera_guid
[16] = UVC_GUID_UVC_CAMERA
;
727 static const __u8 uvc_media_transport_input_guid
[16] =
728 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT
;
730 static int uvc_entity_match_guid(const struct uvc_entity
*entity
,
733 switch (UVC_ENTITY_TYPE(entity
)) {
735 return memcmp(uvc_camera_guid
, guid
, 16) == 0;
737 case UVC_ITT_MEDIA_TRANSPORT_INPUT
:
738 return memcmp(uvc_media_transport_input_guid
, guid
, 16) == 0;
740 case UVC_VC_PROCESSING_UNIT
:
741 return memcmp(uvc_processing_guid
, guid
, 16) == 0;
743 case UVC_VC_EXTENSION_UNIT
:
744 return memcmp(entity
->extension
.guidExtensionCode
,
752 /* ------------------------------------------------------------------------
756 static void __uvc_find_control(struct uvc_entity
*entity
, __u32 v4l2_id
,
757 struct uvc_control_mapping
**mapping
, struct uvc_control
**control
,
760 struct uvc_control
*ctrl
;
761 struct uvc_control_mapping
*map
;
767 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
768 ctrl
= &entity
->controls
[i
];
769 if (!ctrl
->initialized
)
772 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
773 if ((map
->id
== v4l2_id
) && !next
) {
779 if ((*mapping
== NULL
|| (*mapping
)->id
> map
->id
) &&
780 (map
->id
> v4l2_id
) && next
) {
788 struct uvc_control
*uvc_find_control(struct uvc_video_chain
*chain
,
789 __u32 v4l2_id
, struct uvc_control_mapping
**mapping
)
791 struct uvc_control
*ctrl
= NULL
;
792 struct uvc_entity
*entity
;
793 int next
= v4l2_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
797 /* Mask the query flags. */
798 v4l2_id
&= V4L2_CTRL_ID_MASK
;
800 /* Find the control. */
801 list_for_each_entry(entity
, &chain
->entities
, chain
) {
802 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
807 if (ctrl
== NULL
&& !next
)
808 uvc_trace(UVC_TRACE_CONTROL
, "Control 0x%08x not found.\n",
814 static int uvc_ctrl_populate_cache(struct uvc_video_chain
*chain
,
815 struct uvc_control
*ctrl
)
819 if (ctrl
->info
.flags
& UVC_CONTROL_GET_DEF
) {
820 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_DEF
, ctrl
->entity
->id
,
821 chain
->dev
->intfnum
, ctrl
->info
.selector
,
822 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
),
828 if (ctrl
->info
.flags
& UVC_CONTROL_GET_MIN
) {
829 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MIN
, ctrl
->entity
->id
,
830 chain
->dev
->intfnum
, ctrl
->info
.selector
,
831 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
),
836 if (ctrl
->info
.flags
& UVC_CONTROL_GET_MAX
) {
837 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MAX
, ctrl
->entity
->id
,
838 chain
->dev
->intfnum
, ctrl
->info
.selector
,
839 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
),
844 if (ctrl
->info
.flags
& UVC_CONTROL_GET_RES
) {
845 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_RES
, ctrl
->entity
->id
,
846 chain
->dev
->intfnum
, ctrl
->info
.selector
,
847 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
),
857 int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
858 struct v4l2_queryctrl
*v4l2_ctrl
)
860 struct uvc_control
*ctrl
;
861 struct uvc_control_mapping
*mapping
;
862 struct uvc_menu_info
*menu
;
866 ctrl
= uvc_find_control(chain
, v4l2_ctrl
->id
, &mapping
);
870 memset(v4l2_ctrl
, 0, sizeof *v4l2_ctrl
);
871 v4l2_ctrl
->id
= mapping
->id
;
872 v4l2_ctrl
->type
= mapping
->v4l2_type
;
873 strlcpy(v4l2_ctrl
->name
, mapping
->name
, sizeof v4l2_ctrl
->name
);
874 v4l2_ctrl
->flags
= 0;
876 if (!(ctrl
->info
.flags
& UVC_CONTROL_GET_CUR
))
877 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_WRITE_ONLY
;
878 if (!(ctrl
->info
.flags
& UVC_CONTROL_SET_CUR
))
879 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
882 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
887 if (ctrl
->info
.flags
& UVC_CONTROL_GET_DEF
) {
888 v4l2_ctrl
->default_value
= mapping
->get(mapping
, UVC_GET_DEF
,
889 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
));
892 switch (mapping
->v4l2_type
) {
893 case V4L2_CTRL_TYPE_MENU
:
894 v4l2_ctrl
->minimum
= 0;
895 v4l2_ctrl
->maximum
= mapping
->menu_count
- 1;
898 menu
= mapping
->menu_info
;
899 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
900 if (menu
->value
== v4l2_ctrl
->default_value
) {
901 v4l2_ctrl
->default_value
= i
;
908 case V4L2_CTRL_TYPE_BOOLEAN
:
909 v4l2_ctrl
->minimum
= 0;
910 v4l2_ctrl
->maximum
= 1;
914 case V4L2_CTRL_TYPE_BUTTON
:
915 v4l2_ctrl
->minimum
= 0;
916 v4l2_ctrl
->maximum
= 0;
924 if (ctrl
->info
.flags
& UVC_CONTROL_GET_MIN
)
925 v4l2_ctrl
->minimum
= mapping
->get(mapping
, UVC_GET_MIN
,
926 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
928 if (ctrl
->info
.flags
& UVC_CONTROL_GET_MAX
)
929 v4l2_ctrl
->maximum
= mapping
->get(mapping
, UVC_GET_MAX
,
930 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
932 if (ctrl
->info
.flags
& UVC_CONTROL_GET_RES
)
933 v4l2_ctrl
->step
= mapping
->get(mapping
, UVC_GET_RES
,
934 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
940 /* --------------------------------------------------------------------------
941 * Control transactions
943 * To make extended set operations as atomic as the hardware allows, controls
944 * are handled using begin/commit/rollback operations.
946 * At the beginning of a set request, uvc_ctrl_begin should be called to
947 * initialize the request. This function acquires the control lock.
949 * When setting a control, the new value is stored in the control data field
950 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
951 * later processing. If the UVC and V4L2 control sizes differ, the current
952 * value is loaded from the hardware before storing the new value in the data
955 * After processing all controls in the transaction, uvc_ctrl_commit or
956 * uvc_ctrl_rollback must be called to apply the pending changes to the
957 * hardware or revert them. When applying changes, all controls marked as
958 * dirty will be modified in the UVC device, and the dirty flag will be
959 * cleared. When reverting controls, the control data field
960 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
961 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
964 int uvc_ctrl_begin(struct uvc_video_chain
*chain
)
966 return mutex_lock_interruptible(&chain
->ctrl_mutex
) ? -ERESTARTSYS
: 0;
969 static int uvc_ctrl_commit_entity(struct uvc_device
*dev
,
970 struct uvc_entity
*entity
, int rollback
)
972 struct uvc_control
*ctrl
;
979 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
980 ctrl
= &entity
->controls
[i
];
981 if (!ctrl
->initialized
)
984 /* Reset the loaded flag for auto-update controls that were
985 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
986 * uvc_ctrl_get from using the cached value.
988 if (ctrl
->info
.flags
& UVC_CONTROL_AUTO_UPDATE
)
995 ret
= uvc_query_ctrl(dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
996 dev
->intfnum
, ctrl
->info
.selector
,
997 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1002 if (rollback
|| ret
< 0)
1003 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1004 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1016 int __uvc_ctrl_commit(struct uvc_video_chain
*chain
, int rollback
)
1018 struct uvc_entity
*entity
;
1021 /* Find the control. */
1022 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1023 ret
= uvc_ctrl_commit_entity(chain
->dev
, entity
, rollback
);
1029 mutex_unlock(&chain
->ctrl_mutex
);
1033 int uvc_ctrl_get(struct uvc_video_chain
*chain
,
1034 struct v4l2_ext_control
*xctrl
)
1036 struct uvc_control
*ctrl
;
1037 struct uvc_control_mapping
*mapping
;
1038 struct uvc_menu_info
*menu
;
1042 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1043 if (ctrl
== NULL
|| (ctrl
->info
.flags
& UVC_CONTROL_GET_CUR
) == 0)
1046 if (!ctrl
->loaded
) {
1047 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
, ctrl
->entity
->id
,
1048 chain
->dev
->intfnum
, ctrl
->info
.selector
,
1049 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1057 xctrl
->value
= mapping
->get(mapping
, UVC_GET_CUR
,
1058 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1060 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
1061 menu
= mapping
->menu_info
;
1062 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
1063 if (menu
->value
== xctrl
->value
) {
1073 int uvc_ctrl_set(struct uvc_video_chain
*chain
,
1074 struct v4l2_ext_control
*xctrl
)
1076 struct uvc_control
*ctrl
;
1077 struct uvc_control_mapping
*mapping
;
1084 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1085 if (ctrl
== NULL
|| (ctrl
->info
.flags
& UVC_CONTROL_SET_CUR
) == 0)
1088 /* Clamp out of range values. */
1089 switch (mapping
->v4l2_type
) {
1090 case V4L2_CTRL_TYPE_INTEGER
:
1091 if (!ctrl
->cached
) {
1092 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1097 min
= mapping
->get(mapping
, UVC_GET_MIN
,
1098 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1099 max
= mapping
->get(mapping
, UVC_GET_MAX
,
1100 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1101 step
= mapping
->get(mapping
, UVC_GET_RES
,
1102 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1106 xctrl
->value
= min
+ (xctrl
->value
- min
+ step
/2) / step
* step
;
1107 xctrl
->value
= clamp(xctrl
->value
, min
, max
);
1108 value
= xctrl
->value
;
1111 case V4L2_CTRL_TYPE_BOOLEAN
:
1112 xctrl
->value
= clamp(xctrl
->value
, 0, 1);
1113 value
= xctrl
->value
;
1116 case V4L2_CTRL_TYPE_MENU
:
1117 if (xctrl
->value
< 0 || xctrl
->value
>= mapping
->menu_count
)
1119 value
= mapping
->menu_info
[xctrl
->value
].value
;
1123 value
= xctrl
->value
;
1127 /* If the mapping doesn't span the whole UVC control, the current value
1128 * needs to be loaded from the device to perform the read-modify-write
1131 if (!ctrl
->loaded
&& (ctrl
->info
.size
* 8) != mapping
->size
) {
1132 if ((ctrl
->info
.flags
& UVC_CONTROL_GET_CUR
) == 0) {
1133 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1134 0, ctrl
->info
.size
);
1136 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
1137 ctrl
->entity
->id
, chain
->dev
->intfnum
,
1138 ctrl
->info
.selector
,
1139 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1148 /* Backup the current value in case we need to rollback later. */
1150 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1151 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1155 mapping
->set(mapping
, value
,
1156 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1163 /* --------------------------------------------------------------------------
1167 int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
1168 struct uvc_xu_control
*xctrl
, int set
)
1170 struct uvc_entity
*entity
;
1171 struct uvc_control
*ctrl
= NULL
;
1172 unsigned int i
, found
= 0;
1176 /* Find the extension unit. */
1177 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1178 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
&&
1179 entity
->id
== xctrl
->unit
)
1183 if (entity
->id
!= xctrl
->unit
) {
1184 uvc_trace(UVC_TRACE_CONTROL
, "Extension unit %u not found.\n",
1189 /* Find the control. */
1190 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1191 ctrl
= &entity
->controls
[i
];
1192 if (!ctrl
->initialized
)
1195 if (ctrl
->info
.selector
== xctrl
->selector
) {
1202 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u not found.\n",
1203 entity
->extension
.guidExtensionCode
, xctrl
->selector
);
1207 /* Validate control data size. */
1208 if (ctrl
->info
.size
!= xctrl
->size
)
1211 if ((set
&& !(ctrl
->info
.flags
& UVC_CONTROL_SET_CUR
)) ||
1212 (!set
&& !(ctrl
->info
.flags
& UVC_CONTROL_GET_CUR
)))
1215 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
1216 return -ERESTARTSYS
;
1218 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1219 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1221 data
= uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
);
1223 if (set
&& copy_from_user(data
, xctrl
->data
, xctrl
->size
)) {
1228 ret
= uvc_query_ctrl(chain
->dev
, set
? UVC_SET_CUR
: UVC_GET_CUR
,
1229 xctrl
->unit
, chain
->dev
->intfnum
, xctrl
->selector
,
1234 if (!set
&& copy_to_user(xctrl
->data
, data
, xctrl
->size
)) {
1241 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1242 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1245 mutex_unlock(&chain
->ctrl_mutex
);
1249 /* --------------------------------------------------------------------------
1254 * Restore control values after resume, skipping controls that haven't been
1258 * - Don't restore modified controls that are back to their default value.
1259 * - Handle restore order (Auto-Exposure Mode should be restored before
1262 int uvc_ctrl_resume_device(struct uvc_device
*dev
)
1264 struct uvc_control
*ctrl
;
1265 struct uvc_entity
*entity
;
1269 /* Walk the entities list and restore controls when possible. */
1270 list_for_each_entry(entity
, &dev
->entities
, list
) {
1272 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1273 ctrl
= &entity
->controls
[i
];
1275 if (!ctrl
->initialized
|| !ctrl
->modified
||
1276 (ctrl
->info
.flags
& UVC_CONTROL_RESTORE
) == 0)
1279 printk(KERN_INFO
"restoring control %pUl/%u/%u\n",
1280 ctrl
->info
.entity
, ctrl
->info
.index
,
1281 ctrl
->info
.selector
);
1285 ret
= uvc_ctrl_commit_entity(dev
, entity
, 0);
1293 /* --------------------------------------------------------------------------
1294 * Control and mapping handling
1298 * Query control information (size and flags) for XU controls.
1300 static int uvc_ctrl_fill_xu_info(struct uvc_device
*dev
,
1301 const struct uvc_control
*ctrl
, struct uvc_control_info
*info
)
1306 data
= kmalloc(2, GFP_KERNEL
);
1310 memcpy(info
->entity
, ctrl
->entity
->extension
.guidExtensionCode
,
1311 sizeof(info
->entity
));
1312 info
->index
= ctrl
->index
;
1313 info
->selector
= ctrl
->index
+ 1;
1315 /* Query and verify the control length (GET_LEN) */
1316 ret
= uvc_query_ctrl(dev
, UVC_GET_LEN
, ctrl
->entity
->id
, dev
->intfnum
,
1317 info
->selector
, data
, 2);
1319 uvc_trace(UVC_TRACE_CONTROL
,
1320 "GET_LEN failed on control %pUl/%u (%d).\n",
1321 info
->entity
, info
->selector
, ret
);
1325 info
->size
= le16_to_cpup((__le16
*)data
);
1327 /* Query the control information (GET_INFO) */
1328 ret
= uvc_query_ctrl(dev
, UVC_GET_INFO
, ctrl
->entity
->id
, dev
->intfnum
,
1329 info
->selector
, data
, 1);
1331 uvc_trace(UVC_TRACE_CONTROL
,
1332 "GET_INFO failed on control %pUl/%u (%d).\n",
1333 info
->entity
, info
->selector
, ret
);
1337 info
->flags
= UVC_CONTROL_GET_MIN
| UVC_CONTROL_GET_MAX
1338 | UVC_CONTROL_GET_RES
| UVC_CONTROL_GET_DEF
1339 | (data
[0] & UVC_CONTROL_CAP_GET
? UVC_CONTROL_GET_CUR
: 0)
1340 | (data
[0] & UVC_CONTROL_CAP_SET
? UVC_CONTROL_SET_CUR
: 0)
1341 | (data
[0] & UVC_CONTROL_CAP_AUTOUPDATE
?
1342 UVC_CONTROL_AUTO_UPDATE
: 0);
1344 uvc_trace(UVC_TRACE_CONTROL
, "XU control %pUl/%u queried: len %u, "
1345 "flags { get %u set %u auto %u }.\n",
1346 info
->entity
, info
->selector
, info
->size
,
1347 (info
->flags
& UVC_CONTROL_GET_CUR
) ? 1 : 0,
1348 (info
->flags
& UVC_CONTROL_SET_CUR
) ? 1 : 0,
1349 (info
->flags
& UVC_CONTROL_AUTO_UPDATE
) ? 1 : 0);
1357 * Add control information to a given control.
1359 static int uvc_ctrl_add_info(struct uvc_device
*dev
, struct uvc_control
*ctrl
,
1360 const struct uvc_control_info
*info
)
1364 memcpy(&ctrl
->info
, info
, sizeof(*info
));
1365 INIT_LIST_HEAD(&ctrl
->info
.mappings
);
1367 /* Allocate an array to save control values (cur, def, max, etc.) */
1368 ctrl
->uvc_data
= kzalloc(ctrl
->info
.size
* UVC_CTRL_DATA_LAST
+ 1,
1370 if (ctrl
->uvc_data
== NULL
) {
1375 ctrl
->initialized
= 1;
1377 uvc_trace(UVC_TRACE_CONTROL
, "Added control %pUl/%u to device %s "
1378 "entity %u\n", ctrl
->info
.entity
, ctrl
->info
.selector
,
1379 dev
->udev
->devpath
, ctrl
->entity
->id
);
1383 kfree(ctrl
->uvc_data
);
1388 * Add a control mapping to a given control.
1390 static int __uvc_ctrl_add_mapping(struct uvc_device
*dev
,
1391 struct uvc_control
*ctrl
, const struct uvc_control_mapping
*mapping
)
1393 struct uvc_control_mapping
*map
;
1396 /* Most mappings come from static kernel data and need to be duplicated.
1397 * Mappings that come from userspace will be unnecessarily duplicated,
1398 * this could be optimized.
1400 map
= kmemdup(mapping
, sizeof(*mapping
), GFP_KERNEL
);
1404 size
= sizeof(*mapping
->menu_info
) * mapping
->menu_count
;
1405 map
->menu_info
= kmemdup(mapping
->menu_info
, size
, GFP_KERNEL
);
1406 if (map
->menu_info
== NULL
) {
1411 if (map
->get
== NULL
)
1412 map
->get
= uvc_get_le_value
;
1413 if (map
->set
== NULL
)
1414 map
->set
= uvc_set_le_value
;
1416 map
->ctrl
= &ctrl
->info
;
1417 list_add_tail(&map
->list
, &ctrl
->info
.mappings
);
1418 uvc_trace(UVC_TRACE_CONTROL
,
1419 "Adding mapping '%s' to control %pUl/%u.\n",
1420 map
->name
, ctrl
->info
.entity
, ctrl
->info
.selector
);
1425 int uvc_ctrl_add_mapping(struct uvc_video_chain
*chain
,
1426 const struct uvc_control_mapping
*mapping
)
1428 struct uvc_device
*dev
= chain
->dev
;
1429 struct uvc_control_mapping
*map
;
1430 struct uvc_entity
*entity
;
1431 struct uvc_control
*ctrl
;
1435 if (mapping
->id
& ~V4L2_CTRL_ID_MASK
) {
1436 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', control "
1437 "control id 0x%08x is invalid.\n", mapping
->name
,
1442 /* Search for the matching (GUID/CS) control in the given device */
1443 list_for_each_entry(entity
, &dev
->entities
, list
) {
1446 if (!uvc_entity_match_guid(entity
, mapping
->entity
))
1449 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1450 ctrl
= &entity
->controls
[i
];
1451 if (ctrl
->initialized
&&
1452 ctrl
->info
.selector
== mapping
->selector
) {
1464 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
1465 return -ERESTARTSYS
;
1467 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
1468 if (mapping
->id
== map
->id
) {
1469 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', "
1470 "control id 0x%08x already exists.\n",
1471 mapping
->name
, mapping
->id
);
1477 /* Prevent excess memory consumption */
1478 if (atomic_inc_return(&dev
->nmappings
) > UVC_MAX_CONTROL_MAPPINGS
) {
1479 atomic_dec(&dev
->nmappings
);
1480 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', maximum "
1481 "mappings count (%u) exceeded.\n", mapping
->name
,
1482 UVC_MAX_CONTROL_MAPPINGS
);
1487 ret
= __uvc_ctrl_add_mapping(dev
, ctrl
, mapping
);
1489 atomic_dec(&dev
->nmappings
);
1492 mutex_unlock(&chain
->ctrl_mutex
);
1497 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1498 * are currently the ones that crash the camera or unconditionally return an
1499 * error when queried.
1501 static void uvc_ctrl_prune_entity(struct uvc_device
*dev
,
1502 struct uvc_entity
*entity
)
1504 struct uvc_ctrl_blacklist
{
1505 struct usb_device_id id
;
1509 static const struct uvc_ctrl_blacklist processing_blacklist
[] = {
1510 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1511 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1512 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1514 static const struct uvc_ctrl_blacklist camera_blacklist
[] = {
1515 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
1518 const struct uvc_ctrl_blacklist
*blacklist
;
1524 switch (UVC_ENTITY_TYPE(entity
)) {
1525 case UVC_VC_PROCESSING_UNIT
:
1526 blacklist
= processing_blacklist
;
1527 count
= ARRAY_SIZE(processing_blacklist
);
1528 controls
= entity
->processing
.bmControls
;
1529 size
= entity
->processing
.bControlSize
;
1532 case UVC_ITT_CAMERA
:
1533 blacklist
= camera_blacklist
;
1534 count
= ARRAY_SIZE(camera_blacklist
);
1535 controls
= entity
->camera
.bmControls
;
1536 size
= entity
->camera
.bControlSize
;
1543 for (i
= 0; i
< count
; ++i
) {
1544 if (!usb_match_one_id(dev
->intf
, &blacklist
[i
].id
))
1547 if (blacklist
[i
].index
>= 8 * size
||
1548 !uvc_test_bit(controls
, blacklist
[i
].index
))
1551 uvc_trace(UVC_TRACE_CONTROL
, "%u/%u control is black listed, "
1552 "removing it.\n", entity
->id
, blacklist
[i
].index
);
1554 uvc_clear_bit(controls
, blacklist
[i
].index
);
1559 * Add control information and hardcoded stock control mappings to the given
1562 static void uvc_ctrl_init_ctrl(struct uvc_device
*dev
, struct uvc_control
*ctrl
)
1564 const struct uvc_control_info
*info
= uvc_ctrls
;
1565 const struct uvc_control_info
*iend
= info
+ ARRAY_SIZE(uvc_ctrls
);
1566 const struct uvc_control_mapping
*mapping
= uvc_ctrl_mappings
;
1567 const struct uvc_control_mapping
*mend
=
1568 mapping
+ ARRAY_SIZE(uvc_ctrl_mappings
);
1570 /* Query XU controls for control information */
1571 if (UVC_ENTITY_TYPE(ctrl
->entity
) == UVC_VC_EXTENSION_UNIT
) {
1572 struct uvc_control_info info
;
1575 ret
= uvc_ctrl_fill_xu_info(dev
, ctrl
, &info
);
1579 ret
= uvc_ctrl_add_info(dev
, ctrl
, &info
);
1581 /* Skip the control */
1582 uvc_trace(UVC_TRACE_CONTROL
, "Failed to initialize "
1583 "control %pUl/%u on device %s entity %u\n",
1584 info
.entity
, info
.selector
, dev
->udev
->devpath
,
1586 memset(ctrl
, 0, sizeof(*ctrl
));
1591 for (; info
< iend
; ++info
) {
1592 if (uvc_entity_match_guid(ctrl
->entity
, info
->entity
) &&
1593 ctrl
->index
== info
->index
) {
1594 uvc_ctrl_add_info(dev
, ctrl
, info
);
1599 if (!ctrl
->initialized
)
1602 for (; mapping
< mend
; ++mapping
) {
1603 if (uvc_entity_match_guid(ctrl
->entity
, mapping
->entity
) &&
1604 ctrl
->info
.selector
== mapping
->selector
)
1605 __uvc_ctrl_add_mapping(dev
, ctrl
, mapping
);
1610 * Initialize device controls.
1612 int uvc_ctrl_init_device(struct uvc_device
*dev
)
1614 struct uvc_entity
*entity
;
1617 /* Walk the entities list and instantiate controls */
1618 list_for_each_entry(entity
, &dev
->entities
, list
) {
1619 struct uvc_control
*ctrl
;
1620 unsigned int bControlSize
= 0, ncontrols
= 0;
1621 __u8
*bmControls
= NULL
;
1623 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
1624 bmControls
= entity
->extension
.bmControls
;
1625 bControlSize
= entity
->extension
.bControlSize
;
1626 } else if (UVC_ENTITY_TYPE(entity
) == UVC_VC_PROCESSING_UNIT
) {
1627 bmControls
= entity
->processing
.bmControls
;
1628 bControlSize
= entity
->processing
.bControlSize
;
1629 } else if (UVC_ENTITY_TYPE(entity
) == UVC_ITT_CAMERA
) {
1630 bmControls
= entity
->camera
.bmControls
;
1631 bControlSize
= entity
->camera
.bControlSize
;
1634 /* Remove bogus/blacklisted controls */
1635 uvc_ctrl_prune_entity(dev
, entity
);
1637 /* Count supported controls and allocate the controls array */
1638 for (i
= 0; i
< bControlSize
; ++i
)
1639 ncontrols
+= hweight8(bmControls
[i
]);
1643 entity
->controls
= kzalloc(ncontrols
* sizeof(*ctrl
),
1645 if (entity
->controls
== NULL
)
1647 entity
->ncontrols
= ncontrols
;
1649 /* Initialize all supported controls */
1650 ctrl
= entity
->controls
;
1651 for (i
= 0; i
< bControlSize
* 8; ++i
) {
1652 if (uvc_test_bit(bmControls
, i
) == 0)
1655 ctrl
->entity
= entity
;
1658 uvc_ctrl_init_ctrl(dev
, ctrl
);
1667 * Cleanup device controls.
1669 static void uvc_ctrl_cleanup_mappings(struct uvc_device
*dev
,
1670 struct uvc_control
*ctrl
)
1672 struct uvc_control_mapping
*mapping
, *nm
;
1674 list_for_each_entry_safe(mapping
, nm
, &ctrl
->info
.mappings
, list
) {
1675 list_del(&mapping
->list
);
1676 kfree(mapping
->menu_info
);
1681 void uvc_ctrl_cleanup_device(struct uvc_device
*dev
)
1683 struct uvc_entity
*entity
;
1686 /* Free controls and control mappings for all entities. */
1687 list_for_each_entry(entity
, &dev
->entities
, list
) {
1688 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1689 struct uvc_control
*ctrl
= &entity
->controls
[i
];
1691 if (!ctrl
->initialized
)
1694 uvc_ctrl_cleanup_mappings(dev
, ctrl
);
1695 kfree(ctrl
->uvc_data
);
1698 kfree(entity
->controls
);