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