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