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