linux/drivers/media/v4l2-core/v4l2-ctrls.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3    V4L2 controls framework implementation.
   4
   5    Copyright (C) 2010  Hans Verkuil <hverkuil@xs4all.nl>
   6
   7 */
   8
   9#define pr_fmt(fmt) "v4l2-ctrls: " fmt
  10
  11#include <linux/ctype.h>
  12#include <linux/export.h>
  13#include <linux/mm.h>
  14#include <linux/slab.h>
  15#include <media/v4l2-ctrls.h>
  16#include <media/v4l2-dev.h>
  17#include <media/v4l2-device.h>
  18#include <media/v4l2-event.h>
  19#include <media/v4l2-fwnode.h>
  20#include <media/v4l2-ioctl.h>
  21
  22#define dprintk(vdev, fmt, arg...) do {                                 \
  23        if (!WARN_ON(!(vdev)) && ((vdev)->dev_debug & V4L2_DEV_DEBUG_CTRL)) \
  24                printk(KERN_DEBUG pr_fmt("%s: %s: " fmt),               \
  25                       __func__, video_device_node_name(vdev), ##arg);  \
  26} while (0)
  27
  28#define has_op(master, op) \
  29        (master->ops && master->ops->op)
  30#define call_op(master, op) \
  31        (has_op(master, op) ? master->ops->op(master) : 0)
  32
  33static const union v4l2_ctrl_ptr ptr_null;
  34
  35/* Internal temporary helper struct, one for each v4l2_ext_control */
  36struct v4l2_ctrl_helper {
  37        /* Pointer to the control reference of the master control */
  38        struct v4l2_ctrl_ref *mref;
  39        /* The control ref corresponding to the v4l2_ext_control ID field. */
  40        struct v4l2_ctrl_ref *ref;
  41        /* v4l2_ext_control index of the next control belonging to the
  42           same cluster, or 0 if there isn't any. */
  43        u32 next;
  44};
  45
  46/* Small helper function to determine if the autocluster is set to manual
  47   mode. */
  48static bool is_cur_manual(const struct v4l2_ctrl *master)
  49{
  50        return master->is_auto && master->cur.val == master->manual_mode_value;
  51}
  52
  53/* Same as above, but this checks the against the new value instead of the
  54   current value. */
  55static bool is_new_manual(const struct v4l2_ctrl *master)
  56{
  57        return master->is_auto && master->val == master->manual_mode_value;
  58}
  59
  60/* Returns NULL or a character pointer array containing the menu for
  61   the given control ID. The pointer array ends with a NULL pointer.
  62   An empty string signifies a menu entry that is invalid. This allows
  63   drivers to disable certain options if it is not supported. */
  64const char * const *v4l2_ctrl_get_menu(u32 id)
  65{
  66        static const char * const mpeg_audio_sampling_freq[] = {
  67                "44.1 kHz",
  68                "48 kHz",
  69                "32 kHz",
  70                NULL
  71        };
  72        static const char * const mpeg_audio_encoding[] = {
  73                "MPEG-1/2 Layer I",
  74                "MPEG-1/2 Layer II",
  75                "MPEG-1/2 Layer III",
  76                "MPEG-2/4 AAC",
  77                "AC-3",
  78                NULL
  79        };
  80        static const char * const mpeg_audio_l1_bitrate[] = {
  81                "32 kbps",
  82                "64 kbps",
  83                "96 kbps",
  84                "128 kbps",
  85                "160 kbps",
  86                "192 kbps",
  87                "224 kbps",
  88                "256 kbps",
  89                "288 kbps",
  90                "320 kbps",
  91                "352 kbps",
  92                "384 kbps",
  93                "416 kbps",
  94                "448 kbps",
  95                NULL
  96        };
  97        static const char * const mpeg_audio_l2_bitrate[] = {
  98                "32 kbps",
  99                "48 kbps",
 100                "56 kbps",
 101                "64 kbps",
 102                "80 kbps",
 103                "96 kbps",
 104                "112 kbps",
 105                "128 kbps",
 106                "160 kbps",
 107                "192 kbps",
 108                "224 kbps",
 109                "256 kbps",
 110                "320 kbps",
 111                "384 kbps",
 112                NULL
 113        };
 114        static const char * const mpeg_audio_l3_bitrate[] = {
 115                "32 kbps",
 116                "40 kbps",
 117                "48 kbps",
 118                "56 kbps",
 119                "64 kbps",
 120                "80 kbps",
 121                "96 kbps",
 122                "112 kbps",
 123                "128 kbps",
 124                "160 kbps",
 125                "192 kbps",
 126                "224 kbps",
 127                "256 kbps",
 128                "320 kbps",
 129                NULL
 130        };
 131        static const char * const mpeg_audio_ac3_bitrate[] = {
 132                "32 kbps",
 133                "40 kbps",
 134                "48 kbps",
 135                "56 kbps",
 136                "64 kbps",
 137                "80 kbps",
 138                "96 kbps",
 139                "112 kbps",
 140                "128 kbps",
 141                "160 kbps",
 142                "192 kbps",
 143                "224 kbps",
 144                "256 kbps",
 145                "320 kbps",
 146                "384 kbps",
 147                "448 kbps",
 148                "512 kbps",
 149                "576 kbps",
 150                "640 kbps",
 151                NULL
 152        };
 153        static const char * const mpeg_audio_mode[] = {
 154                "Stereo",
 155                "Joint Stereo",
 156                "Dual",
 157                "Mono",
 158                NULL
 159        };
 160        static const char * const mpeg_audio_mode_extension[] = {
 161                "Bound 4",
 162                "Bound 8",
 163                "Bound 12",
 164                "Bound 16",
 165                NULL
 166        };
 167        static const char * const mpeg_audio_emphasis[] = {
 168                "No Emphasis",
 169                "50/15 us",
 170                "CCITT J17",
 171                NULL
 172        };
 173        static const char * const mpeg_audio_crc[] = {
 174                "No CRC",
 175                "16-bit CRC",
 176                NULL
 177        };
 178        static const char * const mpeg_audio_dec_playback[] = {
 179                "Auto",
 180                "Stereo",
 181                "Left",
 182                "Right",
 183                "Mono",
 184                "Swapped Stereo",
 185                NULL
 186        };
 187        static const char * const mpeg_video_encoding[] = {
 188                "MPEG-1",
 189                "MPEG-2",
 190                "MPEG-4 AVC",
 191                NULL
 192        };
 193        static const char * const mpeg_video_aspect[] = {
 194                "1x1",
 195                "4x3",
 196                "16x9",
 197                "2.21x1",
 198                NULL
 199        };
 200        static const char * const mpeg_video_bitrate_mode[] = {
 201                "Variable Bitrate",
 202                "Constant Bitrate",
 203                "Constant Quality",
 204                NULL
 205        };
 206        static const char * const mpeg_stream_type[] = {
 207                "MPEG-2 Program Stream",
 208                "MPEG-2 Transport Stream",
 209                "MPEG-1 System Stream",
 210                "MPEG-2 DVD-compatible Stream",
 211                "MPEG-1 VCD-compatible Stream",
 212                "MPEG-2 SVCD-compatible Stream",
 213                NULL
 214        };
 215        static const char * const mpeg_stream_vbi_fmt[] = {
 216                "No VBI",
 217                "Private Packet, IVTV Format",
 218                NULL
 219        };
 220        static const char * const camera_power_line_frequency[] = {
 221                "Disabled",
 222                "50 Hz",
 223                "60 Hz",
 224                "Auto",
 225                NULL
 226        };
 227        static const char * const camera_exposure_auto[] = {
 228                "Auto Mode",
 229                "Manual Mode",
 230                "Shutter Priority Mode",
 231                "Aperture Priority Mode",
 232                NULL
 233        };
 234        static const char * const camera_exposure_metering[] = {
 235                "Average",
 236                "Center Weighted",
 237                "Spot",
 238                "Matrix",
 239                NULL
 240        };
 241        static const char * const camera_auto_focus_range[] = {
 242                "Auto",
 243                "Normal",
 244                "Macro",
 245                "Infinity",
 246                NULL
 247        };
 248        static const char * const colorfx[] = {
 249                "None",
 250                "Black & White",
 251                "Sepia",
 252                "Negative",
 253                "Emboss",
 254                "Sketch",
 255                "Sky Blue",
 256                "Grass Green",
 257                "Skin Whiten",
 258                "Vivid",
 259                "Aqua",
 260                "Art Freeze",
 261                "Silhouette",
 262                "Solarization",
 263                "Antique",
 264                "Set Cb/Cr",
 265                NULL
 266        };
 267        static const char * const auto_n_preset_white_balance[] = {
 268                "Manual",
 269                "Auto",
 270                "Incandescent",
 271                "Fluorescent",
 272                "Fluorescent H",
 273                "Horizon",
 274                "Daylight",
 275                "Flash",
 276                "Cloudy",
 277                "Shade",
 278                NULL,
 279        };
 280        static const char * const camera_iso_sensitivity_auto[] = {
 281                "Manual",
 282                "Auto",
 283                NULL
 284        };
 285        static const char * const scene_mode[] = {
 286                "None",
 287                "Backlight",
 288                "Beach/Snow",
 289                "Candle Light",
 290                "Dusk/Dawn",
 291                "Fall Colors",
 292                "Fireworks",
 293                "Landscape",
 294                "Night",
 295                "Party/Indoor",
 296                "Portrait",
 297                "Sports",
 298                "Sunset",
 299                "Text",
 300                NULL
 301        };
 302        static const char * const tune_emphasis[] = {
 303                "None",
 304                "50 Microseconds",
 305                "75 Microseconds",
 306                NULL,
 307        };
 308        static const char * const header_mode[] = {
 309                "Separate Buffer",
 310                "Joined With 1st Frame",
 311                NULL,
 312        };
 313        static const char * const multi_slice[] = {
 314                "Single",
 315                "Max Macroblocks",
 316                "Max Bytes",
 317                NULL,
 318        };
 319        static const char * const entropy_mode[] = {
 320                "CAVLC",
 321                "CABAC",
 322                NULL,
 323        };
 324        static const char * const mpeg_h264_level[] = {
 325                "1",
 326                "1b",
 327                "1.1",
 328                "1.2",
 329                "1.3",
 330                "2",
 331                "2.1",
 332                "2.2",
 333                "3",
 334                "3.1",
 335                "3.2",
 336                "4",
 337                "4.1",
 338                "4.2",
 339                "5",
 340                "5.1",
 341                "5.2",
 342                "6.0",
 343                "6.1",
 344                "6.2",
 345                NULL,
 346        };
 347        static const char * const h264_loop_filter[] = {
 348                "Enabled",
 349                "Disabled",
 350                "Disabled at Slice Boundary",
 351                NULL,
 352        };
 353        static const char * const h264_profile[] = {
 354                "Baseline",
 355                "Constrained Baseline",
 356                "Main",
 357                "Extended",
 358                "High",
 359                "High 10",
 360                "High 422",
 361                "High 444 Predictive",
 362                "High 10 Intra",
 363                "High 422 Intra",
 364                "High 444 Intra",
 365                "CAVLC 444 Intra",
 366                "Scalable Baseline",
 367                "Scalable High",
 368                "Scalable High Intra",
 369                "Stereo High",
 370                "Multiview High",
 371                "Constrained High",
 372                NULL,
 373        };
 374        static const char * const vui_sar_idc[] = {
 375                "Unspecified",
 376                "1:1",
 377                "12:11",
 378                "10:11",
 379                "16:11",
 380                "40:33",
 381                "24:11",
 382                "20:11",
 383                "32:11",
 384                "80:33",
 385                "18:11",
 386                "15:11",
 387                "64:33",
 388                "160:99",
 389                "4:3",
 390                "3:2",
 391                "2:1",
 392                "Extended SAR",
 393                NULL,
 394        };
 395        static const char * const h264_fp_arrangement_type[] = {
 396                "Checkerboard",
 397                "Column",
 398                "Row",
 399                "Side by Side",
 400                "Top Bottom",
 401                "Temporal",
 402                NULL,
 403        };
 404        static const char * const h264_fmo_map_type[] = {
 405                "Interleaved Slices",
 406                "Scattered Slices",
 407                "Foreground with Leftover",
 408                "Box Out",
 409                "Raster Scan",
 410                "Wipe Scan",
 411                "Explicit",
 412                NULL,
 413        };
 414        static const char * const h264_decode_mode[] = {
 415                "Slice-Based",
 416                "Frame-Based",
 417                NULL,
 418        };
 419        static const char * const h264_start_code[] = {
 420                "No Start Code",
 421                "Annex B Start Code",
 422                NULL,
 423        };
 424        static const char * const h264_hierarchical_coding_type[] = {
 425                "Hier Coding B",
 426                "Hier Coding P",
 427                NULL,
 428        };
 429        static const char * const mpeg_mpeg2_level[] = {
 430                "Low",
 431                "Main",
 432                "High 1440",
 433                "High",
 434                NULL,
 435        };
 436        static const char * const mpeg2_profile[] = {
 437                "Simple",
 438                "Main",
 439                "SNR Scalable",
 440                "Spatially Scalable",
 441                "High",
 442                NULL,
 443        };
 444        static const char * const mpeg_mpeg4_level[] = {
 445                "0",
 446                "0b",
 447                "1",
 448                "2",
 449                "3",
 450                "3b",
 451                "4",
 452                "5",
 453                NULL,
 454        };
 455        static const char * const mpeg4_profile[] = {
 456                "Simple",
 457                "Advanced Simple",
 458                "Core",
 459                "Simple Scalable",
 460                "Advanced Coding Efficiency",
 461                NULL,
 462        };
 463
 464        static const char * const vpx_golden_frame_sel[] = {
 465                "Use Previous Frame",
 466                "Use Previous Specific Frame",
 467                NULL,
 468        };
 469        static const char * const vp8_profile[] = {
 470                "0",
 471                "1",
 472                "2",
 473                "3",
 474                NULL,
 475        };
 476        static const char * const vp9_profile[] = {
 477                "0",
 478                "1",
 479                "2",
 480                "3",
 481                NULL,
 482        };
 483        static const char * const vp9_level[] = {
 484                "1",
 485                "1.1",
 486                "2",
 487                "2.1",
 488                "3",
 489                "3.1",
 490                "4",
 491                "4.1",
 492                "5",
 493                "5.1",
 494                "5.2",
 495                "6",
 496                "6.1",
 497                "6.2",
 498                NULL,
 499        };
 500
 501        static const char * const flash_led_mode[] = {
 502                "Off",
 503                "Flash",
 504                "Torch",
 505                NULL,
 506        };
 507        static const char * const flash_strobe_source[] = {
 508                "Software",
 509                "External",
 510                NULL,
 511        };
 512
 513        static const char * const jpeg_chroma_subsampling[] = {
 514                "4:4:4",
 515                "4:2:2",
 516                "4:2:0",
 517                "4:1:1",
 518                "4:1:0",
 519                "Gray",
 520                NULL,
 521        };
 522        static const char * const dv_tx_mode[] = {
 523                "DVI-D",
 524                "HDMI",
 525                NULL,
 526        };
 527        static const char * const dv_rgb_range[] = {
 528                "Automatic",
 529                "RGB Limited Range (16-235)",
 530                "RGB Full Range (0-255)",
 531                NULL,
 532        };
 533        static const char * const dv_it_content_type[] = {
 534                "Graphics",
 535                "Photo",
 536                "Cinema",
 537                "Game",
 538                "No IT Content",
 539                NULL,
 540        };
 541        static const char * const detect_md_mode[] = {
 542                "Disabled",
 543                "Global",
 544                "Threshold Grid",
 545                "Region Grid",
 546                NULL,
 547        };
 548
 549        static const char * const hevc_profile[] = {
 550                "Main",
 551                "Main Still Picture",
 552                "Main 10",
 553                NULL,
 554        };
 555        static const char * const hevc_level[] = {
 556                "1",
 557                "2",
 558                "2.1",
 559                "3",
 560                "3.1",
 561                "4",
 562                "4.1",
 563                "5",
 564                "5.1",
 565                "5.2",
 566                "6",
 567                "6.1",
 568                "6.2",
 569                NULL,
 570        };
 571        static const char * const hevc_hierarchial_coding_type[] = {
 572                "B",
 573                "P",
 574                NULL,
 575        };
 576        static const char * const hevc_refresh_type[] = {
 577                "None",
 578                "CRA",
 579                "IDR",
 580                NULL,
 581        };
 582        static const char * const hevc_size_of_length_field[] = {
 583                "0",
 584                "1",
 585                "2",
 586                "4",
 587                NULL,
 588        };
 589        static const char * const hevc_tier[] = {
 590                "Main",
 591                "High",
 592                NULL,
 593        };
 594        static const char * const hevc_loop_filter_mode[] = {
 595                "Disabled",
 596                "Enabled",
 597                "Disabled at slice boundary",
 598                "NULL",
 599        };
 600        static const char * const hevc_decode_mode[] = {
 601                "Slice-Based",
 602                "Frame-Based",
 603                NULL,
 604        };
 605        static const char * const hevc_start_code[] = {
 606                "No Start Code",
 607                "Annex B Start Code",
 608                NULL,
 609        };
 610        static const char * const camera_orientation[] = {
 611                "Front",
 612                "Back",
 613                "External",
 614                NULL,
 615        };
 616        static const char * const mpeg_video_frame_skip[] = {
 617                "Disabled",
 618                "Level Limit",
 619                "VBV/CPB Limit",
 620                NULL,
 621        };
 622
 623        switch (id) {
 624        case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
 625                return mpeg_audio_sampling_freq;
 626        case V4L2_CID_MPEG_AUDIO_ENCODING:
 627                return mpeg_audio_encoding;
 628        case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
 629                return mpeg_audio_l1_bitrate;
 630        case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
 631                return mpeg_audio_l2_bitrate;
 632        case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
 633                return mpeg_audio_l3_bitrate;
 634        case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
 635                return mpeg_audio_ac3_bitrate;
 636        case V4L2_CID_MPEG_AUDIO_MODE:
 637                return mpeg_audio_mode;
 638        case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
 639                return mpeg_audio_mode_extension;
 640        case V4L2_CID_MPEG_AUDIO_EMPHASIS:
 641                return mpeg_audio_emphasis;
 642        case V4L2_CID_MPEG_AUDIO_CRC:
 643                return mpeg_audio_crc;
 644        case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
 645        case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
 646                return mpeg_audio_dec_playback;
 647        case V4L2_CID_MPEG_VIDEO_ENCODING:
 648                return mpeg_video_encoding;
 649        case V4L2_CID_MPEG_VIDEO_ASPECT:
 650                return mpeg_video_aspect;
 651        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 652                return mpeg_video_bitrate_mode;
 653        case V4L2_CID_MPEG_STREAM_TYPE:
 654                return mpeg_stream_type;
 655        case V4L2_CID_MPEG_STREAM_VBI_FMT:
 656                return mpeg_stream_vbi_fmt;
 657        case V4L2_CID_POWER_LINE_FREQUENCY:
 658                return camera_power_line_frequency;
 659        case V4L2_CID_EXPOSURE_AUTO:
 660                return camera_exposure_auto;
 661        case V4L2_CID_EXPOSURE_METERING:
 662                return camera_exposure_metering;
 663        case V4L2_CID_AUTO_FOCUS_RANGE:
 664                return camera_auto_focus_range;
 665        case V4L2_CID_COLORFX:
 666                return colorfx;
 667        case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
 668                return auto_n_preset_white_balance;
 669        case V4L2_CID_ISO_SENSITIVITY_AUTO:
 670                return camera_iso_sensitivity_auto;
 671        case V4L2_CID_SCENE_MODE:
 672                return scene_mode;
 673        case V4L2_CID_TUNE_PREEMPHASIS:
 674                return tune_emphasis;
 675        case V4L2_CID_TUNE_DEEMPHASIS:
 676                return tune_emphasis;
 677        case V4L2_CID_FLASH_LED_MODE:
 678                return flash_led_mode;
 679        case V4L2_CID_FLASH_STROBE_SOURCE:
 680                return flash_strobe_source;
 681        case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
 682                return header_mode;
 683        case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
 684                return mpeg_video_frame_skip;
 685        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
 686                return multi_slice;
 687        case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
 688                return entropy_mode;
 689        case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
 690                return mpeg_h264_level;
 691        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
 692                return h264_loop_filter;
 693        case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
 694                return h264_profile;
 695        case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
 696                return vui_sar_idc;
 697        case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
 698                return h264_fp_arrangement_type;
 699        case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
 700                return h264_fmo_map_type;
 701        case V4L2_CID_STATELESS_H264_DECODE_MODE:
 702                return h264_decode_mode;
 703        case V4L2_CID_STATELESS_H264_START_CODE:
 704                return h264_start_code;
 705        case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE:
 706                return h264_hierarchical_coding_type;
 707        case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
 708                return mpeg_mpeg2_level;
 709        case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
 710                return mpeg2_profile;
 711        case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
 712                return mpeg_mpeg4_level;
 713        case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
 714                return mpeg4_profile;
 715        case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
 716                return vpx_golden_frame_sel;
 717        case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
 718                return vp8_profile;
 719        case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:
 720                return vp9_profile;
 721        case V4L2_CID_MPEG_VIDEO_VP9_LEVEL:
 722                return vp9_level;
 723        case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
 724                return jpeg_chroma_subsampling;
 725        case V4L2_CID_DV_TX_MODE:
 726                return dv_tx_mode;
 727        case V4L2_CID_DV_TX_RGB_RANGE:
 728        case V4L2_CID_DV_RX_RGB_RANGE:
 729                return dv_rgb_range;
 730        case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
 731        case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
 732                return dv_it_content_type;
 733        case V4L2_CID_DETECT_MD_MODE:
 734                return detect_md_mode;
 735        case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
 736                return hevc_profile;
 737        case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
 738                return hevc_level;
 739        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
 740                return hevc_hierarchial_coding_type;
 741        case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
 742                return hevc_refresh_type;
 743        case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
 744                return hevc_size_of_length_field;
 745        case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
 746                return hevc_tier;
 747        case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
 748                return hevc_loop_filter_mode;
 749        case V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE:
 750                return hevc_decode_mode;
 751        case V4L2_CID_MPEG_VIDEO_HEVC_START_CODE:
 752                return hevc_start_code;
 753        case V4L2_CID_CAMERA_ORIENTATION:
 754                return camera_orientation;
 755        default:
 756                return NULL;
 757        }
 758}
 759EXPORT_SYMBOL(v4l2_ctrl_get_menu);
 760
 761#define __v4l2_qmenu_int_len(arr, len) ({ *(len) = ARRAY_SIZE(arr); arr; })
 762/*
 763 * Returns NULL or an s64 type array containing the menu for given
 764 * control ID. The total number of the menu items is returned in @len.
 765 */
 766const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len)
 767{
 768        static const s64 qmenu_int_vpx_num_partitions[] = {
 769                1, 2, 4, 8,
 770        };
 771
 772        static const s64 qmenu_int_vpx_num_ref_frames[] = {
 773                1, 2, 3,
 774        };
 775
 776        switch (id) {
 777        case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
 778                return __v4l2_qmenu_int_len(qmenu_int_vpx_num_partitions, len);
 779        case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
 780                return __v4l2_qmenu_int_len(qmenu_int_vpx_num_ref_frames, len);
 781        default:
 782                *len = 0;
 783                return NULL;
 784        }
 785}
 786EXPORT_SYMBOL(v4l2_ctrl_get_int_menu);
 787
 788/* Return the control name. */
 789const char *v4l2_ctrl_get_name(u32 id)
 790{
 791        switch (id) {
 792        /* USER controls */
 793        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
 794        case V4L2_CID_USER_CLASS:               return "User Controls";
 795        case V4L2_CID_BRIGHTNESS:               return "Brightness";
 796        case V4L2_CID_CONTRAST:                 return "Contrast";
 797        case V4L2_CID_SATURATION:               return "Saturation";
 798        case V4L2_CID_HUE:                      return "Hue";
 799        case V4L2_CID_AUDIO_VOLUME:             return "Volume";
 800        case V4L2_CID_AUDIO_BALANCE:            return "Balance";
 801        case V4L2_CID_AUDIO_BASS:               return "Bass";
 802        case V4L2_CID_AUDIO_TREBLE:             return "Treble";
 803        case V4L2_CID_AUDIO_MUTE:               return "Mute";
 804        case V4L2_CID_AUDIO_LOUDNESS:           return "Loudness";
 805        case V4L2_CID_BLACK_LEVEL:              return "Black Level";
 806        case V4L2_CID_AUTO_WHITE_BALANCE:       return "White Balance, Automatic";
 807        case V4L2_CID_DO_WHITE_BALANCE:         return "Do White Balance";
 808        case V4L2_CID_RED_BALANCE:              return "Red Balance";
 809        case V4L2_CID_BLUE_BALANCE:             return "Blue Balance";
 810        case V4L2_CID_GAMMA:                    return "Gamma";
 811        case V4L2_CID_EXPOSURE:                 return "Exposure";
 812        case V4L2_CID_AUTOGAIN:                 return "Gain, Automatic";
 813        case V4L2_CID_GAIN:                     return "Gain";
 814        case V4L2_CID_HFLIP:                    return "Horizontal Flip";
 815        case V4L2_CID_VFLIP:                    return "Vertical Flip";
 816        case V4L2_CID_POWER_LINE_FREQUENCY:     return "Power Line Frequency";
 817        case V4L2_CID_HUE_AUTO:                 return "Hue, Automatic";
 818        case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
 819        case V4L2_CID_SHARPNESS:                return "Sharpness";
 820        case V4L2_CID_BACKLIGHT_COMPENSATION:   return "Backlight Compensation";
 821        case V4L2_CID_CHROMA_AGC:               return "Chroma AGC";
 822        case V4L2_CID_COLOR_KILLER:             return "Color Killer";
 823        case V4L2_CID_COLORFX:                  return "Color Effects";
 824        case V4L2_CID_AUTOBRIGHTNESS:           return "Brightness, Automatic";
 825        case V4L2_CID_BAND_STOP_FILTER:         return "Band-Stop Filter";
 826        case V4L2_CID_ROTATE:                   return "Rotate";
 827        case V4L2_CID_BG_COLOR:                 return "Background Color";
 828        case V4L2_CID_CHROMA_GAIN:              return "Chroma Gain";
 829        case V4L2_CID_ILLUMINATORS_1:           return "Illuminator 1";
 830        case V4L2_CID_ILLUMINATORS_2:           return "Illuminator 2";
 831        case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:  return "Min Number of Capture Buffers";
 832        case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:   return "Min Number of Output Buffers";
 833        case V4L2_CID_ALPHA_COMPONENT:          return "Alpha Component";
 834        case V4L2_CID_COLORFX_CBCR:             return "Color Effects, CbCr";
 835
 836        /* Codec controls */
 837        /* The MPEG controls are applicable to all codec controls
 838         * and the 'MPEG' part of the define is historical */
 839        /* Keep the order of the 'case's the same as in videodev2.h! */
 840        case V4L2_CID_CODEC_CLASS:              return "Codec Controls";
 841        case V4L2_CID_MPEG_STREAM_TYPE:         return "Stream Type";
 842        case V4L2_CID_MPEG_STREAM_PID_PMT:      return "Stream PMT Program ID";
 843        case V4L2_CID_MPEG_STREAM_PID_AUDIO:    return "Stream Audio Program ID";
 844        case V4L2_CID_MPEG_STREAM_PID_VIDEO:    return "Stream Video Program ID";
 845        case V4L2_CID_MPEG_STREAM_PID_PCR:      return "Stream PCR Program ID";
 846        case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
 847        case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
 848        case V4L2_CID_MPEG_STREAM_VBI_FMT:      return "Stream VBI Format";
 849        case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
 850        case V4L2_CID_MPEG_AUDIO_ENCODING:      return "Audio Encoding";
 851        case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    return "Audio Layer I Bitrate";
 852        case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    return "Audio Layer II Bitrate";
 853        case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    return "Audio Layer III Bitrate";
 854        case V4L2_CID_MPEG_AUDIO_MODE:          return "Audio Stereo Mode";
 855        case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
 856        case V4L2_CID_MPEG_AUDIO_EMPHASIS:      return "Audio Emphasis";
 857        case V4L2_CID_MPEG_AUDIO_CRC:           return "Audio CRC";
 858        case V4L2_CID_MPEG_AUDIO_MUTE:          return "Audio Mute";
 859        case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:   return "Audio AAC Bitrate";
 860        case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:   return "Audio AC-3 Bitrate";
 861        case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:  return "Audio Playback";
 862        case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
 863        case V4L2_CID_MPEG_VIDEO_ENCODING:      return "Video Encoding";
 864        case V4L2_CID_MPEG_VIDEO_ASPECT:        return "Video Aspect";
 865        case V4L2_CID_MPEG_VIDEO_B_FRAMES:      return "Video B Frames";
 866        case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      return "Video GOP Size";
 867        case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   return "Video GOP Closure";
 868        case V4L2_CID_MPEG_VIDEO_PULLDOWN:      return "Video Pulldown";
 869        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  return "Video Bitrate Mode";
 870        case V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY:      return "Constant Quality";
 871        case V4L2_CID_MPEG_VIDEO_BITRATE:       return "Video Bitrate";
 872        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  return "Video Peak Bitrate";
 873        case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
 874        case V4L2_CID_MPEG_VIDEO_MUTE:          return "Video Mute";
 875        case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      return "Video Mute YUV";
 876        case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:       return "Decoder Slice Interface";
 877        case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:  return "MPEG4 Loop Filter Enable";
 878        case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:       return "Number of Intra Refresh MBs";
 879        case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:               return "Frame Level Rate Control Enable";
 880        case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:                  return "H264 MB Level Rate Control";
 881        case V4L2_CID_MPEG_VIDEO_HEADER_MODE:                   return "Sequence Header Mode";
 882        case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC:                   return "Max Number of Reference Pics";
 883        case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:               return "Frame Skip Mode";
 884        case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:             return "Display Delay";
 885        case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:      return "Display Delay Enable";
 886        case V4L2_CID_MPEG_VIDEO_AU_DELIMITER:                  return "Generate Access Unit Delimiters";
 887        case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:               return "H263 I-Frame QP Value";
 888        case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:               return "H263 P-Frame QP Value";
 889        case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:               return "H263 B-Frame QP Value";
 890        case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:                   return "H263 Minimum QP Value";
 891        case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:                   return "H263 Maximum QP Value";
 892        case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:               return "H264 I-Frame QP Value";
 893        case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:               return "H264 P-Frame QP Value";
 894        case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:               return "H264 B-Frame QP Value";
 895        case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:                   return "H264 Maximum QP Value";
 896        case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:                   return "H264 Minimum QP Value";
 897        case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:            return "H264 8x8 Transform Enable";
 898        case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:                 return "H264 CPB Buffer Size";
 899        case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:             return "H264 Entropy Mode";
 900        case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:                 return "H264 I-Frame Period";
 901        case V4L2_CID_MPEG_VIDEO_H264_LEVEL:                    return "H264 Level";
 902        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:        return "H264 Loop Filter Alpha Offset";
 903        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:         return "H264 Loop Filter Beta Offset";
 904        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:         return "H264 Loop Filter Mode";
 905        case V4L2_CID_MPEG_VIDEO_H264_PROFILE:                  return "H264 Profile";
 906        case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:       return "Vertical Size of SAR";
 907        case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:        return "Horizontal Size of SAR";
 908        case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:           return "Aspect Ratio VUI Enable";
 909        case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:              return "VUI Aspect Ratio IDC";
 910        case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:        return "H264 Enable Frame Packing SEI";
 911        case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_CURRENT_FRAME_0:   return "H264 Set Curr. Frame as Frame0";
 912        case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:  return "H264 FP Arrangement Type";
 913        case V4L2_CID_MPEG_VIDEO_H264_FMO:                      return "H264 Flexible MB Ordering";
 914        case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:             return "H264 Map Type for FMO";
 915        case V4L2_CID_MPEG_VIDEO_H264_FMO_SLICE_GROUP:          return "H264 FMO Number of Slice Groups";
 916        case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_DIRECTION:     return "H264 FMO Direction of Change";
 917        case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_RATE:          return "H264 FMO Size of 1st Slice Grp";
 918        case V4L2_CID_MPEG_VIDEO_H264_FMO_RUN_LENGTH:           return "H264 FMO No. of Consecutive MBs";
 919        case V4L2_CID_MPEG_VIDEO_H264_ASO:                      return "H264 Arbitrary Slice Ordering";
 920        case V4L2_CID_MPEG_VIDEO_H264_ASO_SLICE_ORDER:          return "H264 ASO Slice Order";
 921        case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING:      return "Enable H264 Hierarchical Coding";
 922        case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE: return "H264 Hierarchical Coding Type";
 923        case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER:return "H264 Number of HC Layers";
 924        case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER_QP:
 925                                                                return "H264 Set QP Value for HC Layers";
 926        case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
 927                                                                return "H264 Constrained Intra Pred";
 928        case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:   return "H264 Chroma QP Index Offset";
 929        case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MIN_QP:           return "H264 I-Frame Minimum QP Value";
 930        case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MAX_QP:           return "H264 I-Frame Maximum QP Value";
 931        case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MIN_QP:           return "H264 P-Frame Minimum QP Value";
 932        case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MAX_QP:           return "H264 P-Frame Maximum QP Value";
 933        case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MIN_QP:           return "H264 B-Frame Minimum QP Value";
 934        case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MAX_QP:           return "H264 B-Frame Maximum QP Value";
 935        case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L0_BR:        return "H264 Hierarchical Lay 0 Bitrate";
 936        case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L1_BR:        return "H264 Hierarchical Lay 1 Bitrate";
 937        case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L2_BR:        return "H264 Hierarchical Lay 2 Bitrate";
 938        case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L3_BR:        return "H264 Hierarchical Lay 3 Bitrate";
 939        case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L4_BR:        return "H264 Hierarchical Lay 4 Bitrate";
 940        case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L5_BR:        return "H264 Hierarchical Lay 5 Bitrate";
 941        case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L6_BR:        return "H264 Hierarchical Lay 6 Bitrate";
 942        case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:                   return "MPEG2 Level";
 943        case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:                 return "MPEG2 Profile";
 944        case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:              return "MPEG4 I-Frame QP Value";
 945        case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:              return "MPEG4 P-Frame QP Value";
 946        case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:              return "MPEG4 B-Frame QP Value";
 947        case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:                  return "MPEG4 Minimum QP Value";
 948        case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:                  return "MPEG4 Maximum QP Value";
 949        case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:                   return "MPEG4 Level";
 950        case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:                 return "MPEG4 Profile";
 951        case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:                    return "Quarter Pixel Search Enable";
 952        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:         return "Maximum Bytes in a Slice";
 953        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:            return "Number of MBs in a Slice";
 954        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:              return "Slice Partitioning Method";
 955        case V4L2_CID_MPEG_VIDEO_VBV_SIZE:                      return "VBV Buffer Size";
 956        case V4L2_CID_MPEG_VIDEO_DEC_PTS:                       return "Video Decoder PTS";
 957        case V4L2_CID_MPEG_VIDEO_DEC_FRAME:                     return "Video Decoder Frame Count";
 958        case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:             return "Video Decoder Conceal Color";
 959        case V4L2_CID_MPEG_VIDEO_VBV_DELAY:                     return "Initial Delay for VBV Control";
 960        case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:             return "Horizontal MV Search Range";
 961        case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:             return "Vertical MV Search Range";
 962        case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:             return "Repeat Sequence Header";
 963        case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:               return "Force Key Frame";
 964        case V4L2_CID_MPEG_VIDEO_BASELAYER_PRIORITY_ID:         return "Base Layer Priority ID";
 965        case V4L2_CID_MPEG_VIDEO_LTR_COUNT:                     return "LTR Count";
 966        case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:               return "Frame LTR Index";
 967        case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:                return "Use LTR Frames";
 968        case V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS:            return "MPEG-2 Slice Parameters";
 969        case V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION:            return "MPEG-2 Quantization Matrices";
 970        case V4L2_CID_FWHT_I_FRAME_QP:                          return "FWHT I-Frame QP Value";
 971        case V4L2_CID_FWHT_P_FRAME_QP:                          return "FWHT P-Frame QP Value";
 972
 973        /* VPX controls */
 974        case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:            return "VPX Number of Partitions";
 975        case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:           return "VPX Intra Mode Decision Disable";
 976        case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:            return "VPX No. of Refs for P Frame";
 977        case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:              return "VPX Loop Filter Level Range";
 978        case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:          return "VPX Deblocking Effect Control";
 979        case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:   return "VPX Golden Frame Refresh Period";
 980        case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:          return "VPX Golden Frame Indicator";
 981        case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:                    return "VPX Minimum QP Value";
 982        case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:                    return "VPX Maximum QP Value";
 983        case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:                return "VPX I-Frame QP Value";
 984        case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:                return "VPX P-Frame QP Value";
 985        case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:                   return "VP8 Profile";
 986        case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:                   return "VP9 Profile";
 987        case V4L2_CID_MPEG_VIDEO_VP9_LEVEL:                     return "VP9 Level";
 988
 989        /* HEVC controls */
 990        case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:               return "HEVC I-Frame QP Value";
 991        case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP:               return "HEVC P-Frame QP Value";
 992        case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP:               return "HEVC B-Frame QP Value";
 993        case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP:                   return "HEVC Minimum QP Value";
 994        case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP:                   return "HEVC Maximum QP Value";
 995        case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QP:           return "HEVC I-Frame Minimum QP Value";
 996        case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QP:           return "HEVC I-Frame Maximum QP Value";
 997        case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MIN_QP:           return "HEVC P-Frame Minimum QP Value";
 998        case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MAX_QP:           return "HEVC P-Frame Maximum QP Value";
 999        case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MIN_QP:           return "HEVC B-Frame Minimum QP Value";
1000        case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MAX_QP:           return "HEVC B-Frame Maximum QP Value";
1001        case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:                  return "HEVC Profile";
1002        case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:                    return "HEVC Level";
1003        case V4L2_CID_MPEG_VIDEO_HEVC_TIER:                     return "HEVC Tier";
1004        case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION:    return "HEVC Frame Rate Resolution";
1005        case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH:      return "HEVC Maximum Coding Unit Depth";
1006        case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:             return "HEVC Refresh Type";
1007        case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED:         return "HEVC Constant Intra Prediction";
1008        case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU:              return "HEVC Lossless Encoding";
1009        case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT:                return "HEVC Wavefront";
1010        case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:         return "HEVC Loop Filter";
1011        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP:                  return "HEVC QP Values";
1012        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:         return "HEVC Hierarchical Coding Type";
1013        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:        return "HEVC Hierarchical Coding Layer";
1014        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP:        return "HEVC Hierarchical Layer 0 QP";
1015        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP:        return "HEVC Hierarchical Layer 1 QP";
1016        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP:        return "HEVC Hierarchical Layer 2 QP";
1017        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP:        return "HEVC Hierarchical Layer 3 QP";
1018        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP:        return "HEVC Hierarchical Layer 4 QP";
1019        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP:        return "HEVC Hierarchical Layer 5 QP";
1020        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP:        return "HEVC Hierarchical Layer 6 QP";
1021        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:        return "HEVC Hierarchical Lay 0 BitRate";
1022        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:        return "HEVC Hierarchical Lay 1 BitRate";
1023        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:        return "HEVC Hierarchical Lay 2 BitRate";
1024        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:        return "HEVC Hierarchical Lay 3 BitRate";
1025        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:        return "HEVC Hierarchical Lay 4 BitRate";
1026        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:        return "HEVC Hierarchical Lay 5 BitRate";
1027        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR:        return "HEVC Hierarchical Lay 6 BitRate";
1028        case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB:               return "HEVC General PB";
1029        case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID:              return "HEVC Temporal ID";
1030        case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING:         return "HEVC Strong Intra Smoothing";
1031        case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT:           return "HEVC Intra PU Split";
1032        case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION:           return "HEVC TMV Prediction";
1033        case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1:  return "HEVC Max Num of Candidate MVs";
1034        case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE:        return "HEVC ENC Without Startcode";
1035        case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD:           return "HEVC Num of I-Frame b/w 2 IDR";
1036        case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2:      return "HEVC Loop Filter Beta Offset";
1037        case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2:        return "HEVC Loop Filter TC Offset";
1038        case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:     return "HEVC Size of Length Field";
1039        case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:        return "Reference Frames for a P-Frame";
1040        case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:         return "Prepend SPS and PPS to IDR";
1041        case V4L2_CID_MPEG_VIDEO_HEVC_SPS:                      return "HEVC Sequence Parameter Set";
1042        case V4L2_CID_MPEG_VIDEO_HEVC_PPS:                      return "HEVC Picture Parameter Set";
1043        case V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS:             return "HEVC Slice Parameters";
1044        case V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE:              return "HEVC Decode Mode";
1045        case V4L2_CID_MPEG_VIDEO_HEVC_START_CODE:               return "HEVC Start Code";
1046
1047        /* CAMERA controls */
1048        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1049        case V4L2_CID_CAMERA_CLASS:             return "Camera Controls";
1050        case V4L2_CID_EXPOSURE_AUTO:            return "Auto Exposure";
1051        case V4L2_CID_EXPOSURE_ABSOLUTE:        return "Exposure Time, Absolute";
1052        case V4L2_CID_EXPOSURE_AUTO_PRIORITY:   return "Exposure, Dynamic Framerate";
1053        case V4L2_CID_PAN_RELATIVE:             return "Pan, Relative";
1054        case V4L2_CID_TILT_RELATIVE:            return "Tilt, Relative";
1055        case V4L2_CID_PAN_RESET:                return "Pan, Reset";
1056        case V4L2_CID_TILT_RESET:               return "Tilt, Reset";
1057        case V4L2_CID_PAN_ABSOLUTE:             return "Pan, Absolute";
1058        case V4L2_CID_TILT_ABSOLUTE:            return "Tilt, Absolute";
1059        case V4L2_CID_FOCUS_ABSOLUTE:           return "Focus, Absolute";
1060        case V4L2_CID_FOCUS_RELATIVE:           return "Focus, Relative";
1061        case V4L2_CID_FOCUS_AUTO:               return "Focus, Automatic Continuous";
1062        case V4L2_CID_ZOOM_ABSOLUTE:            return "Zoom, Absolute";
1063        case V4L2_CID_ZOOM_RELATIVE:            return "Zoom, Relative";
1064        case V4L2_CID_ZOOM_CONTINUOUS:          return "Zoom, Continuous";
1065        case V4L2_CID_PRIVACY:                  return "Privacy";
1066        case V4L2_CID_IRIS_ABSOLUTE:            return "Iris, Absolute";
1067        case V4L2_CID_IRIS_RELATIVE:            return "Iris, Relative";
1068        case V4L2_CID_AUTO_EXPOSURE_BIAS:       return "Auto Exposure, Bias";
1069        case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
1070        case V4L2_CID_WIDE_DYNAMIC_RANGE:       return "Wide Dynamic Range";
1071        case V4L2_CID_IMAGE_STABILIZATION:      return "Image Stabilization";
1072        case V4L2_CID_ISO_SENSITIVITY:          return "ISO Sensitivity";
1073        case V4L2_CID_ISO_SENSITIVITY_AUTO:     return "ISO Sensitivity, Auto";
1074        case V4L2_CID_EXPOSURE_METERING:        return "Exposure, Metering Mode";
1075        case V4L2_CID_SCENE_MODE:               return "Scene Mode";
1076        case V4L2_CID_3A_LOCK:                  return "3A Lock";
1077        case V4L2_CID_AUTO_FOCUS_START:         return "Auto Focus, Start";
1078        case V4L2_CID_AUTO_FOCUS_STOP:          return "Auto Focus, Stop";
1079        case V4L2_CID_AUTO_FOCUS_STATUS:        return "Auto Focus, Status";
1080        case V4L2_CID_AUTO_FOCUS_RANGE:         return "Auto Focus, Range";
1081        case V4L2_CID_PAN_SPEED:                return "Pan, Speed";
1082        case V4L2_CID_TILT_SPEED:               return "Tilt, Speed";
1083        case V4L2_CID_UNIT_CELL_SIZE:           return "Unit Cell Size";
1084        case V4L2_CID_CAMERA_ORIENTATION:       return "Camera Orientation";
1085        case V4L2_CID_CAMERA_SENSOR_ROTATION:   return "Camera Sensor Rotation";
1086
1087        /* FM Radio Modulator controls */
1088        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1089        case V4L2_CID_FM_TX_CLASS:              return "FM Radio Modulator Controls";
1090        case V4L2_CID_RDS_TX_DEVIATION:         return "RDS Signal Deviation";
1091        case V4L2_CID_RDS_TX_PI:                return "RDS Program ID";
1092        case V4L2_CID_RDS_TX_PTY:               return "RDS Program Type";
1093        case V4L2_CID_RDS_TX_PS_NAME:           return "RDS PS Name";
1094        case V4L2_CID_RDS_TX_RADIO_TEXT:        return "RDS Radio Text";
1095        case V4L2_CID_RDS_TX_MONO_STEREO:       return "RDS Stereo";
1096        case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:   return "RDS Artificial Head";
1097        case V4L2_CID_RDS_TX_COMPRESSED:        return "RDS Compressed";
1098        case V4L2_CID_RDS_TX_DYNAMIC_PTY:       return "RDS Dynamic PTY";
1099        case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
1100        case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:   return "RDS Traffic Program";
1101        case V4L2_CID_RDS_TX_MUSIC_SPEECH:      return "RDS Music";
1102        case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:  return "RDS Enable Alt Frequencies";
1103        case V4L2_CID_RDS_TX_ALT_FREQS:         return "RDS Alternate Frequencies";
1104        case V4L2_CID_AUDIO_LIMITER_ENABLED:    return "Audio Limiter Feature Enabled";
1105        case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
1106        case V4L2_CID_AUDIO_LIMITER_DEVIATION:  return "Audio Limiter Deviation";
1107        case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
1108        case V4L2_CID_AUDIO_COMPRESSION_GAIN:   return "Audio Compression Gain";
1109        case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
1110        case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
1111        case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
1112        case V4L2_CID_PILOT_TONE_ENABLED:       return "Pilot Tone Feature Enabled";
1113        case V4L2_CID_PILOT_TONE_DEVIATION:     return "Pilot Tone Deviation";
1114        case V4L2_CID_PILOT_TONE_FREQUENCY:     return "Pilot Tone Frequency";
1115        case V4L2_CID_TUNE_PREEMPHASIS:         return "Pre-Emphasis";
1116        case V4L2_CID_TUNE_POWER_LEVEL:         return "Tune Power Level";
1117        case V4L2_CID_TUNE_ANTENNA_CAPACITOR:   return "Tune Antenna Capacitor";
1118
1119        /* Flash controls */
1120        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1121        case V4L2_CID_FLASH_CLASS:              return "Flash Controls";
1122        case V4L2_CID_FLASH_LED_MODE:           return "LED Mode";
1123        case V4L2_CID_FLASH_STROBE_SOURCE:      return "Strobe Source";
1124        case V4L2_CID_FLASH_STROBE:             return "Strobe";
1125        case V4L2_CID_FLASH_STROBE_STOP:        return "Stop Strobe";
1126        case V4L2_CID_FLASH_STROBE_STATUS:      return "Strobe Status";
1127        case V4L2_CID_FLASH_TIMEOUT:            return "Strobe Timeout";
1128        case V4L2_CID_FLASH_INTENSITY:          return "Intensity, Flash Mode";
1129        case V4L2_CID_FLASH_TORCH_INTENSITY:    return "Intensity, Torch Mode";
1130        case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
1131        case V4L2_CID_FLASH_FAULT:              return "Faults";
1132        case V4L2_CID_FLASH_CHARGE:             return "Charge";
1133        case V4L2_CID_FLASH_READY:              return "Ready to Strobe";
1134
1135        /* JPEG encoder controls */
1136        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1137        case V4L2_CID_JPEG_CLASS:               return "JPEG Compression Controls";
1138        case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:  return "Chroma Subsampling";
1139        case V4L2_CID_JPEG_RESTART_INTERVAL:    return "Restart Interval";
1140        case V4L2_CID_JPEG_COMPRESSION_QUALITY: return "Compression Quality";
1141        case V4L2_CID_JPEG_ACTIVE_MARKER:       return "Active Markers";
1142
1143        /* Image source controls */
1144        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1145        case V4L2_CID_IMAGE_SOURCE_CLASS:       return "Image Source Controls";
1146        case V4L2_CID_VBLANK:                   return "Vertical Blanking";
1147        case V4L2_CID_HBLANK:                   return "Horizontal Blanking";
1148        case V4L2_CID_ANALOGUE_GAIN:            return "Analogue Gain";
1149        case V4L2_CID_TEST_PATTERN_RED:         return "Red Pixel Value";
1150        case V4L2_CID_TEST_PATTERN_GREENR:      return "Green (Red) Pixel Value";
1151        case V4L2_CID_TEST_PATTERN_BLUE:        return "Blue Pixel Value";
1152        case V4L2_CID_TEST_PATTERN_GREENB:      return "Green (Blue) Pixel Value";
1153
1154        /* Image processing controls */
1155        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1156        case V4L2_CID_IMAGE_PROC_CLASS:         return "Image Processing Controls";
1157        case V4L2_CID_LINK_FREQ:                return "Link Frequency";
1158        case V4L2_CID_PIXEL_RATE:               return "Pixel Rate";
1159        case V4L2_CID_TEST_PATTERN:             return "Test Pattern";
1160        case V4L2_CID_DEINTERLACING_MODE:       return "Deinterlacing Mode";
1161        case V4L2_CID_DIGITAL_GAIN:             return "Digital Gain";
1162
1163        /* DV controls */
1164        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1165        case V4L2_CID_DV_CLASS:                 return "Digital Video Controls";
1166        case V4L2_CID_DV_TX_HOTPLUG:            return "Hotplug Present";
1167        case V4L2_CID_DV_TX_RXSENSE:            return "RxSense Present";
1168        case V4L2_CID_DV_TX_EDID_PRESENT:       return "EDID Present";
1169        case V4L2_CID_DV_TX_MODE:               return "Transmit Mode";
1170        case V4L2_CID_DV_TX_RGB_RANGE:          return "Tx RGB Quantization Range";
1171        case V4L2_CID_DV_TX_IT_CONTENT_TYPE:    return "Tx IT Content Type";
1172        case V4L2_CID_DV_RX_POWER_PRESENT:      return "Power Present";
1173        case V4L2_CID_DV_RX_RGB_RANGE:          return "Rx RGB Quantization Range";
1174        case V4L2_CID_DV_RX_IT_CONTENT_TYPE:    return "Rx IT Content Type";
1175
1176        case V4L2_CID_FM_RX_CLASS:              return "FM Radio Receiver Controls";
1177        case V4L2_CID_TUNE_DEEMPHASIS:          return "De-Emphasis";
1178        case V4L2_CID_RDS_RECEPTION:            return "RDS Reception";
1179        case V4L2_CID_RF_TUNER_CLASS:           return "RF Tuner Controls";
1180        case V4L2_CID_RF_TUNER_RF_GAIN:         return "RF Gain";
1181        case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:   return "LNA Gain, Auto";
1182        case V4L2_CID_RF_TUNER_LNA_GAIN:        return "LNA Gain";
1183        case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO: return "Mixer Gain, Auto";
1184        case V4L2_CID_RF_TUNER_MIXER_GAIN:      return "Mixer Gain";
1185        case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:    return "IF Gain, Auto";
1186        case V4L2_CID_RF_TUNER_IF_GAIN:         return "IF Gain";
1187        case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:  return "Bandwidth, Auto";
1188        case V4L2_CID_RF_TUNER_BANDWIDTH:       return "Bandwidth";
1189        case V4L2_CID_RF_TUNER_PLL_LOCK:        return "PLL Lock";
1190        case V4L2_CID_RDS_RX_PTY:               return "RDS Program Type";
1191        case V4L2_CID_RDS_RX_PS_NAME:           return "RDS PS Name";
1192        case V4L2_CID_RDS_RX_RADIO_TEXT:        return "RDS Radio Text";
1193        case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
1194        case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:   return "RDS Traffic Program";
1195        case V4L2_CID_RDS_RX_MUSIC_SPEECH:      return "RDS Music";
1196
1197        /* Detection controls */
1198        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1199        case V4L2_CID_DETECT_CLASS:             return "Detection Controls";
1200        case V4L2_CID_DETECT_MD_MODE:           return "Motion Detection Mode";
1201        case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD: return "MD Global Threshold";
1202        case V4L2_CID_DETECT_MD_THRESHOLD_GRID: return "MD Threshold Grid";
1203        case V4L2_CID_DETECT_MD_REGION_GRID:    return "MD Region Grid";
1204
1205        /* Stateless Codec controls */
1206        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1207        case V4L2_CID_CODEC_STATELESS_CLASS:    return "Stateless Codec Controls";
1208        case V4L2_CID_STATELESS_H264_DECODE_MODE:               return "H264 Decode Mode";
1209        case V4L2_CID_STATELESS_H264_START_CODE:                return "H264 Start Code";
1210        case V4L2_CID_STATELESS_H264_SPS:                       return "H264 Sequence Parameter Set";
1211        case V4L2_CID_STATELESS_H264_PPS:                       return "H264 Picture Parameter Set";
1212        case V4L2_CID_STATELESS_H264_SCALING_MATRIX:            return "H264 Scaling Matrix";
1213        case V4L2_CID_STATELESS_H264_PRED_WEIGHTS:              return "H264 Prediction Weight Table";
1214        case V4L2_CID_STATELESS_H264_SLICE_PARAMS:              return "H264 Slice Parameters";
1215        case V4L2_CID_STATELESS_H264_DECODE_PARAMS:             return "H264 Decode Parameters";
1216        case V4L2_CID_STATELESS_FWHT_PARAMS:                    return "FWHT Stateless Parameters";
1217        case V4L2_CID_STATELESS_VP8_FRAME:                      return "VP8 Frame Parameters";
1218
1219        /* Colorimetry controls */
1220        /* Keep the order of the 'case's the same as in v4l2-controls.h! */
1221        case V4L2_CID_COLORIMETRY_CLASS:        return "Colorimetry Controls";
1222        case V4L2_CID_COLORIMETRY_HDR10_CLL_INFO:               return "HDR10 Content Light Info";
1223        case V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY:      return "HDR10 Mastering Display";
1224        default:
1225                return NULL;
1226        }
1227}
1228EXPORT_SYMBOL(v4l2_ctrl_get_name);
1229
1230void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
1231                    s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags)
1232{
1233        *name = v4l2_ctrl_get_name(id);
1234        *flags = 0;
1235
1236        switch (id) {
1237        case V4L2_CID_AUDIO_MUTE:
1238        case V4L2_CID_AUDIO_LOUDNESS:
1239        case V4L2_CID_AUTO_WHITE_BALANCE:
1240        case V4L2_CID_AUTOGAIN:
1241        case V4L2_CID_HFLIP:
1242        case V4L2_CID_VFLIP:
1243        case V4L2_CID_HUE_AUTO:
1244        case V4L2_CID_CHROMA_AGC:
1245        case V4L2_CID_COLOR_KILLER:
1246        case V4L2_CID_AUTOBRIGHTNESS:
1247        case V4L2_CID_MPEG_AUDIO_MUTE:
1248        case V4L2_CID_MPEG_VIDEO_MUTE:
1249        case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1250        case V4L2_CID_MPEG_VIDEO_PULLDOWN:
1251        case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1252        case V4L2_CID_FOCUS_AUTO:
1253        case V4L2_CID_PRIVACY:
1254        case V4L2_CID_AUDIO_LIMITER_ENABLED:
1255        case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1256        case V4L2_CID_PILOT_TONE_ENABLED:
1257        case V4L2_CID_ILLUMINATORS_1:
1258        case V4L2_CID_ILLUMINATORS_2:
1259        case V4L2_CID_FLASH_STROBE_STATUS:
1260        case V4L2_CID_FLASH_CHARGE:
1261        case V4L2_CID_FLASH_READY:
1262        case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
1263        case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
1264        case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
1265        case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1266        case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1267        case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1268        case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1269        case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1270        case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:
1271        case V4L2_CID_MPEG_VIDEO_AU_DELIMITER:
1272        case V4L2_CID_WIDE_DYNAMIC_RANGE:
1273        case V4L2_CID_IMAGE_STABILIZATION:
1274        case V4L2_CID_RDS_RECEPTION:
1275        case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
1276        case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
1277        case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
1278        case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1279        case V4L2_CID_RF_TUNER_PLL_LOCK:
1280        case V4L2_CID_RDS_TX_MONO_STEREO:
1281        case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
1282        case V4L2_CID_RDS_TX_COMPRESSED:
1283        case V4L2_CID_RDS_TX_DYNAMIC_PTY:
1284        case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
1285        case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
1286        case V4L2_CID_RDS_TX_MUSIC_SPEECH:
1287        case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
1288        case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
1289        case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
1290        case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1291                *type = V4L2_CTRL_TYPE_BOOLEAN;
1292                *min = 0;
1293                *max = *step = 1;
1294                break;
1295        case V4L2_CID_ROTATE:
1296                *type = V4L2_CTRL_TYPE_INTEGER;
1297                *flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1298                break;
1299        case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1300        case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1301        case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
1302                *type = V4L2_CTRL_TYPE_INTEGER;
1303                break;
1304        case V4L2_CID_MPEG_VIDEO_LTR_COUNT:
1305                *type = V4L2_CTRL_TYPE_INTEGER;
1306                break;
1307        case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:
1308                *type = V4L2_CTRL_TYPE_INTEGER;
1309                *flags |= V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1310                break;
1311        case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:
1312                *type = V4L2_CTRL_TYPE_BITMASK;
1313                *flags |= V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1314                break;
1315        case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1316        case V4L2_CID_PAN_RESET:
1317        case V4L2_CID_TILT_RESET:
1318        case V4L2_CID_FLASH_STROBE:
1319        case V4L2_CID_FLASH_STROBE_STOP:
1320        case V4L2_CID_AUTO_FOCUS_START:
1321        case V4L2_CID_AUTO_FOCUS_STOP:
1322        case V4L2_CID_DO_WHITE_BALANCE:
1323                *type = V4L2_CTRL_TYPE_BUTTON;
1324                *flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1325                          V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1326                *min = *max = *step = *def = 0;
1327                break;
1328        case V4L2_CID_POWER_LINE_FREQUENCY:
1329        case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
1330        case V4L2_CID_MPEG_AUDIO_ENCODING:
1331        case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
1332        case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
1333        case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
1334        case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
1335        case V4L2_CID_MPEG_AUDIO_MODE:
1336        case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
1337        case V4L2_CID_MPEG_AUDIO_EMPHASIS:
1338        case V4L2_CID_MPEG_AUDIO_CRC:
1339        case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
1340        case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
1341        case V4L2_CID_MPEG_VIDEO_ENCODING:
1342        case V4L2_CID_MPEG_VIDEO_ASPECT:
1343        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1344        case V4L2_CID_MPEG_STREAM_TYPE:
1345        case V4L2_CID_MPEG_STREAM_VBI_FMT:
1346        case V4L2_CID_EXPOSURE_AUTO:
1347        case V4L2_CID_AUTO_FOCUS_RANGE:
1348        case V4L2_CID_COLORFX:
1349        case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1350        case V4L2_CID_TUNE_PREEMPHASIS:
1351        case V4L2_CID_FLASH_LED_MODE:
1352        case V4L2_CID_FLASH_STROBE_SOURCE:
1353        case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1354        case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
1355        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1356        case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1357        case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1358        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1359        case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1360        case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1361        case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
1362        case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
1363        case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE:
1364        case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
1365        case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
1366        case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1367        case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1368        case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1369        case V4L2_CID_ISO_SENSITIVITY_AUTO:
1370        case V4L2_CID_EXPOSURE_METERING:
1371        case V4L2_CID_SCENE_MODE:
1372        case V4L2_CID_DV_TX_MODE:
1373        case V4L2_CID_DV_TX_RGB_RANGE:
1374        case V4L2_CID_DV_TX_IT_CONTENT_TYPE:
1375        case V4L2_CID_DV_RX_RGB_RANGE:
1376        case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1377        case V4L2_CID_TEST_PATTERN:
1378        case V4L2_CID_DEINTERLACING_MODE:
1379        case V4L2_CID_TUNE_DEEMPHASIS:
1380        case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
1381        case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
1382        case V4L2_CID_MPEG_VIDEO_VP9_PROFILE:
1383        case V4L2_CID_MPEG_VIDEO_VP9_LEVEL:
1384        case V4L2_CID_DETECT_MD_MODE:
1385        case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
1386        case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
1387        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
1388        case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
1389        case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
1390        case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
1391        case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
1392        case V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE:
1393        case V4L2_CID_MPEG_VIDEO_HEVC_START_CODE:
1394        case V4L2_CID_STATELESS_H264_DECODE_MODE:
1395        case V4L2_CID_STATELESS_H264_START_CODE:
1396        case V4L2_CID_CAMERA_ORIENTATION:
1397                *type = V4L2_CTRL_TYPE_MENU;
1398                break;
1399        case V4L2_CID_LINK_FREQ:
1400                *type = V4L2_CTRL_TYPE_INTEGER_MENU;
1401                break;
1402        case V4L2_CID_RDS_TX_PS_NAME:
1403        case V4L2_CID_RDS_TX_RADIO_TEXT:
1404        case V4L2_CID_RDS_RX_PS_NAME:
1405        case V4L2_CID_RDS_RX_RADIO_TEXT:
1406                *type = V4L2_CTRL_TYPE_STRING;
1407                break;
1408        case V4L2_CID_ISO_SENSITIVITY:
1409        case V4L2_CID_AUTO_EXPOSURE_BIAS:
1410        case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
1411        case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
1412                *type = V4L2_CTRL_TYPE_INTEGER_MENU;
1413                break;
1414        case V4L2_CID_USER_CLASS:
1415        case V4L2_CID_CAMERA_CLASS:
1416        case V4L2_CID_CODEC_CLASS:
1417        case V4L2_CID_FM_TX_CLASS:
1418        case V4L2_CID_FLASH_CLASS:
1419        case V4L2_CID_JPEG_CLASS:
1420        case V4L2_CID_IMAGE_SOURCE_CLASS:
1421        case V4L2_CID_IMAGE_PROC_CLASS:
1422        case V4L2_CID_DV_CLASS:
1423        case V4L2_CID_FM_RX_CLASS:
1424        case V4L2_CID_RF_TUNER_CLASS:
1425        case V4L2_CID_DETECT_CLASS:
1426        case V4L2_CID_CODEC_STATELESS_CLASS:
1427        case V4L2_CID_COLORIMETRY_CLASS:
1428                *type = V4L2_CTRL_TYPE_CTRL_CLASS;
1429                /* You can neither read nor write these */
1430                *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
1431                *min = *max = *step = *def = 0;
1432                break;
1433        case V4L2_CID_BG_COLOR:
1434                *type = V4L2_CTRL_TYPE_INTEGER;
1435                *step = 1;
1436                *min = 0;
1437                /* Max is calculated as RGB888 that is 2^24 */
1438                *max = 0xFFFFFF;
1439                break;
1440        case V4L2_CID_FLASH_FAULT:
1441        case V4L2_CID_JPEG_ACTIVE_MARKER:
1442        case V4L2_CID_3A_LOCK:
1443        case V4L2_CID_AUTO_FOCUS_STATUS:
1444        case V4L2_CID_DV_TX_HOTPLUG:
1445        case V4L2_CID_DV_TX_RXSENSE:
1446        case V4L2_CID_DV_TX_EDID_PRESENT:
1447        case V4L2_CID_DV_RX_POWER_PRESENT:
1448                *type = V4L2_CTRL_TYPE_BITMASK;
1449                break;
1450        case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1451        case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
1452                *type = V4L2_CTRL_TYPE_INTEGER;
1453                *flags |= V4L2_CTRL_FLAG_READ_ONLY;
1454                break;
1455        case V4L2_CID_MPEG_VIDEO_DEC_PTS:
1456                *type = V4L2_CTRL_TYPE_INTEGER64;
1457                *flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
1458                *min = *def = 0;
1459                *max = 0x1ffffffffLL;
1460                *step = 1;
1461                break;
1462        case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
1463                *type = V4L2_CTRL_TYPE_INTEGER64;
1464                *flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
1465                *min = *def = 0;
1466                *max = 0x7fffffffffffffffLL;
1467                *step = 1;
1468                break;
1469        case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
1470                *type = V4L2_CTRL_TYPE_INTEGER64;
1471                *min = 0;
1472                /* default for 8 bit black, luma is 16, chroma is 128 */
1473                *def = 0x8000800010LL;
1474                *max = 0xffffffffffffLL;
1475                *step = 1;
1476                break;
1477        case V4L2_CID_PIXEL_RATE:
1478                *type = V4L2_CTRL_TYPE_INTEGER64;
1479                *flags |= V4L2_CTRL_FLAG_READ_ONLY;
1480                break;
1481        case V4L2_CID_DETECT_MD_REGION_GRID:
1482                *type = V4L2_CTRL_TYPE_U8;
1483                break;
1484        case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
1485                *type = V4L2_CTRL_TYPE_U16;
1486                break;
1487        case V4L2_CID_RDS_TX_ALT_FREQS:
1488                *type = V4L2_CTRL_TYPE_U32;
1489                break;
1490        case V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS:
1491                *type = V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS;
1492                break;
1493        case V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION:
1494                *type = V4L2_CTRL_TYPE_MPEG2_QUANTIZATION;
1495                break;
1496        case V4L2_CID_STATELESS_FWHT_PARAMS:
1497                *type = V4L2_CTRL_TYPE_FWHT_PARAMS;
1498                break;
1499        case V4L2_CID_STATELESS_H264_SPS:
1500                *type = V4L2_CTRL_TYPE_H264_SPS;
1501                break;
1502        case V4L2_CID_STATELESS_H264_PPS:
1503                *type = V4L2_CTRL_TYPE_H264_PPS;
1504                break;
1505        case V4L2_CID_STATELESS_H264_SCALING_MATRIX:
1506                *type = V4L2_CTRL_TYPE_H264_SCALING_MATRIX;
1507                break;
1508        case V4L2_CID_STATELESS_H264_SLICE_PARAMS:
1509                *type = V4L2_CTRL_TYPE_H264_SLICE_PARAMS;
1510                break;
1511        case V4L2_CID_STATELESS_H264_DECODE_PARAMS:
1512                *type = V4L2_CTRL_TYPE_H264_DECODE_PARAMS;
1513                break;
1514        case V4L2_CID_STATELESS_H264_PRED_WEIGHTS:
1515                *type = V4L2_CTRL_TYPE_H264_PRED_WEIGHTS;
1516                break;
1517        case V4L2_CID_STATELESS_VP8_FRAME:
1518                *type = V4L2_CTRL_TYPE_VP8_FRAME;
1519                break;
1520        case V4L2_CID_MPEG_VIDEO_HEVC_SPS:
1521                *type = V4L2_CTRL_TYPE_HEVC_SPS;
1522                break;
1523        case V4L2_CID_MPEG_VIDEO_HEVC_PPS:
1524                *type = V4L2_CTRL_TYPE_HEVC_PPS;
1525                break;
1526        case V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS:
1527                *type = V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS;
1528                break;
1529        case V4L2_CID_UNIT_CELL_SIZE:
1530                *type = V4L2_CTRL_TYPE_AREA;
1531                *flags |= V4L2_CTRL_FLAG_READ_ONLY;
1532                break;
1533        case V4L2_CID_COLORIMETRY_HDR10_CLL_INFO:
1534                *type = V4L2_CTRL_TYPE_HDR10_CLL_INFO;
1535                break;
1536        case V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY:
1537                *type = V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY;
1538                break;
1539        default:
1540                *type = V4L2_CTRL_TYPE_INTEGER;
1541                break;
1542        }
1543        switch (id) {
1544        case V4L2_CID_MPEG_AUDIO_ENCODING:
1545        case V4L2_CID_MPEG_AUDIO_MODE:
1546        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1547        case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1548        case V4L2_CID_MPEG_STREAM_TYPE:
1549                *flags |= V4L2_CTRL_FLAG_UPDATE;
1550                break;
1551        case V4L2_CID_AUDIO_VOLUME:
1552        case V4L2_CID_AUDIO_BALANCE:
1553        case V4L2_CID_AUDIO_BASS:
1554        case V4L2_CID_AUDIO_TREBLE:
1555        case V4L2_CID_BRIGHTNESS:
1556        case V4L2_CID_CONTRAST:
1557        case V4L2_CID_SATURATION:
1558        case V4L2_CID_HUE:
1559        case V4L2_CID_RED_BALANCE:
1560        case V4L2_CID_BLUE_BALANCE:
1561        case V4L2_CID_GAMMA:
1562        case V4L2_CID_SHARPNESS:
1563        case V4L2_CID_CHROMA_GAIN:
1564        case V4L2_CID_RDS_TX_DEVIATION:
1565        case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1566        case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1567        case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1568        case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1569        case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1570        case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1571        case V4L2_CID_PILOT_TONE_DEVIATION:
1572        case V4L2_CID_PILOT_TONE_FREQUENCY:
1573        case V4L2_CID_TUNE_POWER_LEVEL:
1574        case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1575        case V4L2_CID_RF_TUNER_RF_GAIN:
1576        case V4L2_CID_RF_TUNER_LNA_GAIN:
1577        case V4L2_CID_RF_TUNER_MIXER_GAIN:
1578        case V4L2_CID_RF_TUNER_IF_GAIN:
1579        case V4L2_CID_RF_TUNER_BANDWIDTH:
1580        case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
1581                *flags |= V4L2_CTRL_FLAG_SLIDER;
1582                break;
1583        case V4L2_CID_PAN_RELATIVE:
1584        case V4L2_CID_TILT_RELATIVE:
1585        case V4L2_CID_FOCUS_RELATIVE:
1586        case V4L2_CID_IRIS_RELATIVE:
1587        case V4L2_CID_ZOOM_RELATIVE:
1588                *flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
1589                          V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
1590                break;
1591        case V4L2_CID_FLASH_STROBE_STATUS:
1592        case V4L2_CID_AUTO_FOCUS_STATUS:
1593        case V4L2_CID_FLASH_READY:
1594        case V4L2_CID_DV_TX_HOTPLUG:
1595        case V4L2_CID_DV_TX_RXSENSE:
1596        case V4L2_CID_DV_TX_EDID_PRESENT:
1597        case V4L2_CID_DV_RX_POWER_PRESENT:
1598        case V4L2_CID_DV_RX_IT_CONTENT_TYPE:
1599        case V4L2_CID_RDS_RX_PTY:
1600        case V4L2_CID_RDS_RX_PS_NAME:
1601        case V4L2_CID_RDS_RX_RADIO_TEXT:
1602        case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
1603        case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
1604        case V4L2_CID_RDS_RX_MUSIC_SPEECH:
1605        case V4L2_CID_CAMERA_ORIENTATION:
1606        case V4L2_CID_CAMERA_SENSOR_ROTATION:
1607                *flags |= V4L2_CTRL_FLAG_READ_ONLY;
1608                break;
1609        case V4L2_CID_RF_TUNER_PLL_LOCK:
1610                *flags |= V4L2_CTRL_FLAG_VOLATILE;
1611                break;
1612        }
1613}
1614EXPORT_SYMBOL(v4l2_ctrl_fill);
1615
1616static u32 user_flags(const struct v4l2_ctrl *ctrl)
1617{
1618        u32 flags = ctrl->flags;
1619
1620        if (ctrl->is_ptr)
1621                flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
1622
1623        return flags;
1624}
1625
1626static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
1627{
1628        memset(ev, 0, sizeof(*ev));
1629        ev->type = V4L2_EVENT_CTRL;
1630        ev->id = ctrl->id;
1631        ev->u.ctrl.changes = changes;
1632        ev->u.ctrl.type = ctrl->type;
1633        ev->u.ctrl.flags = user_flags(ctrl);
1634        if (ctrl->is_ptr)
1635                ev->u.ctrl.value64 = 0;
1636        else
1637                ev->u.ctrl.value64 = *ctrl->p_cur.p_s64;
1638        ev->u.ctrl.minimum = ctrl->minimum;
1639        ev->u.ctrl.maximum = ctrl->maximum;
1640        if (ctrl->type == V4L2_CTRL_TYPE_MENU
1641            || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1642                ev->u.ctrl.step = 1;
1643        else
1644                ev->u.ctrl.step = ctrl->step;
1645        ev->u.ctrl.default_value = ctrl->default_value;
1646}
1647
1648static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
1649{
1650        struct v4l2_event ev;
1651        struct v4l2_subscribed_event *sev;
1652
1653        if (list_empty(&ctrl->ev_subs))
1654                return;
1655        fill_event(&ev, ctrl, changes);
1656
1657        list_for_each_entry(sev, &ctrl->ev_subs, node)
1658                if (sev->fh != fh ||
1659                    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
1660                        v4l2_event_queue_fh(sev->fh, &ev);
1661}
1662
1663static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx,
1664                      union v4l2_ctrl_ptr ptr1,
1665                      union v4l2_ctrl_ptr ptr2)
1666{
1667        switch (ctrl->type) {
1668        case V4L2_CTRL_TYPE_BUTTON:
1669                return false;
1670        case V4L2_CTRL_TYPE_STRING:
1671                idx *= ctrl->elem_size;
1672                /* strings are always 0-terminated */
1673                return !strcmp(ptr1.p_char + idx, ptr2.p_char + idx);
1674        case V4L2_CTRL_TYPE_INTEGER64:
1675                return ptr1.p_s64[idx] == ptr2.p_s64[idx];
1676        case V4L2_CTRL_TYPE_U8:
1677                return ptr1.p_u8[idx] == ptr2.p_u8[idx];
1678        case V4L2_CTRL_TYPE_U16:
1679                return ptr1.p_u16[idx] == ptr2.p_u16[idx];
1680        case V4L2_CTRL_TYPE_U32:
1681                return ptr1.p_u32[idx] == ptr2.p_u32[idx];
1682        default:
1683                if (ctrl->is_int)
1684                        return ptr1.p_s32[idx] == ptr2.p_s32[idx];
1685                idx *= ctrl->elem_size;
1686                return !memcmp(ptr1.p_const + idx, ptr2.p_const + idx,
1687                               ctrl->elem_size);
1688        }
1689}
1690
1691static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx,
1692                              union v4l2_ctrl_ptr ptr)
1693{
1694        struct v4l2_ctrl_mpeg2_slice_params *p_mpeg2_slice_params;
1695        struct v4l2_ctrl_vp8_frame *p_vp8_frame;
1696        struct v4l2_ctrl_fwht_params *p_fwht_params;
1697        void *p = ptr.p + idx * ctrl->elem_size;
1698
1699        if (ctrl->p_def.p_const)
1700                memcpy(p, ctrl->p_def.p_const, ctrl->elem_size);
1701        else
1702                memset(p, 0, ctrl->elem_size);
1703
1704        /*
1705         * The cast is needed to get rid of a gcc warning complaining that
1706         * V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS is not part of the
1707         * v4l2_ctrl_type enum.
1708         */
1709        switch ((u32)ctrl->type) {
1710        case V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS:
1711                p_mpeg2_slice_params = p;
1712                /* 4:2:0 */
1713                p_mpeg2_slice_params->sequence.chroma_format = 1;
1714                /* interlaced top field */
1715                p_mpeg2_slice_params->picture.picture_structure = 1;
1716                p_mpeg2_slice_params->picture.picture_coding_type =
1717                                        V4L2_MPEG2_PICTURE_CODING_TYPE_I;
1718                break;
1719        case V4L2_CTRL_TYPE_VP8_FRAME:
1720                p_vp8_frame = p;
1721                p_vp8_frame->num_dct_parts = 1;
1722                break;
1723        case V4L2_CTRL_TYPE_FWHT_PARAMS:
1724                p_fwht_params = p;
1725                p_fwht_params->version = V4L2_FWHT_VERSION;
1726                p_fwht_params->width = 1280;
1727                p_fwht_params->height = 720;
1728                p_fwht_params->flags = V4L2_FWHT_FL_PIXENC_YUV |
1729                        (2 << V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET);
1730                break;
1731        }
1732}
1733
1734static void std_init(const struct v4l2_ctrl *ctrl, u32 idx,
1735                     union v4l2_ctrl_ptr ptr)
1736{
1737        switch (ctrl->type) {
1738        case V4L2_CTRL_TYPE_STRING:
1739                idx *= ctrl->elem_size;
1740                memset(ptr.p_char + idx, ' ', ctrl->minimum);
1741                ptr.p_char[idx + ctrl->minimum] = '\0';
1742                break;
1743        case V4L2_CTRL_TYPE_INTEGER64:
1744                ptr.p_s64[idx] = ctrl->default_value;
1745                break;
1746        case V4L2_CTRL_TYPE_INTEGER:
1747        case V4L2_CTRL_TYPE_INTEGER_MENU:
1748        case V4L2_CTRL_TYPE_MENU:
1749        case V4L2_CTRL_TYPE_BITMASK:
1750        case V4L2_CTRL_TYPE_BOOLEAN:
1751                ptr.p_s32[idx] = ctrl->default_value;
1752                break;
1753        case V4L2_CTRL_TYPE_BUTTON:
1754        case V4L2_CTRL_TYPE_CTRL_CLASS:
1755                ptr.p_s32[idx] = 0;
1756                break;
1757        case V4L2_CTRL_TYPE_U8:
1758                ptr.p_u8[idx] = ctrl->default_value;
1759                break;
1760        case V4L2_CTRL_TYPE_U16:
1761                ptr.p_u16[idx] = ctrl->default_value;
1762                break;
1763        case V4L2_CTRL_TYPE_U32:
1764                ptr.p_u32[idx] = ctrl->default_value;
1765                break;
1766        default:
1767                std_init_compound(ctrl, idx, ptr);
1768                break;
1769        }
1770}
1771
1772static void std_log(const struct v4l2_ctrl *ctrl)
1773{
1774        union v4l2_ctrl_ptr ptr = ctrl->p_cur;
1775
1776        if (ctrl->is_array) {
1777                unsigned i;
1778
1779                for (i = 0; i < ctrl->nr_of_dims; i++)
1780                        pr_cont("[%u]", ctrl->dims[i]);
1781                pr_cont(" ");
1782        }
1783
1784        switch (ctrl->type) {
1785        case V4L2_CTRL_TYPE_INTEGER:
1786                pr_cont("%d", *ptr.p_s32);
1787                break;
1788        case V4L2_CTRL_TYPE_BOOLEAN:
1789                pr_cont("%s", *ptr.p_s32 ? "true" : "false");
1790                break;
1791        case V4L2_CTRL_TYPE_MENU:
1792                pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
1793                break;
1794        case V4L2_CTRL_TYPE_INTEGER_MENU:
1795                pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
1796                break;
1797        case V4L2_CTRL_TYPE_BITMASK:
1798                pr_cont("0x%08x", *ptr.p_s32);
1799                break;
1800        case V4L2_CTRL_TYPE_INTEGER64:
1801                pr_cont("%lld", *ptr.p_s64);
1802                break;
1803        case V4L2_CTRL_TYPE_STRING:
1804                pr_cont("%s", ptr.p_char);
1805                break;
1806        case V4L2_CTRL_TYPE_U8:
1807                pr_cont("%u", (unsigned)*ptr.p_u8);
1808                break;
1809        case V4L2_CTRL_TYPE_U16:
1810                pr_cont("%u", (unsigned)*ptr.p_u16);
1811                break;
1812        case V4L2_CTRL_TYPE_U32:
1813                pr_cont("%u", (unsigned)*ptr.p_u32);
1814                break;
1815        case V4L2_CTRL_TYPE_H264_SPS:
1816                pr_cont("H264_SPS");
1817                break;
1818        case V4L2_CTRL_TYPE_H264_PPS:
1819                pr_cont("H264_PPS");
1820                break;
1821        case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
1822                pr_cont("H264_SCALING_MATRIX");
1823                break;
1824        case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
1825                pr_cont("H264_SLICE_PARAMS");
1826                break;
1827        case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
1828                pr_cont("H264_DECODE_PARAMS");
1829                break;
1830        case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
1831                pr_cont("H264_PRED_WEIGHTS");
1832                break;
1833        case V4L2_CTRL_TYPE_FWHT_PARAMS:
1834                pr_cont("FWHT_PARAMS");
1835                break;
1836        case V4L2_CTRL_TYPE_VP8_FRAME:
1837                pr_cont("VP8_FRAME");
1838                break;
1839        case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
1840                pr_cont("HDR10_CLL_INFO");
1841                break;
1842        case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
1843                pr_cont("HDR10_MASTERING_DISPLAY");
1844                break;
1845        default:
1846                pr_cont("unknown type %d", ctrl->type);
1847                break;
1848        }
1849}
1850
1851/*
1852 * Round towards the closest legal value. Be careful when we are
1853 * close to the maximum range of the control type to prevent
1854 * wrap-arounds.
1855 */
1856#define ROUND_TO_RANGE(val, offset_type, ctrl)                  \
1857({                                                              \
1858        offset_type offset;                                     \
1859        if ((ctrl)->maximum >= 0 &&                             \
1860            val >= (ctrl)->maximum - (s32)((ctrl)->step / 2))   \
1861                val = (ctrl)->maximum;                          \
1862        else                                                    \
1863                val += (s32)((ctrl)->step / 2);                 \
1864        val = clamp_t(typeof(val), val,                         \
1865                      (ctrl)->minimum, (ctrl)->maximum);        \
1866        offset = (val) - (ctrl)->minimum;                       \
1867        offset = (ctrl)->step * (offset / (u32)(ctrl)->step);   \
1868        val = (ctrl)->minimum + offset;                         \
1869        0;                                                      \
1870})
1871
1872/* Validate a new control */
1873
1874#define zero_padding(s) \
1875        memset(&(s).padding, 0, sizeof((s).padding))
1876#define zero_reserved(s) \
1877        memset(&(s).reserved, 0, sizeof((s).reserved))
1878
1879/*
1880 * Compound controls validation requires setting unused fields/flags to zero
1881 * in order to properly detect unchanged controls with std_equal's memcmp.
1882 */
1883static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
1884                                 union v4l2_ctrl_ptr ptr)
1885{
1886        struct v4l2_ctrl_mpeg2_slice_params *p_mpeg2_slice_params;
1887        struct v4l2_ctrl_vp8_frame *p_vp8_frame;
1888        struct v4l2_ctrl_fwht_params *p_fwht_params;
1889        struct v4l2_ctrl_h264_sps *p_h264_sps;
1890        struct v4l2_ctrl_h264_pps *p_h264_pps;
1891        struct v4l2_ctrl_h264_pred_weights *p_h264_pred_weights;
1892        struct v4l2_ctrl_h264_slice_params *p_h264_slice_params;
1893        struct v4l2_ctrl_h264_decode_params *p_h264_dec_params;
1894        struct v4l2_ctrl_hevc_sps *p_hevc_sps;
1895        struct v4l2_ctrl_hevc_pps *p_hevc_pps;
1896        struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params;
1897        struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering;
1898        struct v4l2_area *area;
1899        void *p = ptr.p + idx * ctrl->elem_size;
1900        unsigned int i;
1901
1902        switch ((u32)ctrl->type) {
1903        case V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS:
1904                p_mpeg2_slice_params = p;
1905
1906                switch (p_mpeg2_slice_params->sequence.chroma_format) {
1907                case 1: /* 4:2:0 */
1908                case 2: /* 4:2:2 */
1909                case 3: /* 4:4:4 */
1910                        break;
1911                default:
1912                        return -EINVAL;
1913                }
1914
1915                switch (p_mpeg2_slice_params->picture.intra_dc_precision) {
1916                case 0: /* 8 bits */
1917                case 1: /* 9 bits */
1918                case 2: /* 10 bits */
1919                case 3: /* 11 bits */
1920                        break;
1921                default:
1922                        return -EINVAL;
1923                }
1924
1925                switch (p_mpeg2_slice_params->picture.picture_structure) {
1926                case 1: /* interlaced top field */
1927                case 2: /* interlaced bottom field */
1928                case 3: /* progressive */
1929                        break;
1930                default:
1931                        return -EINVAL;
1932                }
1933
1934                switch (p_mpeg2_slice_params->picture.picture_coding_type) {
1935                case V4L2_MPEG2_PICTURE_CODING_TYPE_I:
1936                case V4L2_MPEG2_PICTURE_CODING_TYPE_P:
1937                case V4L2_MPEG2_PICTURE_CODING_TYPE_B:
1938                        break;
1939                default:
1940                        return -EINVAL;
1941                }
1942
1943                break;
1944
1945        case V4L2_CTRL_TYPE_MPEG2_QUANTIZATION:
1946                break;
1947
1948        case V4L2_CTRL_TYPE_FWHT_PARAMS:
1949                p_fwht_params = p;
1950                if (p_fwht_params->version < V4L2_FWHT_VERSION)
1951                        return -EINVAL;
1952                if (!p_fwht_params->width || !p_fwht_params->height)
1953                        return -EINVAL;
1954                break;
1955
1956        case V4L2_CTRL_TYPE_H264_SPS:
1957                p_h264_sps = p;
1958
1959                /* Some syntax elements are only conditionally valid */
1960                if (p_h264_sps->pic_order_cnt_type != 0) {
1961                        p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 = 0;
1962                } else if (p_h264_sps->pic_order_cnt_type != 1) {
1963                        p_h264_sps->num_ref_frames_in_pic_order_cnt_cycle = 0;
1964                        p_h264_sps->offset_for_non_ref_pic = 0;
1965                        p_h264_sps->offset_for_top_to_bottom_field = 0;
1966                        memset(&p_h264_sps->offset_for_ref_frame, 0,
1967                               sizeof(p_h264_sps->offset_for_ref_frame));
1968                }
1969
1970                if (!V4L2_H264_SPS_HAS_CHROMA_FORMAT(p_h264_sps)) {
1971                        p_h264_sps->chroma_format_idc = 1;
1972                        p_h264_sps->bit_depth_luma_minus8 = 0;
1973                        p_h264_sps->bit_depth_chroma_minus8 = 0;
1974
1975                        p_h264_sps->flags &=
1976                                ~V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS;
1977
1978                        if (p_h264_sps->chroma_format_idc < 3)
1979                                p_h264_sps->flags &=
1980                                        ~V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE;
1981                }
1982
1983                if (p_h264_sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)
1984                        p_h264_sps->flags &=
1985                                ~V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD;
1986
1987                /*
1988                 * Chroma 4:2:2 format require at least High 4:2:2 profile.
1989                 *
1990                 * The H264 specification and well-known parser implementations
1991                 * use profile-idc values directly, as that is clearer and
1992                 * less ambiguous. We do the same here.
1993                 */
1994                if (p_h264_sps->profile_idc < 122 &&
1995                    p_h264_sps->chroma_format_idc > 1)
1996                        return -EINVAL;
1997                /* Chroma 4:4:4 format require at least High 4:2:2 profile */
1998                if (p_h264_sps->profile_idc < 244 &&
1999                    p_h264_sps->chroma_format_idc > 2)
2000                        return -EINVAL;
2001                if (p_h264_sps->chroma_format_idc > 3)
2002                        return -EINVAL;
2003
2004                if (p_h264_sps->bit_depth_luma_minus8 > 6)
2005                        return -EINVAL;
2006                if (p_h264_sps->bit_depth_chroma_minus8 > 6)
2007                        return -EINVAL;
2008                if (p_h264_sps->log2_max_frame_num_minus4 > 12)
2009                        return -EINVAL;
2010                if (p_h264_sps->pic_order_cnt_type > 2)
2011                        return -EINVAL;
2012                if (p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 > 12)
2013                        return -EINVAL;
2014                if (p_h264_sps->max_num_ref_frames > V4L2_H264_REF_LIST_LEN)
2015                        return -EINVAL;
2016                break;
2017
2018        case V4L2_CTRL_TYPE_H264_PPS:
2019                p_h264_pps = p;
2020
2021                if (p_h264_pps->num_slice_groups_minus1 > 7)
2022                        return -EINVAL;
2023                if (p_h264_pps->num_ref_idx_l0_default_active_minus1 >
2024                    (V4L2_H264_REF_LIST_LEN - 1))
2025                        return -EINVAL;
2026                if (p_h264_pps->num_ref_idx_l1_default_active_minus1 >
2027                    (V4L2_H264_REF_LIST_LEN - 1))
2028                        return -EINVAL;
2029                if (p_h264_pps->weighted_bipred_idc > 2)
2030                        return -EINVAL;
2031                /*
2032                 * pic_init_qp_minus26 shall be in the range of
2033                 * -(26 + QpBdOffset_y) to +25, inclusive,
2034                 *  where QpBdOffset_y is 6 * bit_depth_luma_minus8
2035                 */
2036                if (p_h264_pps->pic_init_qp_minus26 < -62 ||
2037                    p_h264_pps->pic_init_qp_minus26 > 25)
2038                        return -EINVAL;
2039                if (p_h264_pps->pic_init_qs_minus26 < -26 ||
2040                    p_h264_pps->pic_init_qs_minus26 > 25)
2041                        return -EINVAL;
2042                if (p_h264_pps->chroma_qp_index_offset < -12 ||
2043                    p_h264_pps->chroma_qp_index_offset > 12)
2044                        return -EINVAL;
2045                if (p_h264_pps->second_chroma_qp_index_offset < -12 ||
2046                    p_h264_pps->second_chroma_qp_index_offset > 12)
2047                        return -EINVAL;
2048                break;
2049
2050        case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
2051                break;
2052
2053        case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
2054                p_h264_pred_weights = p;
2055
2056                if (p_h264_pred_weights->luma_log2_weight_denom > 7)
2057                        return -EINVAL;
2058                if (p_h264_pred_weights->chroma_log2_weight_denom > 7)
2059                        return -EINVAL;
2060                break;
2061
2062        case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
2063                p_h264_slice_params = p;
2064
2065                if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
2066                        p_h264_slice_params->flags &=
2067                                ~V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED;
2068
2069                if (p_h264_slice_params->colour_plane_id > 2)
2070                        return -EINVAL;
2071                if (p_h264_slice_params->cabac_init_idc > 2)
2072                        return -EINVAL;
2073                if (p_h264_slice_params->disable_deblocking_filter_idc > 2)
2074                        return -EINVAL;
2075                if (p_h264_slice_params->slice_alpha_c0_offset_div2 < -6 ||
2076                    p_h264_slice_params->slice_alpha_c0_offset_div2 > 6)
2077                        return -EINVAL;
2078                if (p_h264_slice_params->slice_beta_offset_div2 < -6 ||
2079                    p_h264_slice_params->slice_beta_offset_div2 > 6)
2080                        return -EINVAL;
2081
2082                if (p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_I ||
2083                    p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_SI)
2084                        p_h264_slice_params->num_ref_idx_l0_active_minus1 = 0;
2085                if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B)
2086                        p_h264_slice_params->num_ref_idx_l1_active_minus1 = 0;
2087
2088                if (p_h264_slice_params->num_ref_idx_l0_active_minus1 >
2089                    (V4L2_H264_REF_LIST_LEN - 1))
2090                        return -EINVAL;
2091                if (p_h264_slice_params->num_ref_idx_l1_active_minus1 >
2092                    (V4L2_H264_REF_LIST_LEN - 1))
2093                        return -EINVAL;
2094                zero_reserved(*p_h264_slice_params);
2095                break;
2096
2097        case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
2098                p_h264_dec_params = p;
2099
2100                if (p_h264_dec_params->nal_ref_idc > 3)
2101                        return -EINVAL;
2102                for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) {
2103                        struct v4l2_h264_dpb_entry *dpb_entry =
2104                                &p_h264_dec_params->dpb[i];
2105
2106                        zero_reserved(*dpb_entry);
2107                }
2108                zero_reserved(*p_h264_dec_params);
2109                break;
2110
2111        case V4L2_CTRL_TYPE_VP8_FRAME:
2112                p_vp8_frame = p;
2113
2114                switch (p_vp8_frame->num_dct_parts) {
2115                case 1:
2116                case 2:
2117                case 4:
2118                case 8:
2119                        break;
2120                default:
2121                        return -EINVAL;
2122                }
2123                zero_padding(p_vp8_frame->segment);
2124                zero_padding(p_vp8_frame->lf);
2125                zero_padding(p_vp8_frame->quant);
2126                zero_padding(p_vp8_frame->entropy);
2127                zero_padding(p_vp8_frame->coder_state);
2128                break;
2129
2130        case V4L2_CTRL_TYPE_HEVC_SPS:
2131                p_hevc_sps = p;
2132
2133                if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) {
2134                        p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0;
2135                        p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;
2136                        p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0;
2137                        p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0;
2138                }
2139
2140                if (!(p_hevc_sps->flags &
2141                      V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT))
2142                        p_hevc_sps->num_long_term_ref_pics_sps = 0;
2143                break;
2144
2145        case V4L2_CTRL_TYPE_HEVC_PPS:
2146                p_hevc_pps = p;
2147
2148                if (!(p_hevc_pps->flags &
2149                      V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED))
2150                        p_hevc_pps->diff_cu_qp_delta_depth = 0;
2151
2152                if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
2153                        p_hevc_pps->num_tile_columns_minus1 = 0;
2154                        p_hevc_pps->num_tile_rows_minus1 = 0;
2155                        memset(&p_hevc_pps->column_width_minus1, 0,
2156                               sizeof(p_hevc_pps->column_width_minus1));
2157                        memset(&p_hevc_pps->row_height_minus1, 0,
2158                               sizeof(p_hevc_pps->row_height_minus1));
2159
2160                        p_hevc_pps->flags &=
2161                                ~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED;
2162                }
2163
2164                if (p_hevc_pps->flags &
2165                    V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) {
2166                        p_hevc_pps->pps_beta_offset_div2 = 0;
2167                        p_hevc_pps->pps_tc_offset_div2 = 0;
2168                }
2169
2170                zero_padding(*p_hevc_pps);
2171                break;
2172
2173        case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
2174                p_hevc_slice_params = p;
2175
2176                if (p_hevc_slice_params->num_active_dpb_entries >
2177                    V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
2178                        return -EINVAL;
2179
2180                zero_padding(p_hevc_slice_params->pred_weight_table);
2181
2182                for (i = 0; i < p_hevc_slice_params->num_active_dpb_entries;
2183                     i++) {
2184                        struct v4l2_hevc_dpb_entry *dpb_entry =
2185                                &p_hevc_slice_params->dpb[i];
2186
2187                        zero_padding(*dpb_entry);
2188                }
2189
2190                zero_padding(*p_hevc_slice_params);
2191                break;
2192
2193        case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
2194                break;
2195
2196        case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
2197                p_hdr10_mastering = p;
2198
2199                for (i = 0; i < 3; ++i) {
2200                        if (p_hdr10_mastering->display_primaries_x[i] <
2201                                V4L2_HDR10_MASTERING_PRIMARIES_X_LOW ||
2202                            p_hdr10_mastering->display_primaries_x[i] >
2203                                V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH ||
2204                            p_hdr10_mastering->display_primaries_y[i] <
2205                                V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW ||
2206                            p_hdr10_mastering->display_primaries_y[i] >
2207                                V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH)
2208                                return -EINVAL;
2209                }
2210
2211                if (p_hdr10_mastering->white_point_x <
2212                        V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW ||
2213                    p_hdr10_mastering->white_point_x >
2214                        V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH ||
2215                    p_hdr10_mastering->white_point_y <
2216                        V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW ||
2217                    p_hdr10_mastering->white_point_y >
2218                        V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH)
2219                        return -EINVAL;
2220
2221                if (p_hdr10_mastering->max_display_mastering_luminance <
2222                        V4L2_HDR10_MASTERING_MAX_LUMA_LOW ||
2223                    p_hdr10_mastering->max_display_mastering_luminance >
2224                        V4L2_HDR10_MASTERING_MAX_LUMA_HIGH ||
2225                    p_hdr10_mastering->min_display_mastering_luminance <
2226                        V4L2_HDR10_MASTERING_MIN_LUMA_LOW ||
2227                    p_hdr10_mastering->min_display_mastering_luminance >
2228                        V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
2229                        return -EINVAL;
2230
2231                /* The following restriction comes from ITU-T Rec. H.265 spec */
2232                if (p_hdr10_mastering->max_display_mastering_luminance ==
2233                        V4L2_HDR10_MASTERING_MAX_LUMA_LOW &&
2234                    p_hdr10_mastering->min_display_mastering_luminance ==
2235                        V4L2_HDR10_MASTERING_MIN_LUMA_HIGH)
2236                        return -EINVAL;
2237
2238                break;
2239
2240        case V4L2_CTRL_TYPE_AREA:
2241                area = p;
2242                if (!area->width || !area->height)
2243                        return -EINVAL;
2244                break;
2245
2246        default:
2247                return -EINVAL;
2248        }
2249
2250        return 0;
2251}
2252
2253static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx,
2254                        union v4l2_ctrl_ptr ptr)
2255{
2256        size_t len;
2257        u64 offset;
2258        s64 val;
2259
2260        switch ((u32)ctrl->type) {
2261        case V4L2_CTRL_TYPE_INTEGER:
2262                return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
2263        case V4L2_CTRL_TYPE_INTEGER64:
2264                /*
2265                 * We can't use the ROUND_TO_RANGE define here due to
2266                 * the u64 divide that needs special care.
2267                 */
2268                val = ptr.p_s64[idx];
2269                if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
2270                        val = ctrl->maximum;
2271                else
2272                        val += (s64)(ctrl->step / 2);
2273                val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
2274                offset = val - ctrl->minimum;
2275                do_div(offset, ctrl->step);
2276                ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
2277                return 0;
2278        case V4L2_CTRL_TYPE_U8:
2279                return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
2280        case V4L2_CTRL_TYPE_U16:
2281                return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
2282        case V4L2_CTRL_TYPE_U32:
2283                return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
2284
2285        case V4L2_CTRL_TYPE_BOOLEAN:
2286                ptr.p_s32[idx] = !!ptr.p_s32[idx];
2287                return 0;
2288
2289        case V4L2_CTRL_TYPE_MENU:
2290        case V4L2_CTRL_TYPE_INTEGER_MENU:
2291                if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
2292                        return -ERANGE;
2293                if (ptr.p_s32[idx] < BITS_PER_LONG_LONG &&
2294                    (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx])))
2295                        return -EINVAL;
2296                if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
2297                    ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
2298                        return -EINVAL;
2299                return 0;
2300
2301        case V4L2_CTRL_TYPE_BITMASK:
2302                ptr.p_s32[idx] &= ctrl->maximum;
2303                return 0;
2304
2305        case V4L2_CTRL_TYPE_BUTTON:
2306        case V4L2_CTRL_TYPE_CTRL_CLASS:
2307                ptr.p_s32[idx] = 0;
2308                return 0;
2309
2310        case V4L2_CTRL_TYPE_STRING:
2311                idx *= ctrl->elem_size;
2312                len = strlen(ptr.p_char + idx);
2313                if (len < ctrl->minimum)
2314                        return -ERANGE;
2315                if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
2316                        return -ERANGE;
2317                return 0;
2318
2319        default:
2320                return std_validate_compound(ctrl, idx, ptr);
2321        }
2322}
2323
2324static const struct v4l2_ctrl_type_ops std_type_ops = {
2325        .equal = std_equal,
2326        .init = std_init,
2327        .log = std_log,
2328        .validate = std_validate,
2329};
2330
2331/* Helper function: copy the given control value back to the caller */
2332static int ptr_to_user(struct v4l2_ext_control *c,
2333                       struct v4l2_ctrl *ctrl,
2334                       union v4l2_ctrl_ptr ptr)
2335{
2336        u32 len;
2337
2338        if (ctrl->is_ptr && !ctrl->is_string)
2339                return copy_to_user(c->ptr, ptr.p_const, c->size) ?
2340                       -EFAULT : 0;
2341
2342        switch (ctrl->type) {
2343        case V4L2_CTRL_TYPE_STRING:
2344                len = strlen(ptr.p_char);
2345                if (c->size < len + 1) {
2346                        c->size = ctrl->elem_size;
2347                        return -ENOSPC;
2348                }
2349                return copy_to_user(c->string, ptr.p_char, len + 1) ?
2350                       -EFAULT : 0;
2351        case V4L2_CTRL_TYPE_INTEGER64:
2352                c->value64 = *ptr.p_s64;
2353                break;
2354        default:
2355                c->value = *ptr.p_s32;
2356                break;
2357        }
2358        return 0;
2359}
2360
2361/* Helper function: copy the current control value back to the caller */
2362static int cur_to_user(struct v4l2_ext_control *c,
2363                       struct v4l2_ctrl *ctrl)
2364{
2365        return ptr_to_user(c, ctrl, ctrl->p_cur);
2366}
2367
2368/* Helper function: copy the new control value back to the caller */
2369static int new_to_user(struct v4l2_ext_control *c,
2370                       struct v4l2_ctrl *ctrl)
2371{
2372        return ptr_to_user(c, ctrl, ctrl->p_new);
2373}
2374
2375/* Helper function: copy the request value back to the caller */
2376static int req_to_user(struct v4l2_ext_control *c,
2377                       struct v4l2_ctrl_ref *ref)
2378{
2379        return ptr_to_user(c, ref->ctrl, ref->p_req);
2380}
2381
2382/* Helper function: copy the initial control value back to the caller */
2383static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl)
2384{
2385        int idx;
2386
2387        for (idx = 0; idx < ctrl->elems; idx++)
2388                ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
2389
2390        return ptr_to_user(c, ctrl, ctrl->p_new);
2391}
2392
2393/* Helper function: copy the caller-provider value to the given control value */
2394static int user_to_ptr(struct v4l2_ext_control *c,
2395                       struct v4l2_ctrl *ctrl,
2396                       union v4l2_ctrl_ptr ptr)
2397{
2398        int ret;
2399        u32 size;
2400
2401        ctrl->is_new = 1;
2402        if (ctrl->is_ptr && !ctrl->is_string) {
2403                unsigned idx;
2404
2405                ret = copy_from_user(ptr.p, c->ptr, c->size) ? -EFAULT : 0;
2406                if (ret || !ctrl->is_array)
2407                        return ret;
2408                for (idx = c->size / ctrl->elem_size; idx < ctrl->elems; idx++)
2409                        ctrl->type_ops->init(ctrl, idx, ptr);
2410                return 0;
2411        }
2412
2413        switch (ctrl->type) {
2414        case V4L2_CTRL_TYPE_INTEGER64:
2415                *ptr.p_s64 = c->value64;
2416                break;
2417        case V4L2_CTRL_TYPE_STRING:
2418                size = c->size;
2419                if (size == 0)
2420                        return -ERANGE;
2421                if (size > ctrl->maximum + 1)
2422                        size = ctrl->maximum + 1;
2423                ret = copy_from_user(ptr.p_char, c->string, size) ? -EFAULT : 0;
2424                if (!ret) {
2425                        char last = ptr.p_char[size - 1];
2426
2427                        ptr.p_char[size - 1] = 0;
2428                        /* If the string was longer than ctrl->maximum,
2429                           then return an error. */
2430                        if (strlen(ptr.p_char) == ctrl->maximum && last)
2431                                return -ERANGE;
2432                }
2433                return ret;
2434        default:
2435                *ptr.p_s32 = c->value;
2436                break;
2437        }
2438        return 0;
2439}
2440
2441/* Helper function: copy the caller-provider value as the new control value */
2442static int user_to_new(struct v4l2_ext_control *c,
2443                       struct v4l2_ctrl *ctrl)
2444{
2445        return user_to_ptr(c, ctrl, ctrl->p_new);
2446}
2447
2448/* Copy the one value to another. */
2449static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
2450                       union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to)
2451{
2452        if (ctrl == NULL)
2453                return;
2454        memcpy(to.p, from.p_const, ctrl->elems * ctrl->elem_size);
2455}
2456
2457/* Copy the new value to the current value. */
2458static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
2459{
2460        bool changed;
2461
2462        if (ctrl == NULL)
2463                return;
2464
2465        /* has_changed is set by cluster_changed */
2466        changed = ctrl->has_changed;
2467        if (changed)
2468                ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur);
2469
2470        if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
2471                /* Note: CH_FLAGS is only set for auto clusters. */
2472                ctrl->flags &=
2473                        ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
2474                if (!is_cur_manual(ctrl->cluster[0])) {
2475                        ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
2476                        if (ctrl->cluster[0]->has_volatiles)
2477                                ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2478                }
2479                fh = NULL;
2480        }
2481        if (changed || ch_flags) {
2482                /* If a control was changed that was not one of the controls
2483                   modified by the application, then send the event to all. */
2484                if (!ctrl->is_new)
2485                        fh = NULL;
2486                send_event(fh, ctrl,
2487                        (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
2488                if (ctrl->call_notify && changed && ctrl->handler->notify)
2489                        ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
2490        }
2491}
2492
2493/* Copy the current value to the new value */
2494static void cur_to_new(struct v4l2_ctrl *ctrl)
2495{
2496        if (ctrl == NULL)
2497                return;
2498        ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new);
2499}
2500
2501/* Copy the new value to the request value */
2502static void new_to_req(struct v4l2_ctrl_ref *ref)
2503{
2504        if (!ref)
2505                return;
2506        ptr_to_ptr(ref->ctrl, ref->ctrl->p_new, ref->p_req);
2507        ref->valid_p_req = true;
2508}
2509
2510/* Copy the current value to the request value */
2511static void cur_to_req(struct v4l2_ctrl_ref *ref)
2512{
2513        if (!ref)
2514                return;
2515        ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->p_req);
2516        ref->valid_p_req = true;
2517}
2518
2519/* Copy the request value to the new value */
2520static void req_to_new(struct v4l2_ctrl_ref *ref)
2521{
2522        if (!ref)
2523                return;
2524        if (ref->valid_p_req)
2525                ptr_to_ptr(ref->ctrl, ref->p_req, ref->ctrl->p_new);
2526        else
2527                ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->ctrl->p_new);
2528}
2529
2530/* Return non-zero if one or more of the controls in the cluster has a new
2531   value that differs from the current value. */
2532static int cluster_changed(struct v4l2_ctrl *master)
2533{
2534        bool changed = false;
2535        unsigned idx;
2536        int i;
2537
2538        for (i = 0; i < master->ncontrols; i++) {
2539                struct v4l2_ctrl *ctrl = master->cluster[i];
2540                bool ctrl_changed = false;
2541
2542                if (ctrl == NULL)
2543                        continue;
2544
2545                if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE)
2546                        changed = ctrl_changed = true;
2547
2548                /*
2549                 * Set has_changed to false to avoid generating
2550                 * the event V4L2_EVENT_CTRL_CH_VALUE
2551                 */
2552                if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
2553                        ctrl->has_changed = false;
2554                        continue;
2555                }
2556
2557                for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++)
2558                        ctrl_changed = !ctrl->type_ops->equal(ctrl, idx,
2559                                ctrl->p_cur, ctrl->p_new);
2560                ctrl->has_changed = ctrl_changed;
2561                changed |= ctrl->has_changed;
2562        }
2563        return changed;
2564}
2565
2566/* Control range checking */
2567static int check_range(enum v4l2_ctrl_type type,
2568                s64 min, s64 max, u64 step, s64 def)
2569{
2570        switch (type) {
2571        case V4L2_CTRL_TYPE_BOOLEAN:
2572                if (step != 1 || max > 1 || min < 0)
2573                        return -ERANGE;
2574                fallthrough;
2575        case V4L2_CTRL_TYPE_U8:
2576        case V4L2_CTRL_TYPE_U16:
2577        case V4L2_CTRL_TYPE_U32:
2578        case V4L2_CTRL_TYPE_INTEGER:
2579        case V4L2_CTRL_TYPE_INTEGER64:
2580                if (step == 0 || min > max || def < min || def > max)
2581                        return -ERANGE;
2582                return 0;
2583        case V4L2_CTRL_TYPE_BITMASK:
2584                if (step || min || !max || (def & ~max))
2585                        return -ERANGE;
2586                return 0;
2587        case V4L2_CTRL_TYPE_MENU:
2588        case V4L2_CTRL_TYPE_INTEGER_MENU:
2589                if (min > max || def < min || def > max)
2590                        return -ERANGE;
2591                /* Note: step == menu_skip_mask for menu controls.
2592                   So here we check if the default value is masked out. */
2593                if (step && ((1 << def) & step))
2594                        return -EINVAL;
2595                return 0;
2596        case V4L2_CTRL_TYPE_STRING:
2597                if (min > max || min < 0 || step < 1 || def)
2598                        return -ERANGE;
2599                return 0;
2600        default:
2601                return 0;
2602        }
2603}
2604
2605/* Validate a new control */
2606static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
2607{
2608        unsigned idx;
2609        int err = 0;
2610
2611        for (idx = 0; !err && idx < ctrl->elems; idx++)
2612                err = ctrl->type_ops->validate(ctrl, idx, p_new);
2613        return err;
2614}
2615
2616static inline u32 node2id(struct list_head *node)
2617{
2618        return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
2619}
2620
2621/* Set the handler's error code if it wasn't set earlier already */
2622static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
2623{
2624        if (hdl->error == 0)
2625                hdl->error = err;
2626        return err;
2627}
2628
2629/* Initialize the handler */
2630int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
2631                                 unsigned nr_of_controls_hint,
2632                                 struct lock_class_key *key, const char *name)
2633{
2634        mutex_init(&hdl->_lock);
2635        hdl->lock = &hdl->_lock;
2636        lockdep_set_class_and_name(hdl->lock, key, name);
2637        INIT_LIST_HEAD(&hdl->ctrls);
2638        INIT_LIST_HEAD(&hdl->ctrl_refs);
2639        INIT_LIST_HEAD(&hdl->requests);
2640        INIT_LIST_HEAD(&hdl->requests_queued);
2641        hdl->request_is_queued = false;
2642        hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
2643        hdl->buckets = kvmalloc_array(hdl->nr_of_buckets,
2644                                      sizeof(hdl->buckets[0]),
2645                                      GFP_KERNEL | __GFP_ZERO);
2646        hdl->error = hdl->buckets ? 0 : -ENOMEM;
2647        media_request_object_init(&hdl->req_obj);
2648        return hdl->error;
2649}
2650EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
2651
2652/* Free all controls and control refs */
2653void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
2654{
2655        struct v4l2_ctrl_ref *ref, *next_ref;
2656        struct v4l2_ctrl *ctrl, *next_ctrl;
2657        struct v4l2_subscribed_event *sev, *next_sev;
2658
2659        if (hdl == NULL || hdl->buckets == NULL)
2660                return;
2661
2662        /*
2663         * If the main handler is freed and it is used by handler objects in
2664         * outstanding requests, then unbind and put those objects before
2665         * freeing the main handler.
2666         *
2667         * The main handler can be identified by having a NULL ops pointer in
2668         * the request object.
2669         */
2670        if (!hdl->req_obj.ops && !list_empty(&hdl->requests)) {
2671                struct v4l2_ctrl_handler *req, *next_req;
2672
2673                list_for_each_entry_safe(req, next_req, &hdl->requests, requests) {
2674                        media_request_object_unbind(&req->req_obj);
2675                        media_request_object_put(&req->req_obj);
2676                }
2677        }
2678        mutex_lock(hdl->lock);
2679        /* Free all nodes */
2680        list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
2681                list_del(&ref->node);
2682                kfree(ref);
2683        }
2684        /* Free all controls owned by the handler */
2685        list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
2686                list_del(&ctrl->node);
2687                list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
2688                        list_del(&sev->node);
2689                kvfree(ctrl);
2690        }
2691        kvfree(hdl->buckets);
2692        hdl->buckets = NULL;
2693        hdl->cached = NULL;
2694        hdl->error = 0;
2695        mutex_unlock(hdl->lock);
2696        mutex_destroy(&hdl->_lock);
2697}
2698EXPORT_SYMBOL(v4l2_ctrl_handler_free);
2699
2700/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
2701   be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
2702   with applications that do not use the NEXT_CTRL flag.
2703
2704   We just find the n-th private user control. It's O(N), but that should not
2705   be an issue in this particular case. */
2706static struct v4l2_ctrl_ref *find_private_ref(
2707                struct v4l2_ctrl_handler *hdl, u32 id)
2708{
2709        struct v4l2_ctrl_ref *ref;
2710
2711        id -= V4L2_CID_PRIVATE_BASE;
2712        list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2713                /* Search for private user controls that are compatible with
2714                   VIDIOC_G/S_CTRL. */
2715                if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
2716                    V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
2717                        if (!ref->ctrl->is_int)
2718                                continue;
2719                        if (id == 0)
2720                                return ref;
2721                        id--;
2722                }
2723        }
2724        return NULL;
2725}
2726
2727/* Find a control with the given ID. */
2728static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
2729{
2730        struct v4l2_ctrl_ref *ref;
2731        int bucket;
2732
2733        id &= V4L2_CTRL_ID_MASK;
2734
2735        /* Old-style private controls need special handling */
2736        if (id >= V4L2_CID_PRIVATE_BASE)
2737                return find_private_ref(hdl, id);
2738        bucket = id % hdl->nr_of_buckets;
2739
2740        /* Simple optimization: cache the last control found */
2741        if (hdl->cached && hdl->cached->ctrl->id == id)
2742                return hdl->cached;
2743
2744        /* Not in cache, search the hash */
2745        ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
2746        while (ref && ref->ctrl->id != id)
2747                ref = ref->next;
2748
2749        if (ref)
2750                hdl->cached = ref; /* cache it! */
2751        return ref;
2752}
2753
2754/* Find a control with the given ID. Take the handler's lock first. */
2755static struct v4l2_ctrl_ref *find_ref_lock(
2756                struct v4l2_ctrl_handler *hdl, u32 id)
2757{
2758        struct v4l2_ctrl_ref *ref = NULL;
2759
2760        if (hdl) {
2761                mutex_lock(hdl->lock);
2762                ref = find_ref(hdl, id);
2763                mutex_unlock(hdl->lock);
2764        }
2765        return ref;
2766}
2767
2768/* Find a control with the given ID. */
2769struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
2770{
2771        struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
2772
2773        return ref ? ref->ctrl : NULL;
2774}
2775EXPORT_SYMBOL(v4l2_ctrl_find);
2776
2777/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
2778static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
2779                           struct v4l2_ctrl *ctrl,
2780                           struct v4l2_ctrl_ref **ctrl_ref,
2781                           bool from_other_dev, bool allocate_req)
2782{
2783        struct v4l2_ctrl_ref *ref;
2784        struct v4l2_ctrl_ref *new_ref;
2785        u32 id = ctrl->id;
2786        u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
2787        int bucket = id % hdl->nr_of_buckets;   /* which bucket to use */
2788        unsigned int size_extra_req = 0;
2789
2790        if (ctrl_ref)
2791                *ctrl_ref = NULL;
2792
2793        /*
2794         * Automatically add the control class if it is not yet present and
2795         * the new control is not a compound control.
2796         */
2797        if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
2798            id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
2799                if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
2800                        return hdl->error;
2801
2802        if (hdl->error)
2803                return hdl->error;
2804
2805        if (allocate_req)
2806                size_extra_req = ctrl->elems * ctrl->elem_size;
2807        new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL);
2808        if (!new_ref)
2809                return handler_set_err(hdl, -ENOMEM);
2810        new_ref->ctrl = ctrl;
2811        new_ref->from_other_dev = from_other_dev;
2812        if (size_extra_req)
2813                new_ref->p_req.p = &new_ref[1];
2814
2815        INIT_LIST_HEAD(&new_ref->node);
2816
2817        mutex_lock(hdl->lock);
2818
2819        /* Add immediately at the end of the list if the list is empty, or if
2820           the last element in the list has a lower ID.
2821           This ensures that when elements are added in ascending order the
2822           insertion is an O(1) operation. */
2823        if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
2824                list_add_tail(&new_ref->node, &hdl->ctrl_refs);
2825                goto insert_in_hash;
2826        }
2827
2828        /* Find insert position in sorted list */
2829        list_for_each_entry(ref, &hdl->ctrl_refs, node) {
2830                if (ref->ctrl->id < id)
2831                        continue;
2832                /* Don't add duplicates */
2833                if (ref->ctrl->id == id) {
2834                        kfree(new_ref);
2835                        goto unlock;
2836                }
2837                list_add(&new_ref->node, ref->node.prev);
2838                break;
2839        }
2840
2841insert_in_hash:
2842        /* Insert the control node in the hash */
2843        new_ref->next = hdl->buckets[bucket];
2844        hdl->buckets[bucket] = new_ref;
2845        if (ctrl_ref)
2846                *ctrl_ref = new_ref;
2847        if (ctrl->handler == hdl) {
2848                /* By default each control starts in a cluster of its own.
2849                 * new_ref->ctrl is basically a cluster array with one
2850                 * element, so that's perfect to use as the cluster pointer.
2851                 * But only do this for the handler that owns the control.
2852                 */
2853                ctrl->cluster = &new_ref->ctrl;
2854                ctrl->ncontrols = 1;
2855        }
2856
2857unlock:
2858        mutex_unlock(hdl->lock);
2859        return 0;
2860}
2861
2862/* Add a new control */
2863static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
2864                        const struct v4l2_ctrl_ops *ops,
2865                        const struct v4l2_ctrl_type_ops *type_ops,
2866                        u32 id, const char *name, enum v4l2_ctrl_type type,
2867                        s64 min, s64 max, u64 step, s64 def,
2868                        const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
2869                        u32 flags, const char * const *qmenu,
2870                        const s64 *qmenu_int, const union v4l2_ctrl_ptr p_def,
2871                        void *priv)
2872{
2873        struct v4l2_ctrl *ctrl;
2874        unsigned sz_extra;
2875        unsigned nr_of_dims = 0;
2876        unsigned elems = 1;
2877        bool is_array;
2878        unsigned tot_ctrl_size;
2879        unsigned idx;
2880        void *data;
2881        int err;
2882
2883        if (hdl->error)
2884                return NULL;
2885
2886        while (dims && dims[nr_of_dims]) {
2887                elems *= dims[nr_of_dims];
2888                nr_of_dims++;
2889                if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
2890                        break;
2891        }
2892        is_array = nr_of_dims > 0;
2893
2894        /* Prefill elem_size for all types handled by std_type_ops */
2895        switch ((u32)type) {
2896        case V4L2_CTRL_TYPE_INTEGER64:
2897                elem_size = sizeof(s64);
2898                break;
2899        case V4L2_CTRL_TYPE_STRING:
2900                elem_size = max + 1;
2901                break;
2902        case V4L2_CTRL_TYPE_U8:
2903                elem_size = sizeof(u8);
2904                break;
2905        case V4L2_CTRL_TYPE_U16:
2906                elem_size = sizeof(u16);
2907                break;
2908        case V4L2_CTRL_TYPE_U32:
2909                elem_size = sizeof(u32);
2910                break;
2911        case V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS:
2912                elem_size = sizeof(struct v4l2_ctrl_mpeg2_slice_params);
2913                break;
2914        case V4L2_CTRL_TYPE_MPEG2_QUANTIZATION:
2915                elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantization);
2916                break;
2917        case V4L2_CTRL_TYPE_FWHT_PARAMS:
2918                elem_size = sizeof(struct v4l2_ctrl_fwht_params);
2919                break;
2920        case V4L2_CTRL_TYPE_H264_SPS:
2921                elem_size = sizeof(struct v4l2_ctrl_h264_sps);
2922                break;
2923        case V4L2_CTRL_TYPE_H264_PPS:
2924                elem_size = sizeof(struct v4l2_ctrl_h264_pps);
2925                break;
2926        case V4L2_CTRL_TYPE_H264_SCALING_MATRIX:
2927                elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix);
2928                break;
2929        case V4L2_CTRL_TYPE_H264_SLICE_PARAMS:
2930                elem_size = sizeof(struct v4l2_ctrl_h264_slice_params);
2931                break;
2932        case V4L2_CTRL_TYPE_H264_DECODE_PARAMS:
2933                elem_size = sizeof(struct v4l2_ctrl_h264_decode_params);
2934                break;
2935        case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS:
2936                elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights);
2937                break;
2938        case V4L2_CTRL_TYPE_VP8_FRAME:
2939                elem_size = sizeof(struct v4l2_ctrl_vp8_frame);
2940                break;
2941        case V4L2_CTRL_TYPE_HEVC_SPS:
2942                elem_size = sizeof(struct v4l2_ctrl_hevc_sps);
2943                break;
2944        case V4L2_CTRL_TYPE_HEVC_PPS:
2945                elem_size = sizeof(struct v4l2_ctrl_hevc_pps);
2946                break;
2947        case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS:
2948                elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params);
2949                break;
2950        case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
2951                elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info);
2952                break;
2953        case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY:
2954                elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display);
2955                break;
2956        case V4L2_CTRL_TYPE_AREA:
2957                elem_size = sizeof(struct v4l2_area);
2958                break;
2959        default:
2960                if (type < V4L2_CTRL_COMPOUND_TYPES)
2961                        elem_size = sizeof(s32);
2962                break;
2963        }
2964        tot_ctrl_size = elem_size * elems;
2965
2966        /* Sanity checks */
2967        if (id == 0 || name == NULL || !elem_size ||
2968            id >= V4L2_CID_PRIVATE_BASE ||
2969            (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
2970            (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
2971                handler_set_err(hdl, -ERANGE);
2972                return NULL;
2973        }
2974        err = check_range(type, min, max, step, def);
2975        if (err) {
2976                handler_set_err(hdl, err);
2977                return NULL;
2978        }
2979        if (is_array &&
2980            (type == V4L2_CTRL_TYPE_BUTTON ||
2981             type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
2982                handler_set_err(hdl, -EINVAL);
2983                return NULL;
2984        }
2985
2986        sz_extra = 0;
2987        if (type == V4L2_CTRL_TYPE_BUTTON)
2988                flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
2989                        V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
2990        else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
2991                flags |= V4L2_CTRL_FLAG_READ_ONLY;
2992        else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
2993                 type == V4L2_CTRL_TYPE_STRING ||
2994                 type >= V4L2_CTRL_COMPOUND_TYPES ||
2995                 is_array)
2996                sz_extra += 2 * tot_ctrl_size;
2997
2998        if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const)
2999                sz_extra += elem_size;
3000
3001        ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
3002        if (ctrl == NULL) {
3003                handler_set_err(hdl, -ENOMEM);
3004                return NULL;
3005        }
3006
3007        INIT_LIST_HEAD(&ctrl->node);
3008        INIT_LIST_HEAD(&ctrl->ev_subs);
3009        ctrl->handler = hdl;
3010        ctrl->ops = ops;
3011        ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
3012        ctrl->id = id;
3013        ctrl->name = name;
3014        ctrl->type = type;
3015        ctrl->flags = flags;
3016        ctrl->minimum = min;
3017        ctrl->maximum = max;
3018        ctrl->step = step;
3019        ctrl->default_value = def;
3020        ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
3021        ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
3022        ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
3023        ctrl->is_array = is_array;
3024        ctrl->elems = elems;
3025        ctrl->nr_of_dims = nr_of_dims;
3026        if (nr_of_dims)
3027                memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
3028        ctrl->elem_size = elem_size;
3029        if (type == V4L2_CTRL_TYPE_MENU)
3030                ctrl->qmenu = qmenu;
3031        else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
3032                ctrl->qmenu_int = qmenu_int;
3033        ctrl->priv = priv;
3034        ctrl->cur.val = ctrl->val = def;
3035        data = &ctrl[1];
3036
3037        if (!ctrl->is_int) {
3038                ctrl->p_new.p = data;
3039                ctrl->p_cur.p = data + tot_ctrl_size;
3040        } else {
3041                ctrl->p_new.p = &ctrl->val;
3042                ctrl->p_cur.p = &ctrl->cur.val;
3043        }
3044
3045        if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) {
3046                ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size;
3047                memcpy(ctrl->p_def.p, p_def.p_const, elem_size);
3048        }
3049
3050        for (idx = 0; idx < elems; idx++) {
3051                ctrl->type_ops->init(ctrl, idx, ctrl->p_cur);
3052                ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
3053        }
3054
3055        if (handler_new_ref(hdl, ctrl, NULL, false, false)) {
3056                kvfree(ctrl);
3057                return NULL;
3058        }
3059        mutex_lock(hdl->lock);
3060        list_add_tail(&ctrl->node, &hdl->ctrls);
3061        mutex_unlock(hdl->lock);
3062        return ctrl;
3063}
3064
3065struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
3066                        const struct v4l2_ctrl_config *cfg, void *priv)
3067{
3068        bool is_menu;
3069        struct v4l2_ctrl *ctrl;
3070        const char *name = cfg->name;
3071        const char * const *qmenu = cfg->qmenu;
3072        const s64 *qmenu_int = cfg->qmenu_int;
3073        enum v4l2_ctrl_type type = cfg->type;
3074        u32 flags = cfg->flags;
3075        s64 min = cfg->min;
3076        s64 max = cfg->max;
3077        u64 step = cfg->step;
3078        s64 def = cfg->def;
3079
3080        if (name == NULL)
3081                v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
3082                                                                &def, &flags);
3083
3084        is_menu = (type == V4L2_CTRL_TYPE_MENU ||
3085                   type == V4L2_CTRL_TYPE_INTEGER_MENU);
3086        if (is_menu)
3087                WARN_ON(step);
3088        else
3089                WARN_ON(cfg->menu_skip_mask);
3090        if (type == V4L2_CTRL_TYPE_MENU && !qmenu) {
3091                qmenu = v4l2_ctrl_get_menu(cfg->id);
3092        } else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) {
3093                handler_set_err(hdl, -EINVAL);
3094                return NULL;
3095        }
3096
3097        ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
3098                        type, min, max,
3099                        is_menu ? cfg->menu_skip_mask : step, def,
3100                        cfg->dims, cfg->elem_size,
3101                        flags, qmenu, qmenu_int, cfg->p_def, priv);
3102        if (ctrl)
3103                ctrl->is_private = cfg->is_private;
3104        return ctrl;
3105}
3106EXPORT_SYMBOL(v4l2_ctrl_new_custom);
3107
3108/* Helper function for standard non-menu controls */
3109struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
3110                        const struct v4l2_ctrl_ops *ops,
3111                        u32 id, s64 min, s64 max, u64 step, s64 def)
3112{
3113        const char *name;
3114        enum v4l2_ctrl_type type;
3115        u32 flags;
3116
3117        v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3118        if (type == V4L2_CTRL_TYPE_MENU ||
3119            type == V4L2_CTRL_TYPE_INTEGER_MENU ||
3120            type >= V4L2_CTRL_COMPOUND_TYPES) {
3121                handler_set_err(hdl, -EINVAL);
3122                return NULL;
3123        }
3124        return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3125                             min, max, step, def, NULL, 0,
3126                             flags, NULL, NULL, ptr_null, NULL);
3127}
3128EXPORT_SYMBOL(v4l2_ctrl_new_std);
3129
3130/* Helper function for standard menu controls */
3131struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
3132                        const struct v4l2_ctrl_ops *ops,
3133                        u32 id, u8 _max, u64 mask, u8 _def)
3134{
3135        const char * const *qmenu = NULL;
3136        const s64 *qmenu_int = NULL;
3137        unsigned int qmenu_int_len = 0;
3138        const char *name;
3139        enum v4l2_ctrl_type type;
3140        s64 min;
3141        s64 max = _max;
3142        s64 def = _def;
3143        u64 step;
3144        u32 flags;
3145
3146        v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3147
3148        if (type == V4L2_CTRL_TYPE_MENU)
3149                qmenu = v4l2_ctrl_get_menu(id);
3150        else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
3151                qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
3152
3153        if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) {
3154                handler_set_err(hdl, -EINVAL);
3155                return NULL;
3156        }
3157        return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3158                             0, max, mask, def, NULL, 0,
3159                             flags, qmenu, qmenu_int, ptr_null, NULL);
3160}
3161EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
3162
3163/* Helper function for standard menu controls with driver defined menu */
3164struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
3165                        const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
3166                        u64 mask, u8 _def, const char * const *qmenu)
3167{
3168        enum v4l2_ctrl_type type;
3169        const char *name;
3170        u32 flags;
3171        u64 step;
3172        s64 min;
3173        s64 max = _max;
3174        s64 def = _def;
3175
3176        /* v4l2_ctrl_new_std_menu_items() should only be called for
3177         * standard controls without a standard menu.
3178         */
3179        if (v4l2_ctrl_get_menu(id)) {
3180                handler_set_err(hdl, -EINVAL);
3181                return NULL;
3182        }
3183
3184        v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3185        if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
3186                handler_set_err(hdl, -EINVAL);
3187                return NULL;
3188        }
3189        return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3190                             0, max, mask, def, NULL, 0,
3191                             flags, qmenu, NULL, ptr_null, NULL);
3192
3193}
3194EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
3195
3196/* Helper function for standard compound controls */
3197struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
3198                                const struct v4l2_ctrl_ops *ops, u32 id,
3199                                const union v4l2_ctrl_ptr p_def)
3200{
3201        const char *name;
3202        enum v4l2_ctrl_type type;
3203        u32 flags;
3204        s64 min, max, step, def;
3205
3206        v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3207        if (type < V4L2_CTRL_COMPOUND_TYPES) {
3208                handler_set_err(hdl, -EINVAL);
3209                return NULL;
3210        }
3211        return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3212                             min, max, step, def, NULL, 0,
3213                             flags, NULL, NULL, p_def, NULL);
3214}
3215EXPORT_SYMBOL(v4l2_ctrl_new_std_compound);
3216
3217/* Helper function for standard integer menu controls */
3218struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
3219                        const struct v4l2_ctrl_ops *ops,
3220                        u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
3221{
3222        const char *name;
3223        enum v4l2_ctrl_type type;
3224        s64 min;
3225        u64 step;
3226        s64 max = _max;
3227        s64 def = _def;
3228        u32 flags;
3229
3230        v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
3231        if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
3232                handler_set_err(hdl, -EINVAL);
3233                return NULL;
3234        }
3235        return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
3236                             0, max, 0, def, NULL, 0,
3237                             flags, NULL, qmenu_int, ptr_null, NULL);
3238}
3239EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
3240
3241/* Add the controls from another handler to our own. */
3242int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
3243                          struct v4l2_ctrl_handler *add,
3244                          bool (*filter)(const struct v4l2_ctrl *ctrl),
3245                          bool from_other_dev)
3246{
3247        struct v4l2_ctrl_ref *ref;
3248        int ret = 0;
3249
3250        /* Do nothing if either handler is NULL or if they are the same */
3251        if (!hdl || !add || hdl == add)
3252                return 0;
3253        if (hdl->error)
3254                return hdl->error;
3255        mutex_lock(add->lock);
3256        list_for_each_entry(ref, &add->ctrl_refs, node) {
3257                struct v4l2_ctrl *ctrl = ref->ctrl;
3258
3259                /* Skip handler-private controls. */
3260                if (ctrl->is_private)
3261                        continue;
3262                /* And control classes */
3263                if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
3264                        continue;
3265                /* Filter any unwanted controls */
3266                if (filter && !filter(ctrl))
3267                        continue;
3268                ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false);
3269                if (ret)
3270                        break;
3271        }
3272        mutex_unlock(add->lock);
3273        return ret;
3274}
3275EXPORT_SYMBOL(v4l2_ctrl_add_handler);
3276
3277bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
3278{
3279        if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
3280                return true;
3281        if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
3282                return true;
3283        switch (ctrl->id) {
3284        case V4L2_CID_AUDIO_MUTE:
3285        case V4L2_CID_AUDIO_VOLUME:
3286        case V4L2_CID_AUDIO_BALANCE:
3287        case V4L2_CID_AUDIO_BASS:
3288        case V4L2_CID_AUDIO_TREBLE:
3289        case V4L2_CID_AUDIO_LOUDNESS:
3290                return true;
3291        default:
3292                break;
3293        }
3294        return false;
3295}
3296EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
3297
3298/* Cluster controls */
3299void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
3300{
3301        bool has_volatiles = false;
3302        int i;
3303
3304        /* The first control is the master control and it must not be NULL */
3305        if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
3306                return;
3307
3308        for (i = 0; i < ncontrols; i++) {
3309                if (controls[i]) {
3310                        controls[i]->cluster = controls;
3311                        controls[i]->ncontrols = ncontrols;
3312                        if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
3313                                has_volatiles = true;
3314                }
3315        }
3316        controls[0]->has_volatiles = has_volatiles;
3317}
3318EXPORT_SYMBOL(v4l2_ctrl_cluster);
3319
3320void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
3321                            u8 manual_val, bool set_volatile)
3322{
3323        struct v4l2_ctrl *master = controls[0];
3324        u32 flag = 0;
3325        int i;
3326
3327        v4l2_ctrl_cluster(ncontrols, controls);
3328        WARN_ON(ncontrols <= 1);
3329        WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
3330        WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
3331        master->is_auto = true;
3332        master->has_volatiles = set_volatile;
3333        master->manual_mode_value = manual_val;
3334        master->flags |= V4L2_CTRL_FLAG_UPDATE;
3335
3336        if (!is_cur_manual(master))
3337                flag = V4L2_CTRL_FLAG_INACTIVE |
3338                        (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
3339
3340        for (i = 1; i < ncontrols; i++)
3341                if (controls[i])
3342                        controls[i]->flags |= flag;
3343}
3344EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
3345
3346/* Activate/deactivate a control. */
3347void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
3348{
3349        /* invert since the actual flag is called 'inactive' */
3350        bool inactive = !active;
3351        bool old;
3352
3353        if (ctrl == NULL)
3354                return;
3355
3356        if (inactive)
3357                /* set V4L2_CTRL_FLAG_INACTIVE */
3358                old = test_and_set_bit(4, &ctrl->flags);
3359        else
3360                /* clear V4L2_CTRL_FLAG_INACTIVE */
3361                old = test_and_clear_bit(4, &ctrl->flags);
3362        if (old != inactive)
3363                send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
3364}
3365EXPORT_SYMBOL(v4l2_ctrl_activate);
3366
3367void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
3368{
3369        bool old;
3370
3371        if (ctrl == NULL)
3372                return;
3373
3374        lockdep_assert_held(ctrl->handler->lock);
3375
3376        if (grabbed)
3377                /* set V4L2_CTRL_FLAG_GRABBED */
3378                old = test_and_set_bit(1, &ctrl->flags);
3379        else
3380                /* clear V4L2_CTRL_FLAG_GRABBED */
3381                old = test_and_clear_bit(1, &ctrl->flags);
3382        if (old != grabbed)
3383                send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
3384}
3385EXPORT_SYMBOL(__v4l2_ctrl_grab);
3386
3387/* Log the control name and value */
3388static void log_ctrl(const struct v4l2_ctrl *ctrl,
3389                     const char *prefix, const char *colon)
3390{
3391        if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
3392                return;
3393        if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
3394                return;
3395
3396        pr_info("%s%s%s: ", prefix, colon, ctrl->name);
3397
3398        ctrl->type_ops->log(ctrl);
3399
3400        if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
3401                           V4L2_CTRL_FLAG_GRABBED |
3402                           V4L2_CTRL_FLAG_VOLATILE)) {
3403                if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
3404                        pr_cont(" inactive");
3405                if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
3406                        pr_cont(" grabbed");
3407                if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
3408                        pr_cont(" volatile");
3409        }
3410        pr_cont("\n");
3411}
3412
3413/* Log all controls owned by the handler */
3414void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
3415                                  const char *prefix)
3416{
3417        struct v4l2_ctrl *ctrl;
3418        const char *colon = "";
3419        int len;
3420
3421        if (hdl == NULL)
3422                return;
3423        if (prefix == NULL)
3424                prefix = "";
3425        len = strlen(prefix);
3426        if (len && prefix[len - 1] != ' ')
3427                colon = ": ";
3428        mutex_lock(hdl->lock);
3429        list_for_each_entry(ctrl, &hdl->ctrls, node)
3430                if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
3431                        log_ctrl(ctrl, prefix, colon);
3432        mutex_unlock(hdl->lock);
3433}
3434EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
3435
3436int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
3437{
3438        v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
3439        return 0;
3440}
3441EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
3442
3443/* Call s_ctrl for all controls owned by the handler */
3444int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
3445{
3446        struct v4l2_ctrl *ctrl;
3447        int ret = 0;
3448
3449        if (hdl == NULL)
3450                return 0;
3451
3452        lockdep_assert_held(hdl->lock);
3453
3454        list_for_each_entry(ctrl, &hdl->ctrls, node)
3455                ctrl->done = false;
3456
3457        list_for_each_entry(ctrl, &hdl->ctrls, node) {
3458                struct v4l2_ctrl *master = ctrl->cluster[0];
3459                int i;
3460
3461                /* Skip if this control was already handled by a cluster. */
3462                /* Skip button controls and read-only controls. */
3463                if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
3464                    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
3465                        continue;
3466
3467                for (i = 0; i < master->ncontrols; i++) {
3468                        if (master->cluster[i]) {
3469                                cur_to_new(master->cluster[i]);
3470                                master->cluster[i]->is_new = 1;
3471                                master->cluster[i]->done = true;
3472                        }
3473                }
3474                ret = call_op(master, s_ctrl);
3475                if (ret)
3476                        break;
3477        }
3478
3479        return ret;
3480}
3481EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup);
3482
3483int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
3484{
3485        int ret;
3486
3487        if (hdl == NULL)
3488                return 0;
3489
3490        mutex_lock(hdl->lock);
3491        ret = __v4l2_ctrl_handler_setup(hdl);
3492        mutex_unlock(hdl->lock);
3493
3494        return ret;
3495}
3496EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
3497
3498/* Implement VIDIOC_QUERY_EXT_CTRL */
3499int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
3500{
3501        const unsigned next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
3502        u32 id = qc->id & V4L2_CTRL_ID_MASK;
3503        struct v4l2_ctrl_ref *ref;
3504        struct v4l2_ctrl *ctrl;
3505
3506        if (hdl == NULL)
3507                return -EINVAL;
3508
3509        mutex_lock(hdl->lock);
3510
3511        /* Try to find it */
3512        ref = find_ref(hdl, id);
3513
3514        if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
3515                bool is_compound;
3516                /* Match any control that is not hidden */
3517                unsigned mask = 1;
3518                bool match = false;
3519
3520                if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
3521                        /* Match any hidden control */
3522                        match = true;
3523                } else if ((qc->id & next_flags) == next_flags) {
3524                        /* Match any control, compound or not */
3525                        mask = 0;
3526                }
3527
3528                /* Find the next control with ID > qc->id */
3529
3530                /* Did we reach the end of the control list? */
3531                if (id >= node2id(hdl->ctrl_refs.prev)) {
3532                        ref = NULL; /* Yes, so there is no next control */
3533                } else if (ref) {
3534                        /* We found a control with the given ID, so just get
3535                           the next valid one in the list. */
3536                        list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
3537                                is_compound = ref->ctrl->is_array ||
3538                                        ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
3539                                if (id < ref->ctrl->id &&
3540                                    (is_compound & mask) == match)
3541                                        break;
3542                        }
3543                        if (&ref->node == &hdl->ctrl_refs)
3544                                ref = NULL;
3545                } else {
3546                        /* No control with the given ID exists, so start
3547                           searching for the next largest ID. We know there
3548                           is one, otherwise the first 'if' above would have
3549                           been true. */
3550                        list_for_each_entry(ref, &hdl->ctrl_refs, node) {
3551                                is_compound = ref->ctrl->is_array ||
3552                                        ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
3553                                if (id < ref->ctrl->id &&
3554                                    (is_compound & mask) == match)
3555                                        break;
3556                        }
3557                        if (&ref->node == &hdl->ctrl_refs)
3558                                ref = NULL;
3559                }
3560        }
3561        mutex_unlock(hdl->lock);
3562
3563        if (!ref)
3564                return -EINVAL;
3565
3566        ctrl = ref->ctrl;
3567        memset(qc, 0, sizeof(*qc));
3568        if (id >= V4L2_CID_PRIVATE_BASE)
3569                qc->id = id;
3570        else
3571                qc->id = ctrl->id;
3572        strscpy(qc->name, ctrl->name, sizeof(qc->name));
3573        qc->flags = user_flags(ctrl);
3574        qc->type = ctrl->type;
3575        qc->elem_size = ctrl->elem_size;
3576        qc->elems = ctrl->elems;
3577        qc->nr_of_dims = ctrl->nr_of_dims;
3578        memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
3579        qc->minimum = ctrl->minimum;
3580        qc->maximum = ctrl->maximum;
3581        qc->default_value = ctrl->default_value;
3582        if (ctrl->type == V4L2_CTRL_TYPE_MENU
3583            || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
3584                qc->step = 1;
3585        else
3586                qc->step = ctrl->step;
3587        return 0;
3588}
3589EXPORT_SYMBOL(v4l2_query_ext_ctrl);
3590
3591/* Implement VIDIOC_QUERYCTRL */
3592int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
3593{
3594        struct v4l2_query_ext_ctrl qec = { qc->id };
3595        int rc;
3596
3597        rc = v4l2_query_ext_ctrl(hdl, &qec);
3598        if (rc)
3599                return rc;
3600
3601        qc->id = qec.id;
3602        qc->type = qec.type;
3603        qc->flags = qec.flags;
3604        strscpy(qc->name, qec.name, sizeof(qc->name));
3605        switch (qc->type) {
3606        case V4L2_CTRL_TYPE_INTEGER:
3607        case V4L2_CTRL_TYPE_BOOLEAN:
3608        case V4L2_CTRL_TYPE_MENU:
3609        case V4L2_CTRL_TYPE_INTEGER_MENU:
3610        case V4L2_CTRL_TYPE_STRING:
3611        case V4L2_CTRL_TYPE_BITMASK:
3612                qc->minimum = qec.minimum;
3613                qc->maximum = qec.maximum;
3614                qc->step = qec.step;
3615                qc->default_value = qec.default_value;
3616                break;
3617        default:
3618                qc->minimum = 0;
3619                qc->maximum = 0;
3620                qc->step = 0;
3621                qc->default_value = 0;
3622                break;
3623        }
3624        return 0;
3625}
3626EXPORT_SYMBOL(v4l2_queryctrl);
3627
3628/* Implement VIDIOC_QUERYMENU */
3629int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
3630{
3631        struct v4l2_ctrl *ctrl;
3632        u32 i = qm->index;
3633
3634        ctrl = v4l2_ctrl_find(hdl, qm->id);
3635        if (!ctrl)
3636                return -EINVAL;
3637
3638        qm->reserved = 0;
3639        /* Sanity checks */
3640        switch (ctrl->type) {
3641        case V4L2_CTRL_TYPE_MENU:
3642                if (ctrl->qmenu == NULL)
3643                        return -EINVAL;
3644                break;
3645        case V4L2_CTRL_TYPE_INTEGER_MENU:
3646                if (ctrl->qmenu_int == NULL)
3647                        return -EINVAL;
3648                break;
3649        default:
3650                return -EINVAL;
3651        }
3652
3653        if (i < ctrl->minimum || i > ctrl->maximum)
3654                return -EINVAL;
3655
3656        /* Use mask to see if this menu item should be skipped */
3657        if (ctrl->menu_skip_mask & (1ULL << i))
3658                return -EINVAL;
3659        /* Empty menu items should also be skipped */
3660        if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
3661                if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
3662                        return -EINVAL;
3663                strscpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
3664        } else {
3665                qm->value = ctrl->qmenu_int[i];
3666        }
3667        return 0;
3668}
3669EXPORT_SYMBOL(v4l2_querymenu);
3670
3671static int v4l2_ctrl_request_clone(struct v4l2_ctrl_handler *hdl,
3672                                   const struct v4l2_ctrl_handler *from)
3673{
3674        struct v4l2_ctrl_ref *ref;
3675        int err = 0;
3676
3677        if (WARN_ON(!hdl || hdl == from))
3678                return -EINVAL;
3679
3680        if (hdl->error)
3681                return hdl->error;
3682
3683        WARN_ON(hdl->lock != &hdl->_lock);
3684
3685        mutex_lock(from->lock);
3686        list_for_each_entry(ref, &from->ctrl_refs, node) {
3687                struct v4l2_ctrl *ctrl = ref->ctrl;
3688                struct v4l2_ctrl_ref *new_ref;
3689
3690                /* Skip refs inherited from other devices */
3691                if (ref->from_other_dev)
3692                        continue;
3693                err = handler_new_ref(hdl, ctrl, &new_ref, false, true);
3694                if (err)
3695                        break;
3696        }
3697        mutex_unlock(from->lock);
3698        return err;
3699}
3700
3701static void v4l2_ctrl_request_queue(struct media_request_object *obj)
3702{
3703        struct v4l2_ctrl_handler *hdl =
3704                container_of(obj, struct v4l2_ctrl_handler, req_obj);
3705        struct v4l2_ctrl_handler *main_hdl = obj->priv;
3706
3707        mutex_lock(main_hdl->lock);
3708        list_add_tail(&hdl->requests_queued, &main_hdl->requests_queued);
3709        hdl->request_is_queued = true;
3710        mutex_unlock(main_hdl->lock);
3711}
3712
3713static void v4l2_ctrl_request_unbind(struct media_request_object *obj)
3714{
3715        struct v4l2_ctrl_handler *hdl =
3716                container_of(obj, struct v4l2_ctrl_handler, req_obj);
3717        struct v4l2_ctrl_handler *main_hdl = obj->priv;
3718
3719        mutex_lock(main_hdl->lock);
3720        list_del_init(&hdl->requests);
3721        if (hdl->request_is_queued) {
3722                list_del_init(&hdl->requests_queued);
3723                hdl->request_is_queued = false;
3724        }
3725        mutex_unlock(main_hdl->lock);
3726}
3727
3728static void v4l2_ctrl_request_release(struct media_request_object *obj)
3729{
3730        struct v4l2_ctrl_handler *hdl =
3731                container_of(obj, struct v4l2_ctrl_handler, req_obj);
3732
3733        v4l2_ctrl_handler_free(hdl);
3734        kfree(hdl);
3735}
3736
3737static const struct media_request_object_ops req_ops = {
3738        .queue = v4l2_ctrl_request_queue,
3739        .unbind = v4l2_ctrl_request_unbind,
3740        .release = v4l2_ctrl_request_release,
3741};
3742
3743struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(struct media_request *req,
3744                                        struct v4l2_ctrl_handler *parent)
3745{
3746        struct media_request_object *obj;
3747
3748        if (WARN_ON(req->state != MEDIA_REQUEST_STATE_VALIDATING &&
3749                    req->state != MEDIA_REQUEST_STATE_QUEUED))
3750                return NULL;
3751
3752        obj = media_request_object_find(req, &req_ops, parent);
3753        if (obj)
3754                return container_of(obj, struct v4l2_ctrl_handler, req_obj);
3755        return NULL;
3756}
3757EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_find);
3758
3759struct v4l2_ctrl *
3760v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
3761{
3762        struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
3763
3764        return (ref && ref->valid_p_req) ? ref->ctrl : NULL;
3765}
3766EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_ctrl_find);
3767
3768static int v4l2_ctrl_request_bind(struct media_request *req,
3769                           struct v4l2_ctrl_handler *hdl,
3770                           struct v4l2_ctrl_handler *from)
3771{
3772        int ret;
3773
3774        ret = v4l2_ctrl_request_clone(hdl, from);
3775
3776        if (!ret) {
3777                ret = media_request_object_bind(req, &req_ops,
3778                                                from, false, &hdl->req_obj);
3779                if (!ret) {
3780                        mutex_lock(from->lock);
3781                        list_add_tail(&hdl->requests, &from->requests);
3782                        mutex_unlock(from->lock);
3783                }
3784        }
3785        return ret;
3786}
3787
3788/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
3789
3790   It is not a fully atomic operation, just best-effort only. After all, if
3791   multiple controls have to be set through multiple i2c writes (for example)
3792   then some initial writes may succeed while others fail. Thus leaving the
3793   system in an inconsistent state. The question is how much effort you are
3794   willing to spend on trying to make something atomic that really isn't.
3795
3796   From the point of view of an application the main requirement is that
3797   when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
3798   error should be returned without actually affecting any controls.
3799
3800   If all the values are correct, then it is acceptable to just give up
3801   in case of low-level errors.
3802
3803   It is important though that the application can tell when only a partial
3804   configuration was done. The way we do that is through the error_idx field
3805   of struct v4l2_ext_controls: if that is equal to the count field then no
3806   controls were affected. Otherwise all controls before that index were
3807   successful in performing their 'get' or 'set' operation, the control at
3808   the given index failed, and you don't know what happened with the controls
3809   after the failed one. Since if they were part of a control cluster they
3810   could have been successfully processed (if a cluster member was encountered
3811   at index < error_idx), they could have failed (if a cluster member was at
3812   error_idx), or they may not have been processed yet (if the first cluster
3813   member appeared after error_idx).
3814
3815   It is all fairly theoretical, though. In practice all you can do is to
3816   bail out. If error_idx == count, then it is an application bug. If
3817   error_idx < count then it is only an application bug if the error code was
3818   EBUSY. That usually means that something started streaming just when you
3819   tried to set the controls. In all other cases it is a driver/hardware
3820   problem and all you can do is to retry or bail out.
3821
3822   Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
3823   never modifies controls the error_idx is just set to whatever control
3824   has an invalid value.
3825 */
3826
3827/* Prepare for the extended g/s/try functions.
3828   Find the controls in the control array and do some basic checks. */
3829static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
3830                             struct v4l2_ext_controls *cs,
3831                             struct v4l2_ctrl_helper *helpers,
3832                             struct video_device *vdev,
3833                             bool get)
3834{
3835        struct v4l2_ctrl_helper *h;
3836        bool have_clusters = false;
3837        u32 i;
3838
3839        for (i = 0, h = helpers; i < cs->count; i++, h++) {
3840                struct v4l2_ext_control *c = &cs->controls[i];
3841                struct v4l2_ctrl_ref *ref;
3842                struct v4l2_ctrl *ctrl;
3843                u32 id = c->id & V4L2_CTRL_ID_MASK;
3844
3845                cs->error_idx = i;
3846
3847                if (cs->which &&
3848                    cs->which != V4L2_CTRL_WHICH_DEF_VAL &&
3849                    cs->which != V4L2_CTRL_WHICH_REQUEST_VAL &&
3850                    V4L2_CTRL_ID2WHICH(id) != cs->which) {
3851                        dprintk(vdev,
3852                                "invalid which 0x%x or control id 0x%x\n",
3853                                cs->which, id);
3854                        return -EINVAL;
3855                }
3856
3857                /* Old-style private controls are not allowed for
3858                   extended controls */
3859                if (id >= V4L2_CID_PRIVATE_BASE) {
3860                        dprintk(vdev,
3861                                "old-style private controls not allowed\n");
3862                        return -EINVAL;
3863                }
3864                ref = find_ref_lock(hdl, id);
3865                if (ref == NULL) {
3866                        dprintk(vdev, "cannot find control id 0x%x\n", id);
3867                        return -EINVAL;
3868                }
3869                h->ref = ref;
3870                ctrl = ref->ctrl;
3871                if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED) {
3872                        dprintk(vdev, "control id 0x%x is disabled\n", id);
3873                        return -EINVAL;
3874                }
3875
3876                if (ctrl->cluster[0]->ncontrols > 1)
3877                        have_clusters = true;
3878                if (ctrl->cluster[0] != ctrl)
3879                        ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
3880                if (ctrl->is_ptr && !ctrl->is_string) {
3881                        unsigned tot_size = ctrl->elems * ctrl->elem_size;
3882
3883                        if (c->size < tot_size) {
3884                                /*
3885                                 * In the get case the application first
3886                                 * queries to obtain the size of the control.
3887                                 */
3888                                if (get) {
3889                                        c->size = tot_size;
3890                                        return -ENOSPC;
3891                                }
3892                                dprintk(vdev,
3893                                        "pointer control id 0x%x size too small, %d bytes but %d bytes needed\n",
3894                                        id, c->size, tot_size);
3895                                return -EFAULT;
3896                        }
3897                        c->size = tot_size;
3898                }
3899                /* Store the ref to the master control of the cluster */
3900                h->mref = ref;
3901                /* Initially set next to 0, meaning that there is no other
3902                   control in this helper array belonging to the same
3903                   cluster */
3904