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