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