V4L/DVB (5485): Tda827x: delayed probing of tuner version
[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 <bill@thedirks.org>
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/module.h>
48 #include <linux/types.h>
49 #include <linux/kernel.h>
50 #include <linux/smp_lock.h>
51 #include <linux/mm.h>
52 #include <linux/string.h>
53 #include <linux/errno.h>
54 #include <linux/i2c.h>
55 #include <asm/uaccess.h>
56 #include <asm/system.h>
57 #include <asm/pgtable.h>
58 #include <asm/io.h>
59 #include <asm/div64.h>
60 #include <linux/video_decoder.h>
61 #define __OLD_VIDIOC_ /* To allow fixing old calls*/
62 #include <media/v4l2-common.h>
63 #include <media/v4l2-chip-ident.h>
64
65 #ifdef CONFIG_KMOD
66 #include <linux/kmod.h>
67 #endif
68
69 #if defined(CONFIG_UST) || defined(CONFIG_UST_MODULE)
70 #include <linux/ust.h>
71 #endif
72
73
74 #include <linux/videodev.h>
75
76 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
77 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
78 MODULE_LICENSE("GPL");
79
80 /*
81  *
82  *      V 4 L 2   D R I V E R   H E L P E R   A P I
83  *
84  */
85
86 /*
87  *  Video Standard Operations (contributed by Michael Schimek)
88  */
89
90
91 char *v4l2_norm_to_name(v4l2_std_id id)
92 {
93         char *name;
94         u32 myid = id;
95
96         /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
97            64 bit comparations. So, on that architecture, with some gcc variants,
98            compilation fails. Currently, the max value is 30bit wide.
99          */
100         BUG_ON(myid != id);
101
102         switch (myid) {
103         case V4L2_STD_PAL:
104                 name="PAL";             break;
105         case V4L2_STD_PAL_BG:
106                 name="PAL-BG";          break;
107         case V4L2_STD_PAL_DK:
108                 name="PAL-DK";          break;
109         case V4L2_STD_PAL_B:
110                 name="PAL-B";           break;
111         case V4L2_STD_PAL_B1:
112                 name="PAL-B1";          break;
113         case V4L2_STD_PAL_G:
114                 name="PAL-G";           break;
115         case V4L2_STD_PAL_H:
116                 name="PAL-H";           break;
117         case V4L2_STD_PAL_I:
118                 name="PAL-I";           break;
119         case V4L2_STD_PAL_D:
120                 name="PAL-D";           break;
121         case V4L2_STD_PAL_D1:
122                 name="PAL-D1";          break;
123         case V4L2_STD_PAL_K:
124                 name="PAL-K";           break;
125         case V4L2_STD_PAL_M:
126                 name="PAL-M";           break;
127         case V4L2_STD_PAL_N:
128                 name="PAL-N";           break;
129         case V4L2_STD_PAL_Nc:
130                 name="PAL-Nc";          break;
131         case V4L2_STD_PAL_60:
132                 name="PAL-60";          break;
133         case V4L2_STD_NTSC:
134                 name="NTSC";            break;
135         case V4L2_STD_NTSC_M:
136                 name="NTSC-M";          break;
137         case V4L2_STD_NTSC_M_JP:
138                 name="NTSC-M-JP";       break;
139         case V4L2_STD_NTSC_443:
140                 name="NTSC-443";        break;
141         case V4L2_STD_NTSC_M_KR:
142                 name="NTSC-M-KR";       break;
143         case V4L2_STD_SECAM:
144                 name="SECAM";           break;
145         case V4L2_STD_SECAM_DK:
146                 name="SECAM-DK";        break;
147         case V4L2_STD_SECAM_B:
148                 name="SECAM-B";         break;
149         case V4L2_STD_SECAM_D:
150                 name="SECAM-D";         break;
151         case V4L2_STD_SECAM_G:
152                 name="SECAM-G";         break;
153         case V4L2_STD_SECAM_H:
154                 name="SECAM-H";         break;
155         case V4L2_STD_SECAM_K:
156                 name="SECAM-K";         break;
157         case V4L2_STD_SECAM_K1:
158                 name="SECAM-K1";        break;
159         case V4L2_STD_SECAM_L:
160                 name="SECAM-L";         break;
161         case V4L2_STD_SECAM_LC:
162                 name="SECAM-LC";        break;
163         default:
164                 name="Unknown";         break;
165         }
166
167         return name;
168 }
169
170 /* Fill in the fields of a v4l2_standard structure according to the
171    'id' and 'transmission' parameters.  Returns negative on error.  */
172 int v4l2_video_std_construct(struct v4l2_standard *vs,
173                              int id, char *name)
174 {
175         u32 index = vs->index;
176
177         memset(vs, 0, sizeof(struct v4l2_standard));
178         vs->index = index;
179         vs->id    = id;
180         if (id & V4L2_STD_525_60) {
181                 vs->frameperiod.numerator = 1001;
182                 vs->frameperiod.denominator = 30000;
183                 vs->framelines = 525;
184         } else {
185                 vs->frameperiod.numerator = 1;
186                 vs->frameperiod.denominator = 25;
187                 vs->framelines = 625;
188         }
189         strlcpy(vs->name,name,sizeof(vs->name));
190         return 0;
191 }
192
193 /* ----------------------------------------------------------------- */
194 /* priority handling                                                 */
195
196 #define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND   || \
197                               val == V4L2_PRIORITY_INTERACTIVE  || \
198                               val == V4L2_PRIORITY_RECORD)
199
200 int v4l2_prio_init(struct v4l2_prio_state *global)
201 {
202         memset(global,0,sizeof(*global));
203         return 0;
204 }
205
206 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
207                      enum v4l2_priority new)
208 {
209         if (!V4L2_PRIO_VALID(new))
210                 return -EINVAL;
211         if (*local == new)
212                 return 0;
213
214         atomic_inc(&global->prios[new]);
215         if (V4L2_PRIO_VALID(*local))
216                 atomic_dec(&global->prios[*local]);
217         *local = new;
218         return 0;
219 }
220
221 int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
222 {
223         return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
224 }
225
226 int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
227 {
228         if (V4L2_PRIO_VALID(*local))
229                 atomic_dec(&global->prios[*local]);
230         return 0;
231 }
232
233 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
234 {
235         if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
236                 return V4L2_PRIORITY_RECORD;
237         if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
238                 return V4L2_PRIORITY_INTERACTIVE;
239         if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
240                 return V4L2_PRIORITY_BACKGROUND;
241         return V4L2_PRIORITY_UNSET;
242 }
243
244 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
245 {
246         if (*local < v4l2_prio_max(global))
247                 return -EBUSY;
248         return 0;
249 }
250
251
252 /* ----------------------------------------------------------------- */
253 /* some arrays for pretty-printing debug messages of enum types      */
254
255 char *v4l2_field_names[] = {
256         [V4L2_FIELD_ANY]        = "any",
257         [V4L2_FIELD_NONE]       = "none",
258         [V4L2_FIELD_TOP]        = "top",
259         [V4L2_FIELD_BOTTOM]     = "bottom",
260         [V4L2_FIELD_INTERLACED] = "interlaced",
261         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
262         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
263         [V4L2_FIELD_ALTERNATE]  = "alternate",
264         [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
265         [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
266 };
267
268 char *v4l2_type_names[] = {
269         [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "video-cap",
270         [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "video-over",
271         [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "video-out",
272         [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
273         [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
274         [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
275         [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
276         [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "video-out-over",
277 };
278
279
280 #define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
281
282 /* ------------------------------------------------------------------ */
283 /* debug help functions                                               */
284
285 #ifdef CONFIG_VIDEO_V4L1_COMPAT
286 static const char *v4l1_ioctls[] = {
287         [_IOC_NR(VIDIOCGCAP)]       = "VIDIOCGCAP",
288         [_IOC_NR(VIDIOCGCHAN)]      = "VIDIOCGCHAN",
289         [_IOC_NR(VIDIOCSCHAN)]      = "VIDIOCSCHAN",
290         [_IOC_NR(VIDIOCGTUNER)]     = "VIDIOCGTUNER",
291         [_IOC_NR(VIDIOCSTUNER)]     = "VIDIOCSTUNER",
292         [_IOC_NR(VIDIOCGPICT)]      = "VIDIOCGPICT",
293         [_IOC_NR(VIDIOCSPICT)]      = "VIDIOCSPICT",
294         [_IOC_NR(VIDIOCCAPTURE)]    = "VIDIOCCAPTURE",
295         [_IOC_NR(VIDIOCGWIN)]       = "VIDIOCGWIN",
296         [_IOC_NR(VIDIOCSWIN)]       = "VIDIOCSWIN",
297         [_IOC_NR(VIDIOCGFBUF)]      = "VIDIOCGFBUF",
298         [_IOC_NR(VIDIOCSFBUF)]      = "VIDIOCSFBUF",
299         [_IOC_NR(VIDIOCKEY)]        = "VIDIOCKEY",
300         [_IOC_NR(VIDIOCGFREQ)]      = "VIDIOCGFREQ",
301         [_IOC_NR(VIDIOCSFREQ)]      = "VIDIOCSFREQ",
302         [_IOC_NR(VIDIOCGAUDIO)]     = "VIDIOCGAUDIO",
303         [_IOC_NR(VIDIOCSAUDIO)]     = "VIDIOCSAUDIO",
304         [_IOC_NR(VIDIOCSYNC)]       = "VIDIOCSYNC",
305         [_IOC_NR(VIDIOCMCAPTURE)]   = "VIDIOCMCAPTURE",
306         [_IOC_NR(VIDIOCGMBUF)]      = "VIDIOCGMBUF",
307         [_IOC_NR(VIDIOCGUNIT)]      = "VIDIOCGUNIT",
308         [_IOC_NR(VIDIOCGCAPTURE)]   = "VIDIOCGCAPTURE",
309         [_IOC_NR(VIDIOCSCAPTURE)]   = "VIDIOCSCAPTURE",
310         [_IOC_NR(VIDIOCSPLAYMODE)]  = "VIDIOCSPLAYMODE",
311         [_IOC_NR(VIDIOCSWRITEMODE)] = "VIDIOCSWRITEMODE",
312         [_IOC_NR(VIDIOCGPLAYINFO)]  = "VIDIOCGPLAYINFO",
313         [_IOC_NR(VIDIOCSMICROCODE)] = "VIDIOCSMICROCODE",
314         [_IOC_NR(VIDIOCGVBIFMT)]    = "VIDIOCGVBIFMT",
315         [_IOC_NR(VIDIOCSVBIFMT)]    = "VIDIOCSVBIFMT"
316 };
317 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
318 #endif
319
320 static const char *v4l2_ioctls[] = {
321         [_IOC_NR(VIDIOC_QUERYCAP)]         = "VIDIOC_QUERYCAP",
322         [_IOC_NR(VIDIOC_RESERVED)]         = "VIDIOC_RESERVED",
323         [_IOC_NR(VIDIOC_ENUM_FMT)]         = "VIDIOC_ENUM_FMT",
324         [_IOC_NR(VIDIOC_G_FMT)]            = "VIDIOC_G_FMT",
325         [_IOC_NR(VIDIOC_S_FMT)]            = "VIDIOC_S_FMT",
326         [_IOC_NR(VIDIOC_G_MPEGCOMP)]       = "VIDIOC_G_MPEGCOMP",
327         [_IOC_NR(VIDIOC_S_MPEGCOMP)]       = "VIDIOC_S_MPEGCOMP",
328         [_IOC_NR(VIDIOC_REQBUFS)]          = "VIDIOC_REQBUFS",
329         [_IOC_NR(VIDIOC_QUERYBUF)]         = "VIDIOC_QUERYBUF",
330         [_IOC_NR(VIDIOC_G_FBUF)]           = "VIDIOC_G_FBUF",
331         [_IOC_NR(VIDIOC_S_FBUF)]           = "VIDIOC_S_FBUF",
332         [_IOC_NR(VIDIOC_OVERLAY)]          = "VIDIOC_OVERLAY",
333         [_IOC_NR(VIDIOC_QBUF)]             = "VIDIOC_QBUF",
334         [_IOC_NR(VIDIOC_DQBUF)]            = "VIDIOC_DQBUF",
335         [_IOC_NR(VIDIOC_STREAMON)]         = "VIDIOC_STREAMON",
336         [_IOC_NR(VIDIOC_STREAMOFF)]        = "VIDIOC_STREAMOFF",
337         [_IOC_NR(VIDIOC_G_PARM)]           = "VIDIOC_G_PARM",
338         [_IOC_NR(VIDIOC_S_PARM)]           = "VIDIOC_S_PARM",
339         [_IOC_NR(VIDIOC_G_STD)]            = "VIDIOC_G_STD",
340         [_IOC_NR(VIDIOC_S_STD)]            = "VIDIOC_S_STD",
341         [_IOC_NR(VIDIOC_ENUMSTD)]          = "VIDIOC_ENUMSTD",
342         [_IOC_NR(VIDIOC_ENUMINPUT)]        = "VIDIOC_ENUMINPUT",
343         [_IOC_NR(VIDIOC_G_CTRL)]           = "VIDIOC_G_CTRL",
344         [_IOC_NR(VIDIOC_S_CTRL)]           = "VIDIOC_S_CTRL",
345         [_IOC_NR(VIDIOC_G_TUNER)]          = "VIDIOC_G_TUNER",
346         [_IOC_NR(VIDIOC_S_TUNER)]          = "VIDIOC_S_TUNER",
347         [_IOC_NR(VIDIOC_G_AUDIO)]          = "VIDIOC_G_AUDIO",
348         [_IOC_NR(VIDIOC_S_AUDIO)]          = "VIDIOC_S_AUDIO",
349         [_IOC_NR(VIDIOC_QUERYCTRL)]        = "VIDIOC_QUERYCTRL",
350         [_IOC_NR(VIDIOC_QUERYMENU)]        = "VIDIOC_QUERYMENU",
351         [_IOC_NR(VIDIOC_G_INPUT)]          = "VIDIOC_G_INPUT",
352         [_IOC_NR(VIDIOC_S_INPUT)]          = "VIDIOC_S_INPUT",
353         [_IOC_NR(VIDIOC_G_OUTPUT)]         = "VIDIOC_G_OUTPUT",
354         [_IOC_NR(VIDIOC_S_OUTPUT)]         = "VIDIOC_S_OUTPUT",
355         [_IOC_NR(VIDIOC_ENUMOUTPUT)]       = "VIDIOC_ENUMOUTPUT",
356         [_IOC_NR(VIDIOC_G_AUDOUT)]         = "VIDIOC_G_AUDOUT",
357         [_IOC_NR(VIDIOC_S_AUDOUT)]         = "VIDIOC_S_AUDOUT",
358         [_IOC_NR(VIDIOC_G_MODULATOR)]      = "VIDIOC_G_MODULATOR",
359         [_IOC_NR(VIDIOC_S_MODULATOR)]      = "VIDIOC_S_MODULATOR",
360         [_IOC_NR(VIDIOC_G_FREQUENCY)]      = "VIDIOC_G_FREQUENCY",
361         [_IOC_NR(VIDIOC_S_FREQUENCY)]      = "VIDIOC_S_FREQUENCY",
362         [_IOC_NR(VIDIOC_CROPCAP)]          = "VIDIOC_CROPCAP",
363         [_IOC_NR(VIDIOC_G_CROP)]           = "VIDIOC_G_CROP",
364         [_IOC_NR(VIDIOC_S_CROP)]           = "VIDIOC_S_CROP",
365         [_IOC_NR(VIDIOC_G_JPEGCOMP)]       = "VIDIOC_G_JPEGCOMP",
366         [_IOC_NR(VIDIOC_S_JPEGCOMP)]       = "VIDIOC_S_JPEGCOMP",
367         [_IOC_NR(VIDIOC_QUERYSTD)]         = "VIDIOC_QUERYSTD",
368         [_IOC_NR(VIDIOC_TRY_FMT)]          = "VIDIOC_TRY_FMT",
369         [_IOC_NR(VIDIOC_ENUMAUDIO)]        = "VIDIOC_ENUMAUDIO",
370         [_IOC_NR(VIDIOC_ENUMAUDOUT)]       = "VIDIOC_ENUMAUDOUT",
371         [_IOC_NR(VIDIOC_G_PRIORITY)]       = "VIDIOC_G_PRIORITY",
372         [_IOC_NR(VIDIOC_S_PRIORITY)]       = "VIDIOC_S_PRIORITY",
373         [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
374         [_IOC_NR(VIDIOC_LOG_STATUS)]       = "VIDIOC_LOG_STATUS",
375         [_IOC_NR(VIDIOC_G_EXT_CTRLS)]      = "VIDIOC_G_EXT_CTRLS",
376         [_IOC_NR(VIDIOC_S_EXT_CTRLS)]      = "VIDIOC_S_EXT_CTRLS",
377         [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)]    = "VIDIOC_TRY_EXT_CTRLS",
378 #if 1
379         [_IOC_NR(VIDIOC_ENUM_FRAMESIZES)]  = "VIDIOC_ENUM_FRAMESIZES",
380         [_IOC_NR(VIDIOC_ENUM_FRAMEINTERVALS)] = "VIDIOC_ENUM_FRAMEINTERVALS",
381         [_IOC_NR(VIDIOC_G_ENC_INDEX)]      = "VIDIOC_G_ENC_INDEX",
382         [_IOC_NR(VIDIOC_ENCODER_CMD)]      = "VIDIOC_ENCODER_CMD",
383         [_IOC_NR(VIDIOC_TRY_ENCODER_CMD)]  = "VIDIOC_TRY_ENCODER_CMD",
384
385         [_IOC_NR(VIDIOC_DBG_S_REGISTER)]   = "VIDIOC_DBG_S_REGISTER",
386         [_IOC_NR(VIDIOC_DBG_G_REGISTER)]   = "VIDIOC_DBG_G_REGISTER",
387
388         [_IOC_NR(VIDIOC_G_CHIP_IDENT)]     = "VIDIOC_G_CHIP_IDENT",
389 #endif
390 };
391 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
392
393 static const char *v4l2_int_ioctls[] = {
394 #ifdef CONFIG_VIDEO_V4L1_COMPAT
395         [_IOC_NR(DECODER_GET_CAPABILITIES)]    = "DECODER_GET_CAPABILITIES",
396         [_IOC_NR(DECODER_GET_STATUS)]          = "DECODER_GET_STATUS",
397         [_IOC_NR(DECODER_SET_NORM)]            = "DECODER_SET_NORM",
398         [_IOC_NR(DECODER_SET_INPUT)]           = "DECODER_SET_INPUT",
399         [_IOC_NR(DECODER_SET_OUTPUT)]          = "DECODER_SET_OUTPUT",
400         [_IOC_NR(DECODER_ENABLE_OUTPUT)]       = "DECODER_ENABLE_OUTPUT",
401         [_IOC_NR(DECODER_SET_PICTURE)]         = "DECODER_SET_PICTURE",
402         [_IOC_NR(DECODER_SET_GPIO)]            = "DECODER_SET_GPIO",
403         [_IOC_NR(DECODER_INIT)]                = "DECODER_INIT",
404         [_IOC_NR(DECODER_SET_VBI_BYPASS)]      = "DECODER_SET_VBI_BYPASS",
405         [_IOC_NR(DECODER_DUMP)]                = "DECODER_DUMP",
406 #endif
407         [_IOC_NR(AUDC_SET_RADIO)]              = "AUDC_SET_RADIO",
408
409         [_IOC_NR(TUNER_SET_TYPE_ADDR)]         = "TUNER_SET_TYPE_ADDR",
410         [_IOC_NR(TUNER_SET_STANDBY)]           = "TUNER_SET_STANDBY",
411         [_IOC_NR(TDA9887_SET_CONFIG)]          = "TDA9887_SET_CONFIG",
412
413         [_IOC_NR(VIDIOC_INT_S_TUNER_MODE)]     = "VIDIOC_INT_S_TUNER_MODE",
414         [_IOC_NR(VIDIOC_INT_RESET)]            = "VIDIOC_INT_RESET",
415         [_IOC_NR(VIDIOC_INT_AUDIO_CLOCK_FREQ)] = "VIDIOC_INT_AUDIO_CLOCK_FREQ",
416         [_IOC_NR(VIDIOC_INT_DECODE_VBI_LINE)]  = "VIDIOC_INT_DECODE_VBI_LINE",
417         [_IOC_NR(VIDIOC_INT_S_VBI_DATA)]       = "VIDIOC_INT_S_VBI_DATA",
418         [_IOC_NR(VIDIOC_INT_G_VBI_DATA)]       = "VIDIOC_INT_G_VBI_DATA",
419         [_IOC_NR(VIDIOC_INT_I2S_CLOCK_FREQ)]   = "VIDIOC_INT_I2S_CLOCK_FREQ",
420         [_IOC_NR(VIDIOC_INT_S_STANDBY)]        = "VIDIOC_INT_S_STANDBY",
421         [_IOC_NR(VIDIOC_INT_S_AUDIO_ROUTING)]  = "VIDIOC_INT_S_AUDIO_ROUTING",
422         [_IOC_NR(VIDIOC_INT_G_AUDIO_ROUTING)]  = "VIDIOC_INT_G_AUDIO_ROUTING",
423         [_IOC_NR(VIDIOC_INT_S_VIDEO_ROUTING)]  = "VIDIOC_INT_S_VIDEO_ROUTING",
424         [_IOC_NR(VIDIOC_INT_G_VIDEO_ROUTING)]  = "VIDIOC_INT_G_VIDEO_ROUTING",
425         [_IOC_NR(VIDIOC_INT_S_CRYSTAL_FREQ)]   = "VIDIOC_INT_S_CRYSTAL_FREQ",
426         [_IOC_NR(VIDIOC_INT_INIT)]             = "VIDIOC_INT_INIT",
427         [_IOC_NR(VIDIOC_INT_G_STD_OUTPUT)]     = "VIDIOC_INT_G_STD_OUTPUT",
428         [_IOC_NR(VIDIOC_INT_S_STD_OUTPUT)]     = "VIDIOC_INT_S_STD_OUTPUT",
429 };
430 #define V4L2_INT_IOCTLS ARRAY_SIZE(v4l2_int_ioctls)
431
432
433 /* Common ioctl debug function. This function can be used by
434    external ioctl messages as well as internal V4L ioctl */
435 void v4l_printk_ioctl(unsigned int cmd)
436 {
437         char *dir;
438
439         switch (_IOC_DIR(cmd)) {
440         case _IOC_NONE:              dir = "--"; break;
441         case _IOC_READ:              dir = "r-"; break;
442         case _IOC_WRITE:             dir = "-w"; break;
443         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
444         default:                     dir = "*ERR*"; break;
445         }
446         switch (_IOC_TYPE(cmd)) {
447         case 'd':
448                 printk("v4l2_int ioctl %s, dir=%s (0x%08x)\n",
449                        (_IOC_NR(cmd) < V4L2_INT_IOCTLS) ?
450                        v4l2_int_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
451                 break;
452 #ifdef CONFIG_VIDEO_V4L1_COMPAT
453         case 'v':
454                 printk("v4l1 ioctl %s, dir=%s (0x%08x)\n",
455                        (_IOC_NR(cmd) < V4L1_IOCTLS) ?
456                        v4l1_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
457                 break;
458 #endif
459         case 'V':
460                 printk("v4l2 ioctl %s, dir=%s (0x%08x)\n",
461                        (_IOC_NR(cmd) < V4L2_IOCTLS) ?
462                        v4l2_ioctls[_IOC_NR(cmd)] : "UNKNOWN", dir, cmd);
463                 break;
464
465         default:
466                 printk("unknown ioctl '%c', dir=%s, #%d (0x%08x)\n",
467                        _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
468         }
469 }
470
471
472 /* ----------------------------------------------------------------- */
473
474 /* Helper functions for control handling                             */
475
476 /* Check for correctness of the ctrl's value based on the data from
477    struct v4l2_queryctrl and the available menu items. Note that
478    menu_items may be NULL, in that case it is ignored. */
479 int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
480                 const char **menu_items)
481 {
482         if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
483                 return -EINVAL;
484         if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
485                 return -EBUSY;
486         if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
487             qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
488             qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
489                 return 0;
490         if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
491                 return -ERANGE;
492         if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
493                 if (menu_items[ctrl->value] == NULL ||
494                     menu_items[ctrl->value][0] == '\0')
495                         return -EINVAL;
496         }
497         return 0;
498 }
499
500 /* Returns NULL or a character pointer array containing the menu for
501    the given control ID. The pointer array ends with a NULL pointer.
502    An empty string signifies a menu entry that is invalid. This allows
503    drivers to disable certain options if it is not supported. */
504 const char **v4l2_ctrl_get_menu(u32 id)
505 {
506         static const char *mpeg_audio_sampling_freq[] = {
507                 "44.1 kHz",
508                 "48 kHz",
509                 "32 kHz",
510                 NULL
511         };
512         static const char *mpeg_audio_encoding[] = {
513                 "Layer I",
514                 "Layer II",
515                 "Layer III",
516                 NULL
517         };
518         static const char *mpeg_audio_l1_bitrate[] = {
519                 "32 kbps",
520                 "64 kbps",
521                 "96 kbps",
522                 "128 kbps",
523                 "160 kbps",
524                 "192 kbps",
525                 "224 kbps",
526                 "256 kbps",
527                 "288 kbps",
528                 "320 kbps",
529                 "352 kbps",
530                 "384 kbps",
531                 "416 kbps",
532                 "448 kbps",
533                 NULL
534         };
535         static const char *mpeg_audio_l2_bitrate[] = {
536                 "32 kbps",
537                 "48 kbps",
538                 "56 kbps",
539                 "64 kbps",
540                 "80 kbps",
541                 "96 kbps",
542                 "112 kbps",
543                 "128 kbps",
544                 "160 kbps",
545                 "192 kbps",
546                 "224 kbps",
547                 "256 kbps",
548                 "320 kbps",
549                 "384 kbps",
550                 NULL
551         };
552         static const char *mpeg_audio_l3_bitrate[] = {
553                 "32 kbps",
554                 "40 kbps",
555                 "48 kbps",
556                 "56 kbps",
557                 "64 kbps",
558                 "80 kbps",
559                 "96 kbps",
560                 "112 kbps",
561                 "128 kbps",
562                 "160 kbps",
563                 "192 kbps",
564                 "224 kbps",
565                 "256 kbps",
566                 "320 kbps",
567                 NULL
568         };
569         static const char *mpeg_audio_mode[] = {
570                 "Stereo",
571                 "Joint Stereo",
572                 "Dual",
573                 "Mono",
574                 NULL
575         };
576         static const char *mpeg_audio_mode_extension[] = {
577                 "Bound 4",
578                 "Bound 8",
579                 "Bound 12",
580                 "Bound 16",
581                 NULL
582         };
583         static const char *mpeg_audio_emphasis[] = {
584                 "No Emphasis",
585                 "50/15 us",
586                 "CCITT J17",
587                 NULL
588         };
589         static const char *mpeg_audio_crc[] = {
590                 "No CRC",
591                 "16-bit CRC",
592                 NULL
593         };
594         static const char *mpeg_video_encoding[] = {
595                 "MPEG-1",
596                 "MPEG-2",
597                 NULL
598         };
599         static const char *mpeg_video_aspect[] = {
600                 "1x1",
601                 "4x3",
602                 "16x9",
603                 "2.21x1",
604                 NULL
605         };
606         static const char *mpeg_video_bitrate_mode[] = {
607                 "Variable Bitrate",
608                 "Constant Bitrate",
609                 NULL
610         };
611         static const char *mpeg_stream_type[] = {
612                 "MPEG-2 Program Stream",
613                 "MPEG-2 Transport Stream",
614                 "MPEG-1 System Stream",
615                 "MPEG-2 DVD-compatible Stream",
616                 "MPEG-1 VCD-compatible Stream",
617                 "MPEG-2 SVCD-compatible Stream",
618                 NULL
619         };
620         static const char *mpeg_stream_vbi_fmt[] = {
621                 "No VBI",
622                 "Private packet, IVTV format",
623                 NULL
624         };
625
626         switch (id) {
627                 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
628                         return mpeg_audio_sampling_freq;
629                 case V4L2_CID_MPEG_AUDIO_ENCODING:
630                         return mpeg_audio_encoding;
631                 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
632                         return mpeg_audio_l1_bitrate;
633                 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
634                         return mpeg_audio_l2_bitrate;
635                 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
636                         return mpeg_audio_l3_bitrate;
637                 case V4L2_CID_MPEG_AUDIO_MODE:
638                         return mpeg_audio_mode;
639                 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
640                         return mpeg_audio_mode_extension;
641                 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
642                         return mpeg_audio_emphasis;
643                 case V4L2_CID_MPEG_AUDIO_CRC:
644                         return mpeg_audio_crc;
645                 case V4L2_CID_MPEG_VIDEO_ENCODING:
646                         return mpeg_video_encoding;
647                 case V4L2_CID_MPEG_VIDEO_ASPECT:
648                         return mpeg_video_aspect;
649                 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
650                         return mpeg_video_bitrate_mode;
651                 case V4L2_CID_MPEG_STREAM_TYPE:
652                         return mpeg_stream_type;
653                 case V4L2_CID_MPEG_STREAM_VBI_FMT:
654                         return mpeg_stream_vbi_fmt;
655                 default:
656                         return NULL;
657         }
658 }
659
660 /* Fill in a struct v4l2_queryctrl */
661 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
662 {
663         const char *name;
664
665         qctrl->flags = 0;
666         switch (qctrl->id) {
667         /* USER controls */
668         case V4L2_CID_USER_CLASS:       name = "User Controls"; break;
669         case V4L2_CID_AUDIO_VOLUME:     name = "Volume"; break;
670         case V4L2_CID_AUDIO_MUTE:       name = "Mute"; break;
671         case V4L2_CID_AUDIO_BALANCE:    name = "Balance"; break;
672         case V4L2_CID_AUDIO_BASS:       name = "Bass"; break;
673         case V4L2_CID_AUDIO_TREBLE:     name = "Treble"; break;
674         case V4L2_CID_AUDIO_LOUDNESS:   name = "Loudness"; break;
675         case V4L2_CID_BRIGHTNESS:       name = "Brightness"; break;
676         case V4L2_CID_CONTRAST:         name = "Contrast"; break;
677         case V4L2_CID_SATURATION:       name = "Saturation"; break;
678         case V4L2_CID_HUE:              name = "Hue"; break;
679
680         /* MPEG controls */
681         case V4L2_CID_MPEG_CLASS:               name = "MPEG Encoder Controls"; break;
682         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: name = "Audio Sampling Frequency"; break;
683         case V4L2_CID_MPEG_AUDIO_ENCODING:      name = "Audio Encoding Layer"; break;
684         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    name = "Audio Layer I Bitrate"; break;
685         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    name = "Audio Layer II Bitrate"; break;
686         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    name = "Audio Layer III Bitrate"; break;
687         case V4L2_CID_MPEG_AUDIO_MODE:          name = "Audio Stereo Mode"; break;
688         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: name = "Audio Stereo Mode Extension"; break;
689         case V4L2_CID_MPEG_AUDIO_EMPHASIS:      name = "Audio Emphasis"; break;
690         case V4L2_CID_MPEG_AUDIO_CRC:           name = "Audio CRC"; break;
691         case V4L2_CID_MPEG_AUDIO_MUTE:          name = "Audio Mute"; break;
692         case V4L2_CID_MPEG_VIDEO_ENCODING:      name = "Video Encoding"; break;
693         case V4L2_CID_MPEG_VIDEO_ASPECT:        name = "Video Aspect"; break;
694         case V4L2_CID_MPEG_VIDEO_B_FRAMES:      name = "Video B Frames"; break;
695         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      name = "Video GOP Size"; break;
696         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   name = "Video GOP Closure"; break;
697         case V4L2_CID_MPEG_VIDEO_PULLDOWN:      name = "Video Pulldown"; break;
698         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  name = "Video Bitrate Mode"; break;
699         case V4L2_CID_MPEG_VIDEO_BITRATE:       name = "Video Bitrate"; break;
700         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  name = "Video Peak Bitrate"; break;
701         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: name = "Video Temporal Decimation"; break;
702         case V4L2_CID_MPEG_VIDEO_MUTE:          name = "Video Mute"; break;
703         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      name = "Video Mute YUV"; break;
704         case V4L2_CID_MPEG_STREAM_TYPE:         name = "Stream Type"; break;
705         case V4L2_CID_MPEG_STREAM_PID_PMT:      name = "Stream PMT Program ID"; break;
706         case V4L2_CID_MPEG_STREAM_PID_AUDIO:    name = "Stream Audio Program ID"; break;
707         case V4L2_CID_MPEG_STREAM_PID_VIDEO:    name = "Stream Video Program ID"; break;
708         case V4L2_CID_MPEG_STREAM_PID_PCR:      name = "Stream PCR Program ID"; break;
709         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: name = "Stream PES Audio ID"; break;
710         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: name = "Stream PES Video ID"; break;
711         case V4L2_CID_MPEG_STREAM_VBI_FMT:      name = "Stream VBI Format"; break;
712
713         default:
714                 return -EINVAL;
715         }
716         switch (qctrl->id) {
717         case V4L2_CID_AUDIO_MUTE:
718         case V4L2_CID_AUDIO_LOUDNESS:
719         case V4L2_CID_MPEG_AUDIO_MUTE:
720         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
721         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
722                 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
723                 min = 0;
724                 max = step = 1;
725                 break;
726         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
727         case V4L2_CID_MPEG_AUDIO_ENCODING:
728         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
729         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
730         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
731         case V4L2_CID_MPEG_AUDIO_MODE:
732         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
733         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
734         case V4L2_CID_MPEG_AUDIO_CRC:
735         case V4L2_CID_MPEG_VIDEO_ENCODING:
736         case V4L2_CID_MPEG_VIDEO_ASPECT:
737         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
738         case V4L2_CID_MPEG_STREAM_TYPE:
739         case V4L2_CID_MPEG_STREAM_VBI_FMT:
740                 qctrl->type = V4L2_CTRL_TYPE_MENU;
741                 step = 1;
742                 break;
743         case V4L2_CID_USER_CLASS:
744         case V4L2_CID_MPEG_CLASS:
745                 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
746                 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
747                 min = max = step = def = 0;
748                 break;
749         default:
750                 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
751                 break;
752         }
753         switch (qctrl->id) {
754         case V4L2_CID_MPEG_AUDIO_ENCODING:
755         case V4L2_CID_MPEG_AUDIO_MODE:
756         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
757         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
758         case V4L2_CID_MPEG_STREAM_TYPE:
759                 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
760                 break;
761         case V4L2_CID_AUDIO_VOLUME:
762         case V4L2_CID_AUDIO_BALANCE:
763         case V4L2_CID_AUDIO_BASS:
764         case V4L2_CID_AUDIO_TREBLE:
765         case V4L2_CID_BRIGHTNESS:
766         case V4L2_CID_CONTRAST:
767         case V4L2_CID_SATURATION:
768         case V4L2_CID_HUE:
769                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
770                 break;
771         }
772         qctrl->minimum = min;
773         qctrl->maximum = max;
774         qctrl->step = step;
775         qctrl->default_value = def;
776         qctrl->reserved[0] = qctrl->reserved[1] = 0;
777         snprintf(qctrl->name, sizeof(qctrl->name), name);
778         return 0;
779 }
780
781 /* Fill in a struct v4l2_queryctrl with standard values based on
782    the control ID. */
783 int v4l2_ctrl_query_fill_std(struct v4l2_queryctrl *qctrl)
784 {
785         switch (qctrl->id) {
786         /* USER controls */
787         case V4L2_CID_USER_CLASS:
788         case V4L2_CID_MPEG_CLASS:
789                 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
790         case V4L2_CID_AUDIO_VOLUME:
791                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 58880);
792         case V4L2_CID_AUDIO_MUTE:
793         case V4L2_CID_AUDIO_LOUDNESS:
794                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
795         case V4L2_CID_AUDIO_BALANCE:
796         case V4L2_CID_AUDIO_BASS:
797         case V4L2_CID_AUDIO_TREBLE:
798                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 32768);
799         case V4L2_CID_BRIGHTNESS:
800                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
801         case V4L2_CID_CONTRAST:
802         case V4L2_CID_SATURATION:
803                 return v4l2_ctrl_query_fill(qctrl, 0, 127, 1, 64);
804         case V4L2_CID_HUE:
805                 return v4l2_ctrl_query_fill(qctrl, -128, 127, 1, 0);
806
807         /* MPEG controls */
808         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
809                 return v4l2_ctrl_query_fill(qctrl,
810                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100,
811                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000, 1,
812                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
813         case V4L2_CID_MPEG_AUDIO_ENCODING:
814                 return v4l2_ctrl_query_fill(qctrl,
815                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_1,
816                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_3, 1,
817                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
818         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
819                 return v4l2_ctrl_query_fill(qctrl,
820                                 V4L2_MPEG_AUDIO_L1_BITRATE_32K,
821                                 V4L2_MPEG_AUDIO_L1_BITRATE_448K, 1,
822                                 V4L2_MPEG_AUDIO_L1_BITRATE_256K);
823         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
824                 return v4l2_ctrl_query_fill(qctrl,
825                                 V4L2_MPEG_AUDIO_L2_BITRATE_32K,
826                                 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
827                                 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
828         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
829                 return v4l2_ctrl_query_fill(qctrl,
830                                 V4L2_MPEG_AUDIO_L3_BITRATE_32K,
831                                 V4L2_MPEG_AUDIO_L3_BITRATE_320K, 1,
832                                 V4L2_MPEG_AUDIO_L3_BITRATE_192K);
833         case V4L2_CID_MPEG_AUDIO_MODE:
834                 return v4l2_ctrl_query_fill(qctrl,
835                                 V4L2_MPEG_AUDIO_MODE_STEREO,
836                                 V4L2_MPEG_AUDIO_MODE_MONO, 1,
837                                 V4L2_MPEG_AUDIO_MODE_STEREO);
838         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
839                 return v4l2_ctrl_query_fill(qctrl,
840                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
841                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16, 1,
842                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4);
843         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
844                 return v4l2_ctrl_query_fill(qctrl,
845                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE,
846                                 V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17, 1,
847                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE);
848         case V4L2_CID_MPEG_AUDIO_CRC:
849                 return v4l2_ctrl_query_fill(qctrl,
850                                 V4L2_MPEG_AUDIO_CRC_NONE,
851                                 V4L2_MPEG_AUDIO_CRC_CRC16, 1,
852                                 V4L2_MPEG_AUDIO_CRC_NONE);
853         case V4L2_CID_MPEG_AUDIO_MUTE:
854                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
855         case V4L2_CID_MPEG_VIDEO_ENCODING:
856                 return v4l2_ctrl_query_fill(qctrl,
857                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
858                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
859                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
860         case V4L2_CID_MPEG_VIDEO_ASPECT:
861                 return v4l2_ctrl_query_fill(qctrl,
862                                 V4L2_MPEG_VIDEO_ASPECT_1x1,
863                                 V4L2_MPEG_VIDEO_ASPECT_221x100, 1,
864                                 V4L2_MPEG_VIDEO_ASPECT_4x3);
865         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
866                 return v4l2_ctrl_query_fill(qctrl, 0, 33, 1, 2);
867         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
868                 return v4l2_ctrl_query_fill(qctrl, 1, 34, 1, 12);
869         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
870                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
871         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
872                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
873         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
874                 return v4l2_ctrl_query_fill(qctrl,
875                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
876                                 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
877                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
878         case V4L2_CID_MPEG_VIDEO_BITRATE:
879                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
880         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
881                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
882         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
883                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
884         case V4L2_CID_MPEG_VIDEO_MUTE:
885                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
886         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:  /* Init YUV (really YCbCr) to black */
887                 return v4l2_ctrl_query_fill(qctrl, 0, 0xffffff, 1, 0x008080);
888         case V4L2_CID_MPEG_STREAM_TYPE:
889                 return v4l2_ctrl_query_fill(qctrl,
890                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
891                                 V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD, 1,
892                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
893         case V4L2_CID_MPEG_STREAM_PID_PMT:
894                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
895         case V4L2_CID_MPEG_STREAM_PID_AUDIO:
896                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
897         case V4L2_CID_MPEG_STREAM_PID_VIDEO:
898                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
899         case V4L2_CID_MPEG_STREAM_PID_PCR:
900                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
901         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO:
902                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
903         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO:
904                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
905         case V4L2_CID_MPEG_STREAM_VBI_FMT:
906                 return v4l2_ctrl_query_fill(qctrl,
907                                 V4L2_MPEG_STREAM_VBI_FMT_NONE,
908                                 V4L2_MPEG_STREAM_VBI_FMT_IVTV, 1,
909                                 V4L2_MPEG_STREAM_VBI_FMT_NONE);
910         default:
911                 return -EINVAL;
912         }
913 }
914
915 /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
916    the menu. The qctrl pointer may be NULL, in which case it is ignored. */
917 int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
918                const char **menu_items)
919 {
920         int i;
921
922         if (menu_items == NULL ||
923             (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
924                 return -EINVAL;
925         for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
926         if (menu_items[i] == NULL || menu_items[i][0] == '\0')
927                 return -EINVAL;
928         snprintf(qmenu->name, sizeof(qmenu->name), menu_items[qmenu->index]);
929         qmenu->reserved = 0;
930         return 0;
931 }
932
933 /* ctrl_classes points to an array of u32 pointers, the last element is
934    a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
935    Each array must be sorted low to high and belong to the same control
936    class. The array of u32 pointer must also be sorted, from low class IDs
937    to high class IDs.
938
939    This function returns the first ID that follows after the given ID.
940    When no more controls are available 0 is returned. */
941 u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
942 {
943         u32 ctrl_class;
944         const u32 *pctrl;
945
946         /* if no query is desired, then just return the control ID */
947         if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0)
948                 return id;
949         if (ctrl_classes == NULL)
950                 return 0;
951         id &= V4L2_CTRL_ID_MASK;
952         ctrl_class = V4L2_CTRL_ID2CLASS(id);
953         id++;   /* select next control */
954         /* find first class that matches (or is greater than) the class of
955            the ID */
956         while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
957                 ctrl_classes++;
958         /* no more classes */
959         if (*ctrl_classes == NULL)
960                 return 0;
961         pctrl = *ctrl_classes;
962         /* find first ctrl within the class that is >= ID */
963         while (*pctrl && *pctrl < id) pctrl++;
964         if (*pctrl)
965                 return *pctrl;
966         /* we are at the end of the controls of the current class. */
967         /* continue with next class if available */
968         ctrl_classes++;
969         if (*ctrl_classes == NULL)
970                 return 0;
971         return **ctrl_classes;
972 }
973
974 int v4l2_chip_match_i2c_client(struct i2c_client *c, u32 match_type, u32 match_chip)
975 {
976         switch (match_type) {
977         case V4L2_CHIP_MATCH_I2C_DRIVER:
978                 return (c != NULL && c->driver != NULL && c->driver->id == match_chip);
979         case V4L2_CHIP_MATCH_I2C_ADDR:
980                 return (c != NULL && c->addr == match_chip);
981         default:
982                 return 0;
983         }
984 }
985
986 int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_chip_ident *chip,
987                 u32 ident, u32 revision)
988 {
989         if (!v4l2_chip_match_i2c_client(c, chip->match_type, chip->match_chip))
990                 return 0;
991         if (chip->ident == V4L2_IDENT_NONE) {
992                 chip->ident = ident;
993                 chip->revision = revision;
994         }
995         else {
996                 chip->ident = V4L2_IDENT_AMBIGUOUS;
997                 chip->revision = 0;
998         }
999         return 0;
1000 }
1001
1002 int v4l2_chip_match_host(u32 match_type, u32 match_chip)
1003 {
1004         switch (match_type) {
1005         case V4L2_CHIP_MATCH_HOST:
1006                 return match_chip == 0;
1007         default:
1008                 return 0;
1009         }
1010 }
1011
1012 /* ----------------------------------------------------------------- */
1013
1014 EXPORT_SYMBOL(v4l2_norm_to_name);
1015 EXPORT_SYMBOL(v4l2_video_std_construct);
1016
1017 EXPORT_SYMBOL(v4l2_prio_init);
1018 EXPORT_SYMBOL(v4l2_prio_change);
1019 EXPORT_SYMBOL(v4l2_prio_open);
1020 EXPORT_SYMBOL(v4l2_prio_close);
1021 EXPORT_SYMBOL(v4l2_prio_max);
1022 EXPORT_SYMBOL(v4l2_prio_check);
1023
1024 EXPORT_SYMBOL(v4l2_field_names);
1025 EXPORT_SYMBOL(v4l2_type_names);
1026 EXPORT_SYMBOL(v4l_printk_ioctl);
1027
1028 EXPORT_SYMBOL(v4l2_ctrl_next);
1029 EXPORT_SYMBOL(v4l2_ctrl_check);
1030 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
1031 EXPORT_SYMBOL(v4l2_ctrl_query_menu);
1032 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
1033 EXPORT_SYMBOL(v4l2_ctrl_query_fill_std);
1034
1035 EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
1036 EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
1037 EXPORT_SYMBOL(v4l2_chip_match_host);
1038
1039 /*
1040  * Local variables:
1041  * c-basic-offset: 8
1042  * End:
1043  */