Subversion Repositories shark

Rev

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