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