uvc_ctrl.c 46.6 KB
Newer Older
1 2 3
/*
 *      uvc_ctrl.c  --  USB Video Class driver - Controls
 *
4 5
 *      Copyright (C) 2005-2010
 *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 7 8 9 10 11 12 13 14 15 16
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
17
#include <linux/slab.h>
18 19 20 21 22 23 24 25 26 27 28
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/videodev2.h>
#include <linux/vmalloc.h>
#include <linux/wait.h>
#include <asm/atomic.h>

#include "uvcvideo.h"

#define UVC_CTRL_DATA_CURRENT	0
#define UVC_CTRL_DATA_BACKUP	1
29 30 31 32 33
#define UVC_CTRL_DATA_MIN	2
#define UVC_CTRL_DATA_MAX	3
#define UVC_CTRL_DATA_RES	4
#define UVC_CTRL_DATA_DEF	5
#define UVC_CTRL_DATA_LAST	6
34 35

/* ------------------------------------------------------------------------
36
 * Controls
37 38 39 40 41
 */

static struct uvc_control_info uvc_ctrls[] = {
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
42
		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
43 44 45 46 47 48 49
		.index		= 0,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
50
		.selector	= UVC_PU_CONTRAST_CONTROL,
51 52 53 54 55 56 57
		.index		= 1,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
58
		.selector	= UVC_PU_HUE_CONTROL,
59 60 61 62 63 64 65
		.index		= 2,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
66
		.selector	= UVC_PU_SATURATION_CONTROL,
67 68 69 70 71 72 73
		.index		= 3,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
74
		.selector	= UVC_PU_SHARPNESS_CONTROL,
75 76 77 78 79 80 81
		.index		= 4,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
82
		.selector	= UVC_PU_GAMMA_CONTROL,
83 84 85 86 87
		.index		= 5,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
88 89
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
90
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
91 92 93 94 95 96 97
		.index		= 6,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
98
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
99 100 101 102 103
		.index		= 7,
		.size		= 4,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
104 105
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
106
		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
107 108 109 110 111 112 113
		.index		= 8,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
114
		.selector	= UVC_PU_GAIN_CONTROL,
115 116 117 118 119 120 121
		.index		= 9,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
122
		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
123 124
		.index		= 10,
		.size		= 1,
125 126
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
127 128 129
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
130
		.selector	= UVC_PU_HUE_AUTO_CONTROL,
131 132 133 134 135
		.index		= 11,
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
	},
136 137
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
138
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
139 140 141 142 143 144 145
		.index		= 12,
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
146
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
147 148 149 150 151 152 153
		.index		= 13,
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
154
		.selector	= UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
155 156 157 158 159 160 161
		.index		= 14,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
162
		.selector	= UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
163 164 165 166 167 168 169
		.index		= 15,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
170
		.selector	= UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
171 172 173 174 175 176
		.index		= 16,
		.size		= 1,
		.flags		= UVC_CONTROL_GET_CUR,
	},
	{
		.entity		= UVC_GUID_UVC_PROCESSING,
177
		.selector	= UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
178 179 180 181 182 183
		.index		= 17,
		.size		= 1,
		.flags		= UVC_CONTROL_GET_CUR,
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
184
		.selector	= UVC_CT_SCANNING_MODE_CONTROL,
185 186 187 188 189
		.index		= 0,
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_RESTORE,
	},
190 191
	{
		.entity		= UVC_GUID_UVC_CAMERA,
192
		.selector	= UVC_CT_AE_MODE_CONTROL,
193 194 195 196 197 198 199 200
		.index		= 1,
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
201
		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
202 203 204 205 206 207 208
		.index		= 2,
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_RESTORE,
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
209
		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
210 211 212 213 214
		.index		= 3,
		.size		= 4,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE,
	},
215 216
	{
		.entity		= UVC_GUID_UVC_CAMERA,
217
		.selector	= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
218 219
		.index		= 4,
		.size		= 1,
220
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_RESTORE,
221
	},
222 223
	{
		.entity		= UVC_GUID_UVC_CAMERA,
224
		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
225 226 227 228 229 230 231
		.index		= 5,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
232
		.selector	= UVC_CT_FOCUS_RELATIVE_CONTROL,
233 234
		.index		= 6,
		.size		= 2,
235 236 237
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
				| UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
238 239
	},
	{
240
		.entity		= UVC_GUID_UVC_CAMERA,
241
		.selector	= UVC_CT_IRIS_ABSOLUTE_CONTROL,
242 243 244 245 246 247 248
		.index		= 7,
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
249
		.selector	= UVC_CT_IRIS_RELATIVE_CONTROL,
250
		.index		= 8,
251
		.size		= 1,
252
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_AUTO_UPDATE,
253 254
	},
	{
255
		.entity		= UVC_GUID_UVC_CAMERA,
256
		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
257
		.index		= 9,
258 259 260 261 262
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
	{
263
		.entity		= UVC_GUID_UVC_CAMERA,
264
		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
265 266
		.index		= 10,
		.size		= 3,
267 268 269
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
				| UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
270 271 272
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
273
		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
274 275 276 277 278 279 280
		.index		= 11,
		.size		= 8,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
281
		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
282 283
		.index		= 12,
		.size		= 4,
284 285 286
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
				| UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
287 288 289
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
290
		.selector	= UVC_CT_ROLL_ABSOLUTE_CONTROL,
291
		.index		= 13,
292 293 294 295 296 297
		.size		= 2,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
298
		.selector	= UVC_CT_ROLL_RELATIVE_CONTROL,
299 300
		.index		= 14,
		.size		= 2,
301 302 303
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
				| UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
304 305 306
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
307
		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
308
		.index		= 17,
309 310 311 312 313
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
				| UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
	},
	{
314
		.entity		= UVC_GUID_UVC_CAMERA,
315
		.selector	= UVC_CT_PRIVACY_CONTROL,
316 317 318
		.index		= 18,
		.size		= 1,
		.flags		= UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
319 320 321 322 323 324 325 326 327 328 329 330
				| UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
	},
};

static struct uvc_menu_info power_line_frequency_controls[] = {
	{ 0, "Disabled" },
	{ 1, "50 Hz" },
	{ 2, "60 Hz" },
};

static struct uvc_menu_info exposure_auto_controls[] = {
	{ 2, "Auto Mode" },
331
	{ 1, "Manual Mode" },
332 333 334 335
	{ 4, "Shutter Priority Mode" },
	{ 8, "Aperture Priority Mode" },
};

336 337 338 339 340 341
static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
{
	__s8 zoom = (__s8)data[0];

	switch (query) {
342
	case UVC_GET_CUR:
343 344
		return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);

345 346 347 348
	case UVC_GET_MIN:
	case UVC_GET_MAX:
	case UVC_GET_RES:
	case UVC_GET_DEF:
349 350 351 352 353 354 355 356 357
	default:
		return data[2];
	}
}

static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
	__s32 value, __u8 *data)
{
	data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
358
	data[2] = min((int)abs(value), 0xff);
359 360
}

361 362 363 364 365
static struct uvc_control_mapping uvc_ctrl_mappings[] = {
	{
		.id		= V4L2_CID_BRIGHTNESS,
		.name		= "Brightness",
		.entity		= UVC_GUID_UVC_PROCESSING,
366
		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
367 368 369 370 371 372 373 374 375
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
	},
	{
		.id		= V4L2_CID_CONTRAST,
		.name		= "Contrast",
		.entity		= UVC_GUID_UVC_PROCESSING,
376
		.selector	= UVC_PU_CONTRAST_CONTROL,
377 378 379 380 381 382 383 384 385
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_HUE,
		.name		= "Hue",
		.entity		= UVC_GUID_UVC_PROCESSING,
386
		.selector	= UVC_PU_HUE_CONTROL,
387 388 389 390 391 392 393 394 395
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
	},
	{
		.id		= V4L2_CID_SATURATION,
		.name		= "Saturation",
		.entity		= UVC_GUID_UVC_PROCESSING,
396
		.selector	= UVC_PU_SATURATION_CONTROL,
397 398 399 400 401 402 403 404 405
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_SHARPNESS,
		.name		= "Sharpness",
		.entity		= UVC_GUID_UVC_PROCESSING,
406
		.selector	= UVC_PU_SHARPNESS_CONTROL,
407 408 409 410 411 412 413 414 415
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_GAMMA,
		.name		= "Gamma",
		.entity		= UVC_GUID_UVC_PROCESSING,
416
		.selector	= UVC_PU_GAMMA_CONTROL,
417 418 419 420 421 422 423 424 425
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_BACKLIGHT_COMPENSATION,
		.name		= "Backlight Compensation",
		.entity		= UVC_GUID_UVC_PROCESSING,
426
		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
427 428 429 430 431 432 433 434 435
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_GAIN,
		.name		= "Gain",
		.entity		= UVC_GUID_UVC_PROCESSING,
436
		.selector	= UVC_PU_GAIN_CONTROL,
437 438 439 440 441 442 443 444 445
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_POWER_LINE_FREQUENCY,
		.name		= "Power Line Frequency",
		.entity		= UVC_GUID_UVC_PROCESSING,
446
		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
447 448 449 450 451 452 453 454 455 456 457
		.size		= 2,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
		.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
		.menu_info	= power_line_frequency_controls,
		.menu_count	= ARRAY_SIZE(power_line_frequency_controls),
	},
	{
		.id		= V4L2_CID_HUE_AUTO,
		.name		= "Hue, Auto",
		.entity		= UVC_GUID_UVC_PROCESSING,
458
		.selector	= UVC_PU_HUE_AUTO_CONTROL,
459 460 461 462 463 464 465 466 467
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
	{
		.id		= V4L2_CID_EXPOSURE_AUTO,
		.name		= "Exposure, Auto",
		.entity		= UVC_GUID_UVC_CAMERA,
468
		.selector	= UVC_CT_AE_MODE_CONTROL,
469 470 471 472 473 474 475 476 477 478 479
		.size		= 4,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
		.data_type	= UVC_CTRL_DATA_TYPE_BITMASK,
		.menu_info	= exposure_auto_controls,
		.menu_count	= ARRAY_SIZE(exposure_auto_controls),
	},
	{
		.id		= V4L2_CID_EXPOSURE_AUTO_PRIORITY,
		.name		= "Exposure, Auto Priority",
		.entity		= UVC_GUID_UVC_CAMERA,
480
		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
481 482 483 484 485 486 487 488 489
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
	{
		.id		= V4L2_CID_EXPOSURE_ABSOLUTE,
		.name		= "Exposure (Absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
490
		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
491 492 493 494 495 496 497 498 499
		.size		= 32,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
		.name		= "White Balance Temperature, Auto",
		.entity		= UVC_GUID_UVC_PROCESSING,
500
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
501 502 503 504 505 506 507 508 509
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
	{
		.id		= V4L2_CID_WHITE_BALANCE_TEMPERATURE,
		.name		= "White Balance Temperature",
		.entity		= UVC_GUID_UVC_PROCESSING,
510
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
511 512 513 514 515 516 517 518 519
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
		.name		= "White Balance Component, Auto",
		.entity		= UVC_GUID_UVC_PROCESSING,
520
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
521 522 523 524 525 526 527 528 529
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
	{
		.id		= V4L2_CID_BLUE_BALANCE,
		.name		= "White Balance Blue Component",
		.entity		= UVC_GUID_UVC_PROCESSING,
530
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
531 532 533 534 535 536 537 538 539
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
	},
	{
		.id		= V4L2_CID_RED_BALANCE,
		.name		= "White Balance Red Component",
		.entity		= UVC_GUID_UVC_PROCESSING,
540
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
541 542 543 544 545 546 547 548 549
		.size		= 16,
		.offset		= 16,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
	},
	{
		.id		= V4L2_CID_FOCUS_ABSOLUTE,
		.name		= "Focus (absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
550
		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
551 552 553 554 555 556 557 558 559
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_FOCUS_AUTO,
		.name		= "Focus, Auto",
		.entity		= UVC_GUID_UVC_CAMERA,
560
		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
561 562 563 564 565
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	{
		.id		= V4L2_CID_IRIS_ABSOLUTE,
		.name		= "Iris, Absolute",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_IRIS_ABSOLUTE_CONTROL,
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_IRIS_RELATIVE,
		.name		= "Iris, Relative",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_IRIS_RELATIVE_CONTROL,
		.size		= 8,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
	},
586 587 588 589
	{
		.id		= V4L2_CID_ZOOM_ABSOLUTE,
		.name		= "Zoom, Absolute",
		.entity		= UVC_GUID_UVC_CAMERA,
590
		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
591 592 593 594 595 596 597 598 599
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_ZOOM_CONTINUOUS,
		.name		= "Zoom, Continuous",
		.entity		= UVC_GUID_UVC_CAMERA,
600
		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
601 602 603 604 605 606 607
		.size		= 0,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
		.get		= uvc_ctrl_get_zoom,
		.set		= uvc_ctrl_set_zoom,
	},
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	{
		.id		= V4L2_CID_PAN_ABSOLUTE,
		.name		= "Pan (Absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
		.size		= 32,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_TILT_ABSOLUTE,
		.name		= "Tilt (Absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
		.size		= 32,
		.offset		= 32,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
628 629 630 631
	{
		.id		= V4L2_CID_PRIVACY,
		.name		= "Privacy",
		.entity		= UVC_GUID_UVC_CAMERA,
632
		.selector	= UVC_CT_PRIVACY_CONTROL,
633 634 635 636 637
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
638 639 640 641 642 643 644 645
};

/* ------------------------------------------------------------------------
 * Utility functions
 */

static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
{
646
	return ctrl->uvc_data + id * ctrl->info.size;
647 648
}

649
static inline int uvc_test_bit(const __u8 *data, int bit)
650 651 652 653
{
	return (data[bit >> 3] >> (bit & 7)) & 1;
}

654 655 656 657 658
static inline void uvc_clear_bit(__u8 *data, int bit)
{
	data[bit >> 3] &= ~(1 << (bit & 7));
}

659 660 661 662 663
/* Extract the bit string specified by mapping->offset and mapping->size
 * from the little-endian data stored at 'data' and return the result as
 * a signed 32bit integer. Sign extension will be performed if the mapping
 * references a signed data type.
 */
664 665
static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
{
	int bits = mapping->size;
	int offset = mapping->offset;
	__s32 value = 0;
	__u8 mask;

	data += offset / 8;
	offset &= 7;
	mask = ((1LL << bits) - 1) << offset;

	for (; bits > 0; data++) {
		__u8 byte = *data & mask;
		value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
		bits -= 8 - (offset > 0 ? offset : 0);
		offset -= 8;
		mask = (1 << bits) - 1;
	}

684
	/* Sign-extend the value if needed. */
685 686 687 688 689 690 691 692 693
	if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
		value |= -(value & (1 << (mapping->size - 1)));

	return value;
}

/* Set the bit string specified by mapping->offset and mapping->size
 * in the little-endian data stored at 'data' to the value 'value'.
 */
694 695
static void uvc_set_le_value(struct uvc_control_mapping *mapping,
	__s32 value, __u8 *data)
696 697 698 699 700
{
	int bits = mapping->size;
	int offset = mapping->offset;
	__u8 mask;

701 702 703 704 705 706 707 708
	/* According to the v4l2 spec, writing any value to a button control
	 * should result in the action belonging to the button control being
	 * triggered. UVC devices however want to see a 1 written -> override
	 * value.
	 */
	if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
		value = -1;

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	data += offset / 8;
	offset &= 7;

	for (; bits > 0; data++) {
		mask = ((1LL << bits) - 1) << offset;
		*data = (*data & ~mask) | ((value << offset) & mask);
		value >>= offset ? offset : 8;
		bits -= 8 - offset;
		offset = 0;
	}
}

/* ------------------------------------------------------------------------
 * Terminal and unit management
 */

static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
static const __u8 uvc_media_transport_input_guid[16] =
	UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;

730 731
static int uvc_entity_match_guid(const struct uvc_entity *entity,
	const __u8 guid[16])
732 733
{
	switch (UVC_ENTITY_TYPE(entity)) {
734
	case UVC_ITT_CAMERA:
735 736
		return memcmp(uvc_camera_guid, guid, 16) == 0;

737
	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
738 739
		return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;

740
	case UVC_VC_PROCESSING_UNIT:
741 742
		return memcmp(uvc_processing_guid, guid, 16) == 0;

743
	case UVC_VC_EXTENSION_UNIT:
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
		return memcmp(entity->extension.guidExtensionCode,
			      guid, 16) == 0;

	default:
		return 0;
	}
}

/* ------------------------------------------------------------------------
 * UVC Controls
 */

static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
	struct uvc_control_mapping **mapping, struct uvc_control **control,
	int next)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *map;
	unsigned int i;

	if (entity == NULL)
		return;

	for (i = 0; i < entity->ncontrols; ++i) {
		ctrl = &entity->controls[i];
769
		if (!ctrl->initialized)
770 771
			continue;

772
		list_for_each_entry(map, &ctrl->info.mappings, list) {
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
			if ((map->id == v4l2_id) && !next) {
				*control = ctrl;
				*mapping = map;
				return;
			}

			if ((*mapping == NULL || (*mapping)->id > map->id) &&
			    (map->id > v4l2_id) && next) {
				*control = ctrl;
				*mapping = map;
			}
		}
	}
}

788
static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
789 790 791 792 793 794 795 796 797 798 799 800
	__u32 v4l2_id, struct uvc_control_mapping **mapping)
{
	struct uvc_control *ctrl = NULL;
	struct uvc_entity *entity;
	int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;

	*mapping = NULL;

	/* Mask the query flags. */
	v4l2_id &= V4L2_CTRL_ID_MASK;

	/* Find the control. */
801
	list_for_each_entry(entity, &chain->entities, chain) {
802 803 804 805 806 807 808 809 810 811 812 813
		__uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
		if (ctrl && !next)
			return ctrl;
	}

	if (ctrl == NULL && !next)
		uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
				v4l2_id);

	return ctrl;
}

814 815 816 817 818
static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
	struct uvc_control *ctrl)
{
	int ret;

819
	if (ctrl->info.flags & UVC_CONTROL_GET_DEF) {
820
		ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
821
				     chain->dev->intfnum, ctrl->info.selector,
822
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
823
				     ctrl->info.size);
824 825 826 827
		if (ret < 0)
			return ret;
	}

828
	if (ctrl->info.flags & UVC_CONTROL_GET_MIN) {
829
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
830
				     chain->dev->intfnum, ctrl->info.selector,
831
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
832
				     ctrl->info.size);
833 834 835
		if (ret < 0)
			return ret;
	}
836
	if (ctrl->info.flags & UVC_CONTROL_GET_MAX) {
837
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
838
				     chain->dev->intfnum, ctrl->info.selector,
839
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
840
				     ctrl->info.size);
841 842 843
		if (ret < 0)
			return ret;
	}
844
	if (ctrl->info.flags & UVC_CONTROL_GET_RES) {
845
		ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
846
				     chain->dev->intfnum, ctrl->info.selector,
847
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
848
				     ctrl->info.size);
849 850 851 852 853 854 855 856
		if (ret < 0)
			return ret;
	}

	ctrl->cached = 1;
	return 0;
}

857
int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
858 859 860 861 862 863 864 865
	struct v4l2_queryctrl *v4l2_ctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	struct uvc_menu_info *menu;
	unsigned int i;
	int ret;

866 867 868 869
	ret = mutex_lock_interruptible(&chain->ctrl_mutex);
	if (ret < 0)
		return -ERESTARTSYS;

870
	ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
871 872 873 874
	if (ctrl == NULL) {
		ret = -EINVAL;
		goto done;
	}
875

876
	memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
877 878
	v4l2_ctrl->id = mapping->id;
	v4l2_ctrl->type = mapping->v4l2_type;
879
	strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
880 881
	v4l2_ctrl->flags = 0;

882
	if (!(ctrl->info.flags & UVC_CONTROL_GET_CUR))
883
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
884
	if (!(ctrl->info.flags & UVC_CONTROL_SET_CUR))
885 886
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;

887 888
	if (!ctrl->cached) {
		ret = uvc_ctrl_populate_cache(chain, ctrl);
889
		if (ret < 0)
890
			goto done;
891 892
	}

893
	if (ctrl->info.flags & UVC_CONTROL_GET_DEF) {
894 895
		v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
896 897
	}

898 899
	switch (mapping->v4l2_type) {
	case V4L2_CTRL_TYPE_MENU:
900 901 902 903 904 905 906 907 908 909 910 911
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = mapping->menu_count - 1;
		v4l2_ctrl->step = 1;

		menu = mapping->menu_info;
		for (i = 0; i < mapping->menu_count; ++i, ++menu) {
			if (menu->value == v4l2_ctrl->default_value) {
				v4l2_ctrl->default_value = i;
				break;
			}
		}

912
		goto done;
913 914 915 916 917

	case V4L2_CTRL_TYPE_BOOLEAN:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 1;
		v4l2_ctrl->step = 1;
918
		goto done;
919

920 921 922 923
	case V4L2_CTRL_TYPE_BUTTON:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 0;
		v4l2_ctrl->step = 0;
924
		goto done;
925

926 927
	default:
		break;
928 929
	}

930
	if (ctrl->info.flags & UVC_CONTROL_GET_MIN)
931 932
		v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
933

934
	if (ctrl->info.flags & UVC_CONTROL_GET_MAX)
935 936 937
		v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));

938
	if (ctrl->info.flags & UVC_CONTROL_GET_RES)
939 940 941
		v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
				  uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));

942 943 944
done:
	mutex_unlock(&chain->ctrl_mutex);
	return ret;
945 946
}

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
/*
 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
 * must be grouped (for instance the Red Balance, Blue Balance and Do White
 * Balance V4L2 controls use the White Balance Component UVC control) or
 * otherwise translated. The approach we take here is to use a translation
 * table for the controls that can be mapped directly, and handle the others
 * manually.
 */
int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
	struct v4l2_querymenu *query_menu)
{
	struct uvc_menu_info *menu_info;
	struct uvc_control_mapping *mapping;
	struct uvc_control *ctrl;
	u32 index = query_menu->index;
	u32 id = query_menu->id;
	int ret;

	memset(query_menu, 0, sizeof(*query_menu));
	query_menu->id = id;
	query_menu->index = index;

	ret = mutex_lock_interruptible(&chain->ctrl_mutex);
	if (ret < 0)
		return -ERESTARTSYS;

	ctrl = uvc_find_control(chain, query_menu->id, &mapping);
	if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
		ret = -EINVAL;
		goto done;
	}

	if (query_menu->index >= mapping->menu_count) {
		ret = -EINVAL;
		goto done;
	}

	menu_info = &mapping->menu_info[query_menu->index];
	strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);

done:
	mutex_unlock(&chain->ctrl_mutex);
	return ret;
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

/* --------------------------------------------------------------------------
 * Control transactions
 *
 * To make extended set operations as atomic as the hardware allows, controls
 * are handled using begin/commit/rollback operations.
 *
 * At the beginning of a set request, uvc_ctrl_begin should be called to
 * initialize the request. This function acquires the control lock.
 *
 * When setting a control, the new value is stored in the control data field
 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
 * later processing. If the UVC and V4L2 control sizes differ, the current
 * value is loaded from the hardware before storing the new value in the data
 * field.
 *
 * After processing all controls in the transaction, uvc_ctrl_commit or
 * uvc_ctrl_rollback must be called to apply the pending changes to the
 * hardware or revert them. When applying changes, all controls marked as
 * dirty will be modified in the UVC device, and the dirty flag will be
 * cleared. When reverting controls, the control data field
 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
 * control lock.
 */
1018
int uvc_ctrl_begin(struct uvc_video_chain *chain)
1019
{
1020
	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
}

static int uvc_ctrl_commit_entity(struct uvc_device *dev,
	struct uvc_entity *entity, int rollback)
{
	struct uvc_control *ctrl;
	unsigned int i;
	int ret;

	if (entity == NULL)
		return 0;

	for (i = 0; i < entity->ncontrols; ++i) {
		ctrl = &entity->controls[i];
1035
		if (!ctrl->initialized)
1036 1037 1038 1039 1040 1041
			continue;

		/* Reset the loaded flag for auto-update controls that were
		 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
		 * uvc_ctrl_get from using the cached value.
		 */
1042
		if (ctrl->info.flags & UVC_CONTROL_AUTO_UPDATE)
1043 1044 1045
			ctrl->loaded = 0;

		if (!ctrl->dirty)
1046 1047 1048
			continue;

		if (!rollback)
1049
			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1050
				dev->intfnum, ctrl->info.selector,
1051
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1052
				ctrl->info.size);
1053 1054 1055 1056 1057 1058
		else
			ret = 0;

		if (rollback || ret < 0)
			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1059
			       ctrl->info.size);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

		ctrl->dirty = 0;

		if (ret < 0)
			return ret;
	}

	return 0;
}

1070
int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
1071 1072 1073 1074 1075
{
	struct uvc_entity *entity;
	int ret = 0;

	/* Find the control. */
1076
	list_for_each_entry(entity, &chain->entities, chain) {
1077
		ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1078 1079 1080 1081 1082
		if (ret < 0)
			goto done;
	}

done:
1083
	mutex_unlock(&chain->ctrl_mutex);
1084 1085 1086
	return ret;
}

1087
int uvc_ctrl_get(struct uvc_video_chain *chain,
1088 1089 1090 1091 1092 1093 1094 1095
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	struct uvc_menu_info *menu;
	unsigned int i;
	int ret;

1096
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1097
	if (ctrl == NULL || (ctrl->info.flags & UVC_CONTROL_GET_CUR) == 0)
1098 1099 1100
		return -EINVAL;

	if (!ctrl->loaded) {
1101
		ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1102
				chain->dev->intfnum, ctrl->info.selector,
1103
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1104
				ctrl->info.size);
1105 1106 1107
		if (ret < 0)
			return ret;

1108
		ctrl->loaded = 1;
1109 1110
	}

1111
	xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1112
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

	if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
		menu = mapping->menu_info;
		for (i = 0; i < mapping->menu_count; ++i, ++menu) {
			if (menu->value == xctrl->value) {
				xctrl->value = i;
				break;
			}
		}
	}

	return 0;
}

1127
int uvc_ctrl_set(struct uvc_video_chain *chain,
1128 1129 1130 1131
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
1132 1133 1134 1135
	s32 value;
	u32 step;
	s32 min;
	s32 max;
1136 1137
	int ret;

1138
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1139
	if (ctrl == NULL || (ctrl->info.flags & UVC_CONTROL_SET_CUR) == 0)
1140 1141
		return -EINVAL;

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
	/* Clamp out of range values. */
	switch (mapping->v4l2_type) {
	case V4L2_CTRL_TYPE_INTEGER:
		if (!ctrl->cached) {
			ret = uvc_ctrl_populate_cache(chain, ctrl);
			if (ret < 0)
				return ret;
		}

		min = mapping->get(mapping, UVC_GET_MIN,
				   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
		max = mapping->get(mapping, UVC_GET_MAX,
				   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
		step = mapping->get(mapping, UVC_GET_RES,
				    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1157 1158
		if (step == 0)
			step = 1;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

		xctrl->value = min + (xctrl->value - min + step/2) / step * step;
		xctrl->value = clamp(xctrl->value, min, max);
		value = xctrl->value;
		break;

	case V4L2_CTRL_TYPE_BOOLEAN:
		xctrl->value = clamp(xctrl->value, 0, 1);
		value = xctrl->value;
		break;

	case V4L2_CTRL_TYPE_MENU:
		if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1172
			return -ERANGE;
1173 1174 1175 1176 1177 1178
		value = mapping->menu_info[xctrl->value].value;
		break;

	default:
		value = xctrl->value;
		break;
1179 1180
	}

1181 1182 1183 1184
	/* If the mapping doesn't span the whole UVC control, the current value
	 * needs to be loaded from the device to perform the read-modify-write
	 * operation.
	 */
1185 1186
	if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
		if ((ctrl->info.flags & UVC_CONTROL_GET_CUR) == 0) {
1187
			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1188
				0, ctrl->info.size);
1189
		} else {
1190 1191
			ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
				ctrl->entity->id, chain->dev->intfnum,
1192
				ctrl->info.selector,
1193
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1194
				ctrl->info.size);
1195 1196 1197 1198
			if (ret < 0)
				return ret;
		}

1199
		ctrl->loaded = 1;
1200 1201
	}

1202
	/* Backup the current value in case we need to rollback later. */
1203 1204 1205
	if (!ctrl->dirty) {
		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1206
		       ctrl->info.size);
1207 1208
	}

1209 1210
	mapping->set(mapping, value,
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

	ctrl->dirty = 1;
	ctrl->modified = 1;
	return 0;
}

/* --------------------------------------------------------------------------
 * Dynamic controls
 */

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
	const struct uvc_control *ctrl, struct uvc_control_info *info)
{
	struct uvc_ctrl_fixup {
		struct usb_device_id id;
		u8 entity;
		u8 selector;
		u8 flags;
	};

	static const struct uvc_ctrl_fixup fixups[] = {
		{ { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
			UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
			UVC_CONTROL_GET_DEF | UVC_CONTROL_SET_CUR |
			UVC_CONTROL_AUTO_UPDATE },
		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
			UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
			UVC_CONTROL_GET_DEF | UVC_CONTROL_SET_CUR |
			UVC_CONTROL_AUTO_UPDATE },
		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
			UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
			UVC_CONTROL_GET_DEF | UVC_CONTROL_SET_CUR |
			UVC_CONTROL_AUTO_UPDATE },
	};

	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
		if (!usb_match_one_id(dev->intf, &fixups[i].id))
			continue;

		if (fixups[i].entity == ctrl->entity->id &&
		    fixups[i].selector == info->selector) {
			info->flags = fixups[i].flags;
			return;
		}
	}
}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
/*
 * Query control information (size and flags) for XU controls.
 */
static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
	const struct uvc_control *ctrl, struct uvc_control_info *info)
{
	u8 *data;
	int ret;

	data = kmalloc(2, GFP_KERNEL);
	if (data == NULL)
		return -ENOMEM;

	memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
	       sizeof(info->entity));
	info->index = ctrl->index;
	info->selector = ctrl->index + 1;

	/* Query and verify the control length (GET_LEN) */
	ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
			     info->selector, data, 2);
	if (ret < 0) {
		uvc_trace(UVC_TRACE_CONTROL,
			  "GET_LEN failed on control %pUl/%u (%d).\n",
			   info->entity, info->selector, ret);
		goto done;
	}

	info->size = le16_to_cpup((__le16 *)data);

	/* Query the control information (GET_INFO) */
	ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
			     info->selector, data, 1);
	if (ret < 0) {
		uvc_trace(UVC_TRACE_CONTROL,
			  "GET_INFO failed on control %pUl/%u (%d).\n",
			  info->entity, info->selector, ret);
		goto done;
	}

	info->flags = UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX
		    | UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF
		    | (data[0] & UVC_CONTROL_CAP_GET ? UVC_CONTROL_GET_CUR : 0)
		    | (data[0] & UVC_CONTROL_CAP_SET ? UVC_CONTROL_SET_CUR : 0)
		    | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
		       UVC_CONTROL_AUTO_UPDATE : 0);

1307 1308
	uvc_ctrl_fixup_xu_info(dev, ctrl, info);

1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
	uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
		  "flags { get %u set %u auto %u }.\n",
		  info->entity, info->selector, info->size,
		  (info->flags & UVC_CONTROL_GET_CUR) ? 1 : 0,
		  (info->flags & UVC_CONTROL_SET_CUR) ? 1 : 0,
		  (info->flags & UVC_CONTROL_AUTO_UPDATE) ? 1 : 0);

done:
	kfree(data);
	return ret;
}

static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
	const struct uvc_control_info *info);

static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
	struct uvc_control *ctrl)
{
	struct uvc_control_info info;
	int ret;

	if (ctrl->initialized)
		return 0;

	ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
	if (ret < 0)
		return ret;

	ret = uvc_ctrl_add_info(dev, ctrl, &info);
	if (ret < 0)
		uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
			  "%pUl/%u on device %s entity %u\n", info.entity,
			  info.selector, dev->udev->devpath, ctrl->entity->id);

	return ret;
}

1346
int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1347 1348 1349 1350 1351
	struct uvc_xu_control *xctrl, int set)
{
	struct uvc_entity *entity;
	struct uvc_control *ctrl = NULL;
	unsigned int i, found = 0;
1352
	int restore = 0;
1353 1354 1355 1356
	__u8 *data;
	int ret;

	/* Find the extension unit. */
1357 1358 1359
	list_for_each_entry(entity, &chain->entities, chain) {
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
		    entity->id == xctrl->unit)
1360 1361 1362 1363 1364 1365 1366 1367 1368
			break;
	}

	if (entity->id != xctrl->unit) {
		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
			xctrl->unit);
		return -EINVAL;
	}

1369
	/* Find the control and perform delayed initialization if needed. */
1370 1371
	for (i = 0; i < entity->ncontrols; ++i) {
		ctrl = &entity->controls[i];
1372
		if (ctrl->index == xctrl->selector - 1) {
1373 1374 1375 1376 1377 1378
			found = 1;
			break;
		}
	}

	if (!found) {
1379 1380
		uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
			entity->extension.guidExtensionCode, xctrl->selector);
1381 1382 1383
		return -EINVAL;
	}

1384 1385 1386
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1387
	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1388 1389 1390 1391
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}
1392

1393
	/* Validate control data size. */
1394 1395 1396 1397
	if (ctrl->info.size != xctrl->size) {
		ret = -EINVAL;
		goto done;
	}
1398

1399
	if ((set && !(ctrl->info.flags & UVC_CONTROL_SET_CUR)) ||
1400 1401 1402 1403
	    (!set && !(ctrl->info.flags & UVC_CONTROL_GET_CUR))) {
		ret = -EINVAL;
		goto done;
	}
1404 1405 1406

	memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
	       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1407
	       ctrl->info.size);
1408
	data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1409
	restore = set;
1410 1411 1412

	if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
		ret = -EFAULT;
1413
		goto done;
1414 1415
	}

1416 1417
	ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
			     xctrl->unit, chain->dev->intfnum, xctrl->selector,
1418
			     data, xctrl->size);
1419
	if (ret < 0)
1420
		goto done;
1421

1422
	if (!set && copy_to_user(xctrl->data, data, xctrl->size))
1423
		ret = -EFAULT;
1424 1425
done:
	if (ret && restore)
1426 1427 1428 1429
		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
		       xctrl->size);

1430
	mutex_unlock(&chain->ctrl_mutex);
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	return ret;
}

/* --------------------------------------------------------------------------
 * Suspend/resume
 */

/*
 * Restore control values after resume, skipping controls that haven't been
 * changed.
 *
 * TODO
 * - Don't restore modified controls that are back to their default value.
 * - Handle restore order (Auto-Exposure Mode should be restored before
 *   Exposure Time).
 */
int uvc_ctrl_resume_device(struct uvc_device *dev)
{
	struct uvc_control *ctrl;
	struct uvc_entity *entity;
	unsigned int i;
	int ret;

	/* Walk the entities list and restore controls when possible. */
	list_for_each_entry(entity, &dev->entities, list) {

		for (i = 0; i < entity->ncontrols; ++i) {
			ctrl = &entity->controls[i];

1460 1461
			if (!ctrl->initialized || !ctrl->modified ||
			    (ctrl->info.flags & UVC_CONTROL_RESTORE) == 0)
1462 1463
				continue;

1464
			printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1465 1466
				ctrl->info.entity, ctrl->info.index,
				ctrl->info.selector);
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
			ctrl->dirty = 1;
		}

		ret = uvc_ctrl_commit_entity(dev, entity, 0);
		if (ret < 0)
			return ret;
	}

	return 0;
}

/* --------------------------------------------------------------------------
 * Control and mapping handling
 */

1482 1483 1484 1485 1486 1487 1488
/*
 * Add control information to a given control.
 */
static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
	const struct uvc_control_info *info)
{
	int ret = 0;
1489

1490 1491
	memcpy(&ctrl->info, info, sizeof(*info));
	INIT_LIST_HEAD(&ctrl->info.mappings);
1492 1493

	/* Allocate an array to save control values (cur, def, max, etc.) */
1494
	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1495 1496 1497 1498
				 GFP_KERNEL);
	if (ctrl->uvc_data == NULL) {
		ret = -ENOMEM;
		goto done;
1499
	}
1500

1501 1502
	ctrl->initialized = 1;

1503
	uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1504
		"entity %u\n", ctrl->info.entity, ctrl->info.selector,
1505
		dev->udev->devpath, ctrl->entity->id);
1506 1507

done:
1508
	if (ret < 0)
1509
		kfree(ctrl->uvc_data);
1510
	return ret;
1511 1512 1513
}

/*
1514
 * Add a control mapping to a given control.
1515
 */
1516 1517
static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1518
{
1519 1520
	struct uvc_control_mapping *map;
	unsigned int size;
1521

1522 1523 1524
	/* Most mappings come from static kernel data and need to be duplicated.
	 * Mappings that come from userspace will be unnecessarily duplicated,
	 * this could be optimized.
1525
	 */
1526 1527 1528
	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
	if (map == NULL)
		return -ENOMEM;
1529

1530 1531 1532 1533 1534
	size = sizeof(*mapping->menu_info) * mapping->menu_count;
	map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
	if (map->menu_info == NULL) {
		kfree(map);
		return -ENOMEM;
1535 1536
	}

1537 1538 1539 1540
	if (map->get == NULL)
		map->get = uvc_get_le_value;
	if (map->set == NULL)
		map->set = uvc_set_le_value;
1541

1542 1543
	map->ctrl = &ctrl->info;
	list_add_tail(&map->list, &ctrl->info.mappings);
1544 1545
	uvc_trace(UVC_TRACE_CONTROL,
		"Adding mapping '%s' to control %pUl/%u.\n",
1546
		map->name, ctrl->info.entity, ctrl->info.selector);
1547 1548

	return 0;
1549 1550
}

1551 1552
int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
	const struct uvc_control_mapping *mapping)
1553
{
1554
	struct uvc_device *dev = chain->dev;
1555
	struct uvc_control_mapping *map;
1556 1557 1558
	struct uvc_entity *entity;
	struct uvc_control *ctrl;
	int found = 0;
1559
	int ret;
1560

1561
	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1562
		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1563
			"id 0x%08x is invalid.\n", mapping->name,
1564 1565 1566 1567
			mapping->id);
		return -EINVAL;
	}

1568 1569 1570
	/* Search for the matching (GUID/CS) control in the given device */
	list_for_each_entry(entity, &dev->entities, list) {
		unsigned int i;
1571

1572 1573
		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
		    !uvc_entity_match_guid(entity, mapping->entity))
1574
			continue;
1575

1576 1577
		for (i = 0; i < entity->ncontrols; ++i) {
			ctrl = &entity->controls[i];
1578
			if (ctrl->index == mapping->selector - 1) {
1579 1580
				found = 1;
				break;
1581 1582 1583
			}
		}

1584 1585 1586 1587 1588
		if (found)
			break;
	}
	if (!found)
		return -ENOENT;
1589

1590 1591 1592
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1593 1594 1595 1596 1597 1598 1599
	/* Perform delayed initialization of XU controls */
	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}

1600
	list_for_each_entry(map, &ctrl->info.mappings, list) {
1601 1602 1603 1604 1605 1606 1607
		if (mapping->id == map->id) {
			uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
				"control id 0x%08x already exists.\n",
				mapping->name, mapping->id);
			ret = -EEXIST;
			goto done;
		}
1608
	}
1609

1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
	/* Prevent excess memory consumption */
	if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
		atomic_dec(&dev->nmappings);
		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
			"mappings count (%u) exceeded.\n", mapping->name,
			UVC_MAX_CONTROL_MAPPINGS);
		ret = -ENOMEM;
		goto done;
	}

1620
	ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1621 1622 1623
	if (ret < 0)
		atomic_dec(&dev->nmappings);

1624 1625
done:
	mutex_unlock(&chain->ctrl_mutex);
1626 1627 1628
	return ret;
}

1629
/*
1630 1631 1632
 * Prune an entity of its bogus controls using a blacklist. Bogus controls
 * are currently the ones that crash the camera or unconditionally return an
 * error when queried.
1633
 */
1634 1635
static void uvc_ctrl_prune_entity(struct uvc_device *dev,
	struct uvc_entity *entity)
1636
{
1637
	struct uvc_ctrl_blacklist {
1638 1639
		struct usb_device_id id;
		u8 index;
1640 1641 1642
	};

	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1643
		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1644 1645
		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1646
	};
1647 1648 1649
	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
	};
1650

1651
	const struct uvc_ctrl_blacklist *blacklist;
1652
	unsigned int size;
1653
	unsigned int count;
1654
	unsigned int i;
1655
	u8 *controls;
1656

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
	switch (UVC_ENTITY_TYPE(entity)) {
	case UVC_VC_PROCESSING_UNIT:
		blacklist = processing_blacklist;
		count = ARRAY_SIZE(processing_blacklist);
		controls = entity->processing.bmControls;
		size = entity->processing.bControlSize;
		break;

	case UVC_ITT_CAMERA:
		blacklist = camera_blacklist;
		count = ARRAY_SIZE(camera_blacklist);
		controls = entity->camera.bmControls;
		size = entity->camera.bControlSize;
		break;
1671

1672 1673 1674
	default:
		return;
	}
1675

1676
	for (i = 0; i < count; ++i) {
1677
		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1678 1679
			continue;

1680 1681
		if (blacklist[i].index >= 8 * size ||
		    !uvc_test_bit(controls, blacklist[i].index))
1682 1683
			continue;

1684 1685
		uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
			"removing it.\n", entity->id, blacklist[i].index);
1686

1687
		uvc_clear_bit(controls, blacklist[i].index);
1688 1689 1690
	}
}

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
/*
 * Add control information and hardcoded stock control mappings to the given
 * device.
 */
static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
{
	const struct uvc_control_info *info = uvc_ctrls;
	const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
	const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
	const struct uvc_control_mapping *mend =
		mapping + ARRAY_SIZE(uvc_ctrl_mappings);

1703 1704 1705 1706 1707 1708
	/* XU controls initialization requires querying the device for control
	 * information. As some buggy UVC devices will crash when queried
	 * repeatedly in a tight loop, delay XU controls initialization until
	 * first use.
	 */
	if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
		return;

	for (; info < iend; ++info) {
		if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
		    ctrl->index == info->index) {
			uvc_ctrl_add_info(dev, ctrl, info);
			break;
		 }
	}

1719
	if (!ctrl->initialized)
1720 1721 1722 1723
		return;

	for (; mapping < mend; ++mapping) {
		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
1724
		    ctrl->info.selector == mapping->selector)
1725 1726 1727 1728
			__uvc_ctrl_add_mapping(dev, ctrl, mapping);
	}
}

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
/*
 * Initialize device controls.
 */
int uvc_ctrl_init_device(struct uvc_device *dev)
{
	struct uvc_entity *entity;
	unsigned int i;

	/* Walk the entities list and instantiate controls */
	list_for_each_entry(entity, &dev->entities, list) {
1739
		struct uvc_control *ctrl;
1740 1741 1742
		unsigned int bControlSize = 0, ncontrols = 0;
		__u8 *bmControls = NULL;

1743
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1744 1745
			bmControls = entity->extension.bmControls;
			bControlSize = entity->extension.bControlSize;
1746
		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1747 1748
			bmControls = entity->processing.bmControls;
			bControlSize = entity->processing.bControlSize;
1749
		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1750 1751 1752 1753
			bmControls = entity->camera.bmControls;
			bControlSize = entity->camera.bControlSize;
		}

1754
		/* Remove bogus/blacklisted controls */
1755
		uvc_ctrl_prune_entity(dev, entity);
1756

1757
		/* Count supported controls and allocate the controls array */
1758 1759 1760 1761 1762
		for (i = 0; i < bControlSize; ++i)
			ncontrols += hweight8(bmControls[i]);
		if (ncontrols == 0)
			continue;

1763 1764
		entity->controls = kzalloc(ncontrols * sizeof(*ctrl),
					   GFP_KERNEL);
1765 1766 1767 1768
		if (entity->controls == NULL)
			return -ENOMEM;
		entity->ncontrols = ncontrols;

1769
		/* Initialize all supported controls */
1770 1771
		ctrl = entity->controls;
		for (i = 0; i < bControlSize * 8; ++i) {
1772
			if (uvc_test_bit(bmControls, i) == 0)
1773 1774 1775 1776
				continue;

			ctrl->entity = entity;
			ctrl->index = i;
1777 1778

			uvc_ctrl_init_ctrl(dev, ctrl);
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
			ctrl++;
		}
	}

	return 0;
}

/*
 * Cleanup device controls.
 */
1789 1790
static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
	struct uvc_control *ctrl)
1791
{
1792
	struct uvc_control_mapping *mapping, *nm;
1793

1794
	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
1795 1796 1797
		list_del(&mapping->list);
		kfree(mapping->menu_info);
		kfree(mapping);
1798 1799 1800
	}
}

1801
void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1802
{
1803 1804
	struct uvc_entity *entity;
	unsigned int i;
1805

1806 1807 1808 1809 1810
	/* Free controls and control mappings for all entities. */
	list_for_each_entry(entity, &dev->entities, list) {
		for (i = 0; i < entity->ncontrols; ++i) {
			struct uvc_control *ctrl = &entity->controls[i];

1811
			if (!ctrl->initialized)
1812
				continue;
1813

1814 1815
			uvc_ctrl_cleanup_mappings(dev, ctrl);
			kfree(ctrl->uvc_data);
1816 1817
		}

1818
		kfree(entity->controls);
1819 1820
	}
}