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