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