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