Merge branch 'work'
[linux-2.6] / drivers / media / video / v4l2-common.c
1 /*
2  *      Video for Linux Two
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 file replaces the videodev.c file that comes with the
8  *      regular kernel distribution.
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  *
15  * Author:      Bill Dirks <bdirks@pacbell.net>
16  *              based on code by Alan Cox, <alan@cymru.net>
17  *
18  */
19
20 /*
21  * Video capture interface for Linux
22  *
23  *      A generic video device interface for the LINUX operating system
24  *      using a set of device structures/vectors for low level operations.
25  *
26  *              This program is free software; you can redistribute it and/or
27  *              modify it under the terms of the GNU General Public License
28  *              as published by the Free Software Foundation; either version
29  *              2 of the License, or (at your option) any later version.
30  *
31  * Author:      Alan Cox, <alan@redhat.com>
32  *
33  * Fixes:
34  */
35
36 /*
37  * Video4linux 1/2 integration by Justin Schoeman
38  * <justin@suntiger.ee.up.ac.za>
39  * 2.4 PROCFS support ported from 2.4 kernels by
40  *  Iñaki García Etxebarria <garetxe@euskalnet.net>
41  * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
42  * 2.4 devfs support ported from 2.4 kernels by
43  *  Dan Merillat <dan@merillat.org>
44  * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
45  */
46
47 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/smp_lock.h>
53 #include <linux/mm.h>
54 #include <linux/string.h>
55 #include <linux/errno.h>
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58 #include <asm/pgtable.h>
59 #include <asm/io.h>
60 #include <asm/div64.h>
61 #include <linux/video_decoder.h>
62 #include <media/v4l2-common.h>
63
64 #ifdef CONFIG_KMOD
65 #include <linux/kmod.h>
66 #endif
67
68 #if defined(CONFIG_UST) || defined(CONFIG_UST_MODULE)
69 #include <linux/ust.h>
70 #endif
71
72
73 #include <linux/videodev.h>
74
75 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
76 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
77 MODULE_LICENSE("GPL");
78
79 /*
80  *
81  *      V 4 L 2   D R I V E R   H E L P E R   A P I
82  *
83  */
84
85 /*
86  *  Video Standard Operations (contributed by Michael Schimek)
87  */
88
89
90 /* Fill in the fields of a v4l2_standard structure according to the
91    'id' and 'transmission' parameters.  Returns negative on error.  */
92 int v4l2_video_std_construct(struct v4l2_standard *vs,
93                              int id, char *name)
94 {
95         u32 index = vs->index;
96
97         memset(vs, 0, sizeof(struct v4l2_standard));
98         vs->index = index;
99         vs->id    = id;
100         if (id & (V4L2_STD_NTSC | V4L2_STD_PAL_M)) {
101                 vs->frameperiod.numerator = 1001;
102                 vs->frameperiod.denominator = 30000;
103                 vs->framelines = 525;
104         } else {
105                 vs->frameperiod.numerator = 1;
106                 vs->frameperiod.denominator = 25;
107                 vs->framelines = 625;
108         }
109         strlcpy(vs->name,name,sizeof(vs->name));
110         return 0;
111 }
112
113
114 /* ----------------------------------------------------------------- */
115 /* priority handling                                                 */
116
117 #define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND   || \
118                               val == V4L2_PRIORITY_INTERACTIVE  || \
119                               val == V4L2_PRIORITY_RECORD)
120
121 int v4l2_prio_init(struct v4l2_prio_state *global)
122 {
123         memset(global,0,sizeof(*global));
124         return 0;
125 }
126
127 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
128                      enum v4l2_priority new)
129 {
130         if (!V4L2_PRIO_VALID(new))
131                 return -EINVAL;
132         if (*local == new)
133                 return 0;
134
135         atomic_inc(&global->prios[new]);
136         if (V4L2_PRIO_VALID(*local))
137                 atomic_dec(&global->prios[*local]);
138         *local = new;
139         return 0;
140 }
141
142 int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
143 {
144         return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
145 }
146
147 int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
148 {
149         if (V4L2_PRIO_VALID(*local))
150                 atomic_dec(&global->prios[*local]);
151         return 0;
152 }
153
154 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
155 {
156         if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
157                 return V4L2_PRIORITY_RECORD;
158         if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
159                 return V4L2_PRIORITY_INTERACTIVE;
160         if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
161                 return V4L2_PRIORITY_BACKGROUND;
162         return V4L2_PRIORITY_UNSET;
163 }
164
165 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
166 {
167         if (*local < v4l2_prio_max(global))
168                 return -EBUSY;
169         return 0;
170 }
171
172
173 /* ----------------------------------------------------------------- */
174 /* some arrays for pretty-printing debug messages                    */
175
176 char *v4l2_field_names[] = {
177         [V4L2_FIELD_ANY]        = "any",
178         [V4L2_FIELD_NONE]       = "none",
179         [V4L2_FIELD_TOP]        = "top",
180         [V4L2_FIELD_BOTTOM]     = "bottom",
181         [V4L2_FIELD_INTERLACED] = "interlaced",
182         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
183         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
184         [V4L2_FIELD_ALTERNATE]  = "alternate",
185 };
186
187 char *v4l2_type_names[] = {
188         [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
189         [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
190         [V4L2_BUF_TYPE_VIDEO_OUTPUT]  = "video-out",
191         [V4L2_BUF_TYPE_VBI_CAPTURE]   = "vbi-cap",
192         [V4L2_BUF_TYPE_VBI_OUTPUT]    = "vbi-out",
193 };
194
195 /* ------------------------------------------------------------------ */
196 /* debug help functions                                               */
197
198 #ifdef HAVE_V4L1
199 static const char *v4l1_ioctls[] = {
200         [_IOC_NR(VIDIOCGCAP)]       = "VIDIOCGCAP",
201         [_IOC_NR(VIDIOCGCHAN)]      = "VIDIOCGCHAN",
202         [_IOC_NR(VIDIOCSCHAN)]      = "VIDIOCSCHAN",
203         [_IOC_NR(VIDIOCGTUNER)]     = "VIDIOCGTUNER",
204         [_IOC_NR(VIDIOCSTUNER)]     = "VIDIOCSTUNER",
205         [_IOC_NR(VIDIOCGPICT)]      = "VIDIOCGPICT",
206         [_IOC_NR(VIDIOCSPICT)]      = "VIDIOCSPICT",
207         [_IOC_NR(VIDIOCCAPTURE)]    = "VIDIOCCAPTURE",
208         [_IOC_NR(VIDIOCGWIN)]       = "VIDIOCGWIN",
209         [_IOC_NR(VIDIOCSWIN)]       = "VIDIOCSWIN",
210         [_IOC_NR(VIDIOCGFBUF)]      = "VIDIOCGFBUF",
211         [_IOC_NR(VIDIOCSFBUF)]      = "VIDIOCSFBUF",
212         [_IOC_NR(VIDIOCKEY)]        = "VIDIOCKEY",
213         [_IOC_NR(VIDIOCGFREQ)]      = "VIDIOCGFREQ",
214         [_IOC_NR(VIDIOCSFREQ)]      = "VIDIOCSFREQ",
215         [_IOC_NR(VIDIOCGAUDIO)]     = "VIDIOCGAUDIO",
216         [_IOC_NR(VIDIOCSAUDIO)]     = "VIDIOCSAUDIO",
217         [_IOC_NR(VIDIOCSYNC)]       = "VIDIOCSYNC",
218         [_IOC_NR(VIDIOCMCAPTURE)]   = "VIDIOCMCAPTURE",
219         [_IOC_NR(VIDIOCGMBUF)]      = "VIDIOCGMBUF",
220         [_IOC_NR(VIDIOCGUNIT)]      = "VIDIOCGUNIT",
221         [_IOC_NR(VIDIOCGCAPTURE)]   = "VIDIOCGCAPTURE",
222         [_IOC_NR(VIDIOCSCAPTURE)]   = "VIDIOCSCAPTURE",
223         [_IOC_NR(VIDIOCSPLAYMODE)]  = "VIDIOCSPLAYMODE",
224         [_IOC_NR(VIDIOCSWRITEMODE)] = "VIDIOCSWRITEMODE",
225         [_IOC_NR(VIDIOCGPLAYINFO)]  = "VIDIOCGPLAYINFO",
226         [_IOC_NR(VIDIOCSMICROCODE)] = "VIDIOCSMICROCODE",
227         [_IOC_NR(VIDIOCGVBIFMT)]    = "VIDIOCGVBIFMT",
228         [_IOC_NR(VIDIOCSVBIFMT)]    = "VIDIOCSVBIFMT"
229 };
230 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
231 #endif
232
233 static const char *v4l2_ioctls[] = {
234         [_IOC_NR(VIDIOC_QUERYCAP)]         = "VIDIOC_QUERYCAP",
235         [_IOC_NR(VIDIOC_RESERVED)]         = "VIDIOC_RESERVED",
236         [_IOC_NR(VIDIOC_ENUM_FMT)]         = "VIDIOC_ENUM_FMT",
237         [_IOC_NR(VIDIOC_G_FMT)]            = "VIDIOC_G_FMT",
238         [_IOC_NR(VIDIOC_S_FMT)]            = "VIDIOC_S_FMT",
239         [_IOC_NR(VIDIOC_G_MPEGCOMP)]       = "VIDIOC_G_MPEGCOMP",
240         [_IOC_NR(VIDIOC_S_MPEGCOMP)]       = "VIDIOC_S_MPEGCOMP",
241         [_IOC_NR(VIDIOC_REQBUFS)]          = "VIDIOC_REQBUFS",
242         [_IOC_NR(VIDIOC_QUERYBUF)]         = "VIDIOC_QUERYBUF",
243         [_IOC_NR(VIDIOC_G_FBUF)]           = "VIDIOC_G_FBUF",
244         [_IOC_NR(VIDIOC_S_FBUF)]           = "VIDIOC_S_FBUF",
245         [_IOC_NR(VIDIOC_OVERLAY)]          = "VIDIOC_OVERLAY",
246         [_IOC_NR(VIDIOC_QBUF)]             = "VIDIOC_QBUF",
247         [_IOC_NR(VIDIOC_DQBUF)]            = "VIDIOC_DQBUF",
248         [_IOC_NR(VIDIOC_STREAMON)]         = "VIDIOC_STREAMON",
249         [_IOC_NR(VIDIOC_STREAMOFF)]        = "VIDIOC_STREAMOFF",
250         [_IOC_NR(VIDIOC_G_PARM)]           = "VIDIOC_G_PARM",
251         [_IOC_NR(VIDIOC_S_PARM)]           = "VIDIOC_S_PARM",
252         [_IOC_NR(VIDIOC_G_STD)]            = "VIDIOC_G_STD",
253         [_IOC_NR(VIDIOC_S_STD)]            = "VIDIOC_S_STD",
254         [_IOC_NR(VIDIOC_ENUMSTD)]          = "VIDIOC_ENUMSTD",
255         [_IOC_NR(VIDIOC_ENUMINPUT)]        = "VIDIOC_ENUMINPUT",
256         [_IOC_NR(VIDIOC_G_CTRL)]           = "VIDIOC_G_CTRL",
257         [_IOC_NR(VIDIOC_S_CTRL)]           = "VIDIOC_S_CTRL",
258         [_IOC_NR(VIDIOC_G_TUNER)]          = "VIDIOC_G_TUNER",
259         [_IOC_NR(VIDIOC_S_TUNER)]          = "VIDIOC_S_TUNER",
260         [_IOC_NR(VIDIOC_G_AUDIO)]          = "VIDIOC_G_AUDIO",
261         [_IOC_NR(VIDIOC_S_AUDIO)]          = "VIDIOC_S_AUDIO",
262         [_IOC_NR(VIDIOC_QUERYCTRL)]        = "VIDIOC_QUERYCTRL",
263         [_IOC_NR(VIDIOC_QUERYMENU)]        = "VIDIOC_QUERYMENU",
264         [_IOC_NR(VIDIOC_G_INPUT)]          = "VIDIOC_G_INPUT",
265         [_IOC_NR(VIDIOC_S_INPUT)]          = "VIDIOC_S_INPUT",
266         [_IOC_NR(VIDIOC_G_OUTPUT)]         = "VIDIOC_G_OUTPUT",
267         [_IOC_NR(VIDIOC_S_OUTPUT)]         = "VIDIOC_S_OUTPUT",
268         [_IOC_NR(VIDIOC_ENUMOUTPUT)]       = "VIDIOC_ENUMOUTPUT",
269         [_IOC_NR(VIDIOC_G_AUDOUT)]         = "VIDIOC_G_AUDOUT",
270         [_IOC_NR(VIDIOC_S_AUDOUT)]         = "VIDIOC_S_AUDOUT",
271         [_IOC_NR(VIDIOC_G_MODULATOR)]      = "VIDIOC_G_MODULATOR",
272         [_IOC_NR(VIDIOC_S_MODULATOR)]      = "VIDIOC_S_MODULATOR",
273         [_IOC_NR(VIDIOC_G_FREQUENCY)]      = "VIDIOC_G_FREQUENCY",
274         [_IOC_NR(VIDIOC_S_FREQUENCY)]      = "VIDIOC_S_FREQUENCY",
275         [_IOC_NR(VIDIOC_CROPCAP)]          = "VIDIOC_CROPCAP",
276         [_IOC_NR(VIDIOC_G_CROP)]           = "VIDIOC_G_CROP",
277         [_IOC_NR(VIDIOC_S_CROP)]           = "VIDIOC_S_CROP",
278         [_IOC_NR(VIDIOC_G_JPEGCOMP)]       = "VIDIOC_G_JPEGCOMP",
279         [_IOC_NR(VIDIOC_S_JPEGCOMP)]       = "VIDIOC_S_JPEGCOMP",
280         [_IOC_NR(VIDIOC_QUERYSTD)]         = "VIDIOC_QUERYSTD",
281         [_IOC_NR(VIDIOC_TRY_FMT)]          = "VIDIOC_TRY_FMT",
282         [_IOC_NR(VIDIOC_ENUMAUDIO)]        = "VIDIOC_ENUMAUDIO",
283         [_IOC_NR(VIDIOC_ENUMAUDOUT)]       = "VIDIOC_ENUMAUDOUT",
284         [_IOC_NR(VIDIOC_G_PRIORITY)]       = "VIDIOC_G_PRIORITY",
285         [_IOC_NR(VIDIOC_S_PRIORITY)]       = "VIDIOC_S_PRIORITY",
286 #if 1
287         [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
288 #endif
289         [_IOC_NR(VIDIOC_LOG_STATUS)]       = "VIDIOC_LOG_STATUS"
290 };
291 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
292
293 static const char *v4l2_int_ioctls[] = {
294 #ifdef HAVE_VIDEO_DECODER
295         [_IOC_NR(DECODER_GET_CAPABILITIES)]    = "DECODER_GET_CAPABILITIES",
296         [_IOC_NR(DECODER_GET_STATUS)]          = "DECODER_GET_STATUS",
297         [_IOC_NR(DECODER_SET_NORM)]            = "DECODER_SET_NORM",
298         [_IOC_NR(DECODER_SET_INPUT)]           = "DECODER_SET_INPUT",
299         [_IOC_NR(DECODER_SET_OUTPUT)]          = "DECODER_SET_OUTPUT",
300         [_IOC_NR(DECODER_ENABLE_OUTPUT)]       = "DECODER_ENABLE_OUTPUT",
301         [_IOC_NR(DECODER_SET_PICTURE)]         = "DECODER_SET_PICTURE",
302         [_IOC_NR(DECODER_SET_GPIO)]            = "DECODER_SET_GPIO",
303         [_IOC_NR(DECODER_INIT)]                = "DECODER_INIT",
304         [_IOC_NR(DECODER_SET_VBI_BYPASS)]      = "DECODER_SET_VBI_BYPASS",
305         [_IOC_NR(DECODER_DUMP)]                = "DECODER_DUMP",
306 #endif
307         [_IOC_NR(AUDC_SET_RADIO)]              = "AUDC_SET_RADIO",
308         [_IOC_NR(AUDC_SET_INPUT)]              = "AUDC_SET_INPUT",
309         [_IOC_NR(MSP_SET_MATRIX)]              = "MSP_SET_MATRIX",
310
311         [_IOC_NR(TUNER_SET_TYPE_ADDR)]         = "TUNER_SET_TYPE_ADDR",
312         [_IOC_NR(TUNER_SET_STANDBY)]           = "TUNER_SET_STANDBY",
313         [_IOC_NR(TDA9887_SET_CONFIG)]          = "TDA9887_SET_CONFIG",
314
315         [_IOC_NR(VIDIOC_INT_S_REGISTER)]       = "VIDIOC_INT_S_REGISTER",
316         [_IOC_NR(VIDIOC_INT_G_REGISTER)]       = "VIDIOC_INT_G_REGISTER",
317         [_IOC_NR(VIDIOC_INT_RESET)]            = "VIDIOC_INT_RESET",
318         [_IOC_NR(VIDIOC_INT_AUDIO_CLOCK_FREQ)] = "VIDIOC_INT_AUDIO_CLOCK_FREQ",
319         [_IOC_NR(VIDIOC_INT_DECODE_VBI_LINE)]  = "VIDIOC_INT_DECODE_VBI_LINE",
320         [_IOC_NR(VIDIOC_INT_S_VBI_DATA)]       = "VIDIOC_INT_S_VBI_DATA",
321         [_IOC_NR(VIDIOC_INT_G_VBI_DATA)]       = "VIDIOC_INT_G_VBI_DATA",
322         [_IOC_NR(VIDIOC_INT_G_CHIP_IDENT)]     = "VIDIOC_INT_G_CHIP_IDENT",
323         [_IOC_NR(VIDIOC_INT_I2S_CLOCK_FREQ)]   = "VIDIOC_INT_I2S_CLOCK_FREQ"
324 };
325 #define V4L2_INT_IOCTLS ARRAY_SIZE(v4l2_int_ioctls)
326
327 /* Common ioctl debug function. This function can be used by
328    external ioctl messages as well as internal V4L ioctl */
329 void v4l_printk_ioctl(unsigned int cmd)
330 {
331         char *dir;
332
333         switch (_IOC_DIR(cmd)) {
334         case _IOC_NONE:              dir = "--"; break;
335         case _IOC_READ:              dir = "r-"; break;
336         case _IOC_WRITE:             dir = "-w"; break;
337         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
338         default:                     dir = "*ERR*"; break;
339         }
340         switch (_IOC_TYPE(cmd)) {
341         case 'd':
342                 printk("v4l2_int ioctl %s, dir=%s (0x%08x)\n",
343                        (_IOC_NR(cmd) < V4L2_INT_IOCTLS) ?
344                        v4l2_int_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
345                 break;
346 #ifdef HAVE_V4L1
347         case 'v':
348                 printk("v4l1 ioctl %s, dir=%s (0x%08x)\n",
349                        (_IOC_NR(cmd) < V4L1_IOCTLS) ?
350                        v4l1_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
351                 break;
352 #endif
353         case 'V':
354                 printk("v4l2 ioctl %s, dir=%s (0x%08x)\n",
355                        (_IOC_NR(cmd) < V4L2_IOCTLS) ?
356                        v4l2_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
357                 break;
358
359         default:
360                 printk("unknown ioctl '%c', dir=%s, #%d (0x%08x)\n",
361                        _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
362         }
363 }
364
365 /* ----------------------------------------------------------------- */
366
367 EXPORT_SYMBOL(v4l2_video_std_construct);
368
369 EXPORT_SYMBOL(v4l2_prio_init);
370 EXPORT_SYMBOL(v4l2_prio_change);
371 EXPORT_SYMBOL(v4l2_prio_open);
372 EXPORT_SYMBOL(v4l2_prio_close);
373 EXPORT_SYMBOL(v4l2_prio_max);
374 EXPORT_SYMBOL(v4l2_prio_check);
375
376 EXPORT_SYMBOL(v4l2_field_names);
377 EXPORT_SYMBOL(v4l2_type_names);
378 EXPORT_SYMBOL(v4l_printk_ioctl);
379
380 /*
381  * Local variables:
382  * c-basic-offset: 8
383  * End:
384  */