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