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