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