v4l2-ctrls.c 118.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
    V4L2 controls framework implementation.

    Copyright (C) 2010  Hans Verkuil <hverkuil@xs4all.nl>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/ctype.h>
22
#include <linux/mm.h>
23
#include <linux/slab.h>
24
#include <linux/export.h>
25 26 27
#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
28
#include <media/v4l2-event.h>
29 30
#include <media/v4l2-dev.h>

31 32
#define has_op(master, op) \
	(master->ops && master->ops->op)
33
#define call_op(master, op) \
34
	(has_op(master, op) ? master->ops->op(master) : 0)
35

36
/* Internal temporary helper struct, one for each v4l2_ext_control */
37 38 39
struct v4l2_ctrl_helper {
	/* Pointer to the control reference of the master control */
	struct v4l2_ctrl_ref *mref;
40 41
	/* The control ref corresponding to the v4l2_ext_control ID field. */
	struct v4l2_ctrl_ref *ref;
42 43 44
	/* v4l2_ext_control index of the next control belonging to the
	   same cluster, or 0 if there isn't any. */
	u32 next;
45 46
};

47
/* Small helper function to determine if the autocluster is set to manual
48
   mode. */
49 50 51 52 53 54 55 56 57 58 59 60
static bool is_cur_manual(const struct v4l2_ctrl *master)
{
	return master->is_auto && master->cur.val == master->manual_mode_value;
}

/* Same as above, but this checks the against the new value instead of the
   current value. */
static bool is_new_manual(const struct v4l2_ctrl *master)
{
	return master->is_auto && master->val == master->manual_mode_value;
}

61 62 63 64
/* Returns NULL or a character pointer array containing the menu for
   the given control ID. The pointer array ends with a NULL pointer.
   An empty string signifies a menu entry that is invalid. This allows
   drivers to disable certain options if it is not supported. */
65
const char * const *v4l2_ctrl_get_menu(u32 id)
66
{
67
	static const char * const mpeg_audio_sampling_freq[] = {
68 69 70 71 72
		"44.1 kHz",
		"48 kHz",
		"32 kHz",
		NULL
	};
73
	static const char * const mpeg_audio_encoding[] = {
74 75 76 77 78 79 80
		"MPEG-1/2 Layer I",
		"MPEG-1/2 Layer II",
		"MPEG-1/2 Layer III",
		"MPEG-2/4 AAC",
		"AC-3",
		NULL
	};
81
	static const char * const mpeg_audio_l1_bitrate[] = {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		"32 kbps",
		"64 kbps",
		"96 kbps",
		"128 kbps",
		"160 kbps",
		"192 kbps",
		"224 kbps",
		"256 kbps",
		"288 kbps",
		"320 kbps",
		"352 kbps",
		"384 kbps",
		"416 kbps",
		"448 kbps",
		NULL
	};
98
	static const char * const mpeg_audio_l2_bitrate[] = {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		"32 kbps",
		"48 kbps",
		"56 kbps",
		"64 kbps",
		"80 kbps",
		"96 kbps",
		"112 kbps",
		"128 kbps",
		"160 kbps",
		"192 kbps",
		"224 kbps",
		"256 kbps",
		"320 kbps",
		"384 kbps",
		NULL
	};
115
	static const char * const mpeg_audio_l3_bitrate[] = {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		"32 kbps",
		"40 kbps",
		"48 kbps",
		"56 kbps",
		"64 kbps",
		"80 kbps",
		"96 kbps",
		"112 kbps",
		"128 kbps",
		"160 kbps",
		"192 kbps",
		"224 kbps",
		"256 kbps",
		"320 kbps",
		NULL
	};
132
	static const char * const mpeg_audio_ac3_bitrate[] = {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		"32 kbps",
		"40 kbps",
		"48 kbps",
		"56 kbps",
		"64 kbps",
		"80 kbps",
		"96 kbps",
		"112 kbps",
		"128 kbps",
		"160 kbps",
		"192 kbps",
		"224 kbps",
		"256 kbps",
		"320 kbps",
		"384 kbps",
		"448 kbps",
		"512 kbps",
		"576 kbps",
		"640 kbps",
		NULL
	};
154
	static const char * const mpeg_audio_mode[] = {
155 156 157 158 159 160
		"Stereo",
		"Joint Stereo",
		"Dual",
		"Mono",
		NULL
	};
161
	static const char * const mpeg_audio_mode_extension[] = {
162 163 164 165 166 167
		"Bound 4",
		"Bound 8",
		"Bound 12",
		"Bound 16",
		NULL
	};
168
	static const char * const mpeg_audio_emphasis[] = {
169 170 171 172 173
		"No Emphasis",
		"50/15 us",
		"CCITT J17",
		NULL
	};
174
	static const char * const mpeg_audio_crc[] = {
175 176 177 178
		"No CRC",
		"16-bit CRC",
		NULL
	};
179 180 181 182 183 184 185 186 187
	static const char * const mpeg_audio_dec_playback[] = {
		"Auto",
		"Stereo",
		"Left",
		"Right",
		"Mono",
		"Swapped Stereo",
		NULL
	};
188
	static const char * const mpeg_video_encoding[] = {
189 190 191 192 193
		"MPEG-1",
		"MPEG-2",
		"MPEG-4 AVC",
		NULL
	};
194
	static const char * const mpeg_video_aspect[] = {
195 196 197 198 199 200
		"1x1",
		"4x3",
		"16x9",
		"2.21x1",
		NULL
	};
201
	static const char * const mpeg_video_bitrate_mode[] = {
202 203 204 205
		"Variable Bitrate",
		"Constant Bitrate",
		NULL
	};
206
	static const char * const mpeg_stream_type[] = {
207 208 209 210 211 212 213 214
		"MPEG-2 Program Stream",
		"MPEG-2 Transport Stream",
		"MPEG-1 System Stream",
		"MPEG-2 DVD-compatible Stream",
		"MPEG-1 VCD-compatible Stream",
		"MPEG-2 SVCD-compatible Stream",
		NULL
	};
215
	static const char * const mpeg_stream_vbi_fmt[] = {
216
		"No VBI",
217
		"Private Packet, IVTV Format",
218 219
		NULL
	};
220
	static const char * const camera_power_line_frequency[] = {
221 222 223
		"Disabled",
		"50 Hz",
		"60 Hz",
224
		"Auto",
225 226
		NULL
	};
227
	static const char * const camera_exposure_auto[] = {
228 229 230 231 232 233
		"Auto Mode",
		"Manual Mode",
		"Shutter Priority Mode",
		"Aperture Priority Mode",
		NULL
	};
234 235 236 237
	static const char * const camera_exposure_metering[] = {
		"Average",
		"Center Weighted",
		"Spot",
238
		"Matrix",
239 240
		NULL
	};
241 242 243 244 245 246 247
	static const char * const camera_auto_focus_range[] = {
		"Auto",
		"Normal",
		"Macro",
		"Infinity",
		NULL
	};
248
	static const char * const colorfx[] = {
249 250 251 252 253 254
		"None",
		"Black & White",
		"Sepia",
		"Negative",
		"Emboss",
		"Sketch",
255 256 257
		"Sky Blue",
		"Grass Green",
		"Skin Whiten",
258
		"Vivid",
259 260 261 262 263 264
		"Aqua",
		"Art Freeze",
		"Silhouette",
		"Solarization",
		"Antique",
		"Set Cb/Cr",
265 266
		NULL
	};
267 268 269 270 271 272 273 274 275 276 277 278 279
	static const char * const auto_n_preset_white_balance[] = {
		"Manual",
		"Auto",
		"Incandescent",
		"Fluorescent",
		"Fluorescent H",
		"Horizon",
		"Daylight",
		"Flash",
		"Cloudy",
		"Shade",
		NULL,
	};
280 281 282 283 284
	static const char * const camera_iso_sensitivity_auto[] = {
		"Manual",
		"Auto",
		NULL
	};
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	static const char * const scene_mode[] = {
		"None",
		"Backlight",
		"Beach/Snow",
		"Candle Light",
		"Dusk/Dawn",
		"Fall Colors",
		"Fireworks",
		"Landscape",
		"Night",
		"Party/Indoor",
		"Portrait",
		"Sports",
		"Sunset",
		"Text",
		NULL
	};
302 303
	static const char * const tune_emphasis[] = {
		"None",
304 305
		"50 Microseconds",
		"75 Microseconds",
306 307
		NULL,
	};
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	static const char * const header_mode[] = {
		"Separate Buffer",
		"Joined With 1st Frame",
		NULL,
	};
	static const char * const multi_slice[] = {
		"Single",
		"Max Macroblocks",
		"Max Bytes",
		NULL,
	};
	static const char * const entropy_mode[] = {
		"CAVLC",
		"CABAC",
		NULL,
	};
	static const char * const mpeg_h264_level[] = {
		"1",
		"1b",
		"1.1",
		"1.2",
		"1.3",
		"2",
		"2.1",
		"2.2",
		"3",
		"3.1",
		"3.2",
		"4",
		"4.1",
		"4.2",
		"5",
		"5.1",
		NULL,
	};
	static const char * const h264_loop_filter[] = {
		"Enabled",
		"Disabled",
		"Disabled at Slice Boundary",
		NULL,
	};
	static const char * const h264_profile[] = {
		"Baseline",
		"Constrained Baseline",
		"Main",
		"Extended",
		"High",
		"High 10",
		"High 422",
		"High 444 Predictive",
		"High 10 Intra",
		"High 422 Intra",
		"High 444 Intra",
		"CAVLC 444 Intra",
		"Scalable Baseline",
		"Scalable High",
		"Scalable High Intra",
365
		"Stereo High",
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
		"Multiview High",
		NULL,
	};
	static const char * const vui_sar_idc[] = {
		"Unspecified",
		"1:1",
		"12:11",
		"10:11",
		"16:11",
		"40:33",
		"24:11",
		"20:11",
		"32:11",
		"80:33",
		"18:11",
		"15:11",
		"64:33",
		"160:99",
		"4:3",
		"3:2",
		"2:1",
		"Extended SAR",
		NULL,
	};
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	static const char * const h264_fp_arrangement_type[] = {
		"Checkerboard",
		"Column",
		"Row",
		"Side by Side",
		"Top Bottom",
		"Temporal",
		NULL,
	};
	static const char * const h264_fmo_map_type[] = {
		"Interleaved Slices",
		"Scattered Slices",
		"Foreground with Leftover",
		"Box Out",
		"Raster Scan",
		"Wipe Scan",
		"Explicit",
		NULL,
	};
409 410 411 412 413 414 415 416 417 418 419 420 421
	static const char * const mpeg_mpeg4_level[] = {
		"0",
		"0b",
		"1",
		"2",
		"3",
		"3b",
		"4",
		"5",
		NULL,
	};
	static const char * const mpeg4_profile[] = {
		"Simple",
422
		"Advanced Simple",
423 424
		"Core",
		"Simple Scalable",
425
		"Advanced Coding Efficiency",
426 427 428
		NULL,
	};

429 430 431 432 433
	static const char * const vpx_golden_frame_sel[] = {
		"Use Previous Frame",
		"Use Previous Specific Frame",
		NULL,
	};
434 435 436 437 438 439 440
	static const char * const vp8_profile[] = {
		"0",
		"1",
		"2",
		"3",
		NULL,
	};
441 442 443 444 445 446 447
	static const char * const vp9_profile[] = {
		"0",
		"1",
		"2",
		"3",
		NULL,
	};
448

449 450 451 452 453 454 455 456 457 458 459
	static const char * const flash_led_mode[] = {
		"Off",
		"Flash",
		"Torch",
		NULL,
	};
	static const char * const flash_strobe_source[] = {
		"Software",
		"External",
		NULL,
	};
460

461 462 463 464 465 466 467 468 469
	static const char * const jpeg_chroma_subsampling[] = {
		"4:4:4",
		"4:2:2",
		"4:2:0",
		"4:1:1",
		"4:1:0",
		"Gray",
		NULL,
	};
470 471 472 473 474 475 476
	static const char * const dv_tx_mode[] = {
		"DVI-D",
		"HDMI",
		NULL,
	};
	static const char * const dv_rgb_range[] = {
		"Automatic",
477 478
		"RGB Limited Range (16-235)",
		"RGB Full Range (0-255)",
479 480
		NULL,
	};
481 482 483 484 485 486 487 488
	static const char * const dv_it_content_type[] = {
		"Graphics",
		"Photo",
		"Cinema",
		"Game",
		"No IT Content",
		NULL,
	};
489 490 491 492 493 494 495
	static const char * const detect_md_mode[] = {
		"Disabled",
		"Global",
		"Threshold Grid",
		"Region Grid",
		NULL,
	};
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	static const char * const hevc_profile[] = {
		"Main",
		"Main Still Picture",
		"Main 10",
		NULL,
	};
	static const char * const hevc_level[] = {
		"1",
		"2",
		"2.1",
		"3",
		"3.1",
		"4",
		"4.1",
		"5",
		"5.1",
		"5.2",
		"6",
		"6.1",
		"6.2",
		NULL,
	};
	static const char * const hevc_hierarchial_coding_type[] = {
		"B",
		"P",
		NULL,
	};
	static const char * const hevc_refresh_type[] = {
		"None",
		"CRA",
		"IDR",
		NULL,
	};
	static const char * const hevc_size_of_length_field[] = {
		"0",
		"1",
		"2",
		"4",
		NULL,
	};
	static const char * const hevc_tier[] = {
		"Main",
		"High",
		NULL,
	};
	static const char * const hevc_loop_filter_mode[] = {
		"Disabled",
		"Enabled",
		"Disabled at slice boundary",
		"NULL",
	};
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	switch (id) {
	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
		return mpeg_audio_sampling_freq;
	case V4L2_CID_MPEG_AUDIO_ENCODING:
		return mpeg_audio_encoding;
	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
		return mpeg_audio_l1_bitrate;
	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
		return mpeg_audio_l2_bitrate;
	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
		return mpeg_audio_l3_bitrate;
	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
		return mpeg_audio_ac3_bitrate;
	case V4L2_CID_MPEG_AUDIO_MODE:
		return mpeg_audio_mode;
	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
		return mpeg_audio_mode_extension;
	case V4L2_CID_MPEG_AUDIO_EMPHASIS:
		return mpeg_audio_emphasis;
	case V4L2_CID_MPEG_AUDIO_CRC:
		return mpeg_audio_crc;
570 571 572
	case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
	case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
		return mpeg_audio_dec_playback;
573 574 575 576 577 578 579 580 581 582 583 584 585 586
	case V4L2_CID_MPEG_VIDEO_ENCODING:
		return mpeg_video_encoding;
	case V4L2_CID_MPEG_VIDEO_ASPECT:
		return mpeg_video_aspect;
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
		return mpeg_video_bitrate_mode;
	case V4L2_CID_MPEG_STREAM_TYPE:
		return mpeg_stream_type;
	case V4L2_CID_MPEG_STREAM_VBI_FMT:
		return mpeg_stream_vbi_fmt;
	case V4L2_CID_POWER_LINE_FREQUENCY:
		return camera_power_line_frequency;
	case V4L2_CID_EXPOSURE_AUTO:
		return camera_exposure_auto;
587 588
	case V4L2_CID_EXPOSURE_METERING:
		return camera_exposure_metering;
589 590
	case V4L2_CID_AUTO_FOCUS_RANGE:
		return camera_auto_focus_range;
591 592
	case V4L2_CID_COLORFX:
		return colorfx;
593 594
	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
		return auto_n_preset_white_balance;
595 596
	case V4L2_CID_ISO_SENSITIVITY_AUTO:
		return camera_iso_sensitivity_auto;
597 598
	case V4L2_CID_SCENE_MODE:
		return scene_mode;
599
	case V4L2_CID_TUNE_PREEMPHASIS:
600 601 602
		return tune_emphasis;
	case V4L2_CID_TUNE_DEEMPHASIS:
		return tune_emphasis;
603 604 605 606
	case V4L2_CID_FLASH_LED_MODE:
		return flash_led_mode;
	case V4L2_CID_FLASH_STROBE_SOURCE:
		return flash_strobe_source;
607 608 609 610 611 612 613 614 615 616 617 618 619 620
	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
		return header_mode;
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
		return multi_slice;
	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
		return entropy_mode;
	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
		return mpeg_h264_level;
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
		return h264_loop_filter;
	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
		return h264_profile;
	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
		return vui_sar_idc;
621 622 623 624
	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
		return h264_fp_arrangement_type;
	case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
		return h264_fmo_map_type;
625 626 627 628
	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
		return mpeg_mpeg4_level;
	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
		return mpeg4_profile;
629 630
	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
		return vpx_golden_frame_sel;
631 632
	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
		return vp8_profile;
633 634
	case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:
		return vp9_profile;
635 636
	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
		return jpeg_chroma_subsampling;
637 638 639 640 641
	case V4L2_CID_DV_TX_MODE:
		return dv_tx_mode;
	case V4L2_CID_DV_TX_RGB_RANGE:
	case V4L2_CID_DV_RX_RGB_RANGE:
		return dv_rgb_range;
642 643 644
	case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
		return dv_it_content_type;
645 646
	case V4L2_CID_DETECT_MD_MODE:
		return detect_md_mode;
647 648 649 650 651 652 653 654 655 656 657 658 659 660
	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
		return hevc_profile;
	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
		return hevc_level;
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
		return hevc_hierarchial_coding_type;
	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
		return hevc_refresh_type;
	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
		return hevc_size_of_length_field;
	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
		return hevc_tier;
	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
		return hevc_loop_filter_mode;
661

662 663 664 665 666 667
	default:
		return NULL;
	}
}
EXPORT_SYMBOL(v4l2_ctrl_get_menu);

668
#define __v4l2_qmenu_int_len(arr, len) ({ *(len) = ARRAY_SIZE(arr); arr; })
669 670 671 672
/*
 * Returns NULL or an s64 type array containing the menu for given
 * control ID. The total number of the menu items is returned in @len.
 */
673
const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len)
674
{
675
	static const s64 qmenu_int_vpx_num_partitions[] = {
676 677 678
		1, 2, 4, 8,
	};

679
	static const s64 qmenu_int_vpx_num_ref_frames[] = {
680 681 682
		1, 2, 3,
	};

683
	switch (id) {
684 685 686 687
	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
		return __v4l2_qmenu_int_len(qmenu_int_vpx_num_partitions, len);
	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
		return __v4l2_qmenu_int_len(qmenu_int_vpx_num_ref_frames, len);
688 689 690
	default:
		*len = 0;
		return NULL;
691
	}
692 693 694
}
EXPORT_SYMBOL(v4l2_ctrl_get_int_menu);

695 696 697 698 699
/* Return the control name. */
const char *v4l2_ctrl_get_name(u32 id)
{
	switch (id) {
	/* USER controls */
H
Hans Verkuil 已提交
700
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
701 702 703 704 705 706 707 708 709 710 711
	case V4L2_CID_USER_CLASS:		return "User Controls";
	case V4L2_CID_BRIGHTNESS:		return "Brightness";
	case V4L2_CID_CONTRAST:			return "Contrast";
	case V4L2_CID_SATURATION:		return "Saturation";
	case V4L2_CID_HUE:			return "Hue";
	case V4L2_CID_AUDIO_VOLUME:		return "Volume";
	case V4L2_CID_AUDIO_BALANCE:		return "Balance";
	case V4L2_CID_AUDIO_BASS:		return "Bass";
	case V4L2_CID_AUDIO_TREBLE:		return "Treble";
	case V4L2_CID_AUDIO_MUTE:		return "Mute";
	case V4L2_CID_AUDIO_LOUDNESS:		return "Loudness";
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
	case V4L2_CID_BLACK_LEVEL:		return "Black Level";
	case V4L2_CID_AUTO_WHITE_BALANCE:	return "White Balance, Automatic";
	case V4L2_CID_DO_WHITE_BALANCE:		return "Do White Balance";
	case V4L2_CID_RED_BALANCE:		return "Red Balance";
	case V4L2_CID_BLUE_BALANCE:		return "Blue Balance";
	case V4L2_CID_GAMMA:			return "Gamma";
	case V4L2_CID_EXPOSURE:			return "Exposure";
	case V4L2_CID_AUTOGAIN:			return "Gain, Automatic";
	case V4L2_CID_GAIN:			return "Gain";
	case V4L2_CID_HFLIP:			return "Horizontal Flip";
	case V4L2_CID_VFLIP:			return "Vertical Flip";
	case V4L2_CID_POWER_LINE_FREQUENCY:	return "Power Line Frequency";
	case V4L2_CID_HUE_AUTO:			return "Hue, Automatic";
	case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
	case V4L2_CID_SHARPNESS:		return "Sharpness";
	case V4L2_CID_BACKLIGHT_COMPENSATION:	return "Backlight Compensation";
	case V4L2_CID_CHROMA_AGC:		return "Chroma AGC";
	case V4L2_CID_COLOR_KILLER:		return "Color Killer";
	case V4L2_CID_COLORFX:			return "Color Effects";
	case V4L2_CID_AUTOBRIGHTNESS:		return "Brightness, Automatic";
	case V4L2_CID_BAND_STOP_FILTER:		return "Band-Stop Filter";
	case V4L2_CID_ROTATE:			return "Rotate";
	case V4L2_CID_BG_COLOR:			return "Background Color";
735
	case V4L2_CID_CHROMA_GAIN:		return "Chroma Gain";
736 737
	case V4L2_CID_ILLUMINATORS_1:		return "Illuminator 1";
	case V4L2_CID_ILLUMINATORS_2:		return "Illuminator 2";
738 739
	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:	return "Min Number of Capture Buffers";
	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:	return "Min Number of Output Buffers";
740
	case V4L2_CID_ALPHA_COMPONENT:		return "Alpha Component";
741
	case V4L2_CID_COLORFX_CBCR:		return "Color Effects, CbCr";
742

743 744 745
	/* Codec controls */
	/* The MPEG controls are applicable to all codec controls
	 * and the 'MPEG' part of the define is historical */
746
	/* Keep the order of the 'case's the same as in videodev2.h! */
747
	case V4L2_CID_MPEG_CLASS:		return "Codec Controls";
748 749 750 751 752
	case V4L2_CID_MPEG_STREAM_TYPE:		return "Stream Type";
	case V4L2_CID_MPEG_STREAM_PID_PMT:	return "Stream PMT Program ID";
	case V4L2_CID_MPEG_STREAM_PID_AUDIO:	return "Stream Audio Program ID";
	case V4L2_CID_MPEG_STREAM_PID_VIDEO:	return "Stream Video Program ID";
	case V4L2_CID_MPEG_STREAM_PID_PCR:	return "Stream PCR Program ID";
753 754 755
	case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
	case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
	case V4L2_CID_MPEG_STREAM_VBI_FMT:	return "Stream VBI Format";
756
	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
757 758 759 760 761
	case V4L2_CID_MPEG_AUDIO_ENCODING:	return "Audio Encoding";
	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:	return "Audio Layer I Bitrate";
	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:	return "Audio Layer II Bitrate";
	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:	return "Audio Layer III Bitrate";
	case V4L2_CID_MPEG_AUDIO_MODE:		return "Audio Stereo Mode";
762
	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
763 764 765 766 767
	case V4L2_CID_MPEG_AUDIO_EMPHASIS:	return "Audio Emphasis";
	case V4L2_CID_MPEG_AUDIO_CRC:		return "Audio CRC";
	case V4L2_CID_MPEG_AUDIO_MUTE:		return "Audio Mute";
	case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:	return "Audio AAC Bitrate";
	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:	return "Audio AC-3 Bitrate";
768 769
	case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:	return "Audio Playback";
	case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
770 771 772 773 774 775 776 777 778
	case V4L2_CID_MPEG_VIDEO_ENCODING:	return "Video Encoding";
	case V4L2_CID_MPEG_VIDEO_ASPECT:	return "Video Aspect";
	case V4L2_CID_MPEG_VIDEO_B_FRAMES:	return "Video B Frames";
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:	return "Video GOP Size";
	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:	return "Video GOP Closure";
	case V4L2_CID_MPEG_VIDEO_PULLDOWN:	return "Video Pulldown";
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:	return "Video Bitrate Mode";
	case V4L2_CID_MPEG_VIDEO_BITRATE:	return "Video Bitrate";
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:	return "Video Peak Bitrate";
779
	case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
780
	case V4L2_CID_MPEG_VIDEO_MUTE:		return "Video Mute";
781
	case V4L2_CID_MPEG_VIDEO_MUTE_YUV:	return "Video Mute YUV";
782 783
	case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:	return "Decoder Slice Interface";
	case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:	return "MPEG4 Loop Filter Enable";
784
	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:	return "Number of Intra Refresh MBs";
785 786 787
	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:		return "Frame Level Rate Control Enable";
	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:			return "H264 MB Level Rate Control";
	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:			return "Sequence Header Mode";
788
	case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC:			return "Max Number of Reference Pics";
789
	case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:		return "H263 I-Frame QP Value";
790 791
	case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:		return "H263 P-Frame QP Value";
	case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:		return "H263 B-Frame QP Value";
792 793 794
	case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:			return "H263 Minimum QP Value";
	case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:			return "H263 Maximum QP Value";
	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:		return "H264 I-Frame QP Value";
795 796
	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:		return "H264 P-Frame QP Value";
	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:		return "H264 B-Frame QP Value";
797 798 799 800
	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:			return "H264 Maximum QP Value";
	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:			return "H264 Minimum QP Value";
	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:		return "H264 8x8 Transform Enable";
	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:			return "H264 CPB Buffer Size";
801 802
	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:		return "H264 Entropy Mode";
	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:			return "H264 I-Frame Period";
803 804 805 806 807 808 809 810 811
	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:			return "H264 Level";
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:	return "H264 Loop Filter Alpha Offset";
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:		return "H264 Loop Filter Beta Offset";
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:		return "H264 Loop Filter Mode";
	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:			return "H264 Profile";
	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:	return "Vertical Size of SAR";
	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:	return "Horizontal Size of SAR";
	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:		return "Aspect Ratio VUI Enable";
	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:		return "VUI Aspect Ratio IDC";
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:	return "H264 Enable Frame Packing SEI";
	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_CURRENT_FRAME_0:	return "H264 Set Curr. Frame as Frame0";
	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:	return "H264 FP Arrangement Type";
	case V4L2_CID_MPEG_VIDEO_H264_FMO:			return "H264 Flexible MB Ordering";
	case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:		return "H264 Map Type for FMO";
	case V4L2_CID_MPEG_VIDEO_H264_FMO_SLICE_GROUP:		return "H264 FMO Number of Slice Groups";
	case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_DIRECTION:	return "H264 FMO Direction of Change";
	case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_RATE:		return "H264 FMO Size of 1st Slice Grp";
	case V4L2_CID_MPEG_VIDEO_H264_FMO_RUN_LENGTH:		return "H264 FMO No. of Consecutive MBs";
	case V4L2_CID_MPEG_VIDEO_H264_ASO:			return "H264 Arbitrary Slice Ordering";
	case V4L2_CID_MPEG_VIDEO_H264_ASO_SLICE_ORDER:		return "H264 ASO Slice Order";
	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING:	return "Enable H264 Hierarchical Coding";
	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE:	return "H264 Hierarchical Coding Type";
	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER:return "H264 Number of HC Layers";
	case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER_QP:
								return "H264 Set QP Value for HC Layers";
828
	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:		return "MPEG4 I-Frame QP Value";
829 830
	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:		return "MPEG4 P-Frame QP Value";
	case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:		return "MPEG4 B-Frame QP Value";
831 832 833 834 835
	case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:			return "MPEG4 Minimum QP Value";
	case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:			return "MPEG4 Maximum QP Value";
	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:			return "MPEG4 Level";
	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:			return "MPEG4 Profile";
	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:			return "Quarter Pixel Search Enable";
836 837 838
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:		return "Maximum Bytes in a Slice";
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:		return "Number of MBs in a Slice";
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:		return "Slice Partitioning Method";
839
	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:			return "VBV Buffer Size";
840 841
	case V4L2_CID_MPEG_VIDEO_DEC_PTS:			return "Video Decoder PTS";
	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:			return "Video Decoder Frame Count";
842
	case V4L2_CID_MPEG_VIDEO_VBV_DELAY:			return "Initial Delay for VBV Control";
843 844
	case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:		return "Horizontal MV Search Range";
	case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:		return "Vertical MV Search Range";
845
	case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:		return "Repeat Sequence Header";
846
	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:		return "Force Key Frame";
847

848 849 850 851 852 853 854 855
	/* VPX controls */
	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:		return "VPX Number of Partitions";
	case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:		return "VPX Intra Mode Decision Disable";
	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:		return "VPX No. of Refs for P Frame";
	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:		return "VPX Loop Filter Level Range";
	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:		return "VPX Deblocking Effect Control";
	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:	return "VPX Golden Frame Refresh Period";
	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:		return "VPX Golden Frame Indicator";
856 857 858 859
	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:			return "VPX Minimum QP Value";
	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:			return "VPX Maximum QP Value";
	case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:		return "VPX I-Frame QP Value";
	case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:		return "VPX P-Frame QP Value";
860
	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:			return "VP8 Profile";
861
	case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:			return "VP9 Profile";
862

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	/* HEVC controls */
	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:		return "HEVC I-Frame QP Value";
	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP:		return "HEVC P-Frame QP Value";
	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP:		return "HEVC B-Frame QP Value";
	case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP:			return "HEVC Minimum QP Value";
	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP:			return "HEVC Maximum QP Value";
	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:			return "HEVC Profile";
	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:			return "HEVC Level";
	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:			return "HEVC Tier";
	case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION:	return "HEVC Frame Rate Resolution";
	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH:	return "HEVC Maximum Coding Unit Depth";
	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:		return "HEVC Refresh Type";
	case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED:		return "HEVC Constant Intra Prediction";
	case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU:		return "HEVC Lossless Encoding";
	case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT:		return "HEVC Wavefront";
	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:		return "HEVC Loop Filter";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP:			return "HEVC QP Values";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:		return "HEVC Hierarchical Coding Type";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:	return "HEVC Hierarchical Coding Layer";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP:	return "HEVC Hierarchical Layer 0 QP";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP:	return "HEVC Hierarchical Layer 1 QP";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP:	return "HEVC Hierarchical Layer 2 QP";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP:	return "HEVC Hierarchical Layer 3 QP";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP:	return "HEVC Hierarchical Layer 4 QP";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP:	return "HEVC Hierarchical Layer 5 QP";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP:	return "HEVC Hierarchical Layer 6 QP";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:	return "HEVC Hierarchical Lay 0 BitRate";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:	return "HEVC Hierarchical Lay 1 BitRate";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:	return "HEVC Hierarchical Lay 2 BitRate";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:	return "HEVC Hierarchical Lay 3 BitRate";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:	return "HEVC Hierarchical Lay 4 BitRate";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:	return "HEVC Hierarchical Lay 5 BitRate";
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR:	return "HEVC Hierarchical Lay 6 BitRate";
	case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB:		return "HEVC General PB";
	case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID:		return "HEVC Temporal ID";
	case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING:		return "HEVC Strong Intra Smoothing";
	case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT:		return "HEVC Intra PU Split";
	case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION:		return "HEVC TMV Prediction";
	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1:	return "HEVC Max Num of Candidate MVs";
	case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE:	return "HEVC ENC Without Startcode";
	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD:		return "HEVC Num of I-Frame b/w 2 IDR";
	case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2:	return "HEVC Loop Filter Beta Offset";
	case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2:	return "HEVC Loop Filter TC Offset";
	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:	return "HEVC Size of Length Field";
	case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:	return "Reference Frames for a P-Frame";
	case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:		return "Prepend SPS and PPS to IDR";

910
	/* CAMERA controls */
H
Hans Verkuil 已提交
911
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
912 913 914 915 916 917 918 919 920 921 922 923
	case V4L2_CID_CAMERA_CLASS:		return "Camera Controls";
	case V4L2_CID_EXPOSURE_AUTO:		return "Auto Exposure";
	case V4L2_CID_EXPOSURE_ABSOLUTE:	return "Exposure Time, Absolute";
	case V4L2_CID_EXPOSURE_AUTO_PRIORITY:	return "Exposure, Dynamic Framerate";
	case V4L2_CID_PAN_RELATIVE:		return "Pan, Relative";
	case V4L2_CID_TILT_RELATIVE:		return "Tilt, Relative";
	case V4L2_CID_PAN_RESET:		return "Pan, Reset";
	case V4L2_CID_TILT_RESET:		return "Tilt, Reset";
	case V4L2_CID_PAN_ABSOLUTE:		return "Pan, Absolute";
	case V4L2_CID_TILT_ABSOLUTE:		return "Tilt, Absolute";
	case V4L2_CID_FOCUS_ABSOLUTE:		return "Focus, Absolute";
	case V4L2_CID_FOCUS_RELATIVE:		return "Focus, Relative";
924
	case V4L2_CID_FOCUS_AUTO:		return "Focus, Automatic Continuous";
925 926 927 928
	case V4L2_CID_ZOOM_ABSOLUTE:		return "Zoom, Absolute";
	case V4L2_CID_ZOOM_RELATIVE:		return "Zoom, Relative";
	case V4L2_CID_ZOOM_CONTINUOUS:		return "Zoom, Continuous";
	case V4L2_CID_PRIVACY:			return "Privacy";
929 930
	case V4L2_CID_IRIS_ABSOLUTE:		return "Iris, Absolute";
	case V4L2_CID_IRIS_RELATIVE:		return "Iris, Relative";
931
	case V4L2_CID_AUTO_EXPOSURE_BIAS:	return "Auto Exposure, Bias";
932
	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
933
	case V4L2_CID_WIDE_DYNAMIC_RANGE:	return "Wide Dynamic Range";
934
	case V4L2_CID_IMAGE_STABILIZATION:	return "Image Stabilization";
935 936
	case V4L2_CID_ISO_SENSITIVITY:		return "ISO Sensitivity";
	case V4L2_CID_ISO_SENSITIVITY_AUTO:	return "ISO Sensitivity, Auto";
937
	case V4L2_CID_EXPOSURE_METERING:	return "Exposure, Metering Mode";
938
	case V4L2_CID_SCENE_MODE:		return "Scene Mode";
939
	case V4L2_CID_3A_LOCK:			return "3A Lock";
940 941 942 943
	case V4L2_CID_AUTO_FOCUS_START:		return "Auto Focus, Start";
	case V4L2_CID_AUTO_FOCUS_STOP:		return "Auto Focus, Stop";
	case V4L2_CID_AUTO_FOCUS_STATUS:	return "Auto Focus, Status";
	case V4L2_CID_AUTO_FOCUS_RANGE:		return "Auto Focus, Range";
944 945
	case V4L2_CID_PAN_SPEED:		return "Pan, Speed";
	case V4L2_CID_TILT_SPEED:		return "Tilt, Speed";
946

H
Hans Verkuil 已提交
947 948
	/* FM Radio Modulator controls */
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
949 950 951 952 953 954
	case V4L2_CID_FM_TX_CLASS:		return "FM Radio Modulator Controls";
	case V4L2_CID_RDS_TX_DEVIATION:		return "RDS Signal Deviation";
	case V4L2_CID_RDS_TX_PI:		return "RDS Program ID";
	case V4L2_CID_RDS_TX_PTY:		return "RDS Program Type";
	case V4L2_CID_RDS_TX_PS_NAME:		return "RDS PS Name";
	case V4L2_CID_RDS_TX_RADIO_TEXT:	return "RDS Radio Text";
955 956 957 958 959 960 961 962 963
	case V4L2_CID_RDS_TX_MONO_STEREO:	return "RDS Stereo";
	case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:	return "RDS Artificial Head";
	case V4L2_CID_RDS_TX_COMPRESSED:	return "RDS Compressed";
	case V4L2_CID_RDS_TX_DYNAMIC_PTY:	return "RDS Dynamic PTY";
	case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
	case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:	return "RDS Traffic Program";
	case V4L2_CID_RDS_TX_MUSIC_SPEECH:	return "RDS Music";
	case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:	return "RDS Enable Alt Frequencies";
	case V4L2_CID_RDS_TX_ALT_FREQS:		return "RDS Alternate Frequencies";
964 965 966
	case V4L2_CID_AUDIO_LIMITER_ENABLED:	return "Audio Limiter Feature Enabled";
	case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
	case V4L2_CID_AUDIO_LIMITER_DEVIATION:	return "Audio Limiter Deviation";
967
	case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
968 969 970 971 972 973 974
	case V4L2_CID_AUDIO_COMPRESSION_GAIN:	return "Audio Compression Gain";
	case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
	case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
	case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
	case V4L2_CID_PILOT_TONE_ENABLED:	return "Pilot Tone Feature Enabled";
	case V4L2_CID_PILOT_TONE_DEVIATION:	return "Pilot Tone Deviation";
	case V4L2_CID_PILOT_TONE_FREQUENCY:	return "Pilot Tone Frequency";
975
	case V4L2_CID_TUNE_PREEMPHASIS:		return "Pre-Emphasis";
976 977 978
	case V4L2_CID_TUNE_POWER_LEVEL:		return "Tune Power Level";
	case V4L2_CID_TUNE_ANTENNA_CAPACITOR:	return "Tune Antenna Capacitor";

979
	/* Flash controls */
H
Hans Verkuil 已提交
980
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
981 982 983
	case V4L2_CID_FLASH_CLASS:		return "Flash Controls";
	case V4L2_CID_FLASH_LED_MODE:		return "LED Mode";
	case V4L2_CID_FLASH_STROBE_SOURCE:	return "Strobe Source";
984
	case V4L2_CID_FLASH_STROBE:		return "Strobe";
985 986 987 988 989 990
	case V4L2_CID_FLASH_STROBE_STOP:	return "Stop Strobe";
	case V4L2_CID_FLASH_STROBE_STATUS:	return "Strobe Status";
	case V4L2_CID_FLASH_TIMEOUT:		return "Strobe Timeout";
	case V4L2_CID_FLASH_INTENSITY:		return "Intensity, Flash Mode";
	case V4L2_CID_FLASH_TORCH_INTENSITY:	return "Intensity, Torch Mode";
	case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
991 992
	case V4L2_CID_FLASH_FAULT:		return "Faults";
	case V4L2_CID_FLASH_CHARGE:		return "Charge";
993
	case V4L2_CID_FLASH_READY:		return "Ready to Strobe";
994

995
	/* JPEG encoder controls */
H
Hans Verkuil 已提交
996
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
997 998 999 1000 1001 1002
	case V4L2_CID_JPEG_CLASS:		return "JPEG Compression Controls";
	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:	return "Chroma Subsampling";
	case V4L2_CID_JPEG_RESTART_INTERVAL:	return "Restart Interval";
	case V4L2_CID_JPEG_COMPRESSION_QUALITY:	return "Compression Quality";
	case V4L2_CID_JPEG_ACTIVE_MARKER:	return "Active Markers";

1003
	/* Image source controls */
H
Hans Verkuil 已提交
1004
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1005 1006 1007 1008
	case V4L2_CID_IMAGE_SOURCE_CLASS:	return "Image Source Controls";
	case V4L2_CID_VBLANK:			return "Vertical Blanking";
	case V4L2_CID_HBLANK:			return "Horizontal Blanking";
	case V4L2_CID_ANALOGUE_GAIN:		return "Analogue Gain";
1009 1010 1011 1012
	case V4L2_CID_TEST_PATTERN_RED:		return "Red Pixel Value";
	case V4L2_CID_TEST_PATTERN_GREENR:	return "Green (Red) Pixel Value";
	case V4L2_CID_TEST_PATTERN_BLUE:	return "Blue Pixel Value";
	case V4L2_CID_TEST_PATTERN_GREENB:	return "Green (Blue) Pixel Value";
1013

1014
	/* Image processing controls */
H
Hans Verkuil 已提交
1015
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1016 1017 1018
	case V4L2_CID_IMAGE_PROC_CLASS:		return "Image Processing Controls";
	case V4L2_CID_LINK_FREQ:		return "Link Frequency";
	case V4L2_CID_PIXEL_RATE:		return "Pixel Rate";
1019
	case V4L2_CID_TEST_PATTERN:		return "Test Pattern";
1020
	case V4L2_CID_DEINTERLACING_MODE:	return "Deinterlacing Mode";
1021
	case V4L2_CID_DIGITAL_GAIN:		return "Digital Gain";
1022

1023
	/* DV controls */
H
Hans Verkuil 已提交
1024
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
1025 1026 1027 1028 1029 1030
	case V4L2_CID_DV_CLASS:			return "Digital Video Controls";
	case V4L2_CID_DV_TX_HOTPLUG:		return "Hotplug Present";
	case V4L2_CID_DV_TX_RXSENSE:		return "RxSense Present";
	case V4L2_CID_DV_TX_EDID_PRESENT:	return "EDID Present";
	case V4L2_CID_DV_TX_MODE:		return "Transmit Mode";
	case V4L2_CID_DV_TX_RGB_RANGE:		return "Tx RGB Quantization Range";
1031
	case V4L2_CID_DV_TX_IT_CONTENT_TYPE:	return "Tx IT Content Type";
1032 1033
	case V4L2_CID_DV_RX_POWER_PRESENT:	return "Power Present";
	case V4L2_CID_DV_RX_RGB_RANGE:		return "Rx RGB Quantization Range";
1034
	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:	return "Rx IT Content Type";
1035

1036 1037 1038
	case V4L2_CID_FM_RX_CLASS:		return "FM Radio Receiver Controls";
	case V4L2_CID_TUNE_DEEMPHASIS:		return "De-Emphasis";
	case V4L2_CID_RDS_RECEPTION:		return "RDS Reception";
1039
	case V4L2_CID_RF_TUNER_CLASS:		return "RF Tuner Controls";
1040
	case V4L2_CID_RF_TUNER_RF_GAIN:		return "RF Gain";
1041 1042 1043 1044 1045 1046
	case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:	return "LNA Gain, Auto";
	case V4L2_CID_RF_TUNER_LNA_GAIN:	return "LNA Gain";
	case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:	return "Mixer Gain, Auto";
	case V4L2_CID_RF_TUNER_MIXER_GAIN:	return "Mixer Gain";
	case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:	return "IF Gain, Auto";
	case V4L2_CID_RF_TUNER_IF_GAIN:		return "IF Gain";
1047 1048
	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:	return "Bandwidth, Auto";
	case V4L2_CID_RF_TUNER_BANDWIDTH:	return "Bandwidth";
1049
	case V4L2_CID_RF_TUNER_PLL_LOCK:	return "PLL Lock";
1050 1051 1052 1053 1054 1055
	case V4L2_CID_RDS_RX_PTY:		return "RDS Program Type";
	case V4L2_CID_RDS_RX_PS_NAME:		return "RDS PS Name";
	case V4L2_CID_RDS_RX_RADIO_TEXT:	return "RDS Radio Text";
	case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
	case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:	return "RDS Traffic Program";
	case V4L2_CID_RDS_RX_MUSIC_SPEECH:	return "RDS Music";
1056 1057 1058 1059 1060 1061 1062 1063

	/* Detection controls */
	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
	case V4L2_CID_DETECT_CLASS:		return "Detection Controls";
	case V4L2_CID_DETECT_MD_MODE:		return "Motion Detection Mode";
	case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD: return "MD Global Threshold";
	case V4L2_CID_DETECT_MD_THRESHOLD_GRID:	return "MD Threshold Grid";
	case V4L2_CID_DETECT_MD_REGION_GRID:	return "MD Region Grid";
1064 1065 1066 1067 1068 1069 1070
	default:
		return NULL;
	}
}
EXPORT_SYMBOL(v4l2_ctrl_get_name);

void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
1071
		    s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags)
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
{
	*name = v4l2_ctrl_get_name(id);
	*flags = 0;

	switch (id) {
	case V4L2_CID_AUDIO_MUTE:
	case V4L2_CID_AUDIO_LOUDNESS:
	case V4L2_CID_AUTO_WHITE_BALANCE:
	case V4L2_CID_AUTOGAIN:
	case V4L2_CID_HFLIP:
	case V4L2_CID_VFLIP:
	case V4L2_CID_HUE_AUTO:
	case V4L2_CID_CHROMA_AGC:
	case V4L2_CID_COLOR_KILLER:
1086
	case V4L2_CID_AUTOBRIGHTNESS:
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	case V4L2_CID_MPEG_AUDIO_MUTE:
	case V4L2_CID_MPEG_VIDEO_MUTE:
	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
	case V4L2_CID_MPEG_VIDEO_PULLDOWN:
	case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
	case V4L2_CID_FOCUS_AUTO:
	case V4L2_CID_PRIVACY:
	case V4L2_CID_AUDIO_LIMITER_ENABLED:
	case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
	case V4L2_CID_PILOT_TONE_ENABLED:
1097 1098
	case V4L2_CID_ILLUMINATORS_1:
	case V4L2_CID_ILLUMINATORS_2:
1099 1100 1101
	case V4L2_CID_FLASH_STROBE_STATUS:
	case V4L2_CID_FLASH_CHARGE:
	case V4L2_CID_FLASH_READY:
1102 1103 1104 1105 1106 1107 1108
	case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
	case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1109
	case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:
1110
	case V4L2_CID_WIDE_DYNAMIC_RANGE:
1111
	case V4L2_CID_IMAGE_STABILIZATION:
1112
	case V4L2_CID_RDS_RECEPTION:
1113 1114 1115
	case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
	case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
	case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
1116
	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1117
	case V4L2_CID_RF_TUNER_PLL_LOCK:
1118 1119 1120 1121 1122 1123 1124 1125
	case V4L2_CID_RDS_TX_MONO_STEREO:
	case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
	case V4L2_CID_RDS_TX_COMPRESSED:
	case V4L2_CID_RDS_TX_DYNAMIC_PTY:
	case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
	case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
	case V4L2_CID_RDS_TX_MUSIC_SPEECH:
	case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
1126 1127 1128
	case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
	case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
	case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1129 1130 1131 1132
		*type = V4L2_CTRL_TYPE_BOOLEAN;
		*min = 0;
		*max = *step = 1;
		break;
1133 1134 1135 1136
	case V4L2_CID_ROTATE:
		*type = V4L2_CTRL_TYPE_INTEGER;
		*flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
		break;
1137 1138 1139 1140
	case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
	case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
		*type = V4L2_CTRL_TYPE_INTEGER;
		break;
1141
	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1142 1143
	case V4L2_CID_PAN_RESET:
	case V4L2_CID_TILT_RESET:
1144 1145
	case V4L2_CID_FLASH_STROBE:
	case V4L2_CID_FLASH_STROBE_STOP:
1146 1147
	case V4L2_CID_AUTO_FOCUS_START:
	case V4L2_CID_AUTO_FOCUS_STOP:
1148
		*type = V4L2_CTRL_TYPE_BUTTON;
1149 1150
		*flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
			  V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		*min = *max = *step = *def = 0;
		break;
	case V4L2_CID_POWER_LINE_FREQUENCY:
	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
	case V4L2_CID_MPEG_AUDIO_ENCODING:
	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
	case V4L2_CID_MPEG_AUDIO_MODE:
	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
	case V4L2_CID_MPEG_AUDIO_EMPHASIS:
	case V4L2_CID_MPEG_AUDIO_CRC:
1164 1165
	case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
	case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
1166 1167 1168 1169 1170 1171
	case V4L2_CID_MPEG_VIDEO_ENCODING:
	case V4L2_CID_MPEG_VIDEO_ASPECT:
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
	case V4L2_CID_MPEG_STREAM_TYPE:
	case V4L2_CID_MPEG_STREAM_VBI_FMT:
	case V4L2_CID_EXPOSURE_AUTO:
1172
	case V4L2_CID_AUTO_FOCUS_RANGE:
1173
	case V4L2_CID_COLORFX:
1174
	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1175
	case V4L2_CID_TUNE_PREEMPHASIS:
1176 1177
	case V4L2_CID_FLASH_LED_MODE:
	case V4L2_CID_FLASH_STROBE_SOURCE:
1178 1179 1180 1181 1182 1183 1184
	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1185 1186
	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
	case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
1187 1188
	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1189
	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1190
	case V4L2_CID_ISO_SENSITIVITY_AUTO:
1191
	case V4L2_CID_EXPOSURE_METERING:
1192
	case V4L2_CID_SCENE_MODE:
1193 1194
	case V4L2_CID_DV_TX_MODE:
	case V4L2_CID_DV_TX_RGB_RANGE:
1195
	case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
1196
	case V4L2_CID_DV_RX_RGB_RANGE:
1197
	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1198
	case V4L2_CID_TEST_PATTERN:
1199
	case V4L2_CID_DEINTERLACING_MODE:
1200
	case V4L2_CID_TUNE_DEEMPHASIS:
1201
	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
1202
	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
1203
	case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:
1204
	case V4L2_CID_DETECT_MD_MODE:
1205 1206 1207 1208 1209 1210 1211
	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
1212 1213
		*type = V4L2_CTRL_TYPE_MENU;
		break;
1214 1215 1216
	case V4L2_CID_LINK_FREQ:
		*type = V4L2_CTRL_TYPE_INTEGER_MENU;
		break;
1217 1218
	case V4L2_CID_RDS_TX_PS_NAME:
	case V4L2_CID_RDS_TX_RADIO_TEXT:
1219 1220
	case V4L2_CID_RDS_RX_PS_NAME:
	case V4L2_CID_RDS_RX_RADIO_TEXT:
1221 1222
		*type = V4L2_CTRL_TYPE_STRING;
		break;
1223
	case V4L2_CID_ISO_SENSITIVITY:
1224
	case V4L2_CID_AUTO_EXPOSURE_BIAS:
1225 1226
	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
1227 1228
		*type = V4L2_CTRL_TYPE_INTEGER_MENU;
		break;
1229 1230 1231 1232
	case V4L2_CID_USER_CLASS:
	case V4L2_CID_CAMERA_CLASS:
	case V4L2_CID_MPEG_CLASS:
	case V4L2_CID_FM_TX_CLASS:
1233
	case V4L2_CID_FLASH_CLASS:
1234
	case V4L2_CID_JPEG_CLASS:
1235
	case V4L2_CID_IMAGE_SOURCE_CLASS:
1236
	case V4L2_CID_IMAGE_PROC_CLASS:
1237
	case V4L2_CID_DV_CLASS:
1238
	case V4L2_CID_FM_RX_CLASS:
1239
	case V4L2_CID_RF_TUNER_CLASS:
1240
	case V4L2_CID_DETECT_CLASS:
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
		*type = V4L2_CTRL_TYPE_CTRL_CLASS;
		/* You can neither read not write these */
		*flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
		*min = *max = *step = *def = 0;
		break;
	case V4L2_CID_BG_COLOR:
		*type = V4L2_CTRL_TYPE_INTEGER;
		*step = 1;
		*min = 0;
		/* Max is calculated as RGB888 that is 2^24 */
		*max = 0xFFFFFF;
		break;
1253
	case V4L2_CID_FLASH_FAULT:
1254
	case V4L2_CID_JPEG_ACTIVE_MARKER:
1255
	case V4L2_CID_3A_LOCK:
1256
	case V4L2_CID_AUTO_FOCUS_STATUS:
1257 1258 1259 1260
	case V4L2_CID_DV_TX_HOTPLUG:
	case V4L2_CID_DV_TX_RXSENSE:
	case V4L2_CID_DV_TX_EDID_PRESENT:
	case V4L2_CID_DV_RX_POWER_PRESENT:
1261 1262
		*type = V4L2_CTRL_TYPE_BITMASK;
		break;
1263 1264 1265 1266 1267
	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
		*type = V4L2_CTRL_TYPE_INTEGER;
		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
		break;
1268
	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
		*type = V4L2_CTRL_TYPE_INTEGER64;
		*flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
		*min = *def = 0;
		*max = 0x1ffffffffLL;
		*step = 1;
		break;
	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
		*type = V4L2_CTRL_TYPE_INTEGER64;
		*flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
		*min = *def = 0;
		*max = 0x7fffffffffffffffLL;
		*step = 1;
		break;
1282
	case V4L2_CID_PIXEL_RATE:
1283
		*type = V4L2_CTRL_TYPE_INTEGER64;
1284
		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
1285
		break;
1286 1287 1288 1289 1290 1291
	case V4L2_CID_DETECT_MD_REGION_GRID:
		*type = V4L2_CTRL_TYPE_U8;
		break;
	case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
		*type = V4L2_CTRL_TYPE_U16;
		break;
1292 1293 1294
	case V4L2_CID_RDS_TX_ALT_FREQS:
		*type = V4L2_CTRL_TYPE_U32;
		break;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	default:
		*type = V4L2_CTRL_TYPE_INTEGER;
		break;
	}
	switch (id) {
	case V4L2_CID_MPEG_AUDIO_ENCODING:
	case V4L2_CID_MPEG_AUDIO_MODE:
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
	case V4L2_CID_MPEG_STREAM_TYPE:
		*flags |= V4L2_CTRL_FLAG_UPDATE;
		break;
	case V4L2_CID_AUDIO_VOLUME:
	case V4L2_CID_AUDIO_BALANCE:
	case V4L2_CID_AUDIO_BASS:
	case V4L2_CID_AUDIO_TREBLE:
	case V4L2_CID_BRIGHTNESS:
	case V4L2_CID_CONTRAST:
	case V4L2_CID_SATURATION:
	case V4L2_CID_HUE:
	case V4L2_CID_RED_BALANCE:
	case V4L2_CID_BLUE_BALANCE:
	case V4L2_CID_GAMMA:
	case V4L2_CID_SHARPNESS:
	case V4L2_CID_CHROMA_GAIN:
	case V4L2_CID_RDS_TX_DEVIATION:
	case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
	case V4L2_CID_AUDIO_LIMITER_DEVIATION:
	case V4L2_CID_AUDIO_COMPRESSION_GAIN:
	case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
	case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
	case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
	case V4L2_CID_PILOT_TONE_DEVIATION:
	case V4L2_CID_PILOT_TONE_FREQUENCY:
	case V4L2_CID_TUNE_POWER_LEVEL:
	case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1331
	case V4L2_CID_RF_TUNER_RF_GAIN:
1332 1333 1334
	case V4L2_CID_RF_TUNER_LNA_GAIN:
	case V4L2_CID_RF_TUNER_MIXER_GAIN:
	case V4L2_CID_RF_TUNER_IF_GAIN:
1335
	case V4L2_CID_RF_TUNER_BANDWIDTH:
1336
	case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
1337 1338 1339 1340 1341 1342 1343
		*flags |= V4L2_CTRL_FLAG_SLIDER;
		break;
	case V4L2_CID_PAN_RELATIVE:
	case V4L2_CID_TILT_RELATIVE:
	case V4L2_CID_FOCUS_RELATIVE:
	case V4L2_CID_IRIS_RELATIVE:
	case V4L2_CID_ZOOM_RELATIVE:
1344 1345
		*flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
			  V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1346
		break;
1347
	case V4L2_CID_FLASH_STROBE_STATUS:
1348
	case V4L2_CID_AUTO_FOCUS_STATUS:
1349
	case V4L2_CID_FLASH_READY:
1350 1351 1352 1353
	case V4L2_CID_DV_TX_HOTPLUG:
	case V4L2_CID_DV_TX_RXSENSE:
	case V4L2_CID_DV_TX_EDID_PRESENT:
	case V4L2_CID_DV_RX_POWER_PRESENT:
1354
	case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1355 1356 1357 1358 1359 1360
	case V4L2_CID_RDS_RX_PTY:
	case V4L2_CID_RDS_RX_PS_NAME:
	case V4L2_CID_RDS_RX_RADIO_TEXT:
	case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
	case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
	case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1361 1362
		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
		break;
1363 1364 1365
	case V4L2_CID_RF_TUNER_PLL_LOCK:
		*flags |= V4L2_CTRL_FLAG_VOLATILE;
		break;
1366 1367 1368 1369
	}
}
EXPORT_SYMBOL(v4l2_ctrl_fill);

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
static u32 user_flags(const struct v4l2_ctrl *ctrl)
{
	u32 flags = ctrl->flags;

	if (ctrl->is_ptr)
		flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;

	return flags;
}

1380 1381 1382 1383 1384 1385 1386
static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
{
	memset(ev->reserved, 0, sizeof(ev->reserved));
	ev->type = V4L2_EVENT_CTRL;
	ev->id = ctrl->id;
	ev->u.ctrl.changes = changes;
	ev->u.ctrl.type = ctrl->type;
1387
	ev->u.ctrl.flags = user_flags(ctrl);
1388
	if (ctrl->is_ptr)
1389 1390
		ev->u.ctrl.value64 = 0;
	else
1391
		ev->u.ctrl.value64 = *ctrl->p_cur.p_s64;
1392 1393
	ev->u.ctrl.minimum = ctrl->minimum;
	ev->u.ctrl.maximum = ctrl->maximum;
1394 1395
	if (ctrl->type == V4L2_CTRL_TYPE_MENU
	    || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1396 1397 1398 1399 1400 1401 1402 1403 1404
		ev->u.ctrl.step = 1;
	else
		ev->u.ctrl.step = ctrl->step;
	ev->u.ctrl.default_value = ctrl->default_value;
}

static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
{
	struct v4l2_event ev;
1405
	struct v4l2_subscribed_event *sev;
1406

1407
	if (list_empty(&ctrl->ev_subs))
1408
		return;
1409 1410
	fill_event(&ev, ctrl, changes);

1411
	list_for_each_entry(sev, &ctrl->ev_subs, node)
1412 1413
		if (sev->fh != fh ||
		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
1414
			v4l2_event_queue_fh(sev->fh, &ev);
1415 1416
}

1417
static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx,
1418 1419 1420 1421 1422 1423 1424
		      union v4l2_ctrl_ptr ptr1,
		      union v4l2_ctrl_ptr ptr2)
{
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_BUTTON:
		return false;
	case V4L2_CTRL_TYPE_STRING:
1425
		idx *= ctrl->elem_size;
1426
		/* strings are always 0-terminated */
1427
		return !strcmp(ptr1.p_char + idx, ptr2.p_char + idx);
1428
	case V4L2_CTRL_TYPE_INTEGER64:
1429
		return ptr1.p_s64[idx] == ptr2.p_s64[idx];
1430 1431 1432 1433
	case V4L2_CTRL_TYPE_U8:
		return ptr1.p_u8[idx] == ptr2.p_u8[idx];
	case V4L2_CTRL_TYPE_U16:
		return ptr1.p_u16[idx] == ptr2.p_u16[idx];
1434 1435
	case V4L2_CTRL_TYPE_U32:
		return ptr1.p_u32[idx] == ptr2.p_u32[idx];
1436
	default:
1437 1438 1439 1440
		if (ctrl->is_int)
			return ptr1.p_s32[idx] == ptr2.p_s32[idx];
		idx *= ctrl->elem_size;
		return !memcmp(ptr1.p + idx, ptr2.p + idx, ctrl->elem_size);
1441 1442 1443
	}
}

1444
static void std_init(const struct v4l2_ctrl *ctrl, u32 idx,
1445 1446 1447 1448
		     union v4l2_ctrl_ptr ptr)
{
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_STRING:
1449 1450 1451
		idx *= ctrl->elem_size;
		memset(ptr.p_char + idx, ' ', ctrl->minimum);
		ptr.p_char[idx + ctrl->minimum] = '\0';
1452 1453
		break;
	case V4L2_CTRL_TYPE_INTEGER64:
1454
		ptr.p_s64[idx] = ctrl->default_value;
1455 1456 1457 1458 1459 1460
		break;
	case V4L2_CTRL_TYPE_INTEGER:
	case V4L2_CTRL_TYPE_INTEGER_MENU:
	case V4L2_CTRL_TYPE_MENU:
	case V4L2_CTRL_TYPE_BITMASK:
	case V4L2_CTRL_TYPE_BOOLEAN:
1461
		ptr.p_s32[idx] = ctrl->default_value;
1462
		break;
1463 1464 1465 1466 1467 1468
	case V4L2_CTRL_TYPE_U8:
		ptr.p_u8[idx] = ctrl->default_value;
		break;
	case V4L2_CTRL_TYPE_U16:
		ptr.p_u16[idx] = ctrl->default_value;
		break;
1469 1470 1471
	case V4L2_CTRL_TYPE_U32:
		ptr.p_u32[idx] = ctrl->default_value;
		break;
1472
	default:
1473 1474
		idx *= ctrl->elem_size;
		memset(ptr.p + idx, 0, ctrl->elem_size);
1475 1476 1477 1478 1479 1480 1481 1482
		break;
	}
}

static void std_log(const struct v4l2_ctrl *ctrl)
{
	union v4l2_ctrl_ptr ptr = ctrl->p_cur;

1483 1484 1485 1486 1487 1488 1489 1490
	if (ctrl->is_array) {
		unsigned i;

		for (i = 0; i < ctrl->nr_of_dims; i++)
			pr_cont("[%u]", ctrl->dims[i]);
		pr_cont(" ");
	}

1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_INTEGER:
		pr_cont("%d", *ptr.p_s32);
		break;
	case V4L2_CTRL_TYPE_BOOLEAN:
		pr_cont("%s", *ptr.p_s32 ? "true" : "false");
		break;
	case V4L2_CTRL_TYPE_MENU:
		pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
		break;
	case V4L2_CTRL_TYPE_INTEGER_MENU:
		pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
		break;
	case V4L2_CTRL_TYPE_BITMASK:
		pr_cont("0x%08x", *ptr.p_s32);
		break;
	case V4L2_CTRL_TYPE_INTEGER64:
		pr_cont("%lld", *ptr.p_s64);
		break;
	case V4L2_CTRL_TYPE_STRING:
		pr_cont("%s", ptr.p_char);
		break;
1513 1514 1515 1516 1517 1518
	case V4L2_CTRL_TYPE_U8:
		pr_cont("%u", (unsigned)*ptr.p_u8);
		break;
	case V4L2_CTRL_TYPE_U16:
		pr_cont("%u", (unsigned)*ptr.p_u16);
		break;
1519 1520 1521
	case V4L2_CTRL_TYPE_U32:
		pr_cont("%u", (unsigned)*ptr.p_u32);
		break;
1522 1523 1524 1525 1526 1527
	default:
		pr_cont("unknown type %d", ctrl->type);
		break;
	}
}

1528 1529 1530 1531 1532
/*
 * Round towards the closest legal value. Be careful when we are
 * close to the maximum range of the control type to prevent
 * wrap-arounds.
 */
1533 1534 1535
#define ROUND_TO_RANGE(val, offset_type, ctrl)			\
({								\
	offset_type offset;					\
1536
	if ((ctrl)->maximum >= 0 &&				\
1537
	    val >= (ctrl)->maximum - (s32)((ctrl)->step / 2))	\
1538 1539
		val = (ctrl)->maximum;				\
	else							\
1540
		val += (s32)((ctrl)->step / 2);			\
1541 1542 1543
	val = clamp_t(typeof(val), val,				\
		      (ctrl)->minimum, (ctrl)->maximum);	\
	offset = (val) - (ctrl)->minimum;			\
1544
	offset = (ctrl)->step * (offset / (u32)(ctrl)->step);	\
1545 1546 1547 1548 1549
	val = (ctrl)->minimum + offset;				\
	0;							\
})

/* Validate a new control */
1550
static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx,
1551 1552 1553
			union v4l2_ctrl_ptr ptr)
{
	size_t len;
1554 1555
	u64 offset;
	s64 val;
1556 1557 1558

	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_INTEGER:
1559
		return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
1560
	case V4L2_CTRL_TYPE_INTEGER64:
1561 1562 1563 1564 1565
		/*
		 * We can't use the ROUND_TO_RANGE define here due to
		 * the u64 divide that needs special care.
		 */
		val = ptr.p_s64[idx];
1566
		if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
1567 1568
			val = ctrl->maximum;
		else
1569
			val += (s64)(ctrl->step / 2);
1570 1571 1572 1573 1574
		val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
		offset = val - ctrl->minimum;
		do_div(offset, ctrl->step);
		ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
		return 0;
1575 1576 1577 1578
	case V4L2_CTRL_TYPE_U8:
		return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
	case V4L2_CTRL_TYPE_U16:
		return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
1579 1580
	case V4L2_CTRL_TYPE_U32:
		return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
1581 1582

	case V4L2_CTRL_TYPE_BOOLEAN:
1583
		ptr.p_s32[idx] = !!ptr.p_s32[idx];
1584 1585 1586 1587
		return 0;

	case V4L2_CTRL_TYPE_MENU:
	case V4L2_CTRL_TYPE_INTEGER_MENU:
1588
		if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
1589
			return -ERANGE;
1590
		if (ctrl->menu_skip_mask & (1 << ptr.p_s32[idx]))
1591 1592
			return -EINVAL;
		if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1593
		    ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
1594 1595 1596 1597
			return -EINVAL;
		return 0;

	case V4L2_CTRL_TYPE_BITMASK:
1598
		ptr.p_s32[idx] &= ctrl->maximum;
1599 1600 1601 1602
		return 0;

	case V4L2_CTRL_TYPE_BUTTON:
	case V4L2_CTRL_TYPE_CTRL_CLASS:
1603
		ptr.p_s32[idx] = 0;
1604 1605 1606
		return 0;

	case V4L2_CTRL_TYPE_STRING:
1607 1608
		idx *= ctrl->elem_size;
		len = strlen(ptr.p_char + idx);
1609 1610
		if (len < ctrl->minimum)
			return -ERANGE;
1611
		if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
			return -ERANGE;
		return 0;

	default:
		return -EINVAL;
	}
}

static const struct v4l2_ctrl_type_ops std_type_ops = {
	.equal = std_equal,
	.init = std_init,
	.log = std_log,
	.validate = std_validate,
};

1627 1628 1629 1630
/* Helper function: copy the given control value back to the caller */
static int ptr_to_user(struct v4l2_ext_control *c,
		       struct v4l2_ctrl *ctrl,
		       union v4l2_ctrl_ptr ptr)
1631 1632 1633
{
	u32 len;

1634
	if (ctrl->is_ptr && !ctrl->is_string)
1635 1636
		return copy_to_user(c->ptr, ptr.p, c->size) ?
		       -EFAULT : 0;
1637

1638 1639
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_STRING:
1640
		len = strlen(ptr.p_char);
1641
		if (c->size < len + 1) {
1642
			c->size = ctrl->elem_size;
1643 1644
			return -ENOSPC;
		}
1645
		return copy_to_user(c->string, ptr.p_char, len + 1) ?
1646
		       -EFAULT : 0;
1647
	case V4L2_CTRL_TYPE_INTEGER64:
1648
		c->value64 = *ptr.p_s64;
1649 1650
		break;
	default:
1651
		c->value = *ptr.p_s32;
1652 1653 1654 1655 1656
		break;
	}
	return 0;
}

1657 1658
/* Helper function: copy the current control value back to the caller */
static int cur_to_user(struct v4l2_ext_control *c,
1659
		       struct v4l2_ctrl *ctrl)
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
{
	return ptr_to_user(c, ctrl, ctrl->p_cur);
}

/* Helper function: copy the new control value back to the caller */
static int new_to_user(struct v4l2_ext_control *c,
		       struct v4l2_ctrl *ctrl)
{
	return ptr_to_user(c, ctrl, ctrl->p_new);
}

1671 1672 1673 1674 1675 1676 1677
/* Helper function: copy the request value back to the caller */
static int req_to_user(struct v4l2_ext_control *c,
		       struct v4l2_ctrl_ref *ref)
{
	return ptr_to_user(c, ref->ctrl, ref->p_req);
}

1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
/* Helper function: copy the initial control value back to the caller */
static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
{
	int idx;

	for (idx = 0; idx < ctrl->elems; idx++)
		ctrl->type_ops->init(ctrl, idx, ctrl->p_new);

	return ptr_to_user(c, ctrl, ctrl->p_new);
}

1689 1690 1691 1692
/* Helper function: copy the caller-provider value to the given control value */
static int user_to_ptr(struct v4l2_ext_control *c,
		       struct v4l2_ctrl *ctrl,
		       union v4l2_ctrl_ptr ptr)
1693 1694 1695 1696
{
	int ret;
	u32 size;

1697
	ctrl->is_new = 1;
1698 1699 1700
	if (ctrl->is_ptr && !ctrl->is_string) {
		unsigned idx;

1701
		ret = copy_from_user(ptr.p, c->ptr, c->size) ? -EFAULT : 0;
1702 1703 1704 1705 1706 1707
		if (ret || !ctrl->is_array)
			return ret;
		for (idx = c->size / ctrl->elem_size; idx < ctrl->elems; idx++)
			ctrl->type_ops->init(ctrl, idx, ptr);
		return 0;
	}
1708

1709 1710
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_INTEGER64:
1711
		*ptr.p_s64 = c->value64;
1712 1713 1714 1715 1716 1717 1718
		break;
	case V4L2_CTRL_TYPE_STRING:
		size = c->size;
		if (size == 0)
			return -ERANGE;
		if (size > ctrl->maximum + 1)
			size = ctrl->maximum + 1;
1719
		ret = copy_from_user(ptr.p_char, c->string, size) ? -EFAULT : 0;
1720
		if (!ret) {
1721
			char last = ptr.p_char[size - 1];
1722

1723
			ptr.p_char[size - 1] = 0;
1724 1725
			/* If the string was longer than ctrl->maximum,
			   then return an error. */
1726
			if (strlen(ptr.p_char) == ctrl->maximum && last)
1727 1728
				return -ERANGE;
		}
1729
		return ret;
1730
	default:
1731
		*ptr.p_s32 = c->value;
1732 1733 1734 1735 1736
		break;
	}
	return 0;
}

1737 1738
/* Helper function: copy the caller-provider value as the new control value */
static int user_to_new(struct v4l2_ext_control *c,
1739 1740
		       struct v4l2_ctrl *ctrl)
{
1741 1742
	return user_to_ptr(c, ctrl, ctrl->p_new);
}
1743

1744 1745 1746 1747 1748 1749
/* Copy the one value to another. */
static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
		       union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to)
{
	if (ctrl == NULL)
		return;
1750
	memcpy(to.p, from.p, ctrl->elems * ctrl->elem_size);
1751 1752 1753
}

/* Copy the new value to the current value. */
1754
static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
1755
{
1756
	bool changed;
1757

1758 1759
	if (ctrl == NULL)
		return;
1760 1761 1762 1763 1764

	/* has_changed is set by cluster_changed */
	changed = ctrl->has_changed;
	if (changed)
		ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur);
1765

1766 1767
	if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
		/* Note: CH_FLAGS is only set for auto clusters. */
1768 1769 1770
		ctrl->flags &=
			~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
		if (!is_cur_manual(ctrl->cluster[0])) {
1771
			ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1772 1773 1774
			if (ctrl->cluster[0]->has_volatiles)
				ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
		}
1775
		fh = NULL;
1776
	}
1777
	if (changed || ch_flags) {
1778 1779 1780 1781
		/* If a control was changed that was not one of the controls
		   modified by the application, then send the event to all. */
		if (!ctrl->is_new)
			fh = NULL;
1782
		send_event(fh, ctrl,
1783
			(changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
1784 1785
		if (ctrl->call_notify && changed && ctrl->handler->notify)
			ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
1786
	}
1787 1788 1789 1790 1791 1792 1793
}

/* Copy the current value to the new value */
static void cur_to_new(struct v4l2_ctrl *ctrl)
{
	if (ctrl == NULL)
		return;
1794
	ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new);
1795 1796
}

1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
/* Copy the new value to the request value */
static void new_to_req(struct v4l2_ctrl_ref *ref)
{
	if (!ref)
		return;
	ptr_to_ptr(ref->ctrl, ref->ctrl->p_new, ref->p_req);
	ref->req = ref;
}

/* Copy the request value to the new value */
static void req_to_new(struct v4l2_ctrl_ref *ref)
{
	if (!ref)
		return;
	if (ref->req)
		ptr_to_ptr(ref->ctrl, ref->req->p_req, ref->ctrl->p_new);
	else
		ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->ctrl->p_new);
}

1817 1818 1819 1820
/* Return non-zero if one or more of the controls in the cluster has a new
   value that differs from the current value. */
static int cluster_changed(struct v4l2_ctrl *master)
{
1821
	bool changed = false;
1822
	unsigned idx;
1823 1824
	int i;

1825
	for (i = 0; i < master->ncontrols; i++) {
1826
		struct v4l2_ctrl *ctrl = master->cluster[i];
1827
		bool ctrl_changed = false;
1828 1829 1830

		if (ctrl == NULL)
			continue;
1831 1832 1833 1834

		if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE)
			changed = ctrl_changed = true;

1835 1836 1837 1838 1839 1840 1841 1842 1843
		/*
		 * Set has_changed to false to avoid generating
		 * the event V4L2_EVENT_CTRL_CH_VALUE
		 */
		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
			ctrl->has_changed = false;
			continue;
		}

1844 1845
		for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++)
			ctrl_changed = !ctrl->type_ops->equal(ctrl, idx,
1846
				ctrl->p_cur, ctrl->p_new);
1847
		ctrl->has_changed = ctrl_changed;
1848
		changed |= ctrl->has_changed;
1849
	}
1850
	return changed;
1851 1852
}

1853 1854
/* Control range checking */
static int check_range(enum v4l2_ctrl_type type,
1855
		s64 min, s64 max, u64 step, s64 def)
1856 1857 1858 1859 1860 1861
{
	switch (type) {
	case V4L2_CTRL_TYPE_BOOLEAN:
		if (step != 1 || max > 1 || min < 0)
			return -ERANGE;
		/* fall through */
1862 1863
	case V4L2_CTRL_TYPE_U8:
	case V4L2_CTRL_TYPE_U16:
1864
	case V4L2_CTRL_TYPE_U32:
1865
	case V4L2_CTRL_TYPE_INTEGER:
1866 1867
	case V4L2_CTRL_TYPE_INTEGER64:
		if (step == 0 || min > max || def < min || def > max)
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
			return -ERANGE;
		return 0;
	case V4L2_CTRL_TYPE_BITMASK:
		if (step || min || !max || (def & ~max))
			return -ERANGE;
		return 0;
	case V4L2_CTRL_TYPE_MENU:
	case V4L2_CTRL_TYPE_INTEGER_MENU:
		if (min > max || def < min || def > max)
			return -ERANGE;
		/* Note: step == menu_skip_mask for menu controls.
		   So here we check if the default value is masked out. */
		if (step && ((1 << def) & step))
			return -EINVAL;
		return 0;
	case V4L2_CTRL_TYPE_STRING:
		if (min > max || min < 0 || step < 1 || def)
			return -ERANGE;
		return 0;
	default:
		return 0;
	}
}

1892
/* Validate a new control */
1893
static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
1894
{
1895 1896 1897
	unsigned idx;
	int err = 0;

1898 1899
	for (idx = 0; !err && idx < ctrl->elems; idx++)
		err = ctrl->type_ops->validate(ctrl, idx, p_new);
1900
	return err;
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
}

static inline u32 node2id(struct list_head *node)
{
	return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
}

/* Set the handler's error code if it wasn't set earlier already */
static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
{
	if (hdl->error == 0)
		hdl->error = err;
	return err;
}

/* Initialize the handler */
1917 1918 1919
int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
				 unsigned nr_of_controls_hint,
				 struct lock_class_key *key, const char *name)
1920
{
1921
	mutex_init(&hdl->_lock);
1922
	hdl->lock = &hdl->_lock;
1923
	lockdep_set_class_and_name(hdl->lock, key, name);
1924 1925
	INIT_LIST_HEAD(&hdl->ctrls);
	INIT_LIST_HEAD(&hdl->ctrl_refs);
1926 1927 1928
	INIT_LIST_HEAD(&hdl->requests);
	INIT_LIST_HEAD(&hdl->requests_queued);
	hdl->request_is_queued = false;
1929
	hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
1930 1931 1932
	hdl->buckets = kvmalloc_array(hdl->nr_of_buckets,
				      sizeof(hdl->buckets[0]),
				      GFP_KERNEL | __GFP_ZERO);
1933
	hdl->error = hdl->buckets ? 0 : -ENOMEM;
1934
	media_request_object_init(&hdl->req_obj);
1935 1936
	return hdl->error;
}
1937
EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
1938 1939 1940 1941 1942 1943

/* Free all controls and control refs */
void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
{
	struct v4l2_ctrl_ref *ref, *next_ref;
	struct v4l2_ctrl *ctrl, *next_ctrl;
1944
	struct v4l2_subscribed_event *sev, *next_sev;
1945 1946 1947 1948

	if (hdl == NULL || hdl->buckets == NULL)
		return;

1949 1950 1951 1952 1953 1954 1955 1956
	if (!hdl->req_obj.req && !list_empty(&hdl->requests)) {
		struct v4l2_ctrl_handler *req, *next_req;

		list_for_each_entry_safe(req, next_req, &hdl->requests, requests) {
			media_request_object_unbind(&req->req_obj);
			media_request_object_put(&req->req_obj);
		}
	}
1957
	mutex_lock(hdl->lock);
1958 1959 1960 1961 1962 1963 1964 1965
	/* Free all nodes */
	list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
		list_del(&ref->node);
		kfree(ref);
	}
	/* Free all controls owned by the handler */
	list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
		list_del(&ctrl->node);
1966 1967
		list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
			list_del(&sev->node);
1968
		kvfree(ctrl);
1969
	}
1970
	kvfree(hdl->buckets);
1971 1972 1973
	hdl->buckets = NULL;
	hdl->cached = NULL;
	hdl->error = 0;
1974
	mutex_unlock(hdl->lock);
1975
	mutex_destroy(&hdl->_lock);
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
}
EXPORT_SYMBOL(v4l2_ctrl_handler_free);

/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
   be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
   with applications that do not use the NEXT_CTRL flag.

   We just find the n-th private user control. It's O(N), but that should not
   be an issue in this particular case. */
static struct v4l2_ctrl_ref *find_private_ref(
		struct v4l2_ctrl_handler *hdl, u32 id)
{
	struct v4l2_ctrl_ref *ref;

	id -= V4L2_CID_PRIVATE_BASE;
	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
		/* Search for private user controls that are compatible with
		   VIDIOC_G/S_CTRL. */
1994
		if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1995
		    V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1996
			if (!ref->ctrl->is_int)
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
				continue;
			if (id == 0)
				return ref;
			id--;
		}
	}
	return NULL;
}

/* Find a control with the given ID. */
static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
{
	struct v4l2_ctrl_ref *ref;
	int bucket;

	id &= V4L2_CTRL_ID_MASK;

	/* Old-style private controls need special handling */
	if (id >= V4L2_CID_PRIVATE_BASE)
		return find_private_ref(hdl, id);
	bucket = id % hdl->nr_of_buckets;

	/* Simple optimization: cache the last control found */
	if (hdl->cached && hdl->cached->ctrl->id == id)
		return hdl->cached;

	/* Not in cache, search the hash */
	ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
	while (ref && ref->ctrl->id != id)
		ref = ref->next;

	if (ref)
		hdl->cached = ref; /* cache it! */
	return ref;
}

/* Find a control with the given ID. Take the handler's lock first. */
static struct v4l2_ctrl_ref *find_ref_lock(
		struct v4l2_ctrl_handler *hdl, u32 id)
{
	struct v4l2_ctrl_ref *ref = NULL;

	if (hdl) {
2040
		mutex_lock(hdl->lock);
2041
		ref = find_ref(hdl, id);
2042
		mutex_unlock(hdl->lock);
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
	}
	return ref;
}

/* Find a control with the given ID. */
struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
{
	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);

	return ref ? ref->ctrl : NULL;
}
EXPORT_SYMBOL(v4l2_ctrl_find);

/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
2058
			   struct v4l2_ctrl *ctrl,
2059 2060
			   struct v4l2_ctrl_ref **ctrl_ref,
			   bool from_other_dev, bool allocate_req)
2061 2062 2063 2064
{
	struct v4l2_ctrl_ref *ref;
	struct v4l2_ctrl_ref *new_ref;
	u32 id = ctrl->id;
2065
	u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
2066
	int bucket = id % hdl->nr_of_buckets;	/* which bucket to use */
2067 2068 2069 2070
	unsigned int size_extra_req = 0;

	if (ctrl_ref)
		*ctrl_ref = NULL;
2071

2072 2073 2074 2075 2076 2077
	/*
	 * Automatically add the control class if it is not yet present and
	 * the new control is not a compound control.
	 */
	if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
	    id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
2078 2079 2080 2081 2082 2083
		if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
			return hdl->error;

	if (hdl->error)
		return hdl->error;

2084 2085 2086
	if (allocate_req)
		size_extra_req = ctrl->elems * ctrl->elem_size;
	new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
2087 2088 2089
	if (!new_ref)
		return handler_set_err(hdl, -ENOMEM);
	new_ref->ctrl = ctrl;
2090
	new_ref->from_other_dev = from_other_dev;
2091 2092 2093
	if (size_extra_req)
		new_ref->p_req.p = &new_ref[1];

2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
	if (ctrl->handler == hdl) {
		/* By default each control starts in a cluster of its own.
		   new_ref->ctrl is basically a cluster array with one
		   element, so that's perfect to use as the cluster pointer.
		   But only do this for the handler that owns the control. */
		ctrl->cluster = &new_ref->ctrl;
		ctrl->ncontrols = 1;
	}

	INIT_LIST_HEAD(&new_ref->node);

2105
	mutex_lock(hdl->lock);
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

	/* Add immediately at the end of the list if the list is empty, or if
	   the last element in the list has a lower ID.
	   This ensures that when elements are added in ascending order the
	   insertion is an O(1) operation. */
	if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
		list_add_tail(&new_ref->node, &hdl->ctrl_refs);
		goto insert_in_hash;
	}

	/* Find insert position in sorted list */
	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
		if (ref->ctrl->id < id)
			continue;
		/* Don't add duplicates */
		if (ref->ctrl->id == id) {
			kfree(new_ref);
			goto unlock;
		}
		list_add(&new_ref->node, ref->node.prev);
		break;
	}

insert_in_hash:
	/* Insert the control node in the hash */
	new_ref->next = hdl->buckets[bucket];
	hdl->buckets[bucket] = new_ref;
2133 2134
	if (ctrl_ref)
		*ctrl_ref = new_ref;
2135 2136

unlock:
2137
	mutex_unlock(hdl->lock);
2138 2139 2140 2141 2142 2143
	return 0;
}

/* Add a new control */
static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
			const struct v4l2_ctrl_ops *ops,
2144
			const struct v4l2_ctrl_type_ops *type_ops,
2145
			u32 id, const char *name, enum v4l2_ctrl_type type,
2146
			s64 min, s64 max, u64 step, s64 def,
2147
			const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
2148 2149
			u32 flags, const char * const *qmenu,
			const s64 *qmenu_int, void *priv)
2150 2151
{
	struct v4l2_ctrl *ctrl;
2152
	unsigned sz_extra;
2153 2154
	unsigned nr_of_dims = 0;
	unsigned elems = 1;
2155 2156
	bool is_array;
	unsigned tot_ctrl_size;
2157
	unsigned idx;
2158
	void *data;
2159
	int err;
2160 2161 2162 2163

	if (hdl->error)
		return NULL;

2164 2165 2166 2167 2168 2169
	while (dims && dims[nr_of_dims]) {
		elems *= dims[nr_of_dims];
		nr_of_dims++;
		if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
			break;
	}
2170
	is_array = nr_of_dims > 0;
2171

2172 2173 2174
	/* Prefill elem_size for all types handled by std_type_ops */
	switch (type) {
	case V4L2_CTRL_TYPE_INTEGER64:
2175
		elem_size = sizeof(s64);
2176 2177
		break;
	case V4L2_CTRL_TYPE_STRING:
2178
		elem_size = max + 1;
2179 2180 2181 2182 2183 2184 2185
		break;
	case V4L2_CTRL_TYPE_U8:
		elem_size = sizeof(u8);
		break;
	case V4L2_CTRL_TYPE_U16:
		elem_size = sizeof(u16);
		break;
2186 2187 2188
	case V4L2_CTRL_TYPE_U32:
		elem_size = sizeof(u32);
		break;
2189 2190 2191 2192 2193
	default:
		if (type < V4L2_CTRL_COMPOUND_TYPES)
			elem_size = sizeof(s32);
		break;
	}
2194
	tot_ctrl_size = elem_size * elems;
2195

2196
	/* Sanity checks */
2197 2198
	if (id == 0 || name == NULL || !elem_size ||
	    id >= V4L2_CID_PRIVATE_BASE ||
2199
	    (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
2200
	    (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
2201 2202 2203
		handler_set_err(hdl, -ERANGE);
		return NULL;
	}
2204 2205 2206
	err = check_range(type, min, max, step, def);
	if (err) {
		handler_set_err(hdl, err);
2207 2208
		return NULL;
	}
2209 2210 2211 2212 2213 2214
	if (is_array &&
	    (type == V4L2_CTRL_TYPE_BUTTON ||
	     type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
		handler_set_err(hdl, -EINVAL);
		return NULL;
	}
2215

2216
	sz_extra = 0;
2217
	if (type == V4L2_CTRL_TYPE_BUTTON)
2218 2219
		flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
			V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
2220 2221
	else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
		flags |= V4L2_CTRL_FLAG_READ_ONLY;
2222 2223
	else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
		 type == V4L2_CTRL_TYPE_STRING ||
2224 2225 2226
		 type >= V4L2_CTRL_COMPOUND_TYPES ||
		 is_array)
		sz_extra += 2 * tot_ctrl_size;
2227

2228
	ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
2229 2230 2231 2232 2233 2234
	if (ctrl == NULL) {
		handler_set_err(hdl, -ENOMEM);
		return NULL;
	}

	INIT_LIST_HEAD(&ctrl->node);
2235
	INIT_LIST_HEAD(&ctrl->ev_subs);
2236 2237
	ctrl->handler = hdl;
	ctrl->ops = ops;
2238
	ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
2239 2240 2241 2242 2243 2244 2245
	ctrl->id = id;
	ctrl->name = name;
	ctrl->type = type;
	ctrl->flags = flags;
	ctrl->minimum = min;
	ctrl->maximum = max;
	ctrl->step = step;
2246
	ctrl->default_value = def;
2247 2248
	ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
	ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
2249
	ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
2250
	ctrl->is_array = is_array;
2251 2252 2253 2254
	ctrl->elems = elems;
	ctrl->nr_of_dims = nr_of_dims;
	if (nr_of_dims)
		memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
2255
	ctrl->elem_size = elem_size;
2256 2257 2258 2259
	if (type == V4L2_CTRL_TYPE_MENU)
		ctrl->qmenu = qmenu;
	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
		ctrl->qmenu_int = qmenu_int;
2260
	ctrl->priv = priv;
2261
	ctrl->cur.val = ctrl->val = def;
2262
	data = &ctrl[1];
2263

2264 2265
	if (!ctrl->is_int) {
		ctrl->p_new.p = data;
2266
		ctrl->p_cur.p = data + tot_ctrl_size;
2267 2268 2269
	} else {
		ctrl->p_new.p = &ctrl->val;
		ctrl->p_cur.p = &ctrl->cur.val;
2270
	}
2271 2272 2273 2274
	for (idx = 0; idx < elems; idx++) {
		ctrl->type_ops->init(ctrl, idx, ctrl->p_cur);
		ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
	}
2275

2276
	if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
2277
		kvfree(ctrl);
2278 2279
		return NULL;
	}
2280
	mutex_lock(hdl->lock);
2281
	list_add_tail(&ctrl->node, &hdl->ctrls);
2282
	mutex_unlock(hdl->lock);
2283 2284 2285 2286 2287 2288 2289 2290 2291
	return ctrl;
}

struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
			const struct v4l2_ctrl_config *cfg, void *priv)
{
	bool is_menu;
	struct v4l2_ctrl *ctrl;
	const char *name = cfg->name;
2292
	const char * const *qmenu = cfg->qmenu;
2293
	const s64 *qmenu_int = cfg->qmenu_int;
2294 2295
	enum v4l2_ctrl_type type = cfg->type;
	u32 flags = cfg->flags;
2296 2297 2298 2299
	s64 min = cfg->min;
	s64 max = cfg->max;
	u64 step = cfg->step;
	s64 def = cfg->def;
2300 2301 2302 2303 2304

	if (name == NULL)
		v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
								&def, &flags);

2305 2306
	is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU ||
		   cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU);
2307 2308 2309 2310
	if (is_menu)
		WARN_ON(step);
	else
		WARN_ON(cfg->menu_skip_mask);
2311
	if (cfg->type == V4L2_CTRL_TYPE_MENU && qmenu == NULL)
2312
		qmenu = v4l2_ctrl_get_menu(cfg->id);
2313 2314 2315 2316 2317
	else if (cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU &&
		 qmenu_int == NULL) {
		handler_set_err(hdl, -EINVAL);
		return NULL;
	}
2318

2319
	ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
2320
			type, min, max,
2321 2322
			is_menu ? cfg->menu_skip_mask : step, def,
			cfg->dims, cfg->elem_size,
2323
			flags, qmenu, qmenu_int, priv);
2324
	if (ctrl)
2325 2326 2327 2328 2329 2330 2331 2332
		ctrl->is_private = cfg->is_private;
	return ctrl;
}
EXPORT_SYMBOL(v4l2_ctrl_new_custom);

/* Helper function for standard non-menu controls */
struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
			const struct v4l2_ctrl_ops *ops,
2333
			u32 id, s64 min, s64 max, u64 step, s64 def)
2334 2335 2336 2337 2338 2339
{
	const char *name;
	enum v4l2_ctrl_type type;
	u32 flags;

	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2340 2341 2342
	if (type == V4L2_CTRL_TYPE_MENU ||
	    type == V4L2_CTRL_TYPE_INTEGER_MENU ||
	    type >= V4L2_CTRL_COMPOUND_TYPES) {
2343 2344 2345
		handler_set_err(hdl, -EINVAL);
		return NULL;
	}
2346
	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2347
			     min, max, step, def, NULL, 0,
2348
			     flags, NULL, NULL, NULL);
2349 2350 2351 2352 2353 2354
}
EXPORT_SYMBOL(v4l2_ctrl_new_std);

/* Helper function for standard menu controls */
struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
			const struct v4l2_ctrl_ops *ops,
2355
			u32 id, u8 _max, u64 mask, u8 _def)
2356
{
2357 2358
	const char * const *qmenu = NULL;
	const s64 *qmenu_int = NULL;
2359
	unsigned int qmenu_int_len = 0;
2360 2361
	const char *name;
	enum v4l2_ctrl_type type;
2362 2363 2364 2365
	s64 min;
	s64 max = _max;
	s64 def = _def;
	u64 step;
2366 2367 2368
	u32 flags;

	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
2369 2370 2371 2372 2373 2374 2375

	if (type == V4L2_CTRL_TYPE_MENU)
		qmenu = v4l2_ctrl_get_menu(id);
	else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
		qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);

	if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) {
2376 2377 2378
		handler_set_err(hdl, -EINVAL);
		return NULL;
	}
2379
	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2380
			     0, max, mask, def, NULL, 0,
2381
			     flags, qmenu, qmenu_int, NULL);
2382 2383 2384
}
EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);

2385 2386
/* Helper function for standard menu controls with driver defined menu */
struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
2387 2388
			const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
			u64 mask, u8 _def, const char * const *qmenu)
2389 2390 2391 2392
{
	enum v4l2_ctrl_type type;
	const char *name;
	u32 flags;
2393 2394 2395 2396
	u64 step;
	s64 min;
	s64 max = _max;
	s64 def = _def;
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410

	/* v4l2_ctrl_new_std_menu_items() should only be called for
	 * standard controls without a standard menu.
	 */
	if (v4l2_ctrl_get_menu(id)) {
		handler_set_err(hdl, -EINVAL);
		return NULL;
	}

	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
	if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
		handler_set_err(hdl, -EINVAL);
		return NULL;
	}
2411
	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2412 2413
			     0, max, mask, def, NULL, 0,
			     flags, qmenu, NULL, NULL);
2414 2415 2416 2417

}
EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);

2418 2419 2420
/* Helper function for standard integer menu controls */
struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
			const struct v4l2_ctrl_ops *ops,
2421
			u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
2422 2423 2424
{
	const char *name;
	enum v4l2_ctrl_type type;
2425 2426 2427 2428
	s64 min;
	u64 step;
	s64 max = _max;
	s64 def = _def;
2429 2430 2431 2432 2433 2434 2435
	u32 flags;

	v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
	if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
		handler_set_err(hdl, -EINVAL);
		return NULL;
	}
2436
	return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
2437
			     0, max, 0, def, NULL, 0,
2438
			     flags, NULL, qmenu_int, NULL);
2439 2440 2441
}
EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);

2442 2443
/* Add the controls from another handler to our own. */
int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
2444
			  struct v4l2_ctrl_handler *add,
2445 2446
			  bool (*filter)(const struct v4l2_ctrl *ctrl),
			  bool from_other_dev)
2447
{
2448
	struct v4l2_ctrl_ref *ref;
2449 2450 2451 2452 2453 2454 2455
	int ret = 0;

	/* Do nothing if either handler is NULL or if they are the same */
	if (!hdl || !add || hdl == add)
		return 0;
	if (hdl->error)
		return hdl->error;
2456
	mutex_lock(add->lock);
2457 2458 2459
	list_for_each_entry(ref, &add->ctrl_refs, node) {
		struct v4l2_ctrl *ctrl = ref->ctrl;

2460 2461 2462
		/* Skip handler-private controls. */
		if (ctrl->is_private)
			continue;
2463 2464 2465
		/* And control classes */
		if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
			continue;
2466 2467 2468
		/* Filter any unwanted controls */
		if (filter && !filter(ctrl))
			continue;
2469
		ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
2470 2471 2472
		if (ret)
			break;
	}
2473
	mutex_unlock(add->lock);
2474 2475 2476 2477
	return ret;
}
EXPORT_SYMBOL(v4l2_ctrl_add_handler);

2478 2479
bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
{
2480
	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
2481
		return true;
2482
	if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
2483
		return true;
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
	case V4L2_CID_AUDIO_VOLUME:
	case V4L2_CID_AUDIO_BALANCE:
	case V4L2_CID_AUDIO_BASS:
	case V4L2_CID_AUDIO_TREBLE:
	case V4L2_CID_AUDIO_LOUDNESS:
		return true;
	default:
		break;
	}
	return false;
}
EXPORT_SYMBOL(v4l2_ctrl_radio_filter);

2499 2500 2501
/* Cluster controls */
void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
{
2502
	bool has_volatiles = false;
2503 2504 2505
	int i;

	/* The first control is the master control and it must not be NULL */
2506 2507
	if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
		return;
2508 2509 2510 2511 2512

	for (i = 0; i < ncontrols; i++) {
		if (controls[i]) {
			controls[i]->cluster = controls;
			controls[i]->ncontrols = ncontrols;
2513 2514
			if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
				has_volatiles = true;
2515 2516
		}
	}
2517
	controls[0]->has_volatiles = has_volatiles;
2518 2519 2520
}
EXPORT_SYMBOL(v4l2_ctrl_cluster);

2521 2522 2523 2524
void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
			    u8 manual_val, bool set_volatile)
{
	struct v4l2_ctrl *master = controls[0];
2525
	u32 flag = 0;
2526 2527 2528 2529
	int i;

	v4l2_ctrl_cluster(ncontrols, controls);
	WARN_ON(ncontrols <= 1);
2530
	WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
2531
	WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
2532
	master->is_auto = true;
2533
	master->has_volatiles = set_volatile;
2534 2535
	master->manual_mode_value = manual_val;
	master->flags |= V4L2_CTRL_FLAG_UPDATE;
2536 2537 2538 2539

	if (!is_cur_manual(master))
		flag = V4L2_CTRL_FLAG_INACTIVE |
			(set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
2540 2541

	for (i = 1; i < ncontrols; i++)
2542
		if (controls[i])
2543 2544 2545 2546
			controls[i]->flags |= flag;
}
EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);

2547 2548 2549
/* Activate/deactivate a control. */
void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
{
2550 2551 2552 2553
	/* invert since the actual flag is called 'inactive' */
	bool inactive = !active;
	bool old;

2554 2555 2556
	if (ctrl == NULL)
		return;

2557
	if (inactive)
2558
		/* set V4L2_CTRL_FLAG_INACTIVE */
2559
		old = test_and_set_bit(4, &ctrl->flags);
2560 2561
	else
		/* clear V4L2_CTRL_FLAG_INACTIVE */
2562 2563 2564
		old = test_and_clear_bit(4, &ctrl->flags);
	if (old != inactive)
		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
}
EXPORT_SYMBOL(v4l2_ctrl_activate);

/* Grab/ungrab a control.
   Typically used when streaming starts and you want to grab controls,
   preventing the user from changing them.

   Just call this and the framework will block any attempts to change
   these controls. */
void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
{
2576 2577
	bool old;

2578 2579 2580
	if (ctrl == NULL)
		return;

2581
	v4l2_ctrl_lock(ctrl);
2582 2583
	if (grabbed)
		/* set V4L2_CTRL_FLAG_GRABBED */
2584
		old = test_and_set_bit(1, &ctrl->flags);
2585 2586
	else
		/* clear V4L2_CTRL_FLAG_GRABBED */
2587 2588 2589 2590
		old = test_and_clear_bit(1, &ctrl->flags);
	if (old != grabbed)
		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
	v4l2_ctrl_unlock(ctrl);
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
}
EXPORT_SYMBOL(v4l2_ctrl_grab);

/* Log the control name and value */
static void log_ctrl(const struct v4l2_ctrl *ctrl,
		     const char *prefix, const char *colon)
{
	if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
		return;
	if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
		return;

2603
	pr_info("%s%s%s: ", prefix, colon, ctrl->name);
2604

2605 2606
	ctrl->type_ops->log(ctrl);

2607 2608 2609 2610
	if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
			   V4L2_CTRL_FLAG_GRABBED |
			   V4L2_CTRL_FLAG_VOLATILE)) {
		if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
2611
			pr_cont(" inactive");
2612
		if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
2613
			pr_cont(" grabbed");
2614
		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
2615
			pr_cont(" volatile");
2616
	}
2617
	pr_cont("\n");
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
}

/* Log all controls owned by the handler */
void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
				  const char *prefix)
{
	struct v4l2_ctrl *ctrl;
	const char *colon = "";
	int len;

	if (hdl == NULL)
		return;
	if (prefix == NULL)
		prefix = "";
	len = strlen(prefix);
	if (len && prefix[len - 1] != ' ')
		colon = ": ";
2635
	mutex_lock(hdl->lock);
2636 2637 2638
	list_for_each_entry(ctrl, &hdl->ctrls, node)
		if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
			log_ctrl(ctrl, prefix, colon);
2639
	mutex_unlock(hdl->lock);
2640 2641 2642
}
EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);

2643 2644 2645 2646 2647 2648 2649
int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
{
	v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
	return 0;
}
EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);

2650
/* Call s_ctrl for all controls owned by the handler */
2651
int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
2652 2653 2654 2655 2656 2657
{
	struct v4l2_ctrl *ctrl;
	int ret = 0;

	if (hdl == NULL)
		return 0;
2658 2659 2660

	lockdep_assert_held(hdl->lock);

2661 2662 2663 2664 2665 2666 2667 2668
	list_for_each_entry(ctrl, &hdl->ctrls, node)
		ctrl->done = false;

	list_for_each_entry(ctrl, &hdl->ctrls, node) {
		struct v4l2_ctrl *master = ctrl->cluster[0];
		int i;

		/* Skip if this control was already handled by a cluster. */
2669 2670 2671
		/* Skip button controls and read-only controls. */
		if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
2672 2673
			continue;

2674 2675 2676 2677
		for (i = 0; i < master->ncontrols; i++) {
			if (master->cluster[i]) {
				cur_to_new(master->cluster[i]);
				master->cluster[i]->is_new = 1;
2678
				master->cluster[i]->done = true;
2679 2680
			}
		}
2681
		ret = call_op(master, s_ctrl);
2682 2683 2684
		if (ret)
			break;
	}
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698

	return ret;
}
EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);

int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
{
	int ret;

	if (hdl == NULL)
		return 0;

	mutex_lock(hdl->lock);
	ret = __v4l2_ctrl_handler_setup(hdl);
2699
	mutex_unlock(hdl->lock);
2700

2701 2702 2703 2704
	return ret;
}
EXPORT_SYMBOL(v4l2_ctrl_handler_setup);

2705 2706
/* Implement VIDIOC_QUERY_EXT_CTRL */
int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
2707
{
2708
	const unsigned next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
2709 2710 2711 2712 2713 2714 2715
	u32 id = qc->id & V4L2_CTRL_ID_MASK;
	struct v4l2_ctrl_ref *ref;
	struct v4l2_ctrl *ctrl;

	if (hdl == NULL)
		return -EINVAL;

2716
	mutex_lock(hdl->lock);
2717 2718 2719 2720

	/* Try to find it */
	ref = find_ref(hdl, id);

2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
	if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
		bool is_compound;
		/* Match any control that is not hidden */
		unsigned mask = 1;
		bool match = false;

		if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
			/* Match any hidden control */
			match = true;
		} else if ((qc->id & next_flags) == next_flags) {
			/* Match any control, compound or not */
			mask = 0;
		}

2735 2736 2737 2738 2739 2740 2741
		/* Find the next control with ID > qc->id */

		/* Did we reach the end of the control list? */
		if (id >= node2id(hdl->ctrl_refs.prev)) {
			ref = NULL; /* Yes, so there is no next control */
		} else if (ref) {
			/* We found a control with the given ID, so just get
2742 2743
			   the next valid one in the list. */
			list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
2744
				is_compound = ref->ctrl->is_array ||
2745 2746 2747 2748 2749 2750 2751
					ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
				if (id < ref->ctrl->id &&
				    (is_compound & mask) == match)
					break;
			}
			if (&ref->node == &hdl->ctrl_refs)
				ref = NULL;
2752 2753 2754 2755 2756
		} else {
			/* No control with the given ID exists, so start
			   searching for the next largest ID. We know there
			   is one, otherwise the first 'if' above would have
			   been true. */
2757
			list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2758
				is_compound = ref->ctrl->is_array ||
2759 2760 2761
					ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
				if (id < ref->ctrl->id &&
				    (is_compound & mask) == match)
2762
					break;
2763 2764 2765
			}
			if (&ref->node == &hdl->ctrl_refs)
				ref = NULL;
2766 2767
		}
	}
2768
	mutex_unlock(hdl->lock);
2769

2770 2771 2772 2773 2774
	if (!ref)
		return -EINVAL;

	ctrl = ref->ctrl;
	memset(qc, 0, sizeof(*qc));
2775 2776 2777 2778
	if (id >= V4L2_CID_PRIVATE_BASE)
		qc->id = id;
	else
		qc->id = ctrl->id;
2779
	strlcpy(qc->name, ctrl->name, sizeof(qc->name));
2780
	qc->flags = user_flags(ctrl);
2781 2782
	qc->type = ctrl->type;
	qc->elem_size = ctrl->elem_size;
2783 2784 2785
	qc->elems = ctrl->elems;
	qc->nr_of_dims = ctrl->nr_of_dims;
	memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
2786 2787 2788
	qc->minimum = ctrl->minimum;
	qc->maximum = ctrl->maximum;
	qc->default_value = ctrl->default_value;
2789 2790
	if (ctrl->type == V4L2_CTRL_TYPE_MENU
	    || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
2791 2792 2793
		qc->step = 1;
	else
		qc->step = ctrl->step;
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
	return 0;
}
EXPORT_SYMBOL(v4l2_query_ext_ctrl);

/* Implement VIDIOC_QUERYCTRL */
int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
{
	struct v4l2_query_ext_ctrl qec = { qc->id };
	int rc;

	rc = v4l2_query_ext_ctrl(hdl, &qec);
	if (rc)
		return rc;

	qc->id = qec.id;
	qc->type = qec.type;
	qc->flags = qec.flags;
	strlcpy(qc->name, qec.name, sizeof(qc->name));
	switch (qc->type) {
	case V4L2_CTRL_TYPE_INTEGER:
	case V4L2_CTRL_TYPE_BOOLEAN:
	case V4L2_CTRL_TYPE_MENU:
	case V4L2_CTRL_TYPE_INTEGER_MENU:
	case V4L2_CTRL_TYPE_STRING:
	case V4L2_CTRL_TYPE_BITMASK:
		qc->minimum = qec.minimum;
		qc->maximum = qec.maximum;
		qc->step = qec.step;
		qc->default_value = qec.default_value;
		break;
	default:
		qc->minimum = 0;
		qc->maximum = 0;
		qc->step = 0;
		qc->default_value = 0;
		break;
	}
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
	return 0;
}
EXPORT_SYMBOL(v4l2_queryctrl);

/* Implement VIDIOC_QUERYMENU */
int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
{
	struct v4l2_ctrl *ctrl;
	u32 i = qm->index;

	ctrl = v4l2_ctrl_find(hdl, qm->id);
	if (!ctrl)
		return -EINVAL;

	qm->reserved = 0;
	/* Sanity checks */
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_MENU:
		if (ctrl->qmenu == NULL)
			return -EINVAL;
		break;
	case V4L2_CTRL_TYPE_INTEGER_MENU:
		if (ctrl->qmenu_int == NULL)
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	if (i < ctrl->minimum || i > ctrl->maximum)
2861
		return -EINVAL;
2862

2863 2864 2865 2866
	/* Use mask to see if this menu item should be skipped */
	if (ctrl->menu_skip_mask & (1 << i))
		return -EINVAL;
	/* Empty menu items should also be skipped */
2867 2868 2869 2870 2871 2872 2873
	if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
		if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
			return -EINVAL;
		strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
	} else {
		qm->value = ctrl->qmenu_int[i];
	}
2874 2875 2876 2877
	return 0;
}
EXPORT_SYMBOL(v4l2_querymenu);

2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978
static int v4l2_ctrl_request_clone(struct v4l2_ctrl_handler *hdl,
				   const struct v4l2_ctrl_handler *from)
{
	struct v4l2_ctrl_ref *ref;
	int err;

	if (WARN_ON(!hdl || hdl == from))
		return -EINVAL;

	if (hdl->error)
		return hdl->error;

	WARN_ON(hdl->lock != &hdl->_lock);

	mutex_lock(from->lock);
	list_for_each_entry(ref, &from->ctrl_refs, node) {
		struct v4l2_ctrl *ctrl = ref->ctrl;
		struct v4l2_ctrl_ref *new_ref;

		/* Skip refs inherited from other devices */
		if (ref->from_other_dev)
			continue;
		/* And buttons */
		if (ctrl->type == V4L2_CTRL_TYPE_BUTTON)
			continue;
		err = handler_new_ref(hdl, ctrl, &new_ref, false, true);
		if (err)
			break;
	}
	mutex_unlock(from->lock);
	return err;
}

static void v4l2_ctrl_request_queue(struct media_request_object *obj)
{
	struct v4l2_ctrl_handler *hdl =
		container_of(obj, struct v4l2_ctrl_handler, req_obj);
	struct v4l2_ctrl_handler *main_hdl = obj->priv;
	struct v4l2_ctrl_handler *prev_hdl = NULL;
	struct v4l2_ctrl_ref *ref_ctrl, *ref_ctrl_prev = NULL;

	if (list_empty(&main_hdl->requests_queued))
		goto queue;

	prev_hdl = list_last_entry(&main_hdl->requests_queued,
				   struct v4l2_ctrl_handler, requests_queued);
	/*
	 * Note: prev_hdl and hdl must contain the same list of control
	 * references, so if any differences are detected then that is a
	 * driver bug and the WARN_ON is triggered.
	 */
	mutex_lock(prev_hdl->lock);
	ref_ctrl_prev = list_first_entry(&prev_hdl->ctrl_refs,
					 struct v4l2_ctrl_ref, node);
	list_for_each_entry(ref_ctrl, &hdl->ctrl_refs, node) {
		if (ref_ctrl->req)
			continue;
		while (ref_ctrl_prev->ctrl->id < ref_ctrl->ctrl->id) {
			/* Should never happen, but just in case... */
			if (list_is_last(&ref_ctrl_prev->node,
					 &prev_hdl->ctrl_refs))
				break;
			ref_ctrl_prev = list_next_entry(ref_ctrl_prev, node);
		}
		if (WARN_ON(ref_ctrl_prev->ctrl->id != ref_ctrl->ctrl->id))
			break;
		ref_ctrl->req = ref_ctrl_prev->req;
	}
	mutex_unlock(prev_hdl->lock);
queue:
	list_add_tail(&hdl->requests_queued, &main_hdl->requests_queued);
	hdl->request_is_queued = true;
}

static void v4l2_ctrl_request_unbind(struct media_request_object *obj)
{
	struct v4l2_ctrl_handler *hdl =
		container_of(obj, struct v4l2_ctrl_handler, req_obj);

	list_del_init(&hdl->requests);
	if (hdl->request_is_queued) {
		list_del_init(&hdl->requests_queued);
		hdl->request_is_queued = false;
	}
}

static void v4l2_ctrl_request_release(struct media_request_object *obj)
{
	struct v4l2_ctrl_handler *hdl =
		container_of(obj, struct v4l2_ctrl_handler, req_obj);

	v4l2_ctrl_handler_free(hdl);
	kfree(hdl);
}

static const struct media_request_object_ops req_ops = {
	.queue = v4l2_ctrl_request_queue,
	.unbind = v4l2_ctrl_request_unbind,
	.release = v4l2_ctrl_request_release,
};

2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(struct media_request *req,
					struct v4l2_ctrl_handler *parent)
{
	struct media_request_object *obj;

	if (WARN_ON(req->state != MEDIA_REQUEST_STATE_VALIDATING &&
		    req->state != MEDIA_REQUEST_STATE_QUEUED))
		return NULL;

	obj = media_request_object_find(req, &req_ops, parent);
	if (obj)
		return container_of(obj, struct v4l2_ctrl_handler, req_obj);
	return NULL;
}
EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_find);

struct v4l2_ctrl *
v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
{
	struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);

	return (ref && ref->req == ref) ? ref->ctrl : NULL;
}
EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_ctrl_find);

3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
static int v4l2_ctrl_request_bind(struct media_request *req,
			   struct v4l2_ctrl_handler *hdl,
			   struct v4l2_ctrl_handler *from)
{
	int ret;

	ret = v4l2_ctrl_request_clone(hdl, from);

	if (!ret) {
		ret = media_request_object_bind(req, &req_ops,
						from, false, &hdl->req_obj);
		if (!ret)
			list_add_tail(&hdl->requests, &from->requests);
	}
	return ret;
}
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063

/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:

   It is not a fully atomic operation, just best-effort only. After all, if
   multiple controls have to be set through multiple i2c writes (for example)
   then some initial writes may succeed while others fail. Thus leaving the
   system in an inconsistent state. The question is how much effort you are
   willing to spend on trying to make something atomic that really isn't.

   From the point of view of an application the main requirement is that
   when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
   error should be returned without actually affecting any controls.

   If all the values are correct, then it is acceptable to just give up
   in case of low-level errors.

   It is important though that the application can tell when only a partial
   configuration was done. The way we do that is through the error_idx field
   of struct v4l2_ext_controls: if that is equal to the count field then no
   controls were affected. Otherwise all controls before that index were
   successful in performing their 'get' or 'set' operation, the control at
   the given index failed, and you don't know what happened with the controls
   after the failed one. Since if they were part of a control cluster they
   could have been successfully processed (if a cluster member was encountered
   at index < error_idx), they could have failed (if a cluster member was at
   error_idx), or they may not have been processed yet (if the first cluster
   member appeared after error_idx).

   It is all fairly theoretical, though. In practice all you can do is to
   bail out. If error_idx == count, then it is an application bug. If
   error_idx < count then it is only an application bug if the error code was
   EBUSY. That usually means that something started streaming just when you
   tried to set the controls. In all other cases it is a driver/hardware
   problem and all you can do is to retry or bail out.

   Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
   never modifies controls the error_idx is just set to whatever control
   has an invalid value.
 */

/* Prepare for the extended g/s/try functions.
   Find the controls in the control array and do some basic checks. */
static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
			     struct v4l2_ext_controls *cs,
3064 3065
			     struct v4l2_ctrl_helper *helpers,
			     bool get)
3066
{
3067 3068
	struct v4l2_ctrl_helper *h;
	bool have_clusters = false;
3069 3070
	u32 i;

3071
	for (i = 0, h = helpers; i < cs->count; i++, h++) {
3072
		struct v4l2_ext_control *c = &cs->controls[i];
3073
		struct v4l2_ctrl_ref *ref;
3074 3075 3076
		struct v4l2_ctrl *ctrl;
		u32 id = c->id & V4L2_CTRL_ID_MASK;

3077
		cs->error_idx = i;
3078

3079 3080
		if (cs->which &&
		    cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
3081
		    cs->which != V4L2_CTRL_WHICH_REQUEST_VAL &&
3082
		    V4L2_CTRL_ID2WHICH(id) != cs->which)
3083 3084 3085 3086 3087 3088
			return -EINVAL;

		/* Old-style private controls are not allowed for
		   extended controls */
		if (id >= V4L2_CID_PRIVATE_BASE)
			return -EINVAL;
3089 3090
		ref = find_ref_lock(hdl, id);
		if (ref == NULL)
3091
			return -EINVAL;
3092
		h->ref = ref;
3093
		ctrl = ref->ctrl;
3094 3095 3096
		if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
			return -EINVAL;

3097 3098 3099 3100
		if (ctrl->cluster[0]->ncontrols > 1)
			have_clusters = true;
		if (ctrl->cluster[0] != ctrl)
			ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
3101 3102 3103 3104 3105 3106 3107 3108 3109
		if (ctrl->is_ptr && !ctrl->is_string) {
			unsigned tot_size = ctrl->elems * ctrl->elem_size;

			if (c->size < tot_size) {
				if (get) {
					c->size = tot_size;
					return -ENOSPC;
				}
				return -EFAULT;
3110
			}
3111
			c->size = tot_size;
3112
		}
3113 3114 3115 3116 3117 3118
		/* Store the ref to the master control of the cluster */
		h->mref = ref;
		/* Initially set next to 0, meaning that there is no other
		   control in this helper array belonging to the same
		   cluster */
		h->next = 0;
3119 3120
	}

3121 3122 3123 3124
	/* We are done if there were no controls that belong to a multi-
	   control cluster. */
	if (!have_clusters)
		return 0;
3125

3126 3127
	/* The code below figures out in O(n) time which controls in the list
	   belong to the same cluster. */
3128

3129
	/* This has to be done with the handler lock taken. */
3130
	mutex_lock(hdl->lock);
3131

3132 3133
	/* First zero the helper field in the master control references */
	for (i = 0; i < cs->count; i++)
3134
		helpers[i].mref->helper = NULL;
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150
	for (i = 0, h = helpers; i < cs->count; i++, h++) {
		struct v4l2_ctrl_ref *mref = h->mref;

		/* If the mref->helper is set, then it points to an earlier
		   helper that belongs to the same cluster. */
		if (mref->helper) {
			/* Set the next field of mref->helper to the current
			   index: this means that that earlier helper now
			   points to the next helper in the same cluster. */
			mref->helper->next = i;
			/* mref should be set only for the first helper in the
			   cluster, clear the others. */
			h->mref = NULL;
		}
		/* Point the mref helper to the current helper struct. */
		mref->helper = h;
3151
	}
3152
	mutex_unlock(hdl->lock);
3153
	return 0;
3154 3155 3156 3157 3158
}

/* Handles the corner case where cs->count == 0. It checks whether the
   specified control class exists. If that class ID is 0, then it checks
   whether there are any controls at all. */
3159
static int class_check(struct v4l2_ctrl_handler *hdl, u32 which)
3160
{
3161 3162
	if (which == 0 || which == V4L2_CTRL_WHICH_DEF_VAL ||
	    which == V4L2_CTRL_WHICH_REQUEST_VAL)
3163
		return 0;
3164
	return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL;
3165 3166 3167
}

/* Get extended controls. Allocates the helpers array if needed. */
3168 3169
static int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl,
				   struct v4l2_ext_controls *cs)
3170
{
3171 3172
	struct v4l2_ctrl_helper helper[4];
	struct v4l2_ctrl_helper *helpers = helper;
3173
	int ret;
3174
	int i, j;
3175 3176 3177
	bool def_value;

	def_value = (cs->which == V4L2_CTRL_WHICH_DEF_VAL);
3178 3179

	cs->error_idx = cs->count;
3180
	cs->which = V4L2_CTRL_ID2WHICH(cs->which);
3181 3182 3183 3184 3185

	if (hdl == NULL)
		return -EINVAL;

	if (cs->count == 0)
3186
		return class_check(hdl, cs->which);
3187 3188

	if (cs->count > ARRAY_SIZE(helper)) {
3189 3190
		helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
					 GFP_KERNEL);
3191 3192 3193 3194
		if (helpers == NULL)
			return -ENOMEM;
	}

3195
	ret = prepare_ext_ctrls(hdl, cs, helpers, true);
3196
	cs->error_idx = cs->count;
3197 3198

	for (i = 0; !ret && i < cs->count; i++)
3199
		if (helpers[i].ref->ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
3200 3201 3202
			ret = -EACCES;

	for (i = 0; !ret && i < cs->count; i++) {
3203
		int (*ctrl_to_user)(struct v4l2_ext_control *c,
3204
				    struct v4l2_ctrl *ctrl);
3205
		struct v4l2_ctrl *master;
3206

3207 3208
		ctrl_to_user = def_value ? def_to_user : cur_to_user;

3209
		if (helpers[i].mref == NULL)
3210 3211
			continue;

3212
		master = helpers[i].mref->ctrl;
3213 3214 3215
		cs->error_idx = i;

		v4l2_ctrl_lock(master);
3216 3217

		/* g_volatile_ctrl will update the new control values */
3218 3219 3220
		if (!def_value &&
		    ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
		    (master->has_volatiles && !is_cur_manual(master)))) {
3221 3222
			for (j = 0; j < master->ncontrols; j++)
				cur_to_new(master->cluster[j]);
3223
			ret = call_op(master, g_volatile_ctrl);
3224
			ctrl_to_user = new_to_user;
3225 3226 3227 3228
		}
		/* If OK, then copy the current (for non-volatile controls)
		   or the new (for volatile controls) control values to the
		   caller */
3229 3230 3231 3232
		if (!ret) {
			u32 idx = i;

			do {
3233 3234 3235 3236 3237 3238
				if (helpers[idx].ref->req)
					ret = req_to_user(cs->controls + idx,
						helpers[idx].ref->req);
				else
					ret = ctrl_to_user(cs->controls + idx,
						helpers[idx].ref->ctrl);
3239 3240 3241
				idx = helpers[idx].next;
			} while (!ret && idx);
		}
3242 3243 3244 3245
		v4l2_ctrl_unlock(master);
	}

	if (cs->count > ARRAY_SIZE(helper))
3246
		kvfree(helpers);
3247 3248
	return ret;
}
3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291

static struct media_request_object *
v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler *hdl,
			struct media_request *req, bool set)
{
	struct media_request_object *obj;
	struct v4l2_ctrl_handler *new_hdl;
	int ret;

	if (IS_ERR(req))
		return ERR_CAST(req);

	if (set && WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
		return ERR_PTR(-EBUSY);

	obj = media_request_object_find(req, &req_ops, hdl);
	if (obj)
		return obj;
	if (!set)
		return ERR_PTR(-ENOENT);

	new_hdl = kzalloc(sizeof(*new_hdl), GFP_KERNEL);
	if (!new_hdl)
		return ERR_PTR(-ENOMEM);

	obj = &new_hdl->req_obj;
	ret = v4l2_ctrl_handler_init(new_hdl, (hdl->nr_of_buckets - 1) * 8);
	if (!ret)
		ret = v4l2_ctrl_request_bind(req, new_hdl, hdl);
	if (ret) {
		kfree(new_hdl);

		return ERR_PTR(ret);
	}

	media_request_object_get(obj);
	return obj;
}

int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct media_device *mdev,
		     struct v4l2_ext_controls *cs)
{
	struct media_request_object *obj = NULL;
3292
	struct media_request *req = NULL;
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
	int ret;

	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
		if (!mdev || cs->request_fd < 0)
			return -EINVAL;

		req = media_request_get_by_fd(mdev, cs->request_fd);
		if (IS_ERR(req))
			return PTR_ERR(req);

3303
		if (req->state != MEDIA_REQUEST_STATE_COMPLETE) {
3304
			media_request_put(req);
3305
			return -EACCES;
3306 3307
		}

3308 3309 3310 3311 3312 3313
		ret = media_request_lock_for_access(req);
		if (ret) {
			media_request_put(req);
			return ret;
		}

3314
		obj = v4l2_ctrls_find_req_obj(hdl, req, false);
3315 3316 3317
		if (IS_ERR(obj)) {
			media_request_unlock_for_access(req);
			media_request_put(req);
3318
			return PTR_ERR(obj);
3319
		}
3320 3321 3322 3323 3324 3325 3326

		hdl = container_of(obj, struct v4l2_ctrl_handler,
				   req_obj);
	}

	ret = v4l2_g_ext_ctrls_common(hdl, cs);

3327 3328
	if (obj) {
		media_request_unlock_for_access(req);
3329
		media_request_object_put(obj);
3330 3331
		media_request_put(req);
	}
3332 3333
	return ret;
}
3334 3335 3336
EXPORT_SYMBOL(v4l2_g_ext_ctrls);

/* Helper function to get a single control */
3337
static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
3338 3339 3340
{
	struct v4l2_ctrl *master = ctrl->cluster[0];
	int ret = 0;
3341
	int i;
3342

3343
	/* Compound controls are not supported. The new_to_user() and
3344 3345 3346
	 * cur_to_user() calls below would need to be modified not to access
	 * userspace memory when called from get_ctrl().
	 */
3347
	if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64)
3348 3349
		return -EINVAL;

3350 3351 3352 3353 3354
	if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
		return -EACCES;

	v4l2_ctrl_lock(master);
	/* g_volatile_ctrl will update the current control values */
3355
	if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
3356 3357
		for (i = 0; i < master->ncontrols; i++)
			cur_to_new(master->cluster[i]);
3358
		ret = call_op(master, g_volatile_ctrl);
3359
		new_to_user(c, ctrl);
3360
	} else {
3361
		cur_to_user(c, ctrl);
3362
	}
3363 3364 3365 3366 3367 3368 3369
	v4l2_ctrl_unlock(master);
	return ret;
}

int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
{
	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
3370 3371
	struct v4l2_ext_control c;
	int ret;
3372

3373
	if (ctrl == NULL || !ctrl->is_int)
3374
		return -EINVAL;
3375 3376 3377
	ret = get_ctrl(ctrl, &c);
	control->value = c.value;
	return ret;
3378 3379 3380 3381 3382
}
EXPORT_SYMBOL(v4l2_g_ctrl);

s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
{
3383
	struct v4l2_ext_control c;
3384 3385

	/* It's a driver bug if this happens. */
3386
	WARN_ON(!ctrl->is_int);
3387 3388 3389
	c.value = 0;
	get_ctrl(ctrl, &c);
	return c.value;
3390 3391 3392
}
EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);

3393 3394 3395 3396 3397
s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
{
	struct v4l2_ext_control c;

	/* It's a driver bug if this happens. */
3398
	WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
3399
	c.value64 = 0;
3400
	get_ctrl(ctrl, &c);
3401
	return c.value64;
3402 3403 3404
}
EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);

3405 3406 3407 3408

/* Core function that calls try/s_ctrl and ensures that the new value is
   copied to the current value on a set.
   Must be called with ctrl->handler->lock held. */
3409 3410
static int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
			      bool set, u32 ch_flags)
3411
{
3412
	bool update_flag;
3413
	int ret;
3414 3415 3416 3417 3418 3419
	int i;

	/* Go through the cluster and either validate the new value or
	   (if no new value was set), copy the current value to the new
	   value, ensuring a consistent view for the control ops when
	   called. */
3420
	for (i = 0; i < master->ncontrols; i++) {
3421 3422 3423 3424 3425
		struct v4l2_ctrl *ctrl = master->cluster[i];

		if (ctrl == NULL)
			continue;

3426 3427
		if (!ctrl->is_new) {
			cur_to_new(ctrl);
3428 3429
			continue;
		}
3430 3431 3432 3433
		/* Check again: it may have changed since the
		   previous check in try_or_set_ext_ctrls(). */
		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
			return -EBUSY;
3434 3435
	}

3436
	ret = call_op(master, try_ctrl);
3437 3438

	/* Don't set if there is no change */
3439 3440 3441 3442 3443 3444
	if (ret || !set || !cluster_changed(master))
		return ret;
	ret = call_op(master, s_ctrl);
	if (ret)
		return ret;

3445
	/* If OK, then make the new values permanent. */
3446
	update_flag = is_cur_manual(master) != is_new_manual(master);
3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459

	for (i = 0; i < master->ncontrols; i++) {
		/*
		 * If we switch from auto to manual mode, and this cluster
		 * contains volatile controls, then all non-master controls
		 * have to be marked as changed. The 'new' value contains
		 * the volatile value (obtained by update_from_auto_cluster),
		 * which now has to become the current value.
		 */
		if (i && update_flag && is_new_manual(master) &&
		    master->has_volatiles && master->cluster[i])
			master->cluster[i]->has_changed = true;

3460 3461
		new_to_cur(fh, master->cluster[i], ch_flags |
			((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
3462
	}
3463
	return 0;
3464 3465
}

3466 3467 3468
/* Validate controls. */
static int validate_ctrls(struct v4l2_ext_controls *cs,
			  struct v4l2_ctrl_helper *helpers, bool set)
3469
{
3470
	unsigned i;
3471 3472
	int ret = 0;

3473
	cs->error_idx = cs->count;
3474
	for (i = 0; i < cs->count; i++) {
3475
		struct v4l2_ctrl *ctrl = helpers[i].ref->ctrl;
3476
		union v4l2_ctrl_ptr p_new;
3477

3478
		cs->error_idx = i;
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489

		if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
			return -EACCES;
		/* This test is also done in try_set_control_cluster() which
		   is called in atomic context, so that has the final say,
		   but it makes sense to do an up-front check as well. Once
		   an error occurs in try_set_control_cluster() some other
		   controls may have been set already and we want to do a
		   best-effort to avoid that. */
		if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
			return -EBUSY;
3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
		/*
		 * Skip validation for now if the payload needs to be copied
		 * from userspace into kernelspace. We'll validate those later.
		 */
		if (ctrl->is_ptr)
			continue;
		if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
			p_new.p_s64 = &cs->controls[i].value64;
		else
			p_new.p_s32 = &cs->controls[i].value;
		ret = validate_new(ctrl, p_new);
3501 3502
		if (ret)
			return ret;
3503
	}
3504 3505 3506
	return 0;
}

3507 3508 3509 3510 3511 3512
/* Obtain the current volatile values of an autocluster and mark them
   as new. */
static void update_from_auto_cluster(struct v4l2_ctrl *master)
{
	int i;

3513
	for (i = 1; i < master->ncontrols; i++)
3514 3515 3516 3517 3518 3519 3520
		cur_to_new(master->cluster[i]);
	if (!call_op(master, g_volatile_ctrl))
		for (i = 1; i < master->ncontrols; i++)
			if (master->cluster[i])
				master->cluster[i]->is_new = 1;
}

3521
/* Try or try-and-set controls */
3522 3523 3524
static int try_set_ext_ctrls_common(struct v4l2_fh *fh,
				    struct v4l2_ctrl_handler *hdl,
				    struct v4l2_ext_controls *cs, bool set)
3525 3526 3527 3528 3529 3530 3531
{
	struct v4l2_ctrl_helper helper[4];
	struct v4l2_ctrl_helper *helpers = helper;
	unsigned i, j;
	int ret;

	cs->error_idx = cs->count;
3532 3533 3534 3535 3536

	/* Default value cannot be changed */
	if (cs->which == V4L2_CTRL_WHICH_DEF_VAL)
		return -EINVAL;

3537
	cs->which = V4L2_CTRL_ID2WHICH(cs->which);
3538 3539 3540 3541 3542

	if (hdl == NULL)
		return -EINVAL;

	if (cs->count == 0)
3543
		return class_check(hdl, cs->which);
3544

3545
	if (cs->count > ARRAY_SIZE(helper)) {
3546 3547
		helpers = kvmalloc_array(cs->count, sizeof(helper[0]),
					 GFP_KERNEL);
3548 3549 3550
		if (!helpers)
			return -ENOMEM;
	}
3551
	ret = prepare_ext_ctrls(hdl, cs, helpers, false);
3552 3553 3554 3555
	if (!ret)
		ret = validate_ctrls(cs, helpers, set);
	if (ret && set)
		cs->error_idx = cs->count;
3556
	for (i = 0; !ret && i < cs->count; i++) {
3557 3558
		struct v4l2_ctrl *master;
		u32 idx = i;
3559

3560
		if (helpers[i].mref == NULL)
3561 3562
			continue;

3563
		cs->error_idx = i;
3564 3565
		master = helpers[i].mref->ctrl;
		v4l2_ctrl_lock(master);
3566

3567
		/* Reset the 'is_new' flags of the cluster */
3568 3569
		for (j = 0; j < master->ncontrols; j++)
			if (master->cluster[j])
3570
				master->cluster[j]->is_new = 0;
3571

3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586
		/* For volatile autoclusters that are currently in auto mode
		   we need to discover if it will be set to manual mode.
		   If so, then we have to copy the current volatile values
		   first since those will become the new manual values (which
		   may be overwritten by explicit new values from this set
		   of controls). */
		if (master->is_auto && master->has_volatiles &&
						!is_cur_manual(master)) {
			/* Pick an initial non-manual value */
			s32 new_auto_val = master->manual_mode_value + 1;
			u32 tmp_idx = idx;

			do {
				/* Check if the auto control is part of the
				   list, and remember the new value. */
3587
				if (helpers[tmp_idx].ref->ctrl == master)
3588 3589 3590 3591 3592 3593 3594 3595 3596
					new_auto_val = cs->controls[tmp_idx].value;
				tmp_idx = helpers[tmp_idx].next;
			} while (tmp_idx);
			/* If the new value == the manual value, then copy
			   the current volatile values. */
			if (new_auto_val == master->manual_mode_value)
				update_from_auto_cluster(master);
		}

3597
		/* Copy the new caller-supplied control values.
3598
		   user_to_new() sets 'is_new' to 1. */
3599
		do {
3600
			struct v4l2_ctrl *ctrl = helpers[idx].ref->ctrl;
3601 3602 3603 3604

			ret = user_to_new(cs->controls + idx, ctrl);
			if (!ret && ctrl->is_ptr)
				ret = validate_new(ctrl, ctrl->p_new);
3605 3606
			idx = helpers[idx].next;
		} while (!ret && idx);
3607 3608

		if (!ret)
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
			ret = try_or_set_cluster(fh, master,
						 !hdl->req_obj.req && set, 0);
		if (!ret && hdl->req_obj.req && set) {
			for (j = 0; j < master->ncontrols; j++) {
				struct v4l2_ctrl_ref *ref =
					find_ref(hdl, master->cluster[j]->id);

				new_to_req(ref);
			}
		}
3619 3620

		/* Copy the new values back to userspace. */
3621 3622 3623
		if (!ret) {
			idx = i;
			do {
3624
				ret = new_to_user(cs->controls + idx,
3625
						helpers[idx].ref->ctrl);
3626 3627 3628 3629
				idx = helpers[idx].next;
			} while (!ret && idx);
		}
		v4l2_ctrl_unlock(master);
3630 3631 3632
	}

	if (cs->count > ARRAY_SIZE(helper))
3633
		kvfree(helpers);
3634 3635 3636
	return ret;
}

3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
static int try_set_ext_ctrls(struct v4l2_fh *fh,
			     struct v4l2_ctrl_handler *hdl, struct media_device *mdev,
			     struct v4l2_ext_controls *cs, bool set)
{
	struct media_request_object *obj = NULL;
	struct media_request *req = NULL;
	int ret;

	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
		if (!mdev || cs->request_fd < 0)
			return -EINVAL;

		req = media_request_get_by_fd(mdev, cs->request_fd);
		if (IS_ERR(req))
			return PTR_ERR(req);

		ret = media_request_lock_for_update(req);
		if (ret) {
			media_request_put(req);
			return ret;
		}

		obj = v4l2_ctrls_find_req_obj(hdl, req, set);
		/* Reference to the request held through obj */
		media_request_put(req);
		if (IS_ERR(obj)) {
			media_request_unlock_for_update(req);
			return PTR_ERR(obj);
		}
		hdl = container_of(obj, struct v4l2_ctrl_handler,
				   req_obj);
	}

	ret = try_set_ext_ctrls_common(fh, hdl, cs, set);

	if (obj) {
		media_request_unlock_for_update(obj->req);
		media_request_object_put(obj);
	}

	return ret;
}

int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct media_device *mdev,
		       struct v4l2_ext_controls *cs)
3682
{
3683
	return try_set_ext_ctrls(NULL, hdl, mdev, cs, false);
3684 3685 3686
}
EXPORT_SYMBOL(v4l2_try_ext_ctrls);

3687
int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
3688
		     struct media_device *mdev, struct v4l2_ext_controls *cs)
3689
{
3690
	return try_set_ext_ctrls(fh, hdl, mdev, cs, true);
3691 3692 3693 3694
}
EXPORT_SYMBOL(v4l2_s_ext_ctrls);

/* Helper function for VIDIOC_S_CTRL compatibility */
3695
static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
3696 3697
{
	struct v4l2_ctrl *master = ctrl->cluster[0];
3698
	int ret;
3699 3700
	int i;

3701
	/* Reset the 'is_new' flags of the cluster */
3702 3703
	for (i = 0; i < master->ncontrols; i++)
		if (master->cluster[i])
3704
			master->cluster[i]->is_new = 0;
3705

3706 3707 3708
	ret = validate_new(ctrl, ctrl->p_new);
	if (ret)
		return ret;
3709

3710 3711 3712 3713
	/* For autoclusters with volatiles that are switched from auto to
	   manual mode we have to update the current volatile values since
	   those will become the initial manual values after such a switch. */
	if (master->is_auto && master->has_volatiles && ctrl == master &&
3714
	    !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
3715
		update_from_auto_cluster(master);
3716

3717
	ctrl->is_new = 1;
3718 3719
	return try_or_set_cluster(fh, master, true, ch_flags);
}
3720

3721 3722 3723 3724
/* Helper function for VIDIOC_S_CTRL compatibility */
static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
			 struct v4l2_ext_control *c)
{
3725
	int ret;
3726

3727 3728 3729 3730 3731 3732
	v4l2_ctrl_lock(ctrl);
	user_to_new(c, ctrl);
	ret = set_ctrl(fh, ctrl, 0);
	if (!ret)
		cur_to_user(c, ctrl);
	v4l2_ctrl_unlock(ctrl);
3733 3734 3735
	return ret;
}

3736 3737
int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
					struct v4l2_control *control)
3738 3739
{
	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
3740
	struct v4l2_ext_control c = { control->id };
3741
	int ret;
3742

3743
	if (ctrl == NULL || !ctrl->is_int)
3744 3745
		return -EINVAL;

3746 3747 3748
	if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
		return -EACCES;

3749
	c.value = control->value;
3750
	ret = set_ctrl_lock(fh, ctrl, &c);
3751 3752
	control->value = c.value;
	return ret;
3753 3754 3755
}
EXPORT_SYMBOL(v4l2_s_ctrl);

3756
int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
3757
{
3758
	lockdep_assert_held(ctrl->handler->lock);
3759

3760
	/* It's a driver bug if this happens. */
3761
	WARN_ON(!ctrl->is_int);
3762
	ctrl->val = val;
3763
	return set_ctrl(NULL, ctrl, 0);
3764
}
3765
EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
3766

3767
int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
3768
{
3769
	lockdep_assert_held(ctrl->handler->lock);
3770 3771

	/* It's a driver bug if this happens. */
3772
	WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
3773
	*ctrl->p_new.p_s64 = val;
3774
	return set_ctrl(NULL, ctrl, 0);
3775
}
3776
EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
3777

3778 3779 3780 3781 3782 3783 3784
int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
{
	lockdep_assert_held(ctrl->handler->lock);

	/* It's a driver bug if this happens. */
	WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING);
	strlcpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
3785
	return set_ctrl(NULL, ctrl, 0);
3786 3787 3788
}
EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);

3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
void v4l2_ctrl_request_complete(struct media_request *req,
				struct v4l2_ctrl_handler *main_hdl)
{
	struct media_request_object *obj;
	struct v4l2_ctrl_handler *hdl;
	struct v4l2_ctrl_ref *ref;

	if (!req || !main_hdl)
		return;

	/*
	 * Note that it is valid if nothing was found. It means
	 * that this request doesn't have any controls and so just
	 * wants to leave the controls unchanged.
	 */
	obj = media_request_object_find(req, &req_ops, main_hdl);
	if (!obj)
		return;
	hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);

	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
		struct v4l2_ctrl *ctrl = ref->ctrl;
		struct v4l2_ctrl *master = ctrl->cluster[0];
		unsigned int i;

		if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
			ref->req = ref;

			v4l2_ctrl_lock(master);
			/* g_volatile_ctrl will update the current control values */
			for (i = 0; i < master->ncontrols; i++)
				cur_to_new(master->cluster[i]);
			call_op(master, g_volatile_ctrl);
			new_to_req(ref);
			v4l2_ctrl_unlock(master);
			continue;
		}
		if (ref->req == ref)
			continue;

		v4l2_ctrl_lock(ctrl);
		if (ref->req)
			ptr_to_ptr(ctrl, ref->req->p_req, ref->p_req);
		else
			ptr_to_ptr(ctrl, ctrl->p_cur, ref->p_req);
		v4l2_ctrl_unlock(ctrl);
	}

	WARN_ON(!hdl->request_is_queued);
	list_del_init(&hdl->requests_queued);
	hdl->request_is_queued = false;
	media_request_object_complete(obj);
	media_request_object_put(obj);
}
EXPORT_SYMBOL(v4l2_ctrl_request_complete);

void v4l2_ctrl_request_setup(struct media_request *req,
			     struct v4l2_ctrl_handler *main_hdl)
{
	struct media_request_object *obj;
	struct v4l2_ctrl_handler *hdl;
	struct v4l2_ctrl_ref *ref;

	if (!req || !main_hdl)
		return;

	if (WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
		return;

	/*
	 * Note that it is valid if nothing was found. It means
	 * that this request doesn't have any controls and so just
	 * wants to leave the controls unchanged.
	 */
	obj = media_request_object_find(req, &req_ops, main_hdl);
	if (!obj)
		return;
	if (obj->completed) {
		media_request_object_put(obj);
		return;
	}
	hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);

	list_for_each_entry(ref, &hdl->ctrl_refs, node)
		ref->req_done = false;

	list_for_each_entry(ref, &hdl->ctrl_refs, node) {
		struct v4l2_ctrl *ctrl = ref->ctrl;
		struct v4l2_ctrl *master = ctrl->cluster[0];
		bool have_new_data = false;
		int i;

		/*
		 * Skip if this control was already handled by a cluster.
		 * Skip button controls and read-only controls.
		 */
		if (ref->req_done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
			continue;

		v4l2_ctrl_lock(master);
		for (i = 0; i < master->ncontrols; i++) {
			if (master->cluster[i]) {
				struct v4l2_ctrl_ref *r =
					find_ref(hdl, master->cluster[i]->id);

				if (r->req && r == r->req) {
					have_new_data = true;
					break;
				}
			}
		}
		if (!have_new_data) {
			v4l2_ctrl_unlock(master);
			continue;
		}

		for (i = 0; i < master->ncontrols; i++) {
			if (master->cluster[i]) {
				struct v4l2_ctrl_ref *r =
					find_ref(hdl, master->cluster[i]->id);

				req_to_new(r);
				master->cluster[i]->is_new = 1;
				r->req_done = true;
			}
		}
		/*
		 * For volatile autoclusters that are currently in auto mode
		 * we need to discover if it will be set to manual mode.
		 * If so, then we have to copy the current volatile values
		 * first since those will become the new manual values (which
		 * may be overwritten by explicit new values from this set
		 * of controls).
		 */
		if (master->is_auto && master->has_volatiles &&
		    !is_cur_manual(master)) {
			s32 new_auto_val = *master->p_new.p_s32;

			/*
			 * If the new value == the manual value, then copy
			 * the current volatile values.
			 */
			if (new_auto_val == master->manual_mode_value)
				update_from_auto_cluster(master);
		}

		try_or_set_cluster(NULL, master, true, 0);

		v4l2_ctrl_unlock(master);
	}

	media_request_object_put(obj);
}
EXPORT_SYMBOL(v4l2_ctrl_request_setup);

3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
{
	if (ctrl == NULL)
		return;
	if (notify == NULL) {
		ctrl->call_notify = 0;
		return;
	}
	if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
		return;
	ctrl->handler->notify = notify;
	ctrl->handler->notify_priv = priv;
	ctrl->call_notify = 1;
}
EXPORT_SYMBOL(v4l2_ctrl_notify);

3961
int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
3962
			s64 min, s64 max, u64 step, s64 def)
3963
{
3964 3965
	bool value_changed;
	bool range_changed = false;
3966
	int ret;
3967

3968 3969
	lockdep_assert_held(ctrl->handler->lock);

3970 3971
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_INTEGER:
3972
	case V4L2_CTRL_TYPE_INTEGER64:
3973 3974 3975 3976
	case V4L2_CTRL_TYPE_BOOLEAN:
	case V4L2_CTRL_TYPE_MENU:
	case V4L2_CTRL_TYPE_INTEGER_MENU:
	case V4L2_CTRL_TYPE_BITMASK:
3977 3978
	case V4L2_CTRL_TYPE_U8:
	case V4L2_CTRL_TYPE_U16:
3979
	case V4L2_CTRL_TYPE_U32:
3980 3981 3982
		if (ctrl->is_array)
			return -EINVAL;
		ret = check_range(ctrl->type, min, max, step, def);
3983 3984 3985 3986 3987 3988
		if (ret)
			return ret;
		break;
	default:
		return -EINVAL;
	}
3989 3990 3991 3992 3993 3994 3995 3996
	if ((ctrl->minimum != min) || (ctrl->maximum != max) ||
		(ctrl->step != step) || ctrl->default_value != def) {
		range_changed = true;
		ctrl->minimum = min;
		ctrl->maximum = max;
		ctrl->step = step;
		ctrl->default_value = def;
	}
3997 3998 3999 4000 4001 4002 4003 4004 4005
	cur_to_new(ctrl);
	if (validate_new(ctrl, ctrl->p_new)) {
		if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
			*ctrl->p_new.p_s64 = def;
		else
			*ctrl->p_new.p_s32 = def;
	}

	if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
4006
		value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
4007
	else
4008 4009
		value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
	if (value_changed)
4010
		ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
4011
	else if (range_changed)
4012 4013 4014
		send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
	return ret;
}
4015
EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
4016

4017
static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
4018
{
4019 4020 4021 4022 4023
	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);

	if (ctrl == NULL)
		return -EINVAL;

4024
	v4l2_ctrl_lock(ctrl);
4025
	list_add_tail(&sev->node, &ctrl->ev_subs);
4026
	if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
4027
	    (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
4028
		struct v4l2_event ev;
4029
		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
4030

4031 4032 4033
		if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
			changes |= V4L2_EVENT_CTRL_CH_VALUE;
		fill_event(&ev, ctrl, changes);
4034 4035 4036
		/* Mark the queue as active, allowing this initial
		   event to be accepted. */
		sev->elems = elems;
4037
		v4l2_event_queue_fh(sev->fh, &ev);
4038 4039
	}
	v4l2_ctrl_unlock(ctrl);
4040
	return 0;
4041 4042
}

4043
static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
4044
{
4045 4046
	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);

4047 4048 4049
	if (ctrl == NULL)
		return;

4050
	v4l2_ctrl_lock(ctrl);
4051
	list_del(&sev->node);
4052 4053
	v4l2_ctrl_unlock(ctrl);
}
4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076

void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
{
	u32 old_changes = old->u.ctrl.changes;

	old->u.ctrl = new->u.ctrl;
	old->u.ctrl.changes |= old_changes;
}
EXPORT_SYMBOL(v4l2_ctrl_replace);

void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
{
	new->u.ctrl.changes |= old->u.ctrl.changes;
}
EXPORT_SYMBOL(v4l2_ctrl_merge);

const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
	.add = v4l2_ctrl_add_event,
	.del = v4l2_ctrl_del_event,
	.replace = v4l2_ctrl_replace,
	.merge = v4l2_ctrl_merge,
};
EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088

int v4l2_ctrl_log_status(struct file *file, void *fh)
{
	struct video_device *vfd = video_devdata(file);
	struct v4l2_fh *vfh = file->private_data;

	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
		v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
			vfd->v4l2_dev->name);
	return 0;
}
EXPORT_SYMBOL(v4l2_ctrl_log_status);
4089 4090

int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
4091
				const struct v4l2_event_subscription *sub)
4092 4093
{
	if (sub->type == V4L2_EVENT_CTRL)
4094
		return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
4095 4096 4097 4098
	return -EINVAL;
}
EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);

4099 4100 4101 4102 4103 4104 4105 4106 4107
int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
				     struct v4l2_event_subscription *sub)
{
	if (!sd->ctrl_handler)
		return -EINVAL;
	return v4l2_ctrl_subscribe_event(fh, sub);
}
EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);

A
Al Viro 已提交
4108
__poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
4109 4110 4111 4112
{
	struct v4l2_fh *fh = file->private_data;

	if (v4l2_event_pending(fh))
4113
		return EPOLLPRI;
4114 4115 4116 4117
	poll_wait(file, &fh->wait, wait);
	return 0;
}
EXPORT_SYMBOL(v4l2_ctrl_poll);