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/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_RANGE
126 | 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_GET_CUR
221 | UVC_CONTROL_RESTORE
,
224 .entity
= UVC_GUID_UVC_CAMERA
,
225 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
228 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
229 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
232 .entity
= UVC_GUID_UVC_CAMERA
,
233 .selector
= UVC_CT_FOCUS_RELATIVE_CONTROL
,
236 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
237 | 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_GET_CUR
253 | UVC_CONTROL_AUTO_UPDATE
,
256 .entity
= UVC_GUID_UVC_CAMERA
,
257 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
260 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
261 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
264 .entity
= UVC_GUID_UVC_CAMERA
,
265 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
268 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
269 | 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_RANGE
285 | UVC_CONTROL_AUTO_UPDATE
,
288 .entity
= UVC_GUID_UVC_CAMERA
,
289 .selector
= UVC_CT_ROLL_ABSOLUTE_CONTROL
,
292 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
293 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
296 .entity
= UVC_GUID_UVC_CAMERA
,
297 .selector
= UVC_CT_ROLL_RELATIVE_CONTROL
,
300 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_RANGE
301 | UVC_CONTROL_AUTO_UPDATE
,
304 .entity
= UVC_GUID_UVC_CAMERA
,
305 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
308 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
309 | UVC_CONTROL_GET_DEF
| UVC_CONTROL_RESTORE
,
312 .entity
= UVC_GUID_UVC_CAMERA
,
313 .selector
= UVC_CT_PRIVACY_CONTROL
,
316 .flags
= UVC_CONTROL_SET_CUR
| UVC_CONTROL_GET_CUR
317 | UVC_CONTROL_RESTORE
| UVC_CONTROL_AUTO_UPDATE
,
321 static struct uvc_menu_info power_line_frequency_controls
[] = {
327 static struct uvc_menu_info exposure_auto_controls
[] = {
329 { 1, "Manual Mode" },
330 { 4, "Shutter Priority Mode" },
331 { 8, "Aperture Priority Mode" },
334 static __s32
uvc_ctrl_get_zoom(struct uvc_control_mapping
*mapping
,
335 __u8 query
, const __u8
*data
)
337 __s8 zoom
= (__s8
)data
[0];
341 return (zoom
== 0) ? 0 : (zoom
> 0 ? data
[2] : -data
[2]);
352 static void uvc_ctrl_set_zoom(struct uvc_control_mapping
*mapping
,
353 __s32 value
, __u8
*data
)
355 data
[0] = value
== 0 ? 0 : (value
> 0) ? 1 : 0xff;
356 data
[2] = min((int)abs(value
), 0xff);
359 static struct uvc_control_mapping uvc_ctrl_mappings
[] = {
361 .id
= V4L2_CID_BRIGHTNESS
,
362 .name
= "Brightness",
363 .entity
= UVC_GUID_UVC_PROCESSING
,
364 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
367 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
368 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
371 .id
= V4L2_CID_CONTRAST
,
373 .entity
= UVC_GUID_UVC_PROCESSING
,
374 .selector
= UVC_PU_CONTRAST_CONTROL
,
377 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
378 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
383 .entity
= UVC_GUID_UVC_PROCESSING
,
384 .selector
= UVC_PU_HUE_CONTROL
,
387 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
388 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
391 .id
= V4L2_CID_SATURATION
,
392 .name
= "Saturation",
393 .entity
= UVC_GUID_UVC_PROCESSING
,
394 .selector
= UVC_PU_SATURATION_CONTROL
,
397 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
398 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
401 .id
= V4L2_CID_SHARPNESS
,
403 .entity
= UVC_GUID_UVC_PROCESSING
,
404 .selector
= UVC_PU_SHARPNESS_CONTROL
,
407 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
408 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
411 .id
= V4L2_CID_GAMMA
,
413 .entity
= UVC_GUID_UVC_PROCESSING
,
414 .selector
= UVC_PU_GAMMA_CONTROL
,
417 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
418 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
421 .id
= V4L2_CID_BACKLIGHT_COMPENSATION
,
422 .name
= "Backlight Compensation",
423 .entity
= UVC_GUID_UVC_PROCESSING
,
424 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
427 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
428 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
433 .entity
= UVC_GUID_UVC_PROCESSING
,
434 .selector
= UVC_PU_GAIN_CONTROL
,
437 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
438 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
441 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
442 .name
= "Power Line Frequency",
443 .entity
= UVC_GUID_UVC_PROCESSING
,
444 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
447 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
448 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
449 .menu_info
= power_line_frequency_controls
,
450 .menu_count
= ARRAY_SIZE(power_line_frequency_controls
),
453 .id
= V4L2_CID_HUE_AUTO
,
455 .entity
= UVC_GUID_UVC_PROCESSING
,
456 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
459 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
460 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
463 .id
= V4L2_CID_EXPOSURE_AUTO
,
464 .name
= "Exposure, Auto",
465 .entity
= UVC_GUID_UVC_CAMERA
,
466 .selector
= UVC_CT_AE_MODE_CONTROL
,
469 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
470 .data_type
= UVC_CTRL_DATA_TYPE_BITMASK
,
471 .menu_info
= exposure_auto_controls
,
472 .menu_count
= ARRAY_SIZE(exposure_auto_controls
),
475 .id
= V4L2_CID_EXPOSURE_AUTO_PRIORITY
,
476 .name
= "Exposure, Auto Priority",
477 .entity
= UVC_GUID_UVC_CAMERA
,
478 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
481 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
482 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
485 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
486 .name
= "Exposure (Absolute)",
487 .entity
= UVC_GUID_UVC_CAMERA
,
488 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
491 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
492 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
495 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
496 .name
= "White Balance Temperature, Auto",
497 .entity
= UVC_GUID_UVC_PROCESSING
,
498 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
501 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
502 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
505 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
506 .name
= "White Balance Temperature",
507 .entity
= UVC_GUID_UVC_PROCESSING
,
508 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
511 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
512 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
515 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
516 .name
= "White Balance Component, Auto",
517 .entity
= UVC_GUID_UVC_PROCESSING
,
518 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
521 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
522 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
525 .id
= V4L2_CID_BLUE_BALANCE
,
526 .name
= "White Balance Blue Component",
527 .entity
= UVC_GUID_UVC_PROCESSING
,
528 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
531 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
532 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
535 .id
= V4L2_CID_RED_BALANCE
,
536 .name
= "White Balance Red Component",
537 .entity
= UVC_GUID_UVC_PROCESSING
,
538 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
541 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
542 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
545 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
546 .name
= "Focus (absolute)",
547 .entity
= UVC_GUID_UVC_CAMERA
,
548 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
551 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
552 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
555 .id
= V4L2_CID_FOCUS_AUTO
,
556 .name
= "Focus, Auto",
557 .entity
= UVC_GUID_UVC_CAMERA
,
558 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
561 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
562 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
565 .id
= V4L2_CID_ZOOM_ABSOLUTE
,
566 .name
= "Zoom, Absolute",
567 .entity
= UVC_GUID_UVC_CAMERA
,
568 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
571 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
572 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
575 .id
= V4L2_CID_ZOOM_CONTINUOUS
,
576 .name
= "Zoom, Continuous",
577 .entity
= UVC_GUID_UVC_CAMERA
,
578 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
581 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
582 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
583 .get
= uvc_ctrl_get_zoom
,
584 .set
= uvc_ctrl_set_zoom
,
587 .id
= V4L2_CID_PRIVACY
,
589 .entity
= UVC_GUID_UVC_CAMERA
,
590 .selector
= UVC_CT_PRIVACY_CONTROL
,
593 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
594 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
598 /* ------------------------------------------------------------------------
602 static inline __u8
*uvc_ctrl_data(struct uvc_control
*ctrl
, int id
)
604 return ctrl
->data
+ id
* ctrl
->info
->size
;
607 static inline int uvc_test_bit(const __u8
*data
, int bit
)
609 return (data
[bit
>> 3] >> (bit
& 7)) & 1;
612 static inline void uvc_clear_bit(__u8
*data
, int bit
)
614 data
[bit
>> 3] &= ~(1 << (bit
& 7));
617 /* Extract the bit string specified by mapping->offset and mapping->size
618 * from the little-endian data stored at 'data' and return the result as
619 * a signed 32bit integer. Sign extension will be performed if the mapping
620 * references a signed data type.
622 static __s32
uvc_get_le_value(struct uvc_control_mapping
*mapping
,
623 __u8 query
, const __u8
*data
)
625 int bits
= mapping
->size
;
626 int offset
= mapping
->offset
;
632 mask
= ((1LL << bits
) - 1) << offset
;
634 for (; bits
> 0; data
++) {
635 __u8 byte
= *data
& mask
;
636 value
|= offset
> 0 ? (byte
>> offset
) : (byte
<< (-offset
));
637 bits
-= 8 - (offset
> 0 ? offset
: 0);
639 mask
= (1 << bits
) - 1;
642 /* Sign-extend the value if needed. */
643 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
644 value
|= -(value
& (1 << (mapping
->size
- 1)));
649 /* Set the bit string specified by mapping->offset and mapping->size
650 * in the little-endian data stored at 'data' to the value 'value'.
652 static void uvc_set_le_value(struct uvc_control_mapping
*mapping
,
653 __s32 value
, __u8
*data
)
655 int bits
= mapping
->size
;
656 int offset
= mapping
->offset
;
662 for (; bits
> 0; data
++) {
663 mask
= ((1LL << bits
) - 1) << offset
;
664 *data
= (*data
& ~mask
) | ((value
<< offset
) & mask
);
665 value
>>= offset
? offset
: 8;
671 /* ------------------------------------------------------------------------
672 * Terminal and unit management
675 static const __u8 uvc_processing_guid
[16] = UVC_GUID_UVC_PROCESSING
;
676 static const __u8 uvc_camera_guid
[16] = UVC_GUID_UVC_CAMERA
;
677 static const __u8 uvc_media_transport_input_guid
[16] =
678 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT
;
680 static int uvc_entity_match_guid(struct uvc_entity
*entity
, __u8 guid
[16])
682 switch (UVC_ENTITY_TYPE(entity
)) {
684 return memcmp(uvc_camera_guid
, guid
, 16) == 0;
686 case UVC_ITT_MEDIA_TRANSPORT_INPUT
:
687 return memcmp(uvc_media_transport_input_guid
, guid
, 16) == 0;
689 case UVC_VC_PROCESSING_UNIT
:
690 return memcmp(uvc_processing_guid
, guid
, 16) == 0;
692 case UVC_VC_EXTENSION_UNIT
:
693 return memcmp(entity
->extension
.guidExtensionCode
,
701 /* ------------------------------------------------------------------------
705 static void __uvc_find_control(struct uvc_entity
*entity
, __u32 v4l2_id
,
706 struct uvc_control_mapping
**mapping
, struct uvc_control
**control
,
709 struct uvc_control
*ctrl
;
710 struct uvc_control_mapping
*map
;
716 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
717 ctrl
= &entity
->controls
[i
];
718 if (ctrl
->info
== NULL
)
721 list_for_each_entry(map
, &ctrl
->info
->mappings
, list
) {
722 if ((map
->id
== v4l2_id
) && !next
) {
728 if ((*mapping
== NULL
|| (*mapping
)->id
> map
->id
) &&
729 (map
->id
> v4l2_id
) && next
) {
737 struct uvc_control
*uvc_find_control(struct uvc_video_chain
*chain
,
738 __u32 v4l2_id
, struct uvc_control_mapping
**mapping
)
740 struct uvc_control
*ctrl
= NULL
;
741 struct uvc_entity
*entity
;
742 int next
= v4l2_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
746 /* Mask the query flags. */
747 v4l2_id
&= V4L2_CTRL_ID_MASK
;
749 /* Find the control. */
750 list_for_each_entry(entity
, &chain
->entities
, chain
) {
751 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
756 if (ctrl
== NULL
&& !next
)
757 uvc_trace(UVC_TRACE_CONTROL
, "Control 0x%08x not found.\n",
763 static int uvc_ctrl_populate_cache(struct uvc_video_chain
*chain
,
764 struct uvc_control
*ctrl
)
768 if (ctrl
->info
->flags
& UVC_CONTROL_GET_DEF
) {
769 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_DEF
, ctrl
->entity
->id
,
770 chain
->dev
->intfnum
, ctrl
->info
->selector
,
771 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
),
777 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MIN
) {
778 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MIN
, ctrl
->entity
->id
,
779 chain
->dev
->intfnum
, ctrl
->info
->selector
,
780 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
),
785 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MAX
) {
786 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MAX
, ctrl
->entity
->id
,
787 chain
->dev
->intfnum
, ctrl
->info
->selector
,
788 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
),
793 if (ctrl
->info
->flags
& UVC_CONTROL_GET_RES
) {
794 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_RES
, ctrl
->entity
->id
,
795 chain
->dev
->intfnum
, ctrl
->info
->selector
,
796 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
),
806 int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
807 struct v4l2_queryctrl
*v4l2_ctrl
)
809 struct uvc_control
*ctrl
;
810 struct uvc_control_mapping
*mapping
;
811 struct uvc_menu_info
*menu
;
815 ctrl
= uvc_find_control(chain
, v4l2_ctrl
->id
, &mapping
);
819 memset(v4l2_ctrl
, 0, sizeof *v4l2_ctrl
);
820 v4l2_ctrl
->id
= mapping
->id
;
821 v4l2_ctrl
->type
= mapping
->v4l2_type
;
822 strlcpy(v4l2_ctrl
->name
, mapping
->name
, sizeof v4l2_ctrl
->name
);
823 v4l2_ctrl
->flags
= 0;
825 if (!(ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
))
826 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
829 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
834 if (ctrl
->info
->flags
& UVC_CONTROL_GET_DEF
) {
835 v4l2_ctrl
->default_value
= mapping
->get(mapping
, UVC_GET_DEF
,
836 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
));
839 switch (mapping
->v4l2_type
) {
840 case V4L2_CTRL_TYPE_MENU
:
841 v4l2_ctrl
->minimum
= 0;
842 v4l2_ctrl
->maximum
= mapping
->menu_count
- 1;
845 menu
= mapping
->menu_info
;
846 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
847 if (menu
->value
== v4l2_ctrl
->default_value
) {
848 v4l2_ctrl
->default_value
= i
;
855 case V4L2_CTRL_TYPE_BOOLEAN
:
856 v4l2_ctrl
->minimum
= 0;
857 v4l2_ctrl
->maximum
= 1;
861 case V4L2_CTRL_TYPE_BUTTON
:
862 v4l2_ctrl
->minimum
= 0;
863 v4l2_ctrl
->maximum
= 0;
871 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MIN
)
872 v4l2_ctrl
->minimum
= mapping
->get(mapping
, UVC_GET_MIN
,
873 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
875 if (ctrl
->info
->flags
& UVC_CONTROL_GET_MAX
)
876 v4l2_ctrl
->maximum
= mapping
->get(mapping
, UVC_GET_MAX
,
877 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
879 if (ctrl
->info
->flags
& UVC_CONTROL_GET_RES
)
880 v4l2_ctrl
->step
= mapping
->get(mapping
, UVC_GET_RES
,
881 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
887 /* --------------------------------------------------------------------------
888 * Control transactions
890 * To make extended set operations as atomic as the hardware allows, controls
891 * are handled using begin/commit/rollback operations.
893 * At the beginning of a set request, uvc_ctrl_begin should be called to
894 * initialize the request. This function acquires the control lock.
896 * When setting a control, the new value is stored in the control data field
897 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
898 * later processing. If the UVC and V4L2 control sizes differ, the current
899 * value is loaded from the hardware before storing the new value in the data
902 * After processing all controls in the transaction, uvc_ctrl_commit or
903 * uvc_ctrl_rollback must be called to apply the pending changes to the
904 * hardware or revert them. When applying changes, all controls marked as
905 * dirty will be modified in the UVC device, and the dirty flag will be
906 * cleared. When reverting controls, the control data field
907 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
908 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
911 int uvc_ctrl_begin(struct uvc_video_chain
*chain
)
913 return mutex_lock_interruptible(&chain
->ctrl_mutex
) ? -ERESTARTSYS
: 0;
916 static int uvc_ctrl_commit_entity(struct uvc_device
*dev
,
917 struct uvc_entity
*entity
, int rollback
)
919 struct uvc_control
*ctrl
;
926 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
927 ctrl
= &entity
->controls
[i
];
928 if (ctrl
->info
== NULL
)
931 /* Reset the loaded flag for auto-update controls that were
932 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
933 * uvc_ctrl_get from using the cached value.
935 if (ctrl
->info
->flags
& UVC_CONTROL_AUTO_UPDATE
)
942 ret
= uvc_query_ctrl(dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
943 dev
->intfnum
, ctrl
->info
->selector
,
944 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
949 if (rollback
|| ret
< 0)
950 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
951 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
963 int __uvc_ctrl_commit(struct uvc_video_chain
*chain
, int rollback
)
965 struct uvc_entity
*entity
;
968 /* Find the control. */
969 list_for_each_entry(entity
, &chain
->entities
, chain
) {
970 ret
= uvc_ctrl_commit_entity(chain
->dev
, entity
, rollback
);
976 mutex_unlock(&chain
->ctrl_mutex
);
980 int uvc_ctrl_get(struct uvc_video_chain
*chain
,
981 struct v4l2_ext_control
*xctrl
)
983 struct uvc_control
*ctrl
;
984 struct uvc_control_mapping
*mapping
;
985 struct uvc_menu_info
*menu
;
989 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
990 if (ctrl
== NULL
|| (ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
) == 0)
994 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
, ctrl
->entity
->id
,
995 chain
->dev
->intfnum
, ctrl
->info
->selector
,
996 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1004 xctrl
->value
= mapping
->get(mapping
, UVC_GET_CUR
,
1005 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1007 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
1008 menu
= mapping
->menu_info
;
1009 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
1010 if (menu
->value
== xctrl
->value
) {
1020 int uvc_ctrl_set(struct uvc_video_chain
*chain
,
1021 struct v4l2_ext_control
*xctrl
)
1023 struct uvc_control
*ctrl
;
1024 struct uvc_control_mapping
*mapping
;
1031 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1032 if (ctrl
== NULL
|| (ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
) == 0)
1035 /* Clamp out of range values. */
1036 switch (mapping
->v4l2_type
) {
1037 case V4L2_CTRL_TYPE_INTEGER
:
1038 if (!ctrl
->cached
) {
1039 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1044 min
= mapping
->get(mapping
, UVC_GET_MIN
,
1045 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1046 max
= mapping
->get(mapping
, UVC_GET_MAX
,
1047 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1048 step
= mapping
->get(mapping
, UVC_GET_RES
,
1049 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1051 xctrl
->value
= min
+ (xctrl
->value
- min
+ step
/2) / step
* step
;
1052 xctrl
->value
= clamp(xctrl
->value
, min
, max
);
1053 value
= xctrl
->value
;
1056 case V4L2_CTRL_TYPE_BOOLEAN
:
1057 xctrl
->value
= clamp(xctrl
->value
, 0, 1);
1058 value
= xctrl
->value
;
1061 case V4L2_CTRL_TYPE_MENU
:
1062 if (xctrl
->value
< 0 || xctrl
->value
>= mapping
->menu_count
)
1064 value
= mapping
->menu_info
[xctrl
->value
].value
;
1068 value
= xctrl
->value
;
1072 /* If the mapping doesn't span the whole UVC control, the current value
1073 * needs to be loaded from the device to perform the read-modify-write
1076 if (!ctrl
->loaded
&& (ctrl
->info
->size
* 8) != mapping
->size
) {
1077 if ((ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
) == 0) {
1078 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1079 0, ctrl
->info
->size
);
1081 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
1082 ctrl
->entity
->id
, chain
->dev
->intfnum
,
1083 ctrl
->info
->selector
,
1084 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1093 /* Backup the current value in case we need to rollback later. */
1095 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1096 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1100 mapping
->set(mapping
, value
,
1101 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1108 /* --------------------------------------------------------------------------
1112 int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
1113 struct uvc_xu_control
*xctrl
, int set
)
1115 struct uvc_entity
*entity
;
1116 struct uvc_control
*ctrl
= NULL
;
1117 unsigned int i
, found
= 0;
1121 /* Find the extension unit. */
1122 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1123 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
&&
1124 entity
->id
== xctrl
->unit
)
1128 if (entity
->id
!= xctrl
->unit
) {
1129 uvc_trace(UVC_TRACE_CONTROL
, "Extension unit %u not found.\n",
1134 /* Find the control. */
1135 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1136 ctrl
= &entity
->controls
[i
];
1137 if (ctrl
->info
== NULL
)
1140 if (ctrl
->info
->selector
== xctrl
->selector
) {
1147 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u not found.\n",
1148 entity
->extension
.guidExtensionCode
, xctrl
->selector
);
1152 /* Validate control data size. */
1153 if (ctrl
->info
->size
!= xctrl
->size
)
1156 if ((set
&& !(ctrl
->info
->flags
& UVC_CONTROL_SET_CUR
)) ||
1157 (!set
&& !(ctrl
->info
->flags
& UVC_CONTROL_GET_CUR
)))
1160 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
1161 return -ERESTARTSYS
;
1163 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1164 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1166 data
= uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
);
1168 if (set
&& copy_from_user(data
, xctrl
->data
, xctrl
->size
)) {
1173 ret
= uvc_query_ctrl(chain
->dev
, set
? UVC_SET_CUR
: UVC_GET_CUR
,
1174 xctrl
->unit
, chain
->dev
->intfnum
, xctrl
->selector
,
1179 if (!set
&& copy_to_user(xctrl
->data
, data
, xctrl
->size
)) {
1186 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1187 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1190 mutex_unlock(&chain
->ctrl_mutex
);
1194 /* --------------------------------------------------------------------------
1199 * Restore control values after resume, skipping controls that haven't been
1203 * - Don't restore modified controls that are back to their default value.
1204 * - Handle restore order (Auto-Exposure Mode should be restored before
1207 int uvc_ctrl_resume_device(struct uvc_device
*dev
)
1209 struct uvc_control
*ctrl
;
1210 struct uvc_entity
*entity
;
1214 /* Walk the entities list and restore controls when possible. */
1215 list_for_each_entry(entity
, &dev
->entities
, list
) {
1217 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1218 ctrl
= &entity
->controls
[i
];
1220 if (ctrl
->info
== NULL
|| !ctrl
->modified
||
1221 (ctrl
->info
->flags
& UVC_CONTROL_RESTORE
) == 0)
1224 printk(KERN_INFO
"restoring control %pUl/%u/%u\n",
1225 ctrl
->info
->entity
, ctrl
->info
->index
,
1226 ctrl
->info
->selector
);
1230 ret
= uvc_ctrl_commit_entity(dev
, entity
, 0);
1238 /* --------------------------------------------------------------------------
1239 * Control and mapping handling
1242 static void uvc_ctrl_add_ctrl(struct uvc_device
*dev
,
1243 struct uvc_control_info
*info
)
1245 struct uvc_entity
*entity
;
1246 struct uvc_control
*ctrl
= NULL
;
1250 list_for_each_entry(entity
, &dev
->entities
, list
) {
1251 if (!uvc_entity_match_guid(entity
, info
->entity
))
1254 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1255 ctrl
= &entity
->controls
[i
];
1256 if (ctrl
->index
== info
->index
) {
1269 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
1270 /* Check if the device control information and length match
1271 * the user supplied information.
1277 ret
= uvc_query_ctrl(dev
, UVC_GET_LEN
, ctrl
->entity
->id
,
1278 dev
->intfnum
, info
->selector
, (__u8
*)&size
, 2);
1280 uvc_trace(UVC_TRACE_CONTROL
,
1281 "GET_LEN failed on control %pUl/%u (%d).\n",
1282 info
->entity
, info
->selector
, ret
);
1286 if (info
->size
!= le16_to_cpu(size
)) {
1287 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u size "
1288 "doesn't match user supplied value.\n",
1289 info
->entity
, info
->selector
);
1293 ret
= uvc_query_ctrl(dev
, UVC_GET_INFO
, ctrl
->entity
->id
,
1294 dev
->intfnum
, info
->selector
, &inf
, 1);
1296 uvc_trace(UVC_TRACE_CONTROL
,
1297 "GET_INFO failed on control %pUl/%u (%d).\n",
1298 info
->entity
, info
->selector
, ret
);
1302 flags
= info
->flags
;
1303 if (((flags
& UVC_CONTROL_GET_CUR
) && !(inf
& (1 << 0))) ||
1304 ((flags
& UVC_CONTROL_SET_CUR
) && !(inf
& (1 << 1)))) {
1305 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u flags "
1306 "don't match supported operations.\n",
1307 info
->entity
, info
->selector
);
1313 ctrl
->data
= kmalloc(ctrl
->info
->size
* UVC_CTRL_DATA_LAST
, GFP_KERNEL
);
1314 uvc_trace(UVC_TRACE_CONTROL
, "Added control %pUl/%u to device %s "
1315 "entity %u\n", ctrl
->info
->entity
, ctrl
->info
->selector
,
1316 dev
->udev
->devpath
, entity
->id
);
1320 * Add an item to the UVC control information list, and instantiate a control
1321 * structure for each device that supports the control.
1323 int uvc_ctrl_add_info(struct uvc_control_info
*info
)
1325 struct uvc_control_info
*ctrl
;
1326 struct uvc_device
*dev
;
1329 /* Find matching controls by walking the devices, entities and
1332 mutex_lock(&uvc_driver
.ctrl_mutex
);
1334 /* First check if the list contains a control matching the new one.
1335 * Bail out if it does.
1337 list_for_each_entry(ctrl
, &uvc_driver
.controls
, list
) {
1338 if (memcmp(ctrl
->entity
, info
->entity
, 16))
1341 if (ctrl
->selector
== info
->selector
) {
1342 uvc_trace(UVC_TRACE_CONTROL
,
1343 "Control %pUl/%u is already defined.\n",
1344 info
->entity
, info
->selector
);
1348 if (ctrl
->index
== info
->index
) {
1349 uvc_trace(UVC_TRACE_CONTROL
,
1350 "Control %pUl/%u would overwrite index %d.\n",
1351 info
->entity
, info
->selector
, info
->index
);
1357 list_for_each_entry(dev
, &uvc_driver
.devices
, list
)
1358 uvc_ctrl_add_ctrl(dev
, info
);
1360 INIT_LIST_HEAD(&info
->mappings
);
1361 list_add_tail(&info
->list
, &uvc_driver
.controls
);
1363 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1367 int uvc_ctrl_add_mapping(struct uvc_control_mapping
*mapping
)
1369 struct uvc_control_info
*info
;
1370 struct uvc_control_mapping
*map
;
1373 if (mapping
->get
== NULL
)
1374 mapping
->get
= uvc_get_le_value
;
1375 if (mapping
->set
== NULL
)
1376 mapping
->set
= uvc_set_le_value
;
1378 if (mapping
->id
& ~V4L2_CTRL_ID_MASK
) {
1379 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s' with "
1380 "invalid control id 0x%08x\n", mapping
->name
,
1385 mutex_lock(&uvc_driver
.ctrl_mutex
);
1386 list_for_each_entry(info
, &uvc_driver
.controls
, list
) {
1387 if (memcmp(info
->entity
, mapping
->entity
, 16) ||
1388 info
->selector
!= mapping
->selector
)
1391 if (info
->size
* 8 < mapping
->size
+ mapping
->offset
) {
1392 uvc_trace(UVC_TRACE_CONTROL
,
1393 "Mapping '%s' would overflow control %pUl/%u\n",
1394 mapping
->name
, info
->entity
, info
->selector
);
1399 /* Check if the list contains a mapping matching the new one.
1400 * Bail out if it does.
1402 list_for_each_entry(map
, &info
->mappings
, list
) {
1403 if (map
->id
== mapping
->id
) {
1404 uvc_trace(UVC_TRACE_CONTROL
, "Mapping '%s' is "
1405 "already defined.\n", mapping
->name
);
1411 mapping
->ctrl
= info
;
1412 list_add_tail(&mapping
->list
, &info
->mappings
);
1413 uvc_trace(UVC_TRACE_CONTROL
,
1414 "Adding mapping %s to control %pUl/%u.\n",
1415 mapping
->name
, info
->entity
, info
->selector
);
1421 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1426 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1427 * are currently the ones that crash the camera or unconditionally return an
1428 * error when queried.
1431 uvc_ctrl_prune_entity(struct uvc_device
*dev
, struct uvc_entity
*entity
)
1433 static const struct {
1434 struct usb_device_id id
;
1437 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1438 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1439 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1446 if (UVC_ENTITY_TYPE(entity
) != UVC_VC_PROCESSING_UNIT
)
1449 controls
= entity
->processing
.bmControls
;
1450 size
= entity
->processing
.bControlSize
;
1452 for (i
= 0; i
< ARRAY_SIZE(blacklist
); ++i
) {
1453 if (!usb_match_one_id(dev
->intf
, &blacklist
[i
].id
))
1456 if (blacklist
[i
].index
>= 8 * size
||
1457 !uvc_test_bit(controls
, blacklist
[i
].index
))
1460 uvc_trace(UVC_TRACE_CONTROL
, "%u/%u control is black listed, "
1461 "removing it.\n", entity
->id
, blacklist
[i
].index
);
1463 uvc_clear_bit(controls
, blacklist
[i
].index
);
1468 * Initialize device controls.
1470 int uvc_ctrl_init_device(struct uvc_device
*dev
)
1472 struct uvc_control_info
*info
;
1473 struct uvc_control
*ctrl
;
1474 struct uvc_entity
*entity
;
1477 /* Walk the entities list and instantiate controls */
1478 list_for_each_entry(entity
, &dev
->entities
, list
) {
1479 unsigned int bControlSize
= 0, ncontrols
= 0;
1480 __u8
*bmControls
= NULL
;
1482 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
1483 bmControls
= entity
->extension
.bmControls
;
1484 bControlSize
= entity
->extension
.bControlSize
;
1485 } else if (UVC_ENTITY_TYPE(entity
) == UVC_VC_PROCESSING_UNIT
) {
1486 bmControls
= entity
->processing
.bmControls
;
1487 bControlSize
= entity
->processing
.bControlSize
;
1488 } else if (UVC_ENTITY_TYPE(entity
) == UVC_ITT_CAMERA
) {
1489 bmControls
= entity
->camera
.bmControls
;
1490 bControlSize
= entity
->camera
.bControlSize
;
1493 uvc_ctrl_prune_entity(dev
, entity
);
1495 for (i
= 0; i
< bControlSize
; ++i
)
1496 ncontrols
+= hweight8(bmControls
[i
]);
1501 entity
->controls
= kzalloc(ncontrols
*sizeof *ctrl
, GFP_KERNEL
);
1502 if (entity
->controls
== NULL
)
1505 entity
->ncontrols
= ncontrols
;
1507 ctrl
= entity
->controls
;
1508 for (i
= 0; i
< bControlSize
* 8; ++i
) {
1509 if (uvc_test_bit(bmControls
, i
) == 0)
1512 ctrl
->entity
= entity
;
1518 /* Walk the controls info list and associate them with the device
1519 * controls, then add the device to the global device list. This has
1520 * to be done while holding the controls lock, to make sure
1521 * uvc_ctrl_add_info() will not get called in-between.
1523 mutex_lock(&uvc_driver
.ctrl_mutex
);
1524 list_for_each_entry(info
, &uvc_driver
.controls
, list
)
1525 uvc_ctrl_add_ctrl(dev
, info
);
1527 list_add_tail(&dev
->list
, &uvc_driver
.devices
);
1528 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1534 * Cleanup device controls.
1536 void uvc_ctrl_cleanup_device(struct uvc_device
*dev
)
1538 struct uvc_entity
*entity
;
1541 /* Remove the device from the global devices list */
1542 mutex_lock(&uvc_driver
.ctrl_mutex
);
1543 if (dev
->list
.next
!= NULL
)
1544 list_del(&dev
->list
);
1545 mutex_unlock(&uvc_driver
.ctrl_mutex
);
1547 list_for_each_entry(entity
, &dev
->entities
, list
) {
1548 for (i
= 0; i
< entity
->ncontrols
; ++i
)
1549 kfree(entity
->controls
[i
].data
);
1551 kfree(entity
->controls
);
1555 void uvc_ctrl_init(void)
1557 struct uvc_control_info
*ctrl
= uvc_ctrls
;
1558 struct uvc_control_info
*cend
= ctrl
+ ARRAY_SIZE(uvc_ctrls
);
1559 struct uvc_control_mapping
*mapping
= uvc_ctrl_mappings
;
1560 struct uvc_control_mapping
*mend
=
1561 mapping
+ ARRAY_SIZE(uvc_ctrl_mappings
);
1563 for (; ctrl
< cend
; ++ctrl
)
1564 uvc_ctrl_add_info(ctrl
);
1566 for (; mapping
< mend
; ++mapping
)
1567 uvc_ctrl_add_mapping(mapping
);