uvc_ctrl.c 55.2 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 230
		.flags		= UVC_CTRL_FLAG_SET_CUR
				| UVC_CTRL_FLAG_GET_RANGE
				| UVC_CTRL_FLAG_RESTORE,
231
	},
232 233
	{
		.entity		= UVC_GUID_UVC_CAMERA,
234
		.selector	= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
235 236
		.index		= 4,
		.size		= 1,
237
		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
238
	},
239 240
	{
		.entity		= UVC_GUID_UVC_CAMERA,
241
		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
242 243
		.index		= 5,
		.size		= 2,
244 245 246 247
		.flags		= UVC_CTRL_FLAG_SET_CUR
				| UVC_CTRL_FLAG_GET_RANGE
				| UVC_CTRL_FLAG_RESTORE
				| UVC_CTRL_FLAG_AUTO_UPDATE,
248 249 250
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
251
		.selector	= UVC_CT_FOCUS_RELATIVE_CONTROL,
252 253
		.index		= 6,
		.size		= 2,
254 255 256 257
		.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,
258 259
	},
	{
260
		.entity		= UVC_GUID_UVC_CAMERA,
261
		.selector	= UVC_CT_IRIS_ABSOLUTE_CONTROL,
262 263
		.index		= 7,
		.size		= 2,
264 265 266 267
		.flags		= UVC_CTRL_FLAG_SET_CUR
				| UVC_CTRL_FLAG_GET_RANGE
				| UVC_CTRL_FLAG_RESTORE
				| UVC_CTRL_FLAG_AUTO_UPDATE,
268 269 270
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
271
		.selector	= UVC_CT_IRIS_RELATIVE_CONTROL,
272
		.index		= 8,
273
		.size		= 1,
274 275
		.flags		= UVC_CTRL_FLAG_SET_CUR
				| UVC_CTRL_FLAG_AUTO_UPDATE,
276 277
	},
	{
278
		.entity		= UVC_GUID_UVC_CAMERA,
279
		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
280
		.index		= 9,
281
		.size		= 2,
282 283 284 285
		.flags		= UVC_CTRL_FLAG_SET_CUR
				| UVC_CTRL_FLAG_GET_RANGE
				| UVC_CTRL_FLAG_RESTORE
				| UVC_CTRL_FLAG_AUTO_UPDATE,
286 287
	},
	{
288
		.entity		= UVC_GUID_UVC_CAMERA,
289
		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
290 291
		.index		= 10,
		.size		= 3,
292 293 294 295
		.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,
296 297 298
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
299
		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
300 301
		.index		= 11,
		.size		= 8,
302 303 304 305
		.flags		= UVC_CTRL_FLAG_SET_CUR
				| UVC_CTRL_FLAG_GET_RANGE
				| UVC_CTRL_FLAG_RESTORE
				| UVC_CTRL_FLAG_AUTO_UPDATE,
306 307 308
	},
	{
		.entity		= UVC_GUID_UVC_CAMERA,
309
		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
310 311
		.index		= 12,
		.size		= 4,
312 313 314 315
		.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,
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
static struct uvc_control_mapping uvc_ctrl_mappings[] = {
	{
		.id		= V4L2_CID_BRIGHTNESS,
		.name		= "Brightness",
		.entity		= UVC_GUID_UVC_PROCESSING,
399
		.selector	= UVC_PU_BRIGHTNESS_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_SIGNED,
	},
	{
		.id		= V4L2_CID_CONTRAST,
		.name		= "Contrast",
		.entity		= UVC_GUID_UVC_PROCESSING,
409
		.selector	= UVC_PU_CONTRAST_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_HUE,
		.name		= "Hue",
		.entity		= UVC_GUID_UVC_PROCESSING,
419
		.selector	= UVC_PU_HUE_CONTROL,
420 421 422 423
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
424 425
		.master_id	= V4L2_CID_HUE_AUTO,
		.master_manual	= 0,
426 427 428 429 430
	},
	{
		.id		= V4L2_CID_SATURATION,
		.name		= "Saturation",
		.entity		= UVC_GUID_UVC_PROCESSING,
431
		.selector	= UVC_PU_SATURATION_CONTROL,
432 433 434 435 436 437 438 439 440
		.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,
441
		.selector	= UVC_PU_SHARPNESS_CONTROL,
442 443 444 445 446 447 448 449 450
		.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,
451
		.selector	= UVC_PU_GAMMA_CONTROL,
452 453 454 455 456 457 458 459 460
		.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,
461
		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
462 463 464 465 466 467 468 469 470
		.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,
471
		.selector	= UVC_PU_GAIN_CONTROL,
472 473 474 475 476 477 478 479 480
		.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,
481
		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
482 483 484 485 486 487 488 489 490 491 492
		.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,
493
		.selector	= UVC_PU_HUE_AUTO_CONTROL,
494 495 496 497
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
498
		.slave_ids	= { V4L2_CID_HUE, },
499 500 501 502 503
	},
	{
		.id		= V4L2_CID_EXPOSURE_AUTO,
		.name		= "Exposure, Auto",
		.entity		= UVC_GUID_UVC_CAMERA,
504
		.selector	= UVC_CT_AE_MODE_CONTROL,
505 506 507 508 509 510
		.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),
511
		.slave_ids	= { V4L2_CID_EXPOSURE_ABSOLUTE, },
512 513 514 515 516
	},
	{
		.id		= V4L2_CID_EXPOSURE_AUTO_PRIORITY,
		.name		= "Exposure, Auto Priority",
		.entity		= UVC_GUID_UVC_CAMERA,
517
		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
518 519 520 521 522 523 524 525 526
		.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,
527
		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
528 529 530 531
		.size		= 32,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
532 533
		.master_id	= V4L2_CID_EXPOSURE_AUTO,
		.master_manual	= V4L2_EXPOSURE_MANUAL,
534 535 536 537 538
	},
	{
		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
		.name		= "White Balance Temperature, Auto",
		.entity		= UVC_GUID_UVC_PROCESSING,
539
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
540 541 542 543
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
544
		.slave_ids	= { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
545 546 547 548 549
	},
	{
		.id		= V4L2_CID_WHITE_BALANCE_TEMPERATURE,
		.name		= "White Balance Temperature",
		.entity		= UVC_GUID_UVC_PROCESSING,
550
		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
551 552 553 554
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
555 556
		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
		.master_manual	= 0,
557 558 559 560 561
	},
	{
		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
		.name		= "White Balance Component, Auto",
		.entity		= UVC_GUID_UVC_PROCESSING,
562
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
563 564 565 566
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
567 568
		.slave_ids	= { V4L2_CID_BLUE_BALANCE,
				    V4L2_CID_RED_BALANCE },
569 570 571 572 573
	},
	{
		.id		= V4L2_CID_BLUE_BALANCE,
		.name		= "White Balance Blue Component",
		.entity		= UVC_GUID_UVC_PROCESSING,
574
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
575 576 577 578
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
579 580
		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
		.master_manual	= 0,
581 582 583 584 585
	},
	{
		.id		= V4L2_CID_RED_BALANCE,
		.name		= "White Balance Red Component",
		.entity		= UVC_GUID_UVC_PROCESSING,
586
		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
587 588 589 590
		.size		= 16,
		.offset		= 16,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
591 592
		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
		.master_manual	= 0,
593 594 595 596 597
	},
	{
		.id		= V4L2_CID_FOCUS_ABSOLUTE,
		.name		= "Focus (absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
598
		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
599 600 601 602
		.size		= 16,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
603 604
		.master_id	= V4L2_CID_FOCUS_AUTO,
		.master_manual	= 0,
605 606 607 608 609
	},
	{
		.id		= V4L2_CID_FOCUS_AUTO,
		.name		= "Focus, Auto",
		.entity		= UVC_GUID_UVC_CAMERA,
610
		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
611 612 613 614
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
615
		.slave_ids	= { V4L2_CID_FOCUS_ABSOLUTE, },
616
	},
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	{
		.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,
	},
637 638 639 640
	{
		.id		= V4L2_CID_ZOOM_ABSOLUTE,
		.name		= "Zoom, Absolute",
		.entity		= UVC_GUID_UVC_CAMERA,
641
		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
642 643 644 645 646 647 648 649 650
		.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,
651
		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
652 653 654 655 656 657 658
		.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,
	},
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	{
		.id		= V4L2_CID_PAN_ABSOLUTE,
		.name		= "Pan (Absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
		.size		= 32,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
	{
		.id		= V4L2_CID_TILT_ABSOLUTE,
		.name		= "Tilt (Absolute)",
		.entity		= UVC_GUID_UVC_CAMERA,
		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
		.size		= 32,
		.offset		= 32,
		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
	},
679 680 681 682
	{
		.id		= V4L2_CID_PRIVACY,
		.name		= "Privacy",
		.entity		= UVC_GUID_UVC_CAMERA,
683
		.selector	= UVC_CT_PRIVACY_CONTROL,
684 685 686 687 688
		.size		= 1,
		.offset		= 0,
		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
	},
689 690 691 692 693 694 695 696
};

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

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

700
static inline int uvc_test_bit(const __u8 *data, int bit)
701 702 703 704
{
	return (data[bit >> 3] >> (bit & 7)) & 1;
}

705 706 707 708 709
static inline void uvc_clear_bit(__u8 *data, int bit)
{
	data[bit >> 3] &= ~(1 << (bit & 7));
}

710 711 712 713 714
/* 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.
 */
715 716
static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
	__u8 query, const __u8 *data)
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
{
	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;
	}

735
	/* Sign-extend the value if needed. */
736 737 738 739 740 741 742 743 744
	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'.
 */
745 746
static void uvc_set_le_value(struct uvc_control_mapping *mapping,
	__s32 value, __u8 *data)
747 748 749 750 751
{
	int bits = mapping->size;
	int offset = mapping->offset;
	__u8 mask;

752 753 754 755 756 757 758 759
	/* 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;

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	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;

781 782
static int uvc_entity_match_guid(const struct uvc_entity *entity,
	const __u8 guid[16])
783 784
{
	switch (UVC_ENTITY_TYPE(entity)) {
785
	case UVC_ITT_CAMERA:
786 787
		return memcmp(uvc_camera_guid, guid, 16) == 0;

788
	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
789 790
		return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;

791
	case UVC_VC_PROCESSING_UNIT:
792 793
		return memcmp(uvc_processing_guid, guid, 16) == 0;

794
	case UVC_VC_EXTENSION_UNIT:
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		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];
820
		if (!ctrl->initialized)
821 822
			continue;

823
		list_for_each_entry(map, &ctrl->info.mappings, list) {
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
			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;
			}
		}
	}
}

839
static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
840 841 842 843 844 845 846 847 848 849 850 851
	__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. */
852
	list_for_each_entry(entity, &chain->entities, chain) {
853 854 855 856 857 858 859 860 861 862 863 864
		__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;
}

865 866 867 868 869
static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
	struct uvc_control *ctrl)
{
	int ret;

870
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
871
		ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
872
				     chain->dev->intfnum, ctrl->info.selector,
873
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
874
				     ctrl->info.size);
875 876 877 878
		if (ret < 0)
			return ret;
	}

879
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
880
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
881
				     chain->dev->intfnum, ctrl->info.selector,
882
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
883
				     ctrl->info.size);
884 885 886
		if (ret < 0)
			return ret;
	}
887
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
888
		ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
889
				     chain->dev->intfnum, ctrl->info.selector,
890
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
891
				     ctrl->info.size);
892 893 894
		if (ret < 0)
			return ret;
	}
895
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
896
		ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
897
				     chain->dev->intfnum, ctrl->info.selector,
898
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
899
				     ctrl->info.size);
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
		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);
		}
915 916 917 918 919 920
	}

	ctrl->cached = 1;
	return 0;
}

921 922 923 924 925 926 927 928 929
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)
930
		return -EACCES;
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

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

959 960 961
static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
	struct uvc_control *ctrl,
	struct uvc_control_mapping *mapping,
962 963
	struct v4l2_queryctrl *v4l2_ctrl)
{
964 965
	struct uvc_control_mapping *master_map = NULL;
	struct uvc_control *master_ctrl = NULL;
966 967 968
	struct uvc_menu_info *menu;
	unsigned int i;

969
	memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
970 971
	v4l2_ctrl->id = mapping->id;
	v4l2_ctrl->type = mapping->v4l2_type;
972
	strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
973 974
	v4l2_ctrl->flags = 0;

975
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
976
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
977
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
978 979
		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;

980 981 982 983 984 985 986 987 988 989 990 991 992
	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;
	}

993
	if (!ctrl->cached) {
994
		int ret = uvc_ctrl_populate_cache(chain, ctrl);
995
		if (ret < 0)
996
			return ret;
997 998
	}

999
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1000 1001
		v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1002 1003
	}

1004 1005
	switch (mapping->v4l2_type) {
	case V4L2_CTRL_TYPE_MENU:
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
		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;
			}
		}

1018
		return 0;
1019 1020 1021 1022 1023

	case V4L2_CTRL_TYPE_BOOLEAN:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 1;
		v4l2_ctrl->step = 1;
1024
		return 0;
1025

1026 1027 1028 1029
	case V4L2_CTRL_TYPE_BUTTON:
		v4l2_ctrl->minimum = 0;
		v4l2_ctrl->maximum = 0;
		v4l2_ctrl->step = 0;
1030
		return 0;
1031

1032 1033
	default:
		break;
1034 1035
	}

1036
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1037 1038
		v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1039

1040
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1041 1042 1043
		v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));

1044
	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1045 1046 1047
		v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
				  uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	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) {
1064
		ret = -ENOENT;
1065 1066 1067 1068
		goto done;
	}

	ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1069 1070 1071
done:
	mutex_unlock(&chain->ctrl_mutex);
	return ret;
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 1098 1099 1100 1101
/*
 * 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);
1102 1103
	if (ctrl == NULL) {
		ret = -ENOENT;
1104 1105 1106
		goto done;
	}

1107 1108
	if (mapping->v4l2_type != V4L2_CTRL_TYPE_MENU ||
	    query_menu->index >= mapping->menu_count) {
1109 1110 1111 1112 1113
		ret = -EINVAL;
		goto done;
	}

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

1115 1116
	if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
	    (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
		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;
		}
	}

1133 1134 1135 1136 1137 1138 1139
	strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);

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

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
/* --------------------------------------------------------------------------
 * 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 ||
1181 1182
		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
		    (changes & V4L2_EVENT_CTRL_CH_FLAGS)))
1183 1184 1185 1186
			v4l2_event_queue_fh(sev->fh, &ev);
	}
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
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);
}

1216 1217 1218 1219 1220
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;
1221
	u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1222
	unsigned int i;
1223
	unsigned int j;
1224 1225 1226

	for (i = 0; i < xctrls_count; ++i) {
		ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

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

1249
		uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
1250
				    changes);
1251 1252 1253
	}
}

1254
static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
{
	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) {
1267
		ret = -ENOENT;
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
		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);
1282 1283 1284
		/* Mark the queue as active, allowing this initial
		   event to be accepted. */
		sev->elems = elems;
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
		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,
};
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332

/* --------------------------------------------------------------------------
 * 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.
 */
1333
int uvc_ctrl_begin(struct uvc_video_chain *chain)
1334
{
1335
	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
}

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];
1350
		if (!ctrl->initialized)
1351 1352 1353 1354
			continue;

		/* Reset the loaded flag for auto-update controls that were
		 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1355 1356 1357
		 * 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.
1358
		 */
1359 1360
		if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
		    !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1361 1362 1363
			ctrl->loaded = 0;

		if (!ctrl->dirty)
1364 1365 1366
			continue;

		if (!rollback)
1367
			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1368
				dev->intfnum, ctrl->info.selector,
1369
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1370
				ctrl->info.size);
1371 1372 1373 1374 1375 1376
		else
			ret = 0;

		if (rollback || ret < 0)
			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1377
			       ctrl->info.size);
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387

		ctrl->dirty = 0;

		if (ret < 0)
			return ret;
	}

	return 0;
}

1388 1389 1390
int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
		      const struct v4l2_ext_control *xctrls,
		      unsigned int xctrls_count)
1391
{
1392
	struct uvc_video_chain *chain = handle->chain;
1393 1394 1395 1396
	struct uvc_entity *entity;
	int ret = 0;

	/* Find the control. */
1397
	list_for_each_entry(entity, &chain->entities, chain) {
1398
		ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1399 1400 1401 1402
		if (ret < 0)
			goto done;
	}

1403 1404
	if (!rollback)
		uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1405
done:
1406
	mutex_unlock(&chain->ctrl_mutex);
1407 1408 1409
	return ret;
}

1410 1411 1412 1413 1414 1415 1416 1417
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)
1418
		return -ENOENT;
1419 1420 1421 1422

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

1423
int uvc_ctrl_set(struct uvc_video_chain *chain,
1424 1425 1426 1427
	struct v4l2_ext_control *xctrl)
{
	struct uvc_control *ctrl;
	struct uvc_control_mapping *mapping;
1428 1429 1430 1431
	s32 value;
	u32 step;
	s32 min;
	s32 max;
1432 1433
	int ret;

1434
	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1435 1436 1437
	if (ctrl == NULL)
		return -ENOENT;
	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1438
		return -EACCES;
1439

1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	/* 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));
1455 1456
		if (step == 0)
			step = 1;
1457

1458 1459 1460 1461 1462 1463
		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);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
		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)
1474
			return -ERANGE;
1475
		value = mapping->menu_info[xctrl->value].value;
1476 1477 1478 1479

		/* Valid menu indices are reported by the GET_RES request for
		 * UVC controls that support it.
		 */
1480 1481
		if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
		    (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
			if (!ctrl->cached) {
				ret = uvc_ctrl_populate_cache(chain, ctrl);
				if (ret < 0)
					return ret;
			}

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

1494 1495 1496 1497 1498
		break;

	default:
		value = xctrl->value;
		break;
1499 1500
	}

1501 1502 1503 1504
	/* 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.
	 */
1505
	if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1506
		if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1507
			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1508
				0, ctrl->info.size);
1509
		} else {
1510 1511
			ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
				ctrl->entity->id, chain->dev->intfnum,
1512
				ctrl->info.selector,
1513
				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1514
				ctrl->info.size);
1515 1516 1517 1518
			if (ret < 0)
				return ret;
		}

1519
		ctrl->loaded = 1;
1520 1521
	}

1522
	/* Backup the current value in case we need to rollback later. */
1523 1524 1525
	if (!ctrl->dirty) {
		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1526
		       ctrl->info.size);
1527 1528
	}

1529 1530
	mapping->set(mapping, value,
		uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540

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

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

1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
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,
1553 1554 1555
			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 },
1556
		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1557 1558 1559
			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 },
1560
		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1561 1562 1563
			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 },
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
	};

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

1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
/*
 * 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;
	}

1620 1621 1622 1623 1624 1625
	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)
1626
		    | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1627
		       UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1628

1629 1630
	uvc_ctrl_fixup_xu_info(dev, ctrl, info);

1631 1632 1633
	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,
1634 1635 1636
		  (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);
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

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

1668
int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1669
	struct uvc_xu_control_query *xqry)
1670 1671
{
	struct uvc_entity *entity;
1672
	struct uvc_control *ctrl;
1673
	unsigned int i, found = 0;
1674 1675 1676
	__u32 reqflags;
	__u16 size;
	__u8 *data = NULL;
1677 1678 1679
	int ret;

	/* Find the extension unit. */
1680 1681
	list_for_each_entry(entity, &chain->entities, chain) {
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1682
		    entity->id == xqry->unit)
1683 1684 1685
			break;
	}

1686
	if (entity->id != xqry->unit) {
1687
		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1688 1689
			xqry->unit);
		return -ENOENT;
1690 1691
	}

1692
	/* Find the control and perform delayed initialization if needed. */
1693 1694
	for (i = 0; i < entity->ncontrols; ++i) {
		ctrl = &entity->controls[i];
1695
		if (ctrl->index == xqry->selector - 1) {
1696 1697 1698 1699 1700 1701
			found = 1;
			break;
		}
	}

	if (!found) {
1702
		uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1703 1704
			entity->extension.guidExtensionCode, xqry->selector);
		return -ENOENT;
1705 1706
	}

1707 1708 1709
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1710
	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1711 1712 1713 1714
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}
1715

1716 1717 1718 1719 1720 1721
	/* Validate the required buffer size and flags for the request */
	reqflags = 0;
	size = ctrl->info.size;

	switch (xqry->query) {
	case UVC_GET_CUR:
1722
		reqflags = UVC_CTRL_FLAG_GET_CUR;
1723 1724
		break;
	case UVC_GET_MIN:
1725
		reqflags = UVC_CTRL_FLAG_GET_MIN;
1726 1727
		break;
	case UVC_GET_MAX:
1728
		reqflags = UVC_CTRL_FLAG_GET_MAX;
1729 1730
		break;
	case UVC_GET_DEF:
1731
		reqflags = UVC_CTRL_FLAG_GET_DEF;
1732 1733
		break;
	case UVC_GET_RES:
1734
		reqflags = UVC_CTRL_FLAG_GET_RES;
1735 1736
		break;
	case UVC_SET_CUR:
1737
		reqflags = UVC_CTRL_FLAG_SET_CUR;
1738 1739 1740 1741 1742 1743 1744 1745
		break;
	case UVC_GET_LEN:
		size = 2;
		break;
	case UVC_GET_INFO:
		size = 1;
		break;
	default:
1746 1747 1748
		ret = -EINVAL;
		goto done;
	}
1749

1750 1751
	if (size != xqry->size) {
		ret = -ENOBUFS;
1752 1753
		goto done;
	}
1754

1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
	if (reqflags && !(ctrl->info.flags & reqflags)) {
		ret = -EBADRQC;
		goto done;
	}

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

1766 1767
	if (xqry->query == UVC_SET_CUR &&
	    copy_from_user(data, xqry->data, size)) {
1768
		ret = -EFAULT;
1769
		goto done;
1770 1771
	}

1772 1773
	ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
			     chain->dev->intfnum, xqry->selector, data, size);
1774
	if (ret < 0)
1775
		goto done;
1776

1777 1778
	if (xqry->query != UVC_SET_CUR &&
	    copy_to_user(xqry->data, data, size))
1779
		ret = -EFAULT;
1780
done:
1781
	kfree(data);
1782
	mutex_unlock(&chain->ctrl_mutex);
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
	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];

1812
			if (!ctrl->initialized || !ctrl->modified ||
1813
			    (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1814 1815
				continue;

1816
			printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1817 1818
				ctrl->info.entity, ctrl->info.index,
				ctrl->info.selector);
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
			ctrl->dirty = 1;
		}

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

	return 0;
}

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

1834 1835 1836 1837 1838 1839 1840
/*
 * 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;
1841

1842 1843
	memcpy(&ctrl->info, info, sizeof(*info));
	INIT_LIST_HEAD(&ctrl->info.mappings);
1844 1845

	/* Allocate an array to save control values (cur, def, max, etc.) */
1846
	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1847 1848 1849 1850
				 GFP_KERNEL);
	if (ctrl->uvc_data == NULL) {
		ret = -ENOMEM;
		goto done;
1851
	}
1852

1853 1854
	ctrl->initialized = 1;

1855
	uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1856
		"entity %u\n", ctrl->info.entity, ctrl->info.selector,
1857
		dev->udev->devpath, ctrl->entity->id);
1858 1859

done:
1860
	if (ret < 0)
1861
		kfree(ctrl->uvc_data);
1862
	return ret;
1863 1864 1865
}

/*
1866
 * Add a control mapping to a given control.
1867
 */
1868 1869
static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1870
{
1871 1872
	struct uvc_control_mapping *map;
	unsigned int size;
1873

1874 1875 1876
	/* 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.
1877
	 */
1878 1879 1880
	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
	if (map == NULL)
		return -ENOMEM;
1881

1882 1883
	INIT_LIST_HEAD(&map->ev_subs);

1884 1885 1886 1887 1888
	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;
1889 1890
	}

1891 1892 1893 1894
	if (map->get == NULL)
		map->get = uvc_get_le_value;
	if (map->set == NULL)
		map->set = uvc_set_le_value;
1895

1896
	list_add_tail(&map->list, &ctrl->info.mappings);
1897 1898
	uvc_trace(UVC_TRACE_CONTROL,
		"Adding mapping '%s' to control %pUl/%u.\n",
1899
		map->name, ctrl->info.entity, ctrl->info.selector);
1900 1901

	return 0;
1902 1903
}

1904 1905
int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
	const struct uvc_control_mapping *mapping)
1906
{
1907
	struct uvc_device *dev = chain->dev;
1908
	struct uvc_control_mapping *map;
1909 1910 1911
	struct uvc_entity *entity;
	struct uvc_control *ctrl;
	int found = 0;
1912
	int ret;
1913

1914
	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1915
		uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1916
			"id 0x%08x is invalid.\n", mapping->name,
1917 1918 1919 1920
			mapping->id);
		return -EINVAL;
	}

1921 1922
	/* Search for the matching (GUID/CS) control on the current chain */
	list_for_each_entry(entity, &chain->entities, chain) {
1923
		unsigned int i;
1924

1925 1926
		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
		    !uvc_entity_match_guid(entity, mapping->entity))
1927
			continue;
1928

1929 1930
		for (i = 0; i < entity->ncontrols; ++i) {
			ctrl = &entity->controls[i];
1931
			if (ctrl->index == mapping->selector - 1) {
1932 1933
				found = 1;
				break;
1934 1935 1936
			}
		}

1937 1938 1939 1940 1941
		if (found)
			break;
	}
	if (!found)
		return -ENOENT;
1942

1943 1944 1945
	if (mutex_lock_interruptible(&chain->ctrl_mutex))
		return -ERESTARTSYS;

1946 1947 1948 1949 1950 1951 1952
	/* Perform delayed initialization of XU controls */
	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
	if (ret < 0) {
		ret = -ENOENT;
		goto done;
	}

1953
	list_for_each_entry(map, &ctrl->info.mappings, list) {
1954 1955 1956 1957 1958 1959 1960
		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;
		}
1961
	}
1962

1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
	/* 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;
	}

1973
	ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1974 1975 1976
	if (ret < 0)
		atomic_dec(&dev->nmappings);

1977 1978
done:
	mutex_unlock(&chain->ctrl_mutex);
1979 1980 1981
	return ret;
}

1982
/*
1983 1984 1985
 * 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.
1986
 */
1987 1988
static void uvc_ctrl_prune_entity(struct uvc_device *dev,
	struct uvc_entity *entity)
1989
{
1990
	struct uvc_ctrl_blacklist {
1991 1992
		struct usb_device_id id;
		u8 index;
1993 1994 1995
	};

	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1996
		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1997 1998
		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1999
	};
2000 2001 2002
	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
	};
2003

2004
	const struct uvc_ctrl_blacklist *blacklist;
2005
	unsigned int size;
2006
	unsigned int count;
2007
	unsigned int i;
2008
	u8 *controls;
2009

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
	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;
2024

2025 2026 2027
	default:
		return;
	}
2028

2029
	for (i = 0; i < count; ++i) {
2030
		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2031 2032
			continue;

2033 2034
		if (blacklist[i].index >= 8 * size ||
		    !uvc_test_bit(controls, blacklist[i].index))
2035 2036
			continue;

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

2040
		uvc_clear_bit(controls, blacklist[i].index);
2041 2042 2043
	}
}

2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
/*
 * 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);

2056 2057 2058 2059 2060 2061
	/* 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)
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
		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;
		 }
	}

2072
	if (!ctrl->initialized)
2073 2074 2075 2076
		return;

	for (; mapping < mend; ++mapping) {
		if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2077
		    ctrl->info.selector == mapping->selector)
2078 2079 2080 2081
			__uvc_ctrl_add_mapping(dev, ctrl, mapping);
	}
}

2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
/*
 * 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) {
2092
		struct uvc_control *ctrl;
A
Akinobu Mita 已提交
2093
		unsigned int bControlSize = 0, ncontrols;
2094 2095
		__u8 *bmControls = NULL;

2096
		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2097 2098
			bmControls = entity->extension.bmControls;
			bControlSize = entity->extension.bControlSize;
2099
		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2100 2101
			bmControls = entity->processing.bmControls;
			bControlSize = entity->processing.bControlSize;
2102
		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2103 2104 2105 2106
			bmControls = entity->camera.bmControls;
			bControlSize = entity->camera.bControlSize;
		}

2107
		/* Remove bogus/blacklisted controls */
2108
		uvc_ctrl_prune_entity(dev, entity);
2109

2110
		/* Count supported controls and allocate the controls array */
A
Akinobu Mita 已提交
2111
		ncontrols = memweight(bmControls, bControlSize);
2112 2113 2114
		if (ncontrols == 0)
			continue;

2115
		entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2116
					   GFP_KERNEL);
2117 2118 2119 2120
		if (entity->controls == NULL)
			return -ENOMEM;
		entity->ncontrols = ncontrols;

2121
		/* Initialize all supported controls */
2122 2123
		ctrl = entity->controls;
		for (i = 0; i < bControlSize * 8; ++i) {
2124
			if (uvc_test_bit(bmControls, i) == 0)
2125 2126 2127 2128
				continue;

			ctrl->entity = entity;
			ctrl->index = i;
2129 2130

			uvc_ctrl_init_ctrl(dev, ctrl);
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
			ctrl++;
		}
	}

	return 0;
}

/*
 * Cleanup device controls.
 */
2141 2142
static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
	struct uvc_control *ctrl)
2143
{
2144
	struct uvc_control_mapping *mapping, *nm;
2145

2146
	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2147 2148 2149
		list_del(&mapping->list);
		kfree(mapping->menu_info);
		kfree(mapping);
2150 2151 2152
	}
}

2153
void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2154
{
2155 2156
	struct uvc_entity *entity;
	unsigned int i;
2157

2158 2159 2160 2161 2162
	/* 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];

2163
			if (!ctrl->initialized)
2164
				continue;
2165

2166 2167
			uvc_ctrl_cleanup_mappings(dev, ctrl);
			kfree(ctrl->uvc_data);
2168 2169
		}

2170
		kfree(entity->controls);
2171 2172
	}
}