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