linux/drivers/media/video/v4l2-ioctl.c
<<
>>
Prefs
   1/*
   2 * Video capture interface for Linux version 2
   3 *
   4 * A generic framework to process V4L2 ioctl commands.
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License
   8 * as published by the Free Software Foundation; either version
   9 * 2 of the License, or (at your option) any later version.
  10 *
  11 * Authors:     Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
  12 *              Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/slab.h>
  17#include <linux/types.h>
  18#include <linux/kernel.h>
  19
  20#define __OLD_VIDIOC_ /* To allow fixing old calls */
  21#include <linux/videodev2.h>
  22
  23#include <media/v4l2-common.h>
  24#include <media/v4l2-ioctl.h>
  25#include <media/v4l2-ctrls.h>
  26#include <media/v4l2-fh.h>
  27#include <media/v4l2-event.h>
  28#include <media/v4l2-chip-ident.h>
  29
  30#define dbgarg(cmd, fmt, arg...) \
  31                do {                                                    \
  32                    if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {            \
  33                        printk(KERN_DEBUG "%s: ",  vfd->name);          \
  34                        v4l_printk_ioctl(cmd);                          \
  35                        printk(" " fmt,  ## arg);                       \
  36                    }                                                   \
  37                } while (0)
  38
  39#define dbgarg2(fmt, arg...) \
  40                do {                                                    \
  41                    if (vfd->debug & V4L2_DEBUG_IOCTL_ARG)              \
  42                        printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);\
  43                } while (0)
  44
  45#define dbgarg3(fmt, arg...) \
  46                do {                                                    \
  47                    if (vfd->debug & V4L2_DEBUG_IOCTL_ARG)              \
  48                        printk(KERN_CONT "%s: " fmt, vfd->name, ## arg);\
  49                } while (0)
  50
  51/* Zero out the end of the struct pointed to by p.  Everthing after, but
  52 * not including, the specified field is cleared. */
  53#define CLEAR_AFTER_FIELD(p, field) \
  54        memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
  55        0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
  56
  57struct std_descr {
  58        v4l2_std_id std;
  59        const char *descr;
  60};
  61
  62static const struct std_descr standards[] = {
  63        { V4L2_STD_NTSC,        "NTSC"      },
  64        { V4L2_STD_NTSC_M,      "NTSC-M"    },
  65        { V4L2_STD_NTSC_M_JP,   "NTSC-M-JP" },
  66        { V4L2_STD_NTSC_M_KR,   "NTSC-M-KR" },
  67        { V4L2_STD_NTSC_443,    "NTSC-443"  },
  68        { V4L2_STD_PAL,         "PAL"       },
  69        { V4L2_STD_PAL_BG,      "PAL-BG"    },
  70        { V4L2_STD_PAL_B,       "PAL-B"     },
  71        { V4L2_STD_PAL_B1,      "PAL-B1"    },
  72        { V4L2_STD_PAL_G,       "PAL-G"     },
  73        { V4L2_STD_PAL_H,       "PAL-H"     },
  74        { V4L2_STD_PAL_I,       "PAL-I"     },
  75        { V4L2_STD_PAL_DK,      "PAL-DK"    },
  76        { V4L2_STD_PAL_D,       "PAL-D"     },
  77        { V4L2_STD_PAL_D1,      "PAL-D1"    },
  78        { V4L2_STD_PAL_K,       "PAL-K"     },
  79        { V4L2_STD_PAL_M,       "PAL-M"     },
  80        { V4L2_STD_PAL_N,       "PAL-N"     },
  81        { V4L2_STD_PAL_Nc,      "PAL-Nc"    },
  82        { V4L2_STD_PAL_60,      "PAL-60"    },
  83        { V4L2_STD_SECAM,       "SECAM"     },
  84        { V4L2_STD_SECAM_B,     "SECAM-B"   },
  85        { V4L2_STD_SECAM_G,     "SECAM-G"   },
  86        { V4L2_STD_SECAM_H,     "SECAM-H"   },
  87        { V4L2_STD_SECAM_DK,    "SECAM-DK"  },
  88        { V4L2_STD_SECAM_D,     "SECAM-D"   },
  89        { V4L2_STD_SECAM_K,     "SECAM-K"   },
  90        { V4L2_STD_SECAM_K1,    "SECAM-K1"  },
  91        { V4L2_STD_SECAM_L,     "SECAM-L"   },
  92        { V4L2_STD_SECAM_LC,    "SECAM-Lc"  },
  93        { 0,                    "Unknown"   }
  94};
  95
  96/* video4linux standard ID conversion to standard name
  97 */
  98const char *v4l2_norm_to_name(v4l2_std_id id)
  99{
 100        u32 myid = id;
 101        int i;
 102
 103        /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
 104           64 bit comparations. So, on that architecture, with some gcc
 105           variants, compilation fails. Currently, the max value is 30bit wide.
 106         */
 107        BUG_ON(myid != id);
 108
 109        for (i = 0; standards[i].std; i++)
 110                if (myid == standards[i].std)
 111                        break;
 112        return standards[i].descr;
 113}
 114EXPORT_SYMBOL(v4l2_norm_to_name);
 115
 116/* Returns frame period for the given standard */
 117void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
 118{
 119        if (id & V4L2_STD_525_60) {
 120                frameperiod->numerator = 1001;
 121                frameperiod->denominator = 30000;
 122        } else {
 123                frameperiod->numerator = 1;
 124                frameperiod->denominator = 25;
 125        }
 126}
 127EXPORT_SYMBOL(v4l2_video_std_frame_period);
 128
 129/* Fill in the fields of a v4l2_standard structure according to the
 130   'id' and 'transmission' parameters.  Returns negative on error.  */
 131int v4l2_video_std_construct(struct v4l2_standard *vs,
 132                             int id, const char *name)
 133{
 134        vs->id = id;
 135        v4l2_video_std_frame_period(id, &vs->frameperiod);
 136        vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
 137        strlcpy(vs->name, name, sizeof(vs->name));
 138        return 0;
 139}
 140EXPORT_SYMBOL(v4l2_video_std_construct);
 141
 142/* ----------------------------------------------------------------- */
 143/* some arrays for pretty-printing debug messages of enum types      */
 144
 145const char *v4l2_field_names[] = {
 146        [V4L2_FIELD_ANY]        = "any",
 147        [V4L2_FIELD_NONE]       = "none",
 148        [V4L2_FIELD_TOP]        = "top",
 149        [V4L2_FIELD_BOTTOM]     = "bottom",
 150        [V4L2_FIELD_INTERLACED] = "interlaced",
 151        [V4L2_FIELD_SEQ_TB]     = "seq-tb",
 152        [V4L2_FIELD_SEQ_BT]     = "seq-bt",
 153        [V4L2_FIELD_ALTERNATE]  = "alternate",
 154        [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
 155        [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
 156};
 157EXPORT_SYMBOL(v4l2_field_names);
 158
 159const char *v4l2_type_names[] = {
 160        [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
 161        [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
 162        [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
 163        [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
 164        [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
 165        [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
 166        [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
 167        [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
 168};
 169EXPORT_SYMBOL(v4l2_type_names);
 170
 171static const char *v4l2_memory_names[] = {
 172        [V4L2_MEMORY_MMAP]    = "mmap",
 173        [V4L2_MEMORY_USERPTR] = "userptr",
 174        [V4L2_MEMORY_OVERLAY] = "overlay",
 175};
 176
 177#define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
 178                           arr[a] : "unknown")
 179
 180/* ------------------------------------------------------------------ */
 181/* debug help functions                                               */
 182static const char *v4l2_ioctls[] = {
 183        [_IOC_NR(VIDIOC_QUERYCAP)]         = "VIDIOC_QUERYCAP",
 184        [_IOC_NR(VIDIOC_RESERVED)]         = "VIDIOC_RESERVED",
 185        [_IOC_NR(VIDIOC_ENUM_FMT)]         = "VIDIOC_ENUM_FMT",
 186        [_IOC_NR(VIDIOC_G_FMT)]            = "VIDIOC_G_FMT",
 187        [_IOC_NR(VIDIOC_S_FMT)]            = "VIDIOC_S_FMT",
 188        [_IOC_NR(VIDIOC_REQBUFS)]          = "VIDIOC_REQBUFS",
 189        [_IOC_NR(VIDIOC_QUERYBUF)]         = "VIDIOC_QUERYBUF",
 190        [_IOC_NR(VIDIOC_G_FBUF)]           = "VIDIOC_G_FBUF",
 191        [_IOC_NR(VIDIOC_S_FBUF)]           = "VIDIOC_S_FBUF",
 192        [_IOC_NR(VIDIOC_OVERLAY)]          = "VIDIOC_OVERLAY",
 193        [_IOC_NR(VIDIOC_QBUF)]             = "VIDIOC_QBUF",
 194        [_IOC_NR(VIDIOC_DQBUF)]            = "VIDIOC_DQBUF",
 195        [_IOC_NR(VIDIOC_STREAMON)]         = "VIDIOC_STREAMON",
 196        [_IOC_NR(VIDIOC_STREAMOFF)]        = "VIDIOC_STREAMOFF",
 197        [_IOC_NR(VIDIOC_G_PARM)]           = "VIDIOC_G_PARM",
 198        [_IOC_NR(VIDIOC_S_PARM)]           = "VIDIOC_S_PARM",
 199        [_IOC_NR(VIDIOC_G_STD)]            = "VIDIOC_G_STD",
 200        [_IOC_NR(VIDIOC_S_STD)]            = "VIDIOC_S_STD",
 201        [_IOC_NR(VIDIOC_ENUMSTD)]          = "VIDIOC_ENUMSTD",
 202        [_IOC_NR(VIDIOC_ENUMINPUT)]        = "VIDIOC_ENUMINPUT",
 203        [_IOC_NR(VIDIOC_G_CTRL)]           = "VIDIOC_G_CTRL",
 204        [_IOC_NR(VIDIOC_S_CTRL)]           = "VIDIOC_S_CTRL",
 205        [_IOC_NR(VIDIOC_G_TUNER)]          = "VIDIOC_G_TUNER",
 206        [_IOC_NR(VIDIOC_S_TUNER)]          = "VIDIOC_S_TUNER",
 207        [_IOC_NR(VIDIOC_G_AUDIO)]          = "VIDIOC_G_AUDIO",
 208        [_IOC_NR(VIDIOC_S_AUDIO)]          = "VIDIOC_S_AUDIO",
 209        [_IOC_NR(VIDIOC_QUERYCTRL)]        = "VIDIOC_QUERYCTRL",
 210        [_IOC_NR(VIDIOC_QUERYMENU)]        = "VIDIOC_QUERYMENU",
 211        [_IOC_NR(VIDIOC_G_INPUT)]          = "VIDIOC_G_INPUT",
 212        [_IOC_NR(VIDIOC_S_INPUT)]          = "VIDIOC_S_INPUT",
 213        [_IOC_NR(VIDIOC_G_OUTPUT)]         = "VIDIOC_G_OUTPUT",
 214        [_IOC_NR(VIDIOC_S_OUTPUT)]         = "VIDIOC_S_OUTPUT",
 215        [_IOC_NR(VIDIOC_ENUMOUTPUT)]       = "VIDIOC_ENUMOUTPUT",
 216        [_IOC_NR(VIDIOC_G_AUDOUT)]         = "VIDIOC_G_AUDOUT",
 217        [_IOC_NR(VIDIOC_S_AUDOUT)]         = "VIDIOC_S_AUDOUT",
 218        [_IOC_NR(VIDIOC_G_MODULATOR)]      = "VIDIOC_G_MODULATOR",
 219        [_IOC_NR(VIDIOC_S_MODULATOR)]      = "VIDIOC_S_MODULATOR",
 220        [_IOC_NR(VIDIOC_G_FREQUENCY)]      = "VIDIOC_G_FREQUENCY",
 221        [_IOC_NR(VIDIOC_S_FREQUENCY)]      = "VIDIOC_S_FREQUENCY",
 222        [_IOC_NR(VIDIOC_CROPCAP)]          = "VIDIOC_CROPCAP",
 223        [_IOC_NR(VIDIOC_G_CROP)]           = "VIDIOC_G_CROP",
 224        [_IOC_NR(VIDIOC_S_CROP)]           = "VIDIOC_S_CROP",
 225        [_IOC_NR(VIDIOC_G_JPEGCOMP)]       = "VIDIOC_G_JPEGCOMP",
 226        [_IOC_NR(VIDIOC_S_JPEGCOMP)]       = "VIDIOC_S_JPEGCOMP",
 227        [_IOC_NR(VIDIOC_QUERYSTD)]         = "VIDIOC_QUERYSTD",
 228        [_IOC_NR(VIDIOC_TRY_FMT)]          = "VIDIOC_TRY_FMT",
 229        [_IOC_NR(VIDIOC_ENUMAUDIO)]        = "VIDIOC_ENUMAUDIO",
 230        [_IOC_NR(VIDIOC_ENUMAUDOUT)]       = "VIDIOC_ENUMAUDOUT",
 231        [_IOC_NR(VIDIOC_G_PRIORITY)]       = "VIDIOC_G_PRIORITY",
 232        [_IOC_NR(VIDIOC_S_PRIORITY)]       = "VIDIOC_S_PRIORITY",
 233        [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
 234        [_IOC_NR(VIDIOC_LOG_STATUS)]       = "VIDIOC_LOG_STATUS",
 235        [_IOC_NR(VIDIOC_G_EXT_CTRLS)]      = "VIDIOC_G_EXT_CTRLS",
 236        [_IOC_NR(VIDIOC_S_EXT_CTRLS)]      = "VIDIOC_S_EXT_CTRLS",
 237        [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)]    = "VIDIOC_TRY_EXT_CTRLS",
 238#if 1
 239        [_IOC_NR(VIDIOC_ENUM_FRAMESIZES)]  = "VIDIOC_ENUM_FRAMESIZES",
 240        [_IOC_NR(VIDIOC_ENUM_FRAMEINTERVALS)] = "VIDIOC_ENUM_FRAMEINTERVALS",
 241        [_IOC_NR(VIDIOC_G_ENC_INDEX)]      = "VIDIOC_G_ENC_INDEX",
 242        [_IOC_NR(VIDIOC_ENCODER_CMD)]      = "VIDIOC_ENCODER_CMD",
 243        [_IOC_NR(VIDIOC_TRY_ENCODER_CMD)]  = "VIDIOC_TRY_ENCODER_CMD",
 244
 245        [_IOC_NR(VIDIOC_DBG_S_REGISTER)]   = "VIDIOC_DBG_S_REGISTER",
 246        [_IOC_NR(VIDIOC_DBG_G_REGISTER)]   = "VIDIOC_DBG_G_REGISTER",
 247
 248        [_IOC_NR(VIDIOC_DBG_G_CHIP_IDENT)] = "VIDIOC_DBG_G_CHIP_IDENT",
 249        [_IOC_NR(VIDIOC_S_HW_FREQ_SEEK)]   = "VIDIOC_S_HW_FREQ_SEEK",
 250#endif
 251        [_IOC_NR(VIDIOC_ENUM_DV_PRESETS)]  = "VIDIOC_ENUM_DV_PRESETS",
 252        [_IOC_NR(VIDIOC_S_DV_PRESET)]      = "VIDIOC_S_DV_PRESET",
 253        [_IOC_NR(VIDIOC_G_DV_PRESET)]      = "VIDIOC_G_DV_PRESET",
 254        [_IOC_NR(VIDIOC_QUERY_DV_PRESET)]  = "VIDIOC_QUERY_DV_PRESET",
 255        [_IOC_NR(VIDIOC_S_DV_TIMINGS)]     = "VIDIOC_S_DV_TIMINGS",
 256        [_IOC_NR(VIDIOC_G_DV_TIMINGS)]     = "VIDIOC_G_DV_TIMINGS",
 257        [_IOC_NR(VIDIOC_DQEVENT)]          = "VIDIOC_DQEVENT",
 258        [_IOC_NR(VIDIOC_SUBSCRIBE_EVENT)]  = "VIDIOC_SUBSCRIBE_EVENT",
 259        [_IOC_NR(VIDIOC_UNSUBSCRIBE_EVENT)] = "VIDIOC_UNSUBSCRIBE_EVENT",
 260};
 261#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
 262
 263/* Common ioctl debug function. This function can be used by
 264   external ioctl messages as well as internal V4L ioctl */
 265void v4l_printk_ioctl(unsigned int cmd)
 266{
 267        char *dir, *type;
 268
 269        switch (_IOC_TYPE(cmd)) {
 270        case 'd':
 271                type = "v4l2_int";
 272                break;
 273        case 'V':
 274                if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
 275                        type = "v4l2";
 276                        break;
 277                }
 278                printk("%s", v4l2_ioctls[_IOC_NR(cmd)]);
 279                return;
 280        default:
 281                type = "unknown";
 282        }
 283
 284        switch (_IOC_DIR(cmd)) {
 285        case _IOC_NONE:              dir = "--"; break;
 286        case _IOC_READ:              dir = "r-"; break;
 287        case _IOC_WRITE:             dir = "-w"; break;
 288        case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
 289        default:                     dir = "*ERR*"; break;
 290        }
 291        printk("%s ioctl '%c', dir=%s, #%d (0x%08x)",
 292                type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
 293}
 294EXPORT_SYMBOL(v4l_printk_ioctl);
 295
 296/*
 297 * helper function -- handles userspace copying for ioctl arguments
 298 */
 299
 300#ifdef __OLD_VIDIOC_
 301static unsigned int
 302video_fix_command(unsigned int cmd)
 303{
 304        switch (cmd) {
 305        case VIDIOC_OVERLAY_OLD:
 306                cmd = VIDIOC_OVERLAY;
 307                break;
 308        case VIDIOC_S_PARM_OLD:
 309                cmd = VIDIOC_S_PARM;
 310                break;
 311        case VIDIOC_S_CTRL_OLD:
 312                cmd = VIDIOC_S_CTRL;
 313                break;
 314        case VIDIOC_G_AUDIO_OLD:
 315                cmd = VIDIOC_G_AUDIO;
 316                break;
 317        case VIDIOC_G_AUDOUT_OLD:
 318                cmd = VIDIOC_G_AUDOUT;
 319                break;
 320        case VIDIOC_CROPCAP_OLD:
 321                cmd = VIDIOC_CROPCAP;
 322                break;
 323        }
 324        return cmd;
 325}
 326#endif
 327
 328/*
 329 * Obsolete usercopy function - Should be removed soon
 330 */
 331long
 332video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
 333                v4l2_kioctl func)
 334{
 335        char    sbuf[128];
 336        void    *mbuf = NULL;
 337        void    *parg = NULL;
 338        long    err  = -EINVAL;
 339        int     is_ext_ctrl;
 340        size_t  ctrls_size = 0;
 341        void __user *user_ptr = NULL;
 342
 343#ifdef __OLD_VIDIOC_
 344        cmd = video_fix_command(cmd);
 345#endif
 346        is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
 347                       cmd == VIDIOC_TRY_EXT_CTRLS);
 348
 349        /*  Copy arguments into temp kernel buffer  */
 350        switch (_IOC_DIR(cmd)) {
 351        case _IOC_NONE:
 352                parg = NULL;
 353                break;
 354        case _IOC_READ:
 355        case _IOC_WRITE:
 356        case (_IOC_WRITE | _IOC_READ):
 357                if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
 358                        parg = sbuf;
 359                } else {
 360                        /* too big to allocate from stack */
 361                        mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
 362                        if (NULL == mbuf)
 363                                return -ENOMEM;
 364                        parg = mbuf;
 365                }
 366
 367                err = -EFAULT;
 368                if (_IOC_DIR(cmd) & _IOC_WRITE)
 369                        if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
 370                                goto out;
 371                break;
 372        }
 373        if (is_ext_ctrl) {
 374                struct v4l2_ext_controls *p = parg;
 375
 376                /* In case of an error, tell the caller that it wasn't
 377                   a specific control that caused it. */
 378                p->error_idx = p->count;
 379                user_ptr = (void __user *)p->controls;
 380                if (p->count) {
 381                        ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
 382                        /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
 383                        mbuf = kmalloc(ctrls_size, GFP_KERNEL);
 384                        err = -ENOMEM;
 385                        if (NULL == mbuf)
 386                                goto out_ext_ctrl;
 387                        err = -EFAULT;
 388                        if (copy_from_user(mbuf, user_ptr, ctrls_size))
 389                                goto out_ext_ctrl;
 390                        p->controls = mbuf;
 391                }
 392        }
 393
 394        /* call driver */
 395        err = func(file, cmd, parg);
 396        if (err == -ENOIOCTLCMD)
 397                err = -EINVAL;
 398        if (is_ext_ctrl) {
 399                struct v4l2_ext_controls *p = parg;
 400
 401                p->controls = (void *)user_ptr;
 402                if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
 403                        err = -EFAULT;
 404                goto out_ext_ctrl;
 405        }
 406        if (err < 0)
 407                goto out;
 408
 409out_ext_ctrl:
 410        /*  Copy results into user buffer  */
 411        switch (_IOC_DIR(cmd)) {
 412        case _IOC_READ:
 413        case (_IOC_WRITE | _IOC_READ):
 414                if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
 415                        err = -EFAULT;
 416                break;
 417        }
 418
 419out:
 420        kfree(mbuf);
 421        return err;
 422}
 423EXPORT_SYMBOL(video_usercopy);
 424
 425static void dbgbuf(unsigned int cmd, struct video_device *vfd,
 426                                        struct v4l2_buffer *p)
 427{
 428        struct v4l2_timecode *tc = &p->timecode;
 429
 430        dbgarg(cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
 431                "bytesused=%d, flags=0x%08d, "
 432                "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
 433                        p->timestamp.tv_sec / 3600,
 434                        (int)(p->timestamp.tv_sec / 60) % 60,
 435                        (int)(p->timestamp.tv_sec % 60),
 436                        (long)p->timestamp.tv_usec,
 437                        p->index,
 438                        prt_names(p->type, v4l2_type_names),
 439                        p->bytesused, p->flags,
 440                        p->field, p->sequence,
 441                        prt_names(p->memory, v4l2_memory_names),
 442                        p->m.userptr, p->length);
 443        dbgarg2("timecode=%02d:%02d:%02d type=%d, "
 444                "flags=0x%08d, frames=%d, userbits=0x%08x\n",
 445                        tc->hours, tc->minutes, tc->seconds,
 446                        tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
 447}
 448
 449static inline void dbgrect(struct video_device *vfd, char *s,
 450                                                        struct v4l2_rect *r)
 451{
 452        dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
 453                                                r->width, r->height);
 454};
 455
 456static inline void v4l_print_pix_fmt(struct video_device *vfd,
 457                                                struct v4l2_pix_format *fmt)
 458{
 459        dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
 460                "bytesperline=%d sizeimage=%d, colorspace=%d\n",
 461                fmt->width, fmt->height,
 462                (fmt->pixelformat & 0xff),
 463                (fmt->pixelformat >>  8) & 0xff,
 464                (fmt->pixelformat >> 16) & 0xff,
 465                (fmt->pixelformat >> 24) & 0xff,
 466                prt_names(fmt->field, v4l2_field_names),
 467                fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
 468};
 469
 470static inline void v4l_print_ext_ctrls(unsigned int cmd,
 471        struct video_device *vfd, struct v4l2_ext_controls *c, int show_vals)
 472{
 473        __u32 i;
 474
 475        if (!(vfd->debug & V4L2_DEBUG_IOCTL_ARG))
 476                return;
 477        dbgarg(cmd, "");
 478        printk(KERN_CONT "class=0x%x", c->ctrl_class);
 479        for (i = 0; i < c->count; i++) {
 480                if (show_vals && !c->controls[i].size)
 481                        printk(KERN_CONT " id/val=0x%x/0x%x",
 482                                c->controls[i].id, c->controls[i].value);
 483                else
 484                        printk(KERN_CONT " id=0x%x,size=%u",
 485                                c->controls[i].id, c->controls[i].size);
 486        }
 487        printk(KERN_CONT "\n");
 488};
 489
 490static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
 491{
 492        __u32 i;
 493
 494        /* zero the reserved fields */
 495        c->reserved[0] = c->reserved[1] = 0;
 496        for (i = 0; i < c->count; i++)
 497                c->controls[i].reserved2[0] = 0;
 498
 499        /* V4L2_CID_PRIVATE_BASE cannot be used as control class
 500           when using extended controls.
 501           Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
 502           is it allowed for backwards compatibility.
 503         */
 504        if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
 505                return 0;
 506        /* Check that all controls are from the same control class. */
 507        for (i = 0; i < c->count; i++) {
 508                if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
 509                        c->error_idx = i;
 510                        return 0;
 511                }
 512        }
 513        return 1;
 514}
 515
 516static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
 517{
 518        if (ops == NULL)
 519                return -EINVAL;
 520
 521        switch (type) {
 522        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 523                if (ops->vidioc_g_fmt_vid_cap)
 524                        return 0;
 525                break;
 526        case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 527                if (ops->vidioc_g_fmt_vid_overlay)
 528                        return 0;
 529                break;
 530        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 531                if (ops->vidioc_g_fmt_vid_out)
 532                        return 0;
 533                break;
 534        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 535                if (ops->vidioc_g_fmt_vid_out_overlay)
 536                        return 0;
 537                break;
 538        case V4L2_BUF_TYPE_VBI_CAPTURE:
 539                if (ops->vidioc_g_fmt_vbi_cap)
 540                        return 0;
 541                break;
 542        case V4L2_BUF_TYPE_VBI_OUTPUT:
 543                if (ops->vidioc_g_fmt_vbi_out)
 544                        return 0;
 545                break;
 546        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 547                if (ops->vidioc_g_fmt_sliced_vbi_cap)
 548                        return 0;
 549                break;
 550        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 551                if (ops->vidioc_g_fmt_sliced_vbi_out)
 552                        return 0;
 553                break;
 554        case V4L2_BUF_TYPE_PRIVATE:
 555                if (ops->vidioc_g_fmt_type_private)
 556                        return 0;
 557                break;
 558        }
 559        return -EINVAL;
 560}
 561
 562static long __video_do_ioctl(struct file *file,
 563                unsigned int cmd, void *arg)
 564{
 565        struct video_device *vfd = video_devdata(file);
 566        const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
 567        void *fh = file->private_data;
 568        long ret = -EINVAL;
 569
 570        if (ops == NULL) {
 571                printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
 572                                vfd->name);
 573                return -EINVAL;
 574        }
 575
 576        if ((vfd->debug & V4L2_DEBUG_IOCTL) &&
 577                                !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
 578                v4l_print_ioctl(vfd->name, cmd);
 579                printk(KERN_CONT "\n");
 580        }
 581
 582        switch (cmd) {
 583
 584        /* --- capabilities ------------------------------------------ */
 585        case VIDIOC_QUERYCAP:
 586        {
 587                struct v4l2_capability *cap = (struct v4l2_capability *)arg;
 588
 589                if (!ops->vidioc_querycap)
 590                        break;
 591
 592                ret = ops->vidioc_querycap(file, fh, cap);
 593                if (!ret)
 594                        dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
 595                                        "version=0x%08x, "
 596                                        "capabilities=0x%08x\n",
 597                                        cap->driver, cap->card, cap->bus_info,
 598                                        cap->version,
 599                                        cap->capabilities);
 600                break;
 601        }
 602
 603        /* --- priority ------------------------------------------ */
 604        case VIDIOC_G_PRIORITY:
 605        {
 606                enum v4l2_priority *p = arg;
 607
 608                if (!ops->vidioc_g_priority)
 609                        break;
 610                ret = ops->vidioc_g_priority(file, fh, p);
 611                if (!ret)
 612                        dbgarg(cmd, "priority is %d\n", *p);
 613                break;
 614        }
 615        case VIDIOC_S_PRIORITY:
 616        {
 617                enum v4l2_priority *p = arg;
 618
 619                if (!ops->vidioc_s_priority)
 620                        break;
 621                dbgarg(cmd, "setting priority to %d\n", *p);
 622                ret = ops->vidioc_s_priority(file, fh, *p);
 623                break;
 624        }
 625
 626        /* --- capture ioctls ---------------------------------------- */
 627        case VIDIOC_ENUM_FMT:
 628        {
 629                struct v4l2_fmtdesc *f = arg;
 630
 631                switch (f->type) {
 632                case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 633                        if (ops->vidioc_enum_fmt_vid_cap)
 634                                ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
 635                        break;
 636                case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 637                        if (ops->vidioc_enum_fmt_vid_overlay)
 638                                ret = ops->vidioc_enum_fmt_vid_overlay(file,
 639                                        fh, f);
 640                        break;
 641                case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 642                        if (ops->vidioc_enum_fmt_vid_out)
 643                                ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
 644                        break;
 645                case V4L2_BUF_TYPE_PRIVATE:
 646                        if (ops->vidioc_enum_fmt_type_private)
 647                                ret = ops->vidioc_enum_fmt_type_private(file,
 648                                                                fh, f);
 649                        break;
 650                default:
 651                        break;
 652                }
 653                if (!ret)
 654                        dbgarg(cmd, "index=%d, type=%d, flags=%d, "
 655                                "pixelformat=%c%c%c%c, description='%s'\n",
 656                                f->index, f->type, f->flags,
 657                                (f->pixelformat & 0xff),
 658                                (f->pixelformat >>  8) & 0xff,
 659                                (f->pixelformat >> 16) & 0xff,
 660                                (f->pixelformat >> 24) & 0xff,
 661                                f->description);
 662                break;
 663        }
 664        case VIDIOC_G_FMT:
 665        {
 666                struct v4l2_format *f = (struct v4l2_format *)arg;
 667
 668                /* FIXME: Should be one dump per type */
 669                dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
 670
 671                switch (f->type) {
 672                case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 673                        if (ops->vidioc_g_fmt_vid_cap)
 674                                ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
 675                        if (!ret)
 676                                v4l_print_pix_fmt(vfd, &f->fmt.pix);
 677                        break;
 678                case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 679                        if (ops->vidioc_g_fmt_vid_overlay)
 680                                ret = ops->vidioc_g_fmt_vid_overlay(file,
 681                                                                    fh, f);
 682                        break;
 683                case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 684                        if (ops->vidioc_g_fmt_vid_out)
 685                                ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
 686                        if (!ret)
 687                                v4l_print_pix_fmt(vfd, &f->fmt.pix);
 688                        break;
 689                case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 690                        if (ops->vidioc_g_fmt_vid_out_overlay)
 691                                ret = ops->vidioc_g_fmt_vid_out_overlay(file,
 692                                       fh, f);
 693                        break;
 694                case V4L2_BUF_TYPE_VBI_CAPTURE:
 695                        if (ops->vidioc_g_fmt_vbi_cap)
 696                                ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
 697                        break;
 698                case V4L2_BUF_TYPE_VBI_OUTPUT:
 699                        if (ops->vidioc_g_fmt_vbi_out)
 700                                ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
 701                        break;
 702                case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 703                        if (ops->vidioc_g_fmt_sliced_vbi_cap)
 704                                ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
 705                                                                        fh, f);
 706                        break;
 707                case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 708                        if (ops->vidioc_g_fmt_sliced_vbi_out)
 709                                ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
 710                                                                        fh, f);
 711                        break;
 712                case V4L2_BUF_TYPE_PRIVATE:
 713                        if (ops->vidioc_g_fmt_type_private)
 714                                ret = ops->vidioc_g_fmt_type_private(file,
 715                                                                fh, f);
 716                        break;
 717                }
 718
 719                break;
 720        }
 721        case VIDIOC_S_FMT:
 722        {
 723                struct v4l2_format *f = (struct v4l2_format *)arg;
 724
 725                /* FIXME: Should be one dump per type */
 726                dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
 727
 728                switch (f->type) {
 729                case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 730                        CLEAR_AFTER_FIELD(f, fmt.pix);
 731                        v4l_print_pix_fmt(vfd, &f->fmt.pix);
 732                        if (ops->vidioc_s_fmt_vid_cap)
 733                                ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
 734                        break;
 735                case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 736                        CLEAR_AFTER_FIELD(f, fmt.win);
 737                        if (ops->vidioc_s_fmt_vid_overlay)
 738                                ret = ops->vidioc_s_fmt_vid_overlay(file,
 739                                                                    fh, f);
 740                        break;
 741                case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 742                        CLEAR_AFTER_FIELD(f, fmt.pix);
 743                        v4l_print_pix_fmt(vfd, &f->fmt.pix);
 744                        if (ops->vidioc_s_fmt_vid_out)
 745                                ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
 746                        break;
 747                case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 748                        CLEAR_AFTER_FIELD(f, fmt.win);
 749                        if (ops->vidioc_s_fmt_vid_out_overlay)
 750                                ret = ops->vidioc_s_fmt_vid_out_overlay(file,
 751                                        fh, f);
 752                        break;
 753                case V4L2_BUF_TYPE_VBI_CAPTURE:
 754                        CLEAR_AFTER_FIELD(f, fmt.vbi);
 755                        if (ops->vidioc_s_fmt_vbi_cap)
 756                                ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
 757                        break;
 758                case V4L2_BUF_TYPE_VBI_OUTPUT:
 759                        CLEAR_AFTER_FIELD(f, fmt.vbi);
 760                        if (ops->vidioc_s_fmt_vbi_out)
 761                                ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
 762                        break;
 763                case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 764                        CLEAR_AFTER_FIELD(f, fmt.sliced);
 765                        if (ops->vidioc_s_fmt_sliced_vbi_cap)
 766                                ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
 767                                                                        fh, f);
 768                        break;
 769                case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 770                        CLEAR_AFTER_FIELD(f, fmt.sliced);
 771                        if (ops->vidioc_s_fmt_sliced_vbi_out)
 772                                ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
 773                                                                        fh, f);
 774                        break;
 775                case V4L2_BUF_TYPE_PRIVATE:
 776                        /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
 777                        if (ops->vidioc_s_fmt_type_private)
 778                                ret = ops->vidioc_s_fmt_type_private(file,
 779                                                                fh, f);
 780                        break;
 781                }
 782                break;
 783        }
 784        case VIDIOC_TRY_FMT:
 785        {
 786                struct v4l2_format *f = (struct v4l2_format *)arg;
 787
 788                /* FIXME: Should be one dump per type */
 789                dbgarg(cmd, "type=%s\n", prt_names(f->type,
 790                                                v4l2_type_names));
 791                switch (f->type) {
 792                case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 793                        CLEAR_AFTER_FIELD(f, fmt.pix);
 794                        if (ops->vidioc_try_fmt_vid_cap)
 795                                ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
 796                        if (!ret)
 797                                v4l_print_pix_fmt(vfd, &f->fmt.pix);
 798                        break;
 799                case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 800                        CLEAR_AFTER_FIELD(f, fmt.win);
 801                        if (ops->vidioc_try_fmt_vid_overlay)
 802                                ret = ops->vidioc_try_fmt_vid_overlay(file,
 803                                        fh, f);
 804                        break;
 805                case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 806                        CLEAR_AFTER_FIELD(f, fmt.pix);
 807                        if (ops->vidioc_try_fmt_vid_out)
 808                                ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
 809                        if (!ret)
 810                                v4l_print_pix_fmt(vfd, &f->fmt.pix);
 811                        break;
 812                case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
 813                        CLEAR_AFTER_FIELD(f, fmt.win);
 814                        if (ops->vidioc_try_fmt_vid_out_overlay)
 815                                ret = ops->vidioc_try_fmt_vid_out_overlay(file,
 816                                       fh, f);
 817                        break;
 818                case V4L2_BUF_TYPE_VBI_CAPTURE:
 819                        CLEAR_AFTER_FIELD(f, fmt.vbi);
 820                        if (ops->vidioc_try_fmt_vbi_cap)
 821                                ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
 822                        break;
 823                case V4L2_BUF_TYPE_VBI_OUTPUT:
 824                        CLEAR_AFTER_FIELD(f, fmt.vbi);
 825                        if (ops->vidioc_try_fmt_vbi_out)
 826                                ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
 827                        break;
 828                case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 829                        CLEAR_AFTER_FIELD(f, fmt.sliced);
 830                        if (ops->vidioc_try_fmt_sliced_vbi_cap)
 831                                ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
 832                                                                fh, f);
 833                        break;
 834                case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 835                        CLEAR_AFTER_FIELD(f, fmt.sliced);
 836                        if (ops->vidioc_try_fmt_sliced_vbi_out)
 837                                ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
 838                                                                fh, f);
 839                        break;
 840                case V4L2_BUF_TYPE_PRIVATE:
 841                        /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
 842                        if (ops->vidioc_try_fmt_type_private)
 843                                ret = ops->vidioc_try_fmt_type_private(file,
 844                                                                fh, f);
 845                        break;
 846                }
 847
 848                break;
 849        }
 850        /* FIXME: Those buf reqs could be handled here,
 851           with some changes on videobuf to allow its header to be included at
 852           videodev2.h or being merged at videodev2.
 853         */
 854        case VIDIOC_REQBUFS:
 855        {
 856                struct v4l2_requestbuffers *p = arg;
 857
 858                if (!ops->vidioc_reqbufs)
 859                        break;
 860                ret = check_fmt(ops, p->type);
 861                if (ret)
 862                        break;
 863
 864                if (p->type < V4L2_BUF_TYPE_PRIVATE)
 865                        CLEAR_AFTER_FIELD(p, memory);
 866
 867                ret = ops->vidioc_reqbufs(file, fh, p);
 868                dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
 869                                p->count,
 870                                prt_names(p->type, v4l2_type_names),
 871                                prt_names(p->memory, v4l2_memory_names));
 872                break;
 873        }
 874        case VIDIOC_QUERYBUF:
 875        {
 876                struct v4l2_buffer *p = arg;
 877
 878                if (!ops->vidioc_querybuf)
 879                        break;
 880                ret = check_fmt(ops, p->type);
 881                if (ret)
 882                        break;
 883
 884                ret = ops->vidioc_querybuf(file, fh, p);
 885                if (!ret)
 886                        dbgbuf(cmd, vfd, p);
 887                break;
 888        }
 889        case VIDIOC_QBUF:
 890        {
 891                struct v4l2_buffer *p = arg;
 892
 893                if (!ops->vidioc_qbuf)
 894                        break;
 895                ret = check_fmt(ops, p->type);
 896                if (ret)
 897                        break;
 898
 899                ret = ops->vidioc_qbuf(file, fh, p);
 900                if (!ret)
 901                        dbgbuf(cmd, vfd, p);
 902                break;
 903        }
 904        case VIDIOC_DQBUF:
 905        {
 906                struct v4l2_buffer *p = arg;
 907
 908                if (!ops->vidioc_dqbuf)
 909                        break;
 910                ret = check_fmt(ops, p->type);
 911                if (ret)
 912                        break;
 913
 914                ret = ops->vidioc_dqbuf(file, fh, p);
 915                if (!ret)
 916                        dbgbuf(cmd, vfd, p);
 917                break;
 918        }
 919        case VIDIOC_OVERLAY:
 920        {
 921                int *i = arg;
 922
 923                if (!ops->vidioc_overlay)
 924                        break;
 925                dbgarg(cmd, "value=%d\n", *i);
 926                ret = ops->vidioc_overlay(file, fh, *i);
 927                break;
 928        }
 929        case VIDIOC_G_FBUF:
 930        {
 931                struct v4l2_framebuffer *p = arg;
 932
 933                if (!ops->vidioc_g_fbuf)
 934                        break;
 935                ret = ops->vidioc_g_fbuf(file, fh, arg);
 936                if (!ret) {
 937                        dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
 938                                        p->capability, p->flags,
 939                                        (unsigned long)p->base);
 940                        v4l_print_pix_fmt(vfd, &p->fmt);
 941                }
 942                break;
 943        }
 944        case VIDIOC_S_FBUF:
 945        {
 946                struct v4l2_framebuffer *p = arg;
 947
 948                if (!ops->vidioc_s_fbuf)
 949                        break;
 950                dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
 951                        p->capability, p->flags, (unsigned long)p->base);
 952                v4l_print_pix_fmt(vfd, &p->fmt);
 953                ret = ops->vidioc_s_fbuf(file, fh, arg);
 954                break;
 955        }
 956        case VIDIOC_STREAMON:
 957        {
 958                enum v4l2_buf_type i = *(int *)arg;
 959
 960                if (!ops->vidioc_streamon)
 961                        break;
 962                dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
 963                ret = ops->vidioc_streamon(file, fh, i);
 964                break;
 965        }
 966        case VIDIOC_STREAMOFF:
 967        {
 968                enum v4l2_buf_type i = *(int *)arg;
 969
 970                if (!ops->vidioc_streamoff)
 971                        break;
 972                dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
 973                ret = ops->vidioc_streamoff(file, fh, i);
 974                break;
 975        }
 976        /* ---------- tv norms ---------- */
 977        case VIDIOC_ENUMSTD:
 978        {
 979                struct v4l2_standard *p = arg;
 980                v4l2_std_id id = vfd->tvnorms, curr_id = 0;
 981                unsigned int index = p->index, i, j = 0;
 982                const char *descr = "";
 983
 984                /* Return norm array in a canonical way */
 985                for (i = 0; i <= index && id; i++) {
 986                        /* last std value in the standards array is 0, so this
 987                           while always ends there since (id & 0) == 0. */
 988                        while ((id & standards[j].std) != standards[j].std)
 989                                j++;
 990                        curr_id = standards[j].std;
 991                        descr = standards[j].descr;
 992                        j++;
 993                        if (curr_id == 0)
 994                                break;
 995                        if (curr_id != V4L2_STD_PAL &&
 996                            curr_id != V4L2_STD_SECAM &&
 997                            curr_id != V4L2_STD_NTSC)
 998                                id &= ~curr_id;
 999                }
1000                if (i <= index)
1001                        break;
1002
1003                v4l2_video_std_construct(p, curr_id, descr);
1004
1005                dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
1006                                "framelines=%d\n", p->index,
1007                                (unsigned long long)p->id, p->name,
1008                                p->frameperiod.numerator,
1009                                p->frameperiod.denominator,
1010                                p->framelines);
1011
1012                ret = 0;
1013                break;
1014        }
1015        case VIDIOC_G_STD:
1016        {
1017                v4l2_std_id *id = arg;
1018
1019                ret = 0;
1020                /* Calls the specific handler */
1021                if (ops->vidioc_g_std)
1022                        ret = ops->vidioc_g_std(file, fh, id);
1023                else if (vfd->current_norm)
1024                        *id = vfd->current_norm;
1025                else
1026                        ret = -EINVAL;
1027
1028                if (!ret)
1029                        dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
1030                break;
1031        }
1032        case VIDIOC_S_STD:
1033        {
1034                v4l2_std_id *id = arg, norm;
1035
1036                dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
1037
1038                norm = (*id) & vfd->tvnorms;
1039                if (vfd->tvnorms && !norm)      /* Check if std is supported */
1040                        break;
1041
1042                /* Calls the specific handler */
1043                if (ops->vidioc_s_std)
1044                        ret = ops->vidioc_s_std(file, fh, &norm);
1045                else
1046                        ret = -EINVAL;
1047
1048                /* Updates standard information */
1049                if (ret >= 0)
1050                        vfd->current_norm = norm;
1051                break;
1052        }
1053        case VIDIOC_QUERYSTD:
1054        {
1055                v4l2_std_id *p = arg;
1056
1057                if (!ops->vidioc_querystd)
1058                        break;
1059                ret = ops->vidioc_querystd(file, fh, arg);
1060                if (!ret)
1061                        dbgarg(cmd, "detected std=%08Lx\n",
1062                                                (unsigned long long)*p);
1063                break;
1064        }
1065        /* ------ input switching ---------- */
1066        /* FIXME: Inputs can be handled inside videodev2 */
1067        case VIDIOC_ENUMINPUT:
1068        {
1069                struct v4l2_input *p = arg;
1070
1071                /*
1072                 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1073                 * CAP_STD here based on ioctl handler provided by the
1074                 * driver. If the driver doesn't support these
1075                 * for a specific input, it must override these flags.
1076                 */
1077                if (ops->vidioc_s_std)
1078                        p->capabilities |= V4L2_IN_CAP_STD;
1079                if (ops->vidioc_s_dv_preset)
1080                        p->capabilities |= V4L2_IN_CAP_PRESETS;
1081                if (ops->vidioc_s_dv_timings)
1082                        p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
1083
1084                if (!ops->vidioc_enum_input)
1085                        break;
1086
1087                ret = ops->vidioc_enum_input(file, fh, p);
1088                if (!ret)
1089                        dbgarg(cmd, "index=%d, name=%s, type=%d, "
1090                                "audioset=%d, "
1091                                "tuner=%d, std=%08Lx, status=%d\n",
1092                                p->index, p->name, p->type, p->audioset,
1093                                p->tuner,
1094                                (unsigned long long)p->std,
1095                                p->status);
1096                break;
1097        }
1098        case VIDIOC_G_INPUT:
1099        {
1100                unsigned int *i = arg;
1101
1102                if (!ops->vidioc_g_input)
1103                        break;
1104                ret = ops->vidioc_g_input(file, fh, i);
1105                if (!ret)
1106                        dbgarg(cmd, "value=%d\n", *i);
1107                break;
1108        }
1109        case VIDIOC_S_INPUT:
1110        {
1111                unsigned int *i = arg;
1112
1113                if (!ops->vidioc_s_input)
1114                        break;
1115                dbgarg(cmd, "value=%d\n", *i);
1116                ret = ops->vidioc_s_input(file, fh, *i);
1117                break;
1118        }
1119
1120        /* ------ output switching ---------- */
1121        case VIDIOC_ENUMOUTPUT:
1122        {
1123                struct v4l2_output *p = arg;
1124
1125                if (!ops->vidioc_enum_output)
1126                        break;
1127
1128                /*
1129                 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1130                 * CAP_STD here based on ioctl handler provided by the
1131                 * driver. If the driver doesn't support these
1132                 * for a specific output, it must override these flags.
1133                 */
1134                if (ops->vidioc_s_std)
1135                        p->capabilities |= V4L2_OUT_CAP_STD;
1136                if (ops->vidioc_s_dv_preset)
1137                        p->capabilities |= V4L2_OUT_CAP_PRESETS;
1138                if (ops->vidioc_s_dv_timings)
1139                        p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
1140
1141                ret = ops->vidioc_enum_output(file, fh, p);
1142                if (!ret)
1143                        dbgarg(cmd, "index=%d, name=%s, type=%d, "
1144                                "audioset=0x%x, "
1145                                "modulator=%d, std=0x%08Lx\n",
1146                                p->index, p->name, p->type, p->audioset,
1147                                p->modulator, (unsigned long long)p->std);
1148                break;
1149        }
1150        case VIDIOC_G_OUTPUT:
1151        {
1152                unsigned int *i = arg;
1153
1154                if (!ops->vidioc_g_output)
1155                        break;
1156                ret = ops->vidioc_g_output(file, fh, i);
1157                if (!ret)
1158                        dbgarg(cmd, "value=%d\n", *i);
1159                break;
1160        }
1161        case VIDIOC_S_OUTPUT:
1162        {
1163                unsigned int *i = arg;
1164
1165                if (!ops->vidioc_s_output)
1166                        break;
1167                dbgarg(cmd, "value=%d\n", *i);
1168                ret = ops->vidioc_s_output(file, fh, *i);
1169                break;
1170        }
1171
1172        /* --- controls ---------------------------------------------- */
1173        case VIDIOC_QUERYCTRL:
1174        {
1175                struct v4l2_queryctrl *p = arg;
1176
1177                if (vfd->ctrl_handler)
1178                        ret = v4l2_queryctrl(vfd->ctrl_handler, p);
1179                else if (ops->vidioc_queryctrl)
1180                        ret = ops->vidioc_queryctrl(file, fh, p);
1181                else
1182                        break;
1183                if (!ret)
1184                        dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1185                                        "step=%d, default=%d, flags=0x%08x\n",
1186                                        p->id, p->type, p->name,
1187                                        p->minimum, p->maximum,
1188                                        p->step, p->default_value, p->flags);
1189                else
1190                        dbgarg(cmd, "id=0x%x\n", p->id);
1191                break;
1192        }
1193        case VIDIOC_G_CTRL:
1194        {
1195                struct v4l2_control *p = arg;
1196
1197                if (vfd->ctrl_handler)
1198                        ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
1199                else if (ops->vidioc_g_ctrl)
1200                        ret = ops->vidioc_g_ctrl(file, fh, p);
1201                else if (ops->vidioc_g_ext_ctrls) {
1202                        struct v4l2_ext_controls ctrls;
1203                        struct v4l2_ext_control ctrl;
1204
1205                        ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1206                        ctrls.count = 1;
1207                        ctrls.controls = &ctrl;
1208                        ctrl.id = p->id;
1209                        ctrl.value = p->value;
1210                        if (check_ext_ctrls(&ctrls, 1)) {
1211                                ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1212                                if (ret == 0)
1213                                        p->value = ctrl.value;
1214                        }
1215                } else
1216                        break;
1217                if (!ret)
1218                        dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1219                else
1220                        dbgarg(cmd, "id=0x%x\n", p->id);
1221                break;
1222        }
1223        case VIDIOC_S_CTRL:
1224        {
1225                struct v4l2_control *p = arg;
1226                struct v4l2_ext_controls ctrls;
1227                struct v4l2_ext_control ctrl;
1228
1229                if (!vfd->ctrl_handler &&
1230                        !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
1231                        break;
1232
1233                dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1234
1235                if (vfd->ctrl_handler) {
1236                        ret = v4l2_s_ctrl(vfd->ctrl_handler, p);
1237                        break;
1238                }
1239                if (ops->vidioc_s_ctrl) {
1240                        ret = ops->vidioc_s_ctrl(file, fh, p);
1241                        break;
1242                }
1243                if (!ops->vidioc_s_ext_ctrls)
1244                        break;
1245
1246                ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1247                ctrls.count = 1;
1248                ctrls.controls = &ctrl;
1249                ctrl.id = p->id;
1250                ctrl.value = p->value;
1251                if (check_ext_ctrls(&ctrls, 1))
1252                        ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1253                break;
1254        }
1255        case VIDIOC_G_EXT_CTRLS:
1256        {
1257                struct v4l2_ext_controls *p = arg;
1258
1259                p->error_idx = p->count;
1260                if (vfd->ctrl_handler)
1261                        ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1262                else if (ops->vidioc_g_ext_ctrls && check_ext_ctrls(p, 0))
1263                        ret = ops->vidioc_g_ext_ctrls(file, fh, p);
1264                else
1265                        break;
1266                v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1267                break;
1268        }
1269        case VIDIOC_S_EXT_CTRLS:
1270        {
1271                struct v4l2_ext_controls *p = arg;
1272
1273                p->error_idx = p->count;
1274                if (!vfd->ctrl_handler && !ops->vidioc_s_ext_ctrls)
1275                        break;
1276                v4l_print_ext_ctrls(cmd, vfd, p, 1);
1277                if (vfd->ctrl_handler)
1278                        ret = v4l2_s_ext_ctrls(vfd->ctrl_handler, p);
1279                else if (check_ext_ctrls(p, 0))
1280                        ret = ops->vidioc_s_ext_ctrls(file, fh, p);
1281                break;
1282        }
1283        case VIDIOC_TRY_EXT_CTRLS:
1284        {
1285                struct v4l2_ext_controls *p = arg;
1286
1287                p->error_idx = p->count;
1288                if (!vfd->ctrl_handler && !ops->vidioc_try_ext_ctrls)
1289                        break;
1290                v4l_print_ext_ctrls(cmd, vfd, p, 1);
1291                if (vfd->ctrl_handler)
1292                        ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1293                else if (check_ext_ctrls(p, 0))
1294                        ret = ops->vidioc_try_ext_ctrls(file, fh, p);
1295                break;
1296        }
1297        case VIDIOC_QUERYMENU:
1298        {
1299                struct v4l2_querymenu *p = arg;
1300
1301                if (vfd->ctrl_handler)
1302                        ret = v4l2_querymenu(vfd->ctrl_handler, p);
1303                else if (ops->vidioc_querymenu)
1304                        ret = ops->vidioc_querymenu(file, fh, p);
1305                else
1306                        break;
1307                if (!ret)
1308                        dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1309                                p->id, p->index, p->name);
1310                else
1311                        dbgarg(cmd, "id=0x%x, index=%d\n",
1312                                p->id, p->index);
1313                break;
1314        }
1315        /* --- audio ---------------------------------------------- */
1316        case VIDIOC_ENUMAUDIO:
1317        {
1318                struct v4l2_audio *p = arg;
1319
1320                if (!ops->vidioc_enumaudio)
1321                        break;
1322                ret = ops->vidioc_enumaudio(file, fh, p);
1323                if (!ret)
1324                        dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1325                                        "mode=0x%x\n", p->index, p->name,
1326                                        p->capability, p->mode);
1327                else
1328                        dbgarg(cmd, "index=%d\n", p->index);
1329                break;
1330        }
1331        case VIDIOC_G_AUDIO:
1332        {
1333                struct v4l2_audio *p = arg;
1334
1335                if (!ops->vidioc_g_audio)
1336                        break;
1337
1338                ret = ops->vidioc_g_audio(file, fh, p);
1339                if (!ret)
1340                        dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1341                                        "mode=0x%x\n", p->index,
1342                                        p->name, p->capability, p->mode);
1343                else
1344                        dbgarg(cmd, "index=%d\n", p->index);
1345                break;
1346        }
1347        case VIDIOC_S_AUDIO:
1348        {
1349                struct v4l2_audio *p = arg;
1350
1351                if (!ops->vidioc_s_audio)
1352                        break;
1353                dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1354                                        "mode=0x%x\n", p->index, p->name,
1355                                        p->capability, p->mode);
1356                ret = ops->vidioc_s_audio(file, fh, p);
1357                break;
1358        }
1359        case VIDIOC_ENUMAUDOUT:
1360        {
1361                struct v4l2_audioout *p = arg;
1362
1363                if (!ops->vidioc_enumaudout)
1364                        break;
1365                dbgarg(cmd, "Enum for index=%d\n", p->index);
1366                ret = ops->vidioc_enumaudout(file, fh, p);
1367                if (!ret)
1368                        dbgarg2("index=%d, name=%s, capability=%d, "
1369                                        "mode=%d\n", p->index, p->name,
1370                                        p->capability, p->mode);
1371                break;
1372        }
1373        case VIDIOC_G_AUDOUT:
1374        {
1375                struct v4l2_audioout *p = arg;
1376
1377                if (!ops->vidioc_g_audout)
1378                        break;
1379
1380                ret = ops->vidioc_g_audout(file, fh, p);
1381                if (!ret)
1382                        dbgarg2("index=%d, name=%s, capability=%d, "
1383                                        "mode=%d\n", p->index, p->name,
1384                                        p->capability, p->mode);
1385                break;
1386        }
1387        case VIDIOC_S_AUDOUT:
1388        {
1389                struct v4l2_audioout *p = arg;
1390
1391                if (!ops->vidioc_s_audout)
1392                        break;
1393                dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1394                                        "mode=%d\n", p->index, p->name,
1395                                        p->capability, p->mode);
1396
1397                ret = ops->vidioc_s_audout(file, fh, p);
1398                break;
1399        }
1400        case VIDIOC_G_MODULATOR:
1401        {
1402                struct v4l2_modulator *p = arg;
1403
1404                if (!ops->vidioc_g_modulator)
1405                        break;
1406                ret = ops->vidioc_g_modulator(file, fh, p);
1407                if (!ret)
1408                        dbgarg(cmd, "index=%d, name=%s, "
1409                                        "capability=%d, rangelow=%d,"
1410                                        " rangehigh=%d, txsubchans=%d\n",
1411                                        p->index, p->name, p->capability,
1412                                        p->rangelow, p->rangehigh,
1413                                        p->txsubchans);
1414                break;
1415        }
1416        case VIDIOC_S_MODULATOR:
1417        {
1418                struct v4l2_modulator *p = arg;
1419
1420                if (!ops->vidioc_s_modulator)
1421                        break;
1422                dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1423                                "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1424                                p->index, p->name, p->capability, p->rangelow,
1425                                p->rangehigh, p->txsubchans);
1426                        ret = ops->vidioc_s_modulator(file, fh, p);
1427                break;
1428        }
1429        case VIDIOC_G_CROP:
1430        {
1431                struct v4l2_crop *p = arg;
1432
1433                if (!ops->vidioc_g_crop)
1434                        break;
1435
1436                dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1437                ret = ops->vidioc_g_crop(file, fh, p);
1438                if (!ret)
1439                        dbgrect(vfd, "", &p->c);
1440                break;
1441        }
1442        case VIDIOC_S_CROP:
1443        {
1444                struct v4l2_crop *p = arg;
1445
1446                if (!ops->vidioc_s_crop)
1447                        break;
1448                dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1449                dbgrect(vfd, "", &p->c);
1450                ret = ops->vidioc_s_crop(file, fh, p);
1451                break;
1452        }
1453        case VIDIOC_CROPCAP:
1454        {
1455                struct v4l2_cropcap *p = arg;
1456
1457                /*FIXME: Should also show v4l2_fract pixelaspect */
1458                if (!ops->vidioc_cropcap)
1459                        break;
1460
1461                dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1462                ret = ops->vidioc_cropcap(file, fh, p);
1463                if (!ret) {
1464                        dbgrect(vfd, "bounds ", &p->bounds);
1465                        dbgrect(vfd, "defrect ", &p->defrect);
1466                }
1467                break;
1468        }
1469        case VIDIOC_G_JPEGCOMP:
1470        {
1471                struct v4l2_jpegcompression *p = arg;
1472
1473                if (!ops->vidioc_g_jpegcomp)
1474                        break;
1475
1476                ret = ops->vidioc_g_jpegcomp(file, fh, p);
1477                if (!ret)
1478                        dbgarg(cmd, "quality=%d, APPn=%d, "
1479                                        "APP_len=%d, COM_len=%d, "
1480                                        "jpeg_markers=%d\n",
1481                                        p->quality, p->APPn, p->APP_len,
1482                                        p->COM_len, p->jpeg_markers);
1483                break;
1484        }
1485        case VIDIOC_S_JPEGCOMP:
1486        {
1487                struct v4l2_jpegcompression *p = arg;
1488
1489                if (!ops->vidioc_g_jpegcomp)
1490                        break;
1491                dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1492                                        "COM_len=%d, jpeg_markers=%d\n",
1493                                        p->quality, p->APPn, p->APP_len,
1494                                        p->COM_len, p->jpeg_markers);
1495                        ret = ops->vidioc_s_jpegcomp(file, fh, p);
1496                break;
1497        }
1498        case VIDIOC_G_ENC_INDEX:
1499        {
1500                struct v4l2_enc_idx *p = arg;
1501
1502                if (!ops->vidioc_g_enc_index)
1503                        break;
1504                ret = ops->vidioc_g_enc_index(file, fh, p);
1505                if (!ret)
1506                        dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1507                                        p->entries, p->entries_cap);
1508                break;
1509        }
1510        case VIDIOC_ENCODER_CMD:
1511        {
1512                struct v4l2_encoder_cmd *p = arg;
1513
1514                if (!ops->vidioc_encoder_cmd)
1515                        break;
1516                ret = ops->vidioc_encoder_cmd(file, fh, p);
1517                if (!ret)
1518                        dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1519                break;
1520        }
1521        case VIDIOC_TRY_ENCODER_CMD:
1522        {
1523                struct v4l2_encoder_cmd *p = arg;
1524
1525                if (!ops->vidioc_try_encoder_cmd)
1526                        break;
1527                ret = ops->vidioc_try_encoder_cmd(file, fh, p);
1528                if (!ret)
1529                        dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1530                break;
1531        }
1532        case VIDIOC_G_PARM:
1533        {
1534                struct v4l2_streamparm *p = arg;
1535
1536                if (ops->vidioc_g_parm) {
1537                        ret = check_fmt(ops, p->type);
1538                        if (ret)
1539                                break;
1540                        ret = ops->vidioc_g_parm(file, fh, p);
1541                } else {
1542                        v4l2_std_id std = vfd->current_norm;
1543
1544                        if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1545                                break;
1546
1547                        ret = 0;
1548                        if (ops->vidioc_g_std)
1549                                ret = ops->vidioc_g_std(file, fh, &std);
1550                        else if (std == 0)
1551                                ret = -EINVAL;
1552                        if (ret == 0)
1553                                v4l2_video_std_frame_period(std,
1554                                                    &p->parm.capture.timeperframe);
1555                }
1556
1557                dbgarg(cmd, "type=%d\n", p->type);
1558                break;
1559        }
1560        case VIDIOC_S_PARM:
1561        {
1562                struct v4l2_streamparm *p = arg;
1563
1564                if (!ops->vidioc_s_parm)
1565                        break;
1566                ret = check_fmt(ops, p->type);
1567                if (ret)
1568                        break;
1569
1570                dbgarg(cmd, "type=%d\n", p->type);
1571                ret = ops->vidioc_s_parm(file, fh, p);
1572                break;
1573        }
1574        case VIDIOC_G_TUNER:
1575        {
1576                struct v4l2_tuner *p = arg;
1577
1578                if (!ops->vidioc_g_tuner)
1579                        break;
1580
1581                ret = ops->vidioc_g_tuner(file, fh, p);
1582                if (!ret)
1583                        dbgarg(cmd, "index=%d, name=%s, type=%d, "
1584                                        "capability=0x%x, rangelow=%d, "
1585                                        "rangehigh=%d, signal=%d, afc=%d, "
1586                                        "rxsubchans=0x%x, audmode=%d\n",
1587                                        p->index, p->name, p->type,
1588                                        p->capability, p->rangelow,
1589                                        p->rangehigh, p->signal, p->afc,
1590                                        p->rxsubchans, p->audmode);
1591                break;
1592        }
1593        case VIDIOC_S_TUNER:
1594        {
1595                struct v4l2_tuner *p = arg;
1596
1597                if (!ops->vidioc_s_tuner)
1598                        break;
1599                dbgarg(cmd, "index=%d, name=%s, type=%d, "
1600                                "capability=0x%x, rangelow=%d, "
1601                                "rangehigh=%d, signal=%d, afc=%d, "
1602                                "rxsubchans=0x%x, audmode=%d\n",
1603                                p->index, p->name, p->type,
1604                                p->capability, p->rangelow,
1605                                p->rangehigh, p->signal, p->afc,
1606                                p->rxsubchans, p->audmode);
1607                ret = ops->vidioc_s_tuner(file, fh, p);
1608                break;
1609        }
1610        case VIDIOC_G_FREQUENCY:
1611        {
1612                struct v4l2_frequency *p = arg;
1613
1614                if (!ops->vidioc_g_frequency)
1615                        break;
1616
1617                ret = ops->vidioc_g_frequency(file, fh, p);
1618                if (!ret)
1619                        dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1620                                        p->tuner, p->type, p->frequency);
1621                break;
1622        }
1623        case VIDIOC_S_FREQUENCY:
1624        {
1625                struct v4l2_frequency *p = arg;
1626
1627                if (!ops->vidioc_s_frequency)
1628                        break;
1629                dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1630                                p->tuner, p->type, p->frequency);
1631                ret = ops->vidioc_s_frequency(file, fh, p);
1632                break;
1633        }
1634        case VIDIOC_G_SLICED_VBI_CAP:
1635        {
1636                struct v4l2_sliced_vbi_cap *p = arg;
1637
1638                if (!ops->vidioc_g_sliced_vbi_cap)
1639                        break;
1640
1641                /* Clear up to type, everything after type is zerod already */
1642                memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1643
1644                dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1645                ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1646                if (!ret)
1647                        dbgarg2("service_set=%d\n", p->service_set);
1648                break;
1649        }
1650        case VIDIOC_LOG_STATUS:
1651        {
1652                if (!ops->vidioc_log_status)
1653                        break;
1654                ret = ops->vidioc_log_status(file, fh);
1655                break;
1656        }
1657#ifdef CONFIG_VIDEO_ADV_DEBUG
1658        case VIDIOC_DBG_G_REGISTER:
1659        {
1660                struct v4l2_dbg_register *p = arg;
1661
1662                if (ops->vidioc_g_register) {
1663                        if (!capable(CAP_SYS_ADMIN))
1664                                ret = -EPERM;
1665                        else
1666                                ret = ops->vidioc_g_register(file, fh, p);
1667                }
1668                break;
1669        }
1670        case VIDIOC_DBG_S_REGISTER:
1671        {
1672                struct v4l2_dbg_register *p = arg;
1673
1674                if (ops->vidioc_s_register) {
1675                        if (!capable(CAP_SYS_ADMIN))
1676                                ret = -EPERM;
1677                        else
1678                                ret = ops->vidioc_s_register(file, fh, p);
1679                }
1680                break;
1681        }
1682#endif
1683        case VIDIOC_DBG_G_CHIP_IDENT:
1684        {
1685                struct v4l2_dbg_chip_ident *p = arg;
1686
1687                if (!ops->vidioc_g_chip_ident)
1688                        break;
1689                p->ident = V4L2_IDENT_NONE;
1690                p->revision = 0;
1691                ret = ops->vidioc_g_chip_ident(file, fh, p);
1692                if (!ret)
1693                        dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1694                break;
1695        }
1696        case VIDIOC_S_HW_FREQ_SEEK:
1697        {
1698                struct v4l2_hw_freq_seek *p = arg;
1699
1700                if (!ops->vidioc_s_hw_freq_seek)
1701                        break;
1702                dbgarg(cmd,
1703                        "tuner=%d, type=%d, seek_upward=%d, wrap_around=%d\n",
1704                        p->tuner, p->type, p->seek_upward, p->wrap_around);
1705                ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
1706                break;
1707        }
1708        case VIDIOC_ENUM_FRAMESIZES:
1709        {
1710                struct v4l2_frmsizeenum *p = arg;
1711
1712                if (!ops->vidioc_enum_framesizes)
1713                        break;
1714
1715                ret = ops->vidioc_enum_framesizes(file, fh, p);
1716                dbgarg(cmd,
1717                        "index=%d, pixelformat=%c%c%c%c, type=%d ",
1718                        p->index,
1719                        (p->pixel_format & 0xff),
1720                        (p->pixel_format >>  8) & 0xff,
1721                        (p->pixel_format >> 16) & 0xff,
1722                        (p->pixel_format >> 24) & 0xff,
1723                        p->type);
1724                switch (p->type) {
1725                case V4L2_FRMSIZE_TYPE_DISCRETE:
1726                        dbgarg3("width = %d, height=%d\n",
1727                                p->discrete.width, p->discrete.height);
1728                        break;
1729                case V4L2_FRMSIZE_TYPE_STEPWISE:
1730                        dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
1731                                p->stepwise.min_width,  p->stepwise.min_height,
1732                                p->stepwise.step_width, p->stepwise.step_height,
1733                                p->stepwise.max_width,  p->stepwise.max_height);
1734                        break;
1735                case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1736                        dbgarg3("continuous\n");
1737                        break;
1738                default:
1739                        dbgarg3("- Unknown type!\n");
1740                }
1741
1742                break;
1743        }
1744        case VIDIOC_ENUM_FRAMEINTERVALS:
1745        {
1746                struct v4l2_frmivalenum *p = arg;
1747
1748                if (!ops->vidioc_enum_frameintervals)
1749                        break;
1750
1751                ret = ops->vidioc_enum_frameintervals(file, fh, p);
1752                dbgarg(cmd,
1753                        "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1754                        p->index, p->pixel_format,
1755                        p->width, p->height, p->type);
1756                switch (p->type) {
1757                case V4L2_FRMIVAL_TYPE_DISCRETE:
1758                        dbgarg2("fps=%d/%d\n",
1759                                p->discrete.numerator,
1760                                p->discrete.denominator);
1761                        break;
1762                case V4L2_FRMIVAL_TYPE_STEPWISE:
1763                        dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1764                                p->stepwise.min.numerator,
1765                                p->stepwise.min.denominator,
1766                                p->stepwise.max.numerator,
1767                                p->stepwise.max.denominator,
1768                                p->stepwise.step.numerator,
1769                                p->stepwise.step.denominator);
1770                        break;
1771                case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1772                        dbgarg2("continuous\n");
1773                        break;
1774                default:
1775                        dbgarg2("- Unknown type!\n");
1776                }
1777                break;
1778        }
1779        case VIDIOC_ENUM_DV_PRESETS:
1780        {
1781                struct v4l2_dv_enum_preset *p = arg;
1782
1783                if (!ops->vidioc_enum_dv_presets)
1784                        break;
1785
1786                ret = ops->vidioc_enum_dv_presets(file, fh, p);
1787                if (!ret)
1788                        dbgarg(cmd,
1789                                "index=%d, preset=%d, name=%s, width=%d,"
1790                                " height=%d ",
1791                                p->index, p->preset, p->name, p->width,
1792                                p->height);
1793                break;
1794        }
1795        case VIDIOC_S_DV_PRESET:
1796        {
1797                struct v4l2_dv_preset *p = arg;
1798
1799                if (!ops->vidioc_s_dv_preset)
1800                        break;
1801
1802                dbgarg(cmd, "preset=%d\n", p->preset);
1803                ret = ops->vidioc_s_dv_preset(file, fh, p);
1804                break;
1805        }
1806        case VIDIOC_G_DV_PRESET:
1807        {
1808                struct v4l2_dv_preset *p = arg;
1809
1810                if (!ops->vidioc_g_dv_preset)
1811                        break;
1812
1813                ret = ops->vidioc_g_dv_preset(file, fh, p);
1814                if (!ret)
1815                        dbgarg(cmd, "preset=%d\n", p->preset);
1816                break;
1817        }
1818        case VIDIOC_QUERY_DV_PRESET:
1819        {
1820                struct v4l2_dv_preset *p = arg;
1821
1822                if (!ops->vidioc_query_dv_preset)
1823                        break;
1824
1825                ret = ops->vidioc_query_dv_preset(file, fh, p);
1826                if (!ret)
1827                        dbgarg(cmd, "preset=%d\n", p->preset);
1828                break;
1829        }
1830        case VIDIOC_S_DV_TIMINGS:
1831        {
1832                struct v4l2_dv_timings *p = arg;
1833
1834                if (!ops->vidioc_s_dv_timings)
1835                        break;
1836
1837                switch (p->type) {
1838                case V4L2_DV_BT_656_1120:
1839                        dbgarg2("bt-656/1120:interlaced=%d, pixelclock=%lld,"
1840                                " width=%d, height=%d, polarities=%x,"
1841                                " hfrontporch=%d, hsync=%d, hbackporch=%d,"
1842                                " vfrontporch=%d, vsync=%d, vbackporch=%d,"
1843                                " il_vfrontporch=%d, il_vsync=%d,"
1844                                " il_vbackporch=%d\n",
1845                                p->bt.interlaced, p->bt.pixelclock,
1846                                p->bt.width, p->bt.height, p->bt.polarities,
1847                                p->bt.hfrontporch, p->bt.hsync,
1848                                p->bt.hbackporch, p->bt.vfrontporch,
1849                                p->bt.vsync, p->bt.vbackporch,
1850                                p->bt.il_vfrontporch, p->bt.il_vsync,
1851                                p->bt.il_vbackporch);
1852                        ret = ops->vidioc_s_dv_timings(file, fh, p);
1853                        break;
1854                default:
1855                        dbgarg2("Unknown type %d!\n", p->type);
1856                        break;
1857                }
1858                break;
1859        }
1860        case VIDIOC_G_DV_TIMINGS:
1861        {
1862                struct v4l2_dv_timings *p = arg;
1863
1864                if (!ops->vidioc_g_dv_timings)
1865                        break;
1866
1867                ret = ops->vidioc_g_dv_timings(file, fh, p);
1868                if (!ret) {
1869                        switch (p->type) {
1870                        case V4L2_DV_BT_656_1120:
1871                                dbgarg2("bt-656/1120:interlaced=%d,"
1872                                        " pixelclock=%lld,"
1873                                        " width=%d, height=%d, polarities=%x,"
1874                                        " hfrontporch=%d, hsync=%d,"
1875                                        " hbackporch=%d, vfrontporch=%d,"
1876                                        " vsync=%d, vbackporch=%d,"
1877                                        " il_vfrontporch=%d, il_vsync=%d,"
1878                                        " il_vbackporch=%d\n",
1879                                        p->bt.interlaced, p->bt.pixelclock,
1880                                        p->bt.width, p->bt.height,
1881                                        p->bt.polarities, p->bt.hfrontporch,
1882                                        p->bt.hsync, p->bt.hbackporch,
1883                                        p->bt.vfrontporch, p->bt.vsync,
1884                                        p->bt.vbackporch, p->bt.il_vfrontporch,
1885                                        p->bt.il_vsync, p->bt.il_vbackporch);
1886                                break;
1887                        default:
1888                                dbgarg2("Unknown type %d!\n", p->type);
1889                                break;
1890                        }
1891                }
1892                break;
1893        }
1894        case VIDIOC_DQEVENT:
1895        {
1896                struct v4l2_event *ev = arg;
1897
1898                if (!ops->vidioc_subscribe_event)
1899                        break;
1900
1901                ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
1902                if (ret < 0) {
1903                        dbgarg(cmd, "no pending events?");
1904                        break;
1905                }
1906                dbgarg(cmd,
1907                       "pending=%d, type=0x%8.8x, sequence=%d, "
1908                       "timestamp=%lu.%9.9lu ",
1909                       ev->pending, ev->type, ev->sequence,
1910                       ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
1911                break;
1912        }
1913        case VIDIOC_SUBSCRIBE_EVENT:
1914        {
1915                struct v4l2_event_subscription *sub = arg;
1916
1917                if (!ops->vidioc_subscribe_event)
1918                        break;
1919
1920                ret = ops->vidioc_subscribe_event(fh, sub);
1921                if (ret < 0) {
1922                        dbgarg(cmd, "failed, ret=%ld", ret);
1923                        break;
1924                }
1925                dbgarg(cmd, "type=0x%8.8x", sub->type);
1926                break;
1927        }
1928        case VIDIOC_UNSUBSCRIBE_EVENT:
1929        {
1930                struct v4l2_event_subscription *sub = arg;
1931
1932                if (!ops->vidioc_unsubscribe_event)
1933                        break;
1934
1935                ret = ops->vidioc_unsubscribe_event(fh, sub);
1936                if (ret < 0) {
1937                        dbgarg(cmd, "failed, ret=%ld", ret);
1938                        break;
1939                }
1940                dbgarg(cmd, "type=0x%8.8x", sub->type);
1941                break;
1942        }
1943        default:
1944        {
1945                if (!ops->vidioc_default)
1946                        break;
1947                ret = ops->vidioc_default(file, fh, cmd, arg);
1948                break;
1949        }
1950        } /* switch */
1951
1952        if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1953                if (ret < 0) {
1954                        v4l_print_ioctl(vfd->name, cmd);
1955                        printk(KERN_CONT " error %ld\n", ret);
1956                }
1957        }
1958
1959        return ret;
1960}
1961
1962/* In some cases, only a few fields are used as input, i.e. when the app sets
1963 * "index" and then the driver fills in the rest of the structure for the thing
1964 * with that index.  We only need to copy up the first non-input field.  */
1965static unsigned long cmd_input_size(unsigned int cmd)
1966{
1967        /* Size of structure up to and including 'field' */
1968#define CMDINSIZE(cmd, type, field)                             \
1969        case VIDIOC_##cmd:                                      \
1970                return offsetof(struct v4l2_##type, field) +    \
1971                        sizeof(((struct v4l2_##type *)0)->field);
1972
1973        switch (cmd) {
1974                CMDINSIZE(ENUM_FMT,             fmtdesc,        type);
1975                CMDINSIZE(G_FMT,                format,         type);
1976                CMDINSIZE(QUERYBUF,             buffer,         type);
1977                CMDINSIZE(G_PARM,               streamparm,     type);
1978                CMDINSIZE(ENUMSTD,              standard,       index);
1979                CMDINSIZE(ENUMINPUT,            input,          index);
1980                CMDINSIZE(G_CTRL,               control,        id);
1981                CMDINSIZE(G_TUNER,              tuner,          index);
1982                CMDINSIZE(QUERYCTRL,            queryctrl,      id);
1983                CMDINSIZE(QUERYMENU,            querymenu,      index);
1984                CMDINSIZE(ENUMOUTPUT,           output,         index);
1985                CMDINSIZE(G_MODULATOR,          modulator,      index);
1986                CMDINSIZE(G_FREQUENCY,          frequency,      tuner);
1987                CMDINSIZE(CROPCAP,              cropcap,        type);
1988                CMDINSIZE(G_CROP,               crop,           type);
1989                CMDINSIZE(ENUMAUDIO,            audio,          index);
1990                CMDINSIZE(ENUMAUDOUT,           audioout,       index);
1991                CMDINSIZE(ENCODER_CMD,          encoder_cmd,    flags);
1992                CMDINSIZE(TRY_ENCODER_CMD,      encoder_cmd,    flags);
1993                CMDINSIZE(G_SLICED_VBI_CAP,     sliced_vbi_cap, type);
1994                CMDINSIZE(ENUM_FRAMESIZES,      frmsizeenum,    pixel_format);
1995                CMDINSIZE(ENUM_FRAMEINTERVALS,  frmivalenum,    height);
1996        default:
1997                return _IOC_SIZE(cmd);
1998        }
1999}
2000
2001long video_ioctl2(struct file *file,
2002               unsigned int cmd, unsigned long arg)
2003{
2004        char    sbuf[128];
2005        void    *mbuf = NULL;
2006        void    *parg = (void *)arg;
2007        long    err  = -EINVAL;
2008        int     is_ext_ctrl;
2009        size_t  ctrls_size = 0;
2010        void __user *user_ptr = NULL;
2011
2012#ifdef __OLD_VIDIOC_
2013        cmd = video_fix_command(cmd);
2014#endif
2015        is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
2016                       cmd == VIDIOC_TRY_EXT_CTRLS);
2017
2018        /*  Copy arguments into temp kernel buffer  */
2019        if (_IOC_DIR(cmd) != _IOC_NONE) {
2020                if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2021                        parg = sbuf;
2022                } else {
2023                        /* too big to allocate from stack */
2024                        mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2025                        if (NULL == mbuf)
2026                                return -ENOMEM;
2027                        parg = mbuf;
2028                }
2029
2030                err = -EFAULT;
2031                if (_IOC_DIR(cmd) & _IOC_WRITE) {
2032                        unsigned long n = cmd_input_size(cmd);
2033
2034                        if (copy_from_user(parg, (void __user *)arg, n))
2035                                goto out;
2036
2037                        /* zero out anything we don't copy from userspace */
2038                        if (n < _IOC_SIZE(cmd))
2039                                memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
2040                } else {
2041                        /* read-only ioctl */
2042                        memset(parg, 0, _IOC_SIZE(cmd));
2043                }
2044        }
2045
2046        if (is_ext_ctrl) {
2047                struct v4l2_ext_controls *p = parg;
2048
2049                /* In case of an error, tell the caller that it wasn't
2050                   a specific control that caused it. */
2051                p->error_idx = p->count;
2052                user_ptr = (void __user *)p->controls;
2053                if (p->count) {
2054                        ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
2055                        /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
2056                        mbuf = kmalloc(ctrls_size, GFP_KERNEL);
2057                        err = -ENOMEM;
2058                        if (NULL == mbuf)
2059                                goto out_ext_ctrl;
2060                        err = -EFAULT;
2061                        if (copy_from_user(mbuf, user_ptr, ctrls_size))
2062                                goto out_ext_ctrl;
2063                        p->controls = mbuf;
2064                }
2065        }
2066
2067        /* Handles IOCTL */
2068        err = __video_do_ioctl(file, cmd, parg);
2069        if (err == -ENOIOCTLCMD)
2070                err = -EINVAL;
2071        if (is_ext_ctrl) {
2072                struct v4l2_ext_controls *p = parg;
2073
2074                p->controls = (void *)user_ptr;
2075                if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
2076                        err = -EFAULT;
2077                goto out_ext_ctrl;
2078        }
2079        if (err < 0)
2080                goto out;
2081
2082out_ext_ctrl:
2083        /*  Copy results into user buffer  */
2084        switch (_IOC_DIR(cmd)) {
2085        case _IOC_READ:
2086        case (_IOC_WRITE | _IOC_READ):
2087                if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2088                        err = -EFAULT;
2089                break;
2090        }
2091
2092out:
2093        kfree(mbuf);
2094        return err;
2095}
2096EXPORT_SYMBOL(video_ioctl2);
2097