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