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