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