blob: 5d6d7bda233617039695d2776cda538705bc7904 [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_ctrl.c -- USB Video Class driver - Controls
3 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03004 * Copyright (C) 2005-2009
Laurent Pinchartc0efd232008-06-30 15:04:50 -03005 * Laurent Pinchart (laurent.pinchart@skynet.be)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030015#include <linux/list.h>
16#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030018#include <linux/uaccess.h>
19#include <linux/usb.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/wait.h>
23#include <asm/atomic.h>
24
25#include "uvcvideo.h"
26
Laurent Pinchartc0efd232008-06-30 15:04:50 -030027#define UVC_CTRL_DATA_CURRENT 0
28#define UVC_CTRL_DATA_BACKUP 1
Laurent Pinchart59529082010-01-23 06:30:20 -030029#define UVC_CTRL_DATA_MIN 2
30#define UVC_CTRL_DATA_MAX 3
31#define UVC_CTRL_DATA_RES 4
32#define UVC_CTRL_DATA_DEF 5
33#define UVC_CTRL_DATA_LAST 6
Laurent Pinchartc0efd232008-06-30 15:04:50 -030034
35/* ------------------------------------------------------------------------
Laurent Pinchart2c2d2642009-01-03 19:12:40 -030036 * Controls
Laurent Pinchartc0efd232008-06-30 15:04:50 -030037 */
38
39static struct uvc_control_info uvc_ctrls[] = {
40 {
41 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030042 .selector = UVC_PU_BRIGHTNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030043 .index = 0,
44 .size = 2,
45 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
46 | UVC_CONTROL_RESTORE,
47 },
48 {
49 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030050 .selector = UVC_PU_CONTRAST_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030051 .index = 1,
52 .size = 2,
53 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
54 | UVC_CONTROL_RESTORE,
55 },
56 {
57 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030058 .selector = UVC_PU_HUE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030059 .index = 2,
60 .size = 2,
61 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
62 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
63 },
64 {
65 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030066 .selector = UVC_PU_SATURATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030067 .index = 3,
68 .size = 2,
69 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
70 | UVC_CONTROL_RESTORE,
71 },
72 {
73 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030074 .selector = UVC_PU_SHARPNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030075 .index = 4,
76 .size = 2,
77 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
78 | UVC_CONTROL_RESTORE,
79 },
80 {
81 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030082 .selector = UVC_PU_GAMMA_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030083 .index = 5,
84 .size = 2,
85 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
86 | UVC_CONTROL_RESTORE,
87 },
88 {
89 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030090 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -030091 .index = 6,
92 .size = 2,
93 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
94 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
95 },
96 {
97 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030098 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -030099 .index = 7,
100 .size = 4,
101 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
102 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
103 },
104 {
105 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300106 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300107 .index = 8,
108 .size = 2,
109 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
110 | UVC_CONTROL_RESTORE,
111 },
112 {
113 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300114 .selector = UVC_PU_GAIN_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300115 .index = 9,
116 .size = 2,
117 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
118 | UVC_CONTROL_RESTORE,
119 },
120 {
121 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300122 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300123 .index = 10,
124 .size = 1,
125 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
126 | UVC_CONTROL_RESTORE,
127 },
128 {
129 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300130 .selector = UVC_PU_HUE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300131 .index = 11,
132 .size = 1,
133 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
134 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
135 },
136 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300137 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300138 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300139 .index = 12,
140 .size = 1,
141 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
142 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
143 },
144 {
145 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300146 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300147 .index = 13,
148 .size = 1,
149 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
150 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
151 },
152 {
153 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300154 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300155 .index = 14,
156 .size = 2,
157 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
158 | UVC_CONTROL_RESTORE,
159 },
160 {
161 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300162 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300163 .index = 15,
164 .size = 2,
165 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
166 | UVC_CONTROL_RESTORE,
167 },
168 {
169 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300170 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300171 .index = 16,
172 .size = 1,
173 .flags = UVC_CONTROL_GET_CUR,
174 },
175 {
176 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300177 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300178 .index = 17,
179 .size = 1,
180 .flags = UVC_CONTROL_GET_CUR,
181 },
182 {
183 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300184 .selector = UVC_CT_SCANNING_MODE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300185 .index = 0,
186 .size = 1,
187 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
188 | UVC_CONTROL_RESTORE,
189 },
190 {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300191 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300192 .selector = UVC_CT_AE_MODE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300193 .index = 1,
194 .size = 1,
195 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
196 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
197 | UVC_CONTROL_RESTORE,
198 },
199 {
200 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300201 .selector = UVC_CT_AE_PRIORITY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300202 .index = 2,
203 .size = 1,
204 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
205 | UVC_CONTROL_RESTORE,
206 },
207 {
208 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300209 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300210 .index = 3,
211 .size = 4,
212 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
213 | UVC_CONTROL_RESTORE,
214 },
215 {
216 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300217 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300218 .index = 4,
219 .size = 1,
220 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
221 | UVC_CONTROL_RESTORE,
222 },
223 {
224 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300225 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300226 .index = 5,
227 .size = 2,
228 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
229 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
230 },
231 {
232 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300233 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300234 .index = 6,
235 .size = 2,
236 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
237 | UVC_CONTROL_AUTO_UPDATE,
238 },
239 {
240 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300241 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300242 .index = 7,
243 .size = 2,
244 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
245 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
246 },
247 {
248 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300249 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300250 .index = 8,
251 .size = 1,
252 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
253 | UVC_CONTROL_AUTO_UPDATE,
254 },
255 {
256 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300257 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300258 .index = 9,
259 .size = 2,
260 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
261 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
262 },
263 {
264 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300265 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300266 .index = 10,
267 .size = 3,
268 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
269 | UVC_CONTROL_AUTO_UPDATE,
270 },
271 {
272 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300273 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300274 .index = 11,
275 .size = 8,
276 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
277 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
278 },
279 {
280 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300281 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300282 .index = 12,
283 .size = 4,
284 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
285 | UVC_CONTROL_AUTO_UPDATE,
286 },
287 {
288 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300289 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300290 .index = 13,
291 .size = 2,
292 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
293 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
294 },
295 {
296 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300297 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300298 .index = 14,
299 .size = 2,
300 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
301 | UVC_CONTROL_AUTO_UPDATE,
302 },
303 {
304 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300305 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300306 .index = 17,
307 .size = 1,
308 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
309 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
310 },
311 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300312 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300313 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300314 .index = 18,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300315 .size = 1,
316 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300317 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
318 },
319};
320
321static struct uvc_menu_info power_line_frequency_controls[] = {
322 { 0, "Disabled" },
323 { 1, "50 Hz" },
324 { 2, "60 Hz" },
325};
326
327static struct uvc_menu_info exposure_auto_controls[] = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300328 { 2, "Auto Mode" },
Laurent Pinchart90ac5ea2008-07-26 11:42:29 -0300329 { 1, "Manual Mode" },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300330 { 4, "Shutter Priority Mode" },
331 { 8, "Aperture Priority Mode" },
332};
333
Laurent Pinchart97683522008-12-16 06:46:32 -0300334static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
335 __u8 query, const __u8 *data)
336{
337 __s8 zoom = (__s8)data[0];
338
339 switch (query) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300340 case UVC_GET_CUR:
Laurent Pinchart97683522008-12-16 06:46:32 -0300341 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
342
Laurent Pinchartb482d922009-06-26 11:39:42 -0300343 case UVC_GET_MIN:
344 case UVC_GET_MAX:
345 case UVC_GET_RES:
346 case UVC_GET_DEF:
Laurent Pinchart97683522008-12-16 06:46:32 -0300347 default:
348 return data[2];
349 }
350}
351
352static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
353 __s32 value, __u8 *data)
354{
355 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
Laurent Pinchart2e896132009-11-04 13:11:10 -0300356 data[2] = min((int)abs(value), 0xff);
Laurent Pinchart97683522008-12-16 06:46:32 -0300357}
358
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300359static struct uvc_control_mapping uvc_ctrl_mappings[] = {
360 {
361 .id = V4L2_CID_BRIGHTNESS,
362 .name = "Brightness",
363 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300364 .selector = UVC_PU_BRIGHTNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300365 .size = 16,
366 .offset = 0,
367 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
368 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
369 },
370 {
371 .id = V4L2_CID_CONTRAST,
372 .name = "Contrast",
373 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300374 .selector = UVC_PU_CONTRAST_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300375 .size = 16,
376 .offset = 0,
377 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
378 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
379 },
380 {
381 .id = V4L2_CID_HUE,
382 .name = "Hue",
383 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300384 .selector = UVC_PU_HUE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300385 .size = 16,
386 .offset = 0,
387 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
388 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
389 },
390 {
391 .id = V4L2_CID_SATURATION,
392 .name = "Saturation",
393 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300394 .selector = UVC_PU_SATURATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300395 .size = 16,
396 .offset = 0,
397 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
398 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
399 },
400 {
401 .id = V4L2_CID_SHARPNESS,
402 .name = "Sharpness",
403 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300404 .selector = UVC_PU_SHARPNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300405 .size = 16,
406 .offset = 0,
407 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
408 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
409 },
410 {
411 .id = V4L2_CID_GAMMA,
412 .name = "Gamma",
413 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300414 .selector = UVC_PU_GAMMA_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300415 .size = 16,
416 .offset = 0,
417 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
418 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
419 },
420 {
421 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
422 .name = "Backlight Compensation",
423 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300424 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300425 .size = 16,
426 .offset = 0,
427 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
428 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
429 },
430 {
431 .id = V4L2_CID_GAIN,
432 .name = "Gain",
433 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300434 .selector = UVC_PU_GAIN_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300435 .size = 16,
436 .offset = 0,
437 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
438 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
439 },
440 {
441 .id = V4L2_CID_POWER_LINE_FREQUENCY,
442 .name = "Power Line Frequency",
443 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300444 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300445 .size = 2,
446 .offset = 0,
447 .v4l2_type = V4L2_CTRL_TYPE_MENU,
448 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
449 .menu_info = power_line_frequency_controls,
450 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
451 },
452 {
453 .id = V4L2_CID_HUE_AUTO,
454 .name = "Hue, Auto",
455 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300456 .selector = UVC_PU_HUE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300457 .size = 1,
458 .offset = 0,
459 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
460 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
461 },
462 {
463 .id = V4L2_CID_EXPOSURE_AUTO,
464 .name = "Exposure, Auto",
465 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300466 .selector = UVC_CT_AE_MODE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300467 .size = 4,
468 .offset = 0,
469 .v4l2_type = V4L2_CTRL_TYPE_MENU,
470 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
471 .menu_info = exposure_auto_controls,
472 .menu_count = ARRAY_SIZE(exposure_auto_controls),
473 },
474 {
475 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
476 .name = "Exposure, Auto Priority",
477 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300478 .selector = UVC_CT_AE_PRIORITY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300479 .size = 1,
480 .offset = 0,
481 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
482 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
483 },
484 {
485 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
486 .name = "Exposure (Absolute)",
487 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300488 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300489 .size = 32,
490 .offset = 0,
491 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
492 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
493 },
494 {
495 .id = V4L2_CID_AUTO_WHITE_BALANCE,
496 .name = "White Balance Temperature, Auto",
497 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300498 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300499 .size = 1,
500 .offset = 0,
501 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
502 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
503 },
504 {
505 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
506 .name = "White Balance Temperature",
507 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300508 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300509 .size = 16,
510 .offset = 0,
511 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
512 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
513 },
514 {
515 .id = V4L2_CID_AUTO_WHITE_BALANCE,
516 .name = "White Balance Component, Auto",
517 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300518 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300519 .size = 1,
520 .offset = 0,
521 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
522 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
523 },
524 {
525 .id = V4L2_CID_BLUE_BALANCE,
526 .name = "White Balance Blue Component",
527 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300528 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300529 .size = 16,
530 .offset = 0,
531 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
532 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
533 },
534 {
535 .id = V4L2_CID_RED_BALANCE,
536 .name = "White Balance Red Component",
537 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300538 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300539 .size = 16,
540 .offset = 16,
541 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
542 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
543 },
544 {
545 .id = V4L2_CID_FOCUS_ABSOLUTE,
546 .name = "Focus (absolute)",
547 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300548 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300549 .size = 16,
550 .offset = 0,
551 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
552 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
553 },
554 {
555 .id = V4L2_CID_FOCUS_AUTO,
556 .name = "Focus, Auto",
557 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300558 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300559 .size = 1,
560 .offset = 0,
561 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
562 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
563 },
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300564 {
Laurent Pinchart35a00c42010-01-20 12:13:46 -0300565 .id = V4L2_CID_IRIS_ABSOLUTE,
566 .name = "Iris, Absolute",
567 .entity = UVC_GUID_UVC_CAMERA,
568 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
569 .size = 16,
570 .offset = 0,
571 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
572 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
573 },
574 {
575 .id = V4L2_CID_IRIS_RELATIVE,
576 .name = "Iris, Relative",
577 .entity = UVC_GUID_UVC_CAMERA,
578 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
579 .size = 8,
580 .offset = 0,
581 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
582 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
583 },
584 {
Laurent Pinchart97683522008-12-16 06:46:32 -0300585 .id = V4L2_CID_ZOOM_ABSOLUTE,
586 .name = "Zoom, Absolute",
587 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300588 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300589 .size = 16,
590 .offset = 0,
591 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
592 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
593 },
594 {
595 .id = V4L2_CID_ZOOM_CONTINUOUS,
596 .name = "Zoom, Continuous",
597 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300598 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300599 .size = 0,
600 .offset = 0,
601 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
602 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
603 .get = uvc_ctrl_get_zoom,
604 .set = uvc_ctrl_set_zoom,
605 },
606 {
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300607 .id = V4L2_CID_PRIVACY,
608 .name = "Privacy",
609 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300610 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300611 .size = 1,
612 .offset = 0,
613 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
614 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
615 },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300616};
617
618/* ------------------------------------------------------------------------
619 * Utility functions
620 */
621
622static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
623{
624 return ctrl->data + id * ctrl->info->size;
625}
626
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300627static inline int uvc_test_bit(const __u8 *data, int bit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300628{
629 return (data[bit >> 3] >> (bit & 7)) & 1;
630}
631
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300632static inline void uvc_clear_bit(__u8 *data, int bit)
633{
634 data[bit >> 3] &= ~(1 << (bit & 7));
635}
636
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300637/* Extract the bit string specified by mapping->offset and mapping->size
638 * from the little-endian data stored at 'data' and return the result as
639 * a signed 32bit integer. Sign extension will be performed if the mapping
640 * references a signed data type.
641 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300642static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
643 __u8 query, const __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300644{
645 int bits = mapping->size;
646 int offset = mapping->offset;
647 __s32 value = 0;
648 __u8 mask;
649
650 data += offset / 8;
651 offset &= 7;
652 mask = ((1LL << bits) - 1) << offset;
653
654 for (; bits > 0; data++) {
655 __u8 byte = *data & mask;
656 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
657 bits -= 8 - (offset > 0 ? offset : 0);
658 offset -= 8;
659 mask = (1 << bits) - 1;
660 }
661
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300662 /* Sign-extend the value if needed. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300663 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
664 value |= -(value & (1 << (mapping->size - 1)));
665
666 return value;
667}
668
669/* Set the bit string specified by mapping->offset and mapping->size
670 * in the little-endian data stored at 'data' to the value 'value'.
671 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300672static void uvc_set_le_value(struct uvc_control_mapping *mapping,
673 __s32 value, __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300674{
675 int bits = mapping->size;
676 int offset = mapping->offset;
677 __u8 mask;
678
679 data += offset / 8;
680 offset &= 7;
681
682 for (; bits > 0; data++) {
683 mask = ((1LL << bits) - 1) << offset;
684 *data = (*data & ~mask) | ((value << offset) & mask);
685 value >>= offset ? offset : 8;
686 bits -= 8 - offset;
687 offset = 0;
688 }
689}
690
691/* ------------------------------------------------------------------------
692 * Terminal and unit management
693 */
694
695static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
696static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
697static const __u8 uvc_media_transport_input_guid[16] =
698 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
699
700static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
701{
702 switch (UVC_ENTITY_TYPE(entity)) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300703 case UVC_ITT_CAMERA:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300704 return memcmp(uvc_camera_guid, guid, 16) == 0;
705
Laurent Pinchartb482d922009-06-26 11:39:42 -0300706 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300707 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
708
Laurent Pinchartb482d922009-06-26 11:39:42 -0300709 case UVC_VC_PROCESSING_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300710 return memcmp(uvc_processing_guid, guid, 16) == 0;
711
Laurent Pinchartb482d922009-06-26 11:39:42 -0300712 case UVC_VC_EXTENSION_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300713 return memcmp(entity->extension.guidExtensionCode,
714 guid, 16) == 0;
715
716 default:
717 return 0;
718 }
719}
720
721/* ------------------------------------------------------------------------
722 * UVC Controls
723 */
724
725static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
726 struct uvc_control_mapping **mapping, struct uvc_control **control,
727 int next)
728{
729 struct uvc_control *ctrl;
730 struct uvc_control_mapping *map;
731 unsigned int i;
732
733 if (entity == NULL)
734 return;
735
736 for (i = 0; i < entity->ncontrols; ++i) {
737 ctrl = &entity->controls[i];
738 if (ctrl->info == NULL)
739 continue;
740
741 list_for_each_entry(map, &ctrl->info->mappings, list) {
742 if ((map->id == v4l2_id) && !next) {
743 *control = ctrl;
744 *mapping = map;
745 return;
746 }
747
748 if ((*mapping == NULL || (*mapping)->id > map->id) &&
749 (map->id > v4l2_id) && next) {
750 *control = ctrl;
751 *mapping = map;
752 }
753 }
754 }
755}
756
Laurent Pinchart8e113592009-07-01 20:24:47 -0300757struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300758 __u32 v4l2_id, struct uvc_control_mapping **mapping)
759{
760 struct uvc_control *ctrl = NULL;
761 struct uvc_entity *entity;
762 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
763
764 *mapping = NULL;
765
766 /* Mask the query flags. */
767 v4l2_id &= V4L2_CTRL_ID_MASK;
768
769 /* Find the control. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300770 list_for_each_entry(entity, &chain->entities, chain) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300771 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
772 if (ctrl && !next)
773 return ctrl;
774 }
775
776 if (ctrl == NULL && !next)
777 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
778 v4l2_id);
779
780 return ctrl;
781}
782
Laurent Pinchart59529082010-01-23 06:30:20 -0300783static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
784 struct uvc_control *ctrl)
785{
786 int ret;
787
788 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
789 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
790 chain->dev->intfnum, ctrl->info->selector,
791 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
792 ctrl->info->size);
793 if (ret < 0)
794 return ret;
795 }
796
797 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
798 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
799 chain->dev->intfnum, ctrl->info->selector,
800 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
801 ctrl->info->size);
802 if (ret < 0)
803 return ret;
804 }
805 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
806 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
807 chain->dev->intfnum, ctrl->info->selector,
808 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
809 ctrl->info->size);
810 if (ret < 0)
811 return ret;
812 }
813 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
814 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
815 chain->dev->intfnum, ctrl->info->selector,
816 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
817 ctrl->info->size);
818 if (ret < 0)
819 return ret;
820 }
821
822 ctrl->cached = 1;
823 return 0;
824}
825
Laurent Pinchart8e113592009-07-01 20:24:47 -0300826int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300827 struct v4l2_queryctrl *v4l2_ctrl)
828{
829 struct uvc_control *ctrl;
830 struct uvc_control_mapping *mapping;
831 struct uvc_menu_info *menu;
832 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300833 int ret;
834
Laurent Pinchart8e113592009-07-01 20:24:47 -0300835 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300836 if (ctrl == NULL)
837 return -EINVAL;
838
Laurent Pinchart54812c72008-07-17 07:37:37 -0300839 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300840 v4l2_ctrl->id = mapping->id;
841 v4l2_ctrl->type = mapping->v4l2_type;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300842 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300843 v4l2_ctrl->flags = 0;
844
845 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
846 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
847
Laurent Pinchart59529082010-01-23 06:30:20 -0300848 if (!ctrl->cached) {
849 ret = uvc_ctrl_populate_cache(chain, ctrl);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300850 if (ret < 0)
Laurent Pinchart59529082010-01-23 06:30:20 -0300851 return ret;
852 }
853
854 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
855 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
856 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300857 }
858
Laurent Pinchart54812c72008-07-17 07:37:37 -0300859 switch (mapping->v4l2_type) {
860 case V4L2_CTRL_TYPE_MENU:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300861 v4l2_ctrl->minimum = 0;
862 v4l2_ctrl->maximum = mapping->menu_count - 1;
863 v4l2_ctrl->step = 1;
864
865 menu = mapping->menu_info;
866 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
867 if (menu->value == v4l2_ctrl->default_value) {
868 v4l2_ctrl->default_value = i;
869 break;
870 }
871 }
872
Laurent Pinchart59529082010-01-23 06:30:20 -0300873 return 0;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300874
875 case V4L2_CTRL_TYPE_BOOLEAN:
876 v4l2_ctrl->minimum = 0;
877 v4l2_ctrl->maximum = 1;
878 v4l2_ctrl->step = 1;
Laurent Pinchart59529082010-01-23 06:30:20 -0300879 return 0;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300880
Laurent Pinchartf4eabaf2009-08-06 06:05:40 -0300881 case V4L2_CTRL_TYPE_BUTTON:
882 v4l2_ctrl->minimum = 0;
883 v4l2_ctrl->maximum = 0;
884 v4l2_ctrl->step = 0;
Laurent Pinchart59529082010-01-23 06:30:20 -0300885 return 0;
Laurent Pinchartf4eabaf2009-08-06 06:05:40 -0300886
Laurent Pinchart54812c72008-07-17 07:37:37 -0300887 default:
888 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300889 }
890
Laurent Pinchart59529082010-01-23 06:30:20 -0300891 if (ctrl->info->flags & UVC_CONTROL_GET_MIN)
892 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
893 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300894
Laurent Pinchart59529082010-01-23 06:30:20 -0300895 if (ctrl->info->flags & UVC_CONTROL_GET_MAX)
896 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
897 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
898
899 if (ctrl->info->flags & UVC_CONTROL_GET_RES)
900 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
901 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
902
903 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300904}
905
906
907/* --------------------------------------------------------------------------
908 * Control transactions
909 *
910 * To make extended set operations as atomic as the hardware allows, controls
911 * are handled using begin/commit/rollback operations.
912 *
913 * At the beginning of a set request, uvc_ctrl_begin should be called to
914 * initialize the request. This function acquires the control lock.
915 *
916 * When setting a control, the new value is stored in the control data field
917 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
918 * later processing. If the UVC and V4L2 control sizes differ, the current
919 * value is loaded from the hardware before storing the new value in the data
920 * field.
921 *
922 * After processing all controls in the transaction, uvc_ctrl_commit or
923 * uvc_ctrl_rollback must be called to apply the pending changes to the
924 * hardware or revert them. When applying changes, all controls marked as
925 * dirty will be modified in the UVC device, and the dirty flag will be
926 * cleared. When reverting controls, the control data field
927 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
928 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
929 * control lock.
930 */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300931int uvc_ctrl_begin(struct uvc_video_chain *chain)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300932{
Laurent Pinchart8e113592009-07-01 20:24:47 -0300933 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300934}
935
936static int uvc_ctrl_commit_entity(struct uvc_device *dev,
937 struct uvc_entity *entity, int rollback)
938{
939 struct uvc_control *ctrl;
940 unsigned int i;
941 int ret;
942
943 if (entity == NULL)
944 return 0;
945
946 for (i = 0; i < entity->ncontrols; ++i) {
947 ctrl = &entity->controls[i];
Laurent Pinchartb1accfa2008-09-27 20:54:02 -0300948 if (ctrl->info == NULL)
949 continue;
950
951 /* Reset the loaded flag for auto-update controls that were
952 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
953 * uvc_ctrl_get from using the cached value.
954 */
955 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
956 ctrl->loaded = 0;
957
958 if (!ctrl->dirty)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300959 continue;
960
961 if (!rollback)
Laurent Pinchartb482d922009-06-26 11:39:42 -0300962 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300963 dev->intfnum, ctrl->info->selector,
964 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
965 ctrl->info->size);
966 else
967 ret = 0;
968
969 if (rollback || ret < 0)
970 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
971 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
972 ctrl->info->size);
973
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300974 ctrl->dirty = 0;
975
976 if (ret < 0)
977 return ret;
978 }
979
980 return 0;
981}
982
Laurent Pinchart8e113592009-07-01 20:24:47 -0300983int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300984{
985 struct uvc_entity *entity;
986 int ret = 0;
987
988 /* Find the control. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300989 list_for_each_entry(entity, &chain->entities, chain) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300990 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300991 if (ret < 0)
992 goto done;
993 }
994
995done:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300996 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300997 return ret;
998}
999
Laurent Pinchart8e113592009-07-01 20:24:47 -03001000int uvc_ctrl_get(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001001 struct v4l2_ext_control *xctrl)
1002{
1003 struct uvc_control *ctrl;
1004 struct uvc_control_mapping *mapping;
1005 struct uvc_menu_info *menu;
1006 unsigned int i;
1007 int ret;
1008
Laurent Pinchart8e113592009-07-01 20:24:47 -03001009 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001010 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
1011 return -EINVAL;
1012
1013 if (!ctrl->loaded) {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001014 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1015 chain->dev->intfnum, ctrl->info->selector,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001016 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1017 ctrl->info->size);
1018 if (ret < 0)
1019 return ret;
1020
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001021 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001022 }
1023
Laurent Pinchartb482d922009-06-26 11:39:42 -03001024 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
Laurent Pinchart97683522008-12-16 06:46:32 -03001025 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001026
1027 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1028 menu = mapping->menu_info;
1029 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1030 if (menu->value == xctrl->value) {
1031 xctrl->value = i;
1032 break;
1033 }
1034 }
1035 }
1036
1037 return 0;
1038}
1039
Laurent Pinchart8e113592009-07-01 20:24:47 -03001040int uvc_ctrl_set(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001041 struct v4l2_ext_control *xctrl)
1042{
1043 struct uvc_control *ctrl;
1044 struct uvc_control_mapping *mapping;
Laurent Pincharte54532e2010-01-23 07:07:53 -03001045 s32 value;
1046 u32 step;
1047 s32 min;
1048 s32 max;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001049 int ret;
1050
Laurent Pinchart8e113592009-07-01 20:24:47 -03001051 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001052 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1053 return -EINVAL;
1054
Laurent Pincharte54532e2010-01-23 07:07:53 -03001055 /* Clamp out of range values. */
1056 switch (mapping->v4l2_type) {
1057 case V4L2_CTRL_TYPE_INTEGER:
1058 if (!ctrl->cached) {
1059 ret = uvc_ctrl_populate_cache(chain, ctrl);
1060 if (ret < 0)
1061 return ret;
1062 }
1063
1064 min = mapping->get(mapping, UVC_GET_MIN,
1065 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1066 max = mapping->get(mapping, UVC_GET_MAX,
1067 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1068 step = mapping->get(mapping, UVC_GET_RES,
1069 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1070
1071 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1072 xctrl->value = clamp(xctrl->value, min, max);
1073 value = xctrl->value;
1074 break;
1075
1076 case V4L2_CTRL_TYPE_BOOLEAN:
1077 xctrl->value = clamp(xctrl->value, 0, 1);
1078 value = xctrl->value;
1079 break;
1080
1081 case V4L2_CTRL_TYPE_MENU:
1082 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
Laurent Pinchart8a4e76c2010-01-21 16:53:11 -03001083 return -ERANGE;
Laurent Pincharte54532e2010-01-23 07:07:53 -03001084 value = mapping->menu_info[xctrl->value].value;
1085 break;
1086
1087 default:
1088 value = xctrl->value;
1089 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001090 }
1091
Laurent Pincharte54532e2010-01-23 07:07:53 -03001092 /* If the mapping doesn't span the whole UVC control, the current value
1093 * needs to be loaded from the device to perform the read-modify-write
1094 * operation.
1095 */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001096 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1097 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1098 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1099 0, ctrl->info->size);
1100 } else {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001101 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1102 ctrl->entity->id, chain->dev->intfnum,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001103 ctrl->info->selector,
1104 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1105 ctrl->info->size);
1106 if (ret < 0)
1107 return ret;
1108 }
1109
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001110 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001111 }
1112
Laurent Pincharte54532e2010-01-23 07:07:53 -03001113 /* Backup the current value in case we need to rollback later. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001114 if (!ctrl->dirty) {
1115 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1116 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1117 ctrl->info->size);
1118 }
1119
Laurent Pinchart97683522008-12-16 06:46:32 -03001120 mapping->set(mapping, value,
1121 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001122
1123 ctrl->dirty = 1;
1124 ctrl->modified = 1;
1125 return 0;
1126}
1127
1128/* --------------------------------------------------------------------------
1129 * Dynamic controls
1130 */
1131
Laurent Pinchart8e113592009-07-01 20:24:47 -03001132int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001133 struct uvc_xu_control *xctrl, int set)
1134{
1135 struct uvc_entity *entity;
1136 struct uvc_control *ctrl = NULL;
1137 unsigned int i, found = 0;
1138 __u8 *data;
1139 int ret;
1140
1141 /* Find the extension unit. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -03001142 list_for_each_entry(entity, &chain->entities, chain) {
1143 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1144 entity->id == xctrl->unit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001145 break;
1146 }
1147
1148 if (entity->id != xctrl->unit) {
1149 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1150 xctrl->unit);
1151 return -EINVAL;
1152 }
1153
1154 /* Find the control. */
1155 for (i = 0; i < entity->ncontrols; ++i) {
1156 ctrl = &entity->controls[i];
1157 if (ctrl->info == NULL)
1158 continue;
1159
1160 if (ctrl->info->selector == xctrl->selector) {
1161 found = 1;
1162 break;
1163 }
1164 }
1165
1166 if (!found) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001167 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1168 entity->extension.guidExtensionCode, xctrl->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001169 return -EINVAL;
1170 }
1171
1172 /* Validate control data size. */
1173 if (ctrl->info->size != xctrl->size)
1174 return -EINVAL;
1175
1176 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1177 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1178 return -EINVAL;
1179
Laurent Pinchart8e113592009-07-01 20:24:47 -03001180 if (mutex_lock_interruptible(&chain->ctrl_mutex))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001181 return -ERESTARTSYS;
1182
1183 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1184 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1185 xctrl->size);
1186 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1187
1188 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1189 ret = -EFAULT;
1190 goto out;
1191 }
1192
Laurent Pinchart8e113592009-07-01 20:24:47 -03001193 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1194 xctrl->unit, chain->dev->intfnum, xctrl->selector,
Laurent Pinchartb482d922009-06-26 11:39:42 -03001195 data, xctrl->size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001196 if (ret < 0)
1197 goto out;
1198
1199 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1200 ret = -EFAULT;
1201 goto out;
1202 }
1203
1204out:
1205 if (ret)
1206 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1207 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1208 xctrl->size);
1209
Laurent Pinchart8e113592009-07-01 20:24:47 -03001210 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001211 return ret;
1212}
1213
1214/* --------------------------------------------------------------------------
1215 * Suspend/resume
1216 */
1217
1218/*
1219 * Restore control values after resume, skipping controls that haven't been
1220 * changed.
1221 *
1222 * TODO
1223 * - Don't restore modified controls that are back to their default value.
1224 * - Handle restore order (Auto-Exposure Mode should be restored before
1225 * Exposure Time).
1226 */
1227int uvc_ctrl_resume_device(struct uvc_device *dev)
1228{
1229 struct uvc_control *ctrl;
1230 struct uvc_entity *entity;
1231 unsigned int i;
1232 int ret;
1233
1234 /* Walk the entities list and restore controls when possible. */
1235 list_for_each_entry(entity, &dev->entities, list) {
1236
1237 for (i = 0; i < entity->ncontrols; ++i) {
1238 ctrl = &entity->controls[i];
1239
1240 if (ctrl->info == NULL || !ctrl->modified ||
1241 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1242 continue;
1243
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001244 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1245 ctrl->info->entity, ctrl->info->index,
1246 ctrl->info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001247 ctrl->dirty = 1;
1248 }
1249
1250 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1251 if (ret < 0)
1252 return ret;
1253 }
1254
1255 return 0;
1256}
1257
1258/* --------------------------------------------------------------------------
1259 * Control and mapping handling
1260 */
1261
1262static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1263 struct uvc_control_info *info)
1264{
1265 struct uvc_entity *entity;
1266 struct uvc_control *ctrl = NULL;
1267 int ret, found = 0;
1268 unsigned int i;
1269
1270 list_for_each_entry(entity, &dev->entities, list) {
1271 if (!uvc_entity_match_guid(entity, info->entity))
1272 continue;
1273
1274 for (i = 0; i < entity->ncontrols; ++i) {
1275 ctrl = &entity->controls[i];
1276 if (ctrl->index == info->index) {
1277 found = 1;
1278 break;
1279 }
1280 }
1281
1282 if (found)
1283 break;
1284 }
1285
1286 if (!found)
1287 return;
1288
Laurent Pinchartb482d922009-06-26 11:39:42 -03001289 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001290 /* Check if the device control information and length match
1291 * the user supplied information.
1292 */
1293 __u32 flags;
1294 __le16 size;
1295 __u8 inf;
1296
Laurent Pinchartb482d922009-06-26 11:39:42 -03001297 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1298 dev->intfnum, info->selector, (__u8 *)&size, 2);
1299 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001300 uvc_trace(UVC_TRACE_CONTROL,
1301 "GET_LEN failed on control %pUl/%u (%d).\n",
1302 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001303 return;
1304 }
1305
1306 if (info->size != le16_to_cpu(size)) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001307 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1308 "doesn't match user supplied value.\n",
1309 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001310 return;
1311 }
1312
Laurent Pinchartb482d922009-06-26 11:39:42 -03001313 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1314 dev->intfnum, info->selector, &inf, 1);
1315 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001316 uvc_trace(UVC_TRACE_CONTROL,
1317 "GET_INFO failed on control %pUl/%u (%d).\n",
1318 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001319 return;
1320 }
1321
1322 flags = info->flags;
1323 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1324 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001325 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1326 "don't match supported operations.\n",
1327 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001328 return;
1329 }
1330 }
1331
1332 ctrl->info = info;
Laurent Pinchart59529082010-01-23 06:30:20 -03001333 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001334 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1335 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1336 dev->udev->devpath, entity->id);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001337}
1338
1339/*
1340 * Add an item to the UVC control information list, and instantiate a control
1341 * structure for each device that supports the control.
1342 */
1343int uvc_ctrl_add_info(struct uvc_control_info *info)
1344{
1345 struct uvc_control_info *ctrl;
1346 struct uvc_device *dev;
1347 int ret = 0;
1348
1349 /* Find matching controls by walking the devices, entities and
1350 * controls list.
1351 */
1352 mutex_lock(&uvc_driver.ctrl_mutex);
1353
1354 /* First check if the list contains a control matching the new one.
1355 * Bail out if it does.
1356 */
1357 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1358 if (memcmp(ctrl->entity, info->entity, 16))
1359 continue;
1360
1361 if (ctrl->selector == info->selector) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001362 uvc_trace(UVC_TRACE_CONTROL,
1363 "Control %pUl/%u is already defined.\n",
1364 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001365 ret = -EEXIST;
1366 goto end;
1367 }
1368 if (ctrl->index == info->index) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001369 uvc_trace(UVC_TRACE_CONTROL,
1370 "Control %pUl/%u would overwrite index %d.\n",
1371 info->entity, info->selector, info->index);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001372 ret = -EEXIST;
1373 goto end;
1374 }
1375 }
1376
1377 list_for_each_entry(dev, &uvc_driver.devices, list)
1378 uvc_ctrl_add_ctrl(dev, info);
1379
1380 INIT_LIST_HEAD(&info->mappings);
1381 list_add_tail(&info->list, &uvc_driver.controls);
1382end:
1383 mutex_unlock(&uvc_driver.ctrl_mutex);
1384 return ret;
1385}
1386
1387int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1388{
1389 struct uvc_control_info *info;
1390 struct uvc_control_mapping *map;
1391 int ret = -EINVAL;
1392
Laurent Pinchart97683522008-12-16 06:46:32 -03001393 if (mapping->get == NULL)
1394 mapping->get = uvc_get_le_value;
1395 if (mapping->set == NULL)
1396 mapping->set = uvc_set_le_value;
1397
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001398 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1399 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1400 "invalid control id 0x%08x\n", mapping->name,
1401 mapping->id);
1402 return -EINVAL;
1403 }
1404
1405 mutex_lock(&uvc_driver.ctrl_mutex);
1406 list_for_each_entry(info, &uvc_driver.controls, list) {
1407 if (memcmp(info->entity, mapping->entity, 16) ||
1408 info->selector != mapping->selector)
1409 continue;
1410
1411 if (info->size * 8 < mapping->size + mapping->offset) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001412 uvc_trace(UVC_TRACE_CONTROL,
1413 "Mapping '%s' would overflow control %pUl/%u\n",
1414 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001415 ret = -EOVERFLOW;
1416 goto end;
1417 }
1418
1419 /* Check if the list contains a mapping matching the new one.
1420 * Bail out if it does.
1421 */
1422 list_for_each_entry(map, &info->mappings, list) {
1423 if (map->id == mapping->id) {
1424 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1425 "already defined.\n", mapping->name);
1426 ret = -EEXIST;
1427 goto end;
1428 }
1429 }
1430
1431 mapping->ctrl = info;
1432 list_add_tail(&mapping->list, &info->mappings);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001433 uvc_trace(UVC_TRACE_CONTROL,
1434 "Adding mapping %s to control %pUl/%u.\n",
1435 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001436
1437 ret = 0;
1438 break;
1439 }
1440end:
1441 mutex_unlock(&uvc_driver.ctrl_mutex);
1442 return ret;
1443}
1444
1445/*
Laurent Pinchartd732c442009-05-31 17:05:55 -03001446 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1447 * are currently the ones that crash the camera or unconditionally return an
1448 * error when queried.
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001449 */
1450static void
Laurent Pinchartd732c442009-05-31 17:05:55 -03001451uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001452{
1453 static const struct {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001454 struct usb_device_id id;
1455 u8 index;
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001456 } blacklist[] = {
Laurent Pinchart9405e3c2010-02-03 06:49:35 -03001457 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
Laurent Pinchartd732c442009-05-31 17:05:55 -03001458 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1459 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001460 };
1461
1462 u8 *controls;
1463 unsigned int size;
1464 unsigned int i;
1465
Laurent Pinchartb482d922009-06-26 11:39:42 -03001466 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001467 return;
1468
1469 controls = entity->processing.bmControls;
1470 size = entity->processing.bControlSize;
1471
1472 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
Laurent Pinchart385097e2009-12-09 22:31:21 -03001473 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001474 continue;
1475
Laurent Pinchartd732c442009-05-31 17:05:55 -03001476 if (blacklist[i].index >= 8 * size ||
1477 !uvc_test_bit(controls, blacklist[i].index))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001478 continue;
1479
Laurent Pinchartd732c442009-05-31 17:05:55 -03001480 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1481 "removing it.\n", entity->id, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001482
Laurent Pinchartd732c442009-05-31 17:05:55 -03001483 uvc_clear_bit(controls, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001484 }
1485}
1486
1487/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001488 * Initialize device controls.
1489 */
1490int uvc_ctrl_init_device(struct uvc_device *dev)
1491{
1492 struct uvc_control_info *info;
1493 struct uvc_control *ctrl;
1494 struct uvc_entity *entity;
1495 unsigned int i;
1496
1497 /* Walk the entities list and instantiate controls */
1498 list_for_each_entry(entity, &dev->entities, list) {
1499 unsigned int bControlSize = 0, ncontrols = 0;
1500 __u8 *bmControls = NULL;
1501
Laurent Pinchartb482d922009-06-26 11:39:42 -03001502 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001503 bmControls = entity->extension.bmControls;
1504 bControlSize = entity->extension.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001505 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001506 bmControls = entity->processing.bmControls;
1507 bControlSize = entity->processing.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001508 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001509 bmControls = entity->camera.bmControls;
1510 bControlSize = entity->camera.bControlSize;
1511 }
1512
Laurent Pinchartd732c442009-05-31 17:05:55 -03001513 uvc_ctrl_prune_entity(dev, entity);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001514
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001515 for (i = 0; i < bControlSize; ++i)
1516 ncontrols += hweight8(bmControls[i]);
1517
1518 if (ncontrols == 0)
1519 continue;
1520
1521 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1522 if (entity->controls == NULL)
1523 return -ENOMEM;
1524
1525 entity->ncontrols = ncontrols;
1526
1527 ctrl = entity->controls;
1528 for (i = 0; i < bControlSize * 8; ++i) {
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001529 if (uvc_test_bit(bmControls, i) == 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001530 continue;
1531
1532 ctrl->entity = entity;
1533 ctrl->index = i;
1534 ctrl++;
1535 }
1536 }
1537
1538 /* Walk the controls info list and associate them with the device
1539 * controls, then add the device to the global device list. This has
1540 * to be done while holding the controls lock, to make sure
1541 * uvc_ctrl_add_info() will not get called in-between.
1542 */
1543 mutex_lock(&uvc_driver.ctrl_mutex);
1544 list_for_each_entry(info, &uvc_driver.controls, list)
1545 uvc_ctrl_add_ctrl(dev, info);
1546
1547 list_add_tail(&dev->list, &uvc_driver.devices);
1548 mutex_unlock(&uvc_driver.ctrl_mutex);
1549
1550 return 0;
1551}
1552
1553/*
1554 * Cleanup device controls.
1555 */
1556void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1557{
1558 struct uvc_entity *entity;
1559 unsigned int i;
1560
1561 /* Remove the device from the global devices list */
1562 mutex_lock(&uvc_driver.ctrl_mutex);
1563 if (dev->list.next != NULL)
1564 list_del(&dev->list);
1565 mutex_unlock(&uvc_driver.ctrl_mutex);
1566
1567 list_for_each_entry(entity, &dev->entities, list) {
1568 for (i = 0; i < entity->ncontrols; ++i)
1569 kfree(entity->controls[i].data);
1570
1571 kfree(entity->controls);
1572 }
1573}
1574
1575void uvc_ctrl_init(void)
1576{
1577 struct uvc_control_info *ctrl = uvc_ctrls;
1578 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1579 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1580 struct uvc_control_mapping *mend =
1581 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1582
1583 for (; ctrl < cend; ++ctrl)
1584 uvc_ctrl_add_info(ctrl);
1585
1586 for (; mapping < mend; ++mapping)
1587 uvc_ctrl_add_mapping(mapping);
1588}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001589