2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2009
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/list.h>
16 #include <linux/module.h>
17 #include <linux/uaccess.h>
18 #include <linux/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
21 #include <linux/wait.h>
22 #include <asm/atomic.h>
26 #define UVC_CTRL_DATA_CURRENT 0
27 #define UVC_CTRL_DATA_BACKUP 1
28 #define UVC_CTRL_DATA_MIN 2
29 #define UVC_CTRL_DATA_MAX 3
30 #define UVC_CTRL_DATA_RES 4
31 #define UVC_CTRL_DATA_DEF 5
32 #define UVC_CTRL_DATA_LAST 6
34 /* ------------------------------------------------------------------------
38 static struct uvc_control_info uvc_ctrls
[] = {
40 .entity
= UVC_GUID_UVC_PROCESSING
,
41 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
44 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
45 | UVC_CONTROL_RESTORE
,
48 .entity
= UVC_GUID_UVC_PROCESSING
,
49 .selector
= UVC_PU_CONTRAST_CONTROL
,
52 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
53 | UVC_CONTROL_RESTORE
,
56 .entity
= UVC_GUID_UVC_PROCESSING
,
57 .selector
= UVC_PU_HUE_CONTROL
,
60 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
61 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
64 .entity
= UVC_GUID_UVC_PROCESSING
,
65 .selector
= UVC_PU_SATURATION_CONTROL
,
68 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
69 | UVC_CONTROL_RESTORE
,
72 .entity
= UVC_GUID_UVC_PROCESSING
,
73 .selector
= UVC_PU_SHARPNESS_CONTROL
,
76 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
77 | UVC_CONTROL_RESTORE
,
80 .entity
= UVC_GUID_UVC_PROCESSING
,
81 .selector
= UVC_PU_GAMMA_CONTROL
,
84 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
85 | UVC_CONTROL_RESTORE
,
88 .entity
= UVC_GUID_UVC_PROCESSING
,
89 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
92 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
93 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
96 .entity
= UVC_GUID_UVC_PROCESSING
,
97 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
100 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
101 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
104 .entity
= UVC_GUID_UVC_PROCESSING
,
105 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
108 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
109 | UVC_CONTROL_RESTORE
,
112 .entity
= UVC_GUID_UVC_PROCESSING
,
113 .selector
= UVC_PU_GAIN_CONTROL
,
116 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
117 | UVC_CONTROL_RESTORE
,
120 .entity
= UVC_GUID_UVC_PROCESSING
,
121 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
124 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
125 | UVC_CONTROL_RESTORE
,
128 .entity
= UVC_GUID_UVC_PROCESSING
,
129 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
132 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
133 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
136 .entity
= UVC_GUID_UVC_PROCESSING
,
137 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
140 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
141 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
144 .entity
= UVC_GUID_UVC_PROCESSING
,
145 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
148 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
149 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
152 .entity
= UVC_GUID_UVC_PROCESSING
,
153 .selector
= UVC_PU_DIGITAL_MULTIPLIER_CONTROL
,
156 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
157 | UVC_CONTROL_RESTORE
,
160 .entity
= UVC_GUID_UVC_PROCESSING
,
161 .selector
= UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL
,
164 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
165 | UVC_CONTROL_RESTORE
,
168 .entity
= UVC_GUID_UVC_PROCESSING
,
169 .selector
= UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL
,
172 .flags
= UVC_CONTROL_GET_CUR
,
175 .entity
= UVC_GUID_UVC_PROCESSING
,
176 .selector
= UVC_PU_ANALOG_LOCK_STATUS_CONTROL
,
179 .flags
= UVC_CONTROL_GET_CUR
,
182 .entity
= UVC_GUID_UVC_CAMERA
,
183 .selector
= UVC_CT_SCANNING_MODE_CONTROL
,
186 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
187 | UVC_CONTROL_RESTORE
,
190 .entity
= UVC_GUID_UVC_CAMERA
,
191 .selector
= UVC_CT_AE_MODE_CONTROL
,
194 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
195 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_GET_RES
196 | UVC_CONTROL_RESTORE
,
199 .entity
= UVC_GUID_UVC_CAMERA
,
200 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
203 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
204 | UVC_CONTROL_RESTORE
,
207 .entity
= UVC_GUID_UVC_CAMERA
,
208 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
211 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
212 | UVC_CONTROL_RESTORE
,
215 .entity
= UVC_GUID_UVC_CAMERA
,
216 .selector
= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL
,
219 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
220 | 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_RANGE
236 | UVC_CONTROL_AUTO_UPDATE
,
239 .entity
= UVC_GUID_UVC_CAMERA
,
240 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
243 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
244 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
247 .entity
= UVC_GUID_UVC_CAMERA
,
248 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
251 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
252 | 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_RANGE
268 | UVC_CONTROL_AUTO_UPDATE
,
271 .entity
= UVC_GUID_UVC_CAMERA
,
272 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
275 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
276 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
279 .entity
= UVC_GUID_UVC_CAMERA
,
280 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
283 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
284 | UVC_CONTROL_AUTO_UPDATE
,
287 .entity
= UVC_GUID_UVC_CAMERA
,
288 .selector
= UVC_CT_ROLL_ABSOLUTE_CONTROL
,
291 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
292 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
295 .entity
= UVC_GUID_UVC_CAMERA
,
296 .selector
= UVC_CT_ROLL_RELATIVE_CONTROL
,
299 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
300 | UVC_CONTROL_AUTO_UPDATE
,
303 .entity
= UVC_GUID_UVC_CAMERA
,
304 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
307 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
308 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
311 .entity
= UVC_GUID_UVC_CAMERA
,
312 .selector
= UVC_CT_PRIVACY_CONTROL
,
315 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
316 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
320 static struct uvc_menu_info power_line_frequency_controls
[] = {
326 static struct uvc_menu_info exposure_auto_controls
[] = {
328 { 1, "Manual Mode" },
329 { 4, "Shutter Priority Mode" },
330 { 8, "Aperture Priority Mode" },
333 static __s32
uvc_ctrl_get_zoom(struct uvc_control_mapping
*mapping
,
334 __u8 query
, const __u8
*data
)
336 __s8 zoom
= (__s8
)data
[0];
340 return (zoom
== 0) ? 0 : (zoom
> 0 ? data
[2] : -data
[2]);
351 static void uvc_ctrl_set_zoom(struct uvc_control_mapping
*mapping
,
352 __s32 value
, __u8
*data
)
354 data
[0] = value
== 0 ? 0 : (value
> 0) ? 1 : 0xff;
355 data
[2] = min((int)abs(value
), 0xff);
358 static struct uvc_control_mapping uvc_ctrl_mappings
[] = {
360 .id
= V4L2_CID_BRIGHTNESS
,
361 .name
= "Brightness",
362 .entity
= UVC_GUID_UVC_PROCESSING
,
363 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
366 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
367 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
370 .id
= V4L2_CID_CONTRAST
,
372 .entity
= UVC_GUID_UVC_PROCESSING
,
373 .selector
= UVC_PU_CONTRAST_CONTROL
,
376 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
377 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
382 .entity
= UVC_GUID_UVC_PROCESSING
,
383 .selector
= UVC_PU_HUE_CONTROL
,
386 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
387 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
390 .id
= V4L2_CID_SATURATION
,
391 .name
= "Saturation",
392 .entity
= UVC_GUID_UVC_PROCESSING
,
393 .selector
= UVC_PU_SATURATION_CONTROL
,
396 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
397 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
400 .id
= V4L2_CID_SHARPNESS
,
402 .entity
= UVC_GUID_UVC_PROCESSING
,
403 .selector
= UVC_PU_SHARPNESS_CONTROL
,
406 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
407 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
410 .id
= V4L2_CID_GAMMA
,
412 .entity
= UVC_GUID_UVC_PROCESSING
,
413 .selector
= UVC_PU_GAMMA_CONTROL
,
416 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
417 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
420 .id
= V4L2_CID_BACKLIGHT_COMPENSATION
,
421 .name
= "Backlight Compensation",
422 .entity
= UVC_GUID_UVC_PROCESSING
,
423 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
426 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
427 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
432 .entity
= UVC_GUID_UVC_PROCESSING
,
433 .selector
= UVC_PU_GAIN_CONTROL
,
436 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
437 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
440 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
441 .name
= "Power Line Frequency",
442 .entity
= UVC_GUID_UVC_PROCESSING
,
443 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
446 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
447 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
448 .menu_info
= power_line_frequency_controls
,
449 .menu_count
= ARRAY_SIZE(power_line_frequency_controls
),
452 .id
= V4L2_CID_HUE_AUTO
,
454 .entity
= UVC_GUID_UVC_PROCESSING
,
455 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
458 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
459 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
462 .id
= V4L2_CID_EXPOSURE_AUTO
,
463 .name
= "Exposure, Auto",
464 .entity
= UVC_GUID_UVC_CAMERA
,
465 .selector
= UVC_CT_AE_MODE_CONTROL
,
468 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
469 .data_type
= UVC_CTRL_DATA_TYPE_BITMASK
,
470 .menu_info
= exposure_auto_controls
,
471 .menu_count
= ARRAY_SIZE(exposure_auto_controls
),
474 .id
= V4L2_CID_EXPOSURE_AUTO_PRIORITY
,
475 .name
= "Exposure, Auto Priority",
476 .entity
= UVC_GUID_UVC_CAMERA
,
477 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
480 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
481 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
484 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
485 .name
= "Exposure (Absolute)",
486 .entity
= UVC_GUID_UVC_CAMERA
,
487 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
490 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
491 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
494 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
495 .name
= "White Balance Temperature, Auto",
496 .entity
= UVC_GUID_UVC_PROCESSING
,
497 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
500 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
501 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
504 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
505 .name
= "White Balance Temperature",
506 .entity
= UVC_GUID_UVC_PROCESSING
,
507 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
510 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
511 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
514 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
515 .name
= "White Balance Component, Auto",
516 .entity
= UVC_GUID_UVC_PROCESSING
,
517 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
520 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
521 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
524 .id
= V4L2_CID_BLUE_BALANCE
,
525 .name
= "White Balance Blue Component",
526 .entity
= UVC_GUID_UVC_PROCESSING
,
527 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
530 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
531 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
534 .id
= V4L2_CID_RED_BALANCE
,
535 .name
= "White Balance Red Component",
536 .entity
= UVC_GUID_UVC_PROCESSING
,
537 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
540 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
541 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
544 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
545 .name
= "Focus (absolute)",
546 .entity
= UVC_GUID_UVC_CAMERA
,
547 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
550 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
551 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
554 .id
= V4L2_CID_FOCUS_AUTO
,
555 .name
= "Focus, Auto",
556 .entity
= UVC_GUID_UVC_CAMERA
,
557 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
560 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
561 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
564 .id
= V4L2_CID_ZOOM_ABSOLUTE
,
565 .name
= "Zoom, Absolute",
566 .entity
= UVC_GUID_UVC_CAMERA
,
567 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
570 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
571 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
574 .id
= V4L2_CID_ZOOM_CONTINUOUS
,
575 .name
= "Zoom, Continuous",
576 .entity
= UVC_GUID_UVC_CAMERA
,
577 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
580 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
581 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
582 .get
= uvc_ctrl_get_zoom
,
583 .set
= uvc_ctrl_set_zoom
,
586 .id
= V4L2_CID_PRIVACY
,
588 .entity
= UVC_GUID_UVC_CAMERA
,
589 .selector
= UVC_CT_PRIVACY_CONTROL
,
592 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
593 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
597 /* ------------------------------------------------------------------------
601 static inline __u8
*uvc_ctrl_data(struct uvc_control
*ctrl
, int id
)
603 return ctrl
->data
+ id
* ctrl
->info
->size
;
606 static inline int uvc_test_bit(const __u8
*data
, int bit
)
608 return (data
[bit
>> 3] >> (bit
& 7)) & 1;
611 static inline void uvc_clear_bit(__u8
*data
, int bit
)
613 data
[bit
>> 3] &= ~(1 << (bit
& 7));
616 /* Extract the bit string specified by mapping->offset and mapping->size
617 * from the little-endian data stored at 'data' and return the result as
618 * a signed 32bit integer. Sign extension will be performed if the mapping
619 * references a signed data type.
621 static __s32
uvc_get_le_value(struct uvc_control_mapping
*mapping
,
622 __u8 query
, const __u8
*data
)
624 int bits
= mapping
->size
;
625 int offset
= mapping
->offset
;
631 mask
= ((1LL << bits
) - 1) << offset
;
633 for (; bits
> 0; data
++) {
634 __u8 byte
= *data
& mask
;
635 value
|= offset
> 0 ? (byte
>> offset
) : (byte
<< (-offset
));
636 bits
-= 8 - (offset
> 0 ? offset
: 0);
638 mask
= (1 << bits
) - 1;
641 /* Sign-extend the value if needed. */
642 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
643 value
|= -(value
& (1 << (mapping
->size
- 1)));
648 /* Set the bit string specified by mapping->offset and mapping->size
649 * in the little-endian data stored at 'data' to the value 'value'.
651 static void uvc_set_le_value(struct uvc_control_mapping
*mapping
,
652 __s32 value
, __u8
*data
)
654 int bits
= mapping
->size
;
655 int offset
= mapping
->offset
;
661 for (; bits
> 0; data
++) {
662 mask
= ((1LL << bits
) - 1) << offset
;
663 *data
= (*data
& ~mask
) | ((value
<< offset
) & mask
);
664 value
>>= offset
? offset
: 8;
670 /* ------------------------------------------------------------------------
671 * Terminal and unit management
674 static const __u8 uvc_processing_guid
[16] = UVC_GUID_UVC_PROCESSING
;
675 static const __u8 uvc_camera_guid
[16] = UVC_GUID_UVC_CAMERA
;
676 static const __u8 uvc_media_transport_input_guid
[16] =
677 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT
;
679 static int uvc_entity_match_guid(struct uvc_entity
*entity
, __u8 guid
[16])
681 switch (UVC_ENTITY_TYPE(entity
)) {
683 return memcmp(uvc_camera_guid
, guid
, 16) == 0;
685 case UVC_ITT_MEDIA_TRANSPORT_INPUT
:
686 return memcmp(uvc_media_transport_input_guid
, guid
, 16) == 0;
688 case UVC_VC_PROCESSING_UNIT
:
689 return memcmp(uvc_processing_guid
, guid
, 16) == 0;
691 case UVC_VC_EXTENSION_UNIT
:
692 return memcmp(entity
->extension
.guidExtensionCode
,
700 /* ------------------------------------------------------------------------
704 static void __uvc_find_control(struct uvc_entity
*entity
, __u32 v4l2_id
,
705 struct uvc_control_mapping
**mapping
, struct uvc_control
**control
,
708 struct uvc_control
*ctrl
;
709 struct uvc_control_mapping
*map
;
715 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
716 ctrl
= &entity
->controls
[i
];
717 if (ctrl
->info
== NULL
)
720 list_for_each_entry(map
, &ctrl
->info
->mappings
, list
) {
721 if ((map
->id
== v4l2_id
) && !next
) {
727 if ((*mapping
== NULL
|| (*mapping
)->id
> map
->id
) &&
728 (map
->id
> v4l2_id
) && next
) {
736 struct uvc_control
*uvc_find_control(struct uvc_video_chain
*chain
,
737 __u32 v4l2_id
, struct uvc_control_mapping
**mapping
)
739 struct uvc_control
*ctrl
= NULL
;
740 struct uvc_entity
*entity
;
741 int next
= v4l2_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
745 /* Mask the query flags. */
746 v4l2_id
&= V4L2_CTRL_ID_MASK
;
748 /* Find the control. */
749 list_for_each_entry(entity
, &chain
->entities
, chain
) {
750 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
755 if (ctrl
== NULL
&& !next
)
756 uvc_trace(UVC_TRACE_CONTROL
, "Control 0x%08x not found.\n",
762 static int uvc_ctrl_populate_cache(struct uvc_video_chain
*chain
,
763 struct uvc_control
*ctrl
)
767 if (ctrl
->info
->flags
& UVC_CONTROL_GET_DEF
) {
768 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_DEF
, ctrl
->entity
->id
,
769 chain
->dev
->intfnum
, ctrl
->info
->selector
,
770 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
),
776 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MIN
) {
777 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MIN
, ctrl
->entity
->id
,
778 chain
->dev
->intfnum
, ctrl
->info
->selector
,
779 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
),
784 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MAX
) {
785 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MAX
, ctrl
->entity
->id
,
786 chain
->dev
->intfnum
, ctrl
->info
->selector
,
787 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
),
792 if (ctrl
->info
->flags
& UVC_CONTROL_GET_RES
) {
793 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_RES
, ctrl
->entity
->id
,
794 chain
->dev
->intfnum
, ctrl
->info
->selector
,
795 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
),
805 int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
806 struct v4l2_queryctrl
*v4l2_ctrl
)
808 struct uvc_control
*ctrl
;
809 struct uvc_control_mapping
*mapping
;
810 struct uvc_menu_info
*menu
;
814 ctrl
= uvc_find_control(chain
, v4l2_ctrl
->id
, &mapping
);
818 memset(v4l2_ctrl
, 0, sizeof *v4l2_ctrl
);
819 v4l2_ctrl
->id
= mapping
->id
;
820 v4l2_ctrl
->type
= mapping
->v4l2_type
;
821 strlcpy(v4l2_ctrl
->name
, mapping
->name
, sizeof v4l2_ctrl
->name
);
822 v4l2_ctrl
->flags
= 0;
824 if (!(ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
))
825 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
828 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
833 if (ctrl
->info
->flags
& UVC_CONTROL_GET_DEF
) {
834 v4l2_ctrl
->default_value
= mapping
->get(mapping
, UVC_GET_DEF
,
835 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
));
838 switch (mapping
->v4l2_type
) {
839 case V4L2_CTRL_TYPE_MENU
:
840 v4l2_ctrl
->minimum
= 0;
841 v4l2_ctrl
->maximum
= mapping
->menu_count
- 1;
844 menu
= mapping
->menu_info
;
845 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
846 if (menu
->value
== v4l2_ctrl
->default_value
) {
847 v4l2_ctrl
->default_value
= i
;
854 case V4L2_CTRL_TYPE_BOOLEAN
:
855 v4l2_ctrl
->minimum
= 0;
856 v4l2_ctrl
->maximum
= 1;
860 case V4L2_CTRL_TYPE_BUTTON
:
861 v4l2_ctrl
->minimum
= 0;
862 v4l2_ctrl
->maximum
= 0;
870 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MIN
)
871 v4l2_ctrl
->minimum
= mapping
->get(mapping
, UVC_GET_MIN
,
872 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
874 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MAX
)
875 v4l2_ctrl
->maximum
= mapping
->get(mapping
, UVC_GET_MAX
,
876 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
878 if (ctrl
->info
->flags
& UVC_CONTROL_GET_RES
)
879 v4l2_ctrl
->step
= mapping
->get(mapping
, UVC_GET_RES
,
880 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
886 /* --------------------------------------------------------------------------
887 * Control transactions
889 * To make extended set operations as atomic as the hardware allows, controls
890 * are handled using begin/commit/rollback operations.
892 * At the beginning of a set request, uvc_ctrl_begin should be called to
893 * initialize the request. This function acquires the control lock.
895 * When setting a control, the new value is stored in the control data field
896 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
897 * later processing. If the UVC and V4L2 control sizes differ, the current
898 * value is loaded from the hardware before storing the new value in the data
901 * After processing all controls in the transaction, uvc_ctrl_commit or
902 * uvc_ctrl_rollback must be called to apply the pending changes to the
903 * hardware or revert them. When applying changes, all controls marked as
904 * dirty will be modified in the UVC device, and the dirty flag will be
905 * cleared. When reverting controls, the control data field
906 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
907 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
910 int uvc_ctrl_begin(struct uvc_video_chain
*chain
)
912 return mutex_lock_interruptible(&chain
->ctrl_mutex
) ? -ERESTARTSYS
: 0;
915 static int uvc_ctrl_commit_entity(struct uvc_device
*dev
,
916 struct uvc_entity
*entity
, int rollback
)
918 struct uvc_control
*ctrl
;
925 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
926 ctrl
= &entity
->controls
[i
];
927 if (ctrl
->info
== NULL
)
930 /* Reset the loaded flag for auto-update controls that were
931 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
932 * uvc_ctrl_get from using the cached value.
934 if (ctrl
->info
->flags
& UVC_CONTROL_AUTO_UPDATE
)
941 ret
= uvc_query_ctrl(dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
942 dev
->intfnum
, ctrl
->info
->selector
,
943 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
948 if (rollback
|| ret
< 0)
949 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
950 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
962 int __uvc_ctrl_commit(struct uvc_video_chain
*chain
, int rollback
)
964 struct uvc_entity
*entity
;
967 /* Find the control. */
968 list_for_each_entry(entity
, &chain
->entities
, chain
) {
969 ret
= uvc_ctrl_commit_entity(chain
->dev
, entity
, rollback
);
975 mutex_unlock(&chain
->ctrl_mutex
);
979 int uvc_ctrl_get(struct uvc_video_chain
*chain
,
980 struct v4l2_ext_control
*xctrl
)
982 struct uvc_control
*ctrl
;
983 struct uvc_control_mapping
*mapping
;
984 struct uvc_menu_info
*menu
;
988 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
989 if (ctrl
== NULL
|| (ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
) == 0)
993 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
, ctrl
->entity
->id
,
994 chain
->dev
->intfnum
, ctrl
->info
->selector
,
995 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1003 xctrl
->value
= mapping
->get(mapping
, UVC_GET_CUR
,
1004 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1006 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
1007 menu
= mapping
->menu_info
;
1008 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
1009 if (menu
->value
== xctrl
->value
) {
1019 int uvc_ctrl_set(struct uvc_video_chain
*chain
,
1020 struct v4l2_ext_control
*xctrl
)
1022 struct uvc_control
*ctrl
;
1023 struct uvc_control_mapping
*mapping
;
1030 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1031 if (ctrl
== NULL
|| (ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
) == 0)
1034 /* Clamp out of range values. */
1035 switch (mapping
->v4l2_type
) {
1036 case V4L2_CTRL_TYPE_INTEGER
:
1037 if (!ctrl
->cached
) {
1038 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1043 min
= mapping
->get(mapping
, UVC_GET_MIN
,
1044 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1045 max
= mapping
->get(mapping
, UVC_GET_MAX
,
1046 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1047 step
= mapping
->get(mapping
, UVC_GET_RES
,
1048 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1050 xctrl
->value
= min
+ (xctrl
->value
- min
+ step
/2) / step
* step
;
1051 xctrl
->value
= clamp(xctrl
->value
, min
, max
);
1052 value
= xctrl
->value
;
1055 case V4L2_CTRL_TYPE_BOOLEAN
:
1056 xctrl
->value
= clamp(xctrl
->value
, 0, 1);
1057 value
= xctrl
->value
;
1060 case V4L2_CTRL_TYPE_MENU
:
1061 if (xctrl
->value
< 0 || xctrl
->value
>= mapping
->menu_count
)
1063 value
= mapping
->menu_info
[xctrl
->value
].value
;
1067 value
= xctrl
->value
;
1071 /* If the mapping doesn't span the whole UVC control, the current value
1072 * needs to be loaded from the device to perform the read-modify-write
1075 if (!ctrl
->loaded
&& (ctrl
->info
->size
* 8) != mapping
->size
) {
1076 if ((ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
) == 0) {
1077 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1078 0, ctrl
->info
->size
);
1080 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
1081 ctrl
->entity
->id
, chain
->dev
->intfnum
,
1082 ctrl
->info
->selector
,
1083 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1092 /* Backup the current value in case we need to rollback later. */
1094 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1095 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1099 mapping
->set(mapping
, value
,
1100 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1107 /* --------------------------------------------------------------------------
1111 int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
1112 struct uvc_xu_control
*xctrl
, int set
)
1114 struct uvc_entity
*entity
;
1115 struct uvc_control
*ctrl
= NULL
;
1116 unsigned int i
, found
= 0;
1120 /* Find the extension unit. */
1121 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1122 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
&&
1123 entity
->id
== xctrl
->unit
)
1127 if (entity
->id
!= xctrl
->unit
) {
1128 uvc_trace(UVC_TRACE_CONTROL
, "Extension unit %u not found.\n",
1133 /* Find the control. */
1134 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1135 ctrl
= &entity
->controls
[i
];
1136 if (ctrl
->info
== NULL
)
1139 if (ctrl
->info
->selector
== xctrl
->selector
) {
1146 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u not found.\n",
1147 entity
->extension
.guidExtensionCode
, xctrl
->selector
);
1151 /* Validate control data size. */
1152 if (ctrl
->info
->size
!= xctrl
->size
)
1155 if ((set
&& !(ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
)) ||
1156 (!set
&& !(ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
)))
1159 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
1160 return -ERESTARTSYS
;
1162 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1163 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1165 data
= uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
);
1167 if (set
&& copy_from_user(data
, xctrl
->data
, xctrl
->size
)) {
1172 ret
= uvc_query_ctrl(chain
->dev
, set
? UVC_SET_CUR
: UVC_GET_CUR
,
1173 xctrl
->unit
, chain
->dev
->intfnum
, xctrl
->selector
,
1178 if (!set
&& copy_to_user(xctrl
->data
, data
, xctrl
->size
)) {
1185 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1186 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1189 mutex_unlock(&chain
->ctrl_mutex
);
1193 /* --------------------------------------------------------------------------
1198 * Restore control values after resume, skipping controls that haven't been
1202 * - Don't restore modified controls that are back to their default value.
1203 * - Handle restore order (Auto-Exposure Mode should be restored before
1206 int uvc_ctrl_resume_device(struct uvc_device
*dev
)
1208 struct uvc_control
*ctrl
;
1209 struct uvc_entity
*entity
;
1213 /* Walk the entities list and restore controls when possible. */
1214 list_for_each_entry(entity
, &dev
->entities
, list
) {
1216 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1217 ctrl
= &entity
->controls
[i
];
1219 if (ctrl
->info
== NULL
|| !ctrl
->modified
||
1220 (ctrl
->info
->flags
& UVC_CONTROL_RESTORE
) == 0)
1223 printk(KERN_INFO
"restoring control %pUl/%u/%u\n",
1224 ctrl
->info
->entity
, ctrl
->info
->index
,
1225 ctrl
->info
->selector
);
1229 ret
= uvc_ctrl_commit_entity(dev
, entity
, 0);
1237 /* --------------------------------------------------------------------------
1238 * Control and mapping handling
1241 static void uvc_ctrl_add_ctrl(struct uvc_device
*dev
,
1242 struct uvc_control_info
*info
)
1244 struct uvc_entity
*entity
;
1245 struct uvc_control
*ctrl
= NULL
;
1249 list_for_each_entry(entity
, &dev
->entities
, list
) {
1250 if (!uvc_entity_match_guid(entity
, info
->entity
))
1253 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1254 ctrl
= &entity
->controls
[i
];
1255 if (ctrl
->index
== info
->index
) {
1268 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
1269 /* Check if the device control information and length match
1270 * the user supplied information.
1276 ret
= uvc_query_ctrl(dev
, UVC_GET_LEN
, ctrl
->entity
->id
,
1277 dev
->intfnum
, info
->selector
, (__u8
*)&size
, 2);
1279 uvc_trace(UVC_TRACE_CONTROL
,
1280 "GET_LEN failed on control %pUl/%u (%d).\n",
1281 info
->entity
, info
->selector
, ret
);
1285 if (info
->size
!= le16_to_cpu(size
)) {
1286 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u size "
1287 "doesn't match user supplied value.\n",
1288 info
->entity
, info
->selector
);
1292 ret
= uvc_query_ctrl(dev
, UVC_GET_INFO
, ctrl
->entity
->id
,
1293 dev
->intfnum
, info
->selector
, &inf
, 1);
1295 uvc_trace(UVC_TRACE_CONTROL
,
1296 "GET_INFO failed on control %pUl/%u (%d).\n",
1297 info
->entity
, info
->selector
, ret
);
1301 flags
= info
->flags
;
1302 if (((flags
& UVC_CONTROL_GET_CUR
) && !(inf
& (1 << 0))) ||
1303 ((flags
& UVC_CONTROL_SET_CUR
) && !(inf
& (1 << 1)))) {
1304 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u flags "
1305 "don't match supported operations.\n",
1306 info
->entity
, info
->selector
);
1312 ctrl
->data
= kmalloc(ctrl
->info
->size
* UVC_CTRL_DATA_LAST
, GFP_KERNEL
);
1313 uvc_trace(UVC_TRACE_CONTROL
, "Added control %pUl/%u to device %s "
1314 "entity %u\n", ctrl
->info
->entity
, ctrl
->info
->selector
,
1315 dev
->udev
->devpath
, entity
->id
);
1319 * Add an item to the UVC control information list, and instantiate a control
1320 * structure for each device that supports the control.
1322 int uvc_ctrl_add_info(struct uvc_control_info
*info
)
1324 struct uvc_control_info
*ctrl
;
1325 struct uvc_device
*dev
;
1328 /* Find matching controls by walking the devices, entities and
1331 mutex_lock(&uvc_driver
.ctrl_mutex
);
1333 /* First check if the list contains a control matching the new one.
1334 * Bail out if it does.
1336 list_for_each_entry(ctrl
, &uvc_driver
.controls
, list
) {
1337 if (memcmp(ctrl
->entity
, info
->entity
, 16))
1340 if (ctrl
->selector
== info
->selector
) {
1341 uvc_trace(UVC_TRACE_CONTROL
,
1342 "Control %pUl/%u is already defined.\n",
1343 info
->entity
, info
->selector
);
1347 if (ctrl
->index
== info
->index
) {
1348 uvc_trace(UVC_TRACE_CONTROL
,
1349 "Control %pUl/%u would overwrite index %d.\n",
1350 info
->entity
, info
->selector
, info
->index
);
1356 list_for_each_entry(dev
, &uvc_driver
.devices
, list
)
1357 uvc_ctrl_add_ctrl(dev
, info
);
1359 INIT_LIST_HEAD(&info
->mappings
);
1360 list_add_tail(&info
->list
, &uvc_driver
.controls
);
1362 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1366 int uvc_ctrl_add_mapping(struct uvc_control_mapping
*mapping
)
1368 struct uvc_control_info
*info
;
1369 struct uvc_control_mapping
*map
;
1372 if (mapping
->get
== NULL
)
1373 mapping
->get
= uvc_get_le_value
;
1374 if (mapping
->set
== NULL
)
1375 mapping
->set
= uvc_set_le_value
;
1377 if (mapping
->id
& ~V4L2_CTRL_ID_MASK
) {
1378 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s' with "
1379 "invalid control id 0x%08x\n", mapping
->name
,
1384 mutex_lock(&uvc_driver
.ctrl_mutex
);
1385 list_for_each_entry(info
, &uvc_driver
.controls
, list
) {
1386 if (memcmp(info
->entity
, mapping
->entity
, 16) ||
1387 info
->selector
!= mapping
->selector
)
1390 if (info
->size
* 8 < mapping
->size
+ mapping
->offset
) {
1391 uvc_trace(UVC_TRACE_CONTROL
,
1392 "Mapping '%s' would overflow control %pUl/%u\n",
1393 mapping
->name
, info
->entity
, info
->selector
);
1398 /* Check if the list contains a mapping matching the new one.
1399 * Bail out if it does.
1401 list_for_each_entry(map
, &info
->mappings
, list
) {
1402 if (map
->id
== mapping
->id
) {
1403 uvc_trace(UVC_TRACE_CONTROL
, "Mapping '%s' is "
1404 "already defined.\n", mapping
->name
);
1410 mapping
->ctrl
= info
;
1411 list_add_tail(&mapping
->list
, &info
->mappings
);
1412 uvc_trace(UVC_TRACE_CONTROL
,
1413 "Adding mapping %s to control %pUl/%u.\n",
1414 mapping
->name
, info
->entity
, info
->selector
);
1420 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1425 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1426 * are currently the ones that crash the camera or unconditionally return an
1427 * error when queried.
1430 uvc_ctrl_prune_entity(struct uvc_device
*dev
, struct uvc_entity
*entity
)
1432 static const struct {
1433 struct usb_device_id id
;
1436 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1437 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1438 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1445 if (UVC_ENTITY_TYPE(entity
) != UVC_VC_PROCESSING_UNIT
)
1448 controls
= entity
->processing
.bmControls
;
1449 size
= entity
->processing
.bControlSize
;
1451 for (i
= 0; i
< ARRAY_SIZE(blacklist
); ++i
) {
1452 if (!usb_match_one_id(dev
->intf
, &blacklist
[i
].id
))
1455 if (blacklist
[i
].index
>= 8 * size
||
1456 !uvc_test_bit(controls
, blacklist
[i
].index
))
1459 uvc_trace(UVC_TRACE_CONTROL
, "%u/%u control is black listed, "
1460 "removing it.\n", entity
->id
, blacklist
[i
].index
);
1462 uvc_clear_bit(controls
, blacklist
[i
].index
);
1467 * Initialize device controls.
1469 int uvc_ctrl_init_device(struct uvc_device
*dev
)
1471 struct uvc_control_info
*info
;
1472 struct uvc_control
*ctrl
;
1473 struct uvc_entity
*entity
;
1476 /* Walk the entities list and instantiate controls */
1477 list_for_each_entry(entity
, &dev
->entities
, list
) {
1478 unsigned int bControlSize
= 0, ncontrols
= 0;
1479 __u8
*bmControls
= NULL
;
1481 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
1482 bmControls
= entity
->extension
.bmControls
;
1483 bControlSize
= entity
->extension
.bControlSize
;
1484 } else if (UVC_ENTITY_TYPE(entity
) == UVC_VC_PROCESSING_UNIT
) {
1485 bmControls
= entity
->processing
.bmControls
;
1486 bControlSize
= entity
->processing
.bControlSize
;
1487 } else if (UVC_ENTITY_TYPE(entity
) == UVC_ITT_CAMERA
) {
1488 bmControls
= entity
->camera
.bmControls
;
1489 bControlSize
= entity
->camera
.bControlSize
;
1492 uvc_ctrl_prune_entity(dev
, entity
);
1494 for (i
= 0; i
< bControlSize
; ++i
)
1495 ncontrols
+= hweight8(bmControls
[i
]);
1500 entity
->controls
= kzalloc(ncontrols
*sizeof *ctrl
, GFP_KERNEL
);
1501 if (entity
->controls
== NULL
)
1504 entity
->ncontrols
= ncontrols
;
1506 ctrl
= entity
->controls
;
1507 for (i
= 0; i
< bControlSize
* 8; ++i
) {
1508 if (uvc_test_bit(bmControls
, i
) == 0)
1511 ctrl
->entity
= entity
;
1517 /* Walk the controls info list and associate them with the device
1518 * controls, then add the device to the global device list. This has
1519 * to be done while holding the controls lock, to make sure
1520 * uvc_ctrl_add_info() will not get called in-between.
1522 mutex_lock(&uvc_driver
.ctrl_mutex
);
1523 list_for_each_entry(info
, &uvc_driver
.controls
, list
)
1524 uvc_ctrl_add_ctrl(dev
, info
);
1526 list_add_tail(&dev
->list
, &uvc_driver
.devices
);
1527 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1533 * Cleanup device controls.
1535 void uvc_ctrl_cleanup_device(struct uvc_device
*dev
)
1537 struct uvc_entity
*entity
;
1540 /* Remove the device from the global devices list */
1541 mutex_lock(&uvc_driver
.ctrl_mutex
);
1542 if (dev
->list
.next
!= NULL
)
1543 list_del(&dev
->list
);
1544 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1546 list_for_each_entry(entity
, &dev
->entities
, list
) {
1547 for (i
= 0; i
< entity
->ncontrols
; ++i
)
1548 kfree(entity
->controls
[i
].data
);
1550 kfree(entity
->controls
);
1554 void uvc_ctrl_init(void)
1556 struct uvc_control_info
*ctrl
= uvc_ctrls
;
1557 struct uvc_control_info
*cend
= ctrl
+ ARRAY_SIZE(uvc_ctrls
);
1558 struct uvc_control_mapping
*mapping
= uvc_ctrl_mappings
;
1559 struct uvc_control_mapping
*mend
=
1560 mapping
+ ARRAY_SIZE(uvc_ctrl_mappings
);
1562 for (; ctrl
< cend
; ++ctrl
)
1563 uvc_ctrl_add_info(ctrl
);
1565 for (; mapping
< mend
; ++mapping
)
1566 uvc_ctrl_add_mapping(mapping
);