uvc_ctrl.c 48.1 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
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/videodev2.h>
#include <linux/vmalloc.h>
#include <linux/wait.h>
A
Arun Sharma 已提交
23
#include <linux/atomic.h>
24 25 26 27 28

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

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" },
363
	{ 1, "Manual Mode" },
364 365 366 367
	{ 4, "Shutter Priority Mode" },
	{ 8, "Aperture Priority Mode" },
};

368 369 370 371 372 373
static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
{
	__s8 zoom = (__s8)data[0];

	switch (query) {
374
	case UVC_GET_CUR:
375 376
		return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);

377 378 379 380
	case UVC_GET_MIN:
	case UVC_GET_MAX:
	case UVC_GET_RES:
	case UVC_GET_DEF:
381 382 383 384 385 386 387 388 389
	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;
390
	data[2] = min((int)abs(value), 0xff);
391 392
}

393 394 395 396 397
static struct uvc_control_mapping uvc_ctrl_mappings[] = {
	{
		.id		= V4L2_CID_BRIGHTNESS,
		.name		= "Brightness",
		.entity		= UVC_GUID_UVC_PROCESSING,
398
		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
399 400 401 402 403 404 405 406 407
		.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,
408
		.selector	= UVC_PU_CONTRAST_CONTROL,
409 410 411 412 413 414 415 416 417
		.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,
418
		.selector	= UVC_PU_HUE_CONTROL,
419 420 421 422 423 424 425 426 427
		.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,
428
		.selector	= UVC_PU_SATURATION_CONTROL,
429 430 431 432 433 434 435 436 437
		.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,
438
		.selector	= UVC_PU_SHARPNESS_CONTROL,
439 440 441 442 443 444 445 446 447
		.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,
448
		.selector	= UVC_PU_GAMMA_CONTROL,
449 450 451 452 453 454 455 456 457
		.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,
458
		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
459 460 461 462 463 464 465 466 467
		.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,
468
		.selector	= UVC_PU_GAIN_CONTROL,
469 470 471 472 473 474 475 476 477
		.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,
478
		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
479 480 481 482 483 484 485 486 487 488 489
		.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,
490
		.selector	= UVC_PU_HUE_AUTO_CONTROL,
491 492 493 494 495 496 497 498 499
		.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,
500
		.selector	= UVC_CT_AE_MODE_CONTROL,
501 502 503 504 505 506 507 508 509 510 511
		.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,
512
		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
513 514 515 516 517 518 519 520 521
		.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,
522
		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
523 524 525 526 527 528 529 530 531
		.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,
532
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
533 534 535 536 537 538 539 540 541
		.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,
542
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
543 544 545 546 547 548 549 550 551
		.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,
552
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
553 554 555 556 557 558 559 560 561
		.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,
562
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
563 564 565 566 567 568 569 570 571
		.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,
572
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
573 574 575 576 577 578 579 580 581
		.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,
582
		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
583 584 585 586 587 588 589 590 591
		.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,
592
		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
593 594 595 596 597
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	{
		.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,
	},
618 619 620 621
	{
		.id		= V4L2_CID_ZOOM_ABSOLUTE,
		.name		= "Zoom, Absolute",
		.entity		= UVC_GUID_UVC_CAMERA,
622
		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
623 624 625 626 627 628 629 630 631
		.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,
632
		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
633 634 635 636 637 638 639
		.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,
	},
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	{
		.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,
	},
660 661 662 663
	{
		.id		= V4L2_CID_PRIVACY,
		.name		= "Privacy",
		.entity		= UVC_GUID_UVC_CAMERA,
664
		.selector	= UVC_CT_PRIVACY_CONTROL,
665 666 667 668 669
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
670 671 672 673 674 675 676 677
};

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

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

681
static inline int uvc_test_bit(const __u8 *data, int bit)
682 683 684 685
{
	return (data[bit >> 3] >> (bit & 7)) & 1;
}

686 687 688 689 690
static inline void uvc_clear_bit(__u8 *data, int bit)
{
	data[bit >> 3] &= ~(1 << (bit & 7));
}

691 692 693 694 695
/* 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.
 */
696 697
static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
{
	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;
	}

716
	/* Sign-extend the value if needed. */
717 718 719 720 721 722 723 724 725
	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'.
 */
726 727
static void uvc_set_le_value(struct uvc_control_mapping *mapping,
	__s32 value, __u8 *data)
728 729 730 731 732
{
	int bits = mapping->size;
	int offset = mapping->offset;
	__u8 mask;

733 734 735 736 737 738 739 740
	/* 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;

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
	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;

762 763
static int uvc_entity_match_guid(const struct uvc_entity *entity,
	const __u8 guid[16])
764 765
{
	switch (UVC_ENTITY_TYPE(entity)) {
766
	case UVC_ITT_CAMERA:
767 768
		return memcmp(uvc_camera_guid, guid, 16) == 0;

769
	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
770 771
		return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;

772
	case UVC_VC_PROCESSING_UNIT:
773 774
		return memcmp(uvc_processing_guid, guid, 16) == 0;

775
	case UVC_VC_EXTENSION_UNIT:
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
		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];
801
		if (!ctrl->initialized)
802 803
			continue;

804
		list_for_each_entry(map, &ctrl->info.mappings, list) {
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
			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;
			}
		}
	}
}

820
static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
821 822 823 824 825 826 827 828 829 830 831 832
	__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. */
833
	list_for_each_entry(entity, &chain->entities, chain) {
834 835 836 837 838 839 840 841 842 843 844 845
		__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;
}

846 847 848 849 850
static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
	struct uvc_control *ctrl)
{
	int ret;

851
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
852
		ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
853
				     chain->dev->intfnum, ctrl->info.selector,
854
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
855
				     ctrl->info.size);
856 857 858 859
		if (ret < 0)
			return ret;
	}

860
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
861
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
862
				     chain->dev->intfnum, ctrl->info.selector,
863
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
864
				     ctrl->info.size);
865 866 867
		if (ret < 0)
			return ret;
	}
868
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
869
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
870
				     chain->dev->intfnum, ctrl->info.selector,
871
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
872
				     ctrl->info.size);
873 874 875
		if (ret < 0)
			return ret;
	}
876
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
877
		ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
878
				     chain->dev->intfnum, ctrl->info.selector,
879
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
880
				     ctrl->info.size);
881 882 883 884 885 886 887 888
		if (ret < 0)
			return ret;
	}

	ctrl->cached = 1;
	return 0;
}

889
int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
890 891 892 893 894 895 896 897
	struct v4l2_queryctrl *v4l2_ctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	struct uvc_menu_info *menu;
	unsigned int i;
	int ret;

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

902
	ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
903 904 905 906
	if (ctrl == NULL) {
		ret = -EINVAL;
		goto done;
	}
907

908
	memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
909 910
	v4l2_ctrl->id = mapping->id;
	v4l2_ctrl->type = mapping->v4l2_type;
911
	strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
912 913
	v4l2_ctrl->flags = 0;

914
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
915
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
916
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
917 918
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;

919 920
	if (!ctrl->cached) {
		ret = uvc_ctrl_populate_cache(chain, ctrl);
921
		if (ret < 0)
922
			goto done;
923 924
	}

925
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
926 927
		v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
928 929
	}

930 931
	switch (mapping->v4l2_type) {
	case V4L2_CTRL_TYPE_MENU:
932 933 934 935 936 937 938 939 940 941 942 943
		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;
			}
		}

944
		goto done;
945 946 947 948 949

	case V4L2_CTRL_TYPE_BOOLEAN:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 1;
		v4l2_ctrl->step = 1;
950
		goto done;
951

952 953 954 955
	case V4L2_CTRL_TYPE_BUTTON:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 0;
		v4l2_ctrl->step = 0;
956
		goto done;
957

958 959
	default:
		break;
960 961
	}

962
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
963 964
		v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
965

966
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
967 968 969
		v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));

970
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
971 972 973
		v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
				  uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));

974 975 976
done:
	mutex_unlock(&chain->ctrl_mutex);
	return ret;
977 978
}

979 980 981 982 983 984 985 986 987 988 989 990 991 992 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
/*
 * 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];
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
		s32 bitmap;

		if (!ctrl->cached) {
			ret = uvc_ctrl_populate_cache(chain, ctrl);
			if (ret < 0)
				goto done;
		}

		bitmap = mapping->get(mapping, UVC_GET_RES,
				      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
		if (!(bitmap & menu_info->value)) {
			ret = -EINVAL;
			goto done;
		}
	}

1036 1037 1038 1039 1040 1041 1042
	strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);

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

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

/* --------------------------------------------------------------------------
 * 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.
 */
1068
int uvc_ctrl_begin(struct uvc_video_chain *chain)
1069
{
1070
	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
}

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];
1085
		if (!ctrl->initialized)
1086 1087 1088 1089 1090 1091
			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.
		 */
1092
		if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE)
1093 1094 1095
			ctrl->loaded = 0;

		if (!ctrl->dirty)
1096 1097 1098
			continue;

		if (!rollback)
1099
			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1100
				dev->intfnum, ctrl->info.selector,
1101
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1102
				ctrl->info.size);
1103 1104 1105 1106 1107 1108
		else
			ret = 0;

		if (rollback || ret < 0)
			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1109
			       ctrl->info.size);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

		ctrl->dirty = 0;

		if (ret < 0)
			return ret;
	}

	return 0;
}

1120
int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
1121 1122 1123 1124 1125
{
	struct uvc_entity *entity;
	int ret = 0;

	/* Find the control. */
1126
	list_for_each_entry(entity, &chain->entities, chain) {
1127
		ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1128 1129 1130 1131 1132
		if (ret < 0)
			goto done;
	}

done:
1133
	mutex_unlock(&chain->ctrl_mutex);
1134 1135 1136
	return ret;
}

1137
int uvc_ctrl_get(struct uvc_video_chain *chain,
1138 1139 1140 1141 1142 1143 1144 1145
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	struct uvc_menu_info *menu;
	unsigned int i;
	int ret;

1146
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1147
	if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1148 1149 1150
		return -EINVAL;

	if (!ctrl->loaded) {
1151
		ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1152
				chain->dev->intfnum, ctrl->info.selector,
1153
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1154
				ctrl->info.size);
1155 1156 1157
		if (ret < 0)
			return ret;

1158
		ctrl->loaded = 1;
1159 1160
	}

1161
	xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1162
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

	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;
}

1177
int uvc_ctrl_set(struct uvc_video_chain *chain,
1178 1179 1180 1181
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
1182 1183 1184 1185
	s32 value;
	u32 step;
	s32 min;
	s32 max;
1186 1187
	int ret;

1188
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1189
	if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
1190 1191
		return -EINVAL;

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	/* 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));
1207 1208
		if (step == 0)
			step = 1;
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

		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)
1222
			return -ERANGE;
1223
		value = mapping->menu_info[xctrl->value].value;
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

		/* Valid menu indices are reported by the GET_RES request for
		 * UVC controls that support it.
		 */
		if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
			if (!ctrl->cached) {
				ret = uvc_ctrl_populate_cache(chain, ctrl);
				if (ret < 0)
					return ret;
			}

			step = mapping->get(mapping, UVC_GET_RES,
					uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
			if (!(step & value))
				return -ERANGE;
		}

1241 1242 1243 1244 1245
		break;

	default:
		value = xctrl->value;
		break;
1246 1247
	}

1248 1249 1250 1251
	/* 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.
	 */
1252
	if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1253
		if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1254
			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1255
				0, ctrl->info.size);
1256
		} else {
1257 1258
			ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
				ctrl->entity->id, chain->dev->intfnum,
1259
				ctrl->info.selector,
1260
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1261
				ctrl->info.size);
1262 1263 1264 1265
			if (ret < 0)
				return ret;
		}

1266
		ctrl->loaded = 1;
1267 1268
	}

1269
	/* Backup the current value in case we need to rollback later. */
1270 1271 1272
	if (!ctrl->dirty) {
		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1273
		       ctrl->info.size);
1274 1275
	}

1276 1277
	mapping->set(mapping, value,
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

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

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

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
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,
1300 1301 1302
			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
			UVC_CTRL_FLAG_AUTO_UPDATE },
1303
		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1304 1305 1306
			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
			UVC_CTRL_FLAG_AUTO_UPDATE },
1307
		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1308 1309 1310
			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
			UVC_CTRL_FLAG_AUTO_UPDATE },
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
	};

	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;
		}
	}
}

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
/*
 * 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;
	}

1367 1368 1369 1370 1371 1372
	info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
		    | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
		    | (data[0] & UVC_CONTROL_CAP_GET ?
		       UVC_CTRL_FLAG_GET_CUR : 0)
		    | (data[0] & UVC_CONTROL_CAP_SET ?
		       UVC_CTRL_FLAG_SET_CUR : 0)
1373
		    | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1374
		       UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1375

1376 1377
	uvc_ctrl_fixup_xu_info(dev, ctrl, info);

1378 1379 1380
	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,
1381 1382 1383
		  (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
		  (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
		  (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

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;
}

1415
int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1416
	struct uvc_xu_control_query *xqry)
1417 1418
{
	struct uvc_entity *entity;
1419
	struct uvc_control *ctrl;
1420
	unsigned int i, found = 0;
1421 1422 1423
	__u32 reqflags;
	__u16 size;
	__u8 *data = NULL;
1424 1425 1426
	int ret;

	/* Find the extension unit. */
1427 1428
	list_for_each_entry(entity, &chain->entities, chain) {
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1429
		    entity->id == xqry->unit)
1430 1431 1432
			break;
	}

1433
	if (entity->id != xqry->unit) {
1434
		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1435 1436
			xqry->unit);
		return -ENOENT;
1437 1438
	}

1439
	/* Find the control and perform delayed initialization if needed. */
1440 1441
	for (i = 0; i < entity->ncontrols; ++i) {
		ctrl = &entity->controls[i];
1442
		if (ctrl->index == xqry->selector - 1) {
1443 1444 1445 1446 1447 1448
			found = 1;
			break;
		}
	}

	if (!found) {
1449
		uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1450 1451
			entity->extension.guidExtensionCode, xqry->selector);
		return -ENOENT;
1452 1453
	}

1454 1455 1456
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1457
	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1458 1459 1460 1461
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}
1462

1463 1464 1465 1466 1467 1468
	/* Validate the required buffer size and flags for the request */
	reqflags = 0;
	size = ctrl->info.size;

	switch (xqry->query) {
	case UVC_GET_CUR:
1469
		reqflags = UVC_CTRL_FLAG_GET_CUR;
1470 1471
		break;
	case UVC_GET_MIN:
1472
		reqflags = UVC_CTRL_FLAG_GET_MIN;
1473 1474
		break;
	case UVC_GET_MAX:
1475
		reqflags = UVC_CTRL_FLAG_GET_MAX;
1476 1477
		break;
	case UVC_GET_DEF:
1478
		reqflags = UVC_CTRL_FLAG_GET_DEF;
1479 1480
		break;
	case UVC_GET_RES:
1481
		reqflags = UVC_CTRL_FLAG_GET_RES;
1482 1483
		break;
	case UVC_SET_CUR:
1484
		reqflags = UVC_CTRL_FLAG_SET_CUR;
1485 1486 1487 1488 1489 1490 1491 1492
		break;
	case UVC_GET_LEN:
		size = 2;
		break;
	case UVC_GET_INFO:
		size = 1;
		break;
	default:
1493 1494 1495
		ret = -EINVAL;
		goto done;
	}
1496

1497 1498
	if (size != xqry->size) {
		ret = -ENOBUFS;
1499 1500
		goto done;
	}
1501

1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	if (reqflags && !(ctrl->info.flags & reqflags)) {
		ret = -EBADRQC;
		goto done;
	}

	data = kmalloc(size, GFP_KERNEL);
	if (data == NULL) {
		ret = -ENOMEM;
		goto done;
	}
1512

1513 1514
	if (xqry->query == UVC_SET_CUR &&
	    copy_from_user(data, xqry->data, size)) {
1515
		ret = -EFAULT;
1516
		goto done;
1517 1518
	}

1519 1520
	ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
			     chain->dev->intfnum, xqry->selector, data, size);
1521
	if (ret < 0)
1522
		goto done;
1523

1524 1525
	if (xqry->query != UVC_SET_CUR &&
	    copy_to_user(xqry->data, data, size))
1526
		ret = -EFAULT;
1527
done:
1528
	kfree(data);
1529
	mutex_unlock(&chain->ctrl_mutex);
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	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];

1559
			if (!ctrl->initialized || !ctrl->modified ||
1560
			    (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1561 1562
				continue;

1563
			printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1564 1565
				ctrl->info.entity, ctrl->info.index,
				ctrl->info.selector);
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
			ctrl->dirty = 1;
		}

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

	return 0;
}

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

1581 1582 1583 1584 1585 1586 1587
/*
 * 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;
1588

1589 1590
	memcpy(&ctrl->info, info, sizeof(*info));
	INIT_LIST_HEAD(&ctrl->info.mappings);
1591 1592

	/* Allocate an array to save control values (cur, def, max, etc.) */
1593
	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1594 1595 1596 1597
				 GFP_KERNEL);
	if (ctrl->uvc_data == NULL) {
		ret = -ENOMEM;
		goto done;
1598
	}
1599

1600 1601
	ctrl->initialized = 1;

1602
	uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1603
		"entity %u\n", ctrl->info.entity, ctrl->info.selector,
1604
		dev->udev->devpath, ctrl->entity->id);
1605 1606

done:
1607
	if (ret < 0)
1608
		kfree(ctrl->uvc_data);
1609
	return ret;
1610 1611 1612
}

/*
1613
 * Add a control mapping to a given control.
1614
 */
1615 1616
static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1617
{
1618 1619
	struct uvc_control_mapping *map;
	unsigned int size;
1620

1621 1622 1623
	/* 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.
1624
	 */
1625 1626 1627
	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
	if (map == NULL)
		return -ENOMEM;
1628

1629 1630 1631 1632 1633
	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;
1634 1635
	}

1636 1637 1638 1639
	if (map->get == NULL)
		map->get = uvc_get_le_value;
	if (map->set == NULL)
		map->set = uvc_set_le_value;
1640

1641 1642
	map->ctrl = &ctrl->info;
	list_add_tail(&map->list, &ctrl->info.mappings);
1643 1644
	uvc_trace(UVC_TRACE_CONTROL,
		"Adding mapping '%s' to control %pUl/%u.\n",
1645
		map->name, ctrl->info.entity, ctrl->info.selector);
1646 1647

	return 0;
1648 1649
}

1650 1651
int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
	const struct uvc_control_mapping *mapping)
1652
{
1653
	struct uvc_device *dev = chain->dev;
1654
	struct uvc_control_mapping *map;
1655 1656 1657
	struct uvc_entity *entity;
	struct uvc_control *ctrl;
	int found = 0;
1658
	int ret;
1659

1660
	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1661
		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1662
			"id 0x%08x is invalid.\n", mapping->name,
1663 1664 1665 1666
			mapping->id);
		return -EINVAL;
	}

1667 1668
	/* Search for the matching (GUID/CS) control on the current chain */
	list_for_each_entry(entity, &chain->entities, chain) {
1669
		unsigned int i;
1670

1671 1672
		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
		    !uvc_entity_match_guid(entity, mapping->entity))
1673
			continue;
1674

1675 1676
		for (i = 0; i < entity->ncontrols; ++i) {
			ctrl = &entity->controls[i];
1677
			if (ctrl->index == mapping->selector - 1) {
1678 1679
				found = 1;
				break;
1680 1681 1682
			}
		}

1683 1684 1685 1686 1687
		if (found)
			break;
	}
	if (!found)
		return -ENOENT;
1688

1689 1690 1691
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1692 1693 1694 1695 1696 1697 1698
	/* Perform delayed initialization of XU controls */
	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}

1699
	list_for_each_entry(map, &ctrl->info.mappings, list) {
1700 1701 1702 1703 1704 1705 1706
		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;
		}
1707
	}
1708

1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
	/* 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;
	}

1719
	ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1720 1721 1722
	if (ret < 0)
		atomic_dec(&dev->nmappings);

1723 1724
done:
	mutex_unlock(&chain->ctrl_mutex);
1725 1726 1727
	return ret;
}

1728
/*
1729 1730 1731
 * 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.
1732
 */
1733 1734
static void uvc_ctrl_prune_entity(struct uvc_device *dev,
	struct uvc_entity *entity)
1735
{
1736
	struct uvc_ctrl_blacklist {
1737 1738
		struct usb_device_id id;
		u8 index;
1739 1740 1741
	};

	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1742
		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1743 1744
		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1745
	};
1746 1747 1748
	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
	};
1749

1750
	const struct uvc_ctrl_blacklist *blacklist;
1751
	unsigned int size;
1752
	unsigned int count;
1753
	unsigned int i;
1754
	u8 *controls;
1755

1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
	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;
1770

1771 1772 1773
	default:
		return;
	}
1774

1775
	for (i = 0; i < count; ++i) {
1776
		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1777 1778
			continue;

1779 1780
		if (blacklist[i].index >= 8 * size ||
		    !uvc_test_bit(controls, blacklist[i].index))
1781 1782
			continue;

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

1786
		uvc_clear_bit(controls, blacklist[i].index);
1787 1788 1789
	}
}

1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
/*
 * 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);

1802 1803 1804 1805 1806 1807
	/* 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)
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
		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;
		 }
	}

1818
	if (!ctrl->initialized)
1819 1820 1821 1822
		return;

	for (; mapping < mend; ++mapping) {
		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
1823
		    ctrl->info.selector == mapping->selector)
1824 1825 1826 1827
			__uvc_ctrl_add_mapping(dev, ctrl, mapping);
	}
}

1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
/*
 * 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) {
1838
		struct uvc_control *ctrl;
1839 1840 1841
		unsigned int bControlSize = 0, ncontrols = 0;
		__u8 *bmControls = NULL;

1842
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1843 1844
			bmControls = entity->extension.bmControls;
			bControlSize = entity->extension.bControlSize;
1845
		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1846 1847
			bmControls = entity->processing.bmControls;
			bControlSize = entity->processing.bControlSize;
1848
		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1849 1850 1851 1852
			bmControls = entity->camera.bmControls;
			bControlSize = entity->camera.bControlSize;
		}

1853
		/* Remove bogus/blacklisted controls */
1854
		uvc_ctrl_prune_entity(dev, entity);
1855

1856
		/* Count supported controls and allocate the controls array */
1857 1858 1859 1860 1861
		for (i = 0; i < bControlSize; ++i)
			ncontrols += hweight8(bmControls[i]);
		if (ncontrols == 0)
			continue;

1862 1863
		entity->controls = kzalloc(ncontrols * sizeof(*ctrl),
					   GFP_KERNEL);
1864 1865 1866 1867
		if (entity->controls == NULL)
			return -ENOMEM;
		entity->ncontrols = ncontrols;

1868
		/* Initialize all supported controls */
1869 1870
		ctrl = entity->controls;
		for (i = 0; i < bControlSize * 8; ++i) {
1871
			if (uvc_test_bit(bmControls, i) == 0)
1872 1873 1874 1875
				continue;

			ctrl->entity = entity;
			ctrl->index = i;
1876 1877

			uvc_ctrl_init_ctrl(dev, ctrl);
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
			ctrl++;
		}
	}

	return 0;
}

/*
 * Cleanup device controls.
 */
1888 1889
static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
	struct uvc_control *ctrl)
1890
{
1891
	struct uvc_control_mapping *mapping, *nm;
1892

1893
	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
1894 1895 1896
		list_del(&mapping->list);
		kfree(mapping->menu_info);
		kfree(mapping);
1897 1898 1899
	}
}

1900
void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1901
{
1902 1903
	struct uvc_entity *entity;
	unsigned int i;
1904

1905 1906 1907 1908 1909
	/* 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];

1910
			if (!ctrl->initialized)
1911
				continue;
1912

1913 1914
			uvc_ctrl_cleanup_mappings(dev, ctrl);
			kfree(ctrl->uvc_data);
1915 1916
		}

1917
		kfree(entity->controls);
1918 1919
	}
}