V4L/DVB (8634): v4l2: extend MPEG Encoding API with AVC and AAC
[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 #define __OLD_VIDIOC_ /* To allow fixing old calls*/
60 #include <media/v4l2-common.h>
61 #include <media/v4l2-chip-ident.h>
62
63 #ifdef CONFIG_KMOD
64 #include <linux/kmod.h>
65 #endif
66
67 #include <linux/videodev2.h>
68
69 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
70 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
71 MODULE_LICENSE("GPL");
72
73 /*
74  *
75  *      V 4 L 2   D R I V E R   H E L P E R   A P I
76  *
77  */
78
79 /*
80  *  Video Standard Operations (contributed by Michael Schimek)
81  */
82
83
84 /* ----------------------------------------------------------------- */
85 /* priority handling                                                 */
86
87 #define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND   || \
88                               val == V4L2_PRIORITY_INTERACTIVE  || \
89                               val == V4L2_PRIORITY_RECORD)
90
91 int v4l2_prio_init(struct v4l2_prio_state *global)
92 {
93         memset(global,0,sizeof(*global));
94         return 0;
95 }
96 EXPORT_SYMBOL(v4l2_prio_init);
97
98 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
99                      enum v4l2_priority new)
100 {
101         if (!V4L2_PRIO_VALID(new))
102                 return -EINVAL;
103         if (*local == new)
104                 return 0;
105
106         atomic_inc(&global->prios[new]);
107         if (V4L2_PRIO_VALID(*local))
108                 atomic_dec(&global->prios[*local]);
109         *local = new;
110         return 0;
111 }
112 EXPORT_SYMBOL(v4l2_prio_change);
113
114 int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
115 {
116         return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
117 }
118 EXPORT_SYMBOL(v4l2_prio_open);
119
120 int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
121 {
122         if (V4L2_PRIO_VALID(*local))
123                 atomic_dec(&global->prios[*local]);
124         return 0;
125 }
126 EXPORT_SYMBOL(v4l2_prio_close);
127
128 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
129 {
130         if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
131                 return V4L2_PRIORITY_RECORD;
132         if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
133                 return V4L2_PRIORITY_INTERACTIVE;
134         if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
135                 return V4L2_PRIORITY_BACKGROUND;
136         return V4L2_PRIORITY_UNSET;
137 }
138 EXPORT_SYMBOL(v4l2_prio_max);
139
140 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
141 {
142         if (*local < v4l2_prio_max(global))
143                 return -EBUSY;
144         return 0;
145 }
146 EXPORT_SYMBOL(v4l2_prio_check);
147
148 /* ----------------------------------------------------------------- */
149
150 /* Helper functions for control handling                             */
151
152 /* Check for correctness of the ctrl's value based on the data from
153    struct v4l2_queryctrl and the available menu items. Note that
154    menu_items may be NULL, in that case it is ignored. */
155 int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
156                 const char **menu_items)
157 {
158         if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
159                 return -EINVAL;
160         if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
161                 return -EBUSY;
162         if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
163             qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
164             qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
165                 return 0;
166         if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
167                 return -ERANGE;
168         if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
169                 if (menu_items[ctrl->value] == NULL ||
170                     menu_items[ctrl->value][0] == '\0')
171                         return -EINVAL;
172         }
173         return 0;
174 }
175 EXPORT_SYMBOL(v4l2_ctrl_check);
176
177 /* Returns NULL or a character pointer array containing the menu for
178    the given control ID. The pointer array ends with a NULL pointer.
179    An empty string signifies a menu entry that is invalid. This allows
180    drivers to disable certain options if it is not supported. */
181 const char **v4l2_ctrl_get_menu(u32 id)
182 {
183         static const char *mpeg_audio_sampling_freq[] = {
184                 "44.1 kHz",
185                 "48 kHz",
186                 "32 kHz",
187                 NULL
188         };
189         static const char *mpeg_audio_encoding[] = {
190                 "MPEG-1 Layer I",
191                 "MPEG-1 Layer II",
192                 "MPEG-1 Layer III",
193                 "MPEG-4 AAC",
194                 NULL
195         };
196         static const char *mpeg_audio_l1_bitrate[] = {
197                 "32 kbps",
198                 "64 kbps",
199                 "96 kbps",
200                 "128 kbps",
201                 "160 kbps",
202                 "192 kbps",
203                 "224 kbps",
204                 "256 kbps",
205                 "288 kbps",
206                 "320 kbps",
207                 "352 kbps",
208                 "384 kbps",
209                 "416 kbps",
210                 "448 kbps",
211                 NULL
212         };
213         static const char *mpeg_audio_l2_bitrate[] = {
214                 "32 kbps",
215                 "48 kbps",
216                 "56 kbps",
217                 "64 kbps",
218                 "80 kbps",
219                 "96 kbps",
220                 "112 kbps",
221                 "128 kbps",
222                 "160 kbps",
223                 "192 kbps",
224                 "224 kbps",
225                 "256 kbps",
226                 "320 kbps",
227                 "384 kbps",
228                 NULL
229         };
230         static const char *mpeg_audio_l3_bitrate[] = {
231                 "32 kbps",
232                 "40 kbps",
233                 "48 kbps",
234                 "56 kbps",
235                 "64 kbps",
236                 "80 kbps",
237                 "96 kbps",
238                 "112 kbps",
239                 "128 kbps",
240                 "160 kbps",
241                 "192 kbps",
242                 "224 kbps",
243                 "256 kbps",
244                 "320 kbps",
245                 NULL
246         };
247         static const char *mpeg_audio_mode[] = {
248                 "Stereo",
249                 "Joint Stereo",
250                 "Dual",
251                 "Mono",
252                 NULL
253         };
254         static const char *mpeg_audio_mode_extension[] = {
255                 "Bound 4",
256                 "Bound 8",
257                 "Bound 12",
258                 "Bound 16",
259                 NULL
260         };
261         static const char *mpeg_audio_emphasis[] = {
262                 "No Emphasis",
263                 "50/15 us",
264                 "CCITT J17",
265                 NULL
266         };
267         static const char *mpeg_audio_crc[] = {
268                 "No CRC",
269                 "16-bit CRC",
270                 NULL
271         };
272         static const char *mpeg_video_encoding[] = {
273                 "MPEG-1",
274                 "MPEG-2",
275                 "MPEG-4 AVC",
276                 NULL
277         };
278         static const char *mpeg_video_aspect[] = {
279                 "1x1",
280                 "4x3",
281                 "16x9",
282                 "2.21x1",
283                 NULL
284         };
285         static const char *mpeg_video_bitrate_mode[] = {
286                 "Variable Bitrate",
287                 "Constant Bitrate",
288                 NULL
289         };
290         static const char *mpeg_stream_type[] = {
291                 "MPEG-2 Program Stream",
292                 "MPEG-2 Transport Stream",
293                 "MPEG-1 System Stream",
294                 "MPEG-2 DVD-compatible Stream",
295                 "MPEG-1 VCD-compatible Stream",
296                 "MPEG-2 SVCD-compatible Stream",
297                 NULL
298         };
299         static const char *mpeg_stream_vbi_fmt[] = {
300                 "No VBI",
301                 "Private packet, IVTV format",
302                 NULL
303         };
304
305         switch (id) {
306                 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
307                         return mpeg_audio_sampling_freq;
308                 case V4L2_CID_MPEG_AUDIO_ENCODING:
309                         return mpeg_audio_encoding;
310                 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
311                         return mpeg_audio_l1_bitrate;
312                 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
313                         return mpeg_audio_l2_bitrate;
314                 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
315                         return mpeg_audio_l3_bitrate;
316                 case V4L2_CID_MPEG_AUDIO_MODE:
317                         return mpeg_audio_mode;
318                 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
319                         return mpeg_audio_mode_extension;
320                 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
321                         return mpeg_audio_emphasis;
322                 case V4L2_CID_MPEG_AUDIO_CRC:
323                         return mpeg_audio_crc;
324                 case V4L2_CID_MPEG_VIDEO_ENCODING:
325                         return mpeg_video_encoding;
326                 case V4L2_CID_MPEG_VIDEO_ASPECT:
327                         return mpeg_video_aspect;
328                 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
329                         return mpeg_video_bitrate_mode;
330                 case V4L2_CID_MPEG_STREAM_TYPE:
331                         return mpeg_stream_type;
332                 case V4L2_CID_MPEG_STREAM_VBI_FMT:
333                         return mpeg_stream_vbi_fmt;
334                 default:
335                         return NULL;
336         }
337 }
338 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
339
340 /* Fill in a struct v4l2_queryctrl */
341 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
342 {
343         const char *name;
344
345         qctrl->flags = 0;
346         switch (qctrl->id) {
347         /* USER controls */
348         case V4L2_CID_USER_CLASS:       name = "User Controls"; break;
349         case V4L2_CID_AUDIO_VOLUME:     name = "Volume"; break;
350         case V4L2_CID_AUDIO_MUTE:       name = "Mute"; break;
351         case V4L2_CID_AUDIO_BALANCE:    name = "Balance"; break;
352         case V4L2_CID_AUDIO_BASS:       name = "Bass"; break;
353         case V4L2_CID_AUDIO_TREBLE:     name = "Treble"; break;
354         case V4L2_CID_AUDIO_LOUDNESS:   name = "Loudness"; break;
355         case V4L2_CID_BRIGHTNESS:       name = "Brightness"; break;
356         case V4L2_CID_CONTRAST:         name = "Contrast"; break;
357         case V4L2_CID_SATURATION:       name = "Saturation"; break;
358         case V4L2_CID_HUE:              name = "Hue"; break;
359
360         /* MPEG controls */
361         case V4L2_CID_MPEG_CLASS:               name = "MPEG Encoder Controls"; break;
362         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: name = "Audio Sampling Frequency"; break;
363         case V4L2_CID_MPEG_AUDIO_ENCODING:      name = "Audio Encoding"; break;
364         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    name = "Audio Layer I Bitrate"; break;
365         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    name = "Audio Layer II Bitrate"; break;
366         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    name = "Audio Layer III Bitrate"; break;
367         case V4L2_CID_MPEG_AUDIO_MODE:          name = "Audio Stereo Mode"; break;
368         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: name = "Audio Stereo Mode Extension"; break;
369         case V4L2_CID_MPEG_AUDIO_EMPHASIS:      name = "Audio Emphasis"; break;
370         case V4L2_CID_MPEG_AUDIO_CRC:           name = "Audio CRC"; break;
371         case V4L2_CID_MPEG_AUDIO_MUTE:          name = "Audio Mute"; break;
372         case V4L2_CID_MPEG_VIDEO_ENCODING:      name = "Video Encoding"; break;
373         case V4L2_CID_MPEG_VIDEO_ASPECT:        name = "Video Aspect"; break;
374         case V4L2_CID_MPEG_VIDEO_B_FRAMES:      name = "Video B Frames"; break;
375         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      name = "Video GOP Size"; break;
376         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   name = "Video GOP Closure"; break;
377         case V4L2_CID_MPEG_VIDEO_PULLDOWN:      name = "Video Pulldown"; break;
378         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  name = "Video Bitrate Mode"; break;
379         case V4L2_CID_MPEG_VIDEO_BITRATE:       name = "Video Bitrate"; break;
380         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  name = "Video Peak Bitrate"; break;
381         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: name = "Video Temporal Decimation"; break;
382         case V4L2_CID_MPEG_VIDEO_MUTE:          name = "Video Mute"; break;
383         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      name = "Video Mute YUV"; break;
384         case V4L2_CID_MPEG_STREAM_TYPE:         name = "Stream Type"; break;
385         case V4L2_CID_MPEG_STREAM_PID_PMT:      name = "Stream PMT Program ID"; break;
386         case V4L2_CID_MPEG_STREAM_PID_AUDIO:    name = "Stream Audio Program ID"; break;
387         case V4L2_CID_MPEG_STREAM_PID_VIDEO:    name = "Stream Video Program ID"; break;
388         case V4L2_CID_MPEG_STREAM_PID_PCR:      name = "Stream PCR Program ID"; break;
389         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: name = "Stream PES Audio ID"; break;
390         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: name = "Stream PES Video ID"; break;
391         case V4L2_CID_MPEG_STREAM_VBI_FMT:      name = "Stream VBI Format"; break;
392
393         default:
394                 return -EINVAL;
395         }
396         switch (qctrl->id) {
397         case V4L2_CID_AUDIO_MUTE:
398         case V4L2_CID_AUDIO_LOUDNESS:
399         case V4L2_CID_MPEG_AUDIO_MUTE:
400         case V4L2_CID_MPEG_VIDEO_MUTE:
401         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
402         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
403                 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
404                 min = 0;
405                 max = step = 1;
406                 break;
407         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
408         case V4L2_CID_MPEG_AUDIO_ENCODING:
409         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
410         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
411         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
412         case V4L2_CID_MPEG_AUDIO_MODE:
413         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
414         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
415         case V4L2_CID_MPEG_AUDIO_CRC:
416         case V4L2_CID_MPEG_VIDEO_ENCODING:
417         case V4L2_CID_MPEG_VIDEO_ASPECT:
418         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
419         case V4L2_CID_MPEG_STREAM_TYPE:
420         case V4L2_CID_MPEG_STREAM_VBI_FMT:
421                 qctrl->type = V4L2_CTRL_TYPE_MENU;
422                 step = 1;
423                 break;
424         case V4L2_CID_USER_CLASS:
425         case V4L2_CID_MPEG_CLASS:
426                 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
427                 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
428                 min = max = step = def = 0;
429                 break;
430         default:
431                 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
432                 break;
433         }
434         switch (qctrl->id) {
435         case V4L2_CID_MPEG_AUDIO_ENCODING:
436         case V4L2_CID_MPEG_AUDIO_MODE:
437         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
438         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
439         case V4L2_CID_MPEG_STREAM_TYPE:
440                 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
441                 break;
442         case V4L2_CID_AUDIO_VOLUME:
443         case V4L2_CID_AUDIO_BALANCE:
444         case V4L2_CID_AUDIO_BASS:
445         case V4L2_CID_AUDIO_TREBLE:
446         case V4L2_CID_BRIGHTNESS:
447         case V4L2_CID_CONTRAST:
448         case V4L2_CID_SATURATION:
449         case V4L2_CID_HUE:
450                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
451                 break;
452         }
453         qctrl->minimum = min;
454         qctrl->maximum = max;
455         qctrl->step = step;
456         qctrl->default_value = def;
457         qctrl->reserved[0] = qctrl->reserved[1] = 0;
458         snprintf(qctrl->name, sizeof(qctrl->name), name);
459         return 0;
460 }
461 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
462
463 /* Fill in a struct v4l2_queryctrl with standard values based on
464    the control ID. */
465 int v4l2_ctrl_query_fill_std(struct v4l2_queryctrl *qctrl)
466 {
467         switch (qctrl->id) {
468         /* USER controls */
469         case V4L2_CID_USER_CLASS:
470         case V4L2_CID_MPEG_CLASS:
471                 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
472         case V4L2_CID_AUDIO_VOLUME:
473                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 58880);
474         case V4L2_CID_AUDIO_MUTE:
475         case V4L2_CID_AUDIO_LOUDNESS:
476                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
477         case V4L2_CID_AUDIO_BALANCE:
478         case V4L2_CID_AUDIO_BASS:
479         case V4L2_CID_AUDIO_TREBLE:
480                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 32768);
481         case V4L2_CID_BRIGHTNESS:
482                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
483         case V4L2_CID_CONTRAST:
484         case V4L2_CID_SATURATION:
485                 return v4l2_ctrl_query_fill(qctrl, 0, 127, 1, 64);
486         case V4L2_CID_HUE:
487                 return v4l2_ctrl_query_fill(qctrl, -128, 127, 1, 0);
488
489         /* MPEG controls */
490         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
491                 return v4l2_ctrl_query_fill(qctrl,
492                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100,
493                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000, 1,
494                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
495         case V4L2_CID_MPEG_AUDIO_ENCODING:
496                 return v4l2_ctrl_query_fill(qctrl,
497                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_1,
498                                 V4L2_MPEG_AUDIO_ENCODING_AAC, 1,
499                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
500         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
501                 return v4l2_ctrl_query_fill(qctrl,
502                                 V4L2_MPEG_AUDIO_L1_BITRATE_32K,
503                                 V4L2_MPEG_AUDIO_L1_BITRATE_448K, 1,
504                                 V4L2_MPEG_AUDIO_L1_BITRATE_256K);
505         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
506                 return v4l2_ctrl_query_fill(qctrl,
507                                 V4L2_MPEG_AUDIO_L2_BITRATE_32K,
508                                 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
509                                 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
510         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
511                 return v4l2_ctrl_query_fill(qctrl,
512                                 V4L2_MPEG_AUDIO_L3_BITRATE_32K,
513                                 V4L2_MPEG_AUDIO_L3_BITRATE_320K, 1,
514                                 V4L2_MPEG_AUDIO_L3_BITRATE_192K);
515         case V4L2_CID_MPEG_AUDIO_MODE:
516                 return v4l2_ctrl_query_fill(qctrl,
517                                 V4L2_MPEG_AUDIO_MODE_STEREO,
518                                 V4L2_MPEG_AUDIO_MODE_MONO, 1,
519                                 V4L2_MPEG_AUDIO_MODE_STEREO);
520         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
521                 return v4l2_ctrl_query_fill(qctrl,
522                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
523                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16, 1,
524                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4);
525         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
526                 return v4l2_ctrl_query_fill(qctrl,
527                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE,
528                                 V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17, 1,
529                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE);
530         case V4L2_CID_MPEG_AUDIO_CRC:
531                 return v4l2_ctrl_query_fill(qctrl,
532                                 V4L2_MPEG_AUDIO_CRC_NONE,
533                                 V4L2_MPEG_AUDIO_CRC_CRC16, 1,
534                                 V4L2_MPEG_AUDIO_CRC_NONE);
535         case V4L2_CID_MPEG_AUDIO_MUTE:
536                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
537         case V4L2_CID_MPEG_VIDEO_ENCODING:
538                 return v4l2_ctrl_query_fill(qctrl,
539                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
540                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
541                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
542         case V4L2_CID_MPEG_VIDEO_ASPECT:
543                 return v4l2_ctrl_query_fill(qctrl,
544                                 V4L2_MPEG_VIDEO_ASPECT_1x1,
545                                 V4L2_MPEG_VIDEO_ASPECT_221x100, 1,
546                                 V4L2_MPEG_VIDEO_ASPECT_4x3);
547         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
548                 return v4l2_ctrl_query_fill(qctrl, 0, 33, 1, 2);
549         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
550                 return v4l2_ctrl_query_fill(qctrl, 1, 34, 1, 12);
551         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
552                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
553         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
554                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
555         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
556                 return v4l2_ctrl_query_fill(qctrl,
557                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
558                                 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
559                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
560         case V4L2_CID_MPEG_VIDEO_BITRATE:
561                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
562         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
563                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
564         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
565                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
566         case V4L2_CID_MPEG_VIDEO_MUTE:
567                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
568         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:  /* Init YUV (really YCbCr) to black */
569                 return v4l2_ctrl_query_fill(qctrl, 0, 0xffffff, 1, 0x008080);
570         case V4L2_CID_MPEG_STREAM_TYPE:
571                 return v4l2_ctrl_query_fill(qctrl,
572                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
573                                 V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD, 1,
574                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
575         case V4L2_CID_MPEG_STREAM_PID_PMT:
576                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
577         case V4L2_CID_MPEG_STREAM_PID_AUDIO:
578                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
579         case V4L2_CID_MPEG_STREAM_PID_VIDEO:
580                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
581         case V4L2_CID_MPEG_STREAM_PID_PCR:
582                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
583         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO:
584                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
585         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO:
586                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
587         case V4L2_CID_MPEG_STREAM_VBI_FMT:
588                 return v4l2_ctrl_query_fill(qctrl,
589                                 V4L2_MPEG_STREAM_VBI_FMT_NONE,
590                                 V4L2_MPEG_STREAM_VBI_FMT_IVTV, 1,
591                                 V4L2_MPEG_STREAM_VBI_FMT_NONE);
592         default:
593                 return -EINVAL;
594         }
595 }
596 EXPORT_SYMBOL(v4l2_ctrl_query_fill_std);
597
598 /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
599    the menu. The qctrl pointer may be NULL, in which case it is ignored. */
600 int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
601                const char **menu_items)
602 {
603         int i;
604
605         if (menu_items == NULL ||
606             (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
607                 return -EINVAL;
608         for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
609         if (menu_items[i] == NULL || menu_items[i][0] == '\0')
610                 return -EINVAL;
611         snprintf(qmenu->name, sizeof(qmenu->name), menu_items[qmenu->index]);
612         qmenu->reserved = 0;
613         return 0;
614 }
615 EXPORT_SYMBOL(v4l2_ctrl_query_menu);
616
617 /* ctrl_classes points to an array of u32 pointers, the last element is
618    a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
619    Each array must be sorted low to high and belong to the same control
620    class. The array of u32 pointer must also be sorted, from low class IDs
621    to high class IDs.
622
623    This function returns the first ID that follows after the given ID.
624    When no more controls are available 0 is returned. */
625 u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
626 {
627         u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
628         const u32 *pctrl;
629
630         if (ctrl_classes == NULL)
631                 return 0;
632
633         /* if no query is desired, then check if the ID is part of ctrl_classes */
634         if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
635                 /* find class */
636                 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
637                         ctrl_classes++;
638                 if (*ctrl_classes == NULL)
639                         return 0;
640                 pctrl = *ctrl_classes;
641                 /* find control ID */
642                 while (*pctrl && *pctrl != id) pctrl++;
643                 return *pctrl ? id : 0;
644         }
645         id &= V4L2_CTRL_ID_MASK;
646         id++;   /* select next control */
647         /* find first class that matches (or is greater than) the class of
648            the ID */
649         while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
650                 ctrl_classes++;
651         /* no more classes */
652         if (*ctrl_classes == NULL)
653                 return 0;
654         pctrl = *ctrl_classes;
655         /* find first ctrl within the class that is >= ID */
656         while (*pctrl && *pctrl < id) pctrl++;
657         if (*pctrl)
658                 return *pctrl;
659         /* we are at the end of the controls of the current class. */
660         /* continue with next class if available */
661         ctrl_classes++;
662         if (*ctrl_classes == NULL)
663                 return 0;
664         return **ctrl_classes;
665 }
666 EXPORT_SYMBOL(v4l2_ctrl_next);
667
668 int v4l2_chip_match_host(u32 match_type, u32 match_chip)
669 {
670         switch (match_type) {
671         case V4L2_CHIP_MATCH_HOST:
672                 return match_chip == 0;
673         default:
674                 return 0;
675         }
676 }
677 EXPORT_SYMBOL(v4l2_chip_match_host);
678
679 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
680 int v4l2_chip_match_i2c_client(struct i2c_client *c, u32 match_type, u32 match_chip)
681 {
682         switch (match_type) {
683         case V4L2_CHIP_MATCH_I2C_DRIVER:
684                 return (c != NULL && c->driver != NULL && c->driver->id == match_chip);
685         case V4L2_CHIP_MATCH_I2C_ADDR:
686                 return (c != NULL && c->addr == match_chip);
687         default:
688                 return 0;
689         }
690 }
691 EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
692
693 int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_chip_ident *chip,
694                 u32 ident, u32 revision)
695 {
696         if (!v4l2_chip_match_i2c_client(c, chip->match_type, chip->match_chip))
697                 return 0;
698         if (chip->ident == V4L2_IDENT_NONE) {
699                 chip->ident = ident;
700                 chip->revision = revision;
701         }
702         else {
703                 chip->ident = V4L2_IDENT_AMBIGUOUS;
704                 chip->revision = 0;
705         }
706         return 0;
707 }
708 EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
709
710 /* ----------------------------------------------------------------- */
711
712 /* Helper function for I2C legacy drivers */
713
714 int v4l2_i2c_attach(struct i2c_adapter *adapter, int address, struct i2c_driver *driver,
715                 const char *name,
716                 int (*probe)(struct i2c_client *, const struct i2c_device_id *))
717 {
718         struct i2c_client *client;
719         int err;
720
721         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
722         if (!client)
723                 return -ENOMEM;
724
725         client->addr = address;
726         client->adapter = adapter;
727         client->driver = driver;
728         strlcpy(client->name, name, sizeof(client->name));
729
730         err = probe(client, NULL);
731         if (err == 0) {
732                 i2c_attach_client(client);
733         } else {
734                 kfree(client);
735         }
736         return err != -ENOMEM ? 0 : err;
737 }
738 EXPORT_SYMBOL(v4l2_i2c_attach);
739 #endif