linux/drivers/media/v4l2-core/v4l2-ioctl.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Video capture interface for Linux version 2
   4 *
   5 * A generic framework to process V4L2 ioctl commands.
   6 *
   7 * Authors:     Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
   8 *              Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
   9 */
  10
  11#include <linux/mm.h>
  12#include <linux/module.h>
  13#include <linux/slab.h>
  14#include <linux/types.h>
  15#include <linux/kernel.h>
  16#include <linux/version.h>
  17
  18#include <linux/v4l2-subdev.h>
  19#include <linux/videodev2.h>
  20
  21#include <media/v4l2-common.h>
  22#include <media/v4l2-ioctl.h>
  23#include <media/v4l2-ctrls.h>
  24#include <media/v4l2-fh.h>
  25#include <media/v4l2-event.h>
  26#include <media/v4l2-device.h>
  27#include <media/videobuf2-v4l2.h>
  28#include <media/v4l2-mc.h>
  29#include <media/v4l2-mem2mem.h>
  30
  31#include <trace/events/v4l2.h>
  32
  33/* Zero out the end of the struct pointed to by p.  Everything after, but
  34 * not including, the specified field is cleared. */
  35#define CLEAR_AFTER_FIELD(p, field) \
  36        memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
  37        0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
  38
  39#define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
  40
  41struct std_descr {
  42        v4l2_std_id std;
  43        const char *descr;
  44};
  45
  46static const struct std_descr standards[] = {
  47        { V4L2_STD_NTSC,        "NTSC"      },
  48        { V4L2_STD_NTSC_M,      "NTSC-M"    },
  49        { V4L2_STD_NTSC_M_JP,   "NTSC-M-JP" },
  50        { V4L2_STD_NTSC_M_KR,   "NTSC-M-KR" },
  51        { V4L2_STD_NTSC_443,    "NTSC-443"  },
  52        { V4L2_STD_PAL,         "PAL"       },
  53        { V4L2_STD_PAL_BG,      "PAL-BG"    },
  54        { V4L2_STD_PAL_B,       "PAL-B"     },
  55        { V4L2_STD_PAL_B1,      "PAL-B1"    },
  56        { V4L2_STD_PAL_G,       "PAL-G"     },
  57        { V4L2_STD_PAL_H,       "PAL-H"     },
  58        { V4L2_STD_PAL_I,       "PAL-I"     },
  59        { V4L2_STD_PAL_DK,      "PAL-DK"    },
  60        { V4L2_STD_PAL_D,       "PAL-D"     },
  61        { V4L2_STD_PAL_D1,      "PAL-D1"    },
  62        { V4L2_STD_PAL_K,       "PAL-K"     },
  63        { V4L2_STD_PAL_M,       "PAL-M"     },
  64        { V4L2_STD_PAL_N,       "PAL-N"     },
  65        { V4L2_STD_PAL_Nc,      "PAL-Nc"    },
  66        { V4L2_STD_PAL_60,      "PAL-60"    },
  67        { V4L2_STD_SECAM,       "SECAM"     },
  68        { V4L2_STD_SECAM_B,     "SECAM-B"   },
  69        { V4L2_STD_SECAM_G,     "SECAM-G"   },
  70        { V4L2_STD_SECAM_H,     "SECAM-H"   },
  71        { V4L2_STD_SECAM_DK,    "SECAM-DK"  },
  72        { V4L2_STD_SECAM_D,     "SECAM-D"   },
  73        { V4L2_STD_SECAM_K,     "SECAM-K"   },
  74        { V4L2_STD_SECAM_K1,    "SECAM-K1"  },
  75        { V4L2_STD_SECAM_L,     "SECAM-L"   },
  76        { V4L2_STD_SECAM_LC,    "SECAM-Lc"  },
  77        { 0,                    "Unknown"   }
  78};
  79
  80/* video4linux standard ID conversion to standard name
  81 */
  82const char *v4l2_norm_to_name(v4l2_std_id id)
  83{
  84        u32 myid = id;
  85        int i;
  86
  87        /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
  88           64 bit comparisons. So, on that architecture, with some gcc
  89           variants, compilation fails. Currently, the max value is 30bit wide.
  90         */
  91        BUG_ON(myid != id);
  92
  93        for (i = 0; standards[i].std; i++)
  94                if (myid == standards[i].std)
  95                        break;
  96        return standards[i].descr;
  97}
  98EXPORT_SYMBOL(v4l2_norm_to_name);
  99
 100/* Returns frame period for the given standard */
 101void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
 102{
 103        if (id & V4L2_STD_525_60) {
 104                frameperiod->numerator = 1001;
 105                frameperiod->denominator = 30000;
 106        } else {
 107                frameperiod->numerator = 1;
 108                frameperiod->denominator = 25;
 109        }
 110}
 111EXPORT_SYMBOL(v4l2_video_std_frame_period);
 112
 113/* Fill in the fields of a v4l2_standard structure according to the
 114   'id' and 'transmission' parameters.  Returns negative on error.  */
 115int v4l2_video_std_construct(struct v4l2_standard *vs,
 116                             int id, const char *name)
 117{
 118        vs->id = id;
 119        v4l2_video_std_frame_period(id, &vs->frameperiod);
 120        vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
 121        strscpy(vs->name, name, sizeof(vs->name));
 122        return 0;
 123}
 124EXPORT_SYMBOL(v4l2_video_std_construct);
 125
 126/* Fill in the fields of a v4l2_standard structure according to the
 127 * 'id' and 'vs->index' parameters. Returns negative on error. */
 128int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
 129{
 130        v4l2_std_id curr_id = 0;
 131        unsigned int index = vs->index, i, j = 0;
 132        const char *descr = "";
 133
 134        /* Return -ENODATA if the id for the current input
 135           or output is 0, meaning that it doesn't support this API. */
 136        if (id == 0)
 137                return -ENODATA;
 138
 139        /* Return norm array in a canonical way */
 140        for (i = 0; i <= index && id; i++) {
 141                /* last std value in the standards array is 0, so this
 142                   while always ends there since (id & 0) == 0. */
 143                while ((id & standards[j].std) != standards[j].std)
 144                        j++;
 145                curr_id = standards[j].std;
 146                descr = standards[j].descr;
 147                j++;
 148                if (curr_id == 0)
 149                        break;
 150                if (curr_id != V4L2_STD_PAL &&
 151                                curr_id != V4L2_STD_SECAM &&
 152                                curr_id != V4L2_STD_NTSC)
 153                        id &= ~curr_id;
 154        }
 155        if (i <= index)
 156                return -EINVAL;
 157
 158        v4l2_video_std_construct(vs, curr_id, descr);
 159        return 0;
 160}
 161
 162/* ----------------------------------------------------------------- */
 163/* some arrays for pretty-printing debug messages of enum types      */
 164
 165const char *v4l2_field_names[] = {
 166        [V4L2_FIELD_ANY]        = "any",
 167        [V4L2_FIELD_NONE]       = "none",
 168        [V4L2_FIELD_TOP]        = "top",
 169        [V4L2_FIELD_BOTTOM]     = "bottom",
 170        [V4L2_FIELD_INTERLACED] = "interlaced",
 171        [V4L2_FIELD_SEQ_TB]     = "seq-tb",
 172        [V4L2_FIELD_SEQ_BT]     = "seq-bt",
 173        [V4L2_FIELD_ALTERNATE]  = "alternate",
 174        [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
 175        [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
 176};
 177EXPORT_SYMBOL(v4l2_field_names);
 178
 179const char *v4l2_type_names[] = {
 180        [0]                                = "0",
 181        [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
 182        [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
 183        [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
 184        [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
 185        [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
 186        [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
 187        [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
 188        [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
 189        [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
 190        [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
 191        [V4L2_BUF_TYPE_SDR_CAPTURE]        = "sdr-cap",
 192        [V4L2_BUF_TYPE_SDR_OUTPUT]         = "sdr-out",
 193        [V4L2_BUF_TYPE_META_CAPTURE]       = "meta-cap",
 194        [V4L2_BUF_TYPE_META_OUTPUT]        = "meta-out",
 195};
 196EXPORT_SYMBOL(v4l2_type_names);
 197
 198static const char *v4l2_memory_names[] = {
 199        [V4L2_MEMORY_MMAP]    = "mmap",
 200        [V4L2_MEMORY_USERPTR] = "userptr",
 201        [V4L2_MEMORY_OVERLAY] = "overlay",
 202        [V4L2_MEMORY_DMABUF] = "dmabuf",
 203};
 204
 205#define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
 206
 207/* ------------------------------------------------------------------ */
 208/* debug help functions                                               */
 209
 210static void v4l_print_querycap(const void *arg, bool write_only)
 211{
 212        const struct v4l2_capability *p = arg;
 213
 214        pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
 215                (int)sizeof(p->driver), p->driver,
 216                (int)sizeof(p->card), p->card,
 217                (int)sizeof(p->bus_info), p->bus_info,
 218                p->version, p->capabilities, p->device_caps);
 219}
 220
 221static void v4l_print_enuminput(const void *arg, bool write_only)
 222{
 223        const struct v4l2_input *p = arg;
 224
 225        pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
 226                p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
 227                p->tuner, (unsigned long long)p->std, p->status,
 228                p->capabilities);
 229}
 230
 231static void v4l_print_enumoutput(const void *arg, bool write_only)
 232{
 233        const struct v4l2_output *p = arg;
 234
 235        pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
 236                p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
 237                p->modulator, (unsigned long long)p->std, p->capabilities);
 238}
 239
 240static void v4l_print_audio(const void *arg, bool write_only)
 241{
 242        const struct v4l2_audio *p = arg;
 243
 244        if (write_only)
 245                pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
 246        else
 247                pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
 248                        p->index, (int)sizeof(p->name), p->name,
 249                        p->capability, p->mode);
 250}
 251
 252static void v4l_print_audioout(const void *arg, bool write_only)
 253{
 254        const struct v4l2_audioout *p = arg;
 255
 256        if (write_only)
 257                pr_cont("index=%u\n", p->index);
 258        else
 259                pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
 260                        p->index, (int)sizeof(p->name), p->name,
 261                        p->capability, p->mode);
 262}
 263
 264static void v4l_print_fmtdesc(const void *arg, bool write_only)
 265{
 266        const struct v4l2_fmtdesc *p = arg;
 267
 268        pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
 269                p->index, prt_names(p->type, v4l2_type_names),
 270                p->flags, (p->pixelformat & 0xff),
 271                (p->pixelformat >>  8) & 0xff,
 272                (p->pixelformat >> 16) & 0xff,
 273                (p->pixelformat >> 24) & 0xff,
 274                (int)sizeof(p->description), p->description);
 275}
 276
 277static void v4l_print_format(const void *arg, bool write_only)
 278{
 279        const struct v4l2_format *p = arg;
 280        const struct v4l2_pix_format *pix;
 281        const struct v4l2_pix_format_mplane *mp;
 282        const struct v4l2_vbi_format *vbi;
 283        const struct v4l2_sliced_vbi_format *sliced;
 284        const struct v4l2_window *win;
 285        const struct v4l2_sdr_format *sdr;
 286        const struct v4l2_meta_format *meta;
 287        u32 planes;
 288        unsigned i;
 289
 290        pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
 291        switch (p->type) {
 292        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 293        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 294                pix = &p->fmt.pix;
 295                pr_cont(", width=%u, height=%u, pixelformat=%c%c%c%c, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
 296                        pix->width, pix->height,
 297                        (pix->pixelformat & 0xff),
 298                        (pix->pixelformat >>  8) & 0xff,
 299                        (pix->pixelformat >> 16) & 0xff,
 300                        (pix->pixelformat >> 24) & 0xff,
 301                        prt_names(pix->field, v4l2_field_names),
 302                        pix->bytesperline, pix->sizeimage,
 303                        pix->colorspace, pix->flags, pix->ycbcr_enc,
 304                        pix->quantization, pix->xfer_func);
 305                break;
 306        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 307        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 308                mp = &p->fmt.pix_mp;
 309                pr_cont(", width=%u, height=%u, format=%c%c%c%c, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
 310                        mp->width, mp->height,
 311                        (mp->pixelformat & 0xff),
 312                        (mp->pixelformat >>  8) & 0xff,
 313                        (mp->pixelformat >> 16) & 0xff,
 314                        (mp->pixelformat >> 24) & 0xff,
 315                        prt_names(mp->field, v4l2_field_names),
 316                        mp->colorspace, mp->num_planes, mp->flags,
 317                        mp->ycbcr_enc, mp->quantization, mp->xfer_func);
 318                planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
 319                for (i = 0; i < planes; i++)
 320                        printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
 321                                        mp->plane_fmt[i].bytesperline,
 322                                        mp->plane_fmt[i].sizeimage);
 323                break;
 324        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 325        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 326                win = &p->fmt.win;
 327                /* Note: we can't print the clip list here since the clips
 328                 * pointer is a userspace pointer, not a kernelspace
 329                 * pointer. */
 330                pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, chromakey=0x%08x, clipcount=%u, clips=%p, bitmap=%p, global_alpha=0x%02x\n",
 331                        win->w.width, win->w.height, win->w.left, win->w.top,
 332                        prt_names(win->field, v4l2_field_names),
 333                        win->chromakey, win->clipcount, win->clips,
 334                        win->bitmap, win->global_alpha);
 335                break;
 336        case V4L2_BUF_TYPE_VBI_CAPTURE:
 337        case V4L2_BUF_TYPE_VBI_OUTPUT:
 338                vbi = &p->fmt.vbi;
 339                pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
 340                        vbi->sampling_rate, vbi->offset,
 341                        vbi->samples_per_line,
 342                        (vbi->sample_format & 0xff),
 343                        (vbi->sample_format >>  8) & 0xff,
 344                        (vbi->sample_format >> 16) & 0xff,
 345                        (vbi->sample_format >> 24) & 0xff,
 346                        vbi->start[0], vbi->start[1],
 347                        vbi->count[0], vbi->count[1]);
 348                break;
 349        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 350        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 351                sliced = &p->fmt.sliced;
 352                pr_cont(", service_set=0x%08x, io_size=%d\n",
 353                                sliced->service_set, sliced->io_size);
 354                for (i = 0; i < 24; i++)
 355                        printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
 356                                sliced->service_lines[0][i],
 357                                sliced->service_lines[1][i]);
 358                break;
 359        case V4L2_BUF_TYPE_SDR_CAPTURE:
 360        case V4L2_BUF_TYPE_SDR_OUTPUT:
 361                sdr = &p->fmt.sdr;
 362                pr_cont(", pixelformat=%c%c%c%c\n",
 363                        (sdr->pixelformat >>  0) & 0xff,
 364                        (sdr->pixelformat >>  8) & 0xff,
 365                        (sdr->pixelformat >> 16) & 0xff,
 366                        (sdr->pixelformat >> 24) & 0xff);
 367                break;
 368        case V4L2_BUF_TYPE_META_CAPTURE:
 369        case V4L2_BUF_TYPE_META_OUTPUT:
 370                meta = &p->fmt.meta;
 371                pr_cont(", dataformat=%c%c%c%c, buffersize=%u\n",
 372                        (meta->dataformat >>  0) & 0xff,
 373                        (meta->dataformat >>  8) & 0xff,
 374                        (meta->dataformat >> 16) & 0xff,
 375                        (meta->dataformat >> 24) & 0xff,
 376                        meta->buffersize);
 377                break;
 378        }
 379}
 380
 381static void v4l_print_framebuffer(const void *arg, bool write_only)
 382{
 383        const struct v4l2_framebuffer *p = arg;
 384
 385        pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%c%c%c%c, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
 386                        p->capability, p->flags, p->base,
 387                        p->fmt.width, p->fmt.height,
 388                        (p->fmt.pixelformat & 0xff),
 389                        (p->fmt.pixelformat >>  8) & 0xff,
 390                        (p->fmt.pixelformat >> 16) & 0xff,
 391                        (p->fmt.pixelformat >> 24) & 0xff,
 392                        p->fmt.bytesperline, p->fmt.sizeimage,
 393                        p->fmt.colorspace);
 394}
 395
 396static void v4l_print_buftype(const void *arg, bool write_only)
 397{
 398        pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
 399}
 400
 401static void v4l_print_modulator(const void *arg, bool write_only)
 402{
 403        const struct v4l2_modulator *p = arg;
 404
 405        if (write_only)
 406                pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
 407        else
 408                pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
 409                        p->index, (int)sizeof(p->name), p->name, p->capability,
 410                        p->rangelow, p->rangehigh, p->txsubchans);
 411}
 412
 413static void v4l_print_tuner(const void *arg, bool write_only)
 414{
 415        const struct v4l2_tuner *p = arg;
 416
 417        if (write_only)
 418                pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
 419        else
 420                pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, rangelow=%u, rangehigh=%u, signal=%u, afc=%d, rxsubchans=0x%x, audmode=%u\n",
 421                        p->index, (int)sizeof(p->name), p->name, p->type,
 422                        p->capability, p->rangelow,
 423                        p->rangehigh, p->signal, p->afc,
 424                        p->rxsubchans, p->audmode);
 425}
 426
 427static void v4l_print_frequency(const void *arg, bool write_only)
 428{
 429        const struct v4l2_frequency *p = arg;
 430
 431        pr_cont("tuner=%u, type=%u, frequency=%u\n",
 432                                p->tuner, p->type, p->frequency);
 433}
 434
 435static void v4l_print_standard(const void *arg, bool write_only)
 436{
 437        const struct v4l2_standard *p = arg;
 438
 439        pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
 440                p->index,
 441                (unsigned long long)p->id, (int)sizeof(p->name), p->name,
 442                p->frameperiod.numerator,
 443                p->frameperiod.denominator,
 444                p->framelines);
 445}
 446
 447static void v4l_print_std(const void *arg, bool write_only)
 448{
 449        pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
 450}
 451
 452static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
 453{
 454        const struct v4l2_hw_freq_seek *p = arg;
 455
 456        pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
 457                p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
 458                p->rangelow, p->rangehigh);
 459}
 460
 461static void v4l_print_requestbuffers(const void *arg, bool write_only)
 462{
 463        const struct v4l2_requestbuffers *p = arg;
 464
 465        pr_cont("count=%d, type=%s, memory=%s\n",
 466                p->count,
 467                prt_names(p->type, v4l2_type_names),
 468                prt_names(p->memory, v4l2_memory_names));
 469}
 470
 471static void v4l_print_buffer(const void *arg, bool write_only)
 472{
 473        const struct v4l2_buffer *p = arg;
 474        const struct v4l2_timecode *tc = &p->timecode;
 475        const struct v4l2_plane *plane;
 476        int i;
 477
 478        pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
 479                        p->timestamp.tv_sec / 3600,
 480                        (int)(p->timestamp.tv_sec / 60) % 60,
 481                        (int)(p->timestamp.tv_sec % 60),
 482                        (long)p->timestamp.tv_usec,
 483                        p->index,
 484                        prt_names(p->type, v4l2_type_names), p->request_fd,
 485                        p->flags, prt_names(p->field, v4l2_field_names),
 486                        p->sequence, prt_names(p->memory, v4l2_memory_names));
 487
 488        if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
 489                pr_cont("\n");
 490                for (i = 0; i < p->length; ++i) {
 491                        plane = &p->m.planes[i];
 492                        printk(KERN_DEBUG
 493                                "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
 494                                i, plane->bytesused, plane->data_offset,
 495                                plane->m.userptr, plane->length);
 496                }
 497        } else {
 498                pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
 499                        p->bytesused, p->m.userptr, p->length);
 500        }
 501
 502        printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
 503                        tc->hours, tc->minutes, tc->seconds,
 504                        tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
 505}
 506
 507static void v4l_print_exportbuffer(const void *arg, bool write_only)
 508{
 509        const struct v4l2_exportbuffer *p = arg;
 510
 511        pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
 512                p->fd, prt_names(p->type, v4l2_type_names),
 513                p->index, p->plane, p->flags);
 514}
 515
 516static void v4l_print_create_buffers(const void *arg, bool write_only)
 517{
 518        const struct v4l2_create_buffers *p = arg;
 519
 520        pr_cont("index=%d, count=%d, memory=%s, ",
 521                        p->index, p->count,
 522                        prt_names(p->memory, v4l2_memory_names));
 523        v4l_print_format(&p->format, write_only);
 524}
 525
 526static void v4l_print_streamparm(const void *arg, bool write_only)
 527{
 528        const struct v4l2_streamparm *p = arg;
 529
 530        pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
 531
 532        if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
 533            p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 534                const struct v4l2_captureparm *c = &p->parm.capture;
 535
 536                pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
 537                        c->capability, c->capturemode,
 538                        c->timeperframe.numerator, c->timeperframe.denominator,
 539                        c->extendedmode, c->readbuffers);
 540        } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
 541                   p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 542                const struct v4l2_outputparm *c = &p->parm.output;
 543
 544                pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
 545                        c->capability, c->outputmode,
 546                        c->timeperframe.numerator, c->timeperframe.denominator,
 547                        c->extendedmode, c->writebuffers);
 548        } else {
 549                pr_cont("\n");
 550        }
 551}
 552
 553static void v4l_print_queryctrl(const void *arg, bool write_only)
 554{
 555        const struct v4l2_queryctrl *p = arg;
 556
 557        pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
 558                        p->id, p->type, (int)sizeof(p->name), p->name,
 559                        p->minimum, p->maximum,
 560                        p->step, p->default_value, p->flags);
 561}
 562
 563static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
 564{
 565        const struct v4l2_query_ext_ctrl *p = arg;
 566
 567        pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, nr_of_dims=%u, dims=%u,%u,%u,%u\n",
 568                        p->id, p->type, (int)sizeof(p->name), p->name,
 569                        p->minimum, p->maximum,
 570                        p->step, p->default_value, p->flags,
 571                        p->elem_size, p->elems, p->nr_of_dims,
 572                        p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
 573}
 574
 575static void v4l_print_querymenu(const void *arg, bool write_only)
 576{
 577        const struct v4l2_querymenu *p = arg;
 578
 579        pr_cont("id=0x%x, index=%d\n", p->id, p->index);
 580}
 581
 582static void v4l_print_control(const void *arg, bool write_only)
 583{
 584        const struct v4l2_control *p = arg;
 585
 586        pr_cont("id=0x%x, value=%d\n", p->id, p->value);
 587}
 588
 589static void v4l_print_ext_controls(const void *arg, bool write_only)
 590{
 591        const struct v4l2_ext_controls *p = arg;
 592        int i;
 593
 594        pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
 595                        p->which, p->count, p->error_idx, p->request_fd);
 596        for (i = 0; i < p->count; i++) {
 597                if (!p->controls[i].size)
 598                        pr_cont(", id/val=0x%x/0x%x",
 599                                p->controls[i].id, p->controls[i].value);
 600                else
 601                        pr_cont(", id/size=0x%x/%u",
 602                                p->controls[i].id, p->controls[i].size);
 603        }
 604        pr_cont("\n");
 605}
 606
 607static void v4l_print_cropcap(const void *arg, bool write_only)
 608{
 609        const struct v4l2_cropcap *p = arg;
 610
 611        pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
 612                prt_names(p->type, v4l2_type_names),
 613                p->bounds.width, p->bounds.height,
 614                p->bounds.left, p->bounds.top,
 615                p->defrect.width, p->defrect.height,
 616                p->defrect.left, p->defrect.top,
 617                p->pixelaspect.numerator, p->pixelaspect.denominator);
 618}
 619
 620static void v4l_print_crop(const void *arg, bool write_only)
 621{
 622        const struct v4l2_crop *p = arg;
 623
 624        pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
 625                prt_names(p->type, v4l2_type_names),
 626                p->c.width, p->c.height,
 627                p->c.left, p->c.top);
 628}
 629
 630static void v4l_print_selection(const void *arg, bool write_only)
 631{
 632        const struct v4l2_selection *p = arg;
 633
 634        pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
 635                prt_names(p->type, v4l2_type_names),
 636                p->target, p->flags,
 637                p->r.width, p->r.height, p->r.left, p->r.top);
 638}
 639
 640static void v4l_print_jpegcompression(const void *arg, bool write_only)
 641{
 642        const struct v4l2_jpegcompression *p = arg;
 643
 644        pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
 645                p->quality, p->APPn, p->APP_len,
 646                p->COM_len, p->jpeg_markers);
 647}
 648
 649static void v4l_print_enc_idx(const void *arg, bool write_only)
 650{
 651        const struct v4l2_enc_idx *p = arg;
 652
 653        pr_cont("entries=%d, entries_cap=%d\n",
 654                        p->entries, p->entries_cap);
 655}
 656
 657static void v4l_print_encoder_cmd(const void *arg, bool write_only)
 658{
 659        const struct v4l2_encoder_cmd *p = arg;
 660
 661        pr_cont("cmd=%d, flags=0x%x\n",
 662                        p->cmd, p->flags);
 663}
 664
 665static void v4l_print_decoder_cmd(const void *arg, bool write_only)
 666{
 667        const struct v4l2_decoder_cmd *p = arg;
 668
 669        pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
 670
 671        if (p->cmd == V4L2_DEC_CMD_START)
 672                pr_info("speed=%d, format=%u\n",
 673                                p->start.speed, p->start.format);
 674        else if (p->cmd == V4L2_DEC_CMD_STOP)
 675                pr_info("pts=%llu\n", p->stop.pts);
 676}
 677
 678static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
 679{
 680        const struct v4l2_dbg_chip_info *p = arg;
 681
 682        pr_cont("type=%u, ", p->match.type);
 683        if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
 684                pr_cont("name=%.*s, ",
 685                                (int)sizeof(p->match.name), p->match.name);
 686        else
 687                pr_cont("addr=%u, ", p->match.addr);
 688        pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
 689}
 690
 691static void v4l_print_dbg_register(const void *arg, bool write_only)
 692{
 693        const struct v4l2_dbg_register *p = arg;
 694
 695        pr_cont("type=%u, ", p->match.type);
 696        if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
 697                pr_cont("name=%.*s, ",
 698                                (int)sizeof(p->match.name), p->match.name);
 699        else
 700                pr_cont("addr=%u, ", p->match.addr);
 701        pr_cont("reg=0x%llx, val=0x%llx\n",
 702                        p->reg, p->val);
 703}
 704
 705static void v4l_print_dv_timings(const void *arg, bool write_only)
 706{
 707        const struct v4l2_dv_timings *p = arg;
 708
 709        switch (p->type) {
 710        case V4L2_DV_BT_656_1120:
 711                pr_cont("type=bt-656/1120, interlaced=%u, pixelclock=%llu, width=%u, height=%u, polarities=0x%x, hfrontporch=%u, hsync=%u, hbackporch=%u, vfrontporch=%u, vsync=%u, vbackporch=%u, il_vfrontporch=%u, il_vsync=%u, il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
 712                                p->bt.interlaced, p->bt.pixelclock,
 713                                p->bt.width, p->bt.height,
 714                                p->bt.polarities, p->bt.hfrontporch,
 715                                p->bt.hsync, p->bt.hbackporch,
 716                                p->bt.vfrontporch, p->bt.vsync,
 717                                p->bt.vbackporch, p->bt.il_vfrontporch,
 718                                p->bt.il_vsync, p->bt.il_vbackporch,
 719                                p->bt.standards, p->bt.flags);
 720                break;
 721        default:
 722                pr_cont("type=%d\n", p->type);
 723                break;
 724        }
 725}
 726
 727static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
 728{
 729        const struct v4l2_enum_dv_timings *p = arg;
 730
 731        pr_cont("index=%u, ", p->index);
 732        v4l_print_dv_timings(&p->timings, write_only);
 733}
 734
 735static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
 736{
 737        const struct v4l2_dv_timings_cap *p = arg;
 738
 739        switch (p->type) {
 740        case V4L2_DV_BT_656_1120:
 741                pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
 742                        p->bt.min_width, p->bt.max_width,
 743                        p->bt.min_height, p->bt.max_height,
 744                        p->bt.min_pixelclock, p->bt.max_pixelclock,
 745                        p->bt.standards, p->bt.capabilities);
 746                break;
 747        default:
 748                pr_cont("type=%u\n", p->type);
 749                break;
 750        }
 751}
 752
 753static void v4l_print_frmsizeenum(const void *arg, bool write_only)
 754{
 755        const struct v4l2_frmsizeenum *p = arg;
 756
 757        pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
 758                        p->index,
 759                        (p->pixel_format & 0xff),
 760                        (p->pixel_format >>  8) & 0xff,
 761                        (p->pixel_format >> 16) & 0xff,
 762                        (p->pixel_format >> 24) & 0xff,
 763                        p->type);
 764        switch (p->type) {
 765        case V4L2_FRMSIZE_TYPE_DISCRETE:
 766                pr_cont(", wxh=%ux%u\n",
 767                        p->discrete.width, p->discrete.height);
 768                break;
 769        case V4L2_FRMSIZE_TYPE_STEPWISE:
 770                pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
 771                                p->stepwise.min_width,
 772                                p->stepwise.min_height,
 773                                p->stepwise.max_width,
 774                                p->stepwise.max_height,
 775                                p->stepwise.step_width,
 776                                p->stepwise.step_height);
 777                break;
 778        case V4L2_FRMSIZE_TYPE_CONTINUOUS:
 779                /* fall through */
 780        default:
 781                pr_cont("\n");
 782                break;
 783        }
 784}
 785
 786static void v4l_print_frmivalenum(const void *arg, bool write_only)
 787{
 788        const struct v4l2_frmivalenum *p = arg;
 789
 790        pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
 791                        p->index,
 792                        (p->pixel_format & 0xff),
 793                        (p->pixel_format >>  8) & 0xff,
 794                        (p->pixel_format >> 16) & 0xff,
 795                        (p->pixel_format >> 24) & 0xff,
 796                        p->width, p->height, p->type);
 797        switch (p->type) {
 798        case V4L2_FRMIVAL_TYPE_DISCRETE:
 799                pr_cont(", fps=%d/%d\n",
 800                                p->discrete.numerator,
 801                                p->discrete.denominator);
 802                break;
 803        case V4L2_FRMIVAL_TYPE_STEPWISE:
 804                pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
 805                                p->stepwise.min.numerator,
 806                                p->stepwise.min.denominator,
 807                                p->stepwise.max.numerator,
 808                                p->stepwise.max.denominator,
 809                                p->stepwise.step.numerator,
 810                                p->stepwise.step.denominator);
 811                break;
 812        case V4L2_FRMIVAL_TYPE_CONTINUOUS:
 813                /* fall through */
 814        default:
 815                pr_cont("\n");
 816                break;
 817        }
 818}
 819
 820static void v4l_print_event(const void *arg, bool write_only)
 821{
 822        const struct v4l2_event *p = arg;
 823        const struct v4l2_event_ctrl *c;
 824
 825        pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%lu.%9.9lu\n",
 826                        p->type, p->pending, p->sequence, p->id,
 827                        p->timestamp.tv_sec, p->timestamp.tv_nsec);
 828        switch (p->type) {
 829        case V4L2_EVENT_VSYNC:
 830                printk(KERN_DEBUG "field=%s\n",
 831                        prt_names(p->u.vsync.field, v4l2_field_names));
 832                break;
 833        case V4L2_EVENT_CTRL:
 834                c = &p->u.ctrl;
 835                printk(KERN_DEBUG "changes=0x%x, type=%u, ",
 836                        c->changes, c->type);
 837                if (c->type == V4L2_CTRL_TYPE_INTEGER64)
 838                        pr_cont("value64=%lld, ", c->value64);
 839                else
 840                        pr_cont("value=%d, ", c->value);
 841                pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
 842                        c->flags, c->minimum, c->maximum,
 843                        c->step, c->default_value);
 844                break;
 845        case V4L2_EVENT_FRAME_SYNC:
 846                pr_cont("frame_sequence=%u\n",
 847                        p->u.frame_sync.frame_sequence);
 848                break;
 849        }
 850}
 851
 852static void v4l_print_event_subscription(const void *arg, bool write_only)
 853{
 854        const struct v4l2_event_subscription *p = arg;
 855
 856        pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
 857                        p->type, p->id, p->flags);
 858}
 859
 860static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
 861{
 862        const struct v4l2_sliced_vbi_cap *p = arg;
 863        int i;
 864
 865        pr_cont("type=%s, service_set=0x%08x\n",
 866                        prt_names(p->type, v4l2_type_names), p->service_set);
 867        for (i = 0; i < 24; i++)
 868                printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
 869                                p->service_lines[0][i],
 870                                p->service_lines[1][i]);
 871}
 872
 873static void v4l_print_freq_band(const void *arg, bool write_only)
 874{
 875        const struct v4l2_frequency_band *p = arg;
 876
 877        pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
 878                        p->tuner, p->type, p->index,
 879                        p->capability, p->rangelow,
 880                        p->rangehigh, p->modulation);
 881}
 882
 883static void v4l_print_edid(const void *arg, bool write_only)
 884{
 885        const struct v4l2_edid *p = arg;
 886
 887        pr_cont("pad=%u, start_block=%u, blocks=%u\n",
 888                p->pad, p->start_block, p->blocks);
 889}
 890
 891static void v4l_print_u32(const void *arg, bool write_only)
 892{
 893        pr_cont("value=%u\n", *(const u32 *)arg);
 894}
 895
 896static void v4l_print_newline(const void *arg, bool write_only)
 897{
 898        pr_cont("\n");
 899}
 900
 901static void v4l_print_default(const void *arg, bool write_only)
 902{
 903        pr_cont("driver-specific ioctl\n");
 904}
 905
 906static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
 907{
 908        __u32 i;
 909
 910        /* zero the reserved fields */
 911        c->reserved[0] = 0;
 912        for (i = 0; i < c->count; i++)
 913                c->controls[i].reserved2[0] = 0;
 914
 915        /* V4L2_CID_PRIVATE_BASE cannot be used as control class
 916           when using extended controls.
 917           Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
 918           is it allowed for backwards compatibility.
 919         */
 920        if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE)
 921                return 0;
 922        if (!c->which)
 923                return 1;
 924        /* Check that all controls are from the same control class. */
 925        for (i = 0; i < c->count; i++) {
 926                if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
 927                        c->error_idx = i;
 928                        return 0;
 929                }
 930        }
 931        return 1;
 932}
 933
 934static int check_fmt(struct file *file, enum v4l2_buf_type type)
 935{
 936        struct video_device *vfd = video_devdata(file);
 937        const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
 938        bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
 939        bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
 940        bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
 941        bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
 942        bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
 943        bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
 944
 945        if (ops == NULL)
 946                return -EINVAL;
 947
 948        switch (type) {
 949        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 950                if ((is_vid || is_tch) && is_rx &&
 951                    (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
 952                        return 0;
 953                break;
 954        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 955                if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
 956                        return 0;
 957                break;
 958        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 959                if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
 960                        return 0;
 961                break;
 962        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 963                if (is_vid && is_tx &&
 964                    (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
 965                        return 0;
 966                break;
 967        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 968                if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
 969                        return 0;
 970                break;
 971        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 972                if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
 973                        return 0;
 974                break;
 975        case V4L2_BUF_TYPE_VBI_CAPTURE:
 976                if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
 977                        return 0;
 978                break;
 979        case V4L2_BUF_TYPE_VBI_OUTPUT:
 980                if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
 981                        return 0;
 982                break;
 983        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 984                if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
 985                        return 0;
 986                break;
 987        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 988                if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
 989                        return 0;
 990                break;
 991        case V4L2_BUF_TYPE_SDR_CAPTURE:
 992                if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
 993                        return 0;
 994                break;
 995        case V4L2_BUF_TYPE_SDR_OUTPUT:
 996                if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
 997                        return 0;
 998                break;
 999        case V4L2_BUF_TYPE_META_CAPTURE:
1000                if (is_vid && is_rx && ops->vidioc_g_fmt_meta_cap)
1001                        return 0;
1002                break;
1003        case V4L2_BUF_TYPE_META_OUTPUT:
1004                if (is_vid && is_tx && ops->vidioc_g_fmt_meta_out)
1005                        return 0;
1006                break;
1007        default:
1008                break;
1009        }
1010        return -EINVAL;
1011}
1012
1013static void v4l_sanitize_format(struct v4l2_format *fmt)
1014{
1015        unsigned int offset;
1016
1017        /* Make sure num_planes is not bogus */
1018        if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1019            fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1020                fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
1021                                               VIDEO_MAX_PLANES);
1022
1023        /*
1024         * The v4l2_pix_format structure has been extended with fields that were
1025         * not previously required to be set to zero by applications. The priv
1026         * field, when set to a magic value, indicates the the extended fields
1027         * are valid. Otherwise they will contain undefined values. To simplify
1028         * the API towards drivers zero the extended fields and set the priv
1029         * field to the magic value when the extended pixel format structure
1030         * isn't used by applications.
1031         */
1032
1033        if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1034            fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1035                return;
1036
1037        if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
1038                return;
1039
1040        fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1041
1042        offset = offsetof(struct v4l2_pix_format, priv)
1043               + sizeof(fmt->fmt.pix.priv);
1044        memset(((void *)&fmt->fmt.pix) + offset, 0,
1045               sizeof(fmt->fmt.pix) - offset);
1046}
1047
1048static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1049                                struct file *file, void *fh, void *arg)
1050{
1051        struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1052        struct video_device *vfd = video_devdata(file);
1053        int ret;
1054
1055        cap->version = LINUX_VERSION_CODE;
1056        cap->device_caps = vfd->device_caps;
1057        cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1058
1059        ret = ops->vidioc_querycap(file, fh, cap);
1060
1061        /*
1062         * Drivers must not change device_caps, so check for this and
1063         * warn if this happened.
1064         */
1065        WARN_ON(cap->device_caps != vfd->device_caps);
1066        /*
1067         * Check that capabilities is a superset of
1068         * vfd->device_caps | V4L2_CAP_DEVICE_CAPS
1069         */
1070        WARN_ON((cap->capabilities &
1071                 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS)) !=
1072                (vfd->device_caps | V4L2_CAP_DEVICE_CAPS));
1073        cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1074        cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1075
1076        return ret;
1077}
1078
1079static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1080                                struct file *file, void *fh, void *arg)
1081{
1082        struct video_device *vfd = video_devdata(file);
1083        int ret;
1084
1085        ret = v4l_enable_media_source(vfd);
1086        if (ret)
1087                return ret;
1088        return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1089}
1090
1091static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1092                                struct file *file, void *fh, void *arg)
1093{
1094        return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1095}
1096
1097static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1098                                struct file *file, void *fh, void *arg)
1099{
1100        struct video_device *vfd;
1101        u32 *p = arg;
1102
1103        vfd = video_devdata(file);
1104        *p = v4l2_prio_max(vfd->prio);
1105        return 0;
1106}
1107
1108static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1109                                struct file *file, void *fh, void *arg)
1110{
1111        struct video_device *vfd;
1112        struct v4l2_fh *vfh;
1113        u32 *p = arg;
1114
1115        vfd = video_devdata(file);
1116        if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1117                return -ENOTTY;
1118        vfh = file->private_data;
1119        return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1120}
1121
1122static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1123                                struct file *file, void *fh, void *arg)
1124{
1125        struct video_device *vfd = video_devdata(file);
1126        struct v4l2_input *p = arg;
1127
1128        /*
1129         * We set the flags for CAP_DV_TIMINGS &
1130         * CAP_STD here based on ioctl handler provided by the
1131         * driver. If the driver doesn't support these
1132         * for a specific input, it must override these flags.
1133         */
1134        if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1135                p->capabilities |= V4L2_IN_CAP_STD;
1136
1137        return ops->vidioc_enum_input(file, fh, p);
1138}
1139
1140static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1141                                struct file *file, void *fh, void *arg)
1142{
1143        struct video_device *vfd = video_devdata(file);
1144        struct v4l2_output *p = arg;
1145
1146        /*
1147         * We set the flags for CAP_DV_TIMINGS &
1148         * CAP_STD here based on ioctl handler provided by the
1149         * driver. If the driver doesn't support these
1150         * for a specific output, it must override these flags.
1151         */
1152        if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1153                p->capabilities |= V4L2_OUT_CAP_STD;
1154
1155        return ops->vidioc_enum_output(file, fh, p);
1156}
1157
1158static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1159{
1160        const unsigned sz = sizeof(fmt->description);
1161        const char *descr = NULL;
1162        u32 flags = 0;
1163
1164        /*
1165         * We depart from the normal coding style here since the descriptions
1166         * should be aligned so it is easy to see which descriptions will be
1167         * longer than 31 characters (the max length for a description).
1168         * And frankly, this is easier to read anyway.
1169         *
1170         * Note that gcc will use O(log N) comparisons to find the right case.
1171         */
1172        switch (fmt->pixelformat) {
1173        /* Max description length mask: descr = "0123456789012345678901234567890" */
1174        case V4L2_PIX_FMT_RGB332:       descr = "8-bit RGB 3-3-2"; break;
1175        case V4L2_PIX_FMT_RGB444:       descr = "16-bit A/XRGB 4-4-4-4"; break;
1176        case V4L2_PIX_FMT_ARGB444:      descr = "16-bit ARGB 4-4-4-4"; break;
1177        case V4L2_PIX_FMT_XRGB444:      descr = "16-bit XRGB 4-4-4-4"; break;
1178        case V4L2_PIX_FMT_RGBA444:      descr = "16-bit RGBA 4-4-4-4"; break;
1179        case V4L2_PIX_FMT_RGBX444:      descr = "16-bit RGBX 4-4-4-4"; break;
1180        case V4L2_PIX_FMT_ABGR444:      descr = "16-bit ABGR 4-4-4-4"; break;
1181        case V4L2_PIX_FMT_XBGR444:      descr = "16-bit XBGR 4-4-4-4"; break;
1182        case V4L2_PIX_FMT_BGRA444:      descr = "16-bit BGRA 4-4-4-4"; break;
1183        case V4L2_PIX_FMT_BGRX444:      descr = "16-bit BGRX 4-4-4-4"; break;
1184        case V4L2_PIX_FMT_RGB555:       descr = "16-bit A/XRGB 1-5-5-5"; break;
1185        case V4L2_PIX_FMT_ARGB555:      descr = "16-bit ARGB 1-5-5-5"; break;
1186        case V4L2_PIX_FMT_XRGB555:      descr = "16-bit XRGB 1-5-5-5"; break;
1187        case V4L2_PIX_FMT_ABGR555:      descr = "16-bit ABGR 1-5-5-5"; break;
1188        case V4L2_PIX_FMT_XBGR555:      descr = "16-bit XBGR 1-5-5-5"; break;
1189        case V4L2_PIX_FMT_RGBA555:      descr = "16-bit RGBA 5-5-5-1"; break;
1190        case V4L2_PIX_FMT_RGBX555:      descr = "16-bit RGBX 5-5-5-1"; break;
1191        case V4L2_PIX_FMT_BGRA555:      descr = "16-bit BGRA 5-5-5-1"; break;
1192        case V4L2_PIX_FMT_BGRX555:      descr = "16-bit BGRX 5-5-5-1"; break;
1193        case V4L2_PIX_FMT_RGB565:       descr = "16-bit RGB 5-6-5"; break;
1194        case V4L2_PIX_FMT_RGB555X:      descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1195        case V4L2_PIX_FMT_ARGB555X:     descr = "16-bit ARGB 1-5-5-5 BE"; break;
1196        case V4L2_PIX_FMT_XRGB555X:     descr = "16-bit XRGB 1-5-5-5 BE"; break;
1197        case V4L2_PIX_FMT_RGB565X:      descr = "16-bit RGB 5-6-5 BE"; break;
1198        case V4L2_PIX_FMT_BGR666:       descr = "18-bit BGRX 6-6-6-14"; break;
1199        case V4L2_PIX_FMT_BGR24:        descr = "24-bit BGR 8-8-8"; break;
1200        case V4L2_PIX_FMT_RGB24:        descr = "24-bit RGB 8-8-8"; break;
1201        case V4L2_PIX_FMT_BGR32:        descr = "32-bit BGRA/X 8-8-8-8"; break;
1202        case V4L2_PIX_FMT_ABGR32:       descr = "32-bit BGRA 8-8-8-8"; break;
1203        case V4L2_PIX_FMT_XBGR32:       descr = "32-bit BGRX 8-8-8-8"; break;
1204        case V4L2_PIX_FMT_RGB32:        descr = "32-bit A/XRGB 8-8-8-8"; break;
1205        case V4L2_PIX_FMT_ARGB32:       descr = "32-bit ARGB 8-8-8-8"; break;
1206        case V4L2_PIX_FMT_XRGB32:       descr = "32-bit XRGB 8-8-8-8"; break;
1207        case V4L2_PIX_FMT_BGRA32:       descr = "32-bit ABGR 8-8-8-8"; break;
1208        case V4L2_PIX_FMT_BGRX32:       descr = "32-bit XBGR 8-8-8-8"; break;
1209        case V4L2_PIX_FMT_RGBA32:       descr = "32-bit RGBA 8-8-8-8"; break;
1210        case V4L2_PIX_FMT_RGBX32:       descr = "32-bit RGBX 8-8-8-8"; break;
1211        case V4L2_PIX_FMT_XBGR30:       descr = "32-bit XBGR 2-10-10-10"; break;
1212        case V4L2_PIX_FMT_XBGR40:       descr = "40-bit XBGR 4-12-12-12"; break;
1213        case V4L2_PIX_FMT_BGR48:        descr = "48-bit BGR 16-16-16"; break;
1214        case V4L2_PIX_FMT_GREY:         descr = "8-bit Greyscale"; break;
1215        case V4L2_PIX_FMT_Y4:           descr = "4-bit Greyscale"; break;
1216        case V4L2_PIX_FMT_Y6:           descr = "6-bit Greyscale"; break;
1217        case V4L2_PIX_FMT_Y10:          descr = "10-bit Greyscale"; break;
1218        case V4L2_PIX_FMT_Y12:          descr = "12-bit Greyscale"; break;
1219        case V4L2_PIX_FMT_Y16:          descr = "16-bit Greyscale"; break;
1220        case V4L2_PIX_FMT_Y16_BE:       descr = "16-bit Greyscale BE"; break;
1221        case V4L2_PIX_FMT_Y10BPACK:     descr = "10-bit Greyscale (Packed)"; break;
1222        case V4L2_PIX_FMT_Y10P:         descr = "10-bit Greyscale (MIPI Packed)"; break;
1223        case V4L2_PIX_FMT_Y8I:          descr = "Interleaved 8-bit Greyscale"; break;
1224        case V4L2_PIX_FMT_Y12I:         descr = "Interleaved 12-bit Greyscale"; break;
1225        case V4L2_PIX_FMT_Z16:          descr = "16-bit Depth"; break;
1226        case V4L2_PIX_FMT_INZI:         descr = "Planar 10:16 Greyscale Depth"; break;
1227        case V4L2_PIX_FMT_CNF4:         descr = "4-bit Depth Confidence (Packed)"; break;
1228        case V4L2_PIX_FMT_PAL8:         descr = "8-bit Palette"; break;
1229        case V4L2_PIX_FMT_UV8:          descr = "8-bit Chrominance UV 4-4"; break;
1230        case V4L2_PIX_FMT_YVU410:       descr = "Planar YVU 4:1:0"; break;
1231        case V4L2_PIX_FMT_YVU420:       descr = "Planar YVU 4:2:0"; break;
1232        case V4L2_PIX_FMT_YUYV:         descr = "YUYV 4:2:2"; break;
1233        case V4L2_PIX_FMT_YYUV:         descr = "YYUV 4:2:2"; break;
1234        case V4L2_PIX_FMT_YVYU:         descr = "YVYU 4:2:2"; break;
1235        case V4L2_PIX_FMT_UYVY:         descr = "UYVY 4:2:2"; break;
1236        case V4L2_PIX_FMT_VYUY:         descr = "VYUY 4:2:2"; break;
1237        case V4L2_PIX_FMT_YUV422P:      descr = "Planar YUV 4:2:2"; break;
1238        case V4L2_PIX_FMT_YUV411P:      descr = "Planar YUV 4:1:1"; break;
1239        case V4L2_PIX_FMT_Y41P:         descr = "YUV 4:1:1 (Packed)"; break;
1240        case V4L2_PIX_FMT_YUV444:       descr = "16-bit A/XYUV 4-4-4-4"; break;
1241        case V4L2_PIX_FMT_XVUY32:       descr = "32-bit packed XVUY 8-8-8-8"; break;
1242        case V4L2_PIX_FMT_AVUY32:       descr = "32-bit packed AVUY 8-8-8-8"; break;
1243        case V4L2_PIX_FMT_VUY24:        descr = "24-bit packed VUY 8-8-8"; break;
1244        case V4L2_PIX_FMT_YUV555:       descr = "16-bit A/XYUV 1-5-5-5"; break;
1245        case V4L2_PIX_FMT_YUV565:       descr = "16-bit YUV 5-6-5"; break;
1246        case V4L2_PIX_FMT_YUV32:        descr = "32-bit A/XYUV 8-8-8-8"; break;
1247        case V4L2_PIX_FMT_AYUV32:       descr = "32-bit AYUV 8-8-8-8"; break;
1248        case V4L2_PIX_FMT_XYUV32:       descr = "32-bit XYUV 8-8-8-8"; break;
1249        case V4L2_PIX_FMT_VUYA32:       descr = "32-bit VUYA 8-8-8-8"; break;
1250        case V4L2_PIX_FMT_VUYX32:       descr = "32-bit VUYX 8-8-8-8"; break;
1251        case V4L2_PIX_FMT_YUV410:       descr = "Planar YUV 4:1:0"; break;
1252        case V4L2_PIX_FMT_YUV420:       descr = "Planar YUV 4:2:0"; break;
1253        case V4L2_PIX_FMT_HI240:        descr = "8-bit Dithered RGB (BTTV)"; break;
1254        case V4L2_PIX_FMT_HM12:         descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
1255        case V4L2_PIX_FMT_M420:         descr = "YUV 4:2:0 (M420)"; break;
1256        case V4L2_PIX_FMT_XVUY10:       descr = "XVUY 2-10-10-10"; break;
1257        case V4L2_PIX_FMT_NV12:         descr = "Y/CbCr 4:2:0"; break;
1258        case V4L2_PIX_FMT_NV21:         descr = "Y/CrCb 4:2:0"; break;
1259        case V4L2_PIX_FMT_NV16:         descr = "Y/CbCr 4:2:2"; break;
1260        case V4L2_PIX_FMT_NV61:         descr = "Y/CrCb 4:2:2"; break;
1261        case V4L2_PIX_FMT_NV24:         descr = "Y/CbCr 4:4:4"; break;
1262        case V4L2_PIX_FMT_NV42:         descr = "Y/CrCb 4:4:4"; break;
1263        case V4L2_PIX_FMT_XV15:         descr = "Y/CrCb 4:2:0 10-bit"; break;
1264        case V4L2_PIX_FMT_XV20:         descr = "Y/CrCb 4:2:2 10-bit"; break;
1265        case V4L2_PIX_FMT_NV12M:        descr = "Y/CbCr 4:2:0 (N-C)"; break;
1266        case V4L2_PIX_FMT_NV21M:        descr = "Y/CrCb 4:2:0 (N-C)"; break;
1267        case V4L2_PIX_FMT_NV16M:        descr = "Y/CbCr 4:2:2 (N-C)"; break;
1268        case V4L2_PIX_FMT_NV61M:        descr = "Y/CrCb 4:2:2 (N-C)"; break;
1269        case V4L2_PIX_FMT_NV12MT:       descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
1270        case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
1271        case V4L2_PIX_FMT_XV20M:        descr = "Y/CrCb 4:2:2 10-bit (N-C)"; break;
1272        case V4L2_PIX_FMT_XV15M:        descr = "Y/CrCb 4:2:0 10-bit (N-C)"; break;
1273        case V4L2_PIX_FMT_YUV420M:      descr = "Planar YUV 4:2:0 (N-C)"; break;
1274        case V4L2_PIX_FMT_YVU420M:      descr = "Planar YVU 4:2:0 (N-C)"; break;
1275        case V4L2_PIX_FMT_YUV422M:      descr = "Planar YUV 4:2:2 (N-C)"; break;
1276        case V4L2_PIX_FMT_YVU422M:      descr = "Planar YVU 4:2:2 (N-C)"; break;
1277        case V4L2_PIX_FMT_YUV444M:      descr = "Planar YUV 4:4:4 (N-C)"; break;
1278        case V4L2_PIX_FMT_YVU444M:      descr = "Planar YVU 4:4:4 (N-C)"; break;
1279        case V4L2_PIX_FMT_X012:         descr = "Y/CbCr 4:2:0, 4-12-12-12"; break;
1280        case V4L2_PIX_FMT_X012M:        descr = "Y/CbCr 4:2:0, 4-12-12-12 (N-C)"; break;
1281        case V4L2_PIX_FMT_X212:         descr = "Y/CbCr 4:2:2, 4-12-12-12"; break;
1282        case V4L2_PIX_FMT_X212M:        descr = "Y/CbCr 4:2:2, 4-12-12-12 (N-C)"; break;
1283        case V4L2_PIX_FMT_X412:         descr = "Y/CbCr 4:4:4, 4-12-12-12"; break;
1284        case V4L2_PIX_FMT_X412M:        descr = "Y/CbCr 4:4:4, 4-12-12-12 (N-C)"; break;
1285        case V4L2_PIX_FMT_X016:         descr = "Y/CbCr 4:2:0, 16-16-16"; break;
1286        case V4L2_PIX_FMT_X016M:        descr = "Y/CbCr 4:2:0, 16-16-16 (N-C)"; break;
1287        case V4L2_PIX_FMT_X216:         descr = "Y/CbCr 4:2:2, 16-16-16"; break;
1288        case V4L2_PIX_FMT_X216M:        descr = "Y/CbCr 4:2:2, 16-16-16 (N-C)"; break;
1289        case V4L2_PIX_FMT_X416:         descr = "Y/CbCr 4:4:4, 16-16-16"; break;
1290        case V4L2_PIX_FMT_X416M:        descr = "Y/CbCr 4:4:4, 16-16-16 (N-C)"; break;
1291        case V4L2_PIX_FMT_SBGGR8:       descr = "8-bit Bayer BGBG/GRGR"; break;
1292        case V4L2_PIX_FMT_SGBRG8:       descr = "8-bit Bayer GBGB/RGRG"; break;
1293        case V4L2_PIX_FMT_SGRBG8:       descr = "8-bit Bayer GRGR/BGBG"; break;
1294        case V4L2_PIX_FMT_SRGGB8:       descr = "8-bit Bayer RGRG/GBGB"; break;
1295        case V4L2_PIX_FMT_SBGGR10:      descr = "10-bit Bayer BGBG/GRGR"; break;
1296        case V4L2_PIX_FMT_SGBRG10:      descr = "10-bit Bayer GBGB/RGRG"; break;
1297        case V4L2_PIX_FMT_SGRBG10:      descr = "10-bit Bayer GRGR/BGBG"; break;
1298        case V4L2_PIX_FMT_SRGGB10:      descr = "10-bit Bayer RGRG/GBGB"; break;
1299        case V4L2_PIX_FMT_SBGGR10P:     descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1300        case V4L2_PIX_FMT_SGBRG10P:     descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1301        case V4L2_PIX_FMT_SGRBG10P:     descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1302        case V4L2_PIX_FMT_SRGGB10P:     descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1303        case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1304        case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1305        case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1306        case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1307        case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1308        case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1309        case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1310        case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1311        case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1312        case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1313        case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1314        case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1315        case V4L2_PIX_FMT_SBGGR12:      descr = "12-bit Bayer BGBG/GRGR"; break;
1316        case V4L2_PIX_FMT_SGBRG12:      descr = "12-bit Bayer GBGB/RGRG"; break;
1317        case V4L2_PIX_FMT_SGRBG12:      descr = "12-bit Bayer GRGR/BGBG"; break;
1318        case V4L2_PIX_FMT_SRGGB12:      descr = "12-bit Bayer RGRG/GBGB"; break;
1319        case V4L2_PIX_FMT_SBGGR12P:     descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1320        case V4L2_PIX_FMT_SGBRG12P:     descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1321        case V4L2_PIX_FMT_SGRBG12P:     descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1322        case V4L2_PIX_FMT_SRGGB12P:     descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1323        case V4L2_PIX_FMT_SBGGR14P:     descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1324        case V4L2_PIX_FMT_SGBRG14P:     descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1325        case V4L2_PIX_FMT_SGRBG14P:     descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1326        case V4L2_PIX_FMT_SRGGB14P:     descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1327        case V4L2_PIX_FMT_SBGGR16:      descr = "16-bit Bayer BGBG/GRGR"; break;
1328        case V4L2_PIX_FMT_SGBRG16:      descr = "16-bit Bayer GBGB/RGRG"; break;
1329        case V4L2_PIX_FMT_SGRBG16:      descr = "16-bit Bayer GRGR/BGBG"; break;
1330        case V4L2_PIX_FMT_SRGGB16:      descr = "16-bit Bayer RGRG/GBGB"; break;
1331        case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
1332        case V4L2_PIX_FMT_SPCA501:      descr = "GSPCA SPCA501"; break;
1333        case V4L2_PIX_FMT_SPCA505:      descr = "GSPCA SPCA505"; break;
1334        case V4L2_PIX_FMT_SPCA508:      descr = "GSPCA SPCA508"; break;
1335        case V4L2_PIX_FMT_STV0680:      descr = "GSPCA STV0680"; break;
1336        case V4L2_PIX_FMT_TM6000:       descr = "A/V + VBI Mux Packet"; break;
1337        case V4L2_PIX_FMT_CIT_YYVYUY:   descr = "GSPCA CIT YYVYUY"; break;
1338        case V4L2_PIX_FMT_KONICA420:    descr = "GSPCA KONICA420"; break;
1339        case V4L2_PIX_FMT_HSV24:        descr = "24-bit HSV 8-8-8"; break;
1340        case V4L2_PIX_FMT_HSV32:        descr = "32-bit XHSV 8-8-8-8"; break;
1341        case V4L2_SDR_FMT_CU8:          descr = "Complex U8"; break;
1342        case V4L2_SDR_FMT_CU16LE:       descr = "Complex U16LE"; break;
1343        case V4L2_SDR_FMT_CS8:          descr = "Complex S8"; break;
1344        case V4L2_SDR_FMT_CS14LE:       descr = "Complex S14LE"; break;
1345        case V4L2_SDR_FMT_RU12LE:       descr = "Real U12LE"; break;
1346        case V4L2_SDR_FMT_PCU16BE:      descr = "Planar Complex U16BE"; break;
1347        case V4L2_SDR_FMT_PCU18BE:      descr = "Planar Complex U18BE"; break;
1348        case V4L2_SDR_FMT_PCU20BE:      descr = "Planar Complex U20BE"; break;
1349        case V4L2_TCH_FMT_DELTA_TD16:   descr = "16-bit Signed Deltas"; break;
1350        case V4L2_TCH_FMT_DELTA_TD08:   descr = "8-bit Signed Deltas"; break;
1351        case V4L2_TCH_FMT_TU16:         descr = "16-bit Unsigned Touch Data"; break;
1352        case V4L2_TCH_FMT_TU08:         descr = "8-bit Unsigned Touch Data"; break;
1353        case V4L2_META_FMT_VSP1_HGO:    descr = "R-Car VSP1 1-D Histogram"; break;
1354        case V4L2_META_FMT_VSP1_HGT:    descr = "R-Car VSP1 2-D Histogram"; break;
1355        case V4L2_META_FMT_UVC:         descr = "UVC Payload Header Metadata"; break;
1356        case V4L2_META_FMT_D4XX:        descr = "Intel D4xx UVC Metadata"; break;
1357
1358        default:
1359                /* Compressed formats */
1360                flags = V4L2_FMT_FLAG_COMPRESSED;
1361                switch (fmt->pixelformat) {
1362                /* Max description length mask: descr = "0123456789012345678901234567890" */
1363                case V4L2_PIX_FMT_MJPEG:        descr = "Motion-JPEG"; break;
1364                case V4L2_PIX_FMT_JPEG:         descr = "JFIF JPEG"; break;
1365                case V4L2_PIX_FMT_DV:           descr = "1394"; break;
1366                case V4L2_PIX_FMT_MPEG:         descr = "MPEG-1/2/4"; break;
1367                case V4L2_PIX_FMT_H264:         descr = "H.264"; break;
1368                case V4L2_PIX_FMT_H264_NO_SC:   descr = "H.264 (No Start Codes)"; break;
1369                case V4L2_PIX_FMT_H264_MVC:     descr = "H.264 MVC"; break;
1370                case V4L2_PIX_FMT_H264_SLICE:   descr = "H.264 Parsed Slice Data"; break;
1371                case V4L2_PIX_FMT_H263:         descr = "H.263"; break;
1372                case V4L2_PIX_FMT_MPEG1:        descr = "MPEG-1 ES"; break;
1373                case V4L2_PIX_FMT_MPEG2:        descr = "MPEG-2 ES"; break;
1374                case V4L2_PIX_FMT_MPEG2_SLICE:  descr = "MPEG-2 Parsed Slice Data"; break;
1375                case V4L2_PIX_FMT_MPEG4:        descr = "MPEG-4 Part 2 ES"; break;
1376                case V4L2_PIX_FMT_XVID:         descr = "Xvid"; break;
1377                case V4L2_PIX_FMT_VC1_ANNEX_G:  descr = "VC-1 (SMPTE 412M Annex G)"; break;
1378                case V4L2_PIX_FMT_VC1_ANNEX_L:  descr = "VC-1 (SMPTE 412M Annex L)"; break;
1379                case V4L2_PIX_FMT_VP8:          descr = "VP8"; break;
1380                case V4L2_PIX_FMT_VP8_FRAME:    descr = "VP8 Frame"; break;
1381                case V4L2_PIX_FMT_VP9:          descr = "VP9"; break;
1382                case V4L2_PIX_FMT_HEVC:         descr = "HEVC"; break; /* aka H.265 */
1383                case V4L2_PIX_FMT_FWHT:         descr = "FWHT"; break; /* used in vicodec */
1384                case V4L2_PIX_FMT_FWHT_STATELESS:       descr = "FWHT Stateless"; break; /* used in vicodec */
1385                case V4L2_PIX_FMT_CPIA1:        descr = "GSPCA CPiA YUV"; break;
1386                case V4L2_PIX_FMT_WNVA:         descr = "WNVA"; break;
1387                case V4L2_PIX_FMT_SN9C10X:      descr = "GSPCA SN9C10X"; break;
1388                case V4L2_PIX_FMT_PWC1:         descr = "Raw Philips Webcam Type (Old)"; break;
1389                case V4L2_PIX_FMT_PWC2:         descr = "Raw Philips Webcam Type (New)"; break;
1390                case V4L2_PIX_FMT_ET61X251:     descr = "GSPCA ET61X251"; break;
1391                case V4L2_PIX_FMT_SPCA561:      descr = "GSPCA SPCA561"; break;
1392                case V4L2_PIX_FMT_PAC207:       descr = "GSPCA PAC207"; break;
1393                case V4L2_PIX_FMT_MR97310A:     descr = "GSPCA MR97310A"; break;
1394                case V4L2_PIX_FMT_JL2005BCD:    descr = "GSPCA JL2005BCD"; break;
1395                case V4L2_PIX_FMT_SN9C2028:     descr = "GSPCA SN9C2028"; break;
1396                case V4L2_PIX_FMT_SQ905C:       descr = "GSPCA SQ905C"; break;
1397                case V4L2_PIX_FMT_PJPG:         descr = "GSPCA PJPG"; break;
1398                case V4L2_PIX_FMT_OV511:        descr = "GSPCA OV511"; break;
1399                case V4L2_PIX_FMT_OV518:        descr = "GSPCA OV518"; break;
1400                case V4L2_PIX_FMT_JPGL:         descr = "JPEG Lite"; break;
1401                case V4L2_PIX_FMT_SE401:        descr = "GSPCA SE401"; break;
1402                case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
1403                case V4L2_PIX_FMT_MT21C:        descr = "Mediatek Compressed Format"; break;
1404                case V4L2_PIX_FMT_SUNXI_TILED_NV12: descr = "Sunxi Tiled NV12 Format"; break;
1405                default:
1406                        if (fmt->description[0])
1407                                return;
1408                        WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1409                        flags = 0;
1410                        snprintf(fmt->description, sz, "%c%c%c%c%s",
1411                                        (char)(fmt->pixelformat & 0x7f),
1412                                        (char)((fmt->pixelformat >> 8) & 0x7f),
1413                                        (char)((fmt->pixelformat >> 16) & 0x7f),
1414                                        (char)((fmt->pixelformat >> 24) & 0x7f),
1415                                        (fmt->pixelformat & (1UL << 31)) ? "-BE" : "");
1416                        break;
1417                }
1418        }
1419
1420        if (descr)
1421                WARN_ON(strscpy(fmt->description, descr, sz) < 0);
1422        fmt->flags |= flags;
1423}
1424
1425static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1426                                struct file *file, void *fh, void *arg)
1427{
1428        struct video_device *vdev = video_devdata(file);
1429        struct v4l2_fmtdesc *p = arg;
1430        int ret = check_fmt(file, p->type);
1431        u32 cap_mask;
1432
1433        if (ret)
1434                return ret;
1435        ret = -EINVAL;
1436
1437        switch (p->type) {
1438        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1439        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1440                cap_mask = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1441                           V4L2_CAP_VIDEO_M2M_MPLANE;
1442                if (!!(vdev->device_caps & cap_mask) !=
1443                    (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
1444                        break;
1445
1446                if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1447                        break;
1448                ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1449                break;
1450        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1451                if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1452                        break;
1453                ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1454                break;
1455        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1456        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1457                cap_mask = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1458                           V4L2_CAP_VIDEO_M2M_MPLANE;
1459                if (!!(vdev->device_caps & cap_mask) !=
1460                    (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
1461                        break;
1462
1463                if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1464                        break;
1465                ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1466                break;
1467        case V4L2_BUF_TYPE_SDR_CAPTURE:
1468                if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1469                        break;
1470                ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1471                break;
1472        case V4L2_BUF_TYPE_SDR_OUTPUT:
1473                if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1474                        break;
1475                ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1476                break;
1477        case V4L2_BUF_TYPE_META_CAPTURE:
1478                if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1479                        break;
1480                ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1481                break;
1482        case V4L2_BUF_TYPE_META_OUTPUT:
1483                if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1484                        break;
1485                ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1486                break;
1487        }
1488        if (ret == 0)
1489                v4l_fill_fmtdesc(p);
1490        return ret;
1491}
1492
1493static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1494                                struct file *file, void *fh, void *arg)
1495{
1496        struct v4l2_format *p = arg;
1497        int ret = check_fmt(file, p->type);
1498
1499        if (ret)
1500                return ret;
1501
1502        /*
1503         * fmt can't be cleared for these overlay types due to the 'clips'
1504         * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1505         * Those are provided by the user. So handle these two overlay types
1506         * first, and then just do a simple memset for the other types.
1507         */
1508        switch (p->type) {
1509        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1510        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1511                struct v4l2_clip __user *clips = p->fmt.win.clips;
1512                u32 clipcount = p->fmt.win.clipcount;
1513                void __user *bitmap = p->fmt.win.bitmap;
1514
1515                memset(&p->fmt, 0, sizeof(p->fmt));
1516                p->fmt.win.clips = clips;
1517                p->fmt.win.clipcount = clipcount;
1518                p->fmt.win.bitmap = bitmap;
1519                break;
1520        }
1521        default:
1522                memset(&p->fmt, 0, sizeof(p->fmt));
1523                break;
1524        }
1525
1526        switch (p->type) {
1527        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1528                if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1529                        break;
1530                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1531                ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1532                /* just in case the driver zeroed it again */
1533                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1534                return ret;
1535        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1536                return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1537        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1538                return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1539        case V4L2_BUF_TYPE_VBI_CAPTURE:
1540                return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1541        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1542                return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1543        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1544                if (unlikely(!ops->vidioc_g_fmt_vid_out))
1545                        break;
1546                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1547                ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1548                /* just in case the driver zeroed it again */
1549                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1550                return ret;
1551        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1552                return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1553        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1554                return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1555        case V4L2_BUF_TYPE_VBI_OUTPUT:
1556                return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1557        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1558                return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1559        case V4L2_BUF_TYPE_SDR_CAPTURE:
1560                return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1561        case V4L2_BUF_TYPE_SDR_OUTPUT:
1562                return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1563        case V4L2_BUF_TYPE_META_CAPTURE:
1564                return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1565        case V4L2_BUF_TYPE_META_OUTPUT:
1566                return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1567        }
1568        return -EINVAL;
1569}
1570
1571static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1572{
1573        /*
1574         * The v4l2_pix_format structure contains fields that make no sense for
1575         * touch. Set them to default values in this case.
1576         */
1577
1578        p->field = V4L2_FIELD_NONE;
1579        p->colorspace = V4L2_COLORSPACE_RAW;
1580        p->flags = 0;
1581        p->ycbcr_enc = 0;
1582        p->quantization = 0;
1583        p->xfer_func = 0;
1584}
1585
1586static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1587                                struct file *file, void *fh, void *arg)
1588{
1589        struct v4l2_format *p = arg;
1590        struct video_device *vfd = video_devdata(file);
1591        int ret = check_fmt(file, p->type);
1592        unsigned int i;
1593
1594        if (ret)
1595                return ret;
1596
1597        ret = v4l_enable_media_source(vfd);
1598        if (ret)
1599                return ret;
1600        v4l_sanitize_format(p);
1601
1602        switch (p->type) {
1603        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1604                if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1605                        break;
1606                CLEAR_AFTER_FIELD(p, fmt.pix);
1607                ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1608                /* just in case the driver zeroed it again */
1609                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1610                if (vfd->vfl_type == VFL_TYPE_TOUCH)
1611                        v4l_pix_format_touch(&p->fmt.pix);
1612                return ret;
1613        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1614                if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1615                        break;
1616                CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1617                for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1618                        CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1619                                          bytesperline);
1620                return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1621        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1622                if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1623                        break;
1624                CLEAR_AFTER_FIELD(p, fmt.win);
1625                return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1626        case V4L2_BUF_TYPE_VBI_CAPTURE:
1627                if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1628                        break;
1629                CLEAR_AFTER_FIELD(p, fmt.vbi);
1630                return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1631        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1632                if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1633                        break;
1634                CLEAR_AFTER_FIELD(p, fmt.sliced);
1635                return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1636        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1637                if (unlikely(!ops->vidioc_s_fmt_vid_out))
1638                        break;
1639                CLEAR_AFTER_FIELD(p, fmt.pix);
1640                ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1641                /* just in case the driver zeroed it again */
1642                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1643                return ret;
1644        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1645                if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1646                        break;
1647                CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1648                for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1649                        CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1650                                          bytesperline);
1651                return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1652        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1653                if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1654                        break;
1655                CLEAR_AFTER_FIELD(p, fmt.win);
1656                return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1657        case V4L2_BUF_TYPE_VBI_OUTPUT:
1658                if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1659                        break;
1660                CLEAR_AFTER_FIELD(p, fmt.vbi);
1661                return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1662        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1663                if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1664                        break;
1665                CLEAR_AFTER_FIELD(p, fmt.sliced);
1666                return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1667        case V4L2_BUF_TYPE_SDR_CAPTURE:
1668                if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1669                        break;
1670                CLEAR_AFTER_FIELD(p, fmt.sdr);
1671                return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1672        case V4L2_BUF_TYPE_SDR_OUTPUT:
1673                if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1674                        break;
1675                CLEAR_AFTER_FIELD(p, fmt.sdr);
1676                return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1677        case V4L2_BUF_TYPE_META_CAPTURE:
1678                if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1679                        break;
1680                CLEAR_AFTER_FIELD(p, fmt.meta);
1681                return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1682        case V4L2_BUF_TYPE_META_OUTPUT:
1683                if (unlikely(!ops->vidioc_s_fmt_meta_out))
1684                        break;
1685                CLEAR_AFTER_FIELD(p, fmt.meta);
1686                return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1687        }
1688        return -EINVAL;
1689}
1690
1691static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1692                                struct file *file, void *fh, void *arg)
1693{
1694        struct v4l2_format *p = arg;
1695        struct video_device *vfd = video_devdata(file);
1696        int ret = check_fmt(file, p->type);
1697        unsigned int i;
1698
1699        if (ret)
1700                return ret;
1701
1702        v4l_sanitize_format(p);
1703
1704        switch (p->type) {
1705        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1706                if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1707                        break;
1708                CLEAR_AFTER_FIELD(p, fmt.pix);
1709                ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1710                /* just in case the driver zeroed it again */
1711                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1712                if (vfd->vfl_type == VFL_TYPE_TOUCH)
1713                        v4l_pix_format_touch(&p->fmt.pix);
1714                return ret;
1715        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1716                if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1717                        break;
1718                CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1719                for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1720                        CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1721                                          bytesperline);
1722                return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1723        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1724                if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1725                        break;
1726                CLEAR_AFTER_FIELD(p, fmt.win);
1727                return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1728        case V4L2_BUF_TYPE_VBI_CAPTURE:
1729                if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1730                        break;
1731                CLEAR_AFTER_FIELD(p, fmt.vbi);
1732                return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1733        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1734                if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1735                        break;
1736                CLEAR_AFTER_FIELD(p, fmt.sliced);
1737                return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1738        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1739                if (unlikely(!ops->vidioc_try_fmt_vid_out))
1740                        break;
1741                CLEAR_AFTER_FIELD(p, fmt.pix);
1742                ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1743                /* just in case the driver zeroed it again */
1744                p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1745                return ret;
1746        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1747                if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1748                        break;
1749                CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1750                for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1751                        CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1752                                          bytesperline);
1753                return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1754        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1755                if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1756                        break;
1757                CLEAR_AFTER_FIELD(p, fmt.win);
1758                return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1759        case V4L2_BUF_TYPE_VBI_OUTPUT:
1760                if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1761                        break;
1762                CLEAR_AFTER_FIELD(p, fmt.vbi);
1763                return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1764        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1765                if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1766                        break;
1767                CLEAR_AFTER_FIELD(p, fmt.sliced);
1768                return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1769        case V4L2_BUF_TYPE_SDR_CAPTURE:
1770                if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1771                        break;
1772                CLEAR_AFTER_FIELD(p, fmt.sdr);
1773                return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1774        case V4L2_BUF_TYPE_SDR_OUTPUT:
1775                if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1776                        break;
1777                CLEAR_AFTER_FIELD(p, fmt.sdr);
1778                return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1779        case V4L2_BUF_TYPE_META_CAPTURE:
1780                if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1781                        break;
1782                CLEAR_AFTER_FIELD(p, fmt.meta);
1783                return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1784        case V4L2_BUF_TYPE_META_OUTPUT:
1785                if (unlikely(!ops->vidioc_try_fmt_meta_out))
1786                        break;
1787                CLEAR_AFTER_FIELD(p, fmt.meta);
1788                return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1789        }
1790        return -EINVAL;
1791}
1792
1793static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1794                                struct file *file, void *fh, void *arg)
1795{
1796        return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1797}
1798
1799static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1800                                struct file *file, void *fh, void *arg)
1801{
1802        return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1803}
1804
1805static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1806                                struct file *file, void *fh, void *arg)
1807{
1808        struct video_device *vfd = video_devdata(file);
1809        struct v4l2_tuner *p = arg;
1810        int err;
1811
1812        p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1813                        V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1814        err = ops->vidioc_g_tuner(file, fh, p);
1815        if (!err)
1816                p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1817        return err;
1818}
1819
1820static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1821                                struct file *file, void *fh, void *arg)
1822{
1823        struct video_device *vfd = video_devdata(file);
1824        struct v4l2_tuner *p = arg;
1825        int ret;
1826
1827        ret = v4l_enable_media_source(vfd);
1828        if (ret)
1829                return ret;
1830        p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1831                        V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1832        return ops->vidioc_s_tuner(file, fh, p);
1833}
1834
1835static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1836                                struct file *file, void *fh, void *arg)
1837{
1838        struct video_device *vfd = video_devdata(file);
1839        struct v4l2_modulator *p = arg;
1840        int err;
1841
1842        if (vfd->vfl_type == VFL_TYPE_RADIO)
1843                p->type = V4L2_TUNER_RADIO;
1844
1845        err = ops->vidioc_g_modulator(file, fh, p);
1846        if (!err)
1847                p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1848        return err;
1849}
1850
1851static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1852                                struct file *file, void *fh, void *arg)
1853{
1854        struct video_device *vfd = video_devdata(file);
1855        struct v4l2_modulator *p = arg;
1856
1857        if (vfd->vfl_type == VFL_TYPE_RADIO)
1858                p->type = V4L2_TUNER_RADIO;
1859
1860        return ops->vidioc_s_modulator(file, fh, p);
1861}
1862
1863static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1864                                struct file *file, void *fh, void *arg)
1865{
1866        struct video_device *vfd = video_devdata(file);
1867        struct v4l2_frequency *p = arg;
1868
1869        if (vfd->vfl_type == VFL_TYPE_SDR)
1870                p->type = V4L2_TUNER_SDR;
1871        else
1872                p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1873                                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1874        return ops->vidioc_g_frequency(file, fh, p);
1875}
1876
1877static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1878                                struct file *file, void *fh, void *arg)
1879{
1880        struct video_device *vfd = video_devdata(file);
1881        const struct v4l2_frequency *p = arg;
1882        enum v4l2_tuner_type type;
1883        int ret;
1884
1885        ret = v4l_enable_media_source(vfd);
1886        if (ret)
1887                return ret;
1888        if (vfd->vfl_type == VFL_TYPE_SDR) {
1889                if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
1890                        return -EINVAL;
1891        } else {
1892                type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1893                                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1894                if (type != p->type)
1895                        return -EINVAL;
1896        }
1897        return ops->vidioc_s_frequency(file, fh, p);
1898}
1899
1900static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1901                                struct file *file, void *fh, void *arg)
1902{
1903        struct video_device *vfd = video_devdata(file);
1904        struct v4l2_standard *p = arg;
1905
1906        return v4l_video_std_enumstd(p, vfd->tvnorms);
1907}
1908
1909static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1910                                struct file *file, void *fh, void *arg)
1911{
1912        struct video_device *vfd = video_devdata(file);
1913        v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1914        int ret;
1915
1916        ret = v4l_enable_media_source(vfd);
1917        if (ret)
1918                return ret;
1919        norm = id & vfd->tvnorms;
1920        if (vfd->tvnorms && !norm)      /* Check if std is supported */
1921                return -EINVAL;
1922
1923        /* Calls the specific handler */
1924        return ops->vidioc_s_std(file, fh, norm);
1925}
1926
1927static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1928                                struct file *file, void *fh, void *arg)
1929{
1930        struct video_device *vfd = video_devdata(file);
1931        v4l2_std_id *p = arg;
1932        int ret;
1933
1934        ret = v4l_enable_media_source(vfd);
1935        if (ret)
1936                return ret;
1937        /*
1938         * If no signal is detected, then the driver should return
1939         * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1940         * any standards that do not apply removed.
1941         *
1942         * This means that tuners, audio and video decoders can join
1943         * their efforts to improve the standards detection.
1944         */
1945        *p = vfd->tvnorms;
1946        return ops->vidioc_querystd(file, fh, arg);
1947}
1948
1949static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1950                                struct file *file, void *fh, void *arg)
1951{
1952        struct video_device *vfd = video_devdata(file);
1953        struct v4l2_hw_freq_seek *p = arg;
1954        enum v4l2_tuner_type type;
1955        int ret;
1956
1957        ret = v4l_enable_media_source(vfd);
1958        if (ret)
1959                return ret;
1960        /* s_hw_freq_seek is not supported for SDR for now */
1961        if (vfd->vfl_type == VFL_TYPE_SDR)
1962                return -EINVAL;
1963
1964        type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1965                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1966        if (p->type != type)
1967                return -EINVAL;
1968        return ops->vidioc_s_hw_freq_seek(file, fh, p);
1969}
1970
1971static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1972                                struct file *file, void *fh, void *arg)
1973{
1974        return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1975}
1976
1977static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1978                                struct file *file, void *fh, void *arg)
1979{
1980        struct v4l2_requestbuffers *p = arg;
1981        int ret = check_fmt(file, p->type);
1982
1983        if (ret)
1984                return ret;
1985
1986        CLEAR_AFTER_FIELD(p, capabilities);
1987
1988        return ops->vidioc_reqbufs(file, fh, p);
1989}
1990
1991static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1992                                struct file *file, void *fh, void *arg)
1993{
1994        struct v4l2_buffer *p = arg;
1995        int ret = check_fmt(file, p->type);
1996
1997        return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1998}
1999
2000static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
2001                                struct file *file, void *fh, void *arg)
2002{
2003        struct v4l2_buffer *p = arg;
2004        int ret = check_fmt(file, p->type);
2005
2006        return ret ? ret : ops->vidioc_qbuf(file, fh, p);
2007}
2008
2009static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
2010                                struct file *file, void *fh, void *arg)
2011{
2012        struct v4l2_buffer *p = arg;
2013        int ret = check_fmt(file, p->type);
2014
2015        return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
2016}
2017
2018static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
2019                                struct file *file, void *fh, void *arg)
2020{
2021        struct v4l2_create_buffers *create = arg;
2022        int ret = check_fmt(file, create->format.type);
2023
2024        if (ret)
2025                return ret;
2026
2027        CLEAR_AFTER_FIELD(create, capabilities);
2028
2029        v4l_sanitize_format(&create->format);
2030
2031        ret = ops->vidioc_create_bufs(file, fh, create);
2032
2033        if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2034            create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2035                create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
2036
2037        return ret;
2038}
2039
2040static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
2041                                struct file *file, void *fh, void *arg)
2042{
2043        struct v4l2_buffer *b = arg;
2044        int ret = check_fmt(file, b->type);
2045
2046        return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
2047}
2048
2049static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
2050                                struct file *file, void *fh, void *arg)
2051{
2052        struct v4l2_streamparm *p = arg;
2053        v4l2_std_id std;
2054        int ret = check_fmt(file, p->type);
2055
2056        if (ret)
2057                return ret;
2058        if (ops->vidioc_g_parm)
2059                return ops->vidioc_g_parm(file, fh, p);
2060        if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2061            p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2062                return -EINVAL;
2063        p->parm.capture.readbuffers = 2;
2064        ret = ops->vidioc_g_std(file, fh, &std);
2065        if (ret == 0)
2066                v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2067        return ret;
2068}
2069
2070static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2071                                struct file *file, void *fh, void *arg)
2072{
2073        struct v4l2_streamparm *p = arg;
2074        int ret = check_fmt(file, p->type);
2075
2076        if (ret)
2077                return ret;
2078
2079        /* Note: extendedmode is never used in drivers */
2080        if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2081                memset(p->parm.output.reserved, 0,
2082                       sizeof(p->parm.output.reserved));
2083                p->parm.output.extendedmode = 0;
2084                p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2085        } else {
2086                memset(p->parm.capture.reserved, 0,
2087                       sizeof(p->parm.capture.reserved));
2088                p->parm.capture.extendedmode = 0;
2089                p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2090        }
2091        return ops->vidioc_s_parm(file, fh, p);
2092}
2093
2094static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2095                                struct file *file, void *fh, void *arg)
2096{
2097        struct video_device *vfd = video_devdata(file);
2098        struct v4l2_queryctrl *p = arg;
2099        struct v4l2_fh *vfh =
2100                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2101
2102        if (vfh && vfh->ctrl_handler)
2103                return v4l2_queryctrl(vfh->ctrl_handler, p);
2104        if (vfd->ctrl_handler)
2105                return v4l2_queryctrl(vfd->ctrl_handler, p);
2106        if (ops->vidioc_queryctrl)
2107                return ops->vidioc_queryctrl(file, fh, p);
2108        return -ENOTTY;
2109}
2110
2111static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2112                                struct file *file, void *fh, void *arg)
2113{
2114        struct video_device *vfd = video_devdata(file);
2115        struct v4l2_query_ext_ctrl *p = arg;
2116        struct v4l2_fh *vfh =
2117                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2118
2119        if (vfh && vfh->ctrl_handler)
2120                return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2121        if (vfd->ctrl_handler)
2122                return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2123        if (ops->vidioc_query_ext_ctrl)
2124                return ops->vidioc_query_ext_ctrl(file, fh, p);
2125        return -ENOTTY;
2126}
2127
2128static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2129                                struct file *file, void *fh, void *arg)
2130{
2131        struct video_device *vfd = video_devdata(file);
2132        struct v4l2_querymenu *p = arg;
2133        struct v4l2_fh *vfh =
2134                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2135
2136        if (vfh && vfh->ctrl_handler)
2137                return v4l2_querymenu(vfh->ctrl_handler, p);
2138        if (vfd->ctrl_handler)
2139                return v4l2_querymenu(vfd->ctrl_handler, p);
2140        if (ops->vidioc_querymenu)
2141                return ops->vidioc_querymenu(file, fh, p);
2142        return -ENOTTY;
2143}
2144
2145static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2146                                struct file *file, void *fh, void *arg)
2147{
2148        struct video_device *vfd = video_devdata(file);
2149        struct v4l2_control *p = arg;
2150        struct v4l2_fh *vfh =
2151                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2152        struct v4l2_ext_controls ctrls;
2153        struct v4l2_ext_control ctrl;
2154
2155        if (vfh && vfh->ctrl_handler)
2156                return v4l2_g_ctrl(vfh->ctrl_handler, p);
2157        if (vfd->ctrl_handler)
2158                return v4l2_g_ctrl(vfd->ctrl_handler, p);
2159        if (ops->vidioc_g_ctrl)
2160                return ops->vidioc_g_ctrl(file, fh, p);
2161        if (ops->vidioc_g_ext_ctrls == NULL)
2162                return -ENOTTY;
2163
2164        ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2165        ctrls.count = 1;
2166        ctrls.controls = &ctrl;
2167        ctrl.id = p->id;
2168        ctrl.value = p->value;
2169        if (check_ext_ctrls(&ctrls, 1)) {
2170                int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2171
2172                if (ret == 0)
2173                        p->value = ctrl.value;
2174                return ret;
2175        }
2176        return -EINVAL;
2177}
2178
2179static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2180                                struct file *file, void *fh, void *arg)
2181{
2182        struct video_device *vfd = video_devdata(file);
2183        struct v4l2_control *p = arg;
2184        struct v4l2_fh *vfh =
2185                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2186        struct v4l2_ext_controls ctrls;
2187        struct v4l2_ext_control ctrl;
2188
2189        if (vfh && vfh->ctrl_handler)
2190                return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2191        if (vfd->ctrl_handler)
2192                return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2193        if (ops->vidioc_s_ctrl)
2194                return ops->vidioc_s_ctrl(file, fh, p);
2195        if (ops->vidioc_s_ext_ctrls == NULL)
2196                return -ENOTTY;
2197
2198        ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2199        ctrls.count = 1;
2200        ctrls.controls = &ctrl;
2201        ctrl.id = p->id;
2202        ctrl.value = p->value;
2203        if (check_ext_ctrls(&ctrls, 1))
2204                return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2205        return -EINVAL;
2206}
2207
2208static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2209                                struct file *file, void *fh, void *arg)
2210{
2211        struct video_device *vfd = video_devdata(file);
2212        struct v4l2_ext_controls *p = arg;
2213        struct v4l2_fh *vfh =
2214                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2215
2216        p->error_idx = p->count;
2217        if (vfh && vfh->ctrl_handler)
2218                return v4l2_g_ext_ctrls(vfh->ctrl_handler,
2219                                        vfd, vfd->v4l2_dev->mdev, p);
2220        if (vfd->ctrl_handler)
2221                return v4l2_g_ext_ctrls(vfd->ctrl_handler,
2222                                        vfd, vfd->v4l2_dev->mdev, p);
2223        if (ops->vidioc_g_ext_ctrls == NULL)
2224                return -ENOTTY;
2225        return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
2226                                        -EINVAL;
2227}
2228
2229static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2230                                struct file *file, void *fh, void *arg)
2231{
2232        struct video_device *vfd = video_devdata(file);
2233        struct v4l2_ext_controls *p = arg;
2234        struct v4l2_fh *vfh =
2235                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2236
2237        p->error_idx = p->count;
2238        if (vfh && vfh->ctrl_handler)
2239                return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
2240                                        vfd, vfd->v4l2_dev->mdev, p);
2241        if (vfd->ctrl_handler)
2242                return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler,
2243                                        vfd, vfd->v4l2_dev->mdev, p);
2244        if (ops->vidioc_s_ext_ctrls == NULL)
2245                return -ENOTTY;
2246        return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
2247                                        -EINVAL;
2248}
2249
2250static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2251                                struct file *file, void *fh, void *arg)
2252{
2253        struct video_device *vfd = video_devdata(file);
2254        struct v4l2_ext_controls *p = arg;
2255        struct v4l2_fh *vfh =
2256                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2257
2258        p->error_idx = p->count;
2259        if (vfh && vfh->ctrl_handler)
2260                return v4l2_try_ext_ctrls(vfh->ctrl_handler,
2261                                          vfd, vfd->v4l2_dev->mdev, p);
2262        if (vfd->ctrl_handler)
2263                return v4l2_try_ext_ctrls(vfd->ctrl_handler,
2264                                          vfd, vfd->v4l2_dev->mdev, p);
2265        if (ops->vidioc_try_ext_ctrls == NULL)
2266                return -ENOTTY;
2267        return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
2268                                        -EINVAL;
2269}
2270
2271/*
2272 * The selection API specified originally that the _MPLANE buffer types
2273 * shouldn't be used. The reasons for this are lost in the mists of time
2274 * (or just really crappy memories). Regardless, this is really annoying
2275 * for userspace. So to keep things simple we map _MPLANE buffer types
2276 * to their 'regular' counterparts before calling the driver. And we
2277 * restore it afterwards. This way applications can use either buffer
2278 * type and drivers don't need to check for both.
2279 */
2280static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2281                           struct file *file, void *fh, void *arg)
2282{
2283        struct v4l2_selection *p = arg;
2284        u32 old_type = p->type;
2285        int ret;
2286
2287        if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2288                p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2289        else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2290                p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2291        ret = ops->vidioc_g_selection(file, fh, p);
2292        p->type = old_type;
2293        return ret;
2294}
2295
2296static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2297                           struct file *file, void *fh, void *arg)
2298{
2299        struct v4l2_selection *p = arg;
2300        u32 old_type = p->type;
2301        int ret;
2302
2303        if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2304                p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2305        else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2306                p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2307        ret = ops->vidioc_s_selection(file, fh, p);
2308        p->type = old_type;
2309        return ret;
2310}
2311
2312static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2313                                struct file *file, void *fh, void *arg)
2314{
2315        struct video_device *vfd = video_devdata(file);
2316        struct v4l2_crop *p = arg;
2317        struct v4l2_selection s = {
2318                .type = p->type,
2319        };
2320        int ret;
2321
2322        /* simulate capture crop using selection api */
2323
2324        /* crop means compose for output devices */
2325        if (V4L2_TYPE_IS_OUTPUT(p->type))
2326                s.target = V4L2_SEL_TGT_COMPOSE;
2327        else
2328                s.target = V4L2_SEL_TGT_CROP;
2329
2330        if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2331                s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2332                        V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2333
2334        ret = v4l_g_selection(ops, file, fh, &s);
2335
2336        /* copying results to old structure on success */
2337        if (!ret)
2338                p->c = s.r;
2339        return ret;
2340}
2341
2342static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
2343                                struct file *file, void *fh, void *arg)
2344{
2345        struct video_device *vfd = video_devdata(file);
2346        struct v4l2_crop *p = arg;
2347        struct v4l2_selection s = {
2348                .type = p->type,
2349                .r = p->c,
2350        };
2351
2352        /* simulate capture crop using selection api */
2353
2354        /* crop means compose for output devices */
2355        if (V4L2_TYPE_IS_OUTPUT(p->type))
2356                s.target = V4L2_SEL_TGT_COMPOSE;
2357        else
2358                s.target = V4L2_SEL_TGT_CROP;
2359
2360        if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2361                s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2362                        V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2363
2364        return v4l_s_selection(ops, file, fh, &s);
2365}
2366
2367static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2368                                struct file *file, void *fh, void *arg)
2369{
2370        struct video_device *vfd = video_devdata(file);
2371        struct v4l2_cropcap *p = arg;
2372        struct v4l2_selection s = { .type = p->type };
2373        int ret = 0;
2374
2375        /* setting trivial pixelaspect */
2376        p->pixelaspect.numerator = 1;
2377        p->pixelaspect.denominator = 1;
2378
2379        if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2380                s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2381        else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2382                s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2383
2384        /*
2385         * The determine_valid_ioctls() call already should ensure
2386         * that this can never happen, but just in case...
2387         */
2388        if (WARN_ON(!ops->vidioc_g_selection))
2389                return -ENOTTY;
2390
2391        if (ops->vidioc_g_pixelaspect)
2392                ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2393                                                &p->pixelaspect);
2394
2395        /*
2396         * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2397         * square pixel aspect ratio in that case.
2398         */
2399        if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2400                return ret;
2401
2402        /* Use g_selection() to fill in the bounds and defrect rectangles */
2403
2404        /* obtaining bounds */
2405        if (V4L2_TYPE_IS_OUTPUT(p->type))
2406                s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2407        else
2408                s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2409
2410        if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2411                s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2412                        V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2413
2414        ret = v4l_g_selection(ops, file, fh, &s);
2415        if (ret)
2416                return ret;
2417        p->bounds = s.r;
2418
2419        /* obtaining defrect */
2420        if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2421                s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2422        else
2423                s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2424
2425        ret = v4l_g_selection(ops, file, fh, &s);
2426        if (ret)
2427                return ret;
2428        p->defrect = s.r;
2429
2430        return 0;
2431}
2432
2433static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2434                                struct file *file, void *fh, void *arg)
2435{
2436        struct video_device *vfd = video_devdata(file);
2437        int ret;
2438
2439        if (vfd->v4l2_dev)
2440                pr_info("%s: =================  START STATUS  =================\n",
2441                        vfd->v4l2_dev->name);
2442        ret = ops->vidioc_log_status(file, fh);
2443        if (vfd->v4l2_dev)
2444                pr_info("%s: ==================  END STATUS  ==================\n",
2445                        vfd->v4l2_dev->name);
2446        return ret;
2447}
2448
2449static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2450                                struct file *file, void *fh, void *arg)
2451{
2452#ifdef CONFIG_VIDEO_ADV_DEBUG
2453        struct v4l2_dbg_register *p = arg;
2454        struct video_device *vfd = video_devdata(file);
2455        struct v4l2_subdev *sd;
2456        int idx = 0;
2457
2458        if (!capable(CAP_SYS_ADMIN))
2459                return -EPERM;
2460        if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2461                if (vfd->v4l2_dev == NULL)
2462                        return -EINVAL;
2463                v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2464                        if (p->match.addr == idx++)
2465                                return v4l2_subdev_call(sd, core, g_register, p);
2466                return -EINVAL;
2467        }
2468        if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2469            (ops->vidioc_g_chip_info || p->match.addr == 0))
2470                return ops->vidioc_g_register(file, fh, p);
2471        return -EINVAL;
2472#else
2473        return -ENOTTY;
2474#endif
2475}
2476
2477static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2478                                struct file *file, void *fh, void *arg)
2479{
2480#ifdef CONFIG_VIDEO_ADV_DEBUG
2481        const struct v4l2_dbg_register *p = arg;
2482        struct video_device *vfd = video_devdata(file);
2483        struct v4l2_subdev *sd;
2484        int idx = 0;
2485
2486        if (!capable(CAP_SYS_ADMIN))
2487                return -EPERM;
2488        if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2489                if (vfd->v4l2_dev == NULL)
2490                        return -EINVAL;
2491                v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2492                        if (p->match.addr == idx++)
2493                                return v4l2_subdev_call(sd, core, s_register, p);
2494                return -EINVAL;
2495        }
2496        if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2497            (ops->vidioc_g_chip_info || p->match.addr == 0))
2498                return ops->vidioc_s_register(file, fh, p);
2499        return -EINVAL;
2500#else
2501        return -ENOTTY;
2502#endif
2503}
2504
2505static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2506                                struct file *file, void *fh, void *arg)
2507{
2508#ifdef CONFIG_VIDEO_ADV_DEBUG
2509        struct video_device *vfd = video_devdata(file);
2510        struct v4l2_dbg_chip_info *p = arg;
2511        struct v4l2_subdev *sd;
2512        int idx = 0;
2513
2514        switch (p->match.type) {
2515        case V4L2_CHIP_MATCH_BRIDGE:
2516                if (ops->vidioc_s_register)
2517                        p->flags |= V4L2_CHIP_FL_WRITABLE;
2518                if (ops->vidioc_g_register)
2519                        p->flags |= V4L2_CHIP_FL_READABLE;
2520                strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2521                if (ops->vidioc_g_chip_info)
2522                        return ops->vidioc_g_chip_info(file, fh, arg);
2523                if (p->match.addr)
2524                        return -EINVAL;
2525                return 0;
2526
2527        case V4L2_CHIP_MATCH_SUBDEV:
2528                if (vfd->v4l2_dev == NULL)
2529                        break;
2530                v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2531                        if (p->match.addr != idx++)
2532                                continue;
2533                        if (sd->ops->core && sd->ops->core->s_register)
2534                                p->flags |= V4L2_CHIP_FL_WRITABLE;
2535                        if (sd->ops->core && sd->ops->core->g_register)
2536                                p->flags |= V4L2_CHIP_FL_READABLE;
2537                        strscpy(p->name, sd->name, sizeof(p->name));
2538                        return 0;
2539                }
2540                break;
2541        }
2542        return -EINVAL;
2543#else
2544        return -ENOTTY;
2545#endif
2546}
2547
2548static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2549                                struct file *file, void *fh, void *arg)
2550{
2551        return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2552}
2553
2554static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2555                                struct file *file, void *fh, void *arg)
2556{
2557        return ops->vidioc_subscribe_event(fh, arg);
2558}
2559
2560static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2561                                struct file *file, void *fh, void *arg)
2562{
2563        return ops->vidioc_unsubscribe_event(fh, arg);
2564}
2565
2566static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2567                                struct file *file, void *fh, void *arg)
2568{
2569        struct v4l2_sliced_vbi_cap *p = arg;
2570        int ret = check_fmt(file, p->type);
2571
2572        if (ret)
2573                return ret;
2574
2575        /* Clear up to type, everything after type is zeroed already */
2576        memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2577
2578        return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2579}
2580
2581static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2582                                struct file *file, void *fh, void *arg)
2583{
2584        struct video_device *vfd = video_devdata(file);
2585        struct v4l2_frequency_band *p = arg;
2586        enum v4l2_tuner_type type;
2587        int err;
2588
2589        if (vfd->vfl_type == VFL_TYPE_SDR) {
2590                if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2591                        return -EINVAL;
2592                type = p->type;
2593        } else {
2594                type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2595                                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2596                if (type != p->type)
2597                        return -EINVAL;
2598        }
2599        if (ops->vidioc_enum_freq_bands) {
2600                err = ops->vidioc_enum_freq_bands(file, fh, p);
2601                if (err != -ENOTTY)
2602                        return err;
2603        }
2604        if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2605                struct v4l2_tuner t = {
2606                        .index = p->tuner,
2607                        .type = type,
2608                };
2609
2610                if (p->index)
2611                        return -EINVAL;
2612                err = ops->vidioc_g_tuner(file, fh, &t);
2613                if (err)
2614                        return err;
2615                p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2616                p->rangelow = t.rangelow;
2617                p->rangehigh = t.rangehigh;
2618                p->modulation = (type == V4L2_TUNER_RADIO) ?
2619                        V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2620                return 0;
2621        }
2622        if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2623                struct v4l2_modulator m = {
2624                        .index = p->tuner,
2625                };
2626
2627                if (type != V4L2_TUNER_RADIO)
2628                        return -EINVAL;
2629                if (p->index)
2630                        return -EINVAL;
2631                err = ops->vidioc_g_modulator(file, fh, &m);
2632                if (err)
2633                        return err;
2634                p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2635                p->rangelow = m.rangelow;
2636                p->rangehigh = m.rangehigh;
2637                p->modulation = (type == V4L2_TUNER_RADIO) ?
2638                        V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2639                return 0;
2640        }
2641        return -ENOTTY;
2642}
2643
2644struct v4l2_ioctl_info {
2645        unsigned int ioctl;
2646        u32 flags;
2647        const char * const name;
2648        int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2649                    void *fh, void *p);
2650        void (*debug)(const void *arg, bool write_only);
2651};
2652
2653/* This control needs a priority check */
2654#define INFO_FL_PRIO            (1 << 0)
2655/* This control can be valid if the filehandle passes a control handler. */
2656#define INFO_FL_CTRL            (1 << 1)
2657/* Queuing ioctl */
2658#define INFO_FL_QUEUE           (1 << 2)
2659/* Always copy back result, even on error */
2660#define INFO_FL_ALWAYS_COPY     (1 << 3)
2661/* Zero struct from after the field to the end */
2662#define INFO_FL_CLEAR(v4l2_struct, field)                       \
2663        ((offsetof(struct v4l2_struct, field) +                 \
2664          sizeof(((struct v4l2_struct *)0)->field)) << 16)
2665#define INFO_FL_CLEAR_MASK      (_IOC_SIZEMASK << 16)
2666
2667#define DEFINE_V4L_STUB_FUNC(_vidioc)                           \
2668        static int v4l_stub_ ## _vidioc(                        \
2669                        const struct v4l2_ioctl_ops *ops,       \
2670                        struct file *file, void *fh, void *p)   \
2671        {                                                       \
2672                return ops->vidioc_ ## _vidioc(file, fh, p);    \
2673        }
2674
2675#define IOCTL_INFO(_ioctl, _func, _debug, _flags)               \
2676        [_IOC_NR(_ioctl)] = {                                   \
2677                .ioctl = _ioctl,                                \
2678                .flags = _flags,                                \
2679                .name = #_ioctl,                                \
2680                .func = _func,                                  \
2681                .debug = _debug,                                \
2682        }
2683
2684DEFINE_V4L_STUB_FUNC(g_fbuf)
2685DEFINE_V4L_STUB_FUNC(s_fbuf)
2686DEFINE_V4L_STUB_FUNC(expbuf)
2687DEFINE_V4L_STUB_FUNC(g_std)
2688DEFINE_V4L_STUB_FUNC(g_audio)
2689DEFINE_V4L_STUB_FUNC(s_audio)
2690DEFINE_V4L_STUB_FUNC(g_input)
2691DEFINE_V4L_STUB_FUNC(g_edid)
2692DEFINE_V4L_STUB_FUNC(s_edid)
2693DEFINE_V4L_STUB_FUNC(g_output)
2694DEFINE_V4L_STUB_FUNC(g_audout)
2695DEFINE_V4L_STUB_FUNC(s_audout)
2696DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2697DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2698DEFINE_V4L_STUB_FUNC(enumaudio)
2699DEFINE_V4L_STUB_FUNC(enumaudout)
2700DEFINE_V4L_STUB_FUNC(enum_framesizes)
2701DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2702DEFINE_V4L_STUB_FUNC(g_enc_index)
2703DEFINE_V4L_STUB_FUNC(encoder_cmd)
2704DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2705DEFINE_V4L_STUB_FUNC(decoder_cmd)
2706DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2707DEFINE_V4L_STUB_FUNC(s_dv_timings)
2708DEFINE_V4L_STUB_FUNC(g_dv_timings)
2709DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2710DEFINE_V4L_STUB_FUNC(query_dv_timings)
2711DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2712
2713static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2714        IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2715        IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2716        IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2717        IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2718        IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2719        IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2720        IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2721        IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2722        IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2723        IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2724        IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2725        IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2726        IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2727        IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2728        IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2729        IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2730        IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2731        IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2732        IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2733        IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2734        IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2735        IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2736        IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2737        IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2738        IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2739        IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2740        IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2741        IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2742        IOCTL_INFO(VIDIOC_G_INPUT, v4l_stub_g_input, v4l_print_u32, 0),
2743        IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2744        IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2745        IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2746        IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_stub_g_output, v4l_print_u32, 0),
2747        IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2748        IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2749        IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2750        IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2751        IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2752        IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2753        IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2754        IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2755        IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2756        IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2757        IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2758        IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2759        IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2760        IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2761        IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2762        IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2763        IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2764        IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2765        IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2766        IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2767        IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2768        IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
2769        IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2770        IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2771        IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2772        IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2773        IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2774        IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2775        IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2776        IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2777        IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2778        IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2779        IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2780        IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2781        IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2782        IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2783        IOCTL_INFO(VIDIOC_S_DV_TIMINGS, v4l_stub_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_dv_timings, bt.flags)),
2784        IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2785        IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2786        IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2787        IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2788        IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2789        IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2790        IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2791        IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2792        IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2793        IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2794        IOCTL_INFO(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
2795        IOCTL_INFO(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
2796};
2797#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2798
2799static bool v4l2_is_known_ioctl(unsigned int cmd)
2800{
2801        if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2802                return false;
2803        return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2804}
2805
2806static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2807                                         struct v4l2_fh *vfh, unsigned int cmd,
2808                                         void *arg)
2809{
2810        if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2811                return vdev->lock;
2812#if IS_ENABLED(CONFIG_V4L2_MEM2MEM_DEV)
2813        if (vfh && vfh->m2m_ctx &&
2814            (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2815                if (vfh->m2m_ctx->q_lock)
2816                        return vfh->m2m_ctx->q_lock;
2817        }
2818#endif
2819        if (vdev->queue && vdev->queue->lock &&
2820                        (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2821                return vdev->queue->lock;
2822        return vdev->lock;
2823}
2824
2825/* Common ioctl debug function. This function can be used by
2826   external ioctl messages as well as internal V4L ioctl */
2827void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2828{
2829        const char *dir, *type;
2830
2831        if (prefix)
2832                printk(KERN_DEBUG "%s: ", prefix);
2833
2834        switch (_IOC_TYPE(cmd)) {
2835        case 'd':
2836                type = "v4l2_int";
2837                break;
2838        case 'V':
2839                if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2840                        type = "v4l2";
2841                        break;
2842                }
2843                pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2844                return;
2845        default:
2846                type = "unknown";
2847                break;
2848        }
2849
2850        switch (_IOC_DIR(cmd)) {
2851        case _IOC_NONE:              dir = "--"; break;
2852        case _IOC_READ:              dir = "r-"; break;
2853        case _IOC_WRITE:             dir = "-w"; break;
2854        case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2855        default:                     dir = "*ERR*"; break;
2856        }
2857        pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2858                type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2859}
2860EXPORT_SYMBOL(v4l_printk_ioctl);
2861
2862static long __video_do_ioctl(struct file *file,
2863                unsigned int cmd, void *arg)
2864{
2865        struct video_device *vfd = video_devdata(file);
2866        struct mutex *req_queue_lock = NULL;
2867        struct mutex *lock; /* ioctl serialization mutex */
2868        const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2869        bool write_only = false;
2870        struct v4l2_ioctl_info default_info;
2871        const struct v4l2_ioctl_info *info;
2872        void *fh = file->private_data;
2873        struct v4l2_fh *vfh = NULL;
2874        int dev_debug = vfd->dev_debug;
2875        long ret = -ENOTTY;
2876
2877        if (ops == NULL) {
2878                pr_warn("%s: has no ioctl_ops.\n",
2879                                video_device_node_name(vfd));
2880                return ret;
2881        }
2882
2883        if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2884                vfh = file->private_data;
2885
2886        /*
2887         * We need to serialize streamon/off with queueing new requests.
2888         * These ioctls may trigger the cancellation of a streaming
2889         * operation, and that should not be mixed with queueing a new
2890         * request at the same time.
2891         */
2892        if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
2893            (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
2894                req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
2895
2896                if (mutex_lock_interruptible(req_queue_lock))
2897                        return -ERESTARTSYS;
2898        }
2899
2900        lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
2901
2902        if (lock && mutex_lock_interruptible(lock)) {
2903                if (req_queue_lock)
2904                        mutex_unlock(req_queue_lock);
2905                return -ERESTARTSYS;
2906        }
2907
2908        if (!video_is_registered(vfd)) {
2909                ret = -ENODEV;
2910                goto unlock;
2911        }
2912
2913        if (v4l2_is_known_ioctl(cmd)) {
2914                info = &v4l2_ioctls[_IOC_NR(cmd)];
2915
2916                if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2917                    !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2918                        goto done;
2919
2920                if (vfh && (info->flags & INFO_FL_PRIO)) {
2921                        ret = v4l2_prio_check(vfd->prio, vfh->prio);
2922                        if (ret)
2923                                goto done;
2924                }
2925        } else {
2926                default_info.ioctl = cmd;
2927                default_info.flags = 0;
2928                default_info.debug = v4l_print_default;
2929                info = &default_info;
2930        }
2931
2932        write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2933        if (info != &default_info) {
2934                ret = info->func(ops, file, fh, arg);
2935        } else if (!ops->vidioc_default) {
2936                ret = -ENOTTY;
2937        } else {
2938                ret = ops->vidioc_default(file, fh,
2939                        vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2940                        cmd, arg);
2941        }
2942
2943done:
2944        if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
2945                if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
2946                    (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
2947                        goto unlock;
2948
2949                v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2950                if (ret < 0)
2951                        pr_cont(": error %ld", ret);
2952                if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
2953                        pr_cont("\n");
2954                else if (_IOC_DIR(cmd) == _IOC_NONE)
2955                        info->debug(arg, write_only);
2956                else {
2957                        pr_cont(": ");
2958                        info->debug(arg, write_only);
2959                }
2960        }
2961
2962unlock:
2963        if (lock)
2964                mutex_unlock(lock);
2965        if (req_queue_lock)
2966                mutex_unlock(req_queue_lock);
2967        return ret;
2968}
2969
2970static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2971                            void __user **user_ptr, void ***kernel_ptr)
2972{
2973        int ret = 0;
2974
2975        switch (cmd) {
2976        case VIDIOC_PREPARE_BUF:
2977        case VIDIOC_QUERYBUF:
2978        case VIDIOC_QBUF:
2979        case VIDIOC_DQBUF: {
2980                struct v4l2_buffer *buf = parg;
2981
2982                if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2983                        if (buf->length > VIDEO_MAX_PLANES) {
2984                                ret = -EINVAL;
2985                                break;
2986                        }
2987                        *user_ptr = (void __user *)buf->m.planes;
2988                        *kernel_ptr = (void **)&buf->m.planes;
2989                        *array_size = sizeof(struct v4l2_plane) * buf->length;
2990                        ret = 1;
2991                }
2992                break;
2993        }
2994
2995        case VIDIOC_G_EDID:
2996        case VIDIOC_S_EDID: {
2997                struct v4l2_edid *edid = parg;
2998
2999                if (edid->blocks) {
3000                        if (edid->blocks > 256) {
3001                                ret = -EINVAL;
3002                                break;
3003                        }
3004                        *user_ptr = (void __user *)edid->edid;
3005                        *kernel_ptr = (void **)&edid->edid;
3006                        *array_size = edid->blocks * 128;
3007                        ret = 1;
3008                }
3009                break;
3010        }
3011
3012        case VIDIOC_S_EXT_CTRLS:
3013        case VIDIOC_G_EXT_CTRLS:
3014        case VIDIOC_TRY_EXT_CTRLS: {
3015                struct v4l2_ext_controls *ctrls = parg;
3016
3017                if (ctrls->count != 0) {
3018                        if (ctrls->count > V4L2_CID_MAX_CTRLS) {
3019                                ret = -EINVAL;
3020                                break;
3021                        }
3022                        *user_ptr = (void __user *)ctrls->controls;
3023                        *kernel_ptr = (void **)&ctrls->controls;
3024                        *array_size = sizeof(struct v4l2_ext_control)
3025                                    * ctrls->count;
3026                        ret = 1;
3027                }
3028                break;
3029        }
3030
3031        case VIDIOC_SUBDEV_G_ROUTING:
3032        case VIDIOC_SUBDEV_S_ROUTING: {
3033                struct v4l2_subdev_routing *route = parg;
3034
3035                if (route->num_routes > 0) {
3036                        if (route->num_routes > 256)
3037                                return -EINVAL;
3038
3039                        *user_ptr = (void __user *)route->routes;
3040                        *kernel_ptr = (void *)&route->routes;
3041                        *array_size = sizeof(struct v4l2_plane)
3042                                    * route->num_routes;
3043                        ret = 1;
3044                }
3045                break;
3046        }
3047        }
3048
3049        return ret;
3050}
3051
3052long
3053video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
3054               v4l2_kioctl func)
3055{
3056        char    sbuf[128];
3057        void    *mbuf = NULL;
3058        void    *parg = (void *)arg;
3059        long    err  = -EINVAL;
3060        bool    has_array_args;
3061        bool    always_copy = false;
3062        size_t  array_size = 0;
3063        void __user *user_ptr = NULL;
3064        void    **kernel_ptr = NULL;
3065        const size_t ioc_size = _IOC_SIZE(cmd);
3066
3067        /*  Copy arguments into temp kernel buffer  */
3068        if (_IOC_DIR(cmd) != _IOC_NONE) {
3069                if (ioc_size <= sizeof(sbuf)) {
3070                        parg = sbuf;
3071                } else {
3072                        /* too big to allocate from stack */
3073                        mbuf = kvmalloc(ioc_size, GFP_KERNEL);
3074                        if (NULL == mbuf)
3075                                return -ENOMEM;
3076                        parg = mbuf;
3077                }
3078
3079                err = -EFAULT;
3080                if (_IOC_DIR(cmd) & _IOC_WRITE) {
3081                        unsigned int n = ioc_size;
3082
3083                        /*
3084                         * In some cases, only a few fields are used as input,
3085                         * i.e. when the app sets "index" and then the driver
3086                         * fills in the rest of the structure for the thing
3087                         * with that index.  We only need to copy up the first
3088                         * non-input field.
3089                         */
3090                        if (v4l2_is_known_ioctl(cmd)) {
3091                                u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
3092
3093                                if (flags & INFO_FL_CLEAR_MASK)
3094                                        n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3095                                always_copy = flags & INFO_FL_ALWAYS_COPY;
3096                        }
3097
3098                        if (copy_from_user(parg, (void __user *)arg, n))
3099                                goto out;
3100
3101                        /* zero out anything we don't copy from userspace */
3102                        if (n < ioc_size)
3103                                memset((u8 *)parg + n, 0, ioc_size - n);
3104                } else {
3105                        /* read-only ioctl */
3106                        memset(parg, 0, ioc_size);
3107                }
3108        }
3109
3110        err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3111        if (err < 0)
3112                goto out;
3113        has_array_args = err;
3114
3115        if (has_array_args) {
3116                /*
3117                 * When adding new types of array args, make sure that the
3118                 * parent argument to ioctl (which contains the pointer to the
3119                 * array) fits into sbuf (so that mbuf will still remain
3120                 * unused up to here).
3121                 */
3122                mbuf = kvmalloc(array_size, GFP_KERNEL);
3123                err = -ENOMEM;
3124                if (NULL == mbuf)
3125                        goto out_array_args;
3126                err = -EFAULT;
3127                if (copy_from_user(mbuf, user_ptr, array_size))
3128                        goto out_array_args;
3129                *kernel_ptr = mbuf;
3130        }
3131
3132        /* Handles IOCTL */
3133        err = func(file, cmd, parg);
3134        if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3135                err = -ENOTTY;
3136                goto out;
3137        }
3138
3139        if (err == 0) {
3140                if (cmd == VIDIOC_DQBUF)
3141                        trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3142                else if (cmd == VIDIOC_QBUF)
3143                        trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3144        }
3145
3146        if (has_array_args) {
3147                *kernel_ptr = (void __force *)user_ptr;
3148                if (copy_to_user(user_ptr, mbuf, array_size))
3149                        err = -EFAULT;
3150                goto out_array_args;
3151        }
3152        /*
3153         * Some ioctls can return an error, but still have valid
3154         * results that must be returned.
3155         */
3156        if (err < 0 && !always_copy)
3157                goto out;
3158
3159out_array_args:
3160        /*  Copy results into user buffer  */
3161        switch (_IOC_DIR(cmd)) {
3162        case _IOC_READ:
3163        case (_IOC_WRITE | _IOC_READ):
3164                if (copy_to_user((void __user *)arg, parg, ioc_size))
3165                        err = -EFAULT;
3166                break;
3167        }
3168
3169out:
3170        kvfree(mbuf);
3171        return err;
3172}
3173
3174long video_ioctl2(struct file *file,
3175               unsigned int cmd, unsigned long arg)
3176{
3177        return video_usercopy(file, cmd, arg, __video_do_ioctl);
3178}
3179EXPORT_SYMBOL(video_ioctl2);
3180