xref: /openbmc/linux/drivers/media/usb/uvc/uvc_ctrl.c (revision addee42a)
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 /*
1594  * Retrieve flags for a given control
1595  */
1596 static int uvc_ctrl_get_flags(struct uvc_device *dev,
1597 			      const struct uvc_control *ctrl,
1598 			      struct uvc_control_info *info)
1599 {
1600 	u8 *data;
1601 	int ret;
1602 
1603 	data = kmalloc(1, GFP_KERNEL);
1604 	if (data == NULL)
1605 		return -ENOMEM;
1606 
1607 	ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1608 			     info->selector, data, 1);
1609 	if (!ret)
1610 		info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1611 			    | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1612 			    | (data[0] & UVC_CONTROL_CAP_GET ?
1613 			       UVC_CTRL_FLAG_GET_CUR : 0)
1614 			    | (data[0] & UVC_CONTROL_CAP_SET ?
1615 			       UVC_CTRL_FLAG_SET_CUR : 0)
1616 			    | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1617 			       UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1618 
1619 	kfree(data);
1620 	return ret;
1621 }
1622 
1623 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1624 	const struct uvc_control *ctrl, struct uvc_control_info *info)
1625 {
1626 	struct uvc_ctrl_fixup {
1627 		struct usb_device_id id;
1628 		u8 entity;
1629 		u8 selector;
1630 		u8 flags;
1631 	};
1632 
1633 	static const struct uvc_ctrl_fixup fixups[] = {
1634 		{ { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1635 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1636 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1637 			UVC_CTRL_FLAG_AUTO_UPDATE },
1638 		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1639 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1640 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1641 			UVC_CTRL_FLAG_AUTO_UPDATE },
1642 		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1643 			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1644 			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1645 			UVC_CTRL_FLAG_AUTO_UPDATE },
1646 	};
1647 
1648 	unsigned int i;
1649 
1650 	for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1651 		if (!usb_match_one_id(dev->intf, &fixups[i].id))
1652 			continue;
1653 
1654 		if (fixups[i].entity == ctrl->entity->id &&
1655 		    fixups[i].selector == info->selector) {
1656 			info->flags = fixups[i].flags;
1657 			return;
1658 		}
1659 	}
1660 }
1661 
1662 /*
1663  * Query control information (size and flags) for XU controls.
1664  */
1665 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1666 	const struct uvc_control *ctrl, struct uvc_control_info *info)
1667 {
1668 	u8 *data;
1669 	int ret;
1670 
1671 	data = kmalloc(2, GFP_KERNEL);
1672 	if (data == NULL)
1673 		return -ENOMEM;
1674 
1675 	memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1676 	       sizeof(info->entity));
1677 	info->index = ctrl->index;
1678 	info->selector = ctrl->index + 1;
1679 
1680 	/* Query and verify the control length (GET_LEN) */
1681 	ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1682 			     info->selector, data, 2);
1683 	if (ret < 0) {
1684 		uvc_trace(UVC_TRACE_CONTROL,
1685 			  "GET_LEN failed on control %pUl/%u (%d).\n",
1686 			   info->entity, info->selector, ret);
1687 		goto done;
1688 	}
1689 
1690 	info->size = le16_to_cpup((__le16 *)data);
1691 
1692 	ret = uvc_ctrl_get_flags(dev, ctrl, info);
1693 	if (ret < 0) {
1694 		uvc_trace(UVC_TRACE_CONTROL,
1695 			  "Failed to get flags for control %pUl/%u (%d).\n",
1696 			  info->entity, info->selector, ret);
1697 		goto done;
1698 	}
1699 
1700 	uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1701 
1702 	uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1703 		  "flags { get %u set %u auto %u }.\n",
1704 		  info->entity, info->selector, info->size,
1705 		  (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1706 		  (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1707 		  (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1708 
1709 done:
1710 	kfree(data);
1711 	return ret;
1712 }
1713 
1714 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1715 	const struct uvc_control_info *info);
1716 
1717 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1718 	struct uvc_control *ctrl)
1719 {
1720 	struct uvc_control_info info;
1721 	int ret;
1722 
1723 	if (ctrl->initialized)
1724 		return 0;
1725 
1726 	ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1727 	if (ret < 0)
1728 		return ret;
1729 
1730 	ret = uvc_ctrl_add_info(dev, ctrl, &info);
1731 	if (ret < 0)
1732 		uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1733 			  "%pUl/%u on device %s entity %u\n", info.entity,
1734 			  info.selector, dev->udev->devpath, ctrl->entity->id);
1735 
1736 	return ret;
1737 }
1738 
1739 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1740 	struct uvc_xu_control_query *xqry)
1741 {
1742 	struct uvc_entity *entity;
1743 	struct uvc_control *ctrl;
1744 	unsigned int i, found = 0;
1745 	u32 reqflags;
1746 	u16 size;
1747 	u8 *data = NULL;
1748 	int ret;
1749 
1750 	/* Find the extension unit. */
1751 	list_for_each_entry(entity, &chain->entities, chain) {
1752 		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1753 		    entity->id == xqry->unit)
1754 			break;
1755 	}
1756 
1757 	if (entity->id != xqry->unit) {
1758 		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1759 			xqry->unit);
1760 		return -ENOENT;
1761 	}
1762 
1763 	/* Find the control and perform delayed initialization if needed. */
1764 	for (i = 0; i < entity->ncontrols; ++i) {
1765 		ctrl = &entity->controls[i];
1766 		if (ctrl->index == xqry->selector - 1) {
1767 			found = 1;
1768 			break;
1769 		}
1770 	}
1771 
1772 	if (!found) {
1773 		uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1774 			entity->extension.guidExtensionCode, xqry->selector);
1775 		return -ENOENT;
1776 	}
1777 
1778 	if (mutex_lock_interruptible(&chain->ctrl_mutex))
1779 		return -ERESTARTSYS;
1780 
1781 	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1782 	if (ret < 0) {
1783 		ret = -ENOENT;
1784 		goto done;
1785 	}
1786 
1787 	/* Validate the required buffer size and flags for the request */
1788 	reqflags = 0;
1789 	size = ctrl->info.size;
1790 
1791 	switch (xqry->query) {
1792 	case UVC_GET_CUR:
1793 		reqflags = UVC_CTRL_FLAG_GET_CUR;
1794 		break;
1795 	case UVC_GET_MIN:
1796 		reqflags = UVC_CTRL_FLAG_GET_MIN;
1797 		break;
1798 	case UVC_GET_MAX:
1799 		reqflags = UVC_CTRL_FLAG_GET_MAX;
1800 		break;
1801 	case UVC_GET_DEF:
1802 		reqflags = UVC_CTRL_FLAG_GET_DEF;
1803 		break;
1804 	case UVC_GET_RES:
1805 		reqflags = UVC_CTRL_FLAG_GET_RES;
1806 		break;
1807 	case UVC_SET_CUR:
1808 		reqflags = UVC_CTRL_FLAG_SET_CUR;
1809 		break;
1810 	case UVC_GET_LEN:
1811 		size = 2;
1812 		break;
1813 	case UVC_GET_INFO:
1814 		size = 1;
1815 		break;
1816 	default:
1817 		ret = -EINVAL;
1818 		goto done;
1819 	}
1820 
1821 	if (size != xqry->size) {
1822 		ret = -ENOBUFS;
1823 		goto done;
1824 	}
1825 
1826 	if (reqflags && !(ctrl->info.flags & reqflags)) {
1827 		ret = -EBADRQC;
1828 		goto done;
1829 	}
1830 
1831 	data = kmalloc(size, GFP_KERNEL);
1832 	if (data == NULL) {
1833 		ret = -ENOMEM;
1834 		goto done;
1835 	}
1836 
1837 	if (xqry->query == UVC_SET_CUR &&
1838 	    copy_from_user(data, xqry->data, size)) {
1839 		ret = -EFAULT;
1840 		goto done;
1841 	}
1842 
1843 	ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1844 			     chain->dev->intfnum, xqry->selector, data, size);
1845 	if (ret < 0)
1846 		goto done;
1847 
1848 	if (xqry->query != UVC_SET_CUR &&
1849 	    copy_to_user(xqry->data, data, size))
1850 		ret = -EFAULT;
1851 done:
1852 	kfree(data);
1853 	mutex_unlock(&chain->ctrl_mutex);
1854 	return ret;
1855 }
1856 
1857 /* --------------------------------------------------------------------------
1858  * Suspend/resume
1859  */
1860 
1861 /*
1862  * Restore control values after resume, skipping controls that haven't been
1863  * changed.
1864  *
1865  * TODO
1866  * - Don't restore modified controls that are back to their default value.
1867  * - Handle restore order (Auto-Exposure Mode should be restored before
1868  *   Exposure Time).
1869  */
1870 int uvc_ctrl_restore_values(struct uvc_device *dev)
1871 {
1872 	struct uvc_control *ctrl;
1873 	struct uvc_entity *entity;
1874 	unsigned int i;
1875 	int ret;
1876 
1877 	/* Walk the entities list and restore controls when possible. */
1878 	list_for_each_entry(entity, &dev->entities, list) {
1879 
1880 		for (i = 0; i < entity->ncontrols; ++i) {
1881 			ctrl = &entity->controls[i];
1882 
1883 			if (!ctrl->initialized || !ctrl->modified ||
1884 			    (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1885 				continue;
1886 
1887 			printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1888 				ctrl->info.entity, ctrl->info.index,
1889 				ctrl->info.selector);
1890 			ctrl->dirty = 1;
1891 		}
1892 
1893 		ret = uvc_ctrl_commit_entity(dev, entity, 0);
1894 		if (ret < 0)
1895 			return ret;
1896 	}
1897 
1898 	return 0;
1899 }
1900 
1901 /* --------------------------------------------------------------------------
1902  * Control and mapping handling
1903  */
1904 
1905 /*
1906  * Add control information to a given control.
1907  */
1908 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1909 	const struct uvc_control_info *info)
1910 {
1911 	int ret = 0;
1912 
1913 	ctrl->info = *info;
1914 	INIT_LIST_HEAD(&ctrl->info.mappings);
1915 
1916 	/* Allocate an array to save control values (cur, def, max, etc.) */
1917 	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1918 				 GFP_KERNEL);
1919 	if (ctrl->uvc_data == NULL) {
1920 		ret = -ENOMEM;
1921 		goto done;
1922 	}
1923 
1924 	/*
1925 	 * Retrieve control flags from the device. Ignore errors and work with
1926 	 * default flag values from the uvc_ctrl array when the device doesn't
1927 	 * properly implement GET_INFO on standard controls.
1928 	 */
1929 	uvc_ctrl_get_flags(dev, ctrl, &ctrl->info);
1930 
1931 	ctrl->initialized = 1;
1932 
1933 	uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1934 		"entity %u\n", ctrl->info.entity, ctrl->info.selector,
1935 		dev->udev->devpath, ctrl->entity->id);
1936 
1937 done:
1938 	if (ret < 0)
1939 		kfree(ctrl->uvc_data);
1940 	return ret;
1941 }
1942 
1943 /*
1944  * Add a control mapping to a given control.
1945  */
1946 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1947 	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1948 {
1949 	struct uvc_control_mapping *map;
1950 	unsigned int size;
1951 
1952 	/* Most mappings come from static kernel data and need to be duplicated.
1953 	 * Mappings that come from userspace will be unnecessarily duplicated,
1954 	 * this could be optimized.
1955 	 */
1956 	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1957 	if (map == NULL)
1958 		return -ENOMEM;
1959 
1960 	INIT_LIST_HEAD(&map->ev_subs);
1961 
1962 	size = sizeof(*mapping->menu_info) * mapping->menu_count;
1963 	map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1964 	if (map->menu_info == NULL) {
1965 		kfree(map);
1966 		return -ENOMEM;
1967 	}
1968 
1969 	if (map->get == NULL)
1970 		map->get = uvc_get_le_value;
1971 	if (map->set == NULL)
1972 		map->set = uvc_set_le_value;
1973 
1974 	list_add_tail(&map->list, &ctrl->info.mappings);
1975 	uvc_trace(UVC_TRACE_CONTROL,
1976 		"Adding mapping '%s' to control %pUl/%u.\n",
1977 		map->name, ctrl->info.entity, ctrl->info.selector);
1978 
1979 	return 0;
1980 }
1981 
1982 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1983 	const struct uvc_control_mapping *mapping)
1984 {
1985 	struct uvc_device *dev = chain->dev;
1986 	struct uvc_control_mapping *map;
1987 	struct uvc_entity *entity;
1988 	struct uvc_control *ctrl;
1989 	int found = 0;
1990 	int ret;
1991 
1992 	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1993 		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1994 			"id 0x%08x is invalid.\n", mapping->name,
1995 			mapping->id);
1996 		return -EINVAL;
1997 	}
1998 
1999 	/* Search for the matching (GUID/CS) control on the current chain */
2000 	list_for_each_entry(entity, &chain->entities, chain) {
2001 		unsigned int i;
2002 
2003 		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2004 		    !uvc_entity_match_guid(entity, mapping->entity))
2005 			continue;
2006 
2007 		for (i = 0; i < entity->ncontrols; ++i) {
2008 			ctrl = &entity->controls[i];
2009 			if (ctrl->index == mapping->selector - 1) {
2010 				found = 1;
2011 				break;
2012 			}
2013 		}
2014 
2015 		if (found)
2016 			break;
2017 	}
2018 	if (!found)
2019 		return -ENOENT;
2020 
2021 	if (mutex_lock_interruptible(&chain->ctrl_mutex))
2022 		return -ERESTARTSYS;
2023 
2024 	/* Perform delayed initialization of XU controls */
2025 	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2026 	if (ret < 0) {
2027 		ret = -ENOENT;
2028 		goto done;
2029 	}
2030 
2031 	/* Validate the user-provided bit-size and offset */
2032 	if (mapping->size > 32 ||
2033 	    mapping->offset + mapping->size > ctrl->info.size * 8) {
2034 		ret = -EINVAL;
2035 		goto done;
2036 	}
2037 
2038 	list_for_each_entry(map, &ctrl->info.mappings, list) {
2039 		if (mapping->id == map->id) {
2040 			uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
2041 				"control id 0x%08x already exists.\n",
2042 				mapping->name, mapping->id);
2043 			ret = -EEXIST;
2044 			goto done;
2045 		}
2046 	}
2047 
2048 	/* Prevent excess memory consumption */
2049 	if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2050 		atomic_dec(&dev->nmappings);
2051 		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
2052 			"mappings count (%u) exceeded.\n", mapping->name,
2053 			UVC_MAX_CONTROL_MAPPINGS);
2054 		ret = -ENOMEM;
2055 		goto done;
2056 	}
2057 
2058 	ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2059 	if (ret < 0)
2060 		atomic_dec(&dev->nmappings);
2061 
2062 done:
2063 	mutex_unlock(&chain->ctrl_mutex);
2064 	return ret;
2065 }
2066 
2067 /*
2068  * Prune an entity of its bogus controls using a blacklist. Bogus controls
2069  * are currently the ones that crash the camera or unconditionally return an
2070  * error when queried.
2071  */
2072 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2073 	struct uvc_entity *entity)
2074 {
2075 	struct uvc_ctrl_blacklist {
2076 		struct usb_device_id id;
2077 		u8 index;
2078 	};
2079 
2080 	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2081 		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2082 		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2083 		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2084 	};
2085 	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2086 		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2087 	};
2088 
2089 	const struct uvc_ctrl_blacklist *blacklist;
2090 	unsigned int size;
2091 	unsigned int count;
2092 	unsigned int i;
2093 	u8 *controls;
2094 
2095 	switch (UVC_ENTITY_TYPE(entity)) {
2096 	case UVC_VC_PROCESSING_UNIT:
2097 		blacklist = processing_blacklist;
2098 		count = ARRAY_SIZE(processing_blacklist);
2099 		controls = entity->processing.bmControls;
2100 		size = entity->processing.bControlSize;
2101 		break;
2102 
2103 	case UVC_ITT_CAMERA:
2104 		blacklist = camera_blacklist;
2105 		count = ARRAY_SIZE(camera_blacklist);
2106 		controls = entity->camera.bmControls;
2107 		size = entity->camera.bControlSize;
2108 		break;
2109 
2110 	default:
2111 		return;
2112 	}
2113 
2114 	for (i = 0; i < count; ++i) {
2115 		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2116 			continue;
2117 
2118 		if (blacklist[i].index >= 8 * size ||
2119 		    !uvc_test_bit(controls, blacklist[i].index))
2120 			continue;
2121 
2122 		uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
2123 			"removing it.\n", entity->id, blacklist[i].index);
2124 
2125 		uvc_clear_bit(controls, blacklist[i].index);
2126 	}
2127 }
2128 
2129 /*
2130  * Add control information and hardcoded stock control mappings to the given
2131  * device.
2132  */
2133 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2134 {
2135 	const struct uvc_control_info *info = uvc_ctrls;
2136 	const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2137 	const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2138 	const struct uvc_control_mapping *mend =
2139 		mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2140 
2141 	/* XU controls initialization requires querying the device for control
2142 	 * information. As some buggy UVC devices will crash when queried
2143 	 * repeatedly in a tight loop, delay XU controls initialization until
2144 	 * first use.
2145 	 */
2146 	if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2147 		return;
2148 
2149 	for (; info < iend; ++info) {
2150 		if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2151 		    ctrl->index == info->index) {
2152 			uvc_ctrl_add_info(dev, ctrl, info);
2153 			break;
2154 		 }
2155 	}
2156 
2157 	if (!ctrl->initialized)
2158 		return;
2159 
2160 	for (; mapping < mend; ++mapping) {
2161 		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2162 		    ctrl->info.selector == mapping->selector)
2163 			__uvc_ctrl_add_mapping(dev, ctrl, mapping);
2164 	}
2165 }
2166 
2167 /*
2168  * Initialize device controls.
2169  */
2170 int uvc_ctrl_init_device(struct uvc_device *dev)
2171 {
2172 	struct uvc_entity *entity;
2173 	unsigned int i;
2174 
2175 	/* Walk the entities list and instantiate controls */
2176 	list_for_each_entry(entity, &dev->entities, list) {
2177 		struct uvc_control *ctrl;
2178 		unsigned int bControlSize = 0, ncontrols;
2179 		u8 *bmControls = NULL;
2180 
2181 		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2182 			bmControls = entity->extension.bmControls;
2183 			bControlSize = entity->extension.bControlSize;
2184 		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2185 			bmControls = entity->processing.bmControls;
2186 			bControlSize = entity->processing.bControlSize;
2187 		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2188 			bmControls = entity->camera.bmControls;
2189 			bControlSize = entity->camera.bControlSize;
2190 		}
2191 
2192 		/* Remove bogus/blacklisted controls */
2193 		uvc_ctrl_prune_entity(dev, entity);
2194 
2195 		/* Count supported controls and allocate the controls array */
2196 		ncontrols = memweight(bmControls, bControlSize);
2197 		if (ncontrols == 0)
2198 			continue;
2199 
2200 		entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2201 					   GFP_KERNEL);
2202 		if (entity->controls == NULL)
2203 			return -ENOMEM;
2204 		entity->ncontrols = ncontrols;
2205 
2206 		/* Initialize all supported controls */
2207 		ctrl = entity->controls;
2208 		for (i = 0; i < bControlSize * 8; ++i) {
2209 			if (uvc_test_bit(bmControls, i) == 0)
2210 				continue;
2211 
2212 			ctrl->entity = entity;
2213 			ctrl->index = i;
2214 
2215 			uvc_ctrl_init_ctrl(dev, ctrl);
2216 			ctrl++;
2217 		}
2218 	}
2219 
2220 	return 0;
2221 }
2222 
2223 /*
2224  * Cleanup device controls.
2225  */
2226 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2227 	struct uvc_control *ctrl)
2228 {
2229 	struct uvc_control_mapping *mapping, *nm;
2230 
2231 	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2232 		list_del(&mapping->list);
2233 		kfree(mapping->menu_info);
2234 		kfree(mapping);
2235 	}
2236 }
2237 
2238 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2239 {
2240 	struct uvc_entity *entity;
2241 	unsigned int i;
2242 
2243 	/* Free controls and control mappings for all entities. */
2244 	list_for_each_entry(entity, &dev->entities, list) {
2245 		for (i = 0; i < entity->ncontrols; ++i) {
2246 			struct uvc_control *ctrl = &entity->controls[i];
2247 
2248 			if (!ctrl->initialized)
2249 				continue;
2250 
2251 			uvc_ctrl_cleanup_mappings(dev, ctrl);
2252 			kfree(ctrl->uvc_data);
2253 		}
2254 
2255 		kfree(entity->controls);
2256 	}
2257 }
2258