]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/video/bttv-driver.c
058b923b4b1360bc3278189d9316b5fcfe561d99
[karo-tx-linux.git] / drivers / media / video / bttv-driver.c
1 /*
2
3     bttv - Bt848 frame grabber driver
4
5     Copyright (C) 1996,97,98 Ralph  Metzler <rjkm@thp.uni-koeln.de>
6                            & Marcus Metzler <mocm@thp.uni-koeln.de>
7     (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
8
9     some v4l2 code lines are taken from Justin's bttv2 driver which is
10     (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
11
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
32 #include <linux/fs.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
37 #include <linux/dma-mapping.h>
38
39 #include <asm/io.h>
40 #include <asm/byteorder.h>
41
42 #include "bttvp.h"
43
44 #include "rds.h"
45
46
47 unsigned int bttv_num;                  /* number of Bt848s in use */
48 struct bttv bttvs[BTTV_MAX];
49
50 unsigned int bttv_debug = 0;
51 unsigned int bttv_verbose = 1;
52 unsigned int bttv_gpio = 0;
53
54 /* config variables */
55 #ifdef __BIG_ENDIAN
56 static unsigned int bigendian=1;
57 #else
58 static unsigned int bigendian=0;
59 #endif
60 static unsigned int radio[BTTV_MAX];
61 static unsigned int irq_debug = 0;
62 static unsigned int gbuffers = 8;
63 static unsigned int gbufsize = 0x208000;
64
65 static int video_nr = -1;
66 static int radio_nr = -1;
67 static int vbi_nr = -1;
68 static int debug_latency = 0;
69
70 static unsigned int fdsr = 0;
71
72 /* options */
73 static unsigned int combfilter  = 0;
74 static unsigned int lumafilter  = 0;
75 static unsigned int automute    = 1;
76 static unsigned int chroma_agc  = 0;
77 static unsigned int adc_crush   = 1;
78 static unsigned int whitecrush_upper = 0xCF;
79 static unsigned int whitecrush_lower = 0x7F;
80 static unsigned int vcr_hack    = 0;
81 static unsigned int irq_iswitch = 0;
82 static unsigned int uv_ratio    = 50;
83 static unsigned int full_luma_range = 0;
84 static unsigned int coring      = 0;
85 extern int no_overlay;
86
87 /* API features (turn on/off stuff for testing) */
88 static unsigned int v4l2        = 1;
89
90
91 /* insmod args */
92 module_param(bttv_verbose,      int, 0644);
93 module_param(bttv_gpio,         int, 0644);
94 module_param(bttv_debug,        int, 0644);
95 module_param(irq_debug,         int, 0644);
96 module_param(debug_latency,     int, 0644);
97
98 module_param(fdsr,              int, 0444);
99 module_param(video_nr,          int, 0444);
100 module_param(radio_nr,          int, 0444);
101 module_param(vbi_nr,            int, 0444);
102 module_param(gbuffers,          int, 0444);
103 module_param(gbufsize,          int, 0444);
104
105 module_param(v4l2,              int, 0644);
106 module_param(bigendian,         int, 0644);
107 module_param(irq_iswitch,       int, 0644);
108 module_param(combfilter,        int, 0444);
109 module_param(lumafilter,        int, 0444);
110 module_param(automute,          int, 0444);
111 module_param(chroma_agc,        int, 0444);
112 module_param(adc_crush,         int, 0444);
113 module_param(whitecrush_upper,  int, 0444);
114 module_param(whitecrush_lower,  int, 0444);
115 module_param(vcr_hack,          int, 0444);
116 module_param(uv_ratio,          int, 0444);
117 module_param(full_luma_range,   int, 0444);
118 module_param(coring,            int, 0444);
119
120 module_param_array(radio, int, NULL, 0444);
121
122 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
123 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
124 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
125 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
126 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
127 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
128 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
129 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
130 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
131 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
132 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
133 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
134 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
135 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
136 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
137 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
138 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
139 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
140
141 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
142 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
143 MODULE_LICENSE("GPL");
144
145 /* ----------------------------------------------------------------------- */
146 /* sysfs                                                                   */
147
148 static ssize_t show_card(struct class_device *cd, char *buf)
149 {
150         struct video_device *vfd = to_video_device(cd);
151         struct bttv *btv = dev_get_drvdata(vfd->dev);
152         return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
153 }
154 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
155
156 /* ----------------------------------------------------------------------- */
157 /* static data                                                             */
158
159 /* special timing tables from conexant... */
160 static u8 SRAM_Table[][60] =
161 {
162         /* PAL digital input over GPIO[7:0] */
163         {
164                 45, // 45 bytes following
165                 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
166                 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
167                 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
168                 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
169                 0x37,0x00,0xAF,0x21,0x00
170         },
171         /* NTSC digital input over GPIO[7:0] */
172         {
173                 51, // 51 bytes following
174                 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
175                 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
176                 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
177                 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
178                 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
179                 0x00,
180         },
181         // TGB_NTSC392 // quartzsight
182         // This table has been modified to be used for Fusion Rev D
183         {
184                 0x2A, // size of table = 42
185                 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
186                 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
187                 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
188                 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
189                 0x20, 0x00
190         }
191 };
192
193 const struct bttv_tvnorm bttv_tvnorms[] = {
194         /* PAL-BDGHI */
195         /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
196         /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
197         {
198                 .v4l2_id        = V4L2_STD_PAL,
199                 .name           = "PAL",
200                 .Fsc            = 35468950,
201                 .swidth         = 924,
202                 .sheight        = 576,
203                 .totalwidth     = 1135,
204                 .adelay         = 0x7f,
205                 .bdelay         = 0x72,
206                 .iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
207                 .scaledtwidth   = 1135,
208                 .hdelayx1       = 186,
209                 .hactivex1      = 924,
210                 .vdelay         = 0x20,
211                 .vbipack        = 255,
212                 .sram           = 0,
213         },{
214                 .v4l2_id        = V4L2_STD_NTSC_M,
215                 .name           = "NTSC",
216                 .Fsc            = 28636363,
217                 .swidth         = 768,
218                 .sheight        = 480,
219                 .totalwidth     = 910,
220                 .adelay         = 0x68,
221                 .bdelay         = 0x5d,
222                 .iform          = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
223                 .scaledtwidth   = 910,
224                 .hdelayx1       = 128,
225                 .hactivex1      = 910,
226                 .vdelay         = 0x1a,
227                 .vbipack        = 144,
228                 .sram           = 1,
229         },{
230                 .v4l2_id        = V4L2_STD_SECAM,
231                 .name           = "SECAM",
232                 .Fsc            = 35468950,
233                 .swidth         = 924,
234                 .sheight        = 576,
235                 .totalwidth     = 1135,
236                 .adelay         = 0x7f,
237                 .bdelay         = 0xb0,
238                 .iform          = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
239                 .scaledtwidth   = 1135,
240                 .hdelayx1       = 186,
241                 .hactivex1      = 922,
242                 .vdelay         = 0x20,
243                 .vbipack        = 255,
244                 .sram           = 0, /* like PAL, correct? */
245         },{
246                 .v4l2_id        = V4L2_STD_PAL_Nc,
247                 .name           = "PAL-Nc",
248                 .Fsc            = 28636363,
249                 .swidth         = 640,
250                 .sheight        = 576,
251                 .totalwidth     = 910,
252                 .adelay         = 0x68,
253                 .bdelay         = 0x5d,
254                 .iform          = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
255                 .scaledtwidth   = 780,
256                 .hdelayx1       = 130,
257                 .hactivex1      = 734,
258                 .vdelay         = 0x1a,
259                 .vbipack        = 144,
260                 .sram           = -1,
261         },{
262                 .v4l2_id        = V4L2_STD_PAL_M,
263                 .name           = "PAL-M",
264                 .Fsc            = 28636363,
265                 .swidth         = 640,
266                 .sheight        = 480,
267                 .totalwidth     = 910,
268                 .adelay         = 0x68,
269                 .bdelay         = 0x5d,
270                 .iform          = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
271                 .scaledtwidth   = 780,
272                 .hdelayx1       = 135,
273                 .hactivex1      = 754,
274                 .vdelay         = 0x1a,
275                 .vbipack        = 144,
276                 .sram           = -1,
277         },{
278                 .v4l2_id        = V4L2_STD_PAL_N,
279                 .name           = "PAL-N",
280                 .Fsc            = 35468950,
281                 .swidth         = 768,
282                 .sheight        = 576,
283                 .totalwidth     = 1135,
284                 .adelay         = 0x7f,
285                 .bdelay         = 0x72,
286                 .iform          = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
287                 .scaledtwidth   = 944,
288                 .hdelayx1       = 186,
289                 .hactivex1      = 922,
290                 .vdelay         = 0x20,
291                 .vbipack        = 144,
292                 .sram           = -1,
293         },{
294                 .v4l2_id        = V4L2_STD_NTSC_M_JP,
295                 .name           = "NTSC-JP",
296                 .Fsc            = 28636363,
297                 .swidth         = 640,
298                 .sheight        = 480,
299                 .totalwidth     = 910,
300                 .adelay         = 0x68,
301                 .bdelay         = 0x5d,
302                 .iform          = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
303                 .scaledtwidth   = 780,
304                 .hdelayx1       = 135,
305                 .hactivex1      = 754,
306                 .vdelay         = 0x16,
307                 .vbipack        = 144,
308                 .sram           = -1,
309         },{
310                 /* that one hopefully works with the strange timing
311                  * which video recorders produce when playing a NTSC
312                  * tape on a PAL TV ... */
313                 .v4l2_id        = V4L2_STD_PAL_60,
314                 .name           = "PAL-60",
315                 .Fsc            = 35468950,
316                 .swidth         = 924,
317                 .sheight        = 480,
318                 .totalwidth     = 1135,
319                 .adelay         = 0x7f,
320                 .bdelay         = 0x72,
321                 .iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
322                 .scaledtwidth   = 1135,
323                 .hdelayx1       = 186,
324                 .hactivex1      = 924,
325                 .vdelay         = 0x1a,
326                 .vbipack        = 255,
327                 .vtotal         = 524,
328                 .sram           = -1,
329         }
330 };
331 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
332
333 /* ----------------------------------------------------------------------- */
334 /* bttv format list
335    packed pixel formats must come first */
336 static const struct bttv_format bttv_formats[] = {
337         {
338                 .name     = "8 bpp, gray",
339                 .palette  = VIDEO_PALETTE_GREY,
340                 .fourcc   = V4L2_PIX_FMT_GREY,
341                 .btformat = BT848_COLOR_FMT_Y8,
342                 .depth    = 8,
343                 .flags    = FORMAT_FLAGS_PACKED,
344         },{
345                 .name     = "8 bpp, dithered color",
346                 .palette  = VIDEO_PALETTE_HI240,
347                 .fourcc   = V4L2_PIX_FMT_HI240,
348                 .btformat = BT848_COLOR_FMT_RGB8,
349                 .depth    = 8,
350                 .flags    = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
351         },{
352                 .name     = "15 bpp RGB, le",
353                 .palette  = VIDEO_PALETTE_RGB555,
354                 .fourcc   = V4L2_PIX_FMT_RGB555,
355                 .btformat = BT848_COLOR_FMT_RGB15,
356                 .depth    = 16,
357                 .flags    = FORMAT_FLAGS_PACKED,
358         },{
359                 .name     = "15 bpp RGB, be",
360                 .palette  = -1,
361                 .fourcc   = V4L2_PIX_FMT_RGB555X,
362                 .btformat = BT848_COLOR_FMT_RGB15,
363                 .btswap   = 0x03, /* byteswap */
364                 .depth    = 16,
365                 .flags    = FORMAT_FLAGS_PACKED,
366         },{
367                 .name     = "16 bpp RGB, le",
368                 .palette  = VIDEO_PALETTE_RGB565,
369                 .fourcc   = V4L2_PIX_FMT_RGB565,
370                 .btformat = BT848_COLOR_FMT_RGB16,
371                 .depth    = 16,
372                 .flags    = FORMAT_FLAGS_PACKED,
373         },{
374                 .name     = "16 bpp RGB, be",
375                 .palette  = -1,
376                 .fourcc   = V4L2_PIX_FMT_RGB565X,
377                 .btformat = BT848_COLOR_FMT_RGB16,
378                 .btswap   = 0x03, /* byteswap */
379                 .depth    = 16,
380                 .flags    = FORMAT_FLAGS_PACKED,
381         },{
382                 .name     = "24 bpp RGB, le",
383                 .palette  = VIDEO_PALETTE_RGB24,
384                 .fourcc   = V4L2_PIX_FMT_BGR24,
385                 .btformat = BT848_COLOR_FMT_RGB24,
386                 .depth    = 24,
387                 .flags    = FORMAT_FLAGS_PACKED,
388         },{
389                 .name     = "32 bpp RGB, le",
390                 .palette  = VIDEO_PALETTE_RGB32,
391                 .fourcc   = V4L2_PIX_FMT_BGR32,
392                 .btformat = BT848_COLOR_FMT_RGB32,
393                 .depth    = 32,
394                 .flags    = FORMAT_FLAGS_PACKED,
395         },{
396                 .name     = "32 bpp RGB, be",
397                 .palette  = -1,
398                 .fourcc   = V4L2_PIX_FMT_RGB32,
399                 .btformat = BT848_COLOR_FMT_RGB32,
400                 .btswap   = 0x0f, /* byte+word swap */
401                 .depth    = 32,
402                 .flags    = FORMAT_FLAGS_PACKED,
403         },{
404                 .name     = "4:2:2, packed, YUYV",
405                 .palette  = VIDEO_PALETTE_YUV422,
406                 .fourcc   = V4L2_PIX_FMT_YUYV,
407                 .btformat = BT848_COLOR_FMT_YUY2,
408                 .depth    = 16,
409                 .flags    = FORMAT_FLAGS_PACKED,
410         },{
411                 .name     = "4:2:2, packed, YUYV",
412                 .palette  = VIDEO_PALETTE_YUYV,
413                 .fourcc   = V4L2_PIX_FMT_YUYV,
414                 .btformat = BT848_COLOR_FMT_YUY2,
415                 .depth    = 16,
416                 .flags    = FORMAT_FLAGS_PACKED,
417         },{
418                 .name     = "4:2:2, packed, UYVY",
419                 .palette  = VIDEO_PALETTE_UYVY,
420                 .fourcc   = V4L2_PIX_FMT_UYVY,
421                 .btformat = BT848_COLOR_FMT_YUY2,
422                 .btswap   = 0x03, /* byteswap */
423                 .depth    = 16,
424                 .flags    = FORMAT_FLAGS_PACKED,
425         },{
426                 .name     = "4:2:2, planar, Y-Cb-Cr",
427                 .palette  = VIDEO_PALETTE_YUV422P,
428                 .fourcc   = V4L2_PIX_FMT_YUV422P,
429                 .btformat = BT848_COLOR_FMT_YCrCb422,
430                 .depth    = 16,
431                 .flags    = FORMAT_FLAGS_PLANAR,
432                 .hshift   = 1,
433                 .vshift   = 0,
434         },{
435                 .name     = "4:2:0, planar, Y-Cb-Cr",
436                 .palette  = VIDEO_PALETTE_YUV420P,
437                 .fourcc   = V4L2_PIX_FMT_YUV420,
438                 .btformat = BT848_COLOR_FMT_YCrCb422,
439                 .depth    = 12,
440                 .flags    = FORMAT_FLAGS_PLANAR,
441                 .hshift   = 1,
442                 .vshift   = 1,
443         },{
444                 .name     = "4:2:0, planar, Y-Cr-Cb",
445                 .palette  = -1,
446                 .fourcc   = V4L2_PIX_FMT_YVU420,
447                 .btformat = BT848_COLOR_FMT_YCrCb422,
448                 .depth    = 12,
449                 .flags    = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
450                 .hshift   = 1,
451                 .vshift   = 1,
452         },{
453                 .name     = "4:1:1, planar, Y-Cb-Cr",
454                 .palette  = VIDEO_PALETTE_YUV411P,
455                 .fourcc   = V4L2_PIX_FMT_YUV411P,
456                 .btformat = BT848_COLOR_FMT_YCrCb411,
457                 .depth    = 12,
458                 .flags    = FORMAT_FLAGS_PLANAR,
459                 .hshift   = 2,
460                 .vshift   = 0,
461         },{
462                 .name     = "4:1:0, planar, Y-Cb-Cr",
463                 .palette  = VIDEO_PALETTE_YUV410P,
464                 .fourcc   = V4L2_PIX_FMT_YUV410,
465                 .btformat = BT848_COLOR_FMT_YCrCb411,
466                 .depth    = 9,
467                 .flags    = FORMAT_FLAGS_PLANAR,
468                 .hshift   = 2,
469                 .vshift   = 2,
470         },{
471                 .name     = "4:1:0, planar, Y-Cr-Cb",
472                 .palette  = -1,
473                 .fourcc   = V4L2_PIX_FMT_YVU410,
474                 .btformat = BT848_COLOR_FMT_YCrCb411,
475                 .depth    = 9,
476                 .flags    = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
477                 .hshift   = 2,
478                 .vshift   = 2,
479         },{
480                 .name     = "raw scanlines",
481                 .palette  = VIDEO_PALETTE_RAW,
482                 .fourcc   = -1,
483                 .btformat = BT848_COLOR_FMT_RAW,
484                 .depth    = 8,
485                 .flags    = FORMAT_FLAGS_RAW,
486         }
487 };
488 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
489
490 /* ----------------------------------------------------------------------- */
491
492 #define V4L2_CID_PRIVATE_CHROMA_AGC  (V4L2_CID_PRIVATE_BASE + 0)
493 #define V4L2_CID_PRIVATE_COMBFILTER  (V4L2_CID_PRIVATE_BASE + 1)
494 #define V4L2_CID_PRIVATE_AUTOMUTE    (V4L2_CID_PRIVATE_BASE + 2)
495 #define V4L2_CID_PRIVATE_LUMAFILTER  (V4L2_CID_PRIVATE_BASE + 3)
496 #define V4L2_CID_PRIVATE_AGC_CRUSH   (V4L2_CID_PRIVATE_BASE + 4)
497 #define V4L2_CID_PRIVATE_VCR_HACK    (V4L2_CID_PRIVATE_BASE + 5)
498 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER   (V4L2_CID_PRIVATE_BASE + 6)
499 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER   (V4L2_CID_PRIVATE_BASE + 7)
500 #define V4L2_CID_PRIVATE_UV_RATIO    (V4L2_CID_PRIVATE_BASE + 8)
501 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE    (V4L2_CID_PRIVATE_BASE + 9)
502 #define V4L2_CID_PRIVATE_CORING      (V4L2_CID_PRIVATE_BASE + 10)
503 #define V4L2_CID_PRIVATE_LASTP1      (V4L2_CID_PRIVATE_BASE + 11)
504
505 static const struct v4l2_queryctrl no_ctl = {
506         .name  = "42",
507         .flags = V4L2_CTRL_FLAG_DISABLED,
508 };
509 static const struct v4l2_queryctrl bttv_ctls[] = {
510         /* --- video --- */
511         {
512                 .id            = V4L2_CID_BRIGHTNESS,
513                 .name          = "Brightness",
514                 .minimum       = 0,
515                 .maximum       = 65535,
516                 .step          = 256,
517                 .default_value = 32768,
518                 .type          = V4L2_CTRL_TYPE_INTEGER,
519         },{
520                 .id            = V4L2_CID_CONTRAST,
521                 .name          = "Contrast",
522                 .minimum       = 0,
523                 .maximum       = 65535,
524                 .step          = 128,
525                 .default_value = 32768,
526                 .type          = V4L2_CTRL_TYPE_INTEGER,
527         },{
528                 .id            = V4L2_CID_SATURATION,
529                 .name          = "Saturation",
530                 .minimum       = 0,
531                 .maximum       = 65535,
532                 .step          = 128,
533                 .default_value = 32768,
534                 .type          = V4L2_CTRL_TYPE_INTEGER,
535         },{
536                 .id            = V4L2_CID_HUE,
537                 .name          = "Hue",
538                 .minimum       = 0,
539                 .maximum       = 65535,
540                 .step          = 256,
541                 .default_value = 32768,
542                 .type          = V4L2_CTRL_TYPE_INTEGER,
543         },
544         /* --- audio --- */
545         {
546                 .id            = V4L2_CID_AUDIO_MUTE,
547                 .name          = "Mute",
548                 .minimum       = 0,
549                 .maximum       = 1,
550                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
551         },{
552                 .id            = V4L2_CID_AUDIO_VOLUME,
553                 .name          = "Volume",
554                 .minimum       = 0,
555                 .maximum       = 65535,
556                 .step          = 65535/100,
557                 .default_value = 65535,
558                 .type          = V4L2_CTRL_TYPE_INTEGER,
559         },{
560                 .id            = V4L2_CID_AUDIO_BALANCE,
561                 .name          = "Balance",
562                 .minimum       = 0,
563                 .maximum       = 65535,
564                 .step          = 65535/100,
565                 .default_value = 32768,
566                 .type          = V4L2_CTRL_TYPE_INTEGER,
567         },{
568                 .id            = V4L2_CID_AUDIO_BASS,
569                 .name          = "Bass",
570                 .minimum       = 0,
571                 .maximum       = 65535,
572                 .step          = 65535/100,
573                 .default_value = 32768,
574                 .type          = V4L2_CTRL_TYPE_INTEGER,
575         },{
576                 .id            = V4L2_CID_AUDIO_TREBLE,
577                 .name          = "Treble",
578                 .minimum       = 0,
579                 .maximum       = 65535,
580                 .step          = 65535/100,
581                 .default_value = 32768,
582                 .type          = V4L2_CTRL_TYPE_INTEGER,
583         },
584         /* --- private --- */
585         {
586                 .id            = V4L2_CID_PRIVATE_CHROMA_AGC,
587                 .name          = "chroma agc",
588                 .minimum       = 0,
589                 .maximum       = 1,
590                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
591         },{
592                 .id            = V4L2_CID_PRIVATE_COMBFILTER,
593                 .name          = "combfilter",
594                 .minimum       = 0,
595                 .maximum       = 1,
596                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
597         },{
598                 .id            = V4L2_CID_PRIVATE_AUTOMUTE,
599                 .name          = "automute",
600                 .minimum       = 0,
601                 .maximum       = 1,
602                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
603         },{
604                 .id            = V4L2_CID_PRIVATE_LUMAFILTER,
605                 .name          = "luma decimation filter",
606                 .minimum       = 0,
607                 .maximum       = 1,
608                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
609         },{
610                 .id            = V4L2_CID_PRIVATE_AGC_CRUSH,
611                 .name          = "agc crush",
612                 .minimum       = 0,
613                 .maximum       = 1,
614                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
615         },{
616                 .id            = V4L2_CID_PRIVATE_VCR_HACK,
617                 .name          = "vcr hack",
618                 .minimum       = 0,
619                 .maximum       = 1,
620                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
621         },{
622                 .id            = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
623                 .name          = "whitecrush upper",
624                 .minimum       = 0,
625                 .maximum       = 255,
626                 .step          = 1,
627                 .default_value = 0xCF,
628                 .type          = V4L2_CTRL_TYPE_INTEGER,
629         },{
630                 .id            = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
631                 .name          = "whitecrush lower",
632                 .minimum       = 0,
633                 .maximum       = 255,
634                 .step          = 1,
635                 .default_value = 0x7F,
636                 .type          = V4L2_CTRL_TYPE_INTEGER,
637         },{
638                 .id            = V4L2_CID_PRIVATE_UV_RATIO,
639                 .name          = "uv ratio",
640                 .minimum       = 0,
641                 .maximum       = 100,
642                 .step          = 1,
643                 .default_value = 50,
644                 .type          = V4L2_CTRL_TYPE_INTEGER,
645         },{
646                 .id            = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
647                 .name          = "full luma range",
648                 .minimum       = 0,
649                 .maximum       = 1,
650                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
651         },{
652                 .id            = V4L2_CID_PRIVATE_CORING,
653                 .name          = "coring",
654                 .minimum       = 0,
655                 .maximum       = 3,
656                 .step          = 1,
657                 .default_value = 0,
658                 .type          = V4L2_CTRL_TYPE_INTEGER,
659         }
660
661
662
663 };
664 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
665
666 /* ----------------------------------------------------------------------- */
667 /* resource management                                                     */
668
669 static
670 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
671 {
672         if (fh->resources & bit)
673                 /* have it already allocated */
674                 return 1;
675
676         /* is it free? */
677         down(&btv->reslock);
678         if (btv->resources & bit) {
679                 /* no, someone else uses it */
680                 up(&btv->reslock);
681                 return 0;
682         }
683         /* it's free, grab it */
684         fh->resources  |= bit;
685         btv->resources |= bit;
686         up(&btv->reslock);
687         return 1;
688 }
689
690 static
691 int check_btres(struct bttv_fh *fh, int bit)
692 {
693         return (fh->resources & bit);
694 }
695
696 static
697 int locked_btres(struct bttv *btv, int bit)
698 {
699         return (btv->resources & bit);
700 }
701
702 static
703 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
704 {
705         if ((fh->resources & bits) != bits) {
706                 /* trying to free ressources not allocated by us ... */
707                 printk("bttv: BUG! (btres)\n");
708         }
709         down(&btv->reslock);
710         fh->resources  &= ~bits;
711         btv->resources &= ~bits;
712         up(&btv->reslock);
713 }
714
715 /* ----------------------------------------------------------------------- */
716 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC          */
717
718 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
719    PLL_X = Reference pre-divider (0=1, 1=2)
720    PLL_C = Post divider (0=6, 1=4)
721    PLL_I = Integer input
722    PLL_F = Fractional input
723
724    F_input = 28.636363 MHz:
725    PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
726 */
727
728 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
729 {
730         unsigned char fl, fh, fi;
731
732         /* prevent overflows */
733         fin/=4;
734         fout/=4;
735
736         fout*=12;
737         fi=fout/fin;
738
739         fout=(fout%fin)*256;
740         fh=fout/fin;
741
742         fout=(fout%fin)*256;
743         fl=fout/fin;
744
745         btwrite(fl, BT848_PLL_F_LO);
746         btwrite(fh, BT848_PLL_F_HI);
747         btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
748 }
749
750 static void set_pll(struct bttv *btv)
751 {
752         int i;
753
754         if (!btv->pll.pll_crystal)
755                 return;
756
757         if (btv->pll.pll_ofreq == btv->pll.pll_current) {
758                 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
759                 return;
760         }
761
762         if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
763                 /* no PLL needed */
764                 if (btv->pll.pll_current == 0)
765                         return;
766                 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
767                            btv->c.nr,btv->pll.pll_ifreq);
768                 btwrite(0x00,BT848_TGCTRL);
769                 btwrite(0x00,BT848_PLL_XCI);
770                 btv->pll.pll_current = 0;
771                 return;
772         }
773
774         bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
775                    btv->pll.pll_ifreq, btv->pll.pll_ofreq);
776         set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
777
778         for (i=0; i<10; i++) {
779                 /*  Let other people run while the PLL stabilizes */
780                 bttv_printk(".");
781                 msleep(10);
782
783                 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
784                         btwrite(0,BT848_DSTATUS);
785                 } else {
786                         btwrite(0x08,BT848_TGCTRL);
787                         btv->pll.pll_current = btv->pll.pll_ofreq;
788                         bttv_printk(" ok\n");
789                         return;
790                 }
791         }
792         btv->pll.pll_current = -1;
793         bttv_printk("failed\n");
794         return;
795 }
796
797 /* used to switch between the bt848's analog/digital video capture modes */
798 static void bt848A_set_timing(struct bttv *btv)
799 {
800         int i, len;
801         int table_idx = bttv_tvnorms[btv->tvnorm].sram;
802         int fsc       = bttv_tvnorms[btv->tvnorm].Fsc;
803
804         if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
805                 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
806                         btv->c.nr,table_idx);
807
808                 /* timing change...reset timing generator address */
809                 btwrite(0x00, BT848_TGCTRL);
810                 btwrite(0x02, BT848_TGCTRL);
811                 btwrite(0x00, BT848_TGCTRL);
812
813                 len=SRAM_Table[table_idx][0];
814                 for(i = 1; i <= len; i++)
815                         btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
816                 btv->pll.pll_ofreq = 27000000;
817
818                 set_pll(btv);
819                 btwrite(0x11, BT848_TGCTRL);
820                 btwrite(0x41, BT848_DVSIF);
821         } else {
822                 btv->pll.pll_ofreq = fsc;
823                 set_pll(btv);
824                 btwrite(0x0, BT848_DVSIF);
825         }
826 }
827
828 /* ----------------------------------------------------------------------- */
829
830 static void bt848_bright(struct bttv *btv, int bright)
831 {
832         int value;
833
834         // printk("bttv: set bright: %d\n",bright); // DEBUG
835         btv->bright = bright;
836
837         /* We want -128 to 127 we get 0-65535 */
838         value = (bright >> 8) - 128;
839         btwrite(value & 0xff, BT848_BRIGHT);
840 }
841
842 static void bt848_hue(struct bttv *btv, int hue)
843 {
844         int value;
845
846         btv->hue = hue;
847
848         /* -128 to 127 */
849         value = (hue >> 8) - 128;
850         btwrite(value & 0xff, BT848_HUE);
851 }
852
853 static void bt848_contrast(struct bttv *btv, int cont)
854 {
855         int value,hibit;
856
857         btv->contrast = cont;
858
859         /* 0-511 */
860         value = (cont  >> 7);
861         hibit = (value >> 6) & 4;
862         btwrite(value & 0xff, BT848_CONTRAST_LO);
863         btaor(hibit, ~4, BT848_E_CONTROL);
864         btaor(hibit, ~4, BT848_O_CONTROL);
865 }
866
867 static void bt848_sat(struct bttv *btv, int color)
868 {
869         int val_u,val_v,hibits;
870
871         btv->saturation = color;
872
873         /* 0-511 for the color */
874         val_u   = ((color * btv->opt_uv_ratio) / 50) >> 7;
875         val_v   = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
876         hibits  = (val_u >> 7) & 2;
877         hibits |= (val_v >> 8) & 1;
878         btwrite(val_u & 0xff, BT848_SAT_U_LO);
879         btwrite(val_v & 0xff, BT848_SAT_V_LO);
880         btaor(hibits, ~3, BT848_E_CONTROL);
881         btaor(hibits, ~3, BT848_O_CONTROL);
882 }
883
884 /* ----------------------------------------------------------------------- */
885
886 static int
887 video_mux(struct bttv *btv, unsigned int input)
888 {
889         int mux,mask2;
890
891         if (input >= bttv_tvcards[btv->c.type].video_inputs)
892                 return -EINVAL;
893
894         /* needed by RemoteVideo MX */
895         mask2 = bttv_tvcards[btv->c.type].gpiomask2;
896         if (mask2)
897                 gpio_inout(mask2,mask2);
898
899         if (input == btv->svhs)  {
900                 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
901                 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
902         } else {
903                 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
904                 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
905         }
906         mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
907         btaor(mux<<5, ~(3<<5), BT848_IFORM);
908         dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
909                 btv->c.nr,input,mux);
910
911         /* card specific hook */
912         if(bttv_tvcards[btv->c.type].muxsel_hook)
913                 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
914         return 0;
915 }
916
917 static char *audio_modes[] = {
918         "audio: tuner", "audio: radio", "audio: extern",
919         "audio: intern", "audio: off"
920 };
921
922 static int
923 audio_mux(struct bttv *btv, int mode)
924 {
925         int val,mux,i2c_mux,signal;
926
927         gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
928                    bttv_tvcards[btv->c.type].gpiomask);
929         signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
930
931         switch (mode) {
932         case AUDIO_MUTE:
933                 btv->audio |= AUDIO_MUTE;
934                 break;
935         case AUDIO_UNMUTE:
936                 btv->audio &= ~AUDIO_MUTE;
937                 break;
938         case AUDIO_TUNER:
939         case AUDIO_RADIO:
940         case AUDIO_EXTERN:
941         case AUDIO_INTERN:
942                 btv->audio &= AUDIO_MUTE;
943                 btv->audio |= mode;
944         }
945         i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
946         if (btv->opt_automute && !signal && !btv->radio_user)
947                 mux = AUDIO_OFF;
948
949         val = bttv_tvcards[btv->c.type].audiomux[mux];
950         gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
951         if (bttv_gpio)
952                 bttv_gpio_tracking(btv,audio_modes[mux]);
953         if (!in_interrupt())
954                 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
955         return 0;
956 }
957
958 static void
959 i2c_vidiocschan(struct bttv *btv)
960 {
961         struct video_channel c;
962
963         memset(&c,0,sizeof(c));
964         c.norm    = btv->tvnorm;
965         c.channel = btv->input;
966         bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
967         if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
968                 bttv_tda9880_setnorm(btv,c.norm);
969 }
970
971 static int
972 set_tvnorm(struct bttv *btv, unsigned int norm)
973 {
974         const struct bttv_tvnorm *tvnorm;
975
976         if (norm < 0 || norm >= BTTV_TVNORMS)
977                 return -EINVAL;
978
979         btv->tvnorm = norm;
980         tvnorm = &bttv_tvnorms[norm];
981
982         btwrite(tvnorm->adelay, BT848_ADELAY);
983         btwrite(tvnorm->bdelay, BT848_BDELAY);
984         btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
985               BT848_IFORM);
986         btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
987         btwrite(1, BT848_VBI_PACK_DEL);
988         bt848A_set_timing(btv);
989
990         switch (btv->c.type) {
991         case BTTV_BOARD_VOODOOTV_FM:
992                 bttv_tda9880_setnorm(btv,norm);
993                 break;
994         }
995         return 0;
996 }
997
998 static void
999 set_input(struct bttv *btv, unsigned int input)
1000 {
1001         unsigned long flags;
1002
1003         btv->input = input;
1004         if (irq_iswitch) {
1005                 spin_lock_irqsave(&btv->s_lock,flags);
1006                 if (btv->curr.frame_irq) {
1007                         /* active capture -> delayed input switch */
1008                         btv->new_input = input;
1009                 } else {
1010                         video_mux(btv,input);
1011                 }
1012                 spin_unlock_irqrestore(&btv->s_lock,flags);
1013         } else {
1014                 video_mux(btv,input);
1015         }
1016         audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1017                        AUDIO_TUNER : AUDIO_EXTERN));
1018         set_tvnorm(btv,btv->tvnorm);
1019         i2c_vidiocschan(btv);
1020 }
1021
1022 static void init_irqreg(struct bttv *btv)
1023 {
1024         /* clear status */
1025         btwrite(0xfffffUL, BT848_INT_STAT);
1026
1027         if (bttv_tvcards[btv->c.type].no_video) {
1028                 /* i2c only */
1029                 btwrite(BT848_INT_I2CDONE,
1030                         BT848_INT_MASK);
1031         } else {
1032                 /* full video */
1033                 btwrite((btv->triton1)  |
1034                         (btv->gpioirq ? BT848_INT_GPINT : 0) |
1035                         BT848_INT_SCERR |
1036                         (fdsr ? BT848_INT_FDSR : 0) |
1037                         BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1038                         BT848_INT_FMTCHG|BT848_INT_HLOCK|
1039                         BT848_INT_I2CDONE,
1040                         BT848_INT_MASK);
1041         }
1042 }
1043
1044 static void init_bt848(struct bttv *btv)
1045 {
1046         int val;
1047
1048         if (bttv_tvcards[btv->c.type].no_video) {
1049                 /* very basic init only */
1050                 init_irqreg(btv);
1051                 return;
1052         }
1053
1054         btwrite(0x00, BT848_CAP_CTL);
1055         btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1056         btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1057
1058         /* set planar and packed mode trigger points and         */
1059         /* set rising edge of inverted GPINTR pin as irq trigger */
1060         btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1061                 BT848_GPIO_DMA_CTL_PLTP1_16|
1062                 BT848_GPIO_DMA_CTL_PLTP23_16|
1063                 BT848_GPIO_DMA_CTL_GPINTC|
1064                 BT848_GPIO_DMA_CTL_GPINTI,
1065                 BT848_GPIO_DMA_CTL);
1066
1067         val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1068         btwrite(val, BT848_E_SCLOOP);
1069         btwrite(val, BT848_O_SCLOOP);
1070
1071         btwrite(0x20, BT848_E_VSCALE_HI);
1072         btwrite(0x20, BT848_O_VSCALE_HI);
1073         btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1074                 BT848_ADC);
1075
1076         btwrite(whitecrush_upper, BT848_WC_UP);
1077         btwrite(whitecrush_lower, BT848_WC_DOWN);
1078
1079         if (btv->opt_lumafilter) {
1080                 btwrite(0, BT848_E_CONTROL);
1081                 btwrite(0, BT848_O_CONTROL);
1082         } else {
1083                 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1084                 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1085         }
1086
1087         bt848_bright(btv,   btv->bright);
1088         bt848_hue(btv,      btv->hue);
1089         bt848_contrast(btv, btv->contrast);
1090         bt848_sat(btv,      btv->saturation);
1091
1092         /* interrupt */
1093         init_irqreg(btv);
1094 }
1095
1096 static void bttv_reinit_bt848(struct bttv *btv)
1097 {
1098         unsigned long flags;
1099
1100         if (bttv_verbose)
1101                 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1102         spin_lock_irqsave(&btv->s_lock,flags);
1103         btv->errors=0;
1104         bttv_set_dma(btv,0);
1105         spin_unlock_irqrestore(&btv->s_lock,flags);
1106
1107         init_bt848(btv);
1108         btv->pll.pll_current = -1;
1109         set_input(btv,btv->input);
1110 }
1111
1112 static int get_control(struct bttv *btv, struct v4l2_control *c)
1113 {
1114         struct video_audio va;
1115         int i;
1116
1117         for (i = 0; i < BTTV_CTLS; i++)
1118                 if (bttv_ctls[i].id == c->id)
1119                         break;
1120         if (i == BTTV_CTLS)
1121                 return -EINVAL;
1122         if (i >= 4 && i <= 8) {
1123                 memset(&va,0,sizeof(va));
1124                 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1125                 if (btv->audio_hook)
1126                         btv->audio_hook(btv,&va,0);
1127         }
1128         switch (c->id) {
1129         case V4L2_CID_BRIGHTNESS:
1130                 c->value = btv->bright;
1131                 break;
1132         case V4L2_CID_HUE:
1133                 c->value = btv->hue;
1134                 break;
1135         case V4L2_CID_CONTRAST:
1136                 c->value = btv->contrast;
1137                 break;
1138         case V4L2_CID_SATURATION:
1139                 c->value = btv->saturation;
1140                 break;
1141
1142         case V4L2_CID_AUDIO_MUTE:
1143                 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1144                 break;
1145         case V4L2_CID_AUDIO_VOLUME:
1146                 c->value = va.volume;
1147                 break;
1148         case V4L2_CID_AUDIO_BALANCE:
1149                 c->value = va.balance;
1150                 break;
1151         case V4L2_CID_AUDIO_BASS:
1152                 c->value = va.bass;
1153                 break;
1154         case V4L2_CID_AUDIO_TREBLE:
1155                 c->value = va.treble;
1156                 break;
1157
1158         case V4L2_CID_PRIVATE_CHROMA_AGC:
1159                 c->value = btv->opt_chroma_agc;
1160                 break;
1161         case V4L2_CID_PRIVATE_COMBFILTER:
1162                 c->value = btv->opt_combfilter;
1163                 break;
1164         case V4L2_CID_PRIVATE_LUMAFILTER:
1165                 c->value = btv->opt_lumafilter;
1166                 break;
1167         case V4L2_CID_PRIVATE_AUTOMUTE:
1168                 c->value = btv->opt_automute;
1169                 break;
1170         case V4L2_CID_PRIVATE_AGC_CRUSH:
1171                 c->value = btv->opt_adc_crush;
1172                 break;
1173         case V4L2_CID_PRIVATE_VCR_HACK:
1174                 c->value = btv->opt_vcr_hack;
1175                 break;
1176         case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1177                 c->value = btv->opt_whitecrush_upper;
1178                 break;
1179         case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1180                 c->value = btv->opt_whitecrush_lower;
1181                 break;
1182         case V4L2_CID_PRIVATE_UV_RATIO:
1183                 c->value = btv->opt_uv_ratio;
1184                 break;
1185         case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1186                 c->value = btv->opt_full_luma_range;
1187                 break;
1188         case V4L2_CID_PRIVATE_CORING:
1189                 c->value = btv->opt_coring;
1190                 break;
1191         default:
1192                 return -EINVAL;
1193         }
1194         return 0;
1195 }
1196
1197 static int set_control(struct bttv *btv, struct v4l2_control *c)
1198 {
1199         struct video_audio va;
1200         int i,val;
1201
1202         for (i = 0; i < BTTV_CTLS; i++)
1203                 if (bttv_ctls[i].id == c->id)
1204                         break;
1205         if (i == BTTV_CTLS)
1206                 return -EINVAL;
1207         if (i >= 4 && i <= 8) {
1208                 memset(&va,0,sizeof(va));
1209                 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1210                 if (btv->audio_hook)
1211                         btv->audio_hook(btv,&va,0);
1212         }
1213         switch (c->id) {
1214         case V4L2_CID_BRIGHTNESS:
1215                 bt848_bright(btv,c->value);
1216                 break;
1217         case V4L2_CID_HUE:
1218                 bt848_hue(btv,c->value);
1219                 break;
1220         case V4L2_CID_CONTRAST:
1221                 bt848_contrast(btv,c->value);
1222                 break;
1223         case V4L2_CID_SATURATION:
1224                 bt848_sat(btv,c->value);
1225                 break;
1226         case V4L2_CID_AUDIO_MUTE:
1227                 if (c->value) {
1228                         va.flags |= VIDEO_AUDIO_MUTE;
1229                         audio_mux(btv, AUDIO_MUTE);
1230                 } else {
1231                         va.flags &= ~VIDEO_AUDIO_MUTE;
1232                         audio_mux(btv, AUDIO_UNMUTE);
1233                 }
1234                 break;
1235
1236         case V4L2_CID_AUDIO_VOLUME:
1237                 va.volume = c->value;
1238                 break;
1239         case V4L2_CID_AUDIO_BALANCE:
1240                 va.balance = c->value;
1241                 break;
1242         case V4L2_CID_AUDIO_BASS:
1243                 va.bass = c->value;
1244                 break;
1245         case V4L2_CID_AUDIO_TREBLE:
1246                 va.treble = c->value;
1247                 break;
1248
1249         case V4L2_CID_PRIVATE_CHROMA_AGC:
1250                 btv->opt_chroma_agc = c->value;
1251                 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1252                 btwrite(val, BT848_E_SCLOOP);
1253                 btwrite(val, BT848_O_SCLOOP);
1254                 break;
1255         case V4L2_CID_PRIVATE_COMBFILTER:
1256                 btv->opt_combfilter = c->value;
1257                 break;
1258         case V4L2_CID_PRIVATE_LUMAFILTER:
1259                 btv->opt_lumafilter = c->value;
1260                 if (btv->opt_lumafilter) {
1261                         btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1262                         btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1263                 } else {
1264                         btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1265                         btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1266                 }
1267                 break;
1268         case V4L2_CID_PRIVATE_AUTOMUTE:
1269                 btv->opt_automute = c->value;
1270                 break;
1271         case V4L2_CID_PRIVATE_AGC_CRUSH:
1272                 btv->opt_adc_crush = c->value;
1273                 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1274                         BT848_ADC);
1275                 break;
1276         case V4L2_CID_PRIVATE_VCR_HACK:
1277                 btv->opt_vcr_hack = c->value;
1278                 break;
1279         case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1280                 btv->opt_whitecrush_upper = c->value;
1281                 btwrite(c->value, BT848_WC_UP);
1282                 break;
1283         case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1284                 btv->opt_whitecrush_lower = c->value;
1285                 btwrite(c->value, BT848_WC_DOWN);
1286                 break;
1287         case V4L2_CID_PRIVATE_UV_RATIO:
1288                 btv->opt_uv_ratio = c->value;
1289                 bt848_sat(btv, btv->saturation);
1290                 break;
1291         case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1292                 btv->opt_full_luma_range = c->value;
1293                 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1294                 break;
1295         case V4L2_CID_PRIVATE_CORING:
1296                 btv->opt_coring = c->value;
1297                 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1298                 break;
1299         default:
1300                 return -EINVAL;
1301         }
1302         if (i >= 4 && i <= 8) {
1303                 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1304                 if (btv->audio_hook)
1305                         btv->audio_hook(btv,&va,1);
1306         }
1307         return 0;
1308 }
1309
1310 /* ----------------------------------------------------------------------- */
1311
1312 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1313 {
1314         unsigned int outbits, data;
1315         outbits = btread(BT848_GPIO_OUT_EN);
1316         data    = btread(BT848_GPIO_DATA);
1317         printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1318                btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1319 }
1320
1321 static void bttv_field_count(struct bttv *btv)
1322 {
1323         int need_count = 0;
1324
1325         if (btv->users)
1326                 need_count++;
1327
1328         if (need_count) {
1329                 /* start field counter */
1330                 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1331         } else {
1332                 /* stop field counter */
1333                 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1334                 btv->field_count = 0;
1335         }
1336 }
1337
1338 static const struct bttv_format*
1339 format_by_palette(int palette)
1340 {
1341         unsigned int i;
1342
1343         for (i = 0; i < BTTV_FORMATS; i++) {
1344                 if (-1 == bttv_formats[i].palette)
1345                         continue;
1346                 if (bttv_formats[i].palette == palette)
1347                         return bttv_formats+i;
1348         }
1349         return NULL;
1350 }
1351
1352 static const struct bttv_format*
1353 format_by_fourcc(int fourcc)
1354 {
1355         unsigned int i;
1356
1357         for (i = 0; i < BTTV_FORMATS; i++) {
1358                 if (-1 == bttv_formats[i].fourcc)
1359                         continue;
1360                 if (bttv_formats[i].fourcc == fourcc)
1361                         return bttv_formats+i;
1362         }
1363         return NULL;
1364 }
1365
1366 /* ----------------------------------------------------------------------- */
1367 /* misc helpers                                                            */
1368
1369 static int
1370 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1371                     struct bttv_buffer *new)
1372 {
1373         struct bttv_buffer *old;
1374         unsigned long flags;
1375         int retval = 0;
1376
1377         dprintk("switch_overlay: enter [new=%p]\n",new);
1378         if (new)
1379                 new->vb.state = STATE_DONE;
1380         spin_lock_irqsave(&btv->s_lock,flags);
1381         old = btv->screen;
1382         btv->screen = new;
1383         btv->loop_irq |= 1;
1384         bttv_set_dma(btv, 0x03);
1385         spin_unlock_irqrestore(&btv->s_lock,flags);
1386         if (NULL == new)
1387                 free_btres(btv,fh,RESOURCE_OVERLAY);
1388         if (NULL != old) {
1389                 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1390                 bttv_dma_free(btv, old);
1391                 kfree(old);
1392         }
1393         dprintk("switch_overlay: done\n");
1394         return retval;
1395 }
1396
1397 /* ----------------------------------------------------------------------- */
1398 /* video4linux (1) interface                                               */
1399
1400 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1401                                const struct bttv_format *fmt,
1402                                unsigned int width, unsigned int height,
1403                                enum v4l2_field field)
1404 {
1405         int redo_dma_risc = 0;
1406         int rc;
1407
1408         /* check settings */
1409         if (NULL == fmt)
1410                 return -EINVAL;
1411         if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1412                 width  = RAW_BPL;
1413                 height = RAW_LINES*2;
1414                 if (width*height > buf->vb.bsize)
1415                         return -EINVAL;
1416                 buf->vb.size = buf->vb.bsize;
1417         } else {
1418                 if (width  < 48 ||
1419                     height < 32 ||
1420                     width  > bttv_tvnorms[btv->tvnorm].swidth ||
1421                     height > bttv_tvnorms[btv->tvnorm].sheight)
1422                         return -EINVAL;
1423                 buf->vb.size = (width * height * fmt->depth) >> 3;
1424                 if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
1425                         return -EINVAL;
1426         }
1427
1428         /* alloc + fill struct bttv_buffer (if changed) */
1429         if (buf->vb.width != width || buf->vb.height != height ||
1430             buf->vb.field != field ||
1431             buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1432                 buf->vb.width  = width;
1433                 buf->vb.height = height;
1434                 buf->vb.field  = field;
1435                 buf->tvnorm    = btv->tvnorm;
1436                 buf->fmt       = fmt;
1437                 redo_dma_risc = 1;
1438         }
1439
1440         /* alloc risc memory */
1441         if (STATE_NEEDS_INIT == buf->vb.state) {
1442                 redo_dma_risc = 1;
1443                 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1444                         goto fail;
1445         }
1446
1447         if (redo_dma_risc)
1448                 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1449                         goto fail;
1450
1451         buf->vb.state = STATE_PREPARED;
1452         return 0;
1453
1454  fail:
1455         bttv_dma_free(btv,buf);
1456         return rc;
1457 }
1458
1459 static int
1460 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1461 {
1462         struct bttv_fh *fh = q->priv_data;
1463
1464         *size = fh->fmt->depth*fh->width*fh->height >> 3;
1465         if (0 == *count)
1466                 *count = gbuffers;
1467         while (*size * *count > gbuffers * gbufsize)
1468                 (*count)--;
1469         return 0;
1470 }
1471
1472 static int
1473 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1474                enum v4l2_field field)
1475 {
1476         struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1477         struct bttv_fh *fh = q->priv_data;
1478
1479         return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1480                                    fh->width, fh->height, field);
1481 }
1482
1483 static void
1484 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1485 {
1486         struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1487         struct bttv_fh *fh = q->priv_data;
1488         struct bttv    *btv = fh->btv;
1489
1490         buf->vb.state = STATE_QUEUED;
1491         list_add_tail(&buf->vb.queue,&btv->capture);
1492         if (!btv->curr.frame_irq) {
1493                 btv->loop_irq |= 1;
1494                 bttv_set_dma(btv, 0x03);
1495         }
1496 }
1497
1498 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1499 {
1500         struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1501         struct bttv_fh *fh = q->priv_data;
1502
1503         bttv_dma_free(fh->btv,buf);
1504 }
1505
1506 static struct videobuf_queue_ops bttv_video_qops = {
1507         .buf_setup    = buffer_setup,
1508         .buf_prepare  = buffer_prepare,
1509         .buf_queue    = buffer_queue,
1510         .buf_release  = buffer_release,
1511 };
1512
1513 static const char *v4l1_ioctls[] = {
1514         "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
1515         "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
1516         "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
1517         "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
1518         "SMICROCODE", "GVBIFMT", "SVBIFMT" };
1519 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
1520
1521 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1522 {
1523         switch (cmd) {
1524         case BTTV_VERSION:
1525                 return BTTV_VERSION_CODE;
1526
1527         /* ***  v4l1  *** ************************************************ */
1528         case VIDIOCGFREQ:
1529         {
1530                 unsigned long *freq = arg;
1531                 *freq = btv->freq;
1532                 return 0;
1533         }
1534         case VIDIOCSFREQ:
1535         {
1536                 unsigned long *freq = arg;
1537                 down(&btv->lock);
1538                 btv->freq=*freq;
1539                 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1540                 if (btv->has_matchbox && btv->radio_user)
1541                         tea5757_set_freq(btv,*freq);
1542                 up(&btv->lock);
1543                 return 0;
1544         }
1545
1546         case VIDIOCGTUNER:
1547         {
1548                 struct video_tuner *v = arg;
1549
1550                 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1551                         return -EINVAL;
1552                 if (v->tuner) /* Only tuner 0 */
1553                         return -EINVAL;
1554                 strcpy(v->name, "Television");
1555                 v->rangelow  = 0;
1556                 v->rangehigh = 0x7FFFFFFF;
1557                 v->flags     = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1558                 v->mode      = btv->tvnorm;
1559                 v->signal    = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1560                 bttv_call_i2c_clients(btv,cmd,v);
1561                 return 0;
1562         }
1563         case VIDIOCSTUNER:
1564         {
1565                 struct video_tuner *v = arg;
1566
1567                 if (v->tuner) /* Only tuner 0 */
1568                         return -EINVAL;
1569                 if (v->mode >= BTTV_TVNORMS)
1570                         return -EINVAL;
1571
1572                 down(&btv->lock);
1573                 set_tvnorm(btv,v->mode);
1574                 bttv_call_i2c_clients(btv,cmd,v);
1575                 up(&btv->lock);
1576                 return 0;
1577         }
1578
1579         case VIDIOCGCHAN:
1580         {
1581                 struct video_channel *v = arg;
1582                 unsigned int channel = v->channel;
1583
1584                 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1585                         return -EINVAL;
1586                 v->tuners=0;
1587                 v->flags = VIDEO_VC_AUDIO;
1588                 v->type = VIDEO_TYPE_CAMERA;
1589                 v->norm = btv->tvnorm;
1590                 if (channel == bttv_tvcards[btv->c.type].tuner)  {
1591                         strcpy(v->name,"Television");
1592                         v->flags|=VIDEO_VC_TUNER;
1593                         v->type=VIDEO_TYPE_TV;
1594                         v->tuners=1;
1595                 } else if (channel == btv->svhs) {
1596                         strcpy(v->name,"S-Video");
1597                 } else {
1598                         sprintf(v->name,"Composite%d",channel);
1599                 }
1600                 return 0;
1601         }
1602         case VIDIOCSCHAN:
1603         {
1604                 struct video_channel *v = arg;
1605                 unsigned int channel = v->channel;
1606
1607                 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1608                         return -EINVAL;
1609                 if (v->norm >= BTTV_TVNORMS)
1610                         return -EINVAL;
1611
1612                 down(&btv->lock);
1613                 if (channel == btv->input &&
1614                     v->norm == btv->tvnorm) {
1615                         /* nothing to do */
1616                         up(&btv->lock);
1617                         return 0;
1618                 }
1619
1620                 btv->tvnorm = v->norm;
1621                 set_input(btv,v->channel);
1622                 up(&btv->lock);
1623                 return 0;
1624         }
1625
1626         case VIDIOCGAUDIO:
1627         {
1628                 struct video_audio *v = arg;
1629
1630                 memset(v,0,sizeof(*v));
1631                 strcpy(v->name,"Television");
1632                 v->flags |= VIDEO_AUDIO_MUTABLE;
1633                 v->mode  = VIDEO_SOUND_MONO;
1634
1635                 down(&btv->lock);
1636                 bttv_call_i2c_clients(btv,cmd,v);
1637
1638                 /* card specific hooks */
1639                 if (btv->audio_hook)
1640                         btv->audio_hook(btv,v,0);
1641
1642                 up(&btv->lock);
1643                 return 0;
1644         }
1645         case VIDIOCSAUDIO:
1646         {
1647                 struct video_audio *v = arg;
1648                 unsigned int audio = v->audio;
1649
1650                 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1651                         return -EINVAL;
1652
1653                 down(&btv->lock);
1654                 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1655                 bttv_call_i2c_clients(btv,cmd,v);
1656
1657                 /* card specific hooks */
1658                 if (btv->audio_hook)
1659                         btv->audio_hook(btv,v,1);
1660
1661                 up(&btv->lock);
1662                 return 0;
1663         }
1664
1665         /* ***  v4l2  *** ************************************************ */
1666         case VIDIOC_ENUMSTD:
1667         {
1668                 struct v4l2_standard *e = arg;
1669                 unsigned int index = e->index;
1670
1671                 if (index >= BTTV_TVNORMS)
1672                         return -EINVAL;
1673                 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1674                                          bttv_tvnorms[e->index].name);
1675                 e->index = index;
1676                 return 0;
1677         }
1678         case VIDIOC_G_STD:
1679         {
1680                 v4l2_std_id *id = arg;
1681                 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1682                 return 0;
1683         }
1684         case VIDIOC_S_STD:
1685         {
1686                 v4l2_std_id *id = arg;
1687                 unsigned int i;
1688
1689                 for (i = 0; i < BTTV_TVNORMS; i++)
1690                         if (*id & bttv_tvnorms[i].v4l2_id)
1691                                 break;
1692                 if (i == BTTV_TVNORMS)
1693                         return -EINVAL;
1694
1695                 down(&btv->lock);
1696                 set_tvnorm(btv,i);
1697                 i2c_vidiocschan(btv);
1698                 up(&btv->lock);
1699                 return 0;
1700         }
1701         case VIDIOC_QUERYSTD:
1702         {
1703                 v4l2_std_id *id = arg;
1704
1705                 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1706                         *id = V4L2_STD_625_50;
1707                 else
1708                         *id = V4L2_STD_525_60;
1709                 return 0;
1710         }
1711
1712         case VIDIOC_ENUMINPUT:
1713         {
1714                 struct v4l2_input *i = arg;
1715                 unsigned int n;
1716
1717                 n = i->index;
1718                 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1719                         return -EINVAL;
1720                 memset(i,0,sizeof(*i));
1721                 i->index    = n;
1722                 i->type     = V4L2_INPUT_TYPE_CAMERA;
1723                 i->audioset = 1;
1724                 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1725                         sprintf(i->name, "Television");
1726                         i->type  = V4L2_INPUT_TYPE_TUNER;
1727                         i->tuner = 0;
1728                 } else if (i->index == btv->svhs) {
1729                         sprintf(i->name, "S-Video");
1730                 } else {
1731                         sprintf(i->name,"Composite%d",i->index);
1732                 }
1733                 if (i->index == btv->input) {
1734                         __u32 dstatus = btread(BT848_DSTATUS);
1735                         if (0 == (dstatus & BT848_DSTATUS_PRES))
1736                                 i->status |= V4L2_IN_ST_NO_SIGNAL;
1737                         if (0 == (dstatus & BT848_DSTATUS_HLOC))
1738                                 i->status |= V4L2_IN_ST_NO_H_LOCK;
1739                 }
1740                 for (n = 0; n < BTTV_TVNORMS; n++)
1741                         i->std |= bttv_tvnorms[n].v4l2_id;
1742                 return 0;
1743         }
1744         case VIDIOC_G_INPUT:
1745         {
1746                 int *i = arg;
1747                 *i = btv->input;
1748                 return 0;
1749         }
1750         case VIDIOC_S_INPUT:
1751         {
1752                 unsigned int *i = arg;
1753
1754                 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1755                         return -EINVAL;
1756                 down(&btv->lock);
1757                 set_input(btv,*i);
1758                 up(&btv->lock);
1759                 return 0;
1760         }
1761
1762         case VIDIOC_G_TUNER:
1763         {
1764                 struct v4l2_tuner *t = arg;
1765
1766                 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1767                         return -EINVAL;
1768                 if (0 != t->index)
1769                         return -EINVAL;
1770                 down(&btv->lock);
1771                 memset(t,0,sizeof(*t));
1772                 strcpy(t->name, "Television");
1773                 t->type       = V4L2_TUNER_ANALOG_TV;
1774                 t->rangehigh  = 0xffffffffUL;
1775                 t->capability = V4L2_TUNER_CAP_NORM;
1776                 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1777                 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1778                         t->signal = 0xffff;
1779                 {
1780                         /* Hmmm ... */
1781                         struct video_audio va;
1782                         memset(&va, 0, sizeof(struct video_audio));
1783                         bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1784                         if (btv->audio_hook)
1785                                 btv->audio_hook(btv,&va,0);
1786                         if(va.mode & VIDEO_SOUND_STEREO) {
1787                                 t->audmode     = V4L2_TUNER_MODE_STEREO;
1788                                 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1789                         }
1790                         if(va.mode & VIDEO_SOUND_LANG1) {
1791                                 t->audmode    = V4L2_TUNER_MODE_LANG1;
1792                                 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1793                                         | V4L2_TUNER_SUB_LANG2;
1794                         }
1795                 }
1796                 /* FIXME: fill capability+audmode */
1797                 up(&btv->lock);
1798                 return 0;
1799         }
1800         case VIDIOC_S_TUNER:
1801         {
1802                 struct v4l2_tuner *t = arg;
1803
1804                 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1805                         return -EINVAL;
1806                 if (0 != t->index)
1807                         return -EINVAL;
1808                 down(&btv->lock);
1809                 {
1810                         struct video_audio va;
1811                         memset(&va, 0, sizeof(struct video_audio));
1812                         bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1813                         if (t->audmode == V4L2_TUNER_MODE_MONO)
1814                                 va.mode = VIDEO_SOUND_MONO;
1815                         else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1816                                 va.mode = VIDEO_SOUND_STEREO;
1817                         else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1818                                 va.mode = VIDEO_SOUND_LANG1;
1819                         else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1820                                 va.mode = VIDEO_SOUND_LANG2;
1821                         bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1822                         if (btv->audio_hook)
1823                                 btv->audio_hook(btv,&va,1);
1824                 }
1825                 up(&btv->lock);
1826                 return 0;
1827         }
1828
1829         case VIDIOC_G_FREQUENCY:
1830         {
1831                 struct v4l2_frequency *f = arg;
1832
1833                 memset(f,0,sizeof(*f));
1834                 f->type = V4L2_TUNER_ANALOG_TV;
1835                 f->frequency = btv->freq;
1836                 return 0;
1837         }
1838         case VIDIOC_S_FREQUENCY:
1839         {
1840                 struct v4l2_frequency *f = arg;
1841
1842                 if (unlikely(f->tuner != 0))
1843                         return -EINVAL;
1844                 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1845                         return -EINVAL;
1846                 down(&btv->lock);
1847                 btv->freq = f->frequency;
1848                 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1849                 if (btv->has_matchbox && btv->radio_user)
1850                         tea5757_set_freq(btv,btv->freq);
1851                 up(&btv->lock);
1852                 return 0;
1853         }
1854
1855         default:
1856                 return -ENOIOCTLCMD;
1857
1858         }
1859         return 0;
1860 }
1861
1862 static int verify_window(const struct bttv_tvnorm *tvn,
1863                          struct v4l2_window *win, int fixup)
1864 {
1865         enum v4l2_field field;
1866         int maxw, maxh;
1867
1868         if (win->w.width  < 48 || win->w.height < 32)
1869                 return -EINVAL;
1870         if (win->clipcount > 2048)
1871                 return -EINVAL;
1872
1873         field = win->field;
1874         maxw  = tvn->swidth;
1875         maxh  = tvn->sheight;
1876
1877         if (V4L2_FIELD_ANY == field) {
1878                 field = (win->w.height > maxh/2)
1879                         ? V4L2_FIELD_INTERLACED
1880                         : V4L2_FIELD_TOP;
1881         }
1882         switch (field) {
1883         case V4L2_FIELD_TOP:
1884         case V4L2_FIELD_BOTTOM:
1885                 maxh = maxh / 2;
1886                 break;
1887         case V4L2_FIELD_INTERLACED:
1888                 break;
1889         default:
1890                 return -EINVAL;
1891         }
1892
1893         if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1894                 return -EINVAL;
1895
1896         if (win->w.width > maxw)
1897                 win->w.width = maxw;
1898         if (win->w.height > maxh)
1899                 win->w.height = maxh;
1900         win->field = field;
1901         return 0;
1902 }
1903
1904 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1905                         struct v4l2_window *win, int fixup)
1906 {
1907         struct v4l2_clip *clips = NULL;
1908         int n,size,retval = 0;
1909
1910         if (NULL == fh->ovfmt)
1911                 return -EINVAL;
1912         if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1913                 return -EINVAL;
1914         retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1915         if (0 != retval)
1916                 return retval;
1917
1918         /* copy clips  --  luckily v4l1 + v4l2 are binary
1919            compatible here ...*/
1920         n = win->clipcount;
1921         size = sizeof(*clips)*(n+4);
1922         clips = kmalloc(size,GFP_KERNEL);
1923         if (NULL == clips)
1924                 return -ENOMEM;
1925         if (n > 0) {
1926                 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1927                         kfree(clips);
1928                         return -EFAULT;
1929                 }
1930         }
1931         /* clip against screen */
1932         if (NULL != btv->fbuf.base)
1933                 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1934                                       &win->w, clips, n);
1935         btcx_sort_clips(clips,n);
1936
1937         /* 4-byte alignments */
1938         switch (fh->ovfmt->depth) {
1939         case 8:
1940         case 24:
1941                 btcx_align(&win->w, clips, n, 3);
1942                 break;
1943         case 16:
1944                 btcx_align(&win->w, clips, n, 1);
1945                 break;
1946         case 32:
1947                 /* no alignment fixups needed */
1948                 break;
1949         default:
1950                 BUG();
1951         }
1952
1953         down(&fh->cap.lock);
1954         kfree(fh->ov.clips);
1955         fh->ov.clips    = clips;
1956         fh->ov.nclips   = n;
1957
1958         fh->ov.w        = win->w;
1959         fh->ov.field    = win->field;
1960         fh->ov.setup_ok = 1;
1961         btv->init.ov.w.width   = win->w.width;
1962         btv->init.ov.w.height  = win->w.height;
1963         btv->init.ov.field     = win->field;
1964
1965         /* update overlay if needed */
1966         retval = 0;
1967         if (check_btres(fh, RESOURCE_OVERLAY)) {
1968                 struct bttv_buffer *new;
1969
1970                 new = videobuf_alloc(sizeof(*new));
1971                 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1972                 retval = bttv_switch_overlay(btv,fh,new);
1973         }
1974         up(&fh->cap.lock);
1975         return retval;
1976 }
1977
1978 /* ----------------------------------------------------------------------- */
1979
1980 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1981 {
1982         struct videobuf_queue* q = NULL;
1983
1984         switch (fh->type) {
1985         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1986                 q = &fh->cap;
1987                 break;
1988         case V4L2_BUF_TYPE_VBI_CAPTURE:
1989                 q = &fh->vbi;
1990                 break;
1991         default:
1992                 BUG();
1993         }
1994         return q;
1995 }
1996
1997 static int bttv_resource(struct bttv_fh *fh)
1998 {
1999         int res = 0;
2000
2001         switch (fh->type) {
2002         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2003                 res = RESOURCE_VIDEO;
2004                 break;
2005         case V4L2_BUF_TYPE_VBI_CAPTURE:
2006                 res = RESOURCE_VBI;
2007                 break;
2008         default:
2009                 BUG();
2010         }
2011         return res;
2012 }
2013
2014 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2015 {
2016         struct videobuf_queue *q = bttv_queue(fh);
2017         int res = bttv_resource(fh);
2018
2019         if (check_btres(fh,res))
2020                 return -EBUSY;
2021         if (videobuf_queue_is_busy(q))
2022                 return -EBUSY;
2023         fh->type = type;
2024         return 0;
2025 }
2026
2027 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2028 {
2029         switch (f->type) {
2030         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2031                 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2032                 f->fmt.pix.width        = fh->width;
2033                 f->fmt.pix.height       = fh->height;
2034                 f->fmt.pix.field        = fh->cap.field;
2035                 f->fmt.pix.pixelformat  = fh->fmt->fourcc;
2036                 f->fmt.pix.bytesperline =
2037                         (f->fmt.pix.width * fh->fmt->depth) >> 3;
2038                 f->fmt.pix.sizeimage =
2039                         f->fmt.pix.height * f->fmt.pix.bytesperline;
2040                 return 0;
2041         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2042                 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2043                 f->fmt.win.w     = fh->ov.w;
2044                 f->fmt.win.field = fh->ov.field;
2045                 return 0;
2046         case V4L2_BUF_TYPE_VBI_CAPTURE:
2047                 bttv_vbi_get_fmt(fh,f);
2048                 return 0;
2049         default:
2050                 return -EINVAL;
2051         }
2052 }
2053
2054 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2055                         struct v4l2_format *f)
2056 {
2057         switch (f->type) {
2058         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2059         {
2060                 const struct bttv_format *fmt;
2061                 enum v4l2_field field;
2062                 unsigned int maxw,maxh;
2063
2064                 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2065                 if (NULL == fmt)
2066                         return -EINVAL;
2067
2068                 /* fixup format */
2069                 maxw  = bttv_tvnorms[btv->tvnorm].swidth;
2070                 maxh  = bttv_tvnorms[btv->tvnorm].sheight;
2071                 field = f->fmt.pix.field;
2072                 if (V4L2_FIELD_ANY == field)
2073                         field = (f->fmt.pix.height > maxh/2)
2074                                 ? V4L2_FIELD_INTERLACED
2075                                 : V4L2_FIELD_BOTTOM;
2076                 if (V4L2_FIELD_SEQ_BT == field)
2077                         field = V4L2_FIELD_SEQ_TB;
2078                 switch (field) {
2079                 case V4L2_FIELD_TOP:
2080                 case V4L2_FIELD_BOTTOM:
2081                 case V4L2_FIELD_ALTERNATE:
2082                         maxh = maxh/2;
2083                         break;
2084                 case V4L2_FIELD_INTERLACED:
2085                         break;
2086                 case V4L2_FIELD_SEQ_TB:
2087                         if (fmt->flags & FORMAT_FLAGS_PLANAR)
2088                                 return -EINVAL;
2089                         break;
2090                 default:
2091                         return -EINVAL;
2092                 }
2093
2094                 /* update data for the application */
2095                 f->fmt.pix.field = field;
2096                 if (f->fmt.pix.width  < 48)
2097                         f->fmt.pix.width  = 48;
2098                 if (f->fmt.pix.height < 32)
2099                         f->fmt.pix.height = 32;
2100                 if (f->fmt.pix.width  > maxw)
2101                         f->fmt.pix.width = maxw;
2102                 if (f->fmt.pix.height > maxh)
2103                         f->fmt.pix.height = maxh;
2104                 f->fmt.pix.width &= ~0x03;
2105                 f->fmt.pix.bytesperline =
2106                         (f->fmt.pix.width * fmt->depth) >> 3;
2107                 f->fmt.pix.sizeimage =
2108                         f->fmt.pix.height * f->fmt.pix.bytesperline;
2109
2110                 return 0;
2111         }
2112         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2113                 return verify_window(&bttv_tvnorms[btv->tvnorm],
2114                                      &f->fmt.win, 1);
2115         case V4L2_BUF_TYPE_VBI_CAPTURE:
2116                 bttv_vbi_try_fmt(fh,f);
2117                 return 0;
2118         default:
2119                 return -EINVAL;
2120         }
2121 }
2122
2123 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2124                       struct v4l2_format *f)
2125 {
2126         int retval;
2127
2128         switch (f->type) {
2129         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2130         {
2131                 const struct bttv_format *fmt;
2132
2133                 retval = bttv_switch_type(fh,f->type);
2134                 if (0 != retval)
2135                         return retval;
2136                 retval = bttv_try_fmt(fh,btv,f);
2137                 if (0 != retval)
2138                         return retval;
2139                 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2140
2141                 /* update our state informations */
2142                 down(&fh->cap.lock);
2143                 fh->fmt              = fmt;
2144                 fh->cap.field        = f->fmt.pix.field;
2145                 fh->cap.last         = V4L2_FIELD_NONE;
2146                 fh->width            = f->fmt.pix.width;
2147                 fh->height           = f->fmt.pix.height;
2148                 btv->init.fmt        = fmt;
2149                 btv->init.width      = f->fmt.pix.width;
2150                 btv->init.height     = f->fmt.pix.height;
2151                 up(&fh->cap.lock);
2152
2153                 return 0;
2154         }
2155         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2156                 if (no_overlay > 0) {
2157                         printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2158                         return -EINVAL;
2159                 }
2160                 return setup_window(fh, btv, &f->fmt.win, 1);
2161         case V4L2_BUF_TYPE_VBI_CAPTURE:
2162                 retval = bttv_switch_type(fh,f->type);
2163                 if (0 != retval)
2164                         return retval;
2165                 if (locked_btres(fh->btv, RESOURCE_VBI))
2166                         return -EBUSY;
2167                 bttv_vbi_try_fmt(fh,f);
2168                 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2169                 bttv_vbi_get_fmt(fh,f);
2170                 return 0;
2171         default:
2172                 return -EINVAL;
2173         }
2174 }
2175
2176 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2177                          unsigned int cmd, void *arg)
2178 {
2179         struct bttv_fh *fh  = file->private_data;
2180         struct bttv    *btv = fh->btv;
2181         unsigned long flags;
2182         int retval = 0;
2183
2184         if (bttv_debug > 1) {
2185                 switch (_IOC_TYPE(cmd)) {
2186                 case 'v':
2187                         printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2188                                btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2189                                v4l1_ioctls[_IOC_NR(cmd)] : "???");
2190                         break;
2191                 case 'V':
2192                         printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2193                                btv->c.nr, cmd,  v4l2_ioctl_names[_IOC_NR(cmd)]);
2194                         break;
2195                 default:
2196                         printk("bttv%d: ioctl 0x%x (???)\n",
2197                                btv->c.nr, cmd);
2198                 }
2199         }
2200         if (btv->errors)
2201                 bttv_reinit_bt848(btv);
2202
2203         switch (cmd) {
2204         case VIDIOCSFREQ:
2205         case VIDIOCSTUNER:
2206         case VIDIOCSCHAN:
2207         case VIDIOC_S_CTRL:
2208         case VIDIOC_S_STD:
2209         case VIDIOC_S_INPUT:
2210         case VIDIOC_S_TUNER:
2211         case VIDIOC_S_FREQUENCY:
2212                 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2213                 if (0 != retval)
2214                         return retval;
2215         };
2216
2217         switch (cmd) {
2218
2219         /* ***  v4l1  *** ************************************************ */
2220         case VIDIOCGCAP:
2221         {
2222                 struct video_capability *cap = arg;
2223
2224                 memset(cap,0,sizeof(*cap));
2225                 strcpy(cap->name,btv->video_dev->name);
2226                 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2227                         /* vbi */
2228                         cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2229                 } else {
2230                         /* others */
2231                         cap->type = VID_TYPE_CAPTURE|
2232                                 VID_TYPE_TUNER|
2233                                 VID_TYPE_CLIPPING|
2234                                 VID_TYPE_SCALES;
2235                         if (no_overlay <= 0)
2236                                 cap->type |= VID_TYPE_OVERLAY;
2237
2238                         cap->maxwidth  = bttv_tvnorms[btv->tvnorm].swidth;
2239                         cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2240                         cap->minwidth  = 48;
2241                         cap->minheight = 32;
2242                 }
2243                 cap->channels  = bttv_tvcards[btv->c.type].video_inputs;
2244                 cap->audios    = bttv_tvcards[btv->c.type].audio_inputs;
2245                 return 0;
2246         }
2247
2248         case VIDIOCGPICT:
2249         {
2250                 struct video_picture *pic = arg;
2251
2252                 memset(pic,0,sizeof(*pic));
2253                 pic->brightness = btv->bright;
2254                 pic->contrast   = btv->contrast;
2255                 pic->hue        = btv->hue;
2256                 pic->colour     = btv->saturation;
2257                 if (fh->fmt) {
2258                         pic->depth   = fh->fmt->depth;
2259                         pic->palette = fh->fmt->palette;
2260                 }
2261                 return 0;
2262         }
2263         case VIDIOCSPICT:
2264         {
2265                 struct video_picture *pic = arg;
2266                 const struct bttv_format *fmt;
2267
2268                 fmt = format_by_palette(pic->palette);
2269                 if (NULL == fmt)
2270                         return -EINVAL;
2271                 down(&fh->cap.lock);
2272                 if (fmt->depth != pic->depth) {
2273                         retval = -EINVAL;
2274                         goto fh_unlock_and_return;
2275                 }
2276                 fh->ovfmt   = fmt;
2277                 fh->fmt     = fmt;
2278                 btv->init.ovfmt   = fmt;
2279                 btv->init.fmt     = fmt;
2280                 if (bigendian) {
2281                         /* dirty hack time:  swap bytes for overlay if the
2282                            display adaptor is big endian (insmod option) */
2283                         if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2284                             fmt->palette == VIDEO_PALETTE_RGB565 ||
2285                             fmt->palette == VIDEO_PALETTE_RGB32) {
2286                                 fh->ovfmt = fmt+1;
2287                         }
2288                 }
2289                 bt848_bright(btv,pic->brightness);
2290                 bt848_contrast(btv,pic->contrast);
2291                 bt848_hue(btv,pic->hue);
2292                 bt848_sat(btv,pic->colour);
2293                 up(&fh->cap.lock);
2294                 return 0;
2295         }
2296
2297         case VIDIOCGWIN:
2298         {
2299                 struct video_window *win = arg;
2300
2301                 memset(win,0,sizeof(*win));
2302                 win->x      = fh->ov.w.left;
2303                 win->y      = fh->ov.w.top;
2304                 win->width  = fh->ov.w.width;
2305                 win->height = fh->ov.w.height;
2306                 return 0;
2307         }
2308         case VIDIOCSWIN:
2309         {
2310                 struct video_window *win = arg;
2311                 struct v4l2_window w2;
2312
2313                 if (no_overlay > 0) {
2314                         printk ("VIDIOCSWIN: no_overlay\n");
2315                         return -EINVAL;
2316                 }
2317
2318                 w2.field = V4L2_FIELD_ANY;
2319                 w2.w.left    = win->x;
2320                 w2.w.top     = win->y;
2321                 w2.w.width   = win->width;
2322                 w2.w.height  = win->height;
2323                 w2.clipcount = win->clipcount;
2324                 w2.clips     = (struct v4l2_clip __user *)win->clips;
2325                 retval = setup_window(fh, btv, &w2, 0);
2326                 if (0 == retval) {
2327                         /* on v4l1 this ioctl affects the read() size too */
2328                         fh->width  = fh->ov.w.width;
2329                         fh->height = fh->ov.w.height;
2330                         btv->init.width  = fh->ov.w.width;
2331                         btv->init.height = fh->ov.w.height;
2332                 }
2333                 return retval;
2334         }
2335
2336         case VIDIOCGFBUF:
2337         {
2338                 struct video_buffer *fbuf = arg;
2339
2340                 fbuf->base          = btv->fbuf.base;
2341                 fbuf->width         = btv->fbuf.fmt.width;
2342                 fbuf->height        = btv->fbuf.fmt.height;
2343                 fbuf->bytesperline  = btv->fbuf.fmt.bytesperline;
2344                 if (fh->ovfmt)
2345                         fbuf->depth = fh->ovfmt->depth;
2346                 return 0;
2347         }
2348         case VIDIOCSFBUF:
2349         {
2350                 struct video_buffer *fbuf = arg;
2351                 const struct bttv_format *fmt;
2352                 unsigned long end;
2353
2354                 if(!capable(CAP_SYS_ADMIN) &&
2355                    !capable(CAP_SYS_RAWIO))
2356                         return -EPERM;
2357                 end = (unsigned long)fbuf->base +
2358                         fbuf->height * fbuf->bytesperline;
2359                 down(&fh->cap.lock);
2360                 retval = -EINVAL;
2361
2362                 switch (fbuf->depth) {
2363                 case 8:
2364                         fmt = format_by_palette(VIDEO_PALETTE_HI240);
2365                         break;
2366                 case 16:
2367                         fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2368                         break;
2369                 case 24:
2370                         fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2371                         break;
2372                 case 32:
2373                         fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2374                         break;
2375                 case 15:
2376                         fbuf->depth = 16;
2377                         fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2378                         break;
2379                 default:
2380                         fmt = NULL;
2381                         break;
2382                 }
2383                 if (NULL == fmt)
2384                         goto fh_unlock_and_return;
2385
2386                 fh->ovfmt = fmt;
2387                 fh->fmt   = fmt;
2388                 btv->init.ovfmt = fmt;
2389                 btv->init.fmt   = fmt;
2390                 btv->fbuf.base             = fbuf->base;
2391                 btv->fbuf.fmt.width        = fbuf->width;
2392                 btv->fbuf.fmt.height       = fbuf->height;
2393                 if (fbuf->bytesperline)
2394                         btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2395                 else
2396                         btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2397                 up(&fh->cap.lock);
2398                 return 0;
2399         }
2400
2401         case VIDIOCCAPTURE:
2402         case VIDIOC_OVERLAY:
2403         {
2404                 struct bttv_buffer *new;
2405                 int *on = arg;
2406
2407                 if (*on) {
2408                         /* verify args */
2409                         if (NULL == btv->fbuf.base)
2410                                 return -EINVAL;
2411                         if (!fh->ov.setup_ok) {
2412                                 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2413                                 return -EINVAL;
2414                         }
2415                 }
2416
2417                 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2418                         return -EBUSY;
2419
2420                 down(&fh->cap.lock);
2421                 if (*on) {
2422                         fh->ov.tvnorm = btv->tvnorm;
2423                         new = videobuf_alloc(sizeof(*new));
2424                         bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2425                 } else {
2426                         new = NULL;
2427                 }
2428
2429                 /* switch over */
2430                 retval = bttv_switch_overlay(btv,fh,new);
2431                 up(&fh->cap.lock);
2432                 return retval;
2433         }
2434
2435         case VIDIOCGMBUF:
2436         {
2437                 struct video_mbuf *mbuf = arg;
2438                 unsigned int i;
2439
2440                 down(&fh->cap.lock);
2441                 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2442                                              V4L2_MEMORY_MMAP);
2443                 if (retval < 0)
2444                         goto fh_unlock_and_return;
2445                 memset(mbuf,0,sizeof(*mbuf));
2446                 mbuf->frames = gbuffers;
2447                 mbuf->size   = gbuffers * gbufsize;
2448                 for (i = 0; i < gbuffers; i++)
2449                         mbuf->offsets[i] = i * gbufsize;
2450                 up(&fh->cap.lock);
2451                 return 0;
2452         }
2453         case VIDIOCMCAPTURE:
2454         {
2455                 struct video_mmap *vm = arg;
2456                 struct bttv_buffer *buf;
2457                 enum v4l2_field field;
2458
2459                 if (vm->frame >= VIDEO_MAX_FRAME)
2460                         return -EINVAL;
2461
2462                 down(&fh->cap.lock);
2463                 retval = -EINVAL;
2464                 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2465                 if (NULL == buf)
2466                         goto fh_unlock_and_return;
2467                 if (0 == buf->vb.baddr)
2468                         goto fh_unlock_and_return;
2469                 if (buf->vb.state == STATE_QUEUED ||
2470                     buf->vb.state == STATE_ACTIVE)
2471                         goto fh_unlock_and_return;
2472
2473                 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2474                         ? V4L2_FIELD_INTERLACED
2475                         : V4L2_FIELD_BOTTOM;
2476                 retval = bttv_prepare_buffer(btv,buf,
2477                                              format_by_palette(vm->format),
2478                                              vm->width,vm->height,field);
2479                 if (0 != retval)
2480                         goto fh_unlock_and_return;
2481                 spin_lock_irqsave(&btv->s_lock,flags);
2482                 buffer_queue(&fh->cap,&buf->vb);
2483                 spin_unlock_irqrestore(&btv->s_lock,flags);
2484                 up(&fh->cap.lock);
2485                 return 0;
2486         }
2487         case VIDIOCSYNC:
2488         {
2489                 int *frame = arg;
2490                 struct bttv_buffer *buf;
2491
2492                 if (*frame >= VIDEO_MAX_FRAME)
2493                         return -EINVAL;
2494
2495                 down(&fh->cap.lock);
2496                 retval = -EINVAL;
2497                 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2498                 if (NULL == buf)
2499                         goto fh_unlock_and_return;
2500                 retval = videobuf_waiton(&buf->vb,0,1);
2501                 if (0 != retval)
2502                         goto fh_unlock_and_return;
2503                 switch (buf->vb.state) {
2504                 case STATE_ERROR:
2505                         retval = -EIO;
2506                         /* fall through */
2507                 case STATE_DONE:
2508                         videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2509                         bttv_dma_free(btv,buf);
2510                         break;
2511                 default:
2512                         retval = -EINVAL;
2513                         break;
2514                 }
2515                 up(&fh->cap.lock);
2516                 return retval;
2517         }
2518
2519         case VIDIOCGVBIFMT:
2520         {
2521                 struct vbi_format *fmt = (void *) arg;
2522                 struct v4l2_format fmt2;
2523
2524                 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2525                         retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2526                         if (0 != retval)
2527                                 return retval;
2528                 }
2529                 bttv_vbi_get_fmt(fh, &fmt2);
2530
2531                 memset(fmt,0,sizeof(*fmt));
2532                 fmt->sampling_rate    = fmt2.fmt.vbi.sampling_rate;
2533                 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2534                 fmt->sample_format    = VIDEO_PALETTE_RAW;
2535                 fmt->start[0]         = fmt2.fmt.vbi.start[0];
2536                 fmt->count[0]         = fmt2.fmt.vbi.count[0];
2537                 fmt->start[1]         = fmt2.fmt.vbi.start[1];
2538                 fmt->count[1]         = fmt2.fmt.vbi.count[1];
2539                 if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2540                         fmt->flags   |= V4L2_VBI_UNSYNC;
2541                 if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2542                         fmt->flags   |= V4L2_VBI_INTERLACED;
2543                 return 0;
2544         }
2545         case VIDIOCSVBIFMT:
2546         {
2547                 struct vbi_format *fmt = (void *) arg;
2548                 struct v4l2_format fmt2;
2549
2550                 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2551                 if (0 != retval)
2552                         return retval;
2553                 bttv_vbi_get_fmt(fh, &fmt2);
2554
2555                 if (fmt->sampling_rate    != fmt2.fmt.vbi.sampling_rate     ||
2556                     fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line  ||
2557                     fmt->sample_format    != VIDEO_PALETTE_RAW              ||
2558                     fmt->start[0]         != fmt2.fmt.vbi.start[0]          ||
2559                     fmt->start[1]         != fmt2.fmt.vbi.start[1]          ||
2560                     fmt->count[0]         != fmt->count[1]                  ||
2561                     fmt->count[0]         <  1                              ||
2562                     fmt->count[0]         >  32 /* VBI_MAXLINES */)
2563                         return -EINVAL;
2564
2565                 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2566                 return 0;
2567         }
2568
2569         case BTTV_VERSION:
2570         case VIDIOCGFREQ:
2571         case VIDIOCSFREQ:
2572         case VIDIOCGTUNER:
2573         case VIDIOCSTUNER:
2574         case VIDIOCGCHAN:
2575         case VIDIOCSCHAN:
2576         case VIDIOCGAUDIO:
2577         case VIDIOCSAUDIO:
2578                 return bttv_common_ioctls(btv,cmd,arg);
2579
2580         /* ***  v4l2  *** ************************************************ */
2581         case VIDIOC_QUERYCAP:
2582         {
2583                 struct v4l2_capability *cap = arg;
2584
2585                 if (0 == v4l2)
2586                         return -EINVAL;
2587                 strcpy(cap->driver,"bttv");
2588                 strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
2589                 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
2590                 cap->version = BTTV_VERSION_CODE;
2591                 cap->capabilities =
2592                         V4L2_CAP_VIDEO_CAPTURE |
2593                         V4L2_CAP_VBI_CAPTURE |
2594                         V4L2_CAP_READWRITE |
2595                         V4L2_CAP_STREAMING;
2596                 if (no_overlay <= 0)
2597                         cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2598
2599                 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2600                     bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2601                         cap->capabilities |= V4L2_CAP_TUNER;
2602                 return 0;
2603         }
2604
2605         case VIDIOC_ENUM_FMT:
2606         {
2607                 struct v4l2_fmtdesc *f = arg;
2608                 enum v4l2_buf_type type;
2609                 unsigned int i;
2610                 int index;
2611
2612                 type  = f->type;
2613                 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2614                         /* vbi */
2615                         index = f->index;
2616                         if (0 != index)
2617                                 return -EINVAL;
2618                         memset(f,0,sizeof(*f));
2619                         f->index       = index;
2620                         f->type        = type;
2621                         f->pixelformat = V4L2_PIX_FMT_GREY;
2622                         strcpy(f->description,"vbi data");
2623                         return 0;
2624                 }
2625
2626                 /* video capture + overlay */
2627                 index = -1;
2628                 for (i = 0; i < BTTV_FORMATS; i++) {
2629                         if (bttv_formats[i].fourcc != -1)
2630                                 index++;
2631                         if ((unsigned int)index == f->index)
2632                                 break;
2633                 }
2634                 if (BTTV_FORMATS == i)
2635                         return -EINVAL;
2636
2637                 switch (f->type) {
2638                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2639                         break;
2640                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2641                         if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2642                                 return -EINVAL;
2643                         break;
2644                 default:
2645                         return -EINVAL;
2646                 }
2647                 memset(f,0,sizeof(*f));
2648                 f->index       = index;
2649                 f->type        = type;
2650                 f->pixelformat = bttv_formats[i].fourcc;
2651                 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2652                 return 0;
2653         }
2654
2655         case VIDIOC_TRY_FMT:
2656         {
2657                 struct v4l2_format *f = arg;
2658                 return bttv_try_fmt(fh,btv,f);
2659         }
2660         case VIDIOC_G_FMT:
2661         {
2662                 struct v4l2_format *f = arg;
2663                 return bttv_g_fmt(fh,f);
2664         }
2665         case VIDIOC_S_FMT:
2666         {
2667                 struct v4l2_format *f = arg;
2668                 return bttv_s_fmt(fh,btv,f);
2669         }
2670
2671         case VIDIOC_G_FBUF:
2672         {
2673                 struct v4l2_framebuffer *fb = arg;
2674
2675                 *fb = btv->fbuf;
2676                 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2677                 if (fh->ovfmt)
2678                         fb->fmt.pixelformat  = fh->ovfmt->fourcc;
2679                 return 0;
2680         }
2681         case VIDIOC_S_FBUF:
2682         {
2683                 struct v4l2_framebuffer *fb = arg;
2684                 const struct bttv_format *fmt;
2685
2686                 if(!capable(CAP_SYS_ADMIN) &&
2687                    !capable(CAP_SYS_RAWIO))
2688                         return -EPERM;
2689
2690                 /* check args */
2691                 fmt = format_by_fourcc(fb->fmt.pixelformat);
2692                 if (NULL == fmt)
2693                         return -EINVAL;
2694                 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2695                         return -EINVAL;
2696
2697                 down(&fh->cap.lock);
2698                 retval = -EINVAL;
2699                 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2700                         if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2701                                 goto fh_unlock_and_return;
2702                         if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2703                                 goto fh_unlock_and_return;
2704                 }
2705
2706                 /* ok, accept it */
2707                 btv->fbuf.base       = fb->base;
2708                 btv->fbuf.fmt.width  = fb->fmt.width;
2709                 btv->fbuf.fmt.height = fb->fmt.height;
2710                 if (0 != fb->fmt.bytesperline)
2711                         btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2712                 else
2713                         btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2714
2715                 retval = 0;
2716                 fh->ovfmt = fmt;
2717                 btv->init.ovfmt = fmt;
2718                 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2719                         fh->ov.w.left   = 0;
2720                         fh->ov.w.top    = 0;
2721                         fh->ov.w.width  = fb->fmt.width;
2722                         fh->ov.w.height = fb->fmt.height;
2723                         btv->init.ov.w.width  = fb->fmt.width;
2724                         btv->init.ov.w.height = fb->fmt.height;
2725                         kfree(fh->ov.clips);
2726                         fh->ov.clips = NULL;
2727                         fh->ov.nclips = 0;
2728
2729                         if (check_btres(fh, RESOURCE_OVERLAY)) {
2730                                 struct bttv_buffer *new;
2731
2732                                 new = videobuf_alloc(sizeof(*new));
2733                                 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2734                                 retval = bttv_switch_overlay(btv,fh,new);
2735                         }
2736                 }
2737                 up(&fh->cap.lock);
2738                 return retval;
2739         }
2740
2741         case VIDIOC_REQBUFS:
2742                 return videobuf_reqbufs(bttv_queue(fh),arg);
2743
2744         case VIDIOC_QUERYBUF:
2745                 return videobuf_querybuf(bttv_queue(fh),arg);
2746
2747         case VIDIOC_QBUF:
2748                 return videobuf_qbuf(bttv_queue(fh),arg);
2749
2750         case VIDIOC_DQBUF:
2751                 return videobuf_dqbuf(bttv_queue(fh),arg,
2752                                       file->f_flags & O_NONBLOCK);
2753
2754         case VIDIOC_STREAMON:
2755         {
2756                 int res = bttv_resource(fh);
2757
2758                 if (!check_alloc_btres(btv,fh,res))
2759                         return -EBUSY;
2760                 return videobuf_streamon(bttv_queue(fh));
2761         }
2762         case VIDIOC_STREAMOFF:
2763         {
2764                 int res = bttv_resource(fh);
2765
2766                 retval = videobuf_streamoff(bttv_queue(fh));
2767                 if (retval < 0)
2768                         return retval;
2769                 free_btres(btv,fh,res);
2770                 return 0;
2771         }
2772
2773         case VIDIOC_QUERYCTRL:
2774         {
2775                 struct v4l2_queryctrl *c = arg;
2776                 int i;
2777
2778                 if ((c->id <  V4L2_CID_BASE ||
2779                      c->id >= V4L2_CID_LASTP1) &&
2780                     (c->id <  V4L2_CID_PRIVATE_BASE ||
2781                      c->id >= V4L2_CID_PRIVATE_LASTP1))
2782                         return -EINVAL;
2783                 for (i = 0; i < BTTV_CTLS; i++)
2784                         if (bttv_ctls[i].id == c->id)
2785                                 break;
2786                 if (i == BTTV_CTLS) {
2787                         *c = no_ctl;
2788                         return 0;
2789                 }
2790                 *c = bttv_ctls[i];
2791                 if (i >= 4 && i <= 8) {
2792                         struct video_audio va;
2793                         memset(&va,0,sizeof(va));
2794                         bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2795                         if (btv->audio_hook)
2796                                 btv->audio_hook(btv,&va,0);
2797                         switch (bttv_ctls[i].id) {
2798                         case V4L2_CID_AUDIO_VOLUME:
2799                                 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2800                                         *c = no_ctl;
2801                                 break;
2802                         case V4L2_CID_AUDIO_BALANCE:
2803                                 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2804                                         *c = no_ctl;
2805                                 break;
2806                         case V4L2_CID_AUDIO_BASS:
2807                                 if (!(va.flags & VIDEO_AUDIO_BASS))
2808                                         *c = no_ctl;
2809                                 break;
2810                         case V4L2_CID_AUDIO_TREBLE:
2811                                 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2812                                         *c = no_ctl;
2813                                 break;
2814                         }
2815                 }
2816                 return 0;
2817         }
2818         case VIDIOC_G_CTRL:
2819                 return get_control(btv,arg);
2820         case VIDIOC_S_CTRL:
2821                 return set_control(btv,arg);
2822         case VIDIOC_G_PARM:
2823         {
2824                 struct v4l2_streamparm *parm = arg;
2825                 struct v4l2_standard s;
2826                 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2827                         return -EINVAL;
2828                 memset(parm,0,sizeof(*parm));
2829                 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2830                                          bttv_tvnorms[btv->tvnorm].name);
2831                 parm->parm.capture.timeperframe = s.frameperiod;
2832                 return 0;
2833         }
2834
2835         case VIDIOC_G_PRIORITY:
2836         {
2837                 enum v4l2_priority *p = arg;
2838
2839                 *p = v4l2_prio_max(&btv->prio);
2840                 return 0;
2841         }
2842         case VIDIOC_S_PRIORITY:
2843         {
2844                 enum v4l2_priority *prio = arg;
2845
2846                 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2847         }
2848
2849         case VIDIOC_ENUMSTD:
2850         case VIDIOC_G_STD:
2851         case VIDIOC_S_STD:
2852         case VIDIOC_ENUMINPUT:
2853         case VIDIOC_G_INPUT:
2854         case VIDIOC_S_INPUT:
2855         case VIDIOC_G_TUNER:
2856         case VIDIOC_S_TUNER:
2857         case VIDIOC_G_FREQUENCY:
2858         case VIDIOC_S_FREQUENCY:
2859                 return bttv_common_ioctls(btv,cmd,arg);
2860
2861         default:
2862                 return -ENOIOCTLCMD;
2863         }
2864         return 0;
2865
2866  fh_unlock_and_return:
2867         up(&fh->cap.lock);
2868         return retval;
2869 }
2870
2871 static int bttv_ioctl(struct inode *inode, struct file *file,
2872                       unsigned int cmd, unsigned long arg)
2873 {
2874         struct bttv_fh *fh  = file->private_data;
2875
2876         switch (cmd) {
2877         case BTTV_VBISIZE:
2878                 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2879                 return fh->lines * 2 * 2048;
2880         default:
2881                 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2882         }
2883 }
2884
2885 static ssize_t bttv_read(struct file *file, char __user *data,
2886                          size_t count, loff_t *ppos)
2887 {
2888         struct bttv_fh *fh = file->private_data;
2889         int retval = 0;
2890
2891         if (fh->btv->errors)
2892                 bttv_reinit_bt848(fh->btv);
2893         dprintk("bttv%d: read count=%d type=%s\n",
2894                 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2895
2896         switch (fh->type) {
2897         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2898                 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2899                         return -EBUSY;
2900                 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2901                                            file->f_flags & O_NONBLOCK);
2902                 break;
2903         case V4L2_BUF_TYPE_VBI_CAPTURE:
2904                 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2905                         return -EBUSY;
2906                 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2907                                               file->f_flags & O_NONBLOCK);
2908                 break;
2909         default:
2910                 BUG();
2911         }
2912         return retval;
2913 }
2914
2915 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2916 {
2917         struct bttv_fh *fh = file->private_data;
2918         struct bttv_buffer *buf;
2919         enum v4l2_field field;
2920
2921         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2922                 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2923                         return POLLERR;
2924                 return videobuf_poll_stream(file, &fh->vbi, wait);
2925         }
2926
2927         if (check_btres(fh,RESOURCE_VIDEO)) {
2928                 /* streaming capture */
2929                 if (list_empty(&fh->cap.stream))
2930                         return POLLERR;
2931                 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2932         } else {
2933                 /* read() capture */
2934                 down(&fh->cap.lock);
2935                 if (NULL == fh->cap.read_buf) {
2936                         /* need to capture a new frame */
2937                         if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2938                                 up(&fh->cap.lock);
2939                                 return POLLERR;
2940                         }
2941                         fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2942                         if (NULL == fh->cap.read_buf) {
2943                                 up(&fh->cap.lock);
2944                                 return POLLERR;
2945                         }
2946                         fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2947                         field = videobuf_next_field(&fh->cap);
2948                         if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2949                                 up(&fh->cap.lock);
2950                                 return POLLERR;
2951                         }
2952                         fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2953                         fh->cap.read_off = 0;
2954                 }
2955                 up(&fh->cap.lock);
2956                 buf = (struct bttv_buffer*)fh->cap.read_buf;
2957         }
2958
2959         poll_wait(file, &buf->vb.done, wait);
2960         if (buf->vb.state == STATE_DONE ||
2961             buf->vb.state == STATE_ERROR)
2962                 return POLLIN|POLLRDNORM;
2963         return 0;
2964 }
2965
2966 static int bttv_open(struct inode *inode, struct file *file)
2967 {
2968         int minor = iminor(inode);
2969         struct bttv *btv = NULL;
2970         struct bttv_fh *fh;
2971         enum v4l2_buf_type type = 0;
2972         unsigned int i;
2973
2974         dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
2975
2976         for (i = 0; i < bttv_num; i++) {
2977                 if (bttvs[i].video_dev &&
2978                     bttvs[i].video_dev->minor == minor) {
2979                         btv = &bttvs[i];
2980                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2981                         break;
2982                 }
2983                 if (bttvs[i].vbi_dev &&
2984                     bttvs[i].vbi_dev->minor == minor) {
2985                         btv = &bttvs[i];
2986                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
2987                         break;
2988                 }
2989         }
2990         if (NULL == btv)
2991                 return -ENODEV;
2992
2993         dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
2994                 btv->c.nr,v4l2_type_names[type]);
2995
2996         /* allocate per filehandle data */
2997         fh = kmalloc(sizeof(*fh),GFP_KERNEL);
2998         if (NULL == fh)
2999                 return -ENOMEM;
3000         file->private_data = fh;
3001         *fh = btv->init;
3002         fh->type = type;
3003         fh->ov.setup_ok = 0;
3004         v4l2_prio_open(&btv->prio,&fh->prio);
3005
3006         videobuf_queue_init(&fh->cap, &bttv_video_qops,
3007                             btv->c.pci, &btv->s_lock,
3008                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
3009                             V4L2_FIELD_INTERLACED,
3010                             sizeof(struct bttv_buffer),
3011                             fh);
3012         videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3013                             btv->c.pci, &btv->s_lock,
3014                             V4L2_BUF_TYPE_VBI_CAPTURE,
3015                             V4L2_FIELD_SEQ_TB,
3016                             sizeof(struct bttv_buffer),
3017                             fh);
3018         i2c_vidiocschan(btv);
3019
3020         btv->users++;
3021         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3022                 bttv_vbi_setlines(fh,btv,16);
3023         bttv_field_count(btv);
3024         return 0;
3025 }
3026
3027 static int bttv_release(struct inode *inode, struct file *file)
3028 {
3029         struct bttv_fh *fh = file->private_data;
3030         struct bttv *btv = fh->btv;
3031
3032         /* turn off overlay */
3033         if (check_btres(fh, RESOURCE_OVERLAY))
3034                 bttv_switch_overlay(btv,fh,NULL);
3035
3036         /* stop video capture */
3037         if (check_btres(fh, RESOURCE_VIDEO)) {
3038                 videobuf_streamoff(&fh->cap);
3039                 free_btres(btv,fh,RESOURCE_VIDEO);
3040         }
3041         if (fh->cap.read_buf) {
3042                 buffer_release(&fh->cap,fh->cap.read_buf);
3043                 kfree(fh->cap.read_buf);
3044         }
3045
3046         /* stop vbi capture */
3047         if (check_btres(fh, RESOURCE_VBI)) {
3048                 if (fh->vbi.streaming)
3049                         videobuf_streamoff(&fh->vbi);
3050                 if (fh->vbi.reading)
3051                         videobuf_read_stop(&fh->vbi);
3052                 free_btres(btv,fh,RESOURCE_VBI);
3053         }
3054
3055         /* free stuff */
3056         videobuf_mmap_free(&fh->cap);
3057         videobuf_mmap_free(&fh->vbi);
3058         v4l2_prio_close(&btv->prio,&fh->prio);
3059         file->private_data = NULL;
3060         kfree(fh);
3061
3062         btv->users--;
3063         bttv_field_count(btv);
3064         return 0;
3065 }
3066
3067 static int
3068 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3069 {
3070         struct bttv_fh *fh = file->private_data;
3071
3072         dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3073                 fh->btv->c.nr, v4l2_type_names[fh->type],
3074                 vma->vm_start, vma->vm_end - vma->vm_start);
3075         return videobuf_mmap_mapper(bttv_queue(fh),vma);
3076 }
3077
3078 static struct file_operations bttv_fops =
3079 {
3080         .owner    = THIS_MODULE,
3081         .open     = bttv_open,
3082         .release  = bttv_release,
3083         .ioctl    = bttv_ioctl,
3084         .llseek   = no_llseek,
3085         .read     = bttv_read,
3086         .mmap     = bttv_mmap,
3087         .poll     = bttv_poll,
3088 };
3089
3090 static struct video_device bttv_video_template =
3091 {
3092         .name     = "UNSET",
3093         .type     = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3094                     VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3095         .hardware = VID_HARDWARE_BT848,
3096         .fops     = &bttv_fops,
3097         .minor    = -1,
3098 };
3099
3100 static struct video_device bttv_vbi_template =
3101 {
3102         .name     = "bt848/878 vbi",
3103         .type     = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3104         .hardware = VID_HARDWARE_BT848,
3105         .fops     = &bttv_fops,
3106         .minor    = -1,
3107 };
3108
3109 /* ----------------------------------------------------------------------- */
3110 /* radio interface                                                         */
3111
3112 static int radio_open(struct inode *inode, struct file *file)
3113 {
3114         int minor = iminor(inode);
3115         struct bttv *btv = NULL;
3116         unsigned int i;
3117
3118         dprintk("bttv: open minor=%d\n",minor);
3119
3120         for (i = 0; i < bttv_num; i++) {
3121                 if (bttvs[i].radio_dev->minor == minor) {
3122                         btv = &bttvs[i];
3123                         break;
3124                 }
3125         }
3126         if (NULL == btv)
3127                 return -ENODEV;
3128
3129         dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3130         down(&btv->lock);
3131
3132         btv->radio_user++;
3133
3134         file->private_data = btv;
3135
3136         bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3137         audio_mux(btv,AUDIO_RADIO);
3138
3139         up(&btv->lock);
3140         return 0;
3141 }
3142
3143 static int radio_release(struct inode *inode, struct file *file)
3144 {
3145         struct bttv        *btv = file->private_data;
3146         struct rds_command cmd;
3147
3148         btv->radio_user--;
3149
3150         bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3151
3152         return 0;
3153 }
3154
3155 static int radio_do_ioctl(struct inode *inode, struct file *file,
3156                           unsigned int cmd, void *arg)
3157 {
3158         struct bttv    *btv = file->private_data;
3159
3160         switch (cmd) {
3161         case VIDIOCGCAP:
3162         {
3163                 struct video_capability *cap = arg;
3164
3165                 memset(cap,0,sizeof(*cap));
3166                 strcpy(cap->name,btv->radio_dev->name);
3167                 cap->type = VID_TYPE_TUNER;
3168                 cap->channels = 1;
3169                 cap->audios = 1;
3170                 return 0;
3171         }
3172
3173         case VIDIOCGTUNER:
3174         {
3175                 struct video_tuner *v = arg;
3176
3177                 if(v->tuner)
3178                         return -EINVAL;
3179                 memset(v,0,sizeof(*v));
3180                 strcpy(v->name, "Radio");
3181                 bttv_call_i2c_clients(btv,cmd,v);
3182                 return 0;
3183         }
3184         case VIDIOCSTUNER:
3185                 /* nothing to do */
3186                 return 0;
3187
3188         case BTTV_VERSION:
3189         case VIDIOCGFREQ:
3190         case VIDIOCSFREQ:
3191         case VIDIOCGAUDIO:
3192         case VIDIOCSAUDIO:
3193                 return bttv_common_ioctls(btv,cmd,arg);
3194
3195         default:
3196                 return -ENOIOCTLCMD;
3197         }
3198         return 0;
3199 }
3200
3201 static int radio_ioctl(struct inode *inode, struct file *file,
3202                        unsigned int cmd, unsigned long arg)
3203 {
3204         return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3205 }
3206
3207 static ssize_t radio_read(struct file *file, char __user *data,
3208                          size_t count, loff_t *ppos)
3209 {
3210         struct bttv    *btv = file->private_data;
3211         struct rds_command cmd;
3212         cmd.block_count = count/3;
3213         cmd.buffer = data;
3214         cmd.instance = file;
3215         cmd.result = -ENODEV;
3216
3217         bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3218
3219         return cmd.result;
3220 }
3221
3222 static unsigned int radio_poll(struct file *file, poll_table *wait)
3223 {
3224         struct bttv    *btv = file->private_data;
3225         struct rds_command cmd;
3226         cmd.instance = file;
3227         cmd.event_list = wait;
3228         cmd.result = -ENODEV;
3229         bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3230
3231         return cmd.result;
3232 }
3233
3234 static struct file_operations radio_fops =
3235 {
3236         .owner    = THIS_MODULE,
3237         .open     = radio_open,
3238         .read     = radio_read,
3239         .release  = radio_release,
3240         .ioctl    = radio_ioctl,
3241         .llseek   = no_llseek,
3242         .poll     = radio_poll,
3243 };
3244
3245 static struct video_device radio_template =
3246 {
3247         .name     = "bt848/878 radio",
3248         .type     = VID_TYPE_TUNER,
3249         .hardware = VID_HARDWARE_BT848,
3250         .fops     = &radio_fops,
3251         .minor    = -1,
3252 };
3253
3254 /* ----------------------------------------------------------------------- */
3255 /* some debug code                                                         */
3256
3257 static int bttv_risc_decode(u32 risc)
3258 {
3259         static char *instr[16] = {
3260                 [ BT848_RISC_WRITE     >> 28 ] = "write",
3261                 [ BT848_RISC_SKIP      >> 28 ] = "skip",
3262                 [ BT848_RISC_WRITEC    >> 28 ] = "writec",
3263                 [ BT848_RISC_JUMP      >> 28 ] = "jump",
3264                 [ BT848_RISC_SYNC      >> 28 ] = "sync",
3265                 [ BT848_RISC_WRITE123  >> 28 ] = "write123",
3266                 [ BT848_RISC_SKIP123   >> 28 ] = "skip123",
3267                 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3268         };
3269         static int incr[16] = {
3270                 [ BT848_RISC_WRITE     >> 28 ] = 2,
3271                 [ BT848_RISC_JUMP      >> 28 ] = 2,
3272                 [ BT848_RISC_SYNC      >> 28 ] = 2,
3273                 [ BT848_RISC_WRITE123  >> 28 ] = 5,
3274                 [ BT848_RISC_SKIP123   >> 28 ] = 2,
3275                 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3276         };
3277         static char *bits[] = {
3278                 "be0",  "be1",  "be2",  "be3/resync",
3279                 "set0", "set1", "set2", "set3",
3280                 "clr0", "clr1", "clr2", "clr3",
3281                 "irq",  "res",  "eol",  "sol",
3282         };
3283         int i;
3284
3285         printk("0x%08x [ %s", risc,
3286                instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3287         for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3288                 if (risc & (1 << (i + 12)))
3289                         printk(" %s",bits[i]);
3290         printk(" count=%d ]\n", risc & 0xfff);
3291         return incr[risc >> 28] ? incr[risc >> 28] : 1;
3292 }
3293
3294 static void bttv_risc_disasm(struct bttv *btv,
3295                              struct btcx_riscmem *risc)
3296 {
3297         unsigned int i,j,n;
3298
3299         printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3300                btv->c.name, risc->cpu, (unsigned long)risc->dma);
3301         for (i = 0; i < (risc->size >> 2); i += n) {
3302                 printk("%s:   0x%lx: ", btv->c.name,
3303                        (unsigned long)(risc->dma + (i<<2)));
3304                 n = bttv_risc_decode(risc->cpu[i]);
3305                 for (j = 1; j < n; j++)
3306                         printk("%s:   0x%lx: 0x%08x [ arg #%d ]\n",
3307                                btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3308                                risc->cpu[i+j], j);
3309                 if (0 == risc->cpu[i])
3310                         break;
3311         }
3312 }
3313
3314 static void bttv_print_riscaddr(struct bttv *btv)
3315 {
3316         printk("  main: %08Lx\n",
3317                (unsigned long long)btv->main.dma);
3318         printk("  vbi : o=%08Lx e=%08Lx\n",
3319                btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3320                btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3321         printk("  cap : o=%08Lx e=%08Lx\n",
3322                btv->curr.top    ? (unsigned long long)btv->curr.top->top.dma : 0,
3323                btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3324         printk("  scr : o=%08Lx e=%08Lx\n",
3325                btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3326                btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3327         bttv_risc_disasm(btv, &btv->main);
3328 }
3329
3330 /* ----------------------------------------------------------------------- */
3331 /* irq handler                                                             */
3332
3333 static char *irq_name[] = {
3334         "FMTCHG",  // format change detected (525 vs. 625)
3335         "VSYNC",   // vertical sync (new field)
3336         "HSYNC",   // horizontal sync
3337         "OFLOW",   // chroma/luma AGC overflow
3338         "HLOCK",   // horizontal lock changed
3339         "VPRES",   // video presence changed
3340         "6", "7",
3341         "I2CDONE", // hw irc operation finished
3342         "GPINT",   // gpio port triggered irq
3343         "10",
3344         "RISCI",   // risc instruction triggered irq
3345         "FBUS",    // pixel data fifo dropped data (high pci bus latencies)
3346         "FTRGT",   // pixel data fifo overrun
3347         "FDSR",    // fifo data stream resyncronisation
3348         "PPERR",   // parity error (data transfer)
3349         "RIPERR",  // parity error (read risc instructions)
3350         "PABORT",  // pci abort
3351         "OCERR",   // risc instruction error
3352         "SCERR",   // syncronisation error
3353 };
3354
3355 static void bttv_print_irqbits(u32 print, u32 mark)
3356 {
3357         unsigned int i;
3358
3359         printk("bits:");
3360         for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3361                 if (print & (1 << i))
3362                         printk(" %s",irq_name[i]);
3363                 if (mark & (1 << i))
3364                         printk("*");
3365         }
3366 }
3367
3368 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3369 {
3370         printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3371                btv->c.nr,
3372                (unsigned long)btv->main.dma,
3373                (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3374                (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3375                (unsigned long)rc);
3376
3377         if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3378                 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3379                        "Ok, then this is harmless, don't worry ;)\n",
3380                        btv->c.nr);
3381                 return;
3382         }
3383         printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3384                btv->c.nr);
3385         printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3386                btv->c.nr);
3387         dump_stack();
3388 }
3389
3390 static int
3391 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3392 {
3393         struct bttv_buffer *item;
3394
3395         memset(set,0,sizeof(*set));
3396
3397         /* capture request ? */
3398         if (!list_empty(&btv->capture)) {
3399                 set->frame_irq = 1;
3400                 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3401                 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3402                         set->top    = item;
3403                 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3404                         set->bottom = item;
3405
3406                 /* capture request for other field ? */
3407                 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3408                     (item->vb.queue.next != &btv->capture)) {
3409                         item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3410                         if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3411                                 if (NULL == set->top &&
3412                                     V4L2_FIELD_TOP == item->vb.field) {
3413                                         set->top = item;
3414                                 }
3415                                 if (NULL == set->bottom &&
3416                                     V4L2_FIELD_BOTTOM == item->vb.field) {
3417                                         set->bottom = item;
3418                                 }
3419                                 if (NULL != set->top  &&  NULL != set->bottom)
3420                                         set->top_irq = 2;
3421                         }
3422                 }
3423         }
3424
3425         /* screen overlay ? */
3426         if (NULL != btv->screen) {
3427                 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3428                         if (NULL == set->top && NULL == set->bottom) {
3429                                 set->top    = btv->screen;
3430                                 set->bottom = btv->screen;
3431                         }
3432                 } else {
3433                         if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3434                             NULL == set->top) {
3435                                 set->top = btv->screen;
3436                         }
3437                         if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3438                             NULL == set->bottom) {
3439                                 set->bottom = btv->screen;
3440                         }
3441                 }
3442         }
3443
3444         dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3445                 btv->c.nr,set->top, set->bottom,
3446                 btv->screen,set->frame_irq,set->top_irq);
3447         return 0;
3448 }
3449
3450 static void
3451 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3452                       struct bttv_buffer_set *curr, unsigned int state)
3453 {
3454         struct timeval ts;
3455
3456         do_gettimeofday(&ts);
3457
3458         if (wakeup->top == wakeup->bottom) {
3459                 if (NULL != wakeup->top && curr->top != wakeup->top) {
3460                         if (irq_debug > 1)
3461                                 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3462                         wakeup->top->vb.ts = ts;
3463                         wakeup->top->vb.field_count = btv->field_count;
3464                         wakeup->top->vb.state = state;
3465                         wake_up(&wakeup->top->vb.done);
3466                 }
3467         } else {
3468                 if (NULL != wakeup->top && curr->top != wakeup->top) {
3469                         if (irq_debug > 1)
3470                                 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3471                         wakeup->top->vb.ts = ts;
3472                         wakeup->top->vb.field_count = btv->field_count;
3473                         wakeup->top->vb.state = state;
3474                         wake_up(&wakeup->top->vb.done);
3475                 }
3476                 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3477                         if (irq_debug > 1)
3478                                 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3479                         wakeup->bottom->vb.ts = ts;
3480                         wakeup->bottom->vb.field_count = btv->field_count;
3481                         wakeup->bottom->vb.state = state;
3482                         wake_up(&wakeup->bottom->vb.done);
3483                 }
3484         }
3485 }
3486
3487 static void
3488 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3489                     unsigned int state)
3490 {
3491         struct timeval ts;
3492
3493         if (NULL == wakeup)
3494                 return;
3495
3496         do_gettimeofday(&ts);
3497         wakeup->vb.ts = ts;
3498         wakeup->vb.field_count = btv->field_count;
3499         wakeup->vb.state = state;
3500         wake_up(&wakeup->vb.done);
3501 }
3502
3503 static void bttv_irq_timeout(unsigned long data)
3504 {
3505         struct bttv *btv = (struct bttv *)data;
3506         struct bttv_buffer_set old,new;
3507         struct bttv_buffer *ovbi;
3508         struct bttv_buffer *item;
3509         unsigned long flags;
3510
3511         if (bttv_verbose) {
3512                 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3513                        btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3514                        btread(BT848_RISC_COUNT));
3515                 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3516                 printk("\n");
3517         }
3518
3519         spin_lock_irqsave(&btv->s_lock,flags);
3520
3521         /* deactivate stuff */
3522         memset(&new,0,sizeof(new));
3523         old  = btv->curr;
3524         ovbi = btv->cvbi;
3525         btv->curr = new;
3526         btv->cvbi = NULL;
3527         btv->loop_irq = 0;
3528         bttv_buffer_activate_video(btv, &new);
3529         bttv_buffer_activate_vbi(btv,   NULL);
3530         bttv_set_dma(btv, 0);
3531
3532         /* wake up */
3533         bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3534         bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3535
3536         /* cancel all outstanding capture / vbi requests */
3537         while (!list_empty(&btv->capture)) {
3538                 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3539                 list_del(&item->vb.queue);
3540                 item->vb.state = STATE_ERROR;
3541                 wake_up(&item->vb.done);
3542         }
3543         while (!list_empty(&btv->vcapture)) {
3544                 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3545                 list_del(&item->vb.queue);
3546                 item->vb.state = STATE_ERROR;
3547                 wake_up(&item->vb.done);
3548         }
3549
3550         btv->errors++;
3551         spin_unlock_irqrestore(&btv->s_lock,flags);
3552 }
3553
3554 static void
3555 bttv_irq_wakeup_top(struct bttv *btv)
3556 {
3557         struct bttv_buffer *wakeup = btv->curr.top;
3558
3559         if (NULL == wakeup)
3560                 return;
3561
3562         spin_lock(&btv->s_lock);
3563         btv->curr.top_irq = 0;
3564         btv->curr.top = NULL;
3565         bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3566
3567         do_gettimeofday(&wakeup->vb.ts);
3568         wakeup->vb.field_count = btv->field_count;
3569         wakeup->vb.state = STATE_DONE;
3570         wake_up(&wakeup->vb.done);
3571         spin_unlock(&btv->s_lock);
3572 }
3573
3574 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3575 {
3576         if (rc < risc->dma)
3577                 return 0;
3578         if (rc > risc->dma + risc->size)
3579                 return 0;
3580         return 1;
3581 }
3582
3583 static void
3584 bttv_irq_switch_video(struct bttv *btv)
3585 {
3586         struct bttv_buffer_set new;
3587         struct bttv_buffer_set old;
3588         dma_addr_t rc;
3589
3590         spin_lock(&btv->s_lock);
3591
3592         /* new buffer set */
3593         bttv_irq_next_video(btv, &new);
3594         rc = btread(BT848_RISC_COUNT);
3595         if ((btv->curr.top    && is_active(&btv->curr.top->top,       rc)) ||
3596             (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3597                 btv->framedrop++;
3598                 if (debug_latency)
3599                         bttv_irq_debug_low_latency(btv, rc);
3600                 spin_unlock(&btv->s_lock);
3601                 return;
3602         }
3603
3604         /* switch over */
3605         old = btv->curr;
3606         btv->curr = new;
3607         btv->loop_irq &= ~1;
3608         bttv_buffer_activate_video(btv, &new);
3609         bttv_set_dma(btv, 0);
3610
3611         /* switch input */
3612         if (UNSET != btv->new_input) {
3613                 video_mux(btv,btv->new_input);
3614                 btv->new_input = UNSET;
3615         }
3616
3617         /* wake up finished buffers */
3618         bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3619         spin_unlock(&btv->s_lock);
3620 }
3621
3622 static void
3623 bttv_irq_switch_vbi(struct bttv *btv)
3624 {
3625         struct bttv_buffer *new = NULL;
3626         struct bttv_buffer *old;
3627         u32 rc;
3628
3629         spin_lock(&btv->s_lock);
3630
3631         if (!list_empty(&btv->vcapture))
3632                 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3633         old = btv->cvbi;
3634
3635         rc = btread(BT848_RISC_COUNT);
3636         if (NULL != old && (is_active(&old->top,    rc) ||
3637                             is_active(&old->bottom, rc))) {
3638                 btv->framedrop++;
3639                 if (debug_latency)
3640                         bttv_irq_debug_low_latency(btv, rc);
3641                 spin_unlock(&btv->s_lock);
3642                 return;
3643         }
3644
3645         /* switch */
3646         btv->cvbi = new;
3647         btv->loop_irq &= ~4;
3648         bttv_buffer_activate_vbi(btv, new);
3649         bttv_set_dma(btv, 0);
3650
3651         bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3652         spin_unlock(&btv->s_lock);
3653 }
3654
3655 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3656 {
3657         u32 stat,astat;
3658         u32 dstat;
3659         int count;
3660         struct bttv *btv;
3661         int handled = 0;
3662
3663         btv=(struct bttv *)dev_id;
3664         count=0;
3665         while (1) {
3666                 /* get/clear interrupt status bits */
3667                 stat=btread(BT848_INT_STAT);
3668                 astat=stat&btread(BT848_INT_MASK);
3669                 if (!astat)
3670                         break;
3671                 handled = 1;
3672                 btwrite(stat,BT848_INT_STAT);
3673
3674                 /* get device status bits */
3675                 dstat=btread(BT848_DSTATUS);
3676
3677                 if (irq_debug) {
3678                         printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3679                                "riscs=%x, riscc=%08x, ",
3680                                btv->c.nr, count, btv->field_count,
3681                                stat>>28, btread(BT848_RISC_COUNT));
3682                         bttv_print_irqbits(stat,astat);
3683                         if (stat & BT848_INT_HLOCK)
3684                                 printk("   HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3685                                        ? "yes" : "no");
3686                         if (stat & BT848_INT_VPRES)
3687                                 printk("   PRES => %s", (dstat & BT848_DSTATUS_PRES)
3688                                        ? "yes" : "no");
3689                         if (stat & BT848_INT_FMTCHG)
3690                                 printk("   NUML => %s", (dstat & BT848_DSTATUS_NUML)
3691                                        ? "625" : "525");
3692                         printk("\n");
3693                 }
3694
3695                 if (astat&BT848_INT_VSYNC)
3696                         btv->field_count++;
3697
3698                 if (astat & BT848_INT_GPINT) {
3699                         wake_up(&btv->gpioq);
3700                         bttv_gpio_irq(&btv->c);
3701                 }
3702
3703                 if (astat & BT848_INT_I2CDONE) {
3704                         btv->i2c_done = stat;
3705                         wake_up(&btv->i2c_queue);
3706                 }
3707
3708                 if ((astat & BT848_INT_RISCI)  &&  (stat & (4<<28)))
3709                         bttv_irq_switch_vbi(btv);
3710
3711                 if ((astat & BT848_INT_RISCI)  &&  (stat & (2<<28)))
3712                         bttv_irq_wakeup_top(btv);
3713
3714                 if ((astat & BT848_INT_RISCI)  &&  (stat & (1<<28)))
3715                         bttv_irq_switch_video(btv);
3716
3717                 if ((astat & BT848_INT_HLOCK)  &&  btv->opt_automute)
3718                         audio_mux(btv, -1);
3719
3720                 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3721                         printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3722                                (astat & BT848_INT_SCERR) ? "SCERR" : "",
3723                                (astat & BT848_INT_OCERR) ? "OCERR" : "",
3724                                btread(BT848_RISC_COUNT));
3725                         bttv_print_irqbits(stat,astat);
3726                         printk("\n");
3727                         if (bttv_debug)
3728                                 bttv_print_riscaddr(btv);
3729                 }
3730                 if (fdsr && astat & BT848_INT_FDSR) {
3731                         printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3732                                btv->c.nr,btread(BT848_RISC_COUNT));
3733                         if (bttv_debug)
3734                                 bttv_print_riscaddr(btv);
3735                 }
3736
3737                 count++;
3738                 if (count > 4) {
3739
3740                         if (count > 8 || !(astat & BT848_INT_GPINT)) {
3741                                 btwrite(0, BT848_INT_MASK);
3742
3743                                 printk(KERN_ERR
3744                                            "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3745                         } else {
3746                                 printk(KERN_ERR
3747                                            "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
3748
3749                                 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3750                                                 BT848_INT_MASK);
3751                         };
3752
3753                         bttv_print_irqbits(stat,astat);
3754
3755                         printk("]\n");
3756                 }
3757         }
3758         btv->irq_total++;
3759         if (handled)
3760                 btv->irq_me++;
3761         return IRQ_RETVAL(handled);
3762 }
3763
3764
3765 /* ----------------------------------------------------------------------- */
3766 /* initialitation                                                          */
3767
3768 static struct video_device *vdev_init(struct bttv *btv,
3769                                       struct video_device *template,
3770                                       char *type)
3771 {
3772         struct video_device *vfd;
3773
3774         vfd = video_device_alloc();
3775         if (NULL == vfd)
3776                 return NULL;
3777         *vfd = *template;
3778         vfd->minor   = -1;
3779         vfd->dev     = &btv->c.pci->dev;
3780         vfd->release = video_device_release;
3781         snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3782                  btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3783                  type, bttv_tvcards[btv->c.type].name);
3784         return vfd;
3785 }
3786
3787 static void bttv_unregister_video(struct bttv *btv)
3788 {
3789         if (btv->video_dev) {
3790                 if (-1 != btv->video_dev->minor)
3791                         video_unregister_device(btv->video_dev);
3792                 else
3793                         video_device_release(btv->video_dev);
3794                 btv->video_dev = NULL;
3795         }
3796         if (btv->vbi_dev) {
3797                 if (-1 != btv->vbi_dev->minor)
3798                         video_unregister_device(btv->vbi_dev);
3799                 else
3800                         video_device_release(btv->vbi_dev);
3801                 btv->vbi_dev = NULL;
3802         }
3803         if (btv->radio_dev) {
3804                 if (-1 != btv->radio_dev->minor)
3805                         video_unregister_device(btv->radio_dev);
3806                 else
3807                         video_device_release(btv->radio_dev);
3808                 btv->radio_dev = NULL;
3809         }
3810 }
3811
3812 /* register video4linux devices */
3813 static int __devinit bttv_register_video(struct bttv *btv)
3814 {
3815         if (no_overlay <= 0) {
3816                 bttv_video_template.type |= VID_TYPE_OVERLAY;
3817         } else {
3818                 printk("bttv: Overlay support disabled.\n");
3819         }
3820
3821         /* video */
3822         btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3823         if (NULL == btv->video_dev)
3824                 goto err;
3825         if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3826                 goto err;
3827         printk(KERN_INFO "bttv%d: registered device video%d\n",
3828                btv->c.nr,btv->video_dev->minor & 0x1f);
3829         video_device_create_file(btv->video_dev, &class_device_attr_card);
3830
3831         /* vbi */
3832         btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3833         if (NULL == btv->vbi_dev)
3834                 goto err;
3835         if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3836                 goto err;
3837         printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3838                btv->c.nr,btv->vbi_dev->minor & 0x1f);
3839
3840         if (!btv->has_radio)
3841                 return 0;
3842         /* radio */
3843         btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3844         if (NULL == btv->radio_dev)
3845                 goto err;
3846         if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3847                 goto err;
3848         printk(KERN_INFO "bttv%d: registered device radio%d\n",
3849                btv->c.nr,btv->radio_dev->minor & 0x1f);
3850
3851         /* all done */
3852         return 0;
3853
3854  err:
3855         bttv_unregister_video(btv);
3856         return -1;
3857 }
3858
3859
3860 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3861 /* response on cards with no firmware is not enabled by OF */
3862 static void pci_set_command(struct pci_dev *dev)
3863 {
3864 #if defined(__powerpc__)
3865         unsigned int cmd;
3866
3867         pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3868         cmd = (cmd | PCI_COMMAND_MEMORY );
3869         pci_write_config_dword(dev, PCI_COMMAND, cmd);
3870 #endif
3871 }
3872
3873 static int __devinit bttv_probe(struct pci_dev *dev,
3874                                 const struct pci_device_id *pci_id)
3875 {
3876         int result;
3877         unsigned char lat;
3878         struct bttv *btv;
3879
3880         if (bttv_num == BTTV_MAX)
3881                 return -ENOMEM;
3882         printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3883         btv=&bttvs[bttv_num];
3884         memset(btv,0,sizeof(*btv));
3885         btv->c.nr  = bttv_num;
3886         sprintf(btv->c.name,"bttv%d",btv->c.nr);
3887
3888         /* initialize structs / fill in defaults */
3889         init_MUTEX(&btv->lock);
3890         init_MUTEX(&btv->reslock);
3891         spin_lock_init(&btv->s_lock);
3892         spin_lock_init(&btv->gpio_lock);
3893         init_waitqueue_head(&btv->gpioq);
3894         init_waitqueue_head(&btv->i2c_queue);
3895         INIT_LIST_HEAD(&btv->c.subs);
3896         INIT_LIST_HEAD(&btv->capture);
3897         INIT_LIST_HEAD(&btv->vcapture);
3898         v4l2_prio_init(&btv->prio);
3899
3900         init_timer(&btv->timeout);
3901         btv->timeout.function = bttv_irq_timeout;
3902         btv->timeout.data     = (unsigned long)btv;
3903
3904         btv->i2c_rc = -1;
3905         btv->tuner_type  = UNSET;
3906         btv->pinnacle_id = UNSET;
3907         btv->new_input   = UNSET;
3908         btv->has_radio=radio[btv->c.nr];
3909
3910         /* pci stuff (init, get irq/mmio, ... */
3911         btv->c.pci = dev;
3912         btv->id  = dev->device;
3913         if (pci_enable_device(dev)) {
3914                 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3915                        btv->c.nr);
3916                 return -EIO;
3917         }
3918         if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
3919                 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3920                        btv->c.nr);
3921                 return -EIO;
3922         }
3923         if (!request_mem_region(pci_resource_start(dev,0),
3924                                 pci_resource_len(dev,0),
3925                                 btv->c.name)) {
3926                 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3927                        btv->c.nr, pci_resource_start(dev,0));
3928                 return -EBUSY;
3929         }
3930         pci_set_master(dev);
3931         pci_set_command(dev);
3932         pci_set_drvdata(dev,btv);
3933
3934         pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3935         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3936         printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3937                bttv_num,btv->id, btv->revision, pci_name(dev));
3938         printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3939                btv->c.pci->irq, lat, pci_resource_start(dev,0));
3940         schedule();
3941
3942         btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3943         if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3944                 printk("bttv%d: ioremap() failed\n", btv->c.nr);
3945                 result = -EIO;
3946                 goto fail1;
3947         }
3948
3949         /* identify card */
3950         bttv_idcard(btv);
3951
3952         /* disable irqs, register irq handler */
3953         btwrite(0, BT848_INT_MASK);
3954         result = request_irq(btv->c.pci->irq, bttv_irq,
3955                              SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
3956         if (result < 0) {
3957                 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3958                        bttv_num,btv->c.pci->irq);
3959                 goto fail1;
3960         }
3961
3962         if (0 != bttv_handle_chipset(btv)) {
3963                 result = -EIO;
3964                 goto fail2;
3965         }
3966
3967         /* init options from insmod args */
3968         btv->opt_combfilter = combfilter;
3969         btv->opt_lumafilter = lumafilter;
3970         btv->opt_automute   = automute;
3971         btv->opt_chroma_agc = chroma_agc;
3972         btv->opt_adc_crush  = adc_crush;
3973         btv->opt_vcr_hack   = vcr_hack;
3974         btv->opt_whitecrush_upper  = whitecrush_upper;
3975         btv->opt_whitecrush_lower  = whitecrush_lower;
3976         btv->opt_uv_ratio   = uv_ratio;
3977         btv->opt_full_luma_range   = full_luma_range;
3978         btv->opt_coring     = coring;
3979
3980         /* fill struct bttv with some useful defaults */
3981         btv->init.btv         = btv;
3982         btv->init.ov.w.width  = 320;
3983         btv->init.ov.w.height = 240;
3984         btv->init.fmt         = format_by_palette(VIDEO_PALETTE_RGB24);
3985         btv->init.width       = 320;
3986         btv->init.height      = 240;
3987         btv->init.lines       = 16;
3988         btv->input = 0;
3989
3990         /* initialize hardware */
3991         if (bttv_gpio)
3992                 bttv_gpio_tracking(btv,"pre-init");
3993
3994         bttv_risc_init_main(btv);
3995         init_bt848(btv);
3996
3997         /* gpio */
3998         btwrite(0x00, BT848_GPIO_REG_INP);
3999         btwrite(0x00, BT848_GPIO_OUT_EN);
4000         if (bttv_verbose)
4001                 bttv_gpio_tracking(btv,"init");
4002
4003         /* needs to be done before i2c is registered */
4004         bttv_init_card1(btv);
4005
4006         /* register i2c + gpio */
4007         init_bttv_i2c(btv);
4008
4009         /* some card-specific stuff (needs working i2c) */
4010         bttv_init_card2(btv);
4011         init_irqreg(btv);
4012
4013         /* register video4linux + input */
4014         if (!bttv_tvcards[btv->c.type].no_video) {
4015                 bttv_register_video(btv);
4016                 bt848_bright(btv,32768);
4017                 bt848_contrast(btv,32768);
4018                 bt848_hue(btv,32768);
4019                 bt848_sat(btv,32768);
4020                 audio_mux(btv,AUDIO_MUTE);
4021                 set_input(btv,0);
4022         }
4023
4024         /* add subdevices */
4025         if (btv->has_remote)
4026                 bttv_sub_add_device(&btv->c, "remote");
4027         if (bttv_tvcards[btv->c.type].has_dvb)
4028                 bttv_sub_add_device(&btv->c, "dvb");
4029
4030         /* everything is fine */
4031         bttv_num++;
4032         return 0;
4033
4034  fail2:
4035         free_irq(btv->c.pci->irq,btv);
4036
4037  fail1:
4038         if (btv->bt848_mmio)
4039                 iounmap(btv->bt848_mmio);
4040         release_mem_region(pci_resource_start(btv->c.pci,0),
4041                            pci_resource_len(btv->c.pci,0));
4042         pci_set_drvdata(dev,NULL);
4043         return result;
4044 }
4045
4046 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4047 {
4048         struct bttv *btv = pci_get_drvdata(pci_dev);
4049
4050         if (bttv_verbose)
4051                 printk("bttv%d: unloading\n",btv->c.nr);
4052
4053         /* shutdown everything (DMA+IRQs) */
4054         btand(~15, BT848_GPIO_DMA_CTL);
4055         btwrite(0, BT848_INT_MASK);
4056         btwrite(~0x0, BT848_INT_STAT);
4057         btwrite(0x0, BT848_GPIO_OUT_EN);
4058         if (bttv_gpio)
4059                 bttv_gpio_tracking(btv,"cleanup");
4060
4061         /* tell gpio modules we are leaving ... */
4062         btv->shutdown=1;
4063         wake_up(&btv->gpioq);
4064         bttv_sub_del_devices(&btv->c);
4065
4066         /* unregister i2c_bus + input */
4067         fini_bttv_i2c(btv);
4068
4069         /* unregister video4linux */
4070         bttv_unregister_video(btv);
4071
4072         /* free allocated memory */
4073         btcx_riscmem_free(btv->c.pci,&btv->main);
4074
4075         /* free ressources */
4076         free_irq(btv->c.pci->irq,btv);
4077         iounmap(btv->bt848_mmio);
4078         release_mem_region(pci_resource_start(btv->c.pci,0),
4079                            pci_resource_len(btv->c.pci,0));
4080
4081         pci_set_drvdata(pci_dev, NULL);
4082         return;
4083 }
4084
4085 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4086 {
4087         struct bttv *btv = pci_get_drvdata(pci_dev);
4088         struct bttv_buffer_set idle;
4089         unsigned long flags;
4090
4091         dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4092
4093         /* stop dma + irqs */
4094         spin_lock_irqsave(&btv->s_lock,flags);
4095         memset(&idle, 0, sizeof(idle));
4096         btv->state.video = btv->curr;
4097         btv->state.vbi   = btv->cvbi;
4098         btv->state.loop_irq = btv->loop_irq;
4099         btv->curr = idle;
4100         btv->loop_irq = 0;
4101         bttv_buffer_activate_video(btv, &idle);
4102         bttv_buffer_activate_vbi(btv, NULL);
4103         bttv_set_dma(btv, 0);
4104         btwrite(0, BT848_INT_MASK);
4105         spin_unlock_irqrestore(&btv->s_lock,flags);
4106
4107         /* save bt878 state */
4108         btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4109         btv->state.gpio_data   = gpio_read();
4110
4111         /* save pci state */
4112         pci_save_state(pci_dev);
4113         if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4114                 pci_disable_device(pci_dev);
4115                 btv->state.disabled = 1;
4116         }
4117         return 0;
4118 }
4119
4120 static int bttv_resume(struct pci_dev *pci_dev)
4121 {
4122         struct bttv *btv = pci_get_drvdata(pci_dev);
4123         unsigned long flags;
4124         int err;
4125
4126         dprintk("bttv%d: resume\n", btv->c.nr);
4127
4128         /* restore pci state */
4129         if (btv->state.disabled) {
4130                 err=pci_enable_device(pci_dev);
4131                 if (err) {
4132                         printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4133                                                                 btv->c.nr);
4134                         return err;
4135                 }
4136                 btv->state.disabled = 0;
4137         }
4138         err=pci_set_power_state(pci_dev, PCI_D0);
4139         if (err) {
4140                 pci_disable_device(pci_dev);
4141                 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4142                                                         btv->c.nr);
4143                 btv->state.disabled = 1;
4144                 return err;
4145         }
4146
4147         pci_restore_state(pci_dev);
4148
4149         /* restore bt878 state */
4150         bttv_reinit_bt848(btv);
4151         gpio_inout(0xffffff, btv->state.gpio_enable);
4152         gpio_write(btv->state.gpio_data);
4153
4154         /* restart dma */
4155         spin_lock_irqsave(&btv->s_lock,flags);
4156         btv->curr = btv->state.video;
4157         btv->cvbi = btv->state.vbi;
4158         btv->loop_irq = btv->state.loop_irq;
4159         bttv_buffer_activate_video(btv, &btv->curr);
4160         bttv_buffer_activate_vbi(btv, btv->cvbi);
4161         bttv_set_dma(btv, 0);
4162         spin_unlock_irqrestore(&btv->s_lock,flags);
4163         return 0;
4164 }
4165
4166 static struct pci_device_id bttv_pci_tbl[] = {
4167         {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4168          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4169         {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4170          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4171         {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4172          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4173         {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4174          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4175         {0,}
4176 };
4177
4178 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4179
4180 static struct pci_driver bttv_pci_driver = {
4181         .name     = "bttv",
4182         .id_table = bttv_pci_tbl,
4183         .probe    = bttv_probe,
4184         .remove   = __devexit_p(bttv_remove),
4185         .suspend  = bttv_suspend,
4186         .resume   = bttv_resume,
4187 };
4188
4189 static int bttv_init_module(void)
4190 {
4191         bttv_num = 0;
4192
4193         printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4194                (BTTV_VERSION_CODE >> 16) & 0xff,
4195                (BTTV_VERSION_CODE >> 8) & 0xff,
4196                BTTV_VERSION_CODE & 0xff);
4197 #ifdef SNAPSHOT
4198         printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4199                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4200 #endif
4201         if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4202                 gbuffers = 2;
4203         if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4204                 gbufsize = BTTV_MAX_FBUF;
4205         gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4206         if (bttv_verbose)
4207                 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4208                        gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4209
4210         bttv_check_chipset();
4211
4212         bus_register(&bttv_sub_bus_type);
4213         return pci_module_init(&bttv_pci_driver);
4214 }
4215
4216 static void bttv_cleanup_module(void)
4217 {
4218         pci_unregister_driver(&bttv_pci_driver);
4219         bus_unregister(&bttv_sub_bus_type);
4220         return;
4221 }
4222
4223 module_init(bttv_init_module);
4224 module_exit(bttv_cleanup_module);
4225
4226 /*
4227  * Local variables:
4228  * c-basic-offset: 8
4229  * End:
4230  */