blob: 088437a5f60cffddb6f364359511ce9ea26f1b14 [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_ctrl.c -- USB Video Class driver - Controls
3 *
4 * Copyright (C) 2005-2008
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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/version.h>
16#include <linux/list.h>
17#include <linux/module.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 <asm/atomic.h>
24
25#include "uvcvideo.h"
26
27#define UVC_CTRL_NDATA 2
28#define UVC_CTRL_DATA_CURRENT 0
29#define UVC_CTRL_DATA_BACKUP 1
30
31/* ------------------------------------------------------------------------
32 * Control, formats, ...
33 */
34
35static struct uvc_control_info uvc_ctrls[] = {
36 {
37 .entity = UVC_GUID_UVC_PROCESSING,
38 .selector = PU_BRIGHTNESS_CONTROL,
39 .index = 0,
40 .size = 2,
41 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
42 | UVC_CONTROL_RESTORE,
43 },
44 {
45 .entity = UVC_GUID_UVC_PROCESSING,
46 .selector = PU_CONTRAST_CONTROL,
47 .index = 1,
48 .size = 2,
49 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
50 | UVC_CONTROL_RESTORE,
51 },
52 {
53 .entity = UVC_GUID_UVC_PROCESSING,
54 .selector = PU_HUE_CONTROL,
55 .index = 2,
56 .size = 2,
57 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
58 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
59 },
60 {
61 .entity = UVC_GUID_UVC_PROCESSING,
62 .selector = PU_SATURATION_CONTROL,
63 .index = 3,
64 .size = 2,
65 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
66 | UVC_CONTROL_RESTORE,
67 },
68 {
69 .entity = UVC_GUID_UVC_PROCESSING,
70 .selector = PU_SHARPNESS_CONTROL,
71 .index = 4,
72 .size = 2,
73 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
74 | UVC_CONTROL_RESTORE,
75 },
76 {
77 .entity = UVC_GUID_UVC_PROCESSING,
78 .selector = PU_GAMMA_CONTROL,
79 .index = 5,
80 .size = 2,
81 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
82 | UVC_CONTROL_RESTORE,
83 },
84 {
85 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchart5e26d502008-09-22 13:14:59 -030086 .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
87 .index = 6,
88 .size = 2,
89 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
90 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
91 },
92 {
93 .entity = UVC_GUID_UVC_PROCESSING,
94 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
95 .index = 7,
96 .size = 4,
97 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
98 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
99 },
100 {
101 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300102 .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
103 .index = 8,
104 .size = 2,
105 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
106 | UVC_CONTROL_RESTORE,
107 },
108 {
109 .entity = UVC_GUID_UVC_PROCESSING,
110 .selector = PU_GAIN_CONTROL,
111 .index = 9,
112 .size = 2,
113 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
114 | UVC_CONTROL_RESTORE,
115 },
116 {
117 .entity = UVC_GUID_UVC_PROCESSING,
118 .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
119 .index = 10,
120 .size = 1,
121 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
122 | UVC_CONTROL_RESTORE,
123 },
124 {
125 .entity = UVC_GUID_UVC_PROCESSING,
126 .selector = PU_HUE_AUTO_CONTROL,
127 .index = 11,
128 .size = 1,
129 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
130 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
131 },
132 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300133 .entity = UVC_GUID_UVC_PROCESSING,
134 .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
135 .index = 12,
136 .size = 1,
137 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
138 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
139 },
140 {
141 .entity = UVC_GUID_UVC_PROCESSING,
142 .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
143 .index = 13,
144 .size = 1,
145 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
146 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
147 },
148 {
149 .entity = UVC_GUID_UVC_PROCESSING,
150 .selector = PU_DIGITAL_MULTIPLIER_CONTROL,
151 .index = 14,
152 .size = 2,
153 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
154 | UVC_CONTROL_RESTORE,
155 },
156 {
157 .entity = UVC_GUID_UVC_PROCESSING,
158 .selector = PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
159 .index = 15,
160 .size = 2,
161 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
162 | UVC_CONTROL_RESTORE,
163 },
164 {
165 .entity = UVC_GUID_UVC_PROCESSING,
166 .selector = PU_ANALOG_VIDEO_STANDARD_CONTROL,
167 .index = 16,
168 .size = 1,
169 .flags = UVC_CONTROL_GET_CUR,
170 },
171 {
172 .entity = UVC_GUID_UVC_PROCESSING,
173 .selector = PU_ANALOG_LOCK_STATUS_CONTROL,
174 .index = 17,
175 .size = 1,
176 .flags = UVC_CONTROL_GET_CUR,
177 },
178 {
179 .entity = UVC_GUID_UVC_CAMERA,
180 .selector = CT_SCANNING_MODE_CONTROL,
181 .index = 0,
182 .size = 1,
183 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
184 | UVC_CONTROL_RESTORE,
185 },
186 {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300187 .entity = UVC_GUID_UVC_CAMERA,
188 .selector = CT_AE_MODE_CONTROL,
189 .index = 1,
190 .size = 1,
191 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
192 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
193 | UVC_CONTROL_RESTORE,
194 },
195 {
196 .entity = UVC_GUID_UVC_CAMERA,
197 .selector = CT_AE_PRIORITY_CONTROL,
198 .index = 2,
199 .size = 1,
200 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
201 | UVC_CONTROL_RESTORE,
202 },
203 {
204 .entity = UVC_GUID_UVC_CAMERA,
205 .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
206 .index = 3,
207 .size = 4,
208 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
209 | UVC_CONTROL_RESTORE,
210 },
211 {
212 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300213 .selector = CT_EXPOSURE_TIME_RELATIVE_CONTROL,
214 .index = 4,
215 .size = 1,
216 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
217 | UVC_CONTROL_RESTORE,
218 },
219 {
220 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300221 .selector = CT_FOCUS_ABSOLUTE_CONTROL,
222 .index = 5,
223 .size = 2,
224 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
225 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
226 },
227 {
228 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300229 .selector = CT_FOCUS_RELATIVE_CONTROL,
230 .index = 6,
231 .size = 2,
232 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
233 | UVC_CONTROL_AUTO_UPDATE,
234 },
235 {
236 .entity = UVC_GUID_UVC_CAMERA,
237 .selector = CT_IRIS_ABSOLUTE_CONTROL,
238 .index = 7,
239 .size = 2,
240 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
241 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
242 },
243 {
244 .entity = UVC_GUID_UVC_CAMERA,
245 .selector = CT_IRIS_RELATIVE_CONTROL,
246 .index = 8,
247 .size = 1,
248 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
249 | UVC_CONTROL_AUTO_UPDATE,
250 },
251 {
252 .entity = UVC_GUID_UVC_CAMERA,
253 .selector = CT_ZOOM_ABSOLUTE_CONTROL,
254 .index = 9,
255 .size = 2,
256 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
257 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
258 },
259 {
260 .entity = UVC_GUID_UVC_CAMERA,
261 .selector = CT_ZOOM_RELATIVE_CONTROL,
262 .index = 10,
263 .size = 3,
264 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
265 | UVC_CONTROL_AUTO_UPDATE,
266 },
267 {
268 .entity = UVC_GUID_UVC_CAMERA,
269 .selector = CT_PANTILT_ABSOLUTE_CONTROL,
270 .index = 11,
271 .size = 8,
272 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
273 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
274 },
275 {
276 .entity = UVC_GUID_UVC_CAMERA,
277 .selector = CT_PANTILT_RELATIVE_CONTROL,
278 .index = 12,
279 .size = 4,
280 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
281 | UVC_CONTROL_AUTO_UPDATE,
282 },
283 {
284 .entity = UVC_GUID_UVC_CAMERA,
285 .selector = CT_ROLL_ABSOLUTE_CONTROL,
286 .index = 13,
287 .size = 2,
288 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
289 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
290 },
291 {
292 .entity = UVC_GUID_UVC_CAMERA,
293 .selector = CT_ROLL_RELATIVE_CONTROL,
294 .index = 14,
295 .size = 2,
296 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
297 | UVC_CONTROL_AUTO_UPDATE,
298 },
299 {
300 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300301 .selector = CT_FOCUS_AUTO_CONTROL,
302 .index = 17,
303 .size = 1,
304 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
305 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
306 },
307 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300308 .entity = UVC_GUID_UVC_CAMERA,
309 .selector = CT_PRIVACY_CONTROL,
310 .index = 18,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300311 .size = 1,
312 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300313 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
314 },
315};
316
317static struct uvc_menu_info power_line_frequency_controls[] = {
318 { 0, "Disabled" },
319 { 1, "50 Hz" },
320 { 2, "60 Hz" },
321};
322
323static struct uvc_menu_info exposure_auto_controls[] = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300324 { 2, "Auto Mode" },
Laurent Pinchart90ac5ea2008-07-26 11:42:29 -0300325 { 1, "Manual Mode" },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300326 { 4, "Shutter Priority Mode" },
327 { 8, "Aperture Priority Mode" },
328};
329
330static struct uvc_control_mapping uvc_ctrl_mappings[] = {
331 {
332 .id = V4L2_CID_BRIGHTNESS,
333 .name = "Brightness",
334 .entity = UVC_GUID_UVC_PROCESSING,
335 .selector = PU_BRIGHTNESS_CONTROL,
336 .size = 16,
337 .offset = 0,
338 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
339 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
340 },
341 {
342 .id = V4L2_CID_CONTRAST,
343 .name = "Contrast",
344 .entity = UVC_GUID_UVC_PROCESSING,
345 .selector = PU_CONTRAST_CONTROL,
346 .size = 16,
347 .offset = 0,
348 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
349 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
350 },
351 {
352 .id = V4L2_CID_HUE,
353 .name = "Hue",
354 .entity = UVC_GUID_UVC_PROCESSING,
355 .selector = PU_HUE_CONTROL,
356 .size = 16,
357 .offset = 0,
358 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
359 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
360 },
361 {
362 .id = V4L2_CID_SATURATION,
363 .name = "Saturation",
364 .entity = UVC_GUID_UVC_PROCESSING,
365 .selector = PU_SATURATION_CONTROL,
366 .size = 16,
367 .offset = 0,
368 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
369 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
370 },
371 {
372 .id = V4L2_CID_SHARPNESS,
373 .name = "Sharpness",
374 .entity = UVC_GUID_UVC_PROCESSING,
375 .selector = PU_SHARPNESS_CONTROL,
376 .size = 16,
377 .offset = 0,
378 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
379 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
380 },
381 {
382 .id = V4L2_CID_GAMMA,
383 .name = "Gamma",
384 .entity = UVC_GUID_UVC_PROCESSING,
385 .selector = PU_GAMMA_CONTROL,
386 .size = 16,
387 .offset = 0,
388 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
389 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
390 },
391 {
392 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
393 .name = "Backlight Compensation",
394 .entity = UVC_GUID_UVC_PROCESSING,
395 .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
396 .size = 16,
397 .offset = 0,
398 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
399 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
400 },
401 {
402 .id = V4L2_CID_GAIN,
403 .name = "Gain",
404 .entity = UVC_GUID_UVC_PROCESSING,
405 .selector = PU_GAIN_CONTROL,
406 .size = 16,
407 .offset = 0,
408 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
409 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
410 },
411 {
412 .id = V4L2_CID_POWER_LINE_FREQUENCY,
413 .name = "Power Line Frequency",
414 .entity = UVC_GUID_UVC_PROCESSING,
415 .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
416 .size = 2,
417 .offset = 0,
418 .v4l2_type = V4L2_CTRL_TYPE_MENU,
419 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
420 .menu_info = power_line_frequency_controls,
421 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
422 },
423 {
424 .id = V4L2_CID_HUE_AUTO,
425 .name = "Hue, Auto",
426 .entity = UVC_GUID_UVC_PROCESSING,
427 .selector = PU_HUE_AUTO_CONTROL,
428 .size = 1,
429 .offset = 0,
430 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
431 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
432 },
433 {
434 .id = V4L2_CID_EXPOSURE_AUTO,
435 .name = "Exposure, Auto",
436 .entity = UVC_GUID_UVC_CAMERA,
437 .selector = CT_AE_MODE_CONTROL,
438 .size = 4,
439 .offset = 0,
440 .v4l2_type = V4L2_CTRL_TYPE_MENU,
441 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
442 .menu_info = exposure_auto_controls,
443 .menu_count = ARRAY_SIZE(exposure_auto_controls),
444 },
445 {
446 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
447 .name = "Exposure, Auto Priority",
448 .entity = UVC_GUID_UVC_CAMERA,
449 .selector = CT_AE_PRIORITY_CONTROL,
450 .size = 1,
451 .offset = 0,
452 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
453 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
454 },
455 {
456 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
457 .name = "Exposure (Absolute)",
458 .entity = UVC_GUID_UVC_CAMERA,
459 .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
460 .size = 32,
461 .offset = 0,
462 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
463 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
464 },
465 {
466 .id = V4L2_CID_AUTO_WHITE_BALANCE,
467 .name = "White Balance Temperature, Auto",
468 .entity = UVC_GUID_UVC_PROCESSING,
469 .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
470 .size = 1,
471 .offset = 0,
472 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
473 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
474 },
475 {
476 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
477 .name = "White Balance Temperature",
478 .entity = UVC_GUID_UVC_PROCESSING,
479 .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
480 .size = 16,
481 .offset = 0,
482 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
483 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
484 },
485 {
486 .id = V4L2_CID_AUTO_WHITE_BALANCE,
487 .name = "White Balance Component, Auto",
488 .entity = UVC_GUID_UVC_PROCESSING,
489 .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
490 .size = 1,
491 .offset = 0,
492 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
493 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
494 },
495 {
496 .id = V4L2_CID_BLUE_BALANCE,
497 .name = "White Balance Blue Component",
498 .entity = UVC_GUID_UVC_PROCESSING,
499 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
500 .size = 16,
501 .offset = 0,
502 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
503 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
504 },
505 {
506 .id = V4L2_CID_RED_BALANCE,
507 .name = "White Balance Red Component",
508 .entity = UVC_GUID_UVC_PROCESSING,
509 .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
510 .size = 16,
511 .offset = 16,
512 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
513 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
514 },
515 {
516 .id = V4L2_CID_FOCUS_ABSOLUTE,
517 .name = "Focus (absolute)",
518 .entity = UVC_GUID_UVC_CAMERA,
519 .selector = CT_FOCUS_ABSOLUTE_CONTROL,
520 .size = 16,
521 .offset = 0,
522 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
523 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
524 },
525 {
526 .id = V4L2_CID_FOCUS_AUTO,
527 .name = "Focus, Auto",
528 .entity = UVC_GUID_UVC_CAMERA,
529 .selector = CT_FOCUS_AUTO_CONTROL,
530 .size = 1,
531 .offset = 0,
532 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
533 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
534 },
535};
536
537/* ------------------------------------------------------------------------
538 * Utility functions
539 */
540
541static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
542{
543 return ctrl->data + id * ctrl->info->size;
544}
545
546static inline int uvc_get_bit(const __u8 *data, int bit)
547{
548 return (data[bit >> 3] >> (bit & 7)) & 1;
549}
550
551/* Extract the bit string specified by mapping->offset and mapping->size
552 * from the little-endian data stored at 'data' and return the result as
553 * a signed 32bit integer. Sign extension will be performed if the mapping
554 * references a signed data type.
555 */
556static __s32 uvc_get_le_value(const __u8 *data,
557 struct uvc_control_mapping *mapping)
558{
559 int bits = mapping->size;
560 int offset = mapping->offset;
561 __s32 value = 0;
562 __u8 mask;
563
564 data += offset / 8;
565 offset &= 7;
566 mask = ((1LL << bits) - 1) << offset;
567
568 for (; bits > 0; data++) {
569 __u8 byte = *data & mask;
570 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
571 bits -= 8 - (offset > 0 ? offset : 0);
572 offset -= 8;
573 mask = (1 << bits) - 1;
574 }
575
576 /* Sign-extend the value if needed */
577 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
578 value |= -(value & (1 << (mapping->size - 1)));
579
580 return value;
581}
582
583/* Set the bit string specified by mapping->offset and mapping->size
584 * in the little-endian data stored at 'data' to the value 'value'.
585 */
586static void uvc_set_le_value(__s32 value, __u8 *data,
587 struct uvc_control_mapping *mapping)
588{
589 int bits = mapping->size;
590 int offset = mapping->offset;
591 __u8 mask;
592
593 data += offset / 8;
594 offset &= 7;
595
596 for (; bits > 0; data++) {
597 mask = ((1LL << bits) - 1) << offset;
598 *data = (*data & ~mask) | ((value << offset) & mask);
599 value >>= offset ? offset : 8;
600 bits -= 8 - offset;
601 offset = 0;
602 }
603}
604
605/* ------------------------------------------------------------------------
606 * Terminal and unit management
607 */
608
609static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
610static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
611static const __u8 uvc_media_transport_input_guid[16] =
612 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
613
614static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
615{
616 switch (UVC_ENTITY_TYPE(entity)) {
617 case ITT_CAMERA:
618 return memcmp(uvc_camera_guid, guid, 16) == 0;
619
620 case ITT_MEDIA_TRANSPORT_INPUT:
621 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
622
623 case VC_PROCESSING_UNIT:
624 return memcmp(uvc_processing_guid, guid, 16) == 0;
625
626 case VC_EXTENSION_UNIT:
627 return memcmp(entity->extension.guidExtensionCode,
628 guid, 16) == 0;
629
630 default:
631 return 0;
632 }
633}
634
635/* ------------------------------------------------------------------------
636 * UVC Controls
637 */
638
639static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
640 struct uvc_control_mapping **mapping, struct uvc_control **control,
641 int next)
642{
643 struct uvc_control *ctrl;
644 struct uvc_control_mapping *map;
645 unsigned int i;
646
647 if (entity == NULL)
648 return;
649
650 for (i = 0; i < entity->ncontrols; ++i) {
651 ctrl = &entity->controls[i];
652 if (ctrl->info == NULL)
653 continue;
654
655 list_for_each_entry(map, &ctrl->info->mappings, list) {
656 if ((map->id == v4l2_id) && !next) {
657 *control = ctrl;
658 *mapping = map;
659 return;
660 }
661
662 if ((*mapping == NULL || (*mapping)->id > map->id) &&
663 (map->id > v4l2_id) && next) {
664 *control = ctrl;
665 *mapping = map;
666 }
667 }
668 }
669}
670
671struct uvc_control *uvc_find_control(struct uvc_video_device *video,
672 __u32 v4l2_id, struct uvc_control_mapping **mapping)
673{
674 struct uvc_control *ctrl = NULL;
675 struct uvc_entity *entity;
676 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
677
678 *mapping = NULL;
679
680 /* Mask the query flags. */
681 v4l2_id &= V4L2_CTRL_ID_MASK;
682
683 /* Find the control. */
684 __uvc_find_control(video->processing, v4l2_id, mapping, &ctrl, next);
685 if (ctrl && !next)
686 return ctrl;
687
688 list_for_each_entry(entity, &video->iterms, chain) {
689 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
690 if (ctrl && !next)
691 return ctrl;
692 }
693
694 list_for_each_entry(entity, &video->extensions, chain) {
695 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
696 if (ctrl && !next)
697 return ctrl;
698 }
699
700 if (ctrl == NULL && !next)
701 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
702 v4l2_id);
703
704 return ctrl;
705}
706
707int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
708 struct v4l2_queryctrl *v4l2_ctrl)
709{
710 struct uvc_control *ctrl;
711 struct uvc_control_mapping *mapping;
712 struct uvc_menu_info *menu;
713 unsigned int i;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300714 __u8 *data;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300715 int ret;
716
717 ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
718 if (ctrl == NULL)
719 return -EINVAL;
720
Ralph Loaderfe6c7002008-09-22 21:06:48 -0300721 data = kmalloc(ctrl->info->size, GFP_KERNEL);
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300722 if (data == NULL)
723 return -ENOMEM;
724
Laurent Pinchart54812c72008-07-17 07:37:37 -0300725 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300726 v4l2_ctrl->id = mapping->id;
727 v4l2_ctrl->type = mapping->v4l2_type;
728 strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
729 v4l2_ctrl->flags = 0;
730
731 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
732 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
733
734 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
735 if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
736 video->dev->intfnum, ctrl->info->selector,
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300737 data, ctrl->info->size)) < 0)
738 goto out;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300739 v4l2_ctrl->default_value = uvc_get_le_value(data, mapping);
740 }
741
Laurent Pinchart54812c72008-07-17 07:37:37 -0300742 switch (mapping->v4l2_type) {
743 case V4L2_CTRL_TYPE_MENU:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300744 v4l2_ctrl->minimum = 0;
745 v4l2_ctrl->maximum = mapping->menu_count - 1;
746 v4l2_ctrl->step = 1;
747
748 menu = mapping->menu_info;
749 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
750 if (menu->value == v4l2_ctrl->default_value) {
751 v4l2_ctrl->default_value = i;
752 break;
753 }
754 }
755
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300756 ret = 0;
757 goto out;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300758
759 case V4L2_CTRL_TYPE_BOOLEAN:
760 v4l2_ctrl->minimum = 0;
761 v4l2_ctrl->maximum = 1;
762 v4l2_ctrl->step = 1;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300763 ret = 0;
764 goto out;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300765
766 default:
767 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300768 }
769
770 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
771 if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
772 video->dev->intfnum, ctrl->info->selector,
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300773 data, ctrl->info->size)) < 0)
774 goto out;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300775 v4l2_ctrl->minimum = uvc_get_le_value(data, mapping);
776 }
777 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
778 if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
779 video->dev->intfnum, ctrl->info->selector,
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300780 data, ctrl->info->size)) < 0)
781 goto out;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300782 v4l2_ctrl->maximum = uvc_get_le_value(data, mapping);
783 }
784 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
785 if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
786 video->dev->intfnum, ctrl->info->selector,
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300787 data, ctrl->info->size)) < 0)
788 goto out;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300789 v4l2_ctrl->step = uvc_get_le_value(data, mapping);
790 }
791
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300792 ret = 0;
793out:
794 kfree(data);
795 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300796}
797
798
799/* --------------------------------------------------------------------------
800 * Control transactions
801 *
802 * To make extended set operations as atomic as the hardware allows, controls
803 * are handled using begin/commit/rollback operations.
804 *
805 * At the beginning of a set request, uvc_ctrl_begin should be called to
806 * initialize the request. This function acquires the control lock.
807 *
808 * When setting a control, the new value is stored in the control data field
809 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
810 * later processing. If the UVC and V4L2 control sizes differ, the current
811 * value is loaded from the hardware before storing the new value in the data
812 * field.
813 *
814 * After processing all controls in the transaction, uvc_ctrl_commit or
815 * uvc_ctrl_rollback must be called to apply the pending changes to the
816 * hardware or revert them. When applying changes, all controls marked as
817 * dirty will be modified in the UVC device, and the dirty flag will be
818 * cleared. When reverting controls, the control data field
819 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
820 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
821 * control lock.
822 */
823int uvc_ctrl_begin(struct uvc_video_device *video)
824{
825 return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
826}
827
828static int uvc_ctrl_commit_entity(struct uvc_device *dev,
829 struct uvc_entity *entity, int rollback)
830{
831 struct uvc_control *ctrl;
832 unsigned int i;
833 int ret;
834
835 if (entity == NULL)
836 return 0;
837
838 for (i = 0; i < entity->ncontrols; ++i) {
839 ctrl = &entity->controls[i];
840 if (ctrl->info == NULL || !ctrl->dirty)
841 continue;
842
843 if (!rollback)
844 ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
845 dev->intfnum, ctrl->info->selector,
846 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
847 ctrl->info->size);
848 else
849 ret = 0;
850
851 if (rollback || ret < 0)
852 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
853 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
854 ctrl->info->size);
855
856 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
857 ctrl->loaded = 0;
858
859 ctrl->dirty = 0;
860
861 if (ret < 0)
862 return ret;
863 }
864
865 return 0;
866}
867
868int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
869{
870 struct uvc_entity *entity;
871 int ret = 0;
872
873 /* Find the control. */
874 ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
875 if (ret < 0)
876 goto done;
877
878 list_for_each_entry(entity, &video->iterms, chain) {
879 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
880 if (ret < 0)
881 goto done;
882 }
883
884 list_for_each_entry(entity, &video->extensions, chain) {
885 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
886 if (ret < 0)
887 goto done;
888 }
889
890done:
891 mutex_unlock(&video->ctrl_mutex);
892 return ret;
893}
894
895int uvc_ctrl_get(struct uvc_video_device *video,
896 struct v4l2_ext_control *xctrl)
897{
898 struct uvc_control *ctrl;
899 struct uvc_control_mapping *mapping;
900 struct uvc_menu_info *menu;
901 unsigned int i;
902 int ret;
903
904 ctrl = uvc_find_control(video, xctrl->id, &mapping);
905 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
906 return -EINVAL;
907
908 if (!ctrl->loaded) {
909 ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
910 video->dev->intfnum, ctrl->info->selector,
911 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
912 ctrl->info->size);
913 if (ret < 0)
914 return ret;
915
916 if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
917 ctrl->loaded = 1;
918 }
919
920 xctrl->value = uvc_get_le_value(
921 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
922
923 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
924 menu = mapping->menu_info;
925 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
926 if (menu->value == xctrl->value) {
927 xctrl->value = i;
928 break;
929 }
930 }
931 }
932
933 return 0;
934}
935
936int uvc_ctrl_set(struct uvc_video_device *video,
937 struct v4l2_ext_control *xctrl)
938{
939 struct uvc_control *ctrl;
940 struct uvc_control_mapping *mapping;
941 s32 value = xctrl->value;
942 int ret;
943
944 ctrl = uvc_find_control(video, xctrl->id, &mapping);
945 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
946 return -EINVAL;
947
948 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
949 if (value < 0 || value >= mapping->menu_count)
950 return -EINVAL;
951 value = mapping->menu_info[value].value;
952 }
953
954 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
955 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
956 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
957 0, ctrl->info->size);
958 } else {
959 ret = uvc_query_ctrl(video->dev, GET_CUR,
960 ctrl->entity->id, video->dev->intfnum,
961 ctrl->info->selector,
962 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
963 ctrl->info->size);
964 if (ret < 0)
965 return ret;
966 }
967
968 if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
969 ctrl->loaded = 1;
970 }
971
972 if (!ctrl->dirty) {
973 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
974 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
975 ctrl->info->size);
976 }
977
978 uvc_set_le_value(value,
979 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
980
981 ctrl->dirty = 1;
982 ctrl->modified = 1;
983 return 0;
984}
985
986/* --------------------------------------------------------------------------
987 * Dynamic controls
988 */
989
990int uvc_xu_ctrl_query(struct uvc_video_device *video,
991 struct uvc_xu_control *xctrl, int set)
992{
993 struct uvc_entity *entity;
994 struct uvc_control *ctrl = NULL;
995 unsigned int i, found = 0;
996 __u8 *data;
997 int ret;
998
999 /* Find the extension unit. */
1000 list_for_each_entry(entity, &video->extensions, chain) {
1001 if (entity->id == xctrl->unit)
1002 break;
1003 }
1004
1005 if (entity->id != xctrl->unit) {
1006 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1007 xctrl->unit);
1008 return -EINVAL;
1009 }
1010
1011 /* Find the control. */
1012 for (i = 0; i < entity->ncontrols; ++i) {
1013 ctrl = &entity->controls[i];
1014 if (ctrl->info == NULL)
1015 continue;
1016
1017 if (ctrl->info->selector == xctrl->selector) {
1018 found = 1;
1019 break;
1020 }
1021 }
1022
1023 if (!found) {
1024 uvc_trace(UVC_TRACE_CONTROL,
1025 "Control " UVC_GUID_FORMAT "/%u not found.\n",
1026 UVC_GUID_ARGS(entity->extension.guidExtensionCode),
1027 xctrl->selector);
1028 return -EINVAL;
1029 }
1030
1031 /* Validate control data size. */
1032 if (ctrl->info->size != xctrl->size)
1033 return -EINVAL;
1034
1035 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1036 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1037 return -EINVAL;
1038
1039 if (mutex_lock_interruptible(&video->ctrl_mutex))
1040 return -ERESTARTSYS;
1041
1042 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1043 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1044 xctrl->size);
1045 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1046
1047 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1048 ret = -EFAULT;
1049 goto out;
1050 }
1051
1052 ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
1053 video->dev->intfnum, xctrl->selector, data,
1054 xctrl->size);
1055 if (ret < 0)
1056 goto out;
1057
1058 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1059 ret = -EFAULT;
1060 goto out;
1061 }
1062
1063out:
1064 if (ret)
1065 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1066 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1067 xctrl->size);
1068
1069 mutex_unlock(&video->ctrl_mutex);
1070 return ret;
1071}
1072
1073/* --------------------------------------------------------------------------
1074 * Suspend/resume
1075 */
1076
1077/*
1078 * Restore control values after resume, skipping controls that haven't been
1079 * changed.
1080 *
1081 * TODO
1082 * - Don't restore modified controls that are back to their default value.
1083 * - Handle restore order (Auto-Exposure Mode should be restored before
1084 * Exposure Time).
1085 */
1086int uvc_ctrl_resume_device(struct uvc_device *dev)
1087{
1088 struct uvc_control *ctrl;
1089 struct uvc_entity *entity;
1090 unsigned int i;
1091 int ret;
1092
1093 /* Walk the entities list and restore controls when possible. */
1094 list_for_each_entry(entity, &dev->entities, list) {
1095
1096 for (i = 0; i < entity->ncontrols; ++i) {
1097 ctrl = &entity->controls[i];
1098
1099 if (ctrl->info == NULL || !ctrl->modified ||
1100 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1101 continue;
1102
1103 printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
1104 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
1105 ctrl->info->index, ctrl->info->selector);
1106 ctrl->dirty = 1;
1107 }
1108
1109 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1110 if (ret < 0)
1111 return ret;
1112 }
1113
1114 return 0;
1115}
1116
1117/* --------------------------------------------------------------------------
1118 * Control and mapping handling
1119 */
1120
1121static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1122 struct uvc_control_info *info)
1123{
1124 struct uvc_entity *entity;
1125 struct uvc_control *ctrl = NULL;
1126 int ret, found = 0;
1127 unsigned int i;
1128
1129 list_for_each_entry(entity, &dev->entities, list) {
1130 if (!uvc_entity_match_guid(entity, info->entity))
1131 continue;
1132
1133 for (i = 0; i < entity->ncontrols; ++i) {
1134 ctrl = &entity->controls[i];
1135 if (ctrl->index == info->index) {
1136 found = 1;
1137 break;
1138 }
1139 }
1140
1141 if (found)
1142 break;
1143 }
1144
1145 if (!found)
1146 return;
1147
1148 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1149 /* Check if the device control information and length match
1150 * the user supplied information.
1151 */
1152 __u32 flags;
1153 __le16 size;
1154 __u8 inf;
1155
1156 if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
1157 dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
1158 uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1159 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1160 UVC_GUID_ARGS(info->entity), info->selector,
1161 ret);
1162 return;
1163 }
1164
1165 if (info->size != le16_to_cpu(size)) {
1166 uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1167 "/%u size doesn't match user supplied "
1168 "value.\n", UVC_GUID_ARGS(info->entity),
1169 info->selector);
1170 return;
1171 }
1172
1173 if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
1174 dev->intfnum, info->selector, &inf, 1)) < 0) {
1175 uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1176 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1177 UVC_GUID_ARGS(info->entity), info->selector,
1178 ret);
1179 return;
1180 }
1181
1182 flags = info->flags;
1183 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1184 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1185 uvc_trace(UVC_TRACE_CONTROL, "Control "
1186 UVC_GUID_FORMAT "/%u flags don't match "
1187 "supported operations.\n",
1188 UVC_GUID_ARGS(info->entity), info->selector);
1189 return;
1190 }
1191 }
1192
1193 ctrl->info = info;
1194 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1195 uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1196 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1197 ctrl->info->selector, dev->udev->devpath, entity->id);
1198}
1199
1200/*
1201 * Add an item to the UVC control information list, and instantiate a control
1202 * structure for each device that supports the control.
1203 */
1204int uvc_ctrl_add_info(struct uvc_control_info *info)
1205{
1206 struct uvc_control_info *ctrl;
1207 struct uvc_device *dev;
1208 int ret = 0;
1209
1210 /* Find matching controls by walking the devices, entities and
1211 * controls list.
1212 */
1213 mutex_lock(&uvc_driver.ctrl_mutex);
1214
1215 /* First check if the list contains a control matching the new one.
1216 * Bail out if it does.
1217 */
1218 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1219 if (memcmp(ctrl->entity, info->entity, 16))
1220 continue;
1221
1222 if (ctrl->selector == info->selector) {
1223 uvc_trace(UVC_TRACE_CONTROL, "Control "
1224 UVC_GUID_FORMAT "/%u is already defined.\n",
1225 UVC_GUID_ARGS(info->entity), info->selector);
1226 ret = -EEXIST;
1227 goto end;
1228 }
1229 if (ctrl->index == info->index) {
1230 uvc_trace(UVC_TRACE_CONTROL, "Control "
1231 UVC_GUID_FORMAT "/%u would overwrite index "
1232 "%d.\n", UVC_GUID_ARGS(info->entity),
1233 info->selector, info->index);
1234 ret = -EEXIST;
1235 goto end;
1236 }
1237 }
1238
1239 list_for_each_entry(dev, &uvc_driver.devices, list)
1240 uvc_ctrl_add_ctrl(dev, info);
1241
1242 INIT_LIST_HEAD(&info->mappings);
1243 list_add_tail(&info->list, &uvc_driver.controls);
1244end:
1245 mutex_unlock(&uvc_driver.ctrl_mutex);
1246 return ret;
1247}
1248
1249int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1250{
1251 struct uvc_control_info *info;
1252 struct uvc_control_mapping *map;
1253 int ret = -EINVAL;
1254
1255 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1256 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1257 "invalid control id 0x%08x\n", mapping->name,
1258 mapping->id);
1259 return -EINVAL;
1260 }
1261
1262 mutex_lock(&uvc_driver.ctrl_mutex);
1263 list_for_each_entry(info, &uvc_driver.controls, list) {
1264 if (memcmp(info->entity, mapping->entity, 16) ||
1265 info->selector != mapping->selector)
1266 continue;
1267
1268 if (info->size * 8 < mapping->size + mapping->offset) {
1269 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1270 "overflow control " UVC_GUID_FORMAT "/%u\n",
1271 mapping->name, UVC_GUID_ARGS(info->entity),
1272 info->selector);
1273 ret = -EOVERFLOW;
1274 goto end;
1275 }
1276
1277 /* Check if the list contains a mapping matching the new one.
1278 * Bail out if it does.
1279 */
1280 list_for_each_entry(map, &info->mappings, list) {
1281 if (map->id == mapping->id) {
1282 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1283 "already defined.\n", mapping->name);
1284 ret = -EEXIST;
1285 goto end;
1286 }
1287 }
1288
1289 mapping->ctrl = info;
1290 list_add_tail(&mapping->list, &info->mappings);
1291 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1292 UVC_GUID_FORMAT "/%u.\n", mapping->name,
1293 UVC_GUID_ARGS(info->entity), info->selector);
1294
1295 ret = 0;
1296 break;
1297 }
1298end:
1299 mutex_unlock(&uvc_driver.ctrl_mutex);
1300 return ret;
1301}
1302
1303/*
1304 * Initialize device controls.
1305 */
1306int uvc_ctrl_init_device(struct uvc_device *dev)
1307{
1308 struct uvc_control_info *info;
1309 struct uvc_control *ctrl;
1310 struct uvc_entity *entity;
1311 unsigned int i;
1312
1313 /* Walk the entities list and instantiate controls */
1314 list_for_each_entry(entity, &dev->entities, list) {
1315 unsigned int bControlSize = 0, ncontrols = 0;
1316 __u8 *bmControls = NULL;
1317
1318 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1319 bmControls = entity->extension.bmControls;
1320 bControlSize = entity->extension.bControlSize;
1321 } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
1322 bmControls = entity->processing.bmControls;
1323 bControlSize = entity->processing.bControlSize;
1324 } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
1325 bmControls = entity->camera.bmControls;
1326 bControlSize = entity->camera.bControlSize;
1327 }
1328
1329 for (i = 0; i < bControlSize; ++i)
1330 ncontrols += hweight8(bmControls[i]);
1331
1332 if (ncontrols == 0)
1333 continue;
1334
1335 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1336 if (entity->controls == NULL)
1337 return -ENOMEM;
1338
1339 entity->ncontrols = ncontrols;
1340
1341 ctrl = entity->controls;
1342 for (i = 0; i < bControlSize * 8; ++i) {
1343 if (uvc_get_bit(bmControls, i) == 0)
1344 continue;
1345
1346 ctrl->entity = entity;
1347 ctrl->index = i;
1348 ctrl++;
1349 }
1350 }
1351
1352 /* Walk the controls info list and associate them with the device
1353 * controls, then add the device to the global device list. This has
1354 * to be done while holding the controls lock, to make sure
1355 * uvc_ctrl_add_info() will not get called in-between.
1356 */
1357 mutex_lock(&uvc_driver.ctrl_mutex);
1358 list_for_each_entry(info, &uvc_driver.controls, list)
1359 uvc_ctrl_add_ctrl(dev, info);
1360
1361 list_add_tail(&dev->list, &uvc_driver.devices);
1362 mutex_unlock(&uvc_driver.ctrl_mutex);
1363
1364 return 0;
1365}
1366
1367/*
1368 * Cleanup device controls.
1369 */
1370void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1371{
1372 struct uvc_entity *entity;
1373 unsigned int i;
1374
1375 /* Remove the device from the global devices list */
1376 mutex_lock(&uvc_driver.ctrl_mutex);
1377 if (dev->list.next != NULL)
1378 list_del(&dev->list);
1379 mutex_unlock(&uvc_driver.ctrl_mutex);
1380
1381 list_for_each_entry(entity, &dev->entities, list) {
1382 for (i = 0; i < entity->ncontrols; ++i)
1383 kfree(entity->controls[i].data);
1384
1385 kfree(entity->controls);
1386 }
1387}
1388
1389void uvc_ctrl_init(void)
1390{
1391 struct uvc_control_info *ctrl = uvc_ctrls;
1392 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1393 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1394 struct uvc_control_mapping *mend =
1395 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1396
1397 for (; ctrl < cend; ++ctrl)
1398 uvc_ctrl_add_info(ctrl);
1399
1400 for (; mapping < mend; ++mapping)
1401 uvc_ctrl_add_mapping(mapping);
1402}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001403