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/workqueue.h>
24 #include <linux/atomic.h>
25 #include <media/v4l2-ctrls.h>
29 #define UVC_CTRL_DATA_CURRENT 0
30 #define UVC_CTRL_DATA_BACKUP 1
31 #define UVC_CTRL_DATA_MIN 2
32 #define UVC_CTRL_DATA_MAX 3
33 #define UVC_CTRL_DATA_RES 4
34 #define UVC_CTRL_DATA_DEF 5
35 #define UVC_CTRL_DATA_LAST 6
37 /* ------------------------------------------------------------------------
41 static const struct uvc_control_info uvc_ctrls
[] = {
43 .entity
= UVC_GUID_UVC_PROCESSING
,
44 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
47 .flags
= UVC_CTRL_FLAG_SET_CUR
48 | UVC_CTRL_FLAG_GET_RANGE
49 | UVC_CTRL_FLAG_RESTORE
,
52 .entity
= UVC_GUID_UVC_PROCESSING
,
53 .selector
= UVC_PU_CONTRAST_CONTROL
,
56 .flags
= UVC_CTRL_FLAG_SET_CUR
57 | UVC_CTRL_FLAG_GET_RANGE
58 | UVC_CTRL_FLAG_RESTORE
,
61 .entity
= UVC_GUID_UVC_PROCESSING
,
62 .selector
= UVC_PU_HUE_CONTROL
,
65 .flags
= UVC_CTRL_FLAG_SET_CUR
66 | UVC_CTRL_FLAG_GET_RANGE
67 | UVC_CTRL_FLAG_RESTORE
68 | UVC_CTRL_FLAG_AUTO_UPDATE
,
71 .entity
= UVC_GUID_UVC_PROCESSING
,
72 .selector
= UVC_PU_SATURATION_CONTROL
,
75 .flags
= UVC_CTRL_FLAG_SET_CUR
76 | UVC_CTRL_FLAG_GET_RANGE
77 | UVC_CTRL_FLAG_RESTORE
,
80 .entity
= UVC_GUID_UVC_PROCESSING
,
81 .selector
= UVC_PU_SHARPNESS_CONTROL
,
84 .flags
= UVC_CTRL_FLAG_SET_CUR
85 | UVC_CTRL_FLAG_GET_RANGE
86 | UVC_CTRL_FLAG_RESTORE
,
89 .entity
= UVC_GUID_UVC_PROCESSING
,
90 .selector
= UVC_PU_GAMMA_CONTROL
,
93 .flags
= UVC_CTRL_FLAG_SET_CUR
94 | UVC_CTRL_FLAG_GET_RANGE
95 | UVC_CTRL_FLAG_RESTORE
,
98 .entity
= UVC_GUID_UVC_PROCESSING
,
99 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
102 .flags
= UVC_CTRL_FLAG_SET_CUR
103 | UVC_CTRL_FLAG_GET_RANGE
104 | UVC_CTRL_FLAG_RESTORE
105 | UVC_CTRL_FLAG_AUTO_UPDATE
,
108 .entity
= UVC_GUID_UVC_PROCESSING
,
109 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
112 .flags
= UVC_CTRL_FLAG_SET_CUR
113 | UVC_CTRL_FLAG_GET_RANGE
114 | UVC_CTRL_FLAG_RESTORE
115 | UVC_CTRL_FLAG_AUTO_UPDATE
,
118 .entity
= UVC_GUID_UVC_PROCESSING
,
119 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
122 .flags
= UVC_CTRL_FLAG_SET_CUR
123 | UVC_CTRL_FLAG_GET_RANGE
124 | UVC_CTRL_FLAG_RESTORE
,
127 .entity
= UVC_GUID_UVC_PROCESSING
,
128 .selector
= UVC_PU_GAIN_CONTROL
,
131 .flags
= UVC_CTRL_FLAG_SET_CUR
132 | UVC_CTRL_FLAG_GET_RANGE
133 | UVC_CTRL_FLAG_RESTORE
,
136 .entity
= UVC_GUID_UVC_PROCESSING
,
137 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
140 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
141 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
144 .entity
= UVC_GUID_UVC_PROCESSING
,
145 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
148 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
149 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
152 .entity
= UVC_GUID_UVC_PROCESSING
,
153 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
156 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
157 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
160 .entity
= UVC_GUID_UVC_PROCESSING
,
161 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
164 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
165 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
168 .entity
= UVC_GUID_UVC_PROCESSING
,
169 .selector
= UVC_PU_DIGITAL_MULTIPLIER_CONTROL
,
172 .flags
= UVC_CTRL_FLAG_SET_CUR
173 | UVC_CTRL_FLAG_GET_RANGE
174 | UVC_CTRL_FLAG_RESTORE
,
177 .entity
= UVC_GUID_UVC_PROCESSING
,
178 .selector
= UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL
,
181 .flags
= UVC_CTRL_FLAG_SET_CUR
182 | UVC_CTRL_FLAG_GET_RANGE
183 | UVC_CTRL_FLAG_RESTORE
,
186 .entity
= UVC_GUID_UVC_PROCESSING
,
187 .selector
= UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL
,
190 .flags
= UVC_CTRL_FLAG_GET_CUR
,
193 .entity
= UVC_GUID_UVC_PROCESSING
,
194 .selector
= UVC_PU_ANALOG_LOCK_STATUS_CONTROL
,
197 .flags
= UVC_CTRL_FLAG_GET_CUR
,
200 .entity
= UVC_GUID_UVC_CAMERA
,
201 .selector
= UVC_CT_SCANNING_MODE_CONTROL
,
204 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
205 | UVC_CTRL_FLAG_RESTORE
,
208 .entity
= UVC_GUID_UVC_CAMERA
,
209 .selector
= UVC_CT_AE_MODE_CONTROL
,
212 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
213 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_GET_RES
214 | UVC_CTRL_FLAG_RESTORE
,
217 .entity
= UVC_GUID_UVC_CAMERA
,
218 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
221 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
222 | UVC_CTRL_FLAG_RESTORE
,
225 .entity
= UVC_GUID_UVC_CAMERA
,
226 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
229 .flags
= UVC_CTRL_FLAG_SET_CUR
230 | UVC_CTRL_FLAG_GET_RANGE
231 | UVC_CTRL_FLAG_RESTORE
232 | UVC_CTRL_FLAG_AUTO_UPDATE
,
235 .entity
= UVC_GUID_UVC_CAMERA
,
236 .selector
= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL
,
239 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_RESTORE
,
242 .entity
= UVC_GUID_UVC_CAMERA
,
243 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
246 .flags
= UVC_CTRL_FLAG_SET_CUR
247 | UVC_CTRL_FLAG_GET_RANGE
248 | UVC_CTRL_FLAG_RESTORE
249 | UVC_CTRL_FLAG_AUTO_UPDATE
,
252 .entity
= UVC_GUID_UVC_CAMERA
,
253 .selector
= UVC_CT_FOCUS_RELATIVE_CONTROL
,
256 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_MIN
257 | UVC_CTRL_FLAG_GET_MAX
| UVC_CTRL_FLAG_GET_RES
258 | UVC_CTRL_FLAG_GET_DEF
259 | UVC_CTRL_FLAG_AUTO_UPDATE
,
262 .entity
= UVC_GUID_UVC_CAMERA
,
263 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
266 .flags
= UVC_CTRL_FLAG_SET_CUR
267 | UVC_CTRL_FLAG_GET_RANGE
268 | UVC_CTRL_FLAG_RESTORE
269 | UVC_CTRL_FLAG_AUTO_UPDATE
,
272 .entity
= UVC_GUID_UVC_CAMERA
,
273 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
276 .flags
= UVC_CTRL_FLAG_SET_CUR
277 | UVC_CTRL_FLAG_AUTO_UPDATE
,
280 .entity
= UVC_GUID_UVC_CAMERA
,
281 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
284 .flags
= UVC_CTRL_FLAG_SET_CUR
285 | UVC_CTRL_FLAG_GET_RANGE
286 | UVC_CTRL_FLAG_RESTORE
287 | UVC_CTRL_FLAG_AUTO_UPDATE
,
290 .entity
= UVC_GUID_UVC_CAMERA
,
291 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
294 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_MIN
295 | UVC_CTRL_FLAG_GET_MAX
| UVC_CTRL_FLAG_GET_RES
296 | UVC_CTRL_FLAG_GET_DEF
297 | UVC_CTRL_FLAG_AUTO_UPDATE
,
300 .entity
= UVC_GUID_UVC_CAMERA
,
301 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
304 .flags
= UVC_CTRL_FLAG_SET_CUR
305 | UVC_CTRL_FLAG_GET_RANGE
306 | UVC_CTRL_FLAG_RESTORE
307 | UVC_CTRL_FLAG_AUTO_UPDATE
,
310 .entity
= UVC_GUID_UVC_CAMERA
,
311 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
314 .flags
= UVC_CTRL_FLAG_SET_CUR
315 | UVC_CTRL_FLAG_GET_RANGE
316 | UVC_CTRL_FLAG_AUTO_UPDATE
,
319 .entity
= UVC_GUID_UVC_CAMERA
,
320 .selector
= UVC_CT_ROLL_ABSOLUTE_CONTROL
,
323 .flags
= UVC_CTRL_FLAG_SET_CUR
324 | UVC_CTRL_FLAG_GET_RANGE
325 | UVC_CTRL_FLAG_RESTORE
326 | UVC_CTRL_FLAG_AUTO_UPDATE
,
329 .entity
= UVC_GUID_UVC_CAMERA
,
330 .selector
= UVC_CT_ROLL_RELATIVE_CONTROL
,
333 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_MIN
334 | UVC_CTRL_FLAG_GET_MAX
| UVC_CTRL_FLAG_GET_RES
335 | UVC_CTRL_FLAG_GET_DEF
336 | UVC_CTRL_FLAG_AUTO_UPDATE
,
339 .entity
= UVC_GUID_UVC_CAMERA
,
340 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
343 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
344 | UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_RESTORE
,
347 .entity
= UVC_GUID_UVC_CAMERA
,
348 .selector
= UVC_CT_PRIVACY_CONTROL
,
351 .flags
= UVC_CTRL_FLAG_SET_CUR
| UVC_CTRL_FLAG_GET_CUR
352 | UVC_CTRL_FLAG_RESTORE
353 | UVC_CTRL_FLAG_AUTO_UPDATE
,
357 static const struct uvc_menu_info power_line_frequency_controls
[] = {
363 static const struct uvc_menu_info exposure_auto_controls
[] = {
365 { 1, "Manual Mode" },
366 { 4, "Shutter Priority Mode" },
367 { 8, "Aperture Priority Mode" },
370 static s32
uvc_ctrl_get_zoom(struct uvc_control_mapping
*mapping
,
371 u8 query
, const u8
*data
)
373 s8 zoom
= (s8
)data
[0];
377 return (zoom
== 0) ? 0 : (zoom
> 0 ? data
[2] : -data
[2]);
388 static void uvc_ctrl_set_zoom(struct uvc_control_mapping
*mapping
,
391 data
[0] = value
== 0 ? 0 : (value
> 0) ? 1 : 0xff;
392 data
[2] = min((int)abs(value
), 0xff);
395 static s32
uvc_ctrl_get_rel_speed(struct uvc_control_mapping
*mapping
,
396 u8 query
, const u8
*data
)
398 unsigned int first
= mapping
->offset
/ 8;
399 s8 rel
= (s8
)data
[first
];
403 return (rel
== 0) ? 0 : (rel
> 0 ? data
[first
+1]
406 return -data
[first
+1];
411 return data
[first
+1];
415 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping
*mapping
,
418 unsigned int first
= mapping
->offset
/ 8;
420 data
[first
] = value
== 0 ? 0 : (value
> 0) ? 1 : 0xff;
421 data
[first
+1] = min_t(int, abs(value
), 0xff);
424 static const struct uvc_control_mapping uvc_ctrl_mappings
[] = {
426 .id
= V4L2_CID_BRIGHTNESS
,
427 .name
= "Brightness",
428 .entity
= UVC_GUID_UVC_PROCESSING
,
429 .selector
= UVC_PU_BRIGHTNESS_CONTROL
,
432 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
433 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
436 .id
= V4L2_CID_CONTRAST
,
438 .entity
= UVC_GUID_UVC_PROCESSING
,
439 .selector
= UVC_PU_CONTRAST_CONTROL
,
442 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
443 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
448 .entity
= UVC_GUID_UVC_PROCESSING
,
449 .selector
= UVC_PU_HUE_CONTROL
,
452 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
453 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
454 .master_id
= V4L2_CID_HUE_AUTO
,
458 .id
= V4L2_CID_SATURATION
,
459 .name
= "Saturation",
460 .entity
= UVC_GUID_UVC_PROCESSING
,
461 .selector
= UVC_PU_SATURATION_CONTROL
,
464 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
465 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
468 .id
= V4L2_CID_SHARPNESS
,
470 .entity
= UVC_GUID_UVC_PROCESSING
,
471 .selector
= UVC_PU_SHARPNESS_CONTROL
,
474 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
475 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
478 .id
= V4L2_CID_GAMMA
,
480 .entity
= UVC_GUID_UVC_PROCESSING
,
481 .selector
= UVC_PU_GAMMA_CONTROL
,
484 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
485 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
488 .id
= V4L2_CID_BACKLIGHT_COMPENSATION
,
489 .name
= "Backlight Compensation",
490 .entity
= UVC_GUID_UVC_PROCESSING
,
491 .selector
= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL
,
494 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
495 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
500 .entity
= UVC_GUID_UVC_PROCESSING
,
501 .selector
= UVC_PU_GAIN_CONTROL
,
504 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
505 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
508 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
509 .name
= "Power Line Frequency",
510 .entity
= UVC_GUID_UVC_PROCESSING
,
511 .selector
= UVC_PU_POWER_LINE_FREQUENCY_CONTROL
,
514 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
515 .data_type
= UVC_CTRL_DATA_TYPE_ENUM
,
516 .menu_info
= power_line_frequency_controls
,
517 .menu_count
= ARRAY_SIZE(power_line_frequency_controls
),
520 .id
= V4L2_CID_HUE_AUTO
,
522 .entity
= UVC_GUID_UVC_PROCESSING
,
523 .selector
= UVC_PU_HUE_AUTO_CONTROL
,
526 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
527 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
528 .slave_ids
= { V4L2_CID_HUE
, },
531 .id
= V4L2_CID_EXPOSURE_AUTO
,
532 .name
= "Exposure, Auto",
533 .entity
= UVC_GUID_UVC_CAMERA
,
534 .selector
= UVC_CT_AE_MODE_CONTROL
,
537 .v4l2_type
= V4L2_CTRL_TYPE_MENU
,
538 .data_type
= UVC_CTRL_DATA_TYPE_BITMASK
,
539 .menu_info
= exposure_auto_controls
,
540 .menu_count
= ARRAY_SIZE(exposure_auto_controls
),
541 .slave_ids
= { V4L2_CID_EXPOSURE_ABSOLUTE
, },
544 .id
= V4L2_CID_EXPOSURE_AUTO_PRIORITY
,
545 .name
= "Exposure, Auto Priority",
546 .entity
= UVC_GUID_UVC_CAMERA
,
547 .selector
= UVC_CT_AE_PRIORITY_CONTROL
,
550 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
551 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
554 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
555 .name
= "Exposure (Absolute)",
556 .entity
= UVC_GUID_UVC_CAMERA
,
557 .selector
= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL
,
560 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
561 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
562 .master_id
= V4L2_CID_EXPOSURE_AUTO
,
563 .master_manual
= V4L2_EXPOSURE_MANUAL
,
566 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
567 .name
= "White Balance Temperature, Auto",
568 .entity
= UVC_GUID_UVC_PROCESSING
,
569 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL
,
572 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
573 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
574 .slave_ids
= { V4L2_CID_WHITE_BALANCE_TEMPERATURE
, },
577 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
578 .name
= "White Balance Temperature",
579 .entity
= UVC_GUID_UVC_PROCESSING
,
580 .selector
= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL
,
583 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
584 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
585 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
589 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
590 .name
= "White Balance Component, Auto",
591 .entity
= UVC_GUID_UVC_PROCESSING
,
592 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL
,
595 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
596 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
597 .slave_ids
= { V4L2_CID_BLUE_BALANCE
,
598 V4L2_CID_RED_BALANCE
},
601 .id
= V4L2_CID_BLUE_BALANCE
,
602 .name
= "White Balance Blue Component",
603 .entity
= UVC_GUID_UVC_PROCESSING
,
604 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
607 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
608 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
609 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
613 .id
= V4L2_CID_RED_BALANCE
,
614 .name
= "White Balance Red Component",
615 .entity
= UVC_GUID_UVC_PROCESSING
,
616 .selector
= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL
,
619 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
620 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
621 .master_id
= V4L2_CID_AUTO_WHITE_BALANCE
,
625 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
626 .name
= "Focus (absolute)",
627 .entity
= UVC_GUID_UVC_CAMERA
,
628 .selector
= UVC_CT_FOCUS_ABSOLUTE_CONTROL
,
631 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
632 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
633 .master_id
= V4L2_CID_FOCUS_AUTO
,
637 .id
= V4L2_CID_FOCUS_AUTO
,
638 .name
= "Focus, Auto",
639 .entity
= UVC_GUID_UVC_CAMERA
,
640 .selector
= UVC_CT_FOCUS_AUTO_CONTROL
,
643 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
644 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
645 .slave_ids
= { V4L2_CID_FOCUS_ABSOLUTE
, },
648 .id
= V4L2_CID_IRIS_ABSOLUTE
,
649 .name
= "Iris, Absolute",
650 .entity
= UVC_GUID_UVC_CAMERA
,
651 .selector
= UVC_CT_IRIS_ABSOLUTE_CONTROL
,
654 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
655 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
658 .id
= V4L2_CID_IRIS_RELATIVE
,
659 .name
= "Iris, Relative",
660 .entity
= UVC_GUID_UVC_CAMERA
,
661 .selector
= UVC_CT_IRIS_RELATIVE_CONTROL
,
664 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
665 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
668 .id
= V4L2_CID_ZOOM_ABSOLUTE
,
669 .name
= "Zoom, Absolute",
670 .entity
= UVC_GUID_UVC_CAMERA
,
671 .selector
= UVC_CT_ZOOM_ABSOLUTE_CONTROL
,
674 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
675 .data_type
= UVC_CTRL_DATA_TYPE_UNSIGNED
,
678 .id
= V4L2_CID_ZOOM_CONTINUOUS
,
679 .name
= "Zoom, Continuous",
680 .entity
= UVC_GUID_UVC_CAMERA
,
681 .selector
= UVC_CT_ZOOM_RELATIVE_CONTROL
,
684 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
685 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
686 .get
= uvc_ctrl_get_zoom
,
687 .set
= uvc_ctrl_set_zoom
,
690 .id
= V4L2_CID_PAN_ABSOLUTE
,
691 .name
= "Pan (Absolute)",
692 .entity
= UVC_GUID_UVC_CAMERA
,
693 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
696 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
697 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
700 .id
= V4L2_CID_TILT_ABSOLUTE
,
701 .name
= "Tilt (Absolute)",
702 .entity
= UVC_GUID_UVC_CAMERA
,
703 .selector
= UVC_CT_PANTILT_ABSOLUTE_CONTROL
,
706 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
707 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
710 .id
= V4L2_CID_PAN_SPEED
,
711 .name
= "Pan (Speed)",
712 .entity
= UVC_GUID_UVC_CAMERA
,
713 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
716 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
717 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
718 .get
= uvc_ctrl_get_rel_speed
,
719 .set
= uvc_ctrl_set_rel_speed
,
722 .id
= V4L2_CID_TILT_SPEED
,
723 .name
= "Tilt (Speed)",
724 .entity
= UVC_GUID_UVC_CAMERA
,
725 .selector
= UVC_CT_PANTILT_RELATIVE_CONTROL
,
728 .v4l2_type
= V4L2_CTRL_TYPE_INTEGER
,
729 .data_type
= UVC_CTRL_DATA_TYPE_SIGNED
,
730 .get
= uvc_ctrl_get_rel_speed
,
731 .set
= uvc_ctrl_set_rel_speed
,
734 .id
= V4L2_CID_PRIVACY
,
736 .entity
= UVC_GUID_UVC_CAMERA
,
737 .selector
= UVC_CT_PRIVACY_CONTROL
,
740 .v4l2_type
= V4L2_CTRL_TYPE_BOOLEAN
,
741 .data_type
= UVC_CTRL_DATA_TYPE_BOOLEAN
,
745 /* ------------------------------------------------------------------------
749 static inline u8
*uvc_ctrl_data(struct uvc_control
*ctrl
, int id
)
751 return ctrl
->uvc_data
+ id
* ctrl
->info
.size
;
754 static inline int uvc_test_bit(const u8
*data
, int bit
)
756 return (data
[bit
>> 3] >> (bit
& 7)) & 1;
759 static inline void uvc_clear_bit(u8
*data
, int bit
)
761 data
[bit
>> 3] &= ~(1 << (bit
& 7));
764 /* Extract the bit string specified by mapping->offset and mapping->size
765 * from the little-endian data stored at 'data' and return the result as
766 * a signed 32bit integer. Sign extension will be performed if the mapping
767 * references a signed data type.
769 static s32
uvc_get_le_value(struct uvc_control_mapping
*mapping
,
770 u8 query
, const u8
*data
)
772 int bits
= mapping
->size
;
773 int offset
= mapping
->offset
;
779 mask
= ((1LL << bits
) - 1) << offset
;
781 for (; bits
> 0; data
++) {
782 u8 byte
= *data
& mask
;
783 value
|= offset
> 0 ? (byte
>> offset
) : (byte
<< (-offset
));
784 bits
-= 8 - (offset
> 0 ? offset
: 0);
786 mask
= (1 << bits
) - 1;
789 /* Sign-extend the value if needed. */
790 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
791 value
|= -(value
& (1 << (mapping
->size
- 1)));
796 /* Set the bit string specified by mapping->offset and mapping->size
797 * in the little-endian data stored at 'data' to the value 'value'.
799 static void uvc_set_le_value(struct uvc_control_mapping
*mapping
,
802 int bits
= mapping
->size
;
803 int offset
= mapping
->offset
;
806 /* According to the v4l2 spec, writing any value to a button control
807 * should result in the action belonging to the button control being
808 * triggered. UVC devices however want to see a 1 written -> override
811 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_BUTTON
)
817 for (; bits
> 0; data
++) {
818 mask
= ((1LL << bits
) - 1) << offset
;
819 *data
= (*data
& ~mask
) | ((value
<< offset
) & mask
);
820 value
>>= offset
? offset
: 8;
826 /* ------------------------------------------------------------------------
827 * Terminal and unit management
830 static const u8 uvc_processing_guid
[16] = UVC_GUID_UVC_PROCESSING
;
831 static const u8 uvc_camera_guid
[16] = UVC_GUID_UVC_CAMERA
;
832 static const u8 uvc_media_transport_input_guid
[16] =
833 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT
;
835 static int uvc_entity_match_guid(const struct uvc_entity
*entity
,
838 switch (UVC_ENTITY_TYPE(entity
)) {
840 return memcmp(uvc_camera_guid
, guid
, 16) == 0;
842 case UVC_ITT_MEDIA_TRANSPORT_INPUT
:
843 return memcmp(uvc_media_transport_input_guid
, guid
, 16) == 0;
845 case UVC_VC_PROCESSING_UNIT
:
846 return memcmp(uvc_processing_guid
, guid
, 16) == 0;
848 case UVC_VC_EXTENSION_UNIT
:
849 return memcmp(entity
->extension
.guidExtensionCode
,
857 /* ------------------------------------------------------------------------
861 static void __uvc_find_control(struct uvc_entity
*entity
, u32 v4l2_id
,
862 struct uvc_control_mapping
**mapping
, struct uvc_control
**control
,
865 struct uvc_control
*ctrl
;
866 struct uvc_control_mapping
*map
;
872 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
873 ctrl
= &entity
->controls
[i
];
874 if (!ctrl
->initialized
)
877 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
878 if ((map
->id
== v4l2_id
) && !next
) {
884 if ((*mapping
== NULL
|| (*mapping
)->id
> map
->id
) &&
885 (map
->id
> v4l2_id
) && next
) {
893 static struct uvc_control
*uvc_find_control(struct uvc_video_chain
*chain
,
894 u32 v4l2_id
, struct uvc_control_mapping
**mapping
)
896 struct uvc_control
*ctrl
= NULL
;
897 struct uvc_entity
*entity
;
898 int next
= v4l2_id
& V4L2_CTRL_FLAG_NEXT_CTRL
;
902 /* Mask the query flags. */
903 v4l2_id
&= V4L2_CTRL_ID_MASK
;
905 /* Find the control. */
906 list_for_each_entry(entity
, &chain
->entities
, chain
) {
907 __uvc_find_control(entity
, v4l2_id
, mapping
, &ctrl
, next
);
912 if (ctrl
== NULL
&& !next
)
913 uvc_trace(UVC_TRACE_CONTROL
, "Control 0x%08x not found.\n",
919 static int uvc_ctrl_populate_cache(struct uvc_video_chain
*chain
,
920 struct uvc_control
*ctrl
)
924 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_DEF
) {
925 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_DEF
, ctrl
->entity
->id
,
926 chain
->dev
->intfnum
, ctrl
->info
.selector
,
927 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
),
933 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MIN
) {
934 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MIN
, ctrl
->entity
->id
,
935 chain
->dev
->intfnum
, ctrl
->info
.selector
,
936 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
),
941 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MAX
) {
942 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_MAX
, ctrl
->entity
->id
,
943 chain
->dev
->intfnum
, ctrl
->info
.selector
,
944 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
),
949 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
) {
950 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_RES
, ctrl
->entity
->id
,
951 chain
->dev
->intfnum
, ctrl
->info
.selector
,
952 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
),
955 if (UVC_ENTITY_TYPE(ctrl
->entity
) !=
956 UVC_VC_EXTENSION_UNIT
)
959 /* GET_RES is mandatory for XU controls, but some
960 * cameras still choke on it. Ignore errors and set the
961 * resolution value to zero.
963 uvc_warn_once(chain
->dev
, UVC_WARN_XU_GET_RES
,
964 "UVC non compliance - GET_RES failed on "
965 "an XU control. Enabling workaround.\n");
966 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
), 0,
975 static s32
__uvc_ctrl_get_value(struct uvc_control_mapping
*mapping
,
978 s32 value
= mapping
->get(mapping
, UVC_GET_CUR
, data
);
980 if (mapping
->v4l2_type
== V4L2_CTRL_TYPE_MENU
) {
981 const struct uvc_menu_info
*menu
= mapping
->menu_info
;
984 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
985 if (menu
->value
== value
) {
995 static int __uvc_ctrl_get(struct uvc_video_chain
*chain
,
996 struct uvc_control
*ctrl
, struct uvc_control_mapping
*mapping
,
1001 if ((ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
) == 0)
1004 if (!ctrl
->loaded
) {
1005 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
, ctrl
->entity
->id
,
1006 chain
->dev
->intfnum
, ctrl
->info
.selector
,
1007 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1015 *value
= __uvc_ctrl_get_value(mapping
,
1016 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1021 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
1022 struct uvc_control
*ctrl
,
1023 struct uvc_control_mapping
*mapping
,
1024 struct v4l2_queryctrl
*v4l2_ctrl
)
1026 struct uvc_control_mapping
*master_map
= NULL
;
1027 struct uvc_control
*master_ctrl
= NULL
;
1028 const struct uvc_menu_info
*menu
;
1031 memset(v4l2_ctrl
, 0, sizeof(*v4l2_ctrl
));
1032 v4l2_ctrl
->id
= mapping
->id
;
1033 v4l2_ctrl
->type
= mapping
->v4l2_type
;
1034 strscpy(v4l2_ctrl
->name
, mapping
->name
, sizeof(v4l2_ctrl
->name
));
1035 v4l2_ctrl
->flags
= 0;
1037 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
))
1038 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_WRITE_ONLY
;
1039 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_SET_CUR
))
1040 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1042 if (mapping
->master_id
)
1043 __uvc_find_control(ctrl
->entity
, mapping
->master_id
,
1044 &master_map
, &master_ctrl
, 0);
1045 if (master_ctrl
&& (master_ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
)) {
1047 int ret
= __uvc_ctrl_get(chain
, master_ctrl
, master_map
, &val
);
1051 if (val
!= mapping
->master_manual
)
1052 v4l2_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
1055 if (!ctrl
->cached
) {
1056 int ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1061 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_DEF
) {
1062 v4l2_ctrl
->default_value
= mapping
->get(mapping
, UVC_GET_DEF
,
1063 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_DEF
));
1066 switch (mapping
->v4l2_type
) {
1067 case V4L2_CTRL_TYPE_MENU
:
1068 v4l2_ctrl
->minimum
= 0;
1069 v4l2_ctrl
->maximum
= mapping
->menu_count
- 1;
1070 v4l2_ctrl
->step
= 1;
1072 menu
= mapping
->menu_info
;
1073 for (i
= 0; i
< mapping
->menu_count
; ++i
, ++menu
) {
1074 if (menu
->value
== v4l2_ctrl
->default_value
) {
1075 v4l2_ctrl
->default_value
= i
;
1082 case V4L2_CTRL_TYPE_BOOLEAN
:
1083 v4l2_ctrl
->minimum
= 0;
1084 v4l2_ctrl
->maximum
= 1;
1085 v4l2_ctrl
->step
= 1;
1088 case V4L2_CTRL_TYPE_BUTTON
:
1089 v4l2_ctrl
->minimum
= 0;
1090 v4l2_ctrl
->maximum
= 0;
1091 v4l2_ctrl
->step
= 0;
1098 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MIN
)
1099 v4l2_ctrl
->minimum
= mapping
->get(mapping
, UVC_GET_MIN
,
1100 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1102 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_MAX
)
1103 v4l2_ctrl
->maximum
= mapping
->get(mapping
, UVC_GET_MAX
,
1104 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1106 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)
1107 v4l2_ctrl
->step
= mapping
->get(mapping
, UVC_GET_RES
,
1108 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1113 int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
1114 struct v4l2_queryctrl
*v4l2_ctrl
)
1116 struct uvc_control
*ctrl
;
1117 struct uvc_control_mapping
*mapping
;
1120 ret
= mutex_lock_interruptible(&chain
->ctrl_mutex
);
1122 return -ERESTARTSYS
;
1124 ctrl
= uvc_find_control(chain
, v4l2_ctrl
->id
, &mapping
);
1130 ret
= __uvc_query_v4l2_ctrl(chain
, ctrl
, mapping
, v4l2_ctrl
);
1132 mutex_unlock(&chain
->ctrl_mutex
);
1137 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1138 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1139 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1140 * Balance V4L2 controls use the White Balance Component UVC control) or
1141 * otherwise translated. The approach we take here is to use a translation
1142 * table for the controls that can be mapped directly, and handle the others
1145 int uvc_query_v4l2_menu(struct uvc_video_chain
*chain
,
1146 struct v4l2_querymenu
*query_menu
)
1148 const struct uvc_menu_info
*menu_info
;
1149 struct uvc_control_mapping
*mapping
;
1150 struct uvc_control
*ctrl
;
1151 u32 index
= query_menu
->index
;
1152 u32 id
= query_menu
->id
;
1155 memset(query_menu
, 0, sizeof(*query_menu
));
1156 query_menu
->id
= id
;
1157 query_menu
->index
= index
;
1159 ret
= mutex_lock_interruptible(&chain
->ctrl_mutex
);
1161 return -ERESTARTSYS
;
1163 ctrl
= uvc_find_control(chain
, query_menu
->id
, &mapping
);
1164 if (ctrl
== NULL
|| mapping
->v4l2_type
!= V4L2_CTRL_TYPE_MENU
) {
1169 if (query_menu
->index
>= mapping
->menu_count
) {
1174 menu_info
= &mapping
->menu_info
[query_menu
->index
];
1176 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_BITMASK
&&
1177 (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)) {
1180 if (!ctrl
->cached
) {
1181 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1186 bitmap
= mapping
->get(mapping
, UVC_GET_RES
,
1187 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1188 if (!(bitmap
& menu_info
->value
)) {
1194 strscpy(query_menu
->name
, menu_info
->name
, sizeof(query_menu
->name
));
1197 mutex_unlock(&chain
->ctrl_mutex
);
1201 /* --------------------------------------------------------------------------
1202 * Ctrl event handling
1205 static void uvc_ctrl_fill_event(struct uvc_video_chain
*chain
,
1206 struct v4l2_event
*ev
,
1207 struct uvc_control
*ctrl
,
1208 struct uvc_control_mapping
*mapping
,
1209 s32 value
, u32 changes
)
1211 struct v4l2_queryctrl v4l2_ctrl
;
1213 __uvc_query_v4l2_ctrl(chain
, ctrl
, mapping
, &v4l2_ctrl
);
1215 memset(ev
->reserved
, 0, sizeof(ev
->reserved
));
1216 ev
->type
= V4L2_EVENT_CTRL
;
1217 ev
->id
= v4l2_ctrl
.id
;
1218 ev
->u
.ctrl
.value
= value
;
1219 ev
->u
.ctrl
.changes
= changes
;
1220 ev
->u
.ctrl
.type
= v4l2_ctrl
.type
;
1221 ev
->u
.ctrl
.flags
= v4l2_ctrl
.flags
;
1222 ev
->u
.ctrl
.minimum
= v4l2_ctrl
.minimum
;
1223 ev
->u
.ctrl
.maximum
= v4l2_ctrl
.maximum
;
1224 ev
->u
.ctrl
.step
= v4l2_ctrl
.step
;
1225 ev
->u
.ctrl
.default_value
= v4l2_ctrl
.default_value
;
1229 * Send control change events to all subscribers for the @ctrl control. By
1230 * default the subscriber that generated the event, as identified by @handle,
1231 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1232 * @handle can be NULL for asynchronous events related to auto-update controls,
1233 * in which case all subscribers are notified.
1235 static void uvc_ctrl_send_event(struct uvc_video_chain
*chain
,
1236 struct uvc_fh
*handle
, struct uvc_control
*ctrl
,
1237 struct uvc_control_mapping
*mapping
, s32 value
, u32 changes
)
1239 struct v4l2_fh
*originator
= handle
? &handle
->vfh
: NULL
;
1240 struct v4l2_subscribed_event
*sev
;
1241 struct v4l2_event ev
;
1243 if (list_empty(&mapping
->ev_subs
))
1246 uvc_ctrl_fill_event(chain
, &ev
, ctrl
, mapping
, value
, changes
);
1248 list_for_each_entry(sev
, &mapping
->ev_subs
, node
) {
1249 if (sev
->fh
!= originator
||
1250 (sev
->flags
& V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK
) ||
1251 (changes
& V4L2_EVENT_CTRL_CH_FLAGS
))
1252 v4l2_event_queue_fh(sev
->fh
, &ev
);
1257 * Send control change events for the slave of the @master control identified
1258 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1259 * generated the event and may be NULL for auto-update events.
1261 static void uvc_ctrl_send_slave_event(struct uvc_video_chain
*chain
,
1262 struct uvc_fh
*handle
, struct uvc_control
*master
, u32 slave_id
)
1264 struct uvc_control_mapping
*mapping
= NULL
;
1265 struct uvc_control
*ctrl
= NULL
;
1266 u32 changes
= V4L2_EVENT_CTRL_CH_FLAGS
;
1269 __uvc_find_control(master
->entity
, slave_id
, &mapping
, &ctrl
, 0);
1273 if (__uvc_ctrl_get(chain
, ctrl
, mapping
, &val
) == 0)
1274 changes
|= V4L2_EVENT_CTRL_CH_VALUE
;
1276 uvc_ctrl_send_event(chain
, handle
, ctrl
, mapping
, val
, changes
);
1279 static void uvc_ctrl_status_event_work(struct work_struct
*work
)
1281 struct uvc_device
*dev
= container_of(work
, struct uvc_device
,
1283 struct uvc_ctrl_work
*w
= &dev
->async_ctrl
;
1284 struct uvc_video_chain
*chain
= w
->chain
;
1285 struct uvc_control_mapping
*mapping
;
1286 struct uvc_control
*ctrl
= w
->ctrl
;
1287 struct uvc_fh
*handle
;
1291 mutex_lock(&chain
->ctrl_mutex
);
1293 handle
= ctrl
->handle
;
1294 ctrl
->handle
= NULL
;
1296 list_for_each_entry(mapping
, &ctrl
->info
.mappings
, list
) {
1297 s32 value
= __uvc_ctrl_get_value(mapping
, w
->data
);
1300 * handle may be NULL here if the device sends auto-update
1301 * events without a prior related control set from userspace.
1303 for (i
= 0; i
< ARRAY_SIZE(mapping
->slave_ids
); ++i
) {
1304 if (!mapping
->slave_ids
[i
])
1307 uvc_ctrl_send_slave_event(chain
, handle
, ctrl
,
1308 mapping
->slave_ids
[i
]);
1311 uvc_ctrl_send_event(chain
, handle
, ctrl
, mapping
, value
,
1312 V4L2_EVENT_CTRL_CH_VALUE
);
1315 mutex_unlock(&chain
->ctrl_mutex
);
1317 /* Resubmit the URB. */
1318 w
->urb
->interval
= dev
->int_ep
->desc
.bInterval
;
1319 ret
= usb_submit_urb(w
->urb
, GFP_KERNEL
);
1321 uvc_printk(KERN_ERR
, "Failed to resubmit status URB (%d).\n",
1325 bool uvc_ctrl_status_event(struct urb
*urb
, struct uvc_video_chain
*chain
,
1326 struct uvc_control
*ctrl
, const u8
*data
)
1328 struct uvc_device
*dev
= chain
->dev
;
1329 struct uvc_ctrl_work
*w
= &dev
->async_ctrl
;
1331 if (list_empty(&ctrl
->info
.mappings
)) {
1332 ctrl
->handle
= NULL
;
1341 schedule_work(&w
->work
);
1346 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control
*xctrls
,
1347 unsigned int xctrls_count
, u32 id
)
1351 for (i
= 0; i
< xctrls_count
; ++i
) {
1352 if (xctrls
[i
].id
== id
)
1359 static void uvc_ctrl_send_events(struct uvc_fh
*handle
,
1360 const struct v4l2_ext_control
*xctrls
, unsigned int xctrls_count
)
1362 struct uvc_control_mapping
*mapping
;
1363 struct uvc_control
*ctrl
;
1364 u32 changes
= V4L2_EVENT_CTRL_CH_VALUE
;
1368 for (i
= 0; i
< xctrls_count
; ++i
) {
1369 ctrl
= uvc_find_control(handle
->chain
, xctrls
[i
].id
, &mapping
);
1371 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_ASYNCHRONOUS
)
1372 /* Notification will be sent from an Interrupt event. */
1375 for (j
= 0; j
< ARRAY_SIZE(mapping
->slave_ids
); ++j
) {
1376 u32 slave_id
= mapping
->slave_ids
[j
];
1382 * We can skip sending an event for the slave if the
1383 * slave is being modified in the same transaction.
1385 if (uvc_ctrl_xctrls_has_control(xctrls
, xctrls_count
,
1389 uvc_ctrl_send_slave_event(handle
->chain
, handle
, ctrl
,
1394 * If the master is being modified in the same transaction
1395 * flags may change too.
1397 if (mapping
->master_id
&&
1398 uvc_ctrl_xctrls_has_control(xctrls
, xctrls_count
,
1399 mapping
->master_id
))
1400 changes
|= V4L2_EVENT_CTRL_CH_FLAGS
;
1402 uvc_ctrl_send_event(handle
->chain
, handle
, ctrl
, mapping
,
1403 xctrls
[i
].value
, changes
);
1407 static int uvc_ctrl_add_event(struct v4l2_subscribed_event
*sev
, unsigned elems
)
1409 struct uvc_fh
*handle
= container_of(sev
->fh
, struct uvc_fh
, vfh
);
1410 struct uvc_control_mapping
*mapping
;
1411 struct uvc_control
*ctrl
;
1414 ret
= mutex_lock_interruptible(&handle
->chain
->ctrl_mutex
);
1416 return -ERESTARTSYS
;
1418 ctrl
= uvc_find_control(handle
->chain
, sev
->id
, &mapping
);
1424 list_add_tail(&sev
->node
, &mapping
->ev_subs
);
1425 if (sev
->flags
& V4L2_EVENT_SUB_FL_SEND_INITIAL
) {
1426 struct v4l2_event ev
;
1427 u32 changes
= V4L2_EVENT_CTRL_CH_FLAGS
;
1430 if (__uvc_ctrl_get(handle
->chain
, ctrl
, mapping
, &val
) == 0)
1431 changes
|= V4L2_EVENT_CTRL_CH_VALUE
;
1433 uvc_ctrl_fill_event(handle
->chain
, &ev
, ctrl
, mapping
, val
,
1435 /* Mark the queue as active, allowing this initial
1436 event to be accepted. */
1438 v4l2_event_queue_fh(sev
->fh
, &ev
);
1442 mutex_unlock(&handle
->chain
->ctrl_mutex
);
1446 static void uvc_ctrl_del_event(struct v4l2_subscribed_event
*sev
)
1448 struct uvc_fh
*handle
= container_of(sev
->fh
, struct uvc_fh
, vfh
);
1450 mutex_lock(&handle
->chain
->ctrl_mutex
);
1451 list_del(&sev
->node
);
1452 mutex_unlock(&handle
->chain
->ctrl_mutex
);
1455 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops
= {
1456 .add
= uvc_ctrl_add_event
,
1457 .del
= uvc_ctrl_del_event
,
1458 .replace
= v4l2_ctrl_replace
,
1459 .merge
= v4l2_ctrl_merge
,
1462 /* --------------------------------------------------------------------------
1463 * Control transactions
1465 * To make extended set operations as atomic as the hardware allows, controls
1466 * are handled using begin/commit/rollback operations.
1468 * At the beginning of a set request, uvc_ctrl_begin should be called to
1469 * initialize the request. This function acquires the control lock.
1471 * When setting a control, the new value is stored in the control data field
1472 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1473 * later processing. If the UVC and V4L2 control sizes differ, the current
1474 * value is loaded from the hardware before storing the new value in the data
1477 * After processing all controls in the transaction, uvc_ctrl_commit or
1478 * uvc_ctrl_rollback must be called to apply the pending changes to the
1479 * hardware or revert them. When applying changes, all controls marked as
1480 * dirty will be modified in the UVC device, and the dirty flag will be
1481 * cleared. When reverting controls, the control data field
1482 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1483 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1486 int uvc_ctrl_begin(struct uvc_video_chain
*chain
)
1488 return mutex_lock_interruptible(&chain
->ctrl_mutex
) ? -ERESTARTSYS
: 0;
1491 static int uvc_ctrl_commit_entity(struct uvc_device
*dev
,
1492 struct uvc_entity
*entity
, int rollback
)
1494 struct uvc_control
*ctrl
;
1501 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1502 ctrl
= &entity
->controls
[i
];
1503 if (!ctrl
->initialized
)
1506 /* Reset the loaded flag for auto-update controls that were
1507 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1508 * uvc_ctrl_get from using the cached value, and for write-only
1509 * controls to prevent uvc_ctrl_set from setting bits not
1510 * explicitly set by the user.
1512 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_AUTO_UPDATE
||
1513 !(ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
))
1520 ret
= uvc_query_ctrl(dev
, UVC_SET_CUR
, ctrl
->entity
->id
,
1521 dev
->intfnum
, ctrl
->info
.selector
,
1522 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1527 if (rollback
|| ret
< 0)
1528 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1529 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1541 int __uvc_ctrl_commit(struct uvc_fh
*handle
, int rollback
,
1542 const struct v4l2_ext_control
*xctrls
,
1543 unsigned int xctrls_count
)
1545 struct uvc_video_chain
*chain
= handle
->chain
;
1546 struct uvc_entity
*entity
;
1549 /* Find the control. */
1550 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1551 ret
= uvc_ctrl_commit_entity(chain
->dev
, entity
, rollback
);
1557 uvc_ctrl_send_events(handle
, xctrls
, xctrls_count
);
1559 mutex_unlock(&chain
->ctrl_mutex
);
1563 int uvc_ctrl_get(struct uvc_video_chain
*chain
,
1564 struct v4l2_ext_control
*xctrl
)
1566 struct uvc_control
*ctrl
;
1567 struct uvc_control_mapping
*mapping
;
1569 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1573 return __uvc_ctrl_get(chain
, ctrl
, mapping
, &xctrl
->value
);
1576 int uvc_ctrl_set(struct uvc_fh
*handle
,
1577 struct v4l2_ext_control
*xctrl
)
1579 struct uvc_video_chain
*chain
= handle
->chain
;
1580 struct uvc_control
*ctrl
;
1581 struct uvc_control_mapping
*mapping
;
1588 ctrl
= uvc_find_control(chain
, xctrl
->id
, &mapping
);
1591 if (!(ctrl
->info
.flags
& UVC_CTRL_FLAG_SET_CUR
))
1594 /* Clamp out of range values. */
1595 switch (mapping
->v4l2_type
) {
1596 case V4L2_CTRL_TYPE_INTEGER
:
1597 if (!ctrl
->cached
) {
1598 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1603 min
= mapping
->get(mapping
, UVC_GET_MIN
,
1604 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MIN
));
1605 max
= mapping
->get(mapping
, UVC_GET_MAX
,
1606 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_MAX
));
1607 step
= mapping
->get(mapping
, UVC_GET_RES
,
1608 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1612 xctrl
->value
= min
+ ((u32
)(xctrl
->value
- min
) + step
/ 2)
1614 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_SIGNED
)
1615 xctrl
->value
= clamp(xctrl
->value
, min
, max
);
1617 xctrl
->value
= clamp_t(u32
, xctrl
->value
, min
, max
);
1618 value
= xctrl
->value
;
1621 case V4L2_CTRL_TYPE_BOOLEAN
:
1622 xctrl
->value
= clamp(xctrl
->value
, 0, 1);
1623 value
= xctrl
->value
;
1626 case V4L2_CTRL_TYPE_MENU
:
1627 if (xctrl
->value
< 0 || xctrl
->value
>= mapping
->menu_count
)
1629 value
= mapping
->menu_info
[xctrl
->value
].value
;
1631 /* Valid menu indices are reported by the GET_RES request for
1632 * UVC controls that support it.
1634 if (mapping
->data_type
== UVC_CTRL_DATA_TYPE_BITMASK
&&
1635 (ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_RES
)) {
1636 if (!ctrl
->cached
) {
1637 ret
= uvc_ctrl_populate_cache(chain
, ctrl
);
1642 step
= mapping
->get(mapping
, UVC_GET_RES
,
1643 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_RES
));
1644 if (!(step
& value
))
1651 value
= xctrl
->value
;
1655 /* If the mapping doesn't span the whole UVC control, the current value
1656 * needs to be loaded from the device to perform the read-modify-write
1659 if (!ctrl
->loaded
&& (ctrl
->info
.size
* 8) != mapping
->size
) {
1660 if ((ctrl
->info
.flags
& UVC_CTRL_FLAG_GET_CUR
) == 0) {
1661 memset(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1662 0, ctrl
->info
.size
);
1664 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
1665 ctrl
->entity
->id
, chain
->dev
->intfnum
,
1666 ctrl
->info
.selector
,
1667 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1676 /* Backup the current value in case we need to rollback later. */
1678 memcpy(uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_BACKUP
),
1679 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
),
1683 mapping
->set(mapping
, value
,
1684 uvc_ctrl_data(ctrl
, UVC_CTRL_DATA_CURRENT
));
1686 if (ctrl
->info
.flags
& UVC_CTRL_FLAG_ASYNCHRONOUS
)
1687 ctrl
->handle
= handle
;
1694 /* --------------------------------------------------------------------------
1699 * Retrieve flags for a given control
1701 static int uvc_ctrl_get_flags(struct uvc_device
*dev
,
1702 const struct uvc_control
*ctrl
,
1703 struct uvc_control_info
*info
)
1708 data
= kmalloc(1, GFP_KERNEL
);
1712 ret
= uvc_query_ctrl(dev
, UVC_GET_INFO
, ctrl
->entity
->id
, dev
->intfnum
,
1713 info
->selector
, data
, 1);
1715 info
->flags
|= (data
[0] & UVC_CONTROL_CAP_GET
?
1716 UVC_CTRL_FLAG_GET_CUR
: 0)
1717 | (data
[0] & UVC_CONTROL_CAP_SET
?
1718 UVC_CTRL_FLAG_SET_CUR
: 0)
1719 | (data
[0] & UVC_CONTROL_CAP_AUTOUPDATE
?
1720 UVC_CTRL_FLAG_AUTO_UPDATE
: 0)
1721 | (data
[0] & UVC_CONTROL_CAP_ASYNCHRONOUS
?
1722 UVC_CTRL_FLAG_ASYNCHRONOUS
: 0);
1728 static void uvc_ctrl_fixup_xu_info(struct uvc_device
*dev
,
1729 const struct uvc_control
*ctrl
, struct uvc_control_info
*info
)
1731 struct uvc_ctrl_fixup
{
1732 struct usb_device_id id
;
1738 static const struct uvc_ctrl_fixup fixups
[] = {
1739 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1740 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
1741 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
1742 UVC_CTRL_FLAG_AUTO_UPDATE
},
1743 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1744 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
1745 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
1746 UVC_CTRL_FLAG_AUTO_UPDATE
},
1747 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1748 UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
|
1749 UVC_CTRL_FLAG_GET_DEF
| UVC_CTRL_FLAG_SET_CUR
|
1750 UVC_CTRL_FLAG_AUTO_UPDATE
},
1755 for (i
= 0; i
< ARRAY_SIZE(fixups
); ++i
) {
1756 if (!usb_match_one_id(dev
->intf
, &fixups
[i
].id
))
1759 if (fixups
[i
].entity
== ctrl
->entity
->id
&&
1760 fixups
[i
].selector
== info
->selector
) {
1761 info
->flags
= fixups
[i
].flags
;
1768 * Query control information (size and flags) for XU controls.
1770 static int uvc_ctrl_fill_xu_info(struct uvc_device
*dev
,
1771 const struct uvc_control
*ctrl
, struct uvc_control_info
*info
)
1776 data
= kmalloc(2, GFP_KERNEL
);
1780 memcpy(info
->entity
, ctrl
->entity
->extension
.guidExtensionCode
,
1781 sizeof(info
->entity
));
1782 info
->index
= ctrl
->index
;
1783 info
->selector
= ctrl
->index
+ 1;
1785 /* Query and verify the control length (GET_LEN) */
1786 ret
= uvc_query_ctrl(dev
, UVC_GET_LEN
, ctrl
->entity
->id
, dev
->intfnum
,
1787 info
->selector
, data
, 2);
1789 uvc_trace(UVC_TRACE_CONTROL
,
1790 "GET_LEN failed on control %pUl/%u (%d).\n",
1791 info
->entity
, info
->selector
, ret
);
1795 info
->size
= le16_to_cpup((__le16
*)data
);
1797 info
->flags
= UVC_CTRL_FLAG_GET_MIN
| UVC_CTRL_FLAG_GET_MAX
1798 | UVC_CTRL_FLAG_GET_RES
| UVC_CTRL_FLAG_GET_DEF
;
1800 ret
= uvc_ctrl_get_flags(dev
, ctrl
, info
);
1802 uvc_trace(UVC_TRACE_CONTROL
,
1803 "Failed to get flags for control %pUl/%u (%d).\n",
1804 info
->entity
, info
->selector
, ret
);
1808 uvc_ctrl_fixup_xu_info(dev
, ctrl
, info
);
1810 uvc_trace(UVC_TRACE_CONTROL
, "XU control %pUl/%u queried: len %u, "
1811 "flags { get %u set %u auto %u }.\n",
1812 info
->entity
, info
->selector
, info
->size
,
1813 (info
->flags
& UVC_CTRL_FLAG_GET_CUR
) ? 1 : 0,
1814 (info
->flags
& UVC_CTRL_FLAG_SET_CUR
) ? 1 : 0,
1815 (info
->flags
& UVC_CTRL_FLAG_AUTO_UPDATE
) ? 1 : 0);
1822 static int uvc_ctrl_add_info(struct uvc_device
*dev
, struct uvc_control
*ctrl
,
1823 const struct uvc_control_info
*info
);
1825 static int uvc_ctrl_init_xu_ctrl(struct uvc_device
*dev
,
1826 struct uvc_control
*ctrl
)
1828 struct uvc_control_info info
;
1831 if (ctrl
->initialized
)
1834 ret
= uvc_ctrl_fill_xu_info(dev
, ctrl
, &info
);
1838 ret
= uvc_ctrl_add_info(dev
, ctrl
, &info
);
1840 uvc_trace(UVC_TRACE_CONTROL
, "Failed to initialize control "
1841 "%pUl/%u on device %s entity %u\n", info
.entity
,
1842 info
.selector
, dev
->udev
->devpath
, ctrl
->entity
->id
);
1847 int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
1848 struct uvc_xu_control_query
*xqry
)
1850 struct uvc_entity
*entity
;
1851 struct uvc_control
*ctrl
;
1852 unsigned int i
, found
= 0;
1858 /* Find the extension unit. */
1859 list_for_each_entry(entity
, &chain
->entities
, chain
) {
1860 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
&&
1861 entity
->id
== xqry
->unit
)
1865 if (entity
->id
!= xqry
->unit
) {
1866 uvc_trace(UVC_TRACE_CONTROL
, "Extension unit %u not found.\n",
1871 /* Find the control and perform delayed initialization if needed. */
1872 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1873 ctrl
= &entity
->controls
[i
];
1874 if (ctrl
->index
== xqry
->selector
- 1) {
1881 uvc_trace(UVC_TRACE_CONTROL
, "Control %pUl/%u not found.\n",
1882 entity
->extension
.guidExtensionCode
, xqry
->selector
);
1886 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
1887 return -ERESTARTSYS
;
1889 ret
= uvc_ctrl_init_xu_ctrl(chain
->dev
, ctrl
);
1895 /* Validate the required buffer size and flags for the request */
1897 size
= ctrl
->info
.size
;
1899 switch (xqry
->query
) {
1901 reqflags
= UVC_CTRL_FLAG_GET_CUR
;
1904 reqflags
= UVC_CTRL_FLAG_GET_MIN
;
1907 reqflags
= UVC_CTRL_FLAG_GET_MAX
;
1910 reqflags
= UVC_CTRL_FLAG_GET_DEF
;
1913 reqflags
= UVC_CTRL_FLAG_GET_RES
;
1916 reqflags
= UVC_CTRL_FLAG_SET_CUR
;
1929 if (size
!= xqry
->size
) {
1934 if (reqflags
&& !(ctrl
->info
.flags
& reqflags
)) {
1939 data
= kmalloc(size
, GFP_KERNEL
);
1945 if (xqry
->query
== UVC_SET_CUR
&&
1946 copy_from_user(data
, xqry
->data
, size
)) {
1951 ret
= uvc_query_ctrl(chain
->dev
, xqry
->query
, xqry
->unit
,
1952 chain
->dev
->intfnum
, xqry
->selector
, data
, size
);
1956 if (xqry
->query
!= UVC_SET_CUR
&&
1957 copy_to_user(xqry
->data
, data
, size
))
1961 mutex_unlock(&chain
->ctrl_mutex
);
1965 /* --------------------------------------------------------------------------
1970 * Restore control values after resume, skipping controls that haven't been
1974 * - Don't restore modified controls that are back to their default value.
1975 * - Handle restore order (Auto-Exposure Mode should be restored before
1978 int uvc_ctrl_restore_values(struct uvc_device
*dev
)
1980 struct uvc_control
*ctrl
;
1981 struct uvc_entity
*entity
;
1985 /* Walk the entities list and restore controls when possible. */
1986 list_for_each_entry(entity
, &dev
->entities
, list
) {
1988 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
1989 ctrl
= &entity
->controls
[i
];
1991 if (!ctrl
->initialized
|| !ctrl
->modified
||
1992 (ctrl
->info
.flags
& UVC_CTRL_FLAG_RESTORE
) == 0)
1995 printk(KERN_INFO
"restoring control %pUl/%u/%u\n",
1996 ctrl
->info
.entity
, ctrl
->info
.index
,
1997 ctrl
->info
.selector
);
2001 ret
= uvc_ctrl_commit_entity(dev
, entity
, 0);
2009 /* --------------------------------------------------------------------------
2010 * Control and mapping handling
2014 * Add control information to a given control.
2016 static int uvc_ctrl_add_info(struct uvc_device
*dev
, struct uvc_control
*ctrl
,
2017 const struct uvc_control_info
*info
)
2022 INIT_LIST_HEAD(&ctrl
->info
.mappings
);
2024 /* Allocate an array to save control values (cur, def, max, etc.) */
2025 ctrl
->uvc_data
= kzalloc(ctrl
->info
.size
* UVC_CTRL_DATA_LAST
+ 1,
2027 if (ctrl
->uvc_data
== NULL
) {
2033 * Retrieve control flags from the device. Ignore errors and work with
2034 * default flag values from the uvc_ctrl array when the device doesn't
2035 * properly implement GET_INFO on standard controls.
2037 uvc_ctrl_get_flags(dev
, ctrl
, &ctrl
->info
);
2039 ctrl
->initialized
= 1;
2041 uvc_trace(UVC_TRACE_CONTROL
, "Added control %pUl/%u to device %s "
2042 "entity %u\n", ctrl
->info
.entity
, ctrl
->info
.selector
,
2043 dev
->udev
->devpath
, ctrl
->entity
->id
);
2047 kfree(ctrl
->uvc_data
);
2052 * Add a control mapping to a given control.
2054 static int __uvc_ctrl_add_mapping(struct uvc_device
*dev
,
2055 struct uvc_control
*ctrl
, const struct uvc_control_mapping
*mapping
)
2057 struct uvc_control_mapping
*map
;
2060 /* Most mappings come from static kernel data and need to be duplicated.
2061 * Mappings that come from userspace will be unnecessarily duplicated,
2062 * this could be optimized.
2064 map
= kmemdup(mapping
, sizeof(*mapping
), GFP_KERNEL
);
2068 INIT_LIST_HEAD(&map
->ev_subs
);
2070 size
= sizeof(*mapping
->menu_info
) * mapping
->menu_count
;
2071 map
->menu_info
= kmemdup(mapping
->menu_info
, size
, GFP_KERNEL
);
2072 if (map
->menu_info
== NULL
) {
2077 if (map
->get
== NULL
)
2078 map
->get
= uvc_get_le_value
;
2079 if (map
->set
== NULL
)
2080 map
->set
= uvc_set_le_value
;
2082 list_add_tail(&map
->list
, &ctrl
->info
.mappings
);
2083 uvc_trace(UVC_TRACE_CONTROL
,
2084 "Adding mapping '%s' to control %pUl/%u.\n",
2085 map
->name
, ctrl
->info
.entity
, ctrl
->info
.selector
);
2090 int uvc_ctrl_add_mapping(struct uvc_video_chain
*chain
,
2091 const struct uvc_control_mapping
*mapping
)
2093 struct uvc_device
*dev
= chain
->dev
;
2094 struct uvc_control_mapping
*map
;
2095 struct uvc_entity
*entity
;
2096 struct uvc_control
*ctrl
;
2100 if (mapping
->id
& ~V4L2_CTRL_ID_MASK
) {
2101 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', control "
2102 "id 0x%08x is invalid.\n", mapping
->name
,
2107 /* Search for the matching (GUID/CS) control on the current chain */
2108 list_for_each_entry(entity
, &chain
->entities
, chain
) {
2111 if (UVC_ENTITY_TYPE(entity
) != UVC_VC_EXTENSION_UNIT
||
2112 !uvc_entity_match_guid(entity
, mapping
->entity
))
2115 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
2116 ctrl
= &entity
->controls
[i
];
2117 if (ctrl
->index
== mapping
->selector
- 1) {
2129 if (mutex_lock_interruptible(&chain
->ctrl_mutex
))
2130 return -ERESTARTSYS
;
2132 /* Perform delayed initialization of XU controls */
2133 ret
= uvc_ctrl_init_xu_ctrl(dev
, ctrl
);
2139 /* Validate the user-provided bit-size and offset */
2140 if (mapping
->size
> 32 ||
2141 mapping
->offset
+ mapping
->size
> ctrl
->info
.size
* 8) {
2146 list_for_each_entry(map
, &ctrl
->info
.mappings
, list
) {
2147 if (mapping
->id
== map
->id
) {
2148 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', "
2149 "control id 0x%08x already exists.\n",
2150 mapping
->name
, mapping
->id
);
2156 /* Prevent excess memory consumption */
2157 if (atomic_inc_return(&dev
->nmappings
) > UVC_MAX_CONTROL_MAPPINGS
) {
2158 atomic_dec(&dev
->nmappings
);
2159 uvc_trace(UVC_TRACE_CONTROL
, "Can't add mapping '%s', maximum "
2160 "mappings count (%u) exceeded.\n", mapping
->name
,
2161 UVC_MAX_CONTROL_MAPPINGS
);
2166 ret
= __uvc_ctrl_add_mapping(dev
, ctrl
, mapping
);
2168 atomic_dec(&dev
->nmappings
);
2171 mutex_unlock(&chain
->ctrl_mutex
);
2176 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2177 * are currently the ones that crash the camera or unconditionally return an
2178 * error when queried.
2180 static void uvc_ctrl_prune_entity(struct uvc_device
*dev
,
2181 struct uvc_entity
*entity
)
2183 struct uvc_ctrl_blacklist
{
2184 struct usb_device_id id
;
2188 static const struct uvc_ctrl_blacklist processing_blacklist
[] = {
2189 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2190 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2191 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2193 static const struct uvc_ctrl_blacklist camera_blacklist
[] = {
2194 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2197 const struct uvc_ctrl_blacklist
*blacklist
;
2203 switch (UVC_ENTITY_TYPE(entity
)) {
2204 case UVC_VC_PROCESSING_UNIT
:
2205 blacklist
= processing_blacklist
;
2206 count
= ARRAY_SIZE(processing_blacklist
);
2207 controls
= entity
->processing
.bmControls
;
2208 size
= entity
->processing
.bControlSize
;
2211 case UVC_ITT_CAMERA
:
2212 blacklist
= camera_blacklist
;
2213 count
= ARRAY_SIZE(camera_blacklist
);
2214 controls
= entity
->camera
.bmControls
;
2215 size
= entity
->camera
.bControlSize
;
2222 for (i
= 0; i
< count
; ++i
) {
2223 if (!usb_match_one_id(dev
->intf
, &blacklist
[i
].id
))
2226 if (blacklist
[i
].index
>= 8 * size
||
2227 !uvc_test_bit(controls
, blacklist
[i
].index
))
2230 uvc_trace(UVC_TRACE_CONTROL
, "%u/%u control is black listed, "
2231 "removing it.\n", entity
->id
, blacklist
[i
].index
);
2233 uvc_clear_bit(controls
, blacklist
[i
].index
);
2238 * Add control information and hardcoded stock control mappings to the given
2241 static void uvc_ctrl_init_ctrl(struct uvc_device
*dev
, struct uvc_control
*ctrl
)
2243 const struct uvc_control_info
*info
= uvc_ctrls
;
2244 const struct uvc_control_info
*iend
= info
+ ARRAY_SIZE(uvc_ctrls
);
2245 const struct uvc_control_mapping
*mapping
= uvc_ctrl_mappings
;
2246 const struct uvc_control_mapping
*mend
=
2247 mapping
+ ARRAY_SIZE(uvc_ctrl_mappings
);
2249 /* XU controls initialization requires querying the device for control
2250 * information. As some buggy UVC devices will crash when queried
2251 * repeatedly in a tight loop, delay XU controls initialization until
2254 if (UVC_ENTITY_TYPE(ctrl
->entity
) == UVC_VC_EXTENSION_UNIT
)
2257 for (; info
< iend
; ++info
) {
2258 if (uvc_entity_match_guid(ctrl
->entity
, info
->entity
) &&
2259 ctrl
->index
== info
->index
) {
2260 uvc_ctrl_add_info(dev
, ctrl
, info
);
2265 if (!ctrl
->initialized
)
2268 for (; mapping
< mend
; ++mapping
) {
2269 if (uvc_entity_match_guid(ctrl
->entity
, mapping
->entity
) &&
2270 ctrl
->info
.selector
== mapping
->selector
)
2271 __uvc_ctrl_add_mapping(dev
, ctrl
, mapping
);
2276 * Initialize device controls.
2278 int uvc_ctrl_init_device(struct uvc_device
*dev
)
2280 struct uvc_entity
*entity
;
2283 INIT_WORK(&dev
->async_ctrl
.work
, uvc_ctrl_status_event_work
);
2285 /* Walk the entities list and instantiate controls */
2286 list_for_each_entry(entity
, &dev
->entities
, list
) {
2287 struct uvc_control
*ctrl
;
2288 unsigned int bControlSize
= 0, ncontrols
;
2289 u8
*bmControls
= NULL
;
2291 if (UVC_ENTITY_TYPE(entity
) == UVC_VC_EXTENSION_UNIT
) {
2292 bmControls
= entity
->extension
.bmControls
;
2293 bControlSize
= entity
->extension
.bControlSize
;
2294 } else if (UVC_ENTITY_TYPE(entity
) == UVC_VC_PROCESSING_UNIT
) {
2295 bmControls
= entity
->processing
.bmControls
;
2296 bControlSize
= entity
->processing
.bControlSize
;
2297 } else if (UVC_ENTITY_TYPE(entity
) == UVC_ITT_CAMERA
) {
2298 bmControls
= entity
->camera
.bmControls
;
2299 bControlSize
= entity
->camera
.bControlSize
;
2302 /* Remove bogus/blacklisted controls */
2303 uvc_ctrl_prune_entity(dev
, entity
);
2305 /* Count supported controls and allocate the controls array */
2306 ncontrols
= memweight(bmControls
, bControlSize
);
2310 entity
->controls
= kcalloc(ncontrols
, sizeof(*ctrl
),
2312 if (entity
->controls
== NULL
)
2314 entity
->ncontrols
= ncontrols
;
2316 /* Initialize all supported controls */
2317 ctrl
= entity
->controls
;
2318 for (i
= 0; i
< bControlSize
* 8; ++i
) {
2319 if (uvc_test_bit(bmControls
, i
) == 0)
2322 ctrl
->entity
= entity
;
2325 uvc_ctrl_init_ctrl(dev
, ctrl
);
2334 * Cleanup device controls.
2336 static void uvc_ctrl_cleanup_mappings(struct uvc_device
*dev
,
2337 struct uvc_control
*ctrl
)
2339 struct uvc_control_mapping
*mapping
, *nm
;
2341 list_for_each_entry_safe(mapping
, nm
, &ctrl
->info
.mappings
, list
) {
2342 list_del(&mapping
->list
);
2343 kfree(mapping
->menu_info
);
2348 void uvc_ctrl_cleanup_device(struct uvc_device
*dev
)
2350 struct uvc_entity
*entity
;
2353 cancel_work_sync(&dev
->async_ctrl
.work
);
2355 /* Free controls and control mappings for all entities. */
2356 list_for_each_entry(entity
, &dev
->entities
, list
) {
2357 for (i
= 0; i
< entity
->ncontrols
; ++i
) {
2358 struct uvc_control
*ctrl
= &entity
->controls
[i
];
2360 if (!ctrl
->initialized
)
2363 uvc_ctrl_cleanup_mappings(dev
, ctrl
);
2364 kfree(ctrl
->uvc_data
);
2367 kfree(entity
->controls
);