]> err.no Git - linux-2.6/blob - drivers/media/video/videodev.c
V4L/DVB (8083): videodev: zero fields for ENCODER_CMD and VIDIOC_G_SLICED_VBI_CAP
[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         [_IOC_NR(VIDIOC_S_HW_FREQ_SEEK)]   = "VIDIOC_S_HW_FREQ_SEEK",
282 #endif
283 };
284 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
285
286 static const char *v4l2_int_ioctls[] = {
287 #ifdef CONFIG_VIDEO_V4L1_COMPAT
288         [_IOC_NR(DECODER_GET_CAPABILITIES)]    = "DECODER_GET_CAPABILITIES",
289         [_IOC_NR(DECODER_GET_STATUS)]          = "DECODER_GET_STATUS",
290         [_IOC_NR(DECODER_SET_NORM)]            = "DECODER_SET_NORM",
291         [_IOC_NR(DECODER_SET_INPUT)]           = "DECODER_SET_INPUT",
292         [_IOC_NR(DECODER_SET_OUTPUT)]          = "DECODER_SET_OUTPUT",
293         [_IOC_NR(DECODER_ENABLE_OUTPUT)]       = "DECODER_ENABLE_OUTPUT",
294         [_IOC_NR(DECODER_SET_PICTURE)]         = "DECODER_SET_PICTURE",
295         [_IOC_NR(DECODER_SET_GPIO)]            = "DECODER_SET_GPIO",
296         [_IOC_NR(DECODER_INIT)]                = "DECODER_INIT",
297         [_IOC_NR(DECODER_SET_VBI_BYPASS)]      = "DECODER_SET_VBI_BYPASS",
298         [_IOC_NR(DECODER_DUMP)]                = "DECODER_DUMP",
299 #endif
300         [_IOC_NR(AUDC_SET_RADIO)]              = "AUDC_SET_RADIO",
301
302         [_IOC_NR(TUNER_SET_TYPE_ADDR)]         = "TUNER_SET_TYPE_ADDR",
303         [_IOC_NR(TUNER_SET_STANDBY)]           = "TUNER_SET_STANDBY",
304         [_IOC_NR(TUNER_SET_CONFIG)]            = "TUNER_SET_CONFIG",
305
306         [_IOC_NR(VIDIOC_INT_S_TUNER_MODE)]     = "VIDIOC_INT_S_TUNER_MODE",
307         [_IOC_NR(VIDIOC_INT_RESET)]            = "VIDIOC_INT_RESET",
308         [_IOC_NR(VIDIOC_INT_AUDIO_CLOCK_FREQ)] = "VIDIOC_INT_AUDIO_CLOCK_FREQ",
309         [_IOC_NR(VIDIOC_INT_DECODE_VBI_LINE)]  = "VIDIOC_INT_DECODE_VBI_LINE",
310         [_IOC_NR(VIDIOC_INT_S_VBI_DATA)]       = "VIDIOC_INT_S_VBI_DATA",
311         [_IOC_NR(VIDIOC_INT_G_VBI_DATA)]       = "VIDIOC_INT_G_VBI_DATA",
312         [_IOC_NR(VIDIOC_INT_I2S_CLOCK_FREQ)]   = "VIDIOC_INT_I2S_CLOCK_FREQ",
313         [_IOC_NR(VIDIOC_INT_S_STANDBY)]        = "VIDIOC_INT_S_STANDBY",
314         [_IOC_NR(VIDIOC_INT_S_AUDIO_ROUTING)]  = "VIDIOC_INT_S_AUDIO_ROUTING",
315         [_IOC_NR(VIDIOC_INT_G_AUDIO_ROUTING)]  = "VIDIOC_INT_G_AUDIO_ROUTING",
316         [_IOC_NR(VIDIOC_INT_S_VIDEO_ROUTING)]  = "VIDIOC_INT_S_VIDEO_ROUTING",
317         [_IOC_NR(VIDIOC_INT_G_VIDEO_ROUTING)]  = "VIDIOC_INT_G_VIDEO_ROUTING",
318         [_IOC_NR(VIDIOC_INT_S_CRYSTAL_FREQ)]   = "VIDIOC_INT_S_CRYSTAL_FREQ",
319         [_IOC_NR(VIDIOC_INT_INIT)]             = "VIDIOC_INT_INIT",
320         [_IOC_NR(VIDIOC_INT_G_STD_OUTPUT)]     = "VIDIOC_INT_G_STD_OUTPUT",
321         [_IOC_NR(VIDIOC_INT_S_STD_OUTPUT)]     = "VIDIOC_INT_S_STD_OUTPUT",
322 };
323 #define V4L2_INT_IOCTLS ARRAY_SIZE(v4l2_int_ioctls)
324
325 /* Common ioctl debug function. This function can be used by
326    external ioctl messages as well as internal V4L ioctl */
327 void v4l_printk_ioctl(unsigned int cmd)
328 {
329         char *dir, *type;
330
331         switch (_IOC_TYPE(cmd)) {
332         case 'd':
333                 if (_IOC_NR(cmd) >= V4L2_INT_IOCTLS) {
334                         type = "v4l2_int";
335                         break;
336                 }
337                 printk("%s", v4l2_int_ioctls[_IOC_NR(cmd)]);
338                 return;
339 #ifdef CONFIG_VIDEO_V4L1_COMPAT
340         case 'v':
341                 if (_IOC_NR(cmd) >= V4L1_IOCTLS) {
342                         type = "v4l1";
343                         break;
344                 }
345                 printk("%s", v4l1_ioctls[_IOC_NR(cmd)]);
346                 return;
347 #endif
348         case 'V':
349                 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
350                         type = "v4l2";
351                         break;
352                 }
353                 printk("%s", v4l2_ioctls[_IOC_NR(cmd)]);
354                 return;
355         default:
356                 type = "unknown";
357         }
358
359         switch (_IOC_DIR(cmd)) {
360         case _IOC_NONE:              dir = "--"; break;
361         case _IOC_READ:              dir = "r-"; break;
362         case _IOC_WRITE:             dir = "-w"; break;
363         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
364         default:                     dir = "*ERR*"; break;
365         }
366         printk("%s ioctl '%c', dir=%s, #%d (0x%08x)",
367                 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
368 }
369 EXPORT_SYMBOL(v4l_printk_ioctl);
370
371 /*
372  *      sysfs stuff
373  */
374
375 static ssize_t show_index(struct device *cd,
376                          struct device_attribute *attr, char *buf)
377 {
378         struct video_device *vfd = container_of(cd, struct video_device,
379                                                 class_dev);
380         return sprintf(buf, "%i\n", vfd->index);
381 }
382
383 static ssize_t show_name(struct device *cd,
384                          struct device_attribute *attr, char *buf)
385 {
386         struct video_device *vfd = container_of(cd, struct video_device,
387                                                 class_dev);
388         return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
389 }
390
391 struct video_device *video_device_alloc(void)
392 {
393         struct video_device *vfd;
394
395         vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
396         return vfd;
397 }
398 EXPORT_SYMBOL(video_device_alloc);
399
400 void video_device_release(struct video_device *vfd)
401 {
402         kfree(vfd);
403 }
404 EXPORT_SYMBOL(video_device_release);
405
406 static void video_release(struct device *cd)
407 {
408         struct video_device *vfd = container_of(cd, struct video_device,
409                                                                 class_dev);
410
411 #if 1
412         /* needed until all drivers are fixed */
413         if (!vfd->release)
414                 return;
415 #endif
416         vfd->release(vfd);
417 }
418
419 static struct device_attribute video_device_attrs[] = {
420         __ATTR(name, S_IRUGO, show_name, NULL),
421         __ATTR(index, S_IRUGO, show_index, NULL),
422         __ATTR_NULL
423 };
424
425 static struct class video_class = {
426         .name    = VIDEO_NAME,
427         .dev_attrs = video_device_attrs,
428         .dev_release = video_release,
429 };
430
431 /*
432  *      Active devices
433  */
434
435 static struct video_device *video_device[VIDEO_NUM_DEVICES];
436 static DEFINE_MUTEX(videodev_lock);
437
438 struct video_device* video_devdata(struct file *file)
439 {
440         return video_device[iminor(file->f_path.dentry->d_inode)];
441 }
442 EXPORT_SYMBOL(video_devdata);
443
444 /*
445  *      Open a video device - FIXME: Obsoleted
446  */
447 static int video_open(struct inode *inode, struct file *file)
448 {
449         unsigned int minor = iminor(inode);
450         int err = 0;
451         struct video_device *vfl;
452         const struct file_operations *old_fops;
453
454         if(minor>=VIDEO_NUM_DEVICES)
455                 return -ENODEV;
456         lock_kernel();
457         mutex_lock(&videodev_lock);
458         vfl=video_device[minor];
459         if(vfl==NULL) {
460                 mutex_unlock(&videodev_lock);
461                 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
462                 mutex_lock(&videodev_lock);
463                 vfl=video_device[minor];
464                 if (vfl==NULL) {
465                         mutex_unlock(&videodev_lock);
466                         unlock_kernel();
467                         return -ENODEV;
468                 }
469         }
470         old_fops = file->f_op;
471         file->f_op = fops_get(vfl->fops);
472         if(file->f_op->open)
473                 err = file->f_op->open(inode,file);
474         if (err) {
475                 fops_put(file->f_op);
476                 file->f_op = fops_get(old_fops);
477         }
478         fops_put(old_fops);
479         mutex_unlock(&videodev_lock);
480         unlock_kernel();
481         return err;
482 }
483
484 /*
485  * helper function -- handles userspace copying for ioctl arguments
486  */
487
488 #ifdef __OLD_VIDIOC_
489 static unsigned int
490 video_fix_command(unsigned int cmd)
491 {
492         switch (cmd) {
493         case VIDIOC_OVERLAY_OLD:
494                 cmd = VIDIOC_OVERLAY;
495                 break;
496         case VIDIOC_S_PARM_OLD:
497                 cmd = VIDIOC_S_PARM;
498                 break;
499         case VIDIOC_S_CTRL_OLD:
500                 cmd = VIDIOC_S_CTRL;
501                 break;
502         case VIDIOC_G_AUDIO_OLD:
503                 cmd = VIDIOC_G_AUDIO;
504                 break;
505         case VIDIOC_G_AUDOUT_OLD:
506                 cmd = VIDIOC_G_AUDOUT;
507                 break;
508         case VIDIOC_CROPCAP_OLD:
509                 cmd = VIDIOC_CROPCAP;
510                 break;
511         }
512         return cmd;
513 }
514 #endif
515
516 /*
517  * Obsolete usercopy function - Should be removed soon
518  */
519 int
520 video_usercopy(struct inode *inode, struct file *file,
521                unsigned int cmd, unsigned long arg,
522                int (*func)(struct inode *inode, struct file *file,
523                            unsigned int cmd, void *arg))
524 {
525         char    sbuf[128];
526         void    *mbuf = NULL;
527         void    *parg = NULL;
528         int     err  = -EINVAL;
529         int     is_ext_ctrl;
530         size_t  ctrls_size = 0;
531         void __user *user_ptr = NULL;
532
533 #ifdef __OLD_VIDIOC_
534         cmd = video_fix_command(cmd);
535 #endif
536         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
537                        cmd == VIDIOC_TRY_EXT_CTRLS);
538
539         /*  Copy arguments into temp kernel buffer  */
540         switch (_IOC_DIR(cmd)) {
541         case _IOC_NONE:
542                 parg = NULL;
543                 break;
544         case _IOC_READ:
545         case _IOC_WRITE:
546         case (_IOC_WRITE | _IOC_READ):
547                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
548                         parg = sbuf;
549                 } else {
550                         /* too big to allocate from stack */
551                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
552                         if (NULL == mbuf)
553                                 return -ENOMEM;
554                         parg = mbuf;
555                 }
556
557                 err = -EFAULT;
558                 if (_IOC_DIR(cmd) & _IOC_WRITE)
559                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
560                                 goto out;
561                 break;
562         }
563         if (is_ext_ctrl) {
564                 struct v4l2_ext_controls *p = parg;
565
566                 /* In case of an error, tell the caller that it wasn't
567                    a specific control that caused it. */
568                 p->error_idx = p->count;
569                 user_ptr = (void __user *)p->controls;
570                 if (p->count) {
571                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
572                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
573                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
574                         err = -ENOMEM;
575                         if (NULL == mbuf)
576                                 goto out_ext_ctrl;
577                         err = -EFAULT;
578                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
579                                 goto out_ext_ctrl;
580                         p->controls = mbuf;
581                 }
582         }
583
584         /* call driver */
585         err = func(inode, file, cmd, parg);
586         if (err == -ENOIOCTLCMD)
587                 err = -EINVAL;
588         if (is_ext_ctrl) {
589                 struct v4l2_ext_controls *p = parg;
590
591                 p->controls = (void *)user_ptr;
592                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
593                         err = -EFAULT;
594                 goto out_ext_ctrl;
595         }
596         if (err < 0)
597                 goto out;
598
599 out_ext_ctrl:
600         /*  Copy results into user buffer  */
601         switch (_IOC_DIR(cmd))
602         {
603         case _IOC_READ:
604         case (_IOC_WRITE | _IOC_READ):
605                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
606                         err = -EFAULT;
607                 break;
608         }
609
610 out:
611         kfree(mbuf);
612         return err;
613 }
614 EXPORT_SYMBOL(video_usercopy);
615
616 /*
617  * open/release helper functions -- handle exclusive opens
618  * Should be removed soon
619  */
620 int video_exclusive_open(struct inode *inode, struct file *file)
621 {
622         struct  video_device *vfl = video_devdata(file);
623         int retval = 0;
624
625         mutex_lock(&vfl->lock);
626         if (vfl->users) {
627                 retval = -EBUSY;
628         } else {
629                 vfl->users++;
630         }
631         mutex_unlock(&vfl->lock);
632         return retval;
633 }
634 EXPORT_SYMBOL(video_exclusive_open);
635
636 int video_exclusive_release(struct inode *inode, struct file *file)
637 {
638         struct  video_device *vfl = video_devdata(file);
639
640         vfl->users--;
641         return 0;
642 }
643 EXPORT_SYMBOL(video_exclusive_release);
644
645 static void dbgbuf(unsigned int cmd, struct video_device *vfd,
646                                         struct v4l2_buffer *p)
647 {
648         struct v4l2_timecode *tc=&p->timecode;
649
650         dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
651                 "bytesused=%d, flags=0x%08d, "
652                 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
653                         (p->timestamp.tv_sec/3600),
654                         (int)(p->timestamp.tv_sec/60)%60,
655                         (int)(p->timestamp.tv_sec%60),
656                         p->timestamp.tv_usec,
657                         p->index,
658                         prt_names(p->type, v4l2_type_names),
659                         p->bytesused, p->flags,
660                         p->field, p->sequence,
661                         prt_names(p->memory, v4l2_memory_names),
662                         p->m.userptr, p->length);
663         dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
664                 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
665                         tc->hours,tc->minutes,tc->seconds,
666                         tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
667 }
668
669 static inline void dbgrect(struct video_device *vfd, char *s,
670                                                         struct v4l2_rect *r)
671 {
672         dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
673                                                 r->width, r->height);
674 };
675
676 static inline void v4l_print_pix_fmt (struct video_device *vfd,
677                                                 struct v4l2_pix_format *fmt)
678 {
679         dbgarg2 ("width=%d, height=%d, format=%c%c%c%c, field=%s, "
680                 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
681                 fmt->width,fmt->height,
682                 (fmt->pixelformat & 0xff),
683                 (fmt->pixelformat >>  8) & 0xff,
684                 (fmt->pixelformat >> 16) & 0xff,
685                 (fmt->pixelformat >> 24) & 0xff,
686                 prt_names(fmt->field, v4l2_field_names),
687                 fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
688 };
689
690
691 static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
692 {
693         switch (type) {
694         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
695                 if (vfd->vidioc_try_fmt_vid_cap)
696                         return (0);
697                 break;
698         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
699                 if (vfd->vidioc_try_fmt_vid_overlay)
700                         return (0);
701                 break;
702         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
703                 if (vfd->vidioc_try_fmt_vid_out)
704                         return (0);
705                 break;
706         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
707                 if (vfd->vidioc_try_fmt_vid_out_overlay)
708                         return (0);
709                 break;
710         case V4L2_BUF_TYPE_VBI_CAPTURE:
711                 if (vfd->vidioc_try_fmt_vbi_cap)
712                         return (0);
713                 break;
714         case V4L2_BUF_TYPE_VBI_OUTPUT:
715                 if (vfd->vidioc_try_fmt_vbi_out)
716                         return (0);
717                 break;
718         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
719                 if (vfd->vidioc_try_fmt_sliced_vbi_cap)
720                         return (0);
721                 break;
722         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
723                 if (vfd->vidioc_try_fmt_sliced_vbi_out)
724                         return (0);
725                 break;
726         case V4L2_BUF_TYPE_PRIVATE:
727                 if (vfd->vidioc_try_fmt_type_private)
728                         return (0);
729                 break;
730         }
731         return (-EINVAL);
732 }
733
734 static int __video_do_ioctl(struct inode *inode, struct file *file,
735                 unsigned int cmd, void *arg)
736 {
737         struct video_device *vfd = video_devdata(file);
738         void                 *fh = file->private_data;
739         int                  ret = -EINVAL;
740
741         if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
742                                 !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
743                 v4l_print_ioctl(vfd->name, cmd);
744                 printk("\n");
745         }
746
747 #ifdef CONFIG_VIDEO_V4L1_COMPAT
748         /***********************************************************
749          Handles calls to the obsoleted V4L1 API
750          Due to the nature of VIDIOCGMBUF, each driver that supports
751          V4L1 should implement its own handler for this ioctl.
752          ***********************************************************/
753
754         /* --- streaming capture ------------------------------------- */
755         if (cmd == VIDIOCGMBUF) {
756                 struct video_mbuf *p=arg;
757
758                 memset(p, 0, sizeof(*p));
759
760                 if (!vfd->vidiocgmbuf)
761                         return ret;
762                 ret=vfd->vidiocgmbuf(file, fh, p);
763                 if (!ret)
764                         dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
765                                                 p->size, p->frames,
766                                                 (unsigned long)p->offsets);
767                 return ret;
768         }
769
770         /********************************************************
771          All other V4L1 calls are handled by v4l1_compat module.
772          Those calls will be translated into V4L2 calls, and
773          __video_do_ioctl will be called again, with one or more
774          V4L2 ioctls.
775          ********************************************************/
776         if (_IOC_TYPE(cmd)=='v')
777                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
778                                                 __video_do_ioctl);
779 #endif
780
781         switch(cmd) {
782         /* --- capabilities ------------------------------------------ */
783         case VIDIOC_QUERYCAP:
784         {
785                 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
786                 memset(cap, 0, sizeof(*cap));
787
788                 if (!vfd->vidioc_querycap)
789                         break;
790
791                 ret=vfd->vidioc_querycap(file, fh, cap);
792                 if (!ret)
793                         dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
794                                         "version=0x%08x, "
795                                         "capabilities=0x%08x\n",
796                                         cap->driver,cap->card,cap->bus_info,
797                                         cap->version,
798                                         cap->capabilities);
799                 break;
800         }
801
802         /* --- priority ------------------------------------------ */
803         case VIDIOC_G_PRIORITY:
804         {
805                 enum v4l2_priority *p=arg;
806
807                 if (!vfd->vidioc_g_priority)
808                         break;
809                 ret=vfd->vidioc_g_priority(file, fh, p);
810                 if (!ret)
811                         dbgarg(cmd, "priority is %d\n", *p);
812                 break;
813         }
814         case VIDIOC_S_PRIORITY:
815         {
816                 enum v4l2_priority *p=arg;
817
818                 if (!vfd->vidioc_s_priority)
819                         break;
820                 dbgarg(cmd, "setting priority to %d\n", *p);
821                 ret=vfd->vidioc_s_priority(file, fh, *p);
822                 break;
823         }
824
825         /* --- capture ioctls ---------------------------------------- */
826         case VIDIOC_ENUM_FMT:
827         {
828                 struct v4l2_fmtdesc *f = arg;
829                 enum v4l2_buf_type type;
830                 unsigned int index;
831
832                 index = f->index;
833                 type  = f->type;
834                 memset(f,0,sizeof(*f));
835                 f->index = index;
836                 f->type  = type;
837
838                 switch (type) {
839                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
840                         if (vfd->vidioc_enum_fmt_vid_cap)
841                                 ret = vfd->vidioc_enum_fmt_vid_cap(file, fh, f);
842                         break;
843                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
844                         if (vfd->vidioc_enum_fmt_vid_overlay)
845                                 ret = vfd->vidioc_enum_fmt_vid_overlay(file,
846                                         fh, f);
847                         break;
848 #if 1
849                 /* V4L2_BUF_TYPE_VBI_CAPTURE should not support VIDIOC_ENUM_FMT
850                  * according to the spec. The bttv and saa7134 drivers support
851                  * it though, so just warn that this is deprecated and will be
852                  * removed in the near future. */
853                 case V4L2_BUF_TYPE_VBI_CAPTURE:
854                         if (vfd->vidioc_enum_fmt_vbi_cap) {
855                                 printk(KERN_WARNING "vidioc_enum_fmt_vbi_cap will be removed in 2.6.28!\n");
856                                 ret = vfd->vidioc_enum_fmt_vbi_cap(file, fh, f);
857                         }
858                         break;
859 #endif
860                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
861                         if (vfd->vidioc_enum_fmt_vid_out)
862                                 ret = vfd->vidioc_enum_fmt_vid_out(file, fh, f);
863                         break;
864                 case V4L2_BUF_TYPE_PRIVATE:
865                         if (vfd->vidioc_enum_fmt_type_private)
866                                 ret = vfd->vidioc_enum_fmt_type_private(file,
867                                                                 fh, f);
868                         break;
869                 default:
870                         break;
871                 }
872                 if (!ret)
873                         dbgarg (cmd, "index=%d, type=%d, flags=%d, "
874                                         "pixelformat=%c%c%c%c, description='%s'\n",
875                                         f->index, f->type, f->flags,
876                                         (f->pixelformat & 0xff),
877                                         (f->pixelformat >>  8) & 0xff,
878                                         (f->pixelformat >> 16) & 0xff,
879                                         (f->pixelformat >> 24) & 0xff,
880                                         f->description);
881                 break;
882         }
883         case VIDIOC_G_FMT:
884         {
885                 struct v4l2_format *f = (struct v4l2_format *)arg;
886
887                 memset(f->fmt.raw_data, 0, sizeof(f->fmt.raw_data));
888
889                 /* FIXME: Should be one dump per type */
890                 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
891
892                 switch (f->type) {
893                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
894                         if (vfd->vidioc_g_fmt_vid_cap)
895                                 ret = vfd->vidioc_g_fmt_vid_cap(file, fh, f);
896                         if (!ret)
897                                 v4l_print_pix_fmt(vfd,&f->fmt.pix);
898                         break;
899                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
900                         if (vfd->vidioc_g_fmt_vid_overlay)
901                                 ret = vfd->vidioc_g_fmt_vid_overlay(file,
902                                                                     fh, f);
903                         break;
904                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
905                         if (vfd->vidioc_g_fmt_vid_out)
906                                 ret = vfd->vidioc_g_fmt_vid_out(file, fh, f);
907                         break;
908                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
909                         if (vfd->vidioc_g_fmt_vid_out_overlay)
910                                 ret = vfd->vidioc_g_fmt_vid_out_overlay(file,
911                                        fh, f);
912                         break;
913                 case V4L2_BUF_TYPE_VBI_CAPTURE:
914                         if (vfd->vidioc_g_fmt_vbi_cap)
915                                 ret = vfd->vidioc_g_fmt_vbi_cap(file, fh, f);
916                         break;
917                 case V4L2_BUF_TYPE_VBI_OUTPUT:
918                         if (vfd->vidioc_g_fmt_vbi_out)
919                                 ret = vfd->vidioc_g_fmt_vbi_out(file, fh, f);
920                         break;
921                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
922                         if (vfd->vidioc_g_fmt_sliced_vbi_cap)
923                                 ret = vfd->vidioc_g_fmt_sliced_vbi_cap(file,
924                                                                         fh, f);
925                         break;
926                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
927                         if (vfd->vidioc_g_fmt_sliced_vbi_out)
928                                 ret = vfd->vidioc_g_fmt_sliced_vbi_out(file,
929                                                                         fh, f);
930                         break;
931                 case V4L2_BUF_TYPE_PRIVATE:
932                         if (vfd->vidioc_g_fmt_type_private)
933                                 ret = vfd->vidioc_g_fmt_type_private(file,
934                                                                 fh, f);
935                         break;
936                 }
937
938                 break;
939         }
940         case VIDIOC_S_FMT:
941         {
942                 struct v4l2_format *f = (struct v4l2_format *)arg;
943
944                 /* FIXME: Should be one dump per type */
945                 dbgarg (cmd, "type=%s\n", prt_names(f->type,
946                                         v4l2_type_names));
947
948                 switch (f->type) {
949                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
950                         v4l_print_pix_fmt(vfd,&f->fmt.pix);
951                         if (vfd->vidioc_s_fmt_vid_cap)
952                                 ret = vfd->vidioc_s_fmt_vid_cap(file, fh, f);
953                         break;
954                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
955                         if (vfd->vidioc_s_fmt_vid_overlay)
956                                 ret = vfd->vidioc_s_fmt_vid_overlay(file,
957                                                                     fh, f);
958                         break;
959                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
960                         if (vfd->vidioc_s_fmt_vid_out)
961                                 ret = vfd->vidioc_s_fmt_vid_out(file, fh, f);
962                         break;
963                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
964                         if (vfd->vidioc_s_fmt_vid_out_overlay)
965                                 ret = vfd->vidioc_s_fmt_vid_out_overlay(file,
966                                         fh, f);
967                         break;
968                 case V4L2_BUF_TYPE_VBI_CAPTURE:
969                         if (vfd->vidioc_s_fmt_vbi_cap)
970                                 ret = vfd->vidioc_s_fmt_vbi_cap(file, fh, f);
971                         break;
972                 case V4L2_BUF_TYPE_VBI_OUTPUT:
973                         if (vfd->vidioc_s_fmt_vbi_out)
974                                 ret = vfd->vidioc_s_fmt_vbi_out(file, fh, f);
975                         break;
976                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
977                         if (vfd->vidioc_s_fmt_sliced_vbi_cap)
978                                 ret = vfd->vidioc_s_fmt_sliced_vbi_cap(file,
979                                                                         fh, f);
980                         break;
981                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
982                         if (vfd->vidioc_s_fmt_sliced_vbi_out)
983                                 ret = vfd->vidioc_s_fmt_sliced_vbi_out(file,
984                                                                         fh, f);
985                         break;
986                 case V4L2_BUF_TYPE_PRIVATE:
987                         if (vfd->vidioc_s_fmt_type_private)
988                                 ret = vfd->vidioc_s_fmt_type_private(file,
989                                                                 fh, f);
990                         break;
991                 }
992                 break;
993         }
994         case VIDIOC_TRY_FMT:
995         {
996                 struct v4l2_format *f = (struct v4l2_format *)arg;
997
998                 /* FIXME: Should be one dump per type */
999                 dbgarg (cmd, "type=%s\n", prt_names(f->type,
1000                                                 v4l2_type_names));
1001                 switch (f->type) {
1002                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1003                         if (vfd->vidioc_try_fmt_vid_cap)
1004                                 ret = vfd->vidioc_try_fmt_vid_cap(file, fh, f);
1005                         if (!ret)
1006                                 v4l_print_pix_fmt(vfd,&f->fmt.pix);
1007                         break;
1008                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1009                         if (vfd->vidioc_try_fmt_vid_overlay)
1010                                 ret = vfd->vidioc_try_fmt_vid_overlay(file,
1011                                         fh, f);
1012                         break;
1013                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1014                         if (vfd->vidioc_try_fmt_vid_out)
1015                                 ret = vfd->vidioc_try_fmt_vid_out(file, fh, f);
1016                         break;
1017                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1018                         if (vfd->vidioc_try_fmt_vid_out_overlay)
1019                                 ret = vfd->vidioc_try_fmt_vid_out_overlay(file,
1020                                        fh, f);
1021                         break;
1022                 case V4L2_BUF_TYPE_VBI_CAPTURE:
1023                         if (vfd->vidioc_try_fmt_vbi_cap)
1024                                 ret = vfd->vidioc_try_fmt_vbi_cap(file, fh, f);
1025                         break;
1026                 case V4L2_BUF_TYPE_VBI_OUTPUT:
1027                         if (vfd->vidioc_try_fmt_vbi_out)
1028                                 ret = vfd->vidioc_try_fmt_vbi_out(file, fh, f);
1029                         break;
1030                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1031                         if (vfd->vidioc_try_fmt_sliced_vbi_cap)
1032                                 ret = vfd->vidioc_try_fmt_sliced_vbi_cap(file,
1033                                                                 fh, f);
1034                         break;
1035                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1036                         if (vfd->vidioc_try_fmt_sliced_vbi_out)
1037                                 ret = vfd->vidioc_try_fmt_sliced_vbi_out(file,
1038                                                                 fh, f);
1039                         break;
1040                 case V4L2_BUF_TYPE_PRIVATE:
1041                         if (vfd->vidioc_try_fmt_type_private)
1042                                 ret = vfd->vidioc_try_fmt_type_private(file,
1043                                                                 fh, f);
1044                         break;
1045                 }
1046
1047                 break;
1048         }
1049         /* FIXME: Those buf reqs could be handled here,
1050            with some changes on videobuf to allow its header to be included at
1051            videodev2.h or being merged at videodev2.
1052          */
1053         case VIDIOC_REQBUFS:
1054         {
1055                 struct v4l2_requestbuffers *p=arg;
1056
1057                 if (!vfd->vidioc_reqbufs)
1058                         break;
1059                 ret = check_fmt (vfd, p->type);
1060                 if (ret)
1061                         break;
1062
1063                 ret=vfd->vidioc_reqbufs(file, fh, p);
1064                 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
1065                                 p->count,
1066                                 prt_names(p->type, v4l2_type_names),
1067                                 prt_names(p->memory, v4l2_memory_names));
1068                 break;
1069         }
1070         case VIDIOC_QUERYBUF:
1071         {
1072                 struct v4l2_buffer *p=arg;
1073
1074                 if (!vfd->vidioc_querybuf)
1075                         break;
1076                 ret = check_fmt (vfd, p->type);
1077                 if (ret)
1078                         break;
1079
1080                 ret=vfd->vidioc_querybuf(file, fh, p);
1081                 if (!ret)
1082                         dbgbuf(cmd,vfd,p);
1083                 break;
1084         }
1085         case VIDIOC_QBUF:
1086         {
1087                 struct v4l2_buffer *p=arg;
1088
1089                 if (!vfd->vidioc_qbuf)
1090                         break;
1091                 ret = check_fmt (vfd, p->type);
1092                 if (ret)
1093                         break;
1094
1095                 ret=vfd->vidioc_qbuf(file, fh, p);
1096                 if (!ret)
1097                         dbgbuf(cmd,vfd,p);
1098                 break;
1099         }
1100         case VIDIOC_DQBUF:
1101         {
1102                 struct v4l2_buffer *p=arg;
1103                 if (!vfd->vidioc_dqbuf)
1104                         break;
1105                 ret = check_fmt (vfd, p->type);
1106                 if (ret)
1107                         break;
1108
1109                 ret=vfd->vidioc_dqbuf(file, fh, p);
1110                 if (!ret)
1111                         dbgbuf(cmd,vfd,p);
1112                 break;
1113         }
1114         case VIDIOC_OVERLAY:
1115         {
1116                 int *i = arg;
1117
1118                 if (!vfd->vidioc_overlay)
1119                         break;
1120                 dbgarg (cmd, "value=%d\n",*i);
1121                 ret=vfd->vidioc_overlay(file, fh, *i);
1122                 break;
1123         }
1124         case VIDIOC_G_FBUF:
1125         {
1126                 struct v4l2_framebuffer *p=arg;
1127                 if (!vfd->vidioc_g_fbuf)
1128                         break;
1129                 ret=vfd->vidioc_g_fbuf(file, fh, arg);
1130                 if (!ret) {
1131                         dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
1132                                         p->capability,p->flags,
1133                                         (unsigned long)p->base);
1134                         v4l_print_pix_fmt (vfd, &p->fmt);
1135                 }
1136                 break;
1137         }
1138         case VIDIOC_S_FBUF:
1139         {
1140                 struct v4l2_framebuffer *p=arg;
1141                 if (!vfd->vidioc_s_fbuf)
1142                         break;
1143
1144                 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
1145                                 p->capability,p->flags,(unsigned long)p->base);
1146                 v4l_print_pix_fmt (vfd, &p->fmt);
1147                 ret=vfd->vidioc_s_fbuf(file, fh, arg);
1148
1149                 break;
1150         }
1151         case VIDIOC_STREAMON:
1152         {
1153                 enum v4l2_buf_type i = *(int *)arg;
1154                 if (!vfd->vidioc_streamon)
1155                         break;
1156                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1157                 ret=vfd->vidioc_streamon(file, fh,i);
1158                 break;
1159         }
1160         case VIDIOC_STREAMOFF:
1161         {
1162                 enum v4l2_buf_type i = *(int *)arg;
1163
1164                 if (!vfd->vidioc_streamoff)
1165                         break;
1166                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1167                 ret=vfd->vidioc_streamoff(file, fh, i);
1168                 break;
1169         }
1170         /* ---------- tv norms ---------- */
1171         case VIDIOC_ENUMSTD:
1172         {
1173                 struct v4l2_standard *p = arg;
1174                 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1175                 unsigned int index = p->index, i, j = 0;
1176                 const char *descr = "";
1177
1178                 /* Return norm array in a canonical way */
1179                 for (i = 0; i <= index && id; i++) {
1180                         /* last std value in the standards array is 0, so this
1181                            while always ends there since (id & 0) == 0. */
1182                         while ((id & standards[j].std) != standards[j].std)
1183                                 j++;
1184                         curr_id = standards[j].std;
1185                         descr = standards[j].descr;
1186                         j++;
1187                         if (curr_id == 0)
1188                                 break;
1189                         if (curr_id != V4L2_STD_PAL &&
1190                             curr_id != V4L2_STD_SECAM &&
1191                             curr_id != V4L2_STD_NTSC)
1192                                 id &= ~curr_id;
1193                 }
1194                 if (i <= index)
1195                         return -EINVAL;
1196
1197                 v4l2_video_std_construct(p, curr_id, descr);
1198                 p->index = index;
1199
1200                 dbgarg(cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
1201                                 "framelines=%d\n", p->index,
1202                                 (unsigned long long)p->id, p->name,
1203                                 p->frameperiod.numerator,
1204                                 p->frameperiod.denominator,
1205                                 p->framelines);
1206
1207                 ret = 0;
1208                 break;
1209         }
1210         case VIDIOC_G_STD:
1211         {
1212                 v4l2_std_id *id = arg;
1213
1214                 ret = 0;
1215                 /* Calls the specific handler */
1216                 if (vfd->vidioc_g_std)
1217                         ret = vfd->vidioc_g_std(file, fh, id);
1218                 else
1219                         *id = vfd->current_norm;
1220
1221                 if (!ret)
1222                         dbgarg(cmd, "value=%08Lx\n", (long long unsigned)*id);
1223                 break;
1224         }
1225         case VIDIOC_S_STD:
1226         {
1227                 v4l2_std_id *id = arg,norm;
1228
1229                 dbgarg (cmd, "value=%08Lx\n", (long long unsigned) *id);
1230
1231                 norm = (*id) & vfd->tvnorms;
1232                 if ( vfd->tvnorms && !norm)     /* Check if std is supported */
1233                         break;
1234
1235                 /* Calls the specific handler */
1236                 if (vfd->vidioc_s_std)
1237                         ret=vfd->vidioc_s_std(file, fh, &norm);
1238                 else
1239                         ret=-EINVAL;
1240
1241                 /* Updates standard information */
1242                 if (ret>=0)
1243                         vfd->current_norm=norm;
1244
1245                 break;
1246         }
1247         case VIDIOC_QUERYSTD:
1248         {
1249                 v4l2_std_id *p=arg;
1250
1251                 if (!vfd->vidioc_querystd)
1252                         break;
1253                 ret=vfd->vidioc_querystd(file, fh, arg);
1254                 if (!ret)
1255                         dbgarg (cmd, "detected std=%08Lx\n",
1256                                                 (unsigned long long)*p);
1257                 break;
1258         }
1259         /* ------ input switching ---------- */
1260         /* FIXME: Inputs can be handled inside videodev2 */
1261         case VIDIOC_ENUMINPUT:
1262         {
1263                 struct v4l2_input *p=arg;
1264                 int i=p->index;
1265
1266                 if (!vfd->vidioc_enum_input)
1267                         break;
1268                 memset(p, 0, sizeof(*p));
1269                 p->index=i;
1270
1271                 ret=vfd->vidioc_enum_input(file, fh, p);
1272                 if (!ret)
1273                         dbgarg (cmd, "index=%d, name=%s, type=%d, "
1274                                         "audioset=%d, "
1275                                         "tuner=%d, std=%08Lx, status=%d\n",
1276                                         p->index,p->name,p->type,p->audioset,
1277                                         p->tuner,
1278                                         (unsigned long long)p->std,
1279                                         p->status);
1280                 break;
1281         }
1282         case VIDIOC_G_INPUT:
1283         {
1284                 unsigned int *i = arg;
1285
1286                 if (!vfd->vidioc_g_input)
1287                         break;
1288                 ret=vfd->vidioc_g_input(file, fh, i);
1289                 if (!ret)
1290                         dbgarg (cmd, "value=%d\n",*i);
1291                 break;
1292         }
1293         case VIDIOC_S_INPUT:
1294         {
1295                 unsigned int *i = arg;
1296
1297                 if (!vfd->vidioc_s_input)
1298                         break;
1299                 dbgarg (cmd, "value=%d\n",*i);
1300                 ret=vfd->vidioc_s_input(file, fh, *i);
1301                 break;
1302         }
1303
1304         /* ------ output switching ---------- */
1305         case VIDIOC_ENUMOUTPUT:
1306         {
1307                 struct v4l2_output *p = arg;
1308                 int i = p->index;
1309
1310                 if (!vfd->vidioc_enum_output)
1311                         break;
1312                 memset(p, 0, sizeof(*p));
1313                 p->index = i;
1314
1315                 ret = vfd->vidioc_enum_output(file, fh, p);
1316                 if (!ret)
1317                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1318                                 "audioset=%d, "
1319                                 "modulator=%d, std=%08Lx\n",
1320                                 p->index, p->name, p->type, p->audioset,
1321                                 p->modulator, (unsigned long long)p->std);
1322                 break;
1323         }
1324         case VIDIOC_G_OUTPUT:
1325         {
1326                 unsigned int *i = arg;
1327
1328                 if (!vfd->vidioc_g_output)
1329                         break;
1330                 ret=vfd->vidioc_g_output(file, fh, i);
1331                 if (!ret)
1332                         dbgarg (cmd, "value=%d\n",*i);
1333                 break;
1334         }
1335         case VIDIOC_S_OUTPUT:
1336         {
1337                 unsigned int *i = arg;
1338
1339                 if (!vfd->vidioc_s_output)
1340                         break;
1341                 dbgarg (cmd, "value=%d\n",*i);
1342                 ret=vfd->vidioc_s_output(file, fh, *i);
1343                 break;
1344         }
1345
1346         /* --- controls ---------------------------------------------- */
1347         case VIDIOC_QUERYCTRL:
1348         {
1349                 struct v4l2_queryctrl *p=arg;
1350
1351                 if (!vfd->vidioc_queryctrl)
1352                         break;
1353                 ret=vfd->vidioc_queryctrl(file, fh, p);
1354
1355                 if (!ret)
1356                         dbgarg (cmd, "id=%d, type=%d, name=%s, "
1357                                         "min/max=%d/%d,"
1358                                         " step=%d, default=%d, flags=0x%08x\n",
1359                                         p->id,p->type,p->name,p->minimum,
1360                                         p->maximum,p->step,p->default_value,
1361                                         p->flags);
1362                 break;
1363         }
1364         case VIDIOC_G_CTRL:
1365         {
1366                 struct v4l2_control *p = arg;
1367
1368                 if (!vfd->vidioc_g_ctrl)
1369                         break;
1370                 dbgarg(cmd, "Enum for index=%d\n", p->id);
1371
1372                 ret=vfd->vidioc_g_ctrl(file, fh, p);
1373                 if (!ret)
1374                         dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1375                 break;
1376         }
1377         case VIDIOC_S_CTRL:
1378         {
1379                 struct v4l2_control *p = arg;
1380
1381                 if (!vfd->vidioc_s_ctrl)
1382                         break;
1383                 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1384
1385                 ret=vfd->vidioc_s_ctrl(file, fh, p);
1386                 break;
1387         }
1388         case VIDIOC_G_EXT_CTRLS:
1389         {
1390                 struct v4l2_ext_controls *p = arg;
1391
1392                 if (vfd->vidioc_g_ext_ctrls) {
1393                         dbgarg(cmd, "count=%d\n", p->count);
1394
1395                         ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1396                 }
1397                 break;
1398         }
1399         case VIDIOC_S_EXT_CTRLS:
1400         {
1401                 struct v4l2_ext_controls *p = arg;
1402
1403                 if (vfd->vidioc_s_ext_ctrls) {
1404                         dbgarg(cmd, "count=%d\n", p->count);
1405
1406                         ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1407                 }
1408                 break;
1409         }
1410         case VIDIOC_TRY_EXT_CTRLS:
1411         {
1412                 struct v4l2_ext_controls *p = arg;
1413
1414                 if (vfd->vidioc_try_ext_ctrls) {
1415                         dbgarg(cmd, "count=%d\n", p->count);
1416
1417                         ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1418                 }
1419                 break;
1420         }
1421         case VIDIOC_QUERYMENU:
1422         {
1423                 struct v4l2_querymenu *p=arg;
1424                 if (!vfd->vidioc_querymenu)
1425                         break;
1426                 ret=vfd->vidioc_querymenu(file, fh, p);
1427                 if (!ret)
1428                         dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1429                                                 p->id,p->index,p->name);
1430                 break;
1431         }
1432         /* --- audio ---------------------------------------------- */
1433         case VIDIOC_ENUMAUDIO:
1434         {
1435                 struct v4l2_audio *p=arg;
1436
1437                 if (!vfd->vidioc_enumaudio)
1438                         break;
1439                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1440                 ret=vfd->vidioc_enumaudio(file, fh, p);
1441                 if (!ret)
1442                         dbgarg2("index=%d, name=%s, capability=%d, "
1443                                         "mode=%d\n",p->index,p->name,
1444                                         p->capability, p->mode);
1445                 break;
1446         }
1447         case VIDIOC_G_AUDIO:
1448         {
1449                 struct v4l2_audio *p=arg;
1450                 __u32 index=p->index;
1451
1452                 if (!vfd->vidioc_g_audio)
1453                         break;
1454
1455                 memset(p,0,sizeof(*p));
1456                 p->index=index;
1457                 dbgarg(cmd, "Get for index=%d\n", p->index);
1458                 ret=vfd->vidioc_g_audio(file, fh, p);
1459                 if (!ret)
1460                         dbgarg2("index=%d, name=%s, capability=%d, "
1461                                         "mode=%d\n",p->index,
1462                                         p->name,p->capability, p->mode);
1463                 break;
1464         }
1465         case VIDIOC_S_AUDIO:
1466         {
1467                 struct v4l2_audio *p=arg;
1468
1469                 if (!vfd->vidioc_s_audio)
1470                         break;
1471                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1472                                         "mode=%d\n", p->index, p->name,
1473                                         p->capability, p->mode);
1474                 ret=vfd->vidioc_s_audio(file, fh, p);
1475                 break;
1476         }
1477         case VIDIOC_ENUMAUDOUT:
1478         {
1479                 struct v4l2_audioout *p=arg;
1480
1481                 if (!vfd->vidioc_enumaudout)
1482                         break;
1483                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1484                 ret=vfd->vidioc_enumaudout(file, fh, p);
1485                 if (!ret)
1486                         dbgarg2("index=%d, name=%s, capability=%d, "
1487                                         "mode=%d\n", p->index, p->name,
1488                                         p->capability,p->mode);
1489                 break;
1490         }
1491         case VIDIOC_G_AUDOUT:
1492         {
1493                 struct v4l2_audioout *p=arg;
1494
1495                 if (!vfd->vidioc_g_audout)
1496                         break;
1497                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1498                 ret=vfd->vidioc_g_audout(file, fh, p);
1499                 if (!ret)
1500                         dbgarg2("index=%d, name=%s, capability=%d, "
1501                                         "mode=%d\n", p->index, p->name,
1502                                         p->capability,p->mode);
1503                 break;
1504         }
1505         case VIDIOC_S_AUDOUT:
1506         {
1507                 struct v4l2_audioout *p=arg;
1508
1509                 if (!vfd->vidioc_s_audout)
1510                         break;
1511                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1512                                         "mode=%d\n", p->index, p->name,
1513                                         p->capability,p->mode);
1514
1515                 ret=vfd->vidioc_s_audout(file, fh, p);
1516                 break;
1517         }
1518         case VIDIOC_G_MODULATOR:
1519         {
1520                 struct v4l2_modulator *p=arg;
1521                 if (!vfd->vidioc_g_modulator)
1522                         break;
1523                 ret=vfd->vidioc_g_modulator(file, fh, p);
1524                 if (!ret)
1525                         dbgarg(cmd, "index=%d, name=%s, "
1526                                         "capability=%d, rangelow=%d,"
1527                                         " rangehigh=%d, txsubchans=%d\n",
1528                                         p->index, p->name,p->capability,
1529                                         p->rangelow, p->rangehigh,
1530                                         p->txsubchans);
1531                 break;
1532         }
1533         case VIDIOC_S_MODULATOR:
1534         {
1535                 struct v4l2_modulator *p=arg;
1536                 if (!vfd->vidioc_s_modulator)
1537                         break;
1538                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1539                                 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1540                                 p->index, p->name,p->capability,p->rangelow,
1541                                 p->rangehigh,p->txsubchans);
1542                         ret=vfd->vidioc_s_modulator(file, fh, p);
1543                 break;
1544         }
1545         case VIDIOC_G_CROP:
1546         {
1547                 struct v4l2_crop *p=arg;
1548                 if (!vfd->vidioc_g_crop)
1549                         break;
1550                 ret=vfd->vidioc_g_crop(file, fh, p);
1551                 if (!ret) {
1552                         dbgarg(cmd, "type=%d\n", p->type);
1553                         dbgrect(vfd, "", &p->c);
1554                 }
1555                 break;
1556         }
1557         case VIDIOC_S_CROP:
1558         {
1559                 struct v4l2_crop *p=arg;
1560                 if (!vfd->vidioc_s_crop)
1561                         break;
1562                 dbgarg(cmd, "type=%d\n", p->type);
1563                 dbgrect(vfd, "", &p->c);
1564                 ret=vfd->vidioc_s_crop(file, fh, p);
1565                 break;
1566         }
1567         case VIDIOC_CROPCAP:
1568         {
1569                 struct v4l2_cropcap *p=arg;
1570                 /*FIXME: Should also show v4l2_fract pixelaspect */
1571                 if (!vfd->vidioc_cropcap)
1572                         break;
1573                 dbgarg(cmd, "type=%d\n", p->type);
1574                 dbgrect(vfd, "bounds ", &p->bounds);
1575                 dbgrect(vfd, "defrect ", &p->defrect);
1576                 ret=vfd->vidioc_cropcap(file, fh, p);
1577                 break;
1578         }
1579         case VIDIOC_G_JPEGCOMP:
1580         {
1581                 struct v4l2_jpegcompression *p=arg;
1582                 if (!vfd->vidioc_g_jpegcomp)
1583                         break;
1584                 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1585                 if (!ret)
1586                         dbgarg (cmd, "quality=%d, APPn=%d, "
1587                                                 "APP_len=%d, COM_len=%d, "
1588                                                 "jpeg_markers=%d\n",
1589                                                 p->quality,p->APPn,p->APP_len,
1590                                                 p->COM_len,p->jpeg_markers);
1591                 break;
1592         }
1593         case VIDIOC_S_JPEGCOMP:
1594         {
1595                 struct v4l2_jpegcompression *p=arg;
1596                 if (!vfd->vidioc_g_jpegcomp)
1597                         break;
1598                 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1599                                         "COM_len=%d, jpeg_markers=%d\n",
1600                                         p->quality,p->APPn,p->APP_len,
1601                                         p->COM_len,p->jpeg_markers);
1602                         ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1603                 break;
1604         }
1605         case VIDIOC_G_ENC_INDEX:
1606         {
1607                 struct v4l2_enc_idx *p=arg;
1608
1609                 if (!vfd->vidioc_g_enc_index)
1610                         break;
1611                 ret=vfd->vidioc_g_enc_index(file, fh, p);
1612                 if (!ret)
1613                         dbgarg (cmd, "entries=%d, entries_cap=%d\n",
1614                                         p->entries,p->entries_cap);
1615                 break;
1616         }
1617         case VIDIOC_ENCODER_CMD:
1618         {
1619                 struct v4l2_encoder_cmd *p = arg;
1620
1621                 if (!vfd->vidioc_encoder_cmd)
1622                         break;
1623                 memset(&p->raw, 0, sizeof(p->raw));
1624                 ret = vfd->vidioc_encoder_cmd(file, fh, p);
1625                 if (!ret)
1626                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1627                 break;
1628         }
1629         case VIDIOC_TRY_ENCODER_CMD:
1630         {
1631                 struct v4l2_encoder_cmd *p = arg;
1632
1633                 if (!vfd->vidioc_try_encoder_cmd)
1634                         break;
1635                 memset(&p->raw, 0, sizeof(p->raw));
1636                 ret = vfd->vidioc_try_encoder_cmd(file, fh, p);
1637                 if (!ret)
1638                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1639                 break;
1640         }
1641         case VIDIOC_G_PARM:
1642         {
1643                 struct v4l2_streamparm *p=arg;
1644                 __u32 type=p->type;
1645
1646                 memset(p,0,sizeof(*p));
1647                 p->type=type;
1648
1649                 if (vfd->vidioc_g_parm) {
1650                         ret=vfd->vidioc_g_parm(file, fh, p);
1651                 } else {
1652                         struct v4l2_standard s;
1653
1654                         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1655                                 return -EINVAL;
1656
1657                         v4l2_video_std_construct(&s, vfd->current_norm,
1658                                                  v4l2_norm_to_name(vfd->current_norm));
1659
1660                         p->parm.capture.timeperframe = s.frameperiod;
1661                         ret=0;
1662                 }
1663
1664                 dbgarg (cmd, "type=%d\n", p->type);
1665                 break;
1666         }
1667         case VIDIOC_S_PARM:
1668         {
1669                 struct v4l2_streamparm *p=arg;
1670                 if (!vfd->vidioc_s_parm)
1671                         break;
1672                 dbgarg (cmd, "type=%d\n", p->type);
1673                 ret=vfd->vidioc_s_parm(file, fh, p);
1674                 break;
1675         }
1676         case VIDIOC_G_TUNER:
1677         {
1678                 struct v4l2_tuner *p=arg;
1679                 __u32 index=p->index;
1680
1681                 if (!vfd->vidioc_g_tuner)
1682                         break;
1683
1684                 memset(p,0,sizeof(*p));
1685                 p->index=index;
1686
1687                 ret=vfd->vidioc_g_tuner(file, fh, p);
1688                 if (!ret)
1689                         dbgarg (cmd, "index=%d, name=%s, type=%d, "
1690                                         "capability=%d, rangelow=%d, "
1691                                         "rangehigh=%d, signal=%d, afc=%d, "
1692                                         "rxsubchans=%d, audmode=%d\n",
1693                                         p->index, p->name, p->type,
1694                                         p->capability, p->rangelow,
1695                                         p->rangehigh, p->rxsubchans,
1696                                         p->audmode, p->signal, p->afc);
1697                 break;
1698         }
1699         case VIDIOC_S_TUNER:
1700         {
1701                 struct v4l2_tuner *p=arg;
1702                 if (!vfd->vidioc_s_tuner)
1703                         break;
1704                 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1705                                 "capability=%d, rangelow=%d, rangehigh=%d, "
1706                                 "signal=%d, afc=%d, rxsubchans=%d, "
1707                                 "audmode=%d\n",p->index, p->name, p->type,
1708                                 p->capability, p->rangelow,p->rangehigh,
1709                                 p->rxsubchans, p->audmode, p->signal,
1710                                 p->afc);
1711                 ret=vfd->vidioc_s_tuner(file, fh, p);
1712                 break;
1713         }
1714         case VIDIOC_G_FREQUENCY:
1715         {
1716                 struct v4l2_frequency *p = arg;
1717
1718                 if (!vfd->vidioc_g_frequency)
1719                         break;
1720
1721                 memset(p->reserved, 0, sizeof(p->reserved));
1722
1723                 ret = vfd->vidioc_g_frequency(file, fh, p);
1724                 if (!ret)
1725                         dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1726                                         p->tuner, p->type, p->frequency);
1727                 break;
1728         }
1729         case VIDIOC_S_FREQUENCY:
1730         {
1731                 struct v4l2_frequency *p=arg;
1732                 if (!vfd->vidioc_s_frequency)
1733                         break;
1734                 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1735                                 p->tuner,p->type,p->frequency);
1736                 ret=vfd->vidioc_s_frequency(file, fh, p);
1737                 break;
1738         }
1739         case VIDIOC_G_SLICED_VBI_CAP:
1740         {
1741                 struct v4l2_sliced_vbi_cap *p = arg;
1742                 __u32 type = p->type;
1743
1744                 if (!vfd->vidioc_g_sliced_vbi_cap)
1745                         break;
1746                 memset(p, 0, sizeof(*p));
1747                 p->type = type;
1748                 ret = vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1749                 if (!ret)
1750                         dbgarg(cmd, "type=%d, service_set=%d\n",
1751                                         p->type, p->service_set);
1752                 break;
1753         }
1754         case VIDIOC_LOG_STATUS:
1755         {
1756                 if (!vfd->vidioc_log_status)
1757                         break;
1758                 ret=vfd->vidioc_log_status(file, fh);
1759                 break;
1760         }
1761 #ifdef CONFIG_VIDEO_ADV_DEBUG
1762         case VIDIOC_DBG_G_REGISTER:
1763         {
1764                 struct v4l2_register *p=arg;
1765                 if (!capable(CAP_SYS_ADMIN))
1766                         ret=-EPERM;
1767                 else if (vfd->vidioc_g_register)
1768                         ret=vfd->vidioc_g_register(file, fh, p);
1769                 break;
1770         }
1771         case VIDIOC_DBG_S_REGISTER:
1772         {
1773                 struct v4l2_register *p=arg;
1774                 if (!capable(CAP_SYS_ADMIN))
1775                         ret=-EPERM;
1776                 else if (vfd->vidioc_s_register)
1777                         ret=vfd->vidioc_s_register(file, fh, p);
1778                 break;
1779         }
1780 #endif
1781         case VIDIOC_G_CHIP_IDENT:
1782         {
1783                 struct v4l2_chip_ident *p=arg;
1784                 if (!vfd->vidioc_g_chip_ident)
1785                         break;
1786                 ret=vfd->vidioc_g_chip_ident(file, fh, p);
1787                 if (!ret)
1788                         dbgarg (cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1789                 break;
1790         }
1791         default:
1792         {
1793                 if (!vfd->vidioc_default)
1794                         break;
1795                 ret = vfd->vidioc_default(file, fh, cmd, arg);
1796                 break;
1797         }
1798         case VIDIOC_S_HW_FREQ_SEEK:
1799         {
1800                 struct v4l2_hw_freq_seek *p = arg;
1801                 if (!vfd->vidioc_s_hw_freq_seek)
1802                         break;
1803                 dbgarg(cmd,
1804                         "tuner=%d, type=%d, seek_upward=%d, wrap_around=%d\n",
1805                         p->tuner, p->type, p->seek_upward, p->wrap_around);
1806                 ret = vfd->vidioc_s_hw_freq_seek(file, fh, p);
1807                 break;
1808         }
1809         } /* switch */
1810
1811         if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1812                 if (ret<0) {
1813                         printk("%s: err: on ", vfd->name);
1814                         v4l_print_ioctl(vfd->name, cmd);
1815                         printk("\n");
1816                 }
1817         }
1818
1819         return ret;
1820 }
1821
1822 int video_ioctl2 (struct inode *inode, struct file *file,
1823                unsigned int cmd, unsigned long arg)
1824 {
1825         char    sbuf[128];
1826         void    *mbuf = NULL;
1827         void    *parg = NULL;
1828         int     err  = -EINVAL;
1829         int     is_ext_ctrl;
1830         size_t  ctrls_size = 0;
1831         void __user *user_ptr = NULL;
1832
1833 #ifdef __OLD_VIDIOC_
1834         cmd = video_fix_command(cmd);
1835 #endif
1836         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1837                        cmd == VIDIOC_TRY_EXT_CTRLS);
1838
1839         /*  Copy arguments into temp kernel buffer  */
1840         switch (_IOC_DIR(cmd)) {
1841         case _IOC_NONE:
1842                 parg = NULL;
1843                 break;
1844         case _IOC_READ:
1845         case _IOC_WRITE:
1846         case (_IOC_WRITE | _IOC_READ):
1847                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1848                         parg = sbuf;
1849                 } else {
1850                         /* too big to allocate from stack */
1851                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1852                         if (NULL == mbuf)
1853                                 return -ENOMEM;
1854                         parg = mbuf;
1855                 }
1856
1857                 err = -EFAULT;
1858                 if (_IOC_DIR(cmd) & _IOC_WRITE)
1859                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1860                                 goto out;
1861                 break;
1862         }
1863
1864         if (is_ext_ctrl) {
1865                 struct v4l2_ext_controls *p = parg;
1866
1867                 /* In case of an error, tell the caller that it wasn't
1868                    a specific control that caused it. */
1869                 p->error_idx = p->count;
1870                 user_ptr = (void __user *)p->controls;
1871                 if (p->count) {
1872                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1873                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1874                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1875                         err = -ENOMEM;
1876                         if (NULL == mbuf)
1877                                 goto out_ext_ctrl;
1878                         err = -EFAULT;
1879                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
1880                                 goto out_ext_ctrl;
1881                         p->controls = mbuf;
1882                 }
1883         }
1884
1885         /* Handles IOCTL */
1886         err = __video_do_ioctl(inode, file, cmd, parg);
1887         if (err == -ENOIOCTLCMD)
1888                 err = -EINVAL;
1889         if (is_ext_ctrl) {
1890                 struct v4l2_ext_controls *p = parg;
1891
1892                 p->controls = (void *)user_ptr;
1893                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1894                         err = -EFAULT;
1895                 goto out_ext_ctrl;
1896         }
1897         if (err < 0)
1898                 goto out;
1899
1900 out_ext_ctrl:
1901         /*  Copy results into user buffer  */
1902         switch (_IOC_DIR(cmd))
1903         {
1904         case _IOC_READ:
1905         case (_IOC_WRITE | _IOC_READ):
1906                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1907                         err = -EFAULT;
1908                 break;
1909         }
1910
1911 out:
1912         kfree(mbuf);
1913         return err;
1914 }
1915 EXPORT_SYMBOL(video_ioctl2);
1916
1917 struct index_info {
1918         struct device *dev;
1919         unsigned int used[VIDEO_NUM_DEVICES];
1920 };
1921
1922 static int __fill_index_info(struct device *cd, void *data)
1923 {
1924         struct index_info *info = data;
1925         struct video_device *vfd = container_of(cd, struct video_device,
1926                                                 class_dev);
1927
1928         if (info->dev == vfd->dev)
1929                 info->used[vfd->index] = 1;
1930
1931         return 0;
1932 }
1933
1934 /**
1935  * assign_index - assign stream number based on parent device
1936  * @vdev: video_device to assign index number to, vdev->dev should be assigned
1937  * @num: -1 if auto assign, requested number otherwise
1938  *
1939  *
1940  * returns -ENFILE if num is already in use, a free index number if
1941  * successful.
1942  */
1943 static int get_index(struct video_device *vdev, int num)
1944 {
1945         struct index_info *info;
1946         int i;
1947         int ret = 0;
1948
1949         if (num >= VIDEO_NUM_DEVICES)
1950                 return -EINVAL;
1951
1952         info = kzalloc(sizeof(*info), GFP_KERNEL);
1953         if (!info)
1954                 return -ENOMEM;
1955
1956         info->dev = vdev->dev;
1957
1958         ret = class_for_each_device(&video_class, info,
1959                                         __fill_index_info);
1960
1961         if (ret < 0)
1962                 goto out;
1963
1964         if (num >= 0) {
1965                 if (!info->used[num])
1966                         ret = num;
1967                 else
1968                         ret = -ENFILE;
1969
1970                 goto out;
1971         }
1972
1973         for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
1974                 if (info->used[i])
1975                         continue;
1976                 ret = i;
1977                 goto out;
1978         }
1979
1980 out:
1981         kfree(info);
1982         return ret;
1983 }
1984
1985 static const struct file_operations video_fops;
1986
1987 int video_register_device(struct video_device *vfd, int type, int nr)
1988 {
1989         return video_register_device_index(vfd, type, nr, -1);
1990 }
1991 EXPORT_SYMBOL(video_register_device);
1992
1993 /**
1994  *      video_register_device - register video4linux devices
1995  *      @vfd:  video device structure we want to register
1996  *      @type: type of device to register
1997  *      @nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
1998  *             -1 == first free)
1999  *
2000  *      The registration code assigns minor numbers based on the type
2001  *      requested. -ENFILE is returned in all the device slots for this
2002  *      category are full. If not then the minor field is set and the
2003  *      driver initialize function is called (if non %NULL).
2004  *
2005  *      Zero is returned on success.
2006  *
2007  *      Valid types are
2008  *
2009  *      %VFL_TYPE_GRABBER - A frame grabber
2010  *
2011  *      %VFL_TYPE_VTX - A teletext device
2012  *
2013  *      %VFL_TYPE_VBI - Vertical blank data (undecoded)
2014  *
2015  *      %VFL_TYPE_RADIO - A radio card
2016  */
2017
2018 int video_register_device_index(struct video_device *vfd, int type, int nr,
2019                                         int index)
2020 {
2021         int i=0;
2022         int base;
2023         int end;
2024         int ret;
2025         char *name_base;
2026
2027         switch(type)
2028         {
2029                 case VFL_TYPE_GRABBER:
2030                         base=MINOR_VFL_TYPE_GRABBER_MIN;
2031                         end=MINOR_VFL_TYPE_GRABBER_MAX+1;
2032                         name_base = "video";
2033                         break;
2034                 case VFL_TYPE_VTX:
2035                         base=MINOR_VFL_TYPE_VTX_MIN;
2036                         end=MINOR_VFL_TYPE_VTX_MAX+1;
2037                         name_base = "vtx";
2038                         break;
2039                 case VFL_TYPE_VBI:
2040                         base=MINOR_VFL_TYPE_VBI_MIN;
2041                         end=MINOR_VFL_TYPE_VBI_MAX+1;
2042                         name_base = "vbi";
2043                         break;
2044                 case VFL_TYPE_RADIO:
2045                         base=MINOR_VFL_TYPE_RADIO_MIN;
2046                         end=MINOR_VFL_TYPE_RADIO_MAX+1;
2047                         name_base = "radio";
2048                         break;
2049                 default:
2050                         printk(KERN_ERR "%s called with unknown type: %d\n",
2051                                __func__, type);
2052                         return -1;
2053         }
2054
2055         /* pick a minor number */
2056         mutex_lock(&videodev_lock);
2057         if (nr >= 0  &&  nr < end-base) {
2058                 /* use the one the driver asked for */
2059                 i = base+nr;
2060                 if (NULL != video_device[i]) {
2061                         mutex_unlock(&videodev_lock);
2062                         return -ENFILE;
2063                 }
2064         } else {
2065                 /* use first free */
2066                 for(i=base;i<end;i++)
2067                         if (NULL == video_device[i])
2068                                 break;
2069                 if (i == end) {
2070                         mutex_unlock(&videodev_lock);
2071                         return -ENFILE;
2072                 }
2073         }
2074         video_device[i]=vfd;
2075         vfd->minor=i;
2076
2077         ret = get_index(vfd, index);
2078         if (ret < 0) {
2079                 printk(KERN_ERR "%s: get_index failed\n",
2080                        __func__);
2081                 goto fail_minor;
2082         }
2083
2084         vfd->index = ret;
2085
2086         mutex_unlock(&videodev_lock);
2087         mutex_init(&vfd->lock);
2088
2089         /* sysfs class */
2090         memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
2091         if (vfd->dev)
2092                 vfd->class_dev.parent = vfd->dev;
2093         vfd->class_dev.class       = &video_class;
2094         vfd->class_dev.devt        = MKDEV(VIDEO_MAJOR, vfd->minor);
2095         sprintf(vfd->class_dev.bus_id, "%s%d", name_base, i - base);
2096         ret = device_register(&vfd->class_dev);
2097         if (ret < 0) {
2098                 printk(KERN_ERR "%s: device_register failed\n",
2099                        __func__);
2100                 goto fail_minor;
2101         }
2102
2103 #if 1
2104         /* needed until all drivers are fixed */
2105         if (!vfd->release)
2106                 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
2107                        "Please fix your driver for proper sysfs support, see "
2108                        "http://lwn.net/Articles/36850/\n", vfd->name);
2109 #endif
2110         return 0;
2111
2112 fail_minor:
2113         mutex_lock(&videodev_lock);
2114         video_device[vfd->minor] = NULL;
2115         vfd->minor = -1;
2116         mutex_unlock(&videodev_lock);
2117         return ret;
2118 }
2119 EXPORT_SYMBOL(video_register_device_index);
2120
2121 /**
2122  *      video_unregister_device - unregister a video4linux device
2123  *      @vfd: the device to unregister
2124  *
2125  *      This unregisters the passed device and deassigns the minor
2126  *      number. Future open calls will be met with errors.
2127  */
2128
2129 void video_unregister_device(struct video_device *vfd)
2130 {
2131         mutex_lock(&videodev_lock);
2132         if(video_device[vfd->minor]!=vfd)
2133                 panic("videodev: bad unregister");
2134
2135         video_device[vfd->minor]=NULL;
2136         device_unregister(&vfd->class_dev);
2137         mutex_unlock(&videodev_lock);
2138 }
2139 EXPORT_SYMBOL(video_unregister_device);
2140
2141 /*
2142  * Video fs operations
2143  */
2144 static const struct file_operations video_fops=
2145 {
2146         .owner          = THIS_MODULE,
2147         .llseek         = no_llseek,
2148         .open           = video_open,
2149 };
2150
2151 /*
2152  *      Initialise video for linux
2153  */
2154
2155 static int __init videodev_init(void)
2156 {
2157         int ret;
2158
2159         printk(KERN_INFO "Linux video capture interface: v2.00\n");
2160         if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
2161                 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
2162                 return -EIO;
2163         }
2164
2165         ret = class_register(&video_class);
2166         if (ret < 0) {
2167                 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
2168                 printk(KERN_WARNING "video_dev: class_register failed\n");
2169                 return -EIO;
2170         }
2171
2172         return 0;
2173 }
2174
2175 static void __exit videodev_exit(void)
2176 {
2177         class_unregister(&video_class);
2178         unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
2179 }
2180
2181 module_init(videodev_init)
2182 module_exit(videodev_exit)
2183
2184 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
2185 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
2186 MODULE_LICENSE("GPL");
2187
2188
2189 /*
2190  * Local variables:
2191  * c-basic-offset: 8
2192  * End:
2193  */