xref: /openbmc/linux/drivers/media/usb/uvc/uvc_ctrl.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
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,const struct v4l2_ext_control * xctrls,unsigned int xctrls_count)1644 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1645 	const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1646 {
1647 	struct uvc_control_mapping *mapping;
1648 	struct uvc_control *ctrl;
1649 	unsigned int i;
1650 	unsigned int j;
1651 
1652 	for (i = 0; i < xctrls_count; ++i) {
1653 		u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1654 
1655 		ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1656 		if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1657 			/* Notification will be sent from an Interrupt event. */
1658 			continue;
1659 
1660 		for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1661 			u32 slave_id = mapping->slave_ids[j];
1662 
1663 			if (!slave_id)
1664 				break;
1665 
1666 			/*
1667 			 * We can skip sending an event for the slave if the
1668 			 * slave is being modified in the same transaction.
1669 			 */
1670 			if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1671 							slave_id))
1672 				continue;
1673 
1674 			uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1675 						  slave_id);
1676 		}
1677 
1678 		/*
1679 		 * If the master is being modified in the same transaction
1680 		 * flags may change too.
1681 		 */
1682 		if (mapping->master_id &&
1683 		    uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1684 						mapping->master_id))
1685 			changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1686 
1687 		uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1688 				    xctrls[i].value, changes);
1689 	}
1690 }
1691 
uvc_ctrl_add_event(struct v4l2_subscribed_event * sev,unsigned elems)1692 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1693 {
1694 	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1695 	struct uvc_control_mapping *mapping;
1696 	struct uvc_control *ctrl;
1697 	int ret;
1698 
1699 	ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1700 	if (ret < 0)
1701 		return -ERESTARTSYS;
1702 
1703 	if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
1704 		ret = 0;
1705 		goto done;
1706 	}
1707 
1708 	ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1709 	if (ctrl == NULL) {
1710 		ret = -EINVAL;
1711 		goto done;
1712 	}
1713 
1714 	list_add_tail(&sev->node, &mapping->ev_subs);
1715 	if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1716 		struct v4l2_event ev;
1717 		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1718 		s32 val = 0;
1719 
1720 		if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1721 			changes |= V4L2_EVENT_CTRL_CH_VALUE;
1722 
1723 		uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1724 				    changes);
1725 		/*
1726 		 * Mark the queue as active, allowing this initial event to be
1727 		 * accepted.
1728 		 */
1729 		sev->elems = elems;
1730 		v4l2_event_queue_fh(sev->fh, &ev);
1731 	}
1732 
1733 done:
1734 	mutex_unlock(&handle->chain->ctrl_mutex);
1735 	return ret;
1736 }
1737 
uvc_ctrl_del_event(struct v4l2_subscribed_event * sev)1738 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1739 {
1740 	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1741 
1742 	mutex_lock(&handle->chain->ctrl_mutex);
1743 	if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
1744 		goto done;
1745 	list_del(&sev->node);
1746 done:
1747 	mutex_unlock(&handle->chain->ctrl_mutex);
1748 }
1749 
1750 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1751 	.add = uvc_ctrl_add_event,
1752 	.del = uvc_ctrl_del_event,
1753 	.replace = v4l2_ctrl_replace,
1754 	.merge = v4l2_ctrl_merge,
1755 };
1756 
1757 /* --------------------------------------------------------------------------
1758  * Control transactions
1759  *
1760  * To make extended set operations as atomic as the hardware allows, controls
1761  * are handled using begin/commit/rollback operations.
1762  *
1763  * At the beginning of a set request, uvc_ctrl_begin should be called to
1764  * initialize the request. This function acquires the control lock.
1765  *
1766  * When setting a control, the new value is stored in the control data field
1767  * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1768  * later processing. If the UVC and V4L2 control sizes differ, the current
1769  * value is loaded from the hardware before storing the new value in the data
1770  * field.
1771  *
1772  * After processing all controls in the transaction, uvc_ctrl_commit or
1773  * uvc_ctrl_rollback must be called to apply the pending changes to the
1774  * hardware or revert them. When applying changes, all controls marked as
1775  * dirty will be modified in the UVC device, and the dirty flag will be
1776  * cleared. When reverting controls, the control data field
1777  * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1778  * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1779  * control lock.
1780  */
uvc_ctrl_begin(struct uvc_video_chain * chain)1781 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1782 {
1783 	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1784 }
1785 
uvc_ctrl_commit_entity(struct uvc_device * dev,struct uvc_fh * handle,struct uvc_entity * entity,int rollback,struct uvc_control ** err_ctrl)1786 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1787 				  struct uvc_fh *handle,
1788 				  struct uvc_entity *entity,
1789 				  int rollback,
1790 				  struct uvc_control **err_ctrl)
1791 {
1792 	struct uvc_control *ctrl;
1793 	unsigned int i;
1794 	int ret;
1795 
1796 	if (entity == NULL)
1797 		return 0;
1798 
1799 	for (i = 0; i < entity->ncontrols; ++i) {
1800 		ctrl = &entity->controls[i];
1801 		if (!ctrl->initialized)
1802 			continue;
1803 
1804 		/*
1805 		 * Reset the loaded flag for auto-update controls that were
1806 		 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1807 		 * uvc_ctrl_get from using the cached value, and for write-only
1808 		 * controls to prevent uvc_ctrl_set from setting bits not
1809 		 * explicitly set by the user.
1810 		 */
1811 		if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1812 		    !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1813 			ctrl->loaded = 0;
1814 
1815 		if (!ctrl->dirty)
1816 			continue;
1817 
1818 		if (!rollback)
1819 			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1820 				dev->intfnum, ctrl->info.selector,
1821 				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1822 				ctrl->info.size);
1823 		else
1824 			ret = 0;
1825 
1826 		if (rollback || ret < 0)
1827 			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1828 			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1829 			       ctrl->info.size);
1830 
1831 		ctrl->dirty = 0;
1832 
1833 		if (ret < 0) {
1834 			if (err_ctrl)
1835 				*err_ctrl = ctrl;
1836 			return ret;
1837 		}
1838 
1839 		if (!rollback && handle &&
1840 		    ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1841 			uvc_ctrl_set_handle(handle, ctrl, handle);
1842 	}
1843 
1844 	return 0;
1845 }
1846 
uvc_ctrl_find_ctrl_idx(struct uvc_entity * entity,struct v4l2_ext_controls * ctrls,struct uvc_control * uvc_control)1847 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
1848 				  struct v4l2_ext_controls *ctrls,
1849 				  struct uvc_control *uvc_control)
1850 {
1851 	struct uvc_control_mapping *mapping = NULL;
1852 	struct uvc_control *ctrl_found = NULL;
1853 	unsigned int i;
1854 
1855 	if (!entity)
1856 		return ctrls->count;
1857 
1858 	for (i = 0; i < ctrls->count; i++) {
1859 		__uvc_find_control(entity, ctrls->controls[i].id, &mapping,
1860 				   &ctrl_found, 0);
1861 		if (uvc_control == ctrl_found)
1862 			return i;
1863 	}
1864 
1865 	return ctrls->count;
1866 }
1867 
__uvc_ctrl_commit(struct uvc_fh * handle,int rollback,struct v4l2_ext_controls * ctrls)1868 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1869 		      struct v4l2_ext_controls *ctrls)
1870 {
1871 	struct uvc_video_chain *chain = handle->chain;
1872 	struct uvc_control *err_ctrl;
1873 	struct uvc_entity *entity;
1874 	int ret = 0;
1875 
1876 	/* Find the control. */
1877 	list_for_each_entry(entity, &chain->entities, chain) {
1878 		ret = uvc_ctrl_commit_entity(chain->dev, handle, entity,
1879 					     rollback, &err_ctrl);
1880 		if (ret < 0) {
1881 			if (ctrls)
1882 				ctrls->error_idx =
1883 					uvc_ctrl_find_ctrl_idx(entity, ctrls,
1884 							       err_ctrl);
1885 			goto done;
1886 		}
1887 	}
1888 
1889 	if (!rollback)
1890 		uvc_ctrl_send_events(handle, ctrls->controls, ctrls->count);
1891 done:
1892 	mutex_unlock(&chain->ctrl_mutex);
1893 	return ret;
1894 }
1895 
uvc_ctrl_get(struct uvc_video_chain * chain,struct v4l2_ext_control * xctrl)1896 int uvc_ctrl_get(struct uvc_video_chain *chain,
1897 	struct v4l2_ext_control *xctrl)
1898 {
1899 	struct uvc_control *ctrl;
1900 	struct uvc_control_mapping *mapping;
1901 
1902 	if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1903 		return -EACCES;
1904 
1905 	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1906 	if (ctrl == NULL)
1907 		return -EINVAL;
1908 
1909 	return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1910 }
1911 
uvc_ctrl_set(struct uvc_fh * handle,struct v4l2_ext_control * xctrl)1912 int uvc_ctrl_set(struct uvc_fh *handle,
1913 	struct v4l2_ext_control *xctrl)
1914 {
1915 	struct uvc_video_chain *chain = handle->chain;
1916 	struct uvc_control *ctrl;
1917 	struct uvc_control_mapping *mapping;
1918 	s32 value;
1919 	u32 step;
1920 	s32 min;
1921 	s32 max;
1922 	int ret;
1923 
1924 	if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
1925 		return -EACCES;
1926 
1927 	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1928 	if (ctrl == NULL)
1929 		return -EINVAL;
1930 	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1931 		return -EACCES;
1932 
1933 	/* Clamp out of range values. */
1934 	switch (mapping->v4l2_type) {
1935 	case V4L2_CTRL_TYPE_INTEGER:
1936 		if (!ctrl->cached) {
1937 			ret = uvc_ctrl_populate_cache(chain, ctrl);
1938 			if (ret < 0)
1939 				return ret;
1940 		}
1941 
1942 		min = mapping->get(mapping, UVC_GET_MIN,
1943 				   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1944 		max = mapping->get(mapping, UVC_GET_MAX,
1945 				   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1946 		step = mapping->get(mapping, UVC_GET_RES,
1947 				    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1948 		if (step == 0)
1949 			step = 1;
1950 
1951 		xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
1952 							step) * step;
1953 		if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1954 			xctrl->value = clamp(xctrl->value, min, max);
1955 		else
1956 			xctrl->value = clamp_t(u32, xctrl->value, min, max);
1957 		value = xctrl->value;
1958 		break;
1959 
1960 	case V4L2_CTRL_TYPE_BITMASK:
1961 		if (!ctrl->cached) {
1962 			ret = uvc_ctrl_populate_cache(chain, ctrl);
1963 			if (ret < 0)
1964 				return ret;
1965 		}
1966 
1967 		xctrl->value &= uvc_get_ctrl_bitmap(ctrl, mapping);
1968 		value = xctrl->value;
1969 		break;
1970 
1971 	case V4L2_CTRL_TYPE_BOOLEAN:
1972 		xctrl->value = clamp(xctrl->value, 0, 1);
1973 		value = xctrl->value;
1974 		break;
1975 
1976 	case V4L2_CTRL_TYPE_MENU:
1977 		if (xctrl->value < (ffs(mapping->menu_mask) - 1) ||
1978 		    xctrl->value > (fls(mapping->menu_mask) - 1))
1979 			return -ERANGE;
1980 
1981 		if (!test_bit(xctrl->value, &mapping->menu_mask))
1982 			return -EINVAL;
1983 
1984 		value = uvc_mapping_get_menu_value(mapping, xctrl->value);
1985 
1986 		/*
1987 		 * Valid menu indices are reported by the GET_RES request for
1988 		 * UVC controls that support it.
1989 		 */
1990 		if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
1991 			if (!ctrl->cached) {
1992 				ret = uvc_ctrl_populate_cache(chain, ctrl);
1993 				if (ret < 0)
1994 					return ret;
1995 			}
1996 
1997 			if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & value))
1998 				return -EINVAL;
1999 		}
2000 
2001 		break;
2002 
2003 	default:
2004 		value = xctrl->value;
2005 		break;
2006 	}
2007 
2008 	/*
2009 	 * If the mapping doesn't span the whole UVC control, the current value
2010 	 * needs to be loaded from the device to perform the read-modify-write
2011 	 * operation.
2012 	 */
2013 	if ((ctrl->info.size * 8) != mapping->size) {
2014 		ret = __uvc_ctrl_load_cur(chain, ctrl);
2015 		if (ret < 0)
2016 			return ret;
2017 	}
2018 
2019 	/* Backup the current value in case we need to rollback later. */
2020 	if (!ctrl->dirty) {
2021 		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2022 		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2023 		       ctrl->info.size);
2024 	}
2025 
2026 	mapping->set(mapping, value,
2027 		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2028 
2029 	ctrl->dirty = 1;
2030 	ctrl->modified = 1;
2031 	return 0;
2032 }
2033 
2034 /* --------------------------------------------------------------------------
2035  * Dynamic controls
2036  */
2037 
2038 /*
2039  * Retrieve flags for a given control
2040  */
uvc_ctrl_get_flags(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2041 static int uvc_ctrl_get_flags(struct uvc_device *dev,
2042 			      const struct uvc_control *ctrl,
2043 			      struct uvc_control_info *info)
2044 {
2045 	u8 *data;
2046 	int ret;
2047 
2048 	data = kmalloc(1, GFP_KERNEL);
2049 	if (data == NULL)
2050 		return -ENOMEM;
2051 
2052 	if (ctrl->entity->get_info)
2053 		ret = ctrl->entity->get_info(dev, ctrl->entity,
2054 					     ctrl->info.selector, data);
2055 	else
2056 		ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
2057 				     dev->intfnum, info->selector, data, 1);
2058 
2059 	if (!ret) {
2060 		info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
2061 				 UVC_CTRL_FLAG_SET_CUR |
2062 				 UVC_CTRL_FLAG_AUTO_UPDATE |
2063 				 UVC_CTRL_FLAG_ASYNCHRONOUS);
2064 
2065 		info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
2066 				UVC_CTRL_FLAG_GET_CUR : 0)
2067 			    |  (data[0] & UVC_CONTROL_CAP_SET ?
2068 				UVC_CTRL_FLAG_SET_CUR : 0)
2069 			    |  (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
2070 				UVC_CTRL_FLAG_AUTO_UPDATE : 0)
2071 			    |  (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
2072 				UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
2073 	}
2074 
2075 	kfree(data);
2076 	return ret;
2077 }
2078 
uvc_ctrl_fixup_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2079 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
2080 	const struct uvc_control *ctrl, struct uvc_control_info *info)
2081 {
2082 	struct uvc_ctrl_fixup {
2083 		struct usb_device_id id;
2084 		u8 entity;
2085 		u8 selector;
2086 		u8 flags;
2087 	};
2088 
2089 	static const struct uvc_ctrl_fixup fixups[] = {
2090 		{ { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2091 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2092 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2093 			UVC_CTRL_FLAG_AUTO_UPDATE },
2094 		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2095 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2096 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2097 			UVC_CTRL_FLAG_AUTO_UPDATE },
2098 		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2099 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2100 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2101 			UVC_CTRL_FLAG_AUTO_UPDATE },
2102 	};
2103 
2104 	unsigned int i;
2105 
2106 	for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
2107 		if (!usb_match_one_id(dev->intf, &fixups[i].id))
2108 			continue;
2109 
2110 		if (fixups[i].entity == ctrl->entity->id &&
2111 		    fixups[i].selector == info->selector) {
2112 			info->flags = fixups[i].flags;
2113 			return;
2114 		}
2115 	}
2116 }
2117 
2118 /*
2119  * Query control information (size and flags) for XU controls.
2120  */
uvc_ctrl_fill_xu_info(struct uvc_device * dev,const struct uvc_control * ctrl,struct uvc_control_info * info)2121 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
2122 	const struct uvc_control *ctrl, struct uvc_control_info *info)
2123 {
2124 	u8 *data;
2125 	int ret;
2126 
2127 	data = kmalloc(2, GFP_KERNEL);
2128 	if (data == NULL)
2129 		return -ENOMEM;
2130 
2131 	memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
2132 	info->index = ctrl->index;
2133 	info->selector = ctrl->index + 1;
2134 
2135 	/* Query and verify the control length (GET_LEN) */
2136 	ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
2137 			     info->selector, data, 2);
2138 	if (ret < 0) {
2139 		uvc_dbg(dev, CONTROL,
2140 			"GET_LEN failed on control %pUl/%u (%d)\n",
2141 			info->entity, info->selector, ret);
2142 		goto done;
2143 	}
2144 
2145 	info->size = le16_to_cpup((__le16 *)data);
2146 
2147 	info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
2148 		    | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
2149 
2150 	ret = uvc_ctrl_get_flags(dev, ctrl, info);
2151 	if (ret < 0) {
2152 		uvc_dbg(dev, CONTROL,
2153 			"Failed to get flags for control %pUl/%u (%d)\n",
2154 			info->entity, info->selector, ret);
2155 		goto done;
2156 	}
2157 
2158 	uvc_ctrl_fixup_xu_info(dev, ctrl, info);
2159 
2160 	uvc_dbg(dev, CONTROL,
2161 		"XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2162 		info->entity, info->selector, info->size,
2163 		(info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
2164 		(info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
2165 		(info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
2166 
2167 done:
2168 	kfree(data);
2169 	return ret;
2170 }
2171 
2172 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2173 	const struct uvc_control_info *info);
2174 
uvc_ctrl_init_xu_ctrl(struct uvc_device * dev,struct uvc_control * ctrl)2175 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
2176 	struct uvc_control *ctrl)
2177 {
2178 	struct uvc_control_info info;
2179 	int ret;
2180 
2181 	if (ctrl->initialized)
2182 		return 0;
2183 
2184 	ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2185 	if (ret < 0)
2186 		return ret;
2187 
2188 	ret = uvc_ctrl_add_info(dev, ctrl, &info);
2189 	if (ret < 0)
2190 		uvc_dbg(dev, CONTROL,
2191 			"Failed to initialize control %pUl/%u on device %s entity %u\n",
2192 			info.entity, info.selector, dev->udev->devpath,
2193 			ctrl->entity->id);
2194 
2195 	return ret;
2196 }
2197 
uvc_xu_ctrl_query(struct uvc_video_chain * chain,struct uvc_xu_control_query * xqry)2198 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2199 	struct uvc_xu_control_query *xqry)
2200 {
2201 	struct uvc_entity *entity, *iter;
2202 	struct uvc_control *ctrl;
2203 	unsigned int i;
2204 	bool found;
2205 	u32 reqflags;
2206 	u16 size;
2207 	u8 *data = NULL;
2208 	int ret;
2209 
2210 	/* Find the extension unit. */
2211 	entity = NULL;
2212 	list_for_each_entry(iter, &chain->entities, chain) {
2213 		if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
2214 		    iter->id == xqry->unit) {
2215 			entity = iter;
2216 			break;
2217 		}
2218 	}
2219 
2220 	if (!entity) {
2221 		uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2222 			xqry->unit);
2223 		return -ENOENT;
2224 	}
2225 
2226 	/* Find the control and perform delayed initialization if needed. */
2227 	found = false;
2228 	for (i = 0; i < entity->ncontrols; ++i) {
2229 		ctrl = &entity->controls[i];
2230 		if (ctrl->index == xqry->selector - 1) {
2231 			found = true;
2232 			break;
2233 		}
2234 	}
2235 
2236 	if (!found) {
2237 		uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2238 			entity->guid, xqry->selector);
2239 		return -ENOENT;
2240 	}
2241 
2242 	if (mutex_lock_interruptible(&chain->ctrl_mutex))
2243 		return -ERESTARTSYS;
2244 
2245 	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2246 	if (ret < 0) {
2247 		ret = -ENOENT;
2248 		goto done;
2249 	}
2250 
2251 	/* Validate the required buffer size and flags for the request */
2252 	reqflags = 0;
2253 	size = ctrl->info.size;
2254 
2255 	switch (xqry->query) {
2256 	case UVC_GET_CUR:
2257 		reqflags = UVC_CTRL_FLAG_GET_CUR;
2258 		break;
2259 	case UVC_GET_MIN:
2260 		reqflags = UVC_CTRL_FLAG_GET_MIN;
2261 		break;
2262 	case UVC_GET_MAX:
2263 		reqflags = UVC_CTRL_FLAG_GET_MAX;
2264 		break;
2265 	case UVC_GET_DEF:
2266 		reqflags = UVC_CTRL_FLAG_GET_DEF;
2267 		break;
2268 	case UVC_GET_RES:
2269 		reqflags = UVC_CTRL_FLAG_GET_RES;
2270 		break;
2271 	case UVC_SET_CUR:
2272 		reqflags = UVC_CTRL_FLAG_SET_CUR;
2273 		break;
2274 	case UVC_GET_LEN:
2275 		size = 2;
2276 		break;
2277 	case UVC_GET_INFO:
2278 		size = 1;
2279 		break;
2280 	default:
2281 		ret = -EINVAL;
2282 		goto done;
2283 	}
2284 
2285 	if (size != xqry->size) {
2286 		ret = -ENOBUFS;
2287 		goto done;
2288 	}
2289 
2290 	if (reqflags && !(ctrl->info.flags & reqflags)) {
2291 		ret = -EBADRQC;
2292 		goto done;
2293 	}
2294 
2295 	data = kmalloc(size, GFP_KERNEL);
2296 	if (data == NULL) {
2297 		ret = -ENOMEM;
2298 		goto done;
2299 	}
2300 
2301 	if (xqry->query == UVC_SET_CUR &&
2302 	    copy_from_user(data, xqry->data, size)) {
2303 		ret = -EFAULT;
2304 		goto done;
2305 	}
2306 
2307 	ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2308 			     chain->dev->intfnum, xqry->selector, data, size);
2309 	if (ret < 0)
2310 		goto done;
2311 
2312 	if (xqry->query != UVC_SET_CUR &&
2313 	    copy_to_user(xqry->data, data, size))
2314 		ret = -EFAULT;
2315 done:
2316 	kfree(data);
2317 	mutex_unlock(&chain->ctrl_mutex);
2318 	return ret;
2319 }
2320 
2321 /* --------------------------------------------------------------------------
2322  * Suspend/resume
2323  */
2324 
2325 /*
2326  * Restore control values after resume, skipping controls that haven't been
2327  * changed.
2328  *
2329  * TODO
2330  * - Don't restore modified controls that are back to their default value.
2331  * - Handle restore order (Auto-Exposure Mode should be restored before
2332  *   Exposure Time).
2333  */
uvc_ctrl_restore_values(struct uvc_device * dev)2334 int uvc_ctrl_restore_values(struct uvc_device *dev)
2335 {
2336 	struct uvc_control *ctrl;
2337 	struct uvc_entity *entity;
2338 	unsigned int i;
2339 	int ret;
2340 
2341 	/* Walk the entities list and restore controls when possible. */
2342 	list_for_each_entry(entity, &dev->entities, list) {
2343 
2344 		for (i = 0; i < entity->ncontrols; ++i) {
2345 			ctrl = &entity->controls[i];
2346 
2347 			if (!ctrl->initialized || !ctrl->modified ||
2348 			    (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2349 				continue;
2350 			dev_dbg(&dev->udev->dev,
2351 				"restoring control %pUl/%u/%u\n",
2352 				ctrl->info.entity, ctrl->info.index,
2353 				ctrl->info.selector);
2354 			ctrl->dirty = 1;
2355 		}
2356 
2357 		ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
2358 		if (ret < 0)
2359 			return ret;
2360 	}
2361 
2362 	return 0;
2363 }
2364 
2365 /* --------------------------------------------------------------------------
2366  * Control and mapping handling
2367  */
2368 
2369 /*
2370  * Add control information to a given control.
2371  */
uvc_ctrl_add_info(struct uvc_device * dev,struct uvc_control * ctrl,const struct uvc_control_info * info)2372 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2373 	const struct uvc_control_info *info)
2374 {
2375 	ctrl->info = *info;
2376 	INIT_LIST_HEAD(&ctrl->info.mappings);
2377 
2378 	/* Allocate an array to save control values (cur, def, max, etc.) */
2379 	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2380 				 GFP_KERNEL);
2381 	if (!ctrl->uvc_data)
2382 		return -ENOMEM;
2383 
2384 	ctrl->initialized = 1;
2385 
2386 	uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2387 		ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2388 		ctrl->entity->id);
2389 
2390 	return 0;
2391 }
2392 
2393 /*
2394  * Add a control mapping to a given control.
2395  */
__uvc_ctrl_add_mapping(struct uvc_video_chain * chain,struct uvc_control * ctrl,const struct uvc_control_mapping * mapping)2396 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2397 	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2398 {
2399 	struct uvc_control_mapping *map;
2400 	unsigned int size;
2401 	unsigned int i;
2402 
2403 	/*
2404 	 * Most mappings come from static kernel data, and need to be duplicated.
2405 	 * Mappings that come from userspace will be unnecessarily duplicated,
2406 	 * this could be optimized.
2407 	 */
2408 	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2409 	if (!map)
2410 		return -ENOMEM;
2411 
2412 	map->name = NULL;
2413 	map->menu_names = NULL;
2414 	map->menu_mapping = NULL;
2415 
2416 	/* For UVCIOC_CTRL_MAP custom control */
2417 	if (mapping->name) {
2418 		map->name = kstrdup(mapping->name, GFP_KERNEL);
2419 		if (!map->name)
2420 			goto err_nomem;
2421 	}
2422 
2423 	INIT_LIST_HEAD(&map->ev_subs);
2424 
2425 	if (mapping->menu_mapping && mapping->menu_mask) {
2426 		size = sizeof(mapping->menu_mapping[0])
2427 		       * fls(mapping->menu_mask);
2428 		map->menu_mapping = kmemdup(mapping->menu_mapping, size,
2429 					    GFP_KERNEL);
2430 		if (!map->menu_mapping)
2431 			goto err_nomem;
2432 	}
2433 	if (mapping->menu_names && mapping->menu_mask) {
2434 		size = sizeof(mapping->menu_names[0])
2435 		       * fls(mapping->menu_mask);
2436 		map->menu_names = kmemdup(mapping->menu_names, size,
2437 					  GFP_KERNEL);
2438 		if (!map->menu_names)
2439 			goto err_nomem;
2440 	}
2441 
2442 	if (map->get == NULL)
2443 		map->get = uvc_get_le_value;
2444 	if (map->set == NULL)
2445 		map->set = uvc_set_le_value;
2446 
2447 	for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
2448 		if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
2449 						V4L2_CTRL_ID2WHICH(map->id)) {
2450 			chain->ctrl_class_bitmap |= BIT(i);
2451 			break;
2452 		}
2453 	}
2454 
2455 	list_add_tail(&map->list, &ctrl->info.mappings);
2456 	uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
2457 		uvc_map_get_name(map), ctrl->info.entity,
2458 		ctrl->info.selector);
2459 
2460 	return 0;
2461 
2462 err_nomem:
2463 	kfree(map->menu_names);
2464 	kfree(map->menu_mapping);
2465 	kfree(map->name);
2466 	kfree(map);
2467 	return -ENOMEM;
2468 }
2469 
uvc_ctrl_add_mapping(struct uvc_video_chain * chain,const struct uvc_control_mapping * mapping)2470 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2471 	const struct uvc_control_mapping *mapping)
2472 {
2473 	struct uvc_device *dev = chain->dev;
2474 	struct uvc_control_mapping *map;
2475 	struct uvc_entity *entity;
2476 	struct uvc_control *ctrl;
2477 	int found = 0;
2478 	int ret;
2479 
2480 	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2481 		uvc_dbg(dev, CONTROL,
2482 			"Can't add mapping '%s', control id 0x%08x is invalid\n",
2483 			uvc_map_get_name(mapping), mapping->id);
2484 		return -EINVAL;
2485 	}
2486 
2487 	/* Search for the matching (GUID/CS) control on the current chain */
2488 	list_for_each_entry(entity, &chain->entities, chain) {
2489 		unsigned int i;
2490 
2491 		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2492 		    !uvc_entity_match_guid(entity, mapping->entity))
2493 			continue;
2494 
2495 		for (i = 0; i < entity->ncontrols; ++i) {
2496 			ctrl = &entity->controls[i];
2497 			if (ctrl->index == mapping->selector - 1) {
2498 				found = 1;
2499 				break;
2500 			}
2501 		}
2502 
2503 		if (found)
2504 			break;
2505 	}
2506 	if (!found)
2507 		return -ENOENT;
2508 
2509 	if (mutex_lock_interruptible(&chain->ctrl_mutex))
2510 		return -ERESTARTSYS;
2511 
2512 	/* Perform delayed initialization of XU controls */
2513 	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2514 	if (ret < 0) {
2515 		ret = -ENOENT;
2516 		goto done;
2517 	}
2518 
2519 	/* Validate the user-provided bit-size and offset */
2520 	if (mapping->size > 32 ||
2521 	    mapping->offset + mapping->size > ctrl->info.size * 8) {
2522 		ret = -EINVAL;
2523 		goto done;
2524 	}
2525 
2526 	list_for_each_entry(map, &ctrl->info.mappings, list) {
2527 		if (mapping->id == map->id) {
2528 			uvc_dbg(dev, CONTROL,
2529 				"Can't add mapping '%s', control id 0x%08x already exists\n",
2530 				uvc_map_get_name(mapping), mapping->id);
2531 			ret = -EEXIST;
2532 			goto done;
2533 		}
2534 	}
2535 
2536 	/* Prevent excess memory consumption */
2537 	if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2538 		atomic_dec(&dev->nmappings);
2539 		uvc_dbg(dev, CONTROL,
2540 			"Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
2541 			uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
2542 		ret = -ENOMEM;
2543 		goto done;
2544 	}
2545 
2546 	ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
2547 	if (ret < 0)
2548 		atomic_dec(&dev->nmappings);
2549 
2550 done:
2551 	mutex_unlock(&chain->ctrl_mutex);
2552 	return ret;
2553 }
2554 
2555 /*
2556  * Prune an entity of its bogus controls using a blacklist. Bogus controls
2557  * are currently the ones that crash the camera or unconditionally return an
2558  * error when queried.
2559  */
uvc_ctrl_prune_entity(struct uvc_device * dev,struct uvc_entity * entity)2560 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2561 	struct uvc_entity *entity)
2562 {
2563 	struct uvc_ctrl_blacklist {
2564 		struct usb_device_id id;
2565 		u8 index;
2566 	};
2567 
2568 	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2569 		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2570 		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2571 		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2572 	};
2573 	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2574 		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2575 	};
2576 
2577 	const struct uvc_ctrl_blacklist *blacklist;
2578 	unsigned int size;
2579 	unsigned int count;
2580 	unsigned int i;
2581 	u8 *controls;
2582 
2583 	switch (UVC_ENTITY_TYPE(entity)) {
2584 	case UVC_VC_PROCESSING_UNIT:
2585 		blacklist = processing_blacklist;
2586 		count = ARRAY_SIZE(processing_blacklist);
2587 		controls = entity->processing.bmControls;
2588 		size = entity->processing.bControlSize;
2589 		break;
2590 
2591 	case UVC_ITT_CAMERA:
2592 		blacklist = camera_blacklist;
2593 		count = ARRAY_SIZE(camera_blacklist);
2594 		controls = entity->camera.bmControls;
2595 		size = entity->camera.bControlSize;
2596 		break;
2597 
2598 	default:
2599 		return;
2600 	}
2601 
2602 	for (i = 0; i < count; ++i) {
2603 		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2604 			continue;
2605 
2606 		if (blacklist[i].index >= 8 * size ||
2607 		    !uvc_test_bit(controls, blacklist[i].index))
2608 			continue;
2609 
2610 		uvc_dbg(dev, CONTROL,
2611 			"%u/%u control is black listed, removing it\n",
2612 			entity->id, blacklist[i].index);
2613 
2614 		uvc_clear_bit(controls, blacklist[i].index);
2615 	}
2616 }
2617 
2618 /*
2619  * Add control information and hardcoded stock control mappings to the given
2620  * device.
2621  */
uvc_ctrl_init_ctrl(struct uvc_video_chain * chain,struct uvc_control * ctrl)2622 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
2623 			       struct uvc_control *ctrl)
2624 {
2625 	const struct uvc_control_mapping **mappings;
2626 	unsigned int i;
2627 
2628 	/*
2629 	 * XU controls initialization requires querying the device for control
2630 	 * information. As some buggy UVC devices will crash when queried
2631 	 * repeatedly in a tight loop, delay XU controls initialization until
2632 	 * first use.
2633 	 */
2634 	if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2635 		return;
2636 
2637 	for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
2638 		const struct uvc_control_info *info = &uvc_ctrls[i];
2639 
2640 		if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2641 		    ctrl->index == info->index) {
2642 			uvc_ctrl_add_info(chain->dev, ctrl, info);
2643 			/*
2644 			 * Retrieve control flags from the device. Ignore errors
2645 			 * and work with default flag values from the uvc_ctrl
2646 			 * array when the device doesn't properly implement
2647 			 * GET_INFO on standard controls.
2648 			 */
2649 			uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
2650 			break;
2651 		 }
2652 	}
2653 
2654 	if (!ctrl->initialized)
2655 		return;
2656 
2657 	/*
2658 	 * First check if the device provides a custom mapping for this control,
2659 	 * used to override standard mappings for non-conformant devices. Don't
2660 	 * process standard mappings if a custom mapping is found. This
2661 	 * mechanism doesn't support combining standard and custom mappings for
2662 	 * a single control.
2663 	 */
2664 	if (chain->dev->info->mappings) {
2665 		bool custom = false;
2666 
2667 		for (i = 0; chain->dev->info->mappings[i]; ++i) {
2668 			const struct uvc_control_mapping *mapping =
2669 				chain->dev->info->mappings[i];
2670 
2671 			if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2672 			    ctrl->info.selector == mapping->selector) {
2673 				__uvc_ctrl_add_mapping(chain, ctrl, mapping);
2674 				custom = true;
2675 			}
2676 		}
2677 
2678 		if (custom)
2679 			return;
2680 	}
2681 
2682 	/* Process common mappings next. */
2683 	for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
2684 		const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
2685 
2686 		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2687 		    ctrl->info.selector == mapping->selector)
2688 			__uvc_ctrl_add_mapping(chain, ctrl, mapping);
2689 	}
2690 
2691 	/* Finally process version-specific mappings. */
2692 	mappings = chain->dev->uvc_version < 0x0150
2693 		 ? uvc_ctrl_mappings_uvc11 : uvc_ctrl_mappings_uvc15;
2694 
2695 	for (i = 0; mappings[i]; ++i) {
2696 		const struct uvc_control_mapping *mapping = mappings[i];
2697 
2698 		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2699 		    ctrl->info.selector == mapping->selector)
2700 			__uvc_ctrl_add_mapping(chain, ctrl, mapping);
2701 	}
2702 }
2703 
2704 /*
2705  * Initialize device controls.
2706  */
uvc_ctrl_init_chain(struct uvc_video_chain * chain)2707 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
2708 {
2709 	struct uvc_entity *entity;
2710 	unsigned int i;
2711 
2712 	/* Walk the entities list and instantiate controls */
2713 	list_for_each_entry(entity, &chain->entities, chain) {
2714 		struct uvc_control *ctrl;
2715 		unsigned int bControlSize = 0, ncontrols;
2716 		u8 *bmControls = NULL;
2717 
2718 		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2719 			bmControls = entity->extension.bmControls;
2720 			bControlSize = entity->extension.bControlSize;
2721 		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2722 			bmControls = entity->processing.bmControls;
2723 			bControlSize = entity->processing.bControlSize;
2724 		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2725 			bmControls = entity->camera.bmControls;
2726 			bControlSize = entity->camera.bControlSize;
2727 		} else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
2728 			bmControls = entity->gpio.bmControls;
2729 			bControlSize = entity->gpio.bControlSize;
2730 		}
2731 
2732 		/* Remove bogus/blacklisted controls */
2733 		uvc_ctrl_prune_entity(chain->dev, entity);
2734 
2735 		/* Count supported controls and allocate the controls array */
2736 		ncontrols = memweight(bmControls, bControlSize);
2737 		if (ncontrols == 0)
2738 			continue;
2739 
2740 		entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2741 					   GFP_KERNEL);
2742 		if (entity->controls == NULL)
2743 			return -ENOMEM;
2744 		entity->ncontrols = ncontrols;
2745 
2746 		/* Initialize all supported controls */
2747 		ctrl = entity->controls;
2748 		for (i = 0; i < bControlSize * 8; ++i) {
2749 			if (uvc_test_bit(bmControls, i) == 0)
2750 				continue;
2751 
2752 			ctrl->entity = entity;
2753 			ctrl->index = i;
2754 
2755 			uvc_ctrl_init_ctrl(chain, ctrl);
2756 			ctrl++;
2757 		}
2758 	}
2759 
2760 	return 0;
2761 }
2762 
uvc_ctrl_init_device(struct uvc_device * dev)2763 int uvc_ctrl_init_device(struct uvc_device *dev)
2764 {
2765 	struct uvc_video_chain *chain;
2766 	int ret;
2767 
2768 	INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2769 
2770 	list_for_each_entry(chain, &dev->chains, list) {
2771 		ret = uvc_ctrl_init_chain(chain);
2772 		if (ret)
2773 			return ret;
2774 	}
2775 
2776 	return 0;
2777 }
2778 
uvc_ctrl_cleanup_fh(struct uvc_fh * handle)2779 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
2780 {
2781 	struct uvc_entity *entity;
2782 
2783 	guard(mutex)(&handle->chain->ctrl_mutex);
2784 
2785 	if (!handle->pending_async_ctrls)
2786 		return;
2787 
2788 	list_for_each_entry(entity, &handle->chain->dev->entities, list) {
2789 		for (unsigned int i = 0; i < entity->ncontrols; ++i) {
2790 			if (entity->controls[i].handle != handle)
2791 				continue;
2792 			uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
2793 		}
2794 	}
2795 
2796 	WARN_ON(handle->pending_async_ctrls);
2797 }
2798 
2799 /*
2800  * Cleanup device controls.
2801  */
uvc_ctrl_cleanup_mappings(struct uvc_device * dev,struct uvc_control * ctrl)2802 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2803 	struct uvc_control *ctrl)
2804 {
2805 	struct uvc_control_mapping *mapping, *nm;
2806 
2807 	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2808 		list_del(&mapping->list);
2809 		kfree(mapping->menu_names);
2810 		kfree(mapping->menu_mapping);
2811 		kfree(mapping->name);
2812 		kfree(mapping);
2813 	}
2814 }
2815 
uvc_ctrl_cleanup_device(struct uvc_device * dev)2816 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2817 {
2818 	struct uvc_entity *entity;
2819 	unsigned int i;
2820 
2821 	/* Can be uninitialized if we are aborting on probe error. */
2822 	if (dev->async_ctrl.work.func)
2823 		cancel_work_sync(&dev->async_ctrl.work);
2824 
2825 	/* Free controls and control mappings for all entities. */
2826 	list_for_each_entry(entity, &dev->entities, list) {
2827 		for (i = 0; i < entity->ncontrols; ++i) {
2828 			struct uvc_control *ctrl = &entity->controls[i];
2829 
2830 			if (!ctrl->initialized)
2831 				continue;
2832 
2833 			uvc_ctrl_cleanup_mappings(dev, ctrl);
2834 			kfree(ctrl->uvc_data);
2835 		}
2836 
2837 		kfree(entity->controls);
2838 	}
2839 }
2840