uvc_ctrl.c 56.4 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
#include <media/v4l2-ctrls.h>
25 26 27 28 29

#include "uvcvideo.h"

#define UVC_CTRL_DATA_CURRENT	0
#define UVC_CTRL_DATA_BACKUP	1
30 31 32 33 34
#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
35 36

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

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

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

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

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

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

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
static __s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
{
	unsigned int first = mapping->offset / 8;
	__s8 rel = (__s8)data[first];

	switch (query) {
	case UVC_GET_CUR:
		return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
						 : -data[first+1]);
	case UVC_GET_MIN:
		return -data[first+1];
	case UVC_GET_MAX:
	case UVC_GET_RES:
	case UVC_GET_DEF:
	default:
		return data[first+1];
	}
}

static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
	__s32 value, __u8 *data)
{
	unsigned int first = mapping->offset / 8;

	data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
	data[first+1] = min_t(int, abs(value), 0xff);
}

423 424 425 426 427
static struct uvc_control_mapping uvc_ctrl_mappings[] = {
	{
		.id		= V4L2_CID_BRIGHTNESS,
		.name		= "Brightness",
		.entity		= UVC_GUID_UVC_PROCESSING,
428
		.selector	= UVC_PU_BRIGHTNESS_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_SIGNED,
	},
	{
		.id		= V4L2_CID_CONTRAST,
		.name		= "Contrast",
		.entity		= UVC_GUID_UVC_PROCESSING,
438
		.selector	= UVC_PU_CONTRAST_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_HUE,
		.name		= "Hue",
		.entity		= UVC_GUID_UVC_PROCESSING,
448
		.selector	= UVC_PU_HUE_CONTROL,
449 450 451 452
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
453 454
		.master_id	= V4L2_CID_HUE_AUTO,
		.master_manual	= 0,
455 456 457 458 459
	},
	{
		.id		= V4L2_CID_SATURATION,
		.name		= "Saturation",
		.entity		= UVC_GUID_UVC_PROCESSING,
460
		.selector	= UVC_PU_SATURATION_CONTROL,
461 462 463 464 465 466 467 468 469
		.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,
470
		.selector	= UVC_PU_SHARPNESS_CONTROL,
471 472 473 474 475 476 477 478 479
		.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,
480
		.selector	= UVC_PU_GAMMA_CONTROL,
481 482 483 484 485 486 487 488 489
		.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,
490
		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
491 492 493 494 495 496 497 498 499
		.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,
500
		.selector	= UVC_PU_GAIN_CONTROL,
501 502 503 504 505 506 507 508 509
		.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,
510
		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
511 512 513 514 515 516 517 518 519 520 521
		.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,
522
		.selector	= UVC_PU_HUE_AUTO_CONTROL,
523 524 525 526
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
527
		.slave_ids	= { V4L2_CID_HUE, },
528 529 530 531 532
	},
	{
		.id		= V4L2_CID_EXPOSURE_AUTO,
		.name		= "Exposure, Auto",
		.entity		= UVC_GUID_UVC_CAMERA,
533
		.selector	= UVC_CT_AE_MODE_CONTROL,
534 535 536 537 538 539
		.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),
540
		.slave_ids	= { V4L2_CID_EXPOSURE_ABSOLUTE, },
541 542 543 544 545
	},
	{
		.id		= V4L2_CID_EXPOSURE_AUTO_PRIORITY,
		.name		= "Exposure, Auto Priority",
		.entity		= UVC_GUID_UVC_CAMERA,
546
		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
547 548 549 550 551 552 553 554 555
		.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,
556
		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
557 558 559 560
		.size		= 32,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
561 562
		.master_id	= V4L2_CID_EXPOSURE_AUTO,
		.master_manual	= V4L2_EXPOSURE_MANUAL,
563 564 565 566 567
	},
	{
		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
		.name		= "White Balance Temperature, Auto",
		.entity		= UVC_GUID_UVC_PROCESSING,
568
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
569 570 571 572
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
573
		.slave_ids	= { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
574 575 576 577 578
	},
	{
		.id		= V4L2_CID_WHITE_BALANCE_TEMPERATURE,
		.name		= "White Balance Temperature",
		.entity		= UVC_GUID_UVC_PROCESSING,
579
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
580 581 582 583
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
584 585
		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
		.master_manual	= 0,
586 587 588 589 590
	},
	{
		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
		.name		= "White Balance Component, Auto",
		.entity		= UVC_GUID_UVC_PROCESSING,
591
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
592 593 594 595
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
596 597
		.slave_ids	= { V4L2_CID_BLUE_BALANCE,
				    V4L2_CID_RED_BALANCE },
598 599 600 601 602
	},
	{
		.id		= V4L2_CID_BLUE_BALANCE,
		.name		= "White Balance Blue Component",
		.entity		= UVC_GUID_UVC_PROCESSING,
603
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
604 605 606 607
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
608 609
		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
		.master_manual	= 0,
610 611 612 613 614
	},
	{
		.id		= V4L2_CID_RED_BALANCE,
		.name		= "White Balance Red Component",
		.entity		= UVC_GUID_UVC_PROCESSING,
615
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
616 617 618 619
		.size		= 16,
		.offset		= 16,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
620 621
		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
		.master_manual	= 0,
622 623 624 625 626
	},
	{
		.id		= V4L2_CID_FOCUS_ABSOLUTE,
		.name		= "Focus (absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
627
		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
628 629 630 631
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
632 633
		.master_id	= V4L2_CID_FOCUS_AUTO,
		.master_manual	= 0,
634 635 636 637 638
	},
	{
		.id		= V4L2_CID_FOCUS_AUTO,
		.name		= "Focus, Auto",
		.entity		= UVC_GUID_UVC_CAMERA,
639
		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
640 641 642 643
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
644
		.slave_ids	= { V4L2_CID_FOCUS_ABSOLUTE, },
645
	},
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	{
		.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,
	},
666 667 668 669
	{
		.id		= V4L2_CID_ZOOM_ABSOLUTE,
		.name		= "Zoom, Absolute",
		.entity		= UVC_GUID_UVC_CAMERA,
670
		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
671 672 673 674 675 676 677 678 679
		.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,
680
		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
681 682 683 684 685 686 687
		.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,
	},
688 689 690 691 692 693 694 695
	{
		.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,
696
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
697 698 699 700 701 702 703 704 705
	},
	{
		.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,
706
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
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
	{
		.id		= V4L2_CID_PAN_SPEED,
		.name		= "Pan (Speed)",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
		.get		= uvc_ctrl_get_rel_speed,
		.set		= uvc_ctrl_set_rel_speed,
	},
	{
		.id		= V4L2_CID_TILT_SPEED,
		.name		= "Tilt (Speed)",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
		.size		= 16,
		.offset		= 16,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
		.get		= uvc_ctrl_get_rel_speed,
		.set		= uvc_ctrl_set_rel_speed,
	},
732 733 734 735
	{
		.id		= V4L2_CID_PRIVACY,
		.name		= "Privacy",
		.entity		= UVC_GUID_UVC_CAMERA,
736
		.selector	= UVC_CT_PRIVACY_CONTROL,
737 738 739 740 741
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
742 743 744 745 746 747 748 749
};

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

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

753
static inline int uvc_test_bit(const __u8 *data, int bit)
754 755 756 757
{
	return (data[bit >> 3] >> (bit & 7)) & 1;
}

758 759 760 761 762
static inline void uvc_clear_bit(__u8 *data, int bit)
{
	data[bit >> 3] &= ~(1 << (bit & 7));
}

763 764 765 766 767
/* 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.
 */
768 769
static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
{
	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;
	}

788
	/* Sign-extend the value if needed. */
789 790 791 792 793 794 795 796 797
	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'.
 */
798 799
static void uvc_set_le_value(struct uvc_control_mapping *mapping,
	__s32 value, __u8 *data)
800 801 802 803 804
{
	int bits = mapping->size;
	int offset = mapping->offset;
	__u8 mask;

805 806 807 808 809 810 811 812
	/* 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;

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	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;

834 835
static int uvc_entity_match_guid(const struct uvc_entity *entity,
	const __u8 guid[16])
836 837
{
	switch (UVC_ENTITY_TYPE(entity)) {
838
	case UVC_ITT_CAMERA:
839 840
		return memcmp(uvc_camera_guid, guid, 16) == 0;

841
	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
842 843
		return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;

844
	case UVC_VC_PROCESSING_UNIT:
845 846
		return memcmp(uvc_processing_guid, guid, 16) == 0;

847
	case UVC_VC_EXTENSION_UNIT:
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
		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];
873
		if (!ctrl->initialized)
874 875
			continue;

876
		list_for_each_entry(map, &ctrl->info.mappings, list) {
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
			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;
			}
		}
	}
}

892
static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
893 894 895 896 897 898 899 900 901 902 903 904
	__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. */
905
	list_for_each_entry(entity, &chain->entities, chain) {
906 907 908 909 910 911 912 913 914 915 916 917
		__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;
}

918 919 920 921 922
static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
	struct uvc_control *ctrl)
{
	int ret;

923
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
924
		ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
925
				     chain->dev->intfnum, ctrl->info.selector,
926
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
927
				     ctrl->info.size);
928 929 930 931
		if (ret < 0)
			return ret;
	}

932
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
933
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
934
				     chain->dev->intfnum, ctrl->info.selector,
935
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
936
				     ctrl->info.size);
937 938 939
		if (ret < 0)
			return ret;
	}
940
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
941
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
942
				     chain->dev->intfnum, ctrl->info.selector,
943
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
944
				     ctrl->info.size);
945 946 947
		if (ret < 0)
			return ret;
	}
948
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
949
		ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
950
				     chain->dev->intfnum, ctrl->info.selector,
951
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
952
				     ctrl->info.size);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
		if (ret < 0) {
			if (UVC_ENTITY_TYPE(ctrl->entity) !=
			    UVC_VC_EXTENSION_UNIT)
				return ret;

			/* GET_RES is mandatory for XU controls, but some
			 * cameras still choke on it. Ignore errors and set the
			 * resolution value to zero.
			 */
			uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
				      "UVC non compliance - GET_RES failed on "
				      "an XU control. Enabling workaround.\n");
			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
			       ctrl->info.size);
		}
968 969 970 971 972 973
	}

	ctrl->cached = 1;
	return 0;
}

974 975 976 977 978 979 980 981 982
static int __uvc_ctrl_get(struct uvc_video_chain *chain,
	struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
	s32 *value)
{
	struct uvc_menu_info *menu;
	unsigned int i;
	int ret;

	if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
983
		return -EACCES;
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

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

		ctrl->loaded = 1;
	}

	*value = mapping->get(mapping, UVC_GET_CUR,
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));

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

	return 0;
}

1012 1013 1014
static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
	struct uvc_control *ctrl,
	struct uvc_control_mapping *mapping,
1015 1016
	struct v4l2_queryctrl *v4l2_ctrl)
{
1017 1018
	struct uvc_control_mapping *master_map = NULL;
	struct uvc_control *master_ctrl = NULL;
1019 1020 1021
	struct uvc_menu_info *menu;
	unsigned int i;

1022
	memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
1023 1024
	v4l2_ctrl->id = mapping->id;
	v4l2_ctrl->type = mapping->v4l2_type;
1025
	strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
1026 1027
	v4l2_ctrl->flags = 0;

1028
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1029
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1030
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1031 1032
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	if (mapping->master_id)
		__uvc_find_control(ctrl->entity, mapping->master_id,
				   &master_map, &master_ctrl, 0);
	if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
		s32 val;
		int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
		if (ret < 0)
			return ret;

		if (val != mapping->master_manual)
				v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
	}

1046
	if (!ctrl->cached) {
1047
		int ret = uvc_ctrl_populate_cache(chain, ctrl);
1048
		if (ret < 0)
1049
			return ret;
1050 1051
	}

1052
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1053 1054
		v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1055 1056
	}

1057 1058
	switch (mapping->v4l2_type) {
	case V4L2_CTRL_TYPE_MENU:
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		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;
			}
		}

1071
		return 0;
1072 1073 1074 1075 1076

	case V4L2_CTRL_TYPE_BOOLEAN:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 1;
		v4l2_ctrl->step = 1;
1077
		return 0;
1078

1079 1080 1081 1082
	case V4L2_CTRL_TYPE_BUTTON:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 0;
		v4l2_ctrl->step = 0;
1083
		return 0;
1084

1085 1086
	default:
		break;
1087 1088
	}

1089
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1090 1091
		v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1092

1093
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1094 1095 1096
		v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));

1097
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1098 1099 1100
		v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
				  uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	return 0;
}

int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
	struct v4l2_queryctrl *v4l2_ctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
	int ret;

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

	ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
	if (ctrl == NULL) {
1117
		ret = -EINVAL;
1118 1119 1120 1121
		goto done;
	}

	ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1122 1123 1124
done:
	mutex_unlock(&chain->ctrl_mutex);
	return ret;
1125 1126
}

1127 1128 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
/*
 * 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);
1155 1156
	if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
		ret = -EINVAL;
1157 1158 1159
		goto done;
	}

1160
	if (query_menu->index >= mapping->menu_count) {
1161 1162 1163 1164 1165
		ret = -EINVAL;
		goto done;
	}

	menu_info = &mapping->menu_info[query_menu->index];
1166

1167 1168
	if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
	    (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
		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;
		}
	}

1185 1186 1187 1188 1189 1190 1191
	strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);

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

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
/* --------------------------------------------------------------------------
 * Ctrl event handling
 */

static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
	struct v4l2_event *ev,
	struct uvc_control *ctrl,
	struct uvc_control_mapping *mapping,
	s32 value, u32 changes)
{
	struct v4l2_queryctrl v4l2_ctrl;

	__uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);

	memset(ev->reserved, 0, sizeof(ev->reserved));
	ev->type = V4L2_EVENT_CTRL;
	ev->id = v4l2_ctrl.id;
	ev->u.ctrl.value = value;
	ev->u.ctrl.changes = changes;
	ev->u.ctrl.type = v4l2_ctrl.type;
	ev->u.ctrl.flags = v4l2_ctrl.flags;
	ev->u.ctrl.minimum = v4l2_ctrl.minimum;
	ev->u.ctrl.maximum = v4l2_ctrl.maximum;
	ev->u.ctrl.step = v4l2_ctrl.step;
	ev->u.ctrl.default_value = v4l2_ctrl.default_value;
}

static void uvc_ctrl_send_event(struct uvc_fh *handle,
	struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
	s32 value, u32 changes)
{
	struct v4l2_subscribed_event *sev;
	struct v4l2_event ev;

	if (list_empty(&mapping->ev_subs))
		return;

	uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes);

	list_for_each_entry(sev, &mapping->ev_subs, node) {
		if (sev->fh && (sev->fh != &handle->vfh ||
1233 1234
		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
		    (changes & V4L2_EVENT_CTRL_CH_FLAGS)))
1235 1236 1237 1238
			v4l2_event_queue_fh(sev->fh, &ev);
	}
}

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
static void uvc_ctrl_send_slave_event(struct uvc_fh *handle,
	struct uvc_control *master, u32 slave_id,
	const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
{
	struct uvc_control_mapping *mapping = NULL;
	struct uvc_control *ctrl = NULL;
	u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
	unsigned int i;
	s32 val = 0;

	/*
	 * We can skip sending an event for the slave if the slave
	 * is being modified in the same transaction.
	 */
	for (i = 0; i < xctrls_count; i++) {
		if (xctrls[i].id == slave_id)
			return;
	}

	__uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
	if (ctrl == NULL)
		return;

	if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
		changes |= V4L2_EVENT_CTRL_CH_VALUE;

	uvc_ctrl_send_event(handle, ctrl, mapping, val, changes);
}

1268 1269 1270 1271 1272
static void uvc_ctrl_send_events(struct uvc_fh *handle,
	const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
{
	struct uvc_control_mapping *mapping;
	struct uvc_control *ctrl;
1273
	u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1274
	unsigned int i;
1275
	unsigned int j;
1276 1277 1278

	for (i = 0; i < xctrls_count; ++i) {
		ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

		for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
			if (!mapping->slave_ids[j])
				break;
			uvc_ctrl_send_slave_event(handle, ctrl,
						  mapping->slave_ids[j],
						  xctrls, xctrls_count);
		}

		/*
		 * If the master is being modified in the same transaction
		 * flags may change too.
		 */
		if (mapping->master_id) {
			for (j = 0; j < xctrls_count; j++) {
				if (xctrls[j].id == mapping->master_id) {
					changes |= V4L2_EVENT_CTRL_CH_FLAGS;
					break;
				}
			}
		}

1301
		uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
1302
				    changes);
1303 1304 1305
	}
}

1306
static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
{
	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
	struct uvc_control_mapping *mapping;
	struct uvc_control *ctrl;
	int ret;

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

	ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
	if (ctrl == NULL) {
1319
		ret = -EINVAL;
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
		goto done;
	}

	list_add_tail(&sev->node, &mapping->ev_subs);
	if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
		struct v4l2_event ev;
		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
		s32 val = 0;

		if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
			changes |= V4L2_EVENT_CTRL_CH_VALUE;

		uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
				    changes);
1334 1335 1336
		/* Mark the queue as active, allowing this initial
		   event to be accepted. */
		sev->elems = elems;
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
		v4l2_event_queue_fh(sev->fh, &ev);
	}

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

static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
{
	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);

	mutex_lock(&handle->chain->ctrl_mutex);
	list_del(&sev->node);
	mutex_unlock(&handle->chain->ctrl_mutex);
}

const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
	.add = uvc_ctrl_add_event,
	.del = uvc_ctrl_del_event,
	.replace = v4l2_ctrl_replace,
	.merge = v4l2_ctrl_merge,
};
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384

/* --------------------------------------------------------------------------
 * 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.
 */
1385
int uvc_ctrl_begin(struct uvc_video_chain *chain)
1386
{
1387
	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
}

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];
1402
		if (!ctrl->initialized)
1403 1404 1405 1406
			continue;

		/* Reset the loaded flag for auto-update controls that were
		 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1407 1408 1409
		 * uvc_ctrl_get from using the cached value, and for write-only
		 * controls to prevent uvc_ctrl_set from setting bits not
		 * explicitly set by the user.
1410
		 */
1411 1412
		if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
		    !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1413 1414 1415
			ctrl->loaded = 0;

		if (!ctrl->dirty)
1416 1417 1418
			continue;

		if (!rollback)
1419
			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1420
				dev->intfnum, ctrl->info.selector,
1421
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1422
				ctrl->info.size);
1423 1424 1425 1426 1427 1428
		else
			ret = 0;

		if (rollback || ret < 0)
			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1429
			       ctrl->info.size);
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439

		ctrl->dirty = 0;

		if (ret < 0)
			return ret;
	}

	return 0;
}

1440 1441 1442
int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
		      const struct v4l2_ext_control *xctrls,
		      unsigned int xctrls_count)
1443
{
1444
	struct uvc_video_chain *chain = handle->chain;
1445 1446 1447 1448
	struct uvc_entity *entity;
	int ret = 0;

	/* Find the control. */
1449
	list_for_each_entry(entity, &chain->entities, chain) {
1450
		ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1451 1452 1453 1454
		if (ret < 0)
			goto done;
	}

1455 1456
	if (!rollback)
		uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1457
done:
1458
	mutex_unlock(&chain->ctrl_mutex);
1459 1460 1461
	return ret;
}

1462 1463 1464 1465 1466 1467 1468 1469
int uvc_ctrl_get(struct uvc_video_chain *chain,
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;

	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
	if (ctrl == NULL)
1470
		return -EINVAL;
1471 1472 1473 1474

	return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
}

1475
int uvc_ctrl_set(struct uvc_video_chain *chain,
1476 1477 1478 1479
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
1480 1481 1482 1483
	s32 value;
	u32 step;
	s32 min;
	s32 max;
1484 1485
	int ret;

1486
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1487
	if (ctrl == NULL)
1488
		return -EINVAL;
1489 1490
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
		return -EACCES;
1491

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	/* 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));
1507 1508
		if (step == 0)
			step = 1;
1509

1510 1511 1512 1513 1514 1515
		xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
			     / step * step;
		if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
			xctrl->value = clamp(xctrl->value, min, max);
		else
			xctrl->value = clamp_t(u32, xctrl->value, min, max);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
		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)
1526
			return -ERANGE;
1527
		value = mapping->menu_info[xctrl->value].value;
1528 1529 1530 1531

		/* Valid menu indices are reported by the GET_RES request for
		 * UVC controls that support it.
		 */
1532 1533
		if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
		    (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1534 1535 1536 1537 1538 1539 1540 1541 1542
			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))
1543
				return -EINVAL;
1544 1545
		}

1546 1547 1548 1549 1550
		break;

	default:
		value = xctrl->value;
		break;
1551 1552
	}

1553 1554 1555 1556
	/* 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.
	 */
1557
	if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1558
		if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1559
			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1560
				0, ctrl->info.size);
1561
		} else {
1562 1563
			ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
				ctrl->entity->id, chain->dev->intfnum,
1564
				ctrl->info.selector,
1565
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1566
				ctrl->info.size);
1567 1568 1569 1570
			if (ret < 0)
				return ret;
		}

1571
		ctrl->loaded = 1;
1572 1573
	}

1574
	/* Backup the current value in case we need to rollback later. */
1575 1576 1577
	if (!ctrl->dirty) {
		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1578
		       ctrl->info.size);
1579 1580
	}

1581 1582
	mapping->set(mapping, value,
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592

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

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

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
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,
1605 1606 1607
			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 },
1608
		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1609 1610 1611
			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 },
1612
		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1613 1614 1615
			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 },
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
	};

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

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
/*
 * 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;
	}

1672 1673 1674 1675 1676 1677
	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)
1678
		    | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1679
		       UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1680

1681 1682
	uvc_ctrl_fixup_xu_info(dev, ctrl, info);

1683 1684 1685
	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,
1686 1687 1688
		  (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);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719

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

1720
int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1721
	struct uvc_xu_control_query *xqry)
1722 1723
{
	struct uvc_entity *entity;
1724
	struct uvc_control *ctrl;
1725
	unsigned int i, found = 0;
1726 1727 1728
	__u32 reqflags;
	__u16 size;
	__u8 *data = NULL;
1729 1730 1731
	int ret;

	/* Find the extension unit. */
1732 1733
	list_for_each_entry(entity, &chain->entities, chain) {
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1734
		    entity->id == xqry->unit)
1735 1736 1737
			break;
	}

1738
	if (entity->id != xqry->unit) {
1739
		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1740 1741
			xqry->unit);
		return -ENOENT;
1742 1743
	}

1744
	/* Find the control and perform delayed initialization if needed. */
1745 1746
	for (i = 0; i < entity->ncontrols; ++i) {
		ctrl = &entity->controls[i];
1747
		if (ctrl->index == xqry->selector - 1) {
1748 1749 1750 1751 1752 1753
			found = 1;
			break;
		}
	}

	if (!found) {
1754
		uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1755 1756
			entity->extension.guidExtensionCode, xqry->selector);
		return -ENOENT;
1757 1758
	}

1759 1760 1761
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1762
	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1763 1764 1765 1766
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}
1767

1768 1769 1770 1771 1772 1773
	/* Validate the required buffer size and flags for the request */
	reqflags = 0;
	size = ctrl->info.size;

	switch (xqry->query) {
	case UVC_GET_CUR:
1774
		reqflags = UVC_CTRL_FLAG_GET_CUR;
1775 1776
		break;
	case UVC_GET_MIN:
1777
		reqflags = UVC_CTRL_FLAG_GET_MIN;
1778 1779
		break;
	case UVC_GET_MAX:
1780
		reqflags = UVC_CTRL_FLAG_GET_MAX;
1781 1782
		break;
	case UVC_GET_DEF:
1783
		reqflags = UVC_CTRL_FLAG_GET_DEF;
1784 1785
		break;
	case UVC_GET_RES:
1786
		reqflags = UVC_CTRL_FLAG_GET_RES;
1787 1788
		break;
	case UVC_SET_CUR:
1789
		reqflags = UVC_CTRL_FLAG_SET_CUR;
1790 1791 1792 1793 1794 1795 1796 1797
		break;
	case UVC_GET_LEN:
		size = 2;
		break;
	case UVC_GET_INFO:
		size = 1;
		break;
	default:
1798 1799 1800
		ret = -EINVAL;
		goto done;
	}
1801

1802 1803
	if (size != xqry->size) {
		ret = -ENOBUFS;
1804 1805
		goto done;
	}
1806

1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
	if (reqflags && !(ctrl->info.flags & reqflags)) {
		ret = -EBADRQC;
		goto done;
	}

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

1818 1819
	if (xqry->query == UVC_SET_CUR &&
	    copy_from_user(data, xqry->data, size)) {
1820
		ret = -EFAULT;
1821
		goto done;
1822 1823
	}

1824 1825
	ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
			     chain->dev->intfnum, xqry->selector, data, size);
1826
	if (ret < 0)
1827
		goto done;
1828

1829 1830
	if (xqry->query != UVC_SET_CUR &&
	    copy_to_user(xqry->data, data, size))
1831
		ret = -EFAULT;
1832
done:
1833
	kfree(data);
1834
	mutex_unlock(&chain->ctrl_mutex);
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
	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).
 */
1851
int uvc_ctrl_restore_values(struct uvc_device *dev)
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
{
	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];

1864
			if (!ctrl->initialized || !ctrl->modified ||
1865
			    (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1866 1867
				continue;

1868
			printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1869 1870
				ctrl->info.entity, ctrl->info.index,
				ctrl->info.selector);
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
			ctrl->dirty = 1;
		}

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

	return 0;
}

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

1886 1887 1888 1889 1890 1891 1892
/*
 * 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;
1893

1894
	ctrl->info = *info;
1895
	INIT_LIST_HEAD(&ctrl->info.mappings);
1896 1897

	/* Allocate an array to save control values (cur, def, max, etc.) */
1898
	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1899 1900 1901 1902
				 GFP_KERNEL);
	if (ctrl->uvc_data == NULL) {
		ret = -ENOMEM;
		goto done;
1903
	}
1904

1905 1906
	ctrl->initialized = 1;

1907
	uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1908
		"entity %u\n", ctrl->info.entity, ctrl->info.selector,
1909
		dev->udev->devpath, ctrl->entity->id);
1910 1911

done:
1912
	if (ret < 0)
1913
		kfree(ctrl->uvc_data);
1914
	return ret;
1915 1916 1917
}

/*
1918
 * Add a control mapping to a given control.
1919
 */
1920 1921
static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1922
{
1923 1924
	struct uvc_control_mapping *map;
	unsigned int size;
1925

1926 1927 1928
	/* 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.
1929
	 */
1930 1931 1932
	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
	if (map == NULL)
		return -ENOMEM;
1933

1934 1935
	INIT_LIST_HEAD(&map->ev_subs);

1936 1937 1938 1939 1940
	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;
1941 1942
	}

1943 1944 1945 1946
	if (map->get == NULL)
		map->get = uvc_get_le_value;
	if (map->set == NULL)
		map->set = uvc_set_le_value;
1947

1948
	list_add_tail(&map->list, &ctrl->info.mappings);
1949 1950
	uvc_trace(UVC_TRACE_CONTROL,
		"Adding mapping '%s' to control %pUl/%u.\n",
1951
		map->name, ctrl->info.entity, ctrl->info.selector);
1952 1953

	return 0;
1954 1955
}

1956 1957
int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
	const struct uvc_control_mapping *mapping)
1958
{
1959
	struct uvc_device *dev = chain->dev;
1960
	struct uvc_control_mapping *map;
1961 1962 1963
	struct uvc_entity *entity;
	struct uvc_control *ctrl;
	int found = 0;
1964
	int ret;
1965

1966
	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1967
		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1968
			"id 0x%08x is invalid.\n", mapping->name,
1969 1970 1971 1972
			mapping->id);
		return -EINVAL;
	}

1973 1974
	/* Search for the matching (GUID/CS) control on the current chain */
	list_for_each_entry(entity, &chain->entities, chain) {
1975
		unsigned int i;
1976

1977 1978
		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
		    !uvc_entity_match_guid(entity, mapping->entity))
1979
			continue;
1980

1981 1982
		for (i = 0; i < entity->ncontrols; ++i) {
			ctrl = &entity->controls[i];
1983
			if (ctrl->index == mapping->selector - 1) {
1984 1985
				found = 1;
				break;
1986 1987 1988
			}
		}

1989 1990 1991 1992 1993
		if (found)
			break;
	}
	if (!found)
		return -ENOENT;
1994

1995 1996 1997
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1998 1999 2000 2001 2002 2003 2004
	/* Perform delayed initialization of XU controls */
	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}

2005
	list_for_each_entry(map, &ctrl->info.mappings, list) {
2006 2007 2008 2009 2010 2011 2012
		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;
		}
2013
	}
2014

2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
	/* 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;
	}

2025
	ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2026 2027 2028
	if (ret < 0)
		atomic_dec(&dev->nmappings);

2029 2030
done:
	mutex_unlock(&chain->ctrl_mutex);
2031 2032 2033
	return ret;
}

2034
/*
2035 2036 2037
 * 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.
2038
 */
2039 2040
static void uvc_ctrl_prune_entity(struct uvc_device *dev,
	struct uvc_entity *entity)
2041
{
2042
	struct uvc_ctrl_blacklist {
2043 2044
		struct usb_device_id id;
		u8 index;
2045 2046 2047
	};

	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2048
		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2049 2050
		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2051
	};
2052 2053 2054
	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
	};
2055

2056
	const struct uvc_ctrl_blacklist *blacklist;
2057
	unsigned int size;
2058
	unsigned int count;
2059
	unsigned int i;
2060
	u8 *controls;
2061

2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
	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;
2076

2077 2078 2079
	default:
		return;
	}
2080

2081
	for (i = 0; i < count; ++i) {
2082
		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2083 2084
			continue;

2085 2086
		if (blacklist[i].index >= 8 * size ||
		    !uvc_test_bit(controls, blacklist[i].index))
2087 2088
			continue;

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

2092
		uvc_clear_bit(controls, blacklist[i].index);
2093 2094 2095
	}
}

2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
/*
 * 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);

2108 2109 2110 2111 2112 2113
	/* 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)
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
		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;
		 }
	}

2124
	if (!ctrl->initialized)
2125 2126 2127 2128
		return;

	for (; mapping < mend; ++mapping) {
		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2129
		    ctrl->info.selector == mapping->selector)
2130 2131 2132 2133
			__uvc_ctrl_add_mapping(dev, ctrl, mapping);
	}
}

2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
/*
 * 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) {
2144
		struct uvc_control *ctrl;
A
Akinobu Mita 已提交
2145
		unsigned int bControlSize = 0, ncontrols;
2146 2147
		__u8 *bmControls = NULL;

2148
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2149 2150
			bmControls = entity->extension.bmControls;
			bControlSize = entity->extension.bControlSize;
2151
		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2152 2153
			bmControls = entity->processing.bmControls;
			bControlSize = entity->processing.bControlSize;
2154
		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2155 2156 2157 2158
			bmControls = entity->camera.bmControls;
			bControlSize = entity->camera.bControlSize;
		}

2159
		/* Remove bogus/blacklisted controls */
2160
		uvc_ctrl_prune_entity(dev, entity);
2161

2162
		/* Count supported controls and allocate the controls array */
A
Akinobu Mita 已提交
2163
		ncontrols = memweight(bmControls, bControlSize);
2164 2165 2166
		if (ncontrols == 0)
			continue;

2167
		entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2168
					   GFP_KERNEL);
2169 2170 2171 2172
		if (entity->controls == NULL)
			return -ENOMEM;
		entity->ncontrols = ncontrols;

2173
		/* Initialize all supported controls */
2174 2175
		ctrl = entity->controls;
		for (i = 0; i < bControlSize * 8; ++i) {
2176
			if (uvc_test_bit(bmControls, i) == 0)
2177 2178 2179 2180
				continue;

			ctrl->entity = entity;
			ctrl->index = i;
2181 2182

			uvc_ctrl_init_ctrl(dev, ctrl);
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
			ctrl++;
		}
	}

	return 0;
}

/*
 * Cleanup device controls.
 */
2193 2194
static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
	struct uvc_control *ctrl)
2195
{
2196
	struct uvc_control_mapping *mapping, *nm;
2197

2198
	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2199 2200 2201
		list_del(&mapping->list);
		kfree(mapping->menu_info);
		kfree(mapping);
2202 2203 2204
	}
}

2205
void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2206
{
2207 2208
	struct uvc_entity *entity;
	unsigned int i;
2209

2210 2211 2212 2213 2214
	/* 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];

2215
			if (!ctrl->initialized)
2216
				continue;
2217

2218 2219
			uvc_ctrl_cleanup_mappings(dev, ctrl);
			kfree(ctrl->uvc_data);
2220 2221
		}

2222
		kfree(entity->controls);
2223 2224
	}
}