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