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