4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
7 * This file replaces the videodev.c file that comes with the
8 * regular kernel distribution.
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.
15 * Author: Bill Dirks <bdirks@pacbell.net>
16 * based on code by Alan Cox, <alan@cymru.net>
21 * Video capture interface for Linux
23 * A generic video device interface for the LINUX operating system
24 * using a set of device structures/vectors for low level operations.
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.
31 * Author: Alan Cox, <alan@redhat.com>
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)
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>
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>
60 #include <asm/div64.h>
61 #include <linux/video_decoder.h>
62 #include <media/v4l2-common.h>
65 #include <linux/kmod.h>
68 #if defined(CONFIG_UST) || defined(CONFIG_UST_MODULE)
69 #include <linux/ust.h>
73 #include <linux/videodev.h>
75 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
76 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
77 MODULE_LICENSE("GPL");
81 * V 4 L 2 D R I V E R H E L P E R A P I
86 * Video Standard Operations (contributed by Michael Schimek)
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,
95 u32 index = vs->index;
97 memset(vs, 0, sizeof(struct v4l2_standard));
100 if (id & (V4L2_STD_NTSC | V4L2_STD_PAL_M)) {
101 vs->frameperiod.numerator = 1001;
102 vs->frameperiod.denominator = 30000;
103 vs->framelines = 525;
105 vs->frameperiod.numerator = 1;
106 vs->frameperiod.denominator = 25;
107 vs->framelines = 625;
109 strlcpy(vs->name,name,sizeof(vs->name));
114 /* ----------------------------------------------------------------- */
115 /* priority handling */
117 #define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND || \
118 val == V4L2_PRIORITY_INTERACTIVE || \
119 val == V4L2_PRIORITY_RECORD)
121 int v4l2_prio_init(struct v4l2_prio_state *global)
123 memset(global,0,sizeof(*global));
127 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
128 enum v4l2_priority new)
130 if (!V4L2_PRIO_VALID(new))
135 atomic_inc(&global->prios[new]);
136 if (V4L2_PRIO_VALID(*local))
137 atomic_dec(&global->prios[*local]);
142 int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
144 return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
147 int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
149 if (V4L2_PRIO_VALID(*local))
150 atomic_dec(&global->prios[*local]);
154 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
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;
165 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
167 if (*local < v4l2_prio_max(global))
173 /* ----------------------------------------------------------------- */
174 /* some arrays for pretty-printing debug messages */
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",
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",
195 /* ------------------------------------------------------------------ */
196 /* debug help functions */
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"
230 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
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",
287 [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
289 [_IOC_NR(VIDIOC_LOG_STATUS)] = "VIDIOC_LOG_STATUS"
291 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
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",
307 [_IOC_NR(AUDC_SET_RADIO)] = "AUDC_SET_RADIO",
308 [_IOC_NR(AUDC_SET_INPUT)] = "AUDC_SET_INPUT",
310 [_IOC_NR(TUNER_SET_TYPE_ADDR)] = "TUNER_SET_TYPE_ADDR",
311 [_IOC_NR(TUNER_SET_STANDBY)] = "TUNER_SET_STANDBY",
312 [_IOC_NR(TDA9887_SET_CONFIG)] = "TDA9887_SET_CONFIG",
314 [_IOC_NR(VIDIOC_INT_S_REGISTER)] = "VIDIOC_INT_S_REGISTER",
315 [_IOC_NR(VIDIOC_INT_G_REGISTER)] = "VIDIOC_INT_G_REGISTER",
316 [_IOC_NR(VIDIOC_INT_RESET)] = "VIDIOC_INT_RESET",
317 [_IOC_NR(VIDIOC_INT_AUDIO_CLOCK_FREQ)] = "VIDIOC_INT_AUDIO_CLOCK_FREQ",
318 [_IOC_NR(VIDIOC_INT_DECODE_VBI_LINE)] = "VIDIOC_INT_DECODE_VBI_LINE",
319 [_IOC_NR(VIDIOC_INT_S_VBI_DATA)] = "VIDIOC_INT_S_VBI_DATA",
320 [_IOC_NR(VIDIOC_INT_G_VBI_DATA)] = "VIDIOC_INT_G_VBI_DATA",
321 [_IOC_NR(VIDIOC_INT_G_CHIP_IDENT)] = "VIDIOC_INT_G_CHIP_IDENT",
322 [_IOC_NR(VIDIOC_INT_I2S_CLOCK_FREQ)] = "VIDIOC_INT_I2S_CLOCK_FREQ"
324 #define V4L2_INT_IOCTLS ARRAY_SIZE(v4l2_int_ioctls)
326 /* Common ioctl debug function. This function can be used by
327 external ioctl messages as well as internal V4L ioctl */
328 void v4l_printk_ioctl(unsigned int cmd)
332 switch (_IOC_DIR(cmd)) {
333 case _IOC_NONE: dir = "--"; break;
334 case _IOC_READ: dir = "r-"; break;
335 case _IOC_WRITE: dir = "-w"; break;
336 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
337 default: dir = "*ERR*"; break;
339 switch (_IOC_TYPE(cmd)) {
341 printk("v4l2_int ioctl %s, dir=%s (0x%08x)\n",
342 (_IOC_NR(cmd) < V4L2_INT_IOCTLS) ?
343 v4l2_int_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
347 printk("v4l1 ioctl %s, dir=%s (0x%08x)\n",
348 (_IOC_NR(cmd) < V4L1_IOCTLS) ?
349 v4l1_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
353 printk("v4l2 ioctl %s, dir=%s (0x%08x)\n",
354 (_IOC_NR(cmd) < V4L2_IOCTLS) ?
355 v4l2_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
359 printk("unknown ioctl '%c', dir=%s, #%d (0x%08x)\n",
360 _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
364 /* ----------------------------------------------------------------- */
366 EXPORT_SYMBOL(v4l2_video_std_construct);
368 EXPORT_SYMBOL(v4l2_prio_init);
369 EXPORT_SYMBOL(v4l2_prio_change);
370 EXPORT_SYMBOL(v4l2_prio_open);
371 EXPORT_SYMBOL(v4l2_prio_close);
372 EXPORT_SYMBOL(v4l2_prio_max);
373 EXPORT_SYMBOL(v4l2_prio_check);
375 EXPORT_SYMBOL(v4l2_field_names);
376 EXPORT_SYMBOL(v4l2_type_names);
377 EXPORT_SYMBOL(v4l_printk_ioctl);