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