blob: f38bc6bbc3d2eb0b2f39a8acab0bf3f3f31f760b [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;
Laurent Pincharte54532e2010-01-23 07:07:53 -03001024 s32 value;
1025 u32 step;
1026 s32 min;
1027 s32 max;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028 int ret;
1029
Laurent Pinchart8e113592009-07-01 20:24:47 -03001030 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001031 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1032 return -EINVAL;
1033
Laurent Pincharte54532e2010-01-23 07:07:53 -03001034 /* Clamp out of range values. */
1035 switch (mapping->v4l2_type) {
1036 case V4L2_CTRL_TYPE_INTEGER:
1037 if (!ctrl->cached) {
1038 ret = uvc_ctrl_populate_cache(chain, ctrl);
1039 if (ret < 0)
1040 return ret;
1041 }
1042
1043 min = mapping->get(mapping, UVC_GET_MIN,
1044 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1045 max = mapping->get(mapping, UVC_GET_MAX,
1046 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1047 step = mapping->get(mapping, UVC_GET_RES,
1048 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1049
1050 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1051 xctrl->value = clamp(xctrl->value, min, max);
1052 value = xctrl->value;
1053 break;
1054
1055 case V4L2_CTRL_TYPE_BOOLEAN:
1056 xctrl->value = clamp(xctrl->value, 0, 1);
1057 value = xctrl->value;
1058 break;
1059
1060 case V4L2_CTRL_TYPE_MENU:
1061 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
Laurent Pinchart8a4e76c2010-01-21 16:53:11 -03001062 return -ERANGE;
Laurent Pincharte54532e2010-01-23 07:07:53 -03001063 value = mapping->menu_info[xctrl->value].value;
1064 break;
1065
1066 default:
1067 value = xctrl->value;
1068 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001069 }
1070
Laurent Pincharte54532e2010-01-23 07:07:53 -03001071 /* If the mapping doesn't span the whole UVC control, the current value
1072 * needs to be loaded from the device to perform the read-modify-write
1073 * operation.
1074 */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001075 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1076 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1077 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1078 0, ctrl->info->size);
1079 } else {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001080 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1081 ctrl->entity->id, chain->dev->intfnum,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001082 ctrl->info->selector,
1083 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1084 ctrl->info->size);
1085 if (ret < 0)
1086 return ret;
1087 }
1088
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001089 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001090 }
1091
Laurent Pincharte54532e2010-01-23 07:07:53 -03001092 /* Backup the current value in case we need to rollback later. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001093 if (!ctrl->dirty) {
1094 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1095 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1096 ctrl->info->size);
1097 }
1098
Laurent Pinchart97683522008-12-16 06:46:32 -03001099 mapping->set(mapping, value,
1100 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001101
1102 ctrl->dirty = 1;
1103 ctrl->modified = 1;
1104 return 0;
1105}
1106
1107/* --------------------------------------------------------------------------
1108 * Dynamic controls
1109 */
1110
Laurent Pinchart8e113592009-07-01 20:24:47 -03001111int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001112 struct uvc_xu_control *xctrl, int set)
1113{
1114 struct uvc_entity *entity;
1115 struct uvc_control *ctrl = NULL;
1116 unsigned int i, found = 0;
1117 __u8 *data;
1118 int ret;
1119
1120 /* Find the extension unit. */
Laurent Pinchart6241d8c2009-11-25 12:00:22 -03001121 list_for_each_entry(entity, &chain->entities, chain) {
1122 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1123 entity->id == xctrl->unit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001124 break;
1125 }
1126
1127 if (entity->id != xctrl->unit) {
1128 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1129 xctrl->unit);
1130 return -EINVAL;
1131 }
1132
1133 /* Find the control. */
1134 for (i = 0; i < entity->ncontrols; ++i) {
1135 ctrl = &entity->controls[i];
1136 if (ctrl->info == NULL)
1137 continue;
1138
1139 if (ctrl->info->selector == xctrl->selector) {
1140 found = 1;
1141 break;
1142 }
1143 }
1144
1145 if (!found) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001146 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1147 entity->extension.guidExtensionCode, xctrl->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001148 return -EINVAL;
1149 }
1150
1151 /* Validate control data size. */
1152 if (ctrl->info->size != xctrl->size)
1153 return -EINVAL;
1154
1155 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1156 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1157 return -EINVAL;
1158
Laurent Pinchart8e113592009-07-01 20:24:47 -03001159 if (mutex_lock_interruptible(&chain->ctrl_mutex))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001160 return -ERESTARTSYS;
1161
1162 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1163 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1164 xctrl->size);
1165 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1166
1167 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1168 ret = -EFAULT;
1169 goto out;
1170 }
1171
Laurent Pinchart8e113592009-07-01 20:24:47 -03001172 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1173 xctrl->unit, chain->dev->intfnum, xctrl->selector,
Laurent Pinchartb482d922009-06-26 11:39:42 -03001174 data, xctrl->size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001175 if (ret < 0)
1176 goto out;
1177
1178 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1179 ret = -EFAULT;
1180 goto out;
1181 }
1182
1183out:
1184 if (ret)
1185 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1186 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1187 xctrl->size);
1188
Laurent Pinchart8e113592009-07-01 20:24:47 -03001189 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001190 return ret;
1191}
1192
1193/* --------------------------------------------------------------------------
1194 * Suspend/resume
1195 */
1196
1197/*
1198 * Restore control values after resume, skipping controls that haven't been
1199 * changed.
1200 *
1201 * TODO
1202 * - Don't restore modified controls that are back to their default value.
1203 * - Handle restore order (Auto-Exposure Mode should be restored before
1204 * Exposure Time).
1205 */
1206int uvc_ctrl_resume_device(struct uvc_device *dev)
1207{
1208 struct uvc_control *ctrl;
1209 struct uvc_entity *entity;
1210 unsigned int i;
1211 int ret;
1212
1213 /* Walk the entities list and restore controls when possible. */
1214 list_for_each_entry(entity, &dev->entities, list) {
1215
1216 for (i = 0; i < entity->ncontrols; ++i) {
1217 ctrl = &entity->controls[i];
1218
1219 if (ctrl->info == NULL || !ctrl->modified ||
1220 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1221 continue;
1222
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001223 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1224 ctrl->info->entity, ctrl->info->index,
1225 ctrl->info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001226 ctrl->dirty = 1;
1227 }
1228
1229 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1230 if (ret < 0)
1231 return ret;
1232 }
1233
1234 return 0;
1235}
1236
1237/* --------------------------------------------------------------------------
1238 * Control and mapping handling
1239 */
1240
1241static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1242 struct uvc_control_info *info)
1243{
1244 struct uvc_entity *entity;
1245 struct uvc_control *ctrl = NULL;
1246 int ret, found = 0;
1247 unsigned int i;
1248
1249 list_for_each_entry(entity, &dev->entities, list) {
1250 if (!uvc_entity_match_guid(entity, info->entity))
1251 continue;
1252
1253 for (i = 0; i < entity->ncontrols; ++i) {
1254 ctrl = &entity->controls[i];
1255 if (ctrl->index == info->index) {
1256 found = 1;
1257 break;
1258 }
1259 }
1260
1261 if (found)
1262 break;
1263 }
1264
1265 if (!found)
1266 return;
1267
Laurent Pinchartb482d922009-06-26 11:39:42 -03001268 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001269 /* Check if the device control information and length match
1270 * the user supplied information.
1271 */
1272 __u32 flags;
1273 __le16 size;
1274 __u8 inf;
1275
Laurent Pinchartb482d922009-06-26 11:39:42 -03001276 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1277 dev->intfnum, info->selector, (__u8 *)&size, 2);
1278 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001279 uvc_trace(UVC_TRACE_CONTROL,
1280 "GET_LEN failed on control %pUl/%u (%d).\n",
1281 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001282 return;
1283 }
1284
1285 if (info->size != le16_to_cpu(size)) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001286 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1287 "doesn't match user supplied value.\n",
1288 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001289 return;
1290 }
1291
Laurent Pinchartb482d922009-06-26 11:39:42 -03001292 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1293 dev->intfnum, info->selector, &inf, 1);
1294 if (ret < 0) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001295 uvc_trace(UVC_TRACE_CONTROL,
1296 "GET_INFO failed on control %pUl/%u (%d).\n",
1297 info->entity, info->selector, ret);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001298 return;
1299 }
1300
1301 flags = info->flags;
1302 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1303 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001304 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1305 "don't match supported operations.\n",
1306 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001307 return;
1308 }
1309 }
1310
1311 ctrl->info = info;
Laurent Pinchart59529082010-01-23 06:30:20 -03001312 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001313 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1314 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1315 dev->udev->devpath, entity->id);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001316}
1317
1318/*
1319 * Add an item to the UVC control information list, and instantiate a control
1320 * structure for each device that supports the control.
1321 */
1322int uvc_ctrl_add_info(struct uvc_control_info *info)
1323{
1324 struct uvc_control_info *ctrl;
1325 struct uvc_device *dev;
1326 int ret = 0;
1327
1328 /* Find matching controls by walking the devices, entities and
1329 * controls list.
1330 */
1331 mutex_lock(&uvc_driver.ctrl_mutex);
1332
1333 /* First check if the list contains a control matching the new one.
1334 * Bail out if it does.
1335 */
1336 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1337 if (memcmp(ctrl->entity, info->entity, 16))
1338 continue;
1339
1340 if (ctrl->selector == info->selector) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001341 uvc_trace(UVC_TRACE_CONTROL,
1342 "Control %pUl/%u is already defined.\n",
1343 info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001344 ret = -EEXIST;
1345 goto end;
1346 }
1347 if (ctrl->index == info->index) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001348 uvc_trace(UVC_TRACE_CONTROL,
1349 "Control %pUl/%u would overwrite index %d.\n",
1350 info->entity, info->selector, info->index);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001351 ret = -EEXIST;
1352 goto end;
1353 }
1354 }
1355
1356 list_for_each_entry(dev, &uvc_driver.devices, list)
1357 uvc_ctrl_add_ctrl(dev, info);
1358
1359 INIT_LIST_HEAD(&info->mappings);
1360 list_add_tail(&info->list, &uvc_driver.controls);
1361end:
1362 mutex_unlock(&uvc_driver.ctrl_mutex);
1363 return ret;
1364}
1365
1366int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1367{
1368 struct uvc_control_info *info;
1369 struct uvc_control_mapping *map;
1370 int ret = -EINVAL;
1371
Laurent Pinchart97683522008-12-16 06:46:32 -03001372 if (mapping->get == NULL)
1373 mapping->get = uvc_get_le_value;
1374 if (mapping->set == NULL)
1375 mapping->set = uvc_set_le_value;
1376
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001377 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1378 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1379 "invalid control id 0x%08x\n", mapping->name,
1380 mapping->id);
1381 return -EINVAL;
1382 }
1383
1384 mutex_lock(&uvc_driver.ctrl_mutex);
1385 list_for_each_entry(info, &uvc_driver.controls, list) {
1386 if (memcmp(info->entity, mapping->entity, 16) ||
1387 info->selector != mapping->selector)
1388 continue;
1389
1390 if (info->size * 8 < mapping->size + mapping->offset) {
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001391 uvc_trace(UVC_TRACE_CONTROL,
1392 "Mapping '%s' would overflow control %pUl/%u\n",
1393 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001394 ret = -EOVERFLOW;
1395 goto end;
1396 }
1397
1398 /* Check if the list contains a mapping matching the new one.
1399 * Bail out if it does.
1400 */
1401 list_for_each_entry(map, &info->mappings, list) {
1402 if (map->id == mapping->id) {
1403 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1404 "already defined.\n", mapping->name);
1405 ret = -EEXIST;
1406 goto end;
1407 }
1408 }
1409
1410 mapping->ctrl = info;
1411 list_add_tail(&mapping->list, &info->mappings);
Laurent Pinchart36bd8832010-01-19 09:06:22 -03001412 uvc_trace(UVC_TRACE_CONTROL,
1413 "Adding mapping %s to control %pUl/%u.\n",
1414 mapping->name, info->entity, info->selector);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001415
1416 ret = 0;
1417 break;
1418 }
1419end:
1420 mutex_unlock(&uvc_driver.ctrl_mutex);
1421 return ret;
1422}
1423
1424/*
Laurent Pinchartd732c442009-05-31 17:05:55 -03001425 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1426 * are currently the ones that crash the camera or unconditionally return an
1427 * error when queried.
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001428 */
1429static void
Laurent Pinchartd732c442009-05-31 17:05:55 -03001430uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001431{
1432 static const struct {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001433 struct usb_device_id id;
1434 u8 index;
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001435 } blacklist[] = {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001436 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1437 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001438 };
1439
1440 u8 *controls;
1441 unsigned int size;
1442 unsigned int i;
1443
Laurent Pinchartb482d922009-06-26 11:39:42 -03001444 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001445 return;
1446
1447 controls = entity->processing.bmControls;
1448 size = entity->processing.bControlSize;
1449
1450 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
Laurent Pinchart385097e2009-12-09 22:31:21 -03001451 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001452 continue;
1453
Laurent Pinchartd732c442009-05-31 17:05:55 -03001454 if (blacklist[i].index >= 8 * size ||
1455 !uvc_test_bit(controls, blacklist[i].index))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001456 continue;
1457
Laurent Pinchartd732c442009-05-31 17:05:55 -03001458 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1459 "removing it.\n", entity->id, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001460
Laurent Pinchartd732c442009-05-31 17:05:55 -03001461 uvc_clear_bit(controls, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001462 }
1463}
1464
1465/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001466 * Initialize device controls.
1467 */
1468int uvc_ctrl_init_device(struct uvc_device *dev)
1469{
1470 struct uvc_control_info *info;
1471 struct uvc_control *ctrl;
1472 struct uvc_entity *entity;
1473 unsigned int i;
1474
1475 /* Walk the entities list and instantiate controls */
1476 list_for_each_entry(entity, &dev->entities, list) {
1477 unsigned int bControlSize = 0, ncontrols = 0;
1478 __u8 *bmControls = NULL;
1479
Laurent Pinchartb482d922009-06-26 11:39:42 -03001480 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001481 bmControls = entity->extension.bmControls;
1482 bControlSize = entity->extension.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001483 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001484 bmControls = entity->processing.bmControls;
1485 bControlSize = entity->processing.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001486 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001487 bmControls = entity->camera.bmControls;
1488 bControlSize = entity->camera.bControlSize;
1489 }
1490
Laurent Pinchartd732c442009-05-31 17:05:55 -03001491 uvc_ctrl_prune_entity(dev, entity);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001492
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001493 for (i = 0; i < bControlSize; ++i)
1494 ncontrols += hweight8(bmControls[i]);
1495
1496 if (ncontrols == 0)
1497 continue;
1498
1499 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1500 if (entity->controls == NULL)
1501 return -ENOMEM;
1502
1503 entity->ncontrols = ncontrols;
1504
1505 ctrl = entity->controls;
1506 for (i = 0; i < bControlSize * 8; ++i) {
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001507 if (uvc_test_bit(bmControls, i) == 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001508 continue;
1509
1510 ctrl->entity = entity;
1511 ctrl->index = i;
1512 ctrl++;
1513 }
1514 }
1515
1516 /* Walk the controls info list and associate them with the device
1517 * controls, then add the device to the global device list. This has
1518 * to be done while holding the controls lock, to make sure
1519 * uvc_ctrl_add_info() will not get called in-between.
1520 */
1521 mutex_lock(&uvc_driver.ctrl_mutex);
1522 list_for_each_entry(info, &uvc_driver.controls, list)
1523 uvc_ctrl_add_ctrl(dev, info);
1524
1525 list_add_tail(&dev->list, &uvc_driver.devices);
1526 mutex_unlock(&uvc_driver.ctrl_mutex);
1527
1528 return 0;
1529}
1530
1531/*
1532 * Cleanup device controls.
1533 */
1534void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1535{
1536 struct uvc_entity *entity;
1537 unsigned int i;
1538
1539 /* Remove the device from the global devices list */
1540 mutex_lock(&uvc_driver.ctrl_mutex);
1541 if (dev->list.next != NULL)
1542 list_del(&dev->list);
1543 mutex_unlock(&uvc_driver.ctrl_mutex);
1544
1545 list_for_each_entry(entity, &dev->entities, list) {
1546 for (i = 0; i < entity->ncontrols; ++i)
1547 kfree(entity->controls[i].data);
1548
1549 kfree(entity->controls);
1550 }
1551}
1552
1553void uvc_ctrl_init(void)
1554{
1555 struct uvc_control_info *ctrl = uvc_ctrls;
1556 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1557 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1558 struct uvc_control_mapping *mend =
1559 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1560
1561 for (; ctrl < cend; ++ctrl)
1562 uvc_ctrl_add_info(ctrl);
1563
1564 for (; mapping < mend; ++mapping)
1565 uvc_ctrl_add_mapping(mapping);
1566}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001567