OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [media/] [video/] [bttv-driver.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
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-2003 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
#include <linux/module.h>
24
#include <linux/delay.h>
25
#include <linux/errno.h>
26
#include <linux/fs.h>
27
#include <linux/kernel.h>
28
#include <linux/major.h>
29
#include <linux/slab.h>
30
#include <linux/mm.h>
31
#include <linux/poll.h>
32
#include <linux/pci.h>
33
#include <linux/signal.h>
34
#include <linux/ioport.h>
35
#include <linux/sched.h>
36
#include <linux/types.h>
37
#include <linux/wrapper.h>
38
#include <linux/interrupt.h>
39
#include <linux/kmod.h>
40
#include <linux/vmalloc.h>
41
#include <linux/init.h>
42
#include <linux/pagemap.h>
43
 
44
#include <asm/io.h>
45
#include <asm/pgtable.h>
46
#include <asm/page.h>
47
#include <asm/byteorder.h>
48
 
49
#include "bttvp.h"
50
 
51
#define DEBUG(x)        /* Debug driver */
52
#define MIN(a,b) (((a)>(b))?(b):(a))
53
#define MAX(a,b) (((a)>(b))?(a):(b))
54
 
55
/* fwd decl */
56
static void bt848_set_risc_jmps(struct bttv *btv, int state);
57
static void make_vbitab(struct bttv *btv);
58
static void bt848_set_winsize(struct bttv *btv);
59
 
60
unsigned int bttv_num;                  /* number of Bt848s in use */
61
struct bttv bttvs[BTTV_MAX];
62
 
63
/* configuration variables */
64
#ifdef __BIG_ENDIAN
65
static unsigned int bigendian=1;
66
#else
67
static unsigned int bigendian=0;
68
#endif
69
static unsigned int radio[BTTV_MAX];
70
static unsigned int fieldnr = 0;
71
static unsigned int gpint = 1;
72
static unsigned int irq_debug = 0;
73
static unsigned int gbuffers = 4;
74
static unsigned int gbufsize = BTTV_MAX_FBUF;
75
 
76
static unsigned int combfilter = 0;
77
static unsigned int lumafilter = 0;
78
static unsigned int automute = 1;
79
static unsigned int chroma_agc = 0;
80
static unsigned int adc_crush = 1;
81
static int video_nr = -1;
82
static int radio_nr = -1;
83
static int vbi_nr = -1;
84
unsigned int bttv_debug = 0;
85
unsigned int bttv_verbose = 1;
86
unsigned int bttv_gpio = 0;
87
 
88
/* insmod options */
89
MODULE_PARM(radio,"1-" __stringify(BTTV_MAX) "i");
90
MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
91
MODULE_PARM(bigendian,"i");
92
MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
93
MODULE_PARM(fieldnr,"i");
94
MODULE_PARM_DESC(fieldnr,"count fields, default is 0 (no)");
95
MODULE_PARM(bttv_verbose,"i");
96
MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
97
MODULE_PARM(bttv_gpio,"i");
98
MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
99
MODULE_PARM(bttv_debug,"i");
100
MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
101
MODULE_PARM(irq_debug,"i");
102
MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
103
MODULE_PARM(gbuffers,"i");
104
MODULE_PARM_DESC(gbuffers,"number of capture buffers, default is 2 (64 max)");
105
MODULE_PARM(gbufsize,"i");
106
MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
107
MODULE_PARM(gpint,"i");
108
 
109
MODULE_PARM(combfilter,"i");
110
MODULE_PARM(lumafilter,"i");
111
MODULE_PARM(automute,"i");
112
MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
113
MODULE_PARM(chroma_agc,"i");
114
MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
115
MODULE_PARM(adc_crush,"i");
116
MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
117
 
118
MODULE_PARM(video_nr,"i");
119
MODULE_PARM(radio_nr,"i");
120
MODULE_PARM(vbi_nr,"i");
121
 
122
MODULE_DESCRIPTION("bttv - v4l driver module for bt848/878 based cards");
123
MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
124
MODULE_LICENSE("GPL");
125
 
126
/* kernel args */
127
#ifndef MODULE
128
static int __init p_radio(char *str) { return bttv_parse(str,BTTV_MAX,radio); }
129
__setup("bttv.radio=", p_radio);
130
#endif
131
 
132
#define I2C_TIMING (0x7<<4)
133
#define I2C_DELAY   10
134
 
135
#define I2C_SET(CTRL,DATA) \
136
    { btwrite((CTRL<<1)|(DATA), BT848_I2C); udelay(I2C_DELAY); }
137
#define I2C_GET()   (btread(BT848_I2C)&1)
138
 
139
#define BURSTOFFSET 76
140
#define BTTV_ERRORS 5
141
 
142
 
143
/*******************************/
144
/* Memory management functions */
145
/*******************************/
146
 
147
 
148
static inline unsigned long kvirt_to_bus(unsigned long adr)
149
{
150
        unsigned long kva;
151
 
152
        kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
153
        kva |= adr & (PAGE_SIZE-1); /* restore the offset */
154
        return virt_to_bus((void *)kva);
155
}
156
 
157
/* Here we want the physical address of the memory.
158
 * This is used when initializing the contents of the
159
 * area and marking the pages as reserved.
160
 */
161
static inline unsigned long kvirt_to_pa(unsigned long adr)
162
{
163
        unsigned long kva;
164
 
165
        kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
166
        kva |= adr & (PAGE_SIZE-1); /* restore the offset */
167
        return __pa(kva);
168
}
169
 
170
static void * rvmalloc(signed long size)
171
{
172
        struct page *page;
173
        void * mem;
174
        unsigned long adr;
175
 
176
        mem=vmalloc_32(size);
177
        if (NULL == mem)
178
                printk(KERN_INFO "bttv: vmalloc_32(%ld) failed\n",size);
179
        else {
180
                /* Clear the ram out, no junk to the user */
181
                memset(mem, 0, size);
182
                adr=(unsigned long) mem;
183
                while (size > 0) {
184
                        page = vmalloc_to_page((void *)adr);
185
                        mem_map_reserve(page);
186
                        adr+=PAGE_SIZE;
187
                        size-=PAGE_SIZE;
188
                }
189
        }
190
        return mem;
191
}
192
 
193
static void rvfree(void * mem, signed long size)
194
{
195
        struct page *page;
196
        unsigned long adr;
197
 
198
        if (mem) {
199
                adr=(unsigned long) mem;
200
                while (size > 0) {
201
                        page = vmalloc_to_page((void *)adr);
202
                        mem_map_unreserve(page);
203
                        adr+=PAGE_SIZE;
204
                        size-=PAGE_SIZE;
205
                }
206
                vfree(mem);
207
        }
208
}
209
 
210
 
211
 
212
/*
213
 *      Create the giant waste of buffer space we need for now
214
 *      until we get DMA to user space sorted out (probably 2.3.x)
215
 *
216
 *      We only create this as and when someone uses mmap
217
 */
218
 
219
static int fbuffer_alloc(struct bttv *btv)
220
{
221
        if(!btv->fbuffer)
222
                btv->fbuffer=(unsigned char *) rvmalloc(gbuffers*gbufsize);
223
        else
224
                printk(KERN_ERR "bttv%d: Double alloc of fbuffer!\n",
225
                        btv->nr);
226
        if(!btv->fbuffer)
227
                return -ENOBUFS;
228
        return 0;
229
}
230
 
231
/* ----------------------------------------------------------------------- */
232
 
233
void bttv_gpio_tracking(struct bttv *btv, char *comment)
234
{
235
        unsigned int outbits, data;
236
        outbits = btread(BT848_GPIO_OUT_EN);
237
        data    = btread(BT848_GPIO_DATA);
238
        printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
239
               btv->nr,outbits,data & outbits, data & ~outbits, comment);
240
}
241
 
242
static char *audio_modes[] = { "audio: tuner", "audio: radio", "audio: extern",
243
                               "audio: intern", "audio: off" };
244
 
245
static void audio(struct bttv *btv, int mode)
246
{
247
        if (bttv_tvcards[btv->type].gpiomask)
248
                btaor(bttv_tvcards[btv->type].gpiomask,
249
                      ~bttv_tvcards[btv->type].gpiomask,
250
                      BT848_GPIO_OUT_EN);
251
 
252
        switch (mode)
253
        {
254
                case AUDIO_MUTE:
255
                        btv->audio|=AUDIO_MUTE;
256
                        break;
257
                case AUDIO_UNMUTE:
258
                        btv->audio&=~AUDIO_MUTE;
259
                        mode=btv->audio;
260
                        break;
261
                case AUDIO_OFF:
262
                        mode=AUDIO_OFF;
263
                        break;
264
                case AUDIO_ON:
265
                        mode=btv->audio;
266
                        break;
267
                default:
268
                        btv->audio&=AUDIO_MUTE;
269
                        btv->audio|=mode;
270
                        break;
271
        }
272
        /* if audio mute or not in H-lock, turn audio off */
273
        if ((btv->audio&AUDIO_MUTE))
274
                mode=AUDIO_OFF;
275
        if ((mode == AUDIO_TUNER) && (btv->radio))
276
                mode = AUDIO_RADIO;
277
        if (bttv_tvcards[btv->type].gpiomask)
278
                btaor(bttv_tvcards[btv->type].audiomux[mode],
279
                      ~bttv_tvcards[btv->type].gpiomask,
280
                      BT848_GPIO_DATA);
281
        if (bttv_gpio)
282
                bttv_gpio_tracking(btv,audio_modes[mode]);
283
        if (!in_interrupt())
284
                bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(mode));
285
}
286
 
287
 
288
static inline void bt848_dma(struct bttv *btv, uint state)
289
{
290
        if (state)
291
                btor(3, BT848_GPIO_DMA_CTL);
292
        else
293
                btand(~3, BT848_GPIO_DMA_CTL);
294
}
295
 
296
 
297
/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC*/
298
 
299
/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
300
   PLL_X = Reference pre-divider (0=1, 1=2)
301
   PLL_C = Post divider (0=6, 1=4)
302
   PLL_I = Integer input
303
   PLL_F = Fractional input
304
 
305
   F_input = 28.636363 MHz:
306
   PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
307
*/
308
 
309
static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
310
{
311
        unsigned char fl, fh, fi;
312
 
313
        /* prevent overflows */
314
        fin/=4;
315
        fout/=4;
316
 
317
        fout*=12;
318
        fi=fout/fin;
319
 
320
        fout=(fout%fin)*256;
321
        fh=fout/fin;
322
 
323
        fout=(fout%fin)*256;
324
        fl=fout/fin;
325
 
326
        /*printk("0x%02x 0x%02x 0x%02x\n", fi, fh, fl);*/
327
        btwrite(fl, BT848_PLL_F_LO);
328
        btwrite(fh, BT848_PLL_F_HI);
329
        btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
330
}
331
 
332
static void set_pll(struct bttv *btv)
333
{
334
        int i;
335
 
336
        if (!btv->pll.pll_crystal)
337
                return;
338
 
339
        if (btv->pll.pll_ofreq == btv->pll.pll_current) {
340
                dprintk("bttv%d: PLL: no change required\n",btv->nr);
341
                return;
342
        }
343
 
344
        if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
345
                /* no PLL needed */
346
                if (btv->pll.pll_current == 0)
347
                        return;
348
                vprintk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
349
                        btv->nr,btv->pll.pll_ifreq);
350
                btwrite(0x00,BT848_TGCTRL);
351
                btwrite(0x00,BT848_PLL_XCI);
352
                btv->pll.pll_current = 0;
353
                return;
354
        }
355
 
356
        vprintk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->nr,
357
                btv->pll.pll_ifreq, btv->pll.pll_ofreq);
358
        set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
359
 
360
        for (i=0; i<10; i++) {
361
                /*  Let other people run while the PLL stabilizes */
362
                vprintk(".");
363
                current->state = TASK_INTERRUPTIBLE;
364
                schedule_timeout(HZ/10);
365
 
366
                if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
367
                        btwrite(0,BT848_DSTATUS);
368
                } else {
369
                        btwrite(0x08,BT848_TGCTRL);
370
                        btv->pll.pll_current = btv->pll.pll_ofreq;
371
                        vprintk(" ok\n");
372
                        return;
373
                }
374
        }
375
        btv->pll.pll_current = -1;
376
        vprintk("failed\n");
377
        return;
378
}
379
 
380
static void bt848_muxsel(struct bttv *btv, unsigned int input)
381
{
382
        dprintk("bttv%d: bt848_muxsel %d\n",btv->nr,input);
383
 
384
        if (bttv_tvcards[btv->type].muxsel[input] < 0) {
385
                dprintk("bttv%d: digital ccir muxsel\n", btv->nr);
386
                btv->channel = input;
387
                return;
388
        }
389
 
390
        /* needed by RemoteVideo MX */
391
        if (bttv_tvcards[btv->type].gpiomask2)
392
                btaor(bttv_tvcards[btv->type].gpiomask2,
393
                      ~bttv_tvcards[btv->type].gpiomask2,
394
                      BT848_GPIO_OUT_EN);
395
 
396
#if 0
397
        /* This seems to get rid of some synchronization problems */
398
        btand(~(3<<5), BT848_IFORM);
399
        mdelay(10);
400
#endif
401
 
402
        input %= bttv_tvcards[btv->type].video_inputs;
403
        if (input == btv->svhs) {
404
                btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
405
                btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
406
        } else {
407
                btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
408
                btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
409
        }
410
 
411
        btaor((bttv_tvcards[btv->type].muxsel[input]&3)<<5, ~(3<<5), BT848_IFORM);
412
        audio(btv, (input!=bttv_tvcards[btv->type].tuner) ?
413
              AUDIO_EXTERN : AUDIO_TUNER);
414
 
415
        if (bttv_tvcards[btv->type].gpiomask2)
416
                btaor(bttv_tvcards[btv->type].muxsel[input]>>4,
417
                      ~bttv_tvcards[btv->type].gpiomask2,
418
                      BT848_GPIO_DATA);
419
 
420
        /* card specific hook */
421
        if (bttv_tvcards[btv->type].muxsel_hook)
422
                bttv_tvcards[btv->type].muxsel_hook(btv, input);
423
 
424
        if (bttv_gpio)
425
                bttv_gpio_tracking(btv,"muxsel");
426
 
427
        btv->channel=input;
428
}
429
 
430
/* special timing tables from conexant... */
431
static u8 SRAM_Table[][60] =
432
{
433
        /* PAL digital input over GPIO[7:0] */
434
        {
435
                45, // 45 bytes following
436
                0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
437
                0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
438
                0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
439
                0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
440
                0x37,0x00,0xAF,0x21,0x00
441
        },
442
        /* NTSC digital input over GPIO[7:0] */
443
        {
444
                51, // 51 bytes following
445
                0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
446
                0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
447
                0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
448
                0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
449
                0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
450
                0x00,
451
        },
452
        // TGB_NTSC392 // quartzsight
453
        // This table has been modified to be used for Fusion Rev D
454
        {
455
                0x2A, // size of table = 42
456
                0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
457
                0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
458
                0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
459
                0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
460
                0x20, 0x00
461
        }
462
};
463
 
464
struct tvnorm
465
{
466
        u32 Fsc;
467
        u16 swidth, sheight; /* scaled standard width, height */
468
        u16 totalwidth;
469
        u8 adelay, bdelay, iform;
470
        u32 scaledtwidth;
471
        u16 hdelayx1, hactivex1;
472
        u16 vdelay;
473
        u8 vbipack;
474
        int sram; /* index into SRAM_Table */
475
};
476
 
477
static struct tvnorm tvnorms[] = {
478
        /* PAL-BDGHI */
479
        /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
480
        /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
481
        {
482
                .Fsc            = 35468950,
483
                .swidth         = 924,
484
                .sheight        = 576,
485
                .totalwidth     = 1135,
486
                .adelay         = 0x7f,
487
                .bdelay         = 0x72,
488
                .iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
489
                .scaledtwidth   = 1135,
490
                .hdelayx1       = 186,
491
                .hactivex1      = 924,
492
#ifdef VIDEODAT_HACK
493
                .vdelay         = VBI_MAXLINES*2,
494
#else
495
                .vdelay         = 0x20,
496
#endif
497
                .vbipack        = 255,
498
                .sram           = 0,
499
        },{
500
                /* NTSC */
501
                .Fsc            = 28636363,
502
                .swidth         = 768,
503
                .sheight        = 480,
504
                .totalwidth     = 910,
505
                .adelay         = 0x68,
506
                .bdelay         = 0x5d,
507
                .iform          = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
508
                .scaledtwidth   = 910,
509
                .hdelayx1       = 128,
510
                .hactivex1      = 910,
511
                .vdelay         = 0x1a,
512
                .vbipack        = 144,
513
                .sram           = 1,
514
        },{
515
                /* SECAM L */
516
                .Fsc            = 35468950,
517
                .swidth         = 924,
518
                .sheight        = 576,
519
                .totalwidth     = 1135,
520
                .adelay         = 0x7f,
521
                .bdelay         = 0xb0,
522
                .iform          = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
523
                .scaledtwidth   = 1135,
524
                .hdelayx1       = 186,
525
                .hactivex1      = 922,
526
                .vdelay         = 0x20,
527
                .vbipack        = 255,
528
                .sram           = 0, /* like PAL, correct? */
529
        },{
530
                /* PAL-NC */
531
                .Fsc            = 28636363,
532
                .swidth         = 640,
533
                .sheight        = 576,
534
                .totalwidth     = 910,
535
                .adelay         = 0x68,
536
                .bdelay         = 0x5d,
537
                .iform          = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
538
                .scaledtwidth   = 780,
539
                .hdelayx1       = 130,
540
                .hactivex1      = 734,
541
                .vdelay         = 0x1a,
542
                .vbipack        = 144,
543
                .sram           = -1,
544
        },{
545
                /* PAL-M */
546
                .Fsc            = 28636363,
547
                .swidth         = 640,
548
                .sheight        = 480,
549
                .totalwidth     = 910,
550
                .adelay         = 0x68,
551
                .bdelay         = 0x5d,
552
                .iform          = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
553
                .scaledtwidth   = 780,
554
                .hdelayx1       = 135,
555
                .hactivex1      = 754,
556
                .vdelay         = 0x1a,
557
                .vbipack        = 144,
558
                .sram           = -1,
559
        },{
560
                /* PAL-N */
561
                .Fsc             35468950,
562
                .swidth         = 768,
563
                .sheight        = 576,
564
                .totalwidth     = 1135,
565
                .adelay         = 0x7f,
566
                .bdelay         = 0x72,
567
                .iform          = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
568
                .scaledtwidth   = 944,
569
                .hdelayx1       = 186,
570
                .hactivex1      = 922,
571
                .vdelay         = 0x20,
572
                .vbipack        = 144,
573
                .sram           = -1,
574
        },{
575
                /* NTSC-Japan */
576
                .Fsc            = 28636363,
577
                .swidth         = 640,
578
                .sheight        = 480,
579
                .totalwidth     = 910,
580
                .adelay         = 0x68,
581
                .bdelay         = 0x5d,
582
                .iform          = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
583
                .scaledtwidth   = 780,
584
                .hdelayx1       = 135,
585
                .hactivex1      = 754,
586
                .vdelay         = 0x16,
587
                .vbipack        = 144,
588
                .sram           = -1,
589
        },{
590
                /* Quartzsight digital camera
591
                 * From Bt832 datasheet: 393x304 pixel @30Hz,
592
                 * Visible: 352x288 pixel
593
                 */
594
                .Fsc            = 27000000,
595
                .swidth         = 352,
596
                .sheight        = 576, //2*288 ?
597
                .totalwidth     = 392,
598
                .adelay         = 0x68,
599
                .bdelay         = 0x5d,
600
                .iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
601
                .scaledtwidth   = 392,
602
                .hdelayx1       = 0x20,
603
                .hactivex1      = 352,
604
                .vdelay         = 0x08,
605
                .vbipack        = 0, //255
606
                .sram           = 2,
607
        }
608
};
609
#define TVNORMS ARRAY_SIZE(tvnorms)
610
 
611
/* used to switch between the bt848's analog/digital video capture modes */
612
void bt848A_set_timing(struct bttv *btv)
613
{
614
        int i, len;
615
        int table_idx = tvnorms[btv->win.norm].sram;
616
        int fsc       = tvnorms[btv->win.norm].Fsc;
617
 
618
        if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0) {
619
                dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
620
                        btv->nr,table_idx);
621
 
622
                /* timing change...reset timing generator address */
623
                btwrite(0x00, BT848_TGCTRL);
624
                btwrite(0x02, BT848_TGCTRL);
625
                btwrite(0x00, BT848_TGCTRL);
626
 
627
                len=SRAM_Table[table_idx][0];
628
                for(i = 1; i <= len; i++)
629
                        btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
630
                btv->pll.pll_ofreq = 27000000;
631
 
632
                set_pll(btv);
633
                btwrite(0x11, BT848_TGCTRL);
634
                btwrite(0x41, BT848_DVSIF);
635
        } else {
636
                btv->pll.pll_ofreq = fsc;
637
                set_pll(btv);
638
                btwrite(0x0, BT848_DVSIF);
639
        }
640
}
641
 
642
static void bttv_set_norm(struct bttv *btv, int new_norm)
643
{
644
        unsigned long irq_flags;
645
 
646
        if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0 &&
647
            bttv_tvcards[btv->type].digital_mode == DIGITAL_MODE_CAMERA) {
648
                //override norm by quartzsight mode
649
                new_norm=7;
650
                dprintk("bttv%d: set_norm fix-up digital: %d\n",
651
                        btv->nr, new_norm);
652
        }
653
 
654
        if (btv->win.norm != new_norm) {
655
                btv->win.norm = new_norm;
656
 
657
                make_vbitab(btv);
658
                spin_lock_irqsave(&btv->s_lock, irq_flags);
659
                bt848_set_winsize(btv);
660
                spin_unlock_irqrestore(&btv->s_lock, irq_flags);
661
 
662
                bt848A_set_timing(btv);
663
                switch (btv->type) {
664
                case BTTV_VOODOOTV_FM:
665
                        bttv_tda9880_setnorm(btv,new_norm);
666
                        break;
667
#if 0
668
                case BTTV_OSPREY540:
669
                        osprey_540_set_norm(btv,new_norm);
670
                        break;
671
#endif
672
                }
673
        }
674
}
675
 
676
#define VBI_SPL 2044
677
/* RISC command to write one VBI data line */
678
#define VBI_RISC BT848_RISC_WRITE|VBI_SPL|BT848_RISC_EOL|BT848_RISC_SOL
679
 
680
static void make_vbitab(struct bttv *btv)
681
{
682
        int i;
683
        unsigned int *po=(unsigned int *) btv->vbi_odd;
684
        unsigned int *pe=(unsigned int *) btv->vbi_even;
685
 
686
        if (bttv_debug > 1)
687
                printk("bttv%d: vbi1: po=%08lx pe=%08lx\n",
688
                       btv->nr,virt_to_bus(po), virt_to_bus(pe));
689
 
690
        *(po++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(po++)=0;
691
        for (i=0; i<VBI_MAXLINES; i++) {
692
                *(po++)=cpu_to_le32(VBI_RISC);
693
                *(po++)=cpu_to_le32(kvirt_to_bus((unsigned long)btv->vbibuf+i*2048));
694
        }
695
        *(po++)=cpu_to_le32(BT848_RISC_JUMP);
696
        *(po++)=cpu_to_le32(virt_to_bus(btv->risc_jmp+4));
697
 
698
        *(pe++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(pe++)=0;
699
        for (i=VBI_MAXLINES; i<VBI_MAXLINES*2; i++) {
700
                *(pe++)=cpu_to_le32(VBI_RISC);
701
                *(pe++)=cpu_to_le32(kvirt_to_bus((unsigned long)btv->vbibuf+i*2048));
702
        }
703
        *(pe++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(0x01<<16));
704
        *(pe++)=cpu_to_le32(virt_to_bus(btv->risc_jmp+10));
705
 
706
        if (bttv_debug > 1)
707
                printk("bttv%d: vbi2: po=%08lx pe=%08lx\n",
708
                       btv->nr,virt_to_bus(po), virt_to_bus(pe));
709
}
710
 
711
static unsigned int fmtbppx2[16] = {
712
        8, 6, 4, 4, 4, 3, 2, 2, 4, 3, 0, 0, 0, 0, 2, 0
713
};
714
 
715
static unsigned int palette2fmt[] = {
716
        UNSET,
717
        BT848_COLOR_FMT_Y8,
718
        BT848_COLOR_FMT_RGB8,
719
        BT848_COLOR_FMT_RGB16,
720
        BT848_COLOR_FMT_RGB24,
721
        BT848_COLOR_FMT_RGB32,
722
        BT848_COLOR_FMT_RGB15,
723
        BT848_COLOR_FMT_YUY2,
724
        BT848_COLOR_FMT_YUY2,
725
        UNSET,
726
        UNSET,
727
        UNSET,
728
        BT848_COLOR_FMT_RAW,
729
        BT848_COLOR_FMT_YCrCb422,
730
        BT848_COLOR_FMT_YCrCb411,
731
        BT848_COLOR_FMT_YCrCb422,
732
        BT848_COLOR_FMT_YCrCb411,
733
};
734
#define PALETTEFMT_MAX ARRAY_SIZE(palette2fmt)
735
 
736
static int make_rawrisctab(struct bttv *btv, u32 *ro, u32 *re, u32 *vbuf)
737
{
738
        u32 line;
739
        u32 bpl=1024;           /* bytes per line */
740
        unsigned long vadr=(unsigned long) vbuf;
741
 
742
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
743
        *(ro++)=cpu_to_le32(0);
744
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
745
        *(re++)=cpu_to_le32(0);
746
 
747
        /* In PAL 650 blocks of 256 DWORDs are sampled, but only if VDELAY
748
           is 2 and without separate VBI grabbing.
749
           We'll have to handle this inside the IRQ handler ... */
750
 
751
        for (line=0; line < 640; line++)
752
        {
753
                *(ro++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
754
                *(ro++)=cpu_to_le32(kvirt_to_bus(vadr));
755
                *(re++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
756
                *(re++)=cpu_to_le32(kvirt_to_bus(vadr+gbufsize/2));
757
                vadr+=bpl;
758
        }
759
 
760
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
761
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
762
        *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
763
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
764
 
765
        return 0;
766
}
767
 
768
static int  make_prisctab(struct bttv *btv, u32 *ro, u32 *re, u32 *vbuf,
769
                          u16 width, u16 height, u16 fmt)
770
{
771
        u16 line, lmask;
772
        u32 bl, blcr, blcb, rcmd;
773
        u32 todo;
774
        u32 **rp;
775
        int inter;
776
        unsigned long cbadr, cradr;
777
        unsigned long vadr=(unsigned long) vbuf;
778
        u32 shift, csize;
779
 
780
        if (bttv_debug > 1)
781
                printk("bttv%d: prisc1: ro=%08lx re=%08lx\n",
782
                       btv->nr,virt_to_bus(ro), virt_to_bus(re));
783
 
784
        switch(fmt)
785
        {
786
        case VIDEO_PALETTE_YUV422P:
787
                csize=(width*height)>>1;
788
                shift=1;
789
                lmask=0;
790
                break;
791
 
792
        case VIDEO_PALETTE_YUV411P:
793
                csize=(width*height)>>2;
794
                shift=2;
795
                lmask=0;
796
                break;
797
 
798
         case VIDEO_PALETTE_YUV420P:
799
                csize=(width*height)>>2;
800
                shift=1;
801
                lmask=1;
802
                break;
803
 
804
         case VIDEO_PALETTE_YUV410P:
805
                csize=(width*height)>>4;
806
                shift=2;
807
                lmask=3;
808
                break;
809
 
810
        default:
811
                return -1;
812
        }
813
        cbadr=vadr+(width*height);
814
        cradr=cbadr+csize;
815
        inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
816
 
817
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
818
        *(ro++)=0;
819
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
820
        *(re++)=0;
821
 
822
        for (line=0; line < (height<<(1^inter)); line++)
823
        {
824
                if(line==height)
825
                {
826
                        vadr+=csize<<1;
827
                        cbadr=vadr+(width*height);
828
                        cradr=cbadr+csize;
829
                }
830
                if (inter)
831
                        rp= (line&1) ? &re : &ro;
832
                else
833
                        rp= (line>=height) ? &ro : &re;
834
 
835
 
836
                if(line&lmask)
837
                        rcmd=BT848_RISC_WRITE1S23|BT848_RISC_SOL;
838
                else
839
                        rcmd=BT848_RISC_WRITE123|BT848_RISC_SOL;
840
 
841
                todo=width;
842
                while(todo)
843
                {
844
                 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
845
                 blcr=(PAGE_SIZE-((PAGE_SIZE-1)&cradr))<<shift;
846
                 blcb=(PAGE_SIZE-((PAGE_SIZE-1)&cbadr))<<shift;
847
                 bl=(blcr<bl) ? blcr : bl;
848
                 bl=(blcb<bl) ? blcb : bl;
849
                 bl=(bl>todo) ? todo : bl;
850
                 blcr=bl>>shift;
851
                 blcb=blcr;
852
                 /* bl now containts the longest row that can be written */
853
                 todo-=bl;
854
                 if(!todo) rcmd|=BT848_RISC_EOL; /* if this is the last EOL */
855
 
856
                 *((*rp)++)=cpu_to_le32(rcmd|bl);
857
                 *((*rp)++)=cpu_to_le32(blcb|(blcr<<16));
858
                 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
859
                 vadr+=bl;
860
                 if ((rcmd&(15<<28))==BT848_RISC_WRITE123)
861
                 {
862
                        *((*rp)++)=cpu_to_le32(kvirt_to_bus(cbadr));
863
                        cbadr+=blcb;
864
                        *((*rp)++)=cpu_to_le32(kvirt_to_bus(cradr));
865
                        cradr+=blcr;
866
                 }
867
 
868
                 rcmd&=~BT848_RISC_SOL; /* only the first has SOL */
869
                }
870
        }
871
 
872
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
873
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
874
        *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
875
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
876
 
877
        if (bttv_debug > 1)
878
                printk("bttv%d: prisc2: ro=%08lx re=%08lx\n",
879
                       btv->nr,virt_to_bus(ro), virt_to_bus(re));
880
 
881
        return 0;
882
}
883
 
884
static int  make_vrisctab(struct bttv *btv, u32 *ro, u32 *re, u32 *vbuf,
885
                          u16 width, u16 height, u16 palette)
886
{
887
        u16 line;
888
        u32 bpl;  /* bytes per line */
889
        u32 bl;
890
        u32 todo;
891
        u32 **rp;
892
        int inter;
893
        unsigned long vadr=(unsigned long)vbuf;
894
 
895
        if (palette==VIDEO_PALETTE_RAW)
896
                return make_rawrisctab(btv, ro, re, vbuf);
897
        if (palette>=VIDEO_PALETTE_PLANAR)
898
                return make_prisctab(btv, ro, re, vbuf, width, height, palette);
899
        if (bttv_debug > 1)
900
                printk("bttv%d: vrisc1: ro=%08lx re=%08lx\n",
901
                       btv->nr,virt_to_bus(ro), virt_to_bus(re));
902
 
903
        inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
904
        bpl=width*fmtbppx2[palette2fmt[palette]&0xf]/2;
905
 
906
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
907
        *(ro++)=cpu_to_le32(0);
908
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
909
        *(re++)=cpu_to_le32(0);
910
 
911
        for (line=0; line < (height<<(1^inter)); line++)
912
        {
913
                if (inter)
914
                        rp= (line&1) ? &re : &ro;
915
                else
916
                        rp= (line>=height) ? &ro : &re;
917
 
918
                bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
919
                if (bpl<=bl)
920
                {
921
                        *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
922
                                BT848_RISC_EOL|bpl);
923
                        *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
924
                        vadr+=bpl;
925
                }
926
                else
927
                {
928
                        todo=bpl;
929
                        *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|bl);
930
                        *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
931
                        vadr+=bl;
932
                        todo-=bl;
933
                        while (todo>PAGE_SIZE)
934
                        {
935
                                *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|PAGE_SIZE);
936
                                *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
937
                                vadr+=PAGE_SIZE;
938
                                todo-=PAGE_SIZE;
939
                        }
940
                        *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_EOL|todo);
941
                        *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
942
                        vadr+=todo;
943
                }
944
        }
945
 
946
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
947
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
948
        *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
949
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
950
 
951
        if (bttv_debug > 1)
952
                printk("bttv%d: vrisc2: ro=%08lx re=%08lx\n",
953
                       btv->nr,virt_to_bus(ro), virt_to_bus(re));
954
 
955
        return 0;
956
}
957
 
958
static unsigned char lmaskt[8] =
959
{ 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
960
static unsigned char rmaskt[8] =
961
{ 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
962
 
963
static void clip_draw_rectangle(unsigned char *clipmap, int x, int y, int w, int h)
964
{
965
        unsigned char lmask, rmask, *p;
966
        int W, l, r;
967
        int i;
968
 
969
        if (bttv_debug > 1)
970
                printk("bttv clip: %dx%d+%d+%d\n",w,h,x,y);
971
 
972
        /* bitmap is fixed width, 128 bytes (1024 pixels represented) */
973
        if (x<0)
974
        {
975
                w+=x;
976
                x=0;
977
        }
978
        if (y<0)
979
        {
980
                h+=y;
981
                y=0;
982
        }
983
        if (w < 0 || h < 0)       /* catch bad clips */
984
                return;
985
        /* out of range data should just fall through */
986
        if (y+h>=625)
987
                h=625-y;
988
        if (x+w>=1024)
989
                w=1024-x;
990
 
991
        l=x>>3;
992
        r=(x+w-1)>>3;
993
        W=r-l-1;
994
        lmask=lmaskt[x&7];
995
        rmask=rmaskt[(x+w-1)&7];
996
        p=clipmap+128*y+l;
997
 
998
        if (W>0)
999
        {
1000
                for (i=0; i<h; i++, p+=128)
1001
                {
1002
                        *p|=lmask;
1003
                        memset(p+1, 0xff, W);
1004
                        p[W+1]|=rmask;
1005
                }
1006
        } else if (!W) {
1007
                for (i=0; i<h; i++, p+=128)
1008
                {
1009
                        p[0]|=lmask;
1010
                        p[1]|=rmask;
1011
                }
1012
        } else {
1013
                for (i=0; i<h; i++, p+=128)
1014
                        p[0]|=lmask&rmask;
1015
        }
1016
 
1017
 
1018
}
1019
 
1020
static void make_clip_tab(struct bttv *btv, struct video_clip *cr, int ncr)
1021
{
1022
        u32 line, x, y, bpl, width, height, inter, maxw;
1023
        u32 bpp, dx, sx, **rp, *ro, *re, flags, len;
1024
        u32 adr;
1025
        int i;
1026
        unsigned char *clipmap, *clipline, cbit, lastbit, outofmem;
1027
 
1028
        /* take care: bpp != btv->win.bpp is allowed here */
1029
        bpp = fmtbppx2[btv->win.color_fmt&0xf]/2;
1030
        bpl=btv->win.bpl;
1031
        adr=btv->win.vidadr + btv->win.x * btv->win.bpp + btv->win.y * bpl;
1032
        inter=(btv->win.interlace&1)^1;
1033
        width=btv->win.width;
1034
        height=btv->win.height;
1035
        if (bttv_debug > 1)
1036
                printk("bttv%d: clip1: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
1037
                       btv->nr,btv->picture.palette,width,height,bpl,bpp);
1038
        if(width > 1023)
1039
                width = 1023;           /* sanity check */
1040
        if(height > 625)
1041
                height = 625;           /* sanity check */
1042
        ro=btv->risc_scr_odd;
1043
        re=btv->risc_scr_even;
1044
 
1045
        if (bttv_debug)
1046
                printk("bttv%d: clip: ro=%08lx re=%08lx\n",
1047
                       btv->nr,virt_to_bus(ro), virt_to_bus(re));
1048
 
1049
        if ((clipmap=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL) {
1050
                /* can't clip, don't generate any risc code */
1051
                *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1052
                *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1053
                *(re++)=cpu_to_le32(BT848_RISC_JUMP);
1054
                *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1055
                return;
1056
        }
1057
        if (ncr < 0) {   /* bitmap was pased */
1058
                memcpy(clipmap, (unsigned char *)cr, VIDEO_CLIPMAP_SIZE);
1059
        } else {        /* convert rectangular clips to a bitmap */
1060
                memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
1061
                for (i=0; i<ncr; i++)
1062
                        clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
1063
                                cr[i].width, cr[i].height);
1064
        }
1065
        /* clip against viewing window AND screen
1066
           so we do not have to rely on the user program
1067
         */
1068
        maxw = (bpl - btv->win.x * btv->win.bpp) / bpp;
1069
        clip_draw_rectangle(clipmap, (width > maxw) ? maxw : width,
1070
                            0, 1024, 768);
1071
        clip_draw_rectangle(clipmap,0,(btv->win.y+height>btv->win.sheight) ?
1072
                            (btv->win.sheight-btv->win.y) : height,1024,768);
1073
        if (btv->win.x<0)
1074
                clip_draw_rectangle(clipmap, 0, 0, -(btv->win.x), 768);
1075
        if (btv->win.y<0)
1076
                clip_draw_rectangle(clipmap, 0, 0, 1024, -(btv->win.y));
1077
 
1078
        *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1079
        *(ro++)=cpu_to_le32(0);
1080
        *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1081
        *(re++)=cpu_to_le32(0);
1082
 
1083
        /* translate bitmap to risc code */
1084
        for (line=outofmem=0; line < (height<<inter) && !outofmem; line++)
1085
        {
1086
                y = line>>inter;
1087
                rp= (line&1) ? &re : &ro;
1088
                clipline = clipmap + (y<<7); /* running pointers ... */
1089
                lastbit = *clipline & 1;
1090
                for(x=dx=0,sx=0; x<=width && !outofmem;) {
1091
                        if (0 == (x&7)) {
1092
                                /* check bytes not bits if we can ... */
1093
                                if (lastbit) {
1094
                                        while (0xff==*clipline && x<width-8) {
1095
                                                x  += 8;
1096
                                                dx += 8;
1097
                                                clipline++;
1098
                                        }
1099
                                } else {
1100
                                        while (0x00==*clipline && x<width-8) {
1101
                                                x  += 8;
1102
                                                dx += 8;
1103
                                                clipline++;
1104
                                        }
1105
                                }
1106
                        }
1107
                        cbit = *clipline & (1<<(x&7));
1108
                        if (x < width && !lastbit == !cbit) {
1109
                                dx++;
1110
                        } else {
1111
                                /* generate the dma controller code */
1112
                                len = dx * bpp;
1113
                                flags = ((bpp==4) ? BT848_RISC_BYTE3 : 0);
1114
                                flags |= ((!sx) ? BT848_RISC_SOL : 0);
1115
                                flags |= ((sx + dx == width) ? BT848_RISC_EOL : 0);
1116
                                if (!lastbit) {
1117
                                        *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|flags|len);
1118
                                        *((*rp)++)=cpu_to_le32(adr + bpp * sx);
1119
                                } else {
1120
                                        *((*rp)++)=cpu_to_le32(BT848_RISC_SKIP|flags|len);
1121
                                }
1122
                                lastbit=cbit;
1123
                                sx += dx;
1124
                                dx = 1;
1125
                                if (ro - btv->risc_scr_odd>(RISCMEM_LEN>>3) - 16)
1126
                                        outofmem++;
1127
                                if (re - btv->risc_scr_even>(RISCMEM_LEN>>3) - 16)
1128
                                        outofmem++;
1129
                        }
1130
                        x++;
1131
                        if (0 == (x&7))
1132
                                clipline++;
1133
                }
1134
                if ((!inter)||(line&1))
1135
                        adr+=bpl;
1136
        }
1137
 
1138
        vfree(clipmap);
1139
        /* outofmem flag relies on the following code to discard extra data */
1140
        *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1141
        *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1142
        *(re++)=cpu_to_le32(BT848_RISC_JUMP);
1143
        *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1144
 
1145
        if (bttv_debug > 1)
1146
                printk("bttv%d: clip2: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
1147
                       btv->nr,btv->picture.palette,width,height,bpl,bpp);
1148
}
1149
 
1150
/*
1151
 *      Set the registers for the size we have specified. Don't bother
1152
 *      trying to understand this without the BT848 manual in front of
1153
 *      you [AC].
1154
 *
1155
 *      PS: The manual is free for download in .pdf format from
1156
 *      www.brooktree.com - nicely done those folks.
1157
 */
1158
 
1159
static inline void bt848_set_eogeo(struct bttv *btv, struct tvnorm *tvn,
1160
                                   int odd, int width, int height)
1161
{
1162
        u16 vscale, hscale;
1163
        u32 xsf, sr;
1164
        u16 hdelay;
1165
        u8 crop, vtc;
1166
 
1167
        int inter = (height>tvn->sheight/2) ? 0 : 1;
1168
        int off = odd ? 0x80 : 0x00;
1169
 
1170
        int swidth       = tvn->swidth;
1171
        int totalwidth   = tvn->totalwidth;
1172
        int scaledtwidth = tvn->scaledtwidth;
1173
 
1174
        if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0) {
1175
                dprintk("bttv%d: DIGITAL_MODE_VIDEO override width\n",btv->nr);
1176
                swidth       = 720;
1177
                totalwidth   = 858;
1178
                scaledtwidth = 858;
1179
        }
1180
 
1181
        xsf = (width*scaledtwidth)/swidth;
1182
        hscale = ((totalwidth*4096UL)/xsf-4096);
1183
        hdelay =  tvn->hdelayx1;
1184
        hdelay =  (hdelay*width)/swidth;
1185
        hdelay &= 0x3fe;
1186
        sr=((tvn->sheight>>inter)*512)/height-512;
1187
        vscale=(0x10000UL-sr)&0x1fff;
1188
        crop=((width>>8)&0x03)|((hdelay>>6)&0x0c)|
1189
                ((tvn->sheight>>4)&0x30)|((tvn->vdelay>>2)&0xc0);
1190
        vscale |= inter ? (BT848_VSCALE_INT<<8) : 0;
1191
 
1192
        if (combfilter) {
1193
                /* Some people say interpolation looks bad ... */
1194
                vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
1195
                if (width < 769)
1196
                        btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1197
                else
1198
                        btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1199
        } else {
1200
                vtc = 0;
1201
                btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1202
        }
1203
 
1204
        btwrite(vtc, BT848_E_VTC+off);
1205
        btwrite(hscale>>8, BT848_E_HSCALE_HI+off);
1206
        btwrite(hscale&0xff, BT848_E_HSCALE_LO+off);
1207
        btaor((vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
1208
        btwrite(vscale&0xff, BT848_E_VSCALE_LO+off);
1209
        btwrite(width&0xff, BT848_E_HACTIVE_LO+off);
1210
        btwrite(hdelay&0xff, BT848_E_HDELAY_LO+off);
1211
        btwrite(tvn->sheight&0xff, BT848_E_VACTIVE_LO+off);
1212
        btwrite(tvn->vdelay&0xff, BT848_E_VDELAY_LO+off);
1213
        btwrite(crop, BT848_E_CROP+off);
1214
}
1215
 
1216
 
1217
static void bt848_set_geo(struct bttv *btv)
1218
{
1219
        u16 ewidth, eheight, owidth, oheight;
1220
        u16 format, bswap;
1221
        struct tvnorm *tvn;
1222
 
1223
        tvn=&tvnorms[btv->win.norm];
1224
 
1225
        btwrite(tvn->adelay, BT848_ADELAY);
1226
        btwrite(tvn->bdelay, BT848_BDELAY);
1227
        btaor(tvn->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH), BT848_IFORM);
1228
        btwrite(tvn->vbipack, BT848_VBI_PACK_SIZE);
1229
        if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0)
1230
                btwrite(0x39, BT848_VBI_PACK_DEL);
1231
        else
1232
                btwrite(0x01, BT848_VBI_PACK_DEL);
1233
 
1234
        btv->pll.pll_ofreq = tvn->Fsc;
1235
        if (!in_interrupt())
1236
                set_pll(btv);
1237
 
1238
        btv->win.interlace = (btv->win.height>tvn->sheight/2) ? 1 : 0;
1239
 
1240
        if (0 == btv->risc_cap_odd &&
1241
 
1242
                /* overlay only */
1243
                owidth  = btv->win.width;
1244
                oheight = btv->win.height;
1245
                ewidth  = btv->win.width;
1246
                eheight = btv->win.height;
1247
                format  = btv->win.color_fmt;
1248
                bswap   = btv->fb_color_ctl;
1249
        } else if (-1 != btv->gq_grab      &&
1250
 
1251
                   !btv->win.interlace     &&
1252
                   btv->scr_on) {
1253
                /* odd field -> overlay, even field -> capture */
1254
                owidth  = btv->win.width;
1255
                oheight = btv->win.height;
1256
                ewidth  = btv->gbuf[btv->gq_grab].width;
1257
                eheight = btv->gbuf[btv->gq_grab].height;
1258
                format  = (btv->win.color_fmt & 0xf0) |
1259
                        (btv->gbuf[btv->gq_grab].fmt & 0x0f);
1260
                bswap   = btv->fb_color_ctl & 0x0a;
1261
        } else {
1262
                /* capture only */
1263
                owidth  = btv->gbuf[btv->gq_grab].width;
1264
                oheight = btv->gbuf[btv->gq_grab].height;
1265
                ewidth  = btv->gbuf[btv->gq_grab].width;
1266
                eheight = btv->gbuf[btv->gq_grab].height;
1267
                format  = btv->gbuf[btv->gq_grab].fmt;
1268
                bswap   = 0;
1269
        }
1270
 
1271
        /* program odd + even fields */
1272
        bt848_set_eogeo(btv, tvn, 1, owidth, oheight);
1273
        bt848_set_eogeo(btv, tvn, 0, ewidth, eheight);
1274
 
1275
        btwrite(format, BT848_COLOR_FMT);
1276
        btwrite(bswap | BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1277
}
1278
 
1279
 
1280
static int bpp2fmt[4] = {
1281
        BT848_COLOR_FMT_RGB8, BT848_COLOR_FMT_RGB16,
1282
        BT848_COLOR_FMT_RGB24, BT848_COLOR_FMT_RGB32
1283
};
1284
 
1285
static void bt848_set_winsize(struct bttv *btv)
1286
{
1287
        unsigned short format;
1288
 
1289
        if (btv->picture.palette > 0 && btv->picture.palette <= VIDEO_PALETTE_YUV422) {
1290
                /* format set by VIDIOCSPICT */
1291
                format = palette2fmt[btv->picture.palette];
1292
        } else {
1293
                /* use default for the given color depth */
1294
                format = (btv->win.depth==15) ? BT848_COLOR_FMT_RGB15 :
1295
                        bpp2fmt[(btv->win.bpp-1)&3];
1296
        }
1297
        btv->win.color_fmt = format;
1298
        if (bigendian &&
1299
            format == BT848_COLOR_FMT_RGB32) {
1300
                btv->fb_color_ctl =
1301
                        BT848_COLOR_CTL_WSWAP_ODD       |
1302
                        BT848_COLOR_CTL_WSWAP_EVEN      |
1303
                        BT848_COLOR_CTL_BSWAP_ODD       |
1304
                        BT848_COLOR_CTL_BSWAP_EVEN;
1305
        } else if (bigendian &&
1306
                   (format == BT848_COLOR_FMT_RGB16 ||
1307
                    format == BT848_COLOR_FMT_RGB15)) {
1308
                btv->fb_color_ctl =
1309
                        BT848_COLOR_CTL_BSWAP_ODD       |
1310
                        BT848_COLOR_CTL_BSWAP_EVEN;
1311
        } else {
1312
                btv->fb_color_ctl = 0;
1313
        }
1314
 
1315
        /*      RGB8 seems to be a 9x5x5 GRB color cube starting at
1316
         *      color 16. Why the h... can't they even mention this in the
1317
         *      data sheet?  [AC - because it's a standard format so I guess
1318
         *      it never occurred to them]
1319
         *      Enable dithering in this mode.
1320
         */
1321
 
1322
        if (format==BT848_COLOR_FMT_RGB8)
1323
                btand(~BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1324
        else
1325
                btor(BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1326
 
1327
        bt848_set_geo(btv);
1328
}
1329
 
1330
/*
1331
 *      Grab into virtual memory.
1332
 */
1333
 
1334
static int vgrab(struct bttv *btv, struct video_mmap *mp)
1335
{
1336
        u32 *ro, *re;
1337
        u32 *vbuf;
1338
        unsigned long flags;
1339
 
1340
        if(btv->fbuffer==NULL)
1341
        {
1342
                if(fbuffer_alloc(btv))
1343
                        return -ENOBUFS;
1344
        }
1345
 
1346
        if(mp->frame >= gbuffers || mp->frame < 0)
1347
                return -EINVAL;
1348
        if(btv->gbuf[mp->frame].stat != GBUFFER_UNUSED)
1349
                return -EBUSY;
1350
 
1351
        if(mp->height < 32 || mp->width < 48)
1352
                return -EINVAL;
1353
        if (mp->format >= PALETTEFMT_MAX)
1354
                return -EINVAL;
1355
 
1356
        if ((unsigned int)mp->height * mp->width *
1357
            fmtbppx2[palette2fmt[mp->format]&0x0f]/2 > gbufsize)
1358
                return -EINVAL;
1359
        if (UNSET == palette2fmt[mp->format])
1360
                return -EINVAL;
1361
 
1362
        /*
1363
         *      Ok load up the BT848
1364
         */
1365
 
1366
        vbuf=(unsigned int *)(btv->fbuffer+gbufsize*mp->frame);
1367
        ro=btv->gbuf[mp->frame].risc;
1368
        re=ro+2048;
1369
        make_vrisctab(btv, ro, re, vbuf, mp->width, mp->height, mp->format);
1370
 
1371
        if (bttv_debug)
1372
                printk("bttv%d: cap vgrab: queue %d (%d:%dx%d)\n",
1373
                       btv->nr,mp->frame,mp->format,mp->width,mp->height);
1374
        spin_lock_irqsave(&btv->s_lock, flags);
1375
        btv->gbuf[mp->frame].stat    = GBUFFER_GRABBING;
1376
        btv->gbuf[mp->frame].fmt     = palette2fmt[mp->format];
1377
        btv->gbuf[mp->frame].width   = mp->width;
1378
        btv->gbuf[mp->frame].height  = mp->height;
1379
        btv->gbuf[mp->frame].ro      = virt_to_bus(ro);
1380
        btv->gbuf[mp->frame].re      = virt_to_bus(re);
1381
 
1382
#if 1
1383
        if (mp->height <= tvnorms[btv->win.norm].sheight/2 &&
1384
            mp->format != VIDEO_PALETTE_RAW)
1385
                btv->gbuf[mp->frame].ro = 0;
1386
#endif
1387
 
1388
        if (-1 == btv->gq_grab && btv->gq_in == btv->gq_out) {
1389
                btv->gq_start = 1;
1390
                btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
1391
        }
1392
        btv->gqueue[btv->gq_in++] = mp->frame;
1393
        btv->gq_in = btv->gq_in % MAX_GBUFFERS;
1394
 
1395
        btor(3, BT848_CAP_CTL);
1396
        btor(3, BT848_GPIO_DMA_CTL);
1397
        spin_unlock_irqrestore(&btv->s_lock, flags);
1398
        return 0;
1399
}
1400
 
1401
static long bttv_write(struct video_device *v, const char *buf, unsigned long count, int nonblock)
1402
{
1403
        return -EINVAL;
1404
}
1405
 
1406
static long bttv_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
1407
{
1408
        /* use bttv 0.9.x if you need capture via read() */
1409
        return -EINVAL;
1410
}
1411
 
1412
static inline void burst(int on)
1413
{
1414
        tvnorms[0].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1415
        tvnorms[0].hdelayx1     = 186  - (on?BURSTOFFSET  :0);
1416
        tvnorms[2].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1417
        tvnorms[2].hdelayx1     = 186  - (on?BURSTOFFSET  :0);
1418
}
1419
 
1420
/*
1421
 * called from irq handler on fatal errors.  Takes the grabber chip
1422
 * offline, flag it needs a reinitialization (which can't be done
1423
 * from irq context) and wake up all sleeping proccesses.  They would
1424
 * block forever else.  We also need someone who actually does the
1425
 * reinitialization from process context...
1426
 */
1427
static void bt848_offline(struct bttv *btv)
1428
{
1429
        unsigned int i;
1430
        spin_lock(&btv->s_lock);
1431
 
1432
        /* cancel all outstanding grab requests */
1433
        btv->gq_in = 0;
1434
        btv->gq_out = 0;
1435
        btv->gq_grab = -1;
1436
        for (i = 0; i < gbuffers; i++)
1437
                if (btv->gbuf[i].stat == GBUFFER_GRABBING)
1438
                        btv->gbuf[i].stat = GBUFFER_ERROR;
1439
 
1440
        /* disable screen overlay and DMA */
1441
        btv->risc_cap_odd  = 0;
1442
        btv->risc_cap_even = 0;
1443
        bt848_set_risc_jmps(btv,0);
1444
 
1445
        /* flag the chip needs a restart */
1446
        btv->needs_restart = 1;
1447
        spin_unlock(&btv->s_lock);
1448
 
1449
        wake_up_interruptible(&btv->vbiq);
1450
        wake_up_interruptible(&btv->capq);
1451
}
1452
 
1453
static void bt848_restart(struct bttv *btv)
1454
{
1455
        unsigned long irq_flags;
1456
 
1457
        if (bttv_verbose)
1458
                printk("bttv%d: resetting chip\n",btv->nr);
1459
        btwrite(0xfffffUL, BT848_INT_STAT);
1460
        btand(~15, BT848_GPIO_DMA_CTL);
1461
        btwrite(0, BT848_SRESET);
1462
        btwrite(virt_to_bus(btv->risc_jmp+2),
1463
                BT848_RISC_STRT_ADD);
1464
 
1465
        /* enforce pll reprogramming */
1466
        btv->pll.pll_current = -1;
1467
        set_pll(btv);
1468
 
1469
        spin_lock_irqsave(&btv->s_lock, irq_flags);
1470
        btv->errors = 0;
1471
        btv->needs_restart = 0;
1472
        bt848_set_geo(btv);
1473
        bt848_set_risc_jmps(btv,-1);
1474
        spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1475
}
1476
 
1477
/*
1478
 *      Open a bttv card. Right now the flags stuff is just playing
1479
 */
1480
 
1481
static int bttv_open(struct video_device *dev, int flags)
1482
{
1483
        struct bttv *btv = (struct bttv *)dev;
1484
        unsigned int i;
1485
        int ret;
1486
 
1487
        ret = -EBUSY;
1488
        if (bttv_debug)
1489
                printk("bttv%d: open called\n",btv->nr);
1490
 
1491
        down(&btv->lock);
1492
        if (btv->user)
1493
                goto out_unlock;
1494
 
1495
        btv->fbuffer=(unsigned char *) rvmalloc(gbuffers*gbufsize);
1496
        ret = -ENOMEM;
1497
        if (!btv->fbuffer)
1498
                goto out_unlock;
1499
 
1500
        btv->gq_in = 0;
1501
        btv->gq_out = 0;
1502
        btv->gq_grab = -1;
1503
        for (i = 0; i < gbuffers; i++)
1504
                btv->gbuf[i].stat = GBUFFER_UNUSED;
1505
 
1506
        if (btv->needs_restart)
1507
                bt848_restart(btv);
1508
        burst(0);
1509
        set_pll(btv);
1510
        btv->user++;
1511
        up(&btv->lock);
1512
        return 0;
1513
 
1514
 out_unlock:
1515
        up(&btv->lock);
1516
        return ret;
1517
}
1518
 
1519
static void bttv_close(struct video_device *dev)
1520
{
1521
        struct bttv *btv=(struct bttv *)dev;
1522
        unsigned long irq_flags;
1523
        int need_wait;
1524
 
1525
        down(&btv->lock);
1526
        btv->user--;
1527
        spin_lock_irqsave(&btv->s_lock, irq_flags);
1528
        need_wait = (-1 != btv->gq_grab);
1529
        btv->gq_start = 0;
1530
        btv->gq_in = 0;
1531
        btv->gq_out = 0;
1532
        btv->gq_grab = -1;
1533
        btv->scr_on = 0;
1534
        btv->risc_cap_odd = 0;
1535
        btv->risc_cap_even = 0;
1536
        bt848_set_risc_jmps(btv,-1);
1537
        spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1538
 
1539
        /*
1540
         *      A word of warning. At this point the chip
1541
         *      is still capturing because its FIFO hasn't emptied
1542
         *      and the DMA control operations are posted PCI
1543
         *      operations.
1544
         */
1545
 
1546
        btread(BT848_I2C);      /* This fixes the PCI posting delay */
1547
 
1548
        if (need_wait) {
1549
                /*
1550
                 *      This is sucky but right now I can't find a good way to
1551
                 *      be sure its safe to free the buffer. We wait 5-6 fields
1552
                 *      which is more than sufficient to be sure.
1553
                 */
1554
                current->state = TASK_UNINTERRUPTIBLE;
1555
                schedule_timeout(HZ/10);        /* Wait 1/10th of a second */
1556
        }
1557
 
1558
        /*
1559
         *      We have allowed it to drain.
1560
         */
1561
 
1562
        if(btv->fbuffer)
1563
                rvfree((void *) btv->fbuffer, gbuffers*gbufsize);
1564
        btv->fbuffer=0;
1565
        up(&btv->lock);
1566
}
1567
 
1568
 
1569
/***********************************/
1570
/* ioctls and supporting functions */
1571
/***********************************/
1572
 
1573
static inline void bt848_bright(struct bttv *btv, uint bright)
1574
{
1575
        btwrite(bright&0xff, BT848_BRIGHT);
1576
}
1577
 
1578
static inline void bt848_hue(struct bttv *btv, uint hue)
1579
{
1580
        btwrite(hue&0xff, BT848_HUE);
1581
}
1582
 
1583
static inline void bt848_contrast(struct bttv *btv, uint cont)
1584
{
1585
        unsigned int conthi;
1586
 
1587
        conthi=(cont>>6)&4;
1588
        btwrite(cont&0xff, BT848_CONTRAST_LO);
1589
        btaor(conthi, ~4, BT848_E_CONTROL);
1590
        btaor(conthi, ~4, BT848_O_CONTROL);
1591
}
1592
 
1593
static inline void bt848_sat_u(struct bttv *btv, unsigned long data)
1594
{
1595
        u32 datahi;
1596
 
1597
        datahi=(data>>7)&2;
1598
        btwrite(data&0xff, BT848_SAT_U_LO);
1599
        btaor(datahi, ~2, BT848_E_CONTROL);
1600
        btaor(datahi, ~2, BT848_O_CONTROL);
1601
}
1602
 
1603
static inline void bt848_sat_v(struct bttv *btv, unsigned long data)
1604
{
1605
        u32 datahi;
1606
 
1607
        datahi=(data>>8)&1;
1608
        btwrite(data&0xff, BT848_SAT_V_LO);
1609
        btaor(datahi, ~1, BT848_E_CONTROL);
1610
        btaor(datahi, ~1, BT848_O_CONTROL);
1611
}
1612
 
1613
/*
1614
 *      ioctl routine
1615
 */
1616
 
1617
static int bttv_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
1618
{
1619
        struct bttv *btv=(struct bttv *)dev;
1620
        unsigned long irq_flags;
1621
        int ret = 0;
1622
 
1623
        if (bttv_debug > 1)
1624
                printk("bttv%d: ioctl 0x%x\n",btv->nr,cmd);
1625
 
1626
        switch (cmd) {
1627
        case VIDIOCGCAP:
1628
        {
1629
                struct video_capability b;
1630
                strcpy(b.name,btv->video_dev.name);
1631
                b.type = VID_TYPE_CAPTURE|
1632
                        ((bttv_tvcards[btv->type].tuner != UNSET) ? VID_TYPE_TUNER : 0) |
1633
                        VID_TYPE_OVERLAY|
1634
                        VID_TYPE_CLIPPING|
1635
                        VID_TYPE_FRAMERAM|
1636
                        VID_TYPE_SCALES;
1637
                b.channels = bttv_tvcards[btv->type].video_inputs;
1638
                b.audios = bttv_tvcards[btv->type].audio_inputs;
1639
                b.maxwidth = tvnorms[btv->win.norm].swidth;
1640
                b.maxheight = tvnorms[btv->win.norm].sheight;
1641
                b.minwidth = 48;
1642
                b.minheight = 32;
1643
                if(copy_to_user(arg,&b,sizeof(b)))
1644
                        return -EFAULT;
1645
                return 0;
1646
        }
1647
        case VIDIOCGCHAN:
1648
        {
1649
                struct video_channel v;
1650
                unsigned int  channel;
1651
 
1652
                if(copy_from_user(&v, arg,sizeof(v)))
1653
                        return -EFAULT;
1654
                channel = v.channel;
1655
                if (channel>=bttv_tvcards[btv->type].video_inputs)
1656
                        return -EINVAL;
1657
                v.flags=VIDEO_VC_AUDIO;
1658
                v.tuners=0;
1659
                v.type=VIDEO_TYPE_CAMERA;
1660
                v.norm = btv->win.norm;
1661
                if(channel==bttv_tvcards[btv->type].tuner)
1662
                {
1663
                        strcpy(v.name,"Television");
1664
                        v.flags|=VIDEO_VC_TUNER;
1665
                        v.type=VIDEO_TYPE_TV;
1666
                        v.tuners=1;
1667
                }
1668
                else if (channel == btv->svhs)
1669
                        strcpy(v.name,"S-Video");
1670
                else if (bttv_tvcards[btv->type].muxsel[v.channel] < 0)
1671
                        strcpy(v.name,"Digital Video");
1672
                else
1673
                        sprintf(v.name,"Composite%d",v.channel);
1674
 
1675
                if(copy_to_user(arg,&v,sizeof(v)))
1676
                        return -EFAULT;
1677
                return 0;
1678
        }
1679
        /*
1680
         *      Each channel has 1 tuner
1681
         */
1682
        case VIDIOCSCHAN:
1683
        {
1684
                struct video_channel v;
1685
                unsigned int  channel;
1686
 
1687
                if(copy_from_user(&v, arg,sizeof(v)))
1688
                        return -EFAULT;
1689
                channel = v.channel;
1690
 
1691
                if (channel>bttv_tvcards[btv->type].video_inputs)
1692
                        return -EINVAL;
1693
                if (v.norm > TVNORMS)
1694
                        return -EOPNOTSUPP;
1695
 
1696
                bttv_call_i2c_clients(btv,cmd,&v);
1697
                down(&btv->lock);
1698
                bt848_muxsel(btv, channel);
1699
                bttv_set_norm(btv, v.norm);
1700
                up(&btv->lock);
1701
                return 0;
1702
        }
1703
        case VIDIOCGTUNER:
1704
        {
1705
                struct video_tuner v;
1706
                if(copy_from_user(&v,arg,sizeof(v))!=0)
1707
                        return -EFAULT;
1708
#if 0 /* tuner.signal might be of intrest for non-tuner sources too ... */
1709
                if(v.tuner||btv->channel)       /* Only tuner 0 */
1710
                        return -EINVAL;
1711
#endif
1712
                strcpy(v.name, "Television");
1713
                v.rangelow=0;
1714
                v.rangehigh=0xFFFFFFFF;
1715
                v.flags=VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1716
                v.mode = btv->win.norm;
1717
                v.signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1718
                bttv_call_i2c_clients(btv,cmd,&v);
1719
                if(copy_to_user(arg,&v,sizeof(v)))
1720
                        return -EFAULT;
1721
                return 0;
1722
        }
1723
        /* We have but one tuner */
1724
        case VIDIOCSTUNER:
1725
        {
1726
                struct video_tuner v;
1727
                unsigned int tuner;
1728
 
1729
                if(copy_from_user(&v, arg, sizeof(v)))
1730
                        return -EFAULT;
1731
                tuner = v.tuner;
1732
                /* Only one channel has a tuner */
1733
                if(tuner!=bttv_tvcards[btv->type].tuner)
1734
                        return -EINVAL;
1735
 
1736
                if(v.mode!=VIDEO_MODE_PAL&&v.mode!=VIDEO_MODE_NTSC
1737
                   &&v.mode!=VIDEO_MODE_SECAM)
1738
                        return -EOPNOTSUPP;
1739
                bttv_call_i2c_clients(btv,cmd,&v);
1740
                if (btv->win.norm != v.mode) {
1741
                        down(&btv->lock);
1742
                        bttv_set_norm(btv,v.mode);
1743
                        up(&btv->lock);
1744
                }
1745
                return 0;
1746
        }
1747
        case VIDIOCGPICT:
1748
        {
1749
                struct video_picture p=btv->picture;
1750
                if(copy_to_user(arg, &p, sizeof(p)))
1751
                        return -EFAULT;
1752
                return 0;
1753
        }
1754
        case VIDIOCSPICT:
1755
        {
1756
                struct video_picture p;
1757
 
1758
                if (copy_from_user(&p, arg,sizeof(p)))
1759
                        return -EFAULT;
1760
                if (p.palette > PALETTEFMT_MAX)
1761
                        return -EINVAL;
1762
                if (UNSET == palette2fmt[p.palette])
1763
                        return -EINVAL;
1764
                down(&btv->lock);
1765
                /* We want -128 to 127 we get 0-65535 */
1766
                bt848_bright(btv, (p.brightness>>8)-128);
1767
                /* 0-511 for the colour */
1768
                bt848_sat_u(btv, p.colour>>7);
1769
                bt848_sat_v(btv, ((p.colour>>7)*201L)/237);
1770
                /* -128 to 127 */
1771
                bt848_hue(btv, (p.hue>>8)-128);
1772
                /* 0-511 */
1773
                bt848_contrast(btv, p.contrast>>7);
1774
                btv->picture = p;
1775
                up(&btv->lock);
1776
                return 0;
1777
        }
1778
        case VIDIOCSWIN:
1779
        {
1780
                struct video_window vw;
1781
                struct video_clip *vcp = NULL;
1782
 
1783
                if(copy_from_user(&vw,arg,sizeof(vw)))
1784
                        return -EFAULT;
1785
 
1786
                down(&btv->lock);
1787
                if(vw.flags || vw.width < 16 || vw.height < 16)
1788
                {
1789
                        spin_lock_irqsave(&btv->s_lock, irq_flags);
1790
                        btv->scr_on = 0;
1791
                        bt848_set_risc_jmps(btv,-1);
1792
                        spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1793
                        up(&btv->lock);
1794
                        return -EINVAL;
1795
                }
1796
                if (btv->win.bpp < 4)
1797
                {       /* adjust and align writes */
1798
                        vw.x = (vw.x + 3) & ~3;
1799
                        vw.width &= ~3;
1800
                }
1801
                if (btv->needs_restart)
1802
                        bt848_restart(btv);
1803
                btv->win.x=vw.x;
1804
                btv->win.y=vw.y;
1805
                btv->win.width=vw.width;
1806
                btv->win.height=vw.height;
1807
 
1808
                spin_lock_irqsave(&btv->s_lock, irq_flags);
1809
                bt848_set_risc_jmps(btv,0);
1810
                bt848_set_winsize(btv);
1811
                spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1812
 
1813
                /*
1814
                 *      Do any clips.
1815
                 */
1816
                if(vw.clipcount<0) {
1817
                        if((vcp=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL) {
1818
                                up(&btv->lock);
1819
                                return -ENOMEM;
1820
                        }
1821
                        if(copy_from_user(vcp, vw.clips,
1822
                                          VIDEO_CLIPMAP_SIZE)) {
1823
                                up(&btv->lock);
1824
                                vfree(vcp);
1825
                                return -EFAULT;
1826
                        }
1827
                } else if (vw.clipcount > 2048) {
1828
                        up(&btv->lock);
1829
                        return -EINVAL;
1830
                } else if (vw.clipcount) {
1831
                        if((vcp=vmalloc(sizeof(struct video_clip)*
1832
                                        (vw.clipcount))) == NULL) {
1833
                                up(&btv->lock);
1834
                                return -ENOMEM;
1835
                        }
1836
                        if(copy_from_user(vcp,vw.clips,
1837
                                          sizeof(struct video_clip)*
1838
                                          vw.clipcount)) {
1839
                                up(&btv->lock);
1840
                                vfree(vcp);
1841
                                return -EFAULT;
1842
                        }
1843
                }
1844
                make_clip_tab(btv, vcp, vw.clipcount);
1845
                if (vw.clipcount != 0)
1846
                        vfree(vcp);
1847
                spin_lock_irqsave(&btv->s_lock, irq_flags);
1848
                bt848_set_risc_jmps(btv,-1);
1849
                spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1850
                up(&btv->lock);
1851
                return 0;
1852
        }
1853
        case VIDIOCGWIN:
1854
        {
1855
                struct video_window vw;
1856
                memset(&vw,0,sizeof(vw));
1857
                vw.x=btv->win.x;
1858
                vw.y=btv->win.y;
1859
                vw.width=btv->win.width;
1860
                vw.height=btv->win.height;
1861
                if(btv->win.interlace)
1862
                        vw.flags|=VIDEO_WINDOW_INTERLACE;
1863
                if(copy_to_user(arg,&vw,sizeof(vw)))
1864
                        return -EFAULT;
1865
                return 0;
1866
        }
1867
        case VIDIOCCAPTURE:
1868
        {
1869
                int v;
1870
                if(copy_from_user(&v, arg,sizeof(v)))
1871
                        return -EFAULT;
1872
                if(btv->win.vidadr == 0)
1873
                        return -EINVAL;
1874
                if (btv->win.width==0 || btv->win.height==0)
1875
                        return -EINVAL;
1876
                if (1 == no_overlay)
1877
                        return -EIO;
1878
                spin_lock_irqsave(&btv->s_lock, irq_flags);
1879
                if (v == 1 && btv->win.vidadr != 0)
1880
                        btv->scr_on = 1;
1881
                if (v == 0)
1882
                        btv->scr_on = 0;
1883
                bt848_set_risc_jmps(btv,-1);
1884
                spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1885
                return 0;
1886
        }
1887
        case VIDIOCGFBUF:
1888
        {
1889
                struct video_buffer v;
1890
                v.base=(void *)btv->win.vidadr;
1891
                v.height=btv->win.sheight;
1892
                v.width=btv->win.swidth;
1893
                v.depth=btv->win.depth;
1894
                v.bytesperline=btv->win.bpl;
1895
                if(copy_to_user(arg, &v,sizeof(v)))
1896
                        return -EFAULT;
1897
                return 0;
1898
 
1899
        }
1900
        case VIDIOCSFBUF:
1901
        {
1902
                struct video_buffer v;
1903
                if(!capable(CAP_SYS_ADMIN) &&
1904
                   !capable(CAP_SYS_RAWIO))
1905
                        return -EPERM;
1906
                if(copy_from_user(&v, arg,sizeof(v)))
1907
                        return -EFAULT;
1908
                if(v.depth!=8 && v.depth!=15 && v.depth!=16 &&
1909
                   v.depth!=24 && v.depth!=32 && v.width > 16 &&
1910
                   v.height > 16 && v.bytesperline > 16)
1911
                        return -EINVAL;
1912
                down(&btv->lock);
1913
                if (v.base)
1914
                        btv->win.vidadr=(unsigned long)v.base;
1915
                btv->win.sheight=v.height;
1916
                btv->win.swidth=v.width;
1917
                btv->win.bpp=((v.depth+7)&0x38)/8;
1918
                btv->win.depth=v.depth;
1919
                btv->win.bpl=v.bytesperline;
1920
 
1921
#if 0 /* was broken for ages and nobody noticed.  Looks like we don't need
1922
         it any more as everybody explicitly sets the palette using VIDIOCSPICT
1923
         these days */
1924
                /* set sefault color format */
1925
                switch (v.depth) {
1926
                case  8: btv->picture.palette = VIDEO_PALETTE_HI240;  break;
1927
                case 15: btv->picture.palette = VIDEO_PALETTE_RGB555; break;
1928
                case 16: btv->picture.palette = VIDEO_PALETTE_RGB565; break;
1929
                case 24: btv->picture.palette = VIDEO_PALETTE_RGB24;  break;
1930
                case 32: btv->picture.palette = VIDEO_PALETTE_RGB32;  break;
1931
                }
1932
#endif
1933
 
1934
                if (bttv_debug)
1935
                        printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1936
                               v.base, v.width,v.height, btv->win.bpp, btv->win.bpl);
1937
                spin_lock_irqsave(&btv->s_lock, irq_flags);
1938
                bt848_set_winsize(btv);
1939
                spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1940
                up(&btv->lock);
1941
                return 0;
1942
        }
1943
        case VIDIOCKEY:
1944
        {
1945
                /* Will be handled higher up .. */
1946
                return 0;
1947
        }
1948
        case VIDIOCGFREQ:
1949
        {
1950
                unsigned long v=btv->win.freq;
1951
                if(copy_to_user(arg,&v,sizeof(v)))
1952
                        return -EFAULT;
1953
                return 0;
1954
        }
1955
        case VIDIOCSFREQ:
1956
        {
1957
                unsigned long v;
1958
                if(copy_from_user(&v, arg, sizeof(v)))
1959
                        return -EFAULT;
1960
                btv->win.freq=v;
1961
                bttv_call_i2c_clients(btv,cmd,&v);
1962
#if 1
1963
                if (btv->radio && btv->has_matchbox)
1964
                        tea5757_set_freq(btv,v);
1965
#endif
1966
                return 0;
1967
        }
1968
 
1969
        case VIDIOCGAUDIO:
1970
        {
1971
                struct video_audio v;
1972
 
1973
                v=btv->audio_dev;
1974
                v.flags&=~(VIDEO_AUDIO_MUTE|VIDEO_AUDIO_MUTABLE);
1975
                v.flags|=VIDEO_AUDIO_MUTABLE;
1976
                strcpy(v.name,"TV");
1977
 
1978
                v.mode = VIDEO_SOUND_MONO;
1979
                bttv_call_i2c_clients(btv,cmd,&v);
1980
 
1981
                /* card specific hooks */
1982
                if (btv->audio_hook)
1983
                        btv->audio_hook(btv,&v,0);
1984
 
1985
                if(copy_to_user(arg,&v,sizeof(v)))
1986
                        return -EFAULT;
1987
                return 0;
1988
        }
1989
        case VIDIOCSAUDIO:
1990
        {
1991
                struct video_audio v;
1992
                unsigned int n;
1993
 
1994
                if(copy_from_user(&v,arg, sizeof(v)))
1995
                        return -EFAULT;
1996
                n = v.audio;
1997
                if(n >= bttv_tvcards[btv->type].audio_inputs)
1998
                        return -EINVAL;
1999
                down(&btv->lock);
2000
                if(v.flags&VIDEO_AUDIO_MUTE)
2001
                        audio(btv, AUDIO_MUTE);
2002
                /* bt848_muxsel(btv,v.audio); */
2003
                if(!(v.flags&VIDEO_AUDIO_MUTE))
2004
                        audio(btv, AUDIO_UNMUTE);
2005
 
2006
                bttv_call_i2c_clients(btv,cmd,&v);
2007
 
2008
                /* card specific hooks */
2009
                if (btv->audio_hook)
2010
                        btv->audio_hook(btv,&v,1);
2011
 
2012
                btv->audio_dev=v;
2013
                up(&btv->lock);
2014
                return 0;
2015
        }
2016
 
2017
        case VIDIOCSYNC:
2018
        {
2019
                DECLARE_WAITQUEUE(wait, current);
2020
                unsigned int i;
2021
 
2022
                if(copy_from_user((void *)&i,arg,sizeof(int)))
2023
                        return -EFAULT;
2024
                if (i >= gbuffers)
2025
                        return -EINVAL;
2026
                switch (btv->gbuf[i].stat) {
2027
                case GBUFFER_UNUSED:
2028
                        ret = -EINVAL;
2029
                        break;
2030
                case GBUFFER_GRABBING:
2031
                        add_wait_queue(&btv->capq, &wait);
2032
                        current->state = TASK_INTERRUPTIBLE;
2033
                        while(btv->gbuf[i].stat==GBUFFER_GRABBING) {
2034
                                if (bttv_debug)
2035
                                        printk("bttv%d: cap sync: sleep on %d\n",btv->nr,i);
2036
                                schedule();
2037
                                if(signal_pending(current)) {
2038
                                        remove_wait_queue(&btv->capq, &wait);
2039
                                        current->state = TASK_RUNNING;
2040
                                        return -EINTR;
2041
                                }
2042
                        }
2043
                        remove_wait_queue(&btv->capq, &wait);
2044
                        current->state = TASK_RUNNING;
2045
                        /* fall throuth */
2046
                case GBUFFER_DONE:
2047
                case GBUFFER_ERROR:
2048
                        ret = (btv->gbuf[i].stat == GBUFFER_ERROR) ? -EIO : 0;
2049
                        if (bttv_debug)
2050
                                printk("bttv%d: cap sync: buffer %d, retval %d\n",btv->nr,i,ret);
2051
                        btv->gbuf[i].stat = GBUFFER_UNUSED;
2052
                }
2053
                if (btv->needs_restart) {
2054
                        down(&btv->lock);
2055
                        bt848_restart(btv);
2056
                        up(&btv->lock);
2057
                }
2058
                return ret;
2059
        }
2060
 
2061
        case BTTV_FIELDNR:
2062
                if(copy_to_user((void *) arg, (void *) &btv->last_field,
2063
                                sizeof(btv->last_field)))
2064
                        return -EFAULT;
2065
                break;
2066
 
2067
        case BTTV_PLLSET: {
2068
                struct bttv_pll_info p;
2069
                if(!capable(CAP_SYS_ADMIN))
2070
                        return -EPERM;
2071
                if(copy_from_user(&p , (void *) arg, sizeof(btv->pll)))
2072
                        return -EFAULT;
2073
                down(&btv->lock);
2074
                btv->pll.pll_ifreq = p.pll_ifreq;
2075
                btv->pll.pll_ofreq = p.pll_ofreq;
2076
                btv->pll.pll_crystal = p.pll_crystal;
2077
                up(&btv->lock);
2078
                break;
2079
        }
2080
 
2081
        case VIDIOCMCAPTURE:
2082
        {
2083
                struct video_mmap vm;
2084
                int ret;
2085
                if(copy_from_user((void *) &vm, (void *) arg, sizeof(vm)))
2086
                        return -EFAULT;
2087
                down(&btv->lock);
2088
                ret = vgrab(btv, &vm);
2089
                up(&btv->lock);
2090
                return ret;
2091
        }
2092
 
2093
        case VIDIOCGMBUF:
2094
        {
2095
                struct video_mbuf vm;
2096
                unsigned int i;
2097
 
2098
                memset(&vm, 0 , sizeof(vm));
2099
                vm.size=gbufsize*gbuffers;
2100
                vm.frames=gbuffers;
2101
                for (i = 0; i < gbuffers; i++)
2102
                        vm.offsets[i]=i*gbufsize;
2103
                if(copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
2104
                        return -EFAULT;
2105
                return 0;
2106
        }
2107
 
2108
        case VIDIOCGUNIT:
2109
        {
2110
                struct video_unit vu;
2111
                vu.video=btv->video_dev.minor;
2112
                vu.vbi=btv->vbi_dev.minor;
2113
                if(btv->radio_dev.minor!=-1)
2114
                        vu.radio=btv->radio_dev.minor;
2115
                else
2116
                        vu.radio=VIDEO_NO_UNIT;
2117
                vu.audio=VIDEO_NO_UNIT;
2118
                vu.teletext=VIDEO_NO_UNIT;
2119
                if(copy_to_user((void *)arg, (void *)&vu, sizeof(vu)))
2120
                        return -EFAULT;
2121
                return 0;
2122
        }
2123
 
2124
        case BTTV_BURST_ON:
2125
        {
2126
                burst(1);
2127
                return 0;
2128
        }
2129
 
2130
        case BTTV_BURST_OFF:
2131
        {
2132
                burst(0);
2133
                return 0;
2134
        }
2135
 
2136
        case BTTV_VERSION:
2137
        {
2138
                return BTTV_VERSION_CODE;
2139
        }
2140
 
2141
        case BTTV_PICNR:
2142
        {
2143
                /* return picture;*/
2144
                return  0;
2145
        }
2146
 
2147
        default:
2148
                return -ENOIOCTLCMD;
2149
        }
2150
        return 0;
2151
}
2152
 
2153
/*
2154
 *      This maps the vmalloced and reserved fbuffer to user space.
2155
 *
2156
 *  FIXME:
2157
 *  - PAGE_READONLY should suffice!?
2158
 *  - remap_page_range is kind of inefficient for page by page remapping.
2159
 *    But e.g. pte_alloc() does not work in modules ... :-(
2160
 */
2161
 
2162
static int do_bttv_mmap(struct bttv *btv, const char *adr, unsigned long size)
2163
{
2164
        unsigned long start=(unsigned long) adr;
2165
        unsigned long page,pos;
2166
 
2167
        if (size>gbuffers*gbufsize)
2168
                return -EINVAL;
2169
        if (!btv->fbuffer) {
2170
                if(fbuffer_alloc(btv))
2171
                        return -EINVAL;
2172
        }
2173
        pos=(unsigned long) btv->fbuffer;
2174
        while (size > 0) {
2175
                page = kvirt_to_pa(pos);
2176
                if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
2177
                        return -EAGAIN;
2178
                start+=PAGE_SIZE;
2179
                pos+=PAGE_SIZE;
2180
                size-=PAGE_SIZE;
2181
        }
2182
        return 0;
2183
}
2184
 
2185
static int bttv_mmap(struct video_device *dev, const char *adr, unsigned long size)
2186
{
2187
        struct bttv *btv=(struct bttv *)dev;
2188
        int r;
2189
 
2190
        down(&btv->lock);
2191
        r=do_bttv_mmap(btv, adr, size);
2192
        up(&btv->lock);
2193
        return r;
2194
}
2195
 
2196
 
2197
static struct video_device bttv_template=
2198
{
2199
        .owner          = THIS_MODULE,
2200
        .name           = "bttv video",
2201
        .type           = VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY|VID_TYPE_TELETEXT,
2202
        .hardware       = VID_HARDWARE_BT848,
2203
        .open           = bttv_open,
2204
        .close          = bttv_close,
2205
        .read           = bttv_read,
2206
        .write          = bttv_write,
2207
        .ioctl          = bttv_ioctl,
2208
        .mmap           = bttv_mmap,
2209
        .minor          = -1,
2210
};
2211
 
2212
 
2213
static long vbi_read(struct video_device *v, char *buf, unsigned long count,
2214
                     int nonblock)
2215
{
2216
        struct bttv *btv=(struct bttv *)(v-2);
2217
        unsigned long todo;
2218
        unsigned int q;
2219
        DECLARE_WAITQUEUE(wait, current);
2220
 
2221
        todo=count;
2222
        while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
2223
        {
2224
                if (btv->needs_restart) {
2225
                        down(&btv->lock);
2226
                        bt848_restart(btv);
2227
                        up(&btv->lock);
2228
                }
2229
                if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
2230
                        return -EFAULT;
2231
                todo-=q;
2232
                buf+=q;
2233
 
2234
                add_wait_queue(&btv->vbiq, &wait);
2235
                current->state = TASK_INTERRUPTIBLE;
2236
                if (todo && q==VBIBUF_SIZE-btv->vbip)
2237
                {
2238
                        if(nonblock)
2239
                        {
2240
                                remove_wait_queue(&btv->vbiq, &wait);
2241
                                current->state = TASK_RUNNING;
2242
                                if(count==todo)
2243
                                        return -EWOULDBLOCK;
2244
                                return count-todo;
2245
                        }
2246
                        schedule();
2247
                        if(signal_pending(current))
2248
                        {
2249
                                remove_wait_queue(&btv->vbiq, &wait);
2250
                                current->state = TASK_RUNNING;
2251
                                if(todo==count)
2252
                                        return -EINTR;
2253
                                else
2254
                                        return count-todo;
2255
                        }
2256
                }
2257
                remove_wait_queue(&btv->vbiq, &wait);
2258
                current->state = TASK_RUNNING;
2259
        }
2260
        if (todo)
2261
        {
2262
                if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
2263
                        return -EFAULT;
2264
                btv->vbip+=todo;
2265
        }
2266
        return count;
2267
}
2268
 
2269
static unsigned int vbi_poll(struct video_device *dev, struct file *file,
2270
        poll_table *wait)
2271
{
2272
        struct bttv *btv=(struct bttv *)(dev-2);
2273
        unsigned int mask = 0;
2274
 
2275
        poll_wait(file, &btv->vbiq, wait);
2276
 
2277
        if (btv->vbip < VBIBUF_SIZE)
2278
                mask |= (POLLIN | POLLRDNORM);
2279
 
2280
        return mask;
2281
}
2282
 
2283
static int vbi_open(struct video_device *dev, int flags)
2284
{
2285
        struct bttv *btv=(struct bttv *)(dev-2);
2286
        unsigned long irq_flags;
2287
 
2288
        down(&btv->lock);
2289
        if (btv->needs_restart)
2290
                bt848_restart(btv);
2291
        set_pll(btv);
2292
        btv->vbip=VBIBUF_SIZE;
2293
        spin_lock_irqsave(&btv->s_lock, irq_flags);
2294
        btv->vbi_on = 1;
2295
        bt848_set_risc_jmps(btv,-1);
2296
        spin_unlock_irqrestore(&btv->s_lock, irq_flags);
2297
        up(&btv->lock);
2298
 
2299
        return 0;
2300
}
2301
 
2302
static void vbi_close(struct video_device *dev)
2303
{
2304
        struct bttv *btv=(struct bttv *)(dev-2);
2305
        unsigned long irq_flags;
2306
 
2307
        spin_lock_irqsave(&btv->s_lock, irq_flags);
2308
        btv->vbi_on = 0;
2309
        bt848_set_risc_jmps(btv,-1);
2310
        spin_unlock_irqrestore(&btv->s_lock, irq_flags);
2311
}
2312
 
2313
static int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2314
{
2315
        struct bttv *btv=(struct bttv *)(dev-2);
2316
 
2317
        switch (cmd) {
2318
        case VIDIOCGCAP:
2319
        {
2320
                struct video_capability b;
2321
                strcpy(b.name,btv->vbi_dev.name);
2322
                b.type = ((bttv_tvcards[btv->type].tuner != UNSET) ? VID_TYPE_TUNER : 0)
2323
                        | VID_TYPE_TELETEXT;
2324
                b.channels = 0;
2325
                b.audios = 0;
2326
                b.maxwidth = 0;
2327
                b.maxheight = 0;
2328
                b.minwidth = 0;
2329
                b.minheight = 0;
2330
                if(copy_to_user(arg,&b,sizeof(b)))
2331
                        return -EFAULT;
2332
                return 0;
2333
        }
2334
        case VIDIOCGFREQ:
2335
        case VIDIOCSFREQ:
2336
        case VIDIOCGTUNER:
2337
        case VIDIOCSTUNER:
2338
        case VIDIOCGCHAN:
2339
        case VIDIOCSCHAN:
2340
        case BTTV_VERSION:
2341
                return bttv_ioctl(dev-2,cmd,arg);
2342
        case BTTV_VBISIZE:
2343
                /* make alevt happy :-) */
2344
                return VBIBUF_SIZE;
2345
        default:
2346
                return -EINVAL;
2347
        }
2348
}
2349
 
2350
static struct video_device vbi_template=
2351
{
2352
        .owner          = THIS_MODULE,
2353
        .name           = "bttv vbi",
2354
        .type           = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
2355
        .hardware       = VID_HARDWARE_BT848,
2356
        .open           = vbi_open,
2357
        .close          = vbi_close,
2358
        .read           = vbi_read,
2359
        .write          = bttv_write,
2360
        .poll           = vbi_poll,
2361
        .ioctl          = vbi_ioctl,
2362
        .minor          = -1,
2363
};
2364
 
2365
 
2366
static int radio_open(struct video_device *dev, int flags)
2367
{
2368
        struct bttv *btv = (struct bttv *)(dev-1);
2369
 
2370
        down(&btv->lock);
2371
        if (btv->user)
2372
                goto busy_unlock;
2373
        btv->user++;
2374
 
2375
        btv->radio = 1;
2376
        bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
2377
        bt848_muxsel(btv,0);
2378
        up(&btv->lock);
2379
 
2380
        return 0;
2381
 
2382
 busy_unlock:
2383
        up(&btv->lock);
2384
        return -EBUSY;
2385
}
2386
 
2387
static void radio_close(struct video_device *dev)
2388
{
2389
        struct bttv *btv=(struct bttv *)(dev-1);
2390
 
2391
        down(&btv->lock);
2392
        btv->user--;
2393
        btv->radio = 0;
2394
        up(&btv->lock);
2395
}
2396
 
2397
static long radio_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
2398
{
2399
        return -EINVAL;
2400
}
2401
 
2402
static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2403
{
2404
        struct bttv *btv=(struct bttv *)(dev-1);
2405
        switch (cmd) {
2406
        case VIDIOCGCAP:
2407
        {
2408
                struct video_capability v;
2409
                strcpy(v.name,btv->video_dev.name);
2410
                v.type = VID_TYPE_TUNER;
2411
                v.channels = 1;
2412
                v.audios = 1;
2413
                /* No we don't do pictures */
2414
                v.maxwidth = 0;
2415
                v.maxheight = 0;
2416
                v.minwidth = 0;
2417
                v.minheight = 0;
2418
                if (copy_to_user(arg, &v, sizeof(v)))
2419
                        return -EFAULT;
2420
                return 0;
2421
                break;
2422
        }
2423
        case VIDIOCGTUNER:
2424
        {
2425
                struct video_tuner v;
2426
                if(copy_from_user(&v,arg,sizeof(v))!=0)
2427
                        return -EFAULT;
2428
                if(v.tuner||btv->channel)       /* Only tuner 0 */
2429
                        return -EINVAL;
2430
                strcpy(v.name, "Radio");
2431
                /* japan:          76.0 MHz -  89.9 MHz
2432
                   western europe: 87.5 MHz - 108.0 MHz
2433
                   russia:         65.0 MHz - 108.0 MHz */
2434
                v.rangelow=(int)(65*16);
2435
                v.rangehigh=(int)(108*16);
2436
                v.flags= 0; /* XXX */
2437
                v.mode = 0; /* XXX */
2438
                bttv_call_i2c_clients(btv,cmd,&v);
2439
                if(copy_to_user(arg,&v,sizeof(v)))
2440
                        return -EFAULT;
2441
                return 0;
2442
        }
2443
        case VIDIOCSTUNER:
2444
        {
2445
                struct video_tuner v;
2446
                if(copy_from_user(&v, arg, sizeof(v)))
2447
                        return -EFAULT;
2448
                /* Only channel 0 has a tuner */
2449
                if(v.tuner!=0 || btv->channel)
2450
                        return -EINVAL;
2451
                /* XXX anything to do ??? */
2452
                return 0;
2453
        }
2454
        case VIDIOCGFREQ:
2455
        case VIDIOCSFREQ:
2456
        case VIDIOCGAUDIO:
2457
        case VIDIOCSAUDIO:
2458
                bttv_ioctl((struct video_device *)btv,cmd,arg);
2459
                break;
2460
        default:
2461
                return -ENOIOCTLCMD;
2462
        }
2463
        return 0;
2464
}
2465
 
2466
static struct video_device radio_template=
2467
{
2468
        .owner          = THIS_MODULE,
2469
        .name           = "bttv radio",
2470
        .type           = VID_TYPE_TUNER,
2471
        .hardware       = VID_HARDWARE_BT848,
2472
        .open           = radio_open,
2473
        .close          = radio_close,
2474
        .read           = radio_read,          /* just returns -EINVAL */
2475
        .write          = bttv_write,          /* just returns -EINVAL */
2476
        .ioctl          = radio_ioctl,
2477
        .minor          = -1,
2478
};
2479
 
2480
 
2481
static void bt848_set_risc_jmps(struct bttv *btv, int flags)
2482
{
2483
        if (-1 == flags) {
2484
                /* defaults */
2485
                flags = 0;
2486
                if (btv->scr_on)
2487
                        flags |= 0x03;
2488
                if (btv->vbi_on)
2489
                        flags |= 0x0c;
2490
                if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0)
2491
                        flags |= 0x0c;
2492
#if 0
2493
                /* Hmm ... */
2494
                if ((0 != btv->risc_cap_even) ||
2495
                    (0 != btv->risc_cap_odd))
2496
                        flags |= 0x0c;
2497
#endif
2498
        }
2499
 
2500
        if (bttv_debug > 1)
2501
                printk("bttv%d: set_risc_jmp %08lx:",
2502
                       btv->nr,virt_to_bus(btv->risc_jmp));
2503
 
2504
        /* Sync to start of odd field */
2505
        btv->risc_jmp[0]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
2506
                                |BT848_FIFO_STATUS_VRE);
2507
        btv->risc_jmp[1]=cpu_to_le32(0);
2508
 
2509
        /* Jump to odd vbi sub */
2510
        btv->risc_jmp[2]=cpu_to_le32(BT848_RISC_JUMP|(0xd<<20));
2511
        if (flags&8) {
2512
                if (bttv_debug > 1)
2513
                        printk(" ev=%08lx",virt_to_bus(btv->vbi_odd));
2514
                btv->risc_jmp[3]=cpu_to_le32(virt_to_bus(btv->vbi_odd));
2515
        } else {
2516
                if (bttv_debug > 1)
2517
                        printk(" -----------");
2518
                btv->risc_jmp[3]=cpu_to_le32(virt_to_bus(btv->risc_jmp+4));
2519
        }
2520
 
2521
        /* Jump to odd sub */
2522
        btv->risc_jmp[4]=cpu_to_le32(BT848_RISC_JUMP|(0xe<<20));
2523
        if (0 != btv->risc_cap_odd) {
2524
                if (bttv_debug > 1)
2525
                        printk(" e%d=%08x",btv->gq_grab,btv->risc_cap_odd);
2526
                flags |= 3;
2527
                btv->risc_jmp[5]=cpu_to_le32(btv->risc_cap_odd);
2528
        } else if ((flags&2) &&
2529
                   (!btv->win.interlace || 0 == btv->risc_cap_even)) {
2530
                if (bttv_debug > 1)
2531
                        printk(" eo=%08lx",virt_to_bus(btv->risc_scr_odd));
2532
                btv->risc_jmp[5]=cpu_to_le32(virt_to_bus(btv->risc_scr_odd));
2533
        } else {
2534
                if (bttv_debug > 1)
2535
                        printk(" -----------");
2536
                btv->risc_jmp[5]=cpu_to_le32(virt_to_bus(btv->risc_jmp+6));
2537
        }
2538
 
2539
 
2540
        /* Sync to start of even field */
2541
        btv->risc_jmp[6]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
2542
                                |BT848_FIFO_STATUS_VRO);
2543
        btv->risc_jmp[7]=cpu_to_le32(0);
2544
 
2545
        /* Jump to even vbi sub */
2546
        btv->risc_jmp[8]=cpu_to_le32(BT848_RISC_JUMP);
2547
        if (flags&4) {
2548
                if (bttv_debug > 1)
2549
                        printk(" ov=%08lx",virt_to_bus(btv->vbi_even));
2550
                btv->risc_jmp[9]=cpu_to_le32(virt_to_bus(btv->vbi_even));
2551
        } else {
2552
                if (bttv_debug > 1)
2553
                        printk(" -----------");
2554
                btv->risc_jmp[9]=cpu_to_le32(virt_to_bus(btv->risc_jmp+10));
2555
        }
2556
 
2557
        /* Jump to even sub */
2558
        btv->risc_jmp[10]=cpu_to_le32(BT848_RISC_JUMP|(8<<20));
2559
        if (0 != btv->risc_cap_even) {
2560
                if (bttv_debug > 1)
2561
                        printk(" o%d=%08x",btv->gq_grab,btv->risc_cap_even);
2562
                flags |= 3;
2563
                btv->risc_jmp[11]=cpu_to_le32(btv->risc_cap_even);
2564
        } else if ((flags&1) &&
2565
                   btv->win.interlace) {
2566
                if (bttv_debug > 1)
2567
                        printk(" oo=%08lx",virt_to_bus(btv->risc_scr_even));
2568
                btv->risc_jmp[11]=cpu_to_le32(virt_to_bus(btv->risc_scr_even));
2569
        } else {
2570
                if (bttv_debug > 1)
2571
                        printk(" -----------");
2572
                btv->risc_jmp[11]=cpu_to_le32(virt_to_bus(btv->risc_jmp+12));
2573
        }
2574
 
2575
        if (btv->gq_start) {
2576
                btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
2577
        } else {
2578
                btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP);
2579
        }
2580
        btv->risc_jmp[13]=cpu_to_le32(virt_to_bus(btv->risc_jmp));
2581
 
2582
        /* enable cpaturing and DMA */
2583
        if (bttv_debug > 1)
2584
                printk(" flags=0x%x dma=%s\n",
2585
                       flags,(flags&0x0f) ? "on" : "off");
2586
        btaor(flags, ~0x0f, BT848_CAP_CTL);
2587
        if (flags&0x0f)
2588
                bt848_dma(btv, 3);
2589
        else
2590
                bt848_dma(btv, 0);
2591
}
2592
 
2593
static int __devinit init_video_dev(struct bttv *btv)
2594
{
2595
        audio(btv, AUDIO_MUTE);
2596
 
2597
        if (video_register_device(&btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
2598
                return -1;
2599
        printk(KERN_INFO "bttv%d: registered device video%d\n",
2600
               btv->nr,btv->video_dev.minor & 0x1f);
2601
        if (video_register_device(&btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0) {
2602
                video_unregister_device(&btv->video_dev);
2603
                return -1;
2604
        }
2605
        printk(KERN_INFO "bttv%d: registered device vbi%d\n",
2606
               btv->nr,btv->vbi_dev.minor & 0x1f);
2607
        if (btv->has_radio) {
2608
                if(video_register_device(&btv->radio_dev, VFL_TYPE_RADIO, radio_nr)<0) {
2609
                        video_unregister_device(&btv->vbi_dev);
2610
                        video_unregister_device(&btv->video_dev);
2611
                        return -1;
2612
                }
2613
                printk(KERN_INFO "bttv%d: registered device radio%d\n",
2614
                       btv->nr,btv->radio_dev.minor & 0x1f);
2615
        }
2616
        return 1;
2617
}
2618
 
2619
static int __devinit init_bt848(struct bttv *btv)
2620
{
2621
        int val;
2622
        unsigned int j;
2623
        unsigned long irq_flags;
2624
 
2625
        btv->user=0;
2626
        init_MUTEX(&btv->lock);
2627
 
2628
        /* dump current state of the gpio registers before changing them,
2629
         * might help to make a new card work */
2630
        if (bttv_gpio) {
2631
                bttv_gpio_tracking(btv,"init #1");
2632
                bttv_gpio_tracking(btv,"init #1");
2633
        }
2634
 
2635
        /* reset the bt848 */
2636
        btwrite(0, BT848_SRESET);
2637
 
2638
        /* not registered yet */
2639
        btv->video_dev.minor = -1;
2640
        btv->radio_dev.minor = -1;
2641
        btv->vbi_dev.minor = -1;
2642
 
2643
        /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
2644
        btv->win.norm=0; /* change this to 1 for NTSC, 2 for SECAM */
2645
        btv->win.interlace=1;
2646
        btv->win.x=0;
2647
        btv->win.y=0;
2648
        btv->win.width=320;
2649
        btv->win.height=240;
2650
        btv->win.bpp=2;
2651
        btv->win.depth=16;
2652
        btv->win.color_fmt=BT848_COLOR_FMT_RGB16;
2653
        btv->win.bpl=1024*btv->win.bpp;
2654
        btv->win.swidth=1024;
2655
        btv->win.sheight=768;
2656
        btv->win.vidadr=0;
2657
        btv->vbi_on=0;
2658
        btv->scr_on=0;
2659
 
2660
        btv->risc_scr_odd=0;
2661
        btv->risc_scr_even=0;
2662
        btv->risc_cap_odd=0;
2663
        btv->risc_cap_even=0;
2664
        btv->risc_jmp=0;
2665
        btv->vbibuf=0;
2666
        btv->field=btv->last_field=0;
2667
 
2668
        btv->errors=0;
2669
        btv->needs_restart=0;
2670
        btv->has_radio=radio[btv->nr];
2671
 
2672
        if (!(btv->risc_scr_odd=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
2673
                return -1;
2674
        if (!(btv->risc_scr_even=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
2675
                return -1;
2676
        if (!(btv->risc_jmp =(unsigned int *) kmalloc(2048, GFP_KERNEL)))
2677
                return -1;
2678
        btv->vbi_odd=btv->risc_jmp+16;
2679
        btv->vbi_even=btv->vbi_odd+256;
2680
        btv->bus_vbi_odd=virt_to_bus(btv->risc_jmp+12);
2681
        btv->bus_vbi_even=virt_to_bus(btv->risc_jmp+6);
2682
 
2683
        btwrite(virt_to_bus(btv->risc_jmp+2), BT848_RISC_STRT_ADD);
2684
        btv->vbibuf=(unsigned char *) vmalloc_32(VBIBUF_SIZE);
2685
        if (!btv->vbibuf)
2686
                return -1;
2687
        if (!(btv->gbuf = kmalloc(sizeof(struct bttv_gbuf)*gbuffers,GFP_KERNEL)))
2688
                return -1;
2689
        for (j = 0; j < gbuffers; j++) {
2690
                if (!(btv->gbuf[j].risc = kmalloc(16384,GFP_KERNEL)))
2691
                        return -1;
2692
        }
2693
 
2694
        memset(btv->vbibuf, 0, VBIBUF_SIZE); /* We don't want to return random
2695
                                                memory to the user */
2696
 
2697
        btv->fbuffer=NULL;
2698
 
2699
/*      btwrite(0, BT848_TDEC); */
2700
        btwrite(0x10, BT848_COLOR_CTL);
2701
        btwrite(0x00, BT848_CAP_CTL);
2702
        /* set planar and packed mode trigger points and         */
2703
        /* set rising edge of inverted GPINTR pin as irq trigger */
2704
        btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
2705
                BT848_GPIO_DMA_CTL_PLTP1_16|
2706
                BT848_GPIO_DMA_CTL_PLTP23_16|
2707
                BT848_GPIO_DMA_CTL_GPINTC|
2708
                BT848_GPIO_DMA_CTL_GPINTI,
2709
                BT848_GPIO_DMA_CTL);
2710
 
2711
        /* select direct input */
2712
        btwrite(0x00, BT848_GPIO_REG_INP);
2713
        btwrite(0x00, BT848_GPIO_OUT_EN);
2714
        if (bttv_gpio)
2715
                bttv_gpio_tracking(btv,"init #2");
2716
 
2717
        btwrite(BT848_IFORM_MUX1 | BT848_IFORM_XTAUTO | BT848_IFORM_AUTO,
2718
                BT848_IFORM);
2719
 
2720
        btwrite(0xd8, BT848_CONTRAST_LO);
2721
        bt848_bright(btv, 0x10);
2722
 
2723
        btwrite(0x20, BT848_E_VSCALE_HI);
2724
        btwrite(0x20, BT848_O_VSCALE_HI);
2725
        btwrite(BT848_ADC_RESERVED | (adc_crush ? BT848_ADC_CRUSH : 0),
2726
                BT848_ADC);
2727
 
2728
        if (lumafilter) {
2729
                btwrite(0, BT848_E_CONTROL);
2730
                btwrite(0, BT848_O_CONTROL);
2731
        } else {
2732
                btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
2733
                btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
2734
        }
2735
 
2736
        btv->picture.colour     = 254<<7;
2737
        btv->picture.brightness = 128<<8;
2738
        btv->picture.hue        = 128<<8;
2739
        btv->picture.contrast   = 0xd8<<7;
2740
        btv->picture.palette    = VIDEO_PALETTE_RGB24;
2741
 
2742
        val = chroma_agc ? BT848_SCLOOP_CAGC : 0;
2743
        btwrite(val, BT848_E_SCLOOP);
2744
        btwrite(val, BT848_O_SCLOOP);
2745
 
2746
        /* clear interrupt status */
2747
        btwrite(0xfffffUL, BT848_INT_STAT);
2748
 
2749
        /* set interrupt mask */
2750
        btwrite(btv->triton1|
2751
                /*BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|
2752
                  BT848_INT_FDSR|BT848_INT_FTRGT|BT848_INT_FBUS|*/
2753
                (fieldnr ? BT848_INT_VSYNC : 0) |
2754
                (gpint   ? BT848_INT_GPINT : 0) |
2755
                BT848_INT_SCERR|
2756
                BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
2757
                BT848_INT_FMTCHG|BT848_INT_HLOCK,
2758
                BT848_INT_MASK);
2759
 
2760
        make_vbitab(btv);
2761
        spin_lock_irqsave(&btv->s_lock, irq_flags);
2762
        bt848_set_risc_jmps(btv,-1);
2763
        spin_unlock_irqrestore(&btv->s_lock, irq_flags);
2764
 
2765
        /* needs to be done before i2c is registered */
2766
        bttv_init_card1(btv);
2767
 
2768
        /* register i2c */
2769
        btv->tuner_type  = UNSET;
2770
        btv->pinnacle_id = UNSET;
2771
        init_bttv_i2c(btv);
2772
 
2773
        /* some card-specific stuff (needs working i2c) */
2774
        bttv_init_card2(btv);
2775
 
2776
        bt848_muxsel(btv, 1);
2777
        bt848_set_winsize(btv);
2778
 
2779
        /*
2780
         *      Now add the template and register the device unit.
2781
         */
2782
        init_video_dev(btv);
2783
 
2784
        return 0;
2785
}
2786
 
2787
/* ----------------------------------------------------------------------- */
2788
 
2789
static char *irq_name[] = { "FMTCHG", "VSYNC", "HSYNC", "OFLOW", "HLOCK",
2790
                            "VPRES", "6", "7", "I2CDONE", "GPINT", "10",
2791
                            "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
2792
                            "RIPERR", "PABORT", "OCERR", "SCERR" };
2793
 
2794
static void bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
2795
{
2796
        u32 stat,astat;
2797
        u32 dstat;
2798
        int count;
2799
        struct bttv *btv;
2800
 
2801
        btv=(struct bttv *)dev_id;
2802
        count=0;
2803
        while (1)
2804
        {
2805
                /* get/clear interrupt status bits */
2806
                stat=btread(BT848_INT_STAT);
2807
                astat=stat&btread(BT848_INT_MASK);
2808
                if (!astat)
2809
                        return;
2810
                btwrite(stat,BT848_INT_STAT);
2811
 
2812
                /* get device status bits */
2813
                dstat=btread(BT848_DSTATUS);
2814
 
2815
                if (irq_debug) {
2816
                        unsigned int i;
2817
                        printk(KERN_DEBUG "bttv%d: irq loop=%d risc=%x, bits:",
2818
                               btv->nr, count, stat>>28);
2819
                        for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
2820
                                if (stat & (1 << i))
2821
                                        printk(" %s",irq_name[i]);
2822
                                if (astat & (1 << i))
2823
                                        printk("*");
2824
                        }
2825
                        if (stat & BT848_INT_HLOCK)
2826
                                printk("   HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
2827
                                       ? "yes" : "no");
2828
                        if (stat & BT848_INT_VPRES)
2829
                                printk("   PRES => %s", (dstat & BT848_DSTATUS_PRES)
2830
                                       ? "yes" : "no");
2831
                        if (stat & BT848_INT_FMTCHG)
2832
                                printk("   NUML => %s", (dstat & BT848_DSTATUS_PRES)
2833
                                       ? "625" : "525");
2834
                        printk("\n");
2835
                }
2836
 
2837
                if (astat&BT848_INT_GPINT)
2838
                        wake_up_interruptible(&btv->gpioq);
2839
 
2840
                if (astat&BT848_INT_VSYNC)
2841
                        btv->field++;
2842
 
2843
                if (astat&(BT848_INT_SCERR|BT848_INT_OCERR)) {
2844
                        if (bttv_verbose)
2845
                                printk("bttv%d: irq:%s%s risc_count=%08x\n",
2846
                                       btv->nr,
2847
                                       (astat&BT848_INT_SCERR) ? " SCERR" : "",
2848
                                       (astat&BT848_INT_OCERR) ? " OCERR" : "",
2849
                                       btread(BT848_RISC_COUNT));
2850
                        btv->errors++;
2851
                        if (btv->errors < BTTV_ERRORS) {
2852
                                spin_lock(&btv->s_lock);
2853
                                btand(~15, BT848_GPIO_DMA_CTL);
2854
                                btwrite(virt_to_bus(btv->risc_jmp+2),
2855
                                        BT848_RISC_STRT_ADD);
2856
                                bt848_set_geo(btv);
2857
                                bt848_set_risc_jmps(btv,-1);
2858
                                spin_unlock(&btv->s_lock);
2859
                        } else {
2860
                                if (bttv_verbose)
2861
                                        printk("bttv%d: aiee: error loops\n",btv->nr);
2862
                                bt848_offline(btv);
2863
                        }
2864
                }
2865
                if (astat&BT848_INT_RISCI)
2866
                {
2867
                        if (bttv_debug > 1)
2868
                                printk("bttv%d: IRQ_RISCI\n",btv->nr);
2869
 
2870
                        /* captured VBI frame */
2871
                        if (stat&(1<<28))
2872
                        {
2873
                                btv->vbip=0;
2874
                                /* inc vbi frame count for detecting drops */
2875
                                (*(u32 *)&(btv->vbibuf[VBIBUF_SIZE - 4]))++;
2876
                                wake_up_interruptible(&btv->vbiq);
2877
                        }
2878
 
2879
                        /* captured full frame */
2880
                        if (stat&(2<<28) && btv->gq_grab != -1)
2881
                        {
2882
                                btv->last_field=btv->field;
2883
                                if (bttv_debug)
2884
                                        printk("bttv%d: cap irq: done %d\n",btv->nr,btv->gq_grab);
2885
                                do_gettimeofday(&btv->gbuf[btv->gq_grab].tv);
2886
                                spin_lock(&btv->s_lock);
2887
                                btv->gbuf[btv->gq_grab].stat = GBUFFER_DONE;
2888
                                btv->gq_grab = -1;
2889
                                if (btv->gq_in != btv->gq_out)
2890
                                {
2891
                                        btv->gq_grab = btv->gqueue[btv->gq_out++];
2892
                                        btv->gq_out  = btv->gq_out % MAX_GBUFFERS;
2893
                                        if (bttv_debug)
2894
                                                printk("bttv%d: cap irq: capture %d\n",btv->nr,btv->gq_grab);
2895
                                        btv->risc_cap_odd  = btv->gbuf[btv->gq_grab].ro;
2896
                                        btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
2897
                                        bt848_set_risc_jmps(btv,-1);
2898
                                        bt848_set_geo(btv);
2899
                                        btwrite(BT848_COLOR_CTL_GAMMA,
2900
                                                BT848_COLOR_CTL);
2901
                                } else {
2902
                                        btv->risc_cap_odd  = 0;
2903
                                        btv->risc_cap_even = 0;
2904
                                        bt848_set_risc_jmps(btv,-1);
2905
                                        bt848_set_geo(btv);
2906
                                        btwrite(btv->fb_color_ctl | BT848_COLOR_CTL_GAMMA,
2907
                                                BT848_COLOR_CTL);
2908
                                }
2909
                                spin_unlock(&btv->s_lock);
2910
                                wake_up_interruptible(&btv->capq);
2911
                                break;
2912
                        }
2913
                        if (stat&(8<<28) && btv->gq_start)
2914
                        {
2915
                                spin_lock(&btv->s_lock);
2916
                                btv->gq_start = 0;
2917
                                btv->gq_grab = btv->gqueue[btv->gq_out++];
2918
                                btv->gq_out  = btv->gq_out % MAX_GBUFFERS;
2919
                                if (bttv_debug)
2920
                                        printk("bttv%d: cap irq: capture %d [start]\n",btv->nr,btv->gq_grab);
2921
                                btv->risc_cap_odd  = btv->gbuf[btv->gq_grab].ro;
2922
                                btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
2923
                                bt848_set_risc_jmps(btv,-1);
2924
                                bt848_set_geo(btv);
2925
                                btwrite(BT848_COLOR_CTL_GAMMA,
2926
                                        BT848_COLOR_CTL);
2927
                                spin_unlock(&btv->s_lock);
2928
                        }
2929
                }
2930
 
2931
                if (automute && (astat&BT848_INT_HLOCK)) {
2932
                        if ((dstat&BT848_DSTATUS_HLOC) || (btv->radio))
2933
                                audio(btv, AUDIO_ON);
2934
                        else
2935
                                audio(btv, AUDIO_OFF);
2936
                }
2937
 
2938
                count++;
2939
                if (count > 20) {
2940
                        btwrite(0, BT848_INT_MASK);
2941
                        printk(KERN_ERR
2942
                               "bttv%d: IRQ lockup, cleared int mask\n", btv->nr);
2943
                        bt848_offline(btv);
2944
                }
2945
        }
2946
}
2947
 
2948
 
2949
 
2950
/*
2951
 *      Scan for a Bt848 card, request the irq and map the io memory
2952
 */
2953
 
2954
static void bttv_remove(struct pci_dev *pci_dev)
2955
{
2956
        u8 command;
2957
        unsigned int j;
2958
        struct bttv *btv = pci_get_drvdata(pci_dev);
2959
 
2960
        if (bttv_verbose)
2961
                printk("bttv%d: unloading\n",btv->nr);
2962
 
2963
        /* unregister i2c_bus */
2964
        if (0 == btv->i2c_rc)
2965
                i2c_bit_del_bus(&btv->i2c_adap);
2966
 
2967
        /* turn off all capturing, DMA and IRQs */
2968
        btand(~15, BT848_GPIO_DMA_CTL);
2969
 
2970
        /* first disable interrupts before unmapping the memory! */
2971
        btwrite(0, BT848_INT_MASK);
2972
        btwrite(~(u32)0,BT848_INT_STAT);
2973
        btwrite(0, BT848_GPIO_OUT_EN);
2974
        if (bttv_gpio)
2975
                bttv_gpio_tracking(btv,"cleanup");
2976
 
2977
        /* disable PCI bus-mastering */
2978
        pci_read_config_byte(btv->dev, PCI_COMMAND, &command);
2979
        command &= ~PCI_COMMAND_MASTER;
2980
        pci_write_config_byte(btv->dev, PCI_COMMAND, command);
2981
 
2982
        /* unmap and free memory */
2983
        for (j = 0; j < gbuffers; j++)
2984
                if (btv->gbuf[j].risc)
2985
                        kfree(btv->gbuf[j].risc);
2986
        if (btv->gbuf)
2987
                kfree((void *) btv->gbuf);
2988
 
2989
        if (btv->risc_scr_odd)
2990
                kfree((void *) btv->risc_scr_odd);
2991
 
2992
        if (btv->risc_scr_even)
2993
                kfree((void *) btv->risc_scr_even);
2994
 
2995
        DEBUG(printk(KERN_DEBUG "free: risc_jmp: 0x%p.\n", btv->risc_jmp));
2996
        if (btv->risc_jmp)
2997
                kfree((void *) btv->risc_jmp);
2998
 
2999
        DEBUG(printk(KERN_DEBUG "bt848_vbibuf: 0x%p.\n", btv->vbibuf));
3000
        if (btv->vbibuf)
3001
                vfree((void *) btv->vbibuf);
3002
 
3003
        free_irq(btv->dev->irq,btv);
3004
        DEBUG(printk(KERN_DEBUG "bt848_mem: 0x%p.\n", btv->bt848_mem));
3005
        if (btv->bt848_mem)
3006
                iounmap(btv->bt848_mem);
3007
 
3008
        if (btv->video_dev.minor!=-1)
3009
                video_unregister_device(&btv->video_dev);
3010
        if (btv->vbi_dev.minor!=-1)
3011
                video_unregister_device(&btv->vbi_dev);
3012
        if (btv->radio_dev.minor != -1)
3013
                video_unregister_device(&btv->radio_dev);
3014
 
3015
        release_mem_region(pci_resource_start(btv->dev,0),
3016
                           pci_resource_len(btv->dev,0));
3017
        /* wake up any waiting processes
3018
           because shutdown flag is set, no new processes (in this queue)
3019
           are expected
3020
        */
3021
        btv->shutdown=1;
3022
        wake_up(&btv->gpioq);
3023
 
3024
        pci_set_drvdata(pci_dev, NULL);
3025
        return;
3026
}
3027
 
3028
 
3029
static int __devinit bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
3030
{
3031
        int result;
3032
        unsigned char lat;
3033
        struct bttv *btv;
3034
#if defined(__powerpc__)
3035
        unsigned int cmd;
3036
#endif
3037
 
3038
        if (bttv_num == BTTV_MAX)
3039
                return -ENOMEM;
3040
        printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3041
 
3042
        btv=&bttvs[bttv_num];
3043
        btv->dev=dev;
3044
        btv->nr = bttv_num;
3045
        btv->bt848_mem=NULL;
3046
        btv->vbibuf=NULL;
3047
        btv->risc_jmp=NULL;
3048
        btv->vbi_odd=NULL;
3049
        btv->vbi_even=NULL;
3050
        init_waitqueue_head(&btv->vbiq);
3051
        init_waitqueue_head(&btv->capq);
3052
        btv->vbip=VBIBUF_SIZE;
3053
        btv->s_lock = SPIN_LOCK_UNLOCKED;
3054
        init_waitqueue_head(&btv->gpioq);
3055
        btv->shutdown=0;
3056
 
3057
        memcpy(&btv->video_dev,&bttv_template, sizeof(bttv_template));
3058
        memcpy(&btv->vbi_dev,&vbi_template, sizeof(vbi_template));
3059
        memcpy(&btv->radio_dev,&radio_template,sizeof(radio_template));
3060
 
3061
        btv->id=dev->device;
3062
        btv->bt848_adr=pci_resource_start(dev,0);
3063
        if (pci_enable_device(dev)) {
3064
                printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3065
                       btv->nr);
3066
                return -EIO;
3067
        }
3068
        if (pci_set_dma_mask(dev, 0xffffffff)) {
3069
                printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3070
                       btv->nr);
3071
                return -EIO;
3072
        }
3073
        if (!request_mem_region(pci_resource_start(dev,0),
3074
                                pci_resource_len(dev,0),
3075
                                "bttv")) {
3076
                return -EBUSY;
3077
        }
3078
        if (btv->id >= 878)
3079
                btv->i2c_command = 0x83;
3080
        else
3081
                btv->i2c_command=(I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
3082
 
3083
        pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3084
        pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3085
        printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %02x:%02x.%x, ",
3086
               bttv_num,btv->id, btv->revision, dev->bus->number,
3087
               PCI_SLOT(dev->devfn),PCI_FUNC(dev->devfn));
3088
        printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3089
               btv->dev->irq, lat, btv->bt848_adr);
3090
 
3091
        bttv_idcard(btv);
3092
 
3093
#if defined(__powerpc__)
3094
        /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3095
        /* response on cards with no firmware is not enabled by OF */
3096
        pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3097
        cmd = (cmd | PCI_COMMAND_MEMORY );
3098
        pci_write_config_dword(dev, PCI_COMMAND, cmd);
3099
#endif
3100
 
3101
#ifdef __sparc__
3102
        btv->bt848_mem=(unsigned char *)btv->bt848_adr;
3103
#else
3104
        btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
3105
#endif
3106
 
3107
        /* clear interrupt mask */
3108
        btwrite(0, BT848_INT_MASK);
3109
 
3110
        result = request_irq(btv->dev->irq, bttv_irq,
3111
                             SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
3112
        if (result==-EINVAL)
3113
        {
3114
                printk(KERN_ERR "bttv%d: Bad irq number or handler\n",
3115
                       bttv_num);
3116
                goto fail1;
3117
        }
3118
        if (result==-EBUSY)
3119
        {
3120
                printk(KERN_ERR "bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num,btv->dev->irq);
3121
                goto fail1;
3122
        }
3123
        if (result < 0)
3124
                goto fail1;
3125
 
3126
        if (0 != bttv_handle_chipset(btv)) {
3127
                result = -1;
3128
                goto fail2;
3129
        }
3130
 
3131
        pci_set_master(dev);
3132
        pci_set_drvdata(dev,btv);
3133
 
3134
        if(init_bt848(btv) < 0) {
3135
                bttv_remove(dev);
3136
                return -EIO;
3137
        }
3138
        bttv_num++;
3139
 
3140
        return 0;
3141
 
3142
 fail2:
3143
        free_irq(btv->dev->irq,btv);
3144
 fail1:
3145
        release_mem_region(pci_resource_start(btv->dev,0),
3146
                           pci_resource_len(btv->dev,0));
3147
        return result;
3148
}
3149
 
3150
static struct pci_device_id bttv_pci_tbl[] = {
3151
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3152
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3153
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3154
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3155
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3156
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3157
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3158
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3159
        {0,}
3160
};
3161
 
3162
MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3163
 
3164
static struct pci_driver bttv_pci_driver = {
3165
        .name     = "bttv",
3166
        .id_table = bttv_pci_tbl,
3167
        .probe    = bttv_probe,
3168
        .remove   = bttv_remove,
3169
};
3170
 
3171
static int bttv_init_module(void)
3172
{
3173
        int rc;
3174
        bttv_num = 0;
3175
 
3176
        printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
3177
               (BTTV_VERSION_CODE >> 16) & 0xff,
3178
               (BTTV_VERSION_CODE >> 8) & 0xff,
3179
               BTTV_VERSION_CODE & 0xff);
3180
        if (gbuffers < 2 || gbuffers > MAX_GBUFFERS)
3181
                gbuffers = 2;
3182
        if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
3183
                gbufsize = BTTV_MAX_FBUF;
3184
        if (bttv_verbose)
3185
                printk(KERN_INFO "bttv: using %d buffers with %dk (%dk total) for capture\n",
3186
                       gbuffers,gbufsize/1024,gbuffers*gbufsize/1024);
3187
 
3188
        bttv_check_chipset();
3189
 
3190
        rc = pci_module_init(&bttv_pci_driver);
3191
        if (-ENODEV == rc) {
3192
                /* plenty of people trying to use bttv for the cx2388x ... */
3193
                if (NULL != pci_find_device(0x14f1, 0x8800, NULL))
3194
                        printk("bttv doesn't support your Conexant 2388x card.\n");
3195
        }
3196
        return rc;
3197
}
3198
 
3199
static void bttv_cleanup_module(void)
3200
{
3201
        pci_unregister_driver(&bttv_pci_driver);
3202
        return;
3203
}
3204
 
3205
module_init(bttv_init_module);
3206
module_exit(bttv_cleanup_module);
3207
 
3208
/*
3209
 * Local variables:
3210
 * c-basic-offset: 8
3211
 * End:
3212
 */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.