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