1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * uvc_ctrl.c -- USB Video Class driver - Controls
4 *
5 * Copyright (C) 2005-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
8
9 #include <asm/barrier.h>
10 #include <linux/bitops.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/usb.h>
17 #include <linux/usb/uvc.h>
18 #include <linux/videodev2.h>
19 #include <linux/vmalloc.h>
20 #include <linux/wait.h>
21 #include <linux/workqueue.h>
22 #include <linux/atomic.h>
23 #include <media/v4l2-ctrls.h>
24
25 #include "uvcvideo.h"
26
27 #define UVC_CTRL_DATA_CURRENT 0
28 #define UVC_CTRL_DATA_BACKUP 1
29 #define UVC_CTRL_DATA_MIN 2
30 #define UVC_CTRL_DATA_MAX 3
31 #define UVC_CTRL_DATA_RES 4
32 #define UVC_CTRL_DATA_DEF 5
33 #define UVC_CTRL_DATA_LAST 6
34
35 /* ------------------------------------------------------------------------
36 * Controls
37 */
38
39 static const struct uvc_control_info uvc_ctrls[] = {
40 {
41 .entity = UVC_GUID_UVC_PROCESSING,
42 .selector = UVC_PU_BRIGHTNESS_CONTROL,
43 .index = 0,
44 .size = 2,
45 .flags = UVC_CTRL_FLAG_SET_CUR
46 | UVC_CTRL_FLAG_GET_RANGE
47 | UVC_CTRL_FLAG_RESTORE,
48 },
49 {
50 .entity = UVC_GUID_UVC_PROCESSING,
51 .selector = UVC_PU_CONTRAST_CONTROL,
52 .index = 1,
53 .size = 2,
54 .flags = UVC_CTRL_FLAG_SET_CUR
55 | UVC_CTRL_FLAG_GET_RANGE
56 | UVC_CTRL_FLAG_RESTORE,
57 },
58 {
59 .entity = UVC_GUID_UVC_PROCESSING,
60 .selector = UVC_PU_HUE_CONTROL,
61 .index = 2,
62 .size = 2,
63 .flags = UVC_CTRL_FLAG_SET_CUR
64 | UVC_CTRL_FLAG_GET_RANGE
65 | UVC_CTRL_FLAG_RESTORE
66 | UVC_CTRL_FLAG_AUTO_UPDATE,
67 },
68 {
69 .entity = UVC_GUID_UVC_PROCESSING,
70 .selector = UVC_PU_SATURATION_CONTROL,
71 .index = 3,
72 .size = 2,
73 .flags = UVC_CTRL_FLAG_SET_CUR
74 | UVC_CTRL_FLAG_GET_RANGE
75 | UVC_CTRL_FLAG_RESTORE,
76 },
77 {
78 .entity = UVC_GUID_UVC_PROCESSING,
79 .selector = UVC_PU_SHARPNESS_CONTROL,
80 .index = 4,
81 .size = 2,
82 .flags = UVC_CTRL_FLAG_SET_CUR
83 | UVC_CTRL_FLAG_GET_RANGE
84 | UVC_CTRL_FLAG_RESTORE,
85 },
86 {
87 .entity = UVC_GUID_UVC_PROCESSING,
88 .selector = UVC_PU_GAMMA_CONTROL,
89 .index = 5,
90 .size = 2,
91 .flags = UVC_CTRL_FLAG_SET_CUR
92 | UVC_CTRL_FLAG_GET_RANGE
93 | UVC_CTRL_FLAG_RESTORE,
94 },
95 {
96 .entity = UVC_GUID_UVC_PROCESSING,
97 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
98 .index = 6,
99 .size = 2,
100 .flags = UVC_CTRL_FLAG_SET_CUR
101 | UVC_CTRL_FLAG_GET_RANGE
102 | UVC_CTRL_FLAG_RESTORE
103 | UVC_CTRL_FLAG_AUTO_UPDATE,
104 },
105 {
106 .entity = UVC_GUID_UVC_PROCESSING,
107 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
108 .index = 7,
109 .size = 4,
110 .flags = UVC_CTRL_FLAG_SET_CUR
111 | UVC_CTRL_FLAG_GET_RANGE
112 | UVC_CTRL_FLAG_RESTORE
113 | UVC_CTRL_FLAG_AUTO_UPDATE,
114 },
115 {
116 .entity = UVC_GUID_UVC_PROCESSING,
117 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
118 .index = 8,
119 .size = 2,
120 .flags = UVC_CTRL_FLAG_SET_CUR
121 | UVC_CTRL_FLAG_GET_RANGE
122 | UVC_CTRL_FLAG_RESTORE,
123 },
124 {
125 .entity = UVC_GUID_UVC_PROCESSING,
126 .selector = UVC_PU_GAIN_CONTROL,
127 .index = 9,
128 .size = 2,
129 .flags = UVC_CTRL_FLAG_SET_CUR
130 | UVC_CTRL_FLAG_GET_RANGE
131 | UVC_CTRL_FLAG_RESTORE,
132 },
133 {
134 .entity = UVC_GUID_UVC_PROCESSING,
135 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
136 .index = 10,
137 .size = 1,
138 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
139 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
140 },
141 {
142 .entity = UVC_GUID_UVC_PROCESSING,
143 .selector = UVC_PU_HUE_AUTO_CONTROL,
144 .index = 11,
145 .size = 1,
146 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
147 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
148 },
149 {
150 .entity = UVC_GUID_UVC_PROCESSING,
151 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
152 .index = 12,
153 .size = 1,
154 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
155 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
156 },
157 {
158 .entity = UVC_GUID_UVC_PROCESSING,
159 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
160 .index = 13,
161 .size = 1,
162 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
163 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
164 },
165 {
166 .entity = UVC_GUID_UVC_PROCESSING,
167 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
168 .index = 14,
169 .size = 2,
170 .flags = UVC_CTRL_FLAG_SET_CUR
171 | UVC_CTRL_FLAG_GET_RANGE
172 | UVC_CTRL_FLAG_RESTORE,
173 },
174 {
175 .entity = UVC_GUID_UVC_PROCESSING,
176 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
177 .index = 15,
178 .size = 2,
179 .flags = UVC_CTRL_FLAG_SET_CUR
180 | UVC_CTRL_FLAG_GET_RANGE
181 | UVC_CTRL_FLAG_RESTORE,
182 },
183 {
184 .entity = UVC_GUID_UVC_PROCESSING,
185 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
186 .index = 16,
187 .size = 1,
188 .flags = UVC_CTRL_FLAG_GET_CUR,
189 },
190 {
191 .entity = UVC_GUID_UVC_PROCESSING,
192 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
193 .index = 17,
194 .size = 1,
195 .flags = UVC_CTRL_FLAG_GET_CUR,
196 },
197 {
198 .entity = UVC_GUID_UVC_CAMERA,
199 .selector = UVC_CT_SCANNING_MODE_CONTROL,
200 .index = 0,
201 .size = 1,
202 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
203 | UVC_CTRL_FLAG_RESTORE,
204 },
205 {
206 .entity = UVC_GUID_UVC_CAMERA,
207 .selector = UVC_CT_AE_MODE_CONTROL,
208 .index = 1,
209 .size = 1,
210 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
211 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
212 | UVC_CTRL_FLAG_RESTORE,
213 },
214 {
215 .entity = UVC_GUID_UVC_CAMERA,
216 .selector = UVC_CT_AE_PRIORITY_CONTROL,
217 .index = 2,
218 .size = 1,
219 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
220 | UVC_CTRL_FLAG_RESTORE,
221 },
222 {
223 .entity = UVC_GUID_UVC_CAMERA,
224 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
225 .index = 3,
226 .size = 4,
227 .flags = UVC_CTRL_FLAG_SET_CUR
228 | UVC_CTRL_FLAG_GET_RANGE
229 | UVC_CTRL_FLAG_RESTORE
230 | UVC_CTRL_FLAG_AUTO_UPDATE,
231 },
232 {
233 .entity = UVC_GUID_UVC_CAMERA,
234 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
235 .index = 4,
236 .size = 1,
237 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
238 },
239 {
240 .entity = UVC_GUID_UVC_CAMERA,
241 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
242 .index = 5,
243 .size = 2,
244 .flags = UVC_CTRL_FLAG_SET_CUR
245 | UVC_CTRL_FLAG_GET_RANGE
246 | UVC_CTRL_FLAG_RESTORE
247 | UVC_CTRL_FLAG_AUTO_UPDATE,
248 },
249 {
250 .entity = UVC_GUID_UVC_CAMERA,
251 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
252 .index = 6,
253 .size = 2,
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,
258 },
259 {
260 .entity = UVC_GUID_UVC_CAMERA,
261 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
262 .index = 7,
263 .size = 2,
264 .flags = UVC_CTRL_FLAG_SET_CUR
265 | UVC_CTRL_FLAG_GET_RANGE
266 | UVC_CTRL_FLAG_RESTORE
267 | UVC_CTRL_FLAG_AUTO_UPDATE,
268 },
269 {
270 .entity = UVC_GUID_UVC_CAMERA,
271 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
272 .index = 8,
273 .size = 1,
274 .flags = UVC_CTRL_FLAG_SET_CUR
275 | UVC_CTRL_FLAG_AUTO_UPDATE,
276 },
277 {
278 .entity = UVC_GUID_UVC_CAMERA,
279 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
280 .index = 9,
281 .size = 2,
282 .flags = UVC_CTRL_FLAG_SET_CUR
283 | UVC_CTRL_FLAG_GET_RANGE
284 | UVC_CTRL_FLAG_RESTORE
285 | UVC_CTRL_FLAG_AUTO_UPDATE,
286 },
287 {
288 .entity = UVC_GUID_UVC_CAMERA,
289 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
290 .index = 10,
291 .size = 3,
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,
296 },
297 {
298 .entity = UVC_GUID_UVC_CAMERA,
299 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
300 .index = 11,
301 .size = 8,
302 .flags = UVC_CTRL_FLAG_SET_CUR
303 | UVC_CTRL_FLAG_GET_RANGE
304 | UVC_CTRL_FLAG_RESTORE
305 | UVC_CTRL_FLAG_AUTO_UPDATE,
306 },
307 {
308 .entity = UVC_GUID_UVC_CAMERA,
309 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
310 .index = 12,
311 .size = 4,
312 .flags = UVC_CTRL_FLAG_SET_CUR
313 | UVC_CTRL_FLAG_GET_RANGE
314 | UVC_CTRL_FLAG_AUTO_UPDATE,
315 },
316 {
317 .entity = UVC_GUID_UVC_CAMERA,
318 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
319 .index = 13,
320 .size = 2,
321 .flags = UVC_CTRL_FLAG_SET_CUR
322 | UVC_CTRL_FLAG_GET_RANGE
323 | UVC_CTRL_FLAG_RESTORE
324 | UVC_CTRL_FLAG_AUTO_UPDATE,
325 },
326 {
327 .entity = UVC_GUID_UVC_CAMERA,
328 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
329 .index = 14,
330 .size = 2,
331 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
332 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
333 | UVC_CTRL_FLAG_GET_DEF
334 | UVC_CTRL_FLAG_AUTO_UPDATE,
335 },
336 {
337 .entity = UVC_GUID_UVC_CAMERA,
338 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
339 .index = 17,
340 .size = 1,
341 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
342 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
343 },
344 {
345 .entity = UVC_GUID_UVC_CAMERA,
346 .selector = UVC_CT_PRIVACY_CONTROL,
347 .index = 18,
348 .size = 1,
349 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
350 | UVC_CTRL_FLAG_RESTORE
351 | UVC_CTRL_FLAG_AUTO_UPDATE,
352 },
353 {
354 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
355 .selector = UVC_CT_PRIVACY_CONTROL,
356 .index = 0,
357 .size = 1,
358 .flags = UVC_CTRL_FLAG_GET_CUR
359 | UVC_CTRL_FLAG_AUTO_UPDATE,
360 },
361 };
362
363 static const u32 uvc_control_classes[] = {
364 V4L2_CID_CAMERA_CLASS,
365 V4L2_CID_USER_CLASS,
366 };
367
368 static const int exposure_auto_mapping[] = { 2, 1, 4, 8 };
369
370 /*
371 * This function translates the V4L2 menu index @idx, as exposed to userspace as
372 * the V4L2 control value, to the corresponding UVC control value used by the
373 * device. The custom menu_mapping in the control @mapping is used when
374 * available, otherwise the function assumes that the V4L2 and UVC values are
375 * identical.
376 *
377 * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is
378 * expressed as a bitmask and is thus guaranteed to have a single bit set.
379 *
380 * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the
381 * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM
382 * or UVC_CTRL_DATA_TYPE_BITMASK.
383 */
uvc_mapping_get_menu_value(const struct uvc_control_mapping * mapping,u32 idx)384 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
385 u32 idx)
386 {
387 if (!test_bit(idx, &mapping->menu_mask))
388 return -EINVAL;
389
390 if (mapping->menu_mapping)
391 return mapping->menu_mapping[idx];
392
393 return idx;
394 }
395
396 static const char *
uvc_mapping_get_menu_name(const struct uvc_control_mapping * mapping,u32 idx)397 uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx)
398 {
399 if (!test_bit(idx, &mapping->menu_mask))
400 return NULL;
401
402 if (mapping->menu_names)
403 return mapping->menu_names[idx];
404
405 return v4l2_ctrl_get_menu(mapping->id)[idx];
406 }
407
uvc_ctrl_get_zoom(struct uvc_control_mapping * mapping,u8 query,const u8 * data)408 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
409 u8 query, const u8 *data)
410 {
411 s8 zoom = (s8)data[0];
412
413 switch (query) {
414 case UVC_GET_CUR:
415 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
416
417 case UVC_GET_MIN:
418 case UVC_GET_MAX:
419 case UVC_GET_RES:
420 case UVC_GET_DEF:
421 default:
422 return data[2];
423 }
424 }
425
uvc_ctrl_set_zoom(struct uvc_control_mapping * mapping,s32 value,u8 * data)426 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
427 s32 value, u8 *data)
428 {
429 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
430 data[2] = min((int)abs(value), 0xff);
431 }
432
uvc_ctrl_get_rel_speed(struct uvc_control_mapping * mapping,u8 query,const u8 * data)433 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
434 u8 query, const u8 *data)
435 {
436 unsigned int first = mapping->offset / 8;
437 s8 rel = (s8)data[first];
438
439 switch (query) {
440 case UVC_GET_CUR:
441 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
442 : -data[first+1]);
443 case UVC_GET_MIN:
444 return -data[first+1];
445 case UVC_GET_MAX:
446 case UVC_GET_RES:
447 case UVC_GET_DEF:
448 default:
449 return data[first+1];
450 }
451 }
452
uvc_ctrl_set_rel_speed(struct uvc_control_mapping * mapping,s32 value,u8 * data)453 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
454 s32 value, u8 *data)
455 {
456 unsigned int first = mapping->offset / 8;
457
458 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
459 data[first+1] = min_t(int, abs(value), 0xff);
460 }
461
462 static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
463 {
464 .id = V4L2_CID_BRIGHTNESS,
465 .entity = UVC_GUID_UVC_PROCESSING,
466 .selector = UVC_PU_BRIGHTNESS_CONTROL,
467 .size = 16,
468 .offset = 0,
469 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
470 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
471 },
472 {
473 .id = V4L2_CID_CONTRAST,
474 .entity = UVC_GUID_UVC_PROCESSING,
475 .selector = UVC_PU_CONTRAST_CONTROL,
476 .size = 16,
477 .offset = 0,
478 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
479 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
480 },
481 {
482 .id = V4L2_CID_HUE,
483 .entity = UVC_GUID_UVC_PROCESSING,
484 .selector = UVC_PU_HUE_CONTROL,
485 .size = 16,
486 .offset = 0,
487 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
488 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
489 .master_id = V4L2_CID_HUE_AUTO,
490 .master_manual = 0,
491 },
492 {
493 .id = V4L2_CID_SATURATION,
494 .entity = UVC_GUID_UVC_PROCESSING,
495 .selector = UVC_PU_SATURATION_CONTROL,
496 .size = 16,
497 .offset = 0,
498 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
499 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
500 },
501 {
502 .id = V4L2_CID_SHARPNESS,
503 .entity = UVC_GUID_UVC_PROCESSING,
504 .selector = UVC_PU_SHARPNESS_CONTROL,
505 .size = 16,
506 .offset = 0,
507 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
508 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
509 },
510 {
511 .id = V4L2_CID_GAMMA,
512 .entity = UVC_GUID_UVC_PROCESSING,
513 .selector = UVC_PU_GAMMA_CONTROL,
514 .size = 16,
515 .offset = 0,
516 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
517 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
518 },
519 {
520 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
521 .entity = UVC_GUID_UVC_PROCESSING,
522 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
523 .size = 16,
524 .offset = 0,
525 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
526 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
527 },
528 {
529 .id = V4L2_CID_GAIN,
530 .entity = UVC_GUID_UVC_PROCESSING,
531 .selector = UVC_PU_GAIN_CONTROL,
532 .size = 16,
533 .offset = 0,
534 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
535 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
536 },
537 {
538 .id = V4L2_CID_HUE_AUTO,
539 .entity = UVC_GUID_UVC_PROCESSING,
540 .selector = UVC_PU_HUE_AUTO_CONTROL,
541 .size = 1,
542 .offset = 0,
543 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
544 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
545 .slave_ids = { V4L2_CID_HUE, },
546 },
547 {
548 .id = V4L2_CID_EXPOSURE_AUTO,
549 .entity = UVC_GUID_UVC_CAMERA,
550 .selector = UVC_CT_AE_MODE_CONTROL,
551 .size = 4,
552 .offset = 0,
553 .v4l2_type = V4L2_CTRL_TYPE_MENU,
554 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
555 .menu_mapping = exposure_auto_mapping,
556 .menu_mask = GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY,
557 V4L2_EXPOSURE_AUTO),
558 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
559 },
560 {
561 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
562 .entity = UVC_GUID_UVC_CAMERA,
563 .selector = UVC_CT_AE_PRIORITY_CONTROL,
564 .size = 1,
565 .offset = 0,
566 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
567 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
568 },
569 {
570 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
571 .entity = UVC_GUID_UVC_CAMERA,
572 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
573 .size = 32,
574 .offset = 0,
575 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
576 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
577 .master_id = V4L2_CID_EXPOSURE_AUTO,
578 .master_manual = V4L2_EXPOSURE_MANUAL,
579 },
580 {
581 .id = V4L2_CID_AUTO_WHITE_BALANCE,
582 .entity = UVC_GUID_UVC_PROCESSING,
583 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
584 .size = 1,
585 .offset = 0,
586 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
587 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
588 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
589 },
590 {
591 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
592 .entity = UVC_GUID_UVC_PROCESSING,
593 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
594 .size = 16,
595 .offset = 0,
596 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
597 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
598 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
599 .master_manual = 0,
600 },
601 {
602 .id = V4L2_CID_AUTO_WHITE_BALANCE,
603 .entity = UVC_GUID_UVC_PROCESSING,
604 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
605 .size = 1,
606 .offset = 0,
607 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
608 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
609 .slave_ids = { V4L2_CID_BLUE_BALANCE,
610 V4L2_CID_RED_BALANCE },
611 },
612 {
613 .id = V4L2_CID_BLUE_BALANCE,
614 .entity = UVC_GUID_UVC_PROCESSING,
615 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
616 .size = 16,
617 .offset = 0,
618 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
619 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
620 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
621 .master_manual = 0,
622 },
623 {
624 .id = V4L2_CID_RED_BALANCE,
625 .entity = UVC_GUID_UVC_PROCESSING,
626 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
627 .size = 16,
628 .offset = 16,
629 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
630 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
631 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
632 .master_manual = 0,
633 },
634 {
635 .id = V4L2_CID_FOCUS_ABSOLUTE,
636 .entity = UVC_GUID_UVC_CAMERA,
637 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
638 .size = 16,
639 .offset = 0,
640 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
641 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
642 .master_id = V4L2_CID_FOCUS_AUTO,
643 .master_manual = 0,
644 },
645 {
646 .id = V4L2_CID_FOCUS_AUTO,
647 .entity = UVC_GUID_UVC_CAMERA,
648 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
649 .size = 1,
650 .offset = 0,
651 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
652 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
653 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
654 },
655 {
656 .id = V4L2_CID_IRIS_ABSOLUTE,
657 .entity = UVC_GUID_UVC_CAMERA,
658 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
659 .size = 16,
660 .offset = 0,
661 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
662 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
663 },
664 {
665 .id = V4L2_CID_IRIS_RELATIVE,
666 .entity = UVC_GUID_UVC_CAMERA,
667 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
668 .size = 8,
669 .offset = 0,
670 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
671 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
672 },
673 {
674 .id = V4L2_CID_ZOOM_ABSOLUTE,
675 .entity = UVC_GUID_UVC_CAMERA,
676 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
677 .size = 16,
678 .offset = 0,
679 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
680 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
681 },
682 {
683 .id = V4L2_CID_ZOOM_CONTINUOUS,
684 .entity = UVC_GUID_UVC_CAMERA,
685 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
686 .size = 0,
687 .offset = 0,
688 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
689 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
690 .get = uvc_ctrl_get_zoom,
691 .set = uvc_ctrl_set_zoom,
692 },
693 {
694 .id = V4L2_CID_PAN_ABSOLUTE,
695 .entity = UVC_GUID_UVC_CAMERA,
696 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
697 .size = 32,
698 .offset = 0,
699 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
700 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
701 },
702 {
703 .id = V4L2_CID_TILT_ABSOLUTE,
704 .entity = UVC_GUID_UVC_CAMERA,
705 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
706 .size = 32,
707 .offset = 32,
708 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
709 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
710 },
711 {
712 .id = V4L2_CID_PAN_SPEED,
713 .entity = UVC_GUID_UVC_CAMERA,
714 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
715 .size = 16,
716 .offset = 0,
717 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
718 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
719 .get = uvc_ctrl_get_rel_speed,
720 .set = uvc_ctrl_set_rel_speed,
721 },
722 {
723 .id = V4L2_CID_TILT_SPEED,
724 .entity = UVC_GUID_UVC_CAMERA,
725 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
726 .size = 16,
727 .offset = 16,
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,
732 },
733 {
734 .id = V4L2_CID_PRIVACY,
735 .entity = UVC_GUID_UVC_CAMERA,
736 .selector = UVC_CT_PRIVACY_CONTROL,
737 .size = 1,
738 .offset = 0,
739 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
740 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
741 },
742 {
743 .id = V4L2_CID_PRIVACY,
744 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
745 .selector = UVC_CT_PRIVACY_CONTROL,
746 .size = 1,
747 .offset = 0,
748 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
749 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
750 },
751 };
752
753 const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = {
754 .id = V4L2_CID_POWER_LINE_FREQUENCY,
755 .entity = UVC_GUID_UVC_PROCESSING,
756 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
757 .size = 2,
758 .offset = 0,
759 .v4l2_type = V4L2_CTRL_TYPE_MENU,
760 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
761 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
762 V4L2_CID_POWER_LINE_FREQUENCY_50HZ),
763 };
764
765 const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
766 .id = V4L2_CID_POWER_LINE_FREQUENCY,
767 .entity = UVC_GUID_UVC_PROCESSING,
768 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
769 .size = 2,
770 .offset = 0,
771 .v4l2_type = V4L2_CTRL_TYPE_MENU,
772 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
773 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
774 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
775 };
776
777 static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc11[] = {
778 &uvc_ctrl_power_line_mapping_uvc11,
779 NULL, /* Sentinel */
780 };
781
782 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
783 .id = V4L2_CID_POWER_LINE_FREQUENCY,
784 .entity = UVC_GUID_UVC_PROCESSING,
785 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
786 .size = 2,
787 .offset = 0,
788 .v4l2_type = V4L2_CTRL_TYPE_MENU,
789 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
790 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
791 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
792 };
793
794 static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc15[] = {
795 &uvc_ctrl_power_line_mapping_uvc15,
796 NULL, /* Sentinel */
797 };
798
799 /* ------------------------------------------------------------------------
800 * Utility functions
801 */
802
uvc_ctrl_data(struct uvc_control * ctrl,int id)803 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
804 {
805 return ctrl->uvc_data + id * ctrl->info.size;
806 }
807
uvc_test_bit(const u8 * data,int bit)808 static inline int uvc_test_bit(const u8 *data, int bit)
809 {
810 return (data[bit >> 3] >> (bit & 7)) & 1;
811 }
812
uvc_clear_bit(u8 * data,int bit)813 static inline void uvc_clear_bit(u8 *data, int bit)
814 {
815 data[bit >> 3] &= ~(1 << (bit & 7));
816 }
817
uvc_menu_to_v4l2_menu(struct uvc_control_mapping * mapping,s32 val)818 static s32 uvc_menu_to_v4l2_menu(struct uvc_control_mapping *mapping, s32 val)
819 {
820 unsigned int i;
821
822 for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
823 u32 menu_value;
824
825 if (!test_bit(i, &mapping->menu_mask))
826 continue;
827
828 menu_value = uvc_mapping_get_menu_value(mapping, i);
829
830 if (menu_value == val)
831 return i;
832 }
833
834 return val;
835 }
836
837 /*
838 * Extract the bit string specified by mapping->offset and mapping->size
839 * from the little-endian data stored at 'data' and return the result as
840 * a signed 32bit integer. Sign extension will be performed if the mapping
841 * references a signed data type.
842 */
uvc_get_le_value(struct uvc_control_mapping * mapping,u8 query,const u8 * data)843 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
844 u8 query, const u8 *data)
845 {
846 int bits = mapping->size;
847 int offset = mapping->offset;
848 s32 value = 0;
849 u8 mask;
850
851 data += offset / 8;
852 offset &= 7;
853 mask = ((1LL << bits) - 1) << offset;
854
855 while (1) {
856 u8 byte = *data & mask;
857 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
858 bits -= 8 - (offset > 0 ? offset : 0);
859 if (bits <= 0)
860 break;
861
862 offset -= 8;
863 mask = (1 << bits) - 1;
864 data++;
865 }
866
867 /* Sign-extend the value if needed. */
868 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
869 value |= -(value & (1 << (mapping->size - 1)));
870
871 /* If it is a menu, convert from uvc to v4l2. */
872 if (mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
873 return value;
874
875 switch (query) {
876 case UVC_GET_CUR:
877 case UVC_GET_DEF:
878 return uvc_menu_to_v4l2_menu(mapping, value);
879 }
880
881 return value;
882 }
883
884 /*
885 * Set the bit string specified by mapping->offset and mapping->size
886 * in the little-endian data stored at 'data' to the value 'value'.
887 */
uvc_set_le_value(struct uvc_control_mapping * mapping,s32 value,u8 * data)888 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
889 s32 value, u8 *data)
890 {
891 int bits = mapping->size;
892 int offset = mapping->offset;
893 u8 mask;
894
895 /*
896 * According to the v4l2 spec, writing any value to a button control
897 * should result in the action belonging to the button control being
898 * triggered. UVC devices however want to see a 1 written -> override
899 * value.
900 */
901 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
902 value = -1;
903
904 data += offset / 8;
905 offset &= 7;
906
907 for (; bits > 0; data++) {
908 mask = ((1LL << bits) - 1) << offset;
909 *data = (*data & ~mask) | ((value << offset) & mask);
910 value >>= offset ? offset : 8;
911 bits -= 8 - offset;
912 offset = 0;
913 }
914 }
915
916 /* ------------------------------------------------------------------------
917 * Terminal and unit management
918 */
919
uvc_entity_match_guid(const struct uvc_entity * entity,const u8 guid[16])920 static int uvc_entity_match_guid(const struct uvc_entity *entity,
921 const u8 guid[16])
922 {
923 return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
924 }
925
926 /* ------------------------------------------------------------------------
927 * UVC Controls
928 */
929
__uvc_find_control(struct uvc_entity * entity,u32 v4l2_id,struct uvc_control_mapping ** mapping,struct uvc_control ** control,int next)930 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
931 struct uvc_control_mapping **mapping, struct uvc_control **control,
932 int next)
933 {
934 struct uvc_control *ctrl;
935 struct uvc_control_mapping *map;
936 unsigned int i;
937
938 if (entity == NULL)
939 return;
940
941 for (i = 0; i < entity->ncontrols; ++i) {
942 ctrl = &entity->controls[i];
943 if (!ctrl->initialized)
944 continue;
945
946 list_for_each_entry(map, &ctrl->info.mappings, list) {
947 if ((map->id == v4l2_id) && !next) {
948 *control = ctrl;
949 *mapping = map;
950 return;
951 }
952
953 if ((*mapping == NULL || (*mapping)->id > map->id) &&
954 (map->id > v4l2_id) && next) {
955 *control = ctrl;
956 *mapping = map;
957 }
958 }
959 }
960 }
961
uvc_find_control(struct uvc_video_chain * chain,u32 v4l2_id,struct uvc_control_mapping ** mapping)962 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
963 u32 v4l2_id, struct uvc_control_mapping **mapping)
964 {
965 struct uvc_control *ctrl = NULL;
966 struct uvc_entity *entity;
967 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
968
969 *mapping = NULL;
970
971 /* Mask the query flags. */
972 v4l2_id &= V4L2_CTRL_ID_MASK;
973
974 /* Find the control. */
975 list_for_each_entry(entity, &chain->entities, chain) {
976 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
977 if (ctrl && !next)
978 return ctrl;
979 }
980
981 if (ctrl == NULL && !next)
982 uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
983 v4l2_id);
984
985 return ctrl;
986 }
987
uvc_ctrl_populate_cache(struct uvc_video_chain * chain,struct uvc_control * ctrl)988 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
989 struct uvc_control *ctrl)
990 {
991 int ret;
992
993 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
994 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
995 chain->dev->intfnum, ctrl->info.selector,
996 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
997 ctrl->info.size);
998 if (ret < 0)
999 return ret;
1000 }
1001
1002 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
1003 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
1004 chain->dev->intfnum, ctrl->info.selector,
1005 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
1006 ctrl->info.size);
1007 if (ret < 0)
1008 return ret;
1009 }
1010 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
1011 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
1012 chain->dev->intfnum, ctrl->info.selector,
1013 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
1014 ctrl->info.size);
1015 if (ret < 0)
1016 return ret;
1017 }
1018 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
1019 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
1020 chain->dev->intfnum, ctrl->info.selector,
1021 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
1022 ctrl->info.size);
1023 if (ret < 0) {
1024 if (UVC_ENTITY_TYPE(ctrl->entity) !=
1025 UVC_VC_EXTENSION_UNIT)
1026 return ret;
1027
1028 /*
1029 * GET_RES is mandatory for XU controls, but some
1030 * cameras still choke on it. Ignore errors and set the
1031 * resolution value to zero.
1032 */
1033 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
1034 "UVC non compliance - GET_RES failed on "
1035 "an XU control. Enabling workaround.\n");
1036 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
1037 ctrl->info.size);
1038 }
1039 }
1040
1041 ctrl->cached = 1;
1042 return 0;
1043 }
1044
__uvc_ctrl_load_cur(struct uvc_video_chain * chain,struct uvc_control * ctrl)1045 static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
1046 struct uvc_control *ctrl)
1047 {
1048 u8 *data;
1049 int ret;
1050
1051 if (ctrl->loaded)
1052 return 0;
1053
1054 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1055
1056 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1057 memset(data, 0, ctrl->info.size);
1058 ctrl->loaded = 1;
1059
1060 return 0;
1061 }
1062
1063 if (ctrl->entity->get_cur)
1064 ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
1065 ctrl->info.selector, data,
1066 ctrl->info.size);
1067 else
1068 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1069 ctrl->entity->id, chain->dev->intfnum,
1070 ctrl->info.selector, data,
1071 ctrl->info.size);
1072
1073 if (ret < 0)
1074 return ret;
1075
1076 ctrl->loaded = 1;
1077
1078 return ret;
1079 }
1080
__uvc_ctrl_get(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,s32 * value)1081 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1082 struct uvc_control *ctrl,
1083 struct uvc_control_mapping *mapping,
1084 s32 *value)
1085 {
1086 int ret;
1087
1088 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1089 return -EACCES;
1090
1091 ret = __uvc_ctrl_load_cur(chain, ctrl);
1092 if (ret < 0)
1093 return ret;
1094
1095 *value = mapping->get(mapping, UVC_GET_CUR,
1096 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1097
1098 return 0;
1099 }
1100
__uvc_query_v4l2_class(struct uvc_video_chain * chain,u32 req_id,u32 found_id)1101 static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1102 u32 found_id)
1103 {
1104 bool find_next = req_id & V4L2_CTRL_FLAG_NEXT_CTRL;
1105 unsigned int i;
1106
1107 req_id &= V4L2_CTRL_ID_MASK;
1108
1109 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
1110 if (!(chain->ctrl_class_bitmap & BIT(i)))
1111 continue;
1112 if (!find_next) {
1113 if (uvc_control_classes[i] == req_id)
1114 return i;
1115 continue;
1116 }
1117 if (uvc_control_classes[i] > req_id &&
1118 uvc_control_classes[i] < found_id)
1119 return i;
1120 }
1121
1122 return -ENODEV;
1123 }
1124
uvc_query_v4l2_class(struct uvc_video_chain * chain,u32 req_id,u32 found_id,struct v4l2_queryctrl * v4l2_ctrl)1125 static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1126 u32 found_id, struct v4l2_queryctrl *v4l2_ctrl)
1127 {
1128 int idx;
1129
1130 idx = __uvc_query_v4l2_class(chain, req_id, found_id);
1131 if (idx < 0)
1132 return -ENODEV;
1133
1134 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1135 v4l2_ctrl->id = uvc_control_classes[idx];
1136 strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
1137 sizeof(v4l2_ctrl->name));
1138 v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
1139 v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
1140 | V4L2_CTRL_FLAG_READ_ONLY;
1141 return 0;
1142 }
1143
1144 /*
1145 * Check if control @v4l2_id can be accessed by the given control @ioctl
1146 * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
1147 *
1148 * For set operations on slave controls, check if the master's value is set to
1149 * manual, either in the others controls set in the same ioctl call, or from
1150 * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
1151 * both the master and slave control, such as for instance setting
1152 * auto_exposure=1, exposure_time_absolute=251.
1153 */
uvc_ctrl_is_accessible(struct uvc_video_chain * chain,u32 v4l2_id,const struct v4l2_ext_controls * ctrls,unsigned long ioctl)1154 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
1155 const struct v4l2_ext_controls *ctrls,
1156 unsigned long ioctl)
1157 {
1158 struct uvc_control_mapping *master_map = NULL;
1159 struct uvc_control *master_ctrl = NULL;
1160 struct uvc_control_mapping *mapping;
1161 struct uvc_control *ctrl;
1162 bool read = ioctl == VIDIOC_G_EXT_CTRLS;
1163 s32 val;
1164 int ret;
1165 int i;
1166
1167 if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
1168 return -EACCES;
1169
1170 ctrl = uvc_find_control(chain, v4l2_id, &mapping);
1171 if (!ctrl)
1172 return -EINVAL;
1173
1174 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read)
1175 return -EACCES;
1176
1177 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read)
1178 return -EACCES;
1179
1180 if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id)
1181 return 0;
1182
1183 /*
1184 * Iterate backwards in cases where the master control is accessed
1185 * multiple times in the same ioctl. We want the last value.
1186 */
1187 for (i = ctrls->count - 1; i >= 0; i--) {
1188 if (ctrls->controls[i].id == mapping->master_id)
1189 return ctrls->controls[i].value ==
1190 mapping->master_manual ? 0 : -EACCES;
1191 }
1192
1193 __uvc_find_control(ctrl->entity, mapping->master_id, &master_map,
1194 &master_ctrl, 0);
1195
1196 if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1197 return 0;
1198
1199 ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1200 if (ret >= 0 && val != mapping->master_manual)
1201 return -EACCES;
1202
1203 return 0;
1204 }
1205
uvc_map_get_name(const struct uvc_control_mapping * map)1206 static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
1207 {
1208 const char *name;
1209
1210 if (map->name)
1211 return map->name;
1212
1213 name = v4l2_ctrl_get_name(map->id);
1214 if (name)
1215 return name;
1216
1217 return "Unknown Control";
1218 }
1219
uvc_get_ctrl_bitmap(struct uvc_control * ctrl,struct uvc_control_mapping * mapping)1220 static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl,
1221 struct uvc_control_mapping *mapping)
1222 {
1223 /*
1224 * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
1225 * the number of bits supported. Those controls do not list GET_MAX
1226 * as supported.
1227 */
1228 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1229 return mapping->get(mapping, UVC_GET_RES,
1230 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1231
1232 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1233 return mapping->get(mapping, UVC_GET_MAX,
1234 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1235
1236 return ~0;
1237 }
1238
__uvc_query_v4l2_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_queryctrl * v4l2_ctrl)1239 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1240 struct uvc_control *ctrl,
1241 struct uvc_control_mapping *mapping,
1242 struct v4l2_queryctrl *v4l2_ctrl)
1243 {
1244 struct uvc_control_mapping *master_map = NULL;
1245 struct uvc_control *master_ctrl = NULL;
1246
1247 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1248 v4l2_ctrl->id = mapping->id;
1249 v4l2_ctrl->type = mapping->v4l2_type;
1250 strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
1251 sizeof(v4l2_ctrl->name));
1252 v4l2_ctrl->flags = 0;
1253
1254 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1255 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1256 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1257 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1258
1259 if (mapping->master_id)
1260 __uvc_find_control(ctrl->entity, mapping->master_id,
1261 &master_map, &master_ctrl, 0);
1262 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1263 s32 val;
1264 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1265 if (ret < 0)
1266 return ret;
1267
1268 if (val != mapping->master_manual)
1269 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1270 }
1271
1272 if (!ctrl->cached) {
1273 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1274 if (ret < 0)
1275 return ret;
1276 }
1277
1278 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1279 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1280 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1281 }
1282
1283 switch (mapping->v4l2_type) {
1284 case V4L2_CTRL_TYPE_MENU:
1285 v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1;
1286 v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1;
1287 v4l2_ctrl->step = 1;
1288 return 0;
1289
1290 case V4L2_CTRL_TYPE_BOOLEAN:
1291 v4l2_ctrl->minimum = 0;
1292 v4l2_ctrl->maximum = 1;
1293 v4l2_ctrl->step = 1;
1294 return 0;
1295
1296 case V4L2_CTRL_TYPE_BUTTON:
1297 v4l2_ctrl->minimum = 0;
1298 v4l2_ctrl->maximum = 0;
1299 v4l2_ctrl->step = 0;
1300 return 0;
1301
1302 case V4L2_CTRL_TYPE_BITMASK:
1303 v4l2_ctrl->minimum = 0;
1304 v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping);
1305 v4l2_ctrl->step = 0;
1306 return 0;
1307
1308 default:
1309 break;
1310 }
1311
1312 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1313 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1314 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1315
1316 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1317 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1318 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1319
1320 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1321 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1322 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1323
1324 return 0;
1325 }
1326
uvc_query_v4l2_ctrl(struct uvc_video_chain * chain,struct v4l2_queryctrl * v4l2_ctrl)1327 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1328 struct v4l2_queryctrl *v4l2_ctrl)
1329 {
1330 struct uvc_control *ctrl;
1331 struct uvc_control_mapping *mapping;
1332 int ret;
1333
1334 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1335 if (ret < 0)
1336 return -ERESTARTSYS;
1337
1338 /* Check if the ctrl is a know class */
1339 if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
1340 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
1341 if (!ret)
1342 goto done;
1343 }
1344
1345 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1346 if (ctrl == NULL) {
1347 ret = -EINVAL;
1348 goto done;
1349 }
1350
1351 /*
1352 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
1353 * a class should be inserted between the previous control and the one
1354 * we have just found.
1355 */
1356 if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1357 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
1358 v4l2_ctrl);
1359 if (!ret)
1360 goto done;
1361 }
1362
1363 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1364 done:
1365 mutex_unlock(&chain->ctrl_mutex);
1366 return ret;
1367 }
1368
1369 /*
1370 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1371 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1372 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1373 * Balance V4L2 controls use the White Balance Component UVC control) or
1374 * otherwise translated. The approach we take here is to use a translation
1375 * table for the controls that can be mapped directly, and handle the others
1376 * manually.
1377 */
uvc_query_v4l2_menu(struct uvc_video_chain * chain,struct v4l2_querymenu * query_menu)1378 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1379 struct v4l2_querymenu *query_menu)
1380 {
1381 struct uvc_control_mapping *mapping;
1382 struct uvc_control *ctrl;
1383 u32 index = query_menu->index;
1384 u32 id = query_menu->id;
1385 const char *name;
1386 int ret;
1387
1388 memset(query_menu, 0, sizeof(*query_menu));
1389 query_menu->id = id;
1390 query_menu->index = index;
1391
1392 if (index >= BITS_PER_TYPE(mapping->menu_mask))
1393 return -EINVAL;
1394
1395 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1396 if (ret < 0)
1397 return -ERESTARTSYS;
1398
1399 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1400 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1401 ret = -EINVAL;
1402 goto done;
1403 }
1404
1405 if (!test_bit(query_menu->index, &mapping->menu_mask)) {
1406 ret = -EINVAL;
1407 goto done;
1408 }
1409
1410 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
1411 int mask;
1412
1413 if (!ctrl->cached) {
1414 ret = uvc_ctrl_populate_cache(chain, ctrl);
1415 if (ret < 0)
1416 goto done;
1417 }
1418
1419 mask = uvc_mapping_get_menu_value(mapping, query_menu->index);
1420 if (mask < 0) {
1421 ret = mask;
1422 goto done;
1423 }
1424
1425 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) {
1426 ret = -EINVAL;
1427 goto done;
1428 }
1429 }
1430
1431 name = uvc_mapping_get_menu_name(mapping, query_menu->index);
1432 if (!name) {
1433 ret = -EINVAL;
1434 goto done;
1435 }
1436
1437 strscpy(query_menu->name, name, sizeof(query_menu->name));
1438
1439 done:
1440 mutex_unlock(&chain->ctrl_mutex);
1441 return ret;
1442 }
1443
1444 /* --------------------------------------------------------------------------
1445 * Ctrl event handling
1446 */
1447
uvc_ctrl_fill_event(struct uvc_video_chain * chain,struct v4l2_event * ev,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,s32 value,u32 changes)1448 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1449 struct v4l2_event *ev,
1450 struct uvc_control *ctrl,
1451 struct uvc_control_mapping *mapping,
1452 s32 value, u32 changes)
1453 {
1454 struct v4l2_queryctrl v4l2_ctrl;
1455
1456 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1457
1458 memset(ev, 0, sizeof(*ev));
1459 ev->type = V4L2_EVENT_CTRL;
1460 ev->id = v4l2_ctrl.id;
1461 ev->u.ctrl.value = value;
1462 ev->u.ctrl.changes = changes;
1463 ev->u.ctrl.type = v4l2_ctrl.type;
1464 ev->u.ctrl.flags = v4l2_ctrl.flags;
1465 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1466 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1467 ev->u.ctrl.step = v4l2_ctrl.step;
1468 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1469 }
1470
1471 /*
1472 * Send control change events to all subscribers for the @ctrl control. By
1473 * default the subscriber that generated the event, as identified by @handle,
1474 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1475 * @handle can be NULL for asynchronous events related to auto-update controls,
1476 * in which case all subscribers are notified.
1477 */
uvc_ctrl_send_event(struct uvc_video_chain * chain,struct uvc_fh * handle,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,s32 value,u32 changes)1478 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1479 struct uvc_fh *handle, struct uvc_control *ctrl,
1480 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1481 {
1482 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1483 struct v4l2_subscribed_event *sev;
1484 struct v4l2_event ev;
1485
1486 if (list_empty(&mapping->ev_subs))
1487 return;
1488
1489 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1490
1491 list_for_each_entry(sev, &mapping->ev_subs, node) {
1492 if (sev->fh != originator ||
1493 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1494 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1495 v4l2_event_queue_fh(sev->fh, &ev);
1496 }
1497 }
1498
1499 /*
1500 * Send control change events for the slave of the @master control identified
1501 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1502 * generated the event and may be NULL for auto-update events.
1503 */
uvc_ctrl_send_slave_event(struct uvc_video_chain * chain,struct uvc_fh * handle,struct uvc_control * master,u32 slave_id)1504 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1505 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1506 {
1507 struct uvc_control_mapping *mapping = NULL;
1508 struct uvc_control *ctrl = NULL;
1509 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1510 s32 val = 0;
1511
1512 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1513 if (ctrl == NULL)
1514 return;
1515
1516 if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1517 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1518
1519 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1520 }
1521
uvc_ctrl_set_handle(struct uvc_fh * handle,struct uvc_control * ctrl,struct uvc_fh * new_handle)1522 static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
1523 struct uvc_fh *new_handle)
1524 {
1525 lockdep_assert_held(&handle->chain->ctrl_mutex);
1526
1527 if (new_handle) {
1528 if (ctrl->handle)
1529 dev_warn_ratelimited(&handle->stream->dev->udev->dev,
1530 "UVC non compliance: Setting an async control with a pending operation.");
1531
1532 if (new_handle == ctrl->handle)
1533 return;
1534
1535 if (ctrl->handle) {
1536 WARN_ON(!ctrl->handle->pending_async_ctrls);
1537 if (ctrl->handle->pending_async_ctrls)
1538 ctrl->handle->pending_async_ctrls--;
1539 }
1540
1541 ctrl->handle = new_handle;
1542 handle->pending_async_ctrls++;
1543 return;
1544 }
1545
1546 /* Cannot clear the handle for a control not owned by us.*/
1547 if (WARN_ON(ctrl->handle != handle))
1548 return;
1549
1550 ctrl->handle = NULL;
1551 if (WARN_ON(!handle->pending_async_ctrls))
1552 return;
1553 handle->pending_async_ctrls--;
1554 }
1555
uvc_ctrl_status_event(struct uvc_video_chain * chain,struct uvc_control * ctrl,const u8 * data)1556 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1557 struct uvc_control *ctrl, const u8 *data)
1558 {
1559 struct uvc_control_mapping *mapping;
1560 struct uvc_fh *handle;
1561 unsigned int i;
1562
1563 mutex_lock(&chain->ctrl_mutex);
1564
1565 handle = ctrl->handle;
1566 if (handle)
1567 uvc_ctrl_set_handle(handle, ctrl, NULL);
1568
1569 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1570 s32 value = mapping->get(mapping, UVC_GET_CUR, data);
1571
1572 /*
1573 * handle may be NULL here if the device sends auto-update
1574 * events without a prior related control set from userspace.
1575 */
1576 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1577 if (!mapping->slave_ids[i])
1578 break;
1579
1580 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1581 mapping->slave_ids[i]);
1582 }
1583
1584 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1585 V4L2_EVENT_CTRL_CH_VALUE);
1586 }
1587
1588 mutex_unlock(&chain->ctrl_mutex);
1589 }
1590
uvc_ctrl_status_event_work(struct work_struct * work)1591 static void uvc_ctrl_status_event_work(struct work_struct *work)
1592 {
1593 struct uvc_device *dev = container_of(work, struct uvc_device,
1594 async_ctrl.work);
1595 struct uvc_ctrl_work *w = &dev->async_ctrl;
1596 int ret;
1597
1598 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1599
1600 /* The barrier is needed to synchronize with uvc_status_stop(). */
1601 if (smp_load_acquire(&dev->flush_status))
1602 return;
1603
1604 /* Resubmit the URB. */
1605 w->urb->interval = dev->int_ep->desc.bInterval;
1606 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1607 if (ret < 0)
1608 dev_err(&dev->udev->dev,
1609 "Failed to resubmit status URB (%d).\n", ret);
1610 }
1611
uvc_ctrl_status_event_async(struct urb * urb,struct uvc_video_chain * chain,struct uvc_control * ctrl,const u8 * data)1612 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1613 struct uvc_control *ctrl, const u8 *data)
1614 {
1615 struct uvc_device *dev = chain->dev;
1616 struct uvc_ctrl_work *w = &dev->async_ctrl;
1617
1618 if (list_empty(&ctrl->info.mappings))
1619 return false;
1620
1621 w->data = data;
1622 w->urb = urb;
1623 w->chain = chain;
1624 w->ctrl = ctrl;
1625
1626 schedule_work(&w->work);
1627
1628 return true;
1629 }
1630
uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control * xctrls,unsigned int xctrls_count,u32 id)1631 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1632 unsigned int xctrls_count, u32 id)
1633 {
1634 unsigned int i;
1635
1636 for (i = 0; i < xctrls_count; ++i) {
1637 if (xctrls[i].id == id)
1638 return true;
1639 }
1640
1641 return false;
1642 }
1643
uvc_ctrl_send_events(struct uvc_fh * handle,struct uvc_entity * entity,const struct v4l2_ext_control * xctrls,unsigned int xctrls_count)1644 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1645 struct uvc_entity *entity,
1646 const struct v4l2_ext_control *xctrls,
1647 unsigned int xctrls_count)
1648 {
1649 struct uvc_control_mapping *mapping;
1650 struct uvc_control *ctrl;
1651 unsigned int i;
1652 unsigned int j;
1653
1654 for (i = 0; i < xctrls_count; ++i) {
1655 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1656
1657 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1658 if (ctrl->entity != entity)
1659 continue;
1660
1661 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1662 /* Notification will be sent from an Interrupt event. */
1663 continue;
1664
1665 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1666 u32 slave_id = mapping->slave_ids[j];
1667
1668 if (!slave_id)
1669 break;
1670
1671 /*
1672 * We can skip sending an event for the slave if the
1673 * slave is being modified in the same transaction.
1674 */
1675 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1676 slave_id))
1677 continue;
1678
1679 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1680 slave_id);
1681 }
1682
1683 /*
1684 * If the master is being modified in the same transaction
1685 * flags may change too.
1686 */
1687 if (mapping->master_id &&
1688 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1689 mapping->master_id))
1690 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1691
1692 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1693 xctrls[i].value, changes);
1694 }
1695 }
1696
uvc_ctrl_add_event(struct v4l2_subscribed_event * sev,unsigned elems)1697 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1698 {
1699 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1700 struct uvc_control_mapping *mapping;
1701 struct uvc_control *ctrl;
1702 int ret;
1703
1704 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1705 if (ret < 0)
1706 return -ERESTARTSYS;
1707
1708 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
1709 ret = 0;
1710 goto done;
1711 }
1712
1713 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1714 if (ctrl == NULL) {
1715 ret = -EINVAL;
1716 goto done;
1717 }
1718
1719 list_add_tail(&sev->node, &mapping->ev_subs);
1720 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1721 struct v4l2_event ev;
1722 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1723 s32 val = 0;
1724
1725 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1726 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1727
1728 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1729 changes);
1730 /*
1731 * Mark the queue as active, allowing this initial event to be
1732 * accepted.
1733 */
1734 sev->elems = elems;
1735 v4l2_event_queue_fh(sev->fh, &ev);
1736 }
1737
1738 done:
1739 mutex_unlock(&handle->chain->ctrl_mutex);
1740 return ret;
1741 }
1742
uvc_ctrl_del_event(struct v4l2_subscribed_event * sev)1743 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1744 {
1745 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1746
1747 mutex_lock(&handle->chain->ctrl_mutex);
1748 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
1749 goto done;
1750 list_del(&sev->node);
1751 done:
1752 mutex_unlock(&handle->chain->ctrl_mutex);
1753 }
1754
1755 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1756 .add = uvc_ctrl_add_event,
1757 .del = uvc_ctrl_del_event,
1758 .replace = v4l2_ctrl_replace,
1759 .merge = v4l2_ctrl_merge,
1760 };
1761
1762 /* --------------------------------------------------------------------------
1763 * Control transactions
1764 *
1765 * To make extended set operations as atomic as the hardware allows, controls
1766 * are handled using begin/commit/rollback operations.
1767 *
1768 * At the beginning of a set request, uvc_ctrl_begin should be called to
1769 * initialize the request. This function acquires the control lock.
1770 *
1771 * When setting a control, the new value is stored in the control data field
1772 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1773 * later processing. If the UVC and V4L2 control sizes differ, the current
1774 * value is loaded from the hardware before storing the new value in the data
1775 * field.
1776 *
1777 * After processing all controls in the transaction, uvc_ctrl_commit or
1778 * uvc_ctrl_rollback must be called to apply the pending changes to the
1779 * hardware or revert them. When applying changes, all controls marked as
1780 * dirty will be modified in the UVC device, and the dirty flag will be
1781 * cleared. When reverting controls, the control data field
1782 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1783 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1784 * control lock.
1785 */
uvc_ctrl_begin(struct uvc_video_chain * chain)1786 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1787 {
1788 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1789 }
1790
1791 /*
1792 * Returns the number of uvc controls that have been correctly set, or a
1793 * negative number if there has been an error.
1794 */
uvc_ctrl_commit_entity(struct uvc_device * dev,struct uvc_fh * handle,struct uvc_entity * entity,int rollback,struct uvc_control ** err_ctrl)1795 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1796 struct uvc_fh *handle,
1797 struct uvc_entity *entity,
1798 int rollback,
1799 struct uvc_control **err_ctrl)
1800 {
1801 unsigned int processed_ctrls = 0;
1802 struct uvc_control *ctrl;
1803 unsigned int i;
1804 int ret;
1805
1806 if (entity == NULL)
1807 return 0;
1808
1809 for (i = 0; i < entity->ncontrols; ++i) {
1810 ctrl = &entity->controls[i];
1811 if (!ctrl->initialized)
1812 continue;
1813
1814 /*
1815 * Reset the loaded flag for auto-update controls that were
1816 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1817 * uvc_ctrl_get from using the cached value, and for write-only
1818 * controls to prevent uvc_ctrl_set from setting bits not
1819 * explicitly set by the user.
1820 */
1821 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1822 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1823 ctrl->loaded = 0;
1824
1825 if (!ctrl->dirty)
1826 continue;
1827
1828 if (!rollback)
1829 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1830 dev->intfnum, ctrl->info.selector,
1831 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1832 ctrl->info.size);
1833 else
1834 ret = 0;
1835
1836 if (!ret)
1837 processed_ctrls++;
1838
1839 if (rollback || ret < 0)
1840 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1841 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1842 ctrl->info.size);
1843
1844 ctrl->dirty = 0;
1845
1846 if (ret < 0) {
1847 if (err_ctrl)
1848 *err_ctrl = ctrl;
1849 return ret;
1850 }
1851
1852 if (!rollback && handle &&
1853 ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1854 uvc_ctrl_set_handle(handle, ctrl, handle);
1855 }
1856
1857 return processed_ctrls;
1858 }
1859
uvc_ctrl_find_ctrl_idx(struct uvc_entity * entity,struct v4l2_ext_controls * ctrls,struct uvc_control * uvc_control)1860 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
1861 struct v4l2_ext_controls *ctrls,
1862 struct uvc_control *uvc_control)
1863 {
1864 struct uvc_control_mapping *mapping = NULL;
1865 struct uvc_control *ctrl_found = NULL;
1866 unsigned int i;
1867
1868 if (!entity)
1869 return ctrls->count;
1870
1871 for (i = 0; i < ctrls->count; i++) {
1872 __uvc_find_control(entity, ctrls->controls[i].id, &mapping,
1873 &ctrl_found, 0);
1874 if (uvc_control == ctrl_found)
1875 return i;
1876 }
1877
1878 return ctrls->count;
1879 }
1880
__uvc_ctrl_commit(struct uvc_fh * handle,int rollback,struct v4l2_ext_controls * ctrls)1881 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1882 struct v4l2_ext_controls *ctrls)
1883 {
1884 struct uvc_video_chain *chain = handle->chain;
1885 struct uvc_control *err_ctrl;
1886 struct uvc_entity *entity;
1887 int ret = 0;
1888
1889 /* Find the control. */
1890 list_for_each_entry(entity, &chain->entities, chain) {
1891 ret = uvc_ctrl_commit_entity(chain->dev, handle, entity,
1892 rollback, &err_ctrl);
1893 if (ret < 0) {
1894 if (ctrls)
1895 ctrls->error_idx =
1896 uvc_ctrl_find_ctrl_idx(entity, ctrls,
1897 err_ctrl);
1898 goto done;
1899 } else if (ret > 0 && !rollback) {
1900 uvc_ctrl_send_events(handle, entity,
1901 ctrls->controls, ctrls->count);
1902 }
1903 }
1904
1905 ret = 0;
1906 done:
1907 mutex_unlock(&chain->ctrl_mutex);
1908 return ret;
1909 }
1910
uvc_ctrl_get(struct uvc_video_chain * chain,struct v4l2_ext_control * xctrl)1911 int uvc_ctrl_get(struct uvc_video_chain *chain,
1912 struct v4l2_ext_control *xctrl)
1913 {
1914 struct uvc_control *ctrl;
1915 struct uvc_control_mapping *mapping;
1916
1917 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1918 return -EACCES;
1919
1920 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1921 if (ctrl == NULL)
1922 return -EINVAL;
1923
1924 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1925 }
1926
uvc_ctrl_set(struct uvc_fh * handle,struct v4l2_ext_control * xctrl)1927 int uvc_ctrl_set(struct uvc_fh *handle,
1928 struct v4l2_ext_control *xctrl)
1929 {
1930 struct uvc_video_chain *chain = handle->chain;
1931 struct uvc_control *ctrl;
1932 struct uvc_control_mapping *mapping;
1933 s32 value;
1934 u32 step;
1935 s32 min;
1936 s32 max;
1937 int ret;
1938
1939 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1940 return -EACCES;
1941
1942 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1943 if (ctrl == NULL)
1944 return -EINVAL;
1945 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1946 return -EACCES;
1947
1948 /* Clamp out of range values. */
1949 switch (mapping->v4l2_type) {
1950 case V4L2_CTRL_TYPE_INTEGER:
1951 if (!ctrl->cached) {
1952 ret = uvc_ctrl_populate_cache(chain, ctrl);
1953 if (ret < 0)
1954 return ret;
1955 }
1956
1957 min = mapping->get(mapping, UVC_GET_MIN,
1958 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1959 max = mapping->get(mapping, UVC_GET_MAX,
1960 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1961 step = mapping->get(mapping, UVC_GET_RES,
1962 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1963 if (step == 0)
1964 step = 1;
1965
1966 xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
1967 step) * step;
1968 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1969 xctrl->value = clamp(xctrl->value, min, max);
1970 else
1971 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1972 value = xctrl->value;
1973 break;
1974
1975 case V4L2_CTRL_TYPE_BITMASK:
1976 if (!ctrl->cached) {
1977 ret = uvc_ctrl_populate_cache(chain, ctrl);
1978 if (ret < 0)
1979 return ret;
1980 }
1981
1982 xctrl->value &= uvc_get_ctrl_bitmap(ctrl, mapping);
1983 value = xctrl->value;
1984 break;
1985
1986 case V4L2_CTRL_TYPE_BOOLEAN:
1987 xctrl->value = clamp(xctrl->value, 0, 1);
1988 value = xctrl->value;
1989 break;
1990
1991 case V4L2_CTRL_TYPE_MENU:
1992 if (xctrl->value < (ffs(mapping->menu_mask) - 1) ||
1993 xctrl->value > (fls(mapping->menu_mask) - 1))
1994 return -ERANGE;
1995
1996 if (!test_bit(xctrl->value, &mapping->menu_mask))
1997 return -EINVAL;
1998
1999 value = uvc_mapping_get_menu_value(mapping, xctrl->value);
2000
2001 /*
2002 * Valid menu indices are reported by the GET_RES request for
2003 * UVC controls that support it.
2004 */
2005 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
2006 if (!ctrl->cached) {
2007 ret = uvc_ctrl_populate_cache(chain, ctrl);
2008 if (ret < 0)
2009 return ret;
2010 }
2011
2012 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & value))
2013 return -EINVAL;
2014 }
2015
2016 break;
2017
2018 default:
2019 value = xctrl->value;
2020 break;
2021 }
2022
2023 /*
2024 * If the mapping doesn't span the whole UVC control, the current value
2025 * needs to be loaded from the device to perform the read-modify-write
2026 * operation.
2027 */
2028 if ((ctrl->info.size * 8) != mapping->size) {
2029 ret = __uvc_ctrl_load_cur(chain, ctrl);
2030 if (ret < 0)
2031 return ret;
2032 }
2033
2034 /* Backup the current value in case we need to rollback later. */
2035 if (!ctrl->dirty) {
2036 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2037 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2038 ctrl->info.size);
2039 }
2040
2041 mapping->set(mapping, value,
2042 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2043
2044 ctrl->dirty = 1;
2045 ctrl->modified = 1;
2046 return 0;
2047 }
2048
2049 /* --------------------------------------------------------------------------
2050 * Dynamic controls
2051 */
2052
2053 /*
2054 * Retrieve flags for a given control
2055 */
uvc_ctrl_get_flags(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2056 static int uvc_ctrl_get_flags(struct uvc_device *dev,
2057 const struct uvc_control *ctrl,
2058 struct uvc_control_info *info)
2059 {
2060 u8 *data;
2061 int ret;
2062
2063 data = kmalloc(1, GFP_KERNEL);
2064 if (data == NULL)
2065 return -ENOMEM;
2066
2067 if (ctrl->entity->get_info)
2068 ret = ctrl->entity->get_info(dev, ctrl->entity,
2069 ctrl->info.selector, data);
2070 else
2071 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
2072 dev->intfnum, info->selector, data, 1);
2073
2074 if (!ret) {
2075 info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
2076 UVC_CTRL_FLAG_SET_CUR |
2077 UVC_CTRL_FLAG_AUTO_UPDATE |
2078 UVC_CTRL_FLAG_ASYNCHRONOUS);
2079
2080 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
2081 UVC_CTRL_FLAG_GET_CUR : 0)
2082 | (data[0] & UVC_CONTROL_CAP_SET ?
2083 UVC_CTRL_FLAG_SET_CUR : 0)
2084 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
2085 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
2086 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
2087 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
2088 }
2089
2090 kfree(data);
2091 return ret;
2092 }
2093
uvc_ctrl_fixup_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2094 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
2095 const struct uvc_control *ctrl, struct uvc_control_info *info)
2096 {
2097 struct uvc_ctrl_fixup {
2098 struct usb_device_id id;
2099 u8 entity;
2100 u8 selector;
2101 u8 flags;
2102 };
2103
2104 static const struct uvc_ctrl_fixup fixups[] = {
2105 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2106 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2107 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2108 UVC_CTRL_FLAG_AUTO_UPDATE },
2109 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2110 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2111 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2112 UVC_CTRL_FLAG_AUTO_UPDATE },
2113 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2114 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2115 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2116 UVC_CTRL_FLAG_AUTO_UPDATE },
2117 };
2118
2119 unsigned int i;
2120
2121 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
2122 if (!usb_match_one_id(dev->intf, &fixups[i].id))
2123 continue;
2124
2125 if (fixups[i].entity == ctrl->entity->id &&
2126 fixups[i].selector == info->selector) {
2127 info->flags = fixups[i].flags;
2128 return;
2129 }
2130 }
2131 }
2132
2133 /*
2134 * Query control information (size and flags) for XU controls.
2135 */
uvc_ctrl_fill_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2136 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
2137 const struct uvc_control *ctrl, struct uvc_control_info *info)
2138 {
2139 u8 *data;
2140 int ret;
2141
2142 data = kmalloc(2, GFP_KERNEL);
2143 if (data == NULL)
2144 return -ENOMEM;
2145
2146 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
2147 info->index = ctrl->index;
2148 info->selector = ctrl->index + 1;
2149
2150 /* Query and verify the control length (GET_LEN) */
2151 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
2152 info->selector, data, 2);
2153 if (ret < 0) {
2154 uvc_dbg(dev, CONTROL,
2155 "GET_LEN failed on control %pUl/%u (%d)\n",
2156 info->entity, info->selector, ret);
2157 goto done;
2158 }
2159
2160 info->size = le16_to_cpup((__le16 *)data);
2161
2162 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
2163 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
2164
2165 ret = uvc_ctrl_get_flags(dev, ctrl, info);
2166 if (ret < 0) {
2167 uvc_dbg(dev, CONTROL,
2168 "Failed to get flags for control %pUl/%u (%d)\n",
2169 info->entity, info->selector, ret);
2170 goto done;
2171 }
2172
2173 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
2174
2175 uvc_dbg(dev, CONTROL,
2176 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2177 info->entity, info->selector, info->size,
2178 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
2179 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
2180 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
2181
2182 done:
2183 kfree(data);
2184 return ret;
2185 }
2186
2187 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2188 const struct uvc_control_info *info);
2189
uvc_ctrl_init_xu_ctrl(struct uvc_device * dev,struct uvc_control * ctrl)2190 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
2191 struct uvc_control *ctrl)
2192 {
2193 struct uvc_control_info info;
2194 int ret;
2195
2196 if (ctrl->initialized)
2197 return 0;
2198
2199 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2200 if (ret < 0)
2201 return ret;
2202
2203 ret = uvc_ctrl_add_info(dev, ctrl, &info);
2204 if (ret < 0)
2205 uvc_dbg(dev, CONTROL,
2206 "Failed to initialize control %pUl/%u on device %s entity %u\n",
2207 info.entity, info.selector, dev->udev->devpath,
2208 ctrl->entity->id);
2209
2210 return ret;
2211 }
2212
uvc_xu_ctrl_query(struct uvc_video_chain * chain,struct uvc_xu_control_query * xqry)2213 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2214 struct uvc_xu_control_query *xqry)
2215 {
2216 struct uvc_entity *entity, *iter;
2217 struct uvc_control *ctrl;
2218 unsigned int i;
2219 bool found;
2220 u32 reqflags;
2221 u16 size;
2222 u8 *data = NULL;
2223 int ret;
2224
2225 /* Find the extension unit. */
2226 entity = NULL;
2227 list_for_each_entry(iter, &chain->entities, chain) {
2228 if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
2229 iter->id == xqry->unit) {
2230 entity = iter;
2231 break;
2232 }
2233 }
2234
2235 if (!entity) {
2236 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2237 xqry->unit);
2238 return -ENOENT;
2239 }
2240
2241 /* Find the control and perform delayed initialization if needed. */
2242 found = false;
2243 for (i = 0; i < entity->ncontrols; ++i) {
2244 ctrl = &entity->controls[i];
2245 if (ctrl->index == xqry->selector - 1) {
2246 found = true;
2247 break;
2248 }
2249 }
2250
2251 if (!found) {
2252 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2253 entity->guid, xqry->selector);
2254 return -ENOENT;
2255 }
2256
2257 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2258 return -ERESTARTSYS;
2259
2260 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2261 if (ret < 0) {
2262 ret = -ENOENT;
2263 goto done;
2264 }
2265
2266 /* Validate the required buffer size and flags for the request */
2267 reqflags = 0;
2268 size = ctrl->info.size;
2269
2270 switch (xqry->query) {
2271 case UVC_GET_CUR:
2272 reqflags = UVC_CTRL_FLAG_GET_CUR;
2273 break;
2274 case UVC_GET_MIN:
2275 reqflags = UVC_CTRL_FLAG_GET_MIN;
2276 break;
2277 case UVC_GET_MAX:
2278 reqflags = UVC_CTRL_FLAG_GET_MAX;
2279 break;
2280 case UVC_GET_DEF:
2281 reqflags = UVC_CTRL_FLAG_GET_DEF;
2282 break;
2283 case UVC_GET_RES:
2284 reqflags = UVC_CTRL_FLAG_GET_RES;
2285 break;
2286 case UVC_SET_CUR:
2287 reqflags = UVC_CTRL_FLAG_SET_CUR;
2288 break;
2289 case UVC_GET_LEN:
2290 size = 2;
2291 break;
2292 case UVC_GET_INFO:
2293 size = 1;
2294 break;
2295 default:
2296 ret = -EINVAL;
2297 goto done;
2298 }
2299
2300 if (size != xqry->size) {
2301 ret = -ENOBUFS;
2302 goto done;
2303 }
2304
2305 if (reqflags && !(ctrl->info.flags & reqflags)) {
2306 ret = -EBADRQC;
2307 goto done;
2308 }
2309
2310 data = kmalloc(size, GFP_KERNEL);
2311 if (data == NULL) {
2312 ret = -ENOMEM;
2313 goto done;
2314 }
2315
2316 if (xqry->query == UVC_SET_CUR &&
2317 copy_from_user(data, xqry->data, size)) {
2318 ret = -EFAULT;
2319 goto done;
2320 }
2321
2322 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2323 chain->dev->intfnum, xqry->selector, data, size);
2324 if (ret < 0)
2325 goto done;
2326
2327 if (xqry->query != UVC_SET_CUR &&
2328 copy_to_user(xqry->data, data, size))
2329 ret = -EFAULT;
2330 done:
2331 kfree(data);
2332 mutex_unlock(&chain->ctrl_mutex);
2333 return ret;
2334 }
2335
2336 /* --------------------------------------------------------------------------
2337 * Suspend/resume
2338 */
2339
2340 /*
2341 * Restore control values after resume, skipping controls that haven't been
2342 * changed.
2343 *
2344 * TODO
2345 * - Don't restore modified controls that are back to their default value.
2346 * - Handle restore order (Auto-Exposure Mode should be restored before
2347 * Exposure Time).
2348 */
uvc_ctrl_restore_values(struct uvc_device * dev)2349 int uvc_ctrl_restore_values(struct uvc_device *dev)
2350 {
2351 struct uvc_control *ctrl;
2352 struct uvc_entity *entity;
2353 unsigned int i;
2354 int ret;
2355
2356 /* Walk the entities list and restore controls when possible. */
2357 list_for_each_entry(entity, &dev->entities, list) {
2358
2359 for (i = 0; i < entity->ncontrols; ++i) {
2360 ctrl = &entity->controls[i];
2361
2362 if (!ctrl->initialized || !ctrl->modified ||
2363 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2364 continue;
2365 dev_dbg(&dev->udev->dev,
2366 "restoring control %pUl/%u/%u\n",
2367 ctrl->info.entity, ctrl->info.index,
2368 ctrl->info.selector);
2369 ctrl->dirty = 1;
2370 }
2371
2372 ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
2373 if (ret < 0)
2374 return ret;
2375 }
2376
2377 return 0;
2378 }
2379
2380 /* --------------------------------------------------------------------------
2381 * Control and mapping handling
2382 */
2383
2384 /*
2385 * Add control information to a given control.
2386 */
uvc_ctrl_add_info(struct uvc_device * dev,struct uvc_control * ctrl,const struct uvc_control_info * info)2387 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2388 const struct uvc_control_info *info)
2389 {
2390 ctrl->info = *info;
2391 INIT_LIST_HEAD(&ctrl->info.mappings);
2392
2393 /* Allocate an array to save control values (cur, def, max, etc.) */
2394 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2395 GFP_KERNEL);
2396 if (!ctrl->uvc_data)
2397 return -ENOMEM;
2398
2399 ctrl->initialized = 1;
2400
2401 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2402 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2403 ctrl->entity->id);
2404
2405 return 0;
2406 }
2407
2408 /*
2409 * Add a control mapping to a given control.
2410 */
__uvc_ctrl_add_mapping(struct uvc_video_chain * chain,struct uvc_control * ctrl,const struct uvc_control_mapping * mapping)2411 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2412 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2413 {
2414 struct uvc_control_mapping *map;
2415 unsigned int size;
2416 unsigned int i;
2417
2418 /*
2419 * Most mappings come from static kernel data, and need to be duplicated.
2420 * Mappings that come from userspace will be unnecessarily duplicated,
2421 * this could be optimized.
2422 */
2423 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2424 if (!map)
2425 return -ENOMEM;
2426
2427 map->name = NULL;
2428 map->menu_names = NULL;
2429 map->menu_mapping = NULL;
2430
2431 /* For UVCIOC_CTRL_MAP custom control */
2432 if (mapping->name) {
2433 map->name = kstrdup(mapping->name, GFP_KERNEL);
2434 if (!map->name)
2435 goto err_nomem;
2436 }
2437
2438 INIT_LIST_HEAD(&map->ev_subs);
2439
2440 if (mapping->menu_mapping && mapping->menu_mask) {
2441 size = sizeof(mapping->menu_mapping[0])
2442 * fls(mapping->menu_mask);
2443 map->menu_mapping = kmemdup(mapping->menu_mapping, size,
2444 GFP_KERNEL);
2445 if (!map->menu_mapping)
2446 goto err_nomem;
2447 }
2448 if (mapping->menu_names && mapping->menu_mask) {
2449 size = sizeof(mapping->menu_names[0])
2450 * fls(mapping->menu_mask);
2451 map->menu_names = kmemdup(mapping->menu_names, size,
2452 GFP_KERNEL);
2453 if (!map->menu_names)
2454 goto err_nomem;
2455 }
2456
2457 if (map->get == NULL)
2458 map->get = uvc_get_le_value;
2459 if (map->set == NULL)
2460 map->set = uvc_set_le_value;
2461
2462 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
2463 if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
2464 V4L2_CTRL_ID2WHICH(map->id)) {
2465 chain->ctrl_class_bitmap |= BIT(i);
2466 break;
2467 }
2468 }
2469
2470 list_add_tail(&map->list, &ctrl->info.mappings);
2471 uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2472 uvc_map_get_name(map), ctrl->info.entity,
2473 ctrl->info.selector);
2474
2475 return 0;
2476
2477 err_nomem:
2478 kfree(map->menu_names);
2479 kfree(map->menu_mapping);
2480 kfree(map->name);
2481 kfree(map);
2482 return -ENOMEM;
2483 }
2484
uvc_ctrl_add_mapping(struct uvc_video_chain * chain,const struct uvc_control_mapping * mapping)2485 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2486 const struct uvc_control_mapping *mapping)
2487 {
2488 struct uvc_device *dev = chain->dev;
2489 struct uvc_control_mapping *map;
2490 struct uvc_entity *entity;
2491 struct uvc_control *ctrl;
2492 int found = 0;
2493 int ret;
2494
2495 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2496 uvc_dbg(dev, CONTROL,
2497 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2498 uvc_map_get_name(mapping), mapping->id);
2499 return -EINVAL;
2500 }
2501
2502 /* Search for the matching (GUID/CS) control on the current chain */
2503 list_for_each_entry(entity, &chain->entities, chain) {
2504 unsigned int i;
2505
2506 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2507 !uvc_entity_match_guid(entity, mapping->entity))
2508 continue;
2509
2510 for (i = 0; i < entity->ncontrols; ++i) {
2511 ctrl = &entity->controls[i];
2512 if (ctrl->index == mapping->selector - 1) {
2513 found = 1;
2514 break;
2515 }
2516 }
2517
2518 if (found)
2519 break;
2520 }
2521 if (!found)
2522 return -ENOENT;
2523
2524 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2525 return -ERESTARTSYS;
2526
2527 /* Perform delayed initialization of XU controls */
2528 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2529 if (ret < 0) {
2530 ret = -ENOENT;
2531 goto done;
2532 }
2533
2534 /* Validate the user-provided bit-size and offset */
2535 if (mapping->size > 32 ||
2536 mapping->offset + mapping->size > ctrl->info.size * 8) {
2537 ret = -EINVAL;
2538 goto done;
2539 }
2540
2541 list_for_each_entry(map, &ctrl->info.mappings, list) {
2542 if (mapping->id == map->id) {
2543 uvc_dbg(dev, CONTROL,
2544 "Can't add mapping '%s', control id 0x%08x already exists\n",
2545 uvc_map_get_name(mapping), mapping->id);
2546 ret = -EEXIST;
2547 goto done;
2548 }
2549 }
2550
2551 /* Prevent excess memory consumption */
2552 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2553 atomic_dec(&dev->nmappings);
2554 uvc_dbg(dev, CONTROL,
2555 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
2556 uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
2557 ret = -ENOMEM;
2558 goto done;
2559 }
2560
2561 ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2562 if (ret < 0)
2563 atomic_dec(&dev->nmappings);
2564
2565 done:
2566 mutex_unlock(&chain->ctrl_mutex);
2567 return ret;
2568 }
2569
2570 /*
2571 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2572 * are currently the ones that crash the camera or unconditionally return an
2573 * error when queried.
2574 */
uvc_ctrl_prune_entity(struct uvc_device * dev,struct uvc_entity * entity)2575 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2576 struct uvc_entity *entity)
2577 {
2578 struct uvc_ctrl_blacklist {
2579 struct usb_device_id id;
2580 u8 index;
2581 };
2582
2583 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2584 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2585 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2586 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2587 };
2588 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2589 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2590 };
2591
2592 const struct uvc_ctrl_blacklist *blacklist;
2593 unsigned int size;
2594 unsigned int count;
2595 unsigned int i;
2596 u8 *controls;
2597
2598 switch (UVC_ENTITY_TYPE(entity)) {
2599 case UVC_VC_PROCESSING_UNIT:
2600 blacklist = processing_blacklist;
2601 count = ARRAY_SIZE(processing_blacklist);
2602 controls = entity->processing.bmControls;
2603 size = entity->processing.bControlSize;
2604 break;
2605
2606 case UVC_ITT_CAMERA:
2607 blacklist = camera_blacklist;
2608 count = ARRAY_SIZE(camera_blacklist);
2609 controls = entity->camera.bmControls;
2610 size = entity->camera.bControlSize;
2611 break;
2612
2613 default:
2614 return;
2615 }
2616
2617 for (i = 0; i < count; ++i) {
2618 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2619 continue;
2620
2621 if (blacklist[i].index >= 8 * size ||
2622 !uvc_test_bit(controls, blacklist[i].index))
2623 continue;
2624
2625 uvc_dbg(dev, CONTROL,
2626 "%u/%u control is black listed, removing it\n",
2627 entity->id, blacklist[i].index);
2628
2629 uvc_clear_bit(controls, blacklist[i].index);
2630 }
2631 }
2632
2633 /*
2634 * Add control information and hardcoded stock control mappings to the given
2635 * device.
2636 */
uvc_ctrl_init_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl)2637 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
2638 struct uvc_control *ctrl)
2639 {
2640 const struct uvc_control_mapping **mappings;
2641 unsigned int i;
2642
2643 /*
2644 * XU controls initialization requires querying the device for control
2645 * information. As some buggy UVC devices will crash when queried
2646 * repeatedly in a tight loop, delay XU controls initialization until
2647 * first use.
2648 */
2649 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2650 return;
2651
2652 for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
2653 const struct uvc_control_info *info = &uvc_ctrls[i];
2654
2655 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2656 ctrl->index == info->index) {
2657 uvc_ctrl_add_info(chain->dev, ctrl, info);
2658 /*
2659 * Retrieve control flags from the device. Ignore errors
2660 * and work with default flag values from the uvc_ctrl
2661 * array when the device doesn't properly implement
2662 * GET_INFO on standard controls.
2663 */
2664 uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
2665 break;
2666 }
2667 }
2668
2669 if (!ctrl->initialized)
2670 return;
2671
2672 /*
2673 * First check if the device provides a custom mapping for this control,
2674 * used to override standard mappings for non-conformant devices. Don't
2675 * process standard mappings if a custom mapping is found. This
2676 * mechanism doesn't support combining standard and custom mappings for
2677 * a single control.
2678 */
2679 if (chain->dev->info->mappings) {
2680 bool custom = false;
2681
2682 for (i = 0; chain->dev->info->mappings[i]; ++i) {
2683 const struct uvc_control_mapping *mapping =
2684 chain->dev->info->mappings[i];
2685
2686 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2687 ctrl->info.selector == mapping->selector) {
2688 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2689 custom = true;
2690 }
2691 }
2692
2693 if (custom)
2694 return;
2695 }
2696
2697 /* Process common mappings next. */
2698 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
2699 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
2700
2701 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2702 ctrl->info.selector == mapping->selector)
2703 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2704 }
2705
2706 /* Finally process version-specific mappings. */
2707 mappings = chain->dev->uvc_version < 0x0150
2708 ? uvc_ctrl_mappings_uvc11 : uvc_ctrl_mappings_uvc15;
2709
2710 for (i = 0; mappings[i]; ++i) {
2711 const struct uvc_control_mapping *mapping = mappings[i];
2712
2713 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2714 ctrl->info.selector == mapping->selector)
2715 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2716 }
2717 }
2718
2719 /*
2720 * Initialize device controls.
2721 */
uvc_ctrl_init_chain(struct uvc_video_chain * chain)2722 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
2723 {
2724 struct uvc_entity *entity;
2725 unsigned int i;
2726
2727 /* Walk the entities list and instantiate controls */
2728 list_for_each_entry(entity, &chain->entities, chain) {
2729 struct uvc_control *ctrl;
2730 unsigned int bControlSize = 0, ncontrols;
2731 u8 *bmControls = NULL;
2732
2733 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2734 bmControls = entity->extension.bmControls;
2735 bControlSize = entity->extension.bControlSize;
2736 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2737 bmControls = entity->processing.bmControls;
2738 bControlSize = entity->processing.bControlSize;
2739 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2740 bmControls = entity->camera.bmControls;
2741 bControlSize = entity->camera.bControlSize;
2742 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
2743 bmControls = entity->gpio.bmControls;
2744 bControlSize = entity->gpio.bControlSize;
2745 }
2746
2747 /* Remove bogus/blacklisted controls */
2748 uvc_ctrl_prune_entity(chain->dev, entity);
2749
2750 /* Count supported controls and allocate the controls array */
2751 ncontrols = memweight(bmControls, bControlSize);
2752 if (ncontrols == 0)
2753 continue;
2754
2755 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2756 GFP_KERNEL);
2757 if (entity->controls == NULL)
2758 return -ENOMEM;
2759 entity->ncontrols = ncontrols;
2760
2761 /* Initialize all supported controls */
2762 ctrl = entity->controls;
2763 for (i = 0; i < bControlSize * 8; ++i) {
2764 if (uvc_test_bit(bmControls, i) == 0)
2765 continue;
2766
2767 ctrl->entity = entity;
2768 ctrl->index = i;
2769
2770 uvc_ctrl_init_ctrl(chain, ctrl);
2771 ctrl++;
2772 }
2773 }
2774
2775 return 0;
2776 }
2777
uvc_ctrl_init_device(struct uvc_device * dev)2778 int uvc_ctrl_init_device(struct uvc_device *dev)
2779 {
2780 struct uvc_video_chain *chain;
2781 int ret;
2782
2783 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2784
2785 list_for_each_entry(chain, &dev->chains, list) {
2786 ret = uvc_ctrl_init_chain(chain);
2787 if (ret)
2788 return ret;
2789 }
2790
2791 return 0;
2792 }
2793
uvc_ctrl_cleanup_fh(struct uvc_fh * handle)2794 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
2795 {
2796 struct uvc_entity *entity;
2797
2798 guard(mutex)(&handle->chain->ctrl_mutex);
2799
2800 if (!handle->pending_async_ctrls)
2801 return;
2802
2803 list_for_each_entry(entity, &handle->chain->dev->entities, list) {
2804 for (unsigned int i = 0; i < entity->ncontrols; ++i) {
2805 if (entity->controls[i].handle != handle)
2806 continue;
2807 uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
2808 }
2809 }
2810
2811 WARN_ON(handle->pending_async_ctrls);
2812 }
2813
2814 /*
2815 * Cleanup device controls.
2816 */
uvc_ctrl_cleanup_mappings(struct uvc_device * dev,struct uvc_control * ctrl)2817 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2818 struct uvc_control *ctrl)
2819 {
2820 struct uvc_control_mapping *mapping, *nm;
2821
2822 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2823 list_del(&mapping->list);
2824 kfree(mapping->menu_names);
2825 kfree(mapping->menu_mapping);
2826 kfree(mapping->name);
2827 kfree(mapping);
2828 }
2829 }
2830
uvc_ctrl_cleanup_device(struct uvc_device * dev)2831 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2832 {
2833 struct uvc_entity *entity;
2834 unsigned int i;
2835
2836 /* Can be uninitialized if we are aborting on probe error. */
2837 if (dev->async_ctrl.work.func)
2838 cancel_work_sync(&dev->async_ctrl.work);
2839
2840 /* Free controls and control mappings for all entities. */
2841 list_for_each_entry(entity, &dev->entities, list) {
2842 for (i = 0; i < entity->ncontrols; ++i) {
2843 struct uvc_control *ctrl = &entity->controls[i];
2844
2845 if (!ctrl->initialized)
2846 continue;
2847
2848 uvc_ctrl_cleanup_mappings(dev, ctrl);
2849 kfree(ctrl->uvc_data);
2850 }
2851
2852 kfree(entity->controls);
2853 }
2854 }
2855