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