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