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