Linux Audio

Check our new training course

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