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 <linux/atomic.h>
24 #include <media/v4l2-ctrls.h>
28 #define UVC_CTRL_DATA_CURRENT 0
29 #define UVC_CTRL_DATA_BACKUP 1
30 #define UVC_CTRL_DATA_MIN 2
31 #define UVC_CTRL_DATA_MAX 3
32 #define UVC_CTRL_DATA_RES 4
33 #define UVC_CTRL_DATA_DEF 5
34 #define UVC_CTRL_DATA_LAST 6
36 /* ------------------------------------------------------------------------
40 static struct uvc_control_info uvc_ctrls
[] = {
42 .entity
= UVC_GUID_UVC_PROCESSING
,
43 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
46 .flags
= UVC_CTRL_FLAG_SET_CUR
47 | UVC_CTRL_FLAG_GET_RANGE
48 | UVC_CTRL_FLAG_RESTORE
,
51 .entity
= UVC_GUID_UVC_PROCESSING
,
52 .selector
= UVC_PU_CONTRAST_CONTROL
,
55 .flags
= UVC_CTRL_FLAG_SET_CUR
56 | UVC_CTRL_FLAG_GET_RANGE
57 | UVC_CTRL_FLAG_RESTORE
,
60 .entity
= UVC_GUID_UVC_PROCESSING
,
61 .selector
= UVC_PU_HUE_CONTROL
,
64 .flags
= UVC_CTRL_FLAG_SET_CUR
65 | UVC_CTRL_FLAG_GET_RANGE
66 | UVC_CTRL_FLAG_RESTORE
67 | UVC_CTRL_FLAG_AUTO_UPDATE
,
70 .entity
= UVC_GUID_UVC_PROCESSING
,
71 .selector
= UVC_PU_SATURATION_CONTROL
,
74 .flags
= UVC_CTRL_FLAG_SET_CUR
75 | UVC_CTRL_FLAG_GET_RANGE
76 | UVC_CTRL_FLAG_RESTORE
,
79 .entity
= UVC_GUID_UVC_PROCESSING
,
80 .selector
= UVC_PU_SHARPNESS_CONTROL
,
83 .flags
= UVC_CTRL_FLAG_SET_CUR
84 | UVC_CTRL_FLAG_GET_RANGE
85 | UVC_CTRL_FLAG_RESTORE
,
88 .entity
= UVC_GUID_UVC_PROCESSING
,
89 .selector
= UVC_PU_GAMMA_CONTROL
,
92 .flags
= UVC_CTRL_FLAG_SET_CUR
93 | UVC_CTRL_FLAG_GET_RANGE
94 | UVC_CTRL_FLAG_RESTORE
,
97 .entity
= UVC_GUID_UVC_PROCESSING
,
98 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
101 .flags
= UVC_CTRL_FLAG_SET_CUR
102 | UVC_CTRL_FLAG_GET_RANGE
103 | UVC_CTRL_FLAG_RESTORE
104 | UVC_CTRL_FLAG_AUTO_UPDATE
,
107 .entity
= UVC_GUID_UVC_PROCESSING
,
108 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
111 .flags
= UVC_CTRL_FLAG_SET_CUR
112 | UVC_CTRL_FLAG_GET_RANGE
113 | UVC_CTRL_FLAG_RESTORE
114 | UVC_CTRL_FLAG_AUTO_UPDATE
,
117 .entity
= UVC_GUID_UVC_PROCESSING
,
118 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
121 .flags
= UVC_CTRL_FLAG_SET_CUR
122 | UVC_CTRL_FLAG_GET_RANGE
123 | UVC_CTRL_FLAG_RESTORE
,
126 .entity
= UVC_GUID_UVC_PROCESSING
,
127 .selector
= UVC_PU_GAIN_CONTROL
,
130 .flags
= UVC_CTRL_FLAG_SET_CUR
131 | UVC_CTRL_FLAG_GET_RANGE
132 | UVC_CTRL_FLAG_RESTORE
,
135 .entity
= UVC_GUID_UVC_PROCESSING
,
136 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
139 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
140 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
143 .entity
= UVC_GUID_UVC_PROCESSING
,
144 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
147 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
148 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
151 .entity
= UVC_GUID_UVC_PROCESSING
,
152 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
155 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
156 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
159 .entity
= UVC_GUID_UVC_PROCESSING
,
160 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
163 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
164 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
167 .entity
= UVC_GUID_UVC_PROCESSING
,
168 .selector
= UVC_PU_DIGITAL_MULTIPLIER_CONTROL
,
171 .flags
= UVC_CTRL_FLAG_SET_CUR
172 | UVC_CTRL_FLAG_GET_RANGE
173 | UVC_CTRL_FLAG_RESTORE
,
176 .entity
= UVC_GUID_UVC_PROCESSING
,
177 .selector
= UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL
,
180 .flags
= UVC_CTRL_FLAG_SET_CUR
181 | UVC_CTRL_FLAG_GET_RANGE
182 | UVC_CTRL_FLAG_RESTORE
,
185 .entity
= UVC_GUID_UVC_PROCESSING
,
186 .selector
= UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL
,
189 .flags
= UVC_CTRL_FLAG_GET_CUR
,
192 .entity
= UVC_GUID_UVC_PROCESSING
,
193 .selector
= UVC_PU_ANALOG_LOCK_STATUS_CONTROL
,
196 .flags
= UVC_CTRL_FLAG_GET_CUR
,
199 .entity
= UVC_GUID_UVC_CAMERA
,
200 .selector
= UVC_CT_SCANNING_MODE_CONTROL
,
203 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
204 | UVC_CTRL_FLAG_RESTORE
,
207 .entity
= UVC_GUID_UVC_CAMERA
,
208 .selector
= UVC_CT_AE_MODE_CONTROL
,
211 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
212 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_GET_RES
213 | UVC_CTRL_FLAG_RESTORE
,
216 .entity
= UVC_GUID_UVC_CAMERA
,
217 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
220 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
221 | UVC_CTRL_FLAG_RESTORE
,
224 .entity
= UVC_GUID_UVC_CAMERA
,
225 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
228 .flags
= UVC_CTRL_FLAG_SET_CUR
229 | UVC_CTRL_FLAG_GET_RANGE
230 | UVC_CTRL_FLAG_RESTORE
,
233 .entity
= UVC_GUID_UVC_CAMERA
,
234 .selector
= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL
,
237 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_RESTORE
,
240 .entity
= UVC_GUID_UVC_CAMERA
,
241 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
244 .flags
= UVC_CTRL_FLAG_SET_CUR
245 | UVC_CTRL_FLAG_GET_RANGE
246 | UVC_CTRL_FLAG_RESTORE
247 | UVC_CTRL_FLAG_AUTO_UPDATE
,
250 .entity
= UVC_GUID_UVC_CAMERA
,
251 .selector
= UVC_CT_FOCUS_RELATIVE_CONTROL
,
254 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_MIN
255 | UVC_CTRL_FLAG_GET_MAX
| UVC_CTRL_FLAG_GET_RES
256 | UVC_CTRL_FLAG_GET_DEF
257 | UVC_CTRL_FLAG_AUTO_UPDATE
,
260 .entity
= UVC_GUID_UVC_CAMERA
,
261 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
264 .flags
= UVC_CTRL_FLAG_SET_CUR
265 | UVC_CTRL_FLAG_GET_RANGE
266 | UVC_CTRL_FLAG_RESTORE
267 | UVC_CTRL_FLAG_AUTO_UPDATE
,
270 .entity
= UVC_GUID_UVC_CAMERA
,
271 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
274 .flags
= UVC_CTRL_FLAG_SET_CUR
275 | UVC_CTRL_FLAG_AUTO_UPDATE
,
278 .entity
= UVC_GUID_UVC_CAMERA
,
279 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
282 .flags
= UVC_CTRL_FLAG_SET_CUR
283 | UVC_CTRL_FLAG_GET_RANGE
284 | UVC_CTRL_FLAG_RESTORE
285 | UVC_CTRL_FLAG_AUTO_UPDATE
,
288 .entity
= UVC_GUID_UVC_CAMERA
,
289 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
292 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_MIN
293 | UVC_CTRL_FLAG_GET_MAX
| UVC_CTRL_FLAG_GET_RES
294 | UVC_CTRL_FLAG_GET_DEF
295 | UVC_CTRL_FLAG_AUTO_UPDATE
,
298 .entity
= UVC_GUID_UVC_CAMERA
,
299 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
302 .flags
= UVC_CTRL_FLAG_SET_CUR
303 | UVC_CTRL_FLAG_GET_RANGE
304 | UVC_CTRL_FLAG_RESTORE
305 | UVC_CTRL_FLAG_AUTO_UPDATE
,
308 .entity
= UVC_GUID_UVC_CAMERA
,
309 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
312 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_MIN
313 | UVC_CTRL_FLAG_GET_MAX
| UVC_CTRL_FLAG_GET_RES
314 | UVC_CTRL_FLAG_GET_DEF
315 | UVC_CTRL_FLAG_AUTO_UPDATE
,
318 .entity
= UVC_GUID_UVC_CAMERA
,
319 .selector
= UVC_CT_ROLL_ABSOLUTE_CONTROL
,
322 .flags
= UVC_CTRL_FLAG_SET_CUR
323 | UVC_CTRL_FLAG_GET_RANGE
324 | UVC_CTRL_FLAG_RESTORE
325 | UVC_CTRL_FLAG_AUTO_UPDATE
,
328 .entity
= UVC_GUID_UVC_CAMERA
,
329 .selector
= UVC_CT_ROLL_RELATIVE_CONTROL
,
332 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_MIN
333 | UVC_CTRL_FLAG_GET_MAX
| UVC_CTRL_FLAG_GET_RES
334 | UVC_CTRL_FLAG_GET_DEF
335 | UVC_CTRL_FLAG_AUTO_UPDATE
,
338 .entity
= UVC_GUID_UVC_CAMERA
,
339 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
342 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
343 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
346 .entity
= UVC_GUID_UVC_CAMERA
,
347 .selector
= UVC_CT_PRIVACY_CONTROL
,
350 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
351 | UVC_CTRL_FLAG_RESTORE
352 | UVC_CTRL_FLAG_AUTO_UPDATE
,
356 static struct uvc_menu_info power_line_frequency_controls
[] = {
362 static struct uvc_menu_info exposure_auto_controls
[] = {
364 { 1, "Manual Mode" },
365 { 4, "Shutter Priority Mode" },
366 { 8, "Aperture Priority Mode" },
369 static __s32
uvc_ctrl_get_zoom(struct uvc_control_mapping
*mapping
,
370 __u8 query
, const __u8
*data
)
372 __s8 zoom
= (__s8
)data
[0];
376 return (zoom
== 0) ? 0 : (zoom
> 0 ? data
[2] : -data
[2]);
387 static void uvc_ctrl_set_zoom(struct uvc_control_mapping
*mapping
,
388 __s32 value
, __u8
*data
)
390 data
[0] = value
== 0 ? 0 : (value
> 0) ? 1 : 0xff;
391 data
[2] = min((int)abs(value
), 0xff);
394 static struct uvc_control_mapping uvc_ctrl_mappings
[] = {
396 .id
= V4L2_CID_BRIGHTNESS
,
397 .name
= "Brightness",
398 .entity
= UVC_GUID_UVC_PROCESSING
,
399 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
402 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
403 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
406 .id
= V4L2_CID_CONTRAST
,
408 .entity
= UVC_GUID_UVC_PROCESSING
,
409 .selector
= UVC_PU_CONTRAST_CONTROL
,
412 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
413 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
418 .entity
= UVC_GUID_UVC_PROCESSING
,
419 .selector
= UVC_PU_HUE_CONTROL
,
422 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
423 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
424 .master_id
= V4L2_CID_HUE_AUTO
,
428 .id
= V4L2_CID_SATURATION
,
429 .name
= "Saturation",
430 .entity
= UVC_GUID_UVC_PROCESSING
,
431 .selector
= UVC_PU_SATURATION_CONTROL
,
434 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
435 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
438 .id
= V4L2_CID_SHARPNESS
,
440 .entity
= UVC_GUID_UVC_PROCESSING
,
441 .selector
= UVC_PU_SHARPNESS_CONTROL
,
444 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
445 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
448 .id
= V4L2_CID_GAMMA
,
450 .entity
= UVC_GUID_UVC_PROCESSING
,
451 .selector
= UVC_PU_GAMMA_CONTROL
,
454 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
455 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
458 .id
= V4L2_CID_BACKLIGHT_COMPENSATION
,
459 .name
= "Backlight Compensation",
460 .entity
= UVC_GUID_UVC_PROCESSING
,
461 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
464 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
465 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
470 .entity
= UVC_GUID_UVC_PROCESSING
,
471 .selector
= UVC_PU_GAIN_CONTROL
,
474 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
475 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
478 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
479 .name
= "Power Line Frequency",
480 .entity
= UVC_GUID_UVC_PROCESSING
,
481 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
484 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
485 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
486 .menu_info
= power_line_frequency_controls
,
487 .menu_count
= ARRAY_SIZE(power_line_frequency_controls
),
490 .id
= V4L2_CID_HUE_AUTO
,
492 .entity
= UVC_GUID_UVC_PROCESSING
,
493 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
496 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
497 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
498 .slave_ids
= { V4L2_CID_HUE
, },
501 .id
= V4L2_CID_EXPOSURE_AUTO
,
502 .name
= "Exposure, Auto",
503 .entity
= UVC_GUID_UVC_CAMERA
,
504 .selector
= UVC_CT_AE_MODE_CONTROL
,
507 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
508 .data_type
= UVC_CTRL_DATA_TYPE_BITMASK
,
509 .menu_info
= exposure_auto_controls
,
510 .menu_count
= ARRAY_SIZE(exposure_auto_controls
),
511 .slave_ids
= { V4L2_CID_EXPOSURE_ABSOLUTE
, },
514 .id
= V4L2_CID_EXPOSURE_AUTO_PRIORITY
,
515 .name
= "Exposure, Auto Priority",
516 .entity
= UVC_GUID_UVC_CAMERA
,
517 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
520 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
521 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
524 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
525 .name
= "Exposure (Absolute)",
526 .entity
= UVC_GUID_UVC_CAMERA
,
527 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
530 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
531 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
532 .master_id
= V4L2_CID_EXPOSURE_AUTO
,
533 .master_manual
= V4L2_EXPOSURE_MANUAL
,
536 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
537 .name
= "White Balance Temperature, Auto",
538 .entity
= UVC_GUID_UVC_PROCESSING
,
539 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
542 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
543 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
544 .slave_ids
= { V4L2_CID_WHITE_BALANCE_TEMPERATURE
, },
547 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
548 .name
= "White Balance Temperature",
549 .entity
= UVC_GUID_UVC_PROCESSING
,
550 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
553 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
554 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
555 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
559 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
560 .name
= "White Balance Component, Auto",
561 .entity
= UVC_GUID_UVC_PROCESSING
,
562 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
565 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
566 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
567 .slave_ids
= { V4L2_CID_BLUE_BALANCE
,
568 V4L2_CID_RED_BALANCE
},
571 .id
= V4L2_CID_BLUE_BALANCE
,
572 .name
= "White Balance Blue Component",
573 .entity
= UVC_GUID_UVC_PROCESSING
,
574 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
577 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
578 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
579 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
583 .id
= V4L2_CID_RED_BALANCE
,
584 .name
= "White Balance Red Component",
585 .entity
= UVC_GUID_UVC_PROCESSING
,
586 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
589 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
590 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
591 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
595 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
596 .name
= "Focus (absolute)",
597 .entity
= UVC_GUID_UVC_CAMERA
,
598 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
601 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
602 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
603 .master_id
= V4L2_CID_FOCUS_AUTO
,
607 .id
= V4L2_CID_FOCUS_AUTO
,
608 .name
= "Focus, Auto",
609 .entity
= UVC_GUID_UVC_CAMERA
,
610 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
613 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
614 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
615 .slave_ids
= { V4L2_CID_FOCUS_ABSOLUTE
, },
618 .id
= V4L2_CID_IRIS_ABSOLUTE
,
619 .name
= "Iris, Absolute",
620 .entity
= UVC_GUID_UVC_CAMERA
,
621 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
624 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
625 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
628 .id
= V4L2_CID_IRIS_RELATIVE
,
629 .name
= "Iris, Relative",
630 .entity
= UVC_GUID_UVC_CAMERA
,
631 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
634 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
635 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
638 .id
= V4L2_CID_ZOOM_ABSOLUTE
,
639 .name
= "Zoom, Absolute",
640 .entity
= UVC_GUID_UVC_CAMERA
,
641 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
644 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
645 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
648 .id
= V4L2_CID_ZOOM_CONTINUOUS
,
649 .name
= "Zoom, Continuous",
650 .entity
= UVC_GUID_UVC_CAMERA
,
651 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
654 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
655 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
656 .get
= uvc_ctrl_get_zoom
,
657 .set
= uvc_ctrl_set_zoom
,
660 .id
= V4L2_CID_PAN_ABSOLUTE
,
661 .name
= "Pan (Absolute)",
662 .entity
= UVC_GUID_UVC_CAMERA
,
663 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
666 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
667 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
670 .id
= V4L2_CID_TILT_ABSOLUTE
,
671 .name
= "Tilt (Absolute)",
672 .entity
= UVC_GUID_UVC_CAMERA
,
673 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
676 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
677 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
680 .id
= V4L2_CID_PRIVACY
,
682 .entity
= UVC_GUID_UVC_CAMERA
,
683 .selector
= UVC_CT_PRIVACY_CONTROL
,
686 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
687 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
691 /* ------------------------------------------------------------------------
695 static inline __u8
*uvc_ctrl_data(struct uvc_control
*ctrl
, int id
)
697 return ctrl
->uvc_data
+ id
* ctrl
->info
.size
;
700 static inline int uvc_test_bit(const __u8
*data
, int bit
)
702 return (data
[bit
>> 3] >> (bit
& 7)) & 1;
705 static inline void uvc_clear_bit(__u8
*data
, int bit
)
707 data
[bit
>> 3] &= ~(1 << (bit
& 7));
710 /* Extract the bit string specified by mapping->offset and mapping->size
711 * from the little-endian data stored at 'data' and return the result as
712 * a signed 32bit integer. Sign extension will be performed if the mapping
713 * references a signed data type.
715 static __s32
uvc_get_le_value(struct uvc_control_mapping
*mapping
,
716 __u8 query
, const __u8
*data
)
718 int bits
= mapping
->size
;
719 int offset
= mapping
->offset
;
725 mask
= ((1LL << bits
) - 1) << offset
;
727 for (; bits
> 0; data
++) {
728 __u8 byte
= *data
& mask
;
729 value
|= offset
> 0 ? (byte
>> offset
) : (byte
<< (-offset
));
730 bits
-= 8 - (offset
> 0 ? offset
: 0);
732 mask
= (1 << bits
) - 1;
735 /* Sign-extend the value if needed. */
736 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
737 value
|= -(value
& (1 << (mapping
->size
- 1)));
742 /* Set the bit string specified by mapping->offset and mapping->size
743 * in the little-endian data stored at 'data' to the value 'value'.
745 static void uvc_set_le_value(struct uvc_control_mapping
*mapping
,
746 __s32 value
, __u8
*data
)
748 int bits
= mapping
->size
;
749 int offset
= mapping
->offset
;
752 /* According to the v4l2 spec, writing any value to a button control
753 * should result in the action belonging to the button control being
754 * triggered. UVC devices however want to see a 1 written -> override
757 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_BUTTON
)
763 for (; bits
> 0; data
++) {
764 mask
= ((1LL << bits
) - 1) << offset
;
765 *data
= (*data
& ~mask
) | ((value
<< offset
) & mask
);
766 value
>>= offset
? offset
: 8;
772 /* ------------------------------------------------------------------------
773 * Terminal and unit management
776 static const __u8 uvc_processing_guid
[16] = UVC_GUID_UVC_PROCESSING
;
777 static const __u8 uvc_camera_guid
[16] = UVC_GUID_UVC_CAMERA
;
778 static const __u8 uvc_media_transport_input_guid
[16] =
779 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT
;
781 static int uvc_entity_match_guid(const struct uvc_entity
*entity
,
784 switch (UVC_ENTITY_TYPE(entity
)) {
786 return memcmp(uvc_camera_guid
, guid
, 16) == 0;
788 case UVC_ITT_MEDIA_TRANSPORT_INPUT
:
789 return memcmp(uvc_media_transport_input_guid
, guid
, 16) == 0;
791 case UVC_VC_PROCESSING_UNIT
:
792 return memcmp(uvc_processing_guid
, guid
, 16) == 0;
794 case UVC_VC_EXTENSION_UNIT
:
795 return memcmp(entity
->extension
.guidExtensionCode
,
803 /* ------------------------------------------------------------------------
807 static void __uvc_find_control(struct uvc_entity
*entity
, __u32 v4l2_id
,
808 struct uvc_control_mapping
**mapping
, struct uvc_control
**control
,
811 struct uvc_control
*ctrl
;
812 struct uvc_control_mapping
*map
;
818 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
819 ctrl
= &entity
->controls
[i
];
820 if (!ctrl
->initialized
)
823 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
824 if ((map
->id
== v4l2_id
) && !next
) {
830 if ((*mapping
== NULL
|| (*mapping
)->id
> map
->id
) &&
831 (map
->id
> v4l2_id
) && next
) {
839 static struct uvc_control
*uvc_find_control(struct uvc_video_chain
*chain
,
840 __u32 v4l2_id
, struct uvc_control_mapping
**mapping
)
842 struct uvc_control
*ctrl
= NULL
;
843 struct uvc_entity
*entity
;
844 int next
= v4l2_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
848 /* Mask the query flags. */
849 v4l2_id
&= V4L2_CTRL_ID_MASK
;
851 /* Find the control. */
852 list_for_each_entry(entity
, &chain
->entities
, chain
) {
853 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
858 if (ctrl
== NULL
&& !next
)
859 uvc_trace(UVC_TRACE_CONTROL
, "Control 0x%08x not found.\n",
865 static int uvc_ctrl_populate_cache(struct uvc_video_chain
*chain
,
866 struct uvc_control
*ctrl
)
870 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_DEF
) {
871 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_DEF
, ctrl
->entity
->id
,
872 chain
->dev
->intfnum
, ctrl
->info
.selector
,
873 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
),
879 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MIN
) {
880 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MIN
, ctrl
->entity
->id
,
881 chain
->dev
->intfnum
, ctrl
->info
.selector
,
882 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
),
887 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MAX
) {
888 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MAX
, ctrl
->entity
->id
,
889 chain
->dev
->intfnum
, ctrl
->info
.selector
,
890 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
),
895 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
) {
896 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_RES
, ctrl
->entity
->id
,
897 chain
->dev
->intfnum
, ctrl
->info
.selector
,
898 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
),
901 if (UVC_ENTITY_TYPE(ctrl
->entity
) !=
902 UVC_VC_EXTENSION_UNIT
)
905 /* GET_RES is mandatory for XU controls, but some
906 * cameras still choke on it. Ignore errors and set the
907 * resolution value to zero.
909 uvc_warn_once(chain
->dev
, UVC_WARN_XU_GET_RES
,
910 "UVC non compliance - GET_RES failed on "
911 "an XU control. Enabling workaround.\n");
912 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
), 0,
921 static int __uvc_ctrl_get(struct uvc_video_chain
*chain
,
922 struct uvc_control
*ctrl
, struct uvc_control_mapping
*mapping
,
925 struct uvc_menu_info
*menu
;
929 if ((ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
) == 0)
933 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
, ctrl
->entity
->id
,
934 chain
->dev
->intfnum
, ctrl
->info
.selector
,
935 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
943 *value
= mapping
->get(mapping
, UVC_GET_CUR
,
944 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
946 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
947 menu
= mapping
->menu_info
;
948 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
949 if (menu
->value
== *value
) {
959 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
960 struct uvc_control
*ctrl
,
961 struct uvc_control_mapping
*mapping
,
962 struct v4l2_queryctrl
*v4l2_ctrl
)
964 struct uvc_control_mapping
*master_map
= NULL
;
965 struct uvc_control
*master_ctrl
= NULL
;
966 struct uvc_menu_info
*menu
;
969 memset(v4l2_ctrl
, 0, sizeof *v4l2_ctrl
);
970 v4l2_ctrl
->id
= mapping
->id
;
971 v4l2_ctrl
->type
= mapping
->v4l2_type
;
972 strlcpy(v4l2_ctrl
->name
, mapping
->name
, sizeof v4l2_ctrl
->name
);
973 v4l2_ctrl
->flags
= 0;
975 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
))
976 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_WRITE_ONLY
;
977 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_SET_CUR
))
978 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
980 if (mapping
->master_id
)
981 __uvc_find_control(ctrl
->entity
, mapping
->master_id
,
982 &master_map
, &master_ctrl
, 0);
983 if (master_ctrl
&& (master_ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
)) {
985 int ret
= __uvc_ctrl_get(chain
, master_ctrl
, master_map
, &val
);
989 if (val
!= mapping
->master_manual
)
990 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
994 int ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
999 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_DEF
) {
1000 v4l2_ctrl
->default_value
= mapping
->get(mapping
, UVC_GET_DEF
,
1001 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
));
1004 switch (mapping
->v4l2_type
) {
1005 case V4L2_CTRL_TYPE_MENU
:
1006 v4l2_ctrl
->minimum
= 0;
1007 v4l2_ctrl
->maximum
= mapping
->menu_count
- 1;
1008 v4l2_ctrl
->step
= 1;
1010 menu
= mapping
->menu_info
;
1011 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
1012 if (menu
->value
== v4l2_ctrl
->default_value
) {
1013 v4l2_ctrl
->default_value
= i
;
1020 case V4L2_CTRL_TYPE_BOOLEAN
:
1021 v4l2_ctrl
->minimum
= 0;
1022 v4l2_ctrl
->maximum
= 1;
1023 v4l2_ctrl
->step
= 1;
1026 case V4L2_CTRL_TYPE_BUTTON
:
1027 v4l2_ctrl
->minimum
= 0;
1028 v4l2_ctrl
->maximum
= 0;
1029 v4l2_ctrl
->step
= 0;
1036 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MIN
)
1037 v4l2_ctrl
->minimum
= mapping
->get(mapping
, UVC_GET_MIN
,
1038 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1040 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MAX
)
1041 v4l2_ctrl
->maximum
= mapping
->get(mapping
, UVC_GET_MAX
,
1042 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1044 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)
1045 v4l2_ctrl
->step
= mapping
->get(mapping
, UVC_GET_RES
,
1046 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1051 int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
1052 struct v4l2_queryctrl
*v4l2_ctrl
)
1054 struct uvc_control
*ctrl
;
1055 struct uvc_control_mapping
*mapping
;
1058 ret
= mutex_lock_interruptible(&chain
->ctrl_mutex
);
1060 return -ERESTARTSYS
;
1062 ctrl
= uvc_find_control(chain
, v4l2_ctrl
->id
, &mapping
);
1068 ret
= __uvc_query_v4l2_ctrl(chain
, ctrl
, mapping
, v4l2_ctrl
);
1070 mutex_unlock(&chain
->ctrl_mutex
);
1075 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
1076 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1077 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1078 * Balance V4L2 controls use the White Balance Component UVC control) or
1079 * otherwise translated. The approach we take here is to use a translation
1080 * table for the controls that can be mapped directly, and handle the others
1083 int uvc_query_v4l2_menu(struct uvc_video_chain
*chain
,
1084 struct v4l2_querymenu
*query_menu
)
1086 struct uvc_menu_info
*menu_info
;
1087 struct uvc_control_mapping
*mapping
;
1088 struct uvc_control
*ctrl
;
1089 u32 index
= query_menu
->index
;
1090 u32 id
= query_menu
->id
;
1093 memset(query_menu
, 0, sizeof(*query_menu
));
1094 query_menu
->id
= id
;
1095 query_menu
->index
= index
;
1097 ret
= mutex_lock_interruptible(&chain
->ctrl_mutex
);
1099 return -ERESTARTSYS
;
1101 ctrl
= uvc_find_control(chain
, query_menu
->id
, &mapping
);
1102 if (ctrl
== NULL
|| mapping
->v4l2_type
!= V4L2_CTRL_TYPE_MENU
) {
1107 if (query_menu
->index
>= mapping
->menu_count
) {
1112 menu_info
= &mapping
->menu_info
[query_menu
->index
];
1114 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_BITMASK
&&
1115 (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)) {
1118 if (!ctrl
->cached
) {
1119 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1124 bitmap
= mapping
->get(mapping
, UVC_GET_RES
,
1125 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1126 if (!(bitmap
& menu_info
->value
)) {
1132 strlcpy(query_menu
->name
, menu_info
->name
, sizeof query_menu
->name
);
1135 mutex_unlock(&chain
->ctrl_mutex
);
1139 /* --------------------------------------------------------------------------
1140 * Ctrl event handling
1143 static void uvc_ctrl_fill_event(struct uvc_video_chain
*chain
,
1144 struct v4l2_event
*ev
,
1145 struct uvc_control
*ctrl
,
1146 struct uvc_control_mapping
*mapping
,
1147 s32 value
, u32 changes
)
1149 struct v4l2_queryctrl v4l2_ctrl
;
1151 __uvc_query_v4l2_ctrl(chain
, ctrl
, mapping
, &v4l2_ctrl
);
1153 memset(ev
->reserved
, 0, sizeof(ev
->reserved
));
1154 ev
->type
= V4L2_EVENT_CTRL
;
1155 ev
->id
= v4l2_ctrl
.id
;
1156 ev
->u
.ctrl
.value
= value
;
1157 ev
->u
.ctrl
.changes
= changes
;
1158 ev
->u
.ctrl
.type
= v4l2_ctrl
.type
;
1159 ev
->u
.ctrl
.flags
= v4l2_ctrl
.flags
;
1160 ev
->u
.ctrl
.minimum
= v4l2_ctrl
.minimum
;
1161 ev
->u
.ctrl
.maximum
= v4l2_ctrl
.maximum
;
1162 ev
->u
.ctrl
.step
= v4l2_ctrl
.step
;
1163 ev
->u
.ctrl
.default_value
= v4l2_ctrl
.default_value
;
1166 static void uvc_ctrl_send_event(struct uvc_fh
*handle
,
1167 struct uvc_control
*ctrl
, struct uvc_control_mapping
*mapping
,
1168 s32 value
, u32 changes
)
1170 struct v4l2_subscribed_event
*sev
;
1171 struct v4l2_event ev
;
1173 if (list_empty(&mapping
->ev_subs
))
1176 uvc_ctrl_fill_event(handle
->chain
, &ev
, ctrl
, mapping
, value
, changes
);
1178 list_for_each_entry(sev
, &mapping
->ev_subs
, node
) {
1179 if (sev
->fh
&& (sev
->fh
!= &handle
->vfh
||
1180 (sev
->flags
& V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK
) ||
1181 (changes
& V4L2_EVENT_CTRL_CH_FLAGS
)))
1182 v4l2_event_queue_fh(sev
->fh
, &ev
);
1186 static void uvc_ctrl_send_slave_event(struct uvc_fh
*handle
,
1187 struct uvc_control
*master
, u32 slave_id
,
1188 const struct v4l2_ext_control
*xctrls
, unsigned int xctrls_count
)
1190 struct uvc_control_mapping
*mapping
= NULL
;
1191 struct uvc_control
*ctrl
= NULL
;
1192 u32 changes
= V4L2_EVENT_CTRL_CH_FLAGS
;
1197 * We can skip sending an event for the slave if the slave
1198 * is being modified in the same transaction.
1200 for (i
= 0; i
< xctrls_count
; i
++) {
1201 if (xctrls
[i
].id
== slave_id
)
1205 __uvc_find_control(master
->entity
, slave_id
, &mapping
, &ctrl
, 0);
1209 if (__uvc_ctrl_get(handle
->chain
, ctrl
, mapping
, &val
) == 0)
1210 changes
|= V4L2_EVENT_CTRL_CH_VALUE
;
1212 uvc_ctrl_send_event(handle
, ctrl
, mapping
, val
, changes
);
1215 static void uvc_ctrl_send_events(struct uvc_fh
*handle
,
1216 const struct v4l2_ext_control
*xctrls
, unsigned int xctrls_count
)
1218 struct uvc_control_mapping
*mapping
;
1219 struct uvc_control
*ctrl
;
1220 u32 changes
= V4L2_EVENT_CTRL_CH_VALUE
;
1224 for (i
= 0; i
< xctrls_count
; ++i
) {
1225 ctrl
= uvc_find_control(handle
->chain
, xctrls
[i
].id
, &mapping
);
1227 for (j
= 0; j
< ARRAY_SIZE(mapping
->slave_ids
); ++j
) {
1228 if (!mapping
->slave_ids
[j
])
1230 uvc_ctrl_send_slave_event(handle
, ctrl
,
1231 mapping
->slave_ids
[j
],
1232 xctrls
, xctrls_count
);
1236 * If the master is being modified in the same transaction
1237 * flags may change too.
1239 if (mapping
->master_id
) {
1240 for (j
= 0; j
< xctrls_count
; j
++) {
1241 if (xctrls
[j
].id
== mapping
->master_id
) {
1242 changes
|= V4L2_EVENT_CTRL_CH_FLAGS
;
1248 uvc_ctrl_send_event(handle
, ctrl
, mapping
, xctrls
[i
].value
,
1253 static int uvc_ctrl_add_event(struct v4l2_subscribed_event
*sev
, unsigned elems
)
1255 struct uvc_fh
*handle
= container_of(sev
->fh
, struct uvc_fh
, vfh
);
1256 struct uvc_control_mapping
*mapping
;
1257 struct uvc_control
*ctrl
;
1260 ret
= mutex_lock_interruptible(&handle
->chain
->ctrl_mutex
);
1262 return -ERESTARTSYS
;
1264 ctrl
= uvc_find_control(handle
->chain
, sev
->id
, &mapping
);
1270 list_add_tail(&sev
->node
, &mapping
->ev_subs
);
1271 if (sev
->flags
& V4L2_EVENT_SUB_FL_SEND_INITIAL
) {
1272 struct v4l2_event ev
;
1273 u32 changes
= V4L2_EVENT_CTRL_CH_FLAGS
;
1276 if (__uvc_ctrl_get(handle
->chain
, ctrl
, mapping
, &val
) == 0)
1277 changes
|= V4L2_EVENT_CTRL_CH_VALUE
;
1279 uvc_ctrl_fill_event(handle
->chain
, &ev
, ctrl
, mapping
, val
,
1281 /* Mark the queue as active, allowing this initial
1282 event to be accepted. */
1284 v4l2_event_queue_fh(sev
->fh
, &ev
);
1288 mutex_unlock(&handle
->chain
->ctrl_mutex
);
1292 static void uvc_ctrl_del_event(struct v4l2_subscribed_event
*sev
)
1294 struct uvc_fh
*handle
= container_of(sev
->fh
, struct uvc_fh
, vfh
);
1296 mutex_lock(&handle
->chain
->ctrl_mutex
);
1297 list_del(&sev
->node
);
1298 mutex_unlock(&handle
->chain
->ctrl_mutex
);
1301 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops
= {
1302 .add
= uvc_ctrl_add_event
,
1303 .del
= uvc_ctrl_del_event
,
1304 .replace
= v4l2_ctrl_replace
,
1305 .merge
= v4l2_ctrl_merge
,
1308 /* --------------------------------------------------------------------------
1309 * Control transactions
1311 * To make extended set operations as atomic as the hardware allows, controls
1312 * are handled using begin/commit/rollback operations.
1314 * At the beginning of a set request, uvc_ctrl_begin should be called to
1315 * initialize the request. This function acquires the control lock.
1317 * When setting a control, the new value is stored in the control data field
1318 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1319 * later processing. If the UVC and V4L2 control sizes differ, the current
1320 * value is loaded from the hardware before storing the new value in the data
1323 * After processing all controls in the transaction, uvc_ctrl_commit or
1324 * uvc_ctrl_rollback must be called to apply the pending changes to the
1325 * hardware or revert them. When applying changes, all controls marked as
1326 * dirty will be modified in the UVC device, and the dirty flag will be
1327 * cleared. When reverting controls, the control data field
1328 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1329 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1332 int uvc_ctrl_begin(struct uvc_video_chain
*chain
)
1334 return mutex_lock_interruptible(&chain
->ctrl_mutex
) ? -ERESTARTSYS
: 0;
1337 static int uvc_ctrl_commit_entity(struct uvc_device
*dev
,
1338 struct uvc_entity
*entity
, int rollback
)
1340 struct uvc_control
*ctrl
;
1347 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1348 ctrl
= &entity
->controls
[i
];
1349 if (!ctrl
->initialized
)
1352 /* Reset the loaded flag for auto-update controls that were
1353 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1354 * uvc_ctrl_get from using the cached value, and for write-only
1355 * controls to prevent uvc_ctrl_set from setting bits not
1356 * explicitly set by the user.
1358 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_AUTO_UPDATE
||
1359 !(ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
))
1366 ret
= uvc_query_ctrl(dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
1367 dev
->intfnum
, ctrl
->info
.selector
,
1368 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1373 if (rollback
|| ret
< 0)
1374 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1375 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1387 int __uvc_ctrl_commit(struct uvc_fh
*handle
, int rollback
,
1388 const struct v4l2_ext_control
*xctrls
,
1389 unsigned int xctrls_count
)
1391 struct uvc_video_chain
*chain
= handle
->chain
;
1392 struct uvc_entity
*entity
;
1395 /* Find the control. */
1396 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1397 ret
= uvc_ctrl_commit_entity(chain
->dev
, entity
, rollback
);
1403 uvc_ctrl_send_events(handle
, xctrls
, xctrls_count
);
1405 mutex_unlock(&chain
->ctrl_mutex
);
1409 int uvc_ctrl_get(struct uvc_video_chain
*chain
,
1410 struct v4l2_ext_control
*xctrl
)
1412 struct uvc_control
*ctrl
;
1413 struct uvc_control_mapping
*mapping
;
1415 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1419 return __uvc_ctrl_get(chain
, ctrl
, mapping
, &xctrl
->value
);
1422 int uvc_ctrl_set(struct uvc_video_chain
*chain
,
1423 struct v4l2_ext_control
*xctrl
)
1425 struct uvc_control
*ctrl
;
1426 struct uvc_control_mapping
*mapping
;
1433 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1434 if (ctrl
== NULL
|| (ctrl
->info
.flags
& UVC_CTRL_FLAG_SET_CUR
) == 0)
1437 /* Clamp out of range values. */
1438 switch (mapping
->v4l2_type
) {
1439 case V4L2_CTRL_TYPE_INTEGER
:
1440 if (!ctrl
->cached
) {
1441 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1446 min
= mapping
->get(mapping
, UVC_GET_MIN
,
1447 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1448 max
= mapping
->get(mapping
, UVC_GET_MAX
,
1449 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1450 step
= mapping
->get(mapping
, UVC_GET_RES
,
1451 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1455 xctrl
->value
= min
+ ((u32
)(xctrl
->value
- min
) + step
/ 2)
1457 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
1458 xctrl
->value
= clamp(xctrl
->value
, min
, max
);
1460 xctrl
->value
= clamp_t(u32
, xctrl
->value
, min
, max
);
1461 value
= xctrl
->value
;
1464 case V4L2_CTRL_TYPE_BOOLEAN
:
1465 xctrl
->value
= clamp(xctrl
->value
, 0, 1);
1466 value
= xctrl
->value
;
1469 case V4L2_CTRL_TYPE_MENU
:
1470 if (xctrl
->value
< 0 || xctrl
->value
>= mapping
->menu_count
)
1472 value
= mapping
->menu_info
[xctrl
->value
].value
;
1474 /* Valid menu indices are reported by the GET_RES request for
1475 * UVC controls that support it.
1477 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_BITMASK
&&
1478 (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)) {
1479 if (!ctrl
->cached
) {
1480 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1485 step
= mapping
->get(mapping
, UVC_GET_RES
,
1486 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1487 if (!(step
& value
))
1494 value
= xctrl
->value
;
1498 /* If the mapping doesn't span the whole UVC control, the current value
1499 * needs to be loaded from the device to perform the read-modify-write
1502 if (!ctrl
->loaded
&& (ctrl
->info
.size
* 8) != mapping
->size
) {
1503 if ((ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
) == 0) {
1504 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1505 0, ctrl
->info
.size
);
1507 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
1508 ctrl
->entity
->id
, chain
->dev
->intfnum
,
1509 ctrl
->info
.selector
,
1510 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1519 /* Backup the current value in case we need to rollback later. */
1521 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1522 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1526 mapping
->set(mapping
, value
,
1527 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1534 /* --------------------------------------------------------------------------
1538 static void uvc_ctrl_fixup_xu_info(struct uvc_device
*dev
,
1539 const struct uvc_control
*ctrl
, struct uvc_control_info
*info
)
1541 struct uvc_ctrl_fixup
{
1542 struct usb_device_id id
;
1548 static const struct uvc_ctrl_fixup fixups
[] = {
1549 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1550 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
1551 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
1552 UVC_CTRL_FLAG_AUTO_UPDATE
},
1553 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1554 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
1555 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
1556 UVC_CTRL_FLAG_AUTO_UPDATE
},
1557 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1558 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
1559 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
1560 UVC_CTRL_FLAG_AUTO_UPDATE
},
1565 for (i
= 0; i
< ARRAY_SIZE(fixups
); ++i
) {
1566 if (!usb_match_one_id(dev
->intf
, &fixups
[i
].id
))
1569 if (fixups
[i
].entity
== ctrl
->entity
->id
&&
1570 fixups
[i
].selector
== info
->selector
) {
1571 info
->flags
= fixups
[i
].flags
;
1578 * Query control information (size and flags) for XU controls.
1580 static int uvc_ctrl_fill_xu_info(struct uvc_device
*dev
,
1581 const struct uvc_control
*ctrl
, struct uvc_control_info
*info
)
1586 data
= kmalloc(2, GFP_KERNEL
);
1590 memcpy(info
->entity
, ctrl
->entity
->extension
.guidExtensionCode
,
1591 sizeof(info
->entity
));
1592 info
->index
= ctrl
->index
;
1593 info
->selector
= ctrl
->index
+ 1;
1595 /* Query and verify the control length (GET_LEN) */
1596 ret
= uvc_query_ctrl(dev
, UVC_GET_LEN
, ctrl
->entity
->id
, dev
->intfnum
,
1597 info
->selector
, data
, 2);
1599 uvc_trace(UVC_TRACE_CONTROL
,
1600 "GET_LEN failed on control %pUl/%u (%d).\n",
1601 info
->entity
, info
->selector
, ret
);
1605 info
->size
= le16_to_cpup((__le16
*)data
);
1607 /* Query the control information (GET_INFO) */
1608 ret
= uvc_query_ctrl(dev
, UVC_GET_INFO
, ctrl
->entity
->id
, dev
->intfnum
,
1609 info
->selector
, data
, 1);
1611 uvc_trace(UVC_TRACE_CONTROL
,
1612 "GET_INFO failed on control %pUl/%u (%d).\n",
1613 info
->entity
, info
->selector
, ret
);
1617 info
->flags
= UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
1618 | UVC_CTRL_FLAG_GET_RES
| UVC_CTRL_FLAG_GET_DEF
1619 | (data
[0] & UVC_CONTROL_CAP_GET
?
1620 UVC_CTRL_FLAG_GET_CUR
: 0)
1621 | (data
[0] & UVC_CONTROL_CAP_SET
?
1622 UVC_CTRL_FLAG_SET_CUR
: 0)
1623 | (data
[0] & UVC_CONTROL_CAP_AUTOUPDATE
?
1624 UVC_CTRL_FLAG_AUTO_UPDATE
: 0);
1626 uvc_ctrl_fixup_xu_info(dev
, ctrl
, info
);
1628 uvc_trace(UVC_TRACE_CONTROL
, "XU control %pUl/%u queried: len %u, "
1629 "flags { get %u set %u auto %u }.\n",
1630 info
->entity
, info
->selector
, info
->size
,
1631 (info
->flags
& UVC_CTRL_FLAG_GET_CUR
) ? 1 : 0,
1632 (info
->flags
& UVC_CTRL_FLAG_SET_CUR
) ? 1 : 0,
1633 (info
->flags
& UVC_CTRL_FLAG_AUTO_UPDATE
) ? 1 : 0);
1640 static int uvc_ctrl_add_info(struct uvc_device
*dev
, struct uvc_control
*ctrl
,
1641 const struct uvc_control_info
*info
);
1643 static int uvc_ctrl_init_xu_ctrl(struct uvc_device
*dev
,
1644 struct uvc_control
*ctrl
)
1646 struct uvc_control_info info
;
1649 if (ctrl
->initialized
)
1652 ret
= uvc_ctrl_fill_xu_info(dev
, ctrl
, &info
);
1656 ret
= uvc_ctrl_add_info(dev
, ctrl
, &info
);
1658 uvc_trace(UVC_TRACE_CONTROL
, "Failed to initialize control "
1659 "%pUl/%u on device %s entity %u\n", info
.entity
,
1660 info
.selector
, dev
->udev
->devpath
, ctrl
->entity
->id
);
1665 int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
1666 struct uvc_xu_control_query
*xqry
)
1668 struct uvc_entity
*entity
;
1669 struct uvc_control
*ctrl
;
1670 unsigned int i
, found
= 0;
1676 /* Find the extension unit. */
1677 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1678 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
&&
1679 entity
->id
== xqry
->unit
)
1683 if (entity
->id
!= xqry
->unit
) {
1684 uvc_trace(UVC_TRACE_CONTROL
, "Extension unit %u not found.\n",
1689 /* Find the control and perform delayed initialization if needed. */
1690 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1691 ctrl
= &entity
->controls
[i
];
1692 if (ctrl
->index
== xqry
->selector
- 1) {
1699 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u not found.\n",
1700 entity
->extension
.guidExtensionCode
, xqry
->selector
);
1704 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
1705 return -ERESTARTSYS
;
1707 ret
= uvc_ctrl_init_xu_ctrl(chain
->dev
, ctrl
);
1713 /* Validate the required buffer size and flags for the request */
1715 size
= ctrl
->info
.size
;
1717 switch (xqry
->query
) {
1719 reqflags
= UVC_CTRL_FLAG_GET_CUR
;
1722 reqflags
= UVC_CTRL_FLAG_GET_MIN
;
1725 reqflags
= UVC_CTRL_FLAG_GET_MAX
;
1728 reqflags
= UVC_CTRL_FLAG_GET_DEF
;
1731 reqflags
= UVC_CTRL_FLAG_GET_RES
;
1734 reqflags
= UVC_CTRL_FLAG_SET_CUR
;
1747 if (size
!= xqry
->size
) {
1752 if (reqflags
&& !(ctrl
->info
.flags
& reqflags
)) {
1757 data
= kmalloc(size
, GFP_KERNEL
);
1763 if (xqry
->query
== UVC_SET_CUR
&&
1764 copy_from_user(data
, xqry
->data
, size
)) {
1769 ret
= uvc_query_ctrl(chain
->dev
, xqry
->query
, xqry
->unit
,
1770 chain
->dev
->intfnum
, xqry
->selector
, data
, size
);
1774 if (xqry
->query
!= UVC_SET_CUR
&&
1775 copy_to_user(xqry
->data
, data
, size
))
1779 mutex_unlock(&chain
->ctrl_mutex
);
1783 /* --------------------------------------------------------------------------
1788 * Restore control values after resume, skipping controls that haven't been
1792 * - Don't restore modified controls that are back to their default value.
1793 * - Handle restore order (Auto-Exposure Mode should be restored before
1796 int uvc_ctrl_resume_device(struct uvc_device
*dev
)
1798 struct uvc_control
*ctrl
;
1799 struct uvc_entity
*entity
;
1803 /* Walk the entities list and restore controls when possible. */
1804 list_for_each_entry(entity
, &dev
->entities
, list
) {
1806 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1807 ctrl
= &entity
->controls
[i
];
1809 if (!ctrl
->initialized
|| !ctrl
->modified
||
1810 (ctrl
->info
.flags
& UVC_CTRL_FLAG_RESTORE
) == 0)
1813 printk(KERN_INFO
"restoring control %pUl/%u/%u\n",
1814 ctrl
->info
.entity
, ctrl
->info
.index
,
1815 ctrl
->info
.selector
);
1819 ret
= uvc_ctrl_commit_entity(dev
, entity
, 0);
1827 /* --------------------------------------------------------------------------
1828 * Control and mapping handling
1832 * Add control information to a given control.
1834 static int uvc_ctrl_add_info(struct uvc_device
*dev
, struct uvc_control
*ctrl
,
1835 const struct uvc_control_info
*info
)
1839 memcpy(&ctrl
->info
, info
, sizeof(*info
));
1840 INIT_LIST_HEAD(&ctrl
->info
.mappings
);
1842 /* Allocate an array to save control values (cur, def, max, etc.) */
1843 ctrl
->uvc_data
= kzalloc(ctrl
->info
.size
* UVC_CTRL_DATA_LAST
+ 1,
1845 if (ctrl
->uvc_data
== NULL
) {
1850 ctrl
->initialized
= 1;
1852 uvc_trace(UVC_TRACE_CONTROL
, "Added control %pUl/%u to device %s "
1853 "entity %u\n", ctrl
->info
.entity
, ctrl
->info
.selector
,
1854 dev
->udev
->devpath
, ctrl
->entity
->id
);
1858 kfree(ctrl
->uvc_data
);
1863 * Add a control mapping to a given control.
1865 static int __uvc_ctrl_add_mapping(struct uvc_device
*dev
,
1866 struct uvc_control
*ctrl
, const struct uvc_control_mapping
*mapping
)
1868 struct uvc_control_mapping
*map
;
1871 /* Most mappings come from static kernel data and need to be duplicated.
1872 * Mappings that come from userspace will be unnecessarily duplicated,
1873 * this could be optimized.
1875 map
= kmemdup(mapping
, sizeof(*mapping
), GFP_KERNEL
);
1879 INIT_LIST_HEAD(&map
->ev_subs
);
1881 size
= sizeof(*mapping
->menu_info
) * mapping
->menu_count
;
1882 map
->menu_info
= kmemdup(mapping
->menu_info
, size
, GFP_KERNEL
);
1883 if (map
->menu_info
== NULL
) {
1888 if (map
->get
== NULL
)
1889 map
->get
= uvc_get_le_value
;
1890 if (map
->set
== NULL
)
1891 map
->set
= uvc_set_le_value
;
1893 list_add_tail(&map
->list
, &ctrl
->info
.mappings
);
1894 uvc_trace(UVC_TRACE_CONTROL
,
1895 "Adding mapping '%s' to control %pUl/%u.\n",
1896 map
->name
, ctrl
->info
.entity
, ctrl
->info
.selector
);
1901 int uvc_ctrl_add_mapping(struct uvc_video_chain
*chain
,
1902 const struct uvc_control_mapping
*mapping
)
1904 struct uvc_device
*dev
= chain
->dev
;
1905 struct uvc_control_mapping
*map
;
1906 struct uvc_entity
*entity
;
1907 struct uvc_control
*ctrl
;
1911 if (mapping
->id
& ~V4L2_CTRL_ID_MASK
) {
1912 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', control "
1913 "id 0x%08x is invalid.\n", mapping
->name
,
1918 /* Search for the matching (GUID/CS) control on the current chain */
1919 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1922 if (UVC_ENTITY_TYPE(entity
) != UVC_VC_EXTENSION_UNIT
||
1923 !uvc_entity_match_guid(entity
, mapping
->entity
))
1926 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1927 ctrl
= &entity
->controls
[i
];
1928 if (ctrl
->index
== mapping
->selector
- 1) {
1940 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
1941 return -ERESTARTSYS
;
1943 /* Perform delayed initialization of XU controls */
1944 ret
= uvc_ctrl_init_xu_ctrl(dev
, ctrl
);
1950 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
1951 if (mapping
->id
== map
->id
) {
1952 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', "
1953 "control id 0x%08x already exists.\n",
1954 mapping
->name
, mapping
->id
);
1960 /* Prevent excess memory consumption */
1961 if (atomic_inc_return(&dev
->nmappings
) > UVC_MAX_CONTROL_MAPPINGS
) {
1962 atomic_dec(&dev
->nmappings
);
1963 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', maximum "
1964 "mappings count (%u) exceeded.\n", mapping
->name
,
1965 UVC_MAX_CONTROL_MAPPINGS
);
1970 ret
= __uvc_ctrl_add_mapping(dev
, ctrl
, mapping
);
1972 atomic_dec(&dev
->nmappings
);
1975 mutex_unlock(&chain
->ctrl_mutex
);
1980 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1981 * are currently the ones that crash the camera or unconditionally return an
1982 * error when queried.
1984 static void uvc_ctrl_prune_entity(struct uvc_device
*dev
,
1985 struct uvc_entity
*entity
)
1987 struct uvc_ctrl_blacklist
{
1988 struct usb_device_id id
;
1992 static const struct uvc_ctrl_blacklist processing_blacklist
[] = {
1993 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1994 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1995 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1997 static const struct uvc_ctrl_blacklist camera_blacklist
[] = {
1998 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2001 const struct uvc_ctrl_blacklist
*blacklist
;
2007 switch (UVC_ENTITY_TYPE(entity
)) {
2008 case UVC_VC_PROCESSING_UNIT
:
2009 blacklist
= processing_blacklist
;
2010 count
= ARRAY_SIZE(processing_blacklist
);
2011 controls
= entity
->processing
.bmControls
;
2012 size
= entity
->processing
.bControlSize
;
2015 case UVC_ITT_CAMERA
:
2016 blacklist
= camera_blacklist
;
2017 count
= ARRAY_SIZE(camera_blacklist
);
2018 controls
= entity
->camera
.bmControls
;
2019 size
= entity
->camera
.bControlSize
;
2026 for (i
= 0; i
< count
; ++i
) {
2027 if (!usb_match_one_id(dev
->intf
, &blacklist
[i
].id
))
2030 if (blacklist
[i
].index
>= 8 * size
||
2031 !uvc_test_bit(controls
, blacklist
[i
].index
))
2034 uvc_trace(UVC_TRACE_CONTROL
, "%u/%u control is black listed, "
2035 "removing it.\n", entity
->id
, blacklist
[i
].index
);
2037 uvc_clear_bit(controls
, blacklist
[i
].index
);
2042 * Add control information and hardcoded stock control mappings to the given
2045 static void uvc_ctrl_init_ctrl(struct uvc_device
*dev
, struct uvc_control
*ctrl
)
2047 const struct uvc_control_info
*info
= uvc_ctrls
;
2048 const struct uvc_control_info
*iend
= info
+ ARRAY_SIZE(uvc_ctrls
);
2049 const struct uvc_control_mapping
*mapping
= uvc_ctrl_mappings
;
2050 const struct uvc_control_mapping
*mend
=
2051 mapping
+ ARRAY_SIZE(uvc_ctrl_mappings
);
2053 /* XU controls initialization requires querying the device for control
2054 * information. As some buggy UVC devices will crash when queried
2055 * repeatedly in a tight loop, delay XU controls initialization until
2058 if (UVC_ENTITY_TYPE(ctrl
->entity
) == UVC_VC_EXTENSION_UNIT
)
2061 for (; info
< iend
; ++info
) {
2062 if (uvc_entity_match_guid(ctrl
->entity
, info
->entity
) &&
2063 ctrl
->index
== info
->index
) {
2064 uvc_ctrl_add_info(dev
, ctrl
, info
);
2069 if (!ctrl
->initialized
)
2072 for (; mapping
< mend
; ++mapping
) {
2073 if (uvc_entity_match_guid(ctrl
->entity
, mapping
->entity
) &&
2074 ctrl
->info
.selector
== mapping
->selector
)
2075 __uvc_ctrl_add_mapping(dev
, ctrl
, mapping
);
2080 * Initialize device controls.
2082 int uvc_ctrl_init_device(struct uvc_device
*dev
)
2084 struct uvc_entity
*entity
;
2087 /* Walk the entities list and instantiate controls */
2088 list_for_each_entry(entity
, &dev
->entities
, list
) {
2089 struct uvc_control
*ctrl
;
2090 unsigned int bControlSize
= 0, ncontrols
;
2091 __u8
*bmControls
= NULL
;
2093 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
2094 bmControls
= entity
->extension
.bmControls
;
2095 bControlSize
= entity
->extension
.bControlSize
;
2096 } else if (UVC_ENTITY_TYPE(entity
) == UVC_VC_PROCESSING_UNIT
) {
2097 bmControls
= entity
->processing
.bmControls
;
2098 bControlSize
= entity
->processing
.bControlSize
;
2099 } else if (UVC_ENTITY_TYPE(entity
) == UVC_ITT_CAMERA
) {
2100 bmControls
= entity
->camera
.bmControls
;
2101 bControlSize
= entity
->camera
.bControlSize
;
2104 /* Remove bogus/blacklisted controls */
2105 uvc_ctrl_prune_entity(dev
, entity
);
2107 /* Count supported controls and allocate the controls array */
2108 ncontrols
= memweight(bmControls
, bControlSize
);
2112 entity
->controls
= kcalloc(ncontrols
, sizeof(*ctrl
),
2114 if (entity
->controls
== NULL
)
2116 entity
->ncontrols
= ncontrols
;
2118 /* Initialize all supported controls */
2119 ctrl
= entity
->controls
;
2120 for (i
= 0; i
< bControlSize
* 8; ++i
) {
2121 if (uvc_test_bit(bmControls
, i
) == 0)
2124 ctrl
->entity
= entity
;
2127 uvc_ctrl_init_ctrl(dev
, ctrl
);
2136 * Cleanup device controls.
2138 static void uvc_ctrl_cleanup_mappings(struct uvc_device
*dev
,
2139 struct uvc_control
*ctrl
)
2141 struct uvc_control_mapping
*mapping
, *nm
;
2143 list_for_each_entry_safe(mapping
, nm
, &ctrl
->info
.mappings
, list
) {
2144 list_del(&mapping
->list
);
2145 kfree(mapping
->menu_info
);
2150 void uvc_ctrl_cleanup_device(struct uvc_device
*dev
)
2152 struct uvc_entity
*entity
;
2155 /* Free controls and control mappings for all entities. */
2156 list_for_each_entry(entity
, &dev
->entities
, list
) {
2157 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
2158 struct uvc_control
*ctrl
= &entity
->controls
[i
];
2160 if (!ctrl
->initialized
)
2163 uvc_ctrl_cleanup_mappings(dev
, ctrl
);
2164 kfree(ctrl
->uvc_data
);
2167 kfree(entity
->controls
);