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