File Index
Files and directories
Sign in
FILE

uvc_ctrl.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/usb/uvc/uvc_ctrl.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *      uvc_ctrl.c  --  USB Video Class driver - Controls
   4 *
   5 *      Copyright (C) 2005-2010
   6 *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
   7 */
   8
   9#include <asm/barrier.h>
  10#include <linux/bitops.h>
  11#include <linux/kernel.h>
  12#include <linux/list.h>
  13#include <linux/module.h>
  14#include <linux/slab.h>
  15#include <linux/uaccess.h>
  16#include <linux/usb.h>
  17#include <linux/usb/uvc.h>
  18#include <linux/videodev2.h>
  19#include <linux/vmalloc.h>
  20#include <linux/wait.h>
  21#include <linux/workqueue.h>
  22#include <linux/atomic.h>
  23#include <media/v4l2-ctrls.h>
  24
  25#include "uvcvideo.h"
  26
  27#define UVC_CTRL_DATA_CURRENT	0
  28#define UVC_CTRL_DATA_BACKUP	1
  29#define UVC_CTRL_DATA_MIN	2
  30#define UVC_CTRL_DATA_MAX	3
  31#define UVC_CTRL_DATA_RES	4
  32#define UVC_CTRL_DATA_DEF	5
  33#define UVC_CTRL_DATA_LAST	6
  34
  35/* ------------------------------------------------------------------------
  36 * Controls
  37 */
  38
  39static const struct uvc_control_info uvc_ctrls[] = {
  40	{
  41		.entity		= UVC_GUID_UVC_PROCESSING,
  42		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
  43		.index		= 0,
  44		.size		= 2,
  45		.flags		= UVC_CTRL_FLAG_SET_CUR
  46				| UVC_CTRL_FLAG_GET_RANGE
  47				| UVC_CTRL_FLAG_RESTORE,
  48	},
  49	{
  50		.entity		= UVC_GUID_UVC_PROCESSING,
  51		.selector	= UVC_PU_CONTRAST_CONTROL,
  52		.index		= 1,
  53		.size		= 2,
  54		.flags		= UVC_CTRL_FLAG_SET_CUR
  55				| UVC_CTRL_FLAG_GET_RANGE
  56				| UVC_CTRL_FLAG_RESTORE,
  57	},
  58	{
  59		.entity		= UVC_GUID_UVC_PROCESSING,
  60		.selector	= UVC_PU_HUE_CONTROL,
  61		.index		= 2,
  62		.size		= 2,
  63		.flags		= UVC_CTRL_FLAG_SET_CUR
  64				| UVC_CTRL_FLAG_GET_RANGE
  65				| UVC_CTRL_FLAG_RESTORE
  66				| UVC_CTRL_FLAG_AUTO_UPDATE,
  67	},
  68	{
  69		.entity		= UVC_GUID_UVC_PROCESSING,
  70		.selector	= UVC_PU_SATURATION_CONTROL,
  71		.index		= 3,
  72		.size		= 2,
  73		.flags		= UVC_CTRL_FLAG_SET_CUR
  74				| UVC_CTRL_FLAG_GET_RANGE
  75				| UVC_CTRL_FLAG_RESTORE,
  76	},
  77	{
  78		.entity		= UVC_GUID_UVC_PROCESSING,
  79		.selector	= UVC_PU_SHARPNESS_CONTROL,
  80		.index		= 4,
  81		.size		= 2,
  82		.flags		= UVC_CTRL_FLAG_SET_CUR
  83				| UVC_CTRL_FLAG_GET_RANGE
  84				| UVC_CTRL_FLAG_RESTORE,
  85	},
  86	{
  87		.entity		= UVC_GUID_UVC_PROCESSING,
  88		.selector	= UVC_PU_GAMMA_CONTROL,
  89		.index		= 5,
  90		.size		= 2,
  91		.flags		= UVC_CTRL_FLAG_SET_CUR
  92				| UVC_CTRL_FLAG_GET_RANGE
  93				| UVC_CTRL_FLAG_RESTORE,
  94	},
  95	{
  96		.entity		= UVC_GUID_UVC_PROCESSING,
  97		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  98		.index		= 6,
  99		.size		= 2,
 100		.flags		= UVC_CTRL_FLAG_SET_CUR
 101				| UVC_CTRL_FLAG_GET_RANGE
 102				| UVC_CTRL_FLAG_RESTORE
 103				| UVC_CTRL_FLAG_AUTO_UPDATE,
 104	},
 105	{
 106		.entity		= UVC_GUID_UVC_PROCESSING,
 107		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
 108		.index		= 7,
 109		.size		= 4,
 110		.flags		= UVC_CTRL_FLAG_SET_CUR
 111				| UVC_CTRL_FLAG_GET_RANGE
 112				| UVC_CTRL_FLAG_RESTORE
 113				| UVC_CTRL_FLAG_AUTO_UPDATE,
 114	},
 115	{
 116		.entity		= UVC_GUID_UVC_PROCESSING,
 117		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
 118		.index		= 8,
 119		.size		= 2,
 120		.flags		= UVC_CTRL_FLAG_SET_CUR
 121				| UVC_CTRL_FLAG_GET_RANGE
 122				| UVC_CTRL_FLAG_RESTORE,
 123	},
 124	{
 125		.entity		= UVC_GUID_UVC_PROCESSING,
 126		.selector	= UVC_PU_GAIN_CONTROL,
 127		.index		= 9,
 128		.size		= 2,
 129		.flags		= UVC_CTRL_FLAG_SET_CUR
 130				| UVC_CTRL_FLAG_GET_RANGE
 131				| UVC_CTRL_FLAG_RESTORE,
 132	},
 133	{
 134		.entity		= UVC_GUID_UVC_PROCESSING,
 135		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
 136		.index		= 10,
 137		.size		= 1,
 138		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 139				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
 140	},
 141	{
 142		.entity		= UVC_GUID_UVC_PROCESSING,
 143		.selector	= UVC_PU_HUE_AUTO_CONTROL,
 144		.index		= 11,
 145		.size		= 1,
 146		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 147				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
 148	},
 149	{
 150		.entity		= UVC_GUID_UVC_PROCESSING,
 151		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
 152		.index		= 12,
 153		.size		= 1,
 154		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 155				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
 156	},
 157	{
 158		.entity		= UVC_GUID_UVC_PROCESSING,
 159		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
 160		.index		= 13,
 161		.size		= 1,
 162		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 163				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
 164	},
 165	{
 166		.entity		= UVC_GUID_UVC_PROCESSING,
 167		.selector	= UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
 168		.index		= 14,
 169		.size		= 2,
 170		.flags		= UVC_CTRL_FLAG_SET_CUR
 171				| UVC_CTRL_FLAG_GET_RANGE
 172				| UVC_CTRL_FLAG_RESTORE,
 173	},
 174	{
 175		.entity		= UVC_GUID_UVC_PROCESSING,
 176		.selector	= UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
 177		.index		= 15,
 178		.size		= 2,
 179		.flags		= UVC_CTRL_FLAG_SET_CUR
 180				| UVC_CTRL_FLAG_GET_RANGE
 181				| UVC_CTRL_FLAG_RESTORE,
 182	},
 183	{
 184		.entity		= UVC_GUID_UVC_PROCESSING,
 185		.selector	= UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
 186		.index		= 16,
 187		.size		= 1,
 188		.flags		= UVC_CTRL_FLAG_GET_CUR,
 189	},
 190	{
 191		.entity		= UVC_GUID_UVC_PROCESSING,
 192		.selector	= UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
 193		.index		= 17,
 194		.size		= 1,
 195		.flags		= UVC_CTRL_FLAG_GET_CUR,
 196	},
 197	{
 198		.entity		= UVC_GUID_UVC_CAMERA,
 199		.selector	= UVC_CT_SCANNING_MODE_CONTROL,
 200		.index		= 0,
 201		.size		= 1,
 202		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 203				| UVC_CTRL_FLAG_RESTORE,
 204	},
 205	{
 206		.entity		= UVC_GUID_UVC_CAMERA,
 207		.selector	= UVC_CT_AE_MODE_CONTROL,
 208		.index		= 1,
 209		.size		= 1,
 210		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 211				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
 212				| UVC_CTRL_FLAG_RESTORE,
 213	},
 214	{
 215		.entity		= UVC_GUID_UVC_CAMERA,
 216		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
 217		.index		= 2,
 218		.size		= 1,
 219		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 220				| UVC_CTRL_FLAG_RESTORE,
 221	},
 222	{
 223		.entity		= UVC_GUID_UVC_CAMERA,
 224		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
 225		.index		= 3,
 226		.size		= 4,
 227		.flags		= UVC_CTRL_FLAG_SET_CUR
 228				| UVC_CTRL_FLAG_GET_RANGE
 229				| UVC_CTRL_FLAG_RESTORE
 230				| UVC_CTRL_FLAG_AUTO_UPDATE,
 231	},
 232	{
 233		.entity		= UVC_GUID_UVC_CAMERA,
 234		.selector	= UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
 235		.index		= 4,
 236		.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		.index		= 5,
 243		.size		= 2,
 244		.flags		= UVC_CTRL_FLAG_SET_CUR
 245				| UVC_CTRL_FLAG_GET_RANGE
 246				| UVC_CTRL_FLAG_RESTORE
 247				| UVC_CTRL_FLAG_AUTO_UPDATE,
 248	},
 249	{
 250		.entity		= UVC_GUID_UVC_CAMERA,
 251		.selector	= UVC_CT_FOCUS_RELATIVE_CONTROL,
 252		.index		= 6,
 253		.size		= 2,
 254		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
 255				| UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
 256				| UVC_CTRL_FLAG_GET_DEF
 257				| UVC_CTRL_FLAG_AUTO_UPDATE,
 258	},
 259	{
 260		.entity		= UVC_GUID_UVC_CAMERA,
 261		.selector	= UVC_CT_IRIS_ABSOLUTE_CONTROL,
 262		.index		= 7,
 263		.size		= 2,
 264		.flags		= UVC_CTRL_FLAG_SET_CUR
 265				| UVC_CTRL_FLAG_GET_RANGE
 266				| UVC_CTRL_FLAG_RESTORE
 267				| 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		.flags		= UVC_CTRL_FLAG_SET_CUR
 275				| 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		.flags		= UVC_CTRL_FLAG_SET_CUR
 283				| UVC_CTRL_FLAG_GET_RANGE
 284				| UVC_CTRL_FLAG_RESTORE
 285				| UVC_CTRL_FLAG_AUTO_UPDATE,
 286	},
 287	{
 288		.entity		= UVC_GUID_UVC_CAMERA,
 289		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
 290		.index		= 10,
 291		.size		= 3,
 292		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
 293				| UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
 294				| UVC_CTRL_FLAG_GET_DEF
 295				| UVC_CTRL_FLAG_AUTO_UPDATE,
 296	},
 297	{
 298		.entity		= UVC_GUID_UVC_CAMERA,
 299		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
 300		.index		= 11,
 301		.size		= 8,
 302		.flags		= UVC_CTRL_FLAG_SET_CUR
 303				| UVC_CTRL_FLAG_GET_RANGE
 304				| UVC_CTRL_FLAG_RESTORE
 305				| UVC_CTRL_FLAG_AUTO_UPDATE,
 306	},
 307	{
 308		.entity		= UVC_GUID_UVC_CAMERA,
 309		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
 310		.index		= 12,
 311		.size		= 4,
 312		.flags		= UVC_CTRL_FLAG_SET_CUR
 313				| UVC_CTRL_FLAG_GET_RANGE
 314				| UVC_CTRL_FLAG_AUTO_UPDATE,
 315	},
 316	{
 317		.entity		= UVC_GUID_UVC_CAMERA,
 318		.selector	= UVC_CT_ROLL_ABSOLUTE_CONTROL,
 319		.index		= 13,
 320		.size		= 2,
 321		.flags		= UVC_CTRL_FLAG_SET_CUR
 322				| UVC_CTRL_FLAG_GET_RANGE
 323				| UVC_CTRL_FLAG_RESTORE
 324				| UVC_CTRL_FLAG_AUTO_UPDATE,
 325	},
 326	{
 327		.entity		= UVC_GUID_UVC_CAMERA,
 328		.selector	= UVC_CT_ROLL_RELATIVE_CONTROL,
 329		.index		= 14,
 330		.size		= 2,
 331		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
 332				| UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
 333				| UVC_CTRL_FLAG_GET_DEF
 334				| UVC_CTRL_FLAG_AUTO_UPDATE,
 335	},
 336	{
 337		.entity		= UVC_GUID_UVC_CAMERA,
 338		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
 339		.index		= 17,
 340		.size		= 1,
 341		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 342				| UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
 343	},
 344	{
 345		.entity		= UVC_GUID_UVC_CAMERA,
 346		.selector	= UVC_CT_PRIVACY_CONTROL,
 347		.index		= 18,
 348		.size		= 1,
 349		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 350				| UVC_CTRL_FLAG_RESTORE
 351				| UVC_CTRL_FLAG_AUTO_UPDATE,
 352	},
 353	{
 354		.entity		= UVC_GUID_EXT_GPIO_CONTROLLER,
 355		.selector	= UVC_CT_PRIVACY_CONTROL,
 356		.index		= 0,
 357		.size		= 1,
 358		.flags		= UVC_CTRL_FLAG_GET_CUR
 359				| UVC_CTRL_FLAG_AUTO_UPDATE,
 360	},
 361	/*
 362	 * UVC_CTRL_FLAG_AUTO_UPDATE is needed because the RoI may get updated
 363	 * by sensors.
 364	 * "This RoI should be the same as specified in most recent SET_CUR
 365	 * except in the case where the ‘Auto Detect and Track’ and/or
 366	 * ‘Image Stabilization’ bit have been set."
 367	 * 4.2.2.1.20 Digital Region of Interest (ROI) Control
 368	 */
 369	{
 370		.entity		= UVC_GUID_UVC_CAMERA,
 371		.selector	= UVC_CT_REGION_OF_INTEREST_CONTROL,
 372		.index		= 21,
 373		.size		= 10,
 374		.flags		= UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
 375				| UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
 376				| UVC_CTRL_FLAG_GET_DEF
 377				| UVC_CTRL_FLAG_AUTO_UPDATE,
 378	},
 379	{
 380		.entity		= UVC_GUID_CHROMEOS_XU,
 381		.selector	= UVC_CROSXU_CONTROL_IQ_PROFILE,
 382		.index		= 3,
 383		.size		= 1,
 384		.flags		= UVC_CTRL_FLAG_SET_CUR
 385				| UVC_CTRL_FLAG_GET_RANGE
 386				| UVC_CTRL_FLAG_RESTORE,
 387	},
 388};
 389
 390static const u32 uvc_control_classes[] = {
 391	V4L2_CID_CAMERA_CLASS,
 392	V4L2_CID_USER_CLASS,
 393};
 394
 395static const int exposure_auto_mapping[] = { 2, 1, 4, 8 };
 396static const int cros_colorfx_mapping[] = {
 397	1,	/* V4L2_COLORFX_NONE */
 398	-1,	/* V4L2_COLORFX_BW */
 399	-1,	/* V4L2_COLORFX_SEPIA */
 400	-1,	/* V4L2_COLORFX_NEGATIVE */
 401	-1,	/* V4L2_COLORFX_EMBOSS */
 402	-1,	/* V4L2_COLORFX_SKETCH */
 403	-1,	/* V4L2_COLORFX_SKY_BLUE */
 404	-1,	/* V4L2_COLORFX_GRASS_GREEN */
 405	-1,	/* V4L2_COLORFX_SKIN_WHITEN */
 406	0,	/* V4L2_COLORFX_VIVID */
 407};
 408
 409
 410static bool uvc_ctrl_mapping_is_compound(struct uvc_control_mapping *mapping)
 411{
 412	return mapping->v4l2_type >= V4L2_CTRL_COMPOUND_TYPES;
 413}
 414
 415static s32 uvc_mapping_get_s32(struct uvc_control_mapping *mapping,
 416			       u8 query, const void *data_in)
 417{
 418	s32 data_out = 0;
 419
 420	mapping->get(mapping, query, data_in, sizeof(data_out), &data_out);
 421
 422	return data_out;
 423}
 424
 425static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
 426				s32 data_in, void *data_out)
 427{
 428	mapping->set(mapping, sizeof(data_in), &data_in, data_out);
 429}
 430
 431/*
 432 * This function translates the V4L2 menu index @idx, as exposed to userspace as
 433 * the V4L2 control value, to the corresponding UVC control value used by the
 434 * device. The custom menu_mapping in the control @mapping is used when
 435 * available, otherwise the function assumes that the V4L2 and UVC values are
 436 * identical.
 437 *
 438 * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is
 439 * expressed as a bitmask and is thus guaranteed to have a single bit set.
 440 *
 441 * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the
 442 * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM
 443 * or UVC_CTRL_DATA_TYPE_BITMASK.
 444 */
 445static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
 446				      u32 idx)
 447{
 448	if (!test_bit(idx, &mapping->menu_mask))
 449		return -EINVAL;
 450
 451	if (mapping->menu_mapping)
 452		return mapping->menu_mapping[idx];
 453
 454	return idx;
 455}
 456
 457static const char *
 458uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx)
 459{
 460	if (!test_bit(idx, &mapping->menu_mask))
 461		return NULL;
 462
 463	if (mapping->menu_names)
 464		return mapping->menu_names[idx];
 465
 466	return v4l2_ctrl_get_menu(mapping->id)[idx];
 467}
 468
 469static int uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, u8 query,
 470			     const void *uvc_in, size_t v4l2_size,
 471			     void *v4l2_out)
 472{
 473	u8 value = ((u8 *)uvc_in)[2];
 474	s8 sign = ((s8 *)uvc_in)[0];
 475	s32 *out = v4l2_out;
 476
 477	if (WARN_ON(v4l2_size != sizeof(s32)))
 478		return -EINVAL;
 479
 480	switch (query) {
 481	case UVC_GET_CUR:
 482		*out = (sign == 0) ? 0 : (sign > 0 ? value : -value);
 483		return 0;
 484
 485	case UVC_GET_MIN:
 486	case UVC_GET_MAX:
 487	case UVC_GET_RES:
 488	case UVC_GET_DEF:
 489	default:
 490		*out = value;
 491		return 0;
 492	}
 493}
 494
 495static int uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
 496			     size_t v4l2_size, const void *v4l2_in,
 497			     void *uvc_out)
 498{
 499	u8 *out = uvc_out;
 500	s32 value;
 501
 502	if (WARN_ON(v4l2_size != sizeof(s32)))
 503		return -EINVAL;
 504
 505	value = *(u32 *)v4l2_in;
 506	out[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
 507	out[2] = min_t(int, abs(value), 0xff);
 508
 509	return 0;
 510}
 511
 512static int uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
 513				  u8 query, const void *uvc_in,
 514				  size_t v4l2_size, void *v4l2_out)
 515{
 516	unsigned int first = mapping->offset / 8;
 517	u8 value = ((u8 *)uvc_in)[first + 1];
 518	s8 sign = ((s8 *)uvc_in)[first];
 519	s32 *out = v4l2_out;
 520
 521	if (WARN_ON(v4l2_size != sizeof(s32)))
 522		return -EINVAL;
 523
 524	switch (query) {
 525	case UVC_GET_CUR:
 526		*out = (sign == 0) ? 0 : (sign > 0 ? value : -value);
 527		return 0;
 528	case UVC_GET_MIN:
 529		*out = -value;
 530		return 0;
 531	case UVC_GET_MAX:
 532	case UVC_GET_RES:
 533	case UVC_GET_DEF:
 534	default:
 535		*out = value;
 536		return 0;
 537	}
 538}
 539
 540static int uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
 541				  size_t v4l2_size, const void *v4l2_in,
 542				  void *uvc_out)
 543{
 544	unsigned int first = mapping->offset / 8;
 545	u8 *out = uvc_out;
 546	s32 value;
 547
 548	if (WARN_ON(v4l2_size != sizeof(s32)))
 549		return -EINVAL;
 550
 551	value = *(u32 *)v4l2_in;
 552	out[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
 553	out[first + 1] = min_t(int, abs(value), 0xff);
 554
 555	return 0;
 556}
 557
 558static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = {
 559	.id		= V4L2_CID_POWER_LINE_FREQUENCY,
 560	.entity		= UVC_GUID_UVC_PROCESSING,
 561	.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
 562	.size		= 2,
 563	.offset		= 0,
 564	.v4l2_type	= V4L2_CTRL_TYPE_MENU,
 565	.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
 566	.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
 567				  V4L2_CID_POWER_LINE_FREQUENCY_50HZ),
 568};
 569
 570static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
 571	.id		= V4L2_CID_POWER_LINE_FREQUENCY,
 572	.entity		= UVC_GUID_UVC_PROCESSING,
 573	.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
 574	.size		= 2,
 575	.offset		= 0,
 576	.v4l2_type	= V4L2_CTRL_TYPE_MENU,
 577	.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
 578	.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
 579				  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
 580};
 581
 582static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
 583	.id		= V4L2_CID_POWER_LINE_FREQUENCY,
 584	.entity		= UVC_GUID_UVC_PROCESSING,
 585	.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
 586	.size		= 2,
 587	.offset		= 0,
 588	.v4l2_type	= V4L2_CTRL_TYPE_MENU,
 589	.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
 590	.menu_mask	= GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
 591				  V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
 592};
 593
 594static const struct uvc_control_mapping *uvc_ctrl_filter_plf_mapping(
 595	struct uvc_video_chain *chain, struct uvc_control *ctrl)
 596{
 597	const struct uvc_control_mapping *out_mapping =
 598					&uvc_ctrl_power_line_mapping_uvc11;
 599	u8 *buf __free(kfree) = NULL;
 600	u8 init_val;
 601	int ret;
 602
 603	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
 604	if (!buf)
 605		return NULL;
 606
 607	/* Save the current PLF value, so we can restore it. */
 608	ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
 609			     chain->dev->intfnum, ctrl->info.selector,
 610			     buf, sizeof(*buf));
 611	/* If we cannot read the control skip it. */
 612	if (ret)
 613		return NULL;
 614	init_val = *buf;
 615
 616	/* If PLF value cannot be set to off, it is limited. */
 617	*buf = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED;
 618	ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
 619			     chain->dev->intfnum, ctrl->info.selector,
 620			     buf, sizeof(*buf));
 621	if (ret)
 622		return &uvc_ctrl_power_line_mapping_limited;
 623
 624	/* UVC 1.1 does not define auto, we can exit. */
 625	if (chain->dev->uvc_version < 0x150)
 626		goto end;
 627
 628	/* Check if the device supports auto. */
 629	*buf = V4L2_CID_POWER_LINE_FREQUENCY_AUTO;
 630	ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
 631			     chain->dev->intfnum, ctrl->info.selector,
 632			     buf, sizeof(*buf));
 633	if (!ret)
 634		out_mapping = &uvc_ctrl_power_line_mapping_uvc15;
 635
 636end:
 637	/* Restore initial value and add mapping. */
 638	*buf = init_val;
 639	uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
 640		       chain->dev->intfnum, ctrl->info.selector,
 641		       buf, sizeof(*buf));
 642
 643	return out_mapping;
 644}
 645
 646static int uvc_get_rect(struct uvc_control_mapping *mapping, u8 query,
 647			const void *uvc_in, size_t v4l2_size, void *v4l2_out)
 648{
 649	const struct uvc_rect *uvc_rect = uvc_in;
 650	struct v4l2_rect *v4l2_rect = v4l2_out;
 651
 652	if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect)))
 653		return -EINVAL;
 654
 655	if (uvc_rect->left > uvc_rect->right ||
 656	    uvc_rect->top > uvc_rect->bottom)
 657		return -EIO;
 658
 659	v4l2_rect->top = uvc_rect->top;
 660	v4l2_rect->left = uvc_rect->left;
 661	v4l2_rect->height = uvc_rect->bottom - uvc_rect->top + 1;
 662	v4l2_rect->width = uvc_rect->right - uvc_rect->left + 1;
 663
 664	return 0;
 665}
 666
 667static int uvc_set_rect(struct uvc_control_mapping *mapping, size_t v4l2_size,
 668			const void *v4l2_in, void *uvc_out)
 669{
 670	struct uvc_rect *uvc_rect = uvc_out;
 671	const struct v4l2_rect *v4l2_rect = v4l2_in;
 672
 673	if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect)))
 674		return -EINVAL;
 675
 676	uvc_rect->top = min(0xffff, v4l2_rect->top);
 677	uvc_rect->left = min(0xffff, v4l2_rect->left);
 678	uvc_rect->bottom = min(0xffff, v4l2_rect->top + v4l2_rect->height - 1);
 679	uvc_rect->right = min(0xffff, v4l2_rect->left + v4l2_rect->width - 1);
 680
 681	return 0;
 682}
 683
 684static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
 685	{
 686		.id		= V4L2_CID_BRIGHTNESS,
 687		.entity		= UVC_GUID_UVC_PROCESSING,
 688		.selector	= UVC_PU_BRIGHTNESS_CONTROL,
 689		.size		= 16,
 690		.offset		= 0,
 691		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 692		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 693	},
 694	{
 695		.id		= V4L2_CID_CONTRAST,
 696		.entity		= UVC_GUID_UVC_PROCESSING,
 697		.selector	= UVC_PU_CONTRAST_CONTROL,
 698		.size		= 16,
 699		.offset		= 0,
 700		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 701		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 702	},
 703	{
 704		.id		= V4L2_CID_HUE,
 705		.entity		= UVC_GUID_UVC_PROCESSING,
 706		.selector	= UVC_PU_HUE_CONTROL,
 707		.size		= 16,
 708		.offset		= 0,
 709		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 710		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 711		.master_id	= V4L2_CID_HUE_AUTO,
 712		.master_manual	= 0,
 713	},
 714	{
 715		.id		= V4L2_CID_SATURATION,
 716		.entity		= UVC_GUID_UVC_PROCESSING,
 717		.selector	= UVC_PU_SATURATION_CONTROL,
 718		.size		= 16,
 719		.offset		= 0,
 720		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 721		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 722	},
 723	{
 724		.id		= V4L2_CID_SHARPNESS,
 725		.entity		= UVC_GUID_UVC_PROCESSING,
 726		.selector	= UVC_PU_SHARPNESS_CONTROL,
 727		.size		= 16,
 728		.offset		= 0,
 729		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 730		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 731	},
 732	{
 733		.id		= V4L2_CID_GAMMA,
 734		.entity		= UVC_GUID_UVC_PROCESSING,
 735		.selector	= UVC_PU_GAMMA_CONTROL,
 736		.size		= 16,
 737		.offset		= 0,
 738		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 739		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 740	},
 741	{
 742		.id		= V4L2_CID_BACKLIGHT_COMPENSATION,
 743		.entity		= UVC_GUID_UVC_PROCESSING,
 744		.selector	= UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
 745		.size		= 16,
 746		.offset		= 0,
 747		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 748		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 749	},
 750	{
 751		.id		= V4L2_CID_GAIN,
 752		.entity		= UVC_GUID_UVC_PROCESSING,
 753		.selector	= UVC_PU_GAIN_CONTROL,
 754		.size		= 16,
 755		.offset		= 0,
 756		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 757		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 758	},
 759	{
 760		.id		= V4L2_CID_HUE_AUTO,
 761		.entity		= UVC_GUID_UVC_PROCESSING,
 762		.selector	= UVC_PU_HUE_AUTO_CONTROL,
 763		.size		= 1,
 764		.offset		= 0,
 765		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
 766		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
 767		.slave_ids	= { V4L2_CID_HUE, },
 768	},
 769	{
 770		.id		= V4L2_CID_EXPOSURE_AUTO,
 771		.entity		= UVC_GUID_UVC_CAMERA,
 772		.selector	= UVC_CT_AE_MODE_CONTROL,
 773		.size		= 4,
 774		.offset		= 0,
 775		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
 776		.data_type	= UVC_CTRL_DATA_TYPE_BITMASK,
 777		.menu_mapping	= exposure_auto_mapping,
 778		.menu_mask	= GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY,
 779					  V4L2_EXPOSURE_AUTO),
 780		.slave_ids	= { V4L2_CID_EXPOSURE_ABSOLUTE, },
 781	},
 782	{
 783		.id		= V4L2_CID_EXPOSURE_AUTO_PRIORITY,
 784		.entity		= UVC_GUID_UVC_CAMERA,
 785		.selector	= UVC_CT_AE_PRIORITY_CONTROL,
 786		.size		= 1,
 787		.offset		= 0,
 788		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
 789		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
 790	},
 791	{
 792		.id		= V4L2_CID_EXPOSURE_ABSOLUTE,
 793		.entity		= UVC_GUID_UVC_CAMERA,
 794		.selector	= UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
 795		.size		= 32,
 796		.offset		= 0,
 797		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 798		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 799		.master_id	= V4L2_CID_EXPOSURE_AUTO,
 800		.master_manual	= V4L2_EXPOSURE_MANUAL,
 801	},
 802	{
 803		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
 804		.entity		= UVC_GUID_UVC_PROCESSING,
 805		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
 806		.size		= 1,
 807		.offset		= 0,
 808		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
 809		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
 810		.slave_ids	= { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
 811	},
 812	{
 813		.id		= V4L2_CID_WHITE_BALANCE_TEMPERATURE,
 814		.entity		= UVC_GUID_UVC_PROCESSING,
 815		.selector	= UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
 816		.size		= 16,
 817		.offset		= 0,
 818		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 819		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 820		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
 821		.master_manual	= 0,
 822	},
 823	{
 824		.id		= V4L2_CID_AUTO_WHITE_BALANCE,
 825		.entity		= UVC_GUID_UVC_PROCESSING,
 826		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
 827		.size		= 1,
 828		.offset		= 0,
 829		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
 830		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
 831		.slave_ids	= { V4L2_CID_BLUE_BALANCE,
 832				    V4L2_CID_RED_BALANCE },
 833	},
 834	{
 835		.id		= V4L2_CID_BLUE_BALANCE,
 836		.entity		= UVC_GUID_UVC_PROCESSING,
 837		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
 838		.size		= 16,
 839		.offset		= 0,
 840		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 841		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 842		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
 843		.master_manual	= 0,
 844	},
 845	{
 846		.id		= V4L2_CID_RED_BALANCE,
 847		.entity		= UVC_GUID_UVC_PROCESSING,
 848		.selector	= UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
 849		.size		= 16,
 850		.offset		= 16,
 851		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 852		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 853		.master_id	= V4L2_CID_AUTO_WHITE_BALANCE,
 854		.master_manual	= 0,
 855	},
 856	{
 857		.id		= V4L2_CID_FOCUS_ABSOLUTE,
 858		.entity		= UVC_GUID_UVC_CAMERA,
 859		.selector	= UVC_CT_FOCUS_ABSOLUTE_CONTROL,
 860		.size		= 16,
 861		.offset		= 0,
 862		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 863		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 864		.master_id	= V4L2_CID_FOCUS_AUTO,
 865		.master_manual	= 0,
 866	},
 867	{
 868		.id		= V4L2_CID_FOCUS_AUTO,
 869		.entity		= UVC_GUID_UVC_CAMERA,
 870		.selector	= UVC_CT_FOCUS_AUTO_CONTROL,
 871		.size		= 1,
 872		.offset		= 0,
 873		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
 874		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
 875		.slave_ids	= { V4L2_CID_FOCUS_ABSOLUTE, },
 876	},
 877	{
 878		.id		= V4L2_CID_IRIS_ABSOLUTE,
 879		.entity		= UVC_GUID_UVC_CAMERA,
 880		.selector	= UVC_CT_IRIS_ABSOLUTE_CONTROL,
 881		.size		= 16,
 882		.offset		= 0,
 883		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 884		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 885	},
 886	{
 887		.id		= V4L2_CID_IRIS_RELATIVE,
 888		.entity		= UVC_GUID_UVC_CAMERA,
 889		.selector	= UVC_CT_IRIS_RELATIVE_CONTROL,
 890		.size		= 8,
 891		.offset		= 0,
 892		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 893		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 894	},
 895	{
 896		.id		= V4L2_CID_ZOOM_ABSOLUTE,
 897		.entity		= UVC_GUID_UVC_CAMERA,
 898		.selector	= UVC_CT_ZOOM_ABSOLUTE_CONTROL,
 899		.size		= 16,
 900		.offset		= 0,
 901		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 902		.data_type	= UVC_CTRL_DATA_TYPE_UNSIGNED,
 903	},
 904	{
 905		.id		= V4L2_CID_ZOOM_CONTINUOUS,
 906		.entity		= UVC_GUID_UVC_CAMERA,
 907		.selector	= UVC_CT_ZOOM_RELATIVE_CONTROL,
 908		.size		= 0,
 909		.offset		= 0,
 910		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 911		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 912		.get		= uvc_ctrl_get_zoom,
 913		.set		= uvc_ctrl_set_zoom,
 914	},
 915	{
 916		.id		= V4L2_CID_PAN_ABSOLUTE,
 917		.entity		= UVC_GUID_UVC_CAMERA,
 918		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
 919		.size		= 32,
 920		.offset		= 0,
 921		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 922		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 923	},
 924	{
 925		.id		= V4L2_CID_TILT_ABSOLUTE,
 926		.entity		= UVC_GUID_UVC_CAMERA,
 927		.selector	= UVC_CT_PANTILT_ABSOLUTE_CONTROL,
 928		.size		= 32,
 929		.offset		= 32,
 930		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 931		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 932	},
 933	{
 934		.id		= V4L2_CID_PAN_SPEED,
 935		.entity		= UVC_GUID_UVC_CAMERA,
 936		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
 937		.size		= 16,
 938		.offset		= 0,
 939		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 940		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 941		.get		= uvc_ctrl_get_rel_speed,
 942		.set		= uvc_ctrl_set_rel_speed,
 943	},
 944	{
 945		.id		= V4L2_CID_TILT_SPEED,
 946		.entity		= UVC_GUID_UVC_CAMERA,
 947		.selector	= UVC_CT_PANTILT_RELATIVE_CONTROL,
 948		.size		= 16,
 949		.offset		= 16,
 950		.v4l2_type	= V4L2_CTRL_TYPE_INTEGER,
 951		.data_type	= UVC_CTRL_DATA_TYPE_SIGNED,
 952		.get		= uvc_ctrl_get_rel_speed,
 953		.set		= uvc_ctrl_set_rel_speed,
 954	},
 955	{
 956		.id		= V4L2_CID_PRIVACY,
 957		.entity		= UVC_GUID_UVC_CAMERA,
 958		.selector	= UVC_CT_PRIVACY_CONTROL,
 959		.size		= 1,
 960		.offset		= 0,
 961		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
 962		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
 963	},
 964	{
 965		.id		= V4L2_CID_PRIVACY,
 966		.entity		= UVC_GUID_EXT_GPIO_CONTROLLER,
 967		.selector	= UVC_CT_PRIVACY_CONTROL,
 968		.size		= 1,
 969		.offset		= 0,
 970		.v4l2_type	= V4L2_CTRL_TYPE_BOOLEAN,
 971		.data_type	= UVC_CTRL_DATA_TYPE_BOOLEAN,
 972	},
 973	{
 974		.entity		= UVC_GUID_UVC_PROCESSING,
 975		.selector	= UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
 976		.filter_mapping	= uvc_ctrl_filter_plf_mapping,
 977	},
 978	{
 979		.id		= V4L2_CID_UVC_REGION_OF_INTEREST_RECT,
 980		.entity		= UVC_GUID_UVC_CAMERA,
 981		.selector	= UVC_CT_REGION_OF_INTEREST_CONTROL,
 982		.size		= sizeof(struct uvc_rect) * 8,
 983		.offset		= 0,
 984		.v4l2_type	= V4L2_CTRL_TYPE_RECT,
 985		.data_type	= UVC_CTRL_DATA_TYPE_RECT,
 986		.get		= uvc_get_rect,
 987		.set		= uvc_set_rect,
 988		.name		= "Region of Interest Rectangle",
 989	},
 990	{
 991		.id		= V4L2_CID_UVC_REGION_OF_INTEREST_AUTO,
 992		.entity		= UVC_GUID_UVC_CAMERA,
 993		.selector	= UVC_CT_REGION_OF_INTEREST_CONTROL,
 994		.size		= 16,
 995		.offset		= 64,
 996		.v4l2_type	= V4L2_CTRL_TYPE_BITMASK,
 997		.data_type	= UVC_CTRL_DATA_TYPE_BITMASK,
 998		.name		= "Region of Interest Auto Ctrls",
 999	},
1000	{
1001		.id		= V4L2_CID_COLORFX,
1002		.entity		= UVC_GUID_CHROMEOS_XU,
1003		.selector	= UVC_CROSXU_CONTROL_IQ_PROFILE,
1004		.size		= 8,
1005		.offset		= 0,
1006		.v4l2_type	= V4L2_CTRL_TYPE_MENU,
1007		.data_type	= UVC_CTRL_DATA_TYPE_ENUM,
1008		.menu_mapping	= cros_colorfx_mapping,
1009		.menu_mask	= BIT(V4L2_COLORFX_VIVID) |
1010				  BIT(V4L2_COLORFX_NONE),
1011	},
1012};
1013
1014/* ------------------------------------------------------------------------
1015 * Utility functions
1016 */
1017
1018static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
1019{
1020	return ctrl->uvc_data + id * ctrl->info.size;
1021}
1022
1023static inline int uvc_test_bit(const u8 *data, int bit)
1024{
1025	return (data[bit >> 3] >> (bit & 7)) & 1;
1026}
1027
1028static inline void uvc_clear_bit(u8 *data, int bit)
1029{
1030	data[bit >> 3] &= ~(1 << (bit & 7));
1031}
1032
1033static s32 uvc_menu_to_v4l2_menu(struct uvc_control_mapping *mapping, s32 val)
1034{
1035	unsigned int i;
1036
1037	for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
1038		u32 menu_value;
1039
1040		if (!test_bit(i, &mapping->menu_mask))
1041			continue;
1042
1043		menu_value = uvc_mapping_get_menu_value(mapping, i);
1044
1045		if (menu_value == val)
1046			return i;
1047	}
1048
1049	return val;
1050}
1051
1052/*
1053 * Extract the bit string specified by mapping->offset and mapping->size
1054 * from the little-endian data stored at 'data' and return the result as
1055 * a signed 32bit integer. Sign extension will be performed if the mapping
1056 * references a signed data type.
1057 */
1058static int uvc_get_le_value(struct uvc_control_mapping *mapping,
1059			    u8 query, const void *uvc_in, size_t v4l2_size,
1060			    void *v4l2_out)
1061{
1062	int offset = mapping->offset;
1063	int bits = mapping->size;
1064	const u8 *data = uvc_in;
1065	s32 *out = v4l2_out;
1066	s32 value = 0;
1067	u8 mask;
1068
1069	if (WARN_ON(v4l2_size != sizeof(s32)))
1070		return -EINVAL;
1071
1072	data += offset / 8;
1073	offset &= 7;
1074	mask = ((1LL << bits) - 1) << offset;
1075
1076	while (1) {
1077		u8 byte = *data & mask;
1078		value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
1079		bits -= 8 - max(offset, 0);
1080		if (bits <= 0)
1081			break;
1082
1083		offset -= 8;
1084		mask = (1 << bits) - 1;
1085		data++;
1086	}
1087
1088	/* Sign-extend the value if needed. */
1089	if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1090		value |= -(value & (1 << (mapping->size - 1)));
1091
1092	/* If it is a menu, convert from uvc to v4l2. */
1093	if (mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1094		*out = value;
1095		return 0;
1096	}
1097
1098	switch (query) {
1099	case UVC_GET_CUR:
1100	case UVC_GET_DEF:
1101		*out = uvc_menu_to_v4l2_menu(mapping, value);
1102		return 0;
1103	}
1104
1105	*out = value;
1106	return 0;
1107}
1108
1109/*
1110 * Set the bit string specified by mapping->offset and mapping->size
1111 * in the little-endian data stored at 'data' to the value 'value'.
1112 */
1113static int uvc_set_le_value(struct uvc_control_mapping *mapping,
1114			    size_t v4l2_size, const void *v4l2_in,
1115			    void *uvc_out)
1116{
1117	int offset = mapping->offset;
1118	int bits = mapping->size;
1119	u8 *data = uvc_out;
1120	s32 value;
1121	u8 mask;
1122
1123	if (WARN_ON(v4l2_size != sizeof(s32)))
1124		return -EINVAL;
1125
1126	value = *(s32 *)v4l2_in;
1127
1128	switch (mapping->v4l2_type) {
1129	case V4L2_CTRL_TYPE_MENU:
1130		value = uvc_mapping_get_menu_value(mapping, value);
1131		break;
1132	case V4L2_CTRL_TYPE_BUTTON:
1133		/*
1134		 * According to the v4l2 spec, writing any value to a button
1135		 * control should result in the action belonging to the button
1136		 * control being triggered. UVC devices however want to see a 1
1137		 * written -> override value.
1138		 */
1139		value = -1;
1140		break;
1141	default:
1142		break;
1143	}
1144
1145	data += offset / 8;
1146	offset &= 7;
1147
1148	for (; bits > 0; data++) {
1149		mask = ((1LL << bits) - 1) << offset;
1150		*data = (*data & ~mask) | ((value << offset) & mask);
1151		value >>= offset ? offset : 8;
1152		bits -= 8 - offset;
1153		offset = 0;
1154	}
1155
1156	return 0;
1157}
1158
1159/* ------------------------------------------------------------------------
1160 * Terminal and unit management
1161 */
1162
1163static int uvc_entity_match_guid(const struct uvc_entity *entity,
1164				 const u8 guid[16])
1165{
1166	return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
1167}
1168
1169/* ------------------------------------------------------------------------
1170 * UVC Controls
1171 */
1172
1173static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
1174	struct uvc_control_mapping **mapping, struct uvc_control **control,
1175	int next, int next_compound)
1176{
1177	struct uvc_control *ctrl;
1178	struct uvc_control_mapping *map;
1179	unsigned int i;
1180
1181	if (entity == NULL)
1182		return;
1183
1184	for (i = 0; i < entity->ncontrols; ++i) {
1185		ctrl = &entity->controls[i];
1186		if (!ctrl->initialized)
1187			continue;
1188
1189		list_for_each_entry(map, &ctrl->info.mappings, list) {
1190			if (map->id == v4l2_id && !next && !next_compound) {
1191				*control = ctrl;
1192				*mapping = map;
1193				return;
1194			}
1195
1196			if ((*mapping == NULL || (*mapping)->id > map->id) &&
1197			    (map->id > v4l2_id) &&
1198			    (uvc_ctrl_mapping_is_compound(map) ?
1199			     next_compound : next)) {
1200				*control = ctrl;
1201				*mapping = map;
1202			}
1203		}
1204	}
1205}
1206
1207static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
1208	u32 v4l2_id, struct uvc_control_mapping **mapping)
1209{
1210	struct uvc_control *ctrl = NULL;
1211	struct uvc_entity *entity;
1212	int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
1213	int next_compound = v4l2_id & V4L2_CTRL_FLAG_NEXT_COMPOUND;
1214
1215	*mapping = NULL;
1216
1217	/* Mask the query flags. */
1218	v4l2_id &= V4L2_CTRL_ID_MASK;
1219
1220	/* Find the control. */
1221	list_for_each_entry(entity, &chain->entities, chain) {
1222		__uvc_find_control(entity, v4l2_id, mapping, &ctrl, next,
1223				   next_compound);
1224		if (ctrl && !next && !next_compound)
1225			return ctrl;
1226	}
1227
1228	if (!ctrl && !next && !next_compound)
1229		uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
1230			v4l2_id);
1231
1232	return ctrl;
1233}
1234
1235static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
1236	struct uvc_control *ctrl)
1237{
1238	int ret;
1239
1240	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1241		ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
1242				     chain->dev->intfnum, ctrl->info.selector,
1243				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
1244				     ctrl->info.size);
1245		if (ret < 0)
1246			return ret;
1247	}
1248
1249	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
1250		ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
1251				     chain->dev->intfnum, ctrl->info.selector,
1252				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
1253				     ctrl->info.size);
1254		if (ret < 0)
1255			return ret;
1256	}
1257	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
1258		ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
1259				     chain->dev->intfnum, ctrl->info.selector,
1260				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
1261				     ctrl->info.size);
1262		if (ret < 0)
1263			return ret;
1264	}
1265	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
1266		ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
1267				     chain->dev->intfnum, ctrl->info.selector,
1268				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
1269				     ctrl->info.size);
1270		if (ret < 0) {
1271			if (UVC_ENTITY_TYPE(ctrl->entity) !=
1272			    UVC_VC_EXTENSION_UNIT)
1273				return ret;
1274
1275			/*
1276			 * GET_RES is mandatory for XU controls, but some
1277			 * cameras still choke on it. Ignore errors and set the
1278			 * resolution value to zero.
1279			 */
1280			uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
1281				      "UVC non compliance - GET_RES failed on "
1282				      "an XU control. Enabling workaround.\n");
1283			memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
1284			       ctrl->info.size);
1285		}
1286	}
1287
1288	ctrl->cached = 1;
1289	return 0;
1290}
1291
1292static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
1293			       struct uvc_control *ctrl)
1294{
1295	u8 *data;
1296	int ret;
1297
1298	if (ctrl->loaded)
1299		return 0;
1300
1301	data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1302
1303	if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1304		memset(data, 0, ctrl->info.size);
1305		ctrl->loaded = 1;
1306
1307		return 0;
1308	}
1309
1310	if (ctrl->entity->get_cur)
1311		ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
1312					    ctrl->info.selector, data,
1313					    ctrl->info.size);
1314	else
1315		ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1316				     ctrl->entity->id, chain->dev->intfnum,
1317				     ctrl->info.selector, data,
1318				     ctrl->info.size);
1319
1320	if (ret < 0)
1321		return ret;
1322
1323	ctrl->loaded = 1;
1324
1325	return ret;
1326}
1327
1328static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1329			  struct uvc_control *ctrl,
1330			  struct uvc_control_mapping *mapping,
1331			  s32 *value)
1332{
1333	int ret;
1334
1335	if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1336		return -EACCES;
1337
1338	ret = __uvc_ctrl_load_cur(chain, ctrl);
1339	if (ret < 0)
1340		return ret;
1341
1342	*value = uvc_mapping_get_s32(mapping, UVC_GET_CUR,
1343				     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1344
1345	return 0;
1346}
1347
1348static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1349				  u32 found_id)
1350{
1351	bool find_next = req_id &
1352		(V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND);
1353	unsigned int i;
1354
1355	req_id &= V4L2_CTRL_ID_MASK;
1356
1357	for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
1358		if (!(chain->ctrl_class_bitmap & BIT(i)))
1359			continue;
1360		if (!find_next) {
1361			if (uvc_control_classes[i] == req_id)
1362				return i;
1363			continue;
1364		}
1365		if (uvc_control_classes[i] > req_id &&
1366		    uvc_control_classes[i] < found_id)
1367			return i;
1368	}
1369
1370	return -ENODEV;
1371}
1372
1373static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
1374				u32 found_id,
1375				struct v4l2_query_ext_ctrl *v4l2_ctrl)
1376{
1377	int idx;
1378
1379	idx = __uvc_query_v4l2_class(chain, req_id, found_id);
1380	if (idx < 0)
1381		return -ENODEV;
1382
1383	memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1384	v4l2_ctrl->id = uvc_control_classes[idx];
1385	strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
1386		sizeof(v4l2_ctrl->name));
1387	v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
1388	v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
1389			 | V4L2_CTRL_FLAG_READ_ONLY;
1390	return 0;
1391}
1392
1393static bool uvc_ctrl_is_readable(u32 which, struct uvc_control *ctrl,
1394				 struct uvc_control_mapping *mapping)
1395{
1396	if (which == V4L2_CTRL_WHICH_CUR_VAL)
1397		return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR);
1398
1399	if (which == V4L2_CTRL_WHICH_DEF_VAL)
1400		return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF);
1401
1402	/* Types with implicit boundaries. */
1403	switch (mapping->v4l2_type) {
1404	case V4L2_CTRL_TYPE_MENU:
1405	case V4L2_CTRL_TYPE_BOOLEAN:
1406	case V4L2_CTRL_TYPE_BUTTON:
1407		return true;
1408	case V4L2_CTRL_TYPE_BITMASK:
1409		return (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) ||
1410			(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX);
1411	default:
1412		break;
1413	}
1414
1415	if (which == V4L2_CTRL_WHICH_MIN_VAL)
1416		return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN);
1417
1418	if (which == V4L2_CTRL_WHICH_MAX_VAL)
1419		return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX);
1420
1421	return false;
1422}
1423
1424/*
1425 * Check if control @v4l2_id can be accessed by the given control @ioctl
1426 * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
1427 *
1428 * For set operations on slave controls, check if the master's value is set to
1429 * manual, either in the others controls set in the same ioctl call, or from
1430 * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
1431 * both the master and slave control, such as for instance setting
1432 * auto_exposure=1, exposure_time_absolute=251.
1433 */
1434int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
1435			   const struct v4l2_ext_controls *ctrls, u32 which,
1436			   unsigned long ioctl)
1437{
1438	struct uvc_control_mapping *master_map = NULL;
1439	struct uvc_control *master_ctrl = NULL;
1440	struct uvc_control_mapping *mapping;
1441	struct uvc_control *ctrl;
1442	s32 val;
1443	int ret;
1444	int i;
1445	/*
1446	 * There is no need to check the ioctl, all the ioctls except
1447	 * VIDIOC_G_EXT_CTRLS use which=V4L2_CTRL_WHICH_CUR_VAL.
1448	 */
1449	bool is_which_min_max = which == V4L2_CTRL_WHICH_MIN_VAL ||
1450				which == V4L2_CTRL_WHICH_MAX_VAL;
1451
1452	if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
1453		return is_which_min_max ? -EINVAL : -EACCES;
1454
1455	ctrl = uvc_find_control(chain, v4l2_id, &mapping);
1456	if (!ctrl)
1457		return -EINVAL;
1458
1459	if ((!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) ||
1460	     !(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)) && is_which_min_max)
1461		return -EINVAL;
1462
1463	if (ioctl == VIDIOC_G_EXT_CTRLS)
1464		return uvc_ctrl_is_readable(ctrls->which, ctrl, mapping);
1465
1466	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1467		return -EACCES;
1468
1469	if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id)
1470		return 0;
1471
1472	/*
1473	 * Iterate backwards in cases where the master control is accessed
1474	 * multiple times in the same ioctl. We want the last value.
1475	 */
1476	for (i = ctrls->count - 1; i >= 0; i--) {
1477		if (ctrls->controls[i].id == mapping->master_id)
1478			return ctrls->controls[i].value ==
1479					mapping->master_manual ? 0 : -EACCES;
1480	}
1481
1482	__uvc_find_control(ctrl->entity, mapping->master_id, &master_map,
1483			   &master_ctrl, 0, 0);
1484
1485	if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1486		return 0;
1487	if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map)))
1488		return -EIO;
1489
1490	ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1491	if (ret >= 0 && val != mapping->master_manual)
1492		return -EACCES;
1493
1494	return 0;
1495}
1496
1497static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
1498{
1499	const char *name;
1500
1501	if (map->name)
1502		return map->name;
1503
1504	name = v4l2_ctrl_get_name(map->id);
1505	if (name)
1506		return name;
1507
1508	return "Unknown Control";
1509}
1510
1511static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl,
1512			       struct uvc_control_mapping *mapping)
1513{
1514	/*
1515	 * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
1516	 * the number of bits supported. Those controls do not list GET_MAX
1517	 * as supported.
1518	 */
1519	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1520		return uvc_mapping_get_s32(mapping, UVC_GET_RES,
1521					   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1522
1523	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1524		return uvc_mapping_get_s32(mapping, UVC_GET_MAX,
1525					   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1526
1527	return ~0;
1528}
1529
1530/*
1531 * Maximum retry count to avoid spurious errors with controls. Increasing this
1532 * value does no seem to produce better results in the tested hardware.
1533 */
1534#define MAX_QUERY_RETRIES 2
1535
1536static int __uvc_queryctrl_boundaries(struct uvc_video_chain *chain,
1537				      struct uvc_control *ctrl,
1538				      struct uvc_control_mapping *mapping,
1539				      struct v4l2_query_ext_ctrl *v4l2_ctrl)
1540{
1541	if (!ctrl->cached) {
1542		unsigned int retries;
1543		int ret;
1544
1545		for (retries = 0; retries < MAX_QUERY_RETRIES; retries++) {
1546			ret = uvc_ctrl_populate_cache(chain, ctrl);
1547			if (ret != -EIO)
1548				break;
1549		}
1550
1551		if (ret)
1552			return ret;
1553	}
1554
1555	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1556		v4l2_ctrl->default_value = uvc_mapping_get_s32(mapping,
1557				UVC_GET_DEF, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1558	}
1559
1560	switch (mapping->v4l2_type) {
1561	case V4L2_CTRL_TYPE_MENU:
1562		v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1;
1563		v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1;
1564		v4l2_ctrl->step = 1;
1565		return 0;
1566
1567	case V4L2_CTRL_TYPE_BOOLEAN:
1568		v4l2_ctrl->minimum = 0;
1569		v4l2_ctrl->maximum = 1;
1570		v4l2_ctrl->step = 1;
1571		return 0;
1572
1573	case V4L2_CTRL_TYPE_BUTTON:
1574		v4l2_ctrl->minimum = 0;
1575		v4l2_ctrl->maximum = 0;
1576		v4l2_ctrl->step = 0;
1577		return 0;
1578
1579	case V4L2_CTRL_TYPE_BITMASK:
1580		v4l2_ctrl->minimum = 0;
1581		v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping);
1582		v4l2_ctrl->step = 0;
1583		return 0;
1584
1585	default:
1586		break;
1587	}
1588
1589	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1590		v4l2_ctrl->minimum = uvc_mapping_get_s32(mapping, UVC_GET_MIN,
1591				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1592	else
1593		v4l2_ctrl->minimum = 0;
1594
1595	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1596		v4l2_ctrl->maximum = uvc_mapping_get_s32(mapping, UVC_GET_MAX,
1597				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1598	else
1599		v4l2_ctrl->maximum = 0;
1600
1601	if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1602		v4l2_ctrl->step = uvc_mapping_get_s32(mapping, UVC_GET_RES,
1603				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1604	else
1605		v4l2_ctrl->step = 0;
1606
1607	return 0;
1608}
1609
1610static size_t uvc_mapping_v4l2_size(struct uvc_control_mapping *mapping)
1611{
1612	if (mapping->v4l2_type == V4L2_CTRL_TYPE_RECT)
1613		return sizeof(struct v4l2_rect);
1614
1615	if (uvc_ctrl_mapping_is_compound(mapping))
1616		return DIV_ROUND_UP(mapping->size, 8);
1617
1618	return sizeof(s32);
1619}
1620
1621static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1622				 struct uvc_control *ctrl,
1623				 struct uvc_control_mapping *mapping,
1624				 struct v4l2_query_ext_ctrl *v4l2_ctrl)
1625{
1626	struct uvc_control_mapping *master_map = NULL;
1627	struct uvc_control *master_ctrl = NULL;
1628	int ret;
1629
1630	memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1631	v4l2_ctrl->id = mapping->id;
1632	v4l2_ctrl->type = mapping->v4l2_type;
1633	strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
1634		sizeof(v4l2_ctrl->name));
1635	v4l2_ctrl->flags = 0;
1636
1637	if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1638		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1639	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1640		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1641	if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) &&
1642	    (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN))
1643		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX;
1644
1645	if (mapping->master_id)
1646		__uvc_find_control(ctrl->entity, mapping->master_id,
1647				   &master_map, &master_ctrl, 0, 0);
1648	if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1649		unsigned int retries;
1650		s32 val;
1651		int ret;
1652
1653		if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map)))
1654			return -EIO;
1655
1656		for (retries = 0; retries < MAX_QUERY_RETRIES; retries++) {
1657			ret = __uvc_ctrl_get(chain, master_ctrl, master_map,
1658					     &val);
1659			if (!ret)
1660				break;
1661			if (ret < 0 && ret != -EIO)
1662				return ret;
1663		}
1664
1665		if (ret == -EIO) {
1666			dev_warn_ratelimited(&chain->dev->intf->dev,
1667					     "UVC non compliance: Error %d querying master control %x (%s)\n",
1668					     ret, master_map->id,
1669					     uvc_map_get_name(master_map));
1670		} else {
1671			if (val != mapping->master_manual)
1672				v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1673		}
1674	}
1675
1676	v4l2_ctrl->elem_size = uvc_mapping_v4l2_size(mapping);
1677	v4l2_ctrl->elems = 1;
1678
1679	if (v4l2_ctrl->type >= V4L2_CTRL_COMPOUND_TYPES) {
1680		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
1681		v4l2_ctrl->default_value = 0;
1682		v4l2_ctrl->minimum = 0;
1683		v4l2_ctrl->maximum = 0;
1684		v4l2_ctrl->step = 0;
1685		return 0;
1686	}
1687
1688	ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, v4l2_ctrl);
1689	if (ret && !mapping->disabled) {
1690		dev_warn(&chain->dev->intf->dev,
1691			 "UVC non compliance: permanently disabling control %x (%s), due to error %d\n",
1692			 mapping->id, uvc_map_get_name(mapping), ret);
1693		mapping->disabled = true;
1694	}
1695
1696	if (mapping->disabled)
1697		v4l2_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1698
1699	return 0;
1700}
1701
1702int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1703			struct v4l2_query_ext_ctrl *v4l2_ctrl)
1704{
1705	struct uvc_control *ctrl;
1706	struct uvc_control_mapping *mapping;
1707	int ret;
1708
1709	ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1710	if (ret < 0)
1711		return -ERESTARTSYS;
1712
1713	/* Check if the ctrl is a know class */
1714	if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
1715		ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
1716		if (!ret)
1717			goto done;
1718	}
1719
1720	ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1721	if (ctrl == NULL) {
1722		ret = -EINVAL;
1723		goto done;
1724	}
1725
1726	/*
1727	 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
1728	 * a class should be inserted between the previous control and the one
1729	 * we have just found.
1730	 */
1731	if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1732		ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
1733					   v4l2_ctrl);
1734		if (!ret)
1735			goto done;
1736	}
1737
1738	ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1739done:
1740	mutex_unlock(&chain->ctrl_mutex);
1741	return ret;
1742}
1743
1744/*
1745 * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1746 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1747 * must be grouped (for instance the Red Balance, Blue Balance and Do White
1748 * Balance V4L2 controls use the White Balance Component UVC control) or
1749 * otherwise translated. The approach we take here is to use a translation
1750 * table for the controls that can be mapped directly, and handle the others
1751 * manually.
1752 */
1753int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1754	struct v4l2_querymenu *query_menu)
1755{
1756	struct uvc_control_mapping *mapping;
1757	struct uvc_control *ctrl;
1758	u32 index = query_menu->index;
1759	u32 id = query_menu->id;
1760	const char *name;
1761	int ret;
1762
1763	memset(query_menu, 0, sizeof(*query_menu));
1764	query_menu->id = id;
1765	query_menu->index = index;
1766
1767	if (index >= BITS_PER_TYPE(mapping->menu_mask))
1768		return -EINVAL;
1769
1770	ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1771	if (ret < 0)
1772		return -ERESTARTSYS;
1773
1774	ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1775	if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1776		ret = -EINVAL;
1777		goto done;
1778	}
1779
1780	if (!test_bit(query_menu->index, &mapping->menu_mask)) {
1781		ret = -EINVAL;
1782		goto done;
1783	}
1784
1785	if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
1786		int mask;
1787
1788		if (!ctrl->cached) {
1789			ret = uvc_ctrl_populate_cache(chain, ctrl);
1790			if (ret < 0)
1791				goto done;
1792		}
1793
1794		mask = uvc_mapping_get_menu_value(mapping, query_menu->index);
1795		if (mask < 0) {
1796			ret = mask;
1797			goto done;
1798		}
1799
1800		if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) {
1801			ret = -EINVAL;
1802			goto done;
1803		}
1804	}
1805
1806	name = uvc_mapping_get_menu_name(mapping, query_menu->index);
1807	if (!name) {
1808		ret = -EINVAL;
1809		goto done;
1810	}
1811
1812	strscpy(query_menu->name, name, sizeof(query_menu->name));
1813
1814done:
1815	mutex_unlock(&chain->ctrl_mutex);
1816	return ret;
1817}
1818
1819/* --------------------------------------------------------------------------
1820 * Ctrl event handling
1821 */
1822
1823static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1824	struct v4l2_event *ev,
1825	struct uvc_control *ctrl,
1826	struct uvc_control_mapping *mapping,
1827	s32 value, u32 changes)
1828{
1829	struct v4l2_query_ext_ctrl v4l2_ctrl;
1830
1831	__uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1832
1833	memset(ev, 0, sizeof(*ev));
1834	ev->type = V4L2_EVENT_CTRL;
1835	ev->id = v4l2_ctrl.id;
1836	ev->u.ctrl.value = value;
1837	ev->u.ctrl.changes = changes;
1838	ev->u.ctrl.type = v4l2_ctrl.type;
1839	ev->u.ctrl.flags = v4l2_ctrl.flags;
1840	ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1841	ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1842	ev->u.ctrl.step = v4l2_ctrl.step;
1843	ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1844}
1845
1846/*
1847 * Send control change events to all subscribers for the @ctrl control. By
1848 * default the subscriber that generated the event, as identified by @handle,
1849 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1850 * @handle can be NULL for asynchronous events related to auto-update controls,
1851 * in which case all subscribers are notified.
1852 */
1853static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1854	struct uvc_fh *handle, struct uvc_control *ctrl,
1855	struct uvc_control_mapping *mapping, s32 value, u32 changes)
1856{
1857	struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1858	struct v4l2_subscribed_event *sev;
1859	struct v4l2_event ev;
1860
1861	if (list_empty(&mapping->ev_subs))
1862		return;
1863
1864	uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1865
1866	list_for_each_entry(sev, &mapping->ev_subs, node) {
1867		if (sev->fh != originator ||
1868		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1869		    (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1870			v4l2_event_queue_fh(sev->fh, &ev);
1871	}
1872}
1873
1874/*
1875 * Send control change events for the slave of the @master control identified
1876 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1877 * generated the event and may be NULL for auto-update events.
1878 */
1879static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1880	struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1881{
1882	struct uvc_control_mapping *mapping = NULL;
1883	struct uvc_control *ctrl = NULL;
1884	u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1885	s32 val = 0;
1886
1887	__uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0, 0);
1888	if (ctrl == NULL)
1889		return;
1890
1891	if (uvc_ctrl_mapping_is_compound(mapping) ||
1892	    __uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1893		changes |= V4L2_EVENT_CTRL_CH_VALUE;
1894
1895	uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1896}
1897
1898static int uvc_ctrl_set_handle(struct uvc_control *ctrl, struct uvc_fh *handle)
1899{
1900	int ret;
1901
1902	lockdep_assert_held(&handle->chain->ctrl_mutex);
1903
1904	if (ctrl->handle) {
1905		dev_warn_ratelimited(&handle->stream->dev->intf->dev,
1906				     "UVC non compliance: Setting an async control with a pending operation.");
1907
1908		if (ctrl->handle == handle)
1909			return 0;
1910
1911		WARN_ON(!ctrl->handle->pending_async_ctrls);
1912		if (ctrl->handle->pending_async_ctrls)
1913			ctrl->handle->pending_async_ctrls--;
1914		ctrl->handle = handle;
1915		ctrl->handle->pending_async_ctrls++;
1916		return 0;
1917	}
1918
1919	ret = uvc_pm_get(handle->chain->dev);
1920	if (ret)
1921		return ret;
1922
1923	ctrl->handle = handle;
1924	ctrl->handle->pending_async_ctrls++;
1925	return 0;
1926}
1927
1928static int uvc_ctrl_clear_handle(struct uvc_control *ctrl)
1929{
1930	lockdep_assert_held(&ctrl->handle->chain->ctrl_mutex);
1931
1932	if (WARN_ON(!ctrl->handle->pending_async_ctrls)) {
1933		ctrl->handle = NULL;
1934		return -EINVAL;
1935	}
1936
1937	ctrl->handle->pending_async_ctrls--;
1938	uvc_pm_put(ctrl->handle->chain->dev);
1939	ctrl->handle = NULL;
1940	return 0;
1941}
1942
1943void uvc_ctrl_status_event(struct uvc_video_chain *chain,
1944			   struct uvc_control *ctrl, const u8 *data)
1945{
1946	struct uvc_control_mapping *mapping;
1947	struct uvc_fh *handle;
1948	unsigned int i;
1949
1950	mutex_lock(&chain->ctrl_mutex);
1951
1952	/* Flush the control cache, the data might have changed. */
1953	ctrl->loaded = 0;
1954
1955	handle = ctrl->handle;
1956	if (handle)
1957		uvc_ctrl_clear_handle(ctrl);
1958
1959	list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1960		s32 value;
1961
1962		if (uvc_ctrl_mapping_is_compound(mapping))
1963			value = 0;
1964		else
1965			value = uvc_mapping_get_s32(mapping, UVC_GET_CUR, data);
1966
1967		/*
1968		 * handle may be NULL here if the device sends auto-update
1969		 * events without a prior related control set from userspace.
1970		 */
1971		for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1972			if (!mapping->slave_ids[i])
1973				break;
1974
1975			uvc_ctrl_send_slave_event(chain, handle, ctrl,
1976						  mapping->slave_ids[i]);
1977		}
1978
1979		uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1980				    V4L2_EVENT_CTRL_CH_VALUE);
1981	}
1982
1983	mutex_unlock(&chain->ctrl_mutex);
1984}
1985
1986static void uvc_ctrl_status_event_work(struct work_struct *work)
1987{
1988	struct uvc_device *dev = container_of(work, struct uvc_device,
1989					      async_ctrl.work);
1990	struct uvc_ctrl_work *w = &dev->async_ctrl;
1991	int ret;
1992
1993	uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
1994
1995	/* The barrier is needed to synchronize with uvc_status_stop(). */
1996	if (smp_load_acquire(&dev->flush_status))
1997		return;
1998
1999	/* Resubmit the URB. */
2000	w->urb->interval = dev->int_ep->desc.bInterval;
2001	ret = usb_submit_urb(w->urb, GFP_KERNEL);
2002	if (ret < 0)
2003		dev_err(&dev->intf->dev,
2004			"Failed to resubmit status URB (%d).\n", ret);
2005}
2006
2007bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
2008				 struct uvc_control *ctrl, const u8 *data)
2009{
2010	struct uvc_device *dev = chain->dev;
2011	struct uvc_ctrl_work *w = &dev->async_ctrl;
2012
2013	if (list_empty(&ctrl->info.mappings))
2014		return false;
2015
2016	w->data = data;
2017	w->urb = urb;
2018	w->chain = chain;
2019	w->ctrl = ctrl;
2020
2021	schedule_work(&w->work);
2022
2023	return true;
2024}
2025
2026static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
2027					unsigned int xctrls_count, u32 id)
2028{
2029	unsigned int i;
2030
2031	for (i = 0; i < xctrls_count; ++i) {
2032		if (xctrls[i].id == id)
2033			return true;
2034	}
2035
2036	return false;
2037}
2038
2039static void uvc_ctrl_send_events(struct uvc_fh *handle,
2040				 struct uvc_entity *entity,
2041				 const struct v4l2_ext_control *xctrls,
2042				 unsigned int xctrls_count)
2043{
2044	struct uvc_control_mapping *mapping;
2045	struct uvc_control *ctrl;
2046	unsigned int i;
2047	unsigned int j;
2048
2049	for (i = 0; i < xctrls_count; ++i) {
2050		u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
2051		s32 value;
2052
2053		ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
2054		if (ctrl->entity != entity)
2055			continue;
2056
2057		if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
2058			/* Notification will be sent from an Interrupt event. */
2059			continue;
2060
2061		for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
2062			u32 slave_id = mapping->slave_ids[j];
2063
2064			if (!slave_id)
2065				break;
2066
2067			/*
2068			 * We can skip sending an event for the slave if the
2069			 * slave is being modified in the same transaction.
2070			 */
2071			if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
2072							slave_id))
2073				continue;
2074
2075			uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
2076						  slave_id);
2077		}
2078
2079		if (uvc_ctrl_mapping_is_compound(mapping))
2080			value = 0;
2081		else
2082			value = xctrls[i].value;
2083		/*
2084		 * If the master is being modified in the same transaction
2085		 * flags may change too.
2086		 */
2087		if (mapping->master_id &&
2088		    uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
2089						mapping->master_id))
2090			changes |= V4L2_EVENT_CTRL_CH_FLAGS;
2091
2092		uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
2093				    value, changes);
2094	}
2095}
2096
2097static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
2098{
2099	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
2100	struct uvc_control_mapping *mapping;
2101	struct uvc_control *ctrl;
2102	int ret;
2103
2104	ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
2105	if (ret < 0)
2106		return -ERESTARTSYS;
2107
2108	if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
2109		ret = 0;
2110		goto done;
2111	}
2112
2113	ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
2114	if (ctrl == NULL) {
2115		ret = -EINVAL;
2116		goto done;
2117	}
2118
2119	if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
2120		struct v4l2_event ev;
2121		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
2122		s32 val = 0;
2123
2124		ret = uvc_pm_get(handle->chain->dev);
2125		if (ret)
2126			goto done;
2127
2128		if (uvc_ctrl_mapping_is_compound(mapping) ||
2129		    __uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
2130			changes |= V4L2_EVENT_CTRL_CH_VALUE;
2131
2132		uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
2133				    changes);
2134
2135		uvc_pm_put(handle->chain->dev);
2136
2137		/*
2138		 * Mark the queue as active, allowing this initial event to be
2139		 * accepted.
2140		 */
2141		sev->elems = elems;
2142		v4l2_event_queue_fh(sev->fh, &ev);
2143	}
2144
2145	list_add_tail(&sev->node, &mapping->ev_subs);
2146
2147done:
2148	mutex_unlock(&handle->chain->ctrl_mutex);
2149	return ret;
2150}
2151
2152static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
2153{
2154	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
2155
2156	mutex_lock(&handle->chain->ctrl_mutex);
2157	if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
2158		goto done;
2159	list_del(&sev->node);
2160done:
2161	mutex_unlock(&handle->chain->ctrl_mutex);
2162}
2163
2164const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
2165	.add = uvc_ctrl_add_event,
2166	.del = uvc_ctrl_del_event,
2167	.replace = v4l2_ctrl_replace,
2168	.merge = v4l2_ctrl_merge,
2169};
2170
2171/* --------------------------------------------------------------------------
2172 * Control transactions
2173 *
2174 * To make extended set operations as atomic as the hardware allows, controls
2175 * are handled using begin/commit/rollback operations.
2176 *
2177 * At the beginning of a set request, uvc_ctrl_begin should be called to
2178 * initialize the request. This function acquires the control lock.
2179 *
2180 * When setting a control, the new value is stored in the control data field
2181 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
2182 * later processing. If the UVC and V4L2 control sizes differ, the current
2183 * value is loaded from the hardware before storing the new value in the data
2184 * field.
2185 *
2186 * After processing all controls in the transaction, uvc_ctrl_commit or
2187 * uvc_ctrl_rollback must be called to apply the pending changes to the
2188 * hardware or revert them. When applying changes, all controls marked as
2189 * dirty will be modified in the UVC device, and the dirty flag will be
2190 * cleared. When reverting controls, the control data field
2191 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
2192 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
2193 * control lock.
2194 */
2195int uvc_ctrl_begin(struct uvc_video_chain *chain)
2196{
2197	return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
2198}
2199
2200/*
2201 * Returns the number of uvc controls that have been correctly set, or a
2202 * negative number if there has been an error.
2203 */
2204static int uvc_ctrl_commit_entity(struct uvc_device *dev,
2205				  struct uvc_fh *handle,
2206				  struct uvc_entity *entity,
2207				  int rollback,
2208				  struct uvc_control **err_ctrl)
2209{
2210	unsigned int processed_ctrls = 0;
2211	struct uvc_control *ctrl;
2212	unsigned int i;
2213	int ret = 0;
2214
2215	if (entity == NULL)
2216		return 0;
2217
2218	for (i = 0; i < entity->ncontrols; ++i) {
2219		ctrl = &entity->controls[i];
2220		if (!ctrl->initialized)
2221			continue;
2222
2223		/*
2224		 * Reset the loaded flag for auto-update controls that were
2225		 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
2226		 * uvc_ctrl_get from using the cached value, and for write-only
2227		 * controls to prevent uvc_ctrl_set from setting bits not
2228		 * explicitly set by the user.
2229		 */
2230		if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
2231		    !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
2232			ctrl->loaded = 0;
2233
2234		if (!ctrl->dirty)
2235			continue;
2236
2237		if (!rollback)
2238			ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
2239				dev->intfnum, ctrl->info.selector,
2240				uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2241				ctrl->info.size);
2242
2243		if (!ret)
2244			processed_ctrls++;
2245
2246		if (rollback || ret < 0)
2247			memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2248			       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2249			       ctrl->info.size);
2250
2251		ctrl->dirty = 0;
2252
2253		if (!rollback && handle && !ret &&
2254		    ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
2255			ret = uvc_ctrl_set_handle(ctrl, handle);
2256
2257		if (ret < 0 && !rollback) {
2258			if (err_ctrl)
2259				*err_ctrl = ctrl;
2260			/*
2261			 * If we fail to set a control, we need to rollback
2262			 * the next ones.
2263			 */
2264			rollback = 1;
2265		}
2266	}
2267
2268	if (ret)
2269		return ret;
2270
2271	return processed_ctrls;
2272}
2273
2274static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
2275				  struct v4l2_ext_controls *ctrls,
2276				  struct uvc_control *uvc_control)
2277{
2278	struct uvc_control_mapping *mapping = NULL;
2279	struct uvc_control *ctrl_found = NULL;
2280	unsigned int i;
2281
2282	if (!entity)
2283		return ctrls->count;
2284
2285	for (i = 0; i < ctrls->count; i++) {
2286		__uvc_find_control(entity, ctrls->controls[i].id, &mapping,
2287				   &ctrl_found, 0, 0);
2288		if (uvc_control == ctrl_found)
2289			return i;
2290	}
2291
2292	return ctrls->count;
2293}
2294
2295int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
2296		      struct v4l2_ext_controls *ctrls)
2297{
2298	struct uvc_video_chain *chain = handle->chain;
2299	struct uvc_control *err_ctrl;
2300	struct uvc_entity *entity;
2301	int ret_out = 0;
2302	int ret;
2303
2304	/* Find the control. */
2305	list_for_each_entry(entity, &chain->entities, chain) {
2306		ret = uvc_ctrl_commit_entity(chain->dev, handle, entity,
2307					     rollback, &err_ctrl);
2308		if (ret < 0) {
2309			if (ctrls)
2310				ctrls->error_idx =
2311					uvc_ctrl_find_ctrl_idx(entity, ctrls,
2312							       err_ctrl);
2313			/*
2314			 * When we fail to commit an entity, we need to
2315			 * restore the UVC_CTRL_DATA_BACKUP for all the
2316			 * controls in the other entities, otherwise our cache
2317			 * and the hardware will be out of sync.
2318			 */
2319			rollback = 1;
2320
2321			ret_out = ret;
2322		} else if (ret > 0 && !rollback) {
2323			uvc_ctrl_send_events(handle, entity,
2324					     ctrls->controls, ctrls->count);
2325		}
2326	}
2327
2328	mutex_unlock(&chain->ctrl_mutex);
2329	return ret_out;
2330}
2331
2332static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain,
2333					  struct uvc_control *ctrl,
2334					  struct uvc_control_mapping *mapping,
2335					  u32 which,
2336					  struct v4l2_ext_control *xctrl)
2337{
2338	u8 *data __free(kfree) = NULL;
2339	size_t size;
2340	u8 query;
2341	int ret;
2342	int id;
2343
2344	switch (which) {
2345	case V4L2_CTRL_WHICH_CUR_VAL:
2346		id = UVC_CTRL_DATA_CURRENT;
2347		query = UVC_GET_CUR;
2348		break;
2349	case V4L2_CTRL_WHICH_MIN_VAL:
2350		id = UVC_CTRL_DATA_MIN;
2351		query = UVC_GET_MIN;
2352		break;
2353	case V4L2_CTRL_WHICH_MAX_VAL:
2354		id = UVC_CTRL_DATA_MAX;
2355		query = UVC_GET_MAX;
2356		break;
2357	case V4L2_CTRL_WHICH_DEF_VAL:
2358		id = UVC_CTRL_DATA_DEF;
2359		query = UVC_GET_DEF;
2360		break;
2361	default:
2362		return -EINVAL;
2363	}
2364
2365	size = uvc_mapping_v4l2_size(mapping);
2366	if (xctrl->size < size) {
2367		xctrl->size = size;
2368		return -ENOSPC;
2369	}
2370
2371	data = kmalloc(size, GFP_KERNEL);
2372	if (!data)
2373		return -ENOMEM;
2374
2375	if (which == V4L2_CTRL_WHICH_CUR_VAL)
2376		ret = __uvc_ctrl_load_cur(chain, ctrl);
2377	else
2378		ret = uvc_ctrl_populate_cache(chain, ctrl);
2379
2380	if (ret < 0)
2381		return ret;
2382
2383	ret = mapping->get(mapping, query, uvc_ctrl_data(ctrl, id), size, data);
2384	if (ret < 0)
2385		return ret;
2386
2387	/*
2388	 * v4l2_ext_control does not have enough room to fit a compound control.
2389	 * Instead, the value is in the user memory at xctrl->ptr. The v4l2
2390	 * ioctl helper does not copy it for us.
2391	 */
2392	return copy_to_user(xctrl->ptr, data, size) ? -EFAULT : 0;
2393}
2394
2395static int uvc_mapping_get_xctrl_std(struct uvc_video_chain *chain,
2396				     struct uvc_control *ctrl,
2397				     struct uvc_control_mapping *mapping,
2398				     u32 which, struct v4l2_ext_control *xctrl)
2399{
2400	struct v4l2_query_ext_ctrl qec;
2401	int ret;
2402
2403	switch (which) {
2404	case V4L2_CTRL_WHICH_CUR_VAL:
2405		return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
2406	case V4L2_CTRL_WHICH_DEF_VAL:
2407	case V4L2_CTRL_WHICH_MIN_VAL:
2408	case V4L2_CTRL_WHICH_MAX_VAL:
2409		break;
2410	default:
2411		return -EINVAL;
2412	}
2413
2414	ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, &qec);
2415	if (ret < 0)
2416		return ret;
2417
2418	switch (which) {
2419	case V4L2_CTRL_WHICH_DEF_VAL:
2420		xctrl->value = qec.default_value;
2421		break;
2422	case V4L2_CTRL_WHICH_MIN_VAL:
2423		xctrl->value = qec.minimum;
2424		break;
2425	case V4L2_CTRL_WHICH_MAX_VAL:
2426		xctrl->value = qec.maximum;
2427		break;
2428	}
2429
2430	return 0;
2431}
2432
2433static int uvc_mapping_get_xctrl(struct uvc_video_chain *chain,
2434				 struct uvc_control *ctrl,
2435				 struct uvc_control_mapping *mapping,
2436				 u32 which, struct v4l2_ext_control *xctrl)
2437{
2438	if (uvc_ctrl_mapping_is_compound(mapping))
2439		return uvc_mapping_get_xctrl_compound(chain, ctrl, mapping,
2440						      which, xctrl);
2441	return uvc_mapping_get_xctrl_std(chain, ctrl, mapping, which, xctrl);
2442}
2443
2444int uvc_ctrl_get(struct uvc_video_chain *chain, u32 which,
2445		 struct v4l2_ext_control *xctrl)
2446{
2447	struct uvc_control *ctrl;
2448	struct uvc_control_mapping *mapping;
2449
2450	if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
2451		return -EACCES;
2452
2453	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
2454	if (!ctrl)
2455		return -EINVAL;
2456
2457	return uvc_mapping_get_xctrl(chain, ctrl, mapping, which, xctrl);
2458}
2459
2460static int uvc_ctrl_clamp(struct uvc_video_chain *chain,
2461			  struct uvc_control *ctrl,
2462			  struct uvc_control_mapping *mapping,
2463			  s32 *value_in_out)
2464{
2465	s32 value = *value_in_out;
2466	u32 step;
2467	s32 min;
2468	s32 max;
2469	int ret;
2470
2471	switch (mapping->v4l2_type) {
2472	case V4L2_CTRL_TYPE_INTEGER:
2473		if (!ctrl->cached) {
2474			ret = uvc_ctrl_populate_cache(chain, ctrl);
2475			if (ret < 0)
2476				return ret;
2477		}
2478
2479		min = uvc_mapping_get_s32(mapping, UVC_GET_MIN,
2480					  uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
2481		max = uvc_mapping_get_s32(mapping, UVC_GET_MAX,
2482					  uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
2483		step = uvc_mapping_get_s32(mapping, UVC_GET_RES,
2484					   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
2485		if (step == 0)
2486			step = 1;
2487
2488		value = min + DIV_ROUND_CLOSEST((u32)(value - min), step) * step;
2489		if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
2490			value = clamp(value, min, max);
2491		else
2492			value = clamp_t(u32, value, min, max);
2493		*value_in_out = value;
2494		return 0;
2495
2496	case V4L2_CTRL_TYPE_BITMASK:
2497		if (!ctrl->cached) {
2498			ret = uvc_ctrl_populate_cache(chain, ctrl);
2499			if (ret < 0)
2500				return ret;
2501		}
2502
2503		value &= uvc_get_ctrl_bitmap(ctrl, mapping);
2504		*value_in_out = value;
2505		return 0;
2506
2507	case V4L2_CTRL_TYPE_BOOLEAN:
2508		*value_in_out = clamp(value, 0, 1);
2509		return 0;
2510
2511	case V4L2_CTRL_TYPE_MENU:
2512		if (value < (ffs(mapping->menu_mask) - 1) ||
2513		    value > (fls(mapping->menu_mask) - 1))
2514			return -ERANGE;
2515
2516		if (!test_bit(value, &mapping->menu_mask))
2517			return -EINVAL;
2518
2519		/*
2520		 * Valid menu indices are reported by the GET_RES request for
2521		 * UVC controls that support it.
2522		 */
2523		if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
2524			int val = uvc_mapping_get_menu_value(mapping, value);
2525			if (!ctrl->cached) {
2526				ret = uvc_ctrl_populate_cache(chain, ctrl);
2527				if (ret < 0)
2528					return ret;
2529			}
2530
2531			if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & val))
2532				return -EINVAL;
2533		}
2534		return 0;
2535
2536	default:
2537		return 0;
2538	}
2539
2540	return 0;
2541}
2542
2543static int uvc_mapping_set_xctrl_compound(struct uvc_control *ctrl,
2544					  struct uvc_control_mapping *mapping,
2545					  struct v4l2_ext_control *xctrl)
2546{
2547	u8 *data __free(kfree) = NULL;
2548	size_t size = uvc_mapping_v4l2_size(mapping);
2549
2550	if (xctrl->size != size)
2551		return -EINVAL;
2552
2553	/*
2554	 * v4l2_ext_control does not have enough room to fit a compound control.
2555	 * Instead, the value is in the user memory at xctrl->ptr. The v4l2
2556	 * ioctl helper does not copy it for us.
2557	 */
2558	data = memdup_user(xctrl->ptr, size);
2559	if (IS_ERR(data))
2560		return PTR_ERR(data);
2561
2562	return mapping->set(mapping, size, data,
2563			    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2564}
2565
2566static int uvc_mapping_set_xctrl(struct uvc_control *ctrl,
2567				 struct uvc_control_mapping *mapping,
2568				 struct v4l2_ext_control *xctrl)
2569{
2570	if (uvc_ctrl_mapping_is_compound(mapping))
2571		return uvc_mapping_set_xctrl_compound(ctrl, mapping, xctrl);
2572
2573	uvc_mapping_set_s32(mapping, xctrl->value,
2574			    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
2575	return 0;
2576}
2577
2578int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)
2579{
2580	struct uvc_video_chain *chain = handle->chain;
2581	struct uvc_control_mapping *mapping;
2582	struct uvc_control *ctrl;
2583	int ret;
2584
2585	lockdep_assert_held(&chain->ctrl_mutex);
2586
2587	if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
2588		return -EACCES;
2589
2590	ctrl = uvc_find_control(chain, xctrl->id, &mapping);
2591	if (!ctrl)
2592		return -EINVAL;
2593	if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
2594		return -EACCES;
2595
2596	ret = uvc_ctrl_clamp(chain, ctrl, mapping, &xctrl->value);
2597	if (ret)
2598		return ret;
2599	/*
2600	 * If the mapping doesn't span the whole UVC control, the current value
2601	 * needs to be loaded from the device to perform the read-modify-write
2602	 * operation.
2603	 */
2604	if ((ctrl->info.size * 8) != mapping->size) {
2605		ret = __uvc_ctrl_load_cur(chain, ctrl);
2606		if (ret < 0)
2607			return ret;
2608	}
2609
2610	/* Backup the current value in case we need to rollback later. */
2611	if (!ctrl->dirty) {
2612		memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
2613		       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
2614		       ctrl->info.size);
2615	}
2616
2617	ret = uvc_mapping_set_xctrl(ctrl, mapping, xctrl);
2618	if (ret)
2619		return ret;
2620
2621	ctrl->dirty = 1;
2622	ctrl->modified = 1;
2623	return 0;
2624}
2625
2626/* --------------------------------------------------------------------------
2627 * Dynamic controls
2628 */
2629
2630/*
2631 * Retrieve flags for a given control
2632 */
2633static int uvc_ctrl_get_flags(struct uvc_device *dev,
2634			      const struct uvc_control *ctrl,
2635			      struct uvc_control_info *info)
2636{
2637	u8 *data;
2638	int ret;
2639
2640	data = kmalloc(1, GFP_KERNEL);
2641	if (data == NULL)
2642		return -ENOMEM;
2643
2644	if (ctrl->entity->get_info)
2645		ret = ctrl->entity->get_info(dev, ctrl->entity,
2646					     ctrl->info.selector, data);
2647	else
2648		ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
2649				     dev->intfnum, info->selector, data, 1);
2650
2651	if (!ret) {
2652		info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
2653				 UVC_CTRL_FLAG_SET_CUR |
2654				 UVC_CTRL_FLAG_AUTO_UPDATE |
2655				 UVC_CTRL_FLAG_ASYNCHRONOUS);
2656
2657		info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
2658				UVC_CTRL_FLAG_GET_CUR : 0)
2659			    |  (data[0] & UVC_CONTROL_CAP_SET ?
2660				UVC_CTRL_FLAG_SET_CUR : 0)
2661			    |  (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
2662				UVC_CTRL_FLAG_AUTO_UPDATE : 0)
2663			    |  (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
2664				UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
2665	}
2666
2667	kfree(data);
2668	return ret;
2669}
2670
2671static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
2672	const struct uvc_control *ctrl, struct uvc_control_info *info)
2673{
2674	struct uvc_ctrl_fixup {
2675		struct usb_device_id id;
2676		u8 entity;
2677		u8 selector;
2678		u8 flags;
2679	};
2680
2681	static const struct uvc_ctrl_fixup fixups[] = {
2682		{ { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
2683			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2684			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2685			UVC_CTRL_FLAG_AUTO_UPDATE },
2686		{ { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
2687			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2688			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2689			UVC_CTRL_FLAG_AUTO_UPDATE },
2690		{ { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
2691			UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
2692			UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
2693			UVC_CTRL_FLAG_AUTO_UPDATE },
2694	};
2695
2696	unsigned int i;
2697
2698	for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
2699		if (!usb_match_one_id(dev->intf, &fixups[i].id))
2700			continue;
2701
2702		if (fixups[i].entity == ctrl->entity->id &&
2703		    fixups[i].selector == info->selector) {
2704			info->flags = fixups[i].flags;
2705			return;
2706		}
2707	}
2708}
2709
2710/*
2711 * Query control information (size and flags) for XU controls.
2712 */
2713static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
2714	const struct uvc_control *ctrl, struct uvc_control_info *info)
2715{
2716	u8 *data;
2717	int ret;
2718
2719	data = kmalloc(2, GFP_KERNEL);
2720	if (data == NULL)
2721		return -ENOMEM;
2722
2723	memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
2724	info->index = ctrl->index;
2725	info->selector = ctrl->index + 1;
2726
2727	/* Query and verify the control length (GET_LEN) */
2728	ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
2729			     info->selector, data, 2);
2730	if (ret < 0) {
2731		uvc_dbg(dev, CONTROL,
2732			"GET_LEN failed on control %pUl/%u (%d)\n",
2733			info->entity, info->selector, ret);
2734		goto done;
2735	}
2736
2737	info->size = le16_to_cpup((__le16 *)data);
2738
2739	info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
2740		    | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
2741
2742	ret = uvc_ctrl_get_flags(dev, ctrl, info);
2743	if (ret < 0) {
2744		uvc_dbg(dev, CONTROL,
2745			"Failed to get flags for control %pUl/%u (%d)\n",
2746			info->entity, info->selector, ret);
2747		goto done;
2748	}
2749
2750	uvc_ctrl_fixup_xu_info(dev, ctrl, info);
2751
2752	uvc_dbg(dev, CONTROL,
2753		"XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
2754		info->entity, info->selector, info->size,
2755		(info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
2756		(info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
2757		(info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
2758
2759done:
2760	kfree(data);
2761	return ret;
2762}
2763
2764static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2765	const struct uvc_control_info *info);
2766
2767static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
2768	struct uvc_control *ctrl)
2769{
2770	struct uvc_control_info info;
2771	int ret;
2772
2773	if (ctrl->initialized)
2774		return 0;
2775
2776	ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
2777	if (ret < 0)
2778		return ret;
2779
2780	ret = uvc_ctrl_add_info(dev, ctrl, &info);
2781	if (ret < 0)
2782		uvc_dbg(dev, CONTROL,
2783			"Failed to initialize control %pUl/%u on device %s entity %u\n",
2784			info.entity, info.selector, dev->udev->devpath,
2785			ctrl->entity->id);
2786
2787	return ret;
2788}
2789
2790int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
2791	struct uvc_xu_control_query *xqry)
2792{
2793	struct uvc_entity *entity, *iter;
2794	struct uvc_control *ctrl;
2795	unsigned int i;
2796	bool found;
2797	u32 reqflags;
2798	u16 size;
2799	u8 *data = NULL;
2800	int ret;
2801
2802	/* Find the extension unit. */
2803	entity = NULL;
2804	list_for_each_entry(iter, &chain->entities, chain) {
2805		if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
2806		    iter->id == xqry->unit) {
2807			entity = iter;
2808			break;
2809		}
2810	}
2811
2812	if (!entity) {
2813		uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
2814			xqry->unit);
2815		return -ENOENT;
2816	}
2817
2818	/* Find the control and perform delayed initialization if needed. */
2819	found = false;
2820	for (i = 0; i < entity->ncontrols; ++i) {
2821		ctrl = &entity->controls[i];
2822		if (ctrl->index == xqry->selector - 1) {
2823			found = true;
2824			break;
2825		}
2826	}
2827
2828	if (!found) {
2829		uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
2830			entity->guid, xqry->selector);
2831		return -ENOENT;
2832	}
2833
2834	if (mutex_lock_interruptible(&chain->ctrl_mutex))
2835		return -ERESTARTSYS;
2836
2837	ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
2838	if (ret < 0) {
2839		ret = -ENOENT;
2840		goto done;
2841	}
2842
2843	/* Validate the required buffer size and flags for the request */
2844	reqflags = 0;
2845	size = ctrl->info.size;
2846
2847	switch (xqry->query) {
2848	case UVC_GET_CUR:
2849		reqflags = UVC_CTRL_FLAG_GET_CUR;
2850		break;
2851	case UVC_GET_MIN:
2852		reqflags = UVC_CTRL_FLAG_GET_MIN;
2853		break;
2854	case UVC_GET_MAX:
2855		reqflags = UVC_CTRL_FLAG_GET_MAX;
2856		break;
2857	case UVC_GET_DEF:
2858		reqflags = UVC_CTRL_FLAG_GET_DEF;
2859		break;
2860	case UVC_GET_RES:
2861		reqflags = UVC_CTRL_FLAG_GET_RES;
2862		break;
2863	case UVC_SET_CUR:
2864		reqflags = UVC_CTRL_FLAG_SET_CUR;
2865		break;
2866	case UVC_GET_LEN:
2867		size = 2;
2868		break;
2869	case UVC_GET_INFO:
2870		size = 1;
2871		break;
2872	default:
2873		ret = -EINVAL;
2874		goto done;
2875	}
2876
2877	if (size != xqry->size) {
2878		ret = -ENOBUFS;
2879		goto done;
2880	}
2881
2882	if (reqflags && !(ctrl->info.flags & reqflags)) {
2883		ret = -EBADRQC;
2884		goto done;
2885	}
2886
2887	data = kmalloc(size, GFP_KERNEL);
2888	if (data == NULL) {
2889		ret = -ENOMEM;
2890		goto done;
2891	}
2892
2893	if (xqry->query == UVC_SET_CUR &&
2894	    copy_from_user(data, xqry->data, size)) {
2895		ret = -EFAULT;
2896		goto done;
2897	}
2898
2899	ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
2900			     chain->dev->intfnum, xqry->selector, data, size);
2901	if (ret < 0)
2902		goto done;
2903
2904	if (xqry->query != UVC_SET_CUR &&
2905	    copy_to_user(xqry->data, data, size))
2906		ret = -EFAULT;
2907done:
2908	kfree(data);
2909	mutex_unlock(&chain->ctrl_mutex);
2910	return ret;
2911}
2912
2913/* --------------------------------------------------------------------------
2914 * Suspend/resume
2915 */
2916
2917/*
2918 * Restore control values after resume, skipping controls that haven't been
2919 * changed.
2920 *
2921 * TODO
2922 * - Don't restore modified controls that are back to their default value.
2923 * - Handle restore order (Auto-Exposure Mode should be restored before
2924 *   Exposure Time).
2925 */
2926int uvc_ctrl_restore_values(struct uvc_device *dev)
2927{
2928	struct uvc_control *ctrl;
2929	struct uvc_entity *entity;
2930	unsigned int i;
2931	int ret;
2932
2933	/* Walk the entities list and restore controls when possible. */
2934	list_for_each_entry(entity, &dev->entities, list) {
2935
2936		for (i = 0; i < entity->ncontrols; ++i) {
2937			ctrl = &entity->controls[i];
2938
2939			if (!ctrl->initialized || !ctrl->modified ||
2940			    (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2941				continue;
2942			dev_dbg(&dev->intf->dev,
2943				"restoring control %pUl/%u/%u\n",
2944				ctrl->info.entity, ctrl->info.index,
2945				ctrl->info.selector);
2946			ctrl->dirty = 1;
2947		}
2948
2949		ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
2950		if (ret < 0)
2951			return ret;
2952	}
2953
2954	return 0;
2955}
2956
2957/* --------------------------------------------------------------------------
2958 * Control and mapping handling
2959 */
2960
2961/*
2962 * Add control information to a given control.
2963 */
2964static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2965	const struct uvc_control_info *info)
2966{
2967	ctrl->info = *info;
2968	INIT_LIST_HEAD(&ctrl->info.mappings);
2969
2970	/* Allocate an array to save control values (cur, def, max, etc.) */
2971	ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2972				 GFP_KERNEL);
2973	if (!ctrl->uvc_data)
2974		return -ENOMEM;
2975
2976	ctrl->initialized = 1;
2977
2978	uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
2979		ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
2980		ctrl->entity->id);
2981
2982	return 0;
2983}
2984
2985/*
2986 * Add a control mapping to a given control.
2987 */
2988static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2989	struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2990{
2991	struct uvc_control_mapping *map;
2992	unsigned int size;
2993	unsigned int i;
2994	int ret;
2995
2996	/*
2997	 * Most mappings come from static kernel data, and need to be duplicated.
2998	 * Mappings that come from userspace will be unnecessarily duplicated,
2999	 * this could be optimized.
3000	 */
3001	map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
3002	if (!map)
3003		return -ENOMEM;
3004
3005	map->name = NULL;
3006	map->menu_names = NULL;
3007	map->menu_mapping = NULL;
3008
3009	/* For UVCIOC_CTRL_MAP custom control */
3010	if (mapping->name) {
3011		map->name = kstrdup(mapping->name, GFP_KERNEL);
3012		if (!map->name)
3013			goto err_nomem;
3014	}
3015
3016	INIT_LIST_HEAD(&map->ev_subs);
3017
3018	if (mapping->menu_mapping && mapping->menu_mask) {
3019		size = sizeof(mapping->menu_mapping[0])
3020		       * fls(mapping->menu_mask);
3021		map->menu_mapping = kmemdup(mapping->menu_mapping, size,
3022					    GFP_KERNEL);
3023		if (!map->menu_mapping)
3024			goto err_nomem;
3025	}
3026	if (mapping->menu_names && mapping->menu_mask) {
3027		size = sizeof(mapping->menu_names[0])
3028		       * fls(mapping->menu_mask);
3029		map->menu_names = kmemdup(mapping->menu_names, size,
3030					  GFP_KERNEL);
3031		if (!map->menu_names)
3032			goto err_nomem;
3033	}
3034
3035	if (uvc_ctrl_mapping_is_compound(map))
3036		if (WARN_ON(!map->set || !map->get)) {
3037			ret = -EIO;
3038			goto free_mem;
3039		}
3040
3041	if (map->get == NULL)
3042		map->get = uvc_get_le_value;
3043	if (map->set == NULL)
3044		map->set = uvc_set_le_value;
3045
3046	for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
3047		if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
3048						V4L2_CTRL_ID2WHICH(map->id)) {
3049			chain->ctrl_class_bitmap |= BIT(i);
3050			break;
3051		}
3052	}
3053
3054	list_add_tail(&map->list, &ctrl->info.mappings);
3055	uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
3056		uvc_map_get_name(map), ctrl->info.entity,
3057		ctrl->info.selector);
3058
3059	return 0;
3060
3061err_nomem:
3062	ret = -ENOMEM;
3063free_mem:
3064	kfree(map->menu_names);
3065	kfree(map->menu_mapping);
3066	kfree(map->name);
3067	kfree(map);
3068	return ret;
3069}
3070
3071int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
3072	const struct uvc_control_mapping *mapping)
3073{
3074	struct uvc_device *dev = chain->dev;
3075	struct uvc_control_mapping *map;
3076	struct uvc_entity *entity;
3077	struct uvc_control *ctrl;
3078	int found = 0;
3079	int ret;
3080
3081	if (mapping->id & ~V4L2_CTRL_ID_MASK) {
3082		uvc_dbg(dev, CONTROL,
3083			"Can't add mapping '%s', control id 0x%08x is invalid\n",
3084			uvc_map_get_name(mapping), mapping->id);
3085		return -EINVAL;
3086	}
3087
3088	/* Search for the matching (GUID/CS) control on the current chain */
3089	list_for_each_entry(entity, &chain->entities, chain) {
3090		unsigned int i;
3091
3092		if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
3093		    !uvc_entity_match_guid(entity, mapping->entity))
3094			continue;
3095
3096		for (i = 0; i < entity->ncontrols; ++i) {
3097			ctrl = &entity->controls[i];
3098			if (ctrl->index == mapping->selector - 1) {
3099				found = 1;
3100				break;
3101			}
3102		}
3103
3104		if (found)
3105			break;
3106	}
3107	if (!found)
3108		return -ENOENT;
3109
3110	if (mutex_lock_interruptible(&chain->ctrl_mutex))
3111		return -ERESTARTSYS;
3112
3113	/* Perform delayed initialization of XU controls */
3114	ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
3115	if (ret < 0) {
3116		ret = -ENOENT;
3117		goto done;
3118	}
3119
3120	/* Validate the user-provided bit-size and offset */
3121	if (mapping->size > 32 ||
3122	    mapping->offset + mapping->size > ctrl->info.size * 8) {
3123		ret = -EINVAL;
3124		goto done;
3125	}
3126
3127	list_for_each_entry(map, &ctrl->info.mappings, list) {
3128		if (mapping->id == map->id) {
3129			uvc_dbg(dev, CONTROL,
3130				"Can't add mapping '%s', control id 0x%08x already exists\n",
3131				uvc_map_get_name(mapping), mapping->id);
3132			ret = -EEXIST;
3133			goto done;
3134		}
3135	}
3136
3137	/* Prevent excess memory consumption */
3138	if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
3139		atomic_dec(&dev->nmappings);
3140		uvc_dbg(dev, CONTROL,
3141			"Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
3142			uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
3143		ret = -ENOMEM;
3144		goto done;
3145	}
3146
3147	ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
3148	if (ret < 0)
3149		atomic_dec(&dev->nmappings);
3150
3151done:
3152	mutex_unlock(&chain->ctrl_mutex);
3153	return ret;
3154}
3155
3156/*
3157 * Prune an entity of its bogus controls using a blacklist. Bogus controls
3158 * are currently the ones that crash the camera or unconditionally return an
3159 * error when queried.
3160 */
3161static void uvc_ctrl_prune_entity(struct uvc_device *dev,
3162	struct uvc_entity *entity)
3163{
3164	struct uvc_ctrl_blacklist {
3165		struct usb_device_id id;
3166		u8 index;
3167	};
3168
3169	static const struct uvc_ctrl_blacklist processing_blacklist[] = {
3170		{ { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
3171		{ { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
3172		{ { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
3173	};
3174	static const struct uvc_ctrl_blacklist camera_blacklist[] = {
3175		{ { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
3176	};
3177
3178	const struct uvc_ctrl_blacklist *blacklist;
3179	unsigned int size;
3180	unsigned int count;
3181	unsigned int i;
3182	u8 *controls;
3183
3184	switch (UVC_ENTITY_TYPE(entity)) {
3185	case UVC_VC_PROCESSING_UNIT:
3186		blacklist = processing_blacklist;
3187		count = ARRAY_SIZE(processing_blacklist);
3188		controls = entity->processing.bmControls;
3189		size = entity->processing.bControlSize;
3190		break;
3191
3192	case UVC_ITT_CAMERA:
3193		blacklist = camera_blacklist;
3194		count = ARRAY_SIZE(camera_blacklist);
3195		controls = entity->camera.bmControls;
3196		size = entity->camera.bControlSize;
3197		break;
3198
3199	default:
3200		return;
3201	}
3202
3203	for (i = 0; i < count; ++i) {
3204		if (!usb_match_one_id(dev->intf, &blacklist[i].id))
3205			continue;
3206
3207		if (blacklist[i].index >= 8 * size ||
3208		    !uvc_test_bit(controls, blacklist[i].index))
3209			continue;
3210
3211		uvc_dbg(dev, CONTROL,
3212			"%u/%u control is black listed, removing it\n",
3213			entity->id, blacklist[i].index);
3214
3215		uvc_clear_bit(controls, blacklist[i].index);
3216	}
3217}
3218
3219/*
3220 * Add control information and hardcoded stock control mappings to the given
3221 * device.
3222 */
3223static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
3224			       struct uvc_control *ctrl)
3225{
3226	unsigned int i;
3227
3228	for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
3229		const struct uvc_control_info *info = &uvc_ctrls[i];
3230
3231		if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
3232		    ctrl->index == info->index) {
3233			uvc_ctrl_add_info(chain->dev, ctrl, info);
3234			/*
3235			 * Retrieve control flags from the device. Ignore errors
3236			 * and work with default flag values from the uvc_ctrl
3237			 * array when the device doesn't properly implement
3238			 * GET_INFO on standard controls.
3239			 */
3240			uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
3241			break;
3242		 }
3243	}
3244
3245	if (!ctrl->initialized)
3246		return;
3247
3248	/* Process common mappings. */
3249	for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
3250		const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
3251
3252		if (!uvc_entity_match_guid(ctrl->entity, mapping->entity) ||
3253		    ctrl->info.selector != mapping->selector)
3254			continue;
3255
3256		/* Let the device provide a custom mapping. */
3257		if (mapping->filter_mapping) {
3258			mapping = mapping->filter_mapping(chain, ctrl);
3259			if (!mapping)
3260				continue;
3261		}
3262
3263		__uvc_ctrl_add_mapping(chain, ctrl, mapping);
3264	}
3265}
3266
3267/*
3268 * Initialize device controls.
3269 */
3270static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
3271{
3272	struct uvc_entity *entity;
3273	unsigned int i;
3274
3275	/* Walk the entities list and instantiate controls */
3276	list_for_each_entry(entity, &chain->entities, chain) {
3277		struct uvc_control *ctrl;
3278		unsigned int bControlSize = 0, ncontrols;
3279		u8 *bmControls = NULL;
3280
3281		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
3282			bmControls = entity->extension.bmControls;
3283			bControlSize = entity->extension.bControlSize;
3284		} else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
3285			bmControls = entity->processing.bmControls;
3286			bControlSize = entity->processing.bControlSize;
3287		} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
3288			bmControls = entity->camera.bmControls;
3289			bControlSize = entity->camera.bControlSize;
3290		} else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
3291			bmControls = entity->gpio.bmControls;
3292			bControlSize = entity->gpio.bControlSize;
3293		}
3294
3295		/* Remove bogus/blacklisted controls */
3296		uvc_ctrl_prune_entity(chain->dev, entity);
3297
3298		/* Count supported controls and allocate the controls array */
3299		ncontrols = memweight(bmControls, bControlSize);
3300		if (ncontrols == 0)
3301			continue;
3302
3303		entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
3304					   GFP_KERNEL);
3305		if (entity->controls == NULL)
3306			return -ENOMEM;
3307		entity->ncontrols = ncontrols;
3308
3309		/* Initialize all supported controls */
3310		ctrl = entity->controls;
3311		for (i = 0; i < bControlSize * 8; ++i) {
3312			if (uvc_test_bit(bmControls, i) == 0)
3313				continue;
3314
3315			ctrl->entity = entity;
3316			ctrl->index = i;
3317
3318			uvc_ctrl_init_ctrl(chain, ctrl);
3319			ctrl++;
3320		}
3321	}
3322
3323	return 0;
3324}
3325
3326int uvc_ctrl_init_device(struct uvc_device *dev)
3327{
3328	struct uvc_video_chain *chain;
3329	int ret;
3330
3331	INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
3332
3333	list_for_each_entry(chain, &dev->chains, list) {
3334		ret = uvc_ctrl_init_chain(chain);
3335		if (ret)
3336			return ret;
3337	}
3338
3339	return 0;
3340}
3341
3342void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
3343{
3344	struct uvc_entity *entity;
3345
3346	guard(mutex)(&handle->chain->ctrl_mutex);
3347
3348	if (!handle->pending_async_ctrls)
3349		return;
3350
3351	list_for_each_entry(entity, &handle->chain->dev->entities, list) {
3352		for (unsigned int i = 0; i < entity->ncontrols; ++i) {
3353			if (entity->controls[i].handle != handle)
3354				continue;
3355			uvc_ctrl_clear_handle(&entity->controls[i]);
3356		}
3357	}
3358
3359	if (!WARN_ON(handle->pending_async_ctrls))
3360		return;
3361
3362	for (unsigned int i = 0; i < handle->pending_async_ctrls; i++)
3363		uvc_pm_put(handle->stream->dev);
3364}
3365
3366/*
3367 * Cleanup device controls.
3368 */
3369static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
3370	struct uvc_control *ctrl)
3371{
3372	struct uvc_control_mapping *mapping, *nm;
3373
3374	list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
3375		list_del(&mapping->list);
3376		kfree(mapping->menu_names);
3377		kfree(mapping->menu_mapping);
3378		kfree(mapping->name);
3379		kfree(mapping);
3380	}
3381}
3382
3383void uvc_ctrl_cleanup_device(struct uvc_device *dev)
3384{
3385	struct uvc_entity *entity;
3386	unsigned int i;
3387
3388	/* Can be uninitialized if we are aborting on probe error. */
3389	if (dev->async_ctrl.work.func)
3390		cancel_work_sync(&dev->async_ctrl.work);
3391
3392	/* Free controls and control mappings for all entities. */
3393	list_for_each_entry(entity, &dev->entities, list) {
3394		for (i = 0; i < entity->ncontrols; ++i) {
3395			struct uvc_control *ctrl = &entity->controls[i];
3396
3397			if (!ctrl->initialized)
3398				continue;
3399
3400			uvc_ctrl_cleanup_mappings(dev, ctrl);
3401			kfree(ctrl->uvc_data);
3402		}
3403
3404		kfree(entity->controls);
3405	}
3406}
~/ Read-only file preview.