Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arjan/linux...
[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/2 Layer I",
191                 "MPEG-1/2 Layer II",
192                 "MPEG-1/2 Layer III",
193                 "MPEG-2/4 AAC",
194                 "AC-3",
195                 NULL
196         };
197         static const char *mpeg_audio_l1_bitrate[] = {
198                 "32 kbps",
199                 "64 kbps",
200                 "96 kbps",
201                 "128 kbps",
202                 "160 kbps",
203                 "192 kbps",
204                 "224 kbps",
205                 "256 kbps",
206                 "288 kbps",
207                 "320 kbps",
208                 "352 kbps",
209                 "384 kbps",
210                 "416 kbps",
211                 "448 kbps",
212                 NULL
213         };
214         static const char *mpeg_audio_l2_bitrate[] = {
215                 "32 kbps",
216                 "48 kbps",
217                 "56 kbps",
218                 "64 kbps",
219                 "80 kbps",
220                 "96 kbps",
221                 "112 kbps",
222                 "128 kbps",
223                 "160 kbps",
224                 "192 kbps",
225                 "224 kbps",
226                 "256 kbps",
227                 "320 kbps",
228                 "384 kbps",
229                 NULL
230         };
231         static const char *mpeg_audio_l3_bitrate[] = {
232                 "32 kbps",
233                 "40 kbps",
234                 "48 kbps",
235                 "56 kbps",
236                 "64 kbps",
237                 "80 kbps",
238                 "96 kbps",
239                 "112 kbps",
240                 "128 kbps",
241                 "160 kbps",
242                 "192 kbps",
243                 "224 kbps",
244                 "256 kbps",
245                 "320 kbps",
246                 NULL
247         };
248         static const char *mpeg_audio_ac3_bitrate[] = {
249                 "32 kbps",
250                 "40 kbps",
251                 "48 kbps",
252                 "56 kbps",
253                 "64 kbps",
254                 "80 kbps",
255                 "96 kbps",
256                 "112 kbps",
257                 "128 kbps",
258                 "160 kbps",
259                 "192 kbps",
260                 "224 kbps",
261                 "256 kbps",
262                 "320 kbps",
263                 "384 kbps",
264                 "448 kbps",
265                 "512 kbps",
266                 "576 kbps",
267                 "640 kbps",
268                 NULL
269         };
270         static const char *mpeg_audio_mode[] = {
271                 "Stereo",
272                 "Joint Stereo",
273                 "Dual",
274                 "Mono",
275                 NULL
276         };
277         static const char *mpeg_audio_mode_extension[] = {
278                 "Bound 4",
279                 "Bound 8",
280                 "Bound 12",
281                 "Bound 16",
282                 NULL
283         };
284         static const char *mpeg_audio_emphasis[] = {
285                 "No Emphasis",
286                 "50/15 us",
287                 "CCITT J17",
288                 NULL
289         };
290         static const char *mpeg_audio_crc[] = {
291                 "No CRC",
292                 "16-bit CRC",
293                 NULL
294         };
295         static const char *mpeg_video_encoding[] = {
296                 "MPEG-1",
297                 "MPEG-2",
298                 "MPEG-4 AVC",
299                 NULL
300         };
301         static const char *mpeg_video_aspect[] = {
302                 "1x1",
303                 "4x3",
304                 "16x9",
305                 "2.21x1",
306                 NULL
307         };
308         static const char *mpeg_video_bitrate_mode[] = {
309                 "Variable Bitrate",
310                 "Constant Bitrate",
311                 NULL
312         };
313         static const char *mpeg_stream_type[] = {
314                 "MPEG-2 Program Stream",
315                 "MPEG-2 Transport Stream",
316                 "MPEG-1 System Stream",
317                 "MPEG-2 DVD-compatible Stream",
318                 "MPEG-1 VCD-compatible Stream",
319                 "MPEG-2 SVCD-compatible Stream",
320                 NULL
321         };
322         static const char *mpeg_stream_vbi_fmt[] = {
323                 "No VBI",
324                 "Private packet, IVTV format",
325                 NULL
326         };
327
328         switch (id) {
329                 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
330                         return mpeg_audio_sampling_freq;
331                 case V4L2_CID_MPEG_AUDIO_ENCODING:
332                         return mpeg_audio_encoding;
333                 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
334                         return mpeg_audio_l1_bitrate;
335                 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
336                         return mpeg_audio_l2_bitrate;
337                 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
338                         return mpeg_audio_l3_bitrate;
339                 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
340                         return mpeg_audio_ac3_bitrate;
341                 case V4L2_CID_MPEG_AUDIO_MODE:
342                         return mpeg_audio_mode;
343                 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
344                         return mpeg_audio_mode_extension;
345                 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
346                         return mpeg_audio_emphasis;
347                 case V4L2_CID_MPEG_AUDIO_CRC:
348                         return mpeg_audio_crc;
349                 case V4L2_CID_MPEG_VIDEO_ENCODING:
350                         return mpeg_video_encoding;
351                 case V4L2_CID_MPEG_VIDEO_ASPECT:
352                         return mpeg_video_aspect;
353                 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
354                         return mpeg_video_bitrate_mode;
355                 case V4L2_CID_MPEG_STREAM_TYPE:
356                         return mpeg_stream_type;
357                 case V4L2_CID_MPEG_STREAM_VBI_FMT:
358                         return mpeg_stream_vbi_fmt;
359                 default:
360                         return NULL;
361         }
362 }
363 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
364
365 /* Return the control name. */
366 const char *v4l2_ctrl_get_name(u32 id)
367 {
368         switch (id) {
369         /* USER controls */
370         case V4L2_CID_USER_CLASS:       return "User Controls";
371         case V4L2_CID_AUDIO_VOLUME:     return "Volume";
372         case V4L2_CID_AUDIO_MUTE:       return "Mute";
373         case V4L2_CID_AUDIO_BALANCE:    return "Balance";
374         case V4L2_CID_AUDIO_BASS:       return "Bass";
375         case V4L2_CID_AUDIO_TREBLE:     return "Treble";
376         case V4L2_CID_AUDIO_LOUDNESS:   return "Loudness";
377         case V4L2_CID_BRIGHTNESS:       return "Brightness";
378         case V4L2_CID_CONTRAST:         return "Contrast";
379         case V4L2_CID_SATURATION:       return "Saturation";
380         case V4L2_CID_HUE:              return "Hue";
381
382         /* MPEG controls */
383         case V4L2_CID_MPEG_CLASS:               return "MPEG Encoder Controls";
384         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
385         case V4L2_CID_MPEG_AUDIO_ENCODING:      return "Audio Encoding";
386         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    return "Audio Layer I Bitrate";
387         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    return "Audio Layer II Bitrate";
388         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    return "Audio Layer III Bitrate";
389         case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:   return "Audio AAC Bitrate";
390         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:   return "Audio AC-3 Bitrate";
391         case V4L2_CID_MPEG_AUDIO_MODE:          return "Audio Stereo Mode";
392         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
393         case V4L2_CID_MPEG_AUDIO_EMPHASIS:      return "Audio Emphasis";
394         case V4L2_CID_MPEG_AUDIO_CRC:           return "Audio CRC";
395         case V4L2_CID_MPEG_AUDIO_MUTE:          return "Audio Mute";
396         case V4L2_CID_MPEG_VIDEO_ENCODING:      return "Video Encoding";
397         case V4L2_CID_MPEG_VIDEO_ASPECT:        return "Video Aspect";
398         case V4L2_CID_MPEG_VIDEO_B_FRAMES:      return "Video B Frames";
399         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      return "Video GOP Size";
400         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   return "Video GOP Closure";
401         case V4L2_CID_MPEG_VIDEO_PULLDOWN:      return "Video Pulldown";
402         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  return "Video Bitrate Mode";
403         case V4L2_CID_MPEG_VIDEO_BITRATE:       return "Video Bitrate";
404         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  return "Video Peak Bitrate";
405         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
406         case V4L2_CID_MPEG_VIDEO_MUTE:          return "Video Mute";
407         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      return "Video Mute YUV";
408         case V4L2_CID_MPEG_STREAM_TYPE:         return "Stream Type";
409         case V4L2_CID_MPEG_STREAM_PID_PMT:      return "Stream PMT Program ID";
410         case V4L2_CID_MPEG_STREAM_PID_AUDIO:    return "Stream Audio Program ID";
411         case V4L2_CID_MPEG_STREAM_PID_VIDEO:    return "Stream Video Program ID";
412         case V4L2_CID_MPEG_STREAM_PID_PCR:      return "Stream PCR Program ID";
413         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
414         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
415         case V4L2_CID_MPEG_STREAM_VBI_FMT:      return "Stream VBI Format";
416
417         default:
418                 return NULL;
419         }
420 }
421 EXPORT_SYMBOL(v4l2_ctrl_get_name);
422
423 /* Fill in a struct v4l2_queryctrl */
424 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
425 {
426         const char *name = v4l2_ctrl_get_name(qctrl->id);
427
428         qctrl->flags = 0;
429         if (name == NULL)
430                 return -EINVAL;
431
432         switch (qctrl->id) {
433         case V4L2_CID_AUDIO_MUTE:
434         case V4L2_CID_AUDIO_LOUDNESS:
435         case V4L2_CID_MPEG_AUDIO_MUTE:
436         case V4L2_CID_MPEG_VIDEO_MUTE:
437         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
438         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
439                 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
440                 min = 0;
441                 max = step = 1;
442                 break;
443         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
444         case V4L2_CID_MPEG_AUDIO_ENCODING:
445         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
446         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
447         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
448         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
449         case V4L2_CID_MPEG_AUDIO_MODE:
450         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
451         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
452         case V4L2_CID_MPEG_AUDIO_CRC:
453         case V4L2_CID_MPEG_VIDEO_ENCODING:
454         case V4L2_CID_MPEG_VIDEO_ASPECT:
455         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
456         case V4L2_CID_MPEG_STREAM_TYPE:
457         case V4L2_CID_MPEG_STREAM_VBI_FMT:
458                 qctrl->type = V4L2_CTRL_TYPE_MENU;
459                 step = 1;
460                 break;
461         case V4L2_CID_USER_CLASS:
462         case V4L2_CID_MPEG_CLASS:
463                 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
464                 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
465                 min = max = step = def = 0;
466                 break;
467         default:
468                 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
469                 break;
470         }
471         switch (qctrl->id) {
472         case V4L2_CID_MPEG_AUDIO_ENCODING:
473         case V4L2_CID_MPEG_AUDIO_MODE:
474         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
475         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
476         case V4L2_CID_MPEG_STREAM_TYPE:
477                 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
478                 break;
479         case V4L2_CID_AUDIO_VOLUME:
480         case V4L2_CID_AUDIO_BALANCE:
481         case V4L2_CID_AUDIO_BASS:
482         case V4L2_CID_AUDIO_TREBLE:
483         case V4L2_CID_BRIGHTNESS:
484         case V4L2_CID_CONTRAST:
485         case V4L2_CID_SATURATION:
486         case V4L2_CID_HUE:
487                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
488                 break;
489         }
490         qctrl->minimum = min;
491         qctrl->maximum = max;
492         qctrl->step = step;
493         qctrl->default_value = def;
494         qctrl->reserved[0] = qctrl->reserved[1] = 0;
495         snprintf(qctrl->name, sizeof(qctrl->name), name);
496         return 0;
497 }
498 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
499
500 /* Fill in a struct v4l2_queryctrl with standard values based on
501    the control ID. */
502 int v4l2_ctrl_query_fill_std(struct v4l2_queryctrl *qctrl)
503 {
504         switch (qctrl->id) {
505         /* USER controls */
506         case V4L2_CID_USER_CLASS:
507         case V4L2_CID_MPEG_CLASS:
508                 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
509         case V4L2_CID_AUDIO_VOLUME:
510                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 58880);
511         case V4L2_CID_AUDIO_MUTE:
512         case V4L2_CID_AUDIO_LOUDNESS:
513                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
514         case V4L2_CID_AUDIO_BALANCE:
515         case V4L2_CID_AUDIO_BASS:
516         case V4L2_CID_AUDIO_TREBLE:
517                 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 32768);
518         case V4L2_CID_BRIGHTNESS:
519                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
520         case V4L2_CID_CONTRAST:
521         case V4L2_CID_SATURATION:
522                 return v4l2_ctrl_query_fill(qctrl, 0, 127, 1, 64);
523         case V4L2_CID_HUE:
524                 return v4l2_ctrl_query_fill(qctrl, -128, 127, 1, 0);
525
526         /* MPEG controls */
527         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
528                 return v4l2_ctrl_query_fill(qctrl,
529                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100,
530                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000, 1,
531                                 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
532         case V4L2_CID_MPEG_AUDIO_ENCODING:
533                 return v4l2_ctrl_query_fill(qctrl,
534                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_1,
535                                 V4L2_MPEG_AUDIO_ENCODING_AC3, 1,
536                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
537         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
538                 return v4l2_ctrl_query_fill(qctrl,
539                                 V4L2_MPEG_AUDIO_L1_BITRATE_32K,
540                                 V4L2_MPEG_AUDIO_L1_BITRATE_448K, 1,
541                                 V4L2_MPEG_AUDIO_L1_BITRATE_256K);
542         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
543                 return v4l2_ctrl_query_fill(qctrl,
544                                 V4L2_MPEG_AUDIO_L2_BITRATE_32K,
545                                 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
546                                 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
547         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
548                 return v4l2_ctrl_query_fill(qctrl,
549                                 V4L2_MPEG_AUDIO_L3_BITRATE_32K,
550                                 V4L2_MPEG_AUDIO_L3_BITRATE_320K, 1,
551                                 V4L2_MPEG_AUDIO_L3_BITRATE_192K);
552         case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:
553                 return v4l2_ctrl_query_fill(qctrl, 0, 6400, 1, 3200000);
554         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
555                 return v4l2_ctrl_query_fill(qctrl,
556                                 V4L2_MPEG_AUDIO_AC3_BITRATE_32K,
557                                 V4L2_MPEG_AUDIO_AC3_BITRATE_640K, 1,
558                                 V4L2_MPEG_AUDIO_AC3_BITRATE_384K);
559         case V4L2_CID_MPEG_AUDIO_MODE:
560                 return v4l2_ctrl_query_fill(qctrl,
561                                 V4L2_MPEG_AUDIO_MODE_STEREO,
562                                 V4L2_MPEG_AUDIO_MODE_MONO, 1,
563                                 V4L2_MPEG_AUDIO_MODE_STEREO);
564         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
565                 return v4l2_ctrl_query_fill(qctrl,
566                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
567                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16, 1,
568                                 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4);
569         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
570                 return v4l2_ctrl_query_fill(qctrl,
571                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE,
572                                 V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17, 1,
573                                 V4L2_MPEG_AUDIO_EMPHASIS_NONE);
574         case V4L2_CID_MPEG_AUDIO_CRC:
575                 return v4l2_ctrl_query_fill(qctrl,
576                                 V4L2_MPEG_AUDIO_CRC_NONE,
577                                 V4L2_MPEG_AUDIO_CRC_CRC16, 1,
578                                 V4L2_MPEG_AUDIO_CRC_NONE);
579         case V4L2_CID_MPEG_AUDIO_MUTE:
580                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
581         case V4L2_CID_MPEG_VIDEO_ENCODING:
582                 return v4l2_ctrl_query_fill(qctrl,
583                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
584                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
585                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
586         case V4L2_CID_MPEG_VIDEO_ASPECT:
587                 return v4l2_ctrl_query_fill(qctrl,
588                                 V4L2_MPEG_VIDEO_ASPECT_1x1,
589                                 V4L2_MPEG_VIDEO_ASPECT_221x100, 1,
590                                 V4L2_MPEG_VIDEO_ASPECT_4x3);
591         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
592                 return v4l2_ctrl_query_fill(qctrl, 0, 33, 1, 2);
593         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
594                 return v4l2_ctrl_query_fill(qctrl, 1, 34, 1, 12);
595         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
596                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
597         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
598                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
599         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
600                 return v4l2_ctrl_query_fill(qctrl,
601                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
602                                 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
603                                 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
604         case V4L2_CID_MPEG_VIDEO_BITRATE:
605                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
606         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
607                 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
608         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
609                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
610         case V4L2_CID_MPEG_VIDEO_MUTE:
611                 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
612         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:  /* Init YUV (really YCbCr) to black */
613                 return v4l2_ctrl_query_fill(qctrl, 0, 0xffffff, 1, 0x008080);
614         case V4L2_CID_MPEG_STREAM_TYPE:
615                 return v4l2_ctrl_query_fill(qctrl,
616                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
617                                 V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD, 1,
618                                 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
619         case V4L2_CID_MPEG_STREAM_PID_PMT:
620                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
621         case V4L2_CID_MPEG_STREAM_PID_AUDIO:
622                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
623         case V4L2_CID_MPEG_STREAM_PID_VIDEO:
624                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
625         case V4L2_CID_MPEG_STREAM_PID_PCR:
626                 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
627         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO:
628                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
629         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO:
630                 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
631         case V4L2_CID_MPEG_STREAM_VBI_FMT:
632                 return v4l2_ctrl_query_fill(qctrl,
633                                 V4L2_MPEG_STREAM_VBI_FMT_NONE,
634                                 V4L2_MPEG_STREAM_VBI_FMT_IVTV, 1,
635                                 V4L2_MPEG_STREAM_VBI_FMT_NONE);
636         default:
637                 return -EINVAL;
638         }
639 }
640 EXPORT_SYMBOL(v4l2_ctrl_query_fill_std);
641
642 /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
643    the menu. The qctrl pointer may be NULL, in which case it is ignored.
644    If menu_items is NULL, then the menu items are retrieved using
645    v4l2_ctrl_get_menu. */
646 int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
647                const char **menu_items)
648 {
649         int i;
650
651         qmenu->reserved = 0;
652         if (menu_items == NULL)
653                 menu_items = v4l2_ctrl_get_menu(qmenu->id);
654         if (menu_items == NULL ||
655             (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
656                 return -EINVAL;
657         for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
658         if (menu_items[i] == NULL || menu_items[i][0] == '\0')
659                 return -EINVAL;
660         snprintf(qmenu->name, sizeof(qmenu->name), menu_items[qmenu->index]);
661         return 0;
662 }
663 EXPORT_SYMBOL(v4l2_ctrl_query_menu);
664
665 /* Fill in a struct v4l2_querymenu based on the specified array of valid
666    menu items (terminated by V4L2_CTRL_MENU_IDS_END).
667    Use this if there are 'holes' in the list of valid menu items. */
668 int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
669 {
670         const char **menu_items = v4l2_ctrl_get_menu(qmenu->id);
671
672         qmenu->reserved = 0;
673         if (menu_items == NULL || ids == NULL)
674                 return -EINVAL;
675         while (*ids != V4L2_CTRL_MENU_IDS_END) {
676                 if (*ids++ == qmenu->index) {
677                         snprintf(qmenu->name, sizeof(qmenu->name),
678                                        menu_items[qmenu->index]);
679                         return 0;
680                 }
681         }
682         return -EINVAL;
683 }
684 EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
685
686 /* ctrl_classes points to an array of u32 pointers, the last element is
687    a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
688    Each array must be sorted low to high and belong to the same control
689    class. The array of u32 pointer must also be sorted, from low class IDs
690    to high class IDs.
691
692    This function returns the first ID that follows after the given ID.
693    When no more controls are available 0 is returned. */
694 u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
695 {
696         u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
697         const u32 *pctrl;
698
699         if (ctrl_classes == NULL)
700                 return 0;
701
702         /* if no query is desired, then check if the ID is part of ctrl_classes */
703         if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
704                 /* find class */
705                 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
706                         ctrl_classes++;
707                 if (*ctrl_classes == NULL)
708                         return 0;
709                 pctrl = *ctrl_classes;
710                 /* find control ID */
711                 while (*pctrl && *pctrl != id) pctrl++;
712                 return *pctrl ? id : 0;
713         }
714         id &= V4L2_CTRL_ID_MASK;
715         id++;   /* select next control */
716         /* find first class that matches (or is greater than) the class of
717            the ID */
718         while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
719                 ctrl_classes++;
720         /* no more classes */
721         if (*ctrl_classes == NULL)
722                 return 0;
723         pctrl = *ctrl_classes;
724         /* find first ctrl within the class that is >= ID */
725         while (*pctrl && *pctrl < id) pctrl++;
726         if (*pctrl)
727                 return *pctrl;
728         /* we are at the end of the controls of the current class. */
729         /* continue with next class if available */
730         ctrl_classes++;
731         if (*ctrl_classes == NULL)
732                 return 0;
733         return **ctrl_classes;
734 }
735 EXPORT_SYMBOL(v4l2_ctrl_next);
736
737 int v4l2_chip_match_host(u32 match_type, u32 match_chip)
738 {
739         switch (match_type) {
740         case V4L2_CHIP_MATCH_HOST:
741                 return match_chip == 0;
742         default:
743                 return 0;
744         }
745 }
746 EXPORT_SYMBOL(v4l2_chip_match_host);
747
748 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
749 int v4l2_chip_match_i2c_client(struct i2c_client *c, u32 match_type, u32 match_chip)
750 {
751         switch (match_type) {
752         case V4L2_CHIP_MATCH_I2C_DRIVER:
753                 return (c != NULL && c->driver != NULL && c->driver->id == match_chip);
754         case V4L2_CHIP_MATCH_I2C_ADDR:
755                 return (c != NULL && c->addr == match_chip);
756         default:
757                 return 0;
758         }
759 }
760 EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
761
762 int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_chip_ident *chip,
763                 u32 ident, u32 revision)
764 {
765         if (!v4l2_chip_match_i2c_client(c, chip->match_type, chip->match_chip))
766                 return 0;
767         if (chip->ident == V4L2_IDENT_NONE) {
768                 chip->ident = ident;
769                 chip->revision = revision;
770         }
771         else {
772                 chip->ident = V4L2_IDENT_AMBIGUOUS;
773                 chip->revision = 0;
774         }
775         return 0;
776 }
777 EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
778
779 /* ----------------------------------------------------------------- */
780
781 /* Helper function for I2C legacy drivers */
782
783 int v4l2_i2c_attach(struct i2c_adapter *adapter, int address, struct i2c_driver *driver,
784                 const char *name,
785                 int (*probe)(struct i2c_client *, const struct i2c_device_id *))
786 {
787         struct i2c_client *client;
788         int err;
789
790         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
791         if (!client)
792                 return -ENOMEM;
793
794         client->addr = address;
795         client->adapter = adapter;
796         client->driver = driver;
797         strlcpy(client->name, name, sizeof(client->name));
798
799         err = probe(client, NULL);
800         if (err == 0) {
801                 i2c_attach_client(client);
802         } else {
803                 kfree(client);
804         }
805         return err != -ENOMEM ? 0 : err;
806 }
807 EXPORT_SYMBOL(v4l2_i2c_attach);
808 #endif