blob: 6f1487fc3d7c7d70766ff8e825a5ac51d045918b [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
26#define UVC_CTRL_NDATA 2
27#define UVC_CTRL_DATA_CURRENT 0
28#define UVC_CTRL_DATA_BACKUP 1
29
30/* ------------------------------------------------------------------------
Laurent Pinchart2c2d2642009-01-03 19:12:40 -030031 * Controls
Laurent Pinchartc0efd232008-06-30 15:04:50 -030032 */
33
34static struct uvc_control_info uvc_ctrls[] = {
35 {
36 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030037 .selector = UVC_PU_BRIGHTNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030038 .index = 0,
39 .size = 2,
40 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
41 | UVC_CONTROL_RESTORE,
42 },
43 {
44 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030045 .selector = UVC_PU_CONTRAST_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030046 .index = 1,
47 .size = 2,
48 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
49 | UVC_CONTROL_RESTORE,
50 },
51 {
52 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030053 .selector = UVC_PU_HUE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030054 .index = 2,
55 .size = 2,
56 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
57 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
58 },
59 {
60 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030061 .selector = UVC_PU_SATURATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030062 .index = 3,
63 .size = 2,
64 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
65 | UVC_CONTROL_RESTORE,
66 },
67 {
68 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030069 .selector = UVC_PU_SHARPNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030070 .index = 4,
71 .size = 2,
72 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
73 | UVC_CONTROL_RESTORE,
74 },
75 {
76 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030077 .selector = UVC_PU_GAMMA_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030078 .index = 5,
79 .size = 2,
80 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
81 | UVC_CONTROL_RESTORE,
82 },
83 {
84 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030085 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -030086 .index = 6,
87 .size = 2,
88 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
89 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
90 },
91 {
92 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -030093 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -030094 .index = 7,
95 .size = 4,
96 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
97 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
98 },
99 {
100 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300101 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300102 .index = 8,
103 .size = 2,
104 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
105 | UVC_CONTROL_RESTORE,
106 },
107 {
108 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300109 .selector = UVC_PU_GAIN_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300110 .index = 9,
111 .size = 2,
112 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
113 | UVC_CONTROL_RESTORE,
114 },
115 {
116 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300117 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300118 .index = 10,
119 .size = 1,
120 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
121 | UVC_CONTROL_RESTORE,
122 },
123 {
124 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300125 .selector = UVC_PU_HUE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300126 .index = 11,
127 .size = 1,
128 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
129 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
130 },
131 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300132 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300133 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300134 .index = 12,
135 .size = 1,
136 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
137 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
138 },
139 {
140 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300141 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300142 .index = 13,
143 .size = 1,
144 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
145 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
146 },
147 {
148 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300149 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300150 .index = 14,
151 .size = 2,
152 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
153 | UVC_CONTROL_RESTORE,
154 },
155 {
156 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300157 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300158 .index = 15,
159 .size = 2,
160 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
161 | UVC_CONTROL_RESTORE,
162 },
163 {
164 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300165 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300166 .index = 16,
167 .size = 1,
168 .flags = UVC_CONTROL_GET_CUR,
169 },
170 {
171 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300172 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300173 .index = 17,
174 .size = 1,
175 .flags = UVC_CONTROL_GET_CUR,
176 },
177 {
178 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300179 .selector = UVC_CT_SCANNING_MODE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300180 .index = 0,
181 .size = 1,
182 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
183 | UVC_CONTROL_RESTORE,
184 },
185 {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300186 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300187 .selector = UVC_CT_AE_MODE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300188 .index = 1,
189 .size = 1,
190 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
191 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
192 | UVC_CONTROL_RESTORE,
193 },
194 {
195 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300196 .selector = UVC_CT_AE_PRIORITY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300197 .index = 2,
198 .size = 1,
199 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
200 | UVC_CONTROL_RESTORE,
201 },
202 {
203 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300204 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300205 .index = 3,
206 .size = 4,
207 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
208 | UVC_CONTROL_RESTORE,
209 },
210 {
211 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300212 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300213 .index = 4,
214 .size = 1,
215 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
216 | UVC_CONTROL_RESTORE,
217 },
218 {
219 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300220 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300221 .index = 5,
222 .size = 2,
223 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
224 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
225 },
226 {
227 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300228 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300229 .index = 6,
230 .size = 2,
231 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
232 | UVC_CONTROL_AUTO_UPDATE,
233 },
234 {
235 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300236 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300237 .index = 7,
238 .size = 2,
239 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
240 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
241 },
242 {
243 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300244 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300245 .index = 8,
246 .size = 1,
247 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
248 | UVC_CONTROL_AUTO_UPDATE,
249 },
250 {
251 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300252 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300253 .index = 9,
254 .size = 2,
255 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
256 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
257 },
258 {
259 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300260 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300261 .index = 10,
262 .size = 3,
263 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
264 | UVC_CONTROL_AUTO_UPDATE,
265 },
266 {
267 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300268 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300269 .index = 11,
270 .size = 8,
271 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
272 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
273 },
274 {
275 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300276 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300277 .index = 12,
278 .size = 4,
279 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
280 | UVC_CONTROL_AUTO_UPDATE,
281 },
282 {
283 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300284 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300285 .index = 13,
286 .size = 2,
287 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
288 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
289 },
290 {
291 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300292 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300293 .index = 14,
294 .size = 2,
295 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
296 | UVC_CONTROL_AUTO_UPDATE,
297 },
298 {
299 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300300 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300301 .index = 17,
302 .size = 1,
303 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
304 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
305 },
306 {
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300307 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300308 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart5e26d502008-09-22 13:14:59 -0300309 .index = 18,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300310 .size = 1,
311 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300312 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
313 },
314};
315
316static struct uvc_menu_info power_line_frequency_controls[] = {
317 { 0, "Disabled" },
318 { 1, "50 Hz" },
319 { 2, "60 Hz" },
320};
321
322static struct uvc_menu_info exposure_auto_controls[] = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300323 { 2, "Auto Mode" },
Laurent Pinchart90ac5ea2008-07-26 11:42:29 -0300324 { 1, "Manual Mode" },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300325 { 4, "Shutter Priority Mode" },
326 { 8, "Aperture Priority Mode" },
327};
328
Laurent Pinchart97683522008-12-16 06:46:32 -0300329static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
330 __u8 query, const __u8 *data)
331{
332 __s8 zoom = (__s8)data[0];
333
334 switch (query) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300335 case UVC_GET_CUR:
Laurent Pinchart97683522008-12-16 06:46:32 -0300336 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
337
Laurent Pinchartb482d922009-06-26 11:39:42 -0300338 case UVC_GET_MIN:
339 case UVC_GET_MAX:
340 case UVC_GET_RES:
341 case UVC_GET_DEF:
Laurent Pinchart97683522008-12-16 06:46:32 -0300342 default:
343 return data[2];
344 }
345}
346
347static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
348 __s32 value, __u8 *data)
349{
350 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
Laurent Pinchart2e896132009-11-04 13:11:10 -0300351 data[2] = min((int)abs(value), 0xff);
Laurent Pinchart97683522008-12-16 06:46:32 -0300352}
353
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300354static struct uvc_control_mapping uvc_ctrl_mappings[] = {
355 {
356 .id = V4L2_CID_BRIGHTNESS,
357 .name = "Brightness",
358 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300359 .selector = UVC_PU_BRIGHTNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300360 .size = 16,
361 .offset = 0,
362 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
363 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
364 },
365 {
366 .id = V4L2_CID_CONTRAST,
367 .name = "Contrast",
368 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300369 .selector = UVC_PU_CONTRAST_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300370 .size = 16,
371 .offset = 0,
372 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
373 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
374 },
375 {
376 .id = V4L2_CID_HUE,
377 .name = "Hue",
378 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300379 .selector = UVC_PU_HUE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300380 .size = 16,
381 .offset = 0,
382 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
383 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
384 },
385 {
386 .id = V4L2_CID_SATURATION,
387 .name = "Saturation",
388 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300389 .selector = UVC_PU_SATURATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300390 .size = 16,
391 .offset = 0,
392 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
393 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
394 },
395 {
396 .id = V4L2_CID_SHARPNESS,
397 .name = "Sharpness",
398 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300399 .selector = UVC_PU_SHARPNESS_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300400 .size = 16,
401 .offset = 0,
402 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
403 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
404 },
405 {
406 .id = V4L2_CID_GAMMA,
407 .name = "Gamma",
408 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300409 .selector = UVC_PU_GAMMA_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300410 .size = 16,
411 .offset = 0,
412 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
413 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
414 },
415 {
416 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
417 .name = "Backlight Compensation",
418 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300419 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300420 .size = 16,
421 .offset = 0,
422 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
423 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
424 },
425 {
426 .id = V4L2_CID_GAIN,
427 .name = "Gain",
428 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300429 .selector = UVC_PU_GAIN_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300430 .size = 16,
431 .offset = 0,
432 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
433 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
434 },
435 {
436 .id = V4L2_CID_POWER_LINE_FREQUENCY,
437 .name = "Power Line Frequency",
438 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300439 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300440 .size = 2,
441 .offset = 0,
442 .v4l2_type = V4L2_CTRL_TYPE_MENU,
443 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
444 .menu_info = power_line_frequency_controls,
445 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
446 },
447 {
448 .id = V4L2_CID_HUE_AUTO,
449 .name = "Hue, Auto",
450 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300451 .selector = UVC_PU_HUE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452 .size = 1,
453 .offset = 0,
454 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
455 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
456 },
457 {
458 .id = V4L2_CID_EXPOSURE_AUTO,
459 .name = "Exposure, Auto",
460 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300461 .selector = UVC_CT_AE_MODE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300462 .size = 4,
463 .offset = 0,
464 .v4l2_type = V4L2_CTRL_TYPE_MENU,
465 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
466 .menu_info = exposure_auto_controls,
467 .menu_count = ARRAY_SIZE(exposure_auto_controls),
468 },
469 {
470 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
471 .name = "Exposure, Auto Priority",
472 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300473 .selector = UVC_CT_AE_PRIORITY_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300474 .size = 1,
475 .offset = 0,
476 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
477 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
478 },
479 {
480 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
481 .name = "Exposure (Absolute)",
482 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300483 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300484 .size = 32,
485 .offset = 0,
486 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
487 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
488 },
489 {
490 .id = V4L2_CID_AUTO_WHITE_BALANCE,
491 .name = "White Balance Temperature, Auto",
492 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300493 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300494 .size = 1,
495 .offset = 0,
496 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
497 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
498 },
499 {
500 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
501 .name = "White Balance Temperature",
502 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300503 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300504 .size = 16,
505 .offset = 0,
506 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
507 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
508 },
509 {
510 .id = V4L2_CID_AUTO_WHITE_BALANCE,
511 .name = "White Balance Component, Auto",
512 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300513 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300514 .size = 1,
515 .offset = 0,
516 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
517 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
518 },
519 {
520 .id = V4L2_CID_BLUE_BALANCE,
521 .name = "White Balance Blue Component",
522 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300523 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300524 .size = 16,
525 .offset = 0,
526 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
527 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
528 },
529 {
530 .id = V4L2_CID_RED_BALANCE,
531 .name = "White Balance Red Component",
532 .entity = UVC_GUID_UVC_PROCESSING,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300533 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300534 .size = 16,
535 .offset = 16,
536 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
537 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
538 },
539 {
540 .id = V4L2_CID_FOCUS_ABSOLUTE,
541 .name = "Focus (absolute)",
542 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300543 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300544 .size = 16,
545 .offset = 0,
546 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
547 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
548 },
549 {
550 .id = V4L2_CID_FOCUS_AUTO,
551 .name = "Focus, Auto",
552 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300553 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300554 .size = 1,
555 .offset = 0,
556 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
557 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
558 },
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300559 {
Laurent Pinchart97683522008-12-16 06:46:32 -0300560 .id = V4L2_CID_ZOOM_ABSOLUTE,
561 .name = "Zoom, Absolute",
562 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300563 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300564 .size = 16,
565 .offset = 0,
566 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
567 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
568 },
569 {
570 .id = V4L2_CID_ZOOM_CONTINUOUS,
571 .name = "Zoom, Continuous",
572 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300573 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
Laurent Pinchart97683522008-12-16 06:46:32 -0300574 .size = 0,
575 .offset = 0,
576 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
577 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
578 .get = uvc_ctrl_get_zoom,
579 .set = uvc_ctrl_set_zoom,
580 },
581 {
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300582 .id = V4L2_CID_PRIVACY,
583 .name = "Privacy",
584 .entity = UVC_GUID_UVC_CAMERA,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300585 .selector = UVC_CT_PRIVACY_CONTROL,
Laurent Pinchart6df126f2008-12-16 06:44:11 -0300586 .size = 1,
587 .offset = 0,
588 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
589 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
590 },
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300591};
592
593/* ------------------------------------------------------------------------
594 * Utility functions
595 */
596
597static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
598{
599 return ctrl->data + id * ctrl->info->size;
600}
601
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300602static inline int uvc_test_bit(const __u8 *data, int bit)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300603{
604 return (data[bit >> 3] >> (bit & 7)) & 1;
605}
606
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -0300607static inline void uvc_clear_bit(__u8 *data, int bit)
608{
609 data[bit >> 3] &= ~(1 << (bit & 7));
610}
611
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300612/* Extract the bit string specified by mapping->offset and mapping->size
613 * from the little-endian data stored at 'data' and return the result as
614 * a signed 32bit integer. Sign extension will be performed if the mapping
615 * references a signed data type.
616 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300617static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
618 __u8 query, const __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300619{
620 int bits = mapping->size;
621 int offset = mapping->offset;
622 __s32 value = 0;
623 __u8 mask;
624
625 data += offset / 8;
626 offset &= 7;
627 mask = ((1LL << bits) - 1) << offset;
628
629 for (; bits > 0; data++) {
630 __u8 byte = *data & mask;
631 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
632 bits -= 8 - (offset > 0 ? offset : 0);
633 offset -= 8;
634 mask = (1 << bits) - 1;
635 }
636
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300637 /* Sign-extend the value if needed. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300638 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
639 value |= -(value & (1 << (mapping->size - 1)));
640
641 return value;
642}
643
644/* Set the bit string specified by mapping->offset and mapping->size
645 * in the little-endian data stored at 'data' to the value 'value'.
646 */
Laurent Pinchart97683522008-12-16 06:46:32 -0300647static void uvc_set_le_value(struct uvc_control_mapping *mapping,
648 __s32 value, __u8 *data)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300649{
650 int bits = mapping->size;
651 int offset = mapping->offset;
652 __u8 mask;
653
654 data += offset / 8;
655 offset &= 7;
656
657 for (; bits > 0; data++) {
658 mask = ((1LL << bits) - 1) << offset;
659 *data = (*data & ~mask) | ((value << offset) & mask);
660 value >>= offset ? offset : 8;
661 bits -= 8 - offset;
662 offset = 0;
663 }
664}
665
666/* ------------------------------------------------------------------------
667 * Terminal and unit management
668 */
669
670static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
671static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
672static const __u8 uvc_media_transport_input_guid[16] =
673 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
674
675static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
676{
677 switch (UVC_ENTITY_TYPE(entity)) {
Laurent Pinchartb482d922009-06-26 11:39:42 -0300678 case UVC_ITT_CAMERA:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300679 return memcmp(uvc_camera_guid, guid, 16) == 0;
680
Laurent Pinchartb482d922009-06-26 11:39:42 -0300681 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300682 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
683
Laurent Pinchartb482d922009-06-26 11:39:42 -0300684 case UVC_VC_PROCESSING_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300685 return memcmp(uvc_processing_guid, guid, 16) == 0;
686
Laurent Pinchartb482d922009-06-26 11:39:42 -0300687 case UVC_VC_EXTENSION_UNIT:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300688 return memcmp(entity->extension.guidExtensionCode,
689 guid, 16) == 0;
690
691 default:
692 return 0;
693 }
694}
695
696/* ------------------------------------------------------------------------
697 * UVC Controls
698 */
699
700static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
701 struct uvc_control_mapping **mapping, struct uvc_control **control,
702 int next)
703{
704 struct uvc_control *ctrl;
705 struct uvc_control_mapping *map;
706 unsigned int i;
707
708 if (entity == NULL)
709 return;
710
711 for (i = 0; i < entity->ncontrols; ++i) {
712 ctrl = &entity->controls[i];
713 if (ctrl->info == NULL)
714 continue;
715
716 list_for_each_entry(map, &ctrl->info->mappings, list) {
717 if ((map->id == v4l2_id) && !next) {
718 *control = ctrl;
719 *mapping = map;
720 return;
721 }
722
723 if ((*mapping == NULL || (*mapping)->id > map->id) &&
724 (map->id > v4l2_id) && next) {
725 *control = ctrl;
726 *mapping = map;
727 }
728 }
729 }
730}
731
Laurent Pinchart8e113592009-07-01 20:24:47 -0300732struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300733 __u32 v4l2_id, struct uvc_control_mapping **mapping)
734{
735 struct uvc_control *ctrl = NULL;
736 struct uvc_entity *entity;
737 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
738
739 *mapping = NULL;
740
741 /* Mask the query flags. */
742 v4l2_id &= V4L2_CTRL_ID_MASK;
743
744 /* Find the control. */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300745 __uvc_find_control(chain->processing, v4l2_id, mapping, &ctrl, next);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300746 if (ctrl && !next)
747 return ctrl;
748
Laurent Pinchart8e113592009-07-01 20:24:47 -0300749 list_for_each_entry(entity, &chain->iterms, 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
Laurent Pinchart8e113592009-07-01 20:24:47 -0300755 list_for_each_entry(entity, &chain->extensions, chain) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300756 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
757 if (ctrl && !next)
758 return ctrl;
759 }
760
761 if (ctrl == NULL && !next)
762 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
763 v4l2_id);
764
765 return ctrl;
766}
767
Laurent Pinchart8e113592009-07-01 20:24:47 -0300768int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300769 struct v4l2_queryctrl *v4l2_ctrl)
770{
771 struct uvc_control *ctrl;
772 struct uvc_control_mapping *mapping;
773 struct uvc_menu_info *menu;
774 unsigned int i;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300775 __u8 *data;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300776 int ret;
777
Laurent Pinchart8e113592009-07-01 20:24:47 -0300778 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300779 if (ctrl == NULL)
780 return -EINVAL;
781
Ralph Loaderfe6c7002008-09-22 21:06:48 -0300782 data = kmalloc(ctrl->info->size, GFP_KERNEL);
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300783 if (data == NULL)
784 return -ENOMEM;
785
Laurent Pinchart54812c72008-07-17 07:37:37 -0300786 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300787 v4l2_ctrl->id = mapping->id;
788 v4l2_ctrl->type = mapping->v4l2_type;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300789 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300790 v4l2_ctrl->flags = 0;
791
792 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
793 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
794
795 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300796 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
797 chain->dev->intfnum, ctrl->info->selector,
798 data, ctrl->info->size);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300799 if (ret < 0)
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300800 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300801 v4l2_ctrl->default_value =
802 mapping->get(mapping, UVC_GET_DEF, data);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300803 }
804
Laurent Pinchart54812c72008-07-17 07:37:37 -0300805 switch (mapping->v4l2_type) {
806 case V4L2_CTRL_TYPE_MENU:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300807 v4l2_ctrl->minimum = 0;
808 v4l2_ctrl->maximum = mapping->menu_count - 1;
809 v4l2_ctrl->step = 1;
810
811 menu = mapping->menu_info;
812 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
813 if (menu->value == v4l2_ctrl->default_value) {
814 v4l2_ctrl->default_value = i;
815 break;
816 }
817 }
818
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300819 ret = 0;
820 goto out;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300821
822 case V4L2_CTRL_TYPE_BOOLEAN:
823 v4l2_ctrl->minimum = 0;
824 v4l2_ctrl->maximum = 1;
825 v4l2_ctrl->step = 1;
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300826 ret = 0;
827 goto out;
Laurent Pinchart54812c72008-07-17 07:37:37 -0300828
Laurent Pinchartf4eabaf2009-08-06 06:05:40 -0300829 case V4L2_CTRL_TYPE_BUTTON:
830 v4l2_ctrl->minimum = 0;
831 v4l2_ctrl->maximum = 0;
832 v4l2_ctrl->step = 0;
833 ret = 0;
834 goto out;
835
Laurent Pinchart54812c72008-07-17 07:37:37 -0300836 default:
837 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300838 }
839
840 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300841 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
842 chain->dev->intfnum, ctrl->info->selector,
843 data, ctrl->info->size);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300844 if (ret < 0)
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300845 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300846 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, data);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300847 }
848 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300849 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
850 chain->dev->intfnum, ctrl->info->selector,
851 data, ctrl->info->size);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300852 if (ret < 0)
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300853 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300854 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, data);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300855 }
856 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300857 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
858 chain->dev->intfnum, ctrl->info->selector,
859 data, ctrl->info->size);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300860 if (ret < 0)
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300861 goto out;
Laurent Pinchartb482d922009-06-26 11:39:42 -0300862 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, data);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300863 }
864
Laurent Pinchart04793dd2008-07-31 17:11:12 -0300865 ret = 0;
866out:
867 kfree(data);
868 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300869}
870
871
872/* --------------------------------------------------------------------------
873 * Control transactions
874 *
875 * To make extended set operations as atomic as the hardware allows, controls
876 * are handled using begin/commit/rollback operations.
877 *
878 * At the beginning of a set request, uvc_ctrl_begin should be called to
879 * initialize the request. This function acquires the control lock.
880 *
881 * When setting a control, the new value is stored in the control data field
882 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
883 * later processing. If the UVC and V4L2 control sizes differ, the current
884 * value is loaded from the hardware before storing the new value in the data
885 * field.
886 *
887 * After processing all controls in the transaction, uvc_ctrl_commit or
888 * uvc_ctrl_rollback must be called to apply the pending changes to the
889 * hardware or revert them. When applying changes, all controls marked as
890 * dirty will be modified in the UVC device, and the dirty flag will be
891 * cleared. When reverting controls, the control data field
892 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
893 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
894 * control lock.
895 */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300896int uvc_ctrl_begin(struct uvc_video_chain *chain)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300897{
Laurent Pinchart8e113592009-07-01 20:24:47 -0300898 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300899}
900
901static int uvc_ctrl_commit_entity(struct uvc_device *dev,
902 struct uvc_entity *entity, int rollback)
903{
904 struct uvc_control *ctrl;
905 unsigned int i;
906 int ret;
907
908 if (entity == NULL)
909 return 0;
910
911 for (i = 0; i < entity->ncontrols; ++i) {
912 ctrl = &entity->controls[i];
Laurent Pinchartb1accfa2008-09-27 20:54:02 -0300913 if (ctrl->info == NULL)
914 continue;
915
916 /* Reset the loaded flag for auto-update controls that were
917 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
918 * uvc_ctrl_get from using the cached value.
919 */
920 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
921 ctrl->loaded = 0;
922
923 if (!ctrl->dirty)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300924 continue;
925
926 if (!rollback)
Laurent Pinchartb482d922009-06-26 11:39:42 -0300927 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300928 dev->intfnum, ctrl->info->selector,
929 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
930 ctrl->info->size);
931 else
932 ret = 0;
933
934 if (rollback || ret < 0)
935 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
936 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
937 ctrl->info->size);
938
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300939 ctrl->dirty = 0;
940
941 if (ret < 0)
942 return ret;
943 }
944
945 return 0;
946}
947
Laurent Pinchart8e113592009-07-01 20:24:47 -0300948int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300949{
950 struct uvc_entity *entity;
951 int ret = 0;
952
953 /* Find the control. */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300954 ret = uvc_ctrl_commit_entity(chain->dev, chain->processing, rollback);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300955 if (ret < 0)
956 goto done;
957
Laurent Pinchart8e113592009-07-01 20:24:47 -0300958 list_for_each_entry(entity, &chain->iterms, chain) {
959 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300960 if (ret < 0)
961 goto done;
962 }
963
Laurent Pinchart8e113592009-07-01 20:24:47 -0300964 list_for_each_entry(entity, &chain->extensions, chain) {
965 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300966 if (ret < 0)
967 goto done;
968 }
969
970done:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300971 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300972 return ret;
973}
974
Laurent Pinchart8e113592009-07-01 20:24:47 -0300975int uvc_ctrl_get(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300976 struct v4l2_ext_control *xctrl)
977{
978 struct uvc_control *ctrl;
979 struct uvc_control_mapping *mapping;
980 struct uvc_menu_info *menu;
981 unsigned int i;
982 int ret;
983
Laurent Pinchart8e113592009-07-01 20:24:47 -0300984 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300985 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
986 return -EINVAL;
987
988 if (!ctrl->loaded) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300989 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
990 chain->dev->intfnum, ctrl->info->selector,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300991 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
992 ctrl->info->size);
993 if (ret < 0)
994 return ret;
995
Laurent Pinchartb1accfa2008-09-27 20:54:02 -0300996 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300997 }
998
Laurent Pinchartb482d922009-06-26 11:39:42 -0300999 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
Laurent Pinchart97683522008-12-16 06:46:32 -03001000 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001001
1002 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1003 menu = mapping->menu_info;
1004 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1005 if (menu->value == xctrl->value) {
1006 xctrl->value = i;
1007 break;
1008 }
1009 }
1010 }
1011
1012 return 0;
1013}
1014
Laurent Pinchart8e113592009-07-01 20:24:47 -03001015int uvc_ctrl_set(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001016 struct v4l2_ext_control *xctrl)
1017{
1018 struct uvc_control *ctrl;
1019 struct uvc_control_mapping *mapping;
1020 s32 value = xctrl->value;
1021 int ret;
1022
Laurent Pinchart8e113592009-07-01 20:24:47 -03001023 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001024 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1025 return -EINVAL;
1026
1027 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1028 if (value < 0 || value >= mapping->menu_count)
1029 return -EINVAL;
1030 value = mapping->menu_info[value].value;
1031 }
1032
1033 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1034 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1035 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1036 0, ctrl->info->size);
1037 } else {
Laurent Pinchart8e113592009-07-01 20:24:47 -03001038 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1039 ctrl->entity->id, chain->dev->intfnum,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001040 ctrl->info->selector,
1041 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1042 ctrl->info->size);
1043 if (ret < 0)
1044 return ret;
1045 }
1046
Laurent Pinchartb1accfa2008-09-27 20:54:02 -03001047 ctrl->loaded = 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001048 }
1049
1050 if (!ctrl->dirty) {
1051 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1052 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1053 ctrl->info->size);
1054 }
1055
Laurent Pinchart97683522008-12-16 06:46:32 -03001056 mapping->set(mapping, value,
1057 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001058
1059 ctrl->dirty = 1;
1060 ctrl->modified = 1;
1061 return 0;
1062}
1063
1064/* --------------------------------------------------------------------------
1065 * Dynamic controls
1066 */
1067
Laurent Pinchart8e113592009-07-01 20:24:47 -03001068int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001069 struct uvc_xu_control *xctrl, int set)
1070{
1071 struct uvc_entity *entity;
1072 struct uvc_control *ctrl = NULL;
1073 unsigned int i, found = 0;
1074 __u8 *data;
1075 int ret;
1076
1077 /* Find the extension unit. */
Laurent Pinchart8e113592009-07-01 20:24:47 -03001078 list_for_each_entry(entity, &chain->extensions, chain) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001079 if (entity->id == xctrl->unit)
1080 break;
1081 }
1082
1083 if (entity->id != xctrl->unit) {
1084 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1085 xctrl->unit);
1086 return -EINVAL;
1087 }
1088
1089 /* Find the control. */
1090 for (i = 0; i < entity->ncontrols; ++i) {
1091 ctrl = &entity->controls[i];
1092 if (ctrl->info == NULL)
1093 continue;
1094
1095 if (ctrl->info->selector == xctrl->selector) {
1096 found = 1;
1097 break;
1098 }
1099 }
1100
1101 if (!found) {
1102 uvc_trace(UVC_TRACE_CONTROL,
1103 "Control " UVC_GUID_FORMAT "/%u not found.\n",
1104 UVC_GUID_ARGS(entity->extension.guidExtensionCode),
1105 xctrl->selector);
1106 return -EINVAL;
1107 }
1108
1109 /* Validate control data size. */
1110 if (ctrl->info->size != xctrl->size)
1111 return -EINVAL;
1112
1113 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1114 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1115 return -EINVAL;
1116
Laurent Pinchart8e113592009-07-01 20:24:47 -03001117 if (mutex_lock_interruptible(&chain->ctrl_mutex))
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001118 return -ERESTARTSYS;
1119
1120 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1121 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1122 xctrl->size);
1123 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1124
1125 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1126 ret = -EFAULT;
1127 goto out;
1128 }
1129
Laurent Pinchart8e113592009-07-01 20:24:47 -03001130 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1131 xctrl->unit, chain->dev->intfnum, xctrl->selector,
Laurent Pinchartb482d922009-06-26 11:39:42 -03001132 data, xctrl->size);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001133 if (ret < 0)
1134 goto out;
1135
1136 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1137 ret = -EFAULT;
1138 goto out;
1139 }
1140
1141out:
1142 if (ret)
1143 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1144 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1145 xctrl->size);
1146
Laurent Pinchart8e113592009-07-01 20:24:47 -03001147 mutex_unlock(&chain->ctrl_mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001148 return ret;
1149}
1150
1151/* --------------------------------------------------------------------------
1152 * Suspend/resume
1153 */
1154
1155/*
1156 * Restore control values after resume, skipping controls that haven't been
1157 * changed.
1158 *
1159 * TODO
1160 * - Don't restore modified controls that are back to their default value.
1161 * - Handle restore order (Auto-Exposure Mode should be restored before
1162 * Exposure Time).
1163 */
1164int uvc_ctrl_resume_device(struct uvc_device *dev)
1165{
1166 struct uvc_control *ctrl;
1167 struct uvc_entity *entity;
1168 unsigned int i;
1169 int ret;
1170
1171 /* Walk the entities list and restore controls when possible. */
1172 list_for_each_entry(entity, &dev->entities, list) {
1173
1174 for (i = 0; i < entity->ncontrols; ++i) {
1175 ctrl = &entity->controls[i];
1176
1177 if (ctrl->info == NULL || !ctrl->modified ||
1178 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1179 continue;
1180
1181 printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
1182 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
1183 ctrl->info->index, ctrl->info->selector);
1184 ctrl->dirty = 1;
1185 }
1186
1187 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1188 if (ret < 0)
1189 return ret;
1190 }
1191
1192 return 0;
1193}
1194
1195/* --------------------------------------------------------------------------
1196 * Control and mapping handling
1197 */
1198
1199static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1200 struct uvc_control_info *info)
1201{
1202 struct uvc_entity *entity;
1203 struct uvc_control *ctrl = NULL;
1204 int ret, found = 0;
1205 unsigned int i;
1206
1207 list_for_each_entry(entity, &dev->entities, list) {
1208 if (!uvc_entity_match_guid(entity, info->entity))
1209 continue;
1210
1211 for (i = 0; i < entity->ncontrols; ++i) {
1212 ctrl = &entity->controls[i];
1213 if (ctrl->index == info->index) {
1214 found = 1;
1215 break;
1216 }
1217 }
1218
1219 if (found)
1220 break;
1221 }
1222
1223 if (!found)
1224 return;
1225
Laurent Pinchartb482d922009-06-26 11:39:42 -03001226 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001227 /* Check if the device control information and length match
1228 * the user supplied information.
1229 */
1230 __u32 flags;
1231 __le16 size;
1232 __u8 inf;
1233
Laurent Pinchartb482d922009-06-26 11:39:42 -03001234 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1235 dev->intfnum, info->selector, (__u8 *)&size, 2);
1236 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001237 uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1238 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1239 UVC_GUID_ARGS(info->entity), info->selector,
1240 ret);
1241 return;
1242 }
1243
1244 if (info->size != le16_to_cpu(size)) {
1245 uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1246 "/%u size doesn't match user supplied "
1247 "value.\n", UVC_GUID_ARGS(info->entity),
1248 info->selector);
1249 return;
1250 }
1251
Laurent Pinchartb482d922009-06-26 11:39:42 -03001252 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1253 dev->intfnum, info->selector, &inf, 1);
1254 if (ret < 0) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001255 uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1256 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1257 UVC_GUID_ARGS(info->entity), info->selector,
1258 ret);
1259 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)))) {
1265 uvc_trace(UVC_TRACE_CONTROL, "Control "
1266 UVC_GUID_FORMAT "/%u flags don't match "
1267 "supported operations.\n",
1268 UVC_GUID_ARGS(info->entity), info->selector);
1269 return;
1270 }
1271 }
1272
1273 ctrl->info = info;
1274 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1275 uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1276 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1277 ctrl->info->selector, dev->udev->devpath, entity->id);
1278}
1279
1280/*
1281 * Add an item to the UVC control information list, and instantiate a control
1282 * structure for each device that supports the control.
1283 */
1284int uvc_ctrl_add_info(struct uvc_control_info *info)
1285{
1286 struct uvc_control_info *ctrl;
1287 struct uvc_device *dev;
1288 int ret = 0;
1289
1290 /* Find matching controls by walking the devices, entities and
1291 * controls list.
1292 */
1293 mutex_lock(&uvc_driver.ctrl_mutex);
1294
1295 /* First check if the list contains a control matching the new one.
1296 * Bail out if it does.
1297 */
1298 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1299 if (memcmp(ctrl->entity, info->entity, 16))
1300 continue;
1301
1302 if (ctrl->selector == info->selector) {
1303 uvc_trace(UVC_TRACE_CONTROL, "Control "
1304 UVC_GUID_FORMAT "/%u is already defined.\n",
1305 UVC_GUID_ARGS(info->entity), info->selector);
1306 ret = -EEXIST;
1307 goto end;
1308 }
1309 if (ctrl->index == info->index) {
1310 uvc_trace(UVC_TRACE_CONTROL, "Control "
1311 UVC_GUID_FORMAT "/%u would overwrite index "
1312 "%d.\n", UVC_GUID_ARGS(info->entity),
1313 info->selector, info->index);
1314 ret = -EEXIST;
1315 goto end;
1316 }
1317 }
1318
1319 list_for_each_entry(dev, &uvc_driver.devices, list)
1320 uvc_ctrl_add_ctrl(dev, info);
1321
1322 INIT_LIST_HEAD(&info->mappings);
1323 list_add_tail(&info->list, &uvc_driver.controls);
1324end:
1325 mutex_unlock(&uvc_driver.ctrl_mutex);
1326 return ret;
1327}
1328
1329int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1330{
1331 struct uvc_control_info *info;
1332 struct uvc_control_mapping *map;
1333 int ret = -EINVAL;
1334
Laurent Pinchart97683522008-12-16 06:46:32 -03001335 if (mapping->get == NULL)
1336 mapping->get = uvc_get_le_value;
1337 if (mapping->set == NULL)
1338 mapping->set = uvc_set_le_value;
1339
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001340 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1341 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1342 "invalid control id 0x%08x\n", mapping->name,
1343 mapping->id);
1344 return -EINVAL;
1345 }
1346
1347 mutex_lock(&uvc_driver.ctrl_mutex);
1348 list_for_each_entry(info, &uvc_driver.controls, list) {
1349 if (memcmp(info->entity, mapping->entity, 16) ||
1350 info->selector != mapping->selector)
1351 continue;
1352
1353 if (info->size * 8 < mapping->size + mapping->offset) {
1354 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1355 "overflow control " UVC_GUID_FORMAT "/%u\n",
1356 mapping->name, UVC_GUID_ARGS(info->entity),
1357 info->selector);
1358 ret = -EOVERFLOW;
1359 goto end;
1360 }
1361
1362 /* Check if the list contains a mapping matching the new one.
1363 * Bail out if it does.
1364 */
1365 list_for_each_entry(map, &info->mappings, list) {
1366 if (map->id == mapping->id) {
1367 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1368 "already defined.\n", mapping->name);
1369 ret = -EEXIST;
1370 goto end;
1371 }
1372 }
1373
1374 mapping->ctrl = info;
1375 list_add_tail(&mapping->list, &info->mappings);
1376 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1377 UVC_GUID_FORMAT "/%u.\n", mapping->name,
1378 UVC_GUID_ARGS(info->entity), info->selector);
1379
1380 ret = 0;
1381 break;
1382 }
1383end:
1384 mutex_unlock(&uvc_driver.ctrl_mutex);
1385 return ret;
1386}
1387
1388/*
Laurent Pinchartd732c442009-05-31 17:05:55 -03001389 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1390 * are currently the ones that crash the camera or unconditionally return an
1391 * error when queried.
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001392 */
1393static void
Laurent Pinchartd732c442009-05-31 17:05:55 -03001394uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001395{
1396 static const struct {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001397 struct usb_device_id id;
1398 u8 index;
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001399 } blacklist[] = {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001400 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1401 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001402 };
1403
1404 u8 *controls;
1405 unsigned int size;
1406 unsigned int i;
1407
Laurent Pinchartb482d922009-06-26 11:39:42 -03001408 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001409 return;
1410
1411 controls = entity->processing.bmControls;
1412 size = entity->processing.bControlSize;
1413
1414 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
Laurent Pinchartd732c442009-05-31 17:05:55 -03001415 if (!usb_match_id(dev->intf, &blacklist[i].id))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001416 continue;
1417
Laurent Pinchartd732c442009-05-31 17:05:55 -03001418 if (blacklist[i].index >= 8 * size ||
1419 !uvc_test_bit(controls, blacklist[i].index))
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001420 continue;
1421
Laurent Pinchartd732c442009-05-31 17:05:55 -03001422 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1423 "removing it.\n", entity->id, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001424
Laurent Pinchartd732c442009-05-31 17:05:55 -03001425 uvc_clear_bit(controls, blacklist[i].index);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001426 }
1427}
1428
1429/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001430 * Initialize device controls.
1431 */
1432int uvc_ctrl_init_device(struct uvc_device *dev)
1433{
1434 struct uvc_control_info *info;
1435 struct uvc_control *ctrl;
1436 struct uvc_entity *entity;
1437 unsigned int i;
1438
1439 /* Walk the entities list and instantiate controls */
1440 list_for_each_entry(entity, &dev->entities, list) {
1441 unsigned int bControlSize = 0, ncontrols = 0;
1442 __u8 *bmControls = NULL;
1443
Laurent Pinchartb482d922009-06-26 11:39:42 -03001444 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001445 bmControls = entity->extension.bmControls;
1446 bControlSize = entity->extension.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001447 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001448 bmControls = entity->processing.bmControls;
1449 bControlSize = entity->processing.bControlSize;
Laurent Pinchartb482d922009-06-26 11:39:42 -03001450 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001451 bmControls = entity->camera.bmControls;
1452 bControlSize = entity->camera.bControlSize;
1453 }
1454
Laurent Pinchartd732c442009-05-31 17:05:55 -03001455 uvc_ctrl_prune_entity(dev, entity);
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001456
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001457 for (i = 0; i < bControlSize; ++i)
1458 ncontrols += hweight8(bmControls[i]);
1459
1460 if (ncontrols == 0)
1461 continue;
1462
1463 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1464 if (entity->controls == NULL)
1465 return -ENOMEM;
1466
1467 entity->ncontrols = ncontrols;
1468
1469 ctrl = entity->controls;
1470 for (i = 0; i < bControlSize * 8; ++i) {
Laurent Pinchart2bdd29c2008-12-06 17:43:40 -03001471 if (uvc_test_bit(bmControls, i) == 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001472 continue;
1473
1474 ctrl->entity = entity;
1475 ctrl->index = i;
1476 ctrl++;
1477 }
1478 }
1479
1480 /* Walk the controls info list and associate them with the device
1481 * controls, then add the device to the global device list. This has
1482 * to be done while holding the controls lock, to make sure
1483 * uvc_ctrl_add_info() will not get called in-between.
1484 */
1485 mutex_lock(&uvc_driver.ctrl_mutex);
1486 list_for_each_entry(info, &uvc_driver.controls, list)
1487 uvc_ctrl_add_ctrl(dev, info);
1488
1489 list_add_tail(&dev->list, &uvc_driver.devices);
1490 mutex_unlock(&uvc_driver.ctrl_mutex);
1491
1492 return 0;
1493}
1494
1495/*
1496 * Cleanup device controls.
1497 */
1498void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1499{
1500 struct uvc_entity *entity;
1501 unsigned int i;
1502
1503 /* Remove the device from the global devices list */
1504 mutex_lock(&uvc_driver.ctrl_mutex);
1505 if (dev->list.next != NULL)
1506 list_del(&dev->list);
1507 mutex_unlock(&uvc_driver.ctrl_mutex);
1508
1509 list_for_each_entry(entity, &dev->entities, list) {
1510 for (i = 0; i < entity->ncontrols; ++i)
1511 kfree(entity->controls[i].data);
1512
1513 kfree(entity->controls);
1514 }
1515}
1516
1517void uvc_ctrl_init(void)
1518{
1519 struct uvc_control_info *ctrl = uvc_ctrls;
1520 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1521 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1522 struct uvc_control_mapping *mend =
1523 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1524
1525 for (; ctrl < cend; ++ctrl)
1526 uvc_ctrl_add_info(ctrl);
1527
1528 for (; mapping < mend; ++mapping)
1529 uvc_ctrl_add_mapping(mapping);
1530}
Hans Verkuilf87086e2008-07-18 00:50:58 -03001531