]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
c0efd232 LP |
2 | /* |
3 | * uvc_ctrl.c -- USB Video Class driver - Controls | |
4 | * | |
11fc5baf LP |
5 | * Copyright (C) 2005-2010 |
6 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) | |
c0efd232 LP |
7 | */ |
8 | ||
619d9b71 | 9 | #include <asm/barrier.h> |
40140eda | 10 | #include <linux/bitops.h> |
c0efd232 | 11 | #include <linux/kernel.h> |
c0efd232 LP |
12 | #include <linux/list.h> |
13 | #include <linux/module.h> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
c0efd232 LP |
15 | #include <linux/uaccess.h> |
16 | #include <linux/usb.h> | |
e1d5d71d | 17 | #include <linux/usb/uvc.h> |
c0efd232 LP |
18 | #include <linux/videodev2.h> |
19 | #include <linux/vmalloc.h> | |
20 | #include <linux/wait.h> | |
e5225c82 | 21 | #include <linux/workqueue.h> |
60063497 | 22 | #include <linux/atomic.h> |
b4012002 | 23 | #include <media/v4l2-ctrls.h> |
c0efd232 LP |
24 | |
25 | #include "uvcvideo.h" | |
26 | ||
c0efd232 LP |
27 | #define UVC_CTRL_DATA_CURRENT 0 |
28 | #define UVC_CTRL_DATA_BACKUP 1 | |
59529081 LP |
29 | #define UVC_CTRL_DATA_MIN 2 |
30 | #define UVC_CTRL_DATA_MAX 3 | |
31 | #define UVC_CTRL_DATA_RES 4 | |
32 | #define UVC_CTRL_DATA_DEF 5 | |
33 | #define UVC_CTRL_DATA_LAST 6 | |
c0efd232 LP |
34 | |
35 | /* ------------------------------------------------------------------------ | |
2c2d264b | 36 | * Controls |
c0efd232 LP |
37 | */ |
38 | ||
8643d237 | 39 | static const struct uvc_control_info uvc_ctrls[] = { |
c0efd232 LP |
40 | { |
41 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 42 | .selector = UVC_PU_BRIGHTNESS_CONTROL, |
c0efd232 LP |
43 | .index = 0, |
44 | .size = 2, | |
9eb30d2f LP |
45 | .flags = UVC_CTRL_FLAG_SET_CUR |
46 | | UVC_CTRL_FLAG_GET_RANGE | |
47 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
48 | }, |
49 | { | |
50 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 51 | .selector = UVC_PU_CONTRAST_CONTROL, |
c0efd232 LP |
52 | .index = 1, |
53 | .size = 2, | |
9eb30d2f LP |
54 | .flags = UVC_CTRL_FLAG_SET_CUR |
55 | | UVC_CTRL_FLAG_GET_RANGE | |
56 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
57 | }, |
58 | { | |
59 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 60 | .selector = UVC_PU_HUE_CONTROL, |
c0efd232 LP |
61 | .index = 2, |
62 | .size = 2, | |
9eb30d2f LP |
63 | .flags = UVC_CTRL_FLAG_SET_CUR |
64 | | UVC_CTRL_FLAG_GET_RANGE | |
65 | | UVC_CTRL_FLAG_RESTORE | |
66 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
c0efd232 LP |
67 | }, |
68 | { | |
69 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 70 | .selector = UVC_PU_SATURATION_CONTROL, |
c0efd232 LP |
71 | .index = 3, |
72 | .size = 2, | |
9eb30d2f LP |
73 | .flags = UVC_CTRL_FLAG_SET_CUR |
74 | | UVC_CTRL_FLAG_GET_RANGE | |
75 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
76 | }, |
77 | { | |
78 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 79 | .selector = UVC_PU_SHARPNESS_CONTROL, |
c0efd232 LP |
80 | .index = 4, |
81 | .size = 2, | |
9eb30d2f LP |
82 | .flags = UVC_CTRL_FLAG_SET_CUR |
83 | | UVC_CTRL_FLAG_GET_RANGE | |
84 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
85 | }, |
86 | { | |
87 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 88 | .selector = UVC_PU_GAMMA_CONTROL, |
c0efd232 LP |
89 | .index = 5, |
90 | .size = 2, | |
9eb30d2f LP |
91 | .flags = UVC_CTRL_FLAG_SET_CUR |
92 | | UVC_CTRL_FLAG_GET_RANGE | |
93 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 | 94 | }, |
5e26d50f LP |
95 | { |
96 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 97 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, |
5e26d50f LP |
98 | .index = 6, |
99 | .size = 2, | |
9eb30d2f LP |
100 | .flags = UVC_CTRL_FLAG_SET_CUR |
101 | | UVC_CTRL_FLAG_GET_RANGE | |
102 | | UVC_CTRL_FLAG_RESTORE | |
103 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
5e26d50f LP |
104 | }, |
105 | { | |
106 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 107 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, |
5e26d50f LP |
108 | .index = 7, |
109 | .size = 4, | |
9eb30d2f LP |
110 | .flags = UVC_CTRL_FLAG_SET_CUR |
111 | | UVC_CTRL_FLAG_GET_RANGE | |
112 | | UVC_CTRL_FLAG_RESTORE | |
113 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
5e26d50f | 114 | }, |
c0efd232 LP |
115 | { |
116 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 117 | .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, |
c0efd232 LP |
118 | .index = 8, |
119 | .size = 2, | |
9eb30d2f LP |
120 | .flags = UVC_CTRL_FLAG_SET_CUR |
121 | | UVC_CTRL_FLAG_GET_RANGE | |
122 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
123 | }, |
124 | { | |
125 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 126 | .selector = UVC_PU_GAIN_CONTROL, |
c0efd232 LP |
127 | .index = 9, |
128 | .size = 2, | |
9eb30d2f LP |
129 | .flags = UVC_CTRL_FLAG_SET_CUR |
130 | | UVC_CTRL_FLAG_GET_RANGE | |
131 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
132 | }, |
133 | { | |
134 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 135 | .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, |
c0efd232 LP |
136 | .index = 10, |
137 | .size = 1, | |
9eb30d2f LP |
138 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
139 | | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
140 | }, |
141 | { | |
142 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 143 | .selector = UVC_PU_HUE_AUTO_CONTROL, |
c0efd232 LP |
144 | .index = 11, |
145 | .size = 1, | |
9eb30d2f LP |
146 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
147 | | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 | 148 | }, |
5e26d50f LP |
149 | { |
150 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 151 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, |
5e26d50f LP |
152 | .index = 12, |
153 | .size = 1, | |
9eb30d2f LP |
154 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
155 | | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, | |
5e26d50f LP |
156 | }, |
157 | { | |
158 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 159 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, |
5e26d50f LP |
160 | .index = 13, |
161 | .size = 1, | |
9eb30d2f LP |
162 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
163 | | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, | |
5e26d50f LP |
164 | }, |
165 | { | |
166 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 167 | .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL, |
5e26d50f LP |
168 | .index = 14, |
169 | .size = 2, | |
9eb30d2f LP |
170 | .flags = UVC_CTRL_FLAG_SET_CUR |
171 | | UVC_CTRL_FLAG_GET_RANGE | |
172 | | UVC_CTRL_FLAG_RESTORE, | |
5e26d50f LP |
173 | }, |
174 | { | |
175 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 176 | .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL, |
5e26d50f LP |
177 | .index = 15, |
178 | .size = 2, | |
9eb30d2f LP |
179 | .flags = UVC_CTRL_FLAG_SET_CUR |
180 | | UVC_CTRL_FLAG_GET_RANGE | |
181 | | UVC_CTRL_FLAG_RESTORE, | |
5e26d50f LP |
182 | }, |
183 | { | |
184 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 185 | .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL, |
5e26d50f LP |
186 | .index = 16, |
187 | .size = 1, | |
9eb30d2f | 188 | .flags = UVC_CTRL_FLAG_GET_CUR, |
5e26d50f LP |
189 | }, |
190 | { | |
191 | .entity = UVC_GUID_UVC_PROCESSING, | |
b482d923 | 192 | .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL, |
5e26d50f LP |
193 | .index = 17, |
194 | .size = 1, | |
9eb30d2f | 195 | .flags = UVC_CTRL_FLAG_GET_CUR, |
5e26d50f LP |
196 | }, |
197 | { | |
198 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 199 | .selector = UVC_CT_SCANNING_MODE_CONTROL, |
5e26d50f LP |
200 | .index = 0, |
201 | .size = 1, | |
9eb30d2f LP |
202 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
203 | | UVC_CTRL_FLAG_RESTORE, | |
5e26d50f | 204 | }, |
c0efd232 LP |
205 | { |
206 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 207 | .selector = UVC_CT_AE_MODE_CONTROL, |
c0efd232 LP |
208 | .index = 1, |
209 | .size = 1, | |
9eb30d2f LP |
210 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
211 | | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES | |
212 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
213 | }, |
214 | { | |
215 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 216 | .selector = UVC_CT_AE_PRIORITY_CONTROL, |
c0efd232 LP |
217 | .index = 2, |
218 | .size = 1, | |
9eb30d2f LP |
219 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
220 | | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
221 | }, |
222 | { | |
223 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 224 | .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, |
c0efd232 LP |
225 | .index = 3, |
226 | .size = 4, | |
9eb30d2f LP |
227 | .flags = UVC_CTRL_FLAG_SET_CUR |
228 | | UVC_CTRL_FLAG_GET_RANGE | |
1ab8c3fc AS |
229 | | UVC_CTRL_FLAG_RESTORE |
230 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
c0efd232 | 231 | }, |
5e26d50f LP |
232 | { |
233 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 234 | .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL, |
5e26d50f LP |
235 | .index = 4, |
236 | .size = 1, | |
9eb30d2f | 237 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE, |
5e26d50f | 238 | }, |
c0efd232 LP |
239 | { |
240 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 241 | .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, |
c0efd232 LP |
242 | .index = 5, |
243 | .size = 2, | |
9eb30d2f LP |
244 | .flags = UVC_CTRL_FLAG_SET_CUR |
245 | | UVC_CTRL_FLAG_GET_RANGE | |
246 | | UVC_CTRL_FLAG_RESTORE | |
247 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
c0efd232 LP |
248 | }, |
249 | { | |
250 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 251 | .selector = UVC_CT_FOCUS_RELATIVE_CONTROL, |
5e26d50f LP |
252 | .index = 6, |
253 | .size = 2, | |
9eb30d2f LP |
254 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN |
255 | | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES | |
256 | | UVC_CTRL_FLAG_GET_DEF | |
257 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
c0efd232 LP |
258 | }, |
259 | { | |
5e26d50f | 260 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 261 | .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, |
5e26d50f LP |
262 | .index = 7, |
263 | .size = 2, | |
9eb30d2f LP |
264 | .flags = UVC_CTRL_FLAG_SET_CUR |
265 | | UVC_CTRL_FLAG_GET_RANGE | |
266 | | UVC_CTRL_FLAG_RESTORE | |
267 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
5e26d50f LP |
268 | }, |
269 | { | |
270 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 271 | .selector = UVC_CT_IRIS_RELATIVE_CONTROL, |
5e26d50f | 272 | .index = 8, |
c0efd232 | 273 | .size = 1, |
9eb30d2f LP |
274 | .flags = UVC_CTRL_FLAG_SET_CUR |
275 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
c0efd232 LP |
276 | }, |
277 | { | |
5e26d50f | 278 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 279 | .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, |
5e26d50f | 280 | .index = 9, |
c0efd232 | 281 | .size = 2, |
9eb30d2f LP |
282 | .flags = UVC_CTRL_FLAG_SET_CUR |
283 | | UVC_CTRL_FLAG_GET_RANGE | |
284 | | UVC_CTRL_FLAG_RESTORE | |
285 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
c0efd232 LP |
286 | }, |
287 | { | |
5e26d50f | 288 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 289 | .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, |
5e26d50f LP |
290 | .index = 10, |
291 | .size = 3, | |
9eb30d2f LP |
292 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN |
293 | | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES | |
294 | | UVC_CTRL_FLAG_GET_DEF | |
295 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
5e26d50f LP |
296 | }, |
297 | { | |
298 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 299 | .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, |
5e26d50f LP |
300 | .index = 11, |
301 | .size = 8, | |
9eb30d2f LP |
302 | .flags = UVC_CTRL_FLAG_SET_CUR |
303 | | UVC_CTRL_FLAG_GET_RANGE | |
304 | | UVC_CTRL_FLAG_RESTORE | |
305 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
5e26d50f LP |
306 | }, |
307 | { | |
308 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 309 | .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, |
5e26d50f LP |
310 | .index = 12, |
311 | .size = 4, | |
3ea37523 VP |
312 | .flags = UVC_CTRL_FLAG_SET_CUR |
313 | | UVC_CTRL_FLAG_GET_RANGE | |
9eb30d2f | 314 | | UVC_CTRL_FLAG_AUTO_UPDATE, |
5e26d50f LP |
315 | }, |
316 | { | |
317 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 318 | .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL, |
c0efd232 | 319 | .index = 13, |
5e26d50f | 320 | .size = 2, |
9eb30d2f LP |
321 | .flags = UVC_CTRL_FLAG_SET_CUR |
322 | | UVC_CTRL_FLAG_GET_RANGE | |
323 | | UVC_CTRL_FLAG_RESTORE | |
324 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
5e26d50f LP |
325 | }, |
326 | { | |
327 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 328 | .selector = UVC_CT_ROLL_RELATIVE_CONTROL, |
5e26d50f LP |
329 | .index = 14, |
330 | .size = 2, | |
9eb30d2f LP |
331 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN |
332 | | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES | |
333 | | UVC_CTRL_FLAG_GET_DEF | |
334 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
5e26d50f LP |
335 | }, |
336 | { | |
337 | .entity = UVC_GUID_UVC_CAMERA, | |
b482d923 | 338 | .selector = UVC_CT_FOCUS_AUTO_CONTROL, |
5e26d50f | 339 | .index = 17, |
c0efd232 | 340 | .size = 1, |
9eb30d2f LP |
341 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
342 | | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, | |
c0efd232 LP |
343 | }, |
344 | { | |
5e26d50f | 345 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 346 | .selector = UVC_CT_PRIVACY_CONTROL, |
5e26d50f LP |
347 | .index = 18, |
348 | .size = 1, | |
9eb30d2f LP |
349 | .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR |
350 | | UVC_CTRL_FLAG_RESTORE | |
351 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
c0efd232 | 352 | }, |
6f6a87eb RR |
353 | { |
354 | .entity = UVC_GUID_EXT_GPIO_CONTROLLER, | |
355 | .selector = UVC_CT_PRIVACY_CONTROL, | |
356 | .index = 0, | |
357 | .size = 1, | |
358 | .flags = UVC_CTRL_FLAG_GET_CUR | |
359 | | UVC_CTRL_FLAG_AUTO_UPDATE, | |
360 | }, | |
c0efd232 LP |
361 | }; |
362 | ||
9b31ea80 RR |
363 | static const u32 uvc_control_classes[] = { |
364 | V4L2_CID_CAMERA_CLASS, | |
365 | V4L2_CID_USER_CLASS, | |
366 | }; | |
367 | ||
716c3304 | 368 | static const int exposure_auto_mapping[] = { 2, 1, 4, 8 }; |
c0efd232 | 369 | |
716c3304 RR |
370 | /* |
371 | * This function translates the V4L2 menu index @idx, as exposed to userspace as | |
372 | * the V4L2 control value, to the corresponding UVC control value used by the | |
373 | * device. The custom menu_mapping in the control @mapping is used when | |
374 | * available, otherwise the function assumes that the V4L2 and UVC values are | |
375 | * identical. | |
376 | * | |
377 | * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is | |
378 | * expressed as a bitmask and is thus guaranteed to have a single bit set. | |
379 | * | |
380 | * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the | |
381 | * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM | |
382 | * or UVC_CTRL_DATA_TYPE_BITMASK. | |
383 | */ | |
384 | static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping, | |
385 | u32 idx) | |
386 | { | |
387 | if (!test_bit(idx, &mapping->menu_mask)) | |
388 | return -EINVAL; | |
389 | ||
390 | if (mapping->menu_mapping) | |
391 | return mapping->menu_mapping[idx]; | |
392 | ||
393 | return idx; | |
394 | } | |
395 | ||
396 | static const char * | |
397 | uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx) | |
398 | { | |
399 | if (!test_bit(idx, &mapping->menu_mask)) | |
400 | return NULL; | |
401 | ||
402 | if (mapping->menu_names) | |
403 | return mapping->menu_names[idx]; | |
404 | ||
405 | return v4l2_ctrl_get_menu(mapping->id)[idx]; | |
406 | } | |
c0efd232 | 407 | |
2c6b222c LP |
408 | static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, |
409 | u8 query, const u8 *data) | |
9768352a | 410 | { |
2c6b222c | 411 | s8 zoom = (s8)data[0]; |
9768352a LP |
412 | |
413 | switch (query) { | |
b482d923 | 414 | case UVC_GET_CUR: |
9768352a LP |
415 | return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]); |
416 | ||
b482d923 LP |
417 | case UVC_GET_MIN: |
418 | case UVC_GET_MAX: | |
419 | case UVC_GET_RES: | |
420 | case UVC_GET_DEF: | |
9768352a LP |
421 | default: |
422 | return data[2]; | |
423 | } | |
424 | } | |
425 | ||
426 | static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping, | |
2c6b222c | 427 | s32 value, u8 *data) |
9768352a LP |
428 | { |
429 | data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff; | |
2e896133 | 430 | data[2] = min((int)abs(value), 0xff); |
9768352a LP |
431 | } |
432 | ||
2c6b222c LP |
433 | static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping, |
434 | u8 query, const u8 *data) | |
3ea37523 VP |
435 | { |
436 | unsigned int first = mapping->offset / 8; | |
2c6b222c | 437 | s8 rel = (s8)data[first]; |
3ea37523 VP |
438 | |
439 | switch (query) { | |
440 | case UVC_GET_CUR: | |
441 | return (rel == 0) ? 0 : (rel > 0 ? data[first+1] | |
442 | : -data[first+1]); | |
443 | case UVC_GET_MIN: | |
444 | return -data[first+1]; | |
445 | case UVC_GET_MAX: | |
446 | case UVC_GET_RES: | |
447 | case UVC_GET_DEF: | |
448 | default: | |
449 | return data[first+1]; | |
450 | } | |
451 | } | |
452 | ||
453 | static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping, | |
2c6b222c | 454 | s32 value, u8 *data) |
3ea37523 VP |
455 | { |
456 | unsigned int first = mapping->offset / 8; | |
457 | ||
458 | data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff; | |
459 | data[first+1] = min_t(int, abs(value), 0xff); | |
460 | } | |
461 | ||
8643d237 | 462 | static const struct uvc_control_mapping uvc_ctrl_mappings[] = { |
c0efd232 LP |
463 | { |
464 | .id = V4L2_CID_BRIGHTNESS, | |
c0efd232 | 465 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 466 | .selector = UVC_PU_BRIGHTNESS_CONTROL, |
c0efd232 LP |
467 | .size = 16, |
468 | .offset = 0, | |
469 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
470 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
471 | }, | |
472 | { | |
473 | .id = V4L2_CID_CONTRAST, | |
c0efd232 | 474 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 475 | .selector = UVC_PU_CONTRAST_CONTROL, |
c0efd232 LP |
476 | .size = 16, |
477 | .offset = 0, | |
478 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
479 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
480 | }, | |
481 | { | |
482 | .id = V4L2_CID_HUE, | |
c0efd232 | 483 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 484 | .selector = UVC_PU_HUE_CONTROL, |
c0efd232 LP |
485 | .size = 16, |
486 | .offset = 0, | |
487 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
488 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
55afeb8a HG |
489 | .master_id = V4L2_CID_HUE_AUTO, |
490 | .master_manual = 0, | |
c0efd232 LP |
491 | }, |
492 | { | |
493 | .id = V4L2_CID_SATURATION, | |
c0efd232 | 494 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 495 | .selector = UVC_PU_SATURATION_CONTROL, |
c0efd232 LP |
496 | .size = 16, |
497 | .offset = 0, | |
498 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
499 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
500 | }, | |
501 | { | |
502 | .id = V4L2_CID_SHARPNESS, | |
c0efd232 | 503 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 504 | .selector = UVC_PU_SHARPNESS_CONTROL, |
c0efd232 LP |
505 | .size = 16, |
506 | .offset = 0, | |
507 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
508 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
509 | }, | |
510 | { | |
511 | .id = V4L2_CID_GAMMA, | |
c0efd232 | 512 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 513 | .selector = UVC_PU_GAMMA_CONTROL, |
c0efd232 LP |
514 | .size = 16, |
515 | .offset = 0, | |
516 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
517 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
518 | }, | |
519 | { | |
520 | .id = V4L2_CID_BACKLIGHT_COMPENSATION, | |
c0efd232 | 521 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 522 | .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, |
c0efd232 LP |
523 | .size = 16, |
524 | .offset = 0, | |
525 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
526 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
527 | }, | |
528 | { | |
529 | .id = V4L2_CID_GAIN, | |
c0efd232 | 530 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 531 | .selector = UVC_PU_GAIN_CONTROL, |
c0efd232 LP |
532 | .size = 16, |
533 | .offset = 0, | |
534 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
535 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
536 | }, | |
c0efd232 LP |
537 | { |
538 | .id = V4L2_CID_HUE_AUTO, | |
c0efd232 | 539 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 540 | .selector = UVC_PU_HUE_AUTO_CONTROL, |
c0efd232 LP |
541 | .size = 1, |
542 | .offset = 0, | |
543 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, | |
544 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, | |
55afeb8a | 545 | .slave_ids = { V4L2_CID_HUE, }, |
c0efd232 LP |
546 | }, |
547 | { | |
548 | .id = V4L2_CID_EXPOSURE_AUTO, | |
c0efd232 | 549 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 550 | .selector = UVC_CT_AE_MODE_CONTROL, |
c0efd232 LP |
551 | .size = 4, |
552 | .offset = 0, | |
553 | .v4l2_type = V4L2_CTRL_TYPE_MENU, | |
554 | .data_type = UVC_CTRL_DATA_TYPE_BITMASK, | |
716c3304 | 555 | .menu_mapping = exposure_auto_mapping, |
40140eda RR |
556 | .menu_mask = GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY, |
557 | V4L2_EXPOSURE_AUTO), | |
55afeb8a | 558 | .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, }, |
c0efd232 LP |
559 | }, |
560 | { | |
561 | .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY, | |
c0efd232 | 562 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 563 | .selector = UVC_CT_AE_PRIORITY_CONTROL, |
c0efd232 LP |
564 | .size = 1, |
565 | .offset = 0, | |
566 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, | |
567 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, | |
568 | }, | |
569 | { | |
570 | .id = V4L2_CID_EXPOSURE_ABSOLUTE, | |
c0efd232 | 571 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 572 | .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, |
c0efd232 LP |
573 | .size = 32, |
574 | .offset = 0, | |
575 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
576 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
55afeb8a HG |
577 | .master_id = V4L2_CID_EXPOSURE_AUTO, |
578 | .master_manual = V4L2_EXPOSURE_MANUAL, | |
c0efd232 LP |
579 | }, |
580 | { | |
581 | .id = V4L2_CID_AUTO_WHITE_BALANCE, | |
c0efd232 | 582 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 583 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, |
c0efd232 LP |
584 | .size = 1, |
585 | .offset = 0, | |
586 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, | |
587 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, | |
55afeb8a | 588 | .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, }, |
c0efd232 LP |
589 | }, |
590 | { | |
591 | .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE, | |
c0efd232 | 592 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 593 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, |
c0efd232 LP |
594 | .size = 16, |
595 | .offset = 0, | |
596 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
597 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
55afeb8a HG |
598 | .master_id = V4L2_CID_AUTO_WHITE_BALANCE, |
599 | .master_manual = 0, | |
c0efd232 LP |
600 | }, |
601 | { | |
602 | .id = V4L2_CID_AUTO_WHITE_BALANCE, | |
c0efd232 | 603 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 604 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, |
c0efd232 LP |
605 | .size = 1, |
606 | .offset = 0, | |
607 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, | |
608 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, | |
55afeb8a HG |
609 | .slave_ids = { V4L2_CID_BLUE_BALANCE, |
610 | V4L2_CID_RED_BALANCE }, | |
c0efd232 LP |
611 | }, |
612 | { | |
613 | .id = V4L2_CID_BLUE_BALANCE, | |
c0efd232 | 614 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 615 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, |
c0efd232 LP |
616 | .size = 16, |
617 | .offset = 0, | |
618 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
619 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
55afeb8a HG |
620 | .master_id = V4L2_CID_AUTO_WHITE_BALANCE, |
621 | .master_manual = 0, | |
c0efd232 LP |
622 | }, |
623 | { | |
624 | .id = V4L2_CID_RED_BALANCE, | |
c0efd232 | 625 | .entity = UVC_GUID_UVC_PROCESSING, |
b482d923 | 626 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, |
c0efd232 LP |
627 | .size = 16, |
628 | .offset = 16, | |
629 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
630 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
55afeb8a HG |
631 | .master_id = V4L2_CID_AUTO_WHITE_BALANCE, |
632 | .master_manual = 0, | |
c0efd232 LP |
633 | }, |
634 | { | |
635 | .id = V4L2_CID_FOCUS_ABSOLUTE, | |
c0efd232 | 636 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 637 | .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, |
c0efd232 LP |
638 | .size = 16, |
639 | .offset = 0, | |
640 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
641 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
55afeb8a HG |
642 | .master_id = V4L2_CID_FOCUS_AUTO, |
643 | .master_manual = 0, | |
c0efd232 LP |
644 | }, |
645 | { | |
646 | .id = V4L2_CID_FOCUS_AUTO, | |
c0efd232 | 647 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 648 | .selector = UVC_CT_FOCUS_AUTO_CONTROL, |
c0efd232 LP |
649 | .size = 1, |
650 | .offset = 0, | |
651 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, | |
652 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, | |
55afeb8a | 653 | .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, }, |
c0efd232 | 654 | }, |
35a00c41 LP |
655 | { |
656 | .id = V4L2_CID_IRIS_ABSOLUTE, | |
35a00c41 LP |
657 | .entity = UVC_GUID_UVC_CAMERA, |
658 | .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, | |
659 | .size = 16, | |
660 | .offset = 0, | |
661 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
662 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
663 | }, | |
664 | { | |
665 | .id = V4L2_CID_IRIS_RELATIVE, | |
35a00c41 LP |
666 | .entity = UVC_GUID_UVC_CAMERA, |
667 | .selector = UVC_CT_IRIS_RELATIVE_CONTROL, | |
668 | .size = 8, | |
669 | .offset = 0, | |
670 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
671 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
672 | }, | |
9768352a LP |
673 | { |
674 | .id = V4L2_CID_ZOOM_ABSOLUTE, | |
9768352a | 675 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 676 | .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, |
9768352a LP |
677 | .size = 16, |
678 | .offset = 0, | |
679 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
680 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, | |
681 | }, | |
682 | { | |
683 | .id = V4L2_CID_ZOOM_CONTINUOUS, | |
9768352a | 684 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 685 | .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, |
9768352a LP |
686 | .size = 0, |
687 | .offset = 0, | |
688 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
689 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
690 | .get = uvc_ctrl_get_zoom, | |
691 | .set = uvc_ctrl_set_zoom, | |
692 | }, | |
e56be916 MR |
693 | { |
694 | .id = V4L2_CID_PAN_ABSOLUTE, | |
e56be916 MR |
695 | .entity = UVC_GUID_UVC_CAMERA, |
696 | .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, | |
697 | .size = 32, | |
698 | .offset = 0, | |
699 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
8ca5d2d8 | 700 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, |
e56be916 MR |
701 | }, |
702 | { | |
703 | .id = V4L2_CID_TILT_ABSOLUTE, | |
e56be916 MR |
704 | .entity = UVC_GUID_UVC_CAMERA, |
705 | .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, | |
706 | .size = 32, | |
707 | .offset = 32, | |
708 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
8ca5d2d8 | 709 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, |
e56be916 | 710 | }, |
3ea37523 VP |
711 | { |
712 | .id = V4L2_CID_PAN_SPEED, | |
3ea37523 VP |
713 | .entity = UVC_GUID_UVC_CAMERA, |
714 | .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, | |
715 | .size = 16, | |
716 | .offset = 0, | |
717 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
718 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
719 | .get = uvc_ctrl_get_rel_speed, | |
720 | .set = uvc_ctrl_set_rel_speed, | |
721 | }, | |
722 | { | |
723 | .id = V4L2_CID_TILT_SPEED, | |
3ea37523 VP |
724 | .entity = UVC_GUID_UVC_CAMERA, |
725 | .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, | |
726 | .size = 16, | |
727 | .offset = 16, | |
728 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, | |
729 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, | |
730 | .get = uvc_ctrl_get_rel_speed, | |
731 | .set = uvc_ctrl_set_rel_speed, | |
732 | }, | |
6df126f8 LP |
733 | { |
734 | .id = V4L2_CID_PRIVACY, | |
6df126f8 | 735 | .entity = UVC_GUID_UVC_CAMERA, |
b482d923 | 736 | .selector = UVC_CT_PRIVACY_CONTROL, |
6df126f8 LP |
737 | .size = 1, |
738 | .offset = 0, | |
739 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, | |
740 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, | |
741 | }, | |
6f6a87eb RR |
742 | { |
743 | .id = V4L2_CID_PRIVACY, | |
6f6a87eb RR |
744 | .entity = UVC_GUID_EXT_GPIO_CONTROLLER, |
745 | .selector = UVC_CT_PRIVACY_CONTROL, | |
746 | .size = 1, | |
747 | .offset = 0, | |
748 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, | |
749 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, | |
750 | }, | |
c0efd232 LP |
751 | }; |
752 | ||
3aa8628e RR |
753 | const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { |
754 | .id = V4L2_CID_POWER_LINE_FREQUENCY, | |
755 | .entity = UVC_GUID_UVC_PROCESSING, | |
756 | .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, | |
757 | .size = 2, | |
758 | .offset = 0, | |
759 | .v4l2_type = V4L2_CTRL_TYPE_MENU, | |
760 | .data_type = UVC_CTRL_DATA_TYPE_ENUM, | |
3aa8628e RR |
761 | .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, |
762 | V4L2_CID_POWER_LINE_FREQUENCY_50HZ), | |
763 | }; | |
764 | ||
a7c28150 | 765 | const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = { |
96a160b0 RR |
766 | .id = V4L2_CID_POWER_LINE_FREQUENCY, |
767 | .entity = UVC_GUID_UVC_PROCESSING, | |
768 | .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, | |
769 | .size = 2, | |
770 | .offset = 0, | |
771 | .v4l2_type = V4L2_CTRL_TYPE_MENU, | |
772 | .data_type = UVC_CTRL_DATA_TYPE_ENUM, | |
96a160b0 RR |
773 | .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, |
774 | V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), | |
71087116 RR |
775 | }; |
776 | ||
96a160b0 RR |
777 | static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc11[] = { |
778 | &uvc_ctrl_power_line_mapping_uvc11, | |
779 | NULL, /* Sentinel */ | |
780 | }; | |
781 | ||
782 | static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = { | |
783 | .id = V4L2_CID_POWER_LINE_FREQUENCY, | |
784 | .entity = UVC_GUID_UVC_PROCESSING, | |
785 | .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, | |
786 | .size = 2, | |
787 | .offset = 0, | |
788 | .v4l2_type = V4L2_CTRL_TYPE_MENU, | |
789 | .data_type = UVC_CTRL_DATA_TYPE_ENUM, | |
96a160b0 RR |
790 | .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO, |
791 | V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), | |
792 | }; | |
793 | ||
794 | static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc15[] = { | |
795 | &uvc_ctrl_power_line_mapping_uvc15, | |
796 | NULL, /* Sentinel */ | |
71087116 RR |
797 | }; |
798 | ||
c0efd232 LP |
799 | /* ------------------------------------------------------------------------ |
800 | * Utility functions | |
801 | */ | |
802 | ||
2c6b222c | 803 | static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id) |
c0efd232 | 804 | { |
071c8bb8 | 805 | return ctrl->uvc_data + id * ctrl->info.size; |
c0efd232 LP |
806 | } |
807 | ||
2c6b222c | 808 | static inline int uvc_test_bit(const u8 *data, int bit) |
c0efd232 LP |
809 | { |
810 | return (data[bit >> 3] >> (bit & 7)) & 1; | |
811 | } | |
812 | ||
2c6b222c | 813 | static inline void uvc_clear_bit(u8 *data, int bit) |
2bdd29cf LP |
814 | { |
815 | data[bit >> 3] &= ~(1 << (bit & 7)); | |
816 | } | |
817 | ||
699b9a86 LP |
818 | /* |
819 | * Extract the bit string specified by mapping->offset and mapping->size | |
c0efd232 LP |
820 | * from the little-endian data stored at 'data' and return the result as |
821 | * a signed 32bit integer. Sign extension will be performed if the mapping | |
822 | * references a signed data type. | |
823 | */ | |
2c6b222c LP |
824 | static s32 uvc_get_le_value(struct uvc_control_mapping *mapping, |
825 | u8 query, const u8 *data) | |
c0efd232 LP |
826 | { |
827 | int bits = mapping->size; | |
828 | int offset = mapping->offset; | |
2c6b222c LP |
829 | s32 value = 0; |
830 | u8 mask; | |
c0efd232 LP |
831 | |
832 | data += offset / 8; | |
833 | offset &= 7; | |
834 | mask = ((1LL << bits) - 1) << offset; | |
835 | ||
171994e4 | 836 | while (1) { |
2c6b222c | 837 | u8 byte = *data & mask; |
c0efd232 LP |
838 | value |= offset > 0 ? (byte >> offset) : (byte << (-offset)); |
839 | bits -= 8 - (offset > 0 ? offset : 0); | |
171994e4 LP |
840 | if (bits <= 0) |
841 | break; | |
842 | ||
c0efd232 LP |
843 | offset -= 8; |
844 | mask = (1 << bits) - 1; | |
171994e4 | 845 | data++; |
c0efd232 LP |
846 | } |
847 | ||
2c2d264b | 848 | /* Sign-extend the value if needed. */ |
c0efd232 LP |
849 | if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) |
850 | value |= -(value & (1 << (mapping->size - 1))); | |
851 | ||
852 | return value; | |
853 | } | |
854 | ||
699b9a86 LP |
855 | /* |
856 | * Set the bit string specified by mapping->offset and mapping->size | |
c0efd232 LP |
857 | * in the little-endian data stored at 'data' to the value 'value'. |
858 | */ | |
9768352a | 859 | static void uvc_set_le_value(struct uvc_control_mapping *mapping, |
2c6b222c | 860 | s32 value, u8 *data) |
c0efd232 LP |
861 | { |
862 | int bits = mapping->size; | |
863 | int offset = mapping->offset; | |
2c6b222c | 864 | u8 mask; |
c0efd232 | 865 | |
699b9a86 LP |
866 | /* |
867 | * According to the v4l2 spec, writing any value to a button control | |
3653639e HG |
868 | * should result in the action belonging to the button control being |
869 | * triggered. UVC devices however want to see a 1 written -> override | |
870 | * value. | |
871 | */ | |
872 | if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON) | |
873 | value = -1; | |
874 | ||
c0efd232 LP |
875 | data += offset / 8; |
876 | offset &= 7; | |
877 | ||
878 | for (; bits > 0; data++) { | |
879 | mask = ((1LL << bits) - 1) << offset; | |
880 | *data = (*data & ~mask) | ((value << offset) & mask); | |
881 | value >>= offset ? offset : 8; | |
882 | bits -= 8 - offset; | |
883 | offset = 0; | |
884 | } | |
885 | } | |
886 | ||
887 | /* ------------------------------------------------------------------------ | |
888 | * Terminal and unit management | |
889 | */ | |
890 | ||
f9d81df9 | 891 | static int uvc_entity_match_guid(const struct uvc_entity *entity, |
351509c6 | 892 | const u8 guid[16]) |
c0efd232 | 893 | { |
351509c6 | 894 | return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0; |
c0efd232 LP |
895 | } |
896 | ||
897 | /* ------------------------------------------------------------------------ | |
898 | * UVC Controls | |
899 | */ | |
900 | ||
2c6b222c | 901 | static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id, |
c0efd232 LP |
902 | struct uvc_control_mapping **mapping, struct uvc_control **control, |
903 | int next) | |
904 | { | |
905 | struct uvc_control *ctrl; | |
906 | struct uvc_control_mapping *map; | |
907 | unsigned int i; | |
908 | ||
909 | if (entity == NULL) | |
910 | return; | |
911 | ||
912 | for (i = 0; i < entity->ncontrols; ++i) { | |
913 | ctrl = &entity->controls[i]; | |
071c8bb8 | 914 | if (!ctrl->initialized) |
c0efd232 LP |
915 | continue; |
916 | ||
071c8bb8 | 917 | list_for_each_entry(map, &ctrl->info.mappings, list) { |
c0efd232 LP |
918 | if ((map->id == v4l2_id) && !next) { |
919 | *control = ctrl; | |
920 | *mapping = map; | |
921 | return; | |
922 | } | |
923 | ||
924 | if ((*mapping == NULL || (*mapping)->id > map->id) && | |
925 | (map->id > v4l2_id) && next) { | |
926 | *control = ctrl; | |
927 | *mapping = map; | |
928 | } | |
929 | } | |
930 | } | |
931 | } | |
932 | ||
23d9f3ef | 933 | static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain, |
2c6b222c | 934 | u32 v4l2_id, struct uvc_control_mapping **mapping) |
c0efd232 LP |
935 | { |
936 | struct uvc_control *ctrl = NULL; | |
937 | struct uvc_entity *entity; | |
938 | int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL; | |
939 | ||
940 | *mapping = NULL; | |
941 | ||
942 | /* Mask the query flags. */ | |
943 | v4l2_id &= V4L2_CTRL_ID_MASK; | |
944 | ||
945 | /* Find the control. */ | |
6241d8ca | 946 | list_for_each_entry(entity, &chain->entities, chain) { |
c0efd232 LP |
947 | __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next); |
948 | if (ctrl && !next) | |
949 | return ctrl; | |
950 | } | |
951 | ||
952 | if (ctrl == NULL && !next) | |
9e56380a JP |
953 | uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n", |
954 | v4l2_id); | |
c0efd232 LP |
955 | |
956 | return ctrl; | |
957 | } | |
958 | ||
59529081 LP |
959 | static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain, |
960 | struct uvc_control *ctrl) | |
961 | { | |
962 | int ret; | |
963 | ||
9eb30d2f | 964 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) { |
59529081 | 965 | ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id, |
071c8bb8 | 966 | chain->dev->intfnum, ctrl->info.selector, |
59529081 | 967 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF), |
071c8bb8 | 968 | ctrl->info.size); |
59529081 LP |
969 | if (ret < 0) |
970 | return ret; | |
971 | } | |
972 | ||
9eb30d2f | 973 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) { |
59529081 | 974 | ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id, |
071c8bb8 | 975 | chain->dev->intfnum, ctrl->info.selector, |
59529081 | 976 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN), |
071c8bb8 | 977 | ctrl->info.size); |
59529081 LP |
978 | if (ret < 0) |
979 | return ret; | |
980 | } | |
9eb30d2f | 981 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) { |
59529081 | 982 | ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id, |
071c8bb8 | 983 | chain->dev->intfnum, ctrl->info.selector, |
59529081 | 984 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX), |
071c8bb8 | 985 | ctrl->info.size); |
59529081 LP |
986 | if (ret < 0) |
987 | return ret; | |
988 | } | |
9eb30d2f | 989 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) { |
59529081 | 990 | ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id, |
071c8bb8 | 991 | chain->dev->intfnum, ctrl->info.selector, |
59529081 | 992 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), |
071c8bb8 | 993 | ctrl->info.size); |
c4d99f89 LP |
994 | if (ret < 0) { |
995 | if (UVC_ENTITY_TYPE(ctrl->entity) != | |
996 | UVC_VC_EXTENSION_UNIT) | |
997 | return ret; | |
998 | ||
699b9a86 LP |
999 | /* |
1000 | * GET_RES is mandatory for XU controls, but some | |
c4d99f89 LP |
1001 | * cameras still choke on it. Ignore errors and set the |
1002 | * resolution value to zero. | |
1003 | */ | |
1004 | uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES, | |
1005 | "UVC non compliance - GET_RES failed on " | |
1006 | "an XU control. Enabling workaround.\n"); | |
1007 | memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0, | |
1008 | ctrl->info.size); | |
1009 | } | |
59529081 LP |
1010 | } |
1011 | ||
1012 | ctrl->cached = 1; | |
1013 | return 0; | |
1014 | } | |
1015 | ||
e5225c82 GL |
1016 | static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping, |
1017 | const u8 *data) | |
1018 | { | |
1019 | s32 value = mapping->get(mapping, UVC_GET_CUR, data); | |
1020 | ||
1021 | if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) { | |
e5225c82 GL |
1022 | unsigned int i; |
1023 | ||
716c3304 RR |
1024 | for (i = 0; BIT(i) <= mapping->menu_mask; ++i) { |
1025 | u32 menu_value; | |
1026 | ||
40140eda RR |
1027 | if (!test_bit(i, &mapping->menu_mask)) |
1028 | continue; | |
716c3304 RR |
1029 | |
1030 | menu_value = uvc_mapping_get_menu_value(mapping, i); | |
1031 | ||
1032 | if (menu_value == value) { | |
e5225c82 GL |
1033 | value = i; |
1034 | break; | |
1035 | } | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | return value; | |
1040 | } | |
1041 | ||
5f36851c YC |
1042 | static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain, |
1043 | struct uvc_control *ctrl) | |
cb74d482 | 1044 | { |
5f36851c | 1045 | u8 *data; |
cb74d482 HG |
1046 | int ret; |
1047 | ||
5f36851c YC |
1048 | if (ctrl->loaded) |
1049 | return 0; | |
cb74d482 | 1050 | |
5f36851c | 1051 | data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT); |
cb74d482 | 1052 | |
5f36851c YC |
1053 | if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) { |
1054 | memset(data, 0, ctrl->info.size); | |
cb74d482 | 1055 | ctrl->loaded = 1; |
5f36851c YC |
1056 | |
1057 | return 0; | |
cb74d482 HG |
1058 | } |
1059 | ||
5f36851c YC |
1060 | if (ctrl->entity->get_cur) |
1061 | ret = ctrl->entity->get_cur(chain->dev, ctrl->entity, | |
1062 | ctrl->info.selector, data, | |
1063 | ctrl->info.size); | |
1064 | else | |
1065 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, | |
1066 | ctrl->entity->id, chain->dev->intfnum, | |
1067 | ctrl->info.selector, data, | |
1068 | ctrl->info.size); | |
1069 | ||
1070 | if (ret < 0) | |
1071 | return ret; | |
1072 | ||
1073 | ctrl->loaded = 1; | |
1074 | ||
1075 | return ret; | |
1076 | } | |
1077 | ||
1078 | static int __uvc_ctrl_get(struct uvc_video_chain *chain, | |
1079 | struct uvc_control *ctrl, | |
1080 | struct uvc_control_mapping *mapping, | |
1081 | s32 *value) | |
1082 | { | |
1083 | int ret; | |
1084 | ||
1085 | if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) | |
1086 | return -EACCES; | |
1087 | ||
1088 | ret = __uvc_ctrl_load_cur(chain, ctrl); | |
1089 | if (ret < 0) | |
1090 | return ret; | |
1091 | ||
e5225c82 GL |
1092 | *value = __uvc_ctrl_get_value(mapping, |
1093 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); | |
cb74d482 HG |
1094 | |
1095 | return 0; | |
1096 | } | |
1097 | ||
9b31ea80 RR |
1098 | static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id, |
1099 | u32 found_id) | |
1100 | { | |
1101 | bool find_next = req_id & V4L2_CTRL_FLAG_NEXT_CTRL; | |
1102 | unsigned int i; | |
1103 | ||
1104 | req_id &= V4L2_CTRL_ID_MASK; | |
1105 | ||
1106 | for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) { | |
1107 | if (!(chain->ctrl_class_bitmap & BIT(i))) | |
1108 | continue; | |
1109 | if (!find_next) { | |
1110 | if (uvc_control_classes[i] == req_id) | |
1111 | return i; | |
1112 | continue; | |
1113 | } | |
1114 | if (uvc_control_classes[i] > req_id && | |
1115 | uvc_control_classes[i] < found_id) | |
1116 | return i; | |
1117 | } | |
1118 | ||
1119 | return -ENODEV; | |
1120 | } | |
1121 | ||
1122 | static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id, | |
1123 | u32 found_id, struct v4l2_queryctrl *v4l2_ctrl) | |
1124 | { | |
1125 | int idx; | |
1126 | ||
1127 | idx = __uvc_query_v4l2_class(chain, req_id, found_id); | |
1128 | if (idx < 0) | |
1129 | return -ENODEV; | |
1130 | ||
1131 | memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl)); | |
1132 | v4l2_ctrl->id = uvc_control_classes[idx]; | |
1133 | strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id), | |
1134 | sizeof(v4l2_ctrl->name)); | |
1135 | v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS; | |
1136 | v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY | |
1137 | | V4L2_CTRL_FLAG_READ_ONLY; | |
1138 | return 0; | |
1139 | } | |
1140 | ||
9f582f04 HV |
1141 | /* |
1142 | * Check if control @v4l2_id can be accessed by the given control @ioctl | |
1143 | * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS). | |
1144 | * | |
1145 | * For set operations on slave controls, check if the master's value is set to | |
1146 | * manual, either in the others controls set in the same ioctl call, or from | |
1147 | * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set | |
1148 | * both the master and slave control, such as for instance setting | |
1149 | * auto_exposure=1, exposure_time_absolute=251. | |
1150 | */ | |
ee929d5a | 1151 | int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, |
9f582f04 HV |
1152 | const struct v4l2_ext_controls *ctrls, |
1153 | unsigned long ioctl) | |
ee929d5a | 1154 | { |
9f582f04 HV |
1155 | struct uvc_control_mapping *master_map = NULL; |
1156 | struct uvc_control *master_ctrl = NULL; | |
ee929d5a RR |
1157 | struct uvc_control_mapping *mapping; |
1158 | struct uvc_control *ctrl; | |
9f582f04 HV |
1159 | bool read = ioctl == VIDIOC_G_EXT_CTRLS; |
1160 | s32 val; | |
1161 | int ret; | |
1162 | int i; | |
ee929d5a RR |
1163 | |
1164 | if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0) | |
1165 | return -EACCES; | |
1166 | ||
1167 | ctrl = uvc_find_control(chain, v4l2_id, &mapping); | |
1168 | if (!ctrl) | |
1169 | return -EINVAL; | |
1170 | ||
1171 | if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read) | |
1172 | return -EACCES; | |
1173 | ||
1174 | if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read) | |
1175 | return -EACCES; | |
1176 | ||
9f582f04 HV |
1177 | if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id) |
1178 | return 0; | |
1179 | ||
1180 | /* | |
1181 | * Iterate backwards in cases where the master control is accessed | |
1182 | * multiple times in the same ioctl. We want the last value. | |
1183 | */ | |
1184 | for (i = ctrls->count - 1; i >= 0; i--) { | |
1185 | if (ctrls->controls[i].id == mapping->master_id) | |
1186 | return ctrls->controls[i].value == | |
1187 | mapping->master_manual ? 0 : -EACCES; | |
1188 | } | |
1189 | ||
1190 | __uvc_find_control(ctrl->entity, mapping->master_id, &master_map, | |
1191 | &master_ctrl, 0); | |
1192 | ||
1193 | if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) | |
1194 | return 0; | |
1195 | ||
1196 | ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val); | |
1197 | if (ret >= 0 && val != mapping->master_manual) | |
1198 | return -EACCES; | |
1199 | ||
ee929d5a RR |
1200 | return 0; |
1201 | } | |
1202 | ||
70fa906d RR |
1203 | static const char *uvc_map_get_name(const struct uvc_control_mapping *map) |
1204 | { | |
1205 | const char *name; | |
1206 | ||
1207 | if (map->name) | |
1208 | return map->name; | |
1209 | ||
1210 | name = v4l2_ctrl_get_name(map->id); | |
1211 | if (name) | |
1212 | return name; | |
1213 | ||
1214 | return "Unknown Control"; | |
1215 | } | |
1216 | ||
7faf8ae4 RR |
1217 | static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl, |
1218 | struct uvc_control_mapping *mapping) | |
1219 | { | |
1220 | /* | |
1221 | * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent | |
1222 | * the number of bits supported. Those controls do not list GET_MAX | |
1223 | * as supported. | |
1224 | */ | |
1225 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) | |
1226 | return mapping->get(mapping, UVC_GET_RES, | |
1227 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); | |
1228 | ||
1229 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) | |
1230 | return mapping->get(mapping, UVC_GET_MAX, | |
1231 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); | |
1232 | ||
1233 | return ~0; | |
1234 | } | |
1235 | ||
35f16741 HG |
1236 | static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, |
1237 | struct uvc_control *ctrl, | |
1238 | struct uvc_control_mapping *mapping, | |
c0efd232 LP |
1239 | struct v4l2_queryctrl *v4l2_ctrl) |
1240 | { | |
55afeb8a HG |
1241 | struct uvc_control_mapping *master_map = NULL; |
1242 | struct uvc_control *master_ctrl = NULL; | |
c0efd232 | 1243 | unsigned int i; |
c0efd232 | 1244 | |
f14d4988 | 1245 | memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl)); |
c0efd232 LP |
1246 | v4l2_ctrl->id = mapping->id; |
1247 | v4l2_ctrl->type = mapping->v4l2_type; | |
70fa906d RR |
1248 | strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping), |
1249 | sizeof(v4l2_ctrl->name)); | |
c0efd232 LP |
1250 | v4l2_ctrl->flags = 0; |
1251 | ||
9eb30d2f | 1252 | if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) |
a90ef69c | 1253 | v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY; |
9eb30d2f | 1254 | if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) |
c0efd232 LP |
1255 | v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; |
1256 | ||
55afeb8a HG |
1257 | if (mapping->master_id) |
1258 | __uvc_find_control(ctrl->entity, mapping->master_id, | |
1259 | &master_map, &master_ctrl, 0); | |
1260 | if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) { | |
1261 | s32 val; | |
1262 | int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val); | |
1263 | if (ret < 0) | |
1264 | return ret; | |
1265 | ||
1266 | if (val != mapping->master_manual) | |
1267 | v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; | |
1268 | } | |
1269 | ||
59529081 | 1270 | if (!ctrl->cached) { |
35f16741 | 1271 | int ret = uvc_ctrl_populate_cache(chain, ctrl); |
b482d923 | 1272 | if (ret < 0) |
35f16741 | 1273 | return ret; |
59529081 LP |
1274 | } |
1275 | ||
9eb30d2f | 1276 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) { |
59529081 LP |
1277 | v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF, |
1278 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF)); | |
c0efd232 LP |
1279 | } |
1280 | ||
54812c77 LP |
1281 | switch (mapping->v4l2_type) { |
1282 | case V4L2_CTRL_TYPE_MENU: | |
40140eda RR |
1283 | v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1; |
1284 | v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1; | |
c0efd232 LP |
1285 | v4l2_ctrl->step = 1; |
1286 | ||
716c3304 RR |
1287 | for (i = 0; BIT(i) <= mapping->menu_mask; ++i) { |
1288 | u32 menu_value; | |
1289 | ||
40140eda RR |
1290 | if (!test_bit(i, &mapping->menu_mask)) |
1291 | continue; | |
716c3304 RR |
1292 | |
1293 | menu_value = uvc_mapping_get_menu_value(mapping, i); | |
1294 | ||
1295 | if (menu_value == v4l2_ctrl->default_value) { | |
c0efd232 LP |
1296 | v4l2_ctrl->default_value = i; |
1297 | break; | |
1298 | } | |
1299 | } | |
1300 | ||
35f16741 | 1301 | return 0; |
54812c77 LP |
1302 | |
1303 | case V4L2_CTRL_TYPE_BOOLEAN: | |
1304 | v4l2_ctrl->minimum = 0; | |
1305 | v4l2_ctrl->maximum = 1; | |
1306 | v4l2_ctrl->step = 1; | |
35f16741 | 1307 | return 0; |
54812c77 | 1308 | |
f4eabafe LP |
1309 | case V4L2_CTRL_TYPE_BUTTON: |
1310 | v4l2_ctrl->minimum = 0; | |
1311 | v4l2_ctrl->maximum = 0; | |
1312 | v4l2_ctrl->step = 0; | |
35f16741 | 1313 | return 0; |
f4eabafe | 1314 | |
7faf8ae4 RR |
1315 | case V4L2_CTRL_TYPE_BITMASK: |
1316 | v4l2_ctrl->minimum = 0; | |
1317 | v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping); | |
1318 | v4l2_ctrl->step = 0; | |
1319 | return 0; | |
1320 | ||
54812c77 LP |
1321 | default: |
1322 | break; | |
c0efd232 LP |
1323 | } |
1324 | ||
9eb30d2f | 1325 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) |
59529081 LP |
1326 | v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, |
1327 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); | |
c0efd232 | 1328 | |
9eb30d2f | 1329 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) |
59529081 LP |
1330 | v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, |
1331 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); | |
1332 | ||
9eb30d2f | 1333 | if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) |
59529081 LP |
1334 | v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, |
1335 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); | |
1336 | ||
35f16741 HG |
1337 | return 0; |
1338 | } | |
1339 | ||
1340 | int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, | |
1341 | struct v4l2_queryctrl *v4l2_ctrl) | |
1342 | { | |
1343 | struct uvc_control *ctrl; | |
1344 | struct uvc_control_mapping *mapping; | |
1345 | int ret; | |
1346 | ||
1347 | ret = mutex_lock_interruptible(&chain->ctrl_mutex); | |
1348 | if (ret < 0) | |
1349 | return -ERESTARTSYS; | |
1350 | ||
9b31ea80 RR |
1351 | /* Check if the ctrl is a know class */ |
1352 | if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) { | |
1353 | ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl); | |
1354 | if (!ret) | |
1355 | goto done; | |
1356 | } | |
1357 | ||
35f16741 HG |
1358 | ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping); |
1359 | if (ctrl == NULL) { | |
9c016d61 | 1360 | ret = -EINVAL; |
35f16741 HG |
1361 | goto done; |
1362 | } | |
1363 | ||
9b31ea80 RR |
1364 | /* |
1365 | * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if | |
1366 | * a class should be inserted between the previous control and the one | |
1367 | * we have just found. | |
1368 | */ | |
1369 | if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) { | |
1370 | ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id, | |
1371 | v4l2_ctrl); | |
1372 | if (!ret) | |
1373 | goto done; | |
1374 | } | |
1375 | ||
35f16741 | 1376 | ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl); |
27a61c13 LP |
1377 | done: |
1378 | mutex_unlock(&chain->ctrl_mutex); | |
1379 | return ret; | |
c0efd232 LP |
1380 | } |
1381 | ||
23d9f3ef | 1382 | /* |
ec55cdba | 1383 | * Mapping V4L2 controls to UVC controls can be straightforward if done well. |
23d9f3ef LP |
1384 | * Most of the UVC controls exist in V4L2, and can be mapped directly. Some |
1385 | * must be grouped (for instance the Red Balance, Blue Balance and Do White | |
1386 | * Balance V4L2 controls use the White Balance Component UVC control) or | |
1387 | * otherwise translated. The approach we take here is to use a translation | |
1388 | * table for the controls that can be mapped directly, and handle the others | |
1389 | * manually. | |
1390 | */ | |
1391 | int uvc_query_v4l2_menu(struct uvc_video_chain *chain, | |
1392 | struct v4l2_querymenu *query_menu) | |
1393 | { | |
23d9f3ef LP |
1394 | struct uvc_control_mapping *mapping; |
1395 | struct uvc_control *ctrl; | |
1396 | u32 index = query_menu->index; | |
1397 | u32 id = query_menu->id; | |
716c3304 | 1398 | const char *name; |
23d9f3ef LP |
1399 | int ret; |
1400 | ||
1401 | memset(query_menu, 0, sizeof(*query_menu)); | |
1402 | query_menu->id = id; | |
1403 | query_menu->index = index; | |
1404 | ||
41ebaa5e RR |
1405 | if (index >= BITS_PER_TYPE(mapping->menu_mask)) |
1406 | return -EINVAL; | |
1407 | ||
23d9f3ef LP |
1408 | ret = mutex_lock_interruptible(&chain->ctrl_mutex); |
1409 | if (ret < 0) | |
1410 | return -ERESTARTSYS; | |
1411 | ||
1412 | ctrl = uvc_find_control(chain, query_menu->id, &mapping); | |
9c016d61 RW |
1413 | if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) { |
1414 | ret = -EINVAL; | |
23d9f3ef LP |
1415 | goto done; |
1416 | } | |
1417 | ||
40140eda | 1418 | if (!test_bit(query_menu->index, &mapping->menu_mask)) { |
23d9f3ef LP |
1419 | ret = -EINVAL; |
1420 | goto done; | |
1421 | } | |
1422 | ||
7faf8ae4 | 1423 | if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) { |
716c3304 RR |
1424 | int mask; |
1425 | ||
fc2d573f LP |
1426 | if (!ctrl->cached) { |
1427 | ret = uvc_ctrl_populate_cache(chain, ctrl); | |
1428 | if (ret < 0) | |
1429 | goto done; | |
1430 | } | |
1431 | ||
716c3304 RR |
1432 | mask = uvc_mapping_get_menu_value(mapping, query_menu->index); |
1433 | if (mask < 0) { | |
1434 | ret = mask; | |
1435 | goto done; | |
1436 | } | |
1437 | ||
1438 | if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) { | |
fc2d573f LP |
1439 | ret = -EINVAL; |
1440 | goto done; | |
1441 | } | |
1442 | } | |
1443 | ||
716c3304 RR |
1444 | name = uvc_mapping_get_menu_name(mapping, query_menu->index); |
1445 | if (!name) { | |
1446 | ret = -EINVAL; | |
1447 | goto done; | |
1448 | } | |
1449 | ||
1450 | strscpy(query_menu->name, name, sizeof(query_menu->name)); | |
23d9f3ef LP |
1451 | |
1452 | done: | |
1453 | mutex_unlock(&chain->ctrl_mutex); | |
1454 | return ret; | |
1455 | } | |
1456 | ||
b4012002 HG |
1457 | /* -------------------------------------------------------------------------- |
1458 | * Ctrl event handling | |
1459 | */ | |
1460 | ||
1461 | static void uvc_ctrl_fill_event(struct uvc_video_chain *chain, | |
1462 | struct v4l2_event *ev, | |
1463 | struct uvc_control *ctrl, | |
1464 | struct uvc_control_mapping *mapping, | |
1465 | s32 value, u32 changes) | |
1466 | { | |
1467 | struct v4l2_queryctrl v4l2_ctrl; | |
1468 | ||
1469 | __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl); | |
1470 | ||
f45f3f75 | 1471 | memset(ev, 0, sizeof(*ev)); |
b4012002 HG |
1472 | ev->type = V4L2_EVENT_CTRL; |
1473 | ev->id = v4l2_ctrl.id; | |
1474 | ev->u.ctrl.value = value; | |
1475 | ev->u.ctrl.changes = changes; | |
1476 | ev->u.ctrl.type = v4l2_ctrl.type; | |
1477 | ev->u.ctrl.flags = v4l2_ctrl.flags; | |
1478 | ev->u.ctrl.minimum = v4l2_ctrl.minimum; | |
1479 | ev->u.ctrl.maximum = v4l2_ctrl.maximum; | |
1480 | ev->u.ctrl.step = v4l2_ctrl.step; | |
1481 | ev->u.ctrl.default_value = v4l2_ctrl.default_value; | |
1482 | } | |
1483 | ||
e5225c82 GL |
1484 | /* |
1485 | * Send control change events to all subscribers for the @ctrl control. By | |
1486 | * default the subscriber that generated the event, as identified by @handle, | |
1487 | * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag. | |
1488 | * @handle can be NULL for asynchronous events related to auto-update controls, | |
1489 | * in which case all subscribers are notified. | |
1490 | */ | |
1491 | static void uvc_ctrl_send_event(struct uvc_video_chain *chain, | |
1492 | struct uvc_fh *handle, struct uvc_control *ctrl, | |
1493 | struct uvc_control_mapping *mapping, s32 value, u32 changes) | |
b4012002 | 1494 | { |
e5225c82 | 1495 | struct v4l2_fh *originator = handle ? &handle->vfh : NULL; |
b4012002 HG |
1496 | struct v4l2_subscribed_event *sev; |
1497 | struct v4l2_event ev; | |
1498 | ||
1499 | if (list_empty(&mapping->ev_subs)) | |
1500 | return; | |
1501 | ||
e5225c82 | 1502 | uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes); |
b4012002 HG |
1503 | |
1504 | list_for_each_entry(sev, &mapping->ev_subs, node) { | |
e5225c82 | 1505 | if (sev->fh != originator || |
805e9b4a | 1506 | (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) || |
222964ea | 1507 | (changes & V4L2_EVENT_CTRL_CH_FLAGS)) |
b4012002 HG |
1508 | v4l2_event_queue_fh(sev->fh, &ev); |
1509 | } | |
1510 | } | |
1511 | ||
e5225c82 GL |
1512 | /* |
1513 | * Send control change events for the slave of the @master control identified | |
1514 | * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that | |
1515 | * generated the event and may be NULL for auto-update events. | |
1516 | */ | |
1517 | static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain, | |
1518 | struct uvc_fh *handle, struct uvc_control *master, u32 slave_id) | |
805e9b4a HG |
1519 | { |
1520 | struct uvc_control_mapping *mapping = NULL; | |
1521 | struct uvc_control *ctrl = NULL; | |
1522 | u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; | |
805e9b4a HG |
1523 | s32 val = 0; |
1524 | ||
805e9b4a HG |
1525 | __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0); |
1526 | if (ctrl == NULL) | |
1527 | return; | |
1528 | ||
e5225c82 | 1529 | if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0) |
805e9b4a HG |
1530 | changes |= V4L2_EVENT_CTRL_CH_VALUE; |
1531 | ||
e5225c82 GL |
1532 | uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes); |
1533 | } | |
1534 | ||
d9c8763e RR |
1535 | void uvc_ctrl_status_event(struct uvc_video_chain *chain, |
1536 | struct uvc_control *ctrl, const u8 *data) | |
e5225c82 | 1537 | { |
e5225c82 | 1538 | struct uvc_control_mapping *mapping; |
e5225c82 GL |
1539 | struct uvc_fh *handle; |
1540 | unsigned int i; | |
e5225c82 GL |
1541 | |
1542 | mutex_lock(&chain->ctrl_mutex); | |
1543 | ||
1544 | handle = ctrl->handle; | |
1545 | ctrl->handle = NULL; | |
1546 | ||
1547 | list_for_each_entry(mapping, &ctrl->info.mappings, list) { | |
d9c8763e | 1548 | s32 value = __uvc_ctrl_get_value(mapping, data); |
e5225c82 GL |
1549 | |
1550 | /* | |
1551 | * handle may be NULL here if the device sends auto-update | |
1552 | * events without a prior related control set from userspace. | |
1553 | */ | |
1554 | for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) { | |
1555 | if (!mapping->slave_ids[i]) | |
1556 | break; | |
1557 | ||
1558 | uvc_ctrl_send_slave_event(chain, handle, ctrl, | |
1559 | mapping->slave_ids[i]); | |
1560 | } | |
1561 | ||
1562 | uvc_ctrl_send_event(chain, handle, ctrl, mapping, value, | |
1563 | V4L2_EVENT_CTRL_CH_VALUE); | |
1564 | } | |
1565 | ||
1566 | mutex_unlock(&chain->ctrl_mutex); | |
d9c8763e RR |
1567 | } |
1568 | ||
1569 | static void uvc_ctrl_status_event_work(struct work_struct *work) | |
1570 | { | |
1571 | struct uvc_device *dev = container_of(work, struct uvc_device, | |
1572 | async_ctrl.work); | |
1573 | struct uvc_ctrl_work *w = &dev->async_ctrl; | |
1574 | int ret; | |
1575 | ||
1576 | uvc_ctrl_status_event(w->chain, w->ctrl, w->data); | |
e5225c82 | 1577 | |
619d9b71 RR |
1578 | /* The barrier is needed to synchronize with uvc_status_stop(). */ |
1579 | if (smp_load_acquire(&dev->flush_status)) | |
1580 | return; | |
1581 | ||
e5225c82 GL |
1582 | /* Resubmit the URB. */ |
1583 | w->urb->interval = dev->int_ep->desc.bInterval; | |
1584 | ret = usb_submit_urb(w->urb, GFP_KERNEL); | |
1585 | if (ret < 0) | |
69df0954 RR |
1586 | dev_err(&dev->udev->dev, |
1587 | "Failed to resubmit status URB (%d).\n", ret); | |
e5225c82 GL |
1588 | } |
1589 | ||
d9c8763e RR |
1590 | bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain, |
1591 | struct uvc_control *ctrl, const u8 *data) | |
e5225c82 GL |
1592 | { |
1593 | struct uvc_device *dev = chain->dev; | |
1594 | struct uvc_ctrl_work *w = &dev->async_ctrl; | |
1595 | ||
1596 | if (list_empty(&ctrl->info.mappings)) { | |
1597 | ctrl->handle = NULL; | |
1598 | return false; | |
1599 | } | |
1600 | ||
1601 | w->data = data; | |
1602 | w->urb = urb; | |
1603 | w->chain = chain; | |
1604 | w->ctrl = ctrl; | |
1605 | ||
1606 | schedule_work(&w->work); | |
1607 | ||
1608 | return true; | |
1609 | } | |
1610 | ||
1611 | static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls, | |
1612 | unsigned int xctrls_count, u32 id) | |
1613 | { | |
1614 | unsigned int i; | |
1615 | ||
1616 | for (i = 0; i < xctrls_count; ++i) { | |
1617 | if (xctrls[i].id == id) | |
1618 | return true; | |
1619 | } | |
1620 | ||
1621 | return false; | |
805e9b4a HG |
1622 | } |
1623 | ||
b4012002 HG |
1624 | static void uvc_ctrl_send_events(struct uvc_fh *handle, |
1625 | const struct v4l2_ext_control *xctrls, unsigned int xctrls_count) | |
1626 | { | |
1627 | struct uvc_control_mapping *mapping; | |
1628 | struct uvc_control *ctrl; | |
805e9b4a | 1629 | u32 changes = V4L2_EVENT_CTRL_CH_VALUE; |
b4012002 | 1630 | unsigned int i; |
805e9b4a | 1631 | unsigned int j; |
b4012002 HG |
1632 | |
1633 | for (i = 0; i < xctrls_count; ++i) { | |
1634 | ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping); | |
805e9b4a | 1635 | |
e5225c82 GL |
1636 | if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) |
1637 | /* Notification will be sent from an Interrupt event. */ | |
1638 | continue; | |
1639 | ||
805e9b4a | 1640 | for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) { |
e5225c82 GL |
1641 | u32 slave_id = mapping->slave_ids[j]; |
1642 | ||
1643 | if (!slave_id) | |
805e9b4a | 1644 | break; |
e5225c82 GL |
1645 | |
1646 | /* | |
1647 | * We can skip sending an event for the slave if the | |
1648 | * slave is being modified in the same transaction. | |
1649 | */ | |
1650 | if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count, | |
1651 | slave_id)) | |
1652 | continue; | |
1653 | ||
1654 | uvc_ctrl_send_slave_event(handle->chain, handle, ctrl, | |
1655 | slave_id); | |
805e9b4a HG |
1656 | } |
1657 | ||
1658 | /* | |
1659 | * If the master is being modified in the same transaction | |
1660 | * flags may change too. | |
1661 | */ | |
e5225c82 GL |
1662 | if (mapping->master_id && |
1663 | uvc_ctrl_xctrls_has_control(xctrls, xctrls_count, | |
1664 | mapping->master_id)) | |
1665 | changes |= V4L2_EVENT_CTRL_CH_FLAGS; | |
805e9b4a | 1666 | |
e5225c82 GL |
1667 | uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping, |
1668 | xctrls[i].value, changes); | |
b4012002 HG |
1669 | } |
1670 | } | |
1671 | ||
6e6d76cd | 1672 | static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems) |
b4012002 HG |
1673 | { |
1674 | struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh); | |
1675 | struct uvc_control_mapping *mapping; | |
1676 | struct uvc_control *ctrl; | |
1677 | int ret; | |
1678 | ||
1679 | ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex); | |
1680 | if (ret < 0) | |
1681 | return -ERESTARTSYS; | |
1682 | ||
9b31ea80 RR |
1683 | if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) { |
1684 | ret = 0; | |
1685 | goto done; | |
1686 | } | |
1687 | ||
b4012002 HG |
1688 | ctrl = uvc_find_control(handle->chain, sev->id, &mapping); |
1689 | if (ctrl == NULL) { | |
9c016d61 | 1690 | ret = -EINVAL; |
b4012002 HG |
1691 | goto done; |
1692 | } | |
1693 | ||
1694 | list_add_tail(&sev->node, &mapping->ev_subs); | |
1695 | if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) { | |
1696 | struct v4l2_event ev; | |
1697 | u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; | |
1698 | s32 val = 0; | |
1699 | ||
1700 | if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0) | |
1701 | changes |= V4L2_EVENT_CTRL_CH_VALUE; | |
1702 | ||
1703 | uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val, | |
1704 | changes); | |
699b9a86 LP |
1705 | /* |
1706 | * Mark the queue as active, allowing this initial event to be | |
1707 | * accepted. | |
1708 | */ | |
6e6d76cd | 1709 | sev->elems = elems; |
b4012002 HG |
1710 | v4l2_event_queue_fh(sev->fh, &ev); |
1711 | } | |
1712 | ||
1713 | done: | |
1714 | mutex_unlock(&handle->chain->ctrl_mutex); | |
1715 | return ret; | |
1716 | } | |
1717 | ||
1718 | static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev) | |
1719 | { | |
1720 | struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh); | |
1721 | ||
1722 | mutex_lock(&handle->chain->ctrl_mutex); | |
9b31ea80 RR |
1723 | if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) |
1724 | goto done; | |
b4012002 | 1725 | list_del(&sev->node); |
9b31ea80 | 1726 | done: |
b4012002 HG |
1727 | mutex_unlock(&handle->chain->ctrl_mutex); |
1728 | } | |
1729 | ||
1730 | const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = { | |
1731 | .add = uvc_ctrl_add_event, | |
1732 | .del = uvc_ctrl_del_event, | |
1733 | .replace = v4l2_ctrl_replace, | |
1734 | .merge = v4l2_ctrl_merge, | |
1735 | }; | |
c0efd232 LP |
1736 | |
1737 | /* -------------------------------------------------------------------------- | |
1738 | * Control transactions | |
1739 | * | |
1740 | * To make extended set operations as atomic as the hardware allows, controls | |
1741 | * are handled using begin/commit/rollback operations. | |
1742 | * | |
1743 | * At the beginning of a set request, uvc_ctrl_begin should be called to | |
1744 | * initialize the request. This function acquires the control lock. | |
1745 | * | |
1746 | * When setting a control, the new value is stored in the control data field | |
1747 | * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for | |
1748 | * later processing. If the UVC and V4L2 control sizes differ, the current | |
1749 | * value is loaded from the hardware before storing the new value in the data | |
1750 | * field. | |
1751 | * | |
1752 | * After processing all controls in the transaction, uvc_ctrl_commit or | |
1753 | * uvc_ctrl_rollback must be called to apply the pending changes to the | |
1754 | * hardware or revert them. When applying changes, all controls marked as | |
1755 | * dirty will be modified in the UVC device, and the dirty flag will be | |
1756 | * cleared. When reverting controls, the control data field | |
1757 | * UVC_CTRL_DATA_CURRENT is reverted to its previous value | |
1758 | * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the | |
1759 | * control lock. | |
1760 | */ | |
8e113595 | 1761 | int uvc_ctrl_begin(struct uvc_video_chain *chain) |
c0efd232 | 1762 | { |
8e113595 | 1763 | return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0; |
c0efd232 LP |
1764 | } |
1765 | ||
1766 | static int uvc_ctrl_commit_entity(struct uvc_device *dev, | |
6350d6a4 | 1767 | struct uvc_entity *entity, int rollback, struct uvc_control **err_ctrl) |
c0efd232 LP |
1768 | { |
1769 | struct uvc_control *ctrl; | |
1770 | unsigned int i; | |
1771 | int ret; | |
1772 | ||
1773 | if (entity == NULL) | |
1774 | return 0; | |
1775 | ||
1776 | for (i = 0; i < entity->ncontrols; ++i) { | |
1777 | ctrl = &entity->controls[i]; | |
071c8bb8 | 1778 | if (!ctrl->initialized) |
b1accfa1 LP |
1779 | continue; |
1780 | ||
699b9a86 LP |
1781 | /* |
1782 | * Reset the loaded flag for auto-update controls that were | |
b1accfa1 | 1783 | * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent |
6b363f9f LP |
1784 | * uvc_ctrl_get from using the cached value, and for write-only |
1785 | * controls to prevent uvc_ctrl_set from setting bits not | |
1786 | * explicitly set by the user. | |
b1accfa1 | 1787 | */ |
6b363f9f LP |
1788 | if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE || |
1789 | !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) | |
b1accfa1 LP |
1790 | ctrl->loaded = 0; |
1791 | ||
1792 | if (!ctrl->dirty) | |
c0efd232 LP |
1793 | continue; |
1794 | ||
1795 | if (!rollback) | |
b482d923 | 1796 | ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id, |
071c8bb8 | 1797 | dev->intfnum, ctrl->info.selector, |
c0efd232 | 1798 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
071c8bb8 | 1799 | ctrl->info.size); |
c0efd232 LP |
1800 | else |
1801 | ret = 0; | |
1802 | ||
1803 | if (rollback || ret < 0) | |
1804 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), | |
1805 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), | |
071c8bb8 | 1806 | ctrl->info.size); |
c0efd232 | 1807 | |
c0efd232 LP |
1808 | ctrl->dirty = 0; |
1809 | ||
6350d6a4 RR |
1810 | if (ret < 0) { |
1811 | if (err_ctrl) | |
1812 | *err_ctrl = ctrl; | |
c0efd232 | 1813 | return ret; |
6350d6a4 | 1814 | } |
c0efd232 LP |
1815 | } |
1816 | ||
1817 | return 0; | |
1818 | } | |
1819 | ||
6350d6a4 RR |
1820 | static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity, |
1821 | struct v4l2_ext_controls *ctrls, | |
1822 | struct uvc_control *uvc_control) | |
1823 | { | |
f0577b1b | 1824 | struct uvc_control_mapping *mapping = NULL; |
414d3b49 | 1825 | struct uvc_control *ctrl_found = NULL; |
6350d6a4 RR |
1826 | unsigned int i; |
1827 | ||
1828 | if (!entity) | |
1829 | return ctrls->count; | |
1830 | ||
1831 | for (i = 0; i < ctrls->count; i++) { | |
1832 | __uvc_find_control(entity, ctrls->controls[i].id, &mapping, | |
1833 | &ctrl_found, 0); | |
1834 | if (uvc_control == ctrl_found) | |
1835 | return i; | |
1836 | } | |
1837 | ||
1838 | return ctrls->count; | |
1839 | } | |
1840 | ||
b4012002 | 1841 | int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback, |
6350d6a4 | 1842 | struct v4l2_ext_controls *ctrls) |
c0efd232 | 1843 | { |
b4012002 | 1844 | struct uvc_video_chain *chain = handle->chain; |
6350d6a4 | 1845 | struct uvc_control *err_ctrl; |
c0efd232 LP |
1846 | struct uvc_entity *entity; |
1847 | int ret = 0; | |
1848 | ||
1849 | /* Find the control. */ | |
6241d8ca | 1850 | list_for_each_entry(entity, &chain->entities, chain) { |
6350d6a4 RR |
1851 | ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback, |
1852 | &err_ctrl); | |
c0efd232 LP |
1853 | if (ret < 0) |
1854 | goto done; | |
1855 | } | |
1856 | ||
b4012002 | 1857 | if (!rollback) |
6350d6a4 | 1858 | uvc_ctrl_send_events(handle, ctrls->controls, ctrls->count); |
c0efd232 | 1859 | done: |
6350d6a4 RR |
1860 | if (ret < 0 && ctrls) |
1861 | ctrls->error_idx = uvc_ctrl_find_ctrl_idx(entity, ctrls, | |
1862 | err_ctrl); | |
8e113595 | 1863 | mutex_unlock(&chain->ctrl_mutex); |
c0efd232 LP |
1864 | return ret; |
1865 | } | |
1866 | ||
35f16741 HG |
1867 | int uvc_ctrl_get(struct uvc_video_chain *chain, |
1868 | struct v4l2_ext_control *xctrl) | |
1869 | { | |
1870 | struct uvc_control *ctrl; | |
1871 | struct uvc_control_mapping *mapping; | |
1872 | ||
9b31ea80 RR |
1873 | if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0) |
1874 | return -EACCES; | |
1875 | ||
35f16741 HG |
1876 | ctrl = uvc_find_control(chain, xctrl->id, &mapping); |
1877 | if (ctrl == NULL) | |
9c016d61 | 1878 | return -EINVAL; |
35f16741 HG |
1879 | |
1880 | return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value); | |
1881 | } | |
1882 | ||
e5225c82 | 1883 | int uvc_ctrl_set(struct uvc_fh *handle, |
c0efd232 LP |
1884 | struct v4l2_ext_control *xctrl) |
1885 | { | |
e5225c82 | 1886 | struct uvc_video_chain *chain = handle->chain; |
c0efd232 LP |
1887 | struct uvc_control *ctrl; |
1888 | struct uvc_control_mapping *mapping; | |
e54532e5 LP |
1889 | s32 value; |
1890 | u32 step; | |
1891 | s32 min; | |
1892 | s32 max; | |
c0efd232 LP |
1893 | int ret; |
1894 | ||
9b31ea80 RR |
1895 | if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0) |
1896 | return -EACCES; | |
1897 | ||
8e113595 | 1898 | ctrl = uvc_find_control(chain, xctrl->id, &mapping); |
9bf42300 | 1899 | if (ctrl == NULL) |
9c016d61 | 1900 | return -EINVAL; |
9bf42300 LP |
1901 | if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) |
1902 | return -EACCES; | |
c0efd232 | 1903 | |
e54532e5 LP |
1904 | /* Clamp out of range values. */ |
1905 | switch (mapping->v4l2_type) { | |
1906 | case V4L2_CTRL_TYPE_INTEGER: | |
1907 | if (!ctrl->cached) { | |
1908 | ret = uvc_ctrl_populate_cache(chain, ctrl); | |
1909 | if (ret < 0) | |
1910 | return ret; | |
1911 | } | |
1912 | ||
1913 | min = mapping->get(mapping, UVC_GET_MIN, | |
1914 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); | |
1915 | max = mapping->get(mapping, UVC_GET_MAX, | |
1916 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); | |
1917 | step = mapping->get(mapping, UVC_GET_RES, | |
1918 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); | |
cf7a50ee LP |
1919 | if (step == 0) |
1920 | step = 1; | |
e54532e5 | 1921 | |
7ae53081 | 1922 | xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min), |
1923 | step) * step; | |
64ae9958 LP |
1924 | if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) |
1925 | xctrl->value = clamp(xctrl->value, min, max); | |
1926 | else | |
1927 | xctrl->value = clamp_t(u32, xctrl->value, min, max); | |
e54532e5 LP |
1928 | value = xctrl->value; |
1929 | break; | |
1930 | ||
7faf8ae4 RR |
1931 | case V4L2_CTRL_TYPE_BITMASK: |
1932 | if (!ctrl->cached) { | |
1933 | ret = uvc_ctrl_populate_cache(chain, ctrl); | |
1934 | if (ret < 0) | |
1935 | return ret; | |
1936 | } | |
1937 | ||
1938 | xctrl->value &= uvc_get_ctrl_bitmap(ctrl, mapping); | |
1939 | value = xctrl->value; | |
1940 | break; | |
1941 | ||
e54532e5 LP |
1942 | case V4L2_CTRL_TYPE_BOOLEAN: |
1943 | xctrl->value = clamp(xctrl->value, 0, 1); | |
1944 | value = xctrl->value; | |
1945 | break; | |
1946 | ||
1947 | case V4L2_CTRL_TYPE_MENU: | |
40140eda RR |
1948 | if (xctrl->value < (ffs(mapping->menu_mask) - 1) || |
1949 | xctrl->value > (fls(mapping->menu_mask) - 1)) | |
8a4e76c1 | 1950 | return -ERANGE; |
40140eda RR |
1951 | |
1952 | if (!test_bit(xctrl->value, &mapping->menu_mask)) | |
1953 | return -EINVAL; | |
1954 | ||
716c3304 | 1955 | value = uvc_mapping_get_menu_value(mapping, xctrl->value); |
f411f103 | 1956 | |
699b9a86 LP |
1957 | /* |
1958 | * Valid menu indices are reported by the GET_RES request for | |
f411f103 LP |
1959 | * UVC controls that support it. |
1960 | */ | |
7faf8ae4 | 1961 | if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) { |
f411f103 LP |
1962 | if (!ctrl->cached) { |
1963 | ret = uvc_ctrl_populate_cache(chain, ctrl); | |
1964 | if (ret < 0) | |
1965 | return ret; | |
1966 | } | |
1967 | ||
7faf8ae4 | 1968 | if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & value)) |
b4186971 | 1969 | return -EINVAL; |
f411f103 LP |
1970 | } |
1971 | ||
e54532e5 LP |
1972 | break; |
1973 | ||
1974 | default: | |
1975 | value = xctrl->value; | |
1976 | break; | |
c0efd232 LP |
1977 | } |
1978 | ||
699b9a86 LP |
1979 | /* |
1980 | * If the mapping doesn't span the whole UVC control, the current value | |
e54532e5 LP |
1981 | * needs to be loaded from the device to perform the read-modify-write |
1982 | * operation. | |
1983 | */ | |
5f36851c YC |
1984 | if ((ctrl->info.size * 8) != mapping->size) { |
1985 | ret = __uvc_ctrl_load_cur(chain, ctrl); | |
1986 | if (ret < 0) | |
1987 | return ret; | |
c0efd232 LP |
1988 | } |
1989 | ||
e54532e5 | 1990 | /* Backup the current value in case we need to rollback later. */ |
c0efd232 LP |
1991 | if (!ctrl->dirty) { |
1992 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), | |
1993 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), | |
071c8bb8 | 1994 | ctrl->info.size); |
c0efd232 LP |
1995 | } |
1996 | ||
9768352a LP |
1997 | mapping->set(mapping, value, |
1998 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); | |
c0efd232 | 1999 | |
e5225c82 GL |
2000 | if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) |
2001 | ctrl->handle = handle; | |
2002 | ||
c0efd232 LP |
2003 | ctrl->dirty = 1; |
2004 | ctrl->modified = 1; | |
2005 | return 0; | |
2006 | } | |
2007 | ||
2008 | /* -------------------------------------------------------------------------- | |
2009 | * Dynamic controls | |
2010 | */ | |
2011 | ||
859086ae ET |
2012 | /* |
2013 | * Retrieve flags for a given control | |
2014 | */ | |
2015 | static int uvc_ctrl_get_flags(struct uvc_device *dev, | |
2016 | const struct uvc_control *ctrl, | |
2017 | struct uvc_control_info *info) | |
2018 | { | |
2019 | u8 *data; | |
2020 | int ret; | |
2021 | ||
2022 | data = kmalloc(1, GFP_KERNEL); | |
2023 | if (data == NULL) | |
2024 | return -ENOMEM; | |
2025 | ||
65900c58 RR |
2026 | if (ctrl->entity->get_info) |
2027 | ret = ctrl->entity->get_info(dev, ctrl->entity, | |
2028 | ctrl->info.selector, data); | |
2029 | else | |
2030 | ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, | |
2031 | dev->intfnum, info->selector, data, 1); | |
859086ae | 2032 | if (!ret) |
0dc68cab KB |
2033 | info->flags |= (data[0] & UVC_CONTROL_CAP_GET ? |
2034 | UVC_CTRL_FLAG_GET_CUR : 0) | |
2035 | | (data[0] & UVC_CONTROL_CAP_SET ? | |
2036 | UVC_CTRL_FLAG_SET_CUR : 0) | |
2037 | | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ? | |
e5225c82 GL |
2038 | UVC_CTRL_FLAG_AUTO_UPDATE : 0) |
2039 | | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ? | |
2040 | UVC_CTRL_FLAG_ASYNCHRONOUS : 0); | |
859086ae ET |
2041 | |
2042 | kfree(data); | |
2043 | return ret; | |
2044 | } | |
2045 | ||
b5977a58 LP |
2046 | static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev, |
2047 | const struct uvc_control *ctrl, struct uvc_control_info *info) | |
2048 | { | |
2049 | struct uvc_ctrl_fixup { | |
2050 | struct usb_device_id id; | |
2051 | u8 entity; | |
2052 | u8 selector; | |
2053 | u8 flags; | |
2054 | }; | |
2055 | ||
2056 | static const struct uvc_ctrl_fixup fixups[] = { | |
2057 | { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1, | |
9eb30d2f LP |
2058 | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | |
2059 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | | |
2060 | UVC_CTRL_FLAG_AUTO_UPDATE }, | |
b5977a58 | 2061 | { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1, |
9eb30d2f LP |
2062 | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | |
2063 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | | |
2064 | UVC_CTRL_FLAG_AUTO_UPDATE }, | |
b5977a58 | 2065 | { { USB_DEVICE(0x046d, 0x0994) }, 9, 1, |
9eb30d2f LP |
2066 | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | |
2067 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | | |
2068 | UVC_CTRL_FLAG_AUTO_UPDATE }, | |
b5977a58 LP |
2069 | }; |
2070 | ||
2071 | unsigned int i; | |
2072 | ||
2073 | for (i = 0; i < ARRAY_SIZE(fixups); ++i) { | |
2074 | if (!usb_match_one_id(dev->intf, &fixups[i].id)) | |
2075 | continue; | |
2076 | ||
2077 | if (fixups[i].entity == ctrl->entity->id && | |
2078 | fixups[i].selector == info->selector) { | |
2079 | info->flags = fixups[i].flags; | |
2080 | return; | |
2081 | } | |
2082 | } | |
2083 | } | |
2084 | ||
52c58ad6 LP |
2085 | /* |
2086 | * Query control information (size and flags) for XU controls. | |
2087 | */ | |
2088 | static int uvc_ctrl_fill_xu_info(struct uvc_device *dev, | |
2089 | const struct uvc_control *ctrl, struct uvc_control_info *info) | |
2090 | { | |
2091 | u8 *data; | |
2092 | int ret; | |
2093 | ||
2094 | data = kmalloc(2, GFP_KERNEL); | |
2095 | if (data == NULL) | |
2096 | return -ENOMEM; | |
2097 | ||
351509c6 | 2098 | memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity)); |
52c58ad6 LP |
2099 | info->index = ctrl->index; |
2100 | info->selector = ctrl->index + 1; | |
2101 | ||
2102 | /* Query and verify the control length (GET_LEN) */ | |
2103 | ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum, | |
2104 | info->selector, data, 2); | |
2105 | if (ret < 0) { | |
9e56380a JP |
2106 | uvc_dbg(dev, CONTROL, |
2107 | "GET_LEN failed on control %pUl/%u (%d)\n", | |
2108 | info->entity, info->selector, ret); | |
52c58ad6 LP |
2109 | goto done; |
2110 | } | |
2111 | ||
2112 | info->size = le16_to_cpup((__le16 *)data); | |
2113 | ||
0dc68cab KB |
2114 | info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2115 | | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF; | |
2116 | ||
859086ae | 2117 | ret = uvc_ctrl_get_flags(dev, ctrl, info); |
52c58ad6 | 2118 | if (ret < 0) { |
9e56380a JP |
2119 | uvc_dbg(dev, CONTROL, |
2120 | "Failed to get flags for control %pUl/%u (%d)\n", | |
2121 | info->entity, info->selector, ret); | |
52c58ad6 LP |
2122 | goto done; |
2123 | } | |
2124 | ||
b5977a58 LP |
2125 | uvc_ctrl_fixup_xu_info(dev, ctrl, info); |
2126 | ||
9e56380a JP |
2127 | uvc_dbg(dev, CONTROL, |
2128 | "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n", | |
2129 | info->entity, info->selector, info->size, | |
2130 | (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0, | |
2131 | (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0, | |
2132 | (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0); | |
52c58ad6 LP |
2133 | |
2134 | done: | |
2135 | kfree(data); | |
2136 | return ret; | |
2137 | } | |
2138 | ||
2139 | static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, | |
2140 | const struct uvc_control_info *info); | |
2141 | ||
2142 | static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev, | |
2143 | struct uvc_control *ctrl) | |
2144 | { | |
2145 | struct uvc_control_info info; | |
2146 | int ret; | |
2147 | ||
2148 | if (ctrl->initialized) | |
2149 | return 0; | |
2150 | ||
2151 | ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info); | |
2152 | if (ret < 0) | |
2153 | return ret; | |
2154 | ||
2155 | ret = uvc_ctrl_add_info(dev, ctrl, &info); | |
2156 | if (ret < 0) | |
9e56380a JP |
2157 | uvc_dbg(dev, CONTROL, |
2158 | "Failed to initialize control %pUl/%u on device %s entity %u\n", | |
2159 | info.entity, info.selector, dev->udev->devpath, | |
2160 | ctrl->entity->id); | |
52c58ad6 LP |
2161 | |
2162 | return ret; | |
2163 | } | |
2164 | ||
8e113595 | 2165 | int uvc_xu_ctrl_query(struct uvc_video_chain *chain, |
fe78d187 | 2166 | struct uvc_xu_control_query *xqry) |
c0efd232 LP |
2167 | { |
2168 | struct uvc_entity *entity; | |
fe78d187 | 2169 | struct uvc_control *ctrl; |
f875bcc3 DA |
2170 | unsigned int i; |
2171 | bool found; | |
2c6b222c LP |
2172 | u32 reqflags; |
2173 | u16 size; | |
2174 | u8 *data = NULL; | |
c0efd232 LP |
2175 | int ret; |
2176 | ||
2177 | /* Find the extension unit. */ | |
f875bcc3 | 2178 | found = false; |
6241d8ca LP |
2179 | list_for_each_entry(entity, &chain->entities, chain) { |
2180 | if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT && | |
f875bcc3 DA |
2181 | entity->id == xqry->unit) { |
2182 | found = true; | |
c0efd232 | 2183 | break; |
f875bcc3 | 2184 | } |
c0efd232 LP |
2185 | } |
2186 | ||
f875bcc3 | 2187 | if (!found) { |
9e56380a JP |
2188 | uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n", |
2189 | xqry->unit); | |
fe78d187 | 2190 | return -ENOENT; |
c0efd232 LP |
2191 | } |
2192 | ||
52c58ad6 | 2193 | /* Find the control and perform delayed initialization if needed. */ |
f875bcc3 | 2194 | found = false; |
c0efd232 LP |
2195 | for (i = 0; i < entity->ncontrols; ++i) { |
2196 | ctrl = &entity->controls[i]; | |
fe78d187 | 2197 | if (ctrl->index == xqry->selector - 1) { |
f875bcc3 | 2198 | found = true; |
c0efd232 LP |
2199 | break; |
2200 | } | |
2201 | } | |
2202 | ||
2203 | if (!found) { | |
9e56380a JP |
2204 | uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n", |
2205 | entity->guid, xqry->selector); | |
fe78d187 | 2206 | return -ENOENT; |
c0efd232 LP |
2207 | } |
2208 | ||
27a61c13 LP |
2209 | if (mutex_lock_interruptible(&chain->ctrl_mutex)) |
2210 | return -ERESTARTSYS; | |
2211 | ||
52c58ad6 | 2212 | ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl); |
27a61c13 LP |
2213 | if (ret < 0) { |
2214 | ret = -ENOENT; | |
2215 | goto done; | |
2216 | } | |
52c58ad6 | 2217 | |
fe78d187 MR |
2218 | /* Validate the required buffer size and flags for the request */ |
2219 | reqflags = 0; | |
2220 | size = ctrl->info.size; | |
2221 | ||
2222 | switch (xqry->query) { | |
2223 | case UVC_GET_CUR: | |
9eb30d2f | 2224 | reqflags = UVC_CTRL_FLAG_GET_CUR; |
fe78d187 MR |
2225 | break; |
2226 | case UVC_GET_MIN: | |
9eb30d2f | 2227 | reqflags = UVC_CTRL_FLAG_GET_MIN; |
fe78d187 MR |
2228 | break; |
2229 | case UVC_GET_MAX: | |
9eb30d2f | 2230 | reqflags = UVC_CTRL_FLAG_GET_MAX; |
fe78d187 MR |
2231 | break; |
2232 | case UVC_GET_DEF: | |
9eb30d2f | 2233 | reqflags = UVC_CTRL_FLAG_GET_DEF; |
fe78d187 MR |
2234 | break; |
2235 | case UVC_GET_RES: | |
9eb30d2f | 2236 | reqflags = UVC_CTRL_FLAG_GET_RES; |
fe78d187 MR |
2237 | break; |
2238 | case UVC_SET_CUR: | |
9eb30d2f | 2239 | reqflags = UVC_CTRL_FLAG_SET_CUR; |
fe78d187 MR |
2240 | break; |
2241 | case UVC_GET_LEN: | |
2242 | size = 2; | |
2243 | break; | |
2244 | case UVC_GET_INFO: | |
2245 | size = 1; | |
2246 | break; | |
2247 | default: | |
27a61c13 LP |
2248 | ret = -EINVAL; |
2249 | goto done; | |
2250 | } | |
c0efd232 | 2251 | |
fe78d187 MR |
2252 | if (size != xqry->size) { |
2253 | ret = -ENOBUFS; | |
27a61c13 LP |
2254 | goto done; |
2255 | } | |
c0efd232 | 2256 | |
fe78d187 MR |
2257 | if (reqflags && !(ctrl->info.flags & reqflags)) { |
2258 | ret = -EBADRQC; | |
2259 | goto done; | |
2260 | } | |
2261 | ||
2262 | data = kmalloc(size, GFP_KERNEL); | |
2263 | if (data == NULL) { | |
2264 | ret = -ENOMEM; | |
2265 | goto done; | |
2266 | } | |
c0efd232 | 2267 | |
fe78d187 MR |
2268 | if (xqry->query == UVC_SET_CUR && |
2269 | copy_from_user(data, xqry->data, size)) { | |
c0efd232 | 2270 | ret = -EFAULT; |
27a61c13 | 2271 | goto done; |
c0efd232 LP |
2272 | } |
2273 | ||
fe78d187 MR |
2274 | ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit, |
2275 | chain->dev->intfnum, xqry->selector, data, size); | |
c0efd232 | 2276 | if (ret < 0) |
27a61c13 | 2277 | goto done; |
c0efd232 | 2278 | |
fe78d187 MR |
2279 | if (xqry->query != UVC_SET_CUR && |
2280 | copy_to_user(xqry->data, data, size)) | |
c0efd232 | 2281 | ret = -EFAULT; |
27a61c13 | 2282 | done: |
fe78d187 | 2283 | kfree(data); |
8e113595 | 2284 | mutex_unlock(&chain->ctrl_mutex); |
c0efd232 LP |
2285 | return ret; |
2286 | } | |
2287 | ||
2288 | /* -------------------------------------------------------------------------- | |
2289 | * Suspend/resume | |
2290 | */ | |
2291 | ||
2292 | /* | |
2293 | * Restore control values after resume, skipping controls that haven't been | |
2294 | * changed. | |
2295 | * | |
2296 | * TODO | |
2297 | * - Don't restore modified controls that are back to their default value. | |
2298 | * - Handle restore order (Auto-Exposure Mode should be restored before | |
2299 | * Exposure Time). | |
2300 | */ | |
17e1319f | 2301 | int uvc_ctrl_restore_values(struct uvc_device *dev) |
c0efd232 LP |
2302 | { |
2303 | struct uvc_control *ctrl; | |
2304 | struct uvc_entity *entity; | |
2305 | unsigned int i; | |
2306 | int ret; | |
2307 | ||
2308 | /* Walk the entities list and restore controls when possible. */ | |
2309 | list_for_each_entry(entity, &dev->entities, list) { | |
2310 | ||
2311 | for (i = 0; i < entity->ncontrols; ++i) { | |
2312 | ctrl = &entity->controls[i]; | |
2313 | ||
071c8bb8 | 2314 | if (!ctrl->initialized || !ctrl->modified || |
9eb30d2f | 2315 | (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0) |
c0efd232 | 2316 | continue; |
15486e09 HV |
2317 | dev_dbg(&dev->udev->dev, |
2318 | "restoring control %pUl/%u/%u\n", | |
2319 | ctrl->info.entity, ctrl->info.index, | |
2320 | ctrl->info.selector); | |
c0efd232 LP |
2321 | ctrl->dirty = 1; |
2322 | } | |
2323 | ||
6350d6a4 | 2324 | ret = uvc_ctrl_commit_entity(dev, entity, 0, NULL); |
c0efd232 LP |
2325 | if (ret < 0) |
2326 | return ret; | |
2327 | } | |
2328 | ||
2329 | return 0; | |
2330 | } | |
2331 | ||
2332 | /* -------------------------------------------------------------------------- | |
2333 | * Control and mapping handling | |
2334 | */ | |
2335 | ||
ba2fa996 LP |
2336 | /* |
2337 | * Add control information to a given control. | |
2338 | */ | |
2339 | static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, | |
2340 | const struct uvc_control_info *info) | |
2341 | { | |
8c0d44e2 | 2342 | ctrl->info = *info; |
071c8bb8 | 2343 | INIT_LIST_HEAD(&ctrl->info.mappings); |
ba2fa996 LP |
2344 | |
2345 | /* Allocate an array to save control values (cur, def, max, etc.) */ | |
071c8bb8 | 2346 | ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1, |
ba2fa996 | 2347 | GFP_KERNEL); |
bed888de HG |
2348 | if (!ctrl->uvc_data) |
2349 | return -ENOMEM; | |
b30ece53 | 2350 | |
071c8bb8 LP |
2351 | ctrl->initialized = 1; |
2352 | ||
9e56380a JP |
2353 | uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n", |
2354 | ctrl->info.entity, ctrl->info.selector, dev->udev->devpath, | |
2355 | ctrl->entity->id); | |
b30ece53 | 2356 | |
bed888de | 2357 | return 0; |
c0efd232 LP |
2358 | } |
2359 | ||
2360 | /* | |
ba2fa996 | 2361 | * Add a control mapping to a given control. |
c0efd232 | 2362 | */ |
866c6bdd | 2363 | static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain, |
ba2fa996 | 2364 | struct uvc_control *ctrl, const struct uvc_control_mapping *mapping) |
c0efd232 | 2365 | { |
ba2fa996 LP |
2366 | struct uvc_control_mapping *map; |
2367 | unsigned int size; | |
9b31ea80 | 2368 | unsigned int i; |
c0efd232 | 2369 | |
699b9a86 | 2370 | /* |
252d50da | 2371 | * Most mappings come from static kernel data, and need to be duplicated. |
ba2fa996 LP |
2372 | * Mappings that come from userspace will be unnecessarily duplicated, |
2373 | * this could be optimized. | |
c0efd232 | 2374 | */ |
ba2fa996 | 2375 | map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL); |
252d50da | 2376 | if (!map) |
ba2fa996 | 2377 | return -ENOMEM; |
c0efd232 | 2378 | |
252d50da | 2379 | map->name = NULL; |
716c3304 RR |
2380 | map->menu_names = NULL; |
2381 | map->menu_mapping = NULL; | |
252d50da | 2382 | |
18a9b21f RR |
2383 | /* For UVCIOC_CTRL_MAP custom control */ |
2384 | if (mapping->name) { | |
2385 | map->name = kstrdup(mapping->name, GFP_KERNEL); | |
252d50da RR |
2386 | if (!map->name) |
2387 | goto err_nomem; | |
18a9b21f RR |
2388 | } |
2389 | ||
b4012002 HG |
2390 | INIT_LIST_HEAD(&map->ev_subs); |
2391 | ||
716c3304 RR |
2392 | if (mapping->menu_mapping && mapping->menu_mask) { |
2393 | size = sizeof(mapping->menu_mapping[0]) | |
2394 | * fls(mapping->menu_mask); | |
2395 | map->menu_mapping = kmemdup(mapping->menu_mapping, size, | |
2396 | GFP_KERNEL); | |
2397 | if (!map->menu_mapping) | |
2398 | goto err_nomem; | |
2399 | } | |
2400 | if (mapping->menu_names && mapping->menu_mask) { | |
2401 | size = sizeof(mapping->menu_names[0]) | |
2402 | * fls(mapping->menu_mask); | |
2403 | map->menu_names = kmemdup(mapping->menu_names, size, | |
2404 | GFP_KERNEL); | |
2405 | if (!map->menu_names) | |
2406 | goto err_nomem; | |
2407 | } | |
c0efd232 | 2408 | |
ba2fa996 LP |
2409 | if (map->get == NULL) |
2410 | map->get = uvc_get_le_value; | |
2411 | if (map->set == NULL) | |
2412 | map->set = uvc_set_le_value; | |
c0efd232 | 2413 | |
9b31ea80 RR |
2414 | for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) { |
2415 | if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) == | |
2416 | V4L2_CTRL_ID2WHICH(map->id)) { | |
2417 | chain->ctrl_class_bitmap |= BIT(i); | |
2418 | break; | |
2419 | } | |
2420 | } | |
2421 | ||
071c8bb8 | 2422 | list_add_tail(&map->list, &ctrl->info.mappings); |
866c6bdd | 2423 | uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n", |
70fa906d RR |
2424 | uvc_map_get_name(map), ctrl->info.entity, |
2425 | ctrl->info.selector); | |
ba2fa996 LP |
2426 | |
2427 | return 0; | |
252d50da RR |
2428 | |
2429 | err_nomem: | |
716c3304 RR |
2430 | kfree(map->menu_names); |
2431 | kfree(map->menu_mapping); | |
252d50da RR |
2432 | kfree(map->name); |
2433 | kfree(map); | |
2434 | return -ENOMEM; | |
c0efd232 LP |
2435 | } |
2436 | ||
ba2fa996 LP |
2437 | int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, |
2438 | const struct uvc_control_mapping *mapping) | |
c0efd232 | 2439 | { |
ba2fa996 | 2440 | struct uvc_device *dev = chain->dev; |
c0efd232 | 2441 | struct uvc_control_mapping *map; |
ba2fa996 LP |
2442 | struct uvc_entity *entity; |
2443 | struct uvc_control *ctrl; | |
2444 | int found = 0; | |
8fb91b33 | 2445 | int ret; |
9768352a | 2446 | |
c0efd232 | 2447 | if (mapping->id & ~V4L2_CTRL_ID_MASK) { |
9e56380a JP |
2448 | uvc_dbg(dev, CONTROL, |
2449 | "Can't add mapping '%s', control id 0x%08x is invalid\n", | |
70fa906d | 2450 | uvc_map_get_name(mapping), mapping->id); |
c0efd232 LP |
2451 | return -EINVAL; |
2452 | } | |
2453 | ||
c0c5e71e SL |
2454 | /* Search for the matching (GUID/CS) control on the current chain */ |
2455 | list_for_each_entry(entity, &chain->entities, chain) { | |
ba2fa996 | 2456 | unsigned int i; |
c0efd232 | 2457 | |
52c58ad6 LP |
2458 | if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT || |
2459 | !uvc_entity_match_guid(entity, mapping->entity)) | |
ba2fa996 | 2460 | continue; |
c0efd232 | 2461 | |
ba2fa996 LP |
2462 | for (i = 0; i < entity->ncontrols; ++i) { |
2463 | ctrl = &entity->controls[i]; | |
52c58ad6 | 2464 | if (ctrl->index == mapping->selector - 1) { |
ba2fa996 LP |
2465 | found = 1; |
2466 | break; | |
c0efd232 LP |
2467 | } |
2468 | } | |
2469 | ||
ba2fa996 LP |
2470 | if (found) |
2471 | break; | |
2472 | } | |
2473 | if (!found) | |
2474 | return -ENOENT; | |
c0efd232 | 2475 | |
ba2fa996 LP |
2476 | if (mutex_lock_interruptible(&chain->ctrl_mutex)) |
2477 | return -ERESTARTSYS; | |
2478 | ||
52c58ad6 LP |
2479 | /* Perform delayed initialization of XU controls */ |
2480 | ret = uvc_ctrl_init_xu_ctrl(dev, ctrl); | |
2481 | if (ret < 0) { | |
2482 | ret = -ENOENT; | |
2483 | goto done; | |
2484 | } | |
2485 | ||
7e09f7d5 GR |
2486 | /* Validate the user-provided bit-size and offset */ |
2487 | if (mapping->size > 32 || | |
2488 | mapping->offset + mapping->size > ctrl->info.size * 8) { | |
2489 | ret = -EINVAL; | |
2490 | goto done; | |
2491 | } | |
2492 | ||
071c8bb8 | 2493 | list_for_each_entry(map, &ctrl->info.mappings, list) { |
ba2fa996 | 2494 | if (mapping->id == map->id) { |
9e56380a JP |
2495 | uvc_dbg(dev, CONTROL, |
2496 | "Can't add mapping '%s', control id 0x%08x already exists\n", | |
70fa906d | 2497 | uvc_map_get_name(mapping), mapping->id); |
ba2fa996 LP |
2498 | ret = -EEXIST; |
2499 | goto done; | |
2500 | } | |
c0efd232 | 2501 | } |
ba2fa996 | 2502 | |
8fb91b33 MR |
2503 | /* Prevent excess memory consumption */ |
2504 | if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) { | |
2505 | atomic_dec(&dev->nmappings); | |
9e56380a JP |
2506 | uvc_dbg(dev, CONTROL, |
2507 | "Can't add mapping '%s', maximum mappings count (%u) exceeded\n", | |
70fa906d | 2508 | uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS); |
8fb91b33 MR |
2509 | ret = -ENOMEM; |
2510 | goto done; | |
2511 | } | |
2512 | ||
866c6bdd | 2513 | ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping); |
8fb91b33 MR |
2514 | if (ret < 0) |
2515 | atomic_dec(&dev->nmappings); | |
2516 | ||
ba2fa996 LP |
2517 | done: |
2518 | mutex_unlock(&chain->ctrl_mutex); | |
c0efd232 LP |
2519 | return ret; |
2520 | } | |
2521 | ||
2bdd29cf | 2522 | /* |
d732c44c LP |
2523 | * Prune an entity of its bogus controls using a blacklist. Bogus controls |
2524 | * are currently the ones that crash the camera or unconditionally return an | |
2525 | * error when queried. | |
2bdd29cf | 2526 | */ |
ba2fa996 LP |
2527 | static void uvc_ctrl_prune_entity(struct uvc_device *dev, |
2528 | struct uvc_entity *entity) | |
2bdd29cf | 2529 | { |
fa34168b | 2530 | struct uvc_ctrl_blacklist { |
d732c44c LP |
2531 | struct usb_device_id id; |
2532 | u8 index; | |
fa34168b LP |
2533 | }; |
2534 | ||
2535 | static const struct uvc_ctrl_blacklist processing_blacklist[] = { | |
9405e3cb | 2536 | { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */ |
d732c44c LP |
2537 | { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */ |
2538 | { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */ | |
2bdd29cf | 2539 | }; |
fa34168b LP |
2540 | static const struct uvc_ctrl_blacklist camera_blacklist[] = { |
2541 | { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */ | |
2542 | }; | |
2bdd29cf | 2543 | |
fa34168b | 2544 | const struct uvc_ctrl_blacklist *blacklist; |
2bdd29cf | 2545 | unsigned int size; |
fa34168b | 2546 | unsigned int count; |
2bdd29cf | 2547 | unsigned int i; |
fa34168b | 2548 | u8 *controls; |
2bdd29cf | 2549 | |
fa34168b LP |
2550 | switch (UVC_ENTITY_TYPE(entity)) { |
2551 | case UVC_VC_PROCESSING_UNIT: | |
2552 | blacklist = processing_blacklist; | |
2553 | count = ARRAY_SIZE(processing_blacklist); | |
2554 | controls = entity->processing.bmControls; | |
2555 | size = entity->processing.bControlSize; | |
2556 | break; | |
2557 | ||
2558 | case UVC_ITT_CAMERA: | |
2559 | blacklist = camera_blacklist; | |
2560 | count = ARRAY_SIZE(camera_blacklist); | |
2561 | controls = entity->camera.bmControls; | |
2562 | size = entity->camera.bControlSize; | |
2563 | break; | |
2bdd29cf | 2564 | |
fa34168b LP |
2565 | default: |
2566 | return; | |
2567 | } | |
2bdd29cf | 2568 | |
fa34168b | 2569 | for (i = 0; i < count; ++i) { |
385097e0 | 2570 | if (!usb_match_one_id(dev->intf, &blacklist[i].id)) |
2bdd29cf LP |
2571 | continue; |
2572 | ||
d732c44c LP |
2573 | if (blacklist[i].index >= 8 * size || |
2574 | !uvc_test_bit(controls, blacklist[i].index)) | |
2bdd29cf LP |
2575 | continue; |
2576 | ||
9e56380a JP |
2577 | uvc_dbg(dev, CONTROL, |
2578 | "%u/%u control is black listed, removing it\n", | |
2579 | entity->id, blacklist[i].index); | |
2bdd29cf | 2580 | |
d732c44c | 2581 | uvc_clear_bit(controls, blacklist[i].index); |
2bdd29cf LP |
2582 | } |
2583 | } | |
2584 | ||
ba2fa996 LP |
2585 | /* |
2586 | * Add control information and hardcoded stock control mappings to the given | |
2587 | * device. | |
2588 | */ | |
866c6bdd RR |
2589 | static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain, |
2590 | struct uvc_control *ctrl) | |
ba2fa996 | 2591 | { |
96a160b0 | 2592 | const struct uvc_control_mapping **mappings; |
c58874df | 2593 | unsigned int i; |
ba2fa996 | 2594 | |
699b9a86 LP |
2595 | /* |
2596 | * XU controls initialization requires querying the device for control | |
52c58ad6 LP |
2597 | * information. As some buggy UVC devices will crash when queried |
2598 | * repeatedly in a tight loop, delay XU controls initialization until | |
2599 | * first use. | |
2600 | */ | |
2601 | if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT) | |
ba2fa996 | 2602 | return; |
ba2fa996 | 2603 | |
c58874df LP |
2604 | for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) { |
2605 | const struct uvc_control_info *info = &uvc_ctrls[i]; | |
2606 | ||
ba2fa996 LP |
2607 | if (uvc_entity_match_guid(ctrl->entity, info->entity) && |
2608 | ctrl->index == info->index) { | |
866c6bdd | 2609 | uvc_ctrl_add_info(chain->dev, ctrl, info); |
93df48d3 HG |
2610 | /* |
2611 | * Retrieve control flags from the device. Ignore errors | |
2612 | * and work with default flag values from the uvc_ctrl | |
2613 | * array when the device doesn't properly implement | |
2614 | * GET_INFO on standard controls. | |
2615 | */ | |
866c6bdd | 2616 | uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info); |
ba2fa996 LP |
2617 | break; |
2618 | } | |
2619 | } | |
2620 | ||
071c8bb8 | 2621 | if (!ctrl->initialized) |
ba2fa996 LP |
2622 | return; |
2623 | ||
86f7ef77 RR |
2624 | /* |
2625 | * First check if the device provides a custom mapping for this control, | |
2626 | * used to override standard mappings for non-conformant devices. Don't | |
2627 | * process standard mappings if a custom mapping is found. This | |
2628 | * mechanism doesn't support combining standard and custom mappings for | |
2629 | * a single control. | |
2630 | */ | |
2631 | if (chain->dev->info->mappings) { | |
2632 | bool custom = false; | |
86f7ef77 | 2633 | |
c58874df LP |
2634 | for (i = 0; chain->dev->info->mappings[i]; ++i) { |
2635 | const struct uvc_control_mapping *mapping = | |
2636 | chain->dev->info->mappings[i]; | |
2637 | ||
86f7ef77 RR |
2638 | if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && |
2639 | ctrl->info.selector == mapping->selector) { | |
2640 | __uvc_ctrl_add_mapping(chain, ctrl, mapping); | |
2641 | custom = true; | |
2642 | } | |
2643 | } | |
2644 | ||
2645 | if (custom) | |
2646 | return; | |
2647 | } | |
2648 | ||
2649 | /* Process common mappings next. */ | |
c58874df LP |
2650 | for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) { |
2651 | const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i]; | |
21b1b679 | 2652 | |
71087116 RR |
2653 | if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && |
2654 | ctrl->info.selector == mapping->selector) | |
2655 | __uvc_ctrl_add_mapping(chain, ctrl, mapping); | |
2656 | } | |
2657 | ||
86f7ef77 | 2658 | /* Finally process version-specific mappings. */ |
96a160b0 RR |
2659 | mappings = chain->dev->uvc_version < 0x0150 |
2660 | ? uvc_ctrl_mappings_uvc11 : uvc_ctrl_mappings_uvc15; | |
71087116 | 2661 | |
96a160b0 RR |
2662 | for (i = 0; mappings[i]; ++i) { |
2663 | const struct uvc_control_mapping *mapping = mappings[i]; | |
c58874df | 2664 | |
ba2fa996 | 2665 | if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && |
071c8bb8 | 2666 | ctrl->info.selector == mapping->selector) |
866c6bdd | 2667 | __uvc_ctrl_add_mapping(chain, ctrl, mapping); |
ba2fa996 LP |
2668 | } |
2669 | } | |
2670 | ||
c0efd232 LP |
2671 | /* |
2672 | * Initialize device controls. | |
2673 | */ | |
866c6bdd | 2674 | static int uvc_ctrl_init_chain(struct uvc_video_chain *chain) |
c0efd232 | 2675 | { |
c0efd232 LP |
2676 | struct uvc_entity *entity; |
2677 | unsigned int i; | |
2678 | ||
2679 | /* Walk the entities list and instantiate controls */ | |
866c6bdd | 2680 | list_for_each_entry(entity, &chain->entities, chain) { |
ba2fa996 | 2681 | struct uvc_control *ctrl; |
9a601396 | 2682 | unsigned int bControlSize = 0, ncontrols; |
2c6b222c | 2683 | u8 *bmControls = NULL; |
c0efd232 | 2684 | |
b482d923 | 2685 | if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) { |
c0efd232 LP |
2686 | bmControls = entity->extension.bmControls; |
2687 | bControlSize = entity->extension.bControlSize; | |
b482d923 | 2688 | } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) { |
c0efd232 LP |
2689 | bmControls = entity->processing.bmControls; |
2690 | bControlSize = entity->processing.bControlSize; | |
b482d923 | 2691 | } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) { |
c0efd232 LP |
2692 | bmControls = entity->camera.bmControls; |
2693 | bControlSize = entity->camera.bControlSize; | |
2886477f RR |
2694 | } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) { |
2695 | bmControls = entity->gpio.bmControls; | |
2696 | bControlSize = entity->gpio.bControlSize; | |
c0efd232 LP |
2697 | } |
2698 | ||
ba2fa996 | 2699 | /* Remove bogus/blacklisted controls */ |
866c6bdd | 2700 | uvc_ctrl_prune_entity(chain->dev, entity); |
2bdd29cf | 2701 | |
ba2fa996 | 2702 | /* Count supported controls and allocate the controls array */ |
9a601396 | 2703 | ncontrols = memweight(bmControls, bControlSize); |
c0efd232 LP |
2704 | if (ncontrols == 0) |
2705 | continue; | |
2706 | ||
d0d97488 | 2707 | entity->controls = kcalloc(ncontrols, sizeof(*ctrl), |
ba2fa996 | 2708 | GFP_KERNEL); |
c0efd232 LP |
2709 | if (entity->controls == NULL) |
2710 | return -ENOMEM; | |
c0efd232 LP |
2711 | entity->ncontrols = ncontrols; |
2712 | ||
ba2fa996 | 2713 | /* Initialize all supported controls */ |
c0efd232 LP |
2714 | ctrl = entity->controls; |
2715 | for (i = 0; i < bControlSize * 8; ++i) { | |
2bdd29cf | 2716 | if (uvc_test_bit(bmControls, i) == 0) |
c0efd232 LP |
2717 | continue; |
2718 | ||
2719 | ctrl->entity = entity; | |
2720 | ctrl->index = i; | |
ba2fa996 | 2721 | |
866c6bdd | 2722 | uvc_ctrl_init_ctrl(chain, ctrl); |
c0efd232 LP |
2723 | ctrl++; |
2724 | } | |
2725 | } | |
2726 | ||
c0efd232 LP |
2727 | return 0; |
2728 | } | |
2729 | ||
866c6bdd RR |
2730 | int uvc_ctrl_init_device(struct uvc_device *dev) |
2731 | { | |
2732 | struct uvc_video_chain *chain; | |
2733 | int ret; | |
2734 | ||
2735 | INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work); | |
2736 | ||
2737 | list_for_each_entry(chain, &dev->chains, list) { | |
2738 | ret = uvc_ctrl_init_chain(chain); | |
2739 | if (ret) | |
2740 | return ret; | |
2741 | } | |
2742 | ||
2743 | return 0; | |
2744 | } | |
2745 | ||
c0efd232 LP |
2746 | /* |
2747 | * Cleanup device controls. | |
2748 | */ | |
ba2fa996 LP |
2749 | static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev, |
2750 | struct uvc_control *ctrl) | |
c0efd232 | 2751 | { |
ba2fa996 | 2752 | struct uvc_control_mapping *mapping, *nm; |
c0efd232 | 2753 | |
071c8bb8 | 2754 | list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) { |
ba2fa996 | 2755 | list_del(&mapping->list); |
716c3304 RR |
2756 | kfree(mapping->menu_names); |
2757 | kfree(mapping->menu_mapping); | |
70fa906d | 2758 | kfree(mapping->name); |
ba2fa996 | 2759 | kfree(mapping); |
c0efd232 LP |
2760 | } |
2761 | } | |
2762 | ||
ba2fa996 | 2763 | void uvc_ctrl_cleanup_device(struct uvc_device *dev) |
561474c2 | 2764 | { |
ba2fa996 LP |
2765 | struct uvc_entity *entity; |
2766 | unsigned int i; | |
561474c2 | 2767 | |
11a087f4 ON |
2768 | /* Can be uninitialized if we are aborting on probe error. */ |
2769 | if (dev->async_ctrl.work.func) | |
2770 | cancel_work_sync(&dev->async_ctrl.work); | |
e5225c82 | 2771 | |
ba2fa996 LP |
2772 | /* Free controls and control mappings for all entities. */ |
2773 | list_for_each_entry(entity, &dev->entities, list) { | |
2774 | for (i = 0; i < entity->ncontrols; ++i) { | |
2775 | struct uvc_control *ctrl = &entity->controls[i]; | |
2776 | ||
071c8bb8 | 2777 | if (!ctrl->initialized) |
ba2fa996 | 2778 | continue; |
561474c2 | 2779 | |
ba2fa996 LP |
2780 | uvc_ctrl_cleanup_mappings(dev, ctrl); |
2781 | kfree(ctrl->uvc_data); | |
561474c2 LP |
2782 | } |
2783 | ||
ba2fa996 | 2784 | kfree(entity->controls); |
561474c2 LP |
2785 | } |
2786 | } |