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
818 /*
819 * Extract the bit string specified by mapping->offset and mapping->size
820 * from the little-endian data stored at 'data' and return the result as
821 * a signed 32bit integer. Sign extension will be performed if the mapping
822 * references a signed data type.
823 */
uvc_get_le_value(struct uvc_control_mapping * mapping,u8 query,const u8 * data)824 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
825 u8 query, const u8 *data)
826 {
827 int bits = mapping->size;
828 int offset = mapping->offset;
829 s32 value = 0;
830 u8 mask;
831
832 data += offset / 8;
833 offset &= 7;
834 mask = ((1LL << bits) - 1) << offset;
835
836 while (1) {
837 u8 byte = *data & mask;
838 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
839 bits -= 8 - (offset > 0 ? offset : 0);
840 if (bits <= 0)
841 break;
842
843 offset -= 8;
844 mask = (1 << bits) - 1;
845 data++;
846 }
847
848 /* Sign-extend the value if needed. */
849 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
850 value |= -(value & (1 << (mapping->size - 1)));
851
852 return value;
853 }
854
855 /*
856 * Set the bit string specified by mapping->offset and mapping->size
857 * in the little-endian data stored at 'data' to the value 'value'.
858 */
uvc_set_le_value(struct uvc_control_mapping * mapping,s32 value,u8 * data)859 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
860 s32 value, u8 *data)
861 {
862 int bits = mapping->size;
863 int offset = mapping->offset;
864 u8 mask;
865
866 /*
867 * According to the v4l2 spec, writing any value to a button control
868 * should result in the action belonging to the button control being
869 * triggered. UVC devices however want to see a 1 written -> override
870 * value.
871 */
872 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
873 value = -1;
874
875 data += offset / 8;
876 offset &= 7;
877
878 for (; bits > 0; data++) {
879 mask = ((1LL << bits) - 1) << offset;
880 *data = (*data & ~mask) | ((value << offset) & mask);
881 value >>= offset ? offset : 8;
882 bits -= 8 - offset;
883 offset = 0;
884 }
885 }
886
887 /* ------------------------------------------------------------------------
888 * Terminal and unit management
889 */
890
uvc_entity_match_guid(const struct uvc_entity * entity,const u8 guid[16])891 static int uvc_entity_match_guid(const struct uvc_entity *entity,
892 const u8 guid[16])
893 {
894 return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
895 }
896
897 /* ------------------------------------------------------------------------
898 * UVC Controls
899 */
900
__uvc_find_control(struct uvc_entity * entity,u32 v4l2_id,struct uvc_control_mapping ** mapping,struct uvc_control ** control,int next)901 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
902 struct uvc_control_mapping **mapping, struct uvc_control **control,
903 int next)
904 {
905 struct uvc_control *ctrl;
906 struct uvc_control_mapping *map;
907 unsigned int i;
908
909 if (entity == NULL)
910 return;
911
912 for (i = 0; i < entity->ncontrols; ++i) {
913 ctrl = &entity->controls[i];
914 if (!ctrl->initialized)
915 continue;
916
917 list_for_each_entry(map, &ctrl->info.mappings, list) {
918 if ((map->id == v4l2_id) && !next) {
919 *control = ctrl;
920 *mapping = map;
921 return;
922 }
923
924 if ((*mapping == NULL || (*mapping)->id > map->id) &&
925 (map->id > v4l2_id) && next) {
926 *control = ctrl;
927 *mapping = map;
928 }
929 }
930 }
931 }
932
uvc_find_control(struct uvc_video_chain * chain,u32 v4l2_id,struct uvc_control_mapping ** mapping)933 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
934 u32 v4l2_id, struct uvc_control_mapping **mapping)
935 {
936 struct uvc_control *ctrl = NULL;
937 struct uvc_entity *entity;
938 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
939
940 *mapping = NULL;
941
942 /* Mask the query flags. */
943 v4l2_id &= V4L2_CTRL_ID_MASK;
944
945 /* Find the control. */
946 list_for_each_entry(entity, &chain->entities, chain) {
947 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
948 if (ctrl && !next)
949 return ctrl;
950 }
951
952 if (ctrl == NULL && !next)
953 uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
954 v4l2_id);
955
956 return ctrl;
957 }
958
uvc_ctrl_populate_cache(struct uvc_video_chain * chain,struct uvc_control * ctrl)959 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
960 struct uvc_control *ctrl)
961 {
962 int ret;
963
964 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
965 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
966 chain->dev->intfnum, ctrl->info.selector,
967 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
968 ctrl->info.size);
969 if (ret < 0)
970 return ret;
971 }
972
973 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
974 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
975 chain->dev->intfnum, ctrl->info.selector,
976 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
977 ctrl->info.size);
978 if (ret < 0)
979 return ret;
980 }
981 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
982 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
983 chain->dev->intfnum, ctrl->info.selector,
984 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
985 ctrl->info.size);
986 if (ret < 0)
987 return ret;
988 }
989 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
990 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
991 chain->dev->intfnum, ctrl->info.selector,
992 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
993 ctrl->info.size);
994 if (ret < 0) {
995 if (UVC_ENTITY_TYPE(ctrl->entity) !=
996 UVC_VC_EXTENSION_UNIT)
997 return ret;
998
999 /*
1000 * GET_RES is mandatory for XU controls, but some
1001 * cameras still choke on it. Ignore errors and set the
1002 * resolution value to zero.
1003 */
1004 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
1005 "UVC non compliance - GET_RES failed on "
1006 "an XU control. Enabling workaround.\n");
1007 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
1008 ctrl->info.size);
1009 }
1010 }
1011
1012 ctrl->cached = 1;
1013 return 0;
1014 }
1015
__uvc_ctrl_get_value(struct uvc_control_mapping * mapping,const u8 * data)1016 static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
1017 const u8 *data)
1018 {
1019 s32 value = mapping->get(mapping, UVC_GET_CUR, data);
1020
1021 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1022 unsigned int i;
1023
1024 for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
1025 u32 menu_value;
1026
1027 if (!test_bit(i, &mapping->menu_mask))
1028 continue;
1029
1030 menu_value = uvc_mapping_get_menu_value(mapping, i);
1031
1032 if (menu_value == value) {
1033 value = i;
1034 break;
1035 }
1036 }
1037 }
1038
1039 return value;
1040 }
1041
__uvc_ctrl_load_cur(struct uvc_video_chain * chain,struct uvc_control * ctrl)1042 static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
1043 struct uvc_control *ctrl)
1044 {
1045 u8 *data;
1046 int ret;
1047
1048 if (ctrl->loaded)
1049 return 0;
1050
1051 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1052
1053 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1054 memset(data, 0, ctrl->info.size);
1055 ctrl->loaded = 1;
1056
1057 return 0;
1058 }
1059
1060 if (ctrl->entity->get_cur)
1061 ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
1062 ctrl->info.selector, data,
1063 ctrl->info.size);
1064 else
1065 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1066 ctrl->entity->id, chain->dev->intfnum,
1067 ctrl->info.selector, data,
1068 ctrl->info.size);
1069
1070 if (ret < 0)
1071 return ret;
1072
1073 ctrl->loaded = 1;
1074
1075 return ret;
1076 }
1077
__uvc_ctrl_get(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,s32 * value)1078 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1079 struct uvc_control *ctrl,
1080 struct uvc_control_mapping *mapping,
1081 s32 *value)
1082 {
1083 int ret;
1084
1085 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1086 return -EACCES;
1087
1088 ret = __uvc_ctrl_load_cur(chain, ctrl);
1089 if (ret < 0)
1090 return ret;
1091
1092 *value = __uvc_ctrl_get_value(mapping,
1093 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1094
1095 return 0;
1096 }
1097
__uvc_query_v4l2_class(struct uvc_video_chain * chain,u32 req_id,u32 found_id)1098 static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1099 u32 found_id)
1100 {
1101 bool find_next = req_id & V4L2_CTRL_FLAG_NEXT_CTRL;
1102 unsigned int i;
1103
1104 req_id &= V4L2_CTRL_ID_MASK;
1105
1106 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
1107 if (!(chain->ctrl_class_bitmap & BIT(i)))
1108 continue;
1109 if (!find_next) {
1110 if (uvc_control_classes[i] == req_id)
1111 return i;
1112 continue;
1113 }
1114 if (uvc_control_classes[i] > req_id &&
1115 uvc_control_classes[i] < found_id)
1116 return i;
1117 }
1118
1119 return -ENODEV;
1120 }
1121
uvc_query_v4l2_class(struct uvc_video_chain * chain,u32 req_id,u32 found_id,struct v4l2_queryctrl * v4l2_ctrl)1122 static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1123 u32 found_id, struct v4l2_queryctrl *v4l2_ctrl)
1124 {
1125 int idx;
1126
1127 idx = __uvc_query_v4l2_class(chain, req_id, found_id);
1128 if (idx < 0)
1129 return -ENODEV;
1130
1131 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1132 v4l2_ctrl->id = uvc_control_classes[idx];
1133 strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
1134 sizeof(v4l2_ctrl->name));
1135 v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
1136 v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
1137 | V4L2_CTRL_FLAG_READ_ONLY;
1138 return 0;
1139 }
1140
1141 /*
1142 * Check if control @v4l2_id can be accessed by the given control @ioctl
1143 * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
1144 *
1145 * For set operations on slave controls, check if the master's value is set to
1146 * manual, either in the others controls set in the same ioctl call, or from
1147 * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
1148 * both the master and slave control, such as for instance setting
1149 * auto_exposure=1, exposure_time_absolute=251.
1150 */
uvc_ctrl_is_accessible(struct uvc_video_chain * chain,u32 v4l2_id,const struct v4l2_ext_controls * ctrls,unsigned long ioctl)1151 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
1152 const struct v4l2_ext_controls *ctrls,
1153 unsigned long ioctl)
1154 {
1155 struct uvc_control_mapping *master_map = NULL;
1156 struct uvc_control *master_ctrl = NULL;
1157 struct uvc_control_mapping *mapping;
1158 struct uvc_control *ctrl;
1159 bool read = ioctl == VIDIOC_G_EXT_CTRLS;
1160 s32 val;
1161 int ret;
1162 int i;
1163
1164 if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
1165 return -EACCES;
1166
1167 ctrl = uvc_find_control(chain, v4l2_id, &mapping);
1168 if (!ctrl)
1169 return -EINVAL;
1170
1171 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read)
1172 return -EACCES;
1173
1174 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read)
1175 return -EACCES;
1176
1177 if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id)
1178 return 0;
1179
1180 /*
1181 * Iterate backwards in cases where the master control is accessed
1182 * multiple times in the same ioctl. We want the last value.
1183 */
1184 for (i = ctrls->count - 1; i >= 0; i--) {
1185 if (ctrls->controls[i].id == mapping->master_id)
1186 return ctrls->controls[i].value ==
1187 mapping->master_manual ? 0 : -EACCES;
1188 }
1189
1190 __uvc_find_control(ctrl->entity, mapping->master_id, &master_map,
1191 &master_ctrl, 0);
1192
1193 if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1194 return 0;
1195
1196 ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1197 if (ret >= 0 && val != mapping->master_manual)
1198 return -EACCES;
1199
1200 return 0;
1201 }
1202
uvc_map_get_name(const struct uvc_control_mapping * map)1203 static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
1204 {
1205 const char *name;
1206
1207 if (map->name)
1208 return map->name;
1209
1210 name = v4l2_ctrl_get_name(map->id);
1211 if (name)
1212 return name;
1213
1214 return "Unknown Control";
1215 }
1216
uvc_get_ctrl_bitmap(struct uvc_control * ctrl,struct uvc_control_mapping * mapping)1217 static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl,
1218 struct uvc_control_mapping *mapping)
1219 {
1220 /*
1221 * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
1222 * the number of bits supported. Those controls do not list GET_MAX
1223 * as supported.
1224 */
1225 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1226 return mapping->get(mapping, UVC_GET_RES,
1227 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1228
1229 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1230 return mapping->get(mapping, UVC_GET_MAX,
1231 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1232
1233 return ~0;
1234 }
1235
__uvc_query_v4l2_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_queryctrl * v4l2_ctrl)1236 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1237 struct uvc_control *ctrl,
1238 struct uvc_control_mapping *mapping,
1239 struct v4l2_queryctrl *v4l2_ctrl)
1240 {
1241 struct uvc_control_mapping *master_map = NULL;
1242 struct uvc_control *master_ctrl = NULL;
1243 unsigned int i;
1244
1245 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1246 v4l2_ctrl->id = mapping->id;
1247 v4l2_ctrl->type = mapping->v4l2_type;
1248 strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
1249 sizeof(v4l2_ctrl->name));
1250 v4l2_ctrl->flags = 0;
1251
1252 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1253 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1254 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1255 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1256
1257 if (mapping->master_id)
1258 __uvc_find_control(ctrl->entity, mapping->master_id,
1259 &master_map, &master_ctrl, 0);
1260 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1261 s32 val;
1262 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1263 if (ret < 0)
1264 return ret;
1265
1266 if (val != mapping->master_manual)
1267 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1268 }
1269
1270 if (!ctrl->cached) {
1271 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1272 if (ret < 0)
1273 return ret;
1274 }
1275
1276 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1277 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1278 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1279 }
1280
1281 switch (mapping->v4l2_type) {
1282 case V4L2_CTRL_TYPE_MENU:
1283 v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1;
1284 v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1;
1285 v4l2_ctrl->step = 1;
1286
1287 for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
1288 u32 menu_value;
1289
1290 if (!test_bit(i, &mapping->menu_mask))
1291 continue;
1292
1293 menu_value = uvc_mapping_get_menu_value(mapping, i);
1294
1295 if (menu_value == v4l2_ctrl->default_value) {
1296 v4l2_ctrl->default_value = i;
1297 break;
1298 }
1299 }
1300
1301 return 0;
1302
1303 case V4L2_CTRL_TYPE_BOOLEAN:
1304 v4l2_ctrl->minimum = 0;
1305 v4l2_ctrl->maximum = 1;
1306 v4l2_ctrl->step = 1;
1307 return 0;
1308
1309 case V4L2_CTRL_TYPE_BUTTON:
1310 v4l2_ctrl->minimum = 0;
1311 v4l2_ctrl->maximum = 0;
1312 v4l2_ctrl->step = 0;
1313 return 0;
1314
1315 case V4L2_CTRL_TYPE_BITMASK:
1316 v4l2_ctrl->minimum = 0;
1317 v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping);
1318 v4l2_ctrl->step = 0;
1319 return 0;
1320
1321 default:
1322 break;
1323 }
1324
1325 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1326 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1327 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1328
1329 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1330 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1331 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1332
1333 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1334 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1335 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1336
1337 return 0;
1338 }
1339
uvc_query_v4l2_ctrl(struct uvc_video_chain * chain,struct v4l2_queryctrl * v4l2_ctrl)1340 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1341 struct v4l2_queryctrl *v4l2_ctrl)
1342 {
1343 struct uvc_control *ctrl;
1344 struct uvc_control_mapping *mapping;
1345 int ret;
1346
1347 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1348 if (ret < 0)
1349 return -ERESTARTSYS;
1350
1351 /* Check if the ctrl is a know class */
1352 if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
1353 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
1354 if (!ret)
1355 goto done;
1356 }
1357
1358 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1359 if (ctrl == NULL) {
1360 ret = -EINVAL;
1361 goto done;
1362 }
1363
1364 /*
1365 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
1366 * a class should be inserted between the previous control and the one
1367 * we have just found.
1368 */
1369 if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1370 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
1371 v4l2_ctrl);
1372 if (!ret)
1373 goto done;
1374 }
1375
1376 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1377 done:
1378 mutex_unlock(&chain->ctrl_mutex);
1379 return ret;
1380 }
1381
1382 /*
1383 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1384 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1385 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1386 * Balance V4L2 controls use the White Balance Component UVC control) or
1387 * otherwise translated. The approach we take here is to use a translation
1388 * table for the controls that can be mapped directly, and handle the others
1389 * manually.
1390 */
uvc_query_v4l2_menu(struct uvc_video_chain * chain,struct v4l2_querymenu * query_menu)1391 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1392 struct v4l2_querymenu *query_menu)
1393 {
1394 struct uvc_control_mapping *mapping;
1395 struct uvc_control *ctrl;
1396 u32 index = query_menu->index;
1397 u32 id = query_menu->id;
1398 const char *name;
1399 int ret;
1400
1401 memset(query_menu, 0, sizeof(*query_menu));
1402 query_menu->id = id;
1403 query_menu->index = index;
1404
1405 if (index >= BITS_PER_TYPE(mapping->menu_mask))
1406 return -EINVAL;
1407
1408 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1409 if (ret < 0)
1410 return -ERESTARTSYS;
1411
1412 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1413 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1414 ret = -EINVAL;
1415 goto done;
1416 }
1417
1418 if (!test_bit(query_menu->index, &mapping->menu_mask)) {
1419 ret = -EINVAL;
1420 goto done;
1421 }
1422
1423 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
1424 int mask;
1425
1426 if (!ctrl->cached) {
1427 ret = uvc_ctrl_populate_cache(chain, ctrl);
1428 if (ret < 0)
1429 goto done;
1430 }
1431
1432 mask = uvc_mapping_get_menu_value(mapping, query_menu->index);
1433 if (mask < 0) {
1434 ret = mask;
1435 goto done;
1436 }
1437
1438 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) {
1439 ret = -EINVAL;
1440 goto done;
1441 }
1442 }
1443
1444 name = uvc_mapping_get_menu_name(mapping, query_menu->index);
1445 if (!name) {
1446 ret = -EINVAL;
1447 goto done;
1448 }
1449
1450 strscpy(query_menu->name, name, sizeof(query_menu->name));
1451
1452 done:
1453 mutex_unlock(&chain->ctrl_mutex);
1454 return ret;
1455 }
1456
1457 /* --------------------------------------------------------------------------
1458 * Ctrl event handling
1459 */
1460
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)1461 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1462 struct v4l2_event *ev,
1463 struct uvc_control *ctrl,
1464 struct uvc_control_mapping *mapping,
1465 s32 value, u32 changes)
1466 {
1467 struct v4l2_queryctrl v4l2_ctrl;
1468
1469 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1470
1471 memset(ev, 0, sizeof(*ev));
1472 ev->type = V4L2_EVENT_CTRL;
1473 ev->id = v4l2_ctrl.id;
1474 ev->u.ctrl.value = value;
1475 ev->u.ctrl.changes = changes;
1476 ev->u.ctrl.type = v4l2_ctrl.type;
1477 ev->u.ctrl.flags = v4l2_ctrl.flags;
1478 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1479 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1480 ev->u.ctrl.step = v4l2_ctrl.step;
1481 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1482 }
1483
1484 /*
1485 * Send control change events to all subscribers for the @ctrl control. By
1486 * default the subscriber that generated the event, as identified by @handle,
1487 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1488 * @handle can be NULL for asynchronous events related to auto-update controls,
1489 * in which case all subscribers are notified.
1490 */
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)1491 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1492 struct uvc_fh *handle, struct uvc_control *ctrl,
1493 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1494 {
1495 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1496 struct v4l2_subscribed_event *sev;
1497 struct v4l2_event ev;
1498
1499 if (list_empty(&mapping->ev_subs))
1500 return;
1501
1502 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1503
1504 list_for_each_entry(sev, &mapping->ev_subs, node) {
1505 if (sev->fh != originator ||
1506 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1507 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1508 v4l2_event_queue_fh(sev->fh, &ev);
1509 }
1510 }
1511
1512 /*
1513 * Send control change events for the slave of the @master control identified
1514 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1515 * generated the event and may be NULL for auto-update events.
1516 */
uvc_ctrl_send_slave_event(struct uvc_video_chain * chain,struct uvc_fh * handle,struct uvc_control * master,u32 slave_id)1517 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1518 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1519 {
1520 struct uvc_control_mapping *mapping = NULL;
1521 struct uvc_control *ctrl = NULL;
1522 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1523 s32 val = 0;
1524
1525 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1526 if (ctrl == NULL)
1527 return;
1528
1529 if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1530 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1531
1532 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1533 }
1534
uvc_ctrl_set_handle(struct uvc_fh * handle,struct uvc_control * ctrl,struct uvc_fh * new_handle)1535 static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
1536 struct uvc_fh *new_handle)
1537 {
1538 lockdep_assert_held(&handle->chain->ctrl_mutex);
1539
1540 if (new_handle) {
1541 if (ctrl->handle)
1542 dev_warn_ratelimited(&handle->stream->dev->udev->dev,
1543 "UVC non compliance: Setting an async control with a pending operation.");
1544
1545 if (new_handle == ctrl->handle)
1546 return;
1547
1548 if (ctrl->handle) {
1549 WARN_ON(!ctrl->handle->pending_async_ctrls);
1550 if (ctrl->handle->pending_async_ctrls)
1551 ctrl->handle->pending_async_ctrls--;
1552 }
1553
1554 ctrl->handle = new_handle;
1555 handle->pending_async_ctrls++;
1556 return;
1557 }
1558
1559 /* Cannot clear the handle for a control not owned by us.*/
1560 if (WARN_ON(ctrl->handle != handle))
1561 return;
1562
1563 ctrl->handle = NULL;
1564 if (WARN_ON(!handle->pending_async_ctrls))
1565 return;
1566 handle->pending_async_ctrls--;
1567 }
1568
uvc_ctrl_status_event(struct uvc_video_chain * chain,struct uvc_control * ctrl,const u8 * data)1569 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1570 struct uvc_control *ctrl, const u8 *data)
1571 {
1572 struct uvc_control_mapping *mapping;
1573 struct uvc_fh *handle;
1574 unsigned int i;
1575
1576 mutex_lock(&chain->ctrl_mutex);
1577
1578 handle = ctrl->handle;
1579 if (handle)
1580 uvc_ctrl_set_handle(handle, ctrl, NULL);
1581
1582 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1583 s32 value = __uvc_ctrl_get_value(mapping, data);
1584
1585 /*
1586 * handle may be NULL here if the device sends auto-update
1587 * events without a prior related control set from userspace.
1588 */
1589 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1590 if (!mapping->slave_ids[i])
1591 break;
1592
1593 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1594 mapping->slave_ids[i]);
1595 }
1596
1597 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1598 V4L2_EVENT_CTRL_CH_VALUE);
1599 }
1600
1601 mutex_unlock(&chain->ctrl_mutex);
1602 }
1603
uvc_ctrl_status_event_work(struct work_struct * work)1604 static void uvc_ctrl_status_event_work(struct work_struct *work)
1605 {
1606 struct uvc_device *dev = container_of(work, struct uvc_device,
1607 async_ctrl.work);
1608 struct uvc_ctrl_work *w = &dev->async_ctrl;
1609 int ret;
1610
1611 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1612
1613 /* The barrier is needed to synchronize with uvc_status_stop(). */
1614 if (smp_load_acquire(&dev->flush_status))
1615 return;
1616
1617 /* Resubmit the URB. */
1618 w->urb->interval = dev->int_ep->desc.bInterval;
1619 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1620 if (ret < 0)
1621 dev_err(&dev->udev->dev,
1622 "Failed to resubmit status URB (%d).\n", ret);
1623 }
1624
uvc_ctrl_status_event_async(struct urb * urb,struct uvc_video_chain * chain,struct uvc_control * ctrl,const u8 * data)1625 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1626 struct uvc_control *ctrl, const u8 *data)
1627 {
1628 struct uvc_device *dev = chain->dev;
1629 struct uvc_ctrl_work *w = &dev->async_ctrl;
1630
1631 if (list_empty(&ctrl->info.mappings))
1632 return false;
1633
1634 w->data = data;
1635 w->urb = urb;
1636 w->chain = chain;
1637 w->ctrl = ctrl;
1638
1639 schedule_work(&w->work);
1640
1641 return true;
1642 }
1643
uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control * xctrls,unsigned int xctrls_count,u32 id)1644 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1645 unsigned int xctrls_count, u32 id)
1646 {
1647 unsigned int i;
1648
1649 for (i = 0; i < xctrls_count; ++i) {
1650 if (xctrls[i].id == id)
1651 return true;
1652 }
1653
1654 return false;
1655 }
1656
uvc_ctrl_send_events(struct uvc_fh * handle,const struct v4l2_ext_control * xctrls,unsigned int xctrls_count)1657 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1658 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1659 {
1660 struct uvc_control_mapping *mapping;
1661 struct uvc_control *ctrl;
1662 unsigned int i;
1663 unsigned int j;
1664
1665 for (i = 0; i < xctrls_count; ++i) {
1666 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1667
1668 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1669 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1670 /* Notification will be sent from an Interrupt event. */
1671 continue;
1672
1673 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1674 u32 slave_id = mapping->slave_ids[j];
1675
1676 if (!slave_id)
1677 break;
1678
1679 /*
1680 * We can skip sending an event for the slave if the
1681 * slave is being modified in the same transaction.
1682 */
1683 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1684 slave_id))
1685 continue;
1686
1687 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1688 slave_id);
1689 }
1690
1691 /*
1692 * If the master is being modified in the same transaction
1693 * flags may change too.
1694 */
1695 if (mapping->master_id &&
1696 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1697 mapping->master_id))
1698 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1699
1700 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1701 xctrls[i].value, changes);
1702 }
1703 }
1704
uvc_ctrl_add_event(struct v4l2_subscribed_event * sev,unsigned elems)1705 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1706 {
1707 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1708 struct uvc_control_mapping *mapping;
1709 struct uvc_control *ctrl;
1710 int ret;
1711
1712 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1713 if (ret < 0)
1714 return -ERESTARTSYS;
1715
1716 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
1717 ret = 0;
1718 goto done;
1719 }
1720
1721 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1722 if (ctrl == NULL) {
1723 ret = -EINVAL;
1724 goto done;
1725 }
1726
1727 list_add_tail(&sev->node, &mapping->ev_subs);
1728 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1729 struct v4l2_event ev;
1730 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1731 s32 val = 0;
1732
1733 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1734 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1735
1736 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1737 changes);
1738 /*
1739 * Mark the queue as active, allowing this initial event to be
1740 * accepted.
1741 */
1742 sev->elems = elems;
1743 v4l2_event_queue_fh(sev->fh, &ev);
1744 }
1745
1746 done:
1747 mutex_unlock(&handle->chain->ctrl_mutex);
1748 return ret;
1749 }
1750
uvc_ctrl_del_event(struct v4l2_subscribed_event * sev)1751 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1752 {
1753 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1754
1755 mutex_lock(&handle->chain->ctrl_mutex);
1756 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
1757 goto done;
1758 list_del(&sev->node);
1759 done:
1760 mutex_unlock(&handle->chain->ctrl_mutex);
1761 }
1762
1763 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1764 .add = uvc_ctrl_add_event,
1765 .del = uvc_ctrl_del_event,
1766 .replace = v4l2_ctrl_replace,
1767 .merge = v4l2_ctrl_merge,
1768 };
1769
1770 /* --------------------------------------------------------------------------
1771 * Control transactions
1772 *
1773 * To make extended set operations as atomic as the hardware allows, controls
1774 * are handled using begin/commit/rollback operations.
1775 *
1776 * At the beginning of a set request, uvc_ctrl_begin should be called to
1777 * initialize the request. This function acquires the control lock.
1778 *
1779 * When setting a control, the new value is stored in the control data field
1780 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1781 * later processing. If the UVC and V4L2 control sizes differ, the current
1782 * value is loaded from the hardware before storing the new value in the data
1783 * field.
1784 *
1785 * After processing all controls in the transaction, uvc_ctrl_commit or
1786 * uvc_ctrl_rollback must be called to apply the pending changes to the
1787 * hardware or revert them. When applying changes, all controls marked as
1788 * dirty will be modified in the UVC device, and the dirty flag will be
1789 * cleared. When reverting controls, the control data field
1790 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1791 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1792 * control lock.
1793 */
uvc_ctrl_begin(struct uvc_video_chain * chain)1794 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1795 {
1796 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1797 }
1798
uvc_ctrl_commit_entity(struct uvc_device * dev,struct uvc_fh * handle,struct uvc_entity * entity,int rollback,struct uvc_control ** err_ctrl)1799 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1800 struct uvc_fh *handle,
1801 struct uvc_entity *entity,
1802 int rollback,
1803 struct uvc_control **err_ctrl)
1804 {
1805 struct uvc_control *ctrl;
1806 unsigned int i;
1807 int ret;
1808
1809 if (entity == NULL)
1810 return 0;
1811
1812 for (i = 0; i < entity->ncontrols; ++i) {
1813 ctrl = &entity->controls[i];
1814 if (!ctrl->initialized)
1815 continue;
1816
1817 /*
1818 * Reset the loaded flag for auto-update controls that were
1819 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1820 * uvc_ctrl_get from using the cached value, and for write-only
1821 * controls to prevent uvc_ctrl_set from setting bits not
1822 * explicitly set by the user.
1823 */
1824 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1825 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1826 ctrl->loaded = 0;
1827
1828 if (!ctrl->dirty)
1829 continue;
1830
1831 if (!rollback)
1832 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1833 dev->intfnum, ctrl->info.selector,
1834 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1835 ctrl->info.size);
1836 else
1837 ret = 0;
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 0;
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 }
1900 }
1901
1902 if (!rollback)
1903 uvc_ctrl_send_events(handle, ctrls->controls, ctrls->count);
1904 done:
1905 mutex_unlock(&chain->ctrl_mutex);
1906 return ret;
1907 }
1908
uvc_ctrl_get(struct uvc_video_chain * chain,struct v4l2_ext_control * xctrl)1909 int uvc_ctrl_get(struct uvc_video_chain *chain,
1910 struct v4l2_ext_control *xctrl)
1911 {
1912 struct uvc_control *ctrl;
1913 struct uvc_control_mapping *mapping;
1914
1915 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1916 return -EACCES;
1917
1918 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1919 if (ctrl == NULL)
1920 return -EINVAL;
1921
1922 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1923 }
1924
uvc_ctrl_set(struct uvc_fh * handle,struct v4l2_ext_control * xctrl)1925 int uvc_ctrl_set(struct uvc_fh *handle,
1926 struct v4l2_ext_control *xctrl)
1927 {
1928 struct uvc_video_chain *chain = handle->chain;
1929 struct uvc_control *ctrl;
1930 struct uvc_control_mapping *mapping;
1931 s32 value;
1932 u32 step;
1933 s32 min;
1934 s32 max;
1935 int ret;
1936
1937 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1938 return -EACCES;
1939
1940 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1941 if (ctrl == NULL)
1942 return -EINVAL;
1943 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1944 return -EACCES;
1945
1946 /* Clamp out of range values. */
1947 switch (mapping->v4l2_type) {
1948 case V4L2_CTRL_TYPE_INTEGER:
1949 if (!ctrl->cached) {
1950 ret = uvc_ctrl_populate_cache(chain, ctrl);
1951 if (ret < 0)
1952 return ret;
1953 }
1954
1955 min = mapping->get(mapping, UVC_GET_MIN,
1956 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1957 max = mapping->get(mapping, UVC_GET_MAX,
1958 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1959 step = mapping->get(mapping, UVC_GET_RES,
1960 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1961 if (step == 0)
1962 step = 1;
1963
1964 xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
1965 step) * step;
1966 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1967 xctrl->value = clamp(xctrl->value, min, max);
1968 else
1969 xctrl->value = clamp_t(u32, xctrl->value, min, max);
1970 value = xctrl->value;
1971 break;
1972
1973 case V4L2_CTRL_TYPE_BITMASK:
1974 if (!ctrl->cached) {
1975 ret = uvc_ctrl_populate_cache(chain, ctrl);
1976 if (ret < 0)
1977 return ret;
1978 }
1979
1980 xctrl->value &= uvc_get_ctrl_bitmap(ctrl, mapping);
1981 value = xctrl->value;
1982 break;
1983
1984 case V4L2_CTRL_TYPE_BOOLEAN:
1985 xctrl->value = clamp(xctrl->value, 0, 1);
1986 value = xctrl->value;
1987 break;
1988
1989 case V4L2_CTRL_TYPE_MENU:
1990 if (xctrl->value < (ffs(mapping->menu_mask) - 1) ||
1991 xctrl->value > (fls(mapping->menu_mask) - 1))
1992 return -ERANGE;
1993
1994 if (!test_bit(xctrl->value, &mapping->menu_mask))
1995 return -EINVAL;
1996
1997 value = uvc_mapping_get_menu_value(mapping, xctrl->value);
1998
1999 /*
2000 * Valid menu indices are reported by the GET_RES request for
2001 * UVC controls that support it.
2002 */
2003 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
2004 if (!ctrl->cached) {
2005 ret = uvc_ctrl_populate_cache(chain, ctrl);
2006 if (ret < 0)
2007 return ret;
2008 }
2009
2010 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & value))
2011 return -EINVAL;
2012 }
2013
2014 break;
2015
2016 default:
2017 value = xctrl->value;
2018 break;
2019 }
2020
2021 /*
2022 * If the mapping doesn't span the whole UVC control, the current value
2023 * needs to be loaded from the device to perform the read-modify-write
2024 * operation.
2025 */
2026 if ((ctrl->info.size * 8) != mapping->size) {
2027 ret = __uvc_ctrl_load_cur(chain, ctrl);
2028 if (ret < 0)
2029 return ret;
2030 }
2031
2032 /* Backup the current value in case we need to rollback later. */
2033 if (!ctrl->dirty) {
2034 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2035 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2036 ctrl->info.size);
2037 }
2038
2039 mapping->set(mapping, value,
2040 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2041
2042 ctrl->dirty = 1;
2043 ctrl->modified = 1;
2044 return 0;
2045 }
2046
2047 /* --------------------------------------------------------------------------
2048 * Dynamic controls
2049 */
2050
2051 /*
2052 * Retrieve flags for a given control
2053 */
uvc_ctrl_get_flags(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2054 static int uvc_ctrl_get_flags(struct uvc_device *dev,
2055 const struct uvc_control *ctrl,
2056 struct uvc_control_info *info)
2057 {
2058 u8 *data;
2059 int ret;
2060
2061 data = kmalloc(1, GFP_KERNEL);
2062 if (data == NULL)
2063 return -ENOMEM;
2064
2065 if (ctrl->entity->get_info)
2066 ret = ctrl->entity->get_info(dev, ctrl->entity,
2067 ctrl->info.selector, data);
2068 else
2069 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
2070 dev->intfnum, info->selector, data, 1);
2071
2072 if (!ret) {
2073 info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
2074 UVC_CTRL_FLAG_SET_CUR |
2075 UVC_CTRL_FLAG_AUTO_UPDATE |
2076 UVC_CTRL_FLAG_ASYNCHRONOUS);
2077
2078 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
2079 UVC_CTRL_FLAG_GET_CUR : 0)
2080 | (data[0] & UVC_CONTROL_CAP_SET ?
2081 UVC_CTRL_FLAG_SET_CUR : 0)
2082 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
2083 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
2084 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
2085 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
2086 }
2087
2088 kfree(data);
2089 return ret;
2090 }
2091
uvc_ctrl_fixup_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2092 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
2093 const struct uvc_control *ctrl, struct uvc_control_info *info)
2094 {
2095 struct uvc_ctrl_fixup {
2096 struct usb_device_id id;
2097 u8 entity;
2098 u8 selector;
2099 u8 flags;
2100 };
2101
2102 static const struct uvc_ctrl_fixup fixups[] = {
2103 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2104 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2105 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2106 UVC_CTRL_FLAG_AUTO_UPDATE },
2107 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2108 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2109 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2110 UVC_CTRL_FLAG_AUTO_UPDATE },
2111 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2112 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2113 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2114 UVC_CTRL_FLAG_AUTO_UPDATE },
2115 };
2116
2117 unsigned int i;
2118
2119 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
2120 if (!usb_match_one_id(dev->intf, &fixups[i].id))
2121 continue;
2122
2123 if (fixups[i].entity == ctrl->entity->id &&
2124 fixups[i].selector == info->selector) {
2125 info->flags = fixups[i].flags;
2126 return;
2127 }
2128 }
2129 }
2130
2131 /*
2132 * Query control information (size and flags) for XU controls.
2133 */
uvc_ctrl_fill_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2134 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
2135 const struct uvc_control *ctrl, struct uvc_control_info *info)
2136 {
2137 u8 *data;
2138 int ret;
2139
2140 data = kmalloc(2, GFP_KERNEL);
2141 if (data == NULL)
2142 return -ENOMEM;
2143
2144 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
2145 info->index = ctrl->index;
2146 info->selector = ctrl->index + 1;
2147
2148 /* Query and verify the control length (GET_LEN) */
2149 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
2150 info->selector, data, 2);
2151 if (ret < 0) {
2152 uvc_dbg(dev, CONTROL,
2153 "GET_LEN failed on control %pUl/%u (%d)\n",
2154 info->entity, info->selector, ret);
2155 goto done;
2156 }
2157
2158 info->size = le16_to_cpup((__le16 *)data);
2159
2160 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
2161 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
2162
2163 ret = uvc_ctrl_get_flags(dev, ctrl, info);
2164 if (ret < 0) {
2165 uvc_dbg(dev, CONTROL,
2166 "Failed to get flags for control %pUl/%u (%d)\n",
2167 info->entity, info->selector, ret);
2168 goto done;
2169 }
2170
2171 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
2172
2173 uvc_dbg(dev, CONTROL,
2174 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2175 info->entity, info->selector, info->size,
2176 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
2177 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
2178 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
2179
2180 done:
2181 kfree(data);
2182 return ret;
2183 }
2184
2185 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2186 const struct uvc_control_info *info);
2187
uvc_ctrl_init_xu_ctrl(struct uvc_device * dev,struct uvc_control * ctrl)2188 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
2189 struct uvc_control *ctrl)
2190 {
2191 struct uvc_control_info info;
2192 int ret;
2193
2194 if (ctrl->initialized)
2195 return 0;
2196
2197 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2198 if (ret < 0)
2199 return ret;
2200
2201 ret = uvc_ctrl_add_info(dev, ctrl, &info);
2202 if (ret < 0)
2203 uvc_dbg(dev, CONTROL,
2204 "Failed to initialize control %pUl/%u on device %s entity %u\n",
2205 info.entity, info.selector, dev->udev->devpath,
2206 ctrl->entity->id);
2207
2208 return ret;
2209 }
2210
uvc_xu_ctrl_query(struct uvc_video_chain * chain,struct uvc_xu_control_query * xqry)2211 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2212 struct uvc_xu_control_query *xqry)
2213 {
2214 struct uvc_entity *entity, *iter;
2215 struct uvc_control *ctrl;
2216 unsigned int i;
2217 bool found;
2218 u32 reqflags;
2219 u16 size;
2220 u8 *data = NULL;
2221 int ret;
2222
2223 /* Find the extension unit. */
2224 entity = NULL;
2225 list_for_each_entry(iter, &chain->entities, chain) {
2226 if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
2227 iter->id == xqry->unit) {
2228 entity = iter;
2229 break;
2230 }
2231 }
2232
2233 if (!entity) {
2234 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2235 xqry->unit);
2236 return -ENOENT;
2237 }
2238
2239 /* Find the control and perform delayed initialization if needed. */
2240 found = false;
2241 for (i = 0; i < entity->ncontrols; ++i) {
2242 ctrl = &entity->controls[i];
2243 if (ctrl->index == xqry->selector - 1) {
2244 found = true;
2245 break;
2246 }
2247 }
2248
2249 if (!found) {
2250 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2251 entity->guid, xqry->selector);
2252 return -ENOENT;
2253 }
2254
2255 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2256 return -ERESTARTSYS;
2257
2258 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2259 if (ret < 0) {
2260 ret = -ENOENT;
2261 goto done;
2262 }
2263
2264 /* Validate the required buffer size and flags for the request */
2265 reqflags = 0;
2266 size = ctrl->info.size;
2267
2268 switch (xqry->query) {
2269 case UVC_GET_CUR:
2270 reqflags = UVC_CTRL_FLAG_GET_CUR;
2271 break;
2272 case UVC_GET_MIN:
2273 reqflags = UVC_CTRL_FLAG_GET_MIN;
2274 break;
2275 case UVC_GET_MAX:
2276 reqflags = UVC_CTRL_FLAG_GET_MAX;
2277 break;
2278 case UVC_GET_DEF:
2279 reqflags = UVC_CTRL_FLAG_GET_DEF;
2280 break;
2281 case UVC_GET_RES:
2282 reqflags = UVC_CTRL_FLAG_GET_RES;
2283 break;
2284 case UVC_SET_CUR:
2285 reqflags = UVC_CTRL_FLAG_SET_CUR;
2286 break;
2287 case UVC_GET_LEN:
2288 size = 2;
2289 break;
2290 case UVC_GET_INFO:
2291 size = 1;
2292 break;
2293 default:
2294 ret = -EINVAL;
2295 goto done;
2296 }
2297
2298 if (size != xqry->size) {
2299 ret = -ENOBUFS;
2300 goto done;
2301 }
2302
2303 if (reqflags && !(ctrl->info.flags & reqflags)) {
2304 ret = -EBADRQC;
2305 goto done;
2306 }
2307
2308 data = kmalloc(size, GFP_KERNEL);
2309 if (data == NULL) {
2310 ret = -ENOMEM;
2311 goto done;
2312 }
2313
2314 if (xqry->query == UVC_SET_CUR &&
2315 copy_from_user(data, xqry->data, size)) {
2316 ret = -EFAULT;
2317 goto done;
2318 }
2319
2320 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2321 chain->dev->intfnum, xqry->selector, data, size);
2322 if (ret < 0)
2323 goto done;
2324
2325 if (xqry->query != UVC_SET_CUR &&
2326 copy_to_user(xqry->data, data, size))
2327 ret = -EFAULT;
2328 done:
2329 kfree(data);
2330 mutex_unlock(&chain->ctrl_mutex);
2331 return ret;
2332 }
2333
2334 /* --------------------------------------------------------------------------
2335 * Suspend/resume
2336 */
2337
2338 /*
2339 * Restore control values after resume, skipping controls that haven't been
2340 * changed.
2341 *
2342 * TODO
2343 * - Don't restore modified controls that are back to their default value.
2344 * - Handle restore order (Auto-Exposure Mode should be restored before
2345 * Exposure Time).
2346 */
uvc_ctrl_restore_values(struct uvc_device * dev)2347 int uvc_ctrl_restore_values(struct uvc_device *dev)
2348 {
2349 struct uvc_control *ctrl;
2350 struct uvc_entity *entity;
2351 unsigned int i;
2352 int ret;
2353
2354 /* Walk the entities list and restore controls when possible. */
2355 list_for_each_entry(entity, &dev->entities, list) {
2356
2357 for (i = 0; i < entity->ncontrols; ++i) {
2358 ctrl = &entity->controls[i];
2359
2360 if (!ctrl->initialized || !ctrl->modified ||
2361 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2362 continue;
2363 dev_dbg(&dev->udev->dev,
2364 "restoring control %pUl/%u/%u\n",
2365 ctrl->info.entity, ctrl->info.index,
2366 ctrl->info.selector);
2367 ctrl->dirty = 1;
2368 }
2369
2370 ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
2371 if (ret < 0)
2372 return ret;
2373 }
2374
2375 return 0;
2376 }
2377
2378 /* --------------------------------------------------------------------------
2379 * Control and mapping handling
2380 */
2381
2382 /*
2383 * Add control information to a given control.
2384 */
uvc_ctrl_add_info(struct uvc_device * dev,struct uvc_control * ctrl,const struct uvc_control_info * info)2385 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2386 const struct uvc_control_info *info)
2387 {
2388 ctrl->info = *info;
2389 INIT_LIST_HEAD(&ctrl->info.mappings);
2390
2391 /* Allocate an array to save control values (cur, def, max, etc.) */
2392 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2393 GFP_KERNEL);
2394 if (!ctrl->uvc_data)
2395 return -ENOMEM;
2396
2397 ctrl->initialized = 1;
2398
2399 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2400 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2401 ctrl->entity->id);
2402
2403 return 0;
2404 }
2405
2406 /*
2407 * Add a control mapping to a given control.
2408 */
__uvc_ctrl_add_mapping(struct uvc_video_chain * chain,struct uvc_control * ctrl,const struct uvc_control_mapping * mapping)2409 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2410 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2411 {
2412 struct uvc_control_mapping *map;
2413 unsigned int size;
2414 unsigned int i;
2415
2416 /*
2417 * Most mappings come from static kernel data, and need to be duplicated.
2418 * Mappings that come from userspace will be unnecessarily duplicated,
2419 * this could be optimized.
2420 */
2421 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2422 if (!map)
2423 return -ENOMEM;
2424
2425 map->name = NULL;
2426 map->menu_names = NULL;
2427 map->menu_mapping = NULL;
2428
2429 /* For UVCIOC_CTRL_MAP custom control */
2430 if (mapping->name) {
2431 map->name = kstrdup(mapping->name, GFP_KERNEL);
2432 if (!map->name)
2433 goto err_nomem;
2434 }
2435
2436 INIT_LIST_HEAD(&map->ev_subs);
2437
2438 if (mapping->menu_mapping && mapping->menu_mask) {
2439 size = sizeof(mapping->menu_mapping[0])
2440 * fls(mapping->menu_mask);
2441 map->menu_mapping = kmemdup(mapping->menu_mapping, size,
2442 GFP_KERNEL);
2443 if (!map->menu_mapping)
2444 goto err_nomem;
2445 }
2446 if (mapping->menu_names && mapping->menu_mask) {
2447 size = sizeof(mapping->menu_names[0])
2448 * fls(mapping->menu_mask);
2449 map->menu_names = kmemdup(mapping->menu_names, size,
2450 GFP_KERNEL);
2451 if (!map->menu_names)
2452 goto err_nomem;
2453 }
2454
2455 if (map->get == NULL)
2456 map->get = uvc_get_le_value;
2457 if (map->set == NULL)
2458 map->set = uvc_set_le_value;
2459
2460 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
2461 if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
2462 V4L2_CTRL_ID2WHICH(map->id)) {
2463 chain->ctrl_class_bitmap |= BIT(i);
2464 break;
2465 }
2466 }
2467
2468 list_add_tail(&map->list, &ctrl->info.mappings);
2469 uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2470 uvc_map_get_name(map), ctrl->info.entity,
2471 ctrl->info.selector);
2472
2473 return 0;
2474
2475 err_nomem:
2476 kfree(map->menu_names);
2477 kfree(map->menu_mapping);
2478 kfree(map->name);
2479 kfree(map);
2480 return -ENOMEM;
2481 }
2482
uvc_ctrl_add_mapping(struct uvc_video_chain * chain,const struct uvc_control_mapping * mapping)2483 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2484 const struct uvc_control_mapping *mapping)
2485 {
2486 struct uvc_device *dev = chain->dev;
2487 struct uvc_control_mapping *map;
2488 struct uvc_entity *entity;
2489 struct uvc_control *ctrl;
2490 int found = 0;
2491 int ret;
2492
2493 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2494 uvc_dbg(dev, CONTROL,
2495 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2496 uvc_map_get_name(mapping), mapping->id);
2497 return -EINVAL;
2498 }
2499
2500 /* Search for the matching (GUID/CS) control on the current chain */
2501 list_for_each_entry(entity, &chain->entities, chain) {
2502 unsigned int i;
2503
2504 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2505 !uvc_entity_match_guid(entity, mapping->entity))
2506 continue;
2507
2508 for (i = 0; i < entity->ncontrols; ++i) {
2509 ctrl = &entity->controls[i];
2510 if (ctrl->index == mapping->selector - 1) {
2511 found = 1;
2512 break;
2513 }
2514 }
2515
2516 if (found)
2517 break;
2518 }
2519 if (!found)
2520 return -ENOENT;
2521
2522 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2523 return -ERESTARTSYS;
2524
2525 /* Perform delayed initialization of XU controls */
2526 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2527 if (ret < 0) {
2528 ret = -ENOENT;
2529 goto done;
2530 }
2531
2532 /* Validate the user-provided bit-size and offset */
2533 if (mapping->size > 32 ||
2534 mapping->offset + mapping->size > ctrl->info.size * 8) {
2535 ret = -EINVAL;
2536 goto done;
2537 }
2538
2539 list_for_each_entry(map, &ctrl->info.mappings, list) {
2540 if (mapping->id == map->id) {
2541 uvc_dbg(dev, CONTROL,
2542 "Can't add mapping '%s', control id 0x%08x already exists\n",
2543 uvc_map_get_name(mapping), mapping->id);
2544 ret = -EEXIST;
2545 goto done;
2546 }
2547 }
2548
2549 /* Prevent excess memory consumption */
2550 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2551 atomic_dec(&dev->nmappings);
2552 uvc_dbg(dev, CONTROL,
2553 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
2554 uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
2555 ret = -ENOMEM;
2556 goto done;
2557 }
2558
2559 ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2560 if (ret < 0)
2561 atomic_dec(&dev->nmappings);
2562
2563 done:
2564 mutex_unlock(&chain->ctrl_mutex);
2565 return ret;
2566 }
2567
2568 /*
2569 * Prune an entity of its bogus controls using a blacklist. Bogus controls
2570 * are currently the ones that crash the camera or unconditionally return an
2571 * error when queried.
2572 */
uvc_ctrl_prune_entity(struct uvc_device * dev,struct uvc_entity * entity)2573 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2574 struct uvc_entity *entity)
2575 {
2576 struct uvc_ctrl_blacklist {
2577 struct usb_device_id id;
2578 u8 index;
2579 };
2580
2581 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2582 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2583 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2584 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2585 };
2586 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2587 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2588 };
2589
2590 const struct uvc_ctrl_blacklist *blacklist;
2591 unsigned int size;
2592 unsigned int count;
2593 unsigned int i;
2594 u8 *controls;
2595
2596 switch (UVC_ENTITY_TYPE(entity)) {
2597 case UVC_VC_PROCESSING_UNIT:
2598 blacklist = processing_blacklist;
2599 count = ARRAY_SIZE(processing_blacklist);
2600 controls = entity->processing.bmControls;
2601 size = entity->processing.bControlSize;
2602 break;
2603
2604 case UVC_ITT_CAMERA:
2605 blacklist = camera_blacklist;
2606 count = ARRAY_SIZE(camera_blacklist);
2607 controls = entity->camera.bmControls;
2608 size = entity->camera.bControlSize;
2609 break;
2610
2611 default:
2612 return;
2613 }
2614
2615 for (i = 0; i < count; ++i) {
2616 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2617 continue;
2618
2619 if (blacklist[i].index >= 8 * size ||
2620 !uvc_test_bit(controls, blacklist[i].index))
2621 continue;
2622
2623 uvc_dbg(dev, CONTROL,
2624 "%u/%u control is black listed, removing it\n",
2625 entity->id, blacklist[i].index);
2626
2627 uvc_clear_bit(controls, blacklist[i].index);
2628 }
2629 }
2630
2631 /*
2632 * Add control information and hardcoded stock control mappings to the given
2633 * device.
2634 */
uvc_ctrl_init_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl)2635 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
2636 struct uvc_control *ctrl)
2637 {
2638 const struct uvc_control_mapping **mappings;
2639 unsigned int i;
2640
2641 /*
2642 * XU controls initialization requires querying the device for control
2643 * information. As some buggy UVC devices will crash when queried
2644 * repeatedly in a tight loop, delay XU controls initialization until
2645 * first use.
2646 */
2647 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2648 return;
2649
2650 for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
2651 const struct uvc_control_info *info = &uvc_ctrls[i];
2652
2653 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2654 ctrl->index == info->index) {
2655 uvc_ctrl_add_info(chain->dev, ctrl, info);
2656 /*
2657 * Retrieve control flags from the device. Ignore errors
2658 * and work with default flag values from the uvc_ctrl
2659 * array when the device doesn't properly implement
2660 * GET_INFO on standard controls.
2661 */
2662 uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
2663 break;
2664 }
2665 }
2666
2667 if (!ctrl->initialized)
2668 return;
2669
2670 /*
2671 * First check if the device provides a custom mapping for this control,
2672 * used to override standard mappings for non-conformant devices. Don't
2673 * process standard mappings if a custom mapping is found. This
2674 * mechanism doesn't support combining standard and custom mappings for
2675 * a single control.
2676 */
2677 if (chain->dev->info->mappings) {
2678 bool custom = false;
2679
2680 for (i = 0; chain->dev->info->mappings[i]; ++i) {
2681 const struct uvc_control_mapping *mapping =
2682 chain->dev->info->mappings[i];
2683
2684 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2685 ctrl->info.selector == mapping->selector) {
2686 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2687 custom = true;
2688 }
2689 }
2690
2691 if (custom)
2692 return;
2693 }
2694
2695 /* Process common mappings next. */
2696 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
2697 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
2698
2699 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2700 ctrl->info.selector == mapping->selector)
2701 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2702 }
2703
2704 /* Finally process version-specific mappings. */
2705 mappings = chain->dev->uvc_version < 0x0150
2706 ? uvc_ctrl_mappings_uvc11 : uvc_ctrl_mappings_uvc15;
2707
2708 for (i = 0; mappings[i]; ++i) {
2709 const struct uvc_control_mapping *mapping = mappings[i];
2710
2711 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2712 ctrl->info.selector == mapping->selector)
2713 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2714 }
2715 }
2716
2717 /*
2718 * Initialize device controls.
2719 */
uvc_ctrl_init_chain(struct uvc_video_chain * chain)2720 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
2721 {
2722 struct uvc_entity *entity;
2723 unsigned int i;
2724
2725 /* Walk the entities list and instantiate controls */
2726 list_for_each_entry(entity, &chain->entities, chain) {
2727 struct uvc_control *ctrl;
2728 unsigned int bControlSize = 0, ncontrols;
2729 u8 *bmControls = NULL;
2730
2731 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2732 bmControls = entity->extension.bmControls;
2733 bControlSize = entity->extension.bControlSize;
2734 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2735 bmControls = entity->processing.bmControls;
2736 bControlSize = entity->processing.bControlSize;
2737 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2738 bmControls = entity->camera.bmControls;
2739 bControlSize = entity->camera.bControlSize;
2740 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
2741 bmControls = entity->gpio.bmControls;
2742 bControlSize = entity->gpio.bControlSize;
2743 }
2744
2745 /* Remove bogus/blacklisted controls */
2746 uvc_ctrl_prune_entity(chain->dev, entity);
2747
2748 /* Count supported controls and allocate the controls array */
2749 ncontrols = memweight(bmControls, bControlSize);
2750 if (ncontrols == 0)
2751 continue;
2752
2753 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2754 GFP_KERNEL);
2755 if (entity->controls == NULL)
2756 return -ENOMEM;
2757 entity->ncontrols = ncontrols;
2758
2759 /* Initialize all supported controls */
2760 ctrl = entity->controls;
2761 for (i = 0; i < bControlSize * 8; ++i) {
2762 if (uvc_test_bit(bmControls, i) == 0)
2763 continue;
2764
2765 ctrl->entity = entity;
2766 ctrl->index = i;
2767
2768 uvc_ctrl_init_ctrl(chain, ctrl);
2769 ctrl++;
2770 }
2771 }
2772
2773 return 0;
2774 }
2775
uvc_ctrl_init_device(struct uvc_device * dev)2776 int uvc_ctrl_init_device(struct uvc_device *dev)
2777 {
2778 struct uvc_video_chain *chain;
2779 int ret;
2780
2781 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2782
2783 list_for_each_entry(chain, &dev->chains, list) {
2784 ret = uvc_ctrl_init_chain(chain);
2785 if (ret)
2786 return ret;
2787 }
2788
2789 return 0;
2790 }
2791
uvc_ctrl_cleanup_fh(struct uvc_fh * handle)2792 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
2793 {
2794 struct uvc_entity *entity;
2795
2796 guard(mutex)(&handle->chain->ctrl_mutex);
2797
2798 if (!handle->pending_async_ctrls)
2799 return;
2800
2801 list_for_each_entry(entity, &handle->chain->dev->entities, list) {
2802 for (unsigned int i = 0; i < entity->ncontrols; ++i) {
2803 if (entity->controls[i].handle != handle)
2804 continue;
2805 uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
2806 }
2807 }
2808
2809 WARN_ON(handle->pending_async_ctrls);
2810 }
2811
2812 /*
2813 * Cleanup device controls.
2814 */
uvc_ctrl_cleanup_mappings(struct uvc_device * dev,struct uvc_control * ctrl)2815 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2816 struct uvc_control *ctrl)
2817 {
2818 struct uvc_control_mapping *mapping, *nm;
2819
2820 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2821 list_del(&mapping->list);
2822 kfree(mapping->menu_names);
2823 kfree(mapping->menu_mapping);
2824 kfree(mapping->name);
2825 kfree(mapping);
2826 }
2827 }
2828
uvc_ctrl_cleanup_device(struct uvc_device * dev)2829 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2830 {
2831 struct uvc_entity *entity;
2832 unsigned int i;
2833
2834 /* Can be uninitialized if we are aborting on probe error. */
2835 if (dev->async_ctrl.work.func)
2836 cancel_work_sync(&dev->async_ctrl.work);
2837
2838 /* Free controls and control mappings for all entities. */
2839 list_for_each_entry(entity, &dev->entities, list) {
2840 for (i = 0; i < entity->ncontrols; ++i) {
2841 struct uvc_control *ctrl = &entity->controls[i];
2842
2843 if (!ctrl->initialized)
2844 continue;
2845
2846 uvc_ctrl_cleanup_mappings(dev, ctrl);
2847 kfree(ctrl->uvc_data);
2848 }
2849
2850 kfree(entity->controls);
2851 }
2852 }
2853