blob: 5ff501390842b4655a4dc79399cb1d76721e816a [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>
17#include <linux/uaccess.h>
18#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/wait.h>
22#include <asm/atomic.h>
23
24#include "uvcvideo.h"
25
Laurent Pinchartc0efd232008-06-30 15:04:50 -030026#define UVC_CTRL_DATA_CURRENT 0
27#define UVC_CTRL_DATA_BACKUP 1
Laurent Pinchart59529082010-01-23 06:30:20 -030028#define UVC_CTRL_DATA_MIN 2
29#define UVC_CTRL_DATA_MAX 3
30#define UVC_CTRL_DATA_RES 4
31#define UVC_CTRL_DATA_DEF 5
32#define UVC_CTRL_DATA_LAST 6
Laurent Pinchartc0efd232008-06-30 15:04:50 -030033
34/* ------------------------------------------------------------------------
Laurent Pinchart2c2d2642009-01-03 19:12:40 -030035 * Controls
Laurent Pinchartc0efd232008-06-30 15:04:50 -030036 */
37
38static struct uvc_control_info uvc_ctrls[] = {
39 {
40 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030041 .selector = UVC_PU_BRIGHTNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030042 .index = 0,
43 .size = 2,
44 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
45 | UVC_CONTROL_RESTORE,
46 },
47 {
48 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030049 .selector = UVC_PU_CONTRAST_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030050 .index = 1,
51 .size = 2,
52 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
53 | UVC_CONTROL_RESTORE,
54 },
55 {
56 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030057 .selector = UVC_PU_HUE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030058 .index = 2,
59 .size = 2,
60 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
61 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
62 },
63 {
64 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030065 .selector = UVC_PU_SATURATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030066 .index = 3,
67 .size = 2,
68 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
69 | UVC_CONTROL_RESTORE,
70 },
71 {
72 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030073 .selector = UVC_PU_SHARPNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030074 .index = 4,
75 .size = 2,
76 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
77 | UVC_CONTROL_RESTORE,
78 },
79 {
80 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030081 .selector = UVC_PU_GAMMA_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030082 .index = 5,
83 .size = 2,
84 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
85 | UVC_CONTROL_RESTORE,
86 },
87 {
88 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030089 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -030090 .index = 6,
91 .size = 2,
92 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
93 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
94 },
95 {
96 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030097 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -030098 .index = 7,
99 .size = 4,
100 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
101 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
102 },
103 {
104 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300105 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300106 .index = 8,
107 .size = 2,
108 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
109 | UVC_CONTROL_RESTORE,
110 },
111 {
112 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300113 .selector = UVC_PU_GAIN_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300114 .index = 9,
115 .size = 2,
116 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
117 | UVC_CONTROL_RESTORE,
118 },
119 {
120 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300121 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300122 .index = 10,
123 .size = 1,
124 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
125 | UVC_CONTROL_RESTORE,
126 },
127 {
128 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300129 .selector = UVC_PU_HUE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300130 .index = 11,
131 .size = 1,
132 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
133 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
134 },
135 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300136 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300137 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300138 .index = 12,
139 .size = 1,
140 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
141 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
142 },
143 {
144 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300145 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300146 .index = 13,
147 .size = 1,
148 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
149 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
150 },
151 {
152 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300153 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300154 .index = 14,
155 .size = 2,
156 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
157 | UVC_CONTROL_RESTORE,
158 },
159 {
160 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300161 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300162 .index = 15,
163 .size = 2,
164 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
165 | UVC_CONTROL_RESTORE,
166 },
167 {
168 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300169 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300170 .index = 16,
171 .size = 1,
172 .flags = UVC_CONTROL_GET_CUR,
173 },
174 {
175 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300176 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300177 .index = 17,
178 .size = 1,
179 .flags = UVC_CONTROL_GET_CUR,
180 },
181 {
182 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300183 .selector = UVC_CT_SCANNING_MODE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300184 .index = 0,
185 .size = 1,
186 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
187 | UVC_CONTROL_RESTORE,
188 },
189 {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300190 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300191 .selector = UVC_CT_AE_MODE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300192 .index = 1,
193 .size = 1,
194 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
195 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
196 | UVC_CONTROL_RESTORE,
197 },
198 {
199 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300200 .selector = UVC_CT_AE_PRIORITY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300201 .index = 2,
202 .size = 1,
203 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
204 | UVC_CONTROL_RESTORE,
205 },
206 {
207 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300208 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300209 .index = 3,
210 .size = 4,
211 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
212 | UVC_CONTROL_RESTORE,
213 },
214 {
215 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300216 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300217 .index = 4,
218 .size = 1,
219 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
220 | UVC_CONTROL_RESTORE,
221 },
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,
235 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
236 | UVC_CONTROL_AUTO_UPDATE,
237 },
238 {
239 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300240 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300241 .index = 7,
242 .size = 2,
243 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
244 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
245 },
246 {
247 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300248 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300249 .index = 8,
250 .size = 1,
251 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
252 | UVC_CONTROL_AUTO_UPDATE,
253 },
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,
267 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
268 | UVC_CONTROL_AUTO_UPDATE,
269 },
270 {
271 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300272 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300273 .index = 11,
274 .size = 8,
275 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
276 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
277 },
278 {
279 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300280 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300281 .index = 12,
282 .size = 4,
283 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
284 | UVC_CONTROL_AUTO_UPDATE,
285 },
286 {
287 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300288 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300289 .index = 13,
290 .size = 2,
291 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
292 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
293 },
294 {
295 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300296 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300297 .index = 14,
298 .size = 2,
299 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
300 | UVC_CONTROL_AUTO_UPDATE,
301 },
302 {
303 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300304 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300305 .index = 17,
306 .size = 1,
307 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
308 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
309 },
310 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300311 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300312 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300313 .index = 18,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300314 .size = 1,
315 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300316 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
317 },
318};
319
320static struct uvc_menu_info power_line_frequency_controls[] = {
321 { 0, "Disabled" },
322 { 1, "50 Hz" },
323 { 2, "60 Hz" },
324};
325
326static struct uvc_menu_info exposure_auto_controls[] = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300327 { 2, "Auto Mode" },
Laurent Pinchart90ac5ea2008-07-26 11:42:29 -0300328 { 1, "Manual Mode" },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300329 { 4, "Shutter Priority Mode" },
330 { 8, "Aperture Priority Mode" },
331};
332
Laurent Pinchart97683522008-12-16 06:46:32 -0300333static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
334 __u8 query, const __u8 *data)
335{
336 __s8 zoom = (__s8)data[0];
337
338 switch (query) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300339 case UVC_GET_CUR:
Laurent Pinchart97683522008-12-16 06:46:32 -0300340 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
341
Laurent Pinchartb482d922009-06-26 11:39:42 -0300342 case UVC_GET_MIN:
343 case UVC_GET_MAX:
344 case UVC_GET_RES:
345 case UVC_GET_DEF:
Laurent Pinchart97683522008-12-16 06:46:32 -0300346 default:
347 return data[2];
348 }
349}
350
351static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
352 __s32 value, __u8 *data)
353{
354 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
Laurent Pinchart2e896132009-11-04 13:11:10 -0300355 data[2] = min((int)abs(value), 0xff);
Laurent Pinchart97683522008-12-16 06:46:32 -0300356}
357
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300358static struct uvc_control_mapping uvc_ctrl_mappings[] = {
359 {
360 .id = V4L2_CID_BRIGHTNESS,
361 .name = "Brightness",
362 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300363 .selector = UVC_PU_BRIGHTNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300364 .size = 16,
365 .offset = 0,
366 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
367 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
368 },
369 {
370 .id = V4L2_CID_CONTRAST,
371 .name = "Contrast",
372 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300373 .selector = UVC_PU_CONTRAST_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300374 .size = 16,
375 .offset = 0,
376 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
377 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
378 },
379 {
380 .id = V4L2_CID_HUE,
381 .name = "Hue",
382 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300383 .selector = UVC_PU_HUE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300384 .size = 16,
385 .offset = 0,
386 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
387 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
388 },
389 {
390 .id = V4L2_CID_SATURATION,
391 .name = "Saturation",
392 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300393 .selector = UVC_PU_SATURATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300394 .size = 16,
395 .offset = 0,
396 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
397 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
398 },
399 {
400 .id = V4L2_CID_SHARPNESS,
401 .name = "Sharpness",
402 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300403 .selector = UVC_PU_SHARPNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300404 .size = 16,
405 .offset = 0,
406 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
407 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
408 },
409 {
410 .id = V4L2_CID_GAMMA,
411 .name = "Gamma",
412 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300413 .selector = UVC_PU_GAMMA_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300414 .size = 16,
415 .offset = 0,
416 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
417 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
418 },
419 {
420 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
421 .name = "Backlight Compensation",
422 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300423 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300424 .size = 16,
425 .offset = 0,
426 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
427 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
428 },
429 {
430 .id = V4L2_CID_GAIN,
431 .name = "Gain",
432 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300433 .selector = UVC_PU_GAIN_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300434 .size = 16,
435 .offset = 0,
436 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
437 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
438 },
439 {
440 .id = V4L2_CID_POWER_LINE_FREQUENCY,
441 .name = "Power Line Frequency",
442 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300443 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300444 .size = 2,
445 .offset = 0,
446 .v4l2_type = V4L2_CTRL_TYPE_MENU,
447 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
448 .menu_info = power_line_frequency_controls,
449 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
450 },
451 {
452 .id = V4L2_CID_HUE_AUTO,
453 .name = "Hue, Auto",
454 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300455 .selector = UVC_PU_HUE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300456 .size = 1,
457 .offset = 0,
458 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
459 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
460 },
461 {
462 .id = V4L2_CID_EXPOSURE_AUTO,
463 .name = "Exposure, Auto",
464 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300465 .selector = UVC_CT_AE_MODE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300466 .size = 4,
467 .offset = 0,
468 .v4l2_type = V4L2_CTRL_TYPE_MENU,
469 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
470 .menu_info = exposure_auto_controls,
471 .menu_count = ARRAY_SIZE(exposure_auto_controls),
472 },
473 {
474 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
475 .name = "Exposure, Auto Priority",
476 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300477 .selector = UVC_CT_AE_PRIORITY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300478 .size = 1,
479 .offset = 0,
480 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
481 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
482 },
483 {
484 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
485 .name = "Exposure (Absolute)",
486 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300487 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300488 .size = 32,
489 .offset = 0,
490 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
491 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
492 },
493 {
494 .id = V4L2_CID_AUTO_WHITE_BALANCE,
495 .name = "White Balance Temperature, Auto",
496 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300497 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300498 .size = 1,
499 .offset = 0,
500 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
501 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
502 },
503 {
504 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
505 .name = "White Balance Temperature",
506 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300507 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300508 .size = 16,
509 .offset = 0,
510 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
511 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
512 },
513 {
514 .id = V4L2_CID_AUTO_WHITE_BALANCE,
515 .name = "White Balance Component, Auto",
516 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300517 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300518 .size = 1,
519 .offset = 0,
520 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
521 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
522 },
523 {
524 .id = V4L2_CID_BLUE_BALANCE,
525 .name = "White Balance Blue Component",
526 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300527 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300528 .size = 16,
529 .offset = 0,
530 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
531 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
532 },
533 {
534 .id = V4L2_CID_RED_BALANCE,
535 .name = "White Balance Red Component",
536 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300537 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300538 .size = 16,
539 .offset = 16,
540 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
541 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
542 },
543 {
544 .id = V4L2_CID_FOCUS_ABSOLUTE,
545 .name = "Focus (absolute)",
546 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300547 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300548 .size = 16,
549 .offset = 0,
550 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
551 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
552 },
553 {
554 .id = V4L2_CID_FOCUS_AUTO,
555 .name = "Focus, Auto",
556 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300557 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300558 .size = 1,
559 .offset = 0,
560 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
561 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
562 },
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300563 {
Laurent Pinchart97683522008-12-16 06:46:32 -0300564 .id = V4L2_CID_ZOOM_ABSOLUTE,
565 .name = "Zoom, Absolute",
566 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300567 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300568 .size = 16,
569 .offset = 0,
570 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
571 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
572 },
573 {
574 .id = V4L2_CID_ZOOM_CONTINUOUS,
575 .name = "Zoom, Continuous",
576 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300577 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300578 .size = 0,
579 .offset = 0,
580 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
581 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
582 .get = uvc_ctrl_get_zoom,
583 .set = uvc_ctrl_set_zoom,
584 },
585 {
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300586 .id = V4L2_CID_PRIVACY,
587 .name = "Privacy",
588 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300589 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300590 .size = 1,
591 .offset = 0,
592 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
593 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
594 },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300595};
596
597/* ------------------------------------------------------------------------
598 * Utility functions
599 */
600
601static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
602{
603 return ctrl->data + id * ctrl->info->size;
604}
605
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300606static inline int uvc_test_bit(const __u8 *data, int bit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300607{
608 return (data[bit >> 3] >> (bit & 7)) & 1;
609}
610
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300611static inline void uvc_clear_bit(__u8 *data, int bit)
612{
613 data[bit >> 3] &= ~(1 << (bit & 7));
614}
615
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300616/* Extract the bit string specified by mapping->offset and mapping->size
617 * from the little-endian data stored at 'data' and return the result as
618 * a signed 32bit integer. Sign extension will be performed if the mapping
619 * references a signed data type.
620 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300621static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
622 __u8 query, const __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300623{
624 int bits = mapping->size;
625 int offset = mapping->offset;
626 __s32 value = 0;
627 __u8 mask;
628
629 data += offset / 8;
630 offset &= 7;
631 mask = ((1LL << bits) - 1) << offset;
632
633 for (; bits > 0; data++) {
634 __u8 byte = *data & mask;
635 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
636 bits -= 8 - (offset > 0 ? offset : 0);
637 offset -= 8;
638 mask = (1 << bits) - 1;
639 }
640
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300641 /* Sign-extend the value if needed. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300642 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
643 value |= -(value & (1 << (mapping->size - 1)));
644
645 return value;
646}
647
648/* Set the bit string specified by mapping->offset and mapping->size
649 * in the little-endian data stored at 'data' to the value 'value'.
650 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300651static void uvc_set_le_value(struct uvc_control_mapping *mapping,
652 __s32 value, __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300653{
654 int bits = mapping->size;
655 int offset = mapping->offset;
656 __u8 mask;
657
658 data += offset / 8;
659 offset &= 7;
660
661 for (; bits > 0; data++) {
662 mask = ((1LL << bits) - 1) << offset;
663 *data = (*data & ~mask) | ((value << offset) & mask);
664 value >>= offset ? offset : 8;
665 bits -= 8 - offset;
666 offset = 0;
667 }
668}
669
670/* ------------------------------------------------------------------------
671 * Terminal and unit management
672 */
673
674static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
675static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
676static const __u8 uvc_media_transport_input_guid[16] =
677 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
678
679static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
680{
681 switch (UVC_ENTITY_TYPE(entity)) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300682 case UVC_ITT_CAMERA:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300683 return memcmp(uvc_camera_guid, guid, 16) == 0;
684
Laurent Pinchartb482d922009-06-26 11:39:42 -0300685 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300686 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
687
Laurent Pinchartb482d922009-06-26 11:39:42 -0300688 case UVC_VC_PROCESSING_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300689 return memcmp(uvc_processing_guid, guid, 16) == 0;
690
Laurent Pinchartb482d922009-06-26 11:39:42 -0300691 case UVC_VC_EXTENSION_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300692 return memcmp(entity->extension.guidExtensionCode,
693 guid, 16) == 0;
694
695 default:
696 return 0;
697 }
698}
699
700/* ------------------------------------------------------------------------
701 * UVC Controls
702 */
703
704static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
705 struct uvc_control_mapping **mapping, struct uvc_control **control,
706 int next)
707{
708 struct uvc_control *ctrl;
709 struct uvc_control_mapping *map;
710 unsigned int i;
711
712 if (entity == NULL)
713 return;
714
715 for (i = 0; i < entity->ncontrols; ++i) {
716 ctrl = &entity->controls[i];
717 if (ctrl->info == NULL)
718 continue;
719
720 list_for_each_entry(map, &ctrl->info->mappings, list) {
721 if ((map->id == v4l2_id) && !next) {
722 *control = ctrl;
723 *mapping = map;
724 return;
725 }
726
727 if ((*mapping == NULL || (*mapping)->id > map->id) &&
728 (map->id > v4l2_id) && next) {
729 *control = ctrl;
730 *mapping = map;
731 }
732 }
733 }
734}
735
Laurent Pinchart8e113592009-07-01 20:24:47 -0300736struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300737 __u32 v4l2_id, struct uvc_control_mapping **mapping)
738{
739 struct uvc_control *ctrl = NULL;
740 struct uvc_entity *entity;
741 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
742
743 *mapping = NULL;
744
745 /* Mask the query flags. */
746 v4l2_id &= V4L2_CTRL_ID_MASK;
747
748 /* Find the control. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300749 list_for_each_entry(entity, &chain->entities, chain) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300750 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
751 if (ctrl && !next)
752 return ctrl;
753 }
754
755 if (ctrl == NULL && !next)
756 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
757 v4l2_id);
758
759 return ctrl;
760}
761
Laurent Pinchart59529082010-01-23 06:30:20 -0300762static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
763 struct uvc_control *ctrl)
764{
765 int ret;
766
767 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
768 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
769 chain->dev->intfnum, ctrl->info->selector,
770 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
771 ctrl->info->size);
772 if (ret < 0)
773 return ret;
774 }
775
776 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
777 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
778 chain->dev->intfnum, ctrl->info->selector,
779 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
780 ctrl->info->size);
781 if (ret < 0)
782 return ret;
783 }
784 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
785 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
786 chain->dev->intfnum, ctrl->info->selector,
787 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
788 ctrl->info->size);
789 if (ret < 0)
790 return ret;
791 }
792 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
793 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
794 chain->dev->intfnum, ctrl->info->selector,
795 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
796 ctrl->info->size);
797 if (ret < 0)
798 return ret;
799 }
800
801 ctrl->cached = 1;
802 return 0;
803}
804
Laurent Pinchart8e113592009-07-01 20:24:47 -0300805int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300806 struct v4l2_queryctrl *v4l2_ctrl)
807{
808 struct uvc_control *ctrl;
809 struct uvc_control_mapping *mapping;
810 struct uvc_menu_info *menu;
811 unsigned int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300812 int ret;
813
Laurent Pinchart8e113592009-07-01 20:24:47 -0300814 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300815 if (ctrl == NULL)
816 return -EINVAL;
817
Laurent Pinchart54812c72008-07-17 07:37:37 -0300818 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300819 v4l2_ctrl->id = mapping->id;
820 v4l2_ctrl->type = mapping->v4l2_type;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300821 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300822 v4l2_ctrl->flags = 0;
823
824 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
825 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
826
Laurent Pinchart59529082010-01-23 06:30:20 -0300827 if (!ctrl->cached) {
828 ret = uvc_ctrl_populate_cache(chain, ctrl);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300829 if (ret < 0)
Laurent Pinchart59529082010-01-23 06:30:20 -0300830 return ret;
831 }
832
833 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
834 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
835 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300836 }
837
Laurent Pinchart54812c72008-07-17 07:37:37 -0300838 switch (mapping->v4l2_type) {
839 case V4L2_CTRL_TYPE_MENU:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300840 v4l2_ctrl->minimum = 0;
841 v4l2_ctrl->maximum = mapping->menu_count - 1;
842 v4l2_ctrl->step = 1;
843
844 menu = mapping->menu_info;
845 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
846 if (menu->value == v4l2_ctrl->default_value) {
847 v4l2_ctrl->default_value = i;
848 break;
849 }
850 }
851
Laurent Pinchart59529082010-01-23 06:30:20 -0300852 return 0;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300853
854 case V4L2_CTRL_TYPE_BOOLEAN:
855 v4l2_ctrl->minimum = 0;
856 v4l2_ctrl->maximum = 1;
857 v4l2_ctrl->step = 1;
Laurent Pinchart59529082010-01-23 06:30:20 -0300858 return 0;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300859
Laurent Pinchartf4eabaf2009-08-06 06:05:40 -0300860 case V4L2_CTRL_TYPE_BUTTON:
861 v4l2_ctrl->minimum = 0;
862 v4l2_ctrl->maximum = 0;
863 v4l2_ctrl->step = 0;
Laurent Pinchart59529082010-01-23 06:30:20 -0300864 return 0;
Laurent Pinchartf4eabaf2009-08-06 06:05:40 -0300865
Laurent Pinchart54812c72008-07-17 07:37:37 -0300866 default:
867 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300868 }
869
Laurent Pinchart59529082010-01-23 06:30:20 -0300870 if (ctrl->info->flags & UVC_CONTROL_GET_MIN)
871 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
872 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300873
Laurent Pinchart59529082010-01-23 06:30:20 -0300874 if (ctrl->info->flags & UVC_CONTROL_GET_MAX)
875 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
876 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
877
878 if (ctrl->info->flags & UVC_CONTROL_GET_RES)
879 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
880 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
881
882 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300883}
884
885
886/* --------------------------------------------------------------------------
887 * Control transactions
888 *
889 * To make extended set operations as atomic as the hardware allows, controls
890 * are handled using begin/commit/rollback operations.
891 *
892 * At the beginning of a set request, uvc_ctrl_begin should be called to
893 * initialize the request. This function acquires the control lock.
894 *
895 * When setting a control, the new value is stored in the control data field
896 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
897 * later processing. If the UVC and V4L2 control sizes differ, the current
898 * value is loaded from the hardware before storing the new value in the data
899 * field.
900 *
901 * After processing all controls in the transaction, uvc_ctrl_commit or
902 * uvc_ctrl_rollback must be called to apply the pending changes to the
903 * hardware or revert them. When applying changes, all controls marked as
904 * dirty will be modified in the UVC device, and the dirty flag will be
905 * cleared. When reverting controls, the control data field
906 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
907 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
908 * control lock.
909 */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300910int uvc_ctrl_begin(struct uvc_video_chain *chain)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300911{
Laurent Pinchart8e113592009-07-01 20:24:47 -0300912 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300913}
914
915static int uvc_ctrl_commit_entity(struct uvc_device *dev,
916 struct uvc_entity *entity, int rollback)
917{
918 struct uvc_control *ctrl;
919 unsigned int i;
920 int ret;
921
922 if (entity == NULL)
923 return 0;
924
925 for (i = 0; i < entity->ncontrols; ++i) {
926 ctrl = &entity->controls[i];
Laurent Pinchartb1accfa2008-09-27 20:54:02 -0300927 if (ctrl->info == NULL)
928 continue;
929
930 /* Reset the loaded flag for auto-update controls that were
931 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
932 * uvc_ctrl_get from using the cached value.
933 */
934 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
935 ctrl->loaded = 0;
936
937 if (!ctrl->dirty)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300938 continue;
939
940 if (!rollback)
Laurent Pinchartb482d922009-06-26 11:39:42 -0300941 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300942 dev->intfnum, ctrl->info->selector,
943 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
944 ctrl->info->size);
945 else
946 ret = 0;
947
948 if (rollback || ret < 0)
949 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
950 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
951 ctrl->info->size);
952
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300953 ctrl->dirty = 0;
954
955 if (ret < 0)
956 return ret;
957 }
958
959 return 0;
960}
961
Laurent Pinchart8e113592009-07-01 20:24:47 -0300962int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300963{
964 struct uvc_entity *entity;
965 int ret = 0;
966
967 /* Find the control. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300968 list_for_each_entry(entity, &chain->entities, chain) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300969 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300970 if (ret < 0)
971 goto done;
972 }
973
974done:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300975 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300976 return ret;
977}
978
Laurent Pinchart8e113592009-07-01 20:24:47 -0300979int uvc_ctrl_get(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300980 struct v4l2_ext_control *xctrl)
981{
982 struct uvc_control *ctrl;
983 struct uvc_control_mapping *mapping;
984 struct uvc_menu_info *menu;
985 unsigned int i;
986 int ret;
987
Laurent Pinchart8e113592009-07-01 20:24:47 -0300988 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300989 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
990 return -EINVAL;
991
992 if (!ctrl->loaded) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300993 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
994 chain->dev->intfnum, ctrl->info->selector,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300995 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
996 ctrl->info->size);
997 if (ret < 0)
998 return ret;
999
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001000 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001001 }
1002
Laurent Pinchartb482d922009-06-26 11:39:42 -03001003 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
Laurent Pinchart97683522008-12-16 06:46:32 -03001004 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001005
1006 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1007 menu = mapping->menu_info;
1008 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1009 if (menu->value == xctrl->value) {
1010 xctrl->value = i;
1011 break;
1012 }
1013 }
1014 }
1015
1016 return 0;
1017}
1018
Laurent Pinchart8e113592009-07-01 20:24:47 -03001019int uvc_ctrl_set(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001020 struct v4l2_ext_control *xctrl)
1021{
1022 struct uvc_control *ctrl;
1023 struct uvc_control_mapping *mapping;
1024 s32 value = xctrl->value;
1025 int ret;
1026
Laurent Pinchart8e113592009-07-01 20:24:47 -03001027 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1029 return -EINVAL;
1030
1031 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1032 if (value < 0 || value >= mapping->menu_count)
Laurent Pinchart8a4e76c2010-01-21 16:53:11 -03001033 return -ERANGE;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001034 value = mapping->menu_info[value].value;
1035 }
1036
1037 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1038 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1039 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1040 0, ctrl->info->size);
1041 } else {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001042 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1043 ctrl->entity->id, chain->dev->intfnum,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001044 ctrl->info->selector,
1045 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1046 ctrl->info->size);
1047 if (ret < 0)
1048 return ret;
1049 }
1050
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001051 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001052 }
1053
1054 if (!ctrl->dirty) {
1055 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1056 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1057 ctrl->info->size);
1058 }
1059
Laurent Pinchart97683522008-12-16 06:46:32 -03001060 mapping->set(mapping, value,
1061 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001062
1063 ctrl->dirty = 1;
1064 ctrl->modified = 1;
1065 return 0;
1066}
1067
1068/* --------------------------------------------------------------------------
1069 * Dynamic controls
1070 */
1071
Laurent Pinchart8e113592009-07-01 20:24:47 -03001072int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001073 struct uvc_xu_control *xctrl, int set)
1074{
1075 struct uvc_entity *entity;
1076 struct uvc_control *ctrl = NULL;
1077 unsigned int i, found = 0;
1078 __u8 *data;
1079 int ret;
1080
1081 /* Find the extension unit. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -03001082 list_for_each_entry(entity, &chain->entities, chain) {
1083 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1084 entity->id == xctrl->unit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001085 break;
1086 }
1087
1088 if (entity->id != xctrl->unit) {
1089 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1090 xctrl->unit);
1091 return -EINVAL;
1092 }
1093
1094 /* Find the control. */
1095 for (i = 0; i < entity->ncontrols; ++i) {
1096 ctrl = &entity->controls[i];
1097 if (ctrl->info == NULL)
1098 continue;
1099
1100 if (ctrl->info->selector == xctrl->selector) {
1101 found = 1;
1102 break;
1103 }
1104 }
1105
1106 if (!found) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001107 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1108 entity->extension.guidExtensionCode, xctrl->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001109 return -EINVAL;
1110 }
1111
1112 /* Validate control data size. */
1113 if (ctrl->info->size != xctrl->size)
1114 return -EINVAL;
1115
1116 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1117 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1118 return -EINVAL;
1119
Laurent Pinchart8e113592009-07-01 20:24:47 -03001120 if (mutex_lock_interruptible(&chain->ctrl_mutex))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001121 return -ERESTARTSYS;
1122
1123 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1124 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1125 xctrl->size);
1126 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1127
1128 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1129 ret = -EFAULT;
1130 goto out;
1131 }
1132
Laurent Pinchart8e113592009-07-01 20:24:47 -03001133 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1134 xctrl->unit, chain->dev->intfnum, xctrl->selector,
Laurent Pinchartb482d922009-06-26 11:39:42 -03001135 data, xctrl->size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001136 if (ret < 0)
1137 goto out;
1138
1139 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1140 ret = -EFAULT;
1141 goto out;
1142 }
1143
1144out:
1145 if (ret)
1146 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1147 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1148 xctrl->size);
1149
Laurent Pinchart8e113592009-07-01 20:24:47 -03001150 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001151 return ret;
1152}
1153
1154/* --------------------------------------------------------------------------
1155 * Suspend/resume
1156 */
1157
1158/*
1159 * Restore control values after resume, skipping controls that haven't been
1160 * changed.
1161 *
1162 * TODO
1163 * - Don't restore modified controls that are back to their default value.
1164 * - Handle restore order (Auto-Exposure Mode should be restored before
1165 * Exposure Time).
1166 */
1167int uvc_ctrl_resume_device(struct uvc_device *dev)
1168{
1169 struct uvc_control *ctrl;
1170 struct uvc_entity *entity;
1171 unsigned int i;
1172 int ret;
1173
1174 /* Walk the entities list and restore controls when possible. */
1175 list_for_each_entry(entity, &dev->entities, list) {
1176
1177 for (i = 0; i < entity->ncontrols; ++i) {
1178 ctrl = &entity->controls[i];
1179
1180 if (ctrl->info == NULL || !ctrl->modified ||
1181 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1182 continue;
1183
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001184 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1185 ctrl->info->entity, ctrl->info->index,
1186 ctrl->info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001187 ctrl->dirty = 1;
1188 }
1189
1190 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1191 if (ret < 0)
1192 return ret;
1193 }
1194
1195 return 0;
1196}
1197
1198/* --------------------------------------------------------------------------
1199 * Control and mapping handling
1200 */
1201
1202static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1203 struct uvc_control_info *info)
1204{
1205 struct uvc_entity *entity;
1206 struct uvc_control *ctrl = NULL;
1207 int ret, found = 0;
1208 unsigned int i;
1209
1210 list_for_each_entry(entity, &dev->entities, list) {
1211 if (!uvc_entity_match_guid(entity, info->entity))
1212 continue;
1213
1214 for (i = 0; i < entity->ncontrols; ++i) {
1215 ctrl = &entity->controls[i];
1216 if (ctrl->index == info->index) {
1217 found = 1;
1218 break;
1219 }
1220 }
1221
1222 if (found)
1223 break;
1224 }
1225
1226 if (!found)
1227 return;
1228
Laurent Pinchartb482d922009-06-26 11:39:42 -03001229 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001230 /* Check if the device control information and length match
1231 * the user supplied information.
1232 */
1233 __u32 flags;
1234 __le16 size;
1235 __u8 inf;
1236
Laurent Pinchartb482d922009-06-26 11:39:42 -03001237 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1238 dev->intfnum, info->selector, (__u8 *)&size, 2);
1239 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001240 uvc_trace(UVC_TRACE_CONTROL,
1241 "GET_LEN failed on control %pUl/%u (%d).\n",
1242 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001243 return;
1244 }
1245
1246 if (info->size != le16_to_cpu(size)) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001247 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1248 "doesn't match user supplied value.\n",
1249 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001250 return;
1251 }
1252
Laurent Pinchartb482d922009-06-26 11:39:42 -03001253 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1254 dev->intfnum, info->selector, &inf, 1);
1255 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001256 uvc_trace(UVC_TRACE_CONTROL,
1257 "GET_INFO failed on control %pUl/%u (%d).\n",
1258 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001259 return;
1260 }
1261
1262 flags = info->flags;
1263 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1264 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001265 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1266 "don't match supported operations.\n",
1267 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001268 return;
1269 }
1270 }
1271
1272 ctrl->info = info;
Laurent Pinchart59529082010-01-23 06:30:20 -03001273 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001274 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1275 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1276 dev->udev->devpath, entity->id);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001277}
1278
1279/*
1280 * Add an item to the UVC control information list, and instantiate a control
1281 * structure for each device that supports the control.
1282 */
1283int uvc_ctrl_add_info(struct uvc_control_info *info)
1284{
1285 struct uvc_control_info *ctrl;
1286 struct uvc_device *dev;
1287 int ret = 0;
1288
1289 /* Find matching controls by walking the devices, entities and
1290 * controls list.
1291 */
1292 mutex_lock(&uvc_driver.ctrl_mutex);
1293
1294 /* First check if the list contains a control matching the new one.
1295 * Bail out if it does.
1296 */
1297 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1298 if (memcmp(ctrl->entity, info->entity, 16))
1299 continue;
1300
1301 if (ctrl->selector == info->selector) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001302 uvc_trace(UVC_TRACE_CONTROL,
1303 "Control %pUl/%u is already defined.\n",
1304 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001305 ret = -EEXIST;
1306 goto end;
1307 }
1308 if (ctrl->index == info->index) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001309 uvc_trace(UVC_TRACE_CONTROL,
1310 "Control %pUl/%u would overwrite index %d.\n",
1311 info->entity, info->selector, info->index);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001312 ret = -EEXIST;
1313 goto end;
1314 }
1315 }
1316
1317 list_for_each_entry(dev, &uvc_driver.devices, list)
1318 uvc_ctrl_add_ctrl(dev, info);
1319
1320 INIT_LIST_HEAD(&info->mappings);
1321 list_add_tail(&info->list, &uvc_driver.controls);
1322end:
1323 mutex_unlock(&uvc_driver.ctrl_mutex);
1324 return ret;
1325}
1326
1327int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1328{
1329 struct uvc_control_info *info;
1330 struct uvc_control_mapping *map;
1331 int ret = -EINVAL;
1332
Laurent Pinchart97683522008-12-16 06:46:32 -03001333 if (mapping->get == NULL)
1334 mapping->get = uvc_get_le_value;
1335 if (mapping->set == NULL)
1336 mapping->set = uvc_set_le_value;
1337
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001338 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1339 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1340 "invalid control id 0x%08x\n", mapping->name,
1341 mapping->id);
1342 return -EINVAL;
1343 }
1344
1345 mutex_lock(&uvc_driver.ctrl_mutex);
1346 list_for_each_entry(info, &uvc_driver.controls, list) {
1347 if (memcmp(info->entity, mapping->entity, 16) ||
1348 info->selector != mapping->selector)
1349 continue;
1350
1351 if (info->size * 8 < mapping->size + mapping->offset) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001352 uvc_trace(UVC_TRACE_CONTROL,
1353 "Mapping '%s' would overflow control %pUl/%u\n",
1354 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001355 ret = -EOVERFLOW;
1356 goto end;
1357 }
1358
1359 /* Check if the list contains a mapping matching the new one.
1360 * Bail out if it does.
1361 */
1362 list_for_each_entry(map, &info->mappings, list) {
1363 if (map->id == mapping->id) {
1364 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1365 "already defined.\n", mapping->name);
1366 ret = -EEXIST;
1367 goto end;
1368 }
1369 }
1370
1371 mapping->ctrl = info;
1372 list_add_tail(&mapping->list, &info->mappings);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001373 uvc_trace(UVC_TRACE_CONTROL,
1374 "Adding mapping %s to control %pUl/%u.\n",
1375 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001376
1377 ret = 0;
1378 break;
1379 }
1380end:
1381 mutex_unlock(&uvc_driver.ctrl_mutex);
1382 return ret;
1383}
1384
1385/*
Laurent Pinchartd732c442009-05-31 17:05:55 -03001386 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1387 * are currently the ones that crash the camera or unconditionally return an
1388 * error when queried.
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001389 */
1390static void
Laurent Pinchartd732c442009-05-31 17:05:55 -03001391uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001392{
1393 static const struct {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001394 struct usb_device_id id;
1395 u8 index;
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001396 } blacklist[] = {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001397 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1398 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001399 };
1400
1401 u8 *controls;
1402 unsigned int size;
1403 unsigned int i;
1404
Laurent Pinchartb482d922009-06-26 11:39:42 -03001405 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001406 return;
1407
1408 controls = entity->processing.bmControls;
1409 size = entity->processing.bControlSize;
1410
1411 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
Laurent Pinchart385097e2009-12-09 22:31:21 -03001412 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001413 continue;
1414
Laurent Pinchartd732c442009-05-31 17:05:55 -03001415 if (blacklist[i].index >= 8 * size ||
1416 !uvc_test_bit(controls, blacklist[i].index))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001417 continue;
1418
Laurent Pinchartd732c442009-05-31 17:05:55 -03001419 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1420 "removing it.\n", entity->id, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001421
Laurent Pinchartd732c442009-05-31 17:05:55 -03001422 uvc_clear_bit(controls, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001423 }
1424}
1425
1426/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001427 * Initialize device controls.
1428 */
1429int uvc_ctrl_init_device(struct uvc_device *dev)
1430{
1431 struct uvc_control_info *info;
1432 struct uvc_control *ctrl;
1433 struct uvc_entity *entity;
1434 unsigned int i;
1435
1436 /* Walk the entities list and instantiate controls */
1437 list_for_each_entry(entity, &dev->entities, list) {
1438 unsigned int bControlSize = 0, ncontrols = 0;
1439 __u8 *bmControls = NULL;
1440
Laurent Pinchartb482d922009-06-26 11:39:42 -03001441 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001442 bmControls = entity->extension.bmControls;
1443 bControlSize = entity->extension.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001444 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001445 bmControls = entity->processing.bmControls;
1446 bControlSize = entity->processing.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001447 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001448 bmControls = entity->camera.bmControls;
1449 bControlSize = entity->camera.bControlSize;
1450 }
1451
Laurent Pinchartd732c442009-05-31 17:05:55 -03001452 uvc_ctrl_prune_entity(dev, entity);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001453
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001454 for (i = 0; i < bControlSize; ++i)
1455 ncontrols += hweight8(bmControls[i]);
1456
1457 if (ncontrols == 0)
1458 continue;
1459
1460 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1461 if (entity->controls == NULL)
1462 return -ENOMEM;
1463
1464 entity->ncontrols = ncontrols;
1465
1466 ctrl = entity->controls;
1467 for (i = 0; i < bControlSize * 8; ++i) {
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001468 if (uvc_test_bit(bmControls, i) == 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001469 continue;
1470
1471 ctrl->entity = entity;
1472 ctrl->index = i;
1473 ctrl++;
1474 }
1475 }
1476
1477 /* Walk the controls info list and associate them with the device
1478 * controls, then add the device to the global device list. This has
1479 * to be done while holding the controls lock, to make sure
1480 * uvc_ctrl_add_info() will not get called in-between.
1481 */
1482 mutex_lock(&uvc_driver.ctrl_mutex);
1483 list_for_each_entry(info, &uvc_driver.controls, list)
1484 uvc_ctrl_add_ctrl(dev, info);
1485
1486 list_add_tail(&dev->list, &uvc_driver.devices);
1487 mutex_unlock(&uvc_driver.ctrl_mutex);
1488
1489 return 0;
1490}
1491
1492/*
1493 * Cleanup device controls.
1494 */
1495void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1496{
1497 struct uvc_entity *entity;
1498 unsigned int i;
1499
1500 /* Remove the device from the global devices list */
1501 mutex_lock(&uvc_driver.ctrl_mutex);
1502 if (dev->list.next != NULL)
1503 list_del(&dev->list);
1504 mutex_unlock(&uvc_driver.ctrl_mutex);
1505
1506 list_for_each_entry(entity, &dev->entities, list) {
1507 for (i = 0; i < entity->ncontrols; ++i)
1508 kfree(entity->controls[i].data);
1509
1510 kfree(entity->controls);
1511 }
1512}
1513
1514void uvc_ctrl_init(void)
1515{
1516 struct uvc_control_info *ctrl = uvc_ctrls;
1517 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1518 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1519 struct uvc_control_mapping *mend =
1520 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1521
1522 for (; ctrl < cend; ++ctrl)
1523 uvc_ctrl_add_info(ctrl);
1524
1525 for (; mapping < mend; ++mapping)
1526 uvc_ctrl_add_mapping(mapping);
1527}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001528