V4L/DVB (9821): v4l2-common: add i2c helper functions
[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@lxorguk.ukuu.org.uk>
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-device.h>
62 #include <media/v4l2-chip-ident.h>
63
64 #include <linux/videodev2.h>
65
66 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
67 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
68 MODULE_LICENSE("GPL");
69
70 /*
71  *
72  *      V 4 L 2   D R I V E R   H E L P E R   A P I
73  *
74  */
75
76 /*
77  *  Video Standard Operations (contributed by Michael Schimek)
78  */
79
80
81 /* ----------------------------------------------------------------- */
82 /* priority handling                                                 */
83
84 #define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND   || \
85                               val == V4L2_PRIORITY_INTERACTIVE  || \
86                               val == V4L2_PRIORITY_RECORD)
87
88 int v4l2_prio_init(struct v4l2_prio_state *global)
89 {
90         memset(global,0,sizeof(*global));
91         return 0;
92 }
93 EXPORT_SYMBOL(v4l2_prio_init);
94
95 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
96                      enum v4l2_priority new)
97 {
98         if (!V4L2_PRIO_VALID(new))
99                 return -EINVAL;
100         if (*local == new)
101                 return 0;
102
103         atomic_inc(&global->prios[new]);
104         if (V4L2_PRIO_VALID(*local))
105                 atomic_dec(&global->prios[*local]);
106         *local = new;
107         return 0;
108 }
109 EXPORT_SYMBOL(v4l2_prio_change);
110
111 int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
112 {
113         return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
114 }
115 EXPORT_SYMBOL(v4l2_prio_open);
116
117 int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
118 {
119         if (V4L2_PRIO_VALID(*local))
120                 atomic_dec(&global->prios[*local]);
121         return 0;
122 }
123 EXPORT_SYMBOL(v4l2_prio_close);
124
125 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
126 {
127         if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
128                 return V4L2_PRIORITY_RECORD;
129         if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
130                 return V4L2_PRIORITY_INTERACTIVE;
131         if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
132                 return V4L2_PRIORITY_BACKGROUND;
133         return V4L2_PRIORITY_UNSET;
134 }
135 EXPORT_SYMBOL(v4l2_prio_max);
136
137 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
138 {
139         if (*local < v4l2_prio_max(global))
140                 return -EBUSY;
141         return 0;
142 }
143 EXPORT_SYMBOL(v4l2_prio_check);
144
145 /* ----------------------------------------------------------------- */
146
147 /* Helper functions for control handling                             */
148
149 /* Check for correctness of the ctrl's value based on the data from
150    struct v4l2_queryctrl and the available menu items. Note that
151    menu_items may be NULL, in that case it is ignored. */
152 int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
153                 const char **menu_items)
154 {
155         if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
156                 return -EINVAL;
157         if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
158                 return -EBUSY;
159         if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
160             qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
161             qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
162                 return 0;
163         if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
164                 return -ERANGE;
165         if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
166                 if (menu_items[ctrl->value] == NULL ||
167                     menu_items[ctrl->value][0] == '\0')
168                         return -EINVAL;
169         }
170         return 0;
171 }
172 EXPORT_SYMBOL(v4l2_ctrl_check);
173
174 /* Returns NULL or a character pointer array containing the menu for
175    the given control ID. The pointer array ends with a NULL pointer.
176    An empty string signifies a menu entry that is invalid. This allows
177    drivers to disable certain options if it is not supported. */
178 const char **v4l2_ctrl_get_menu(u32 id)
179 {
180         static const char *mpeg_audio_sampling_freq[] = {
181                 "44.1 kHz",
182                 "48 kHz",
183                 "32 kHz",
184                 NULL
185         };
186         static const char *mpeg_audio_encoding[] = {
187                 "MPEG-1/2 Layer I",
188                 "MPEG-1/2 Layer II",
189                 "MPEG-1/2 Layer III",
190                 "MPEG-2/4 AAC",
191                 "AC-3",
192                 NULL
193         };
194         static const char *mpeg_audio_l1_bitrate[] = {
195                 "32 kbps",
196                 "64 kbps",
197                 "96 kbps",
198                 "128 kbps",
199                 "160 kbps",
200                 "192 kbps",
201                 "224 kbps",
202                 "256 kbps",
203                 "288 kbps",
204                 "320 kbps",
205                 "352 kbps",
206                 "384 kbps",
207                 "416 kbps",
208                 "448 kbps",
209                 NULL
210         };
211         static const char *mpeg_audio_l2_bitrate[] = {
212                 "32 kbps",
213                 "48 kbps",
214                 "56 kbps",
215                 "64 kbps",
216                 "80 kbps",
217                 "96 kbps",
218                 "112 kbps",
219                 "128 kbps",
220                 "160 kbps",
221                 "192 kbps",
222                 "224 kbps",
223                 "256 kbps",
224                 "320 kbps",
225                 "384 kbps",
226                 NULL
227         };
228         static const char *mpeg_audio_l3_bitrate[] = {
229                 "32 kbps",
230                 "40 kbps",
231                 "48 kbps",
232                 "56 kbps",
233                 "64 kbps",
234                 "80 kbps",
235                 "96 kbps",
236                 "112 kbps",
237                 "128 kbps",
238                 "160 kbps",
239                 "192 kbps",
240                 "224 kbps",
241                 "256 kbps",
242                 "320 kbps",
243                 NULL
244         };
245         static const char *mpeg_audio_ac3_bitrate[] = {
246                 "32 kbps",
247                 "40 kbps",
248                 "48 kbps",
249                 "56 kbps",
250                 "64 kbps",
251                 "80 kbps",
252                 "96 kbps",
253                 "112 kbps",
254                 "128 kbps",
255                 "160 kbps",
256                 "192 kbps",
257                 "224 kbps",
258                 "256 kbps",
259                 "320 kbps",
260                 "384 kbps",
261                 "448 kbps",
262                 "512 kbps",
263                 "576 kbps",
264                 "640 kbps",
265                 NULL
266         };
267         static const char *mpeg_audio_mode[] = {
268                 "Stereo",
269                 "Joint Stereo",
270                 "Dual",
271                 "Mono",
272                 NULL
273         };
274         static const char *mpeg_audio_mode_extension[] = {
275                 "Bound 4",
276                 "Bound 8",
277                 "Bound 12",
278                 "Bound 16",
279                 NULL
280         };
281         static const char *mpeg_audio_emphasis[] = {
282                 "No Emphasis",
283                 "50/15 us",
284                 "CCITT J17",
285                 NULL
286         };
287         static const char *mpeg_audio_crc[] = {
288                 "No CRC",
289                 "16-bit CRC",
290                 NULL
291         };
292         static const char *mpeg_video_encoding[] = {
293                 "MPEG-1",
294                 "MPEG-2",
295                 "MPEG-4 AVC",
296                 NULL
297         };
298         static const char *mpeg_video_aspect[] = {
299                 "1x1",
300                 "4x3",
301                 "16x9",
302                 "2.21x1",
303                 NULL
304         };
305         static const char *mpeg_video_bitrate_mode[] = {
306                 "Variable Bitrate",
307                 "Constant Bitrate",
308                 NULL
309         };
310         static const char *mpeg_stream_type[] = {
311                 "MPEG-2 Program Stream",
312                 "MPEG-2 Transport Stream",
313                 "MPEG-1 System Stream",
314                 "MPEG-2 DVD-compatible Stream",
315                 "MPEG-1 VCD-compatible Stream",
316                 "MPEG-2 SVCD-compatible Stream",
317                 NULL
318         };
319         static const char *mpeg_stream_vbi_fmt[] = {
320                 "No VBI",
321                 "Private packet, IVTV format",
322                 NULL
323         };
324
325         switch (id) {
326                 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
327                         return mpeg_audio_sampling_freq;
328                 case V4L2_CID_MPEG_AUDIO_ENCODING:
329                         return mpeg_audio_encoding;
330                 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
331                         return mpeg_audio_l1_bitrate;
332                 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
333                         return mpeg_audio_l2_bitrate;
334                 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
335                         return mpeg_audio_l3_bitrate;
336                 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
337                         return mpeg_audio_ac3_bitrate;
338                 case V4L2_CID_MPEG_AUDIO_MODE:
339                         return mpeg_audio_mode;
340                 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
341                         return mpeg_audio_mode_extension;
342                 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
343                         return mpeg_audio_emphasis;
344                 case V4L2_CID_MPEG_AUDIO_CRC:
345                         return mpeg_audio_crc;
346                 case V4L2_CID_MPEG_VIDEO_ENCODING:
347                         return mpeg_video_encoding;
348                 case V4L2_CID_MPEG_VIDEO_ASPECT:
349                         return mpeg_video_aspect;
350                 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
351                         return mpeg_video_bitrate_mode;
352                 case V4L2_CID_MPEG_STREAM_TYPE:
353                         return mpeg_stream_type;
354                 case V4L2_CID_MPEG_STREAM_VBI_FMT:
355                         return mpeg_stream_vbi_fmt;
356                 default:
357                         return NULL;
358         }
359 }
360 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
361
362 /* Return the control name. */
363 const char *v4l2_ctrl_get_name(u32 id)
364 {
365         switch (id) {
366         /* USER controls */
367         case V4L2_CID_USER_CLASS:       return "User Controls";
368         case V4L2_CID_AUDIO_VOLUME:     return "Volume";
369         case V4L2_CID_AUDIO_MUTE:       return "Mute";
370         case V4L2_CID_AUDIO_BALANCE:    return "Balance";
371         case V4L2_CID_AUDIO_BASS:       return "Bass";
372         case V4L2_CID_AUDIO_TREBLE:     return "Treble";
373         case V4L2_CID_AUDIO_LOUDNESS:   return "Loudness";
374         case V4L2_CID_BRIGHTNESS:       return "Brightness";
375         case V4L2_CID_CONTRAST:         return "Contrast";
376         case V4L2_CID_SATURATION:       return "Saturation";
377         case V4L2_CID_HUE:              return "Hue";
378
379         /* MPEG controls */
380         case V4L2_CID_MPEG_CLASS:               return "MPEG Encoder Controls";
381         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
382         case V4L2_CID_MPEG_AUDIO_ENCODING:      return "Audio Encoding";
383         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    return "Audio Layer I Bitrate";
384         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    return "Audio Layer II Bitrate";
385         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    return "Audio Layer III Bitrate";
386         case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:   return "Audio AAC Bitrate";
387         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:   return "Audio AC-3 Bitrate";
388         case V4L2_CID_MPEG_AUDIO_MODE:          return "Audio Stereo Mode";
389         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
390         case V4L2_CID_MPEG_AUDIO_EMPHASIS:      return "Audio Emphasis";
391         case V4L2_CID_MPEG_AUDIO_CRC:           return "Audio CRC";
392         case V4L2_CID_MPEG_AUDIO_MUTE:          return "Audio Mute";
393         case V4L2_CID_MPEG_VIDEO_ENCODING:      return "Video Encoding";
394         case V4L2_CID_MPEG_VIDEO_ASPECT:        return "Video Aspect";
395         case V4L2_CID_MPEG_VIDEO_B_FRAMES:      return "Video B Frames";
396         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      return "Video GOP Size";
397         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   return "Video GOP Closure";
398         case V4L2_CID_MPEG_VIDEO_PULLDOWN:      return "Video Pulldown";
399         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  return "Video Bitrate Mode";
400         case V4L2_CID_MPEG_VIDEO_BITRATE:       return "Video Bitrate";
401         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  return "Video Peak Bitrate";
402         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
403         case V4L2_CID_MPEG_VIDEO_MUTE:          return "Video Mute";
404         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      return "Video Mute YUV";
405         case V4L2_CID_MPEG_STREAM_TYPE:         return "Stream Type";
406         case V4L2_CID_MPEG_STREAM_PID_PMT:      return "Stream PMT Program ID";
407         case V4L2_CID_MPEG_STREAM_PID_AUDIO:    return "Stream Audio Program ID";
408         case V4L2_CID_MPEG_STREAM_PID_VIDEO:    return "Stream Video Program ID";
409         case V4L2_CID_MPEG_STREAM_PID_PCR:      return "Stream PCR Program ID";
410         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
411         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
412         case V4L2_CID_MPEG_STREAM_VBI_FMT:      return "Stream VBI Format";
413
414         default:
415                 return NULL;
416         }
417 }
418 EXPORT_SYMBOL(v4l2_ctrl_get_name);
419
420 /* Fill in a struct v4l2_queryctrl */
421 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
422 {
423         const char *name = v4l2_ctrl_get_name(qctrl->id);
424
425         qctrl->flags = 0;
426         if (name == NULL)
427                 return -EINVAL;
428
429         switch (qctrl->id) {
430         case V4L2_CID_AUDIO_MUTE:
431         case V4L2_CID_AUDIO_LOUDNESS:
432         case V4L2_CID_MPEG_AUDIO_MUTE:
433         case V4L2_CID_MPEG_VIDEO_MUTE:
434         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
435         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
436                 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
437                 min = 0;
438                 max = step = 1;
439                 break;
440         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
441         case V4L2_CID_MPEG_AUDIO_ENCODING:
442         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
443         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
444         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
445         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
446         case V4L2_CID_MPEG_AUDIO_MODE:
447         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
448         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
449         case V4L2_CID_MPEG_AUDIO_CRC:
450         case V4L2_CID_MPEG_VIDEO_ENCODING:
451         case V4L2_CID_MPEG_VIDEO_ASPECT:
452         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
453         case V4L2_CID_MPEG_STREAM_TYPE:
454         case V4L2_CID_MPEG_STREAM_VBI_FMT:
455                 qctrl->type = V4L2_CTRL_TYPE_MENU;
456                 step = 1;
457                 break;
458         case V4L2_CID_USER_CLASS:
459         case V4L2_CID_MPEG_CLASS:
460                 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
461                 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
462                 min = max = step = def = 0;
463                 break;
464         default:
465                 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
466                 break;
467         }
468         switch (qctrl->id) {
469         case V4L2_CID_MPEG_AUDIO_ENCODING:
470         case V4L2_CID_MPEG_AUDIO_MODE:
471         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
472         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
473         case V4L2_CID_MPEG_STREAM_TYPE:
474                 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
475                 break;
476         case V4L2_CID_AUDIO_VOLUME:
477         case V4L2_CID_AUDIO_BALANCE:
478         case V4L2_CID_AUDIO_BASS:
479         case V4L2_CID_AUDIO_TREBLE:
480         case V4L2_CID_BRIGHTNESS:
481         case V4L2_CID_CONTRAST:
482         case V4L2_CID_SATURATION:
483         case V4L2_CID_HUE:
484                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
485                 break;
486         }
487         qctrl->minimum = min;
488         qctrl->maximum = max;
489         qctrl->step = step;
490         qctrl->default_value = def;
491         qctrl->reserved[0] = qctrl->reserved[1] = 0;
492         snprintf(qctrl->name, sizeof(qctrl->name), name);
493         return 0;
494 }
495 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
496
497 /* Fill in a struct v4l2_queryctrl with standard values based on
498    the control ID. */
499 int v4l2_ctrl_query_fill_std(struct v4l2_queryctrl *qctrl)
500 {
501         switch (qctrl->id) {
502         /* USER controls */
503         case V4L2_CID_USER_CLASS:
504         case V4L2_CID_MPEG_CLASS:
505                 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
506         case V4L2_CID_AUDIO_VOLUME:
507                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 58880);
508         case V4L2_CID_AUDIO_MUTE:
509         case V4L2_CID_AUDIO_LOUDNESS:
510                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
511         case V4L2_CID_AUDIO_BALANCE:
512         case V4L2_CID_AUDIO_BASS:
513         case V4L2_CID_AUDIO_TREBLE:
514                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 32768);
515         case V4L2_CID_BRIGHTNESS:
516                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
517         case V4L2_CID_CONTRAST:
518         case V4L2_CID_SATURATION:
519                 return v4l2_ctrl_query_fill(qctrl, 0, 127, 1, 64);
520         case V4L2_CID_HUE:
521                 return v4l2_ctrl_query_fill(qctrl, -128, 127, 1, 0);
522
523         /* MPEG controls */
524         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
525                 return v4l2_ctrl_query_fill(qctrl,
526                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100,
527                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000, 1,
528                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
529         case V4L2_CID_MPEG_AUDIO_ENCODING:
530                 return v4l2_ctrl_query_fill(qctrl,
531                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_1,
532                                 V4L2_MPEG_AUDIO_ENCODING_AC3, 1,
533                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
534         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
535                 return v4l2_ctrl_query_fill(qctrl,
536                                 V4L2_MPEG_AUDIO_L1_BITRATE_32K,
537                                 V4L2_MPEG_AUDIO_L1_BITRATE_448K, 1,
538                                 V4L2_MPEG_AUDIO_L1_BITRATE_256K);
539         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
540                 return v4l2_ctrl_query_fill(qctrl,
541                                 V4L2_MPEG_AUDIO_L2_BITRATE_32K,
542                                 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
543                                 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
544         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
545                 return v4l2_ctrl_query_fill(qctrl,
546                                 V4L2_MPEG_AUDIO_L3_BITRATE_32K,
547                                 V4L2_MPEG_AUDIO_L3_BITRATE_320K, 1,
548                                 V4L2_MPEG_AUDIO_L3_BITRATE_192K);
549         case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:
550                 return v4l2_ctrl_query_fill(qctrl, 0, 6400, 1, 3200000);
551         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
552                 return v4l2_ctrl_query_fill(qctrl,
553                                 V4L2_MPEG_AUDIO_AC3_BITRATE_32K,
554                                 V4L2_MPEG_AUDIO_AC3_BITRATE_640K, 1,
555                                 V4L2_MPEG_AUDIO_AC3_BITRATE_384K);
556         case V4L2_CID_MPEG_AUDIO_MODE:
557                 return v4l2_ctrl_query_fill(qctrl,
558                                 V4L2_MPEG_AUDIO_MODE_STEREO,
559                                 V4L2_MPEG_AUDIO_MODE_MONO, 1,
560                                 V4L2_MPEG_AUDIO_MODE_STEREO);
561         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
562                 return v4l2_ctrl_query_fill(qctrl,
563                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
564                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16, 1,
565                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4);
566         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
567                 return v4l2_ctrl_query_fill(qctrl,
568                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE,
569                                 V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17, 1,
570                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE);
571         case V4L2_CID_MPEG_AUDIO_CRC:
572                 return v4l2_ctrl_query_fill(qctrl,
573                                 V4L2_MPEG_AUDIO_CRC_NONE,
574                                 V4L2_MPEG_AUDIO_CRC_CRC16, 1,
575                                 V4L2_MPEG_AUDIO_CRC_NONE);
576         case V4L2_CID_MPEG_AUDIO_MUTE:
577                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
578         case V4L2_CID_MPEG_VIDEO_ENCODING:
579                 return v4l2_ctrl_query_fill(qctrl,
580                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
581                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
582                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
583         case V4L2_CID_MPEG_VIDEO_ASPECT:
584                 return v4l2_ctrl_query_fill(qctrl,
585                                 V4L2_MPEG_VIDEO_ASPECT_1x1,
586                                 V4L2_MPEG_VIDEO_ASPECT_221x100, 1,
587                                 V4L2_MPEG_VIDEO_ASPECT_4x3);
588         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
589                 return v4l2_ctrl_query_fill(qctrl, 0, 33, 1, 2);
590         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
591                 return v4l2_ctrl_query_fill(qctrl, 1, 34, 1, 12);
592         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
593                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
594         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
595                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
596         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
597                 return v4l2_ctrl_query_fill(qctrl,
598                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
599                                 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
600                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
601         case V4L2_CID_MPEG_VIDEO_BITRATE:
602                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
603         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
604                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
605         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
606                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
607         case V4L2_CID_MPEG_VIDEO_MUTE:
608                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
609         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:  /* Init YUV (really YCbCr) to black */
610                 return v4l2_ctrl_query_fill(qctrl, 0, 0xffffff, 1, 0x008080);
611         case V4L2_CID_MPEG_STREAM_TYPE:
612                 return v4l2_ctrl_query_fill(qctrl,
613                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
614                                 V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD, 1,
615                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
616         case V4L2_CID_MPEG_STREAM_PID_PMT:
617                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
618         case V4L2_CID_MPEG_STREAM_PID_AUDIO:
619                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
620         case V4L2_CID_MPEG_STREAM_PID_VIDEO:
621                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
622         case V4L2_CID_MPEG_STREAM_PID_PCR:
623                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
624         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO:
625                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
626         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO:
627                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
628         case V4L2_CID_MPEG_STREAM_VBI_FMT:
629                 return v4l2_ctrl_query_fill(qctrl,
630                                 V4L2_MPEG_STREAM_VBI_FMT_NONE,
631                                 V4L2_MPEG_STREAM_VBI_FMT_IVTV, 1,
632                                 V4L2_MPEG_STREAM_VBI_FMT_NONE);
633         default:
634                 return -EINVAL;
635         }
636 }
637 EXPORT_SYMBOL(v4l2_ctrl_query_fill_std);
638
639 /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
640    the menu. The qctrl pointer may be NULL, in which case it is ignored.
641    If menu_items is NULL, then the menu items are retrieved using
642    v4l2_ctrl_get_menu. */
643 int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
644                const char **menu_items)
645 {
646         int i;
647
648         qmenu->reserved = 0;
649         if (menu_items == NULL)
650                 menu_items = v4l2_ctrl_get_menu(qmenu->id);
651         if (menu_items == NULL ||
652             (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
653                 return -EINVAL;
654         for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
655         if (menu_items[i] == NULL || menu_items[i][0] == '\0')
656                 return -EINVAL;
657         snprintf(qmenu->name, sizeof(qmenu->name), menu_items[qmenu->index]);
658         return 0;
659 }
660 EXPORT_SYMBOL(v4l2_ctrl_query_menu);
661
662 /* Fill in a struct v4l2_querymenu based on the specified array of valid
663    menu items (terminated by V4L2_CTRL_MENU_IDS_END).
664    Use this if there are 'holes' in the list of valid menu items. */
665 int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
666 {
667         const char **menu_items = v4l2_ctrl_get_menu(qmenu->id);
668
669         qmenu->reserved = 0;
670         if (menu_items == NULL || ids == NULL)
671                 return -EINVAL;
672         while (*ids != V4L2_CTRL_MENU_IDS_END) {
673                 if (*ids++ == qmenu->index) {
674                         snprintf(qmenu->name, sizeof(qmenu->name),
675                                        menu_items[qmenu->index]);
676                         return 0;
677                 }
678         }
679         return -EINVAL;
680 }
681 EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
682
683 /* ctrl_classes points to an array of u32 pointers, the last element is
684    a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
685    Each array must be sorted low to high and belong to the same control
686    class. The array of u32 pointer must also be sorted, from low class IDs
687    to high class IDs.
688
689    This function returns the first ID that follows after the given ID.
690    When no more controls are available 0 is returned. */
691 u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
692 {
693         u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
694         const u32 *pctrl;
695
696         if (ctrl_classes == NULL)
697                 return 0;
698
699         /* if no query is desired, then check if the ID is part of ctrl_classes */
700         if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
701                 /* find class */
702                 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
703                         ctrl_classes++;
704                 if (*ctrl_classes == NULL)
705                         return 0;
706                 pctrl = *ctrl_classes;
707                 /* find control ID */
708                 while (*pctrl && *pctrl != id) pctrl++;
709                 return *pctrl ? id : 0;
710         }
711         id &= V4L2_CTRL_ID_MASK;
712         id++;   /* select next control */
713         /* find first class that matches (or is greater than) the class of
714            the ID */
715         while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
716                 ctrl_classes++;
717         /* no more classes */
718         if (*ctrl_classes == NULL)
719                 return 0;
720         pctrl = *ctrl_classes;
721         /* find first ctrl within the class that is >= ID */
722         while (*pctrl && *pctrl < id) pctrl++;
723         if (*pctrl)
724                 return *pctrl;
725         /* we are at the end of the controls of the current class. */
726         /* continue with next class if available */
727         ctrl_classes++;
728         if (*ctrl_classes == NULL)
729                 return 0;
730         return **ctrl_classes;
731 }
732 EXPORT_SYMBOL(v4l2_ctrl_next);
733
734 int v4l2_chip_match_host(u32 match_type, u32 match_chip)
735 {
736         switch (match_type) {
737         case V4L2_CHIP_MATCH_HOST:
738                 return match_chip == 0;
739         default:
740                 return 0;
741         }
742 }
743 EXPORT_SYMBOL(v4l2_chip_match_host);
744
745 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
746 int v4l2_chip_match_i2c_client(struct i2c_client *c, u32 match_type, u32 match_chip)
747 {
748         switch (match_type) {
749         case V4L2_CHIP_MATCH_I2C_DRIVER:
750                 return (c != NULL && c->driver != NULL && c->driver->id == match_chip);
751         case V4L2_CHIP_MATCH_I2C_ADDR:
752                 return (c != NULL && c->addr == match_chip);
753         default:
754                 return 0;
755         }
756 }
757 EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
758
759 int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_chip_ident *chip,
760                 u32 ident, u32 revision)
761 {
762         if (!v4l2_chip_match_i2c_client(c, chip->match_type, chip->match_chip))
763                 return 0;
764         if (chip->ident == V4L2_IDENT_NONE) {
765                 chip->ident = ident;
766                 chip->revision = revision;
767         }
768         else {
769                 chip->ident = V4L2_IDENT_AMBIGUOUS;
770                 chip->revision = 0;
771         }
772         return 0;
773 }
774 EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
775
776 /* ----------------------------------------------------------------- */
777
778 /* Helper function for I2C legacy drivers */
779
780 int v4l2_i2c_attach(struct i2c_adapter *adapter, int address, struct i2c_driver *driver,
781                 const char *name,
782                 int (*probe)(struct i2c_client *, const struct i2c_device_id *))
783 {
784         struct i2c_client *client;
785         int err;
786
787         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
788         if (!client)
789                 return -ENOMEM;
790
791         client->addr = address;
792         client->adapter = adapter;
793         client->driver = driver;
794         strlcpy(client->name, name, sizeof(client->name));
795
796         err = probe(client, NULL);
797         if (err == 0) {
798                 i2c_attach_client(client);
799         } else {
800                 kfree(client);
801         }
802         return err != -ENOMEM ? 0 : err;
803 }
804 EXPORT_SYMBOL(v4l2_i2c_attach);
805
806 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
807                 const struct v4l2_subdev_ops *ops)
808 {
809         v4l2_subdev_init(sd, ops);
810         /* the owner is the same as the i2c_client's driver owner */
811         sd->owner = client->driver->driver.owner;
812         /* i2c_client and v4l2_subdev point to one another */
813         v4l2_set_subdevdata(sd, client);
814         i2c_set_clientdata(client, sd);
815         /* initialize name */
816         snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
817                 client->driver->driver.name, i2c_adapter_id(client->adapter),
818                 client->addr);
819 }
820 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
821
822
823
824 /* Load an i2c sub-device. It assumes that i2c_get_adapdata(adapter)
825    returns the v4l2_device and that i2c_get_clientdata(client)
826    returns the v4l2_subdev. */
827 struct v4l2_subdev *v4l2_i2c_new_subdev(struct i2c_adapter *adapter,
828                 const char *module_name, const char *client_type, u8 addr)
829 {
830         struct v4l2_device *dev = i2c_get_adapdata(adapter);
831         struct v4l2_subdev *sd = NULL;
832         struct i2c_client *client;
833         struct i2c_board_info info;
834
835         BUG_ON(!dev);
836 #ifdef MODULE
837         if (module_name)
838                 request_module(module_name);
839 #endif
840         /* Setup the i2c board info with the device type and
841            the device address. */
842         memset(&info, 0, sizeof(info));
843         strlcpy(info.type, client_type, sizeof(info.type));
844         info.addr = addr;
845
846         /* Create the i2c client */
847         client = i2c_new_device(adapter, &info);
848         /* Note: it is possible in the future that
849            c->driver is NULL if the driver is still being loaded.
850            We need better support from the kernel so that we
851            can easily wait for the load to finish. */
852         if (client == NULL || client->driver == NULL)
853                 return NULL;
854
855         /* Lock the module so we can safely get the v4l2_subdev pointer */
856         if (!try_module_get(client->driver->driver.owner))
857                 return NULL;
858         sd = i2c_get_clientdata(client);
859
860         /* Register with the v4l2_device which increases the module's
861            use count as well. */
862         if (v4l2_device_register_subdev(dev, sd))
863                 sd = NULL;
864         /* Decrease the module use count to match the first try_module_get. */
865         module_put(client->driver->driver.owner);
866         return sd;
867
868 }
869 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
870
871 /* Probe and load an i2c sub-device. It assumes that i2c_get_adapdata(adapter)
872    returns the v4l2_device and that i2c_get_clientdata(client)
873    returns the v4l2_subdev. */
874 struct v4l2_subdev *v4l2_i2c_new_probed_subdev(struct i2c_adapter *adapter,
875         const char *module_name, const char *client_type,
876         const unsigned short *addrs)
877 {
878         struct v4l2_device *dev = i2c_get_adapdata(adapter);
879         struct v4l2_subdev *sd = NULL;
880         struct i2c_client *client = NULL;
881         struct i2c_board_info info;
882
883         BUG_ON(!dev);
884 #ifdef MODULE
885         if (module_name)
886                 request_module(module_name);
887 #endif
888         /* Setup the i2c board info with the device type and
889            the device address. */
890         memset(&info, 0, sizeof(info));
891         strlcpy(info.type, client_type, sizeof(info.type));
892
893         /* Probe and create the i2c client */
894         client = i2c_new_probed_device(adapter, &info, addrs);
895         /* Note: it is possible in the future that
896            c->driver is NULL if the driver is still being loaded.
897            We need better support from the kernel so that we
898            can easily wait for the load to finish. */
899         if (client == NULL || client->driver == NULL)
900                 return NULL;
901
902         /* Lock the module so we can safely get the v4l2_subdev pointer */
903         if (!try_module_get(client->driver->driver.owner))
904                 return NULL;
905         sd = i2c_get_clientdata(client);
906
907         /* Register with the v4l2_device which increases the module's
908            use count as well. */
909         if (v4l2_device_register_subdev(dev, sd))
910                 sd = NULL;
911         /* Decrease the module use count to match the first try_module_get. */
912         module_put(client->driver->driver.owner);
913         return sd;
914 }
915 EXPORT_SYMBOL_GPL(v4l2_i2c_new_probed_subdev);
916
917 #endif