blob: fcfcfbce0a2d199bdfb846298086f0ed931948df [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,
Laurent Pinchart9c3b10b2010-07-05 15:24:39 -0300125 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
126 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300127 },
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,
Laurent Pincharta90ef692010-04-25 16:23:24 -0300220 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_RESTORE,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300221 },
222 {
223 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300224 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300225 .index = 5,
226 .size = 2,
227 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
228 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
229 },
230 {
231 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300232 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300233 .index = 6,
234 .size = 2,
Laurent Pincharta90ef692010-04-25 16:23:24 -0300235 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
236 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
237 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300238 },
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,
Laurent Pincharta90ef692010-04-25 16:23:24 -0300252 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_AUTO_UPDATE,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300253 },
254 {
255 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300256 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300257 .index = 9,
258 .size = 2,
259 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
260 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
261 },
262 {
263 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300264 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300265 .index = 10,
266 .size = 3,
Laurent Pincharta90ef692010-04-25 16:23:24 -0300267 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
268 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
269 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300270 },
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,
Laurent Pincharta90ef692010-04-25 16:23:24 -0300284 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
285 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
286 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300287 },
288 {
289 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300290 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300291 .index = 13,
292 .size = 2,
293 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
294 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
295 },
296 {
297 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300298 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300299 .index = 14,
300 .size = 2,
Laurent Pincharta90ef692010-04-25 16:23:24 -0300301 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
302 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
303 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300304 },
305 {
306 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300307 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300308 .index = 17,
309 .size = 1,
310 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
311 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
312 },
313 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300314 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300315 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300316 .index = 18,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300317 .size = 1,
318 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300319 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
320 },
321};
322
323static struct uvc_menu_info power_line_frequency_controls[] = {
324 { 0, "Disabled" },
325 { 1, "50 Hz" },
326 { 2, "60 Hz" },
327};
328
329static struct uvc_menu_info exposure_auto_controls[] = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300330 { 2, "Auto Mode" },
Laurent Pinchart90ac5ea2008-07-26 11:42:29 -0300331 { 1, "Manual Mode" },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300332 { 4, "Shutter Priority Mode" },
333 { 8, "Aperture Priority Mode" },
334};
335
Laurent Pinchart97683522008-12-16 06:46:32 -0300336static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
337 __u8 query, const __u8 *data)
338{
339 __s8 zoom = (__s8)data[0];
340
341 switch (query) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300342 case UVC_GET_CUR:
Laurent Pinchart97683522008-12-16 06:46:32 -0300343 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
344
Laurent Pinchartb482d922009-06-26 11:39:42 -0300345 case UVC_GET_MIN:
346 case UVC_GET_MAX:
347 case UVC_GET_RES:
348 case UVC_GET_DEF:
Laurent Pinchart97683522008-12-16 06:46:32 -0300349 default:
350 return data[2];
351 }
352}
353
354static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
355 __s32 value, __u8 *data)
356{
357 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
Laurent Pinchart2e896132009-11-04 13:11:10 -0300358 data[2] = min((int)abs(value), 0xff);
Laurent Pinchart97683522008-12-16 06:46:32 -0300359}
360
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300361static struct uvc_control_mapping uvc_ctrl_mappings[] = {
362 {
363 .id = V4L2_CID_BRIGHTNESS,
364 .name = "Brightness",
365 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300366 .selector = UVC_PU_BRIGHTNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300367 .size = 16,
368 .offset = 0,
369 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
370 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
371 },
372 {
373 .id = V4L2_CID_CONTRAST,
374 .name = "Contrast",
375 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300376 .selector = UVC_PU_CONTRAST_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300377 .size = 16,
378 .offset = 0,
379 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
380 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
381 },
382 {
383 .id = V4L2_CID_HUE,
384 .name = "Hue",
385 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300386 .selector = UVC_PU_HUE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300387 .size = 16,
388 .offset = 0,
389 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
390 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
391 },
392 {
393 .id = V4L2_CID_SATURATION,
394 .name = "Saturation",
395 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300396 .selector = UVC_PU_SATURATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300397 .size = 16,
398 .offset = 0,
399 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
400 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
401 },
402 {
403 .id = V4L2_CID_SHARPNESS,
404 .name = "Sharpness",
405 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300406 .selector = UVC_PU_SHARPNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300407 .size = 16,
408 .offset = 0,
409 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
410 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
411 },
412 {
413 .id = V4L2_CID_GAMMA,
414 .name = "Gamma",
415 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300416 .selector = UVC_PU_GAMMA_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300417 .size = 16,
418 .offset = 0,
419 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
420 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
421 },
422 {
423 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
424 .name = "Backlight Compensation",
425 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300426 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300427 .size = 16,
428 .offset = 0,
429 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
430 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
431 },
432 {
433 .id = V4L2_CID_GAIN,
434 .name = "Gain",
435 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300436 .selector = UVC_PU_GAIN_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300437 .size = 16,
438 .offset = 0,
439 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
440 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
441 },
442 {
443 .id = V4L2_CID_POWER_LINE_FREQUENCY,
444 .name = "Power Line Frequency",
445 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300446 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300447 .size = 2,
448 .offset = 0,
449 .v4l2_type = V4L2_CTRL_TYPE_MENU,
450 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
451 .menu_info = power_line_frequency_controls,
452 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
453 },
454 {
455 .id = V4L2_CID_HUE_AUTO,
456 .name = "Hue, Auto",
457 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300458 .selector = UVC_PU_HUE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300459 .size = 1,
460 .offset = 0,
461 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
462 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
463 },
464 {
465 .id = V4L2_CID_EXPOSURE_AUTO,
466 .name = "Exposure, Auto",
467 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300468 .selector = UVC_CT_AE_MODE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300469 .size = 4,
470 .offset = 0,
471 .v4l2_type = V4L2_CTRL_TYPE_MENU,
472 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
473 .menu_info = exposure_auto_controls,
474 .menu_count = ARRAY_SIZE(exposure_auto_controls),
475 },
476 {
477 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
478 .name = "Exposure, Auto Priority",
479 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300480 .selector = UVC_CT_AE_PRIORITY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300481 .size = 1,
482 .offset = 0,
483 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
484 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
485 },
486 {
487 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
488 .name = "Exposure (Absolute)",
489 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300490 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300491 .size = 32,
492 .offset = 0,
493 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
494 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
495 },
496 {
497 .id = V4L2_CID_AUTO_WHITE_BALANCE,
498 .name = "White Balance Temperature, Auto",
499 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300500 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300501 .size = 1,
502 .offset = 0,
503 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
504 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
505 },
506 {
507 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
508 .name = "White Balance Temperature",
509 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300510 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300511 .size = 16,
512 .offset = 0,
513 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
514 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
515 },
516 {
517 .id = V4L2_CID_AUTO_WHITE_BALANCE,
518 .name = "White Balance Component, Auto",
519 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300520 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300521 .size = 1,
522 .offset = 0,
523 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
524 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
525 },
526 {
527 .id = V4L2_CID_BLUE_BALANCE,
528 .name = "White Balance Blue Component",
529 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300530 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300531 .size = 16,
532 .offset = 0,
533 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
534 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
535 },
536 {
537 .id = V4L2_CID_RED_BALANCE,
538 .name = "White Balance Red Component",
539 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300540 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300541 .size = 16,
542 .offset = 16,
543 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
544 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
545 },
546 {
547 .id = V4L2_CID_FOCUS_ABSOLUTE,
548 .name = "Focus (absolute)",
549 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300550 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300551 .size = 16,
552 .offset = 0,
553 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
554 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
555 },
556 {
557 .id = V4L2_CID_FOCUS_AUTO,
558 .name = "Focus, Auto",
559 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300560 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300561 .size = 1,
562 .offset = 0,
563 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
564 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
565 },
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300566 {
Laurent Pinchart35a00c42010-01-20 12:13:46 -0300567 .id = V4L2_CID_IRIS_ABSOLUTE,
568 .name = "Iris, Absolute",
569 .entity = UVC_GUID_UVC_CAMERA,
570 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
571 .size = 16,
572 .offset = 0,
573 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
574 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
575 },
576 {
577 .id = V4L2_CID_IRIS_RELATIVE,
578 .name = "Iris, Relative",
579 .entity = UVC_GUID_UVC_CAMERA,
580 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
581 .size = 8,
582 .offset = 0,
583 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
584 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
585 },
586 {
Laurent Pinchart97683522008-12-16 06:46:32 -0300587 .id = V4L2_CID_ZOOM_ABSOLUTE,
588 .name = "Zoom, Absolute",
589 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300590 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300591 .size = 16,
592 .offset = 0,
593 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
594 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
595 },
596 {
597 .id = V4L2_CID_ZOOM_CONTINUOUS,
598 .name = "Zoom, Continuous",
599 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300600 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300601 .size = 0,
602 .offset = 0,
603 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
604 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
605 .get = uvc_ctrl_get_zoom,
606 .set = uvc_ctrl_set_zoom,
607 },
608 {
Martin Rublie56be912010-05-19 19:51:56 -0300609 .id = V4L2_CID_PAN_ABSOLUTE,
610 .name = "Pan (Absolute)",
611 .entity = UVC_GUID_UVC_CAMERA,
612 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
613 .size = 32,
614 .offset = 0,
615 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
616 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
617 },
618 {
619 .id = V4L2_CID_TILT_ABSOLUTE,
620 .name = "Tilt (Absolute)",
621 .entity = UVC_GUID_UVC_CAMERA,
622 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
623 .size = 32,
624 .offset = 32,
625 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
626 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
627 },
628 {
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300629 .id = V4L2_CID_PRIVACY,
630 .name = "Privacy",
631 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300632 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300633 .size = 1,
634 .offset = 0,
635 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
636 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
637 },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300638};
639
640/* ------------------------------------------------------------------------
641 * Utility functions
642 */
643
644static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
645{
646 return ctrl->data + id * ctrl->info->size;
647}
648
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300649static inline int uvc_test_bit(const __u8 *data, int bit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300650{
651 return (data[bit >> 3] >> (bit & 7)) & 1;
652}
653
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300654static inline void uvc_clear_bit(__u8 *data, int bit)
655{
656 data[bit >> 3] &= ~(1 << (bit & 7));
657}
658
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300659/* Extract the bit string specified by mapping->offset and mapping->size
660 * from the little-endian data stored at 'data' and return the result as
661 * a signed 32bit integer. Sign extension will be performed if the mapping
662 * references a signed data type.
663 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300664static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
665 __u8 query, const __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300666{
667 int bits = mapping->size;
668 int offset = mapping->offset;
669 __s32 value = 0;
670 __u8 mask;
671
672 data += offset / 8;
673 offset &= 7;
674 mask = ((1LL << bits) - 1) << offset;
675
676 for (; bits > 0; data++) {
677 __u8 byte = *data & mask;
678 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
679 bits -= 8 - (offset > 0 ? offset : 0);
680 offset -= 8;
681 mask = (1 << bits) - 1;
682 }
683
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300684 /* Sign-extend the value if needed. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300685 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
686 value |= -(value & (1 << (mapping->size - 1)));
687
688 return value;
689}
690
691/* Set the bit string specified by mapping->offset and mapping->size
692 * in the little-endian data stored at 'data' to the value 'value'.
693 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300694static void uvc_set_le_value(struct uvc_control_mapping *mapping,
695 __s32 value, __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300696{
697 int bits = mapping->size;
698 int offset = mapping->offset;
699 __u8 mask;
700
701 data += offset / 8;
702 offset &= 7;
703
704 for (; bits > 0; data++) {
705 mask = ((1LL << bits) - 1) << offset;
706 *data = (*data & ~mask) | ((value << offset) & mask);
707 value >>= offset ? offset : 8;
708 bits -= 8 - offset;
709 offset = 0;
710 }
711}
712
713/* ------------------------------------------------------------------------
714 * Terminal and unit management
715 */
716
717static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
718static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
719static const __u8 uvc_media_transport_input_guid[16] =
720 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
721
722static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
723{
724 switch (UVC_ENTITY_TYPE(entity)) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300725 case UVC_ITT_CAMERA:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300726 return memcmp(uvc_camera_guid, guid, 16) == 0;
727
Laurent Pinchartb482d922009-06-26 11:39:42 -0300728 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300729 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
730
Laurent Pinchartb482d922009-06-26 11:39:42 -0300731 case UVC_VC_PROCESSING_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300732 return memcmp(uvc_processing_guid, guid, 16) == 0;
733
Laurent Pinchartb482d922009-06-26 11:39:42 -0300734 case UVC_VC_EXTENSION_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300735 return memcmp(entity->extension.guidExtensionCode,
736 guid, 16) == 0;
737
738 default:
739 return 0;
740 }
741}
742
743/* ------------------------------------------------------------------------
744 * UVC Controls
745 */
746
747static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
748 struct uvc_control_mapping **mapping, struct uvc_control **control,
749 int next)
750{
751 struct uvc_control *ctrl;
752 struct uvc_control_mapping *map;
753 unsigned int i;
754
755 if (entity == NULL)
756 return;
757
758 for (i = 0; i < entity->ncontrols; ++i) {
759 ctrl = &entity->controls[i];
760 if (ctrl->info == NULL)
761 continue;
762
763 list_for_each_entry(map, &ctrl->info->mappings, list) {
764 if ((map->id == v4l2_id) && !next) {
765 *control = ctrl;
766 *mapping = map;
767 return;
768 }
769
770 if ((*mapping == NULL || (*mapping)->id > map->id) &&
771 (map->id > v4l2_id) && next) {
772 *control = ctrl;
773 *mapping = map;
774 }
775 }
776 }
777}
778
Laurent Pinchart8e113592009-07-01 20:24:47 -0300779struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300780 __u32 v4l2_id, struct uvc_control_mapping **mapping)
781{
782 struct uvc_control *ctrl = NULL;
783 struct uvc_entity *entity;
784 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
785
786 *mapping = NULL;
787
788 /* Mask the query flags. */
789 v4l2_id &= V4L2_CTRL_ID_MASK;
790
791 /* Find the control. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300792 list_for_each_entry(entity, &chain->entities, chain) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300793 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
794 if (ctrl && !next)
795 return ctrl;
796 }
797
798 if (ctrl == NULL && !next)
799 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
800 v4l2_id);
801
802 return ctrl;
803}
804
Laurent Pinchart59529082010-01-23 06:30:20 -0300805static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
806 struct uvc_control *ctrl)
807{
808 int ret;
809
810 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
811 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
812 chain->dev->intfnum, ctrl->info->selector,
813 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
814 ctrl->info->size);
815 if (ret < 0)
816 return ret;
817 }
818
819 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
820 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
821 chain->dev->intfnum, ctrl->info->selector,
822 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
823 ctrl->info->size);
824 if (ret < 0)
825 return ret;
826 }
827 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
828 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
829 chain->dev->intfnum, ctrl->info->selector,
830 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
831 ctrl->info->size);
832 if (ret < 0)
833 return ret;
834 }
835 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
836 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
837 chain->dev->intfnum, ctrl->info->selector,
838 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
839 ctrl->info->size);
840 if (ret < 0)
841 return ret;
842 }
843
844 ctrl->cached = 1;
845 return 0;
846}
847
Laurent Pinchart8e113592009-07-01 20:24:47 -0300848int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300849 struct v4l2_queryctrl *v4l2_ctrl)
850{
851 struct uvc_control *ctrl;
852 struct uvc_control_mapping *mapping;
853 struct uvc_menu_info *menu;
854 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300855 int ret;
856
Laurent Pinchart8e113592009-07-01 20:24:47 -0300857 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300858 if (ctrl == NULL)
859 return -EINVAL;
860
Laurent Pinchart54812c72008-07-17 07:37:37 -0300861 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300862 v4l2_ctrl->id = mapping->id;
863 v4l2_ctrl->type = mapping->v4l2_type;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300864 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300865 v4l2_ctrl->flags = 0;
866
Laurent Pincharta90ef692010-04-25 16:23:24 -0300867 if (!(ctrl->info->flags & UVC_CONTROL_GET_CUR))
868 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300869 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
870 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
871
Laurent Pinchart59529082010-01-23 06:30:20 -0300872 if (!ctrl->cached) {
873 ret = uvc_ctrl_populate_cache(chain, ctrl);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300874 if (ret < 0)
Laurent Pinchart59529082010-01-23 06:30:20 -0300875 return ret;
876 }
877
878 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
879 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
880 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300881 }
882
Laurent Pinchart54812c72008-07-17 07:37:37 -0300883 switch (mapping->v4l2_type) {
884 case V4L2_CTRL_TYPE_MENU:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300885 v4l2_ctrl->minimum = 0;
886 v4l2_ctrl->maximum = mapping->menu_count - 1;
887 v4l2_ctrl->step = 1;
888
889 menu = mapping->menu_info;
890 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
891 if (menu->value == v4l2_ctrl->default_value) {
892 v4l2_ctrl->default_value = i;
893 break;
894 }
895 }
896
Laurent Pinchart59529082010-01-23 06:30:20 -0300897 return 0;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300898
899 case V4L2_CTRL_TYPE_BOOLEAN:
900 v4l2_ctrl->minimum = 0;
901 v4l2_ctrl->maximum = 1;
902 v4l2_ctrl->step = 1;
Laurent Pinchart59529082010-01-23 06:30:20 -0300903 return 0;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300904
Laurent Pinchartf4eabaf2009-08-06 06:05:40 -0300905 case V4L2_CTRL_TYPE_BUTTON:
906 v4l2_ctrl->minimum = 0;
907 v4l2_ctrl->maximum = 0;
908 v4l2_ctrl->step = 0;
Laurent Pinchart59529082010-01-23 06:30:20 -0300909 return 0;
Laurent Pinchartf4eabaf2009-08-06 06:05:40 -0300910
Laurent Pinchart54812c72008-07-17 07:37:37 -0300911 default:
912 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300913 }
914
Laurent Pinchart59529082010-01-23 06:30:20 -0300915 if (ctrl->info->flags & UVC_CONTROL_GET_MIN)
916 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
917 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300918
Laurent Pinchart59529082010-01-23 06:30:20 -0300919 if (ctrl->info->flags & UVC_CONTROL_GET_MAX)
920 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
921 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
922
923 if (ctrl->info->flags & UVC_CONTROL_GET_RES)
924 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
925 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
926
927 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300928}
929
930
931/* --------------------------------------------------------------------------
932 * Control transactions
933 *
934 * To make extended set operations as atomic as the hardware allows, controls
935 * are handled using begin/commit/rollback operations.
936 *
937 * At the beginning of a set request, uvc_ctrl_begin should be called to
938 * initialize the request. This function acquires the control lock.
939 *
940 * When setting a control, the new value is stored in the control data field
941 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
942 * later processing. If the UVC and V4L2 control sizes differ, the current
943 * value is loaded from the hardware before storing the new value in the data
944 * field.
945 *
946 * After processing all controls in the transaction, uvc_ctrl_commit or
947 * uvc_ctrl_rollback must be called to apply the pending changes to the
948 * hardware or revert them. When applying changes, all controls marked as
949 * dirty will be modified in the UVC device, and the dirty flag will be
950 * cleared. When reverting controls, the control data field
951 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
952 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
953 * control lock.
954 */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300955int uvc_ctrl_begin(struct uvc_video_chain *chain)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300956{
Laurent Pinchart8e113592009-07-01 20:24:47 -0300957 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300958}
959
960static int uvc_ctrl_commit_entity(struct uvc_device *dev,
961 struct uvc_entity *entity, int rollback)
962{
963 struct uvc_control *ctrl;
964 unsigned int i;
965 int ret;
966
967 if (entity == NULL)
968 return 0;
969
970 for (i = 0; i < entity->ncontrols; ++i) {
971 ctrl = &entity->controls[i];
Laurent Pinchartb1accfa2008-09-27 20:54:02 -0300972 if (ctrl->info == NULL)
973 continue;
974
975 /* Reset the loaded flag for auto-update controls that were
976 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
977 * uvc_ctrl_get from using the cached value.
978 */
979 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
980 ctrl->loaded = 0;
981
982 if (!ctrl->dirty)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300983 continue;
984
985 if (!rollback)
Laurent Pinchartb482d922009-06-26 11:39:42 -0300986 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300987 dev->intfnum, ctrl->info->selector,
988 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
989 ctrl->info->size);
990 else
991 ret = 0;
992
993 if (rollback || ret < 0)
994 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
995 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
996 ctrl->info->size);
997
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300998 ctrl->dirty = 0;
999
1000 if (ret < 0)
1001 return ret;
1002 }
1003
1004 return 0;
1005}
1006
Laurent Pinchart8e113592009-07-01 20:24:47 -03001007int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001008{
1009 struct uvc_entity *entity;
1010 int ret = 0;
1011
1012 /* Find the control. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -03001013 list_for_each_entry(entity, &chain->entities, chain) {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001014 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001015 if (ret < 0)
1016 goto done;
1017 }
1018
1019done:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001020 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001021 return ret;
1022}
1023
Laurent Pinchart8e113592009-07-01 20:24:47 -03001024int uvc_ctrl_get(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001025 struct v4l2_ext_control *xctrl)
1026{
1027 struct uvc_control *ctrl;
1028 struct uvc_control_mapping *mapping;
1029 struct uvc_menu_info *menu;
1030 unsigned int i;
1031 int ret;
1032
Laurent Pinchart8e113592009-07-01 20:24:47 -03001033 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001034 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
1035 return -EINVAL;
1036
1037 if (!ctrl->loaded) {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001038 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1039 chain->dev->intfnum, ctrl->info->selector,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001040 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1041 ctrl->info->size);
1042 if (ret < 0)
1043 return ret;
1044
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001045 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001046 }
1047
Laurent Pinchartb482d922009-06-26 11:39:42 -03001048 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
Laurent Pinchart97683522008-12-16 06:46:32 -03001049 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001050
1051 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1052 menu = mapping->menu_info;
1053 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1054 if (menu->value == xctrl->value) {
1055 xctrl->value = i;
1056 break;
1057 }
1058 }
1059 }
1060
1061 return 0;
1062}
1063
Laurent Pinchart8e113592009-07-01 20:24:47 -03001064int uvc_ctrl_set(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001065 struct v4l2_ext_control *xctrl)
1066{
1067 struct uvc_control *ctrl;
1068 struct uvc_control_mapping *mapping;
Laurent Pincharte54532e2010-01-23 07:07:53 -03001069 s32 value;
1070 u32 step;
1071 s32 min;
1072 s32 max;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001073 int ret;
1074
Laurent Pinchart8e113592009-07-01 20:24:47 -03001075 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001076 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1077 return -EINVAL;
1078
Laurent Pincharte54532e2010-01-23 07:07:53 -03001079 /* Clamp out of range values. */
1080 switch (mapping->v4l2_type) {
1081 case V4L2_CTRL_TYPE_INTEGER:
1082 if (!ctrl->cached) {
1083 ret = uvc_ctrl_populate_cache(chain, ctrl);
1084 if (ret < 0)
1085 return ret;
1086 }
1087
1088 min = mapping->get(mapping, UVC_GET_MIN,
1089 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1090 max = mapping->get(mapping, UVC_GET_MAX,
1091 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1092 step = mapping->get(mapping, UVC_GET_RES,
1093 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
Laurent Pinchartcf7a50e2010-04-25 16:27:14 -03001094 if (step == 0)
1095 step = 1;
Laurent Pincharte54532e2010-01-23 07:07:53 -03001096
1097 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1098 xctrl->value = clamp(xctrl->value, min, max);
1099 value = xctrl->value;
1100 break;
1101
1102 case V4L2_CTRL_TYPE_BOOLEAN:
1103 xctrl->value = clamp(xctrl->value, 0, 1);
1104 value = xctrl->value;
1105 break;
1106
1107 case V4L2_CTRL_TYPE_MENU:
1108 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
Laurent Pinchart8a4e76c2010-01-21 16:53:11 -03001109 return -ERANGE;
Laurent Pincharte54532e2010-01-23 07:07:53 -03001110 value = mapping->menu_info[xctrl->value].value;
1111 break;
1112
1113 default:
1114 value = xctrl->value;
1115 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001116 }
1117
Laurent Pincharte54532e2010-01-23 07:07:53 -03001118 /* If the mapping doesn't span the whole UVC control, the current value
1119 * needs to be loaded from the device to perform the read-modify-write
1120 * operation.
1121 */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001122 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1123 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1124 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1125 0, ctrl->info->size);
1126 } else {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001127 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1128 ctrl->entity->id, chain->dev->intfnum,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001129 ctrl->info->selector,
1130 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1131 ctrl->info->size);
1132 if (ret < 0)
1133 return ret;
1134 }
1135
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001136 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001137 }
1138
Laurent Pincharte54532e2010-01-23 07:07:53 -03001139 /* Backup the current value in case we need to rollback later. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001140 if (!ctrl->dirty) {
1141 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1142 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1143 ctrl->info->size);
1144 }
1145
Laurent Pinchart97683522008-12-16 06:46:32 -03001146 mapping->set(mapping, value,
1147 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001148
1149 ctrl->dirty = 1;
1150 ctrl->modified = 1;
1151 return 0;
1152}
1153
1154/* --------------------------------------------------------------------------
1155 * Dynamic controls
1156 */
1157
Laurent Pinchart8e113592009-07-01 20:24:47 -03001158int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001159 struct uvc_xu_control *xctrl, int set)
1160{
1161 struct uvc_entity *entity;
1162 struct uvc_control *ctrl = NULL;
1163 unsigned int i, found = 0;
1164 __u8 *data;
1165 int ret;
1166
1167 /* Find the extension unit. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -03001168 list_for_each_entry(entity, &chain->entities, chain) {
1169 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1170 entity->id == xctrl->unit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001171 break;
1172 }
1173
1174 if (entity->id != xctrl->unit) {
1175 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1176 xctrl->unit);
1177 return -EINVAL;
1178 }
1179
1180 /* Find the control. */
1181 for (i = 0; i < entity->ncontrols; ++i) {
1182 ctrl = &entity->controls[i];
1183 if (ctrl->info == NULL)
1184 continue;
1185
1186 if (ctrl->info->selector == xctrl->selector) {
1187 found = 1;
1188 break;
1189 }
1190 }
1191
1192 if (!found) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001193 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1194 entity->extension.guidExtensionCode, xctrl->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001195 return -EINVAL;
1196 }
1197
1198 /* Validate control data size. */
1199 if (ctrl->info->size != xctrl->size)
1200 return -EINVAL;
1201
1202 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1203 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1204 return -EINVAL;
1205
Laurent Pinchart8e113592009-07-01 20:24:47 -03001206 if (mutex_lock_interruptible(&chain->ctrl_mutex))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001207 return -ERESTARTSYS;
1208
1209 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1210 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1211 xctrl->size);
1212 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1213
1214 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1215 ret = -EFAULT;
1216 goto out;
1217 }
1218
Laurent Pinchart8e113592009-07-01 20:24:47 -03001219 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1220 xctrl->unit, chain->dev->intfnum, xctrl->selector,
Laurent Pinchartb482d922009-06-26 11:39:42 -03001221 data, xctrl->size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001222 if (ret < 0)
1223 goto out;
1224
1225 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1226 ret = -EFAULT;
1227 goto out;
1228 }
1229
1230out:
1231 if (ret)
1232 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1233 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1234 xctrl->size);
1235
Laurent Pinchart8e113592009-07-01 20:24:47 -03001236 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001237 return ret;
1238}
1239
1240/* --------------------------------------------------------------------------
1241 * Suspend/resume
1242 */
1243
1244/*
1245 * Restore control values after resume, skipping controls that haven't been
1246 * changed.
1247 *
1248 * TODO
1249 * - Don't restore modified controls that are back to their default value.
1250 * - Handle restore order (Auto-Exposure Mode should be restored before
1251 * Exposure Time).
1252 */
1253int uvc_ctrl_resume_device(struct uvc_device *dev)
1254{
1255 struct uvc_control *ctrl;
1256 struct uvc_entity *entity;
1257 unsigned int i;
1258 int ret;
1259
1260 /* Walk the entities list and restore controls when possible. */
1261 list_for_each_entry(entity, &dev->entities, list) {
1262
1263 for (i = 0; i < entity->ncontrols; ++i) {
1264 ctrl = &entity->controls[i];
1265
1266 if (ctrl->info == NULL || !ctrl->modified ||
1267 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1268 continue;
1269
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001270 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1271 ctrl->info->entity, ctrl->info->index,
1272 ctrl->info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001273 ctrl->dirty = 1;
1274 }
1275
1276 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1277 if (ret < 0)
1278 return ret;
1279 }
1280
1281 return 0;
1282}
1283
1284/* --------------------------------------------------------------------------
1285 * Control and mapping handling
1286 */
1287
1288static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1289 struct uvc_control_info *info)
1290{
1291 struct uvc_entity *entity;
1292 struct uvc_control *ctrl = NULL;
1293 int ret, found = 0;
1294 unsigned int i;
1295
1296 list_for_each_entry(entity, &dev->entities, list) {
1297 if (!uvc_entity_match_guid(entity, info->entity))
1298 continue;
1299
1300 for (i = 0; i < entity->ncontrols; ++i) {
1301 ctrl = &entity->controls[i];
1302 if (ctrl->index == info->index) {
1303 found = 1;
1304 break;
1305 }
1306 }
1307
1308 if (found)
1309 break;
1310 }
1311
1312 if (!found)
1313 return;
1314
Laurent Pinchartb482d922009-06-26 11:39:42 -03001315 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001316 /* Check if the device control information and length match
1317 * the user supplied information.
1318 */
1319 __u32 flags;
1320 __le16 size;
1321 __u8 inf;
1322
Laurent Pinchartb482d922009-06-26 11:39:42 -03001323 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1324 dev->intfnum, info->selector, (__u8 *)&size, 2);
1325 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001326 uvc_trace(UVC_TRACE_CONTROL,
1327 "GET_LEN failed on control %pUl/%u (%d).\n",
1328 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001329 return;
1330 }
1331
1332 if (info->size != le16_to_cpu(size)) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001333 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1334 "doesn't match user supplied value.\n",
1335 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001336 return;
1337 }
1338
Laurent Pinchartb482d922009-06-26 11:39:42 -03001339 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1340 dev->intfnum, info->selector, &inf, 1);
1341 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001342 uvc_trace(UVC_TRACE_CONTROL,
1343 "GET_INFO failed on control %pUl/%u (%d).\n",
1344 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001345 return;
1346 }
1347
1348 flags = info->flags;
1349 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1350 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001351 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1352 "don't match supported operations.\n",
1353 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001354 return;
1355 }
1356 }
1357
1358 ctrl->info = info;
Laurent Pinchart59529082010-01-23 06:30:20 -03001359 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001360 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1361 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1362 dev->udev->devpath, entity->id);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001363}
1364
1365/*
1366 * Add an item to the UVC control information list, and instantiate a control
1367 * structure for each device that supports the control.
1368 */
1369int uvc_ctrl_add_info(struct uvc_control_info *info)
1370{
1371 struct uvc_control_info *ctrl;
1372 struct uvc_device *dev;
1373 int ret = 0;
1374
1375 /* Find matching controls by walking the devices, entities and
1376 * controls list.
1377 */
1378 mutex_lock(&uvc_driver.ctrl_mutex);
1379
1380 /* First check if the list contains a control matching the new one.
1381 * Bail out if it does.
1382 */
1383 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1384 if (memcmp(ctrl->entity, info->entity, 16))
1385 continue;
1386
1387 if (ctrl->selector == info->selector) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001388 uvc_trace(UVC_TRACE_CONTROL,
1389 "Control %pUl/%u is already defined.\n",
1390 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001391 ret = -EEXIST;
1392 goto end;
1393 }
1394 if (ctrl->index == info->index) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001395 uvc_trace(UVC_TRACE_CONTROL,
1396 "Control %pUl/%u would overwrite index %d.\n",
1397 info->entity, info->selector, info->index);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001398 ret = -EEXIST;
1399 goto end;
1400 }
1401 }
1402
1403 list_for_each_entry(dev, &uvc_driver.devices, list)
1404 uvc_ctrl_add_ctrl(dev, info);
1405
1406 INIT_LIST_HEAD(&info->mappings);
1407 list_add_tail(&info->list, &uvc_driver.controls);
1408end:
1409 mutex_unlock(&uvc_driver.ctrl_mutex);
1410 return ret;
1411}
1412
1413int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1414{
1415 struct uvc_control_info *info;
1416 struct uvc_control_mapping *map;
1417 int ret = -EINVAL;
1418
Laurent Pinchart97683522008-12-16 06:46:32 -03001419 if (mapping->get == NULL)
1420 mapping->get = uvc_get_le_value;
1421 if (mapping->set == NULL)
1422 mapping->set = uvc_set_le_value;
1423
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001424 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1425 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1426 "invalid control id 0x%08x\n", mapping->name,
1427 mapping->id);
1428 return -EINVAL;
1429 }
1430
1431 mutex_lock(&uvc_driver.ctrl_mutex);
1432 list_for_each_entry(info, &uvc_driver.controls, list) {
1433 if (memcmp(info->entity, mapping->entity, 16) ||
1434 info->selector != mapping->selector)
1435 continue;
1436
1437 if (info->size * 8 < mapping->size + mapping->offset) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001438 uvc_trace(UVC_TRACE_CONTROL,
1439 "Mapping '%s' would overflow control %pUl/%u\n",
1440 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001441 ret = -EOVERFLOW;
1442 goto end;
1443 }
1444
1445 /* Check if the list contains a mapping matching the new one.
1446 * Bail out if it does.
1447 */
1448 list_for_each_entry(map, &info->mappings, list) {
1449 if (map->id == mapping->id) {
1450 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1451 "already defined.\n", mapping->name);
1452 ret = -EEXIST;
1453 goto end;
1454 }
1455 }
1456
1457 mapping->ctrl = info;
1458 list_add_tail(&mapping->list, &info->mappings);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001459 uvc_trace(UVC_TRACE_CONTROL,
1460 "Adding mapping %s to control %pUl/%u.\n",
1461 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001462
1463 ret = 0;
1464 break;
1465 }
1466end:
1467 mutex_unlock(&uvc_driver.ctrl_mutex);
1468 return ret;
1469}
1470
1471/*
Laurent Pinchartd732c442009-05-31 17:05:55 -03001472 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1473 * are currently the ones that crash the camera or unconditionally return an
1474 * error when queried.
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001475 */
1476static void
Laurent Pinchartd732c442009-05-31 17:05:55 -03001477uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001478{
1479 static const struct {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001480 struct usb_device_id id;
1481 u8 index;
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001482 } blacklist[] = {
Laurent Pinchart9405e3c2010-02-03 06:49:35 -03001483 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
Laurent Pinchartd732c442009-05-31 17:05:55 -03001484 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1485 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001486 };
1487
1488 u8 *controls;
1489 unsigned int size;
1490 unsigned int i;
1491
Laurent Pinchartb482d922009-06-26 11:39:42 -03001492 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001493 return;
1494
1495 controls = entity->processing.bmControls;
1496 size = entity->processing.bControlSize;
1497
1498 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
Laurent Pinchart385097e2009-12-09 22:31:21 -03001499 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001500 continue;
1501
Laurent Pinchartd732c442009-05-31 17:05:55 -03001502 if (blacklist[i].index >= 8 * size ||
1503 !uvc_test_bit(controls, blacklist[i].index))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001504 continue;
1505
Laurent Pinchartd732c442009-05-31 17:05:55 -03001506 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1507 "removing it.\n", entity->id, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001508
Laurent Pinchartd732c442009-05-31 17:05:55 -03001509 uvc_clear_bit(controls, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001510 }
1511}
1512
1513/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001514 * Initialize device controls.
1515 */
1516int uvc_ctrl_init_device(struct uvc_device *dev)
1517{
1518 struct uvc_control_info *info;
1519 struct uvc_control *ctrl;
1520 struct uvc_entity *entity;
1521 unsigned int i;
1522
1523 /* Walk the entities list and instantiate controls */
1524 list_for_each_entry(entity, &dev->entities, list) {
1525 unsigned int bControlSize = 0, ncontrols = 0;
1526 __u8 *bmControls = NULL;
1527
Laurent Pinchartb482d922009-06-26 11:39:42 -03001528 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001529 bmControls = entity->extension.bmControls;
1530 bControlSize = entity->extension.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001531 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001532 bmControls = entity->processing.bmControls;
1533 bControlSize = entity->processing.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001534 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001535 bmControls = entity->camera.bmControls;
1536 bControlSize = entity->camera.bControlSize;
1537 }
1538
Laurent Pinchartd732c442009-05-31 17:05:55 -03001539 uvc_ctrl_prune_entity(dev, entity);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001540
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001541 for (i = 0; i < bControlSize; ++i)
1542 ncontrols += hweight8(bmControls[i]);
1543
1544 if (ncontrols == 0)
1545 continue;
1546
1547 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1548 if (entity->controls == NULL)
1549 return -ENOMEM;
1550
1551 entity->ncontrols = ncontrols;
1552
1553 ctrl = entity->controls;
1554 for (i = 0; i < bControlSize * 8; ++i) {
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001555 if (uvc_test_bit(bmControls, i) == 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001556 continue;
1557
1558 ctrl->entity = entity;
1559 ctrl->index = i;
1560 ctrl++;
1561 }
1562 }
1563
1564 /* Walk the controls info list and associate them with the device
1565 * controls, then add the device to the global device list. This has
1566 * to be done while holding the controls lock, to make sure
1567 * uvc_ctrl_add_info() will not get called in-between.
1568 */
1569 mutex_lock(&uvc_driver.ctrl_mutex);
1570 list_for_each_entry(info, &uvc_driver.controls, list)
1571 uvc_ctrl_add_ctrl(dev, info);
1572
1573 list_add_tail(&dev->list, &uvc_driver.devices);
1574 mutex_unlock(&uvc_driver.ctrl_mutex);
1575
1576 return 0;
1577}
1578
1579/*
1580 * Cleanup device controls.
1581 */
1582void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1583{
1584 struct uvc_entity *entity;
1585 unsigned int i;
1586
1587 /* Remove the device from the global devices list */
1588 mutex_lock(&uvc_driver.ctrl_mutex);
1589 if (dev->list.next != NULL)
1590 list_del(&dev->list);
1591 mutex_unlock(&uvc_driver.ctrl_mutex);
1592
1593 list_for_each_entry(entity, &dev->entities, list) {
1594 for (i = 0; i < entity->ncontrols; ++i)
1595 kfree(entity->controls[i].data);
1596
1597 kfree(entity->controls);
1598 }
1599}
1600
1601void uvc_ctrl_init(void)
1602{
1603 struct uvc_control_info *ctrl = uvc_ctrls;
1604 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1605 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1606 struct uvc_control_mapping *mend =
1607 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1608
1609 for (; ctrl < cend; ++ctrl)
1610 uvc_ctrl_add_info(ctrl);
1611
1612 for (; mapping < mend; ++mapping)
1613 uvc_ctrl_add_mapping(mapping);
1614}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001615