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