uvc_ctrl.c 38.3 KB
Newer Older
1 2 3
/*
 *      uvc_ctrl.c  --  USB Video Class driver - Controls
 *
4
 *      Copyright (C) 2005-2009
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *          Laurent Pinchart (laurent.pinchart@skynet.be)
 *
 *      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>
#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_NDATA		2
#define UVC_CTRL_DATA_CURRENT	0
#define UVC_CTRL_DATA_BACKUP	1

/* ------------------------------------------------------------------------
31
 * Controls
32 33 34 35 36
 */

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

329 330 331 332 333 334
static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
{
	__s8 zoom = (__s8)data[0];

	switch (query) {
335
	case UVC_GET_CUR:
336 337
		return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);

338 339 340 341
	case UVC_GET_MIN:
	case UVC_GET_MAX:
	case UVC_GET_RES:
	case UVC_GET_DEF:
342 343 344 345 346 347 348 349 350
	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;
351
	data[2] = min((int)abs(value), 0xff);
352 353
}

354 355 356 357 358
static struct uvc_control_mapping uvc_ctrl_mappings[] = {
	{
		.id		= V4L2_CID_BRIGHTNESS,
		.name		= "Brightness",
		.entity		= UVC_GUID_UVC_PROCESSING,
359
		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
360 361 362 363 364 365 366 367 368
		.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,
369
		.selector	= UVC_PU_CONTRAST_CONTROL,
370 371 372 373 374 375 376 377 378
		.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,
379
		.selector	= UVC_PU_HUE_CONTROL,
380 381 382 383 384 385 386 387 388
		.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,
389
		.selector	= UVC_PU_SATURATION_CONTROL,
390 391 392 393 394 395 396 397 398
		.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,
399
		.selector	= UVC_PU_SHARPNESS_CONTROL,
400 401 402 403 404 405 406 407 408
		.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,
409
		.selector	= UVC_PU_GAMMA_CONTROL,
410 411 412 413 414 415 416 417 418
		.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,
419
		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
420 421 422 423 424 425 426 427 428
		.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,
429
		.selector	= UVC_PU_GAIN_CONTROL,
430 431 432 433 434 435 436 437 438
		.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,
439
		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
440 441 442 443 444 445 446 447 448 449 450
		.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,
451
		.selector	= UVC_PU_HUE_AUTO_CONTROL,
452 453 454 455 456 457 458 459 460
		.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,
461
		.selector	= UVC_CT_AE_MODE_CONTROL,
462 463 464 465 466 467 468 469 470 471 472
		.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,
473
		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
474 475 476 477 478 479 480 481 482
		.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,
483
		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
484 485 486 487 488 489 490 491 492
		.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,
493
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
494 495 496 497 498 499 500 501 502
		.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,
503
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
504 505 506 507 508 509 510 511 512
		.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,
513
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
514 515 516 517 518 519 520 521 522
		.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,
523
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
524 525 526 527 528 529 530 531 532
		.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,
533
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
534 535 536 537 538 539 540 541 542
		.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,
543
		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
544 545 546 547 548 549 550 551 552
		.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,
553
		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
554 555 556 557 558
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
559 560 561 562
	{
		.id		= V4L2_CID_ZOOM_ABSOLUTE,
		.name		= "Zoom, Absolute",
		.entity		= UVC_GUID_UVC_CAMERA,
563
		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
564 565 566 567 568 569 570 571 572
		.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,
573
		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
574 575 576 577 578 579 580
		.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,
	},
581 582 583 584
	{
		.id		= V4L2_CID_PRIVACY,
		.name		= "Privacy",
		.entity		= UVC_GUID_UVC_CAMERA,
585
		.selector	= UVC_CT_PRIVACY_CONTROL,
586 587 588 589 590
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
591 592 593 594 595 596 597 598 599 600 601
};

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

static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
{
	return ctrl->data + id * ctrl->info->size;
}

602
static inline int uvc_test_bit(const __u8 *data, int bit)
603 604 605 606
{
	return (data[bit >> 3] >> (bit & 7)) & 1;
}

607 608 609 610 611
static inline void uvc_clear_bit(__u8 *data, int bit)
{
	data[bit >> 3] &= ~(1 << (bit & 7));
}

612 613 614 615 616
/* 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.
 */
617 618
static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
{
	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;
	}

637
	/* Sign-extend the value if needed. */
638 639 640 641 642 643 644 645 646
	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'.
 */
647 648
static void uvc_set_le_value(struct uvc_control_mapping *mapping,
	__s32 value, __u8 *data)
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
{
	int bits = mapping->size;
	int offset = mapping->offset;
	__u8 mask;

	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;

static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
{
	switch (UVC_ENTITY_TYPE(entity)) {
678
	case UVC_ITT_CAMERA:
679 680
		return memcmp(uvc_camera_guid, guid, 16) == 0;

681
	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
682 683
		return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;

684
	case UVC_VC_PROCESSING_UNIT:
685 686
		return memcmp(uvc_processing_guid, guid, 16) == 0;

687
	case UVC_VC_EXTENSION_UNIT:
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
		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];
		if (ctrl->info == NULL)
			continue;

		list_for_each_entry(map, &ctrl->info->mappings, list) {
			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;
			}
		}
	}
}

732
struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
733 734 735 736 737 738 739 740 741 742 743 744
	__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. */
745
	list_for_each_entry(entity, &chain->entities, chain) {
746 747 748 749 750 751 752 753 754 755 756 757
		__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;
}

758
int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
759 760 761 762 763 764
	struct v4l2_queryctrl *v4l2_ctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	struct uvc_menu_info *menu;
	unsigned int i;
765
	__u8 *data;
766 767
	int ret;

768
	ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
769 770 771
	if (ctrl == NULL)
		return -EINVAL;

772
	data = kmalloc(ctrl->info->size, GFP_KERNEL);
773 774 775
	if (data == NULL)
		return -ENOMEM;

776
	memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
777 778
	v4l2_ctrl->id = mapping->id;
	v4l2_ctrl->type = mapping->v4l2_type;
779
	strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
780 781 782 783 784 785
	v4l2_ctrl->flags = 0;

	if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;

	if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
786 787 788
		ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
				     chain->dev->intfnum, ctrl->info->selector,
				     data, ctrl->info->size);
789
		if (ret < 0)
790
			goto out;
791 792
		v4l2_ctrl->default_value =
			mapping->get(mapping, UVC_GET_DEF, data);
793 794
	}

795 796
	switch (mapping->v4l2_type) {
	case V4L2_CTRL_TYPE_MENU:
797 798 799 800 801 802 803 804 805 806 807 808
		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;
			}
		}

809 810
		ret = 0;
		goto out;
811 812 813 814 815

	case V4L2_CTRL_TYPE_BOOLEAN:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 1;
		v4l2_ctrl->step = 1;
816 817
		ret = 0;
		goto out;
818

819 820 821 822 823 824 825
	case V4L2_CTRL_TYPE_BUTTON:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 0;
		v4l2_ctrl->step = 0;
		ret = 0;
		goto out;

826 827
	default:
		break;
828 829 830
	}

	if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
831 832 833
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
				     chain->dev->intfnum, ctrl->info->selector,
				     data, ctrl->info->size);
834
		if (ret < 0)
835
			goto out;
836
		v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, data);
837 838
	}
	if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
839 840 841
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
				     chain->dev->intfnum, ctrl->info->selector,
				     data, ctrl->info->size);
842
		if (ret < 0)
843
			goto out;
844
		v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, data);
845 846
	}
	if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
847 848 849
		ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
				     chain->dev->intfnum, ctrl->info->selector,
				     data, ctrl->info->size);
850
		if (ret < 0)
851
			goto out;
852
		v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, data);
853 854
	}

855 856 857 858
	ret = 0;
out:
	kfree(data);
	return ret;
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
}


/* --------------------------------------------------------------------------
 * 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.
 */
886
int uvc_ctrl_begin(struct uvc_video_chain *chain)
887
{
888
	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
889 890 891 892 893 894 895 896 897 898 899 900 901 902
}

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];
903 904 905 906 907 908 909 910 911 912 913
		if (ctrl->info == NULL)
			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.
		 */
		if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
			ctrl->loaded = 0;

		if (!ctrl->dirty)
914 915 916
			continue;

		if (!rollback)
917
			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
				dev->intfnum, ctrl->info->selector,
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
				ctrl->info->size);
		else
			ret = 0;

		if (rollback || ret < 0)
			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
			       ctrl->info->size);

		ctrl->dirty = 0;

		if (ret < 0)
			return ret;
	}

	return 0;
}

938
int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
939 940 941 942 943
{
	struct uvc_entity *entity;
	int ret = 0;

	/* Find the control. */
944
	list_for_each_entry(entity, &chain->entities, chain) {
945
		ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
946 947 948 949 950
		if (ret < 0)
			goto done;
	}

done:
951
	mutex_unlock(&chain->ctrl_mutex);
952 953 954
	return ret;
}

955
int uvc_ctrl_get(struct uvc_video_chain *chain,
956 957 958 959 960 961 962 963
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	struct uvc_menu_info *menu;
	unsigned int i;
	int ret;

964
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
965 966 967 968
	if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
		return -EINVAL;

	if (!ctrl->loaded) {
969 970
		ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
				chain->dev->intfnum, ctrl->info->selector,
971 972 973 974 975
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
				ctrl->info->size);
		if (ret < 0)
			return ret;

976
		ctrl->loaded = 1;
977 978
	}

979
	xctrl->value = mapping->get(mapping, UVC_GET_CUR,
980
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
981 982 983 984 985 986 987 988 989 990 991 992 993 994

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

995
int uvc_ctrl_set(struct uvc_video_chain *chain,
996 997 998 999 1000 1001 1002
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	s32 value = xctrl->value;
	int ret;

1003
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
		return -EINVAL;

	if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
		if (value < 0 || value >= mapping->menu_count)
			return -EINVAL;
		value = mapping->menu_info[value].value;
	}

	if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
		if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
				0, ctrl->info->size);
		} else {
1018 1019
			ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
				ctrl->entity->id, chain->dev->intfnum,
1020 1021 1022 1023 1024 1025 1026
				ctrl->info->selector,
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
				ctrl->info->size);
			if (ret < 0)
				return ret;
		}

1027
		ctrl->loaded = 1;
1028 1029 1030 1031 1032 1033 1034 1035
	}

	if (!ctrl->dirty) {
		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
		       ctrl->info->size);
	}

1036 1037
	mapping->set(mapping, value,
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

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

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

1048
int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1049 1050 1051 1052 1053 1054 1055 1056 1057
	struct uvc_xu_control *xctrl, int set)
{
	struct uvc_entity *entity;
	struct uvc_control *ctrl = NULL;
	unsigned int i, found = 0;
	__u8 *data;
	int ret;

	/* Find the extension unit. */
1058 1059 1060
	list_for_each_entry(entity, &chain->entities, chain) {
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
		    entity->id == xctrl->unit)
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
			break;
	}

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

	/* Find the control. */
	for (i = 0; i < entity->ncontrols; ++i) {
		ctrl = &entity->controls[i];
		if (ctrl->info == NULL)
			continue;

		if (ctrl->info->selector == xctrl->selector) {
			found = 1;
			break;
		}
	}

	if (!found) {
		uvc_trace(UVC_TRACE_CONTROL,
			"Control " UVC_GUID_FORMAT "/%u not found.\n",
			UVC_GUID_ARGS(entity->extension.guidExtensionCode),
			xctrl->selector);
		return -EINVAL;
	}

	/* Validate control data size. */
	if (ctrl->info->size != xctrl->size)
		return -EINVAL;

	if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
	    (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
		return -EINVAL;

1098
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
		return -ERESTARTSYS;

	memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
	       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
	       xctrl->size);
	data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);

	if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
		ret = -EFAULT;
		goto out;
	}

1111 1112
	ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
			     xctrl->unit, chain->dev->intfnum, xctrl->selector,
1113
			     data, xctrl->size);
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	if (ret < 0)
		goto out;

	if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
		ret = -EFAULT;
		goto out;
	}

out:
	if (ret)
		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
		       xctrl->size);

1128
	mutex_unlock(&chain->ctrl_mutex);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	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];

			if (ctrl->info == NULL || !ctrl->modified ||
			    (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
				continue;

			printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
				"/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
				ctrl->info->index, ctrl->info->selector);
			ctrl->dirty = 1;
		}

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

	return 0;
}

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

static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
	struct uvc_control_info *info)
{
	struct uvc_entity *entity;
	struct uvc_control *ctrl = NULL;
	int ret, found = 0;
	unsigned int i;

	list_for_each_entry(entity, &dev->entities, list) {
		if (!uvc_entity_match_guid(entity, info->entity))
			continue;

		for (i = 0; i < entity->ncontrols; ++i) {
			ctrl = &entity->controls[i];
			if (ctrl->index == info->index) {
				found = 1;
				break;
			}
		}

		if (found)
			break;
	}

	if (!found)
		return;

1207
	if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1208 1209 1210 1211 1212 1213 1214
		/* Check if the device control information and length match
		 * the user supplied information.
		 */
		__u32 flags;
		__le16 size;
		__u8 inf;

1215 1216 1217
		ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
			dev->intfnum, info->selector, (__u8 *)&size, 2);
		if (ret < 0) {
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
			uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
				"control " UVC_GUID_FORMAT "/%u (%d).\n",
				UVC_GUID_ARGS(info->entity), info->selector,
				ret);
			return;
		}

		if (info->size != le16_to_cpu(size)) {
			uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
				"/%u size doesn't match user supplied "
				"value.\n", UVC_GUID_ARGS(info->entity),
				info->selector);
			return;
		}

1233 1234 1235
		ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
			dev->intfnum, info->selector, &inf, 1);
		if (ret < 0) {
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 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 1307 1308 1309 1310 1311 1312 1313 1314 1315
			uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
				"control " UVC_GUID_FORMAT "/%u (%d).\n",
				UVC_GUID_ARGS(info->entity), info->selector,
				ret);
			return;
		}

		flags = info->flags;
		if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
		    ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
			uvc_trace(UVC_TRACE_CONTROL, "Control "
				UVC_GUID_FORMAT "/%u flags don't match "
				"supported operations.\n",
				UVC_GUID_ARGS(info->entity), info->selector);
			return;
		}
	}

	ctrl->info = info;
	ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
	uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
		"to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
		ctrl->info->selector, dev->udev->devpath, entity->id);
}

/*
 * Add an item to the UVC control information list, and instantiate a control
 * structure for each device that supports the control.
 */
int uvc_ctrl_add_info(struct uvc_control_info *info)
{
	struct uvc_control_info *ctrl;
	struct uvc_device *dev;
	int ret = 0;

	/* Find matching controls by walking the devices, entities and
	 * controls list.
	 */
	mutex_lock(&uvc_driver.ctrl_mutex);

	/* First check if the list contains a control matching the new one.
	 * Bail out if it does.
	 */
	list_for_each_entry(ctrl, &uvc_driver.controls, list) {
		if (memcmp(ctrl->entity, info->entity, 16))
			continue;

		if (ctrl->selector == info->selector) {
			uvc_trace(UVC_TRACE_CONTROL, "Control "
				UVC_GUID_FORMAT "/%u is already defined.\n",
				UVC_GUID_ARGS(info->entity), info->selector);
			ret = -EEXIST;
			goto end;
		}
		if (ctrl->index == info->index) {
			uvc_trace(UVC_TRACE_CONTROL, "Control "
				UVC_GUID_FORMAT "/%u would overwrite index "
				"%d.\n", UVC_GUID_ARGS(info->entity),
				info->selector, info->index);
			ret = -EEXIST;
			goto end;
		}
	}

	list_for_each_entry(dev, &uvc_driver.devices, list)
		uvc_ctrl_add_ctrl(dev, info);

	INIT_LIST_HEAD(&info->mappings);
	list_add_tail(&info->list, &uvc_driver.controls);
end:
	mutex_unlock(&uvc_driver.ctrl_mutex);
	return ret;
}

int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
{
	struct uvc_control_info *info;
	struct uvc_control_mapping *map;
	int ret = -EINVAL;

1316 1317 1318 1319 1320
	if (mapping->get == NULL)
		mapping->get = uvc_get_le_value;
	if (mapping->set == NULL)
		mapping->set = uvc_set_le_value;

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 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
			"invalid control id 0x%08x\n", mapping->name,
			mapping->id);
		return -EINVAL;
	}

	mutex_lock(&uvc_driver.ctrl_mutex);
	list_for_each_entry(info, &uvc_driver.controls, list) {
		if (memcmp(info->entity, mapping->entity, 16) ||
			info->selector != mapping->selector)
			continue;

		if (info->size * 8 < mapping->size + mapping->offset) {
			uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
				"overflow control " UVC_GUID_FORMAT "/%u\n",
				mapping->name, UVC_GUID_ARGS(info->entity),
				info->selector);
			ret = -EOVERFLOW;
			goto end;
		}

		/* Check if the list contains a mapping matching the new one.
		 * Bail out if it does.
		 */
		list_for_each_entry(map, &info->mappings, list) {
			if (map->id == mapping->id) {
				uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
					"already defined.\n", mapping->name);
				ret = -EEXIST;
				goto end;
			}
		}

		mapping->ctrl = info;
		list_add_tail(&mapping->list, &info->mappings);
		uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
			UVC_GUID_FORMAT "/%u.\n", mapping->name,
			UVC_GUID_ARGS(info->entity), info->selector);

		ret = 0;
		break;
	}
end:
	mutex_unlock(&uvc_driver.ctrl_mutex);
	return ret;
}

1369
/*
1370 1371 1372
 * 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.
1373 1374
 */
static void
1375
uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
1376 1377
{
	static const struct {
1378 1379
		struct usb_device_id id;
		u8 index;
1380
	} blacklist[] = {
1381 1382
		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1383 1384 1385 1386 1387 1388
	};

	u8 *controls;
	unsigned int size;
	unsigned int i;

1389
	if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
1390 1391 1392 1393 1394 1395
		return;

	controls = entity->processing.bmControls;
	size = entity->processing.bControlSize;

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

1399 1400
		if (blacklist[i].index >= 8 * size ||
		    !uvc_test_bit(controls, blacklist[i].index))
1401 1402
			continue;

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

1406
		uvc_clear_bit(controls, blacklist[i].index);
1407 1408 1409
	}
}

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
/*
 * Initialize device controls.
 */
int uvc_ctrl_init_device(struct uvc_device *dev)
{
	struct uvc_control_info *info;
	struct uvc_control *ctrl;
	struct uvc_entity *entity;
	unsigned int i;

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

1425
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1426 1427
			bmControls = entity->extension.bmControls;
			bControlSize = entity->extension.bControlSize;
1428
		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1429 1430
			bmControls = entity->processing.bmControls;
			bControlSize = entity->processing.bControlSize;
1431
		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1432 1433 1434 1435
			bmControls = entity->camera.bmControls;
			bControlSize = entity->camera.bControlSize;
		}

1436
		uvc_ctrl_prune_entity(dev, entity);
1437

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
		for (i = 0; i < bControlSize; ++i)
			ncontrols += hweight8(bmControls[i]);

		if (ncontrols == 0)
			continue;

		entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
		if (entity->controls == NULL)
			return -ENOMEM;

		entity->ncontrols = ncontrols;

		ctrl = entity->controls;
		for (i = 0; i < bControlSize * 8; ++i) {
1452
			if (uvc_test_bit(bmControls, i) == 0)
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
				continue;

			ctrl->entity = entity;
			ctrl->index = i;
			ctrl++;
		}
	}

	/* Walk the controls info list and associate them with the device
	 * controls, then add the device to the global device list. This has
	 * to be done while holding the controls lock, to make sure
	 * uvc_ctrl_add_info() will not get called in-between.
	 */
	mutex_lock(&uvc_driver.ctrl_mutex);
	list_for_each_entry(info, &uvc_driver.controls, list)
		uvc_ctrl_add_ctrl(dev, info);

	list_add_tail(&dev->list, &uvc_driver.devices);
	mutex_unlock(&uvc_driver.ctrl_mutex);

	return 0;
}

/*
 * Cleanup device controls.
 */
void uvc_ctrl_cleanup_device(struct uvc_device *dev)
{
	struct uvc_entity *entity;
	unsigned int i;

	/* Remove the device from the global devices list */
	mutex_lock(&uvc_driver.ctrl_mutex);
	if (dev->list.next != NULL)
		list_del(&dev->list);
	mutex_unlock(&uvc_driver.ctrl_mutex);

	list_for_each_entry(entity, &dev->entities, list) {
		for (i = 0; i < entity->ncontrols; ++i)
			kfree(entity->controls[i].data);

		kfree(entity->controls);
	}
}

void uvc_ctrl_init(void)
{
	struct uvc_control_info *ctrl = uvc_ctrls;
	struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
	struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
	struct uvc_control_mapping *mend =
		mapping + ARRAY_SIZE(uvc_ctrl_mappings);

	for (; ctrl < cend; ++ctrl)
		uvc_ctrl_add_info(ctrl);

	for (; mapping < mend; ++mapping)
		uvc_ctrl_add_mapping(mapping);
}
1512