xref: /openbmc/linux/drivers/media/usb/uvc/uvc_ctrl.c (revision 8730046c)
1 /*
2  *      uvc_ctrl.c  --  USB Video Class driver - Controls
3  *
4  *      Copyright (C) 2005-2010
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <linux/atomic.h>
24 #include <media/v4l2-ctrls.h>
25 
26 #include "uvcvideo.h"
27 
28 #define UVC_CTRL_DATA_CURRENT	0
29 #define UVC_CTRL_DATA_BACKUP	1
30 #define UVC_CTRL_DATA_MIN	2
31 #define UVC_CTRL_DATA_MAX	3
32 #define UVC_CTRL_DATA_RES	4
33 #define UVC_CTRL_DATA_DEF	5
34 #define UVC_CTRL_DATA_LAST	6
35 
36 /* ------------------------------------------------------------------------
37  * Controls
38  */
39 
40 static struct uvc_control_info uvc_ctrls[] = {
41 	{
42 		.entity		= UVC_GUID_UVC_PROCESSING,
43 		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
44 		.index		= 0,
45 		.size		= 2,
46 		.flags		= UVC_CTRL_FLAG_SET_CUR
47 				| UVC_CTRL_FLAG_GET_RANGE
48 				| UVC_CTRL_FLAG_RESTORE,
49 	},
50 	{
51 		.entity		= UVC_GUID_UVC_PROCESSING,
52 		.selector	= UVC_PU_CONTRAST_CONTROL,
53 		.index		= 1,
54 		.size		= 2,
55 		.flags		= UVC_CTRL_FLAG_SET_CUR
56 				| UVC_CTRL_FLAG_GET_RANGE
57 				| UVC_CTRL_FLAG_RESTORE,
58 	},
59 	{
60 		.entity		= UVC_GUID_UVC_PROCESSING,
61 		.selector	= UVC_PU_HUE_CONTROL,
62 		.index		= 2,
63 		.size		= 2,
64 		.flags		= UVC_CTRL_FLAG_SET_CUR
65 				| UVC_CTRL_FLAG_GET_RANGE
66 				| UVC_CTRL_FLAG_RESTORE
67 				| UVC_CTRL_FLAG_AUTO_UPDATE,
68 	},
69 	{
70 		.entity		= UVC_GUID_UVC_PROCESSING,
71 		.selector	= UVC_PU_SATURATION_CONTROL,
72 		.index		= 3,
73 		.size		= 2,
74 		.flags		= UVC_CTRL_FLAG_SET_CUR
75 				| UVC_CTRL_FLAG_GET_RANGE
76 				| UVC_CTRL_FLAG_RESTORE,
77 	},
78 	{
79 		.entity		= UVC_GUID_UVC_PROCESSING,
80 		.selector	= UVC_PU_SHARPNESS_CONTROL,
81 		.index		= 4,
82 		.size		= 2,
83 		.flags		= UVC_CTRL_FLAG_SET_CUR
84 				| UVC_CTRL_FLAG_GET_RANGE
85 				| UVC_CTRL_FLAG_RESTORE,
86 	},
87 	{
88 		.entity		= UVC_GUID_UVC_PROCESSING,
89 		.selector	= UVC_PU_GAMMA_CONTROL,
90 		.index		= 5,
91 		.size		= 2,
92 		.flags		= UVC_CTRL_FLAG_SET_CUR
93 				| UVC_CTRL_FLAG_GET_RANGE
94 				| UVC_CTRL_FLAG_RESTORE,
95 	},
96 	{
97 		.entity		= UVC_GUID_UVC_PROCESSING,
98 		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
99 		.index		= 6,
100 		.size		= 2,
101 		.flags		= UVC_CTRL_FLAG_SET_CUR
102 				| UVC_CTRL_FLAG_GET_RANGE
103 				| UVC_CTRL_FLAG_RESTORE
104 				| UVC_CTRL_FLAG_AUTO_UPDATE,
105 	},
106 	{
107 		.entity		= UVC_GUID_UVC_PROCESSING,
108 		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
109 		.index		= 7,
110 		.size		= 4,
111 		.flags		= UVC_CTRL_FLAG_SET_CUR
112 				| UVC_CTRL_FLAG_GET_RANGE
113 				| UVC_CTRL_FLAG_RESTORE
114 				| UVC_CTRL_FLAG_AUTO_UPDATE,
115 	},
116 	{
117 		.entity		= UVC_GUID_UVC_PROCESSING,
118 		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
119 		.index		= 8,
120 		.size		= 2,
121 		.flags		= UVC_CTRL_FLAG_SET_CUR
122 				| UVC_CTRL_FLAG_GET_RANGE
123 				| UVC_CTRL_FLAG_RESTORE,
124 	},
125 	{
126 		.entity		= UVC_GUID_UVC_PROCESSING,
127 		.selector	= UVC_PU_GAIN_CONTROL,
128 		.index		= 9,
129 		.size		= 2,
130 		.flags		= UVC_CTRL_FLAG_SET_CUR
131 				| UVC_CTRL_FLAG_GET_RANGE
132 				| UVC_CTRL_FLAG_RESTORE,
133 	},
134 	{
135 		.entity		= UVC_GUID_UVC_PROCESSING,
136 		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
137 		.index		= 10,
138 		.size		= 1,
139 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
140 				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
141 	},
142 	{
143 		.entity		= UVC_GUID_UVC_PROCESSING,
144 		.selector	= UVC_PU_HUE_AUTO_CONTROL,
145 		.index		= 11,
146 		.size		= 1,
147 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
148 				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
149 	},
150 	{
151 		.entity		= UVC_GUID_UVC_PROCESSING,
152 		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
153 		.index		= 12,
154 		.size		= 1,
155 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
156 				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
157 	},
158 	{
159 		.entity		= UVC_GUID_UVC_PROCESSING,
160 		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
161 		.index		= 13,
162 		.size		= 1,
163 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
164 				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
165 	},
166 	{
167 		.entity		= UVC_GUID_UVC_PROCESSING,
168 		.selector	= UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
169 		.index		= 14,
170 		.size		= 2,
171 		.flags		= UVC_CTRL_FLAG_SET_CUR
172 				| UVC_CTRL_FLAG_GET_RANGE
173 				| UVC_CTRL_FLAG_RESTORE,
174 	},
175 	{
176 		.entity		= UVC_GUID_UVC_PROCESSING,
177 		.selector	= UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
178 		.index		= 15,
179 		.size		= 2,
180 		.flags		= UVC_CTRL_FLAG_SET_CUR
181 				| UVC_CTRL_FLAG_GET_RANGE
182 				| UVC_CTRL_FLAG_RESTORE,
183 	},
184 	{
185 		.entity		= UVC_GUID_UVC_PROCESSING,
186 		.selector	= UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
187 		.index		= 16,
188 		.size		= 1,
189 		.flags		= UVC_CTRL_FLAG_GET_CUR,
190 	},
191 	{
192 		.entity		= UVC_GUID_UVC_PROCESSING,
193 		.selector	= UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
194 		.index		= 17,
195 		.size		= 1,
196 		.flags		= UVC_CTRL_FLAG_GET_CUR,
197 	},
198 	{
199 		.entity		= UVC_GUID_UVC_CAMERA,
200 		.selector	= UVC_CT_SCANNING_MODE_CONTROL,
201 		.index		= 0,
202 		.size		= 1,
203 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
204 				| UVC_CTRL_FLAG_RESTORE,
205 	},
206 	{
207 		.entity		= UVC_GUID_UVC_CAMERA,
208 		.selector	= UVC_CT_AE_MODE_CONTROL,
209 		.index		= 1,
210 		.size		= 1,
211 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
212 				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
213 				| UVC_CTRL_FLAG_RESTORE,
214 	},
215 	{
216 		.entity		= UVC_GUID_UVC_CAMERA,
217 		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
218 		.index		= 2,
219 		.size		= 1,
220 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
221 				| UVC_CTRL_FLAG_RESTORE,
222 	},
223 	{
224 		.entity		= UVC_GUID_UVC_CAMERA,
225 		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
226 		.index		= 3,
227 		.size		= 4,
228 		.flags		= UVC_CTRL_FLAG_SET_CUR
229 				| UVC_CTRL_FLAG_GET_RANGE
230 				| UVC_CTRL_FLAG_RESTORE
231 				| UVC_CTRL_FLAG_AUTO_UPDATE,
232 	},
233 	{
234 		.entity		= UVC_GUID_UVC_CAMERA,
235 		.selector	= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
236 		.index		= 4,
237 		.size		= 1,
238 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
239 	},
240 	{
241 		.entity		= UVC_GUID_UVC_CAMERA,
242 		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
243 		.index		= 5,
244 		.size		= 2,
245 		.flags		= UVC_CTRL_FLAG_SET_CUR
246 				| UVC_CTRL_FLAG_GET_RANGE
247 				| UVC_CTRL_FLAG_RESTORE
248 				| UVC_CTRL_FLAG_AUTO_UPDATE,
249 	},
250 	{
251 		.entity		= UVC_GUID_UVC_CAMERA,
252 		.selector	= UVC_CT_FOCUS_RELATIVE_CONTROL,
253 		.index		= 6,
254 		.size		= 2,
255 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
256 				| UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
257 				| UVC_CTRL_FLAG_GET_DEF
258 				| UVC_CTRL_FLAG_AUTO_UPDATE,
259 	},
260 	{
261 		.entity		= UVC_GUID_UVC_CAMERA,
262 		.selector	= UVC_CT_IRIS_ABSOLUTE_CONTROL,
263 		.index		= 7,
264 		.size		= 2,
265 		.flags		= UVC_CTRL_FLAG_SET_CUR
266 				| UVC_CTRL_FLAG_GET_RANGE
267 				| UVC_CTRL_FLAG_RESTORE
268 				| UVC_CTRL_FLAG_AUTO_UPDATE,
269 	},
270 	{
271 		.entity		= UVC_GUID_UVC_CAMERA,
272 		.selector	= UVC_CT_IRIS_RELATIVE_CONTROL,
273 		.index		= 8,
274 		.size		= 1,
275 		.flags		= UVC_CTRL_FLAG_SET_CUR
276 				| UVC_CTRL_FLAG_AUTO_UPDATE,
277 	},
278 	{
279 		.entity		= UVC_GUID_UVC_CAMERA,
280 		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
281 		.index		= 9,
282 		.size		= 2,
283 		.flags		= UVC_CTRL_FLAG_SET_CUR
284 				| UVC_CTRL_FLAG_GET_RANGE
285 				| UVC_CTRL_FLAG_RESTORE
286 				| UVC_CTRL_FLAG_AUTO_UPDATE,
287 	},
288 	{
289 		.entity		= UVC_GUID_UVC_CAMERA,
290 		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
291 		.index		= 10,
292 		.size		= 3,
293 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
294 				| UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
295 				| UVC_CTRL_FLAG_GET_DEF
296 				| UVC_CTRL_FLAG_AUTO_UPDATE,
297 	},
298 	{
299 		.entity		= UVC_GUID_UVC_CAMERA,
300 		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
301 		.index		= 11,
302 		.size		= 8,
303 		.flags		= UVC_CTRL_FLAG_SET_CUR
304 				| UVC_CTRL_FLAG_GET_RANGE
305 				| UVC_CTRL_FLAG_RESTORE
306 				| UVC_CTRL_FLAG_AUTO_UPDATE,
307 	},
308 	{
309 		.entity		= UVC_GUID_UVC_CAMERA,
310 		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
311 		.index		= 12,
312 		.size		= 4,
313 		.flags		= UVC_CTRL_FLAG_SET_CUR
314 				| UVC_CTRL_FLAG_GET_RANGE
315 				| UVC_CTRL_FLAG_AUTO_UPDATE,
316 	},
317 	{
318 		.entity		= UVC_GUID_UVC_CAMERA,
319 		.selector	= UVC_CT_ROLL_ABSOLUTE_CONTROL,
320 		.index		= 13,
321 		.size		= 2,
322 		.flags		= UVC_CTRL_FLAG_SET_CUR
323 				| UVC_CTRL_FLAG_GET_RANGE
324 				| UVC_CTRL_FLAG_RESTORE
325 				| UVC_CTRL_FLAG_AUTO_UPDATE,
326 	},
327 	{
328 		.entity		= UVC_GUID_UVC_CAMERA,
329 		.selector	= UVC_CT_ROLL_RELATIVE_CONTROL,
330 		.index		= 14,
331 		.size		= 2,
332 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
333 				| UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
334 				| UVC_CTRL_FLAG_GET_DEF
335 				| UVC_CTRL_FLAG_AUTO_UPDATE,
336 	},
337 	{
338 		.entity		= UVC_GUID_UVC_CAMERA,
339 		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
340 		.index		= 17,
341 		.size		= 1,
342 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
343 				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
344 	},
345 	{
346 		.entity		= UVC_GUID_UVC_CAMERA,
347 		.selector	= UVC_CT_PRIVACY_CONTROL,
348 		.index		= 18,
349 		.size		= 1,
350 		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
351 				| UVC_CTRL_FLAG_RESTORE
352 				| UVC_CTRL_FLAG_AUTO_UPDATE,
353 	},
354 };
355 
356 static struct uvc_menu_info power_line_frequency_controls[] = {
357 	{ 0, "Disabled" },
358 	{ 1, "50 Hz" },
359 	{ 2, "60 Hz" },
360 };
361 
362 static struct uvc_menu_info exposure_auto_controls[] = {
363 	{ 2, "Auto Mode" },
364 	{ 1, "Manual Mode" },
365 	{ 4, "Shutter Priority Mode" },
366 	{ 8, "Aperture Priority Mode" },
367 };
368 
369 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
370 	__u8 query, const __u8 *data)
371 {
372 	__s8 zoom = (__s8)data[0];
373 
374 	switch (query) {
375 	case UVC_GET_CUR:
376 		return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
377 
378 	case UVC_GET_MIN:
379 	case UVC_GET_MAX:
380 	case UVC_GET_RES:
381 	case UVC_GET_DEF:
382 	default:
383 		return data[2];
384 	}
385 }
386 
387 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
388 	__s32 value, __u8 *data)
389 {
390 	data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
391 	data[2] = min((int)abs(value), 0xff);
392 }
393 
394 static __s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
395 	__u8 query, const __u8 *data)
396 {
397 	unsigned int first = mapping->offset / 8;
398 	__s8 rel = (__s8)data[first];
399 
400 	switch (query) {
401 	case UVC_GET_CUR:
402 		return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
403 						 : -data[first+1]);
404 	case UVC_GET_MIN:
405 		return -data[first+1];
406 	case UVC_GET_MAX:
407 	case UVC_GET_RES:
408 	case UVC_GET_DEF:
409 	default:
410 		return data[first+1];
411 	}
412 }
413 
414 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
415 	__s32 value, __u8 *data)
416 {
417 	unsigned int first = mapping->offset / 8;
418 
419 	data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
420 	data[first+1] = min_t(int, abs(value), 0xff);
421 }
422 
423 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
424 	{
425 		.id		= V4L2_CID_BRIGHTNESS,
426 		.name		= "Brightness",
427 		.entity		= UVC_GUID_UVC_PROCESSING,
428 		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
429 		.size		= 16,
430 		.offset		= 0,
431 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
432 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
433 	},
434 	{
435 		.id		= V4L2_CID_CONTRAST,
436 		.name		= "Contrast",
437 		.entity		= UVC_GUID_UVC_PROCESSING,
438 		.selector	= UVC_PU_CONTRAST_CONTROL,
439 		.size		= 16,
440 		.offset		= 0,
441 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
442 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
443 	},
444 	{
445 		.id		= V4L2_CID_HUE,
446 		.name		= "Hue",
447 		.entity		= UVC_GUID_UVC_PROCESSING,
448 		.selector	= UVC_PU_HUE_CONTROL,
449 		.size		= 16,
450 		.offset		= 0,
451 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
452 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
453 		.master_id	= V4L2_CID_HUE_AUTO,
454 		.master_manual	= 0,
455 	},
456 	{
457 		.id		= V4L2_CID_SATURATION,
458 		.name		= "Saturation",
459 		.entity		= UVC_GUID_UVC_PROCESSING,
460 		.selector	= UVC_PU_SATURATION_CONTROL,
461 		.size		= 16,
462 		.offset		= 0,
463 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
464 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
465 	},
466 	{
467 		.id		= V4L2_CID_SHARPNESS,
468 		.name		= "Sharpness",
469 		.entity		= UVC_GUID_UVC_PROCESSING,
470 		.selector	= UVC_PU_SHARPNESS_CONTROL,
471 		.size		= 16,
472 		.offset		= 0,
473 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
474 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
475 	},
476 	{
477 		.id		= V4L2_CID_GAMMA,
478 		.name		= "Gamma",
479 		.entity		= UVC_GUID_UVC_PROCESSING,
480 		.selector	= UVC_PU_GAMMA_CONTROL,
481 		.size		= 16,
482 		.offset		= 0,
483 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
484 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
485 	},
486 	{
487 		.id		= V4L2_CID_BACKLIGHT_COMPENSATION,
488 		.name		= "Backlight Compensation",
489 		.entity		= UVC_GUID_UVC_PROCESSING,
490 		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
491 		.size		= 16,
492 		.offset		= 0,
493 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
494 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
495 	},
496 	{
497 		.id		= V4L2_CID_GAIN,
498 		.name		= "Gain",
499 		.entity		= UVC_GUID_UVC_PROCESSING,
500 		.selector	= UVC_PU_GAIN_CONTROL,
501 		.size		= 16,
502 		.offset		= 0,
503 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
504 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
505 	},
506 	{
507 		.id		= V4L2_CID_POWER_LINE_FREQUENCY,
508 		.name		= "Power Line Frequency",
509 		.entity		= UVC_GUID_UVC_PROCESSING,
510 		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
511 		.size		= 2,
512 		.offset		= 0,
513 		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
514 		.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
515 		.menu_info	= power_line_frequency_controls,
516 		.menu_count	= ARRAY_SIZE(power_line_frequency_controls),
517 	},
518 	{
519 		.id		= V4L2_CID_HUE_AUTO,
520 		.name		= "Hue, Auto",
521 		.entity		= UVC_GUID_UVC_PROCESSING,
522 		.selector	= UVC_PU_HUE_AUTO_CONTROL,
523 		.size		= 1,
524 		.offset		= 0,
525 		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
526 		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
527 		.slave_ids	= { V4L2_CID_HUE, },
528 	},
529 	{
530 		.id		= V4L2_CID_EXPOSURE_AUTO,
531 		.name		= "Exposure, Auto",
532 		.entity		= UVC_GUID_UVC_CAMERA,
533 		.selector	= UVC_CT_AE_MODE_CONTROL,
534 		.size		= 4,
535 		.offset		= 0,
536 		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
537 		.data_type	= UVC_CTRL_DATA_TYPE_BITMASK,
538 		.menu_info	= exposure_auto_controls,
539 		.menu_count	= ARRAY_SIZE(exposure_auto_controls),
540 		.slave_ids	= { V4L2_CID_EXPOSURE_ABSOLUTE, },
541 	},
542 	{
543 		.id		= V4L2_CID_EXPOSURE_AUTO_PRIORITY,
544 		.name		= "Exposure, Auto Priority",
545 		.entity		= UVC_GUID_UVC_CAMERA,
546 		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
547 		.size		= 1,
548 		.offset		= 0,
549 		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
550 		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
551 	},
552 	{
553 		.id		= V4L2_CID_EXPOSURE_ABSOLUTE,
554 		.name		= "Exposure (Absolute)",
555 		.entity		= UVC_GUID_UVC_CAMERA,
556 		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
557 		.size		= 32,
558 		.offset		= 0,
559 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
560 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
561 		.master_id	= V4L2_CID_EXPOSURE_AUTO,
562 		.master_manual	= V4L2_EXPOSURE_MANUAL,
563 	},
564 	{
565 		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
566 		.name		= "White Balance Temperature, Auto",
567 		.entity		= UVC_GUID_UVC_PROCESSING,
568 		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
569 		.size		= 1,
570 		.offset		= 0,
571 		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
572 		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
573 		.slave_ids	= { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
574 	},
575 	{
576 		.id		= V4L2_CID_WHITE_BALANCE_TEMPERATURE,
577 		.name		= "White Balance Temperature",
578 		.entity		= UVC_GUID_UVC_PROCESSING,
579 		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
580 		.size		= 16,
581 		.offset		= 0,
582 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
583 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
584 		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
585 		.master_manual	= 0,
586 	},
587 	{
588 		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
589 		.name		= "White Balance Component, Auto",
590 		.entity		= UVC_GUID_UVC_PROCESSING,
591 		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
592 		.size		= 1,
593 		.offset		= 0,
594 		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
595 		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
596 		.slave_ids	= { V4L2_CID_BLUE_BALANCE,
597 				    V4L2_CID_RED_BALANCE },
598 	},
599 	{
600 		.id		= V4L2_CID_BLUE_BALANCE,
601 		.name		= "White Balance Blue Component",
602 		.entity		= UVC_GUID_UVC_PROCESSING,
603 		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
604 		.size		= 16,
605 		.offset		= 0,
606 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
607 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
608 		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
609 		.master_manual	= 0,
610 	},
611 	{
612 		.id		= V4L2_CID_RED_BALANCE,
613 		.name		= "White Balance Red Component",
614 		.entity		= UVC_GUID_UVC_PROCESSING,
615 		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
616 		.size		= 16,
617 		.offset		= 16,
618 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
619 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
620 		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
621 		.master_manual	= 0,
622 	},
623 	{
624 		.id		= V4L2_CID_FOCUS_ABSOLUTE,
625 		.name		= "Focus (absolute)",
626 		.entity		= UVC_GUID_UVC_CAMERA,
627 		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
628 		.size		= 16,
629 		.offset		= 0,
630 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
631 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
632 		.master_id	= V4L2_CID_FOCUS_AUTO,
633 		.master_manual	= 0,
634 	},
635 	{
636 		.id		= V4L2_CID_FOCUS_AUTO,
637 		.name		= "Focus, Auto",
638 		.entity		= UVC_GUID_UVC_CAMERA,
639 		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
640 		.size		= 1,
641 		.offset		= 0,
642 		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
643 		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
644 		.slave_ids	= { V4L2_CID_FOCUS_ABSOLUTE, },
645 	},
646 	{
647 		.id		= V4L2_CID_IRIS_ABSOLUTE,
648 		.name		= "Iris, Absolute",
649 		.entity		= UVC_GUID_UVC_CAMERA,
650 		.selector	= UVC_CT_IRIS_ABSOLUTE_CONTROL,
651 		.size		= 16,
652 		.offset		= 0,
653 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
654 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
655 	},
656 	{
657 		.id		= V4L2_CID_IRIS_RELATIVE,
658 		.name		= "Iris, Relative",
659 		.entity		= UVC_GUID_UVC_CAMERA,
660 		.selector	= UVC_CT_IRIS_RELATIVE_CONTROL,
661 		.size		= 8,
662 		.offset		= 0,
663 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
664 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
665 	},
666 	{
667 		.id		= V4L2_CID_ZOOM_ABSOLUTE,
668 		.name		= "Zoom, Absolute",
669 		.entity		= UVC_GUID_UVC_CAMERA,
670 		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
671 		.size		= 16,
672 		.offset		= 0,
673 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
674 		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
675 	},
676 	{
677 		.id		= V4L2_CID_ZOOM_CONTINUOUS,
678 		.name		= "Zoom, Continuous",
679 		.entity		= UVC_GUID_UVC_CAMERA,
680 		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
681 		.size		= 0,
682 		.offset		= 0,
683 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
684 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
685 		.get		= uvc_ctrl_get_zoom,
686 		.set		= uvc_ctrl_set_zoom,
687 	},
688 	{
689 		.id		= V4L2_CID_PAN_ABSOLUTE,
690 		.name		= "Pan (Absolute)",
691 		.entity		= UVC_GUID_UVC_CAMERA,
692 		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
693 		.size		= 32,
694 		.offset		= 0,
695 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
696 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
697 	},
698 	{
699 		.id		= V4L2_CID_TILT_ABSOLUTE,
700 		.name		= "Tilt (Absolute)",
701 		.entity		= UVC_GUID_UVC_CAMERA,
702 		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
703 		.size		= 32,
704 		.offset		= 32,
705 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
706 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
707 	},
708 	{
709 		.id		= V4L2_CID_PAN_SPEED,
710 		.name		= "Pan (Speed)",
711 		.entity		= UVC_GUID_UVC_CAMERA,
712 		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
713 		.size		= 16,
714 		.offset		= 0,
715 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
716 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
717 		.get		= uvc_ctrl_get_rel_speed,
718 		.set		= uvc_ctrl_set_rel_speed,
719 	},
720 	{
721 		.id		= V4L2_CID_TILT_SPEED,
722 		.name		= "Tilt (Speed)",
723 		.entity		= UVC_GUID_UVC_CAMERA,
724 		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
725 		.size		= 16,
726 		.offset		= 16,
727 		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
728 		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
729 		.get		= uvc_ctrl_get_rel_speed,
730 		.set		= uvc_ctrl_set_rel_speed,
731 	},
732 	{
733 		.id		= V4L2_CID_PRIVACY,
734 		.name		= "Privacy",
735 		.entity		= UVC_GUID_UVC_CAMERA,
736 		.selector	= UVC_CT_PRIVACY_CONTROL,
737 		.size		= 1,
738 		.offset		= 0,
739 		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
740 		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
741 	},
742 };
743 
744 /* ------------------------------------------------------------------------
745  * Utility functions
746  */
747 
748 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
749 {
750 	return ctrl->uvc_data + id * ctrl->info.size;
751 }
752 
753 static inline int uvc_test_bit(const __u8 *data, int bit)
754 {
755 	return (data[bit >> 3] >> (bit & 7)) & 1;
756 }
757 
758 static inline void uvc_clear_bit(__u8 *data, int bit)
759 {
760 	data[bit >> 3] &= ~(1 << (bit & 7));
761 }
762 
763 /* Extract the bit string specified by mapping->offset and mapping->size
764  * from the little-endian data stored at 'data' and return the result as
765  * a signed 32bit integer. Sign extension will be performed if the mapping
766  * references a signed data type.
767  */
768 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
769 	__u8 query, const __u8 *data)
770 {
771 	int bits = mapping->size;
772 	int offset = mapping->offset;
773 	__s32 value = 0;
774 	__u8 mask;
775 
776 	data += offset / 8;
777 	offset &= 7;
778 	mask = ((1LL << bits) - 1) << offset;
779 
780 	for (; bits > 0; data++) {
781 		__u8 byte = *data & mask;
782 		value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
783 		bits -= 8 - (offset > 0 ? offset : 0);
784 		offset -= 8;
785 		mask = (1 << bits) - 1;
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 int __uvc_ctrl_get(struct uvc_video_chain *chain,
975 	struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
976 	s32 *value)
977 {
978 	struct uvc_menu_info *menu;
979 	unsigned int i;
980 	int ret;
981 
982 	if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
983 		return -EACCES;
984 
985 	if (!ctrl->loaded) {
986 		ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
987 				chain->dev->intfnum, ctrl->info.selector,
988 				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
989 				ctrl->info.size);
990 		if (ret < 0)
991 			return ret;
992 
993 		ctrl->loaded = 1;
994 	}
995 
996 	*value = mapping->get(mapping, UVC_GET_CUR,
997 		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
998 
999 	if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1000 		menu = mapping->menu_info;
1001 		for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1002 			if (menu->value == *value) {
1003 				*value = i;
1004 				break;
1005 			}
1006 		}
1007 	}
1008 
1009 	return 0;
1010 }
1011 
1012 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1013 	struct uvc_control *ctrl,
1014 	struct uvc_control_mapping *mapping,
1015 	struct v4l2_queryctrl *v4l2_ctrl)
1016 {
1017 	struct uvc_control_mapping *master_map = NULL;
1018 	struct uvc_control *master_ctrl = NULL;
1019 	struct uvc_menu_info *menu;
1020 	unsigned int i;
1021 
1022 	memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
1023 	v4l2_ctrl->id = mapping->id;
1024 	v4l2_ctrl->type = mapping->v4l2_type;
1025 	strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
1026 	v4l2_ctrl->flags = 0;
1027 
1028 	if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1029 		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1030 	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1031 		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1032 
1033 	if (mapping->master_id)
1034 		__uvc_find_control(ctrl->entity, mapping->master_id,
1035 				   &master_map, &master_ctrl, 0);
1036 	if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1037 		s32 val;
1038 		int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1039 		if (ret < 0)
1040 			return ret;
1041 
1042 		if (val != mapping->master_manual)
1043 				v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1044 	}
1045 
1046 	if (!ctrl->cached) {
1047 		int ret = uvc_ctrl_populate_cache(chain, ctrl);
1048 		if (ret < 0)
1049 			return ret;
1050 	}
1051 
1052 	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1053 		v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1054 				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1055 	}
1056 
1057 	switch (mapping->v4l2_type) {
1058 	case V4L2_CTRL_TYPE_MENU:
1059 		v4l2_ctrl->minimum = 0;
1060 		v4l2_ctrl->maximum = mapping->menu_count - 1;
1061 		v4l2_ctrl->step = 1;
1062 
1063 		menu = mapping->menu_info;
1064 		for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1065 			if (menu->value == v4l2_ctrl->default_value) {
1066 				v4l2_ctrl->default_value = i;
1067 				break;
1068 			}
1069 		}
1070 
1071 		return 0;
1072 
1073 	case V4L2_CTRL_TYPE_BOOLEAN:
1074 		v4l2_ctrl->minimum = 0;
1075 		v4l2_ctrl->maximum = 1;
1076 		v4l2_ctrl->step = 1;
1077 		return 0;
1078 
1079 	case V4L2_CTRL_TYPE_BUTTON:
1080 		v4l2_ctrl->minimum = 0;
1081 		v4l2_ctrl->maximum = 0;
1082 		v4l2_ctrl->step = 0;
1083 		return 0;
1084 
1085 	default:
1086 		break;
1087 	}
1088 
1089 	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1090 		v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1091 				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1092 
1093 	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1094 		v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1095 				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1096 
1097 	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1098 		v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1099 				  uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1100 
1101 	return 0;
1102 }
1103 
1104 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1105 	struct v4l2_queryctrl *v4l2_ctrl)
1106 {
1107 	struct uvc_control *ctrl;
1108 	struct uvc_control_mapping *mapping;
1109 	int ret;
1110 
1111 	ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1112 	if (ret < 0)
1113 		return -ERESTARTSYS;
1114 
1115 	ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1116 	if (ctrl == NULL) {
1117 		ret = -EINVAL;
1118 		goto done;
1119 	}
1120 
1121 	ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1122 done:
1123 	mutex_unlock(&chain->ctrl_mutex);
1124 	return ret;
1125 }
1126 
1127 /*
1128  * Mapping V4L2 controls to UVC controls can be straighforward if done well.
1129  * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1130  * must be grouped (for instance the Red Balance, Blue Balance and Do White
1131  * Balance V4L2 controls use the White Balance Component UVC control) or
1132  * otherwise translated. The approach we take here is to use a translation
1133  * table for the controls that can be mapped directly, and handle the others
1134  * manually.
1135  */
1136 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1137 	struct v4l2_querymenu *query_menu)
1138 {
1139 	struct uvc_menu_info *menu_info;
1140 	struct uvc_control_mapping *mapping;
1141 	struct uvc_control *ctrl;
1142 	u32 index = query_menu->index;
1143 	u32 id = query_menu->id;
1144 	int ret;
1145 
1146 	memset(query_menu, 0, sizeof(*query_menu));
1147 	query_menu->id = id;
1148 	query_menu->index = index;
1149 
1150 	ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1151 	if (ret < 0)
1152 		return -ERESTARTSYS;
1153 
1154 	ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1155 	if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1156 		ret = -EINVAL;
1157 		goto done;
1158 	}
1159 
1160 	if (query_menu->index >= mapping->menu_count) {
1161 		ret = -EINVAL;
1162 		goto done;
1163 	}
1164 
1165 	menu_info = &mapping->menu_info[query_menu->index];
1166 
1167 	if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1168 	    (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1169 		s32 bitmap;
1170 
1171 		if (!ctrl->cached) {
1172 			ret = uvc_ctrl_populate_cache(chain, ctrl);
1173 			if (ret < 0)
1174 				goto done;
1175 		}
1176 
1177 		bitmap = mapping->get(mapping, UVC_GET_RES,
1178 				      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1179 		if (!(bitmap & menu_info->value)) {
1180 			ret = -EINVAL;
1181 			goto done;
1182 		}
1183 	}
1184 
1185 	strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
1186 
1187 done:
1188 	mutex_unlock(&chain->ctrl_mutex);
1189 	return ret;
1190 }
1191 
1192 /* --------------------------------------------------------------------------
1193  * Ctrl event handling
1194  */
1195 
1196 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1197 	struct v4l2_event *ev,
1198 	struct uvc_control *ctrl,
1199 	struct uvc_control_mapping *mapping,
1200 	s32 value, u32 changes)
1201 {
1202 	struct v4l2_queryctrl v4l2_ctrl;
1203 
1204 	__uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1205 
1206 	memset(ev->reserved, 0, sizeof(ev->reserved));
1207 	ev->type = V4L2_EVENT_CTRL;
1208 	ev->id = v4l2_ctrl.id;
1209 	ev->u.ctrl.value = value;
1210 	ev->u.ctrl.changes = changes;
1211 	ev->u.ctrl.type = v4l2_ctrl.type;
1212 	ev->u.ctrl.flags = v4l2_ctrl.flags;
1213 	ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1214 	ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1215 	ev->u.ctrl.step = v4l2_ctrl.step;
1216 	ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1217 }
1218 
1219 static void uvc_ctrl_send_event(struct uvc_fh *handle,
1220 	struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
1221 	s32 value, u32 changes)
1222 {
1223 	struct v4l2_subscribed_event *sev;
1224 	struct v4l2_event ev;
1225 
1226 	if (list_empty(&mapping->ev_subs))
1227 		return;
1228 
1229 	uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes);
1230 
1231 	list_for_each_entry(sev, &mapping->ev_subs, node) {
1232 		if (sev->fh && (sev->fh != &handle->vfh ||
1233 		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1234 		    (changes & V4L2_EVENT_CTRL_CH_FLAGS)))
1235 			v4l2_event_queue_fh(sev->fh, &ev);
1236 	}
1237 }
1238 
1239 static void uvc_ctrl_send_slave_event(struct uvc_fh *handle,
1240 	struct uvc_control *master, u32 slave_id,
1241 	const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1242 {
1243 	struct uvc_control_mapping *mapping = NULL;
1244 	struct uvc_control *ctrl = NULL;
1245 	u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1246 	unsigned int i;
1247 	s32 val = 0;
1248 
1249 	/*
1250 	 * We can skip sending an event for the slave if the slave
1251 	 * is being modified in the same transaction.
1252 	 */
1253 	for (i = 0; i < xctrls_count; i++) {
1254 		if (xctrls[i].id == slave_id)
1255 			return;
1256 	}
1257 
1258 	__uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1259 	if (ctrl == NULL)
1260 		return;
1261 
1262 	if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1263 		changes |= V4L2_EVENT_CTRL_CH_VALUE;
1264 
1265 	uvc_ctrl_send_event(handle, ctrl, mapping, val, changes);
1266 }
1267 
1268 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1269 	const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1270 {
1271 	struct uvc_control_mapping *mapping;
1272 	struct uvc_control *ctrl;
1273 	u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1274 	unsigned int i;
1275 	unsigned int j;
1276 
1277 	for (i = 0; i < xctrls_count; ++i) {
1278 		ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1279 
1280 		for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1281 			if (!mapping->slave_ids[j])
1282 				break;
1283 			uvc_ctrl_send_slave_event(handle, ctrl,
1284 						  mapping->slave_ids[j],
1285 						  xctrls, xctrls_count);
1286 		}
1287 
1288 		/*
1289 		 * If the master is being modified in the same transaction
1290 		 * flags may change too.
1291 		 */
1292 		if (mapping->master_id) {
1293 			for (j = 0; j < xctrls_count; j++) {
1294 				if (xctrls[j].id == mapping->master_id) {
1295 					changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1296 					break;
1297 				}
1298 			}
1299 		}
1300 
1301 		uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
1302 				    changes);
1303 	}
1304 }
1305 
1306 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1307 {
1308 	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1309 	struct uvc_control_mapping *mapping;
1310 	struct uvc_control *ctrl;
1311 	int ret;
1312 
1313 	ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1314 	if (ret < 0)
1315 		return -ERESTARTSYS;
1316 
1317 	ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1318 	if (ctrl == NULL) {
1319 		ret = -EINVAL;
1320 		goto done;
1321 	}
1322 
1323 	list_add_tail(&sev->node, &mapping->ev_subs);
1324 	if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1325 		struct v4l2_event ev;
1326 		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1327 		s32 val = 0;
1328 
1329 		if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1330 			changes |= V4L2_EVENT_CTRL_CH_VALUE;
1331 
1332 		uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1333 				    changes);
1334 		/* Mark the queue as active, allowing this initial
1335 		   event to be accepted. */
1336 		sev->elems = elems;
1337 		v4l2_event_queue_fh(sev->fh, &ev);
1338 	}
1339 
1340 done:
1341 	mutex_unlock(&handle->chain->ctrl_mutex);
1342 	return ret;
1343 }
1344 
1345 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1346 {
1347 	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1348 
1349 	mutex_lock(&handle->chain->ctrl_mutex);
1350 	list_del(&sev->node);
1351 	mutex_unlock(&handle->chain->ctrl_mutex);
1352 }
1353 
1354 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1355 	.add = uvc_ctrl_add_event,
1356 	.del = uvc_ctrl_del_event,
1357 	.replace = v4l2_ctrl_replace,
1358 	.merge = v4l2_ctrl_merge,
1359 };
1360 
1361 /* --------------------------------------------------------------------------
1362  * Control transactions
1363  *
1364  * To make extended set operations as atomic as the hardware allows, controls
1365  * are handled using begin/commit/rollback operations.
1366  *
1367  * At the beginning of a set request, uvc_ctrl_begin should be called to
1368  * initialize the request. This function acquires the control lock.
1369  *
1370  * When setting a control, the new value is stored in the control data field
1371  * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1372  * later processing. If the UVC and V4L2 control sizes differ, the current
1373  * value is loaded from the hardware before storing the new value in the data
1374  * field.
1375  *
1376  * After processing all controls in the transaction, uvc_ctrl_commit or
1377  * uvc_ctrl_rollback must be called to apply the pending changes to the
1378  * hardware or revert them. When applying changes, all controls marked as
1379  * dirty will be modified in the UVC device, and the dirty flag will be
1380  * cleared. When reverting controls, the control data field
1381  * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1382  * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1383  * control lock.
1384  */
1385 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1386 {
1387 	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1388 }
1389 
1390 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1391 	struct uvc_entity *entity, int rollback)
1392 {
1393 	struct uvc_control *ctrl;
1394 	unsigned int i;
1395 	int ret;
1396 
1397 	if (entity == NULL)
1398 		return 0;
1399 
1400 	for (i = 0; i < entity->ncontrols; ++i) {
1401 		ctrl = &entity->controls[i];
1402 		if (!ctrl->initialized)
1403 			continue;
1404 
1405 		/* Reset the loaded flag for auto-update controls that were
1406 		 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1407 		 * uvc_ctrl_get from using the cached value, and for write-only
1408 		 * controls to prevent uvc_ctrl_set from setting bits not
1409 		 * explicitly set by the user.
1410 		 */
1411 		if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1412 		    !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1413 			ctrl->loaded = 0;
1414 
1415 		if (!ctrl->dirty)
1416 			continue;
1417 
1418 		if (!rollback)
1419 			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1420 				dev->intfnum, ctrl->info.selector,
1421 				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1422 				ctrl->info.size);
1423 		else
1424 			ret = 0;
1425 
1426 		if (rollback || ret < 0)
1427 			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1428 			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1429 			       ctrl->info.size);
1430 
1431 		ctrl->dirty = 0;
1432 
1433 		if (ret < 0)
1434 			return ret;
1435 	}
1436 
1437 	return 0;
1438 }
1439 
1440 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1441 		      const struct v4l2_ext_control *xctrls,
1442 		      unsigned int xctrls_count)
1443 {
1444 	struct uvc_video_chain *chain = handle->chain;
1445 	struct uvc_entity *entity;
1446 	int ret = 0;
1447 
1448 	/* Find the control. */
1449 	list_for_each_entry(entity, &chain->entities, chain) {
1450 		ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1451 		if (ret < 0)
1452 			goto done;
1453 	}
1454 
1455 	if (!rollback)
1456 		uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1457 done:
1458 	mutex_unlock(&chain->ctrl_mutex);
1459 	return ret;
1460 }
1461 
1462 int uvc_ctrl_get(struct uvc_video_chain *chain,
1463 	struct v4l2_ext_control *xctrl)
1464 {
1465 	struct uvc_control *ctrl;
1466 	struct uvc_control_mapping *mapping;
1467 
1468 	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1469 	if (ctrl == NULL)
1470 		return -EINVAL;
1471 
1472 	return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1473 }
1474 
1475 int uvc_ctrl_set(struct uvc_video_chain *chain,
1476 	struct v4l2_ext_control *xctrl)
1477 {
1478 	struct uvc_control *ctrl;
1479 	struct uvc_control_mapping *mapping;
1480 	s32 value;
1481 	u32 step;
1482 	s32 min;
1483 	s32 max;
1484 	int ret;
1485 
1486 	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1487 	if (ctrl == NULL)
1488 		return -EINVAL;
1489 	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1490 		return -EACCES;
1491 
1492 	/* Clamp out of range values. */
1493 	switch (mapping->v4l2_type) {
1494 	case V4L2_CTRL_TYPE_INTEGER:
1495 		if (!ctrl->cached) {
1496 			ret = uvc_ctrl_populate_cache(chain, ctrl);
1497 			if (ret < 0)
1498 				return ret;
1499 		}
1500 
1501 		min = mapping->get(mapping, UVC_GET_MIN,
1502 				   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1503 		max = mapping->get(mapping, UVC_GET_MAX,
1504 				   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1505 		step = mapping->get(mapping, UVC_GET_RES,
1506 				    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1507 		if (step == 0)
1508 			step = 1;
1509 
1510 		xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
1511 			     / step * step;
1512 		if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1513 			xctrl->value = clamp(xctrl->value, min, max);
1514 		else
1515 			xctrl->value = clamp_t(u32, xctrl->value, min, max);
1516 		value = xctrl->value;
1517 		break;
1518 
1519 	case V4L2_CTRL_TYPE_BOOLEAN:
1520 		xctrl->value = clamp(xctrl->value, 0, 1);
1521 		value = xctrl->value;
1522 		break;
1523 
1524 	case V4L2_CTRL_TYPE_MENU:
1525 		if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1526 			return -ERANGE;
1527 		value = mapping->menu_info[xctrl->value].value;
1528 
1529 		/* Valid menu indices are reported by the GET_RES request for
1530 		 * UVC controls that support it.
1531 		 */
1532 		if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1533 		    (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1534 			if (!ctrl->cached) {
1535 				ret = uvc_ctrl_populate_cache(chain, ctrl);
1536 				if (ret < 0)
1537 					return ret;
1538 			}
1539 
1540 			step = mapping->get(mapping, UVC_GET_RES,
1541 					uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1542 			if (!(step & value))
1543 				return -EINVAL;
1544 		}
1545 
1546 		break;
1547 
1548 	default:
1549 		value = xctrl->value;
1550 		break;
1551 	}
1552 
1553 	/* If the mapping doesn't span the whole UVC control, the current value
1554 	 * needs to be loaded from the device to perform the read-modify-write
1555 	 * operation.
1556 	 */
1557 	if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1558 		if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1559 			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1560 				0, ctrl->info.size);
1561 		} else {
1562 			ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1563 				ctrl->entity->id, chain->dev->intfnum,
1564 				ctrl->info.selector,
1565 				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1566 				ctrl->info.size);
1567 			if (ret < 0)
1568 				return ret;
1569 		}
1570 
1571 		ctrl->loaded = 1;
1572 	}
1573 
1574 	/* Backup the current value in case we need to rollback later. */
1575 	if (!ctrl->dirty) {
1576 		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1577 		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1578 		       ctrl->info.size);
1579 	}
1580 
1581 	mapping->set(mapping, value,
1582 		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1583 
1584 	ctrl->dirty = 1;
1585 	ctrl->modified = 1;
1586 	return 0;
1587 }
1588 
1589 /* --------------------------------------------------------------------------
1590  * Dynamic controls
1591  */
1592 
1593 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1594 	const struct uvc_control *ctrl, struct uvc_control_info *info)
1595 {
1596 	struct uvc_ctrl_fixup {
1597 		struct usb_device_id id;
1598 		u8 entity;
1599 		u8 selector;
1600 		u8 flags;
1601 	};
1602 
1603 	static const struct uvc_ctrl_fixup fixups[] = {
1604 		{ { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1605 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1606 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1607 			UVC_CTRL_FLAG_AUTO_UPDATE },
1608 		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1609 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1610 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1611 			UVC_CTRL_FLAG_AUTO_UPDATE },
1612 		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1613 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1614 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1615 			UVC_CTRL_FLAG_AUTO_UPDATE },
1616 	};
1617 
1618 	unsigned int i;
1619 
1620 	for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1621 		if (!usb_match_one_id(dev->intf, &fixups[i].id))
1622 			continue;
1623 
1624 		if (fixups[i].entity == ctrl->entity->id &&
1625 		    fixups[i].selector == info->selector) {
1626 			info->flags = fixups[i].flags;
1627 			return;
1628 		}
1629 	}
1630 }
1631 
1632 /*
1633  * Query control information (size and flags) for XU controls.
1634  */
1635 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1636 	const struct uvc_control *ctrl, struct uvc_control_info *info)
1637 {
1638 	u8 *data;
1639 	int ret;
1640 
1641 	data = kmalloc(2, GFP_KERNEL);
1642 	if (data == NULL)
1643 		return -ENOMEM;
1644 
1645 	memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1646 	       sizeof(info->entity));
1647 	info->index = ctrl->index;
1648 	info->selector = ctrl->index + 1;
1649 
1650 	/* Query and verify the control length (GET_LEN) */
1651 	ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1652 			     info->selector, data, 2);
1653 	if (ret < 0) {
1654 		uvc_trace(UVC_TRACE_CONTROL,
1655 			  "GET_LEN failed on control %pUl/%u (%d).\n",
1656 			   info->entity, info->selector, ret);
1657 		goto done;
1658 	}
1659 
1660 	info->size = le16_to_cpup((__le16 *)data);
1661 
1662 	/* Query the control information (GET_INFO) */
1663 	ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1664 			     info->selector, data, 1);
1665 	if (ret < 0) {
1666 		uvc_trace(UVC_TRACE_CONTROL,
1667 			  "GET_INFO failed on control %pUl/%u (%d).\n",
1668 			  info->entity, info->selector, ret);
1669 		goto done;
1670 	}
1671 
1672 	info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1673 		    | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1674 		    | (data[0] & UVC_CONTROL_CAP_GET ?
1675 		       UVC_CTRL_FLAG_GET_CUR : 0)
1676 		    | (data[0] & UVC_CONTROL_CAP_SET ?
1677 		       UVC_CTRL_FLAG_SET_CUR : 0)
1678 		    | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1679 		       UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1680 
1681 	uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1682 
1683 	uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1684 		  "flags { get %u set %u auto %u }.\n",
1685 		  info->entity, info->selector, info->size,
1686 		  (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1687 		  (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1688 		  (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1689 
1690 done:
1691 	kfree(data);
1692 	return ret;
1693 }
1694 
1695 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1696 	const struct uvc_control_info *info);
1697 
1698 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1699 	struct uvc_control *ctrl)
1700 {
1701 	struct uvc_control_info info;
1702 	int ret;
1703 
1704 	if (ctrl->initialized)
1705 		return 0;
1706 
1707 	ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1708 	if (ret < 0)
1709 		return ret;
1710 
1711 	ret = uvc_ctrl_add_info(dev, ctrl, &info);
1712 	if (ret < 0)
1713 		uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1714 			  "%pUl/%u on device %s entity %u\n", info.entity,
1715 			  info.selector, dev->udev->devpath, ctrl->entity->id);
1716 
1717 	return ret;
1718 }
1719 
1720 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1721 	struct uvc_xu_control_query *xqry)
1722 {
1723 	struct uvc_entity *entity;
1724 	struct uvc_control *ctrl;
1725 	unsigned int i, found = 0;
1726 	__u32 reqflags;
1727 	__u16 size;
1728 	__u8 *data = NULL;
1729 	int ret;
1730 
1731 	/* Find the extension unit. */
1732 	list_for_each_entry(entity, &chain->entities, chain) {
1733 		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1734 		    entity->id == xqry->unit)
1735 			break;
1736 	}
1737 
1738 	if (entity->id != xqry->unit) {
1739 		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1740 			xqry->unit);
1741 		return -ENOENT;
1742 	}
1743 
1744 	/* Find the control and perform delayed initialization if needed. */
1745 	for (i = 0; i < entity->ncontrols; ++i) {
1746 		ctrl = &entity->controls[i];
1747 		if (ctrl->index == xqry->selector - 1) {
1748 			found = 1;
1749 			break;
1750 		}
1751 	}
1752 
1753 	if (!found) {
1754 		uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1755 			entity->extension.guidExtensionCode, xqry->selector);
1756 		return -ENOENT;
1757 	}
1758 
1759 	if (mutex_lock_interruptible(&chain->ctrl_mutex))
1760 		return -ERESTARTSYS;
1761 
1762 	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1763 	if (ret < 0) {
1764 		ret = -ENOENT;
1765 		goto done;
1766 	}
1767 
1768 	/* Validate the required buffer size and flags for the request */
1769 	reqflags = 0;
1770 	size = ctrl->info.size;
1771 
1772 	switch (xqry->query) {
1773 	case UVC_GET_CUR:
1774 		reqflags = UVC_CTRL_FLAG_GET_CUR;
1775 		break;
1776 	case UVC_GET_MIN:
1777 		reqflags = UVC_CTRL_FLAG_GET_MIN;
1778 		break;
1779 	case UVC_GET_MAX:
1780 		reqflags = UVC_CTRL_FLAG_GET_MAX;
1781 		break;
1782 	case UVC_GET_DEF:
1783 		reqflags = UVC_CTRL_FLAG_GET_DEF;
1784 		break;
1785 	case UVC_GET_RES:
1786 		reqflags = UVC_CTRL_FLAG_GET_RES;
1787 		break;
1788 	case UVC_SET_CUR:
1789 		reqflags = UVC_CTRL_FLAG_SET_CUR;
1790 		break;
1791 	case UVC_GET_LEN:
1792 		size = 2;
1793 		break;
1794 	case UVC_GET_INFO:
1795 		size = 1;
1796 		break;
1797 	default:
1798 		ret = -EINVAL;
1799 		goto done;
1800 	}
1801 
1802 	if (size != xqry->size) {
1803 		ret = -ENOBUFS;
1804 		goto done;
1805 	}
1806 
1807 	if (reqflags && !(ctrl->info.flags & reqflags)) {
1808 		ret = -EBADRQC;
1809 		goto done;
1810 	}
1811 
1812 	data = kmalloc(size, GFP_KERNEL);
1813 	if (data == NULL) {
1814 		ret = -ENOMEM;
1815 		goto done;
1816 	}
1817 
1818 	if (xqry->query == UVC_SET_CUR &&
1819 	    copy_from_user(data, xqry->data, size)) {
1820 		ret = -EFAULT;
1821 		goto done;
1822 	}
1823 
1824 	ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1825 			     chain->dev->intfnum, xqry->selector, data, size);
1826 	if (ret < 0)
1827 		goto done;
1828 
1829 	if (xqry->query != UVC_SET_CUR &&
1830 	    copy_to_user(xqry->data, data, size))
1831 		ret = -EFAULT;
1832 done:
1833 	kfree(data);
1834 	mutex_unlock(&chain->ctrl_mutex);
1835 	return ret;
1836 }
1837 
1838 /* --------------------------------------------------------------------------
1839  * Suspend/resume
1840  */
1841 
1842 /*
1843  * Restore control values after resume, skipping controls that haven't been
1844  * changed.
1845  *
1846  * TODO
1847  * - Don't restore modified controls that are back to their default value.
1848  * - Handle restore order (Auto-Exposure Mode should be restored before
1849  *   Exposure Time).
1850  */
1851 int uvc_ctrl_restore_values(struct uvc_device *dev)
1852 {
1853 	struct uvc_control *ctrl;
1854 	struct uvc_entity *entity;
1855 	unsigned int i;
1856 	int ret;
1857 
1858 	/* Walk the entities list and restore controls when possible. */
1859 	list_for_each_entry(entity, &dev->entities, list) {
1860 
1861 		for (i = 0; i < entity->ncontrols; ++i) {
1862 			ctrl = &entity->controls[i];
1863 
1864 			if (!ctrl->initialized || !ctrl->modified ||
1865 			    (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1866 				continue;
1867 
1868 			printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1869 				ctrl->info.entity, ctrl->info.index,
1870 				ctrl->info.selector);
1871 			ctrl->dirty = 1;
1872 		}
1873 
1874 		ret = uvc_ctrl_commit_entity(dev, entity, 0);
1875 		if (ret < 0)
1876 			return ret;
1877 	}
1878 
1879 	return 0;
1880 }
1881 
1882 /* --------------------------------------------------------------------------
1883  * Control and mapping handling
1884  */
1885 
1886 /*
1887  * Add control information to a given control.
1888  */
1889 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1890 	const struct uvc_control_info *info)
1891 {
1892 	int ret = 0;
1893 
1894 	ctrl->info = *info;
1895 	INIT_LIST_HEAD(&ctrl->info.mappings);
1896 
1897 	/* Allocate an array to save control values (cur, def, max, etc.) */
1898 	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1899 				 GFP_KERNEL);
1900 	if (ctrl->uvc_data == NULL) {
1901 		ret = -ENOMEM;
1902 		goto done;
1903 	}
1904 
1905 	ctrl->initialized = 1;
1906 
1907 	uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1908 		"entity %u\n", ctrl->info.entity, ctrl->info.selector,
1909 		dev->udev->devpath, ctrl->entity->id);
1910 
1911 done:
1912 	if (ret < 0)
1913 		kfree(ctrl->uvc_data);
1914 	return ret;
1915 }
1916 
1917 /*
1918  * Add a control mapping to a given control.
1919  */
1920 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1921 	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1922 {
1923 	struct uvc_control_mapping *map;
1924 	unsigned int size;
1925 
1926 	/* Most mappings come from static kernel data and need to be duplicated.
1927 	 * Mappings that come from userspace will be unnecessarily duplicated,
1928 	 * this could be optimized.
1929 	 */
1930 	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1931 	if (map == NULL)
1932 		return -ENOMEM;
1933 
1934 	INIT_LIST_HEAD(&map->ev_subs);
1935 
1936 	size = sizeof(*mapping->menu_info) * mapping->menu_count;
1937 	map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1938 	if (map->menu_info == NULL) {
1939 		kfree(map);
1940 		return -ENOMEM;
1941 	}
1942 
1943 	if (map->get == NULL)
1944 		map->get = uvc_get_le_value;
1945 	if (map->set == NULL)
1946 		map->set = uvc_set_le_value;
1947 
1948 	list_add_tail(&map->list, &ctrl->info.mappings);
1949 	uvc_trace(UVC_TRACE_CONTROL,
1950 		"Adding mapping '%s' to control %pUl/%u.\n",
1951 		map->name, ctrl->info.entity, ctrl->info.selector);
1952 
1953 	return 0;
1954 }
1955 
1956 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1957 	const struct uvc_control_mapping *mapping)
1958 {
1959 	struct uvc_device *dev = chain->dev;
1960 	struct uvc_control_mapping *map;
1961 	struct uvc_entity *entity;
1962 	struct uvc_control *ctrl;
1963 	int found = 0;
1964 	int ret;
1965 
1966 	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1967 		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1968 			"id 0x%08x is invalid.\n", mapping->name,
1969 			mapping->id);
1970 		return -EINVAL;
1971 	}
1972 
1973 	/* Search for the matching (GUID/CS) control on the current chain */
1974 	list_for_each_entry(entity, &chain->entities, chain) {
1975 		unsigned int i;
1976 
1977 		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1978 		    !uvc_entity_match_guid(entity, mapping->entity))
1979 			continue;
1980 
1981 		for (i = 0; i < entity->ncontrols; ++i) {
1982 			ctrl = &entity->controls[i];
1983 			if (ctrl->index == mapping->selector - 1) {
1984 				found = 1;
1985 				break;
1986 			}
1987 		}
1988 
1989 		if (found)
1990 			break;
1991 	}
1992 	if (!found)
1993 		return -ENOENT;
1994 
1995 	if (mutex_lock_interruptible(&chain->ctrl_mutex))
1996 		return -ERESTARTSYS;
1997 
1998 	/* Perform delayed initialization of XU controls */
1999 	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2000 	if (ret < 0) {
2001 		ret = -ENOENT;
2002 		goto done;
2003 	}
2004 
2005 	list_for_each_entry(map, &ctrl->info.mappings, list) {
2006 		if (mapping->id == map->id) {
2007 			uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
2008 				"control id 0x%08x already exists.\n",
2009 				mapping->name, mapping->id);
2010 			ret = -EEXIST;
2011 			goto done;
2012 		}
2013 	}
2014 
2015 	/* Prevent excess memory consumption */
2016 	if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2017 		atomic_dec(&dev->nmappings);
2018 		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
2019 			"mappings count (%u) exceeded.\n", mapping->name,
2020 			UVC_MAX_CONTROL_MAPPINGS);
2021 		ret = -ENOMEM;
2022 		goto done;
2023 	}
2024 
2025 	ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2026 	if (ret < 0)
2027 		atomic_dec(&dev->nmappings);
2028 
2029 done:
2030 	mutex_unlock(&chain->ctrl_mutex);
2031 	return ret;
2032 }
2033 
2034 /*
2035  * Prune an entity of its bogus controls using a blacklist. Bogus controls
2036  * are currently the ones that crash the camera or unconditionally return an
2037  * error when queried.
2038  */
2039 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2040 	struct uvc_entity *entity)
2041 {
2042 	struct uvc_ctrl_blacklist {
2043 		struct usb_device_id id;
2044 		u8 index;
2045 	};
2046 
2047 	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2048 		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2049 		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2050 		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2051 	};
2052 	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2053 		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2054 	};
2055 
2056 	const struct uvc_ctrl_blacklist *blacklist;
2057 	unsigned int size;
2058 	unsigned int count;
2059 	unsigned int i;
2060 	u8 *controls;
2061 
2062 	switch (UVC_ENTITY_TYPE(entity)) {
2063 	case UVC_VC_PROCESSING_UNIT:
2064 		blacklist = processing_blacklist;
2065 		count = ARRAY_SIZE(processing_blacklist);
2066 		controls = entity->processing.bmControls;
2067 		size = entity->processing.bControlSize;
2068 		break;
2069 
2070 	case UVC_ITT_CAMERA:
2071 		blacklist = camera_blacklist;
2072 		count = ARRAY_SIZE(camera_blacklist);
2073 		controls = entity->camera.bmControls;
2074 		size = entity->camera.bControlSize;
2075 		break;
2076 
2077 	default:
2078 		return;
2079 	}
2080 
2081 	for (i = 0; i < count; ++i) {
2082 		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2083 			continue;
2084 
2085 		if (blacklist[i].index >= 8 * size ||
2086 		    !uvc_test_bit(controls, blacklist[i].index))
2087 			continue;
2088 
2089 		uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
2090 			"removing it.\n", entity->id, blacklist[i].index);
2091 
2092 		uvc_clear_bit(controls, blacklist[i].index);
2093 	}
2094 }
2095 
2096 /*
2097  * Add control information and hardcoded stock control mappings to the given
2098  * device.
2099  */
2100 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2101 {
2102 	const struct uvc_control_info *info = uvc_ctrls;
2103 	const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2104 	const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2105 	const struct uvc_control_mapping *mend =
2106 		mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2107 
2108 	/* XU controls initialization requires querying the device for control
2109 	 * information. As some buggy UVC devices will crash when queried
2110 	 * repeatedly in a tight loop, delay XU controls initialization until
2111 	 * first use.
2112 	 */
2113 	if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2114 		return;
2115 
2116 	for (; info < iend; ++info) {
2117 		if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2118 		    ctrl->index == info->index) {
2119 			uvc_ctrl_add_info(dev, ctrl, info);
2120 			break;
2121 		 }
2122 	}
2123 
2124 	if (!ctrl->initialized)
2125 		return;
2126 
2127 	for (; mapping < mend; ++mapping) {
2128 		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2129 		    ctrl->info.selector == mapping->selector)
2130 			__uvc_ctrl_add_mapping(dev, ctrl, mapping);
2131 	}
2132 }
2133 
2134 /*
2135  * Initialize device controls.
2136  */
2137 int uvc_ctrl_init_device(struct uvc_device *dev)
2138 {
2139 	struct uvc_entity *entity;
2140 	unsigned int i;
2141 
2142 	/* Walk the entities list and instantiate controls */
2143 	list_for_each_entry(entity, &dev->entities, list) {
2144 		struct uvc_control *ctrl;
2145 		unsigned int bControlSize = 0, ncontrols;
2146 		__u8 *bmControls = NULL;
2147 
2148 		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2149 			bmControls = entity->extension.bmControls;
2150 			bControlSize = entity->extension.bControlSize;
2151 		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2152 			bmControls = entity->processing.bmControls;
2153 			bControlSize = entity->processing.bControlSize;
2154 		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2155 			bmControls = entity->camera.bmControls;
2156 			bControlSize = entity->camera.bControlSize;
2157 		}
2158 
2159 		/* Remove bogus/blacklisted controls */
2160 		uvc_ctrl_prune_entity(dev, entity);
2161 
2162 		/* Count supported controls and allocate the controls array */
2163 		ncontrols = memweight(bmControls, bControlSize);
2164 		if (ncontrols == 0)
2165 			continue;
2166 
2167 		entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2168 					   GFP_KERNEL);
2169 		if (entity->controls == NULL)
2170 			return -ENOMEM;
2171 		entity->ncontrols = ncontrols;
2172 
2173 		/* Initialize all supported controls */
2174 		ctrl = entity->controls;
2175 		for (i = 0; i < bControlSize * 8; ++i) {
2176 			if (uvc_test_bit(bmControls, i) == 0)
2177 				continue;
2178 
2179 			ctrl->entity = entity;
2180 			ctrl->index = i;
2181 
2182 			uvc_ctrl_init_ctrl(dev, ctrl);
2183 			ctrl++;
2184 		}
2185 	}
2186 
2187 	return 0;
2188 }
2189 
2190 /*
2191  * Cleanup device controls.
2192  */
2193 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2194 	struct uvc_control *ctrl)
2195 {
2196 	struct uvc_control_mapping *mapping, *nm;
2197 
2198 	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2199 		list_del(&mapping->list);
2200 		kfree(mapping->menu_info);
2201 		kfree(mapping);
2202 	}
2203 }
2204 
2205 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2206 {
2207 	struct uvc_entity *entity;
2208 	unsigned int i;
2209 
2210 	/* Free controls and control mappings for all entities. */
2211 	list_for_each_entry(entity, &dev->entities, list) {
2212 		for (i = 0; i < entity->ncontrols; ++i) {
2213 			struct uvc_control *ctrl = &entity->controls[i];
2214 
2215 			if (!ctrl->initialized)
2216 				continue;
2217 
2218 			uvc_ctrl_cleanup_mappings(dev, ctrl);
2219 			kfree(ctrl->uvc_data);
2220 		}
2221 
2222 		kfree(entity->controls);
2223 	}
2224 }
2225