Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[linux-2.6-block.git] / drivers / media / video / uvc / uvc_ctrl.c
CommitLineData
c0efd232
LP
1/*
2 * uvc_ctrl.c -- USB Video Class driver - Controls
3 *
2c2d264b 4 * Copyright (C) 2005-2009
c0efd232
LP
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>
c0efd232
LP
15#include <linux/list.h>
16#include <linux/module.h>
5a0e3ad6 17#include <linux/slab.h>
c0efd232
LP
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
c0efd232
LP
27#define UVC_CTRL_DATA_CURRENT 0
28#define UVC_CTRL_DATA_BACKUP 1
59529081
LP
29#define UVC_CTRL_DATA_MIN 2
30#define UVC_CTRL_DATA_MAX 3
31#define UVC_CTRL_DATA_RES 4
32#define UVC_CTRL_DATA_DEF 5
33#define UVC_CTRL_DATA_LAST 6
c0efd232
LP
34
35/* ------------------------------------------------------------------------
2c2d264b 36 * Controls
c0efd232
LP
37 */
38
39static struct uvc_control_info uvc_ctrls[] = {
40 {
41 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 42 .selector = UVC_PU_BRIGHTNESS_CONTROL,
c0efd232
LP
43 .index = 0,
44 .size = 2,
45 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
46 | UVC_CONTROL_RESTORE,
47 },
48 {
49 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 50 .selector = UVC_PU_CONTRAST_CONTROL,
c0efd232
LP
51 .index = 1,
52 .size = 2,
53 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
54 | UVC_CONTROL_RESTORE,
55 },
56 {
57 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 58 .selector = UVC_PU_HUE_CONTROL,
c0efd232
LP
59 .index = 2,
60 .size = 2,
61 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
62 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
63 },
64 {
65 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 66 .selector = UVC_PU_SATURATION_CONTROL,
c0efd232
LP
67 .index = 3,
68 .size = 2,
69 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
70 | UVC_CONTROL_RESTORE,
71 },
72 {
73 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 74 .selector = UVC_PU_SHARPNESS_CONTROL,
c0efd232
LP
75 .index = 4,
76 .size = 2,
77 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
78 | UVC_CONTROL_RESTORE,
79 },
80 {
81 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 82 .selector = UVC_PU_GAMMA_CONTROL,
c0efd232
LP
83 .index = 5,
84 .size = 2,
85 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
86 | UVC_CONTROL_RESTORE,
87 },
5e26d50f
LP
88 {
89 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 90 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
5e26d50f
LP
91 .index = 6,
92 .size = 2,
93 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
94 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
95 },
96 {
97 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 98 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
5e26d50f
LP
99 .index = 7,
100 .size = 4,
101 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
102 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
103 },
c0efd232
LP
104 {
105 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 106 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
c0efd232
LP
107 .index = 8,
108 .size = 2,
109 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
110 | UVC_CONTROL_RESTORE,
111 },
112 {
113 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 114 .selector = UVC_PU_GAIN_CONTROL,
c0efd232
LP
115 .index = 9,
116 .size = 2,
117 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
118 | UVC_CONTROL_RESTORE,
119 },
120 {
121 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 122 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
c0efd232
LP
123 .index = 10,
124 .size = 1,
125 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
126 | UVC_CONTROL_RESTORE,
127 },
128 {
129 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 130 .selector = UVC_PU_HUE_AUTO_CONTROL,
c0efd232
LP
131 .index = 11,
132 .size = 1,
133 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
134 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
135 },
5e26d50f
LP
136 {
137 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 138 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
5e26d50f
LP
139 .index = 12,
140 .size = 1,
141 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
142 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
143 },
144 {
145 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 146 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
5e26d50f
LP
147 .index = 13,
148 .size = 1,
149 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
150 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
151 },
152 {
153 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 154 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
5e26d50f
LP
155 .index = 14,
156 .size = 2,
157 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
158 | UVC_CONTROL_RESTORE,
159 },
160 {
161 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 162 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
5e26d50f
LP
163 .index = 15,
164 .size = 2,
165 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
166 | UVC_CONTROL_RESTORE,
167 },
168 {
169 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 170 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
5e26d50f
LP
171 .index = 16,
172 .size = 1,
173 .flags = UVC_CONTROL_GET_CUR,
174 },
175 {
176 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 177 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
5e26d50f
LP
178 .index = 17,
179 .size = 1,
180 .flags = UVC_CONTROL_GET_CUR,
181 },
182 {
183 .entity = UVC_GUID_UVC_CAMERA,
b482d923 184 .selector = UVC_CT_SCANNING_MODE_CONTROL,
5e26d50f
LP
185 .index = 0,
186 .size = 1,
187 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
188 | UVC_CONTROL_RESTORE,
189 },
c0efd232
LP
190 {
191 .entity = UVC_GUID_UVC_CAMERA,
b482d923 192 .selector = UVC_CT_AE_MODE_CONTROL,
c0efd232
LP
193 .index = 1,
194 .size = 1,
195 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
196 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
197 | UVC_CONTROL_RESTORE,
198 },
199 {
200 .entity = UVC_GUID_UVC_CAMERA,
b482d923 201 .selector = UVC_CT_AE_PRIORITY_CONTROL,
c0efd232
LP
202 .index = 2,
203 .size = 1,
204 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
205 | UVC_CONTROL_RESTORE,
206 },
207 {
208 .entity = UVC_GUID_UVC_CAMERA,
b482d923 209 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
c0efd232
LP
210 .index = 3,
211 .size = 4,
212 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
213 | UVC_CONTROL_RESTORE,
214 },
5e26d50f
LP
215 {
216 .entity = UVC_GUID_UVC_CAMERA,
b482d923 217 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
5e26d50f
LP
218 .index = 4,
219 .size = 1,
a90ef69c 220 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_RESTORE,
5e26d50f 221 },
c0efd232
LP
222 {
223 .entity = UVC_GUID_UVC_CAMERA,
b482d923 224 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
c0efd232
LP
225 .index = 5,
226 .size = 2,
227 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
228 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
229 },
230 {
231 .entity = UVC_GUID_UVC_CAMERA,
b482d923 232 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
5e26d50f
LP
233 .index = 6,
234 .size = 2,
a90ef69c
LP
235 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
236 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
237 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
c0efd232
LP
238 },
239 {
5e26d50f 240 .entity = UVC_GUID_UVC_CAMERA,
b482d923 241 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
5e26d50f
LP
242 .index = 7,
243 .size = 2,
244 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
245 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
246 },
247 {
248 .entity = UVC_GUID_UVC_CAMERA,
b482d923 249 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
5e26d50f 250 .index = 8,
c0efd232 251 .size = 1,
a90ef69c 252 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_AUTO_UPDATE,
c0efd232
LP
253 },
254 {
5e26d50f 255 .entity = UVC_GUID_UVC_CAMERA,
b482d923 256 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
5e26d50f 257 .index = 9,
c0efd232
LP
258 .size = 2,
259 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
260 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
261 },
262 {
5e26d50f 263 .entity = UVC_GUID_UVC_CAMERA,
b482d923 264 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
5e26d50f
LP
265 .index = 10,
266 .size = 3,
a90ef69c
LP
267 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
268 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
269 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
5e26d50f
LP
270 },
271 {
272 .entity = UVC_GUID_UVC_CAMERA,
b482d923 273 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
5e26d50f
LP
274 .index = 11,
275 .size = 8,
276 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
277 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
278 },
279 {
280 .entity = UVC_GUID_UVC_CAMERA,
b482d923 281 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
5e26d50f
LP
282 .index = 12,
283 .size = 4,
a90ef69c
LP
284 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
285 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
286 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
5e26d50f
LP
287 },
288 {
289 .entity = UVC_GUID_UVC_CAMERA,
b482d923 290 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
c0efd232 291 .index = 13,
5e26d50f
LP
292 .size = 2,
293 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
294 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
295 },
296 {
297 .entity = UVC_GUID_UVC_CAMERA,
b482d923 298 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
5e26d50f
LP
299 .index = 14,
300 .size = 2,
a90ef69c
LP
301 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
302 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
303 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
5e26d50f
LP
304 },
305 {
306 .entity = UVC_GUID_UVC_CAMERA,
b482d923 307 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
5e26d50f 308 .index = 17,
c0efd232
LP
309 .size = 1,
310 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
311 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
312 },
313 {
5e26d50f 314 .entity = UVC_GUID_UVC_CAMERA,
b482d923 315 .selector = UVC_CT_PRIVACY_CONTROL,
5e26d50f
LP
316 .index = 18,
317 .size = 1,
318 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
c0efd232
LP
319 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
320 },
321};
322
323static struct uvc_menu_info power_line_frequency_controls[] = {
324 { 0, "Disabled" },
325 { 1, "50 Hz" },
326 { 2, "60 Hz" },
327};
328
329static struct uvc_menu_info exposure_auto_controls[] = {
c0efd232 330 { 2, "Auto Mode" },
90ac5ea3 331 { 1, "Manual Mode" },
c0efd232
LP
332 { 4, "Shutter Priority Mode" },
333 { 8, "Aperture Priority Mode" },
334};
335
9768352a
LP
336static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
337 __u8 query, const __u8 *data)
338{
339 __s8 zoom = (__s8)data[0];
340
341 switch (query) {
b482d923 342 case UVC_GET_CUR:
9768352a
LP
343 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
344
b482d923
LP
345 case UVC_GET_MIN:
346 case UVC_GET_MAX:
347 case UVC_GET_RES:
348 case UVC_GET_DEF:
9768352a
LP
349 default:
350 return data[2];
351 }
352}
353
354static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
355 __s32 value, __u8 *data)
356{
357 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
2e896133 358 data[2] = min((int)abs(value), 0xff);
9768352a
LP
359}
360
c0efd232
LP
361static struct uvc_control_mapping uvc_ctrl_mappings[] = {
362 {
363 .id = V4L2_CID_BRIGHTNESS,
364 .name = "Brightness",
365 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 366 .selector = UVC_PU_BRIGHTNESS_CONTROL,
c0efd232
LP
367 .size = 16,
368 .offset = 0,
369 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
370 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
371 },
372 {
373 .id = V4L2_CID_CONTRAST,
374 .name = "Contrast",
375 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 376 .selector = UVC_PU_CONTRAST_CONTROL,
c0efd232
LP
377 .size = 16,
378 .offset = 0,
379 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
380 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
381 },
382 {
383 .id = V4L2_CID_HUE,
384 .name = "Hue",
385 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 386 .selector = UVC_PU_HUE_CONTROL,
c0efd232
LP
387 .size = 16,
388 .offset = 0,
389 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
390 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
391 },
392 {
393 .id = V4L2_CID_SATURATION,
394 .name = "Saturation",
395 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 396 .selector = UVC_PU_SATURATION_CONTROL,
c0efd232
LP
397 .size = 16,
398 .offset = 0,
399 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
400 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
401 },
402 {
403 .id = V4L2_CID_SHARPNESS,
404 .name = "Sharpness",
405 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 406 .selector = UVC_PU_SHARPNESS_CONTROL,
c0efd232
LP
407 .size = 16,
408 .offset = 0,
409 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
410 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
411 },
412 {
413 .id = V4L2_CID_GAMMA,
414 .name = "Gamma",
415 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 416 .selector = UVC_PU_GAMMA_CONTROL,
c0efd232
LP
417 .size = 16,
418 .offset = 0,
419 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
420 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
421 },
422 {
423 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
424 .name = "Backlight Compensation",
425 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 426 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
c0efd232
LP
427 .size = 16,
428 .offset = 0,
429 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
430 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
431 },
432 {
433 .id = V4L2_CID_GAIN,
434 .name = "Gain",
435 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 436 .selector = UVC_PU_GAIN_CONTROL,
c0efd232
LP
437 .size = 16,
438 .offset = 0,
439 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
440 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
441 },
442 {
443 .id = V4L2_CID_POWER_LINE_FREQUENCY,
444 .name = "Power Line Frequency",
445 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 446 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
c0efd232
LP
447 .size = 2,
448 .offset = 0,
449 .v4l2_type = V4L2_CTRL_TYPE_MENU,
450 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
451 .menu_info = power_line_frequency_controls,
452 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
453 },
454 {
455 .id = V4L2_CID_HUE_AUTO,
456 .name = "Hue, Auto",
457 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 458 .selector = UVC_PU_HUE_AUTO_CONTROL,
c0efd232
LP
459 .size = 1,
460 .offset = 0,
461 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
462 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
463 },
464 {
465 .id = V4L2_CID_EXPOSURE_AUTO,
466 .name = "Exposure, Auto",
467 .entity = UVC_GUID_UVC_CAMERA,
b482d923 468 .selector = UVC_CT_AE_MODE_CONTROL,
c0efd232
LP
469 .size = 4,
470 .offset = 0,
471 .v4l2_type = V4L2_CTRL_TYPE_MENU,
472 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
473 .menu_info = exposure_auto_controls,
474 .menu_count = ARRAY_SIZE(exposure_auto_controls),
475 },
476 {
477 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
478 .name = "Exposure, Auto Priority",
479 .entity = UVC_GUID_UVC_CAMERA,
b482d923 480 .selector = UVC_CT_AE_PRIORITY_CONTROL,
c0efd232
LP
481 .size = 1,
482 .offset = 0,
483 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
484 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
485 },
486 {
487 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
488 .name = "Exposure (Absolute)",
489 .entity = UVC_GUID_UVC_CAMERA,
b482d923 490 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
c0efd232
LP
491 .size = 32,
492 .offset = 0,
493 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
494 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
495 },
496 {
497 .id = V4L2_CID_AUTO_WHITE_BALANCE,
498 .name = "White Balance Temperature, Auto",
499 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 500 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
c0efd232
LP
501 .size = 1,
502 .offset = 0,
503 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
504 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
505 },
506 {
507 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
508 .name = "White Balance Temperature",
509 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 510 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
c0efd232
LP
511 .size = 16,
512 .offset = 0,
513 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
514 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
515 },
516 {
517 .id = V4L2_CID_AUTO_WHITE_BALANCE,
518 .name = "White Balance Component, Auto",
519 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 520 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
c0efd232
LP
521 .size = 1,
522 .offset = 0,
523 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
524 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
525 },
526 {
527 .id = V4L2_CID_BLUE_BALANCE,
528 .name = "White Balance Blue Component",
529 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 530 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
c0efd232
LP
531 .size = 16,
532 .offset = 0,
533 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
534 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
535 },
536 {
537 .id = V4L2_CID_RED_BALANCE,
538 .name = "White Balance Red Component",
539 .entity = UVC_GUID_UVC_PROCESSING,
b482d923 540 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
c0efd232
LP
541 .size = 16,
542 .offset = 16,
543 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
544 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
545 },
546 {
547 .id = V4L2_CID_FOCUS_ABSOLUTE,
548 .name = "Focus (absolute)",
549 .entity = UVC_GUID_UVC_CAMERA,
b482d923 550 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
c0efd232
LP
551 .size = 16,
552 .offset = 0,
553 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
554 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
555 },
556 {
557 .id = V4L2_CID_FOCUS_AUTO,
558 .name = "Focus, Auto",
559 .entity = UVC_GUID_UVC_CAMERA,
b482d923 560 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
c0efd232
LP
561 .size = 1,
562 .offset = 0,
563 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
564 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
565 },
35a00c41
LP
566 {
567 .id = V4L2_CID_IRIS_ABSOLUTE,
568 .name = "Iris, Absolute",
569 .entity = UVC_GUID_UVC_CAMERA,
570 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
571 .size = 16,
572 .offset = 0,
573 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
574 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
575 },
576 {
577 .id = V4L2_CID_IRIS_RELATIVE,
578 .name = "Iris, Relative",
579 .entity = UVC_GUID_UVC_CAMERA,
580 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
581 .size = 8,
582 .offset = 0,
583 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
584 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
585 },
9768352a
LP
586 {
587 .id = V4L2_CID_ZOOM_ABSOLUTE,
588 .name = "Zoom, Absolute",
589 .entity = UVC_GUID_UVC_CAMERA,
b482d923 590 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
9768352a
LP
591 .size = 16,
592 .offset = 0,
593 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
594 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
595 },
596 {
597 .id = V4L2_CID_ZOOM_CONTINUOUS,
598 .name = "Zoom, Continuous",
599 .entity = UVC_GUID_UVC_CAMERA,
b482d923 600 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
9768352a
LP
601 .size = 0,
602 .offset = 0,
603 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
604 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
605 .get = uvc_ctrl_get_zoom,
606 .set = uvc_ctrl_set_zoom,
607 },
6df126f8
LP
608 {
609 .id = V4L2_CID_PRIVACY,
610 .name = "Privacy",
611 .entity = UVC_GUID_UVC_CAMERA,
b482d923 612 .selector = UVC_CT_PRIVACY_CONTROL,
6df126f8
LP
613 .size = 1,
614 .offset = 0,
615 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
616 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
617 },
c0efd232
LP
618};
619
620/* ------------------------------------------------------------------------
621 * Utility functions
622 */
623
624static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
625{
626 return ctrl->data + id * ctrl->info->size;
627}
628
2bdd29cf 629static inline int uvc_test_bit(const __u8 *data, int bit)
c0efd232
LP
630{
631 return (data[bit >> 3] >> (bit & 7)) & 1;
632}
633
2bdd29cf
LP
634static inline void uvc_clear_bit(__u8 *data, int bit)
635{
636 data[bit >> 3] &= ~(1 << (bit & 7));
637}
638
c0efd232
LP
639/* Extract the bit string specified by mapping->offset and mapping->size
640 * from the little-endian data stored at 'data' and return the result as
641 * a signed 32bit integer. Sign extension will be performed if the mapping
642 * references a signed data type.
643 */
9768352a
LP
644static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
645 __u8 query, const __u8 *data)
c0efd232
LP
646{
647 int bits = mapping->size;
648 int offset = mapping->offset;
649 __s32 value = 0;
650 __u8 mask;
651
652 data += offset / 8;
653 offset &= 7;
654 mask = ((1LL << bits) - 1) << offset;
655
656 for (; bits > 0; data++) {
657 __u8 byte = *data & mask;
658 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
659 bits -= 8 - (offset > 0 ? offset : 0);
660 offset -= 8;
661 mask = (1 << bits) - 1;
662 }
663
2c2d264b 664 /* Sign-extend the value if needed. */
c0efd232
LP
665 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
666 value |= -(value & (1 << (mapping->size - 1)));
667
668 return value;
669}
670
671/* Set the bit string specified by mapping->offset and mapping->size
672 * in the little-endian data stored at 'data' to the value 'value'.
673 */
9768352a
LP
674static void uvc_set_le_value(struct uvc_control_mapping *mapping,
675 __s32 value, __u8 *data)
c0efd232
LP
676{
677 int bits = mapping->size;
678 int offset = mapping->offset;
679 __u8 mask;
680
681 data += offset / 8;
682 offset &= 7;
683
684 for (; bits > 0; data++) {
685 mask = ((1LL << bits) - 1) << offset;
686 *data = (*data & ~mask) | ((value << offset) & mask);
687 value >>= offset ? offset : 8;
688 bits -= 8 - offset;
689 offset = 0;
690 }
691}
692
693/* ------------------------------------------------------------------------
694 * Terminal and unit management
695 */
696
697static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
698static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
699static const __u8 uvc_media_transport_input_guid[16] =
700 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
701
702static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
703{
704 switch (UVC_ENTITY_TYPE(entity)) {
b482d923 705 case UVC_ITT_CAMERA:
c0efd232
LP
706 return memcmp(uvc_camera_guid, guid, 16) == 0;
707
b482d923 708 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
c0efd232
LP
709 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
710
b482d923 711 case UVC_VC_PROCESSING_UNIT:
c0efd232
LP
712 return memcmp(uvc_processing_guid, guid, 16) == 0;
713
b482d923 714 case UVC_VC_EXTENSION_UNIT:
c0efd232
LP
715 return memcmp(entity->extension.guidExtensionCode,
716 guid, 16) == 0;
717
718 default:
719 return 0;
720 }
721}
722
723/* ------------------------------------------------------------------------
724 * UVC Controls
725 */
726
727static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
728 struct uvc_control_mapping **mapping, struct uvc_control **control,
729 int next)
730{
731 struct uvc_control *ctrl;
732 struct uvc_control_mapping *map;
733 unsigned int i;
734
735 if (entity == NULL)
736 return;
737
738 for (i = 0; i < entity->ncontrols; ++i) {
739 ctrl = &entity->controls[i];
740 if (ctrl->info == NULL)
741 continue;
742
743 list_for_each_entry(map, &ctrl->info->mappings, list) {
744 if ((map->id == v4l2_id) && !next) {
745 *control = ctrl;
746 *mapping = map;
747 return;
748 }
749
750 if ((*mapping == NULL || (*mapping)->id > map->id) &&
751 (map->id > v4l2_id) && next) {
752 *control = ctrl;
753 *mapping = map;
754 }
755 }
756 }
757}
758
8e113595 759struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
c0efd232
LP
760 __u32 v4l2_id, struct uvc_control_mapping **mapping)
761{
762 struct uvc_control *ctrl = NULL;
763 struct uvc_entity *entity;
764 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
765
766 *mapping = NULL;
767
768 /* Mask the query flags. */
769 v4l2_id &= V4L2_CTRL_ID_MASK;
770
771 /* Find the control. */
6241d8ca 772 list_for_each_entry(entity, &chain->entities, chain) {
c0efd232
LP
773 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
774 if (ctrl && !next)
775 return ctrl;
776 }
777
778 if (ctrl == NULL && !next)
779 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
780 v4l2_id);
781
782 return ctrl;
783}
784
59529081
LP
785static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
786 struct uvc_control *ctrl)
787{
788 int ret;
789
790 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
791 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
792 chain->dev->intfnum, ctrl->info->selector,
793 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
794 ctrl->info->size);
795 if (ret < 0)
796 return ret;
797 }
798
799 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
800 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
801 chain->dev->intfnum, ctrl->info->selector,
802 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
803 ctrl->info->size);
804 if (ret < 0)
805 return ret;
806 }
807 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
808 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
809 chain->dev->intfnum, ctrl->info->selector,
810 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
811 ctrl->info->size);
812 if (ret < 0)
813 return ret;
814 }
815 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
816 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
817 chain->dev->intfnum, ctrl->info->selector,
818 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
819 ctrl->info->size);
820 if (ret < 0)
821 return ret;
822 }
823
824 ctrl->cached = 1;
825 return 0;
826}
827
8e113595 828int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
c0efd232
LP
829 struct v4l2_queryctrl *v4l2_ctrl)
830{
831 struct uvc_control *ctrl;
832 struct uvc_control_mapping *mapping;
833 struct uvc_menu_info *menu;
834 unsigned int i;
c0efd232
LP
835 int ret;
836
8e113595 837 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
c0efd232
LP
838 if (ctrl == NULL)
839 return -EINVAL;
840
54812c77 841 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
c0efd232
LP
842 v4l2_ctrl->id = mapping->id;
843 v4l2_ctrl->type = mapping->v4l2_type;
d0ebf307 844 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
c0efd232
LP
845 v4l2_ctrl->flags = 0;
846
a90ef69c
LP
847 if (!(ctrl->info->flags & UVC_CONTROL_GET_CUR))
848 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
c0efd232
LP
849 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
850 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
851
59529081
LP
852 if (!ctrl->cached) {
853 ret = uvc_ctrl_populate_cache(chain, ctrl);
b482d923 854 if (ret < 0)
59529081
LP
855 return ret;
856 }
857
858 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
859 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
860 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
c0efd232
LP
861 }
862
54812c77
LP
863 switch (mapping->v4l2_type) {
864 case V4L2_CTRL_TYPE_MENU:
c0efd232
LP
865 v4l2_ctrl->minimum = 0;
866 v4l2_ctrl->maximum = mapping->menu_count - 1;
867 v4l2_ctrl->step = 1;
868
869 menu = mapping->menu_info;
870 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
871 if (menu->value == v4l2_ctrl->default_value) {
872 v4l2_ctrl->default_value = i;
873 break;
874 }
875 }
876
59529081 877 return 0;
54812c77
LP
878
879 case V4L2_CTRL_TYPE_BOOLEAN:
880 v4l2_ctrl->minimum = 0;
881 v4l2_ctrl->maximum = 1;
882 v4l2_ctrl->step = 1;
59529081 883 return 0;
54812c77 884
f4eabafe
LP
885 case V4L2_CTRL_TYPE_BUTTON:
886 v4l2_ctrl->minimum = 0;
887 v4l2_ctrl->maximum = 0;
888 v4l2_ctrl->step = 0;
59529081 889 return 0;
f4eabafe 890
54812c77
LP
891 default:
892 break;
c0efd232
LP
893 }
894
59529081
LP
895 if (ctrl->info->flags & UVC_CONTROL_GET_MIN)
896 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
897 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
c0efd232 898
59529081
LP
899 if (ctrl->info->flags & UVC_CONTROL_GET_MAX)
900 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
901 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
902
903 if (ctrl->info->flags & UVC_CONTROL_GET_RES)
904 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
905 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
906
907 return 0;
c0efd232
LP
908}
909
910
911/* --------------------------------------------------------------------------
912 * Control transactions
913 *
914 * To make extended set operations as atomic as the hardware allows, controls
915 * are handled using begin/commit/rollback operations.
916 *
917 * At the beginning of a set request, uvc_ctrl_begin should be called to
918 * initialize the request. This function acquires the control lock.
919 *
920 * When setting a control, the new value is stored in the control data field
921 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
922 * later processing. If the UVC and V4L2 control sizes differ, the current
923 * value is loaded from the hardware before storing the new value in the data
924 * field.
925 *
926 * After processing all controls in the transaction, uvc_ctrl_commit or
927 * uvc_ctrl_rollback must be called to apply the pending changes to the
928 * hardware or revert them. When applying changes, all controls marked as
929 * dirty will be modified in the UVC device, and the dirty flag will be
930 * cleared. When reverting controls, the control data field
931 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
932 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
933 * control lock.
934 */
8e113595 935int uvc_ctrl_begin(struct uvc_video_chain *chain)
c0efd232 936{
8e113595 937 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
c0efd232
LP
938}
939
940static int uvc_ctrl_commit_entity(struct uvc_device *dev,
941 struct uvc_entity *entity, int rollback)
942{
943 struct uvc_control *ctrl;
944 unsigned int i;
945 int ret;
946
947 if (entity == NULL)
948 return 0;
949
950 for (i = 0; i < entity->ncontrols; ++i) {
951 ctrl = &entity->controls[i];
b1accfa1
LP
952 if (ctrl->info == NULL)
953 continue;
954
955 /* Reset the loaded flag for auto-update controls that were
956 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
957 * uvc_ctrl_get from using the cached value.
958 */
959 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
960 ctrl->loaded = 0;
961
962 if (!ctrl->dirty)
c0efd232
LP
963 continue;
964
965 if (!rollback)
b482d923 966 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
c0efd232
LP
967 dev->intfnum, ctrl->info->selector,
968 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
969 ctrl->info->size);
970 else
971 ret = 0;
972
973 if (rollback || ret < 0)
974 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
975 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
976 ctrl->info->size);
977
c0efd232
LP
978 ctrl->dirty = 0;
979
980 if (ret < 0)
981 return ret;
982 }
983
984 return 0;
985}
986
8e113595 987int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
c0efd232
LP
988{
989 struct uvc_entity *entity;
990 int ret = 0;
991
992 /* Find the control. */
6241d8ca 993 list_for_each_entry(entity, &chain->entities, chain) {
8e113595 994 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
c0efd232
LP
995 if (ret < 0)
996 goto done;
997 }
998
999done:
8e113595 1000 mutex_unlock(&chain->ctrl_mutex);
c0efd232
LP
1001 return ret;
1002}
1003
8e113595 1004int uvc_ctrl_get(struct uvc_video_chain *chain,
c0efd232
LP
1005 struct v4l2_ext_control *xctrl)
1006{
1007 struct uvc_control *ctrl;
1008 struct uvc_control_mapping *mapping;
1009 struct uvc_menu_info *menu;
1010 unsigned int i;
1011 int ret;
1012
8e113595 1013 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
c0efd232
LP
1014 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
1015 return -EINVAL;
1016
1017 if (!ctrl->loaded) {
8e113595
LP
1018 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1019 chain->dev->intfnum, ctrl->info->selector,
c0efd232
LP
1020 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1021 ctrl->info->size);
1022 if (ret < 0)
1023 return ret;
1024
b1accfa1 1025 ctrl->loaded = 1;
c0efd232
LP
1026 }
1027
b482d923 1028 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
9768352a 1029 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
c0efd232
LP
1030
1031 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1032 menu = mapping->menu_info;
1033 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1034 if (menu->value == xctrl->value) {
1035 xctrl->value = i;
1036 break;
1037 }
1038 }
1039 }
1040
1041 return 0;
1042}
1043
8e113595 1044int uvc_ctrl_set(struct uvc_video_chain *chain,
c0efd232
LP
1045 struct v4l2_ext_control *xctrl)
1046{
1047 struct uvc_control *ctrl;
1048 struct uvc_control_mapping *mapping;
e54532e5
LP
1049 s32 value;
1050 u32 step;
1051 s32 min;
1052 s32 max;
c0efd232
LP
1053 int ret;
1054
8e113595 1055 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
c0efd232
LP
1056 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1057 return -EINVAL;
1058
e54532e5
LP
1059 /* Clamp out of range values. */
1060 switch (mapping->v4l2_type) {
1061 case V4L2_CTRL_TYPE_INTEGER:
1062 if (!ctrl->cached) {
1063 ret = uvc_ctrl_populate_cache(chain, ctrl);
1064 if (ret < 0)
1065 return ret;
1066 }
1067
1068 min = mapping->get(mapping, UVC_GET_MIN,
1069 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1070 max = mapping->get(mapping, UVC_GET_MAX,
1071 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1072 step = mapping->get(mapping, UVC_GET_RES,
1073 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
cf7a50ee
LP
1074 if (step == 0)
1075 step = 1;
e54532e5
LP
1076
1077 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1078 xctrl->value = clamp(xctrl->value, min, max);
1079 value = xctrl->value;
1080 break;
1081
1082 case V4L2_CTRL_TYPE_BOOLEAN:
1083 xctrl->value = clamp(xctrl->value, 0, 1);
1084 value = xctrl->value;
1085 break;
1086
1087 case V4L2_CTRL_TYPE_MENU:
1088 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
8a4e76c1 1089 return -ERANGE;
e54532e5
LP
1090 value = mapping->menu_info[xctrl->value].value;
1091 break;
1092
1093 default:
1094 value = xctrl->value;
1095 break;
c0efd232
LP
1096 }
1097
e54532e5
LP
1098 /* If the mapping doesn't span the whole UVC control, the current value
1099 * needs to be loaded from the device to perform the read-modify-write
1100 * operation.
1101 */
c0efd232
LP
1102 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1103 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1104 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1105 0, ctrl->info->size);
1106 } else {
8e113595
LP
1107 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1108 ctrl->entity->id, chain->dev->intfnum,
c0efd232
LP
1109 ctrl->info->selector,
1110 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1111 ctrl->info->size);
1112 if (ret < 0)
1113 return ret;
1114 }
1115
b1accfa1 1116 ctrl->loaded = 1;
c0efd232
LP
1117 }
1118
e54532e5 1119 /* Backup the current value in case we need to rollback later. */
c0efd232
LP
1120 if (!ctrl->dirty) {
1121 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1122 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1123 ctrl->info->size);
1124 }
1125
9768352a
LP
1126 mapping->set(mapping, value,
1127 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
c0efd232
LP
1128
1129 ctrl->dirty = 1;
1130 ctrl->modified = 1;
1131 return 0;
1132}
1133
1134/* --------------------------------------------------------------------------
1135 * Dynamic controls
1136 */
1137
8e113595 1138int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
c0efd232
LP
1139 struct uvc_xu_control *xctrl, int set)
1140{
1141 struct uvc_entity *entity;
1142 struct uvc_control *ctrl = NULL;
1143 unsigned int i, found = 0;
1144 __u8 *data;
1145 int ret;
1146
1147 /* Find the extension unit. */
6241d8ca
LP
1148 list_for_each_entry(entity, &chain->entities, chain) {
1149 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1150 entity->id == xctrl->unit)
c0efd232
LP
1151 break;
1152 }
1153
1154 if (entity->id != xctrl->unit) {
1155 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1156 xctrl->unit);
1157 return -EINVAL;
1158 }
1159
1160 /* Find the control. */
1161 for (i = 0; i < entity->ncontrols; ++i) {
1162 ctrl = &entity->controls[i];
1163 if (ctrl->info == NULL)
1164 continue;
1165
1166 if (ctrl->info->selector == xctrl->selector) {
1167 found = 1;
1168 break;
1169 }
1170 }
1171
1172 if (!found) {
36bd883e
LP
1173 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1174 entity->extension.guidExtensionCode, xctrl->selector);
c0efd232
LP
1175 return -EINVAL;
1176 }
1177
1178 /* Validate control data size. */
1179 if (ctrl->info->size != xctrl->size)
1180 return -EINVAL;
1181
1182 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1183 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1184 return -EINVAL;
1185
8e113595 1186 if (mutex_lock_interruptible(&chain->ctrl_mutex))
c0efd232
LP
1187 return -ERESTARTSYS;
1188
1189 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1190 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1191 xctrl->size);
1192 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1193
1194 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1195 ret = -EFAULT;
1196 goto out;
1197 }
1198
8e113595
LP
1199 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1200 xctrl->unit, chain->dev->intfnum, xctrl->selector,
b482d923 1201 data, xctrl->size);
c0efd232
LP
1202 if (ret < 0)
1203 goto out;
1204
1205 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1206 ret = -EFAULT;
1207 goto out;
1208 }
1209
1210out:
1211 if (ret)
1212 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1213 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1214 xctrl->size);
1215
8e113595 1216 mutex_unlock(&chain->ctrl_mutex);
c0efd232
LP
1217 return ret;
1218}
1219
1220/* --------------------------------------------------------------------------
1221 * Suspend/resume
1222 */
1223
1224/*
1225 * Restore control values after resume, skipping controls that haven't been
1226 * changed.
1227 *
1228 * TODO
1229 * - Don't restore modified controls that are back to their default value.
1230 * - Handle restore order (Auto-Exposure Mode should be restored before
1231 * Exposure Time).
1232 */
1233int uvc_ctrl_resume_device(struct uvc_device *dev)
1234{
1235 struct uvc_control *ctrl;
1236 struct uvc_entity *entity;
1237 unsigned int i;
1238 int ret;
1239
1240 /* Walk the entities list and restore controls when possible. */
1241 list_for_each_entry(entity, &dev->entities, list) {
1242
1243 for (i = 0; i < entity->ncontrols; ++i) {
1244 ctrl = &entity->controls[i];
1245
1246 if (ctrl->info == NULL || !ctrl->modified ||
1247 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1248 continue;
1249
36bd883e
LP
1250 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1251 ctrl->info->entity, ctrl->info->index,
1252 ctrl->info->selector);
c0efd232
LP
1253 ctrl->dirty = 1;
1254 }
1255
1256 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1257 if (ret < 0)
1258 return ret;
1259 }
1260
1261 return 0;
1262}
1263
1264/* --------------------------------------------------------------------------
1265 * Control and mapping handling
1266 */
1267
1268static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1269 struct uvc_control_info *info)
1270{
1271 struct uvc_entity *entity;
1272 struct uvc_control *ctrl = NULL;
1273 int ret, found = 0;
1274 unsigned int i;
1275
1276 list_for_each_entry(entity, &dev->entities, list) {
1277 if (!uvc_entity_match_guid(entity, info->entity))
1278 continue;
1279
1280 for (i = 0; i < entity->ncontrols; ++i) {
1281 ctrl = &entity->controls[i];
1282 if (ctrl->index == info->index) {
1283 found = 1;
1284 break;
1285 }
1286 }
1287
1288 if (found)
1289 break;
1290 }
1291
1292 if (!found)
1293 return;
1294
b482d923 1295 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
c0efd232
LP
1296 /* Check if the device control information and length match
1297 * the user supplied information.
1298 */
1299 __u32 flags;
1300 __le16 size;
1301 __u8 inf;
1302
b482d923
LP
1303 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1304 dev->intfnum, info->selector, (__u8 *)&size, 2);
1305 if (ret < 0) {
36bd883e
LP
1306 uvc_trace(UVC_TRACE_CONTROL,
1307 "GET_LEN failed on control %pUl/%u (%d).\n",
1308 info->entity, info->selector, ret);
c0efd232
LP
1309 return;
1310 }
1311
1312 if (info->size != le16_to_cpu(size)) {
36bd883e
LP
1313 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1314 "doesn't match user supplied value.\n",
1315 info->entity, info->selector);
c0efd232
LP
1316 return;
1317 }
1318
b482d923
LP
1319 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1320 dev->intfnum, info->selector, &inf, 1);
1321 if (ret < 0) {
36bd883e
LP
1322 uvc_trace(UVC_TRACE_CONTROL,
1323 "GET_INFO failed on control %pUl/%u (%d).\n",
1324 info->entity, info->selector, ret);
c0efd232
LP
1325 return;
1326 }
1327
1328 flags = info->flags;
1329 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1330 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
36bd883e
LP
1331 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1332 "don't match supported operations.\n",
1333 info->entity, info->selector);
c0efd232
LP
1334 return;
1335 }
1336 }
1337
1338 ctrl->info = info;
59529081 1339 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
36bd883e
LP
1340 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1341 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1342 dev->udev->devpath, entity->id);
c0efd232
LP
1343}
1344
1345/*
1346 * Add an item to the UVC control information list, and instantiate a control
1347 * structure for each device that supports the control.
1348 */
1349int uvc_ctrl_add_info(struct uvc_control_info *info)
1350{
1351 struct uvc_control_info *ctrl;
1352 struct uvc_device *dev;
1353 int ret = 0;
1354
1355 /* Find matching controls by walking the devices, entities and
1356 * controls list.
1357 */
1358 mutex_lock(&uvc_driver.ctrl_mutex);
1359
1360 /* First check if the list contains a control matching the new one.
1361 * Bail out if it does.
1362 */
1363 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1364 if (memcmp(ctrl->entity, info->entity, 16))
1365 continue;
1366
1367 if (ctrl->selector == info->selector) {
36bd883e
LP
1368 uvc_trace(UVC_TRACE_CONTROL,
1369 "Control %pUl/%u is already defined.\n",
1370 info->entity, info->selector);
c0efd232
LP
1371 ret = -EEXIST;
1372 goto end;
1373 }
1374 if (ctrl->index == info->index) {
36bd883e
LP
1375 uvc_trace(UVC_TRACE_CONTROL,
1376 "Control %pUl/%u would overwrite index %d.\n",
1377 info->entity, info->selector, info->index);
c0efd232
LP
1378 ret = -EEXIST;
1379 goto end;
1380 }
1381 }
1382
1383 list_for_each_entry(dev, &uvc_driver.devices, list)
1384 uvc_ctrl_add_ctrl(dev, info);
1385
1386 INIT_LIST_HEAD(&info->mappings);
1387 list_add_tail(&info->list, &uvc_driver.controls);
1388end:
1389 mutex_unlock(&uvc_driver.ctrl_mutex);
1390 return ret;
1391}
1392
1393int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1394{
1395 struct uvc_control_info *info;
1396 struct uvc_control_mapping *map;
1397 int ret = -EINVAL;
1398
9768352a
LP
1399 if (mapping->get == NULL)
1400 mapping->get = uvc_get_le_value;
1401 if (mapping->set == NULL)
1402 mapping->set = uvc_set_le_value;
1403
c0efd232
LP
1404 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1405 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1406 "invalid control id 0x%08x\n", mapping->name,
1407 mapping->id);
1408 return -EINVAL;
1409 }
1410
1411 mutex_lock(&uvc_driver.ctrl_mutex);
1412 list_for_each_entry(info, &uvc_driver.controls, list) {
1413 if (memcmp(info->entity, mapping->entity, 16) ||
1414 info->selector != mapping->selector)
1415 continue;
1416
1417 if (info->size * 8 < mapping->size + mapping->offset) {
36bd883e
LP
1418 uvc_trace(UVC_TRACE_CONTROL,
1419 "Mapping '%s' would overflow control %pUl/%u\n",
1420 mapping->name, info->entity, info->selector);
c0efd232
LP
1421 ret = -EOVERFLOW;
1422 goto end;
1423 }
1424
1425 /* Check if the list contains a mapping matching the new one.
1426 * Bail out if it does.
1427 */
1428 list_for_each_entry(map, &info->mappings, list) {
1429 if (map->id == mapping->id) {
1430 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1431 "already defined.\n", mapping->name);
1432 ret = -EEXIST;
1433 goto end;
1434 }
1435 }
1436
1437 mapping->ctrl = info;
1438 list_add_tail(&mapping->list, &info->mappings);
36bd883e
LP
1439 uvc_trace(UVC_TRACE_CONTROL,
1440 "Adding mapping %s to control %pUl/%u.\n",
1441 mapping->name, info->entity, info->selector);
c0efd232
LP
1442
1443 ret = 0;
1444 break;
1445 }
1446end:
1447 mutex_unlock(&uvc_driver.ctrl_mutex);
1448 return ret;
1449}
1450
2bdd29cf 1451/*
d732c44c
LP
1452 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1453 * are currently the ones that crash the camera or unconditionally return an
1454 * error when queried.
2bdd29cf
LP
1455 */
1456static void
d732c44c 1457uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
2bdd29cf
LP
1458{
1459 static const struct {
d732c44c
LP
1460 struct usb_device_id id;
1461 u8 index;
2bdd29cf 1462 } blacklist[] = {
9405e3cb 1463 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
d732c44c
LP
1464 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1465 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2bdd29cf
LP
1466 };
1467
1468 u8 *controls;
1469 unsigned int size;
1470 unsigned int i;
1471
b482d923 1472 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
2bdd29cf
LP
1473 return;
1474
1475 controls = entity->processing.bmControls;
1476 size = entity->processing.bControlSize;
1477
1478 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
385097e0 1479 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2bdd29cf
LP
1480 continue;
1481
d732c44c
LP
1482 if (blacklist[i].index >= 8 * size ||
1483 !uvc_test_bit(controls, blacklist[i].index))
2bdd29cf
LP
1484 continue;
1485
d732c44c
LP
1486 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1487 "removing it.\n", entity->id, blacklist[i].index);
2bdd29cf 1488
d732c44c 1489 uvc_clear_bit(controls, blacklist[i].index);
2bdd29cf
LP
1490 }
1491}
1492
c0efd232
LP
1493/*
1494 * Initialize device controls.
1495 */
1496int uvc_ctrl_init_device(struct uvc_device *dev)
1497{
1498 struct uvc_control_info *info;
1499 struct uvc_control *ctrl;
1500 struct uvc_entity *entity;
1501 unsigned int i;
1502
1503 /* Walk the entities list and instantiate controls */
1504 list_for_each_entry(entity, &dev->entities, list) {
1505 unsigned int bControlSize = 0, ncontrols = 0;
1506 __u8 *bmControls = NULL;
1507
b482d923 1508 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
c0efd232
LP
1509 bmControls = entity->extension.bmControls;
1510 bControlSize = entity->extension.bControlSize;
b482d923 1511 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
c0efd232
LP
1512 bmControls = entity->processing.bmControls;
1513 bControlSize = entity->processing.bControlSize;
b482d923 1514 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
c0efd232
LP
1515 bmControls = entity->camera.bmControls;
1516 bControlSize = entity->camera.bControlSize;
1517 }
1518
d732c44c 1519 uvc_ctrl_prune_entity(dev, entity);
2bdd29cf 1520
c0efd232
LP
1521 for (i = 0; i < bControlSize; ++i)
1522 ncontrols += hweight8(bmControls[i]);
1523
1524 if (ncontrols == 0)
1525 continue;
1526
1527 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1528 if (entity->controls == NULL)
1529 return -ENOMEM;
1530
1531 entity->ncontrols = ncontrols;
1532
1533 ctrl = entity->controls;
1534 for (i = 0; i < bControlSize * 8; ++i) {
2bdd29cf 1535 if (uvc_test_bit(bmControls, i) == 0)
c0efd232
LP
1536 continue;
1537
1538 ctrl->entity = entity;
1539 ctrl->index = i;
1540 ctrl++;
1541 }
1542 }
1543
1544 /* Walk the controls info list and associate them with the device
1545 * controls, then add the device to the global device list. This has
1546 * to be done while holding the controls lock, to make sure
1547 * uvc_ctrl_add_info() will not get called in-between.
1548 */
1549 mutex_lock(&uvc_driver.ctrl_mutex);
1550 list_for_each_entry(info, &uvc_driver.controls, list)
1551 uvc_ctrl_add_ctrl(dev, info);
1552
1553 list_add_tail(&dev->list, &uvc_driver.devices);
1554 mutex_unlock(&uvc_driver.ctrl_mutex);
1555
1556 return 0;
1557}
1558
1559/*
1560 * Cleanup device controls.
1561 */
1562void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1563{
1564 struct uvc_entity *entity;
1565 unsigned int i;
1566
1567 /* Remove the device from the global devices list */
1568 mutex_lock(&uvc_driver.ctrl_mutex);
1569 if (dev->list.next != NULL)
1570 list_del(&dev->list);
1571 mutex_unlock(&uvc_driver.ctrl_mutex);
1572
1573 list_for_each_entry(entity, &dev->entities, list) {
1574 for (i = 0; i < entity->ncontrols; ++i)
1575 kfree(entity->controls[i].data);
1576
1577 kfree(entity->controls);
1578 }
1579}
1580
1581void uvc_ctrl_init(void)
1582{
1583 struct uvc_control_info *ctrl = uvc_ctrls;
1584 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1585 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1586 struct uvc_control_mapping *mend =
1587 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1588
1589 for (; ctrl < cend; ++ctrl)
1590 uvc_ctrl_add_info(ctrl);
1591
1592 for (; mapping < mend; ++mapping)
1593 uvc_ctrl_add_mapping(mapping);
1594}
f87086e3 1595