linux/drivers/media/v4l2-core/v4l2-ioctl.c
<<
>>
Prefs
   1/*
   2 * Video capture interface for Linux version 2
   3 *
   4 * A generic framework to process V4L2 ioctl commands.
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License
   8 * as published by the Free Software Foundation; either version
   9 * 2 of the License, or (at your option) any later version.
  10 *
  11 * Authors:     Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
  12 *              Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/slab.h>
  17#include <linux/types.h>
  18#include <linux/kernel.h>
  19#include <linux/version.h>
  20
  21#include <linux/videodev2.h>
  22
  23#include <media/v4l2-common.h>
  24#include <media/v4l2-ioctl.h>
  25#include <media/v4l2-ctrls.h>
  26#include <media/v4l2-fh.h>
  27#include <media/v4l2-event.h>
  28#include <media/v4l2-device.h>
  29#include <media/videobuf2-core.h>
  30
  31#define CREATE_TRACE_POINTS
  32#include <trace/events/v4l2.h>
  33
  34/* Zero out the end of the struct pointed to by p.  Everything after, but
  35 * not including, the specified field is cleared. */
  36#define CLEAR_AFTER_FIELD(p, field) \
  37        memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
  38        0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
  39
  40#define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
  41
  42struct std_descr {
  43        v4l2_std_id std;
  44        const char *descr;
  45};
  46
  47static const struct std_descr standards[] = {
  48        { V4L2_STD_NTSC,        "NTSC"      },
  49        { V4L2_STD_NTSC_M,      "NTSC-M"    },
  50        { V4L2_STD_NTSC_M_JP,   "NTSC-M-JP" },
  51        { V4L2_STD_NTSC_M_KR,   "NTSC-M-KR" },
  52        { V4L2_STD_NTSC_443,    "NTSC-443"  },
  53        { V4L2_STD_PAL,         "PAL"       },
  54        { V4L2_STD_PAL_BG,      "PAL-BG"    },
  55        { V4L2_STD_PAL_B,       "PAL-B"     },
  56        { V4L2_STD_PAL_B1,      "PAL-B1"    },
  57        { V4L2_STD_PAL_G,       "PAL-G"     },
  58        { V4L2_STD_PAL_H,       "PAL-H"     },
  59        { V4L2_STD_PAL_I,       "PAL-I"     },
  60        { V4L2_STD_PAL_DK,      "PAL-DK"    },
  61        { V4L2_STD_PAL_D,       "PAL-D"     },
  62        { V4L2_STD_PAL_D1,      "PAL-D1"    },
  63        { V4L2_STD_PAL_K,       "PAL-K"     },
  64        { V4L2_STD_PAL_M,       "PAL-M"     },
  65        { V4L2_STD_PAL_N,       "PAL-N"     },
  66        { V4L2_STD_PAL_Nc,      "PAL-Nc"    },
  67        { V4L2_STD_PAL_60,      "PAL-60"    },
  68        { V4L2_STD_SECAM,       "SECAM"     },
  69        { V4L2_STD_SECAM_B,     "SECAM-B"   },
  70        { V4L2_STD_SECAM_G,     "SECAM-G"   },
  71        { V4L2_STD_SECAM_H,     "SECAM-H"   },
  72        { V4L2_STD_SECAM_DK,    "SECAM-DK"  },
  73        { V4L2_STD_SECAM_D,     "SECAM-D"   },
  74        { V4L2_STD_SECAM_K,     "SECAM-K"   },
  75        { V4L2_STD_SECAM_K1,    "SECAM-K1"  },
  76        { V4L2_STD_SECAM_L,     "SECAM-L"   },
  77        { V4L2_STD_SECAM_LC,    "SECAM-Lc"  },
  78        { 0,                    "Unknown"   }
  79};
  80
  81/* video4linux standard ID conversion to standard name
  82 */
  83const char *v4l2_norm_to_name(v4l2_std_id id)
  84{
  85        u32 myid = id;
  86        int i;
  87
  88        /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
  89           64 bit comparations. So, on that architecture, with some gcc
  90           variants, compilation fails. Currently, the max value is 30bit wide.
  91         */
  92        BUG_ON(myid != id);
  93
  94        for (i = 0; standards[i].std; i++)
  95                if (myid == standards[i].std)
  96                        break;
  97        return standards[i].descr;
  98}
  99EXPORT_SYMBOL(v4l2_norm_to_name);
 100
 101/* Returns frame period for the given standard */
 102void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
 103{
 104        if (id & V4L2_STD_525_60) {
 105                frameperiod->numerator = 1001;
 106                frameperiod->denominator = 30000;
 107        } else {
 108                frameperiod->numerator = 1;
 109                frameperiod->denominator = 25;
 110        }
 111}
 112EXPORT_SYMBOL(v4l2_video_std_frame_period);
 113
 114/* Fill in the fields of a v4l2_standard structure according to the
 115   'id' and 'transmission' parameters.  Returns negative on error.  */
 116int v4l2_video_std_construct(struct v4l2_standard *vs,
 117                             int id, const char *name)
 118{
 119        vs->id = id;
 120        v4l2_video_std_frame_period(id, &vs->frameperiod);
 121        vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
 122        strlcpy(vs->name, name, sizeof(vs->name));
 123        return 0;
 124}
 125EXPORT_SYMBOL(v4l2_video_std_construct);
 126
 127/* ----------------------------------------------------------------- */
 128/* some arrays for pretty-printing debug messages of enum types      */
 129
 130const char *v4l2_field_names[] = {
 131        [V4L2_FIELD_ANY]        = "any",
 132        [V4L2_FIELD_NONE]       = "none",
 133        [V4L2_FIELD_TOP]        = "top",
 134        [V4L2_FIELD_BOTTOM]     = "bottom",
 135        [V4L2_FIELD_INTERLACED] = "interlaced",
 136        [V4L2_FIELD_SEQ_TB]     = "seq-tb",
 137        [V4L2_FIELD_SEQ_BT]     = "seq-bt",
 138        [V4L2_FIELD_ALTERNATE]  = "alternate",
 139        [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
 140        [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
 141};
 142EXPORT_SYMBOL(v4l2_field_names);
 143
 144const char *v4l2_type_names[] = {
 145        [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
 146        [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
 147        [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
 148        [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
 149        [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
 150        [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
 151        [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
 152        [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
 153        [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
 154        [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
 155        [V4L2_BUF_TYPE_SDR_CAPTURE]        = "sdr-cap",
 156};
 157EXPORT_SYMBOL(v4l2_type_names);
 158
 159static const char *v4l2_memory_names[] = {
 160        [V4L2_MEMORY_MMAP]    = "mmap",
 161        [V4L2_MEMORY_USERPTR] = "userptr",
 162        [V4L2_MEMORY_OVERLAY] = "overlay",
 163        [V4L2_MEMORY_DMABUF] = "dmabuf",
 164};
 165
 166#define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
 167
 168/* ------------------------------------------------------------------ */
 169/* debug help functions                                               */
 170
 171static void v4l_print_querycap(const void *arg, bool write_only)
 172{
 173        const struct v4l2_capability *p = arg;
 174
 175        pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
 176                "capabilities=0x%08x, device_caps=0x%08x\n",
 177                (int)sizeof(p->driver), p->driver,
 178                (int)sizeof(p->card), p->card,
 179                (int)sizeof(p->bus_info), p->bus_info,
 180                p->version, p->capabilities, p->device_caps);
 181}
 182
 183static void v4l_print_enuminput(const void *arg, bool write_only)
 184{
 185        const struct v4l2_input *p = arg;
 186
 187        pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
 188                "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
 189                p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
 190                p->tuner, (unsigned long long)p->std, p->status,
 191                p->capabilities);
 192}
 193
 194static void v4l_print_enumoutput(const void *arg, bool write_only)
 195{
 196        const struct v4l2_output *p = arg;
 197
 198        pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
 199                "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
 200                p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
 201                p->modulator, (unsigned long long)p->std, p->capabilities);
 202}
 203
 204static void v4l_print_audio(const void *arg, bool write_only)
 205{
 206        const struct v4l2_audio *p = arg;
 207
 208        if (write_only)
 209                pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
 210        else
 211                pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
 212                        p->index, (int)sizeof(p->name), p->name,
 213                        p->capability, p->mode);
 214}
 215
 216static void v4l_print_audioout(const void *arg, bool write_only)
 217{
 218        const struct v4l2_audioout *p = arg;
 219
 220        if (write_only)
 221                pr_cont("index=%u\n", p->index);
 222        else
 223                pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
 224                        p->index, (int)sizeof(p->name), p->name,
 225                        p->capability, p->mode);
 226}
 227
 228static void v4l_print_fmtdesc(const void *arg, bool write_only)
 229{
 230        const struct v4l2_fmtdesc *p = arg;
 231
 232        pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
 233                p->index, prt_names(p->type, v4l2_type_names),
 234                p->flags, (p->pixelformat & 0xff),
 235                (p->pixelformat >>  8) & 0xff,
 236                (p->pixelformat >> 16) & 0xff,
 237                (p->pixelformat >> 24) & 0xff,
 238                (int)sizeof(p->description), p->description);
 239}
 240
 241static void v4l_print_format(const void *arg, bool write_only)
 242{
 243        const struct v4l2_format *p = arg;
 244        const struct v4l2_pix_format *pix;
 245        const struct v4l2_pix_format_mplane *mp;
 246        const struct v4l2_vbi_format *vbi;
 247        const struct v4l2_sliced_vbi_format *sliced;
 248        const struct v4l2_window *win;
 249        const struct v4l2_sdr_format *sdr;
 250        unsigned i;
 251
 252        pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
 253        switch (p->type) {
 254        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 255        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 256                pix = &p->fmt.pix;
 257                pr_cont(", width=%u, height=%u, "
 258                        "pixelformat=%c%c%c%c, field=%s, "
 259                        "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
 260                        pix->width, pix->height,
 261                        (pix->pixelformat & 0xff),
 262                        (pix->pixelformat >>  8) & 0xff,
 263                        (pix->pixelformat >> 16) & 0xff,
 264                        (pix->pixelformat >> 24) & 0xff,
 265                        prt_names(pix->field, v4l2_field_names),
 266                        pix->bytesperline, pix->sizeimage,
 267                        pix->colorspace);
 268                break;
 269        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 270        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 271                mp = &p->fmt.pix_mp;
 272                pr_cont(", width=%u, height=%u, "
 273                        "format=%c%c%c%c, field=%s, "
 274                        "colorspace=%d, num_planes=%u\n",
 275                        mp->width, mp->height,
 276                        (mp->pixelformat & 0xff),
 277                        (mp->pixelformat >>  8) & 0xff,
 278                        (mp->pixelformat >> 16) & 0xff,
 279                        (mp->pixelformat >> 24) & 0xff,
 280                        prt_names(mp->field, v4l2_field_names),
 281                        mp->colorspace, mp->num_planes);
 282                for (i = 0; i < mp->num_planes; i++)
 283                        printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
 284                                        mp->plane_fmt[i].bytesperline,
 285                                        mp->plane_fmt[i].sizeimage);
 286                break;
 287        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 288        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 289                win = &p->fmt.win;
 290                /* Note: we can't print the clip list here since the clips
 291                 * pointer is a userspace pointer, not a kernelspace
 292                 * pointer. */
 293                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",
 294                        win->w.width, win->w.height, win->w.left, win->w.top,
 295                        prt_names(win->field, v4l2_field_names),
 296                        win->chromakey, win->clipcount, win->clips,
 297                        win->bitmap, win->global_alpha);
 298                break;
 299        case V4L2_BUF_TYPE_VBI_CAPTURE:
 300        case V4L2_BUF_TYPE_VBI_OUTPUT:
 301                vbi = &p->fmt.vbi;
 302                pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
 303                        "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
 304                        vbi->sampling_rate, vbi->offset,
 305                        vbi->samples_per_line,
 306                        (vbi->sample_format & 0xff),
 307                        (vbi->sample_format >>  8) & 0xff,
 308                        (vbi->sample_format >> 16) & 0xff,
 309                        (vbi->sample_format >> 24) & 0xff,
 310                        vbi->start[0], vbi->start[1],
 311                        vbi->count[0], vbi->count[1]);
 312                break;
 313        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 314        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 315                sliced = &p->fmt.sliced;
 316                pr_cont(", service_set=0x%08x, io_size=%d\n",
 317                                sliced->service_set, sliced->io_size);
 318                for (i = 0; i < 24; i++)
 319                        printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
 320                                sliced->service_lines[0][i],
 321                                sliced->service_lines[1][i]);
 322                break;
 323        case V4L2_BUF_TYPE_SDR_CAPTURE:
 324                sdr = &p->fmt.sdr;
 325                pr_cont(", pixelformat=%c%c%c%c\n",
 326                        (sdr->pixelformat >>  0) & 0xff,
 327                        (sdr->pixelformat >>  8) & 0xff,
 328                        (sdr->pixelformat >> 16) & 0xff,
 329                        (sdr->pixelformat >> 24) & 0xff);
 330                break;
 331        }
 332}
 333
 334static void v4l_print_framebuffer(const void *arg, bool write_only)
 335{
 336        const struct v4l2_framebuffer *p = arg;
 337
 338        pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
 339                "height=%u, pixelformat=%c%c%c%c, "
 340                "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
 341                        p->capability, p->flags, p->base,
 342                        p->fmt.width, p->fmt.height,
 343                        (p->fmt.pixelformat & 0xff),
 344                        (p->fmt.pixelformat >>  8) & 0xff,
 345                        (p->fmt.pixelformat >> 16) & 0xff,
 346                        (p->fmt.pixelformat >> 24) & 0xff,
 347                        p->fmt.bytesperline, p->fmt.sizeimage,
 348                        p->fmt.colorspace);
 349}
 350
 351static void v4l_print_buftype(const void *arg, bool write_only)
 352{
 353        pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
 354}
 355
 356static void v4l_print_modulator(const void *arg, bool write_only)
 357{
 358        const struct v4l2_modulator *p = arg;
 359
 360        if (write_only)
 361                pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
 362        else
 363                pr_cont("index=%u, name=%.*s, capability=0x%x, "
 364                        "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
 365                        p->index, (int)sizeof(p->name), p->name, p->capability,
 366                        p->rangelow, p->rangehigh, p->txsubchans);
 367}
 368
 369static void v4l_print_tuner(const void *arg, bool write_only)
 370{
 371        const struct v4l2_tuner *p = arg;
 372
 373        if (write_only)
 374                pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
 375        else
 376                pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
 377                        "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
 378                        "rxsubchans=0x%x, audmode=%u\n",
 379                        p->index, (int)sizeof(p->name), p->name, p->type,
 380                        p->capability, p->rangelow,
 381                        p->rangehigh, p->signal, p->afc,
 382                        p->rxsubchans, p->audmode);
 383}
 384
 385static void v4l_print_frequency(const void *arg, bool write_only)
 386{
 387        const struct v4l2_frequency *p = arg;
 388
 389        pr_cont("tuner=%u, type=%u, frequency=%u\n",
 390                                p->tuner, p->type, p->frequency);
 391}
 392
 393static void v4l_print_standard(const void *arg, bool write_only)
 394{
 395        const struct v4l2_standard *p = arg;
 396
 397        pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
 398                "framelines=%u\n", p->index,
 399                (unsigned long long)p->id, (int)sizeof(p->name), p->name,
 400                p->frameperiod.numerator,
 401                p->frameperiod.denominator,
 402                p->framelines);
 403}
 404
 405static void v4l_print_std(const void *arg, bool write_only)
 406{
 407        pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
 408}
 409
 410static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
 411{
 412        const struct v4l2_hw_freq_seek *p = arg;
 413
 414        pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
 415                "rangelow=%u, rangehigh=%u\n",
 416                p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
 417                p->rangelow, p->rangehigh);
 418}
 419
 420static void v4l_print_requestbuffers(const void *arg, bool write_only)
 421{
 422        const struct v4l2_requestbuffers *p = arg;
 423
 424        pr_cont("count=%d, type=%s, memory=%s\n",
 425                p->count,
 426                prt_names(p->type, v4l2_type_names),
 427                prt_names(p->memory, v4l2_memory_names));
 428}
 429
 430static void v4l_print_buffer(const void *arg, bool write_only)
 431{
 432        const struct v4l2_buffer *p = arg;
 433        const struct v4l2_timecode *tc = &p->timecode;
 434        const struct v4l2_plane *plane;
 435        int i;
 436
 437        pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
 438                "flags=0x%08x, field=%s, sequence=%d, memory=%s",
 439                        p->timestamp.tv_sec / 3600,
 440                        (int)(p->timestamp.tv_sec / 60) % 60,
 441                        (int)(p->timestamp.tv_sec % 60),
 442                        (long)p->timestamp.tv_usec,
 443                        p->index,
 444                        prt_names(p->type, v4l2_type_names),
 445                        p->flags, prt_names(p->field, v4l2_field_names),
 446                        p->sequence, prt_names(p->memory, v4l2_memory_names));
 447
 448        if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
 449                pr_cont("\n");
 450                for (i = 0; i < p->length; ++i) {
 451                        plane = &p->m.planes[i];
 452                        printk(KERN_DEBUG
 453                                "plane %d: bytesused=%d, data_offset=0x%08x, "
 454                                "offset/userptr=0x%lx, length=%d\n",
 455                                i, plane->bytesused, plane->data_offset,
 456                                plane->m.userptr, plane->length);
 457                }
 458        } else {
 459                pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
 460                        p->bytesused, p->m.userptr, p->length);
 461        }
 462
 463        printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
 464                "flags=0x%08x, frames=%d, userbits=0x%08x\n",
 465                        tc->hours, tc->minutes, tc->seconds,
 466                        tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
 467}
 468
 469static void v4l_print_exportbuffer(const void *arg, bool write_only)
 470{
 471        const struct v4l2_exportbuffer *p = arg;
 472
 473        pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
 474                p->fd, prt_names(p->type, v4l2_type_names),
 475                p->index, p->plane, p->flags);
 476}
 477
 478static void v4l_print_create_buffers(const void *arg, bool write_only)
 479{
 480        const struct v4l2_create_buffers *p = arg;
 481
 482        pr_cont("index=%d, count=%d, memory=%s, ",
 483                        p->index, p->count,
 484                        prt_names(p->memory, v4l2_memory_names));
 485        v4l_print_format(&p->format, write_only);
 486}
 487
 488static void v4l_print_streamparm(const void *arg, bool write_only)
 489{
 490        const struct v4l2_streamparm *p = arg;
 491
 492        pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
 493
 494        if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
 495            p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 496                const struct v4l2_captureparm *c = &p->parm.capture;
 497
 498                pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
 499                        "extendedmode=%d, readbuffers=%d\n",
 500                        c->capability, c->capturemode,
 501                        c->timeperframe.numerator, c->timeperframe.denominator,
 502                        c->extendedmode, c->readbuffers);
 503        } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
 504                   p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 505                const struct v4l2_outputparm *c = &p->parm.output;
 506
 507                pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
 508                        "extendedmode=%d, writebuffers=%d\n",
 509                        c->capability, c->outputmode,
 510                        c->timeperframe.numerator, c->timeperframe.denominator,
 511                        c->extendedmode, c->writebuffers);
 512        } else {
 513                pr_cont("\n");
 514        }
 515}
 516
 517static void v4l_print_queryctrl(const void *arg, bool write_only)
 518{
 519        const struct v4l2_queryctrl *p = arg;
 520
 521        pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
 522                "step=%d, default=%d, flags=0x%08x\n",
 523                        p->id, p->type, (int)sizeof(p->name), p->name,
 524                        p->minimum, p->maximum,
 525                        p->step, p->default_value, p->flags);
 526}
 527
 528static void v4l_print_querymenu(const void *arg, bool write_only)
 529{
 530        const struct v4l2_querymenu *p = arg;
 531
 532        pr_cont("id=0x%x, index=%d\n", p->id, p->index);
 533}
 534
 535static void v4l_print_control(const void *arg, bool write_only)
 536{
 537        const struct v4l2_control *p = arg;
 538
 539        pr_cont("id=0x%x, value=%d\n", p->id, p->value);
 540}
 541
 542static void v4l_print_ext_controls(const void *arg, bool write_only)
 543{
 544        const struct v4l2_ext_controls *p = arg;
 545        int i;
 546
 547        pr_cont("class=0x%x, count=%d, error_idx=%d",
 548                        p->ctrl_class, p->count, p->error_idx);
 549        for (i = 0; i < p->count; i++) {
 550                if (p->controls[i].size)
 551                        pr_cont(", id/val=0x%x/0x%x",
 552                                p->controls[i].id, p->controls[i].value);
 553                else
 554                        pr_cont(", id/size=0x%x/%u",
 555                                p->controls[i].id, p->controls[i].size);
 556        }
 557        pr_cont("\n");
 558}
 559
 560static void v4l_print_cropcap(const void *arg, bool write_only)
 561{
 562        const struct v4l2_cropcap *p = arg;
 563
 564        pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
 565                "defrect wxh=%dx%d, x,y=%d,%d\n, "
 566                "pixelaspect %d/%d\n",
 567                prt_names(p->type, v4l2_type_names),
 568                p->bounds.width, p->bounds.height,
 569                p->bounds.left, p->bounds.top,
 570                p->defrect.width, p->defrect.height,
 571                p->defrect.left, p->defrect.top,
 572                p->pixelaspect.numerator, p->pixelaspect.denominator);
 573}
 574
 575static void v4l_print_crop(const void *arg, bool write_only)
 576{
 577        const struct v4l2_crop *p = arg;
 578
 579        pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
 580                prt_names(p->type, v4l2_type_names),
 581                p->c.width, p->c.height,
 582                p->c.left, p->c.top);
 583}
 584
 585static void v4l_print_selection(const void *arg, bool write_only)
 586{
 587        const struct v4l2_selection *p = arg;
 588
 589        pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
 590                prt_names(p->type, v4l2_type_names),
 591                p->target, p->flags,
 592                p->r.width, p->r.height, p->r.left, p->r.top);
 593}
 594
 595static void v4l_print_jpegcompression(const void *arg, bool write_only)
 596{
 597        const struct v4l2_jpegcompression *p = arg;
 598
 599        pr_cont("quality=%d, APPn=%d, APP_len=%d, "
 600                "COM_len=%d, jpeg_markers=0x%x\n",
 601                p->quality, p->APPn, p->APP_len,
 602                p->COM_len, p->jpeg_markers);
 603}
 604
 605static void v4l_print_enc_idx(const void *arg, bool write_only)
 606{
 607        const struct v4l2_enc_idx *p = arg;
 608
 609        pr_cont("entries=%d, entries_cap=%d\n",
 610                        p->entries, p->entries_cap);
 611}
 612
 613static void v4l_print_encoder_cmd(const void *arg, bool write_only)
 614{
 615        const struct v4l2_encoder_cmd *p = arg;
 616
 617        pr_cont("cmd=%d, flags=0x%x\n",
 618                        p->cmd, p->flags);
 619}
 620
 621static void v4l_print_decoder_cmd(const void *arg, bool write_only)
 622{
 623        const struct v4l2_decoder_cmd *p = arg;
 624
 625        pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
 626
 627        if (p->cmd == V4L2_DEC_CMD_START)
 628                pr_info("speed=%d, format=%u\n",
 629                                p->start.speed, p->start.format);
 630        else if (p->cmd == V4L2_DEC_CMD_STOP)
 631                pr_info("pts=%llu\n", p->stop.pts);
 632}
 633
 634static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
 635{
 636        const struct v4l2_dbg_chip_info *p = arg;
 637
 638        pr_cont("type=%u, ", p->match.type);
 639        if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
 640                pr_cont("name=%.*s, ",
 641                                (int)sizeof(p->match.name), p->match.name);
 642        else
 643                pr_cont("addr=%u, ", p->match.addr);
 644        pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
 645}
 646
 647static void v4l_print_dbg_register(const void *arg, bool write_only)
 648{
 649        const struct v4l2_dbg_register *p = arg;
 650
 651        pr_cont("type=%u, ", p->match.type);
 652        if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
 653                pr_cont("name=%.*s, ",
 654                                (int)sizeof(p->match.name), p->match.name);
 655        else
 656                pr_cont("addr=%u, ", p->match.addr);
 657        pr_cont("reg=0x%llx, val=0x%llx\n",
 658                        p->reg, p->val);
 659}
 660
 661static void v4l_print_dv_timings(const void *arg, bool write_only)
 662{
 663        const struct v4l2_dv_timings *p = arg;
 664
 665        switch (p->type) {
 666        case V4L2_DV_BT_656_1120:
 667                pr_cont("type=bt-656/1120, interlaced=%u, "
 668                        "pixelclock=%llu, "
 669                        "width=%u, height=%u, polarities=0x%x, "
 670                        "hfrontporch=%u, hsync=%u, "
 671                        "hbackporch=%u, vfrontporch=%u, "
 672                        "vsync=%u, vbackporch=%u, "
 673                        "il_vfrontporch=%u, il_vsync=%u, "
 674                        "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
 675                                p->bt.interlaced, p->bt.pixelclock,
 676                                p->bt.width, p->bt.height,
 677                                p->bt.polarities, p->bt.hfrontporch,
 678                                p->bt.hsync, p->bt.hbackporch,
 679                                p->bt.vfrontporch, p->bt.vsync,
 680                                p->bt.vbackporch, p->bt.il_vfrontporch,
 681                                p->bt.il_vsync, p->bt.il_vbackporch,
 682                                p->bt.standards, p->bt.flags);
 683                break;
 684        default:
 685                pr_cont("type=%d\n", p->type);
 686                break;
 687        }
 688}
 689
 690static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
 691{
 692        const struct v4l2_enum_dv_timings *p = arg;
 693
 694        pr_cont("index=%u, ", p->index);
 695        v4l_print_dv_timings(&p->timings, write_only);
 696}
 697
 698static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
 699{
 700        const struct v4l2_dv_timings_cap *p = arg;
 701
 702        switch (p->type) {
 703        case V4L2_DV_BT_656_1120:
 704                pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
 705                        "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
 706                        p->bt.min_width, p->bt.max_width,
 707                        p->bt.min_height, p->bt.max_height,
 708                        p->bt.min_pixelclock, p->bt.max_pixelclock,
 709                        p->bt.standards, p->bt.capabilities);
 710                break;
 711        default:
 712                pr_cont("type=%u\n", p->type);
 713                break;
 714        }
 715}
 716
 717static void v4l_print_frmsizeenum(const void *arg, bool write_only)
 718{
 719        const struct v4l2_frmsizeenum *p = arg;
 720
 721        pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
 722                        p->index,
 723                        (p->pixel_format & 0xff),
 724                        (p->pixel_format >>  8) & 0xff,
 725                        (p->pixel_format >> 16) & 0xff,
 726                        (p->pixel_format >> 24) & 0xff,
 727                        p->type);
 728        switch (p->type) {
 729        case V4L2_FRMSIZE_TYPE_DISCRETE:
 730                pr_cont(", wxh=%ux%u\n",
 731                        p->discrete.width, p->discrete.height);
 732                break;
 733        case V4L2_FRMSIZE_TYPE_STEPWISE:
 734                pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
 735                                p->stepwise.min_width,  p->stepwise.min_height,
 736                                p->stepwise.step_width, p->stepwise.step_height,
 737                                p->stepwise.max_width,  p->stepwise.max_height);
 738                break;
 739        case V4L2_FRMSIZE_TYPE_CONTINUOUS:
 740                /* fall through */
 741        default:
 742                pr_cont("\n");
 743                break;
 744        }
 745}
 746
 747static void v4l_print_frmivalenum(const void *arg, bool write_only)
 748{
 749        const struct v4l2_frmivalenum *p = arg;
 750
 751        pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
 752                        p->index,
 753                        (p->pixel_format & 0xff),
 754                        (p->pixel_format >>  8) & 0xff,
 755                        (p->pixel_format >> 16) & 0xff,
 756                        (p->pixel_format >> 24) & 0xff,
 757                        p->width, p->height, p->type);
 758        switch (p->type) {
 759        case V4L2_FRMIVAL_TYPE_DISCRETE:
 760                pr_cont(", fps=%d/%d\n",
 761                                p->discrete.numerator,
 762                                p->discrete.denominator);
 763                break;
 764        case V4L2_FRMIVAL_TYPE_STEPWISE:
 765                pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
 766                                p->stepwise.min.numerator,
 767                                p->stepwise.min.denominator,
 768                                p->stepwise.max.numerator,
 769                                p->stepwise.max.denominator,
 770                                p->stepwise.step.numerator,
 771                                p->stepwise.step.denominator);
 772                break;
 773        case V4L2_FRMIVAL_TYPE_CONTINUOUS:
 774                /* fall through */
 775        default:
 776                pr_cont("\n");
 777                break;
 778        }
 779}
 780
 781static void v4l_print_event(const void *arg, bool write_only)
 782{
 783        const struct v4l2_event *p = arg;
 784        const struct v4l2_event_ctrl *c;
 785
 786        pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
 787                "timestamp=%lu.%9.9lu\n",
 788                        p->type, p->pending, p->sequence, p->id,
 789                        p->timestamp.tv_sec, p->timestamp.tv_nsec);
 790        switch (p->type) {
 791        case V4L2_EVENT_VSYNC:
 792                printk(KERN_DEBUG "field=%s\n",
 793                        prt_names(p->u.vsync.field, v4l2_field_names));
 794                break;
 795        case V4L2_EVENT_CTRL:
 796                c = &p->u.ctrl;
 797                printk(KERN_DEBUG "changes=0x%x, type=%u, ",
 798                        c->changes, c->type);
 799                if (c->type == V4L2_CTRL_TYPE_INTEGER64)
 800                        pr_cont("value64=%lld, ", c->value64);
 801                else
 802                        pr_cont("value=%d, ", c->value);
 803                pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
 804                        "default_value=%d\n",
 805                        c->flags, c->minimum, c->maximum,
 806                        c->step, c->default_value);
 807                break;
 808        case V4L2_EVENT_FRAME_SYNC:
 809                pr_cont("frame_sequence=%u\n",
 810                        p->u.frame_sync.frame_sequence);
 811                break;
 812        }
 813}
 814
 815static void v4l_print_event_subscription(const void *arg, bool write_only)
 816{
 817        const struct v4l2_event_subscription *p = arg;
 818
 819        pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
 820                        p->type, p->id, p->flags);
 821}
 822
 823static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
 824{
 825        const struct v4l2_sliced_vbi_cap *p = arg;
 826        int i;
 827
 828        pr_cont("type=%s, service_set=0x%08x\n",
 829                        prt_names(p->type, v4l2_type_names), p->service_set);
 830        for (i = 0; i < 24; i++)
 831                printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
 832                                p->service_lines[0][i],
 833                                p->service_lines[1][i]);
 834}
 835
 836static void v4l_print_freq_band(const void *arg, bool write_only)
 837{
 838        const struct v4l2_frequency_band *p = arg;
 839
 840        pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
 841                "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
 842                        p->tuner, p->type, p->index,
 843                        p->capability, p->rangelow,
 844                        p->rangehigh, p->modulation);
 845}
 846
 847static void v4l_print_edid(const void *arg, bool write_only)
 848{
 849        const struct v4l2_edid *p = arg;
 850
 851        pr_cont("pad=%u, start_block=%u, blocks=%u\n",
 852                p->pad, p->start_block, p->blocks);
 853}
 854
 855static void v4l_print_u32(const void *arg, bool write_only)
 856{
 857        pr_cont("value=%u\n", *(const u32 *)arg);
 858}
 859
 860static void v4l_print_newline(const void *arg, bool write_only)
 861{
 862        pr_cont("\n");
 863}
 864
 865static void v4l_print_default(const void *arg, bool write_only)
 866{
 867        pr_cont("driver-specific ioctl\n");
 868}
 869
 870static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
 871{
 872        __u32 i;
 873
 874        /* zero the reserved fields */
 875        c->reserved[0] = c->reserved[1] = 0;
 876        for (i = 0; i < c->count; i++)
 877                c->controls[i].reserved2[0] = 0;
 878
 879        /* V4L2_CID_PRIVATE_BASE cannot be used as control class
 880           when using extended controls.
 881           Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
 882           is it allowed for backwards compatibility.
 883         */
 884        if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
 885                return 0;
 886        /* Check that all controls are from the same control class. */
 887        for (i = 0; i < c->count; i++) {
 888                if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
 889                        c->error_idx = i;
 890                        return 0;
 891                }
 892        }
 893        return 1;
 894}
 895
 896static int check_fmt(struct file *file, enum v4l2_buf_type type)
 897{
 898        struct video_device *vfd = video_devdata(file);
 899        const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
 900        bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
 901        bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
 902        bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
 903        bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
 904        bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
 905
 906        if (ops == NULL)
 907                return -EINVAL;
 908
 909        switch (type) {
 910        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 911                if (is_vid && is_rx &&
 912                    (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
 913                        return 0;
 914                break;
 915        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 916                if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
 917                        return 0;
 918                break;
 919        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 920                if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
 921                        return 0;
 922                break;
 923        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 924                if (is_vid && is_tx &&
 925                    (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
 926                        return 0;
 927                break;
 928        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 929                if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
 930                        return 0;
 931                break;
 932        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 933                if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
 934                        return 0;
 935                break;
 936        case V4L2_BUF_TYPE_VBI_CAPTURE:
 937                if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
 938                        return 0;
 939                break;
 940        case V4L2_BUF_TYPE_VBI_OUTPUT:
 941                if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
 942                        return 0;
 943                break;
 944        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 945                if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
 946                        return 0;
 947                break;
 948        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 949                if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
 950                        return 0;
 951                break;
 952        case V4L2_BUF_TYPE_SDR_CAPTURE:
 953                if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
 954                        return 0;
 955                break;
 956        default:
 957                break;
 958        }
 959        return -EINVAL;
 960}
 961
 962static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
 963                                struct file *file, void *fh, void *arg)
 964{
 965        struct v4l2_capability *cap = (struct v4l2_capability *)arg;
 966
 967        cap->version = LINUX_VERSION_CODE;
 968        return ops->vidioc_querycap(file, fh, cap);
 969}
 970
 971static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
 972                                struct file *file, void *fh, void *arg)
 973{
 974        return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
 975}
 976
 977static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
 978                                struct file *file, void *fh, void *arg)
 979{
 980        return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
 981}
 982
 983static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
 984                                struct file *file, void *fh, void *arg)
 985{
 986        struct video_device *vfd;
 987        u32 *p = arg;
 988
 989        if (ops->vidioc_g_priority)
 990                return ops->vidioc_g_priority(file, fh, arg);
 991        vfd = video_devdata(file);
 992        *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
 993        return 0;
 994}
 995
 996static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
 997                                struct file *file, void *fh, void *arg)
 998{
 999        struct video_device *vfd;
1000        struct v4l2_fh *vfh;
1001        u32 *p = arg;
1002
1003        if (ops->vidioc_s_priority)
1004                return ops->vidioc_s_priority(file, fh, *p);
1005        vfd = video_devdata(file);
1006        vfh = file->private_data;
1007        return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1008}
1009
1010static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1011                                struct file *file, void *fh, void *arg)
1012{
1013        struct video_device *vfd = video_devdata(file);
1014        struct v4l2_input *p = arg;
1015
1016        /*
1017         * We set the flags for CAP_DV_TIMINGS &
1018         * CAP_STD here based on ioctl handler provided by the
1019         * driver. If the driver doesn't support these
1020         * for a specific input, it must override these flags.
1021         */
1022        if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1023                p->capabilities |= V4L2_IN_CAP_STD;
1024
1025        return ops->vidioc_enum_input(file, fh, p);
1026}
1027
1028static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1029                                struct file *file, void *fh, void *arg)
1030{
1031        struct video_device *vfd = video_devdata(file);
1032        struct v4l2_output *p = arg;
1033
1034        /*
1035         * We set the flags for CAP_DV_TIMINGS &
1036         * CAP_STD here based on ioctl handler provided by the
1037         * driver. If the driver doesn't support these
1038         * for a specific output, it must override these flags.
1039         */
1040        if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1041                p->capabilities |= V4L2_OUT_CAP_STD;
1042
1043        return ops->vidioc_enum_output(file, fh, p);
1044}
1045
1046static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1047                                struct file *file, void *fh, void *arg)
1048{
1049        struct v4l2_fmtdesc *p = arg;
1050        struct video_device *vfd = video_devdata(file);
1051        bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1052        bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1053
1054        switch (p->type) {
1055        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1056                if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap))
1057                        break;
1058                return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1059        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1060                if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap_mplane))
1061                        break;
1062                return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1063        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1064                if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_overlay))
1065                        break;
1066                return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1067        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1068                if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out))
1069                        break;
1070                return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1071        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1072                if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
1073                        break;
1074                return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1075        case V4L2_BUF_TYPE_SDR_CAPTURE:
1076                if (unlikely(!is_rx || !ops->vidioc_enum_fmt_sdr_cap))
1077                        break;
1078                return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1079        }
1080        return -EINVAL;
1081}
1082
1083static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1084                                struct file *file, void *fh, void *arg)
1085{
1086        struct v4l2_format *p = arg;
1087        struct video_device *vfd = video_devdata(file);
1088        bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1089        bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1090        bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1091        bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1092
1093        switch (p->type) {
1094        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1095                if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
1096                        break;
1097                return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1098        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1099                if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
1100                        break;
1101                return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1102        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1103                if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
1104                        break;
1105                return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1106        case V4L2_BUF_TYPE_VBI_CAPTURE:
1107                if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1108                        break;
1109                return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1110        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1111                if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1112                        break;
1113                return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1114        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1115                if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
1116                        break;
1117                return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1118        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1119                if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
1120                        break;
1121                return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1122        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1123                if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
1124                        break;
1125                return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1126        case V4L2_BUF_TYPE_VBI_OUTPUT:
1127                if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
1128                        break;
1129                return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1130        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1131                if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
1132                        break;
1133                return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1134        case V4L2_BUF_TYPE_SDR_CAPTURE:
1135                if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
1136                        break;
1137                return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1138        }
1139        return -EINVAL;
1140}
1141
1142static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1143                                struct file *file, void *fh, void *arg)
1144{
1145        struct v4l2_format *p = arg;
1146        struct video_device *vfd = video_devdata(file);
1147        bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1148        bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1149        bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1150        bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1151
1152        switch (p->type) {
1153        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1154                if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
1155                        break;
1156                CLEAR_AFTER_FIELD(p, fmt.pix);
1157                return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1158        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1159                if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
1160                        break;
1161                CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1162                return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1163        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1164                if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
1165                        break;
1166                CLEAR_AFTER_FIELD(p, fmt.win);
1167                return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1168        case V4L2_BUF_TYPE_VBI_CAPTURE:
1169                if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1170                        break;
1171                CLEAR_AFTER_FIELD(p, fmt.vbi);
1172                return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1173        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1174                if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1175                        break;
1176                CLEAR_AFTER_FIELD(p, fmt.sliced);
1177                return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1178        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1179                if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
1180                        break;
1181                CLEAR_AFTER_FIELD(p, fmt.pix);
1182                return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1183        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1184                if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
1185                        break;
1186                CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1187                return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1188        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1189                if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
1190                        break;
1191                CLEAR_AFTER_FIELD(p, fmt.win);
1192                return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1193        case V4L2_BUF_TYPE_VBI_OUTPUT:
1194                if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
1195                        break;
1196                CLEAR_AFTER_FIELD(p, fmt.vbi);
1197                return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1198        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1199                if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
1200                        break;
1201                CLEAR_AFTER_FIELD(p, fmt.sliced);
1202                return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1203        case V4L2_BUF_TYPE_SDR_CAPTURE:
1204                if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
1205                        break;
1206                CLEAR_AFTER_FIELD(p, fmt.sdr);
1207                return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1208        }
1209        return -EINVAL;
1210}
1211
1212static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1213                                struct file *file, void *fh, void *arg)
1214{
1215        struct v4l2_format *p = arg;
1216        struct video_device *vfd = video_devdata(file);
1217        bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1218        bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1219        bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1220        bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1221
1222        switch (p->type) {
1223        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1224                if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
1225                        break;
1226                CLEAR_AFTER_FIELD(p, fmt.pix);
1227                return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1228        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1229                if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
1230                        break;
1231                CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1232                return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1233        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1234                if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
1235                        break;
1236                CLEAR_AFTER_FIELD(p, fmt.win);
1237                return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1238        case V4L2_BUF_TYPE_VBI_CAPTURE:
1239                if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1240                        break;
1241                CLEAR_AFTER_FIELD(p, fmt.vbi);
1242                return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1243        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1244                if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1245                        break;
1246                CLEAR_AFTER_FIELD(p, fmt.sliced);
1247                return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1248        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1249                if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
1250                        break;
1251                CLEAR_AFTER_FIELD(p, fmt.pix);
1252                return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1253        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1254                if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
1255                        break;
1256                CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1257                return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1258        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1259                if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
1260                        break;
1261                CLEAR_AFTER_FIELD(p, fmt.win);
1262                return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1263        case V4L2_BUF_TYPE_VBI_OUTPUT:
1264                if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
1265                        break;
1266                CLEAR_AFTER_FIELD(p, fmt.vbi);
1267                return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1268        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1269                if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
1270                        break;
1271                CLEAR_AFTER_FIELD(p, fmt.sliced);
1272                return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1273        case V4L2_BUF_TYPE_SDR_CAPTURE:
1274                if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
1275                        break;
1276                CLEAR_AFTER_FIELD(p, fmt.sdr);
1277                return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1278        }
1279        return -EINVAL;
1280}
1281
1282static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1283                                struct file *file, void *fh, void *arg)
1284{
1285        return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1286}
1287
1288static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1289                                struct file *file, void *fh, void *arg)
1290{
1291        return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1292}
1293
1294static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1295                                struct file *file, void *fh, void *arg)
1296{
1297        struct video_device *vfd = video_devdata(file);
1298        struct v4l2_tuner *p = arg;
1299        int err;
1300
1301        p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1302                        V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1303        err = ops->vidioc_g_tuner(file, fh, p);
1304        if (!err)
1305                p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1306        return err;
1307}
1308
1309static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1310                                struct file *file, void *fh, void *arg)
1311{
1312        struct video_device *vfd = video_devdata(file);
1313        struct v4l2_tuner *p = arg;
1314
1315        p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1316                        V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1317        return ops->vidioc_s_tuner(file, fh, p);
1318}
1319
1320static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1321                                struct file *file, void *fh, void *arg)
1322{
1323        struct v4l2_modulator *p = arg;
1324        int err;
1325
1326        err = ops->vidioc_g_modulator(file, fh, p);
1327        if (!err)
1328                p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1329        return err;
1330}
1331
1332static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1333                                struct file *file, void *fh, void *arg)
1334{
1335        struct video_device *vfd = video_devdata(file);
1336        struct v4l2_frequency *p = arg;
1337
1338        if (vfd->vfl_type == VFL_TYPE_SDR)
1339                p->type = V4L2_TUNER_ADC;
1340        else
1341                p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1342                                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1343        return ops->vidioc_g_frequency(file, fh, p);
1344}
1345
1346static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1347                                struct file *file, void *fh, void *arg)
1348{
1349        struct video_device *vfd = video_devdata(file);
1350        const struct v4l2_frequency *p = arg;
1351        enum v4l2_tuner_type type;
1352
1353        if (vfd->vfl_type == VFL_TYPE_SDR) {
1354                if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1355                        return -EINVAL;
1356        } else {
1357                type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1358                                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1359                if (type != p->type)
1360                        return -EINVAL;
1361        }
1362        return ops->vidioc_s_frequency(file, fh, p);
1363}
1364
1365static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1366                                struct file *file, void *fh, void *arg)
1367{
1368        struct video_device *vfd = video_devdata(file);
1369        struct v4l2_standard *p = arg;
1370        v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1371        unsigned int index = p->index, i, j = 0;
1372        const char *descr = "";
1373
1374        /* Return -ENODATA if the tvnorms for the current input
1375           or output is 0, meaning that it doesn't support this API. */
1376        if (id == 0)
1377                return -ENODATA;
1378
1379        /* Return norm array in a canonical way */
1380        for (i = 0; i <= index && id; i++) {
1381                /* last std value in the standards array is 0, so this
1382                   while always ends there since (id & 0) == 0. */
1383                while ((id & standards[j].std) != standards[j].std)
1384                        j++;
1385                curr_id = standards[j].std;
1386                descr = standards[j].descr;
1387                j++;
1388                if (curr_id == 0)
1389                        break;
1390                if (curr_id != V4L2_STD_PAL &&
1391                                curr_id != V4L2_STD_SECAM &&
1392                                curr_id != V4L2_STD_NTSC)
1393                        id &= ~curr_id;
1394        }
1395        if (i <= index)
1396                return -EINVAL;
1397
1398        v4l2_video_std_construct(p, curr_id, descr);
1399        return 0;
1400}
1401
1402static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1403                                struct file *file, void *fh, void *arg)
1404{
1405        struct video_device *vfd = video_devdata(file);
1406        v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1407
1408        norm = id & vfd->tvnorms;
1409        if (vfd->tvnorms && !norm)      /* Check if std is supported */
1410                return -EINVAL;
1411
1412        /* Calls the specific handler */
1413        return ops->vidioc_s_std(file, fh, norm);
1414}
1415
1416static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1417                                struct file *file, void *fh, void *arg)
1418{
1419        struct video_device *vfd = video_devdata(file);
1420        v4l2_std_id *p = arg;
1421
1422        /*
1423         * If no signal is detected, then the driver should return
1424         * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1425         * any standards that do not apply removed.
1426         *
1427         * This means that tuners, audio and video decoders can join
1428         * their efforts to improve the standards detection.
1429         */
1430        *p = vfd->tvnorms;
1431        return ops->vidioc_querystd(file, fh, arg);
1432}
1433
1434static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1435                                struct file *file, void *fh, void *arg)
1436{
1437        struct video_device *vfd = video_devdata(file);
1438        struct v4l2_hw_freq_seek *p = arg;
1439        enum v4l2_tuner_type type;
1440
1441        /* s_hw_freq_seek is not supported for SDR for now */
1442        if (vfd->vfl_type == VFL_TYPE_SDR)
1443                return -EINVAL;
1444
1445        type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1446                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1447        if (p->type != type)
1448                return -EINVAL;
1449        return ops->vidioc_s_hw_freq_seek(file, fh, p);
1450}
1451
1452static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1453                                struct file *file, void *fh, void *arg)
1454{
1455        return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1456}
1457
1458static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1459                                struct file *file, void *fh, void *arg)
1460{
1461        struct v4l2_requestbuffers *p = arg;
1462        int ret = check_fmt(file, p->type);
1463
1464        if (ret)
1465                return ret;
1466
1467        CLEAR_AFTER_FIELD(p, memory);
1468
1469        return ops->vidioc_reqbufs(file, fh, p);
1470}
1471
1472static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1473                                struct file *file, void *fh, void *arg)
1474{
1475        struct v4l2_buffer *p = arg;
1476        int ret = check_fmt(file, p->type);
1477
1478        return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1479}
1480
1481static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1482                                struct file *file, void *fh, void *arg)
1483{
1484        struct v4l2_buffer *p = arg;
1485        int ret = check_fmt(file, p->type);
1486
1487        return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1488}
1489
1490static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1491                                struct file *file, void *fh, void *arg)
1492{
1493        struct v4l2_buffer *p = arg;
1494        int ret = check_fmt(file, p->type);
1495
1496        return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1497}
1498
1499static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1500                                struct file *file, void *fh, void *arg)
1501{
1502        struct v4l2_create_buffers *create = arg;
1503        int ret = check_fmt(file, create->format.type);
1504
1505        return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1506}
1507
1508static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1509                                struct file *file, void *fh, void *arg)
1510{
1511        struct v4l2_buffer *b = arg;
1512        int ret = check_fmt(file, b->type);
1513
1514        return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1515}
1516
1517static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1518                                struct file *file, void *fh, void *arg)
1519{
1520        struct v4l2_streamparm *p = arg;
1521        v4l2_std_id std;
1522        int ret = check_fmt(file, p->type);
1523
1524        if (ret)
1525                return ret;
1526        if (ops->vidioc_g_parm)
1527                return ops->vidioc_g_parm(file, fh, p);
1528        if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1529            p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1530                return -EINVAL;
1531        p->parm.capture.readbuffers = 2;
1532        ret = ops->vidioc_g_std(file, fh, &std);
1533        if (ret == 0)
1534                v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
1535        return ret;
1536}
1537
1538static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1539                                struct file *file, void *fh, void *arg)
1540{
1541        struct v4l2_streamparm *p = arg;
1542        int ret = check_fmt(file, p->type);
1543
1544        return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1545}
1546
1547static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1548                                struct file *file, void *fh, void *arg)
1549{
1550        struct video_device *vfd = video_devdata(file);
1551        struct v4l2_queryctrl *p = arg;
1552        struct v4l2_fh *vfh =
1553                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1554
1555        if (vfh && vfh->ctrl_handler)
1556                return v4l2_queryctrl(vfh->ctrl_handler, p);
1557        if (vfd->ctrl_handler)
1558                return v4l2_queryctrl(vfd->ctrl_handler, p);
1559        if (ops->vidioc_queryctrl)
1560                return ops->vidioc_queryctrl(file, fh, p);
1561        return -ENOTTY;
1562}
1563
1564static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1565                                struct file *file, void *fh, void *arg)
1566{
1567        struct video_device *vfd = video_devdata(file);
1568        struct v4l2_querymenu *p = arg;
1569        struct v4l2_fh *vfh =
1570                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1571
1572        if (vfh && vfh->ctrl_handler)
1573                return v4l2_querymenu(vfh->ctrl_handler, p);
1574        if (vfd->ctrl_handler)
1575                return v4l2_querymenu(vfd->ctrl_handler, p);
1576        if (ops->vidioc_querymenu)
1577                return ops->vidioc_querymenu(file, fh, p);
1578        return -ENOTTY;
1579}
1580
1581static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1582                                struct file *file, void *fh, void *arg)
1583{
1584        struct video_device *vfd = video_devdata(file);
1585        struct v4l2_control *p = arg;
1586        struct v4l2_fh *vfh =
1587                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1588        struct v4l2_ext_controls ctrls;
1589        struct v4l2_ext_control ctrl;
1590
1591        if (vfh && vfh->ctrl_handler)
1592                return v4l2_g_ctrl(vfh->ctrl_handler, p);
1593        if (vfd->ctrl_handler)
1594                return v4l2_g_ctrl(vfd->ctrl_handler, p);
1595        if (ops->vidioc_g_ctrl)
1596                return ops->vidioc_g_ctrl(file, fh, p);
1597        if (ops->vidioc_g_ext_ctrls == NULL)
1598                return -ENOTTY;
1599
1600        ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1601        ctrls.count = 1;
1602        ctrls.controls = &ctrl;
1603        ctrl.id = p->id;
1604        ctrl.value = p->value;
1605        if (check_ext_ctrls(&ctrls, 1)) {
1606                int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1607
1608                if (ret == 0)
1609                        p->value = ctrl.value;
1610                return ret;
1611        }
1612        return -EINVAL;
1613}
1614
1615static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1616                                struct file *file, void *fh, void *arg)
1617{
1618        struct video_device *vfd = video_devdata(file);
1619        struct v4l2_control *p = arg;
1620        struct v4l2_fh *vfh =
1621                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1622        struct v4l2_ext_controls ctrls;
1623        struct v4l2_ext_control ctrl;
1624
1625        if (vfh && vfh->ctrl_handler)
1626                return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1627        if (vfd->ctrl_handler)
1628                return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1629        if (ops->vidioc_s_ctrl)
1630                return ops->vidioc_s_ctrl(file, fh, p);
1631        if (ops->vidioc_s_ext_ctrls == NULL)
1632                return -ENOTTY;
1633
1634        ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1635        ctrls.count = 1;
1636        ctrls.controls = &ctrl;
1637        ctrl.id = p->id;
1638        ctrl.value = p->value;
1639        if (check_ext_ctrls(&ctrls, 1))
1640                return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1641        return -EINVAL;
1642}
1643
1644static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1645                                struct file *file, void *fh, void *arg)
1646{
1647        struct video_device *vfd = video_devdata(file);
1648        struct v4l2_ext_controls *p = arg;
1649        struct v4l2_fh *vfh =
1650                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1651
1652        p->error_idx = p->count;
1653        if (vfh && vfh->ctrl_handler)
1654                return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1655        if (vfd->ctrl_handler)
1656                return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1657        if (ops->vidioc_g_ext_ctrls == NULL)
1658                return -ENOTTY;
1659        return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1660                                        -EINVAL;
1661}
1662
1663static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1664                                struct file *file, void *fh, void *arg)
1665{
1666        struct video_device *vfd = video_devdata(file);
1667        struct v4l2_ext_controls *p = arg;
1668        struct v4l2_fh *vfh =
1669                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1670
1671        p->error_idx = p->count;
1672        if (vfh && vfh->ctrl_handler)
1673                return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1674        if (vfd->ctrl_handler)
1675                return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1676        if (ops->vidioc_s_ext_ctrls == NULL)
1677                return -ENOTTY;
1678        return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1679                                        -EINVAL;
1680}
1681
1682static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1683                                struct file *file, void *fh, void *arg)
1684{
1685        struct video_device *vfd = video_devdata(file);
1686        struct v4l2_ext_controls *p = arg;
1687        struct v4l2_fh *vfh =
1688                test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1689
1690        p->error_idx = p->count;
1691        if (vfh && vfh->ctrl_handler)
1692                return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1693        if (vfd->ctrl_handler)
1694                return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1695        if (ops->vidioc_try_ext_ctrls == NULL)
1696                return -ENOTTY;
1697        return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1698                                        -EINVAL;
1699}
1700
1701static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1702                                struct file *file, void *fh, void *arg)
1703{
1704        struct v4l2_crop *p = arg;
1705        struct v4l2_selection s = {
1706                .type = p->type,
1707        };
1708        int ret;
1709
1710        if (ops->vidioc_g_crop)
1711                return ops->vidioc_g_crop(file, fh, p);
1712        /* simulate capture crop using selection api */
1713
1714        /* crop means compose for output devices */
1715        if (V4L2_TYPE_IS_OUTPUT(p->type))
1716                s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1717        else
1718                s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1719
1720        ret = ops->vidioc_g_selection(file, fh, &s);
1721
1722        /* copying results to old structure on success */
1723        if (!ret)
1724                p->c = s.r;
1725        return ret;
1726}
1727
1728static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1729                                struct file *file, void *fh, void *arg)
1730{
1731        struct v4l2_crop *p = arg;
1732        struct v4l2_selection s = {
1733                .type = p->type,
1734                .r = p->c,
1735        };
1736
1737        if (ops->vidioc_s_crop)
1738                return ops->vidioc_s_crop(file, fh, p);
1739        /* simulate capture crop using selection api */
1740
1741        /* crop means compose for output devices */
1742        if (V4L2_TYPE_IS_OUTPUT(p->type))
1743                s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1744        else
1745                s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1746
1747        return ops->vidioc_s_selection(file, fh, &s);
1748}
1749
1750static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1751                                struct file *file, void *fh, void *arg)
1752{
1753        struct v4l2_cropcap *p = arg;
1754        struct v4l2_selection s = { .type = p->type };
1755        int ret;
1756
1757        if (ops->vidioc_cropcap)
1758                return ops->vidioc_cropcap(file, fh, p);
1759
1760        /* obtaining bounds */
1761        if (V4L2_TYPE_IS_OUTPUT(p->type))
1762                s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1763        else
1764                s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1765
1766        ret = ops->vidioc_g_selection(file, fh, &s);
1767        if (ret)
1768                return ret;
1769        p->bounds = s.r;
1770
1771        /* obtaining defrect */
1772        if (V4L2_TYPE_IS_OUTPUT(p->type))
1773                s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1774        else
1775                s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1776
1777        ret = ops->vidioc_g_selection(file, fh, &s);
1778        if (ret)
1779                return ret;
1780        p->defrect = s.r;
1781
1782        /* setting trivial pixelaspect */
1783        p->pixelaspect.numerator = 1;
1784        p->pixelaspect.denominator = 1;
1785        return 0;
1786}
1787
1788static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1789                                struct file *file, void *fh, void *arg)
1790{
1791        struct video_device *vfd = video_devdata(file);
1792        int ret;
1793
1794        if (vfd->v4l2_dev)
1795                pr_info("%s: =================  START STATUS  =================\n",
1796                        vfd->v4l2_dev->name);
1797        ret = ops->vidioc_log_status(file, fh);
1798        if (vfd->v4l2_dev)
1799                pr_info("%s: ==================  END STATUS  ==================\n",
1800                        vfd->v4l2_dev->name);
1801        return ret;
1802}
1803
1804static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1805                                struct file *file, void *fh, void *arg)
1806{
1807#ifdef CONFIG_VIDEO_ADV_DEBUG
1808        struct v4l2_dbg_register *p = arg;
1809        struct video_device *vfd = video_devdata(file);
1810        struct v4l2_subdev *sd;
1811        int idx = 0;
1812
1813        if (!capable(CAP_SYS_ADMIN))
1814                return -EPERM;
1815        if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1816                if (vfd->v4l2_dev == NULL)
1817                        return -EINVAL;
1818                v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1819                        if (p->match.addr == idx++)
1820                                return v4l2_subdev_call(sd, core, g_register, p);
1821                return -EINVAL;
1822        }
1823        if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1824            (ops->vidioc_g_chip_info || p->match.addr == 0))
1825                return ops->vidioc_g_register(file, fh, p);
1826        return -EINVAL;
1827#else
1828        return -ENOTTY;
1829#endif
1830}
1831
1832static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1833                                struct file *file, void *fh, void *arg)
1834{
1835#ifdef CONFIG_VIDEO_ADV_DEBUG
1836        const struct v4l2_dbg_register *p = arg;
1837        struct video_device *vfd = video_devdata(file);
1838        struct v4l2_subdev *sd;
1839        int idx = 0;
1840
1841        if (!capable(CAP_SYS_ADMIN))
1842                return -EPERM;
1843        if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1844                if (vfd->v4l2_dev == NULL)
1845                        return -EINVAL;
1846                v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1847                        if (p->match.addr == idx++)
1848                                return v4l2_subdev_call(sd, core, s_register, p);
1849                return -EINVAL;
1850        }
1851        if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1852            (ops->vidioc_g_chip_info || p->match.addr == 0))
1853                return ops->vidioc_s_register(file, fh, p);
1854        return -EINVAL;
1855#else
1856        return -ENOTTY;
1857#endif
1858}
1859
1860static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
1861                                struct file *file, void *fh, void *arg)
1862{
1863#ifdef CONFIG_VIDEO_ADV_DEBUG
1864        struct video_device *vfd = video_devdata(file);
1865        struct v4l2_dbg_chip_info *p = arg;
1866        struct v4l2_subdev *sd;
1867        int idx = 0;
1868
1869        switch (p->match.type) {
1870        case V4L2_CHIP_MATCH_BRIDGE:
1871                if (ops->vidioc_s_register)
1872                        p->flags |= V4L2_CHIP_FL_WRITABLE;
1873                if (ops->vidioc_g_register)
1874                        p->flags |= V4L2_CHIP_FL_READABLE;
1875                strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
1876                if (ops->vidioc_g_chip_info)
1877                        return ops->vidioc_g_chip_info(file, fh, arg);
1878                if (p->match.addr)
1879                        return -EINVAL;
1880                return 0;
1881
1882        case V4L2_CHIP_MATCH_SUBDEV:
1883                if (vfd->v4l2_dev == NULL)
1884                        break;
1885                v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
1886                        if (p->match.addr != idx++)
1887                                continue;
1888                        if (sd->ops->core && sd->ops->core->s_register)
1889                                p->flags |= V4L2_CHIP_FL_WRITABLE;
1890                        if (sd->ops->core && sd->ops->core->g_register)
1891                                p->flags |= V4L2_CHIP_FL_READABLE;
1892                        strlcpy(p->name, sd->name, sizeof(p->name));
1893                        return 0;
1894                }
1895                break;
1896        }
1897        return -EINVAL;
1898#else
1899        return -ENOTTY;
1900#endif
1901}
1902
1903static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1904                                struct file *file, void *fh, void *arg)
1905{
1906        return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1907}
1908
1909static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1910                                struct file *file, void *fh, void *arg)
1911{
1912        return ops->vidioc_subscribe_event(fh, arg);
1913}
1914
1915static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1916                                struct file *file, void *fh, void *arg)
1917{
1918        return ops->vidioc_unsubscribe_event(fh, arg);
1919}
1920
1921static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1922                                struct file *file, void *fh, void *arg)
1923{
1924        struct v4l2_sliced_vbi_cap *p = arg;
1925        int ret = check_fmt(file, p->type);
1926
1927        if (ret)
1928                return ret;
1929
1930        /* Clear up to type, everything after type is zeroed already */
1931        memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1932
1933        return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1934}
1935
1936static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1937                                struct file *file, void *fh, void *arg)
1938{
1939        struct video_device *vfd = video_devdata(file);
1940        struct v4l2_frequency_band *p = arg;
1941        enum v4l2_tuner_type type;
1942        int err;
1943
1944        if (vfd->vfl_type == VFL_TYPE_SDR) {
1945                if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1946                        return -EINVAL;
1947                type = p->type;
1948        } else {
1949                type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1950                                V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1951                if (type != p->type)
1952                        return -EINVAL;
1953        }
1954        if (ops->vidioc_enum_freq_bands)
1955                return ops->vidioc_enum_freq_bands(file, fh, p);
1956        if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
1957                struct v4l2_tuner t = {
1958                        .index = p->tuner,
1959                        .type = type,
1960                };
1961
1962                if (p->index)
1963                        return -EINVAL;
1964                err = ops->vidioc_g_tuner(file, fh, &t);
1965                if (err)
1966                        return err;
1967                p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1968                p->rangelow = t.rangelow;
1969                p->rangehigh = t.rangehigh;
1970                p->modulation = (type == V4L2_TUNER_RADIO) ?
1971                        V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1972                return 0;
1973        }
1974        if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
1975                struct v4l2_modulator m = {
1976                        .index = p->tuner,
1977                };
1978
1979                if (type != V4L2_TUNER_RADIO)
1980                        return -EINVAL;
1981                if (p->index)
1982                        return -EINVAL;
1983                err = ops->vidioc_g_modulator(file, fh, &m);
1984                if (err)
1985                        return err;
1986                p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1987                p->rangelow = m.rangelow;
1988                p->rangehigh = m.rangehigh;
1989                p->modulation = (type == V4L2_TUNER_RADIO) ?
1990                        V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1991                return 0;
1992        }
1993        return -ENOTTY;
1994}
1995
1996struct v4l2_ioctl_info {
1997        unsigned int ioctl;
1998        u32 flags;
1999        const char * const name;
2000        union {
2001                u32 offset;
2002                int (*func)(const struct v4l2_ioctl_ops *ops,
2003                                struct file *file, void *fh, void *p);
2004        } u;
2005        void (*debug)(const void *arg, bool write_only);
2006};
2007
2008/* This control needs a priority check */
2009#define INFO_FL_PRIO    (1 << 0)
2010/* This control can be valid if the filehandle passes a control handler. */
2011#define INFO_FL_CTRL    (1 << 1)
2012/* This is a standard ioctl, no need for special code */
2013#define INFO_FL_STD     (1 << 2)
2014/* This is ioctl has its own function */
2015#define INFO_FL_FUNC    (1 << 3)
2016/* Queuing ioctl */
2017#define INFO_FL_QUEUE   (1 << 4)
2018/* Zero struct from after the field to the end */
2019#define INFO_FL_CLEAR(v4l2_struct, field)                       \
2020        ((offsetof(struct v4l2_struct, field) +                 \
2021          sizeof(((struct v4l2_struct *)0)->field)) << 16)
2022#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
2023
2024#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags)                 \
2025        [_IOC_NR(_ioctl)] = {                                           \
2026                .ioctl = _ioctl,                                        \
2027                .flags = _flags | INFO_FL_STD,                          \
2028                .name = #_ioctl,                                        \
2029                .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc),   \
2030                .debug = _debug,                                        \
2031        }
2032
2033#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags)                   \
2034        [_IOC_NR(_ioctl)] = {                                           \
2035                .ioctl = _ioctl,                                        \
2036                .flags = _flags | INFO_FL_FUNC,                         \
2037                .name = #_ioctl,                                        \
2038                .u.func = _func,                                        \
2039                .debug = _debug,                                        \
2040        }
2041
2042static struct v4l2_ioctl_info v4l2_ioctls[] = {
2043        IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2044        IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2045        IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
2046        IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2047        IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2048        IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2049        IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
2050        IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2051        IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2052        IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2053        IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2054        IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2055        IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2056        IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2057        IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2058        IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2059        IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
2060        IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2061        IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2062        IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2063        IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2064        IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2065        IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2066        IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2067        IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2068        IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
2069        IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2070        IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2071        IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2072        IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2073        IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, INFO_FL_CLEAR(v4l2_edid, edid)),
2074        IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_edid, edid)),
2075        IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2076        IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2077        IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2078        IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2079        IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2080        IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2081        IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2082        IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2083        IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2084        IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2085        IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2086        IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2087        IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2088        IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
2089        IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2090        IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2091        IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2092        IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2093        IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2094        IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2095        IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2096        IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2097        IOCTL_INFO_FNC(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
2098        IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2099        IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2100        IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2101        IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2102        IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2103        IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2104        IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2105        IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2106        IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2107        IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2108        IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2109        IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2110        IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2111        IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2112        IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2113        IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
2114        IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2115        IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2116        IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2117        IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2118        IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2119        IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2120        IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
2121        IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
2122        IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2123        IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
2124};
2125#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2126
2127bool v4l2_is_known_ioctl(unsigned int cmd)
2128{
2129        if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2130                return false;
2131        return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2132}
2133
2134struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2135{
2136        if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2137                return vdev->lock;
2138        if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2139                return NULL;
2140        if (vdev->queue && vdev->queue->lock &&
2141                        (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2142                return vdev->queue->lock;
2143        return vdev->lock;
2144}
2145
2146/* Common ioctl debug function. This function can be used by
2147   external ioctl messages as well as internal V4L ioctl */
2148void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2149{
2150        const char *dir, *type;
2151
2152        if (prefix)
2153                printk(KERN_DEBUG "%s: ", prefix);
2154
2155        switch (_IOC_TYPE(cmd)) {
2156        case 'd':
2157                type = "v4l2_int";
2158                break;
2159        case 'V':
2160                if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2161                        type = "v4l2";
2162                        break;
2163                }
2164                pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2165                return;
2166        default:
2167                type = "unknown";
2168                break;
2169        }
2170
2171        switch (_IOC_DIR(cmd)) {
2172        case _IOC_NONE:              dir = "--"; break;
2173        case _IOC_READ:              dir = "r-"; break;
2174        case _IOC_WRITE:             dir = "-w"; break;
2175        case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2176        default:                     dir = "*ERR*"; break;
2177        }
2178        pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2179                type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2180}
2181EXPORT_SYMBOL(v4l_printk_ioctl);
2182
2183static long __video_do_ioctl(struct file *file,
2184                unsigned int cmd, void *arg)
2185{
2186        struct video_device *vfd = video_devdata(file);
2187        const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2188        bool write_only = false;
2189        struct v4l2_ioctl_info default_info;
2190        const struct v4l2_ioctl_info *info;
2191        void *fh = file->private_data;
2192        struct v4l2_fh *vfh = NULL;
2193        int use_fh_prio = 0;
2194        int debug = vfd->debug;
2195        long ret = -ENOTTY;
2196
2197        if (ops == NULL) {
2198                pr_warn("%s: has no ioctl_ops.\n",
2199                                video_device_node_name(vfd));
2200                return ret;
2201        }
2202
2203        if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2204                vfh = file->private_data;
2205                use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2206        }
2207
2208        if (v4l2_is_known_ioctl(cmd)) {
2209                info = &v4l2_ioctls[_IOC_NR(cmd)];
2210
2211                if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2212                    !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2213                        goto done;
2214
2215                if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2216                        ret = v4l2_prio_check(vfd->prio, vfh->prio);
2217                        if (ret)
2218                                goto done;
2219                }
2220        } else {
2221                default_info.ioctl = cmd;
2222                default_info.flags = 0;
2223                default_info.debug = v4l_print_default;
2224                info = &default_info;
2225        }
2226
2227        write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2228        if (info->flags & INFO_FL_STD) {
2229                typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2230                const void *p = vfd->ioctl_ops;
2231                const vidioc_op *vidioc = p + info->u.offset;
2232
2233                ret = (*vidioc)(file, fh, arg);
2234        } else if (info->flags & INFO_FL_FUNC) {
2235                ret = info->u.func(ops, file, fh, arg);
2236        } else if (!ops->vidioc_default) {
2237                ret = -ENOTTY;
2238        } else {
2239                ret = ops->vidioc_default(file, fh,
2240                        use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2241                        cmd, arg);
2242        }
2243
2244done:
2245        if (debug) {
2246                v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2247                if (ret < 0)
2248                        pr_cont(": error %ld", ret);
2249                if (debug == V4L2_DEBUG_IOCTL)
2250                        pr_cont("\n");
2251                else if (_IOC_DIR(cmd) == _IOC_NONE)
2252                        info->debug(arg, write_only);
2253                else {
2254                        pr_cont(": ");
2255                        info->debug(arg, write_only);
2256                }
2257        }
2258
2259        return ret;
2260}
2261
2262static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2263                            void * __user *user_ptr, void ***kernel_ptr)
2264{
2265        int ret = 0;
2266
2267        switch (cmd) {
2268        case VIDIOC_PREPARE_BUF:
2269        case VIDIOC_QUERYBUF:
2270        case VIDIOC_QBUF:
2271        case VIDIOC_DQBUF: {
2272                struct v4l2_buffer *buf = parg;
2273
2274                if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2275                        if (buf->length > VIDEO_MAX_PLANES) {
2276                                ret = -EINVAL;
2277                                break;
2278                        }
2279                        *user_ptr = (void __user *)buf->m.planes;
2280                        *kernel_ptr = (void *)&buf->m.planes;
2281                        *array_size = sizeof(struct v4l2_plane) * buf->length;
2282                        ret = 1;
2283                }
2284                break;
2285        }
2286
2287        case VIDIOC_G_EDID:
2288        case VIDIOC_S_EDID: {
2289                struct v4l2_edid *edid = parg;
2290
2291                if (edid->blocks) {
2292                        if (edid->blocks > 256) {
2293                                ret = -EINVAL;
2294                                break;
2295                        }
2296                        *user_ptr = (void __user *)edid->edid;
2297                        *kernel_ptr = (void *)&edid->edid;
2298                        *array_size = edid->blocks * 128;
2299                        ret = 1;
2300                }
2301                break;
2302        }
2303
2304        case VIDIOC_S_EXT_CTRLS:
2305        case VIDIOC_G_EXT_CTRLS:
2306        case VIDIOC_TRY_EXT_CTRLS: {
2307                struct v4l2_ext_controls *ctrls = parg;
2308
2309                if (ctrls->count != 0) {
2310                        if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2311                                ret = -EINVAL;
2312                                break;
2313                        }
2314                        *user_ptr = (void __user *)ctrls->controls;
2315                        *kernel_ptr = (void *)&ctrls->controls;
2316                        *array_size = sizeof(struct v4l2_ext_control)
2317                                    * ctrls->count;
2318                        ret = 1;
2319                }
2320                break;
2321        }
2322        }
2323
2324        return ret;
2325}
2326
2327long
2328video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2329               v4l2_kioctl func)
2330{
2331        char    sbuf[128];
2332        void    *mbuf = NULL;
2333        void    *parg = (void *)arg;
2334        long    err  = -EINVAL;
2335        bool    has_array_args;
2336        size_t  array_size = 0;
2337        void __user *user_ptr = NULL;
2338        void    **kernel_ptr = NULL;
2339
2340        /*  Copy arguments into temp kernel buffer  */
2341        if (_IOC_DIR(cmd) != _IOC_NONE) {
2342                if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2343                        parg = sbuf;
2344                } else {
2345                        /* too big to allocate from stack */
2346                        mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2347                        if (NULL == mbuf)
2348                                return -ENOMEM;
2349                        parg = mbuf;
2350                }
2351
2352                err = -EFAULT;
2353                if (_IOC_DIR(cmd) & _IOC_WRITE) {
2354                        unsigned int n = _IOC_SIZE(cmd);
2355
2356                        /*
2357                         * In some cases, only a few fields are used as input,
2358                         * i.e. when the app sets "index" and then the driver
2359                         * fills in the rest of the structure for the thing
2360                         * with that index.  We only need to copy up the first
2361                         * non-input field.
2362                         */
2363                        if (v4l2_is_known_ioctl(cmd)) {
2364                                u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2365                                if (flags & INFO_FL_CLEAR_MASK)
2366                                        n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2367                        }
2368
2369                        if (copy_from_user(parg, (void __user *)arg, n))
2370                                goto out;
2371
2372                        /* zero out anything we don't copy from userspace */
2373                        if (n < _IOC_SIZE(cmd))
2374                                memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
2375                } else {
2376                        /* read-only ioctl */
2377                        memset(parg, 0, _IOC_SIZE(cmd));
2378                }
2379        }
2380
2381        err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2382        if (err < 0)
2383                goto out;
2384        has_array_args = err;
2385
2386        if (has_array_args) {
2387                /*
2388                 * When adding new types of array args, make sure that the
2389                 * parent argument to ioctl (which contains the pointer to the
2390                 * array) fits into sbuf (so that mbuf will still remain
2391                 * unused up to here).
2392                 */
2393                mbuf = kmalloc(array_size, GFP_KERNEL);
2394                err = -ENOMEM;
2395                if (NULL == mbuf)
2396                        goto out_array_args;
2397                err = -EFAULT;
2398                if (copy_from_user(mbuf, user_ptr, array_size))
2399                        goto out_array_args;
2400                *kernel_ptr = mbuf;
2401        }
2402
2403        /* Handles IOCTL */
2404        err = func(file, cmd, parg);
2405        if (err == -ENOIOCTLCMD)
2406                err = -ENOTTY;
2407        if (err == 0) {
2408                if (cmd == VIDIOC_DQBUF)
2409                        trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2410                else if (cmd == VIDIOC_QBUF)
2411                        trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2412        }
2413
2414        if (has_array_args) {
2415                *kernel_ptr = user_ptr;
2416                if (copy_to_user(user_ptr, mbuf, array_size))
2417                        err = -EFAULT;
2418                goto out_array_args;
2419        }
2420        /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2421           results that must be returned. */
2422        if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
2423                goto out;
2424
2425out_array_args:
2426        /*  Copy results into user buffer  */
2427        switch (_IOC_DIR(cmd)) {
2428        case _IOC_READ:
2429        case (_IOC_WRITE | _IOC_READ):
2430                if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2431                        err = -EFAULT;
2432                break;
2433        }
2434
2435out:
2436        kfree(mbuf);
2437        return err;
2438}
2439EXPORT_SYMBOL(video_usercopy);
2440
2441long video_ioctl2(struct file *file,
2442               unsigned int cmd, unsigned long arg)
2443{
2444        return video_usercopy(file, cmd, arg, __video_do_ioctl);
2445}
2446EXPORT_SYMBOL(video_ioctl2);
2447