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 * UVC_CTRL_FLAG_AUTO_UPDATE is needed because the RoI may get updated
363 * by sensors.
364 * "This RoI should be the same as specified in most recent SET_CUR
365 * except in the case where the ‘Auto Detect and Track’ and/or
366 * ‘Image Stabilization’ bit have been set."
367 * 4.2.2.1.20 Digital Region of Interest (ROI) Control
368 */
369 {
370 .entity = UVC_GUID_UVC_CAMERA,
371 .selector = UVC_CT_REGION_OF_INTEREST_CONTROL,
372 .index = 21,
373 .size = 10,
374 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
375 | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
376 | UVC_CTRL_FLAG_GET_DEF
377 | UVC_CTRL_FLAG_AUTO_UPDATE,
378 },
379 };
380
381 static const u32 uvc_control_classes[] = {
382 V4L2_CID_CAMERA_CLASS,
383 V4L2_CID_USER_CLASS,
384 };
385
386 static const int exposure_auto_mapping[] = { 2, 1, 4, 8 };
387
uvc_ctrl_mapping_is_compound(struct uvc_control_mapping * mapping)388 static bool uvc_ctrl_mapping_is_compound(struct uvc_control_mapping *mapping)
389 {
390 return mapping->v4l2_type >= V4L2_CTRL_COMPOUND_TYPES;
391 }
392
uvc_mapping_get_s32(struct uvc_control_mapping * mapping,u8 query,const void * data_in)393 static s32 uvc_mapping_get_s32(struct uvc_control_mapping *mapping,
394 u8 query, const void *data_in)
395 {
396 s32 data_out = 0;
397
398 mapping->get(mapping, query, data_in, sizeof(data_out), &data_out);
399
400 return data_out;
401 }
402
uvc_mapping_set_s32(struct uvc_control_mapping * mapping,s32 data_in,void * data_out)403 static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
404 s32 data_in, void *data_out)
405 {
406 mapping->set(mapping, sizeof(data_in), &data_in, data_out);
407 }
408
409 /*
410 * This function translates the V4L2 menu index @idx, as exposed to userspace as
411 * the V4L2 control value, to the corresponding UVC control value used by the
412 * device. The custom menu_mapping in the control @mapping is used when
413 * available, otherwise the function assumes that the V4L2 and UVC values are
414 * identical.
415 *
416 * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is
417 * expressed as a bitmask and is thus guaranteed to have a single bit set.
418 *
419 * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the
420 * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM
421 * or UVC_CTRL_DATA_TYPE_BITMASK.
422 */
uvc_mapping_get_menu_value(const struct uvc_control_mapping * mapping,u32 idx)423 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
424 u32 idx)
425 {
426 if (!test_bit(idx, &mapping->menu_mask))
427 return -EINVAL;
428
429 if (mapping->menu_mapping)
430 return mapping->menu_mapping[idx];
431
432 return idx;
433 }
434
435 static const char *
uvc_mapping_get_menu_name(const struct uvc_control_mapping * mapping,u32 idx)436 uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx)
437 {
438 if (!test_bit(idx, &mapping->menu_mask))
439 return NULL;
440
441 if (mapping->menu_names)
442 return mapping->menu_names[idx];
443
444 return v4l2_ctrl_get_menu(mapping->id)[idx];
445 }
446
uvc_ctrl_get_zoom(struct uvc_control_mapping * mapping,u8 query,const void * uvc_in,size_t v4l2_size,void * v4l2_out)447 static int uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, u8 query,
448 const void *uvc_in, size_t v4l2_size,
449 void *v4l2_out)
450 {
451 u8 value = ((u8 *)uvc_in)[2];
452 s8 sign = ((s8 *)uvc_in)[0];
453 s32 *out = v4l2_out;
454
455 if (WARN_ON(v4l2_size != sizeof(s32)))
456 return -EINVAL;
457
458 switch (query) {
459 case UVC_GET_CUR:
460 *out = (sign == 0) ? 0 : (sign > 0 ? value : -value);
461 return 0;
462
463 case UVC_GET_MIN:
464 case UVC_GET_MAX:
465 case UVC_GET_RES:
466 case UVC_GET_DEF:
467 default:
468 *out = value;
469 return 0;
470 }
471 }
472
uvc_ctrl_set_zoom(struct uvc_control_mapping * mapping,size_t v4l2_size,const void * v4l2_in,void * uvc_out)473 static int uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
474 size_t v4l2_size, const void *v4l2_in,
475 void *uvc_out)
476 {
477 u8 *out = uvc_out;
478 s32 value;
479
480 if (WARN_ON(v4l2_size != sizeof(s32)))
481 return -EINVAL;
482
483 value = *(u32 *)v4l2_in;
484 out[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
485 out[2] = min_t(int, abs(value), 0xff);
486
487 return 0;
488 }
489
uvc_ctrl_get_rel_speed(struct uvc_control_mapping * mapping,u8 query,const void * uvc_in,size_t v4l2_size,void * v4l2_out)490 static int uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
491 u8 query, const void *uvc_in,
492 size_t v4l2_size, void *v4l2_out)
493 {
494 unsigned int first = mapping->offset / 8;
495 u8 value = ((u8 *)uvc_in)[first + 1];
496 s8 sign = ((s8 *)uvc_in)[first];
497 s32 *out = v4l2_out;
498
499 if (WARN_ON(v4l2_size != sizeof(s32)))
500 return -EINVAL;
501
502 switch (query) {
503 case UVC_GET_CUR:
504 *out = (sign == 0) ? 0 : (sign > 0 ? value : -value);
505 return 0;
506 case UVC_GET_MIN:
507 *out = -value;
508 return 0;
509 case UVC_GET_MAX:
510 case UVC_GET_RES:
511 case UVC_GET_DEF:
512 default:
513 *out = value;
514 return 0;
515 }
516 }
517
uvc_ctrl_set_rel_speed(struct uvc_control_mapping * mapping,size_t v4l2_size,const void * v4l2_in,void * uvc_out)518 static int uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
519 size_t v4l2_size, const void *v4l2_in,
520 void *uvc_out)
521 {
522 unsigned int first = mapping->offset / 8;
523 u8 *out = uvc_out;
524 s32 value;
525
526 if (WARN_ON(v4l2_size != sizeof(s32)))
527 return -EINVAL;
528
529 value = *(u32 *)v4l2_in;
530 out[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
531 out[first + 1] = min_t(int, abs(value), 0xff);
532
533 return 0;
534 }
535
536 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = {
537 .id = V4L2_CID_POWER_LINE_FREQUENCY,
538 .entity = UVC_GUID_UVC_PROCESSING,
539 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
540 .size = 2,
541 .offset = 0,
542 .v4l2_type = V4L2_CTRL_TYPE_MENU,
543 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
544 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
545 V4L2_CID_POWER_LINE_FREQUENCY_50HZ),
546 };
547
548 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
549 .id = V4L2_CID_POWER_LINE_FREQUENCY,
550 .entity = UVC_GUID_UVC_PROCESSING,
551 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
552 .size = 2,
553 .offset = 0,
554 .v4l2_type = V4L2_CTRL_TYPE_MENU,
555 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
556 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
557 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
558 };
559
560 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
561 .id = V4L2_CID_POWER_LINE_FREQUENCY,
562 .entity = UVC_GUID_UVC_PROCESSING,
563 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
564 .size = 2,
565 .offset = 0,
566 .v4l2_type = V4L2_CTRL_TYPE_MENU,
567 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
568 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
569 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
570 };
571
uvc_ctrl_filter_plf_mapping(struct uvc_video_chain * chain,struct uvc_control * ctrl)572 static const struct uvc_control_mapping *uvc_ctrl_filter_plf_mapping(
573 struct uvc_video_chain *chain, struct uvc_control *ctrl)
574 {
575 const struct uvc_control_mapping *out_mapping =
576 &uvc_ctrl_power_line_mapping_uvc11;
577 u8 *buf __free(kfree) = NULL;
578 u8 init_val;
579 int ret;
580
581 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
582 if (!buf)
583 return NULL;
584
585 /* Save the current PLF value, so we can restore it. */
586 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
587 chain->dev->intfnum, ctrl->info.selector,
588 buf, sizeof(*buf));
589 /* If we cannot read the control skip it. */
590 if (ret)
591 return NULL;
592 init_val = *buf;
593
594 /* If PLF value cannot be set to off, it is limited. */
595 *buf = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED;
596 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
597 chain->dev->intfnum, ctrl->info.selector,
598 buf, sizeof(*buf));
599 if (ret)
600 return &uvc_ctrl_power_line_mapping_limited;
601
602 /* UVC 1.1 does not define auto, we can exit. */
603 if (chain->dev->uvc_version < 0x150)
604 goto end;
605
606 /* Check if the device supports auto. */
607 *buf = V4L2_CID_POWER_LINE_FREQUENCY_AUTO;
608 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
609 chain->dev->intfnum, ctrl->info.selector,
610 buf, sizeof(*buf));
611 if (!ret)
612 out_mapping = &uvc_ctrl_power_line_mapping_uvc15;
613
614 end:
615 /* Restore initial value and add mapping. */
616 *buf = init_val;
617 uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
618 chain->dev->intfnum, ctrl->info.selector,
619 buf, sizeof(*buf));
620
621 return out_mapping;
622 }
623
uvc_get_rect(struct uvc_control_mapping * mapping,u8 query,const void * uvc_in,size_t v4l2_size,void * v4l2_out)624 static int uvc_get_rect(struct uvc_control_mapping *mapping, u8 query,
625 const void *uvc_in, size_t v4l2_size, void *v4l2_out)
626 {
627 const struct uvc_rect *uvc_rect = uvc_in;
628 struct v4l2_rect *v4l2_rect = v4l2_out;
629
630 if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect)))
631 return -EINVAL;
632
633 if (uvc_rect->left > uvc_rect->right ||
634 uvc_rect->top > uvc_rect->bottom)
635 return -EIO;
636
637 v4l2_rect->top = uvc_rect->top;
638 v4l2_rect->left = uvc_rect->left;
639 v4l2_rect->height = uvc_rect->bottom - uvc_rect->top + 1;
640 v4l2_rect->width = uvc_rect->right - uvc_rect->left + 1;
641
642 return 0;
643 }
644
uvc_set_rect(struct uvc_control_mapping * mapping,size_t v4l2_size,const void * v4l2_in,void * uvc_out)645 static int uvc_set_rect(struct uvc_control_mapping *mapping, size_t v4l2_size,
646 const void *v4l2_in, void *uvc_out)
647 {
648 struct uvc_rect *uvc_rect = uvc_out;
649 const struct v4l2_rect *v4l2_rect = v4l2_in;
650
651 if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect)))
652 return -EINVAL;
653
654 uvc_rect->top = min(0xffff, v4l2_rect->top);
655 uvc_rect->left = min(0xffff, v4l2_rect->left);
656 uvc_rect->bottom = min(0xffff, v4l2_rect->top + v4l2_rect->height - 1);
657 uvc_rect->right = min(0xffff, v4l2_rect->left + v4l2_rect->width - 1);
658
659 return 0;
660 }
661
662 static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
663 {
664 .id = V4L2_CID_BRIGHTNESS,
665 .entity = UVC_GUID_UVC_PROCESSING,
666 .selector = UVC_PU_BRIGHTNESS_CONTROL,
667 .size = 16,
668 .offset = 0,
669 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
670 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
671 },
672 {
673 .id = V4L2_CID_CONTRAST,
674 .entity = UVC_GUID_UVC_PROCESSING,
675 .selector = UVC_PU_CONTRAST_CONTROL,
676 .size = 16,
677 .offset = 0,
678 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
679 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
680 },
681 {
682 .id = V4L2_CID_HUE,
683 .entity = UVC_GUID_UVC_PROCESSING,
684 .selector = UVC_PU_HUE_CONTROL,
685 .size = 16,
686 .offset = 0,
687 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
688 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
689 .master_id = V4L2_CID_HUE_AUTO,
690 .master_manual = 0,
691 },
692 {
693 .id = V4L2_CID_SATURATION,
694 .entity = UVC_GUID_UVC_PROCESSING,
695 .selector = UVC_PU_SATURATION_CONTROL,
696 .size = 16,
697 .offset = 0,
698 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
699 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
700 },
701 {
702 .id = V4L2_CID_SHARPNESS,
703 .entity = UVC_GUID_UVC_PROCESSING,
704 .selector = UVC_PU_SHARPNESS_CONTROL,
705 .size = 16,
706 .offset = 0,
707 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
708 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
709 },
710 {
711 .id = V4L2_CID_GAMMA,
712 .entity = UVC_GUID_UVC_PROCESSING,
713 .selector = UVC_PU_GAMMA_CONTROL,
714 .size = 16,
715 .offset = 0,
716 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
717 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
718 },
719 {
720 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
721 .entity = UVC_GUID_UVC_PROCESSING,
722 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
723 .size = 16,
724 .offset = 0,
725 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
726 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
727 },
728 {
729 .id = V4L2_CID_GAIN,
730 .entity = UVC_GUID_UVC_PROCESSING,
731 .selector = UVC_PU_GAIN_CONTROL,
732 .size = 16,
733 .offset = 0,
734 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
735 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
736 },
737 {
738 .id = V4L2_CID_HUE_AUTO,
739 .entity = UVC_GUID_UVC_PROCESSING,
740 .selector = UVC_PU_HUE_AUTO_CONTROL,
741 .size = 1,
742 .offset = 0,
743 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
744 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
745 .slave_ids = { V4L2_CID_HUE, },
746 },
747 {
748 .id = V4L2_CID_EXPOSURE_AUTO,
749 .entity = UVC_GUID_UVC_CAMERA,
750 .selector = UVC_CT_AE_MODE_CONTROL,
751 .size = 4,
752 .offset = 0,
753 .v4l2_type = V4L2_CTRL_TYPE_MENU,
754 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
755 .menu_mapping = exposure_auto_mapping,
756 .menu_mask = GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY,
757 V4L2_EXPOSURE_AUTO),
758 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
759 },
760 {
761 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
762 .entity = UVC_GUID_UVC_CAMERA,
763 .selector = UVC_CT_AE_PRIORITY_CONTROL,
764 .size = 1,
765 .offset = 0,
766 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
767 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
768 },
769 {
770 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
771 .entity = UVC_GUID_UVC_CAMERA,
772 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
773 .size = 32,
774 .offset = 0,
775 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
776 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
777 .master_id = V4L2_CID_EXPOSURE_AUTO,
778 .master_manual = V4L2_EXPOSURE_MANUAL,
779 },
780 {
781 .id = V4L2_CID_AUTO_WHITE_BALANCE,
782 .entity = UVC_GUID_UVC_PROCESSING,
783 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
784 .size = 1,
785 .offset = 0,
786 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
787 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
788 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
789 },
790 {
791 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
792 .entity = UVC_GUID_UVC_PROCESSING,
793 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
794 .size = 16,
795 .offset = 0,
796 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
797 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
798 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
799 .master_manual = 0,
800 },
801 {
802 .id = V4L2_CID_AUTO_WHITE_BALANCE,
803 .entity = UVC_GUID_UVC_PROCESSING,
804 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
805 .size = 1,
806 .offset = 0,
807 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
808 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
809 .slave_ids = { V4L2_CID_BLUE_BALANCE,
810 V4L2_CID_RED_BALANCE },
811 },
812 {
813 .id = V4L2_CID_BLUE_BALANCE,
814 .entity = UVC_GUID_UVC_PROCESSING,
815 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
816 .size = 16,
817 .offset = 0,
818 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
819 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
820 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
821 .master_manual = 0,
822 },
823 {
824 .id = V4L2_CID_RED_BALANCE,
825 .entity = UVC_GUID_UVC_PROCESSING,
826 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
827 .size = 16,
828 .offset = 16,
829 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
830 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
831 .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
832 .master_manual = 0,
833 },
834 {
835 .id = V4L2_CID_FOCUS_ABSOLUTE,
836 .entity = UVC_GUID_UVC_CAMERA,
837 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
838 .size = 16,
839 .offset = 0,
840 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
841 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
842 .master_id = V4L2_CID_FOCUS_AUTO,
843 .master_manual = 0,
844 },
845 {
846 .id = V4L2_CID_FOCUS_AUTO,
847 .entity = UVC_GUID_UVC_CAMERA,
848 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
849 .size = 1,
850 .offset = 0,
851 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
852 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
853 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
854 },
855 {
856 .id = V4L2_CID_IRIS_ABSOLUTE,
857 .entity = UVC_GUID_UVC_CAMERA,
858 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
859 .size = 16,
860 .offset = 0,
861 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
862 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
863 },
864 {
865 .id = V4L2_CID_IRIS_RELATIVE,
866 .entity = UVC_GUID_UVC_CAMERA,
867 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
868 .size = 8,
869 .offset = 0,
870 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
871 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
872 },
873 {
874 .id = V4L2_CID_ZOOM_ABSOLUTE,
875 .entity = UVC_GUID_UVC_CAMERA,
876 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
877 .size = 16,
878 .offset = 0,
879 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
880 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
881 },
882 {
883 .id = V4L2_CID_ZOOM_CONTINUOUS,
884 .entity = UVC_GUID_UVC_CAMERA,
885 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
886 .size = 0,
887 .offset = 0,
888 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
889 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
890 .get = uvc_ctrl_get_zoom,
891 .set = uvc_ctrl_set_zoom,
892 },
893 {
894 .id = V4L2_CID_PAN_ABSOLUTE,
895 .entity = UVC_GUID_UVC_CAMERA,
896 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
897 .size = 32,
898 .offset = 0,
899 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
900 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
901 },
902 {
903 .id = V4L2_CID_TILT_ABSOLUTE,
904 .entity = UVC_GUID_UVC_CAMERA,
905 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
906 .size = 32,
907 .offset = 32,
908 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
909 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
910 },
911 {
912 .id = V4L2_CID_PAN_SPEED,
913 .entity = UVC_GUID_UVC_CAMERA,
914 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
915 .size = 16,
916 .offset = 0,
917 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
918 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
919 .get = uvc_ctrl_get_rel_speed,
920 .set = uvc_ctrl_set_rel_speed,
921 },
922 {
923 .id = V4L2_CID_TILT_SPEED,
924 .entity = UVC_GUID_UVC_CAMERA,
925 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
926 .size = 16,
927 .offset = 16,
928 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
929 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
930 .get = uvc_ctrl_get_rel_speed,
931 .set = uvc_ctrl_set_rel_speed,
932 },
933 {
934 .id = V4L2_CID_PRIVACY,
935 .entity = UVC_GUID_UVC_CAMERA,
936 .selector = UVC_CT_PRIVACY_CONTROL,
937 .size = 1,
938 .offset = 0,
939 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
940 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
941 },
942 {
943 .id = V4L2_CID_PRIVACY,
944 .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
945 .selector = UVC_CT_PRIVACY_CONTROL,
946 .size = 1,
947 .offset = 0,
948 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
949 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
950 },
951 {
952 .entity = UVC_GUID_UVC_PROCESSING,
953 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
954 .filter_mapping = uvc_ctrl_filter_plf_mapping,
955 },
956 {
957 .id = V4L2_CID_UVC_REGION_OF_INTEREST_RECT,
958 .entity = UVC_GUID_UVC_CAMERA,
959 .selector = UVC_CT_REGION_OF_INTEREST_CONTROL,
960 .size = sizeof(struct uvc_rect) * 8,
961 .offset = 0,
962 .v4l2_type = V4L2_CTRL_TYPE_RECT,
963 .data_type = UVC_CTRL_DATA_TYPE_RECT,
964 .get = uvc_get_rect,
965 .set = uvc_set_rect,
966 .name = "Region of Interest Rectangle",
967 },
968 {
969 .id = V4L2_CID_UVC_REGION_OF_INTEREST_AUTO,
970 .entity = UVC_GUID_UVC_CAMERA,
971 .selector = UVC_CT_REGION_OF_INTEREST_CONTROL,
972 .size = 16,
973 .offset = 64,
974 .v4l2_type = V4L2_CTRL_TYPE_BITMASK,
975 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
976 .name = "Region of Interest Auto Ctrls",
977 },
978 };
979
980 /* ------------------------------------------------------------------------
981 * Utility functions
982 */
983
uvc_ctrl_data(struct uvc_control * ctrl,int id)984 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
985 {
986 return ctrl->uvc_data + id * ctrl->info.size;
987 }
988
uvc_test_bit(const u8 * data,int bit)989 static inline int uvc_test_bit(const u8 *data, int bit)
990 {
991 return (data[bit >> 3] >> (bit & 7)) & 1;
992 }
993
uvc_clear_bit(u8 * data,int bit)994 static inline void uvc_clear_bit(u8 *data, int bit)
995 {
996 data[bit >> 3] &= ~(1 << (bit & 7));
997 }
998
uvc_menu_to_v4l2_menu(struct uvc_control_mapping * mapping,s32 val)999 static s32 uvc_menu_to_v4l2_menu(struct uvc_control_mapping *mapping, s32 val)
1000 {
1001 unsigned int i;
1002
1003 for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
1004 u32 menu_value;
1005
1006 if (!test_bit(i, &mapping->menu_mask))
1007 continue;
1008
1009 menu_value = uvc_mapping_get_menu_value(mapping, i);
1010
1011 if (menu_value == val)
1012 return i;
1013 }
1014
1015 return val;
1016 }
1017
1018 /*
1019 * Extract the bit string specified by mapping->offset and mapping->size
1020 * from the little-endian data stored at 'data' and return the result as
1021 * a signed 32bit integer. Sign extension will be performed if the mapping
1022 * references a signed data type.
1023 */
uvc_get_le_value(struct uvc_control_mapping * mapping,u8 query,const void * uvc_in,size_t v4l2_size,void * v4l2_out)1024 static int uvc_get_le_value(struct uvc_control_mapping *mapping,
1025 u8 query, const void *uvc_in, size_t v4l2_size,
1026 void *v4l2_out)
1027 {
1028 int offset = mapping->offset;
1029 int bits = mapping->size;
1030 const u8 *data = uvc_in;
1031 s32 *out = v4l2_out;
1032 s32 value = 0;
1033 u8 mask;
1034
1035 if (WARN_ON(v4l2_size != sizeof(s32)))
1036 return -EINVAL;
1037
1038 data += offset / 8;
1039 offset &= 7;
1040 mask = ((1LL << bits) - 1) << offset;
1041
1042 while (1) {
1043 u8 byte = *data & mask;
1044 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
1045 bits -= 8 - max(offset, 0);
1046 if (bits <= 0)
1047 break;
1048
1049 offset -= 8;
1050 mask = (1 << bits) - 1;
1051 data++;
1052 }
1053
1054 /* Sign-extend the value if needed. */
1055 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1056 value |= -(value & (1 << (mapping->size - 1)));
1057
1058 /* If it is a menu, convert from uvc to v4l2. */
1059 if (mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1060 *out = value;
1061 return 0;
1062 }
1063
1064 switch (query) {
1065 case UVC_GET_CUR:
1066 case UVC_GET_DEF:
1067 *out = uvc_menu_to_v4l2_menu(mapping, value);
1068 return 0;
1069 }
1070
1071 *out = value;
1072 return 0;
1073 }
1074
1075 /*
1076 * Set the bit string specified by mapping->offset and mapping->size
1077 * in the little-endian data stored at 'data' to the value 'value'.
1078 */
uvc_set_le_value(struct uvc_control_mapping * mapping,size_t v4l2_size,const void * v4l2_in,void * uvc_out)1079 static int uvc_set_le_value(struct uvc_control_mapping *mapping,
1080 size_t v4l2_size, const void *v4l2_in,
1081 void *uvc_out)
1082 {
1083 int offset = mapping->offset;
1084 int bits = mapping->size;
1085 u8 *data = uvc_out;
1086 s32 value;
1087 u8 mask;
1088
1089 if (WARN_ON(v4l2_size != sizeof(s32)))
1090 return -EINVAL;
1091
1092 value = *(s32 *)v4l2_in;
1093
1094 switch (mapping->v4l2_type) {
1095 case V4L2_CTRL_TYPE_MENU:
1096 value = uvc_mapping_get_menu_value(mapping, value);
1097 break;
1098 case V4L2_CTRL_TYPE_BUTTON:
1099 /*
1100 * According to the v4l2 spec, writing any value to a button
1101 * control should result in the action belonging to the button
1102 * control being triggered. UVC devices however want to see a 1
1103 * written -> override value.
1104 */
1105 value = -1;
1106 break;
1107 default:
1108 break;
1109 }
1110
1111 data += offset / 8;
1112 offset &= 7;
1113
1114 for (; bits > 0; data++) {
1115 mask = ((1LL << bits) - 1) << offset;
1116 *data = (*data & ~mask) | ((value << offset) & mask);
1117 value >>= offset ? offset : 8;
1118 bits -= 8 - offset;
1119 offset = 0;
1120 }
1121
1122 return 0;
1123 }
1124
1125 /* ------------------------------------------------------------------------
1126 * Terminal and unit management
1127 */
1128
uvc_entity_match_guid(const struct uvc_entity * entity,const u8 guid[16])1129 static int uvc_entity_match_guid(const struct uvc_entity *entity,
1130 const u8 guid[16])
1131 {
1132 return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
1133 }
1134
1135 /* ------------------------------------------------------------------------
1136 * UVC Controls
1137 */
1138
__uvc_find_control(struct uvc_entity * entity,u32 v4l2_id,struct uvc_control_mapping ** mapping,struct uvc_control ** control,int next,int next_compound)1139 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
1140 struct uvc_control_mapping **mapping, struct uvc_control **control,
1141 int next, int next_compound)
1142 {
1143 struct uvc_control *ctrl;
1144 struct uvc_control_mapping *map;
1145 unsigned int i;
1146
1147 if (entity == NULL)
1148 return;
1149
1150 for (i = 0; i < entity->ncontrols; ++i) {
1151 ctrl = &entity->controls[i];
1152 if (!ctrl->initialized)
1153 continue;
1154
1155 list_for_each_entry(map, &ctrl->info.mappings, list) {
1156 if (map->id == v4l2_id && !next && !next_compound) {
1157 *control = ctrl;
1158 *mapping = map;
1159 return;
1160 }
1161
1162 if ((*mapping == NULL || (*mapping)->id > map->id) &&
1163 (map->id > v4l2_id) &&
1164 (uvc_ctrl_mapping_is_compound(map) ?
1165 next_compound : next)) {
1166 *control = ctrl;
1167 *mapping = map;
1168 }
1169 }
1170 }
1171 }
1172
uvc_find_control(struct uvc_video_chain * chain,u32 v4l2_id,struct uvc_control_mapping ** mapping)1173 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
1174 u32 v4l2_id, struct uvc_control_mapping **mapping)
1175 {
1176 struct uvc_control *ctrl = NULL;
1177 struct uvc_entity *entity;
1178 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
1179 int next_compound = v4l2_id & V4L2_CTRL_FLAG_NEXT_COMPOUND;
1180
1181 *mapping = NULL;
1182
1183 /* Mask the query flags. */
1184 v4l2_id &= V4L2_CTRL_ID_MASK;
1185
1186 /* Find the control. */
1187 list_for_each_entry(entity, &chain->entities, chain) {
1188 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next,
1189 next_compound);
1190 if (ctrl && !next && !next_compound)
1191 return ctrl;
1192 }
1193
1194 if (!ctrl && !next && !next_compound)
1195 uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
1196 v4l2_id);
1197
1198 return ctrl;
1199 }
1200
uvc_ctrl_populate_cache(struct uvc_video_chain * chain,struct uvc_control * ctrl)1201 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
1202 struct uvc_control *ctrl)
1203 {
1204 int ret;
1205
1206 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1207 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
1208 chain->dev->intfnum, ctrl->info.selector,
1209 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
1210 ctrl->info.size);
1211 if (ret < 0)
1212 return ret;
1213 }
1214
1215 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
1216 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
1217 chain->dev->intfnum, ctrl->info.selector,
1218 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
1219 ctrl->info.size);
1220 if (ret < 0)
1221 return ret;
1222 }
1223 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
1224 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
1225 chain->dev->intfnum, ctrl->info.selector,
1226 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
1227 ctrl->info.size);
1228 if (ret < 0)
1229 return ret;
1230 }
1231 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
1232 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
1233 chain->dev->intfnum, ctrl->info.selector,
1234 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
1235 ctrl->info.size);
1236 if (ret < 0) {
1237 if (UVC_ENTITY_TYPE(ctrl->entity) !=
1238 UVC_VC_EXTENSION_UNIT)
1239 return ret;
1240
1241 /*
1242 * GET_RES is mandatory for XU controls, but some
1243 * cameras still choke on it. Ignore errors and set the
1244 * resolution value to zero.
1245 */
1246 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
1247 "UVC non compliance - GET_RES failed on "
1248 "an XU control. Enabling workaround.\n");
1249 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
1250 ctrl->info.size);
1251 }
1252 }
1253
1254 ctrl->cached = 1;
1255 return 0;
1256 }
1257
__uvc_ctrl_load_cur(struct uvc_video_chain * chain,struct uvc_control * ctrl)1258 static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
1259 struct uvc_control *ctrl)
1260 {
1261 u8 *data;
1262 int ret;
1263
1264 if (ctrl->loaded)
1265 return 0;
1266
1267 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1268
1269 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1270 memset(data, 0, ctrl->info.size);
1271 ctrl->loaded = 1;
1272
1273 return 0;
1274 }
1275
1276 if (ctrl->entity->get_cur)
1277 ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
1278 ctrl->info.selector, data,
1279 ctrl->info.size);
1280 else
1281 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1282 ctrl->entity->id, chain->dev->intfnum,
1283 ctrl->info.selector, data,
1284 ctrl->info.size);
1285
1286 if (ret < 0)
1287 return ret;
1288
1289 ctrl->loaded = 1;
1290
1291 return ret;
1292 }
1293
__uvc_ctrl_get(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,s32 * value)1294 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1295 struct uvc_control *ctrl,
1296 struct uvc_control_mapping *mapping,
1297 s32 *value)
1298 {
1299 int ret;
1300
1301 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1302 return -EACCES;
1303
1304 ret = __uvc_ctrl_load_cur(chain, ctrl);
1305 if (ret < 0)
1306 return ret;
1307
1308 *value = uvc_mapping_get_s32(mapping, UVC_GET_CUR,
1309 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1310
1311 return 0;
1312 }
1313
__uvc_query_v4l2_class(struct uvc_video_chain * chain,u32 req_id,u32 found_id)1314 static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1315 u32 found_id)
1316 {
1317 bool find_next = req_id &
1318 (V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND);
1319 unsigned int i;
1320
1321 req_id &= V4L2_CTRL_ID_MASK;
1322
1323 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
1324 if (!(chain->ctrl_class_bitmap & BIT(i)))
1325 continue;
1326 if (!find_next) {
1327 if (uvc_control_classes[i] == req_id)
1328 return i;
1329 continue;
1330 }
1331 if (uvc_control_classes[i] > req_id &&
1332 uvc_control_classes[i] < found_id)
1333 return i;
1334 }
1335
1336 return -ENODEV;
1337 }
1338
uvc_query_v4l2_class(struct uvc_video_chain * chain,u32 req_id,u32 found_id,struct v4l2_query_ext_ctrl * v4l2_ctrl)1339 static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1340 u32 found_id,
1341 struct v4l2_query_ext_ctrl *v4l2_ctrl)
1342 {
1343 int idx;
1344
1345 idx = __uvc_query_v4l2_class(chain, req_id, found_id);
1346 if (idx < 0)
1347 return -ENODEV;
1348
1349 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1350 v4l2_ctrl->id = uvc_control_classes[idx];
1351 strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
1352 sizeof(v4l2_ctrl->name));
1353 v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
1354 v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
1355 | V4L2_CTRL_FLAG_READ_ONLY;
1356 return 0;
1357 }
1358
uvc_ctrl_is_readable(u32 which,struct uvc_control * ctrl,struct uvc_control_mapping * mapping)1359 static bool uvc_ctrl_is_readable(u32 which, struct uvc_control *ctrl,
1360 struct uvc_control_mapping *mapping)
1361 {
1362 if (which == V4L2_CTRL_WHICH_CUR_VAL)
1363 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR);
1364
1365 if (which == V4L2_CTRL_WHICH_DEF_VAL)
1366 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF);
1367
1368 /* Types with implicit boundaries. */
1369 switch (mapping->v4l2_type) {
1370 case V4L2_CTRL_TYPE_MENU:
1371 case V4L2_CTRL_TYPE_BOOLEAN:
1372 case V4L2_CTRL_TYPE_BUTTON:
1373 return true;
1374 case V4L2_CTRL_TYPE_BITMASK:
1375 return (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) ||
1376 (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX);
1377 default:
1378 break;
1379 }
1380
1381 if (which == V4L2_CTRL_WHICH_MIN_VAL)
1382 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN);
1383
1384 if (which == V4L2_CTRL_WHICH_MAX_VAL)
1385 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX);
1386
1387 return false;
1388 }
1389
1390 /*
1391 * Check if control @v4l2_id can be accessed by the given control @ioctl
1392 * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
1393 *
1394 * For set operations on slave controls, check if the master's value is set to
1395 * manual, either in the others controls set in the same ioctl call, or from
1396 * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
1397 * both the master and slave control, such as for instance setting
1398 * auto_exposure=1, exposure_time_absolute=251.
1399 */
uvc_ctrl_is_accessible(struct uvc_video_chain * chain,u32 v4l2_id,const struct v4l2_ext_controls * ctrls,unsigned long ioctl)1400 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
1401 const struct v4l2_ext_controls *ctrls,
1402 unsigned long ioctl)
1403 {
1404 struct uvc_control_mapping *master_map = NULL;
1405 struct uvc_control *master_ctrl = NULL;
1406 struct uvc_control_mapping *mapping;
1407 struct uvc_control *ctrl;
1408 s32 val;
1409 int ret;
1410 int i;
1411
1412 if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
1413 return -EACCES;
1414
1415 ctrl = uvc_find_control(chain, v4l2_id, &mapping);
1416 if (!ctrl)
1417 return -EINVAL;
1418
1419 if (ioctl == VIDIOC_G_EXT_CTRLS)
1420 return uvc_ctrl_is_readable(ctrls->which, ctrl, mapping);
1421
1422 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1423 return -EACCES;
1424
1425 if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id)
1426 return 0;
1427
1428 /*
1429 * Iterate backwards in cases where the master control is accessed
1430 * multiple times in the same ioctl. We want the last value.
1431 */
1432 for (i = ctrls->count - 1; i >= 0; i--) {
1433 if (ctrls->controls[i].id == mapping->master_id)
1434 return ctrls->controls[i].value ==
1435 mapping->master_manual ? 0 : -EACCES;
1436 }
1437
1438 __uvc_find_control(ctrl->entity, mapping->master_id, &master_map,
1439 &master_ctrl, 0, 0);
1440
1441 if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1442 return 0;
1443 if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map)))
1444 return -EIO;
1445
1446 ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1447 if (ret >= 0 && val != mapping->master_manual)
1448 return -EACCES;
1449
1450 return 0;
1451 }
1452
uvc_map_get_name(const struct uvc_control_mapping * map)1453 static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
1454 {
1455 const char *name;
1456
1457 if (map->name)
1458 return map->name;
1459
1460 name = v4l2_ctrl_get_name(map->id);
1461 if (name)
1462 return name;
1463
1464 return "Unknown Control";
1465 }
1466
uvc_get_ctrl_bitmap(struct uvc_control * ctrl,struct uvc_control_mapping * mapping)1467 static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl,
1468 struct uvc_control_mapping *mapping)
1469 {
1470 /*
1471 * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
1472 * the number of bits supported. Those controls do not list GET_MAX
1473 * as supported.
1474 */
1475 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1476 return uvc_mapping_get_s32(mapping, UVC_GET_RES,
1477 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1478
1479 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1480 return uvc_mapping_get_s32(mapping, UVC_GET_MAX,
1481 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1482
1483 return ~0;
1484 }
1485
__uvc_queryctrl_boundaries(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_query_ext_ctrl * v4l2_ctrl)1486 static int __uvc_queryctrl_boundaries(struct uvc_video_chain *chain,
1487 struct uvc_control *ctrl,
1488 struct uvc_control_mapping *mapping,
1489 struct v4l2_query_ext_ctrl *v4l2_ctrl)
1490 {
1491 if (!ctrl->cached) {
1492 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1493 if (ret < 0)
1494 return ret;
1495 }
1496
1497 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1498 v4l2_ctrl->default_value = uvc_mapping_get_s32(mapping,
1499 UVC_GET_DEF, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1500 }
1501
1502 switch (mapping->v4l2_type) {
1503 case V4L2_CTRL_TYPE_MENU:
1504 v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1;
1505 v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1;
1506 v4l2_ctrl->step = 1;
1507 return 0;
1508
1509 case V4L2_CTRL_TYPE_BOOLEAN:
1510 v4l2_ctrl->minimum = 0;
1511 v4l2_ctrl->maximum = 1;
1512 v4l2_ctrl->step = 1;
1513 return 0;
1514
1515 case V4L2_CTRL_TYPE_BUTTON:
1516 v4l2_ctrl->minimum = 0;
1517 v4l2_ctrl->maximum = 0;
1518 v4l2_ctrl->step = 0;
1519 return 0;
1520
1521 case V4L2_CTRL_TYPE_BITMASK:
1522 v4l2_ctrl->minimum = 0;
1523 v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping);
1524 v4l2_ctrl->step = 0;
1525 return 0;
1526
1527 default:
1528 break;
1529 }
1530
1531 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1532 v4l2_ctrl->minimum = uvc_mapping_get_s32(mapping, UVC_GET_MIN,
1533 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1534 else
1535 v4l2_ctrl->minimum = 0;
1536
1537 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1538 v4l2_ctrl->maximum = uvc_mapping_get_s32(mapping, UVC_GET_MAX,
1539 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1540 else
1541 v4l2_ctrl->maximum = 0;
1542
1543 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1544 v4l2_ctrl->step = uvc_mapping_get_s32(mapping, UVC_GET_RES,
1545 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1546 else
1547 v4l2_ctrl->step = 0;
1548
1549 return 0;
1550 }
1551
uvc_mapping_v4l2_size(struct uvc_control_mapping * mapping)1552 static size_t uvc_mapping_v4l2_size(struct uvc_control_mapping *mapping)
1553 {
1554 if (mapping->v4l2_type == V4L2_CTRL_TYPE_RECT)
1555 return sizeof(struct v4l2_rect);
1556
1557 if (uvc_ctrl_mapping_is_compound(mapping))
1558 return DIV_ROUND_UP(mapping->size, 8);
1559
1560 return sizeof(s32);
1561 }
1562
__uvc_query_v4l2_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_query_ext_ctrl * v4l2_ctrl)1563 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1564 struct uvc_control *ctrl,
1565 struct uvc_control_mapping *mapping,
1566 struct v4l2_query_ext_ctrl *v4l2_ctrl)
1567 {
1568 struct uvc_control_mapping *master_map = NULL;
1569 struct uvc_control *master_ctrl = NULL;
1570
1571 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1572 v4l2_ctrl->id = mapping->id;
1573 v4l2_ctrl->type = mapping->v4l2_type;
1574 strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
1575 sizeof(v4l2_ctrl->name));
1576 v4l2_ctrl->flags = 0;
1577
1578 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1579 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1580 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1581 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1582 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) &&
1583 (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN))
1584 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX;
1585
1586 if (mapping->master_id)
1587 __uvc_find_control(ctrl->entity, mapping->master_id,
1588 &master_map, &master_ctrl, 0, 0);
1589 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1590 s32 val;
1591 int ret;
1592
1593 if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map)))
1594 return -EIO;
1595
1596 ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1597 if (ret < 0)
1598 return ret;
1599
1600 if (val != mapping->master_manual)
1601 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1602 }
1603
1604 v4l2_ctrl->elem_size = uvc_mapping_v4l2_size(mapping);
1605 v4l2_ctrl->elems = 1;
1606
1607 if (v4l2_ctrl->type >= V4L2_CTRL_COMPOUND_TYPES) {
1608 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
1609 v4l2_ctrl->default_value = 0;
1610 v4l2_ctrl->minimum = 0;
1611 v4l2_ctrl->maximum = 0;
1612 v4l2_ctrl->step = 0;
1613 return 0;
1614 }
1615
1616 return __uvc_queryctrl_boundaries(chain, ctrl, mapping, v4l2_ctrl);
1617 }
1618
uvc_query_v4l2_ctrl(struct uvc_video_chain * chain,struct v4l2_query_ext_ctrl * v4l2_ctrl)1619 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1620 struct v4l2_query_ext_ctrl *v4l2_ctrl)
1621 {
1622 struct uvc_control *ctrl;
1623 struct uvc_control_mapping *mapping;
1624 int ret;
1625
1626 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1627 if (ret < 0)
1628 return -ERESTARTSYS;
1629
1630 /* Check if the ctrl is a know class */
1631 if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
1632 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
1633 if (!ret)
1634 goto done;
1635 }
1636
1637 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1638 if (ctrl == NULL) {
1639 ret = -EINVAL;
1640 goto done;
1641 }
1642
1643 /*
1644 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
1645 * a class should be inserted between the previous control and the one
1646 * we have just found.
1647 */
1648 if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1649 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
1650 v4l2_ctrl);
1651 if (!ret)
1652 goto done;
1653 }
1654
1655 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1656 done:
1657 mutex_unlock(&chain->ctrl_mutex);
1658 return ret;
1659 }
1660
1661 /*
1662 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1663 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1664 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1665 * Balance V4L2 controls use the White Balance Component UVC control) or
1666 * otherwise translated. The approach we take here is to use a translation
1667 * table for the controls that can be mapped directly, and handle the others
1668 * manually.
1669 */
uvc_query_v4l2_menu(struct uvc_video_chain * chain,struct v4l2_querymenu * query_menu)1670 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1671 struct v4l2_querymenu *query_menu)
1672 {
1673 struct uvc_control_mapping *mapping;
1674 struct uvc_control *ctrl;
1675 u32 index = query_menu->index;
1676 u32 id = query_menu->id;
1677 const char *name;
1678 int ret;
1679
1680 memset(query_menu, 0, sizeof(*query_menu));
1681 query_menu->id = id;
1682 query_menu->index = index;
1683
1684 if (index >= BITS_PER_TYPE(mapping->menu_mask))
1685 return -EINVAL;
1686
1687 ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1688 if (ret < 0)
1689 return -ERESTARTSYS;
1690
1691 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1692 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1693 ret = -EINVAL;
1694 goto done;
1695 }
1696
1697 if (!test_bit(query_menu->index, &mapping->menu_mask)) {
1698 ret = -EINVAL;
1699 goto done;
1700 }
1701
1702 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
1703 int mask;
1704
1705 if (!ctrl->cached) {
1706 ret = uvc_ctrl_populate_cache(chain, ctrl);
1707 if (ret < 0)
1708 goto done;
1709 }
1710
1711 mask = uvc_mapping_get_menu_value(mapping, query_menu->index);
1712 if (mask < 0) {
1713 ret = mask;
1714 goto done;
1715 }
1716
1717 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) {
1718 ret = -EINVAL;
1719 goto done;
1720 }
1721 }
1722
1723 name = uvc_mapping_get_menu_name(mapping, query_menu->index);
1724 if (!name) {
1725 ret = -EINVAL;
1726 goto done;
1727 }
1728
1729 strscpy(query_menu->name, name, sizeof(query_menu->name));
1730
1731 done:
1732 mutex_unlock(&chain->ctrl_mutex);
1733 return ret;
1734 }
1735
1736 /* --------------------------------------------------------------------------
1737 * Ctrl event handling
1738 */
1739
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)1740 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1741 struct v4l2_event *ev,
1742 struct uvc_control *ctrl,
1743 struct uvc_control_mapping *mapping,
1744 s32 value, u32 changes)
1745 {
1746 struct v4l2_query_ext_ctrl v4l2_ctrl;
1747
1748 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1749
1750 memset(ev, 0, sizeof(*ev));
1751 ev->type = V4L2_EVENT_CTRL;
1752 ev->id = v4l2_ctrl.id;
1753 ev->u.ctrl.value = value;
1754 ev->u.ctrl.changes = changes;
1755 ev->u.ctrl.type = v4l2_ctrl.type;
1756 ev->u.ctrl.flags = v4l2_ctrl.flags;
1757 ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1758 ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1759 ev->u.ctrl.step = v4l2_ctrl.step;
1760 ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1761 }
1762
1763 /*
1764 * Send control change events to all subscribers for the @ctrl control. By
1765 * default the subscriber that generated the event, as identified by @handle,
1766 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1767 * @handle can be NULL for asynchronous events related to auto-update controls,
1768 * in which case all subscribers are notified.
1769 */
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)1770 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1771 struct uvc_fh *handle, struct uvc_control *ctrl,
1772 struct uvc_control_mapping *mapping, s32 value, u32 changes)
1773 {
1774 struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1775 struct v4l2_subscribed_event *sev;
1776 struct v4l2_event ev;
1777
1778 if (list_empty(&mapping->ev_subs))
1779 return;
1780
1781 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1782
1783 list_for_each_entry(sev, &mapping->ev_subs, node) {
1784 if (sev->fh != originator ||
1785 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1786 (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1787 v4l2_event_queue_fh(sev->fh, &ev);
1788 }
1789 }
1790
1791 /*
1792 * Send control change events for the slave of the @master control identified
1793 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1794 * generated the event and may be NULL for auto-update events.
1795 */
uvc_ctrl_send_slave_event(struct uvc_video_chain * chain,struct uvc_fh * handle,struct uvc_control * master,u32 slave_id)1796 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1797 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1798 {
1799 struct uvc_control_mapping *mapping = NULL;
1800 struct uvc_control *ctrl = NULL;
1801 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1802 s32 val = 0;
1803
1804 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0, 0);
1805 if (ctrl == NULL)
1806 return;
1807
1808 if (uvc_ctrl_mapping_is_compound(mapping) ||
1809 __uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1810 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1811
1812 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1813 }
1814
uvc_ctrl_set_handle(struct uvc_fh * handle,struct uvc_control * ctrl,struct uvc_fh * new_handle)1815 static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
1816 struct uvc_fh *new_handle)
1817 {
1818 lockdep_assert_held(&handle->chain->ctrl_mutex);
1819
1820 if (new_handle) {
1821 if (ctrl->handle)
1822 dev_warn_ratelimited(&handle->stream->dev->udev->dev,
1823 "UVC non compliance: Setting an async control with a pending operation.");
1824
1825 if (new_handle == ctrl->handle)
1826 return;
1827
1828 if (ctrl->handle) {
1829 WARN_ON(!ctrl->handle->pending_async_ctrls);
1830 if (ctrl->handle->pending_async_ctrls)
1831 ctrl->handle->pending_async_ctrls--;
1832 }
1833
1834 ctrl->handle = new_handle;
1835 handle->pending_async_ctrls++;
1836 return;
1837 }
1838
1839 /* Cannot clear the handle for a control not owned by us.*/
1840 if (WARN_ON(ctrl->handle != handle))
1841 return;
1842
1843 ctrl->handle = NULL;
1844 if (WARN_ON(!handle->pending_async_ctrls))
1845 return;
1846 handle->pending_async_ctrls--;
1847 }
1848
uvc_ctrl_status_event(struct uvc_video_chain * chain,struct uvc_control * ctrl,const u8 * data)1849 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1850 struct uvc_control *ctrl, const u8 *data)
1851 {
1852 struct uvc_control_mapping *mapping;
1853 struct uvc_fh *handle;
1854 unsigned int i;
1855
1856 mutex_lock(&chain->ctrl_mutex);
1857
1858 handle = ctrl->handle;
1859 if (handle)
1860 uvc_ctrl_set_handle(handle, ctrl, NULL);
1861
1862 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1863 s32 value;
1864
1865 if (uvc_ctrl_mapping_is_compound(mapping))
1866 value = 0;
1867 else
1868 value = uvc_mapping_get_s32(mapping, UVC_GET_CUR, data);
1869
1870 /*
1871 * handle may be NULL here if the device sends auto-update
1872 * events without a prior related control set from userspace.
1873 */
1874 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1875 if (!mapping->slave_ids[i])
1876 break;
1877
1878 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1879 mapping->slave_ids[i]);
1880 }
1881
1882 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1883 V4L2_EVENT_CTRL_CH_VALUE);
1884 }
1885
1886 mutex_unlock(&chain->ctrl_mutex);
1887 }
1888
uvc_ctrl_status_event_work(struct work_struct * work)1889 static void uvc_ctrl_status_event_work(struct work_struct *work)
1890 {
1891 struct uvc_device *dev = container_of(work, struct uvc_device,
1892 async_ctrl.work);
1893 struct uvc_ctrl_work *w = &dev->async_ctrl;
1894 int ret;
1895
1896 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1897
1898 /* The barrier is needed to synchronize with uvc_status_stop(). */
1899 if (smp_load_acquire(&dev->flush_status))
1900 return;
1901
1902 /* Resubmit the URB. */
1903 w->urb->interval = dev->int_ep->desc.bInterval;
1904 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1905 if (ret < 0)
1906 dev_err(&dev->udev->dev,
1907 "Failed to resubmit status URB (%d).\n", ret);
1908 }
1909
uvc_ctrl_status_event_async(struct urb * urb,struct uvc_video_chain * chain,struct uvc_control * ctrl,const u8 * data)1910 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1911 struct uvc_control *ctrl, const u8 *data)
1912 {
1913 struct uvc_device *dev = chain->dev;
1914 struct uvc_ctrl_work *w = &dev->async_ctrl;
1915
1916 if (list_empty(&ctrl->info.mappings))
1917 return false;
1918
1919 w->data = data;
1920 w->urb = urb;
1921 w->chain = chain;
1922 w->ctrl = ctrl;
1923
1924 schedule_work(&w->work);
1925
1926 return true;
1927 }
1928
uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control * xctrls,unsigned int xctrls_count,u32 id)1929 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1930 unsigned int xctrls_count, u32 id)
1931 {
1932 unsigned int i;
1933
1934 for (i = 0; i < xctrls_count; ++i) {
1935 if (xctrls[i].id == id)
1936 return true;
1937 }
1938
1939 return false;
1940 }
1941
uvc_ctrl_send_events(struct uvc_fh * handle,struct uvc_entity * entity,const struct v4l2_ext_control * xctrls,unsigned int xctrls_count)1942 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1943 struct uvc_entity *entity,
1944 const struct v4l2_ext_control *xctrls,
1945 unsigned int xctrls_count)
1946 {
1947 struct uvc_control_mapping *mapping;
1948 struct uvc_control *ctrl;
1949 unsigned int i;
1950 unsigned int j;
1951
1952 for (i = 0; i < xctrls_count; ++i) {
1953 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1954 s32 value;
1955
1956 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1957 if (ctrl->entity != entity)
1958 continue;
1959
1960 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1961 /* Notification will be sent from an Interrupt event. */
1962 continue;
1963
1964 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1965 u32 slave_id = mapping->slave_ids[j];
1966
1967 if (!slave_id)
1968 break;
1969
1970 /*
1971 * We can skip sending an event for the slave if the
1972 * slave is being modified in the same transaction.
1973 */
1974 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1975 slave_id))
1976 continue;
1977
1978 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1979 slave_id);
1980 }
1981
1982 if (uvc_ctrl_mapping_is_compound(mapping))
1983 value = 0;
1984 else
1985 value = xctrls[i].value;
1986 /*
1987 * If the master is being modified in the same transaction
1988 * flags may change too.
1989 */
1990 if (mapping->master_id &&
1991 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1992 mapping->master_id))
1993 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1994
1995 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1996 value, changes);
1997 }
1998 }
1999
uvc_ctrl_add_event(struct v4l2_subscribed_event * sev,unsigned elems)2000 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
2001 {
2002 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
2003 struct uvc_control_mapping *mapping;
2004 struct uvc_control *ctrl;
2005 int ret;
2006
2007 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
2008 if (ret < 0)
2009 return -ERESTARTSYS;
2010
2011 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
2012 ret = 0;
2013 goto done;
2014 }
2015
2016 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
2017 if (ctrl == NULL) {
2018 ret = -EINVAL;
2019 goto done;
2020 }
2021
2022 list_add_tail(&sev->node, &mapping->ev_subs);
2023 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
2024 struct v4l2_event ev;
2025 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
2026 s32 val = 0;
2027
2028 if (uvc_ctrl_mapping_is_compound(mapping) ||
2029 __uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
2030 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2031
2032 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
2033 changes);
2034 /*
2035 * Mark the queue as active, allowing this initial event to be
2036 * accepted.
2037 */
2038 sev->elems = elems;
2039 v4l2_event_queue_fh(sev->fh, &ev);
2040 }
2041
2042 done:
2043 mutex_unlock(&handle->chain->ctrl_mutex);
2044 return ret;
2045 }
2046
uvc_ctrl_del_event(struct v4l2_subscribed_event * sev)2047 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
2048 {
2049 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
2050
2051 mutex_lock(&handle->chain->ctrl_mutex);
2052 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
2053 goto done;
2054 list_del(&sev->node);
2055 done:
2056 mutex_unlock(&handle->chain->ctrl_mutex);
2057 }
2058
2059 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
2060 .add = uvc_ctrl_add_event,
2061 .del = uvc_ctrl_del_event,
2062 .replace = v4l2_ctrl_replace,
2063 .merge = v4l2_ctrl_merge,
2064 };
2065
2066 /* --------------------------------------------------------------------------
2067 * Control transactions
2068 *
2069 * To make extended set operations as atomic as the hardware allows, controls
2070 * are handled using begin/commit/rollback operations.
2071 *
2072 * At the beginning of a set request, uvc_ctrl_begin should be called to
2073 * initialize the request. This function acquires the control lock.
2074 *
2075 * When setting a control, the new value is stored in the control data field
2076 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
2077 * later processing. If the UVC and V4L2 control sizes differ, the current
2078 * value is loaded from the hardware before storing the new value in the data
2079 * field.
2080 *
2081 * After processing all controls in the transaction, uvc_ctrl_commit or
2082 * uvc_ctrl_rollback must be called to apply the pending changes to the
2083 * hardware or revert them. When applying changes, all controls marked as
2084 * dirty will be modified in the UVC device, and the dirty flag will be
2085 * cleared. When reverting controls, the control data field
2086 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
2087 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
2088 * control lock.
2089 */
uvc_ctrl_begin(struct uvc_video_chain * chain)2090 int uvc_ctrl_begin(struct uvc_video_chain *chain)
2091 {
2092 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
2093 }
2094
2095 /*
2096 * Returns the number of uvc controls that have been correctly set, or a
2097 * negative number if there has been an error.
2098 */
uvc_ctrl_commit_entity(struct uvc_device * dev,struct uvc_fh * handle,struct uvc_entity * entity,int rollback,struct uvc_control ** err_ctrl)2099 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
2100 struct uvc_fh *handle,
2101 struct uvc_entity *entity,
2102 int rollback,
2103 struct uvc_control **err_ctrl)
2104 {
2105 unsigned int processed_ctrls = 0;
2106 struct uvc_control *ctrl;
2107 unsigned int i;
2108 int ret = 0;
2109
2110 if (entity == NULL)
2111 return 0;
2112
2113 for (i = 0; i < entity->ncontrols; ++i) {
2114 ctrl = &entity->controls[i];
2115 if (!ctrl->initialized)
2116 continue;
2117
2118 /*
2119 * Reset the loaded flag for auto-update controls that were
2120 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
2121 * uvc_ctrl_get from using the cached value, and for write-only
2122 * controls to prevent uvc_ctrl_set from setting bits not
2123 * explicitly set by the user.
2124 */
2125 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
2126 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
2127 ctrl->loaded = 0;
2128
2129 if (!ctrl->dirty)
2130 continue;
2131
2132 if (!rollback)
2133 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
2134 dev->intfnum, ctrl->info.selector,
2135 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2136 ctrl->info.size);
2137
2138 if (!ret)
2139 processed_ctrls++;
2140
2141 if (rollback || ret < 0)
2142 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2143 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2144 ctrl->info.size);
2145
2146 ctrl->dirty = 0;
2147
2148 if (ret < 0 && !rollback) {
2149 if (err_ctrl)
2150 *err_ctrl = ctrl;
2151 /*
2152 * If we fail to set a control, we need to rollback
2153 * the next ones.
2154 */
2155 rollback = 1;
2156 }
2157
2158 if (!rollback && handle && !ret &&
2159 ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
2160 uvc_ctrl_set_handle(handle, ctrl, handle);
2161 }
2162
2163 if (ret)
2164 return ret;
2165
2166 return processed_ctrls;
2167 }
2168
uvc_ctrl_find_ctrl_idx(struct uvc_entity * entity,struct v4l2_ext_controls * ctrls,struct uvc_control * uvc_control)2169 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
2170 struct v4l2_ext_controls *ctrls,
2171 struct uvc_control *uvc_control)
2172 {
2173 struct uvc_control_mapping *mapping = NULL;
2174 struct uvc_control *ctrl_found = NULL;
2175 unsigned int i;
2176
2177 if (!entity)
2178 return ctrls->count;
2179
2180 for (i = 0; i < ctrls->count; i++) {
2181 __uvc_find_control(entity, ctrls->controls[i].id, &mapping,
2182 &ctrl_found, 0, 0);
2183 if (uvc_control == ctrl_found)
2184 return i;
2185 }
2186
2187 return ctrls->count;
2188 }
2189
__uvc_ctrl_commit(struct uvc_fh * handle,int rollback,struct v4l2_ext_controls * ctrls)2190 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
2191 struct v4l2_ext_controls *ctrls)
2192 {
2193 struct uvc_video_chain *chain = handle->chain;
2194 struct uvc_control *err_ctrl;
2195 struct uvc_entity *entity;
2196 int ret_out = 0;
2197 int ret;
2198
2199 /* Find the control. */
2200 list_for_each_entry(entity, &chain->entities, chain) {
2201 ret = uvc_ctrl_commit_entity(chain->dev, handle, entity,
2202 rollback, &err_ctrl);
2203 if (ret < 0) {
2204 if (ctrls)
2205 ctrls->error_idx =
2206 uvc_ctrl_find_ctrl_idx(entity, ctrls,
2207 err_ctrl);
2208 /*
2209 * When we fail to commit an entity, we need to
2210 * restore the UVC_CTRL_DATA_BACKUP for all the
2211 * controls in the other entities, otherwise our cache
2212 * and the hardware will be out of sync.
2213 */
2214 rollback = 1;
2215
2216 ret_out = ret;
2217 } else if (ret > 0 && !rollback) {
2218 uvc_ctrl_send_events(handle, entity,
2219 ctrls->controls, ctrls->count);
2220 }
2221 }
2222
2223 mutex_unlock(&chain->ctrl_mutex);
2224 return ret_out;
2225 }
2226
uvc_mapping_get_xctrl_compound(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,u32 which,struct v4l2_ext_control * xctrl)2227 static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain,
2228 struct uvc_control *ctrl,
2229 struct uvc_control_mapping *mapping,
2230 u32 which,
2231 struct v4l2_ext_control *xctrl)
2232 {
2233 u8 *data __free(kfree) = NULL;
2234 size_t size;
2235 u8 query;
2236 int ret;
2237 int id;
2238
2239 switch (which) {
2240 case V4L2_CTRL_WHICH_CUR_VAL:
2241 id = UVC_CTRL_DATA_CURRENT;
2242 query = UVC_GET_CUR;
2243 break;
2244 case V4L2_CTRL_WHICH_MIN_VAL:
2245 id = UVC_CTRL_DATA_MIN;
2246 query = UVC_GET_MIN;
2247 break;
2248 case V4L2_CTRL_WHICH_MAX_VAL:
2249 id = UVC_CTRL_DATA_MAX;
2250 query = UVC_GET_MAX;
2251 break;
2252 case V4L2_CTRL_WHICH_DEF_VAL:
2253 id = UVC_CTRL_DATA_DEF;
2254 query = UVC_GET_DEF;
2255 break;
2256 default:
2257 return -EINVAL;
2258 }
2259
2260 size = uvc_mapping_v4l2_size(mapping);
2261 if (xctrl->size < size) {
2262 xctrl->size = size;
2263 return -ENOSPC;
2264 }
2265
2266 data = kmalloc(size, GFP_KERNEL);
2267 if (!data)
2268 return -ENOMEM;
2269
2270 if (which == V4L2_CTRL_WHICH_CUR_VAL)
2271 ret = __uvc_ctrl_load_cur(chain, ctrl);
2272 else
2273 ret = uvc_ctrl_populate_cache(chain, ctrl);
2274
2275 if (ret < 0)
2276 return ret;
2277
2278 ret = mapping->get(mapping, query, uvc_ctrl_data(ctrl, id), size, data);
2279 if (ret < 0)
2280 return ret;
2281
2282 /*
2283 * v4l2_ext_control does not have enough room to fit a compound control.
2284 * Instead, the value is in the user memory at xctrl->ptr. The v4l2
2285 * ioctl helper does not copy it for us.
2286 */
2287 return copy_to_user(xctrl->ptr, data, size) ? -EFAULT : 0;
2288 }
2289
uvc_mapping_get_xctrl_std(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,u32 which,struct v4l2_ext_control * xctrl)2290 static int uvc_mapping_get_xctrl_std(struct uvc_video_chain *chain,
2291 struct uvc_control *ctrl,
2292 struct uvc_control_mapping *mapping,
2293 u32 which, struct v4l2_ext_control *xctrl)
2294 {
2295 struct v4l2_query_ext_ctrl qec;
2296 int ret;
2297
2298 switch (which) {
2299 case V4L2_CTRL_WHICH_CUR_VAL:
2300 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
2301 case V4L2_CTRL_WHICH_DEF_VAL:
2302 case V4L2_CTRL_WHICH_MIN_VAL:
2303 case V4L2_CTRL_WHICH_MAX_VAL:
2304 break;
2305 default:
2306 return -EINVAL;
2307 }
2308
2309 ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, &qec);
2310 if (ret < 0)
2311 return ret;
2312
2313 switch (which) {
2314 case V4L2_CTRL_WHICH_DEF_VAL:
2315 xctrl->value = qec.default_value;
2316 break;
2317 case V4L2_CTRL_WHICH_MIN_VAL:
2318 xctrl->value = qec.minimum;
2319 break;
2320 case V4L2_CTRL_WHICH_MAX_VAL:
2321 xctrl->value = qec.maximum;
2322 break;
2323 }
2324
2325 return 0;
2326 }
2327
uvc_mapping_get_xctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,u32 which,struct v4l2_ext_control * xctrl)2328 static int uvc_mapping_get_xctrl(struct uvc_video_chain *chain,
2329 struct uvc_control *ctrl,
2330 struct uvc_control_mapping *mapping,
2331 u32 which, struct v4l2_ext_control *xctrl)
2332 {
2333 if (uvc_ctrl_mapping_is_compound(mapping))
2334 return uvc_mapping_get_xctrl_compound(chain, ctrl, mapping,
2335 which, xctrl);
2336 return uvc_mapping_get_xctrl_std(chain, ctrl, mapping, which, xctrl);
2337 }
2338
uvc_ctrl_get(struct uvc_video_chain * chain,u32 which,struct v4l2_ext_control * xctrl)2339 int uvc_ctrl_get(struct uvc_video_chain *chain, u32 which,
2340 struct v4l2_ext_control *xctrl)
2341 {
2342 struct uvc_control *ctrl;
2343 struct uvc_control_mapping *mapping;
2344
2345 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
2346 return -EACCES;
2347
2348 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
2349 if (!ctrl)
2350 return -EINVAL;
2351
2352 return uvc_mapping_get_xctrl(chain, ctrl, mapping, which, xctrl);
2353 }
2354
uvc_ctrl_clamp(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,s32 * value_in_out)2355 static int uvc_ctrl_clamp(struct uvc_video_chain *chain,
2356 struct uvc_control *ctrl,
2357 struct uvc_control_mapping *mapping,
2358 s32 *value_in_out)
2359 {
2360 s32 value = *value_in_out;
2361 u32 step;
2362 s32 min;
2363 s32 max;
2364 int ret;
2365
2366 switch (mapping->v4l2_type) {
2367 case V4L2_CTRL_TYPE_INTEGER:
2368 if (!ctrl->cached) {
2369 ret = uvc_ctrl_populate_cache(chain, ctrl);
2370 if (ret < 0)
2371 return ret;
2372 }
2373
2374 min = uvc_mapping_get_s32(mapping, UVC_GET_MIN,
2375 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
2376 max = uvc_mapping_get_s32(mapping, UVC_GET_MAX,
2377 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
2378 step = uvc_mapping_get_s32(mapping, UVC_GET_RES,
2379 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
2380 if (step == 0)
2381 step = 1;
2382
2383 value = min + DIV_ROUND_CLOSEST((u32)(value - min), step) * step;
2384 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
2385 value = clamp(value, min, max);
2386 else
2387 value = clamp_t(u32, value, min, max);
2388 *value_in_out = value;
2389 return 0;
2390
2391 case V4L2_CTRL_TYPE_BITMASK:
2392 if (!ctrl->cached) {
2393 ret = uvc_ctrl_populate_cache(chain, ctrl);
2394 if (ret < 0)
2395 return ret;
2396 }
2397
2398 value &= uvc_get_ctrl_bitmap(ctrl, mapping);
2399 *value_in_out = value;
2400 return 0;
2401
2402 case V4L2_CTRL_TYPE_BOOLEAN:
2403 *value_in_out = clamp(value, 0, 1);
2404 return 0;
2405
2406 case V4L2_CTRL_TYPE_MENU:
2407 if (value < (ffs(mapping->menu_mask) - 1) ||
2408 value > (fls(mapping->menu_mask) - 1))
2409 return -ERANGE;
2410
2411 if (!test_bit(value, &mapping->menu_mask))
2412 return -EINVAL;
2413
2414 /*
2415 * Valid menu indices are reported by the GET_RES request for
2416 * UVC controls that support it.
2417 */
2418 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
2419 int val = uvc_mapping_get_menu_value(mapping, value);
2420 if (!ctrl->cached) {
2421 ret = uvc_ctrl_populate_cache(chain, ctrl);
2422 if (ret < 0)
2423 return ret;
2424 }
2425
2426 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & val))
2427 return -EINVAL;
2428 }
2429 return 0;
2430
2431 default:
2432 return 0;
2433 }
2434
2435 return 0;
2436 }
2437
uvc_mapping_set_xctrl_compound(struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_ext_control * xctrl)2438 static int uvc_mapping_set_xctrl_compound(struct uvc_control *ctrl,
2439 struct uvc_control_mapping *mapping,
2440 struct v4l2_ext_control *xctrl)
2441 {
2442 u8 *data __free(kfree) = NULL;
2443 size_t size = uvc_mapping_v4l2_size(mapping);
2444
2445 if (xctrl->size != size)
2446 return -EINVAL;
2447
2448 /*
2449 * v4l2_ext_control does not have enough room to fit a compound control.
2450 * Instead, the value is in the user memory at xctrl->ptr. The v4l2
2451 * ioctl helper does not copy it for us.
2452 */
2453 data = memdup_user(xctrl->ptr, size);
2454 if (IS_ERR(data))
2455 return PTR_ERR(data);
2456
2457 return mapping->set(mapping, size, data,
2458 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2459 }
2460
uvc_mapping_set_xctrl(struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_ext_control * xctrl)2461 static int uvc_mapping_set_xctrl(struct uvc_control *ctrl,
2462 struct uvc_control_mapping *mapping,
2463 struct v4l2_ext_control *xctrl)
2464 {
2465 if (uvc_ctrl_mapping_is_compound(mapping))
2466 return uvc_mapping_set_xctrl_compound(ctrl, mapping, xctrl);
2467
2468 uvc_mapping_set_s32(mapping, xctrl->value,
2469 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2470 return 0;
2471 }
2472
uvc_ctrl_set(struct uvc_fh * handle,struct v4l2_ext_control * xctrl)2473 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)
2474 {
2475 struct uvc_video_chain *chain = handle->chain;
2476 struct uvc_control_mapping *mapping;
2477 struct uvc_control *ctrl;
2478 int ret;
2479
2480 lockdep_assert_held(&chain->ctrl_mutex);
2481
2482 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
2483 return -EACCES;
2484
2485 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
2486 if (!ctrl)
2487 return -EINVAL;
2488 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
2489 return -EACCES;
2490
2491 ret = uvc_ctrl_clamp(chain, ctrl, mapping, &xctrl->value);
2492 if (ret)
2493 return ret;
2494 /*
2495 * If the mapping doesn't span the whole UVC control, the current value
2496 * needs to be loaded from the device to perform the read-modify-write
2497 * operation.
2498 */
2499 if ((ctrl->info.size * 8) != mapping->size) {
2500 ret = __uvc_ctrl_load_cur(chain, ctrl);
2501 if (ret < 0)
2502 return ret;
2503 }
2504
2505 /* Backup the current value in case we need to rollback later. */
2506 if (!ctrl->dirty) {
2507 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2508 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2509 ctrl->info.size);
2510 }
2511
2512 ret = uvc_mapping_set_xctrl(ctrl, mapping, xctrl);
2513 if (ret)
2514 return ret;
2515
2516 ctrl->dirty = 1;
2517 ctrl->modified = 1;
2518 return 0;
2519 }
2520
2521 /* --------------------------------------------------------------------------
2522 * Dynamic controls
2523 */
2524
2525 /*
2526 * Retrieve flags for a given control
2527 */
uvc_ctrl_get_flags(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2528 static int uvc_ctrl_get_flags(struct uvc_device *dev,
2529 const struct uvc_control *ctrl,
2530 struct uvc_control_info *info)
2531 {
2532 u8 *data;
2533 int ret;
2534
2535 data = kmalloc(1, GFP_KERNEL);
2536 if (data == NULL)
2537 return -ENOMEM;
2538
2539 if (ctrl->entity->get_info)
2540 ret = ctrl->entity->get_info(dev, ctrl->entity,
2541 ctrl->info.selector, data);
2542 else
2543 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
2544 dev->intfnum, info->selector, data, 1);
2545
2546 if (!ret) {
2547 info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
2548 UVC_CTRL_FLAG_SET_CUR |
2549 UVC_CTRL_FLAG_AUTO_UPDATE |
2550 UVC_CTRL_FLAG_ASYNCHRONOUS);
2551
2552 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
2553 UVC_CTRL_FLAG_GET_CUR : 0)
2554 | (data[0] & UVC_CONTROL_CAP_SET ?
2555 UVC_CTRL_FLAG_SET_CUR : 0)
2556 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
2557 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
2558 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
2559 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
2560 }
2561
2562 kfree(data);
2563 return ret;
2564 }
2565
uvc_ctrl_fixup_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2566 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
2567 const struct uvc_control *ctrl, struct uvc_control_info *info)
2568 {
2569 struct uvc_ctrl_fixup {
2570 struct usb_device_id id;
2571 u8 entity;
2572 u8 selector;
2573 u8 flags;
2574 };
2575
2576 static const struct uvc_ctrl_fixup fixups[] = {
2577 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2578 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2579 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2580 UVC_CTRL_FLAG_AUTO_UPDATE },
2581 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2582 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2583 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2584 UVC_CTRL_FLAG_AUTO_UPDATE },
2585 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2586 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2587 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2588 UVC_CTRL_FLAG_AUTO_UPDATE },
2589 };
2590
2591 unsigned int i;
2592
2593 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
2594 if (!usb_match_one_id(dev->intf, &fixups[i].id))
2595 continue;
2596
2597 if (fixups[i].entity == ctrl->entity->id &&
2598 fixups[i].selector == info->selector) {
2599 info->flags = fixups[i].flags;
2600 return;
2601 }
2602 }
2603 }
2604
2605 /*
2606 * Query control information (size and flags) for XU controls.
2607 */
uvc_ctrl_fill_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2608 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
2609 const struct uvc_control *ctrl, struct uvc_control_info *info)
2610 {
2611 u8 *data;
2612 int ret;
2613
2614 data = kmalloc(2, GFP_KERNEL);
2615 if (data == NULL)
2616 return -ENOMEM;
2617
2618 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
2619 info->index = ctrl->index;
2620 info->selector = ctrl->index + 1;
2621
2622 /* Query and verify the control length (GET_LEN) */
2623 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
2624 info->selector, data, 2);
2625 if (ret < 0) {
2626 uvc_dbg(dev, CONTROL,
2627 "GET_LEN failed on control %pUl/%u (%d)\n",
2628 info->entity, info->selector, ret);
2629 goto done;
2630 }
2631
2632 info->size = le16_to_cpup((__le16 *)data);
2633
2634 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
2635 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
2636
2637 ret = uvc_ctrl_get_flags(dev, ctrl, info);
2638 if (ret < 0) {
2639 uvc_dbg(dev, CONTROL,
2640 "Failed to get flags for control %pUl/%u (%d)\n",
2641 info->entity, info->selector, ret);
2642 goto done;
2643 }
2644
2645 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
2646
2647 uvc_dbg(dev, CONTROL,
2648 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2649 info->entity, info->selector, info->size,
2650 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
2651 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
2652 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
2653
2654 done:
2655 kfree(data);
2656 return ret;
2657 }
2658
2659 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2660 const struct uvc_control_info *info);
2661
uvc_ctrl_init_xu_ctrl(struct uvc_device * dev,struct uvc_control * ctrl)2662 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
2663 struct uvc_control *ctrl)
2664 {
2665 struct uvc_control_info info;
2666 int ret;
2667
2668 if (ctrl->initialized)
2669 return 0;
2670
2671 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2672 if (ret < 0)
2673 return ret;
2674
2675 ret = uvc_ctrl_add_info(dev, ctrl, &info);
2676 if (ret < 0)
2677 uvc_dbg(dev, CONTROL,
2678 "Failed to initialize control %pUl/%u on device %s entity %u\n",
2679 info.entity, info.selector, dev->udev->devpath,
2680 ctrl->entity->id);
2681
2682 return ret;
2683 }
2684
uvc_xu_ctrl_query(struct uvc_video_chain * chain,struct uvc_xu_control_query * xqry)2685 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2686 struct uvc_xu_control_query *xqry)
2687 {
2688 struct uvc_entity *entity, *iter;
2689 struct uvc_control *ctrl;
2690 unsigned int i;
2691 bool found;
2692 u32 reqflags;
2693 u16 size;
2694 u8 *data = NULL;
2695 int ret;
2696
2697 /* Find the extension unit. */
2698 entity = NULL;
2699 list_for_each_entry(iter, &chain->entities, chain) {
2700 if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
2701 iter->id == xqry->unit) {
2702 entity = iter;
2703 break;
2704 }
2705 }
2706
2707 if (!entity) {
2708 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2709 xqry->unit);
2710 return -ENOENT;
2711 }
2712
2713 /* Find the control and perform delayed initialization if needed. */
2714 found = false;
2715 for (i = 0; i < entity->ncontrols; ++i) {
2716 ctrl = &entity->controls[i];
2717 if (ctrl->index == xqry->selector - 1) {
2718 found = true;
2719 break;
2720 }
2721 }
2722
2723 if (!found) {
2724 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2725 entity->guid, xqry->selector);
2726 return -ENOENT;
2727 }
2728
2729 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2730 return -ERESTARTSYS;
2731
2732 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2733 if (ret < 0) {
2734 ret = -ENOENT;
2735 goto done;
2736 }
2737
2738 /* Validate the required buffer size and flags for the request */
2739 reqflags = 0;
2740 size = ctrl->info.size;
2741
2742 switch (xqry->query) {
2743 case UVC_GET_CUR:
2744 reqflags = UVC_CTRL_FLAG_GET_CUR;
2745 break;
2746 case UVC_GET_MIN:
2747 reqflags = UVC_CTRL_FLAG_GET_MIN;
2748 break;
2749 case UVC_GET_MAX:
2750 reqflags = UVC_CTRL_FLAG_GET_MAX;
2751 break;
2752 case UVC_GET_DEF:
2753 reqflags = UVC_CTRL_FLAG_GET_DEF;
2754 break;
2755 case UVC_GET_RES:
2756 reqflags = UVC_CTRL_FLAG_GET_RES;
2757 break;
2758 case UVC_SET_CUR:
2759 reqflags = UVC_CTRL_FLAG_SET_CUR;
2760 break;
2761 case UVC_GET_LEN:
2762 size = 2;
2763 break;
2764 case UVC_GET_INFO:
2765 size = 1;
2766 break;
2767 default:
2768 ret = -EINVAL;
2769 goto done;
2770 }
2771
2772 if (size != xqry->size) {
2773 ret = -ENOBUFS;
2774 goto done;
2775 }
2776
2777 if (reqflags && !(ctrl->info.flags & reqflags)) {
2778 ret = -EBADRQC;
2779 goto done;
2780 }
2781
2782 data = kmalloc(size, GFP_KERNEL);
2783 if (data == NULL) {
2784 ret = -ENOMEM;
2785 goto done;
2786 }
2787
2788 if (xqry->query == UVC_SET_CUR &&
2789 copy_from_user(data, xqry->data, size)) {
2790 ret = -EFAULT;
2791 goto done;
2792 }
2793
2794 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2795 chain->dev->intfnum, xqry->selector, data, size);
2796 if (ret < 0)
2797 goto done;
2798
2799 if (xqry->query != UVC_SET_CUR &&
2800 copy_to_user(xqry->data, data, size))
2801 ret = -EFAULT;
2802 done:
2803 kfree(data);
2804 mutex_unlock(&chain->ctrl_mutex);
2805 return ret;
2806 }
2807
2808 /* --------------------------------------------------------------------------
2809 * Suspend/resume
2810 */
2811
2812 /*
2813 * Restore control values after resume, skipping controls that haven't been
2814 * changed.
2815 *
2816 * TODO
2817 * - Don't restore modified controls that are back to their default value.
2818 * - Handle restore order (Auto-Exposure Mode should be restored before
2819 * Exposure Time).
2820 */
uvc_ctrl_restore_values(struct uvc_device * dev)2821 int uvc_ctrl_restore_values(struct uvc_device *dev)
2822 {
2823 struct uvc_control *ctrl;
2824 struct uvc_entity *entity;
2825 unsigned int i;
2826 int ret;
2827
2828 /* Walk the entities list and restore controls when possible. */
2829 list_for_each_entry(entity, &dev->entities, list) {
2830
2831 for (i = 0; i < entity->ncontrols; ++i) {
2832 ctrl = &entity->controls[i];
2833
2834 if (!ctrl->initialized || !ctrl->modified ||
2835 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2836 continue;
2837 dev_dbg(&dev->udev->dev,
2838 "restoring control %pUl/%u/%u\n",
2839 ctrl->info.entity, ctrl->info.index,
2840 ctrl->info.selector);
2841 ctrl->dirty = 1;
2842 }
2843
2844 ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
2845 if (ret < 0)
2846 return ret;
2847 }
2848
2849 return 0;
2850 }
2851
2852 /* --------------------------------------------------------------------------
2853 * Control and mapping handling
2854 */
2855
2856 /*
2857 * Add control information to a given control.
2858 */
uvc_ctrl_add_info(struct uvc_device * dev,struct uvc_control * ctrl,const struct uvc_control_info * info)2859 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2860 const struct uvc_control_info *info)
2861 {
2862 ctrl->info = *info;
2863 INIT_LIST_HEAD(&ctrl->info.mappings);
2864
2865 /* Allocate an array to save control values (cur, def, max, etc.) */
2866 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2867 GFP_KERNEL);
2868 if (!ctrl->uvc_data)
2869 return -ENOMEM;
2870
2871 ctrl->initialized = 1;
2872
2873 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2874 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2875 ctrl->entity->id);
2876
2877 return 0;
2878 }
2879
2880 /*
2881 * Add a control mapping to a given control.
2882 */
__uvc_ctrl_add_mapping(struct uvc_video_chain * chain,struct uvc_control * ctrl,const struct uvc_control_mapping * mapping)2883 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2884 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2885 {
2886 struct uvc_control_mapping *map;
2887 unsigned int size;
2888 unsigned int i;
2889 int ret;
2890
2891 /*
2892 * Most mappings come from static kernel data, and need to be duplicated.
2893 * Mappings that come from userspace will be unnecessarily duplicated,
2894 * this could be optimized.
2895 */
2896 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2897 if (!map)
2898 return -ENOMEM;
2899
2900 map->name = NULL;
2901 map->menu_names = NULL;
2902 map->menu_mapping = NULL;
2903
2904 /* For UVCIOC_CTRL_MAP custom control */
2905 if (mapping->name) {
2906 map->name = kstrdup(mapping->name, GFP_KERNEL);
2907 if (!map->name)
2908 goto err_nomem;
2909 }
2910
2911 INIT_LIST_HEAD(&map->ev_subs);
2912
2913 if (mapping->menu_mapping && mapping->menu_mask) {
2914 size = sizeof(mapping->menu_mapping[0])
2915 * fls(mapping->menu_mask);
2916 map->menu_mapping = kmemdup(mapping->menu_mapping, size,
2917 GFP_KERNEL);
2918 if (!map->menu_mapping)
2919 goto err_nomem;
2920 }
2921 if (mapping->menu_names && mapping->menu_mask) {
2922 size = sizeof(mapping->menu_names[0])
2923 * fls(mapping->menu_mask);
2924 map->menu_names = kmemdup(mapping->menu_names, size,
2925 GFP_KERNEL);
2926 if (!map->menu_names)
2927 goto err_nomem;
2928 }
2929
2930 if (uvc_ctrl_mapping_is_compound(map))
2931 if (WARN_ON(!map->set || !map->get)) {
2932 ret = -EIO;
2933 goto free_mem;
2934 }
2935
2936 if (map->get == NULL)
2937 map->get = uvc_get_le_value;
2938 if (map->set == NULL)
2939 map->set = uvc_set_le_value;
2940
2941 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
2942 if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
2943 V4L2_CTRL_ID2WHICH(map->id)) {
2944 chain->ctrl_class_bitmap |= BIT(i);
2945 break;
2946 }
2947 }
2948
2949 list_add_tail(&map->list, &ctrl->info.mappings);
2950 uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2951 uvc_map_get_name(map), ctrl->info.entity,
2952 ctrl->info.selector);
2953
2954 return 0;
2955
2956 err_nomem:
2957 ret = -ENOMEM;
2958 free_mem:
2959 kfree(map->menu_names);
2960 kfree(map->menu_mapping);
2961 kfree(map->name);
2962 kfree(map);
2963 return ret;
2964 }
2965
uvc_ctrl_add_mapping(struct uvc_video_chain * chain,const struct uvc_control_mapping * mapping)2966 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2967 const struct uvc_control_mapping *mapping)
2968 {
2969 struct uvc_device *dev = chain->dev;
2970 struct uvc_control_mapping *map;
2971 struct uvc_entity *entity;
2972 struct uvc_control *ctrl;
2973 int found = 0;
2974 int ret;
2975
2976 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2977 uvc_dbg(dev, CONTROL,
2978 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2979 uvc_map_get_name(mapping), mapping->id);
2980 return -EINVAL;
2981 }
2982
2983 /* Search for the matching (GUID/CS) control on the current chain */
2984 list_for_each_entry(entity, &chain->entities, chain) {
2985 unsigned int i;
2986
2987 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2988 !uvc_entity_match_guid(entity, mapping->entity))
2989 continue;
2990
2991 for (i = 0; i < entity->ncontrols; ++i) {
2992 ctrl = &entity->controls[i];
2993 if (ctrl->index == mapping->selector - 1) {
2994 found = 1;
2995 break;
2996 }
2997 }
2998
2999 if (found)
3000 break;
3001 }
3002 if (!found)
3003 return -ENOENT;
3004
3005 if (mutex_lock_interruptible(&chain->ctrl_mutex))
3006 return -ERESTARTSYS;
3007
3008 /* Perform delayed initialization of XU controls */
3009 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
3010 if (ret < 0) {
3011 ret = -ENOENT;
3012 goto done;
3013 }
3014
3015 /* Validate the user-provided bit-size and offset */
3016 if (mapping->size > 32 ||
3017 mapping->offset + mapping->size > ctrl->info.size * 8) {
3018 ret = -EINVAL;
3019 goto done;
3020 }
3021
3022 list_for_each_entry(map, &ctrl->info.mappings, list) {
3023 if (mapping->id == map->id) {
3024 uvc_dbg(dev, CONTROL,
3025 "Can't add mapping '%s', control id 0x%08x already exists\n",
3026 uvc_map_get_name(mapping), mapping->id);
3027 ret = -EEXIST;
3028 goto done;
3029 }
3030 }
3031
3032 /* Prevent excess memory consumption */
3033 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
3034 atomic_dec(&dev->nmappings);
3035 uvc_dbg(dev, CONTROL,
3036 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
3037 uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
3038 ret = -ENOMEM;
3039 goto done;
3040 }
3041
3042 ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
3043 if (ret < 0)
3044 atomic_dec(&dev->nmappings);
3045
3046 done:
3047 mutex_unlock(&chain->ctrl_mutex);
3048 return ret;
3049 }
3050
3051 /*
3052 * Prune an entity of its bogus controls using a blacklist. Bogus controls
3053 * are currently the ones that crash the camera or unconditionally return an
3054 * error when queried.
3055 */
uvc_ctrl_prune_entity(struct uvc_device * dev,struct uvc_entity * entity)3056 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
3057 struct uvc_entity *entity)
3058 {
3059 struct uvc_ctrl_blacklist {
3060 struct usb_device_id id;
3061 u8 index;
3062 };
3063
3064 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
3065 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
3066 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
3067 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
3068 };
3069 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
3070 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
3071 };
3072
3073 const struct uvc_ctrl_blacklist *blacklist;
3074 unsigned int size;
3075 unsigned int count;
3076 unsigned int i;
3077 u8 *controls;
3078
3079 switch (UVC_ENTITY_TYPE(entity)) {
3080 case UVC_VC_PROCESSING_UNIT:
3081 blacklist = processing_blacklist;
3082 count = ARRAY_SIZE(processing_blacklist);
3083 controls = entity->processing.bmControls;
3084 size = entity->processing.bControlSize;
3085 break;
3086
3087 case UVC_ITT_CAMERA:
3088 blacklist = camera_blacklist;
3089 count = ARRAY_SIZE(camera_blacklist);
3090 controls = entity->camera.bmControls;
3091 size = entity->camera.bControlSize;
3092 break;
3093
3094 default:
3095 return;
3096 }
3097
3098 for (i = 0; i < count; ++i) {
3099 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
3100 continue;
3101
3102 if (blacklist[i].index >= 8 * size ||
3103 !uvc_test_bit(controls, blacklist[i].index))
3104 continue;
3105
3106 uvc_dbg(dev, CONTROL,
3107 "%u/%u control is black listed, removing it\n",
3108 entity->id, blacklist[i].index);
3109
3110 uvc_clear_bit(controls, blacklist[i].index);
3111 }
3112 }
3113
3114 /*
3115 * Add control information and hardcoded stock control mappings to the given
3116 * device.
3117 */
uvc_ctrl_init_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl)3118 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
3119 struct uvc_control *ctrl)
3120 {
3121 unsigned int i;
3122
3123 /*
3124 * XU controls initialization requires querying the device for control
3125 * information. As some buggy UVC devices will crash when queried
3126 * repeatedly in a tight loop, delay XU controls initialization until
3127 * first use.
3128 */
3129 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
3130 return;
3131
3132 for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
3133 const struct uvc_control_info *info = &uvc_ctrls[i];
3134
3135 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
3136 ctrl->index == info->index) {
3137 uvc_ctrl_add_info(chain->dev, ctrl, info);
3138 /*
3139 * Retrieve control flags from the device. Ignore errors
3140 * and work with default flag values from the uvc_ctrl
3141 * array when the device doesn't properly implement
3142 * GET_INFO on standard controls.
3143 */
3144 uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
3145 break;
3146 }
3147 }
3148
3149 if (!ctrl->initialized)
3150 return;
3151
3152 /* Process common mappings. */
3153 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
3154 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
3155
3156 if (!uvc_entity_match_guid(ctrl->entity, mapping->entity) ||
3157 ctrl->info.selector != mapping->selector)
3158 continue;
3159
3160 /* Let the device provide a custom mapping. */
3161 if (mapping->filter_mapping) {
3162 mapping = mapping->filter_mapping(chain, ctrl);
3163 if (!mapping)
3164 continue;
3165 }
3166
3167 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
3168 }
3169 }
3170
3171 /*
3172 * Initialize device controls.
3173 */
uvc_ctrl_init_chain(struct uvc_video_chain * chain)3174 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
3175 {
3176 struct uvc_entity *entity;
3177 unsigned int i;
3178
3179 /* Walk the entities list and instantiate controls */
3180 list_for_each_entry(entity, &chain->entities, chain) {
3181 struct uvc_control *ctrl;
3182 unsigned int bControlSize = 0, ncontrols;
3183 u8 *bmControls = NULL;
3184
3185 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
3186 bmControls = entity->extension.bmControls;
3187 bControlSize = entity->extension.bControlSize;
3188 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
3189 bmControls = entity->processing.bmControls;
3190 bControlSize = entity->processing.bControlSize;
3191 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
3192 bmControls = entity->camera.bmControls;
3193 bControlSize = entity->camera.bControlSize;
3194 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
3195 bmControls = entity->gpio.bmControls;
3196 bControlSize = entity->gpio.bControlSize;
3197 }
3198
3199 /* Remove bogus/blacklisted controls */
3200 uvc_ctrl_prune_entity(chain->dev, entity);
3201
3202 /* Count supported controls and allocate the controls array */
3203 ncontrols = memweight(bmControls, bControlSize);
3204 if (ncontrols == 0)
3205 continue;
3206
3207 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
3208 GFP_KERNEL);
3209 if (entity->controls == NULL)
3210 return -ENOMEM;
3211 entity->ncontrols = ncontrols;
3212
3213 /* Initialize all supported controls */
3214 ctrl = entity->controls;
3215 for (i = 0; i < bControlSize * 8; ++i) {
3216 if (uvc_test_bit(bmControls, i) == 0)
3217 continue;
3218
3219 ctrl->entity = entity;
3220 ctrl->index = i;
3221
3222 uvc_ctrl_init_ctrl(chain, ctrl);
3223 ctrl++;
3224 }
3225 }
3226
3227 return 0;
3228 }
3229
uvc_ctrl_init_device(struct uvc_device * dev)3230 int uvc_ctrl_init_device(struct uvc_device *dev)
3231 {
3232 struct uvc_video_chain *chain;
3233 int ret;
3234
3235 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
3236
3237 list_for_each_entry(chain, &dev->chains, list) {
3238 ret = uvc_ctrl_init_chain(chain);
3239 if (ret)
3240 return ret;
3241 }
3242
3243 return 0;
3244 }
3245
uvc_ctrl_cleanup_fh(struct uvc_fh * handle)3246 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
3247 {
3248 struct uvc_entity *entity;
3249
3250 guard(mutex)(&handle->chain->ctrl_mutex);
3251
3252 if (!handle->pending_async_ctrls)
3253 return;
3254
3255 list_for_each_entry(entity, &handle->chain->dev->entities, list) {
3256 for (unsigned int i = 0; i < entity->ncontrols; ++i) {
3257 if (entity->controls[i].handle != handle)
3258 continue;
3259 uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
3260 }
3261 }
3262
3263 WARN_ON(handle->pending_async_ctrls);
3264 }
3265
3266 /*
3267 * Cleanup device controls.
3268 */
uvc_ctrl_cleanup_mappings(struct uvc_device * dev,struct uvc_control * ctrl)3269 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
3270 struct uvc_control *ctrl)
3271 {
3272 struct uvc_control_mapping *mapping, *nm;
3273
3274 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
3275 list_del(&mapping->list);
3276 kfree(mapping->menu_names);
3277 kfree(mapping->menu_mapping);
3278 kfree(mapping->name);
3279 kfree(mapping);
3280 }
3281 }
3282
uvc_ctrl_cleanup_device(struct uvc_device * dev)3283 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
3284 {
3285 struct uvc_entity *entity;
3286 unsigned int i;
3287
3288 /* Can be uninitialized if we are aborting on probe error. */
3289 if (dev->async_ctrl.work.func)
3290 cancel_work_sync(&dev->async_ctrl.work);
3291
3292 /* Free controls and control mappings for all entities. */
3293 list_for_each_entry(entity, &dev->entities, list) {
3294 for (i = 0; i < entity->ncontrols; ++i) {
3295 struct uvc_control *ctrl = &entity->controls[i];
3296
3297 if (!ctrl->initialized)
3298 continue;
3299
3300 uvc_ctrl_cleanup_mappings(dev, ctrl);
3301 kfree(ctrl->uvc_data);
3302 }
3303
3304 kfree(entity->controls);
3305 }
3306 }
3307