Merge /spare/repo/linux-2.6/
[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
62 #ifdef CONFIG_KMOD
63 #include <linux/kmod.h>
64 #endif
65
66 #if defined(CONFIG_UST) || defined(CONFIG_UST_MODULE)
67 #include <linux/ust.h>
68 #endif
69
70
71 #include <linux/videodev.h>
72
73 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
74 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
75 MODULE_LICENSE("GPL");
76
77 /*
78  *
79  *      V 4 L 2   D R I V E R   H E L P E R   A P I
80  *
81  */
82
83 /*
84  *  Video Standard Operations (contributed by Michael Schimek)
85  */
86
87
88 /* Fill in the fields of a v4l2_standard structure according to the
89    'id' and 'transmission' parameters.  Returns negative on error.  */
90 int v4l2_video_std_construct(struct v4l2_standard *vs,
91                              int id, char *name)
92 {
93         u32 index = vs->index;
94
95         memset(vs, 0, sizeof(struct v4l2_standard));
96         vs->index = index;
97         vs->id    = id;
98         if (id & (V4L2_STD_NTSC | V4L2_STD_PAL_M)) {
99                 vs->frameperiod.numerator = 1001;
100                 vs->frameperiod.denominator = 30000;
101                 vs->framelines = 525;
102         } else {
103                 vs->frameperiod.numerator = 1;
104                 vs->frameperiod.denominator = 25;
105                 vs->framelines = 625;
106         }
107         strlcpy(vs->name,name,sizeof(vs->name));
108         return 0;
109 }
110
111
112 /* ----------------------------------------------------------------- */
113 /* priority handling                                                 */
114
115 #define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND   || \
116                               val == V4L2_PRIORITY_INTERACTIVE  || \
117                               val == V4L2_PRIORITY_RECORD)
118
119 int v4l2_prio_init(struct v4l2_prio_state *global)
120 {
121         memset(global,0,sizeof(*global));
122         return 0;
123 }
124
125 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
126                      enum v4l2_priority new)
127 {
128         if (!V4L2_PRIO_VALID(new))
129                 return -EINVAL;
130         if (*local == new)
131                 return 0;
132
133         atomic_inc(&global->prios[new]);
134         if (V4L2_PRIO_VALID(*local))
135                 atomic_dec(&global->prios[*local]);
136         *local = new;
137         return 0;
138 }
139
140 int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
141 {
142         return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
143 }
144
145 int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
146 {
147         if (V4L2_PRIO_VALID(*local))
148                 atomic_dec(&global->prios[*local]);
149         return 0;
150 }
151
152 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
153 {
154         if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
155                 return V4L2_PRIORITY_RECORD;
156         if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
157                 return V4L2_PRIORITY_INTERACTIVE;
158         if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
159                 return V4L2_PRIORITY_BACKGROUND;
160         return V4L2_PRIORITY_UNSET;
161 }
162
163 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
164 {
165         if (*local < v4l2_prio_max(global))
166                 return -EBUSY;
167         return 0;
168 }
169
170
171 /* ----------------------------------------------------------------- */
172 /* some arrays for pretty-printing debug messages                    */
173
174 char *v4l2_field_names[] = {
175         [V4L2_FIELD_ANY]        = "any",
176         [V4L2_FIELD_NONE]       = "none",
177         [V4L2_FIELD_TOP]        = "top",
178         [V4L2_FIELD_BOTTOM]     = "bottom",
179         [V4L2_FIELD_INTERLACED] = "interlaced",
180         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
181         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
182         [V4L2_FIELD_ALTERNATE]  = "alternate",
183 };
184
185 char *v4l2_type_names[] = {
186         [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
187         [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
188         [V4L2_BUF_TYPE_VIDEO_OUTPUT]  = "video-out",
189         [V4L2_BUF_TYPE_VBI_CAPTURE]   = "vbi-cap",
190         [V4L2_BUF_TYPE_VBI_OUTPUT]    = "vbi-out",
191 };
192
193 char *v4l2_ioctl_names[256] = {
194 #if __GNUC__ >= 3
195         [0 ... 255]                      = "UNKNOWN",
196 #endif
197         [_IOC_NR(VIDIOC_QUERYCAP)]       = "VIDIOC_QUERYCAP",
198         [_IOC_NR(VIDIOC_RESERVED)]       = "VIDIOC_RESERVED",
199         [_IOC_NR(VIDIOC_ENUM_FMT)]       = "VIDIOC_ENUM_FMT",
200         [_IOC_NR(VIDIOC_G_FMT)]          = "VIDIOC_G_FMT",
201         [_IOC_NR(VIDIOC_S_FMT)]          = "VIDIOC_S_FMT",
202         [_IOC_NR(VIDIOC_REQBUFS)]        = "VIDIOC_REQBUFS",
203         [_IOC_NR(VIDIOC_QUERYBUF)]       = "VIDIOC_QUERYBUF",
204         [_IOC_NR(VIDIOC_G_FBUF)]         = "VIDIOC_G_FBUF",
205         [_IOC_NR(VIDIOC_S_FBUF)]         = "VIDIOC_S_FBUF",
206         [_IOC_NR(VIDIOC_OVERLAY)]        = "VIDIOC_OVERLAY",
207         [_IOC_NR(VIDIOC_QBUF)]           = "VIDIOC_QBUF",
208         [_IOC_NR(VIDIOC_DQBUF)]          = "VIDIOC_DQBUF",
209         [_IOC_NR(VIDIOC_STREAMON)]       = "VIDIOC_STREAMON",
210         [_IOC_NR(VIDIOC_STREAMOFF)]      = "VIDIOC_STREAMOFF",
211         [_IOC_NR(VIDIOC_G_PARM)]         = "VIDIOC_G_PARM",
212         [_IOC_NR(VIDIOC_S_PARM)]         = "VIDIOC_S_PARM",
213         [_IOC_NR(VIDIOC_G_STD)]          = "VIDIOC_G_STD",
214         [_IOC_NR(VIDIOC_S_STD)]          = "VIDIOC_S_STD",
215         [_IOC_NR(VIDIOC_ENUMSTD)]        = "VIDIOC_ENUMSTD",
216         [_IOC_NR(VIDIOC_ENUMINPUT)]      = "VIDIOC_ENUMINPUT",
217         [_IOC_NR(VIDIOC_G_CTRL)]         = "VIDIOC_G_CTRL",
218         [_IOC_NR(VIDIOC_S_CTRL)]         = "VIDIOC_S_CTRL",
219         [_IOC_NR(VIDIOC_G_TUNER)]        = "VIDIOC_G_TUNER",
220         [_IOC_NR(VIDIOC_S_TUNER)]        = "VIDIOC_S_TUNER",
221         [_IOC_NR(VIDIOC_G_AUDIO)]        = "VIDIOC_G_AUDIO",
222         [_IOC_NR(VIDIOC_S_AUDIO)]        = "VIDIOC_S_AUDIO",
223         [_IOC_NR(VIDIOC_QUERYCTRL)]      = "VIDIOC_QUERYCTRL",
224         [_IOC_NR(VIDIOC_QUERYMENU)]      = "VIDIOC_QUERYMENU",
225         [_IOC_NR(VIDIOC_G_INPUT)]        = "VIDIOC_G_INPUT",
226         [_IOC_NR(VIDIOC_S_INPUT)]        = "VIDIOC_S_INPUT",
227         [_IOC_NR(VIDIOC_G_OUTPUT)]       = "VIDIOC_G_OUTPUT",
228         [_IOC_NR(VIDIOC_S_OUTPUT)]       = "VIDIOC_S_OUTPUT",
229         [_IOC_NR(VIDIOC_ENUMOUTPUT)]     = "VIDIOC_ENUMOUTPUT",
230         [_IOC_NR(VIDIOC_G_AUDOUT)]       = "VIDIOC_G_AUDOUT",
231         [_IOC_NR(VIDIOC_S_AUDOUT)]       = "VIDIOC_S_AUDOUT",
232         [_IOC_NR(VIDIOC_G_MODULATOR)]    = "VIDIOC_G_MODULATOR",
233         [_IOC_NR(VIDIOC_S_MODULATOR)]    = "VIDIOC_S_MODULATOR",
234         [_IOC_NR(VIDIOC_G_FREQUENCY)]    = "VIDIOC_G_FREQUENCY",
235         [_IOC_NR(VIDIOC_S_FREQUENCY)]    = "VIDIOC_S_FREQUENCY",
236         [_IOC_NR(VIDIOC_CROPCAP)]        = "VIDIOC_CROPCAP",
237         [_IOC_NR(VIDIOC_G_CROP)]         = "VIDIOC_G_CROP",
238         [_IOC_NR(VIDIOC_S_CROP)]         = "VIDIOC_S_CROP",
239         [_IOC_NR(VIDIOC_G_JPEGCOMP)]     = "VIDIOC_G_JPEGCOMP",
240         [_IOC_NR(VIDIOC_S_JPEGCOMP)]     = "VIDIOC_S_JPEGCOMP",
241         [_IOC_NR(VIDIOC_QUERYSTD)]       = "VIDIOC_QUERYSTD",
242         [_IOC_NR(VIDIOC_TRY_FMT)]        = "VIDIOC_TRY_FMT",
243 };
244
245 /* ----------------------------------------------------------------- */
246
247 EXPORT_SYMBOL(v4l2_video_std_construct);
248
249 EXPORT_SYMBOL(v4l2_prio_init);
250 EXPORT_SYMBOL(v4l2_prio_change);
251 EXPORT_SYMBOL(v4l2_prio_open);
252 EXPORT_SYMBOL(v4l2_prio_close);
253 EXPORT_SYMBOL(v4l2_prio_max);
254 EXPORT_SYMBOL(v4l2_prio_check);
255
256 EXPORT_SYMBOL(v4l2_field_names);
257 EXPORT_SYMBOL(v4l2_type_names);
258 EXPORT_SYMBOL(v4l2_ioctl_names);
259
260 /*
261  * Local variables:
262  * c-basic-offset: 8
263  * End:
264  */