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