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