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