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 /* Flush the control cache, the data might have changed. */
1859 ctrl->loaded = 0;
1860
1861 handle = ctrl->handle;
1862 if (handle)
1863 uvc_ctrl_set_handle(handle, ctrl, NULL);
1864
1865 list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1866 s32 value;
1867
1868 if (uvc_ctrl_mapping_is_compound(mapping))
1869 value = 0;
1870 else
1871 value = uvc_mapping_get_s32(mapping, UVC_GET_CUR, data);
1872
1873 /*
1874 * handle may be NULL here if the device sends auto-update
1875 * events without a prior related control set from userspace.
1876 */
1877 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1878 if (!mapping->slave_ids[i])
1879 break;
1880
1881 uvc_ctrl_send_slave_event(chain, handle, ctrl,
1882 mapping->slave_ids[i]);
1883 }
1884
1885 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1886 V4L2_EVENT_CTRL_CH_VALUE);
1887 }
1888
1889 mutex_unlock(&chain->ctrl_mutex);
1890 }
1891
uvc_ctrl_status_event_work(struct work_struct * work)1892 static void uvc_ctrl_status_event_work(struct work_struct *work)
1893 {
1894 struct uvc_device *dev = container_of(work, struct uvc_device,
1895 async_ctrl.work);
1896 struct uvc_ctrl_work *w = &dev->async_ctrl;
1897 int ret;
1898
1899 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1900
1901 /* The barrier is needed to synchronize with uvc_status_stop(). */
1902 if (smp_load_acquire(&dev->flush_status))
1903 return;
1904
1905 /* Resubmit the URB. */
1906 w->urb->interval = dev->int_ep->desc.bInterval;
1907 ret = usb_submit_urb(w->urb, GFP_KERNEL);
1908 if (ret < 0)
1909 dev_err(&dev->udev->dev,
1910 "Failed to resubmit status URB (%d).\n", ret);
1911 }
1912
uvc_ctrl_status_event_async(struct urb * urb,struct uvc_video_chain * chain,struct uvc_control * ctrl,const u8 * data)1913 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
1914 struct uvc_control *ctrl, const u8 *data)
1915 {
1916 struct uvc_device *dev = chain->dev;
1917 struct uvc_ctrl_work *w = &dev->async_ctrl;
1918
1919 if (list_empty(&ctrl->info.mappings))
1920 return false;
1921
1922 w->data = data;
1923 w->urb = urb;
1924 w->chain = chain;
1925 w->ctrl = ctrl;
1926
1927 schedule_work(&w->work);
1928
1929 return true;
1930 }
1931
uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control * xctrls,unsigned int xctrls_count,u32 id)1932 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1933 unsigned int xctrls_count, u32 id)
1934 {
1935 unsigned int i;
1936
1937 for (i = 0; i < xctrls_count; ++i) {
1938 if (xctrls[i].id == id)
1939 return true;
1940 }
1941
1942 return false;
1943 }
1944
uvc_ctrl_send_events(struct uvc_fh * handle,const struct v4l2_ext_control * xctrls,unsigned int xctrls_count)1945 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1946 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1947 {
1948 struct uvc_control_mapping *mapping;
1949 struct uvc_control *ctrl;
1950 unsigned int i;
1951 unsigned int j;
1952
1953 for (i = 0; i < xctrls_count; ++i) {
1954 u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1955 s32 value;
1956
1957 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1958 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1959 /* Notification will be sent from an Interrupt event. */
1960 continue;
1961
1962 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1963 u32 slave_id = mapping->slave_ids[j];
1964
1965 if (!slave_id)
1966 break;
1967
1968 /*
1969 * We can skip sending an event for the slave if the
1970 * slave is being modified in the same transaction.
1971 */
1972 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1973 slave_id))
1974 continue;
1975
1976 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1977 slave_id);
1978 }
1979
1980 if (uvc_ctrl_mapping_is_compound(mapping))
1981 value = 0;
1982 else
1983 value = xctrls[i].value;
1984 /*
1985 * If the master is being modified in the same transaction
1986 * flags may change too.
1987 */
1988 if (mapping->master_id &&
1989 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1990 mapping->master_id))
1991 changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1992
1993 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1994 value, changes);
1995 }
1996 }
1997
uvc_ctrl_add_event(struct v4l2_subscribed_event * sev,unsigned elems)1998 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1999 {
2000 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
2001 struct uvc_control_mapping *mapping;
2002 struct uvc_control *ctrl;
2003 int ret;
2004
2005 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
2006 if (ret < 0)
2007 return -ERESTARTSYS;
2008
2009 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
2010 ret = 0;
2011 goto done;
2012 }
2013
2014 ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
2015 if (ctrl == NULL) {
2016 ret = -EINVAL;
2017 goto done;
2018 }
2019
2020 list_add_tail(&sev->node, &mapping->ev_subs);
2021 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
2022 struct v4l2_event ev;
2023 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
2024 s32 val = 0;
2025
2026 if (uvc_ctrl_mapping_is_compound(mapping) ||
2027 __uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
2028 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2029
2030 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
2031 changes);
2032 /*
2033 * Mark the queue as active, allowing this initial event to be
2034 * accepted.
2035 */
2036 sev->elems = elems;
2037 v4l2_event_queue_fh(sev->fh, &ev);
2038 }
2039
2040 done:
2041 mutex_unlock(&handle->chain->ctrl_mutex);
2042 return ret;
2043 }
2044
uvc_ctrl_del_event(struct v4l2_subscribed_event * sev)2045 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
2046 {
2047 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
2048
2049 mutex_lock(&handle->chain->ctrl_mutex);
2050 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
2051 goto done;
2052 list_del(&sev->node);
2053 done:
2054 mutex_unlock(&handle->chain->ctrl_mutex);
2055 }
2056
2057 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
2058 .add = uvc_ctrl_add_event,
2059 .del = uvc_ctrl_del_event,
2060 .replace = v4l2_ctrl_replace,
2061 .merge = v4l2_ctrl_merge,
2062 };
2063
2064 /* --------------------------------------------------------------------------
2065 * Control transactions
2066 *
2067 * To make extended set operations as atomic as the hardware allows, controls
2068 * are handled using begin/commit/rollback operations.
2069 *
2070 * At the beginning of a set request, uvc_ctrl_begin should be called to
2071 * initialize the request. This function acquires the control lock.
2072 *
2073 * When setting a control, the new value is stored in the control data field
2074 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
2075 * later processing. If the UVC and V4L2 control sizes differ, the current
2076 * value is loaded from the hardware before storing the new value in the data
2077 * field.
2078 *
2079 * After processing all controls in the transaction, uvc_ctrl_commit or
2080 * uvc_ctrl_rollback must be called to apply the pending changes to the
2081 * hardware or revert them. When applying changes, all controls marked as
2082 * dirty will be modified in the UVC device, and the dirty flag will be
2083 * cleared. When reverting controls, the control data field
2084 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
2085 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
2086 * control lock.
2087 */
uvc_ctrl_begin(struct uvc_video_chain * chain)2088 int uvc_ctrl_begin(struct uvc_video_chain *chain)
2089 {
2090 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
2091 }
2092
uvc_ctrl_commit_entity(struct uvc_device * dev,struct uvc_fh * handle,struct uvc_entity * entity,int rollback,struct uvc_control ** err_ctrl)2093 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
2094 struct uvc_fh *handle,
2095 struct uvc_entity *entity,
2096 int rollback,
2097 struct uvc_control **err_ctrl)
2098 {
2099 struct uvc_control *ctrl;
2100 unsigned int i;
2101 int ret;
2102
2103 if (entity == NULL)
2104 return 0;
2105
2106 for (i = 0; i < entity->ncontrols; ++i) {
2107 ctrl = &entity->controls[i];
2108 if (!ctrl->initialized)
2109 continue;
2110
2111 /*
2112 * Reset the loaded flag for auto-update controls that were
2113 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
2114 * uvc_ctrl_get from using the cached value, and for write-only
2115 * controls to prevent uvc_ctrl_set from setting bits not
2116 * explicitly set by the user.
2117 */
2118 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
2119 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
2120 ctrl->loaded = 0;
2121
2122 if (!ctrl->dirty)
2123 continue;
2124
2125 if (!rollback)
2126 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
2127 dev->intfnum, ctrl->info.selector,
2128 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2129 ctrl->info.size);
2130 else
2131 ret = 0;
2132
2133 if (rollback || ret < 0)
2134 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2135 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2136 ctrl->info.size);
2137
2138 ctrl->dirty = 0;
2139
2140 if (ret < 0) {
2141 if (err_ctrl)
2142 *err_ctrl = ctrl;
2143 return ret;
2144 }
2145
2146 if (!rollback && handle &&
2147 ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
2148 uvc_ctrl_set_handle(handle, ctrl, handle);
2149 }
2150
2151 return 0;
2152 }
2153
uvc_ctrl_find_ctrl_idx(struct uvc_entity * entity,struct v4l2_ext_controls * ctrls,struct uvc_control * uvc_control)2154 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
2155 struct v4l2_ext_controls *ctrls,
2156 struct uvc_control *uvc_control)
2157 {
2158 struct uvc_control_mapping *mapping = NULL;
2159 struct uvc_control *ctrl_found = NULL;
2160 unsigned int i;
2161
2162 if (!entity)
2163 return ctrls->count;
2164
2165 for (i = 0; i < ctrls->count; i++) {
2166 __uvc_find_control(entity, ctrls->controls[i].id, &mapping,
2167 &ctrl_found, 0, 0);
2168 if (uvc_control == ctrl_found)
2169 return i;
2170 }
2171
2172 return ctrls->count;
2173 }
2174
__uvc_ctrl_commit(struct uvc_fh * handle,int rollback,struct v4l2_ext_controls * ctrls)2175 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
2176 struct v4l2_ext_controls *ctrls)
2177 {
2178 struct uvc_video_chain *chain = handle->chain;
2179 struct uvc_control *err_ctrl;
2180 struct uvc_entity *entity;
2181 int ret = 0;
2182
2183 /* Find the control. */
2184 list_for_each_entry(entity, &chain->entities, chain) {
2185 ret = uvc_ctrl_commit_entity(chain->dev, handle, entity,
2186 rollback, &err_ctrl);
2187 if (ret < 0) {
2188 if (ctrls)
2189 ctrls->error_idx =
2190 uvc_ctrl_find_ctrl_idx(entity, ctrls,
2191 err_ctrl);
2192 goto done;
2193 }
2194 }
2195
2196 if (!rollback)
2197 uvc_ctrl_send_events(handle, ctrls->controls, ctrls->count);
2198 done:
2199 mutex_unlock(&chain->ctrl_mutex);
2200 return ret;
2201 }
2202
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)2203 static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain,
2204 struct uvc_control *ctrl,
2205 struct uvc_control_mapping *mapping,
2206 u32 which,
2207 struct v4l2_ext_control *xctrl)
2208 {
2209 u8 *data __free(kfree) = NULL;
2210 size_t size;
2211 u8 query;
2212 int ret;
2213 int id;
2214
2215 switch (which) {
2216 case V4L2_CTRL_WHICH_CUR_VAL:
2217 id = UVC_CTRL_DATA_CURRENT;
2218 query = UVC_GET_CUR;
2219 break;
2220 case V4L2_CTRL_WHICH_MIN_VAL:
2221 id = UVC_CTRL_DATA_MIN;
2222 query = UVC_GET_MIN;
2223 break;
2224 case V4L2_CTRL_WHICH_MAX_VAL:
2225 id = UVC_CTRL_DATA_MAX;
2226 query = UVC_GET_MAX;
2227 break;
2228 case V4L2_CTRL_WHICH_DEF_VAL:
2229 id = UVC_CTRL_DATA_DEF;
2230 query = UVC_GET_DEF;
2231 break;
2232 default:
2233 return -EINVAL;
2234 }
2235
2236 size = uvc_mapping_v4l2_size(mapping);
2237 if (xctrl->size < size) {
2238 xctrl->size = size;
2239 return -ENOSPC;
2240 }
2241
2242 data = kmalloc(size, GFP_KERNEL);
2243 if (!data)
2244 return -ENOMEM;
2245
2246 if (which == V4L2_CTRL_WHICH_CUR_VAL)
2247 ret = __uvc_ctrl_load_cur(chain, ctrl);
2248 else
2249 ret = uvc_ctrl_populate_cache(chain, ctrl);
2250
2251 if (ret < 0)
2252 return ret;
2253
2254 ret = mapping->get(mapping, query, uvc_ctrl_data(ctrl, id), size, data);
2255 if (ret < 0)
2256 return ret;
2257
2258 /*
2259 * v4l2_ext_control does not have enough room to fit a compound control.
2260 * Instead, the value is in the user memory at xctrl->ptr. The v4l2
2261 * ioctl helper does not copy it for us.
2262 */
2263 return copy_to_user(xctrl->ptr, data, size) ? -EFAULT : 0;
2264 }
2265
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)2266 static int uvc_mapping_get_xctrl_std(struct uvc_video_chain *chain,
2267 struct uvc_control *ctrl,
2268 struct uvc_control_mapping *mapping,
2269 u32 which, struct v4l2_ext_control *xctrl)
2270 {
2271 struct v4l2_query_ext_ctrl qec;
2272 int ret;
2273
2274 switch (which) {
2275 case V4L2_CTRL_WHICH_CUR_VAL:
2276 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
2277 case V4L2_CTRL_WHICH_DEF_VAL:
2278 case V4L2_CTRL_WHICH_MIN_VAL:
2279 case V4L2_CTRL_WHICH_MAX_VAL:
2280 break;
2281 default:
2282 return -EINVAL;
2283 }
2284
2285 ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, &qec);
2286 if (ret < 0)
2287 return ret;
2288
2289 switch (which) {
2290 case V4L2_CTRL_WHICH_DEF_VAL:
2291 xctrl->value = qec.default_value;
2292 break;
2293 case V4L2_CTRL_WHICH_MIN_VAL:
2294 xctrl->value = qec.minimum;
2295 break;
2296 case V4L2_CTRL_WHICH_MAX_VAL:
2297 xctrl->value = qec.maximum;
2298 break;
2299 }
2300
2301 return 0;
2302 }
2303
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)2304 static int uvc_mapping_get_xctrl(struct uvc_video_chain *chain,
2305 struct uvc_control *ctrl,
2306 struct uvc_control_mapping *mapping,
2307 u32 which, struct v4l2_ext_control *xctrl)
2308 {
2309 if (uvc_ctrl_mapping_is_compound(mapping))
2310 return uvc_mapping_get_xctrl_compound(chain, ctrl, mapping,
2311 which, xctrl);
2312 return uvc_mapping_get_xctrl_std(chain, ctrl, mapping, which, xctrl);
2313 }
2314
uvc_ctrl_get(struct uvc_video_chain * chain,u32 which,struct v4l2_ext_control * xctrl)2315 int uvc_ctrl_get(struct uvc_video_chain *chain, u32 which,
2316 struct v4l2_ext_control *xctrl)
2317 {
2318 struct uvc_control *ctrl;
2319 struct uvc_control_mapping *mapping;
2320
2321 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
2322 return -EACCES;
2323
2324 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
2325 if (!ctrl)
2326 return -EINVAL;
2327
2328 return uvc_mapping_get_xctrl(chain, ctrl, mapping, which, xctrl);
2329 }
2330
uvc_ctrl_clamp(struct uvc_video_chain * chain,struct uvc_control * ctrl,struct uvc_control_mapping * mapping,s32 * value_in_out)2331 static int uvc_ctrl_clamp(struct uvc_video_chain *chain,
2332 struct uvc_control *ctrl,
2333 struct uvc_control_mapping *mapping,
2334 s32 *value_in_out)
2335 {
2336 s32 value = *value_in_out;
2337 u32 step;
2338 s32 min;
2339 s32 max;
2340 int ret;
2341
2342 switch (mapping->v4l2_type) {
2343 case V4L2_CTRL_TYPE_INTEGER:
2344 if (!ctrl->cached) {
2345 ret = uvc_ctrl_populate_cache(chain, ctrl);
2346 if (ret < 0)
2347 return ret;
2348 }
2349
2350 min = uvc_mapping_get_s32(mapping, UVC_GET_MIN,
2351 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
2352 max = uvc_mapping_get_s32(mapping, UVC_GET_MAX,
2353 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
2354 step = uvc_mapping_get_s32(mapping, UVC_GET_RES,
2355 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
2356 if (step == 0)
2357 step = 1;
2358
2359 value = min + DIV_ROUND_CLOSEST((u32)(value - min), step) * step;
2360 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
2361 value = clamp(value, min, max);
2362 else
2363 value = clamp_t(u32, value, min, max);
2364 *value_in_out = value;
2365 return 0;
2366
2367 case V4L2_CTRL_TYPE_BITMASK:
2368 if (!ctrl->cached) {
2369 ret = uvc_ctrl_populate_cache(chain, ctrl);
2370 if (ret < 0)
2371 return ret;
2372 }
2373
2374 value &= uvc_get_ctrl_bitmap(ctrl, mapping);
2375 *value_in_out = value;
2376 return 0;
2377
2378 case V4L2_CTRL_TYPE_BOOLEAN:
2379 *value_in_out = clamp(value, 0, 1);
2380 return 0;
2381
2382 case V4L2_CTRL_TYPE_MENU:
2383 if (value < (ffs(mapping->menu_mask) - 1) ||
2384 value > (fls(mapping->menu_mask) - 1))
2385 return -ERANGE;
2386
2387 if (!test_bit(value, &mapping->menu_mask))
2388 return -EINVAL;
2389
2390 /*
2391 * Valid menu indices are reported by the GET_RES request for
2392 * UVC controls that support it.
2393 */
2394 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
2395 int val = uvc_mapping_get_menu_value(mapping, value);
2396 if (!ctrl->cached) {
2397 ret = uvc_ctrl_populate_cache(chain, ctrl);
2398 if (ret < 0)
2399 return ret;
2400 }
2401
2402 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & val))
2403 return -EINVAL;
2404 }
2405 return 0;
2406
2407 default:
2408 return 0;
2409 }
2410
2411 return 0;
2412 }
2413
uvc_mapping_set_xctrl_compound(struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_ext_control * xctrl)2414 static int uvc_mapping_set_xctrl_compound(struct uvc_control *ctrl,
2415 struct uvc_control_mapping *mapping,
2416 struct v4l2_ext_control *xctrl)
2417 {
2418 u8 *data __free(kfree) = NULL;
2419 size_t size = uvc_mapping_v4l2_size(mapping);
2420
2421 if (xctrl->size != size)
2422 return -EINVAL;
2423
2424 /*
2425 * v4l2_ext_control does not have enough room to fit a compound control.
2426 * Instead, the value is in the user memory at xctrl->ptr. The v4l2
2427 * ioctl helper does not copy it for us.
2428 */
2429 data = memdup_user(xctrl->ptr, size);
2430 if (IS_ERR(data))
2431 return PTR_ERR(data);
2432
2433 return mapping->set(mapping, size, data,
2434 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2435 }
2436
uvc_mapping_set_xctrl(struct uvc_control * ctrl,struct uvc_control_mapping * mapping,struct v4l2_ext_control * xctrl)2437 static int uvc_mapping_set_xctrl(struct uvc_control *ctrl,
2438 struct uvc_control_mapping *mapping,
2439 struct v4l2_ext_control *xctrl)
2440 {
2441 if (uvc_ctrl_mapping_is_compound(mapping))
2442 return uvc_mapping_set_xctrl_compound(ctrl, mapping, xctrl);
2443
2444 uvc_mapping_set_s32(mapping, xctrl->value,
2445 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2446 return 0;
2447 }
2448
uvc_ctrl_set(struct uvc_fh * handle,struct v4l2_ext_control * xctrl)2449 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)
2450 {
2451 struct uvc_video_chain *chain = handle->chain;
2452 struct uvc_control_mapping *mapping;
2453 struct uvc_control *ctrl;
2454 int ret;
2455
2456 lockdep_assert_held(&chain->ctrl_mutex);
2457
2458 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
2459 return -EACCES;
2460
2461 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
2462 if (!ctrl)
2463 return -EINVAL;
2464 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
2465 return -EACCES;
2466
2467 ret = uvc_ctrl_clamp(chain, ctrl, mapping, &xctrl->value);
2468 if (ret)
2469 return ret;
2470 /*
2471 * If the mapping doesn't span the whole UVC control, the current value
2472 * needs to be loaded from the device to perform the read-modify-write
2473 * operation.
2474 */
2475 if ((ctrl->info.size * 8) != mapping->size) {
2476 ret = __uvc_ctrl_load_cur(chain, ctrl);
2477 if (ret < 0)
2478 return ret;
2479 }
2480
2481 /* Backup the current value in case we need to rollback later. */
2482 if (!ctrl->dirty) {
2483 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2484 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2485 ctrl->info.size);
2486 }
2487
2488 ret = uvc_mapping_set_xctrl(ctrl, mapping, xctrl);
2489 if (ret)
2490 return ret;
2491
2492 ctrl->dirty = 1;
2493 ctrl->modified = 1;
2494 return 0;
2495 }
2496
2497 /* --------------------------------------------------------------------------
2498 * Dynamic controls
2499 */
2500
2501 /*
2502 * Retrieve flags for a given control
2503 */
uvc_ctrl_get_flags(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2504 static int uvc_ctrl_get_flags(struct uvc_device *dev,
2505 const struct uvc_control *ctrl,
2506 struct uvc_control_info *info)
2507 {
2508 u8 *data;
2509 int ret;
2510
2511 data = kmalloc(1, GFP_KERNEL);
2512 if (data == NULL)
2513 return -ENOMEM;
2514
2515 if (ctrl->entity->get_info)
2516 ret = ctrl->entity->get_info(dev, ctrl->entity,
2517 ctrl->info.selector, data);
2518 else
2519 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
2520 dev->intfnum, info->selector, data, 1);
2521
2522 if (!ret) {
2523 info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
2524 UVC_CTRL_FLAG_SET_CUR |
2525 UVC_CTRL_FLAG_AUTO_UPDATE |
2526 UVC_CTRL_FLAG_ASYNCHRONOUS);
2527
2528 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
2529 UVC_CTRL_FLAG_GET_CUR : 0)
2530 | (data[0] & UVC_CONTROL_CAP_SET ?
2531 UVC_CTRL_FLAG_SET_CUR : 0)
2532 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
2533 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
2534 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
2535 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
2536 }
2537
2538 kfree(data);
2539 return ret;
2540 }
2541
uvc_ctrl_fixup_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2542 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
2543 const struct uvc_control *ctrl, struct uvc_control_info *info)
2544 {
2545 struct uvc_ctrl_fixup {
2546 struct usb_device_id id;
2547 u8 entity;
2548 u8 selector;
2549 u8 flags;
2550 };
2551
2552 static const struct uvc_ctrl_fixup fixups[] = {
2553 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2554 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2555 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2556 UVC_CTRL_FLAG_AUTO_UPDATE },
2557 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2558 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2559 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2560 UVC_CTRL_FLAG_AUTO_UPDATE },
2561 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2562 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2563 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2564 UVC_CTRL_FLAG_AUTO_UPDATE },
2565 };
2566
2567 unsigned int i;
2568
2569 for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
2570 if (!usb_match_one_id(dev->intf, &fixups[i].id))
2571 continue;
2572
2573 if (fixups[i].entity == ctrl->entity->id &&
2574 fixups[i].selector == info->selector) {
2575 info->flags = fixups[i].flags;
2576 return;
2577 }
2578 }
2579 }
2580
2581 /*
2582 * Query control information (size and flags) for XU controls.
2583 */
uvc_ctrl_fill_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2584 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
2585 const struct uvc_control *ctrl, struct uvc_control_info *info)
2586 {
2587 u8 *data;
2588 int ret;
2589
2590 data = kmalloc(2, GFP_KERNEL);
2591 if (data == NULL)
2592 return -ENOMEM;
2593
2594 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
2595 info->index = ctrl->index;
2596 info->selector = ctrl->index + 1;
2597
2598 /* Query and verify the control length (GET_LEN) */
2599 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
2600 info->selector, data, 2);
2601 if (ret < 0) {
2602 uvc_dbg(dev, CONTROL,
2603 "GET_LEN failed on control %pUl/%u (%d)\n",
2604 info->entity, info->selector, ret);
2605 goto done;
2606 }
2607
2608 info->size = le16_to_cpup((__le16 *)data);
2609
2610 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
2611 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
2612
2613 ret = uvc_ctrl_get_flags(dev, ctrl, info);
2614 if (ret < 0) {
2615 uvc_dbg(dev, CONTROL,
2616 "Failed to get flags for control %pUl/%u (%d)\n",
2617 info->entity, info->selector, ret);
2618 goto done;
2619 }
2620
2621 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
2622
2623 uvc_dbg(dev, CONTROL,
2624 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2625 info->entity, info->selector, info->size,
2626 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
2627 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
2628 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
2629
2630 done:
2631 kfree(data);
2632 return ret;
2633 }
2634
2635 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2636 const struct uvc_control_info *info);
2637
uvc_ctrl_init_xu_ctrl(struct uvc_device * dev,struct uvc_control * ctrl)2638 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
2639 struct uvc_control *ctrl)
2640 {
2641 struct uvc_control_info info;
2642 int ret;
2643
2644 if (ctrl->initialized)
2645 return 0;
2646
2647 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2648 if (ret < 0)
2649 return ret;
2650
2651 ret = uvc_ctrl_add_info(dev, ctrl, &info);
2652 if (ret < 0)
2653 uvc_dbg(dev, CONTROL,
2654 "Failed to initialize control %pUl/%u on device %s entity %u\n",
2655 info.entity, info.selector, dev->udev->devpath,
2656 ctrl->entity->id);
2657
2658 return ret;
2659 }
2660
uvc_xu_ctrl_query(struct uvc_video_chain * chain,struct uvc_xu_control_query * xqry)2661 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2662 struct uvc_xu_control_query *xqry)
2663 {
2664 struct uvc_entity *entity, *iter;
2665 struct uvc_control *ctrl;
2666 unsigned int i;
2667 bool found;
2668 u32 reqflags;
2669 u16 size;
2670 u8 *data = NULL;
2671 int ret;
2672
2673 /* Find the extension unit. */
2674 entity = NULL;
2675 list_for_each_entry(iter, &chain->entities, chain) {
2676 if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
2677 iter->id == xqry->unit) {
2678 entity = iter;
2679 break;
2680 }
2681 }
2682
2683 if (!entity) {
2684 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2685 xqry->unit);
2686 return -ENOENT;
2687 }
2688
2689 /* Find the control and perform delayed initialization if needed. */
2690 found = false;
2691 for (i = 0; i < entity->ncontrols; ++i) {
2692 ctrl = &entity->controls[i];
2693 if (ctrl->index == xqry->selector - 1) {
2694 found = true;
2695 break;
2696 }
2697 }
2698
2699 if (!found) {
2700 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2701 entity->guid, xqry->selector);
2702 return -ENOENT;
2703 }
2704
2705 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2706 return -ERESTARTSYS;
2707
2708 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2709 if (ret < 0) {
2710 ret = -ENOENT;
2711 goto done;
2712 }
2713
2714 /* Validate the required buffer size and flags for the request */
2715 reqflags = 0;
2716 size = ctrl->info.size;
2717
2718 switch (xqry->query) {
2719 case UVC_GET_CUR:
2720 reqflags = UVC_CTRL_FLAG_GET_CUR;
2721 break;
2722 case UVC_GET_MIN:
2723 reqflags = UVC_CTRL_FLAG_GET_MIN;
2724 break;
2725 case UVC_GET_MAX:
2726 reqflags = UVC_CTRL_FLAG_GET_MAX;
2727 break;
2728 case UVC_GET_DEF:
2729 reqflags = UVC_CTRL_FLAG_GET_DEF;
2730 break;
2731 case UVC_GET_RES:
2732 reqflags = UVC_CTRL_FLAG_GET_RES;
2733 break;
2734 case UVC_SET_CUR:
2735 reqflags = UVC_CTRL_FLAG_SET_CUR;
2736 break;
2737 case UVC_GET_LEN:
2738 size = 2;
2739 break;
2740 case UVC_GET_INFO:
2741 size = 1;
2742 break;
2743 default:
2744 ret = -EINVAL;
2745 goto done;
2746 }
2747
2748 if (size != xqry->size) {
2749 ret = -ENOBUFS;
2750 goto done;
2751 }
2752
2753 if (reqflags && !(ctrl->info.flags & reqflags)) {
2754 ret = -EBADRQC;
2755 goto done;
2756 }
2757
2758 data = kmalloc(size, GFP_KERNEL);
2759 if (data == NULL) {
2760 ret = -ENOMEM;
2761 goto done;
2762 }
2763
2764 if (xqry->query == UVC_SET_CUR &&
2765 copy_from_user(data, xqry->data, size)) {
2766 ret = -EFAULT;
2767 goto done;
2768 }
2769
2770 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2771 chain->dev->intfnum, xqry->selector, data, size);
2772 if (ret < 0)
2773 goto done;
2774
2775 if (xqry->query != UVC_SET_CUR &&
2776 copy_to_user(xqry->data, data, size))
2777 ret = -EFAULT;
2778 done:
2779 kfree(data);
2780 mutex_unlock(&chain->ctrl_mutex);
2781 return ret;
2782 }
2783
2784 /* --------------------------------------------------------------------------
2785 * Suspend/resume
2786 */
2787
2788 /*
2789 * Restore control values after resume, skipping controls that haven't been
2790 * changed.
2791 *
2792 * TODO
2793 * - Don't restore modified controls that are back to their default value.
2794 * - Handle restore order (Auto-Exposure Mode should be restored before
2795 * Exposure Time).
2796 */
uvc_ctrl_restore_values(struct uvc_device * dev)2797 int uvc_ctrl_restore_values(struct uvc_device *dev)
2798 {
2799 struct uvc_control *ctrl;
2800 struct uvc_entity *entity;
2801 unsigned int i;
2802 int ret;
2803
2804 /* Walk the entities list and restore controls when possible. */
2805 list_for_each_entry(entity, &dev->entities, list) {
2806
2807 for (i = 0; i < entity->ncontrols; ++i) {
2808 ctrl = &entity->controls[i];
2809
2810 if (!ctrl->initialized || !ctrl->modified ||
2811 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2812 continue;
2813 dev_dbg(&dev->udev->dev,
2814 "restoring control %pUl/%u/%u\n",
2815 ctrl->info.entity, ctrl->info.index,
2816 ctrl->info.selector);
2817 ctrl->dirty = 1;
2818 }
2819
2820 ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
2821 if (ret < 0)
2822 return ret;
2823 }
2824
2825 return 0;
2826 }
2827
2828 /* --------------------------------------------------------------------------
2829 * Control and mapping handling
2830 */
2831
2832 /*
2833 * Add control information to a given control.
2834 */
uvc_ctrl_add_info(struct uvc_device * dev,struct uvc_control * ctrl,const struct uvc_control_info * info)2835 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2836 const struct uvc_control_info *info)
2837 {
2838 ctrl->info = *info;
2839 INIT_LIST_HEAD(&ctrl->info.mappings);
2840
2841 /* Allocate an array to save control values (cur, def, max, etc.) */
2842 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2843 GFP_KERNEL);
2844 if (!ctrl->uvc_data)
2845 return -ENOMEM;
2846
2847 ctrl->initialized = 1;
2848
2849 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2850 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2851 ctrl->entity->id);
2852
2853 return 0;
2854 }
2855
2856 /*
2857 * Add a control mapping to a given control.
2858 */
__uvc_ctrl_add_mapping(struct uvc_video_chain * chain,struct uvc_control * ctrl,const struct uvc_control_mapping * mapping)2859 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2860 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2861 {
2862 struct uvc_control_mapping *map;
2863 unsigned int size;
2864 unsigned int i;
2865 int ret;
2866
2867 /*
2868 * Most mappings come from static kernel data, and need to be duplicated.
2869 * Mappings that come from userspace will be unnecessarily duplicated,
2870 * this could be optimized.
2871 */
2872 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2873 if (!map)
2874 return -ENOMEM;
2875
2876 map->name = NULL;
2877 map->menu_names = NULL;
2878 map->menu_mapping = NULL;
2879
2880 /* For UVCIOC_CTRL_MAP custom control */
2881 if (mapping->name) {
2882 map->name = kstrdup(mapping->name, GFP_KERNEL);
2883 if (!map->name)
2884 goto err_nomem;
2885 }
2886
2887 INIT_LIST_HEAD(&map->ev_subs);
2888
2889 if (mapping->menu_mapping && mapping->menu_mask) {
2890 size = sizeof(mapping->menu_mapping[0])
2891 * fls(mapping->menu_mask);
2892 map->menu_mapping = kmemdup(mapping->menu_mapping, size,
2893 GFP_KERNEL);
2894 if (!map->menu_mapping)
2895 goto err_nomem;
2896 }
2897 if (mapping->menu_names && mapping->menu_mask) {
2898 size = sizeof(mapping->menu_names[0])
2899 * fls(mapping->menu_mask);
2900 map->menu_names = kmemdup(mapping->menu_names, size,
2901 GFP_KERNEL);
2902 if (!map->menu_names)
2903 goto err_nomem;
2904 }
2905
2906 if (uvc_ctrl_mapping_is_compound(map))
2907 if (WARN_ON(!map->set || !map->get)) {
2908 ret = -EIO;
2909 goto free_mem;
2910 }
2911
2912 if (map->get == NULL)
2913 map->get = uvc_get_le_value;
2914 if (map->set == NULL)
2915 map->set = uvc_set_le_value;
2916
2917 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
2918 if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
2919 V4L2_CTRL_ID2WHICH(map->id)) {
2920 chain->ctrl_class_bitmap |= BIT(i);
2921 break;
2922 }
2923 }
2924
2925 list_add_tail(&map->list, &ctrl->info.mappings);
2926 uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2927 uvc_map_get_name(map), ctrl->info.entity,
2928 ctrl->info.selector);
2929
2930 return 0;
2931
2932 err_nomem:
2933 ret = -ENOMEM;
2934 free_mem:
2935 kfree(map->menu_names);
2936 kfree(map->menu_mapping);
2937 kfree(map->name);
2938 kfree(map);
2939 return ret;
2940 }
2941
uvc_ctrl_add_mapping(struct uvc_video_chain * chain,const struct uvc_control_mapping * mapping)2942 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2943 const struct uvc_control_mapping *mapping)
2944 {
2945 struct uvc_device *dev = chain->dev;
2946 struct uvc_control_mapping *map;
2947 struct uvc_entity *entity;
2948 struct uvc_control *ctrl;
2949 int found = 0;
2950 int ret;
2951
2952 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2953 uvc_dbg(dev, CONTROL,
2954 "Can't add mapping '%s', control id 0x%08x is invalid\n",
2955 uvc_map_get_name(mapping), mapping->id);
2956 return -EINVAL;
2957 }
2958
2959 /* Search for the matching (GUID/CS) control on the current chain */
2960 list_for_each_entry(entity, &chain->entities, chain) {
2961 unsigned int i;
2962
2963 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2964 !uvc_entity_match_guid(entity, mapping->entity))
2965 continue;
2966
2967 for (i = 0; i < entity->ncontrols; ++i) {
2968 ctrl = &entity->controls[i];
2969 if (ctrl->index == mapping->selector - 1) {
2970 found = 1;
2971 break;
2972 }
2973 }
2974
2975 if (found)
2976 break;
2977 }
2978 if (!found)
2979 return -ENOENT;
2980
2981 if (mutex_lock_interruptible(&chain->ctrl_mutex))
2982 return -ERESTARTSYS;
2983
2984 /* Perform delayed initialization of XU controls */
2985 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2986 if (ret < 0) {
2987 ret = -ENOENT;
2988 goto done;
2989 }
2990
2991 /* Validate the user-provided bit-size and offset */
2992 if (mapping->size > 32 ||
2993 mapping->offset + mapping->size > ctrl->info.size * 8) {
2994 ret = -EINVAL;
2995 goto done;
2996 }
2997
2998 list_for_each_entry(map, &ctrl->info.mappings, list) {
2999 if (mapping->id == map->id) {
3000 uvc_dbg(dev, CONTROL,
3001 "Can't add mapping '%s', control id 0x%08x already exists\n",
3002 uvc_map_get_name(mapping), mapping->id);
3003 ret = -EEXIST;
3004 goto done;
3005 }
3006 }
3007
3008 /* Prevent excess memory consumption */
3009 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
3010 atomic_dec(&dev->nmappings);
3011 uvc_dbg(dev, CONTROL,
3012 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
3013 uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
3014 ret = -ENOMEM;
3015 goto done;
3016 }
3017
3018 ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
3019 if (ret < 0)
3020 atomic_dec(&dev->nmappings);
3021
3022 done:
3023 mutex_unlock(&chain->ctrl_mutex);
3024 return ret;
3025 }
3026
3027 /*
3028 * Prune an entity of its bogus controls using a blacklist. Bogus controls
3029 * are currently the ones that crash the camera or unconditionally return an
3030 * error when queried.
3031 */
uvc_ctrl_prune_entity(struct uvc_device * dev,struct uvc_entity * entity)3032 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
3033 struct uvc_entity *entity)
3034 {
3035 struct uvc_ctrl_blacklist {
3036 struct usb_device_id id;
3037 u8 index;
3038 };
3039
3040 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
3041 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
3042 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
3043 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
3044 };
3045 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
3046 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
3047 };
3048
3049 const struct uvc_ctrl_blacklist *blacklist;
3050 unsigned int size;
3051 unsigned int count;
3052 unsigned int i;
3053 u8 *controls;
3054
3055 switch (UVC_ENTITY_TYPE(entity)) {
3056 case UVC_VC_PROCESSING_UNIT:
3057 blacklist = processing_blacklist;
3058 count = ARRAY_SIZE(processing_blacklist);
3059 controls = entity->processing.bmControls;
3060 size = entity->processing.bControlSize;
3061 break;
3062
3063 case UVC_ITT_CAMERA:
3064 blacklist = camera_blacklist;
3065 count = ARRAY_SIZE(camera_blacklist);
3066 controls = entity->camera.bmControls;
3067 size = entity->camera.bControlSize;
3068 break;
3069
3070 default:
3071 return;
3072 }
3073
3074 for (i = 0; i < count; ++i) {
3075 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
3076 continue;
3077
3078 if (blacklist[i].index >= 8 * size ||
3079 !uvc_test_bit(controls, blacklist[i].index))
3080 continue;
3081
3082 uvc_dbg(dev, CONTROL,
3083 "%u/%u control is black listed, removing it\n",
3084 entity->id, blacklist[i].index);
3085
3086 uvc_clear_bit(controls, blacklist[i].index);
3087 }
3088 }
3089
3090 /*
3091 * Add control information and hardcoded stock control mappings to the given
3092 * device.
3093 */
uvc_ctrl_init_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl)3094 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
3095 struct uvc_control *ctrl)
3096 {
3097 unsigned int i;
3098
3099 /*
3100 * XU controls initialization requires querying the device for control
3101 * information. As some buggy UVC devices will crash when queried
3102 * repeatedly in a tight loop, delay XU controls initialization until
3103 * first use.
3104 */
3105 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
3106 return;
3107
3108 for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
3109 const struct uvc_control_info *info = &uvc_ctrls[i];
3110
3111 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
3112 ctrl->index == info->index) {
3113 uvc_ctrl_add_info(chain->dev, ctrl, info);
3114 /*
3115 * Retrieve control flags from the device. Ignore errors
3116 * and work with default flag values from the uvc_ctrl
3117 * array when the device doesn't properly implement
3118 * GET_INFO on standard controls.
3119 */
3120 uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
3121 break;
3122 }
3123 }
3124
3125 if (!ctrl->initialized)
3126 return;
3127
3128 /* Process common mappings. */
3129 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
3130 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
3131
3132 if (!uvc_entity_match_guid(ctrl->entity, mapping->entity) ||
3133 ctrl->info.selector != mapping->selector)
3134 continue;
3135
3136 /* Let the device provide a custom mapping. */
3137 if (mapping->filter_mapping) {
3138 mapping = mapping->filter_mapping(chain, ctrl);
3139 if (!mapping)
3140 continue;
3141 }
3142
3143 __uvc_ctrl_add_mapping(chain, ctrl, mapping);
3144 }
3145 }
3146
3147 /*
3148 * Initialize device controls.
3149 */
uvc_ctrl_init_chain(struct uvc_video_chain * chain)3150 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
3151 {
3152 struct uvc_entity *entity;
3153 unsigned int i;
3154
3155 /* Walk the entities list and instantiate controls */
3156 list_for_each_entry(entity, &chain->entities, chain) {
3157 struct uvc_control *ctrl;
3158 unsigned int bControlSize = 0, ncontrols;
3159 u8 *bmControls = NULL;
3160
3161 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
3162 bmControls = entity->extension.bmControls;
3163 bControlSize = entity->extension.bControlSize;
3164 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
3165 bmControls = entity->processing.bmControls;
3166 bControlSize = entity->processing.bControlSize;
3167 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
3168 bmControls = entity->camera.bmControls;
3169 bControlSize = entity->camera.bControlSize;
3170 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
3171 bmControls = entity->gpio.bmControls;
3172 bControlSize = entity->gpio.bControlSize;
3173 }
3174
3175 /* Remove bogus/blacklisted controls */
3176 uvc_ctrl_prune_entity(chain->dev, entity);
3177
3178 /* Count supported controls and allocate the controls array */
3179 ncontrols = memweight(bmControls, bControlSize);
3180 if (ncontrols == 0)
3181 continue;
3182
3183 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
3184 GFP_KERNEL);
3185 if (entity->controls == NULL)
3186 return -ENOMEM;
3187 entity->ncontrols = ncontrols;
3188
3189 /* Initialize all supported controls */
3190 ctrl = entity->controls;
3191 for (i = 0; i < bControlSize * 8; ++i) {
3192 if (uvc_test_bit(bmControls, i) == 0)
3193 continue;
3194
3195 ctrl->entity = entity;
3196 ctrl->index = i;
3197
3198 uvc_ctrl_init_ctrl(chain, ctrl);
3199 ctrl++;
3200 }
3201 }
3202
3203 return 0;
3204 }
3205
uvc_ctrl_init_device(struct uvc_device * dev)3206 int uvc_ctrl_init_device(struct uvc_device *dev)
3207 {
3208 struct uvc_video_chain *chain;
3209 int ret;
3210
3211 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
3212
3213 list_for_each_entry(chain, &dev->chains, list) {
3214 ret = uvc_ctrl_init_chain(chain);
3215 if (ret)
3216 return ret;
3217 }
3218
3219 return 0;
3220 }
3221
uvc_ctrl_cleanup_fh(struct uvc_fh * handle)3222 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
3223 {
3224 struct uvc_entity *entity;
3225
3226 guard(mutex)(&handle->chain->ctrl_mutex);
3227
3228 if (!handle->pending_async_ctrls)
3229 return;
3230
3231 list_for_each_entry(entity, &handle->chain->dev->entities, list) {
3232 for (unsigned int i = 0; i < entity->ncontrols; ++i) {
3233 if (entity->controls[i].handle != handle)
3234 continue;
3235 uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
3236 }
3237 }
3238
3239 WARN_ON(handle->pending_async_ctrls);
3240 }
3241
3242 /*
3243 * Cleanup device controls.
3244 */
uvc_ctrl_cleanup_mappings(struct uvc_device * dev,struct uvc_control * ctrl)3245 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
3246 struct uvc_control *ctrl)
3247 {
3248 struct uvc_control_mapping *mapping, *nm;
3249
3250 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
3251 list_del(&mapping->list);
3252 kfree(mapping->menu_names);
3253 kfree(mapping->menu_mapping);
3254 kfree(mapping->name);
3255 kfree(mapping);
3256 }
3257 }
3258
uvc_ctrl_cleanup_device(struct uvc_device * dev)3259 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
3260 {
3261 struct uvc_entity *entity;
3262 unsigned int i;
3263
3264 /* Can be uninitialized if we are aborting on probe error. */
3265 if (dev->async_ctrl.work.func)
3266 cancel_work_sync(&dev->async_ctrl.work);
3267
3268 /* Free controls and control mappings for all entities. */
3269 list_for_each_entry(entity, &dev->entities, list) {
3270 for (i = 0; i < entity->ncontrols; ++i) {
3271 struct uvc_control *ctrl = &entity->controls[i];
3272
3273 if (!ctrl->initialized)
3274 continue;
3275
3276 uvc_ctrl_cleanup_mappings(dev, ctrl);
3277 kfree(ctrl->uvc_data);
3278 }
3279
3280 kfree(entity->controls);
3281 }
3282 }
3283