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