Subversion Repositories shark

Rev

Rev 170 | Rev 184 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
170 giacomo 1
/*
2
    bttv - Bt848 frame grabber driver
3
 
4
    Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
5
                           & Marcus Metzler (mocm@thp.uni-koeln.de)
6
    (c) 1999,2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
7
 
8
    This program is free software; you can redistribute it and/or modify
9
    it under the terms of the GNU General Public License as published by
10
    the Free Software Foundation; either version 2 of the License, or
11
    (at your option) any later version.
12
 
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17
 
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
*/
22
 
23
/* SHARK version by Giacomo Guidi <giacomo@gandalf.sssup.it> */
24
 
25
#include <sys/types.h>
26
#include <string.h>
27
#include <stdio.h>
28
#include <unistd.h>
29
#include <stdlib.h>
30
#include <linux/pci.h>
31
 
32
#include "drivers/compbttv.h"
33
#include "drivers/bttv.h"
34
#include "drivers/tuner.h"
35
 
36
#define DEBUG(x)        /* Debug driver */
37
#define MIN(a,b) (((a)>(b))?(b):(a))
38
#define MAX(a,b) (((a)>(b))?(a):(b))
39
 
40
static inline int copy_to_user (void *s, void *d, size_t size)
41
{
42
        memcpy(s,d,size);
43
        return 0;
44
 
45
}
46
 
47
static inline int copy_from_user (void *s, void *d, size_t size)
48
{
49
        memcpy(s,d,size);
50
        return 0;
51
 
52
}
53
 
54
static void bt848_set_risc_jmps(struct bttv *btv, int state);
55
 
56
int bttv_num;                   /* number of Bt848s in use */
57
int in_irq = 0;
179 giacomo 58
DWORD oldpci_command;
170 giacomo 59
struct bttv *btvirq;
60
struct bttv bttvs[BTTV_MAX];
61
 
62
/* configuration variables */
63
#if defined(__sparc__) || defined(__powerpc__) || defined(__hppa__)
64
static unsigned int bigendian=1;
65
#else
66
static unsigned int bigendian=0;
67
#endif
68
static unsigned int radio[BTTV_MAX];
69
static unsigned int fieldnr = 0;
70
static unsigned int irq_debug = 0;
71
static unsigned int gbuffers = 2;
72
unsigned int gbufsize = BTTV_MAX_FBUF;
73
static int latency = -1;
74
 
75
static unsigned int combfilter = 0;
76
static unsigned int lumafilter = 0;
77
static unsigned int chroma_agc = 0;
78
static unsigned int adc_crush = 1;
79
unsigned int bttv_debug = 0;
80
unsigned int bttv_verbose = 1;
81
unsigned int bttv_gpio = 0;
82
 
83
//#define I2C_TIMING (0x7<<4)
84
//#define I2C_DELAY   10
85
 
86
#define I2C_TIMING (0x3<<4)
87
#define I2C_DELAY   40
88
 
89
#define I2C_SET(CTRL,DATA) \
90
    { btwrite((CTRL<<1)|(DATA), BT848_I2C); udelay(I2C_DELAY); }
91
#define I2C_GET()   (btread(BT848_I2C)&1)
92
 
93
#define BURSTOFFSET 76
94
#define BTTV_ERRORS 5
95
 
96
 
97
/*******************************/
98
/* Memory management functions */
99
/*******************************/
100
 
101
#define MDEBUG(x)       do { } while(0)         /* Debug memory management */
102
 
103
/* [DaveM] I've recoded most of this so that:
104
 * 1) It's easier to tell what is happening
105
 * 2) It's more portable, especially for translating things
106
 *    out of vmalloc mapped areas in the kernel.
107
 * 3) Less unnecessary translations happen.
108
 *
109
 * The code used to assume that the kernel vmalloc mappings
110
 * existed in the page tables of every process, this is simply
111
 * not guarenteed.  We now use pgd_offset_k which is the
112
 * defined way to get at the kernel page tables.
113
 */
114
 
115
 
116
static inline unsigned long cpu_to_le32(unsigned long adr)
117
{
118
        return adr;
119
}
120
 
121
static inline unsigned long virt_to_bus(unsigned long adr)
122
{
123
        return (unsigned long)(adr);
124
}
125
 
126
static void * vmalloc_32(size_t size)
127
{
128
        void *mem;
129
        unsigned long diff;
130
 
131
        mem = malloc(size+8);
132
 
133
        diff = (unsigned long)((((unsigned long)mem/4)+1)*4-(unsigned long)mem);
134
 
135
        *(unsigned long *)(mem+diff) = (unsigned long)diff;
136
 
137
        return (mem+diff+4);
138
 
139
}
140
 
141
static void vfree_32(void *ptr)
142
{
143
 
144
        free(ptr-4-*(unsigned long *)(ptr-4));
145
 
146
}
147
 
148
void bttv_gpio_tracking(struct bttv *btv, char *comment)
149
{
150
        unsigned int outbits, data;
151
        outbits = btread(BT848_GPIO_OUT_EN);
152
        data    = gpioread();
153
        printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
154
               btv->nr,outbits,data & outbits, data & ~outbits, comment);
155
}
156
 
157
static inline void bt848_dma(struct bttv *btv, uint state)
158
{
159
        if (state)
160
                btor(3, BT848_GPIO_DMA_CTL);
161
        else
162
                btand(~3, BT848_GPIO_DMA_CTL);
163
}
164
 
165
 
166
/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC*/
167
 
168
/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
169
   PLL_X = Reference pre-divider (0=1, 1=2)
170
   PLL_C = Post divider (0=6, 1=4)
171
   PLL_I = Integer input
172
   PLL_F = Fractional input
173
 
174
   F_input = 28.636363 MHz:
175
   PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
176
*/
177
 
178
static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
179
{
180
        unsigned char fl, fh, fi;
181
 
182
        /* prevent overflows */
183
        fin/=4;
184
        fout/=4;
185
 
186
        fout*=12;
187
        fi=fout/fin;
188
 
189
        fout=(fout%fin)*256;
190
        fh=fout/fin;
191
 
192
        fout=(fout%fin)*256;
193
        fl=fout/fin;
194
 
195
        /*printk("0x%02x 0x%02x 0x%02x\n", fi, fh, fl);*/
196
        btwrite(fl, BT848_PLL_F_LO);
197
        btwrite(fh, BT848_PLL_F_HI);
198
        btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
199
}
200
 
201
static int set_pll(struct bttv *btv)
202
{
203
        int i;
204
 
205
        if (!btv->pll.pll_crystal)
206
                return 0;
207
 
208
        if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
209
                /* no PLL needed */
210
                if (btv->pll.pll_current == 0) {
211
                        /* printk ("bttv%d: PLL: is off\n",btv->nr); */
212
                        return 0;
213
                }
214
                if (bttv_verbose)
215
                        printk(KERN_INFO "bttv%d: PLL: switching off\n",btv->nr);
216
                btwrite(0x00,BT848_TGCTRL);
217
                btwrite(0x00,BT848_PLL_XCI);
218
                btv->pll.pll_current = 0;
219
                return 0;
220
        }
221
 
222
        if (btv->pll.pll_ofreq == btv->pll.pll_current) {
223
                /* printk("bttv%d: PLL: no change required\n",btv->nr); */
224
                return 1;
225
        }
226
 
227
        if (bttv_verbose)
228
                cprintf("[info  ] bttv%d: PLL: %d => %d ... ",btv->nr,
229
                       btv->pll.pll_ifreq, btv->pll.pll_ofreq);
230
 
231
        set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
232
 
233
        /*  Let other people run while the PLL stabilizes */
234
        udelay(100000);
235
 
236
        for (i=0; i<100; i++)
237
        {
238
                if ((btread(BT848_DSTATUS)&BT848_DSTATUS_PLOCK))
239
                        btwrite(0,BT848_DSTATUS);
240
                else
241
                {
242
                        btwrite(0x08,BT848_TGCTRL);
243
                        btv->pll.pll_current = btv->pll.pll_ofreq;
244
                        if (bttv_verbose)
245
                                cprintf("ok\n");
246
                        return 1;
247
                }
248
                udelay(10000);
249
        }
250
        btv->pll.pll_current = 0;
251
        if (bttv_verbose)
252
                cprintf("oops\n");
253
        return -1;
254
}
255
 
256
static void bt848_muxsel(struct bttv *btv, unsigned int input)
257
{
258
        /* needed by RemoteVideo MX */
259
        btor(bttv_tvcards[btv->type].gpiomask2,BT848_GPIO_OUT_EN);
260
 
261
        /* This seems to get rid of some synchronization problems */
262
        btand(~(3<<5), BT848_IFORM);
263
        udelay(10000);
264
 
265
        input %= bttv_tvcards[btv->type].video_inputs;
266
        if (input==bttv_tvcards[btv->type].svhs)
267
        {
268
                btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
269
                btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
270
        }
271
        else
272
        {
273
                btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
274
                btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
275
        }
276
 
277
        btaor((bttv_tvcards[btv->type].muxsel[input]&3)<<5, ~(3<<5), BT848_IFORM);
278
 
279
        gpioaor(bttv_tvcards[btv->type].muxsel[input]>>4,
280
                ~bttv_tvcards[btv->type].gpiomask2);
281
 
282
        /* card specific hook */
283
        if( bttv_tvcards[btv->type].muxsel_hook )
284
                bttv_tvcards[btv->type].muxsel_hook ( btv, input );
285
 
286
        if (bttv_gpio)
287
                bttv_gpio_tracking(btv,"muxsel");
288
 
289
}
290
 
291
 
292
struct tvnorm
293
{
294
        u32 Fsc;
295
        u16 swidth, sheight; /* scaled standard width, height */
296
        u16 totalwidth;
297
        u8 adelay, bdelay, iform;
298
        u32 scaledtwidth;
299
        u16 hdelayx1, hactivex1;
300
        u16 vdelay;
301
        u8 vbipack;
302
};
303
 
304
static struct tvnorm tvnorms[] = {
305
        /* PAL-BDGHI */
306
        /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
307
        /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
308
        { 35468950,
309
          924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
310
          1135, 186, 924,
311
#ifdef VIDEODAT_HACK
312
          VBI_MAXLINES*2,
313
#else
314
          0x20,
315
#endif
316
          255},
317
 
318
        /* NTSC */
319
        { 28636363,
320
          768, 480,  910, 0x68, 0x5d, (BT848_IFORM_NTSC|BT848_IFORM_XT0),
321
          910, 128, 910, 0x1a, 144},
322
#if 0
323
        /* SECAM EAST */
324
        { 35468950,
325
          768, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
326
          944, 186, 922, 0x20, 255},
327
#else
328
        /* SECAM L */
329
        { 35468950,
330
          924, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
331
          1135, 186, 922, 0x20, 255},
332
#endif
333
        /* PAL-NC */
334
        { 28636363,
335
          640, 576,  910, 0x68, 0x5d, (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
336
          780, 130, 734, 0x1a, 144},
337
        /* PAL-M */
338
        { 28636363,
339
          640, 480, 910, 0x68, 0x5d, (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
340
          780, 135, 754, 0x1a, 144},
341
        /* PAL-N */
342
        { 35468950,
343
          768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
344
          944, 186, 922, 0x20, 144},
345
        /* NTSC-Japan */
346
        { 28636363,
347
          640, 480,  910, 0x68, 0x5d, (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
348
          780, 135, 754, 0x16, 144},
349
};
350
#define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
351
#define VBI_SPL 2044
352
 
353
/* RISC command to write one VBI data line */
354
#define VBI_RISC BT848_RISC_WRITE|VBI_SPL|BT848_RISC_EOL|BT848_RISC_SOL
355
 
356
static void make_vbitab(struct bttv *btv)
357
{
358
        int i;
359
        unsigned int *po=(unsigned int *) btv->vbi_odd;
360
        unsigned int *pe=(unsigned int *) btv->vbi_even;
361
 
362
        if (bttv_debug > 1)
363
                cprintf("bttv%d: vbi1: po=%08lx pe=%08lx\n",
364
                       btv->nr,virt_to_bus((unsigned long)po), virt_to_bus((unsigned long)pe));
365
 
366
        *(po++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(po++)=0;
367
        for (i=0; i<VBI_MAXLINES; i++)
368
        {
369
                *(po++)=cpu_to_le32(VBI_RISC);
370
                *(po++)=cpu_to_le32(virt_to_bus((unsigned long)btv->vbibuf+i*2048));
371
        }
372
        *(po++)=cpu_to_le32(BT848_RISC_JUMP);
373
        *(po++)=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_jmp+16));
374
 
375
        *(pe++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(pe++)=0;
376
        for (i=VBI_MAXLINES; i<VBI_MAXLINES*2; i++)
377
        {
378
                *(pe++)=cpu_to_le32(VBI_RISC);
379
                *(pe++)=cpu_to_le32(virt_to_bus((unsigned long)btv->vbibuf+i*2048));
380
        }
381
        *(pe++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(0x01<<16));
382
        *(pe++)=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_jmp+40));
383
 
384
        if (bttv_debug > 1)
385
                cprintf("bttv%d: vbi2: po=%08lx pe=%08lx\n",
386
                       btv->nr,virt_to_bus((unsigned long)po), virt_to_bus((unsigned long)pe));
387
}
388
 
389
static int fmtbppx2[16] = {
390
        8, 6, 4, 4, 4, 3, 2, 2, 4, 3, 0, 0, 0, 0, 2, 0
391
};
392
 
393
static int palette2fmt[] = {
394
        0,
395
        BT848_COLOR_FMT_Y8,
396
        BT848_COLOR_FMT_RGB8,
397
        BT848_COLOR_FMT_RGB16,
398
        BT848_COLOR_FMT_RGB24,
399
        BT848_COLOR_FMT_RGB32,
400
        BT848_COLOR_FMT_RGB15,
401
        BT848_COLOR_FMT_YUY2,
402
        BT848_COLOR_FMT_YUY2,
403
        -1,
404
        -1,
405
        -1,
406
        BT848_COLOR_FMT_RAW,
407
        BT848_COLOR_FMT_YCrCb422,
408
        BT848_COLOR_FMT_YCrCb411,
409
        BT848_COLOR_FMT_YCrCb422,
410
        BT848_COLOR_FMT_YCrCb411,
411
};
412
#define PALETTEFMT_MAX (sizeof(palette2fmt)/sizeof(int))
413
 
414
static int make_rawrisctab(struct bttv *btv, unsigned int *ro,
415
                           unsigned int *re, unsigned int *vbuf)
416
{
417
        unsigned long line;
418
        unsigned long bpl=1024;         /* bytes per line */
419
        unsigned long vadr = (unsigned long)vbuf;
420
 
421
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
422
        *(ro++)=cpu_to_le32(0);
423
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
424
        *(re++)=cpu_to_le32(0);
425
 
426
        /* In PAL 650 blocks of 256 DWORDs are sampled, but only if VDELAY
427
           is 2 and without separate VBI grabbing.
428
           We'll have to handle this inside the IRQ handler ... */
429
 
430
        for (line=0; line < 640; line++)
431
        {
432
                *(ro++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
433
                *(ro++)=cpu_to_le32(virt_to_bus(vadr));
434
                *(re++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
435
                *(re++)=cpu_to_le32(virt_to_bus(vadr+gbufsize/2));
436
                vadr+=bpl;
437
        }
438
 
439
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
440
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
441
        *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
442
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
443
 
444
        return 0;
445
}
446
 
447
static int  make_prisctab(struct bttv *btv, unsigned int *ro,
448
                          unsigned int *re,
449
                          unsigned int *vbuf, unsigned short width,
450
                          unsigned short height, unsigned short fmt)
451
{
452
        unsigned long line, lmask;
453
        unsigned long bl, blcr, blcb, rcmd;
454
        unsigned long todo;
455
        unsigned int **rp;
456
        int inter;
457
        unsigned long cbadr, cradr;
458
        unsigned long vadr=(unsigned long) vbuf;
459
        int shift, csize;      
460
 
461
        if (bttv_debug > 1)
462
                cprintf("bttv%d: prisc1: ro=%08lx re=%08lx\n",
463
                       btv->nr,virt_to_bus((unsigned long)ro), virt_to_bus((unsigned long)re));
464
 
465
        switch(fmt)
466
        {
467
        case VIDEO_PALETTE_YUV422P:
468
                csize=(width*height)>>1;
469
                shift=1;
470
                lmask=0;
471
                break;
472
 
473
        case VIDEO_PALETTE_YUV411P:
474
                csize=(width*height)>>2;
475
                shift=2;
476
                lmask=0;
477
                break;
478
 
479
         case VIDEO_PALETTE_YUV420P:
480
                csize=(width*height)>>2;
481
                shift=1;
482
                lmask=1;
483
                break;
484
 
485
         case VIDEO_PALETTE_YUV410P:
486
                csize=(width*height)>>4;
487
                shift=2;
488
                lmask=3;
489
                break;
490
 
491
        default:
492
                return -1;
493
        }
494
        cbadr=vadr+(width*height);
495
        cradr=cbadr+csize;
496
        inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
497
 
498
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
499
        *(ro++)=0;
500
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
501
        *(re++)=0;
502
 
503
        for (line=0; line < (height<<(1^inter)); line++)
504
        {
505
                if(line==height)
506
                {
507
                        vadr+=csize<<1;
508
                        cbadr=vadr+(width*height);
509
                        cradr=cbadr+csize;
510
                }
511
                if (inter)
512
                        rp= (line&1) ? &re : &ro;
513
                else
514
                        rp= (line>=height) ? &ro : &re;
515
 
516
 
517
                if(line&lmask)
518
                        rcmd=BT848_RISC_WRITE1S23|BT848_RISC_SOL;
519
                else
520
                        rcmd=BT848_RISC_WRITE123|BT848_RISC_SOL;
521
 
522
                todo=width;
523
                while(todo)
524
                {
525
                 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
526
                 blcr=(PAGE_SIZE-((PAGE_SIZE-1)&cradr))<<shift;
527
                 blcb=(PAGE_SIZE-((PAGE_SIZE-1)&cbadr))<<shift;
528
                 bl=(blcr<bl) ? blcr : bl;
529
                 bl=(blcb<bl) ? blcb : bl;
530
                 bl=(bl>todo) ? todo : bl;
531
                 blcr=bl>>shift;
532
                 blcb=blcr;
533
                 /* bl now containts the longest row that can be written */
534
                 todo-=bl;
535
                 if(!todo) rcmd|=BT848_RISC_EOL; /* if this is the last EOL */
536
 
537
                 *((*rp)++)=cpu_to_le32(rcmd|bl);
538
                 *((*rp)++)=cpu_to_le32(blcb|(blcr<<16));
539
                 *((*rp)++)=cpu_to_le32(virt_to_bus(vadr));
540
                 vadr+=bl;
541
                 if((rcmd&(15<<28))==BT848_RISC_WRITE123)
542
                 {
543
                        *((*rp)++)=cpu_to_le32(virt_to_bus(cbadr));
544
                        cbadr+=blcb;
545
                        *((*rp)++)=cpu_to_le32(virt_to_bus(cradr));
546
                        cradr+=blcr;
547
                 }
548
 
549
                 rcmd&=~BT848_RISC_SOL; /* only the first has SOL */
550
                }
551
        }
552
 
553
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
554
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
555
        *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
556
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
557
 
558
        if (bttv_debug > 1)
559
                cprintf("bttv%d: prisc2: ro=%08lx re=%08lx\n",
560
                       btv->nr,virt_to_bus((unsigned long)ro), virt_to_bus((unsigned long)re));
561
 
562
        return 0;
563
}
564
 
565
static int  make_vrisctab(struct bttv *btv, unsigned int *ro,
566
                          unsigned int *re,
567
                          unsigned int *vbuf, unsigned short width,
568
                          unsigned short height, unsigned short palette)
569
{
570
        unsigned long line;
571
        unsigned long bpl;  /* bytes per line */
572
        unsigned int **rp;
573
        int inter;
574
        unsigned long vadr=(unsigned long) vbuf;
575
 
576
        if (palette==VIDEO_PALETTE_RAW)
577
                return make_rawrisctab(btv, ro, re, vbuf);
578
        if (palette>=VIDEO_PALETTE_PLANAR)
579
                return make_prisctab(btv, ro, re, vbuf, width, height, palette);
580
 
581
        if (bttv_debug > 1)
582
                cprintf("bttv%d: vrisc1: ro=%08lx re=%08lx\n",
583
                       btv->nr,virt_to_bus((unsigned long)ro), virt_to_bus((unsigned long)re));
584
 
585
        inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
586
        bpl=width*fmtbppx2[palette2fmt[palette]&0xf]/2;
587
 
588
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
589
        *(ro++)=cpu_to_le32(0);
590
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
591
        *(re++)=cpu_to_le32(0);
592
 
593
        for (line=0; line < (height<<(1^inter)); line++)
594
        {
595
                if (inter)
596
                        rp= (line&1) ? &re : &ro;
597
                else
598
                        rp= (line>=height) ? &ro : &re;
599
 
600
                        *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
601
                                BT848_RISC_EOL|bpl);
602
                        *((*rp)++)=cpu_to_le32(virt_to_bus(vadr));
603
                        vadr+=bpl;
604
        }
605
 
606
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
607
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
608
        *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
609
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
610
 
611
        if (bttv_debug > 1)
612
                cprintf("bttv%d: vrisc2: ro=%08lx re=%08lx\n",
613
                       btv->nr,virt_to_bus((unsigned long)ro), virt_to_bus((unsigned long)re));
614
 
615
        return 0;
616
}
617
 
618
static unsigned char lmaskt[8] =
619
{ 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
620
static unsigned char rmaskt[8] =
621
{ 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
622
 
623
static void clip_draw_rectangle(unsigned char *clipmap, int x, int y, int w, int h)
624
{
625
        unsigned char lmask, rmask, *p;
626
        int W, l, r;
627
        int i;
628
 
629
        if (bttv_debug > 1)
630
                cprintf("bttv clip: %dx%d+%d+%d\n",w,h,x,y);
631
 
632
        /* bitmap is fixed width, 128 bytes (1024 pixels represented) */
633
        if (x<0)
634
        {
635
                w+=x;
636
                x=0;
637
        }
638
        if (y<0)
639
        {
640
                h+=y;
641
                y=0;
642
        }
643
        if (w < 0 || h < 0)     /* catch bad clips */
644
                return;
645
        /* out of range data should just fall through */
646
        if (y+h>=625)
647
                h=625-y;
648
        if (x+w>=1024)
649
                w=1024-x;
650
 
651
        l=x>>3;
652
        r=(x+w-1)>>3;
653
        W=r-l-1;
654
        lmask=lmaskt[x&7];
655
        rmask=rmaskt[(x+w-1)&7];
656
        p=clipmap+128*y+l;
657
 
658
        if (W>0)
659
        {
660
                for (i=0; i<h; i++, p+=128)
661
                {
662
                        *p|=lmask;
663
                        memset(p+1, 0xff, W);
664
                        p[W+1]|=rmask;
665
                }
666
        } else if (!W) {
667
                for (i=0; i<h; i++, p+=128)
668
                {
669
                        p[0]|=lmask;
670
                        p[1]|=rmask;
671
                }
672
        } else {
673
                for (i=0; i<h; i++, p+=128)
674
                        p[0]|=lmask&rmask;
675
        }
676
 
677
 
678
}
679
 
680
static void make_clip_tab(struct bttv *btv, struct video_clip *cr, int ncr)
681
{
682
        int i, line, x, y, bpl, width, height, inter, maxw;
683
        unsigned int bpp, dx, sx, **rp, *ro, *re, flags, len;
684
        unsigned long adr;
685
        unsigned char *clipmap, *clipline, cbit, lastbit, outofmem;
686
 
687
        /* take care: bpp != btv->win.bpp is allowed here */
688
        bpp = fmtbppx2[btv->win.color_fmt&0xf]/2;
689
        bpl=btv->win.bpl;
690
        adr=btv->win.vidadr + btv->win.x * btv->win.bpp + btv->win.y * bpl;
691
        inter=(btv->win.interlace&1)^1;
692
        width=btv->win.width;
693
        height=btv->win.height;
694
        if (bttv_debug > 1)
695
                cprintf("bttv%d: clip1: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
696
                       btv->nr,btv->picture.palette,width,height,bpl,bpp);
697
        if(width > 1023)
698
                width = 1023;           /* sanity check */
699
        if(height > 625)
700
                height = 625;           /* sanity check */
701
        ro=(unsigned int *)btv->risc_scr_odd;
702
        re=(unsigned int *)btv->risc_scr_even;
703
 
704
        if (bttv_debug)
705
                cprintf("bttv%d: clip: ro=%08lx re=%08lx\n",
706
                       btv->nr,virt_to_bus((unsigned long)ro), virt_to_bus((unsigned long)re));
707
 
708
        if ((clipmap=vmalloc_32(VIDEO_CLIPMAP_SIZE))==NULL) {
709
                /* can't clip, don't generate any risc code */
710
                *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
711
                *(ro++)=cpu_to_le32(btv->bus_vbi_even);
712
                *(re++)=cpu_to_le32(BT848_RISC_JUMP);
713
                *(re++)=cpu_to_le32(btv->bus_vbi_odd);
714
        }
715
        if (ncr < 0) {  /* bitmap was pased */
716
                memcpy(clipmap, (unsigned char *)cr, VIDEO_CLIPMAP_SIZE);
717
        } else {        /* convert rectangular clips to a bitmap */
718
                memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
719
                for (i=0; i<ncr; i++)
720
                        clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
721
                                cr[i].width, cr[i].height);
722
        }
723
        /* clip against viewing window AND screen
724
           so we do not have to rely on the user program
725
         */
726
        maxw = (bpl - btv->win.x * btv->win.bpp) / bpp;
727
        clip_draw_rectangle(clipmap, (width > maxw) ? maxw : width,
728
                            0, 1024, 768);
729
        clip_draw_rectangle(clipmap,0,(btv->win.y+height>btv->win.sheight) ?
730
                            (btv->win.sheight-btv->win.y) : height,1024,768);
731
        if (btv->win.x<0)
732
                clip_draw_rectangle(clipmap, 0, 0, -(btv->win.x), 768);
733
        if (btv->win.y<0)
734
                clip_draw_rectangle(clipmap, 0, 0, 1024, -(btv->win.y));
735
 
736
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
737
        *(ro++)=cpu_to_le32(0);
738
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
739
        *(re++)=cpu_to_le32(0);
740
 
741
        /* translate bitmap to risc code */
742
        for (line=outofmem=0; line < (height<<inter) && !outofmem; line++)
743
        {
744
                y = line>>inter;
745
                rp= (line&1) ? &re : &ro;
746
                clipline = clipmap + (y<<7); /* running pointers ... */
747
                lastbit = *clipline & 1;
748
                for(x=dx=0,sx=0; x<=width && !outofmem;) {
749
                        if (0 == (x&7)) {
750
                                /* check bytes not bits if we can ... */
751
                                if (lastbit) {
752
                                        while (0xff==*clipline && x<width-8) {
753
                                                x  += 8;
754
                                                dx += 8;
755
                                                clipline++;
756
                                        }
757
                                } else {
758
                                        while (0x00==*clipline && x<width-8) {
759
                                                x  += 8;
760
                                                dx += 8;
761
                                                clipline++;
762
                                        }
763
                                }
764
                        }
765
                        cbit = *clipline & (1<<(x&7));
766
                        if (x < width && !lastbit == !cbit) {
767
                                dx++;
768
                        } else {
769
                                /* generate the dma controller code */
770
                                len = dx * bpp;
771
                                flags = ((bpp==4) ? BT848_RISC_BYTE3 : 0);
772
                                flags |= ((!sx) ? BT848_RISC_SOL : 0);
773
                                flags |= ((sx + dx == width) ? BT848_RISC_EOL : 0);
774
                                if (!lastbit) {
775
                                        *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|flags|len);
776
                                        *((*rp)++)=cpu_to_le32(adr + bpp * sx);
777
                                } else {
778
                                        *((*rp)++)=cpu_to_le32(BT848_RISC_SKIP|flags|len);
779
                                }
780
                                lastbit=cbit;
781
                                sx += dx;
782
                                dx = 1;
783
                                if (ro - (unsigned int *)btv->risc_scr_odd>(RISCMEM_LEN>>3) - 16)
784
                                        outofmem++;
785
                                if (re - (unsigned int *)btv->risc_scr_even>(RISCMEM_LEN>>3) - 16)
786
                                        outofmem++;
787
                        }
788
                        x++;
789
                        if (0 == (x&7))
790
                                clipline++;
791
                }
792
                if ((!inter)||(line&1))
793
                        adr+=bpl;
794
        }
795
 
796
        vfree_32(clipmap);
797
        /* outofmem flag relies on the following code to discard extra data */
798
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
799
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
800
        *(re++)=cpu_to_le32(BT848_RISC_JUMP);
801
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
802
 
803
        if (bttv_debug > 1)
804
                cprintf("bttv%d: clip2: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
805
                       btv->nr,btv->picture.palette,width,height,bpl,bpp);
806
}
807
 
808
/*
809
 *      Set the registers for the size we have specified. Don't bother
810
 *      trying to understand this without the BT848 manual in front of
811
 *      you [AC].
812
 *
813
 *      PS: The manual is free for download in .pdf format from
814
 *      www.brooktree.com - nicely done those folks.
815
 */
816
 
817
static inline void bt848_set_eogeo(struct bttv *btv, struct tvnorm *tvn,
818
                                   int odd, int width, int height)
819
{
820
        u16 vscale, hscale;
821
        u32 xsf, sr;
822
        u16 hdelay;
823
        u8 crop, vtc;
824
        int inter = (height>tvn->sheight/2) ? 0 : 1;
825
        int off = odd ? 0x80 : 0x00;
826
 
827
        xsf = (width*tvn->scaledtwidth)/tvn->swidth;
828
        hscale = ((tvn->totalwidth*4096UL)/xsf-4096);
829
        hdelay =  tvn->hdelayx1;
830
        hdelay =  (hdelay*width)/tvn->swidth;
831
        hdelay &= 0x3fe;
832
        sr=((tvn->sheight>>inter)*512)/height-512;
833
        vscale=(0x10000UL-sr)&0x1fff;
834
        crop=((width>>8)&0x03)|((hdelay>>6)&0x0c)|
835
                ((tvn->sheight>>4)&0x30)|((tvn->vdelay>>2)&0xc0);
836
        vscale |= inter ? (BT848_VSCALE_INT<<8) : 0;
837
 
838
        if (combfilter) {
839
                /* Some people say interpolation looks bad ... */
840
                vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
841
                if (width < 769)
842
                        btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
843
                else
844
                        btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
845
        } else {
846
                vtc = 0;
847
                btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
848
        }
849
 
850
        btwrite(vtc, BT848_E_VTC+off);
851
        btwrite(hscale>>8, BT848_E_HSCALE_HI+off);
852
        btwrite(hscale&0xff, BT848_E_HSCALE_LO+off);
853
        btaor((vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
854
        btwrite(vscale&0xff, BT848_E_VSCALE_LO+off);
855
        btwrite(width&0xff, BT848_E_HACTIVE_LO+off);
856
        btwrite(hdelay&0xff, BT848_E_HDELAY_LO+off);
857
        btwrite(tvn->sheight&0xff, BT848_E_VACTIVE_LO+off);
858
        btwrite(tvn->vdelay&0xff, BT848_E_VDELAY_LO+off);
859
        btwrite(crop, BT848_E_CROP+off);
860
}
861
 
862
 
863
static void bt848_set_geo(struct bttv *btv)
864
{
865
        u16 ewidth, eheight, owidth, oheight;
866
        u16 format, bswap;
867
        struct tvnorm *tvn;
868
 
869
        tvn=&tvnorms[btv->win.norm];
870
 
871
        btwrite(tvn->adelay, BT848_ADELAY);
872
        btwrite(tvn->bdelay, BT848_BDELAY);
873
        btaor(tvn->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH), BT848_IFORM);
874
        btwrite(tvn->vbipack, BT848_VBI_PACK_SIZE);
875
        btwrite(1, BT848_VBI_PACK_DEL);
876
 
877
        btv->pll.pll_ofreq = tvn->Fsc;
878
        if (!in_irq) set_pll(btv);
879
 
880
        btv->win.interlace = (btv->win.height>tvn->sheight/2) ? 1 : 0;
881
 
882
        if (0 == btv->risc_cap_odd &&
883
 
884
                /* overlay only */
885
                owidth  = btv->win.width;
886
                oheight = btv->win.height;
887
                ewidth  = btv->win.width;
888
                eheight = btv->win.height;
889
                format  = btv->win.color_fmt;
890
                bswap   = btv->fb_color_ctl;
891
        } else if (-1 != btv->gq_grab      &&
892
 
893
                   !btv->win.interlace     &&
894
                   btv->scr_on) {
895
                /* odd field -> overlay, even field -> capture */
896
                owidth  = btv->win.width;
897
                oheight = btv->win.height;
898
                ewidth  = btv->gbuf[btv->gq_grab].width;
899
                eheight = btv->gbuf[btv->gq_grab].height;
900
                format  = (btv->win.color_fmt & 0xf0) |
901
                        (btv->gbuf[btv->gq_grab].fmt & 0x0f);
902
                bswap   = btv->fb_color_ctl & 0x0a;
903
        } else {
904
                /* capture only */
905
                owidth  = btv->gbuf[btv->gq_grab].width;
906
                oheight = btv->gbuf[btv->gq_grab].height;
907
                ewidth  = btv->gbuf[btv->gq_grab].width;
908
                eheight = btv->gbuf[btv->gq_grab].height;
909
                format  = btv->gbuf[btv->gq_grab].fmt;
910
                bswap   = 0;
911
        }
912
 
913
        /* program odd + even fields */
914
        bt848_set_eogeo(btv, tvn, 1, owidth, oheight);
915
        bt848_set_eogeo(btv, tvn, 0, ewidth, eheight);
916
 
917
        btwrite(format, BT848_COLOR_FMT);
918
        btwrite(bswap | BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
919
}
920
 
921
 
922
static int bpp2fmt[4] = {
923
        BT848_COLOR_FMT_RGB8, BT848_COLOR_FMT_RGB16,
924
        BT848_COLOR_FMT_RGB24, BT848_COLOR_FMT_RGB32
925
};
926
 
927
static void bt848_set_winsize(struct bttv *btv)
928
{
929
        unsigned short format;
930
 
931
        if (btv->picture.palette > 0 && btv->picture.palette <= VIDEO_PALETTE_YUV422) {
932
                /* format set by VIDIOCSPICT */
933
                format = palette2fmt[btv->picture.palette];
934
        } else {
935
                /* use default for the given color depth */
936
                format = (btv->win.depth==15) ? BT848_COLOR_FMT_RGB15 :
937
                        bpp2fmt[(btv->win.bpp-1)&3];
938
        }
939
        btv->win.color_fmt = format;
940
        if (bigendian &&
941
            format == BT848_COLOR_FMT_RGB32) {
942
                btv->fb_color_ctl =
943
                        BT848_COLOR_CTL_WSWAP_ODD       |
944
                        BT848_COLOR_CTL_WSWAP_EVEN      |
945
                        BT848_COLOR_CTL_BSWAP_ODD       |
946
                        BT848_COLOR_CTL_BSWAP_EVEN;
947
        } else if (bigendian &&
948
                   (format == BT848_COLOR_FMT_RGB16 ||
949
                    format == BT848_COLOR_FMT_RGB15)) {
950
                btv->fb_color_ctl =
951
                        BT848_COLOR_CTL_BSWAP_ODD       |
952
                        BT848_COLOR_CTL_BSWAP_EVEN;
953
        } else {
954
                btv->fb_color_ctl = 0;
955
        }
956
 
957
        /*      RGB8 seems to be a 9x5x5 GRB color cube starting at
958
         *      color 16. Why the h... can't they even mention this in the
959
         *      data sheet?  [AC - because it's a standard format so I guess
960
         *      it never occurred to them]
961
         *      Enable dithering in this mode.
962
         */
963
 
964
        if (format==BT848_COLOR_FMT_RGB8)
965
                btand(~BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
966
        else
967
                btor(BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
968
 
969
        bt848_set_geo(btv);
970
}
971
 
972
/*
973
 *      Grab into virtual memory.
974
 */
975
static int vgrab(struct bttv *btv, struct video_mmap *mp)
976
{
977
        unsigned int *ro, *re;
978
        unsigned int *vbuf;
979
 
980
        if(btv->fbuffer==NULL)
179 giacomo 981
                return -ENOBUFS;
170 giacomo 982
 
983
        if(mp->frame >= gbuffers || mp->frame < 0)
984
                return -EINVAL;
985
 
986
        if(mp->height < 32 || mp->width < 48)
987
                return -EINVAL;
988
        if (mp->format >= PALETTEFMT_MAX)
989
                return -EINVAL;
990
 
991
        if (mp->height*mp->width*fmtbppx2[palette2fmt[mp->format]&0x0f]/2
992
            > gbufsize)
993
                return -EINVAL;
994
        if (-1 == palette2fmt[mp->format])
995
                return -EINVAL;
996
 
997
        /*
998
         *      Ok load up the BT848
999
         */
1000
 
1001
        vbuf=(unsigned int *)(btv->fbuffer+gbufsize*mp->frame);
1002
        ro=(unsigned int *)btv->gbuf[mp->frame].risc;
1003
        re=ro+2048;
1004
        make_vrisctab(btv, ro, re, vbuf, mp->width, mp->height, mp->format);
1005
 
1006
        if (bttv_debug)
1007
                printk("bttv%d: cap vgrab: queue %d (%d:%dx%d)\n",
1008
                       btv->nr,mp->frame,mp->format,mp->width,mp->height);
1009
        btv->gbuf[mp->frame].stat    = GBUFFER_GRABBING;
1010
        btv->gbuf[mp->frame].fmt     = palette2fmt[mp->format];
1011
        btv->gbuf[mp->frame].width   = mp->width;
1012
        btv->gbuf[mp->frame].height  = mp->height;
1013
        btv->gbuf[mp->frame].ro      = virt_to_bus((unsigned long)ro);
1014
        btv->gbuf[mp->frame].re      = virt_to_bus((unsigned long)re);
1015
 
1016
#if 1
1017
        if (mp->height <= tvnorms[btv->win.norm].sheight/2 &&
1018
            mp->format != VIDEO_PALETTE_RAW)
1019
                btv->gbuf[mp->frame].ro = 0;
1020
#endif
1021
 
1022
        if (-1 == btv->gq_grab && btv->gq_in == btv->gq_out) {
1023
                btv->gq_start = 1;
1024
                btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
1025
        }
1026
        btv->gqueue[btv->gq_in++] = mp->frame;
1027
        btv->gq_in = btv->gq_in % MAX_GBUFFERS;
1028
 
1029
        btor(3, BT848_CAP_CTL);
1030
        btor(3, BT848_GPIO_DMA_CTL);
1031
        return 0;
1032
}
1033
 
1034
static inline void burst(int on)
1035
{
1036
        tvnorms[0].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1037
        tvnorms[0].hdelayx1     = 186  - (on?BURSTOFFSET  :0);
1038
        tvnorms[2].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1039
        tvnorms[2].hdelayx1     = 186  - (on?BURSTOFFSET  :0);
1040
}
1041
 
1042
/*
1043
 * called from irq handler on fatal errors.  Takes the grabber chip
1044
 * offline, flag it needs a reinitialization (which can't be done
1045
 * from irq context) and wake up all sleeping proccesses.  They would
1046
 * block forever else.  We also need someone who actually does the
1047
 * reinitialization from process context...
1048
 */
1049
static void bt848_offline(struct bttv *btv)
1050
{
1051
        int i;
1052
 
1053
        /* cancel all outstanding grab requests */
1054
        btv->gq_in = 0;
1055
        btv->gq_out = 0;
1056
        btv->gq_grab = -1;
1057
        for (i = 0; i < gbuffers; i++)
1058
                if (btv->gbuf[i].stat == GBUFFER_GRABBING)
1059
                        btv->gbuf[i].stat = GBUFFER_ERROR;
1060
 
1061
        /* disable screen overlay and DMA */
1062
        btv->risc_cap_odd  = 0;
1063
        btv->risc_cap_even = 0;
1064
        bt848_set_risc_jmps(btv,0);
1065
 
1066
        /* flag the chip needs a restart */
1067
        btv->needs_restart = 1;
1068
 
1069
}
1070
 
1071
static void bt848_restart(struct bttv *btv)
1072
{
1073
        if (bttv_verbose)
1074
                printk(KERN_INFO "bttv%d: resetting chip\n",btv->nr);
1075
        btwrite(0xfffffUL, BT848_INT_STAT);
1076
        btand(~15, BT848_GPIO_DMA_CTL);
1077
        btwrite(0, BT848_SRESET);
1078
        btwrite(virt_to_bus((unsigned long)btv->risc_jmp+8),
1079
                BT848_RISC_STRT_ADD);
1080
 
1081
        /* enforce pll reprogramming */
1082
        btv->pll.pll_current = 0;
1083
        set_pll(btv);
1084
 
1085
        btv->errors = 0;
1086
        btv->needs_restart = 0;
1087
        bt848_set_geo(btv);
1088
        bt848_set_risc_jmps(btv,-1);
1089
}
1090
 
1091
/*
1092
 *      Open a bttv card. Right now the flags stuff is just playing
1093
 */
1094
 
1095
int bttv_open(struct bttv *btv)
1096
{
1097
        int i,ret;
1098
 
1099
        ret = -EBUSY;
1100
        if (bttv_debug)
1101
                cprintf("bttv%d: open called\n",btv->nr);
1102
 
1103
        btv->fbuffer=(unsigned char *) vmalloc_32(gbuffers*gbufsize);
1104
        ret = -ENOMEM;
1105
 
1106
        btv->gq_in = 0;
1107
        btv->gq_out = 0;
1108
        btv->gq_grab = -1;
1109
        for (i = 0; i < gbuffers; i++)
1110
                btv->gbuf[i].stat = GBUFFER_UNUSED;
1111
 
1112
        burst(0);
1113
        set_pll(btv);
1114
        btv->user++;
1115
        return 0;
1116
 
1117
}
1118
 
1119
void bttv_close(struct bttv *btv)
1120
{
1121
        unsigned long tmp;
1122
        int need_wait;
1123
 
179 giacomo 1124
        vfree_32(btv->fbuffer);
1125
        btv->fbuffer = NULL;
1126
 
170 giacomo 1127
        btv->user--;
1128
        need_wait = (-1 != btv->gq_grab);
1129
        btv->gq_start = 0;
1130
        btv->gq_in = 0;
1131
        btv->gq_out = 0;
1132
        btv->gq_grab = -1;
1133
        btv->scr_on = 0;
1134
        btv->risc_cap_odd = 0;
1135
        btv->risc_cap_even = 0;
1136
        bt848_set_risc_jmps(btv,-1);
1137
 
1138
        handler_remove(btv->dev->irq);
1139
 
1140
        /*
1141
         *      A word of warning. At this point the chip
1142
         *      is still capturing because its FIFO hasn't emptied
1143
         *      and the DMA control operations are posted PCI
1144
         *      operations.
1145
         */
179 giacomo 1146
 
1147
        tmp = btread(BT848_I2C);        /* This fixes the PCI posting delay */
170 giacomo 1148
 
179 giacomo 1149
        pci_write_config_dword(btv->dev,PCI_COMMAND,oldpci_command);
1150
 
170 giacomo 1151
}
1152
 
1153
/***********************************/
1154
/* ioctls and supporting functions */
1155
/***********************************/
1156
 
1157
static inline void bt848_bright(struct bttv *btv, uint bright)
1158
{
1159
        btwrite(bright&0xff, BT848_BRIGHT);
1160
}
1161
 
1162
static inline void bt848_hue(struct bttv *btv, uint hue)
1163
{
1164
        btwrite(hue&0xff, BT848_HUE);
1165
}
1166
 
1167
static inline void bt848_contrast(struct bttv *btv, uint cont)
1168
{
1169
        unsigned int conthi;
1170
 
1171
        conthi=(cont>>6)&4;
1172
        btwrite(cont&0xff, BT848_CONTRAST_LO);
1173
        btaor(conthi, ~4, BT848_E_CONTROL);
1174
        btaor(conthi, ~4, BT848_O_CONTROL);
1175
}
1176
 
1177
static inline void bt848_sat_u(struct bttv *btv, unsigned long data)
1178
{
1179
        u32 datahi;
1180
 
1181
        datahi=(data>>7)&2;
1182
        btwrite(data&0xff, BT848_SAT_U_LO);
1183
        btaor(datahi, ~2, BT848_E_CONTROL);
1184
        btaor(datahi, ~2, BT848_O_CONTROL);
1185
}
1186
 
1187
static inline void bt848_sat_v(struct bttv *btv, unsigned long data)
1188
{
1189
        u32 datahi;
1190
 
1191
        datahi=(data>>8)&1;
1192
        btwrite(data&0xff, BT848_SAT_V_LO);
1193
        btaor(datahi, ~1, BT848_E_CONTROL);
1194
        btaor(datahi, ~1, BT848_O_CONTROL);
1195
}
1196
 
1197
/*
1198
 *      ioctl routine
1199
 */
1200
 
1201
int bttv_ioctl(struct bttv *btv, unsigned int cmd, void *arg)
1202
{
1203
        int i;
1204
 
1205
        if (bttv_debug > 1)
1206
                cprintf("bttv%d: ioctl 0x%x\n",btv->nr,cmd);
1207
 
1208
        switch (cmd) {
1209
        case VIDIOCGCAP:
1210
        {
1211
                struct video_capability b;
1212
                strcpy(b.name,btv->video_dev.name);
1213
                b.type = VID_TYPE_CAPTURE|
1214
                        ((bttv_tvcards[btv->type].tuner != -1) ? VID_TYPE_TUNER : 0) |
1215
                        VID_TYPE_OVERLAY|
1216
                        VID_TYPE_CLIPPING|
1217
                        VID_TYPE_FRAMERAM|
1218
                        VID_TYPE_SCALES;
1219
                b.channels = bttv_tvcards[btv->type].video_inputs;
1220
                b.audios = bttv_tvcards[btv->type].audio_inputs;
1221
                b.maxwidth = tvnorms[btv->win.norm].swidth;
1222
                b.maxheight = tvnorms[btv->win.norm].sheight;
1223
                b.minwidth = 48;
1224
                b.minheight = 32;
1225
                if(copy_to_user(arg,&b,sizeof(b)))
1226
                        return -EFAULT;
1227
                return 0;
1228
        }
1229
        case VIDIOCGCHAN:
1230
        {
1231
                struct video_channel v;
1232
                if(copy_from_user(&v, arg,sizeof(v)))
1233
                        return -EFAULT;
1234
                v.flags=VIDEO_VC_AUDIO;
1235
                v.tuners=0;
1236
                v.type=VIDEO_TYPE_CAMERA;
1237
                v.norm = btv->win.norm;
1238
                if (v.channel>=bttv_tvcards[btv->type].video_inputs)
1239
                        return -EINVAL;
1240
                if(v.channel==bttv_tvcards[btv->type].tuner)
1241
                {
1242
                        strcpy(v.name,"Television");
1243
                        v.flags|=VIDEO_VC_TUNER;
1244
                        v.type=VIDEO_TYPE_TV;
1245
                        v.tuners=1;
1246
                }
1247
                else if(v.channel==bttv_tvcards[btv->type].svhs)
1248
                        strcpy(v.name,"S-Video");
1249
                else
1250
                        sprintf(v.name,"Composite%d",v.channel);
1251
 
1252
                if(copy_to_user(arg,&v,sizeof(v)))
1253
                        return -EFAULT;
1254
                return 0;
1255
        }
1256
        /*
1257
         *      Each channel has 1 tuner
1258
         */
1259
        case VIDIOCSCHAN:
1260
        {
1261
                struct video_channel v;
1262
                if(copy_from_user(&v, arg,sizeof(v)))
1263
                        return -EFAULT;
1264
 
1265
                if (v.channel>bttv_tvcards[btv->type].video_inputs)
1266
                        return -EINVAL;
1267
                if (v.norm > (sizeof(tvnorms)/sizeof(*tvnorms)))
1268
                        return -EOPNOTSUPP;
1269
 
1270
                bttv_call_i2c_clients(btv,cmd,&v);
1271
                bt848_muxsel(btv, v.channel);
1272
                btv->channel=v.channel;
1273
                if (btv->win.norm != v.norm) {
1274
                        if(btv->type== BTTV_VOODOOTV_FM)
1275
                                bttv_tda9880_setnorm(btv,v.norm);
1276
                        btv->win.norm = v.norm;
1277
                        make_vbitab(btv);
1278
                        bt848_set_winsize(btv);
1279
                }
1280
                return 0;
1281
        }
1282
        case VIDIOCGTUNER:
1283
        {
1284
                struct video_tuner v;
1285
                if(copy_from_user(&v,arg,sizeof(v))!=0)
1286
                        return -EFAULT;
1287
#if 0 /* tuner.signal might be of intrest for non-tuner sources too ... */
1288
                if(v.tuner||btv->channel)       /* Only tuner 0 */
1289
                        return -EINVAL;
1290
#endif
1291
                strcpy(v.name, "Television");
1292
                v.rangelow=0;
1293
                v.rangehigh=0xFFFFFFFF;
1294
                v.flags=VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1295
                v.mode = btv->win.norm;
1296
                v.signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1297
                bttv_call_i2c_clients(btv,cmd,&v);
1298
                if(copy_to_user(arg,&v,sizeof(v)))
1299
                        return -EFAULT;
1300
                return 0;
1301
        }
1302
        /* We have but one tuner */
1303
        case VIDIOCSTUNER:
1304
        {
1305
                struct video_tuner v;
1306
                if(copy_from_user(&v, arg, sizeof(v)))
1307
                        return -EFAULT;
1308
                /* Only one channel has a tuner */
1309
                if(v.tuner!=bttv_tvcards[btv->type].tuner)
1310
                        return -EINVAL;
1311
 
1312
                if(v.mode!=VIDEO_MODE_PAL&&v.mode!=VIDEO_MODE_NTSC
1313
                   &&v.mode!=VIDEO_MODE_SECAM)
1314
                        return -EOPNOTSUPP;
1315
                bttv_call_i2c_clients(btv,cmd,&v);
1316
                if (btv->win.norm != v.mode) {
1317
                        btv->win.norm = v.mode;
1318
                        set_pll(btv);
1319
                        make_vbitab(btv);
1320
                        bt848_set_winsize(btv);
1321
                }
1322
                return 0;
1323
        }
1324
        case VIDIOCGPICT:
1325
        {
1326
                struct video_picture p=btv->picture;
1327
                if(copy_to_user(arg, &p, sizeof(p)))
1328
                        return -EFAULT;
1329
                return 0;
1330
        }
1331
        case VIDIOCSPICT:
1332
        {
1333
                struct video_picture p;
1334
                if (copy_from_user(&p, arg,sizeof(p)))
1335
                        return -EFAULT;
1336
                if (p.palette > PALETTEFMT_MAX)
1337
                        return -EINVAL;
1338
                if (-1 == palette2fmt[p.palette])
1339
                        return -EINVAL;
1340
                /* We want -128 to 127 we get 0-65535 */
1341
                bt848_bright(btv, (p.brightness>>8)-128);
1342
                /* 0-511 for the colour */
1343
                bt848_sat_u(btv, p.colour>>7);
1344
                bt848_sat_v(btv, ((p.colour>>7)*201L)/237);
1345
                /* -128 to 127 */
1346
                bt848_hue(btv, (p.hue>>8)-128);
1347
                /* 0-511 */
1348
                bt848_contrast(btv, p.contrast>>7);
1349
                btv->picture = p;
1350
                return 0;
1351
        }
1352
        case VIDIOCSWIN:
1353
        {
1354
                struct video_window vw;
1355
                struct video_clip *vcp = NULL;
1356
 
1357
                if(copy_from_user(&vw,arg,sizeof(vw)))
1358
                        return -EFAULT;
1359
 
1360
                if(vw.flags || vw.width < 16 || vw.height < 16)
1361
                {
1362
                        btv->scr_on = 0;
1363
                        bt848_set_risc_jmps(btv,-1);
1364
                        return -EINVAL;
1365
                }
1366
                if (btv->win.bpp < 4)
1367
                {       /* adjust and align writes */
1368
                        vw.x = (vw.x + 3) & ~3;
1369
                        vw.width &= ~3;
1370
                }
1371
                if (btv->needs_restart)
1372
                        bt848_restart(btv);
1373
                btv->win.x=vw.x;
1374
                btv->win.y=vw.y;
1375
                btv->win.width=vw.width;
1376
                btv->win.height=vw.height;
1377
 
1378
                bt848_set_risc_jmps(btv,0);
1379
                bt848_set_winsize(btv);
1380
 
1381
                /*
1382
                 *      Do any clips.
1383
                 */
1384
                if(vw.clipcount<0) {
1385
                        if((vcp=vmalloc_32(VIDEO_CLIPMAP_SIZE))==NULL) {
1386
                                return -ENOMEM;
1387
                        }
1388
                        if(copy_from_user(vcp, vw.clips,
1389
                                          VIDEO_CLIPMAP_SIZE)) {
1390
                                vfree_32(vcp);
1391
                                return -EFAULT;
1392
                        }
1393
                } else if (vw.clipcount > 2048) {
1394
                        return -EINVAL;
1395
                } else if (vw.clipcount) {
1396
                        if((vcp=vmalloc_32(sizeof(struct video_clip)*
1397
                                        (vw.clipcount))) == NULL) {
1398
                                return -ENOMEM;
1399
                        }
1400
                        if(copy_from_user(vcp,vw.clips,
1401
                                          sizeof(struct video_clip)*
1402
                                          vw.clipcount)) {
1403
                                vfree_32(vcp);
1404
                                return -EFAULT;
1405
                        }
1406
                }
1407
                make_clip_tab(btv, vcp, vw.clipcount);
1408
                if (vw.clipcount != 0)
1409
                        vfree_32(vcp);
1410
                bt848_set_risc_jmps(btv,-1);
1411
                return 0;
1412
        }
1413
        case VIDIOCGWIN:
1414
        {
1415
                struct video_window vw;
1416
                memset(&vw,0,sizeof(vw));
1417
                vw.x=btv->win.x;
1418
                vw.y=btv->win.y;
1419
                vw.width=btv->win.width;
1420
                vw.height=btv->win.height;
1421
                if(btv->win.interlace)
1422
                        vw.flags|=VIDEO_WINDOW_INTERLACE;
1423
                if(copy_to_user(arg,&vw,sizeof(vw)))
1424
                        return -EFAULT;
1425
                return 0;
1426
        }
1427
        case VIDIOCCAPTURE:
1428
        {
1429
                int v;
1430
                if(copy_from_user(&v, arg,sizeof(v)))
1431
                        return -EFAULT;
1432
                if(btv->win.vidadr == 0)
1433
                        return -EINVAL;
1434
                if (btv->win.width==0 || btv->win.height==0)
1435
                        return -EINVAL;
1436
                if (1 == no_overlay)
1437
                        return -EIO;
1438
                if (v == 1 && btv->win.vidadr != 0)
1439
                        btv->scr_on = 1;
1440
                if (v == 0)
1441
                        btv->scr_on = 0;
1442
                bt848_set_risc_jmps(btv,-1);
1443
                return 0;
1444
        }
1445
        case VIDIOCGFBUF:
1446
        {
1447
                struct video_buffer v;
1448
                v.base=(void *)btv->win.vidadr;
1449
                v.height=btv->win.sheight;
1450
                v.width=btv->win.swidth;
1451
                v.depth=btv->win.depth;
1452
                v.bytesperline=btv->win.bpl;
1453
                if(copy_to_user(arg, &v,sizeof(v)))
1454
                        return -EFAULT;
1455
                return 0;
1456
 
1457
        }
1458
        case VIDIOCSFBUF:
1459
        {
1460
                struct video_buffer v;
1461
                if(copy_from_user(&v, arg,sizeof(v)))
1462
                        return -EFAULT;
1463
                if(v.depth!=8 && v.depth!=15 && v.depth!=16 &&
1464
                   v.depth!=24 && v.depth!=32 && v.width > 16 &&
1465
                   v.height > 16 && v.bytesperline > 16)
1466
                        return -EINVAL;
1467
                if (v.base)
1468
                        btv->win.vidadr=(unsigned long)v.base;
1469
                btv->win.sheight=v.height;
1470
                btv->win.swidth=v.width;
1471
                btv->win.bpp=((v.depth+7)&0x38)/8;
1472
                btv->win.depth=v.depth;
1473
                btv->win.bpl=v.bytesperline;
1474
 
1475
#if 0 /* was broken for ages and nobody noticed.  Looks like we don't need
1476
         it any more as everybody explicitly sets the palette using VIDIOCSPICT
1477
         these days */
1478
                /* set sefault color format */
1479
                switch (v.depth) {
1480
                case  8: btv->picture.palette = VIDEO_PALETTE_HI240;  break;
1481
                case 15: btv->picture.palette = VIDEO_PALETTE_RGB555; break;
1482
                case 16: btv->picture.palette = VIDEO_PALETTE_RGB565; break;
1483
                case 24: btv->picture.palette = VIDEO_PALETTE_RGB24;  break;
1484
                case 32: btv->picture.palette = VIDEO_PALETTE_RGB32;  break;
1485
                }
1486
#endif
1487
 
1488
                if (bttv_debug)
1489
                        cprintf("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1490
                               v.base, v.width,v.height, btv->win.bpp, btv->win.bpl);
1491
                bt848_set_winsize(btv);
1492
                return 0;              
1493
        }
1494
        case VIDIOCKEY:
1495
        {
1496
                /* Will be handled higher up .. */
1497
                return 0;
1498
        }
1499
        case VIDIOCGFREQ:
1500
        {
1501
                unsigned long v=btv->win.freq;
1502
                if(copy_to_user(arg,&v,sizeof(v)))
1503
                        return -EFAULT;
1504
                return 0;
1505
        }
1506
        case VIDIOCSFREQ:
1507
        {
1508
                unsigned long v;
1509
                if(copy_from_user(&v, arg, sizeof(v)))
1510
                        return -EFAULT;
1511
                btv->win.freq=v;
1512
                bttv_call_i2c_clients(btv,cmd,&v);
1513
                return 0;
1514
        }
1515
 
1516
        case VIDIOCSYNC:
1517
        {
1518
                return (btv->gbuf[0].stat == GBUFFER_DONE);
1519
 
1520
        }
1521
 
1522
        case BTTV_FIELDNR:
1523
                if(copy_to_user((void *) arg, (void *) &btv->last_field,
1524
                                sizeof(btv->last_field)))
1525
                        return -EFAULT;
1526
                break;
1527
 
1528
        case BTTV_PLLSET: {
1529
                struct bttv_pll_info p;
1530
                if(!capable(CAP_SYS_ADMIN))
1531
                        return -EPERM;
1532
                if(copy_from_user(&p , (void *) arg, sizeof(btv->pll)))
1533
                        return -EFAULT;
1534
                btv->pll.pll_ifreq = p.pll_ifreq;
1535
                btv->pll.pll_ofreq = p.pll_ofreq;
1536
                btv->pll.pll_crystal = p.pll_crystal;
1537
                break;
1538
        }
1539
 
1540
        case VIDIOCMCAPTURE:
1541
        {
1542
                struct video_mmap vm;
1543
                int ret;
1544
                if(copy_from_user((void *) &vm, (void *) arg, sizeof(vm)))
1545
                        return -EFAULT;
1546
                ret = vgrab(btv, &vm);
1547
                return ret;
1548
        }
1549
 
1550
        case VIDIOCGMBUF:
1551
        {
1552
                struct video_mbuf vm;
1553
                memset(&vm, 0 , sizeof(vm));
1554
                vm.size=gbufsize*gbuffers;
1555
                vm.frames=gbuffers;
1556
                for (i = 0; i < gbuffers; i++)
1557
                        vm.offsets[i]=i*gbufsize;
1558
                if(copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
1559
                        return -EFAULT;
1560
                return 0;
1561
        }
1562
 
1563
        case VIDIOCGUNIT:
1564
        {
1565
                struct video_unit vu;
1566
                vu.video=btv->video_dev.minor;
1567
                vu.vbi=btv->vbi_dev.minor;
1568
                if(btv->radio_dev.minor!=-1)
1569
                        vu.radio=btv->radio_dev.minor;
1570
                else
1571
                        vu.radio=VIDEO_NO_UNIT;
1572
                vu.audio=VIDEO_NO_UNIT;
1573
                vu.teletext=VIDEO_NO_UNIT;
1574
                if(copy_to_user((void *)arg, (void *)&vu, sizeof(vu)))
1575
                        return -EFAULT;
1576
                return 0;
1577
        }
1578
 
1579
        case BTTV_BURST_ON:
1580
        {
1581
                burst(1);
1582
                return 0;
1583
        }
1584
 
1585
        case BTTV_BURST_OFF:
1586
        {
1587
                burst(0);
1588
                return 0;
1589
        }
1590
 
1591
        case BTTV_VERSION:
1592
        {
1593
                return 0;
1594
        }
1595
 
1596
        case BTTV_PICNR:
1597
        {
1598
                /* return picture;*/
1599
                return  0;
1600
        }
1601
 
1602
        default:
1603
                return 0;
1604
        }
1605
        return 0;
1606
}
1607
 
1608
static void bt848_set_risc_jmps(struct bttv *btv, int flags)
1609
{
1610
        if (-1 == flags) {
1611
                /* defaults */
1612
                flags = 0;
1613
                if (btv->scr_on)
1614
                        flags |= 0x03;
1615
                if (btv->vbi_on)
1616
                        flags |= 0x0c;
1617
#if 0
1618
                /* Hmm ... */
1619
                if ((0 != btv->risc_cap_even) ||
1620
                    (0 != btv->risc_cap_odd))
1621
                        flags |= 0x0c;
1622
#endif
1623
        }
1624
 
1625
        if (bttv_debug > 1)
1626
                cprintf("bttv%d: set_risc_jmp %08lx:",
1627
                       btv->nr,virt_to_bus((unsigned long)btv->risc_jmp));
1628
 
1629
        /* Sync to start of odd field */
1630
        btv->risc_jmp[0]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
1631
                                |BT848_FIFO_STATUS_VRE);
1632
        btv->risc_jmp[1]=cpu_to_le32(0);
1633
 
1634
        /* Jump to odd vbi sub */
1635
        btv->risc_jmp[2]=cpu_to_le32(BT848_RISC_JUMP|(0xd<<20));
1636
        if (flags&8) {
1637
                if (bttv_debug > 1)
1638
                        cprintf(" ev=%08lx",virt_to_bus((unsigned long)btv->vbi_odd));
1639
                btv->risc_jmp[3]=cpu_to_le32(virt_to_bus((unsigned long)btv->vbi_odd));
1640
        } else {
1641
                if (bttv_debug > 1)
1642
                        cprintf(" -----------");
1643
                btv->risc_jmp[3]=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_jmp+16));
1644
        }
1645
 
1646
        /* Jump to odd sub */
1647
        btv->risc_jmp[4]=cpu_to_le32(BT848_RISC_JUMP|(0xe<<20));
1648
        if (0 != btv->risc_cap_odd) {
1649
                if (bttv_debug > 1)
1650
                        cprintf(" e%d=%08lx",btv->gq_grab,btv->risc_cap_odd);
1651
                flags |= 3;
1652
                btv->risc_jmp[5]=cpu_to_le32(btv->risc_cap_odd);
1653
        } else if ((flags&2) &&
1654
                   (!btv->win.interlace || 0 == btv->risc_cap_even)) {
1655
                if (bttv_debug > 1)
1656
                        cprintf(" eo=%08lx",virt_to_bus((unsigned long)btv->risc_scr_odd));
1657
                btv->risc_jmp[5]=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_scr_odd));
1658
        } else {
1659
                if (bttv_debug > 1)
1660
                        cprintf(" -----------");
1661
                btv->risc_jmp[5]=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_jmp+24));
1662
        }
1663
 
1664
 
1665
        /* Sync to start of even field */
1666
        btv->risc_jmp[6]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
1667
                                |BT848_FIFO_STATUS_VRO);
1668
        btv->risc_jmp[7]=cpu_to_le32(0);
1669
 
1670
        /* Jump to even vbi sub */
1671
        btv->risc_jmp[8]=cpu_to_le32(BT848_RISC_JUMP);
1672
        if (flags&4) {
1673
                if (bttv_debug > 1)
1674
                        cprintf(" ov=%08lx",virt_to_bus((unsigned long)btv->vbi_even));
1675
                btv->risc_jmp[9]=cpu_to_le32(virt_to_bus((unsigned long)btv->vbi_even));
1676
        } else {
1677
                if (bttv_debug > 1)
1678
                        cprintf(" -----------");
1679
                btv->risc_jmp[9]=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_jmp+40));
1680
        }
1681
 
1682
        /* Jump to even sub */
1683
        btv->risc_jmp[10]=cpu_to_le32(BT848_RISC_JUMP|(8<<20));
1684
        if (0 != btv->risc_cap_even) {
1685
                if (bttv_debug > 1)
1686
                        cprintf(" o%d=%08lx",btv->gq_grab,btv->risc_cap_even);
1687
                flags |= 3;
1688
                btv->risc_jmp[11]=cpu_to_le32(btv->risc_cap_even);
1689
        } else if ((flags&1) &&
1690
                   btv->win.interlace) {
1691
                if (bttv_debug > 1)
1692
                        cprintf(" oo=%08lx",virt_to_bus((unsigned long)btv->risc_scr_even));
1693
                btv->risc_jmp[11]=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_scr_even));
1694
        } else {
1695
                if (bttv_debug > 1)
1696
                        cprintf(" -----------");
1697
                btv->risc_jmp[11]=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_jmp+48));
1698
        }
1699
 
1700
        if (btv->gq_start) {
1701
                btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
1702
        } else {
1703
                btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP);
1704
        }
1705
        btv->risc_jmp[13]=cpu_to_le32(virt_to_bus((unsigned long)btv->risc_jmp));
1706
 
1707
        /* enable cpaturing and DMA */
1708
        if (bttv_debug > 1)
1709
                cprintf(" flags=0x%x dma=%s\n",
1710
                       flags,(flags&0x0f) ? "on" : "off");
1711
        btaor(flags, ~0x0f, BT848_CAP_CTL);
1712
        if (flags&0x0f)
1713
                bt848_dma(btv, 3);
1714
        else
1715
                bt848_dma(btv, 0);
1716
}
1717
 
1718
static int init_bt848(struct bttv *btv)
1719
{
1720
        int j,val;
1721
 
1722
        btv->user=0;
1723
 
1724
        /* bringup the gpio internals (quick! before anyone uses it) */
1725
        init_gpio_adapter(&(btv->gpio_adap));
1726
 
1727
        /* dump current state of the gpio registers before changing them,
1728
         * might help to make a new card work */
1729
        if (bttv_gpio)
1730
                bttv_gpio_tracking(btv,"init #1");
1731
 
1732
 
1733
        /* reset the bt848 */
1734
        btwrite(0, BT848_SRESET);
1735
        DEBUG(printk(KERN_DEBUG "bttv%d: bt848_mem: 0x%lx\n", btv->nr, (unsigned long) btv->bt848_mem));
1736
 
1737
        /* not registered yet */
1738
        btv->video_dev.minor = -1;
1739
        btv->radio_dev.minor = -1;
1740
        btv->vbi_dev.minor = -1;
1741
 
1742
        /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
1743
        btv->win.norm=0; /* change this to 1 for NTSC, 2 for SECAM */
1744
        btv->win.interlace=1;
1745
        btv->win.x=0;
1746
        btv->win.y=0;
1747
        btv->win.width=320;
1748
        btv->win.height=240;
1749
        btv->win.bpp=2;
1750
        btv->win.depth=16;
1751
        btv->win.color_fmt=BT848_COLOR_FMT_RGB16;
1752
        btv->win.bpl=1024*btv->win.bpp;
1753
        btv->win.swidth=1024;
1754
        btv->win.sheight=768;
1755
        btv->win.vidadr=0;
1756
        btv->vbi_on=0;
1757
        btv->scr_on=0;
1758
 
1759
        btv->risc_scr_odd=0;
1760
        btv->risc_scr_even=0;
1761
        btv->risc_cap_odd=0;
1762
        btv->risc_cap_even=0;
1763
        btv->risc_jmp=0;
1764
        btv->vbibuf=0;
1765
        btv->field=btv->last_field=0;
1766
 
1767
        btv->errors=0;
1768
        btv->needs_restart=0;
1769
        btv->has_radio=radio[btv->nr];
1770
 
1771
        if (!(btv->risc_scr_odd= (unsigned long *) vmalloc_32(RISCMEM_LEN/2)))
1772
                return -1;
1773
        if (!(btv->risc_scr_even=(unsigned long *) vmalloc_32(RISCMEM_LEN/2)))
1774
                return -1;
1775
        if (!(btv->risc_jmp =(unsigned long *) vmalloc_32(2048)))
1776
                return -1;
1777
        btv->vbi_odd=btv->risc_jmp+64;
1778
        btv->vbi_even=btv->vbi_odd+1024;
1779
        btv->bus_vbi_odd=virt_to_bus((unsigned long)btv->risc_jmp+48);
1780
        btv->bus_vbi_even=virt_to_bus((unsigned long)btv->risc_jmp+24);
1781
 
1782
        btwrite(virt_to_bus((unsigned long)btv->risc_jmp+8), BT848_RISC_STRT_ADD);
1783
        btv->vbibuf=(unsigned char *) vmalloc_32(VBIBUF_SIZE);
1784
        if (!btv->vbibuf)
1785
                return -1;
1786
        if (!(btv->gbuf = vmalloc_32(sizeof(struct bttv_gbuf)*gbuffers)))
1787
                return -1;
1788
        for (j = 0; j < gbuffers; j++) {
1789
                if (!(btv->gbuf[j].risc = vmalloc_32(16384)))
1790
                        return -1;
1791
        }
1792
 
1793
        memset(btv->vbibuf, 0, VBIBUF_SIZE); /* We don't want to return random
1794
                                                memory to the user */
1795
 
1796
        btv->fbuffer=NULL;
1797
 
1798
/*      btwrite(0, BT848_TDEC); */
1799
        btwrite(0x10, BT848_COLOR_CTL);
1800
        btwrite(0x00, BT848_CAP_CTL);
1801
        /* set planar and packed mode trigger points and         */
1802
        /* set rising edge of inverted GPINTR pin as irq trigger */
1803
        btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1804
                BT848_GPIO_DMA_CTL_PLTP1_16|
1805
                BT848_GPIO_DMA_CTL_PLTP23_16|
1806
                BT848_GPIO_DMA_CTL_GPINTC|
1807
                BT848_GPIO_DMA_CTL_GPINTI,
1808
                BT848_GPIO_DMA_CTL);
1809
 
1810
        /* select direct input */
1811
        btwrite(0x00, BT848_GPIO_REG_INP);
1812
        btwrite(0x00, BT848_GPIO_OUT_EN);
1813
        if (bttv_gpio)
1814
                bttv_gpio_tracking(btv,"init #2");
1815
 
1816
        btwrite(BT848_IFORM_MUX1 | BT848_IFORM_XTAUTO | BT848_IFORM_AUTO,
1817
                BT848_IFORM);
1818
 
1819
        btwrite(0xd8, BT848_CONTRAST_LO);
1820
        bt848_bright(btv, 0x10);
1821
 
1822
        btwrite(0x20, BT848_E_VSCALE_HI);
1823
        btwrite(0x20, BT848_O_VSCALE_HI);
1824
        btwrite(BT848_ADC_RESERVED | (adc_crush ? BT848_ADC_CRUSH : 0),
1825
                BT848_ADC);
1826
 
1827
        if (lumafilter) {
1828
                btwrite(0, BT848_E_CONTROL);
1829
                btwrite(0, BT848_O_CONTROL);
1830
        } else {
1831
                btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1832
                btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1833
        }
1834
 
1835
        btv->picture.colour=254<<7;
1836
        btv->picture.brightness=128<<8;
1837
        btv->picture.hue=128<<8;
1838
        btv->picture.contrast=0xd8<<7;
1839
 
1840
        val = chroma_agc ? BT848_SCLOOP_CAGC : 0;
1841
        btwrite(val, BT848_E_SCLOOP);
1842
        btwrite(val, BT848_O_SCLOOP);
1843
 
1844
        /* clear interrupt status */
1845
        btwrite(0xfffffUL, BT848_INT_STAT);
1846
 
1847
        /* set interrupt mask */
1848
        btwrite(btv->triton1|
1849
                /*BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|
1850
                  BT848_INT_FDSR|BT848_INT_FTRGT|BT848_INT_FBUS|*/
1851
                (fieldnr ? BT848_INT_VSYNC : 0)|
1852
                BT848_INT_GPINT|
1853
                BT848_INT_SCERR|
1854
                BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1855
                BT848_INT_FMTCHG|BT848_INT_HLOCK,
1856
                BT848_INT_MASK);
1857
 
1858
        bt848_muxsel(btv, 1);
1859
        bt848_set_winsize(btv);
1860
        make_vbitab(btv);
1861
        bt848_set_risc_jmps(btv,-1);
1862
 
1863
        /* needs to be done before i2c is registered */
1864
        bttv_init_card1(btv);
1865
 
1866
        /* register i2c */
1867
        btv->tuner_type=-1;
1868
        init_bttv_i2c(btv);
1869
 
1870
        /* some card-specific stuff (needs working i2c) */
1871
        bttv_init_card2(btv);
1872
 
1873
        return 0;
1874
}
1875
 
1876
/* ----------------------------------------------------------------------- */
1877
 
1878
static char *irq_name[] = { "FMTCHG", "VSYNC", "HSYNC", "OFLOW", "HLOCK",
1879
                            "VPRES", "6", "7", "I2CDONE", "GPINT", "10",
1880
                            "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
1881
                            "RIPERR", "PABORT", "OCERR", "SCERR" };
1882
 
1883
static void bttv_irq(void)
1884
{
1885
        u32 stat,astat;
1886
        u32 dstat;
1887
        int count;
1888
        struct bttv *btv = btvirq;
1889
 
1890
        in_irq = 1;
1891
        count=0;
1892
        while (1)
1893
        {
1894
                /* get/clear interrupt status bits */
1895
                stat=btread(BT848_INT_STAT);
1896
                astat=stat&btread(BT848_INT_MASK);
1897
                if (!astat) {
1898
                        in_irq = 0;
1899
                        return;
1900
                }
1901
                btwrite(stat,BT848_INT_STAT);
1902
 
1903
                /* get device status bits */
1904
                dstat=btread(BT848_DSTATUS);
1905
 
1906
                if (irq_debug) {
1907
                        int i;
1908
                        cprintf("bttv%d: irq loop=%d risc=%lx, bits:",
1909
                               btv->nr, count, stat>>28);
1910
                        for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) {
1911
                                if (stat & (1 << i))
1912
                                        cprintf(" %s",irq_name[i]);
1913
                                if (astat & (1 << i))
1914
                                        cprintf(" *");
1915
                        }
1916
                        if (stat & BT848_INT_HLOCK)
1917
                                cprintf("   HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
1918
                                       ? "yes" : "no");
1919
                        if (stat & BT848_INT_VPRES)
1920
                                cprintf("   PRES => %s", (dstat & BT848_DSTATUS_PRES)
1921
                                       ? "yes" : "no");
1922
                        if (stat & BT848_INT_FMTCHG)
1923
                                cprintf("   NUML => %s", (dstat & BT848_DSTATUS_PRES)
1924
                                       ? "625" : "525");
1925
                        cprintf("\n");
1926
                }
1927
 
1928
                if (astat&BT848_INT_VSYNC)
1929
                        btv->field++;
1930
 
1931
                if (astat&(BT848_INT_SCERR|BT848_INT_OCERR)) {
1932
                        if (bttv_verbose)
1933
                                cprintf("bttv%d: irq:%s%s risc_count=%08lx\n",
1934
                                       btv->nr,
1935
                                       (astat&BT848_INT_SCERR) ? " SCERR" : "",
1936
                                       (astat&BT848_INT_OCERR) ? " OCERR" : "",
1937
                                       btread(BT848_RISC_COUNT));
1938
                        btv->errors++;
1939
                        if (btv->errors < BTTV_ERRORS) {
1940
                                btand(~15, BT848_GPIO_DMA_CTL);
1941
                                btwrite(virt_to_bus((unsigned long)(btv->risc_jmp+8)),
1942
                                        BT848_RISC_STRT_ADD);
1943
                                bt848_set_geo(btv);
1944
                                bt848_set_risc_jmps(btv,-1);
1945
                        } else {
1946
                                if (bttv_verbose)
1947
                                        cprintf("bttv%d: aiee: error loops\n",btv->nr);
1948
                                bt848_offline(btv);
1949
                        }
1950
                }
1951
                if (astat&BT848_INT_RISCI)
1952
                {
1953
                        if (bttv_debug > 1)
1954
                                cprintf("bttv%d: IRQ_RISCI\n",btv->nr);
1955
 
1956
                        /* captured VBI frame */
1957
                        if (stat&(1<<28))
1958
                        {
1959
                                btv->vbip=0;
1960
                                /* inc vbi frame count for detecting drops */
1961
                                (*(u32 *)&(btv->vbibuf[VBIBUF_SIZE - 4]))++;
1962
                        }
1963
 
1964
                        /* captured full frame */
1965
                        if (stat&(2<<28) && btv->gq_grab != -1)
1966
                        {
1967
                                btv->last_field=btv->field;
1968
                                if (bttv_debug)
1969
                                        cprintf("bttv%d: cap irq: done %d\n",btv->nr,btv->gq_grab);
1970
                                btv->gbuf[btv->gq_grab].stat = GBUFFER_DONE;
1971
                                btv->gq_grab = -1;
1972
                                if (btv->gq_in != btv->gq_out)
1973
                                {
1974
                                        btv->gq_grab = btv->gqueue[btv->gq_out++];
1975
                                        btv->gq_out  = btv->gq_out % MAX_GBUFFERS;
1976
                                        if (bttv_debug)
1977
                                                cprintf("bttv%d: cap irq: capture %d\n",btv->nr,btv->gq_grab);
1978
                                        btv->risc_cap_odd  = btv->gbuf[btv->gq_grab].ro;
1979
                                        btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
1980
                                        bt848_set_risc_jmps(btv,-1);
1981
                                        bt848_set_geo(btv);
1982
                                        btwrite(BT848_COLOR_CTL_GAMMA,
1983
                                                BT848_COLOR_CTL);
1984
                                } else {
1985
                                        btv->risc_cap_odd  = 0;
1986
                                        btv->risc_cap_even = 0;
1987
                                        bt848_set_risc_jmps(btv,-1);
1988
                                        bt848_set_geo(btv);
1989
                                        btwrite(btv->fb_color_ctl | BT848_COLOR_CTL_GAMMA,
1990
                                                BT848_COLOR_CTL);
1991
                                }
1992
                                break;
1993
                        }
1994
                        if (stat&(8<<28) && btv->gq_start)
1995
                        {
1996
                                btv->gq_start = 0;
1997
                                btv->gq_grab = btv->gqueue[btv->gq_out++];
1998
                                btv->gq_out  = btv->gq_out % MAX_GBUFFERS;
1999
                                if (bttv_debug)
2000
                                        cprintf("bttv%d: cap irq: capture %d [start]\n",btv->nr,btv->gq_grab);
2001
                                btv->risc_cap_odd  = btv->gbuf[btv->gq_grab].ro;
2002
                                btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
2003
                                bt848_set_risc_jmps(btv,-1);
2004
                                bt848_set_geo(btv);
2005
                                btwrite(BT848_COLOR_CTL_GAMMA,
2006
                                        BT848_COLOR_CTL);
2007
                        }
2008
                }
2009
 
2010
                count++;
2011
                if (count > 20) {
2012
                        btwrite(0, BT848_INT_MASK);
2013
                        cprintf("bttv%d: IRQ lockup, cleared int mask\n", btv->nr);
2014
                        bt848_offline(btv);
2015
                }
2016
        }
2017
 
2018
        in_irq = 0;
2019
 
2020
}
2021
 
2022
int bttv_probe(struct bttv *btv)
2023
{
2024
        unsigned char lat;
2025
 
2026
        if (bttv_num == BTTV_MAX)
2027
                return -ENOMEM;
2028
        printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
2029
 
2030
        btvirq = btv;
2031
        btv->nr = bttv_num;
2032
        //because not all compilers allow anonymous unions duplication is needed
2033
        btv->gpio_adap.bt848_mem = btv->bt848_mem = NULL;
2034
        btv->vbibuf=NULL;
2035
        btv->risc_jmp=NULL;
2036
        btv->vbi_odd=NULL;
2037
        btv->vbi_even=NULL;
2038
        btv->vbip=VBIBUF_SIZE;
2039
        btv->shutdown=0;
2040
 
2041
        btv->id=848;
2042
        btv->bt848_adr=pci_resource_start(btv->dev,0);
2043
        if (btv->id >= 878)
2044
                btv->i2c_command = 0x83;                  
2045
        else
2046
                btv->i2c_command=(I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
2047
 
2048
        if (-1 != latency) {
2049
                printk(KERN_INFO "bttv%d: setting pci latency timer to %d\n",
2050
                       bttv_num,latency);
2051
                pci_write_config_byte(btv->dev, PCI_LATENCY_TIMER, latency);
2052
        }
2053
        pci_read_config_byte(btv->dev, PCI_CLASS_REVISION, &btv->revision);
2054
        pci_read_config_byte(btv->dev, PCI_LATENCY_TIMER, &lat);
2055
        printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %2x:%2x\n", bttv_num,btv->id, btv->revision, btv->dev->bus->number, btv->dev->devfn);
2056
        printk(KERN_INFO "irq: %d, latency: %d, mmio: 0x%lx\n",
2057
               btv->dev->irq, lat, btv->bt848_adr);
2058
 
2059
        btv->bt848_mem = (void *)btv->bt848_adr;
2060
        btv->gpio_adap.bt848_mem = btv->bt848_mem;
2061
 
2062
        btv->pll.pll_crystal = 0;
2063
 
2064
        bttv_idcard(btv);
2065
 
2066
        /* clear interrupt mask */
2067
        btwrite(0, BT848_INT_MASK);
2068
 
2069
        handler_set(btv->dev->irq, (void (*)(int)) bttv_irq, NIL);
2070
 
2071
        bttv_handle_chipset(btv);
2072
 
2073
        init_bt848(btv);
2074
 
2075
        bttv_num++;
2076
 
2077
        return 0;
2078
 
2079
}
2080
 
2081
extern void i2c_init_all(void);
2082
 
2083
int bttv_start(struct bttv *btv)
2084
{
2085
 
2086
        BYTE dv, bus;
2087
        DWORD dw;
2088
 
2089
        printk(KERN_INFO "Initializing PCI BUS...\n");
179 giacomo 2090
 
170 giacomo 2091
        btv->dev = vmalloc_32(sizeof(struct pci_dev));
2092
 
2093
        /* Scan the devices connected to the PCI bus */
2094
        if (pci_init() == 1) {
2095
                clear();
2096
                pci_show();
2097
        } else {
2098
                return -1;
2099
        }
2100
 
2101
        bttv_num = 0;
2102
 
2103
        if (gbuffers < 2 || gbuffers > MAX_GBUFFERS)
2104
                gbuffers = 2;
2105
        if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
2106
                gbufsize = BTTV_MAX_FBUF;
2107
        if (bttv_verbose)
2108
                printk(KERN_INFO "bttv: using %d buffers with %dk (%dk total) for capture\n",
2109
                       gbuffers,gbufsize/1024,gbuffers*gbufsize/1024);
2110
 
2111
        if(pci_present()) {
2112
 
2113
            pci_class(PCI_CLASS_MULTIMEDIA_VIDEO<<8,0,&bus,&dv);
2114
 
2115
            btv->dev->bus->number = bus;
2116
            btv->dev->devfn = dv;
2117
 
2118
            pcibios_read_config_dword(bus, dv, PCI_BASE_ADDRESS_0, (DWORD *)&(btv->dev->base_address[0]));
2119
            pcibios_read_config_byte(bus, dv, PCI_INTERRUPT_LINE, (BYTE *)&(btv->dev->irq));
179 giacomo 2120
 
170 giacomo 2121
            pcibios_read_config_dword(bus,dv,PCI_COMMAND,&dw);
179 giacomo 2122
            oldpci_command = dw;
170 giacomo 2123
            dw |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
2124
            pcibios_write_config_dword(bus,dv,PCI_COMMAND,dw);
2125
 
2126
        } else return -1;
2127
 
2128
        i2c_init_all();
2129
 
2130
        bttv_check_chipset();
2131
 
2132
        bttv_probe(btv);
2133
 
2134
        bttv_open(btv);
2135
 
2136
        return 0;
2137
 
2138
}