Merge with ../linux-2.6-smp
[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 #if 0 /* seems to have no users */
88 /* This is the recommended method to deal with the framerate fields. More
89    sophisticated drivers will access the fields directly. */
90 unsigned int
91 v4l2_video_std_fps(struct v4l2_standard *vs)
92 {
93         if (vs->frameperiod.numerator > 0)
94                 return (((vs->frameperiod.denominator << 8) /
95                          vs->frameperiod.numerator) +
96                         (1 << 7)) / (1 << 8);
97         return 0;
98 }
99 EXPORT_SYMBOL(v4l2_video_std_fps);
100 #endif
101
102 /* Fill in the fields of a v4l2_standard structure according to the
103    'id' and 'transmission' parameters.  Returns negative on error.  */
104 int v4l2_video_std_construct(struct v4l2_standard *vs,
105                              int id, char *name)
106 {
107         u32 index = vs->index;
108
109         memset(vs, 0, sizeof(struct v4l2_standard));
110         vs->index = index;
111         vs->id    = id;
112         if (id & (V4L2_STD_NTSC | V4L2_STD_PAL_M)) {
113                 vs->frameperiod.numerator = 1001;
114                 vs->frameperiod.denominator = 30000;
115                 vs->framelines = 525;
116         } else {
117                 vs->frameperiod.numerator = 1;
118                 vs->frameperiod.denominator = 25;
119                 vs->framelines = 625;
120         }
121         strlcpy(vs->name,name,sizeof(vs->name));
122         return 0;
123 }
124
125
126 /* ----------------------------------------------------------------- */
127 /* priority handling                                                 */
128
129 #define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND   || \
130                               val == V4L2_PRIORITY_INTERACTIVE  || \
131                               val == V4L2_PRIORITY_RECORD)
132
133 int v4l2_prio_init(struct v4l2_prio_state *global)
134 {
135         memset(global,0,sizeof(*global));
136         return 0;
137 }
138
139 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
140                      enum v4l2_priority new)
141 {
142         if (!V4L2_PRIO_VALID(new))
143                 return -EINVAL;
144         if (*local == new)
145                 return 0;
146
147         atomic_inc(&global->prios[new]);
148         if (V4L2_PRIO_VALID(*local))
149                 atomic_dec(&global->prios[*local]);
150         *local = new;
151         return 0;
152 }
153
154 int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
155 {
156         return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
157 }
158
159 int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
160 {
161         if (V4L2_PRIO_VALID(*local))
162                 atomic_dec(&global->prios[*local]);
163         return 0;
164 }
165
166 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
167 {
168         if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
169                 return V4L2_PRIORITY_RECORD;
170         if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
171                 return V4L2_PRIORITY_INTERACTIVE;
172         if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
173                 return V4L2_PRIORITY_BACKGROUND;
174         return V4L2_PRIORITY_UNSET;
175 }
176
177 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
178 {
179         if (*local < v4l2_prio_max(global))
180                 return -EBUSY;
181         return 0;
182 }
183
184
185 /* ----------------------------------------------------------------- */
186 /* some arrays for pretty-printing debug messages                    */
187
188 char *v4l2_field_names[] = {
189         [V4L2_FIELD_ANY]        = "any",
190         [V4L2_FIELD_NONE]       = "none",
191         [V4L2_FIELD_TOP]        = "top",
192         [V4L2_FIELD_BOTTOM]     = "bottom",
193         [V4L2_FIELD_INTERLACED] = "interlaced",
194         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
195         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
196         [V4L2_FIELD_ALTERNATE]  = "alternate",
197 };
198
199 char *v4l2_type_names[] = {
200         [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
201         [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
202         [V4L2_BUF_TYPE_VIDEO_OUTPUT]  = "video-out",
203         [V4L2_BUF_TYPE_VBI_CAPTURE]   = "vbi-cap",
204         [V4L2_BUF_TYPE_VBI_OUTPUT]    = "vbi-out",
205 };
206
207 char *v4l2_ioctl_names[256] = {
208 #if __GNUC__ >= 3
209         [0 ... 255]                      = "UNKNOWN",
210 #endif
211         [_IOC_NR(VIDIOC_QUERYCAP)]       = "VIDIOC_QUERYCAP",
212         [_IOC_NR(VIDIOC_RESERVED)]       = "VIDIOC_RESERVED",
213         [_IOC_NR(VIDIOC_ENUM_FMT)]       = "VIDIOC_ENUM_FMT",
214         [_IOC_NR(VIDIOC_G_FMT)]          = "VIDIOC_G_FMT",
215         [_IOC_NR(VIDIOC_S_FMT)]          = "VIDIOC_S_FMT",
216 #if 0
217         [_IOC_NR(VIDIOC_G_COMP)]         = "VIDIOC_G_COMP",
218         [_IOC_NR(VIDIOC_S_COMP)]         = "VIDIOC_S_COMP",
219 #endif
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 };
262
263 /* ----------------------------------------------------------------- */
264
265 EXPORT_SYMBOL(v4l2_video_std_construct);
266
267 EXPORT_SYMBOL(v4l2_prio_init);
268 EXPORT_SYMBOL(v4l2_prio_change);
269 EXPORT_SYMBOL(v4l2_prio_open);
270 EXPORT_SYMBOL(v4l2_prio_close);
271 EXPORT_SYMBOL(v4l2_prio_max);
272 EXPORT_SYMBOL(v4l2_prio_check);
273
274 EXPORT_SYMBOL(v4l2_field_names);
275 EXPORT_SYMBOL(v4l2_type_names);
276 EXPORT_SYMBOL(v4l2_ioctl_names);
277
278 /*
279  * Local variables:
280  * c-basic-offset: 8
281  * End:
282  */