2 bttv - Bt848 frame grabber driver
4 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
5 & Marcus Metzler (mocm@thp.uni-koeln.de)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/major.h>
29 #include <linux/malloc.h>
31 #if LINUX_VERSION_CODE >= 0x020100
32 #include <linux/poll.h>
34 #include <linux/pci.h>
35 #include <linux/signal.h>
37 #include <linux/ioport.h>
38 #include <asm/pgtable.h>
40 #include <linux/sched.h>
41 #include <asm/segment.h>
42 #include <linux/types.h>
43 #include <linux/wrapper.h>
44 #include <linux/interrupt.h>
45 #include <linux/version.h>
47 #if LINUX_VERSION_CODE >= 0x020100
48 #include <asm/uaccess.h>
49 #include <linux/vmalloc.h>
51 #include <linux/bios32.h>
52 #define mdelay(x) udelay((x)*1000)
53 #define signal_pending(current) (current->signal & ~current->blocked)
54 #define sigfillset(set)
56 static inline int time_before(unsigned long a
, unsigned long b
)
58 return((long)((a
) - (b
)) < 0L);
61 static inline unsigned long
62 copy_to_user(void *to
, const void *from
, unsigned long n
)
64 memcpy_tofs(to
,from
,n
);
68 static inline unsigned long
69 copy_from_user(void *to
, const void *from
, unsigned long n
)
71 memcpy_fromfs(to
,from
,n
);
74 #define ioremap vremap
78 #include <linux/videodev.h>
79 #include <linux/i2c.h>
83 #define DEBUG(x) /* Debug driver */
84 #define IDEBUG(x) /* Debug interrupt handler */
86 #if LINUX_VERSION_CODE >= 0x020117
87 MODULE_PARM(vidmem
,"i");
88 MODULE_PARM(triton1
,"i");
89 MODULE_PARM(remap
,"1-4i");
90 MODULE_PARM(radio
,"1-4i");
91 MODULE_PARM(card
,"1-4i");
92 MODULE_PARM(pll
,"1-4i");
95 /* Anybody who uses more than four? */
98 static int find_vga(void);
99 static void bt848_set_risc_jmps(struct bttv
*btv
);
101 static unsigned int vidmem
=0; /* manually set video mem address */
102 static int triton1
=0;
104 /* 0=no pll, 1=28MHz, 2=34MHz */
108 /* card type (see bttv.h) 0=autodetect */
109 #define CARD_DEFAULT 0
112 static unsigned int remap
[BTTV_MAX
]; /* remap Bt848 */
113 static unsigned int radio
[BTTV_MAX
];
114 static unsigned int card
[BTTV_MAX
] = { CARD_DEFAULT
, CARD_DEFAULT
,
115 CARD_DEFAULT
, CARD_DEFAULT
};
116 static unsigned int pll
[BTTV_MAX
] = { USE_PLL
, USE_PLL
, USE_PLL
, USE_PLL
};
118 static int bttv_num
; /* number of Bt848s in use */
119 static struct bttv bttvs
[BTTV_MAX
];
121 #define I2C_TIMING (0x7<<4)
123 #define I2C_SET(CTRL,DATA) \
124 { btwrite((CTRL<<1)|(DATA), BT848_I2C); udelay(I2C_DELAY); }
125 #define I2C_GET() (btread(BT848_I2C)&1)
127 #define EEPROM_WRITE_DELAY 20000
128 #define BURSTOFFSET 76
132 /*******************************/
133 /* Memory management functions */
134 /*******************************/
136 /* convert virtual user memory address to physical address */
137 /* (virt_to_phys only works for kmalloced kernel memory) */
139 static inline unsigned long uvirt_to_phys(unsigned long adr
)
145 pgd
= pgd_offset(current
->mm
, adr
);
148 pmd
= pmd_offset(pgd
, adr
);
151 ptep
= pte_offset(pmd
, adr
/*&(~PGDIR_MASK)*/);
155 virt_to_phys((void *)(pte_page(pte
)|(adr
&(PAGE_SIZE
-1))));
159 static inline unsigned long uvirt_to_bus(unsigned long adr
)
161 return virt_to_bus(phys_to_virt(uvirt_to_phys(adr
)));
164 /* convert virtual kernel memory address to physical address */
165 /* (virt_to_phys only works for kmalloced kernel memory) */
167 static inline unsigned long kvirt_to_phys(unsigned long adr
)
169 return uvirt_to_phys(VMALLOC_VMADDR(adr
));
172 static inline unsigned long kvirt_to_bus(unsigned long adr
)
174 return uvirt_to_bus(VMALLOC_VMADDR(adr
));
177 static void * rvmalloc(unsigned long size
)
180 unsigned long adr
, page
;
185 memset(mem
, 0, size
); /* Clear the ram out, no junk to the user */
186 adr
=(unsigned long) mem
;
189 page
= kvirt_to_phys(adr
);
190 mem_map_reserve(MAP_NR(phys_to_virt(page
)));
198 static void rvfree(void * mem
, unsigned long size
)
200 unsigned long adr
, page
;
204 adr
=(unsigned long) mem
;
207 page
= kvirt_to_phys(adr
);
208 mem_map_unreserve(MAP_NR(phys_to_virt(page
)));
219 * Create the giant waste of buffer space we need for now
220 * until we get DMA to user space sorted out (probably 2.3.x)
222 * We only create this as and when someone uses mmap
225 static int fbuffer_alloc(struct bttv
*btv
)
228 btv
->fbuffer
=(unsigned char *) rvmalloc(2*BTTV_MAX_FBUF
);
230 printk(KERN_ERR
"bttv%d: Double alloc of fbuffer!\n",
238 /* ----------------------------------------------------------------------- */
241 /* software I2C functions */
243 static void i2c_setlines(struct i2c_bus
*bus
,int ctrl
,int data
)
245 struct bttv
*btv
= (struct bttv
*)bus
->data
;
246 btwrite((ctrl
<<1)|data
, BT848_I2C
);
250 static int i2c_getdataline(struct i2c_bus
*bus
)
252 struct bttv
*btv
= (struct bttv
*)bus
->data
;
253 return btread(BT848_I2C
)&1;
256 /* hardware I2C functions */
259 static int I2CRead(struct i2c_bus
*bus
, unsigned char addr
)
263 struct bttv
*btv
= (struct bttv
*)bus
->data
;
265 /* clear status bit ; BT848_INT_RACK is ro */
266 btwrite(BT848_INT_I2CDONE
, BT848_INT_STAT
);
268 btwrite(((addr
& 0xff) << 24) | btv
->i2c_command
, BT848_I2C
);
271 * Timeout for I2CRead is 1 second (this should be enough, really!)
275 stat
=btread(BT848_INT_STAT
);
276 if (stat
& BT848_INT_I2CDONE
)
283 printk(KERN_DEBUG
"bttv%d: I2CRead timeout\n",
287 if (!(stat
& BT848_INT_RACK
))
290 i
=(btread(BT848_I2C
)>>8)&0xff;
294 /* set both to write both bytes, reset it to write only b1 */
296 static int I2CWrite(struct i2c_bus
*bus
, unsigned char addr
, unsigned char b1
,
297 unsigned char b2
, int both
)
302 struct bttv
*btv
= (struct bttv
*)bus
->data
;
304 /* clear status bit; BT848_INT_RACK is ro */
305 btwrite(BT848_INT_I2CDONE
, BT848_INT_STAT
);
307 data
=((addr
& 0xff) << 24) | ((b1
& 0xff) << 16) | btv
->i2c_command
;
310 data
|=((b2
& 0xff) << 8);
314 btwrite(data
, BT848_I2C
);
316 for (i
=0x1000; i
; i
--)
318 stat
=btread(BT848_INT_STAT
);
319 if (stat
& BT848_INT_I2CDONE
)
326 printk(KERN_DEBUG
"bttv%d: I2CWrite timeout\n",
330 if (!(stat
& BT848_INT_RACK
))
337 static void readee(struct i2c_bus
*bus
, unsigned char *eedata
)
341 if (I2CWrite(bus
, 0xa0, 0, -1, 0)<0)
343 printk(KERN_WARNING
"bttv: readee error\n");
347 for (i
=0; i
<256; i
++)
349 k
=I2CRead(bus
, 0xa1);
352 printk(KERN_WARNING
"bttv: readee error\n");
360 static void writeee(struct i2c_bus
*bus
, unsigned char *eedata
)
364 for (i
=0; i
<256; i
++)
366 if (I2CWrite(bus
, 0xa0, i
, eedata
[i
], 1)<0)
368 printk(KERN_WARNING
"bttv: writeee error (%d)\n", i
);
371 udelay(EEPROM_WRITE_DELAY
);
375 void attach_inform(struct i2c_bus
*bus
, int id
)
377 struct bttv
*btv
= (struct bttv
*)bus
->data
;
381 case I2C_DRIVERID_MSP3400
:
382 btv
->have_msp3400
= 1;
384 case I2C_DRIVERID_TUNER
:
386 if (btv
->tuner_type
!= -1)
387 i2c_control_device(&(btv
->i2c
),
389 TUNER_SET_TYPE
,&btv
->tuner_type
);
394 void detach_inform(struct i2c_bus
*bus
, int id
)
396 struct bttv
*btv
= (struct bttv
*)bus
->data
;
400 case I2C_DRIVERID_MSP3400
:
401 btv
->have_msp3400
= 0;
403 case I2C_DRIVERID_TUNER
:
409 static struct i2c_bus bttv_i2c_bus_template
=
415 #if LINUX_VERSION_CODE >= 0x020100
428 /* ----------------------------------------------------------------------- */
429 /* some hauppauge specific stuff */
431 static unsigned char eeprom_data
[256];
432 static struct HAUPPAUGE_TUNER
439 { TUNER_ABSENT
, "" },
440 { TUNER_ABSENT
, "External" },
441 { TUNER_ABSENT
, "Unspecified" },
442 { TUNER_ABSENT
, "Philips FI1216" },
443 { TUNER_ABSENT
, "Philips FI1216MF" },
444 { TUNER_PHILIPS_NTSC
, "Philips FI1236" },
445 { TUNER_ABSENT
, "Philips FI1246" },
446 { TUNER_ABSENT
, "Philips FI1256" },
447 { TUNER_PHILIPS_PAL
, "Philips FI1216 MK2" },
448 { TUNER_PHILIPS_SECAM
, "Philips FI1216MF MK2" },
449 { TUNER_PHILIPS_NTSC
, "Philips FI1236 MK2" },
450 { TUNER_PHILIPS_PAL_I
, "Philips FI1246 MK2" },
451 { TUNER_ABSENT
, "Philips FI1256 MK2" },
452 { TUNER_ABSENT
, "Temic 4032FY5" },
453 { TUNER_TEMIC_PAL
, "Temic 4002FH5" },
454 { TUNER_TEMIC_PAL_I
, "Temic 4062FY5" },
455 { TUNER_ABSENT
, "Philips FR1216 MK2" },
456 { TUNER_PHILIPS_SECAM
, "Philips FR1216MF MK2" },
457 { TUNER_PHILIPS_NTSC
, "Philips FR1236 MK2" },
458 { TUNER_PHILIPS_PAL_I
, "Philips FR1246 MK2" },
459 { TUNER_ABSENT
, "Philips FR1256 MK2" },
460 { TUNER_PHILIPS_PAL
, "Philips FM1216" },
461 { TUNER_ABSENT
, "Philips FM1216MF" },
462 { TUNER_PHILIPS_NTSC
, "Philips FM1236" },
466 hauppauge_eeprom(struct i2c_bus
*bus
)
468 struct bttv
*btv
= (struct bttv
*)bus
->data
;
470 readee(bus
, eeprom_data
);
471 if (eeprom_data
[9] < sizeof(hauppauge_tuner
)/sizeof(struct HAUPPAUGE_TUNER
))
473 btv
->tuner_type
= hauppauge_tuner
[eeprom_data
[9]].id
;
474 printk("bttv%d: Hauppauge eeprom: tuner=%s (%d)\n",btv
->nr
,
475 hauppauge_tuner
[eeprom_data
[9]].name
,btv
->tuner_type
);
480 hauppauge_msp_reset(struct bttv
*btv
)
482 /* Reset the MSP on some Hauppauge cards */
483 /* Thanks to Kyösti Mälkki (kmalkki@cc.hut.fi)! */
484 /* Can this hurt cards without one? What about Miros with MSP? */
485 btaor(32, ~32, BT848_GPIO_OUT_EN
);
486 btaor(0, ~32, BT848_GPIO_DATA
);
488 btaor(32, ~32, BT848_GPIO_DATA
);
489 /* btaor(0, ~32, BT848_GPIO_OUT_EN); */
492 /* ----------------------------------------------------------------------- */
503 u32 audiomux
[6]; /* Tuner, Radio, internal, external, mute, stereo */
504 u32 gpiomask2
; /* GPIO MUX mask */
507 static struct tvcard tvcards
[] =
510 { 3, 1, 0, 2, 0, { 2, 3, 1, 1}, { 0, 0, 0, 0, 0}},
512 { 4, 1, 0, 2,15, { 2, 3, 1, 1}, { 2, 0, 0, 0,10}},
514 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
516 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 4, 0, 2, 3, 1}},
518 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
519 /* Diamond DTV2000 */
520 { 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 0, 1, 0, 1, 3}},
521 /* AVerMedia TVPhone */
522 { 3, 1, 0, 3,15, { 2, 3, 1, 1}, {12, 0,11,11, 0}},
523 /* Matrix Vision MV-Delta */
524 { 5, 1, -1, 3, 0, { 2, 3, 1, 0, 0}},
526 { 3, 1, 0, 2, 0xc00, { 2, 3, 1, 1},
527 {0, 0xc00, 0x800, 0x400, 0xc00, 0}},
529 { 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 1, 1, 2, 3, 0}},
530 /* Newer Hauppauge (bt878) */
531 { 3, 1, 0, 2, 7, { 2, 0, 1, 1}, { 0, 1, 2, 3, 4}},
533 { 3, 1, 0, 2, 65551, { 2, 3, 1, 1}, {1,65537, 0, 0,10}},
534 /* ADS Technologies Channel Surfer TV (and maybe TV+FM) */
535 { 3, 4, 0, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0}, 0},
536 /* AVerMedia TVCapture 98 */
537 { 3, 4, 0, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0}, 0},
539 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
541 #define TVCARDS (sizeof(tvcards)/sizeof(tvcard))
543 static void audio(struct bttv
*btv
, int mode
)
545 btaor(tvcards
[btv
->type
].gpiomask
, ~tvcards
[btv
->type
].gpiomask
,
551 btv
->audio
|=AUDIO_MUTE
;
554 btv
->audio
&=~AUDIO_MUTE
;
564 btv
->audio
&=AUDIO_MUTE
;
568 /* if audio mute or not in H-lock, turn audio off */
569 if ((btv
->audio
&AUDIO_MUTE
)
572 (!btv
->radio
&& !(btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
))
576 if ((mode
== 0) && (btv
->radio
))
578 btaor(tvcards
[btv
->type
].audiomux
[mode
],
579 ~tvcards
[btv
->type
].gpiomask
, BT848_GPIO_DATA
);
583 extern inline void bt848_dma(struct bttv
*btv
, uint state
)
586 btor(3, BT848_GPIO_DMA_CTL
);
588 btand(~3, BT848_GPIO_DMA_CTL
);
592 static void bt848_cap(struct bttv
*btv
, uint state
)
597 bt848_set_risc_jmps(btv
);
602 bt848_set_risc_jmps(btv
);
607 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC*/
609 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
610 PLL_X = Reference pre-divider (0=1, 1=2)
611 PLL_C = Post divider (0=6, 1=4)
612 PLL_I = Integer input
613 PLL_F = Fractional input
615 F_input = 28.636363 MHz:
616 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
619 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
621 unsigned char fl
, fh
, fi
;
623 /* prevent overflows */
636 /*printk("0x%02x 0x%02x 0x%02x\n", fi, fh, fl);*/
637 btwrite(fl
, BT848_PLL_F_LO
);
638 btwrite(fh
, BT848_PLL_F_HI
);
639 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
642 static int set_pll(struct bttv
*btv
)
647 if (!btv
->pll
.pll_crystal
)
650 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
652 if (btv
->pll
.pll_current
== 0) {
653 /* printk ("bttv%d: PLL: is off\n",btv->nr); */
656 printk ("bttv%d: PLL: switching off\n",btv
->nr
);
657 btwrite(0x00,BT848_TGCTRL
);
658 btwrite(0x00,BT848_PLL_XCI
);
659 btv
->pll
.pll_current
= 0;
663 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
664 /* printk("bttv%d: PLL: no change required\n",btv->nr); */
668 printk("bttv%d: PLL: %d => %d ... ",btv
->nr
,
669 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
671 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
673 /* Let other people run while the PLL stabilizes */
674 tv
=jiffies
+HZ
/10; /* .1 seconds */
679 while(time_before(jiffies
,tv
));
683 if ((btread(BT848_DSTATUS
)&BT848_DSTATUS_PLOCK
))
684 btwrite(0,BT848_DSTATUS
);
687 btwrite(0x08,BT848_TGCTRL
);
688 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
694 btv
->pll
.pll_current
= 0;
699 static void bt848_muxsel(struct bttv
*btv
, unsigned int input
)
701 btaor(tvcards
[btv
->type
].gpiomask2
,~tvcards
[btv
->type
].gpiomask2
,
704 /* This seems to get rid of some synchronization problems */
705 btand(~(3<<5), BT848_IFORM
);
709 input
%= tvcards
[btv
->type
].video_inputs
;
710 if (input
==tvcards
[btv
->type
].svhs
)
712 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
713 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
717 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
718 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
720 btaor((tvcards
[btv
->type
].muxsel
[input
&7]&3)<<5, ~(3<<5), BT848_IFORM
);
721 audio(btv
, (input
!=tvcards
[btv
->type
].tuner
) ?
722 AUDIO_EXTERN
: AUDIO_TUNER
);
723 btaor(tvcards
[btv
->type
].muxsel
[input
]>>4,
724 ~tvcards
[btv
->type
].gpiomask2
, BT848_GPIO_DATA
);
728 * Set the registers for the size we have specified. Don't bother
729 * trying to understand this without the BT848 manual in front of
732 * PS: The manual is free for download in .pdf format from
733 * www.brooktree.com - nicely done those folks.
739 u16 swidth
, sheight
; /* scaled standard width, height */
741 u8 adelay
, bdelay
, iform
;
743 u16 hdelayx1
, hactivex1
;
748 static struct tvnorm tvnorms
[] = {
750 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
751 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
754 924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
755 1135, 186, 924, 0x20, 255},
758 924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
759 1135, 186, 924, 0x20, 255},
763 768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
764 944, 186, 922, 0x20, 255},
768 768, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
769 910, 128, 910, 0x1a, 144},
772 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC|BT848_IFORM_XT0),
773 780, 122, 754, 0x1a, 144},
778 768, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
779 944, 186, 922, 0x20, 255},
783 924, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
784 1135, 186, 922, 0x20, 255},
788 640, 576, 910, 0x68, 0x5d, (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
789 780, 130, 734, 0x1a, 144},
792 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
793 780, 135, 754, 0x1a, 144},
796 768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
797 944, 186, 922, 0x20, 144},
800 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
801 780, 135, 754, 0x16, 144},
803 #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
806 /* RISC command to write one VBI data line */
807 #define VBI_RISC BT848_RISC_WRITE|VBI_SPL|BT848_RISC_EOL|BT848_RISC_SOL
809 static void make_vbitab(struct bttv
*btv
)
812 unsigned int *po
=(unsigned int *) btv
->vbi_odd
;
813 unsigned int *pe
=(unsigned int *) btv
->vbi_even
;
815 DEBUG(printk(KERN_DEBUG
"vbiodd: 0x%08x\n",(int)btv
->vbi_odd
));
816 DEBUG(printk(KERN_DEBUG
"vbievn: 0x%08x\n",(int)btv
->vbi_even
));
817 DEBUG(printk(KERN_DEBUG
"po: 0x%08x\n",(int)po
));
818 DEBUG(printk(KERN_DEBUG
"pe: 0x%08x\n",(int)pe
));
820 *(po
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(po
++)=0;
824 *(po
++)=kvirt_to_bus((unsigned long)btv
->vbibuf
+i
*2048);
826 *(po
++)=BT848_RISC_JUMP
;
827 *(po
++)=virt_to_bus(btv
->risc_jmp
+4);
829 *(pe
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(pe
++)=0;
830 for (i
=16; i
<32; i
++)
833 *(pe
++)=kvirt_to_bus((unsigned long)btv
->vbibuf
+i
*2048);
835 *(pe
++)=BT848_RISC_JUMP
|BT848_RISC_IRQ
|(0x01<<16);
836 *(pe
++)=virt_to_bus(btv
->risc_jmp
+10);
837 DEBUG(printk(KERN_DEBUG
"po: 0x%08x\n",(int)po
));
838 DEBUG(printk(KERN_DEBUG
"pe: 0x%08x\n",(int)pe
));
842 8, 6, 4, 4, 4, 3, 2, 2, 4, 3, 0, 0, 0, 0, 2, 0
845 int palette2fmt
[] = {
848 BT848_COLOR_FMT_RGB8
,
849 BT848_COLOR_FMT_RGB16
,
850 BT848_COLOR_FMT_RGB24
,
851 BT848_COLOR_FMT_RGB32
,
852 BT848_COLOR_FMT_RGB15
,
853 BT848_COLOR_FMT_YUY2
,
854 BT848_COLOR_FMT_BtYUV
,
859 BT848_COLOR_FMT_YCrCb422
,
860 BT848_COLOR_FMT_YCrCb411
,
861 BT848_COLOR_FMT_YCrCb422
,
862 BT848_COLOR_FMT_YCrCb411
,
864 #define PALETTEFMT_MAX (sizeof(palette2fmt)/sizeof(int))
866 static int make_rawrisctab(struct bttv
*btv
, unsigned int *ro
,
867 unsigned int *re
, unsigned int *vbuf
)
870 unsigned long bpl
=1024; /* bytes per line */
871 unsigned long vadr
=(unsigned long) vbuf
;
873 *(ro
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(ro
++)=0;
874 *(re
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(re
++)=0;
876 /* In PAL 650 blocks of 256 DWORDs are sampled, but only if VDELAY
877 is 2 and without separate VBI grabbing.
878 We'll have to handle this inside the IRQ handler ... */
880 for (line
=0; line
< 640; line
++)
882 *(ro
++)=BT848_RISC_WRITE
|bpl
|BT848_RISC_SOL
|BT848_RISC_EOL
;
883 *(ro
++)=kvirt_to_bus(vadr
);
884 *(re
++)=BT848_RISC_WRITE
|bpl
|BT848_RISC_SOL
|BT848_RISC_EOL
;
885 *(re
++)=kvirt_to_bus(vadr
+BTTV_MAX_FBUF
/2);
889 *(ro
++)=BT848_RISC_JUMP
;
890 *(ro
++)=btv
->bus_vbi_even
;
891 *(re
++)=BT848_RISC_JUMP
|BT848_RISC_IRQ
|(2<<16);
892 *(re
++)=btv
->bus_vbi_odd
;
898 static int make_prisctab(struct bttv
*btv
, unsigned int *ro
,
900 unsigned int *vbuf
, unsigned short width
,
901 unsigned short height
, unsigned short fmt
)
903 unsigned long line
, lmask
;
904 unsigned long bl
, blcr
, blcb
, rcmd
;
908 unsigned long cbadr
, cradr
;
909 unsigned long vadr
=(unsigned long) vbuf
;
915 case VIDEO_PALETTE_YUV422P
:
916 csize
=(width
*height
)>>1;
921 case VIDEO_PALETTE_YUV411P
:
922 csize
=(width
*height
)>>2;
927 case VIDEO_PALETTE_YUV420P
:
928 csize
=(width
*height
)>>2;
933 case VIDEO_PALETTE_YUV410P
:
934 csize
=(width
*height
)>>4;
942 cbadr
=vadr
+(width
*height
);
944 inter
= (height
>btv
->win
.cropheight
/2) ? 1 : 0;
946 *(ro
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM3
; *(ro
++)=0;
947 *(re
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM3
; *(re
++)=0;
949 for (line
=0; line
< (height
<<(1^inter
)); line
++)
954 cbadr
=vadr
+(width
*height
);
958 rp
= (line
&1) ? &re
: &ro
;
960 rp
= (line
>=height
) ? &re
: &ro
;
964 rcmd
=BT848_RISC_WRITE1S23
|BT848_RISC_SOL
;
966 rcmd
=BT848_RISC_WRITE123
|BT848_RISC_SOL
;
971 bl
=PAGE_SIZE
-((PAGE_SIZE
-1)&vadr
);
972 blcr
=(PAGE_SIZE
-((PAGE_SIZE
-1)&cradr
))<<shift
;
973 blcb
=(PAGE_SIZE
-((PAGE_SIZE
-1)&cbadr
))<<shift
;
974 bl
=(blcr
<bl
) ? blcr
: bl
;
975 bl
=(blcb
<bl
) ? blcb
: bl
;
976 bl
=(bl
>todo
) ? todo
: bl
;
979 /* bl now containts the longest row that can be written */
981 if(!todo
) rcmd
|=BT848_RISC_EOL
; /* if this is the last EOL */
984 *((*rp
)++)=blcb
|(blcr
<<16);
985 *((*rp
)++)=kvirt_to_bus(vadr
);
987 if((rcmd
&(15<<28))==BT848_RISC_WRITE123
)
989 *((*rp
)++)=kvirt_to_bus(cbadr
);
991 *((*rp
)++)=kvirt_to_bus(cradr
);
995 rcmd
&=~BT848_RISC_SOL
; /* only the first has SOL */
999 *(ro
++)=BT848_RISC_JUMP
;
1000 *(ro
++)=btv
->bus_vbi_even
;
1001 *(re
++)=BT848_RISC_JUMP
|BT848_RISC_IRQ
|(2<<16);
1002 *(re
++)=btv
->bus_vbi_odd
;
1007 static int make_vrisctab(struct bttv
*btv
, unsigned int *ro
,
1009 unsigned int *vbuf
, unsigned short width
,
1010 unsigned short height
, unsigned short palette
)
1013 unsigned long bpl
; /* bytes per line */
1018 unsigned long vadr
=(unsigned long) vbuf
;
1020 if (palette
==VIDEO_PALETTE_RAW
)
1021 return make_rawrisctab(btv
, ro
, re
, vbuf
);
1022 if (palette
>=VIDEO_PALETTE_PLANAR
)
1023 return make_prisctab(btv
, ro
, re
, vbuf
, width
, height
, palette
);
1026 inter
= (height
>btv
->win
.cropheight
/2) ? 1 : 0;
1027 bpl
=width
*fmtbppx2
[palette2fmt
[palette
]&0xf]/2;
1029 *(ro
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(ro
++)=0;
1030 *(re
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(re
++)=0;
1032 for (line
=0; line
< (height
<<(1^inter
)); line
++)
1035 rp
= (line
&1) ? &re
: &ro
;
1037 rp
= (line
>=height
) ? &re
: &ro
;
1039 bl
=PAGE_SIZE
-((PAGE_SIZE
-1)&vadr
);
1042 *((*rp
)++)=BT848_RISC_WRITE
|BT848_RISC_SOL
|
1044 *((*rp
)++)=kvirt_to_bus(vadr
);
1050 *((*rp
)++)=BT848_RISC_WRITE
|BT848_RISC_SOL
|bl
;
1051 *((*rp
)++)=kvirt_to_bus(vadr
);
1054 while (todo
>PAGE_SIZE
)
1056 *((*rp
)++)=BT848_RISC_WRITE
|PAGE_SIZE
;
1057 *((*rp
)++)=kvirt_to_bus(vadr
);
1061 *((*rp
)++)=BT848_RISC_WRITE
|BT848_RISC_EOL
|todo
;
1062 *((*rp
)++)=kvirt_to_bus(vadr
);
1067 *(ro
++)=BT848_RISC_JUMP
;
1068 *(ro
++)=btv
->bus_vbi_even
;
1069 *(re
++)=BT848_RISC_JUMP
|BT848_RISC_IRQ
|(2<<16);
1070 *(re
++)=btv
->bus_vbi_odd
;
1075 static unsigned char lmaskt
[8] =
1076 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
1077 static unsigned char rmaskt
[8] =
1078 { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1080 static void clip_draw_rectangle(unsigned char *clipmap
, int x
, int y
, int w
, int h
)
1082 unsigned char lmask
, rmask
, *p
;
1085 /* bitmap is fixed width, 128 bytes (1024 pixels represented) */
1096 if (w
< 0 || h
< 0) /* catch bad clips */
1098 /* out of range data should just fall through */
1108 rmask
=rmaskt
[(x
+w
)&7];
1113 for (i
=0; i
<h
; i
++, p
+=128)
1116 memset(p
+1, 0xff, W
);
1120 for (i
=0; i
<h
; i
++, p
+=128)
1126 for (i
=0; i
<h
; i
++, p
+=128)
1133 static void make_clip_tab(struct bttv
*btv
, struct video_clip
*cr
, int ncr
)
1135 int i
, line
, x
, y
, bpl
, width
, height
, inter
;
1136 unsigned int bpp
, dx
, sx
, **rp
, *ro
, *re
, flags
, len
;
1138 unsigned char *clipmap
, cbit
, lastbit
, outofmem
;
1140 inter
=(btv
->win
.interlace
&1)^1;
1142 if (bpp
==15) /* handle 15bpp as 16bpp in calculations */
1147 if((width
=btv
->win
.width
)>1023)
1148 width
= 1023; /* sanity check */
1149 if((height
=btv
->win
.height
)>625)
1150 height
= 625; /* sanity check */
1151 adr
=btv
->win
.vidadr
+btv
->win
.x
*bpp
+btv
->win
.y
*bpl
;
1152 if ((clipmap
=vmalloc(VIDEO_CLIPMAP_SIZE
))==NULL
) {
1153 /* can't clip, don't generate any risc code */
1154 *(ro
++)=BT848_RISC_JUMP
;
1155 *(ro
++)=btv
->bus_vbi_even
;
1156 *(re
++)=BT848_RISC_JUMP
;
1157 *(re
++)=btv
->bus_vbi_odd
;
1159 if (ncr
< 0) { /* bitmap was pased */
1160 memcpy(clipmap
, (unsigned char *)cr
, VIDEO_CLIPMAP_SIZE
);
1161 } else { /* convert rectangular clips to a bitmap */
1162 memset(clipmap
, 0, VIDEO_CLIPMAP_SIZE
); /* clear map */
1163 for (i
=0; i
<ncr
; i
++)
1164 clip_draw_rectangle(clipmap
, cr
[i
].x
, cr
[i
].y
,
1165 cr
[i
].width
, cr
[i
].height
);
1167 /* clip against viewing window AND screen
1168 so we do not have to rely on the user program
1170 clip_draw_rectangle(clipmap
,(btv
->win
.x
+width
>btv
->win
.swidth
) ?
1171 (btv
->win
.swidth
-btv
->win
.x
) : width
, 0, 1024, 768);
1172 clip_draw_rectangle(clipmap
,0,(btv
->win
.y
+height
>btv
->win
.sheight
) ?
1173 (btv
->win
.sheight
-btv
->win
.y
) : height
,1024,768);
1175 clip_draw_rectangle(clipmap
, 0, 0, -(btv
->win
.x
), 768);
1177 clip_draw_rectangle(clipmap
, 0, 0, 1024, -(btv
->win
.y
));
1179 *(ro
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(ro
++)=0;
1180 *(re
++)=BT848_RISC_SYNC
|BT848_FIFO_STATUS_FM1
; *(re
++)=0;
1182 /* translate bitmap to risc code */
1183 for (line
=outofmem
=0; line
< (height
<<inter
) && !outofmem
; line
++)
1186 rp
= (line
&1) ? &re
: &ro
;
1187 lastbit
=(clipmap
[y
<<7]&1);
1188 for(x
=dx
=1,sx
=0; x
<=width
&& !outofmem
; x
++) {
1189 cbit
= (clipmap
[(y
<<7)+(x
>>3)] & (1<<(x
&7)));
1190 if (x
< width
&& !lastbit
== !cbit
)
1192 else { /* generate the dma controller code */
1194 flags
= ((bpp
==4) ? BT848_RISC_BYTE3
: 0);
1195 flags
|= ((!sx
) ? BT848_RISC_SOL
: 0);
1196 flags
|= ((sx
+ dx
== width
) ? BT848_RISC_EOL
: 0);
1198 *((*rp
)++)=BT848_RISC_WRITE
|flags
|len
;
1199 *((*rp
)++)=adr
+ bpp
* sx
;
1201 *((*rp
)++)=BT848_RISC_SKIP
|flags
|len
;
1205 if (ro
- btv
->risc_odd
> RISCMEM_LEN
/2 - 16)
1207 if (re
- btv
->risc_even
> RISCMEM_LEN
/2 - 16)
1211 if ((!inter
)||(line
&1))
1215 /* outofmem flag relies on the following code to discard extra data */
1216 *(ro
++)=BT848_RISC_JUMP
;
1217 *(ro
++)=btv
->bus_vbi_even
;
1218 *(re
++)=BT848_RISC_JUMP
;
1219 *(re
++)=btv
->bus_vbi_odd
;
1222 /* set geometry for even/odd frames
1223 just if you are wondering:
1224 handling of even and odd frames will be separated, e.g. for grabbing
1225 the even ones as RGB into videomem and the others as YUV in main memory for
1226 compressing and sending to the video conferencing partner.
1229 static inline void bt848_set_eogeo(struct bttv
*btv
, int odd
, u8 vtc
,
1230 u16 hscale
, u16 vscale
,
1231 u16 hactive
, u16 vactive
,
1232 u16 hdelay
, u16 vdelay
,
1235 int off
= odd
? 0x80 : 0x00;
1237 btwrite(vtc
, BT848_E_VTC
+off
);
1238 btwrite(hscale
>>8, BT848_E_HSCALE_HI
+off
);
1239 btwrite(hscale
&0xff, BT848_E_HSCALE_LO
+off
);
1240 btaor((vscale
>>8), 0xe0, BT848_E_VSCALE_HI
+off
);
1241 btwrite(vscale
&0xff, BT848_E_VSCALE_LO
+off
);
1242 btwrite(hactive
&0xff, BT848_E_HACTIVE_LO
+off
);
1243 btwrite(hdelay
&0xff, BT848_E_HDELAY_LO
+off
);
1244 btwrite(vactive
&0xff, BT848_E_VACTIVE_LO
+off
);
1245 btwrite(vdelay
&0xff, BT848_E_VDELAY_LO
+off
);
1246 btwrite(crop
, BT848_E_CROP
+off
);
1250 static void bt848_set_geo(struct bttv
*btv
, u16 width
, u16 height
, u16 fmt
)
1255 u16 hactive
, vactive
;
1260 if (!width
|| !height
)
1263 tvn
=&tvnorms
[btv
->win
.norm
];
1265 btv
->win
.cropheight
=tvn
->sheight
;
1266 btv
->win
.cropwidth
=tvn
->swidth
;
1269 if (btv->win.cropwidth>tvn->cropwidth)
1270 btv->win.cropwidth=tvn->cropwidth;
1271 if (btv->win.cropheight>tvn->cropheight)
1272 btv->win.cropheight=tvn->cropheight;
1274 if (width>btv->win.cropwidth)
1275 width=btv->win.cropwidth;
1276 if (height>btv->win.cropheight)
1277 height=btv->win.cropheight;
1279 btwrite(tvn
->adelay
, BT848_ADELAY
);
1280 btwrite(tvn
->bdelay
, BT848_BDELAY
);
1281 btaor(tvn
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
), BT848_IFORM
);
1282 btwrite(tvn
->vbipack
, BT848_VBI_PACK_SIZE
);
1283 btwrite(1, BT848_VBI_PACK_DEL
);
1285 btv
->pll
.pll_ofreq
= tvn
->Fsc
;
1288 btwrite(fmt
, BT848_COLOR_FMT
);
1292 /* Some people say interpolation looks bad ... */
1293 /* vtc = (hactive < 193) ? 2 : ((hactive < 385) ? 1 : 0); */
1295 btv
->win
.interlace
= (height
>btv
->win
.cropheight
/2) ? 1 : 0;
1296 inter
=(btv
->win
.interlace
&1)^1;
1297 vdelay
=btv
->win
.cropy
+tvn
->vdelay
;
1299 xsf
= (hactive
*tvn
->scaledtwidth
)/btv
->win
.cropwidth
;
1300 hscale
= ((tvn
->totalwidth
*4096UL)/xsf
-4096);
1302 hdelay
=tvn
->hdelayx1
+btv
->win
.cropx
;
1303 hdelay
=(hdelay
*hactive
)/btv
->win
.cropwidth
;
1306 sr
=((btv
->win
.cropheight
>>inter
)*512)/height
-512;
1307 vscale
=(0x10000UL
-sr
)&0x1fff;
1308 vactive
=btv
->win
.cropheight
;
1309 crop
=((hactive
>>8)&0x03)|((hdelay
>>6)&0x0c)|
1310 ((vactive
>>4)&0x30)|((vdelay
>>2)&0xc0);
1311 vscale
|= btv
->win
.interlace
? (BT848_VSCALE_INT
<<8) : 0;
1313 bt848_set_eogeo(btv
, 0, vtc
, hscale
, vscale
, hactive
, vactive
,
1314 hdelay
, vdelay
, crop
);
1315 bt848_set_eogeo(btv
, 1, vtc
, hscale
, vscale
, hactive
, vactive
,
1316 hdelay
, vdelay
, crop
);
1321 BT848_COLOR_FMT_RGB8
, BT848_COLOR_FMT_RGB16
,
1322 BT848_COLOR_FMT_RGB24
, BT848_COLOR_FMT_RGB32
1325 static void bt848_set_winsize(struct bttv
*btv
)
1327 unsigned short format
;
1329 btv
->win
.color_fmt
= format
=
1330 (btv
->win
.depth
==15) ? BT848_COLOR_FMT_RGB15
:
1331 bpp2fmt
[(btv
->win
.bpp
-1)&3];
1333 /* RGB8 seems to be a 9x5x5 GRB color cube starting at
1334 * color 16. Why the h... can't they even mention this in the
1335 * data sheet? [AC - because it's a standard format so I guess
1336 * it never occurred to them]
1337 * Enable dithering in this mode.
1340 if (format
==BT848_COLOR_FMT_RGB8
)
1341 btand(~BT848_CAP_CTL_DITH_FRAME
, BT848_CAP_CTL
);
1343 btor(BT848_CAP_CTL_DITH_FRAME
, BT848_CAP_CTL
);
1345 bt848_set_geo(btv
, btv
->win
.width
, btv
->win
.height
, format
);
1349 * Set TSA5522 synthesizer frequency in 1/16 Mhz steps
1352 static void set_freq(struct bttv
*btv
, unsigned short freq
)
1354 int fixme
= freq
; /* XXX */
1357 if (btv
->have_msp3400
)
1358 i2c_control_device(&(btv
->i2c
),I2C_DRIVERID_MSP3400
,
1361 /* switch channel */
1362 if (btv
->have_tuner
) {
1364 i2c_control_device(&(btv
->i2c
), I2C_DRIVERID_TUNER
,
1365 TUNER_SET_RADIOFREQ
,&fixme
);
1367 i2c_control_device(&(btv
->i2c
), I2C_DRIVERID_TUNER
,
1368 TUNER_SET_TVFREQ
,&fixme
);
1372 if (btv
->have_msp3400
) {
1374 i2c_control_device(&(btv
->i2c
),I2C_DRIVERID_MSP3400
,
1377 i2c_control_device(&(btv
->i2c
),I2C_DRIVERID_MSP3400
,
1378 MSP_SET_TVNORM
,&(btv
->win
.norm
));
1379 i2c_control_device(&(btv
->i2c
),I2C_DRIVERID_MSP3400
,
1386 * Grab into virtual memory.
1387 * Currently only does double buffering. Do we need more?
1390 static int vgrab(struct bttv
*btv
, struct video_mmap
*mp
)
1392 unsigned int *ro
, *re
;
1395 if(btv
->fbuffer
==NULL
)
1397 if(fbuffer_alloc(btv
))
1400 if(btv
->grabbing
>= MAX_GBUFFERS
)
1404 * No grabbing past the end of the buffer!
1407 if(mp
->frame
>1 || mp
->frame
<0)
1410 if(mp
->height
<0 || mp
->width
<0)
1413 /* This doesn´t work like this for NTSC anyway.
1414 So, better check the total image size ...
1417 if(mp->height>576 || mp->width>768+BURSTOFFSET)
1420 if (mp
->format
>= PALETTEFMT_MAX
)
1422 if (mp
->height
*mp
->width
*fmtbppx2
[palette2fmt
[mp
->format
]&0x0f]/2
1425 if(-1 == palette2fmt
[mp
->format
])
1429 * FIXME: Check the format of the grab here. This is probably
1430 * also less restrictive than the normal overlay grabs. Stuff
1431 * like QCIF has meaning as a capture.
1435 * Ok load up the BT848
1438 vbuf
=(unsigned int *)(btv
->fbuffer
+BTTV_MAX_FBUF
*mp
->frame
);
1439 /* if (!(btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC))
1441 ro
=btv
->grisc
+(((btv
->grabcount
++)&1) ? 4096 :0);
1443 make_vrisctab(btv
, ro
, re
, vbuf
, mp
->width
, mp
->height
, mp
->format
);
1444 /* bt848_set_risc_jmps(btv); */
1445 btv
->frame_stat
[mp
->frame
] = GBUFFER_GRABBING
;
1446 if (btv
->grabbing
) {
1447 btv
->gfmt_next
=palette2fmt
[mp
->format
];
1448 btv
->gwidth_next
=mp
->width
;
1449 btv
->gheight_next
=mp
->height
;
1450 btv
->gro_next
=virt_to_bus(ro
);
1451 btv
->gre_next
=virt_to_bus(re
);
1452 btv
->grf_next
=mp
->frame
;
1454 btv
->gfmt
=palette2fmt
[mp
->format
];
1455 btv
->gwidth
=mp
->width
;
1456 btv
->gheight
=mp
->height
;
1457 btv
->gro
=virt_to_bus(ro
);
1458 btv
->gre
=virt_to_bus(re
);
1461 if (!(btv
->grabbing
++)) {
1462 if(mp
->format
>=VIDEO_PALETTE_COMPONENT
) {
1463 btor(BT848_VSCALE_COMB
, BT848_E_VSCALE_HI
);
1464 btor(BT848_VSCALE_COMB
, BT848_O_VSCALE_HI
);
1466 btv
->risc_jmp
[12]=BT848_RISC_JUMP
|(0x8<<16)|BT848_RISC_IRQ
;
1468 btor(3, BT848_CAP_CTL
);
1469 btor(3, BT848_GPIO_DMA_CTL
);
1470 /* interruptible_sleep_on(&btv->capq); */
1474 static long bttv_write(struct video_device
*v
, const char *buf
, unsigned long count
, int nonblock
)
1479 static long bttv_read(struct video_device
*v
, char *buf
, unsigned long count
, int nonblock
)
1481 struct bttv
*btv
= (struct bttv
*)v
;
1483 /* BROKEN: RETURNS VBI WHEN IT SHOULD RETURN GRABBED VIDEO FRAME */
1485 while (todo
&& todo
>(q
=VBIBUF_SIZE
-btv
->vbip
))
1487 if(copy_to_user((void *) buf
, (void *) btv
->vbibuf
+btv
->vbip
, q
))
1493 if (todo
&& q
==VBIBUF_SIZE
-btv
->vbip
)
1499 return -EWOULDBLOCK
;
1502 interruptible_sleep_on(&btv
->vbiq
);
1504 if(signal_pending(current
))
1515 if(copy_to_user((void *) buf
, (void *) btv
->vbibuf
+btv
->vbip
, todo
))
1523 * Open a bttv card. Right now the flags stuff is just playing
1526 static int bttv_open(struct video_device
*dev
, int flags
)
1528 struct bttv
*btv
= (struct bttv
*)dev
;
1533 audio(btv
, AUDIO_UNMUTE
);
1534 for (i
=users
=0; i
<bttv_num
; i
++)
1535 users
+=bttvs
[i
].user
;
1540 btv
->fbuffer
=(unsigned char *) rvmalloc(2*BTTV_MAX_FBUF
);
1546 for (i
= 0; i
< MAX_GBUFFERS
; i
++)
1547 btv
->frame_stat
[i
] = GBUFFER_UNUSED
;
1554 static void bttv_close(struct video_device
*dev
)
1556 struct bttv
*btv
=(struct bttv
*)dev
;
1559 audio(btv
, AUDIO_INTERN
);
1561 bt848_set_risc_jmps(btv
);
1564 rvfree((void *) btv
->fbuffer
, 2*BTTV_MAX_FBUF
);
1570 /***********************************/
1571 /* ioctls and supporting functions */
1572 /***********************************/
1574 extern inline void bt848_bright(struct bttv
*btv
, uint bright
)
1576 btwrite(bright
&0xff, BT848_BRIGHT
);
1579 extern inline void bt848_hue(struct bttv
*btv
, uint hue
)
1581 btwrite(hue
&0xff, BT848_HUE
);
1584 extern inline void bt848_contrast(struct bttv
*btv
, uint cont
)
1586 unsigned int conthi
;
1589 btwrite(cont
&0xff, BT848_CONTRAST_LO
);
1590 btaor(conthi
, ~4, BT848_E_CONTROL
);
1591 btaor(conthi
, ~4, BT848_O_CONTROL
);
1594 extern inline void bt848_sat_u(struct bttv
*btv
, unsigned long data
)
1599 btwrite(data
&0xff, BT848_SAT_U_LO
);
1600 btaor(datahi
, ~2, BT848_E_CONTROL
);
1601 btaor(datahi
, ~2, BT848_O_CONTROL
);
1604 static inline void bt848_sat_v(struct bttv
*btv
, unsigned long data
)
1609 btwrite(data
&0xff, BT848_SAT_V_LO
);
1610 btaor(datahi
, ~1, BT848_E_CONTROL
);
1611 btaor(datahi
, ~1, BT848_O_CONTROL
);
1619 static int bttv_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
1621 unsigned char eedata
[256];
1622 struct bttv
*btv
=(struct bttv
*)dev
;
1629 struct video_capability b
;
1630 strcpy(b
.name
,btv
->video_dev
.name
);
1631 b
.type
= VID_TYPE_CAPTURE
|
1638 b
.channels
= tvcards
[btv
->type
].video_inputs
;
1639 b
.audios
= tvcards
[btv
->type
].audio_inputs
;
1640 b
.maxwidth
= tvnorms
[btv
->win
.norm
].swidth
;
1641 b
.maxheight
= tvnorms
[btv
->win
.norm
].sheight
;
1644 if(copy_to_user(arg
,&b
,sizeof(b
)))
1650 struct video_channel v
;
1651 if(copy_from_user(&v
, arg
,sizeof(v
)))
1653 v
.flags
=VIDEO_VC_AUDIO
;
1655 v
.type
=VIDEO_TYPE_CAMERA
;
1656 v
.norm
= btv
->win
.norm
;
1657 if (v
.channel
>=tvcards
[btv
->type
].video_inputs
)
1659 if(v
.channel
==tvcards
[btv
->type
].tuner
)
1661 strcpy(v
.name
,"Television");
1662 v
.flags
|=VIDEO_VC_TUNER
;
1663 v
.type
=VIDEO_TYPE_TV
;
1666 else if(v
.channel
==tvcards
[btv
->type
].svhs
)
1667 strcpy(v
.name
,"S-Video");
1669 sprintf(v
.name
,"Composite%d",v
.channel
);
1671 if(copy_to_user(arg
,&v
,sizeof(v
)))
1676 * Each channel has 1 tuner
1680 struct video_channel v
;
1681 if(copy_from_user(&v
, arg
,sizeof(v
)))
1684 if (v
.channel
>tvcards
[btv
->type
].video_inputs
)
1686 bt848_muxsel(btv
, v
.channel
);
1687 if(v
.norm
!=VIDEO_MODE_PAL
&&v
.norm
!=VIDEO_MODE_NTSC
1688 &&v
.norm
!=VIDEO_MODE_SECAM
)
1690 btv
->win
.norm
= v
.norm
;
1692 bt848_set_winsize(btv
);
1693 btv
->channel
=v
.channel
;
1698 struct video_tuner v
;
1699 if(copy_from_user(&v
,arg
,sizeof(v
))!=0)
1701 if(v
.tuner
||btv
->channel
) /* Only tuner 0 */
1703 strcpy(v
.name
, "Television");
1705 v
.rangehigh
=0xFFFFFFFF;
1706 v
.flags
=VIDEO_TUNER_PAL
|VIDEO_TUNER_NTSC
|VIDEO_TUNER_SECAM
;
1707 if (btv
->audio_chip
== TDA9840
) {
1708 v
.flags
|= VIDEO_AUDIO_VOLUME
;
1709 v
.mode
= VIDEO_SOUND_MONO
|VIDEO_SOUND_STEREO
;
1710 v
.mode
|= VIDEO_SOUND_LANG1
|VIDEO_SOUND_LANG2
;
1712 if (btv
->audio_chip
== TDA9850
) {
1714 ALR1
= I2CRead(&(btv
->i2c
), I2C_TDA9850
|1);
1716 v
.flags
|= VIDEO_TUNER_STEREO_ON
;
1718 v
.mode
= btv
->win
.norm
;
1719 v
.signal
= (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
) ? 0xFFFF : 0;
1720 if(copy_to_user(arg
,&v
,sizeof(v
)))
1724 /* We have but one tuner */
1727 struct video_tuner v
;
1728 if(copy_from_user(&v
, arg
, sizeof(v
)))
1730 /* Only one channel has a tuner */
1731 if(v
.tuner
!=tvcards
[btv
->type
].tuner
)
1734 if(v
.mode
!=VIDEO_MODE_PAL
&&v
.mode
!=VIDEO_MODE_NTSC
1735 &&v
.mode
!=VIDEO_MODE_SECAM
)
1737 btv
->win
.norm
= v
.mode
;
1738 bt848_set_winsize(btv
);
1743 struct video_picture p
=btv
->picture
;
1744 if(btv
->win
.depth
==8)
1745 p
.palette
=VIDEO_PALETTE_HI240
;
1746 if(btv
->win
.depth
==15)
1747 p
.palette
=VIDEO_PALETTE_RGB555
;
1748 if(btv
->win
.depth
==16)
1749 p
.palette
=VIDEO_PALETTE_RGB565
;
1750 if(btv
->win
.depth
==24)
1751 p
.palette
=VIDEO_PALETTE_RGB24
;
1752 if(btv
->win
.depth
==32)
1753 p
.palette
=VIDEO_PALETTE_RGB32
;
1755 if(copy_to_user(arg
, &p
, sizeof(p
)))
1761 struct video_picture p
;
1763 if(copy_from_user(&p
, arg
,sizeof(p
)))
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);
1771 bt848_hue(btv
, (p
.hue
>>8)-128);
1773 bt848_contrast(btv
, p
.contrast
>>7);
1776 /* set palette if bpp matches */
1777 if (p
.palette
< sizeof(palette2fmt
)/sizeof(int)) {
1778 format
= palette2fmt
[p
.palette
];
1779 if (fmtbppx2
[format
&0x0f]/2 == btv
->win
.bpp
)
1780 btv
->win
.color_fmt
= format
;
1786 struct video_window vw
;
1787 struct video_clip
*vcp
= NULL
;
1790 if(copy_from_user(&vw
,arg
,sizeof(vw
)))
1793 if(vw
.flags
|| vw
.width
< 16 || vw
.height
< 16)
1798 if (btv
->win
.bpp
< 4)
1799 { /* 32-bit align start and adjust width */
1801 vw
.x
= (vw
.x
+ 3) & ~3;
1807 btv
->win
.width
=vw
.width
;
1808 btv
->win
.height
=vw
.height
;
1810 if(btv
->win
.height
>btv
->win
.cropheight
/2)
1811 btv
->win
.interlace
=1;
1813 btv
->win
.interlace
=0;
1818 bt848_set_winsize(btv
);
1823 if(vw
.clipcount
<0) {
1824 if((vcp
=vmalloc(VIDEO_CLIPMAP_SIZE
))==NULL
)
1826 if(copy_from_user(vcp
, vw
.clips
,
1827 VIDEO_CLIPMAP_SIZE
)) {
1831 } else if (vw
.clipcount
) {
1832 if((vcp
=vmalloc(sizeof(struct video_clip
)*
1833 (vw
.clipcount
))) == NULL
)
1835 if(copy_from_user(vcp
,vw
.clips
,
1836 sizeof(struct video_clip
)*
1842 make_clip_tab(btv
, vcp
, vw
.clipcount
);
1843 if (vw
.clipcount
!= 0)
1845 if(on
&& btv
->win
.vidadr
!=0)
1851 struct video_window vw
;
1852 /* Oh for a COBOL move corresponding .. */
1855 vw
.width
=btv
->win
.width
;
1856 vw
.height
=btv
->win
.height
;
1859 if(btv
->win
.interlace
)
1860 vw
.flags
|=VIDEO_WINDOW_INTERLACE
;
1861 if(copy_to_user(arg
,&vw
,sizeof(vw
)))
1868 if(copy_from_user(&v
, arg
,sizeof(v
)))
1876 if(btv
->win
.vidadr
==0 || btv
->win
.width
==0
1877 || btv
->win
.height
==0)
1885 struct video_buffer v
;
1886 v
.base
=(void *)btv
->win
.vidadr
;
1887 v
.height
=btv
->win
.sheight
;
1888 v
.width
=btv
->win
.swidth
;
1889 v
.depth
=btv
->win
.depth
;
1890 v
.bytesperline
=btv
->win
.bpl
;
1891 if(copy_to_user(arg
, &v
,sizeof(v
)))
1898 struct video_buffer v
;
1899 #if LINUX_VERSION_CODE >= 0x020100
1900 if(!capable(CAP_SYS_ADMIN
))
1905 if(copy_from_user(&v
, arg
,sizeof(v
)))
1907 if(v
.depth
!=8 && v
.depth
!=15 && v
.depth
!=16 &&
1908 v
.depth
!=24 && v
.depth
!=32 && v
.width
> 16 &&
1909 v
.height
> 16 && v
.bytesperline
> 16)
1913 if ((unsigned long)v
.base
&1)
1914 btv
->win
.vidadr
=(unsigned long)(PAGE_OFFSET
|uvirt_to_bus((unsigned long)v
.base
));
1916 btv
->win
.vidadr
=(unsigned long)v
.base
;
1918 btv
->win
.sheight
=v
.height
;
1919 btv
->win
.swidth
=v
.width
;
1920 btv
->win
.bpp
=((v
.depth
+7)&0x38)/8;
1921 btv
->win
.depth
=v
.depth
;
1922 btv
->win
.bpl
=v
.bytesperline
;
1924 DEBUG(printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1925 v
.base
, v
.width
,v
.height
, btv
->win
.bpp
, btv
->win
.bpl
));
1926 bt848_set_winsize(btv
);
1931 /* Will be handled higher up .. */
1936 unsigned long v
=btv
->win
.freq
;
1937 if(copy_to_user(arg
,&v
,sizeof(v
)))
1944 if(copy_from_user(&v
, arg
, sizeof(v
)))
1947 set_freq(btv
, btv
->win
.freq
);
1953 struct video_audio v
;
1955 v
.flags
&=~(VIDEO_AUDIO_MUTE
|VIDEO_AUDIO_MUTABLE
);
1956 v
.flags
|=VIDEO_AUDIO_MUTABLE
;
1957 strcpy(v
.name
,"TV");
1958 if (btv
->audio_chip
== TDA9850
) {
1960 ALR1
= I2CRead(&(btv
->i2c
), I2C_TDA9850
|1);
1961 v
.mode
= VIDEO_SOUND_MONO
;
1962 v
.mode
|= (ALR1
& 32) ? VIDEO_SOUND_STEREO
:0;
1963 v
.mode
|= (ALR1
& 64) ? VIDEO_SOUND_LANG1
:0;
1965 if (btv
->have_msp3400
)
1967 v
.flags
|=VIDEO_AUDIO_VOLUME
|
1970 i2c_control_device(&(btv
->i2c
),
1971 I2C_DRIVERID_MSP3400
,
1972 MSP_GET_VOLUME
,&(v
.volume
));
1973 i2c_control_device(&(btv
->i2c
),
1974 I2C_DRIVERID_MSP3400
,
1975 MSP_GET_BASS
,&(v
.bass
));
1976 i2c_control_device(&(btv
->i2c
),
1977 I2C_DRIVERID_MSP3400
,
1978 MSP_GET_TREBLE
,&(v
.treble
));
1979 i2c_control_device(&(btv
->i2c
),
1980 I2C_DRIVERID_MSP3400
,
1981 MSP_GET_STEREO
,&(v
.mode
));
1983 else v
.mode
= VIDEO_SOUND_MONO
;
1984 if(copy_to_user(arg
,&v
,sizeof(v
)))
1990 struct video_audio v
;
1991 if(copy_from_user(&v
,arg
, sizeof(v
)))
1993 if(v
.flags
&VIDEO_AUDIO_MUTE
)
1994 audio(btv
, AUDIO_MUTE
);
1995 /* One audio source per tuner */
1996 /* if(v.audio!=0) */
1997 /* ADSTech TV card has more than one */
1998 if(v
.audio
<0 || v
.audio
>= tvcards
[btv
->type
].audio_inputs
)
2000 bt848_muxsel(btv
,v
.audio
);
2001 if(!(v
.flags
&VIDEO_AUDIO_MUTE
))
2002 audio(btv
, AUDIO_UNMUTE
);
2003 if (btv
->audio_chip
== TDA9850
) {
2004 unsigned char con3
= 0;
2005 if (v
.mode
& VIDEO_SOUND_LANG1
)
2006 con3
= 0x80; /* sap */
2007 if (v
.mode
& VIDEO_SOUND_STEREO
)
2008 con3
= 0x40; /* stereo */
2009 I2CWrite(&(btv
->i2c
), I2C_TDA9850
,
2010 TDA9850_CON3
, con3
, 1);
2012 if (btv
->have_msp3400
)
2014 i2c_control_device(&(btv
->i2c
),
2015 I2C_DRIVERID_MSP3400
,
2016 MSP_SET_VOLUME
,&(v
.volume
));
2017 i2c_control_device(&(btv
->i2c
),
2018 I2C_DRIVERID_MSP3400
,
2019 MSP_SET_BASS
,&(v
.bass
));
2020 i2c_control_device(&(btv
->i2c
),
2021 I2C_DRIVERID_MSP3400
,
2022 MSP_SET_TREBLE
,&(v
.treble
));
2023 i2c_control_device(&(btv
->i2c
),
2024 I2C_DRIVERID_MSP3400
,
2025 MSP_SET_STEREO
,&(v
.mode
));
2032 if(copy_from_user((void *)&i
,arg
,sizeof(int)))
2037 switch (btv
->frame_stat
[i
]) {
2038 case GBUFFER_UNUSED
:
2040 case GBUFFER_GRABBING
:
2041 while(btv
->frame_stat
[i
]==GBUFFER_GRABBING
) {
2042 interruptible_sleep_on(&btv
->capq
);
2043 if(signal_pending(current
))
2048 btv
->frame_stat
[i
] = GBUFFER_UNUSED
;
2054 #if LINUX_VERSION_CODE >= 0x020100
2055 if(!capable(CAP_SYS_ADMIN
))
2060 if(copy_from_user((void *) eedata
, (void *) arg
, 256))
2062 writeee(&(btv
->i2c
), eedata
);
2066 #if LINUX_VERSION_CODE >= 0x020100
2067 if(!capable(CAP_SYS_ADMIN
))
2072 readee(&(btv
->i2c
), eedata
);
2073 if(copy_to_user((void *) arg
, (void *) eedata
, 256))
2078 if(copy_to_user((void *) arg
, (void *) &btv
->last_field
,
2079 sizeof(btv
->last_field
)))
2084 struct bttv_pll_info p
;
2085 #if LINUX_VERSION_CODE >= 0x020100
2086 if(!capable(CAP_SYS_ADMIN
))
2091 if(copy_from_user(&p
, (void *) arg
, sizeof(btv
->pll
)))
2093 btv
->pll
.pll_ifreq
= p
.pll_ifreq
;
2094 btv
->pll
.pll_ofreq
= p
.pll_ofreq
;
2095 btv
->pll
.pll_crystal
= p
.pll_crystal
;
2099 case VIDIOCMCAPTURE
:
2101 struct video_mmap vm
;
2102 if(copy_from_user((void *) &vm
, (void *) arg
, sizeof(vm
)))
2104 if (btv
->frame_stat
[vm
.frame
] == GBUFFER_GRABBING
)
2106 return vgrab(btv
, &vm
);
2111 struct video_mbuf vm
;
2112 memset(&vm
, 0 , sizeof(vm
));
2113 vm
.size
=BTTV_MAX_FBUF
*2;
2116 vm
.offsets
[1]=BTTV_MAX_FBUF
;
2117 if(copy_to_user((void *)arg
, (void *)&vm
, sizeof(vm
)))
2124 struct video_unit vu
;
2125 vu
.video
=btv
->video_dev
.minor
;
2126 vu
.vbi
=btv
->vbi_dev
.minor
;
2127 if(btv
->radio_dev
.minor
!=-1)
2128 vu
.radio
=btv
->radio_dev
.minor
;
2130 vu
.radio
=VIDEO_NO_UNIT
;
2131 vu
.audio
=VIDEO_NO_UNIT
;
2132 if(btv
->have_msp3400
)
2134 i2c_control_device(&(btv
->i2c
), I2C_DRIVERID_MSP3400
,
2135 MSP_GET_UNIT
, &vu
.audio
);
2137 vu
.teletext
=VIDEO_NO_UNIT
;
2138 if(copy_to_user((void *)arg
, (void *)&vu
, sizeof(vu
)))
2145 tvnorms
[0].scaledtwidth
=1135-BURSTOFFSET
-2;
2146 tvnorms
[0].hdelayx1
=186-BURSTOFFSET
;
2150 case BTTV_BURST_OFF
:
2152 tvnorms
[0].scaledtwidth
=1135;
2153 tvnorms
[0].hdelayx1
=186;
2159 return BTTV_VERSION_CODE
;
2164 /* return picture;*/
2169 return -ENOIOCTLCMD
;
2174 static int bttv_init_done(struct video_device
*dev
)
2180 * This maps the vmalloced and reserved fbuffer to user space.
2183 * - PAGE_READONLY should suffice!?
2184 * - remap_page_range is kind of inefficient for page by page remapping.
2185 * But e.g. pte_alloc() does not work in modules ... :-(
2188 static int bttv_mmap(struct video_device
*dev
, const char *adr
, unsigned long size
)
2190 struct bttv
*btv
=(struct bttv
*)dev
;
2191 unsigned long start
=(unsigned long) adr
;
2192 unsigned long page
,pos
;
2194 if (size
>2*BTTV_MAX_FBUF
)
2198 if(fbuffer_alloc(btv
))
2201 pos
=(unsigned long) btv
->fbuffer
;
2204 page
= kvirt_to_phys(pos
);
2205 if (remap_page_range(start
, page
, PAGE_SIZE
, PAGE_SHARED
))
2214 static struct video_device bttv_template
=
2217 VID_TYPE_TUNER
|VID_TYPE_CAPTURE
|VID_TYPE_OVERLAY
|VID_TYPE_TELETEXT
,
2223 #if LINUX_VERSION_CODE >= 0x020100
2235 static long vbi_read(struct video_device
*v
, char *buf
, unsigned long count
,
2238 struct bttv
*btv
=(struct bttv
*)(v
-2);
2242 while (todo
&& todo
>(q
=VBIBUF_SIZE
-btv
->vbip
))
2244 if(copy_to_user((void *) buf
, (void *) btv
->vbibuf
+btv
->vbip
, q
))
2250 if (todo
&& q
==VBIBUF_SIZE
-btv
->vbip
)
2256 return -EWOULDBLOCK
;
2259 interruptible_sleep_on(&btv
->vbiq
);
2261 if(signal_pending(current
))
2272 if(copy_to_user((void *) buf
, (void *) btv
->vbibuf
+btv
->vbip
, todo
))
2279 #if LINUX_VERSION_CODE >= 0x020100
2280 static unsigned int vbi_poll(struct video_device
*dev
, struct file
*file
,
2283 struct bttv
*btv
=(struct bttv
*)(dev
-2);
2284 unsigned int mask
= 0;
2286 poll_wait(file
, &btv
->vbiq
, wait
);
2288 if (btv
->vbip
< VBIBUF_SIZE
)
2289 mask
|= (POLLIN
| POLLRDNORM
);
2295 static int vbi_open(struct video_device
*dev
, int flags
)
2297 struct bttv
*btv
=(struct bttv
*)(dev
-2);
2299 btv
->vbip
=VBIBUF_SIZE
;
2301 bt848_set_risc_jmps(btv
);
2307 static void vbi_close(struct video_device
*dev
)
2309 struct bttv
*btv
=(struct bttv
*)(dev
-2);
2312 bt848_set_risc_jmps(btv
);
2318 static int vbi_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
2323 static struct video_device vbi_template
=
2326 VID_TYPE_CAPTURE
|VID_TYPE_TELETEXT
,
2332 #if LINUX_VERSION_CODE >= 0x020100
2336 NULL
, /* no mmap yet */
2344 static int radio_open(struct video_device
*dev
, int flags
)
2346 struct bttv
*btv
= (struct bttv
*)(dev
-1);
2351 set_freq(btv
,400*16);
2353 bt848_muxsel(btv
,0);
2354 audio(btv
, AUDIO_UNMUTE
);
2360 static void radio_close(struct video_device
*dev
)
2362 struct bttv
*btv
=(struct bttv
*)(dev
-1);
2366 audio(btv
, AUDIO_MUTE
);
2370 static long radio_read(struct video_device
*v
, char *buf
, unsigned long count
, int nonblock
)
2375 static int radio_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
2377 struct bttv
*btv
=(struct bttv
*)(dev
-1);
2381 struct video_capability v
;
2382 strcpy(v
.name
,btv
->video_dev
.name
);
2383 v
.type
= VID_TYPE_TUNER
;
2386 /* No we don't do pictures */
2391 if (copy_to_user(arg
, &v
, sizeof(v
)))
2398 struct video_tuner v
;
2399 if(copy_from_user(&v
,arg
,sizeof(v
))!=0)
2401 if(v
.tuner
||btv
->channel
) /* Only tuner 0 */
2403 strcpy(v
.name
, "Radio");
2404 v
.rangelow
=(int)(87.5*16);
2405 v
.rangehigh
=(int)(108*16);
2406 v
.flags
= 0; /* XXX */
2407 v
.mode
= 0; /* XXX */
2408 if(copy_to_user(arg
,&v
,sizeof(v
)))
2414 struct video_tuner v
;
2415 if(copy_from_user(&v
, arg
, sizeof(v
)))
2417 /* Only channel 0 has a tuner */
2418 if(v
.tuner
!=0 || btv
->channel
)
2420 /* XXX anything to do ??? */
2427 bttv_ioctl((struct video_device
*)btv
,cmd
,arg
);
2430 return -ENOIOCTLCMD
;
2435 static struct video_device radio_template
=
2442 radio_read
, /* just returns -EINVAL */
2443 bttv_write
, /* just returns -EINVAL */
2444 #if LINUX_VERSION_CODE >= 0x020100
2449 bttv_init_done
, /* just returns 0 */
2458 unsigned short vendor
, device
;
2463 static struct vidbases vbs
[] = {
2464 { PCI_VENDOR_ID_ALLIANCE
, PCI_DEVICE_ID_ALLIANCE_AT3D
,
2465 "Alliance AT3D", PCI_BASE_ADDRESS_0
},
2466 { PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_215CT222
,
2467 "ATI MACH64 CT", PCI_BASE_ADDRESS_0
},
2468 { PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_210888GX
,
2469 "ATI MACH64 Winturbo", PCI_BASE_ADDRESS_0
},
2470 { PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_215GT
,
2471 "ATI MACH64 GT", PCI_BASE_ADDRESS_0
},
2472 { PCI_VENDOR_ID_CIRRUS
, 0, "Cirrus Logic", PCI_BASE_ADDRESS_0
},
2473 { PCI_VENDOR_ID_DEC
, PCI_DEVICE_ID_DEC_TGA
,
2474 "DEC DC21030", PCI_BASE_ADDRESS_0
},
2475 { PCI_VENDOR_ID_MATROX
, PCI_DEVICE_ID_MATROX_MIL
,
2476 "Matrox Millennium", PCI_BASE_ADDRESS_1
},
2477 { PCI_VENDOR_ID_MATROX
, PCI_DEVICE_ID_MATROX_MIL_2
,
2478 "Matrox Millennium II", PCI_BASE_ADDRESS_0
},
2479 { PCI_VENDOR_ID_MATROX
, PCI_DEVICE_ID_MATROX_MIL_2_AGP
,
2480 "Matrox Millennium II AGP", PCI_BASE_ADDRESS_0
},
2481 { PCI_VENDOR_ID_MATROX
, 0x051a, "Matrox Mystique", PCI_BASE_ADDRESS_1
},
2482 { PCI_VENDOR_ID_MATROX
, 0x0521, "Matrox G200", PCI_BASE_ADDRESS_0
},
2483 { PCI_VENDOR_ID_N9
, PCI_DEVICE_ID_N9_I128
,
2484 "Number Nine Imagine 128", PCI_BASE_ADDRESS_0
},
2485 { PCI_VENDOR_ID_N9
, PCI_DEVICE_ID_N9_I128_2
,
2486 "Number Nine Imagine 128 Series 2", PCI_BASE_ADDRESS_0
},
2487 { PCI_VENDOR_ID_S3
, 0, "S3", PCI_BASE_ADDRESS_0
},
2488 { PCI_VENDOR_ID_TSENG
, 0, "TSENG", PCI_BASE_ADDRESS_0
},
2489 { PCI_VENDOR_ID_NVIDIA_SGS
, PCI_DEVICE_ID_NVIDIA_SGS_RIVA128
,
2490 "Riva128", PCI_BASE_ADDRESS_1
},
2494 /* DEC TGA offsets stolen from XFree-3.2 */
2496 static uint dec_offsets
[4] = {
2503 #define NR_CARDS (sizeof(vbs)/sizeof(struct vidbases))
2505 /* Scan for PCI display adapter
2506 if more than one card is present the last one is used for now */
2508 #if LINUX_VERSION_CODE >= 0x020100
2510 static int find_vga(void)
2512 unsigned short badr
;
2513 int found
= 0, i
, tga_type
;
2514 unsigned int vidadr
=0;
2515 struct pci_dev
*dev
;
2518 for (dev
= pci_devices
; dev
!= NULL
; dev
= dev
->next
)
2520 if (dev
->class != PCI_CLASS_NOT_DEFINED_VGA
&&
2521 ((dev
->class) >> 16 != PCI_BASE_CLASS_DISPLAY
))
2525 if (PCI_FUNC(dev
->devfn
) != 0)
2529 printk(KERN_INFO
"bttv: PCI display adapter: ");
2530 for (i
=0; i
<NR_CARDS
; i
++)
2532 if (dev
->vendor
== vbs
[i
].vendor
)
2535 if (vbs
[i
].device
!=dev
->device
)
2537 printk("%s.\n", vbs
[i
].name
);
2544 printk(KERN_ERR
"bttv: Unknown video memory base address.\n");
2547 pci_read_config_dword(dev
, badr
, &vidadr
);
2548 if (vidadr
& PCI_BASE_ADDRESS_SPACE_IO
)
2550 printk(KERN_ERR
"bttv: Memory seems to be I/O memory.\n");
2551 printk(KERN_ERR
"bttv: Check entry for your card type in bttv.c vidbases struct.\n");
2554 vidadr
&= PCI_BASE_ADDRESS_MEM_MASK
;
2557 printk(KERN_ERR
"bttv: Memory @ 0, must be something wrong!");
2561 if (dev
->vendor
== PCI_VENDOR_ID_DEC
&&
2562 dev
->device
== PCI_DEVICE_ID_DEC_TGA
)
2564 tga_type
= (readl((unsigned long)vidadr
) >> 12) & 0x0f;
2565 if (tga_type
!= 0 && tga_type
!= 1 && tga_type
!= 3)
2567 printk(KERN_ERR
"bttv: TGA type (0x%x) unrecognized!\n", tga_type
);
2570 vidadr
+=dec_offsets
[tga_type
];
2572 DEBUG(printk(KERN_DEBUG
"bttv: memory @ 0x%08x, ", vidadr
));
2573 DEBUG(printk(KERN_DEBUG
"devfn: 0x%04x.\n", dev
->devfn
));
2580 printk(KERN_INFO
"bttv: Video memory override: 0x%08x\n", vidadr
);
2583 for (i
=0; i
<BTTV_MAX
; i
++)
2584 bttvs
[i
].win
.vidadr
=vidadr
;
2590 static int find_vga(void)
2592 unsigned int devfn
, class, vendev
;
2593 unsigned short vendor
, device
, badr
;
2594 int found
=0, bus
=0, i
, tga_type
;
2595 unsigned int vidadr
=0;
2598 for (devfn
= 0; devfn
< 0xff; devfn
++)
2600 if (PCI_FUNC(devfn
) != 0)
2602 pcibios_read_config_dword(bus
, devfn
, PCI_VENDOR_ID
, &vendev
);
2603 if (vendev
== 0xffffffff || vendev
== 0x00000000)
2605 pcibios_read_config_word(bus
, devfn
, PCI_VENDOR_ID
, &vendor
);
2606 pcibios_read_config_word(bus
, devfn
, PCI_DEVICE_ID
, &device
);
2607 pcibios_read_config_dword(bus
, devfn
, PCI_CLASS_REVISION
, &class);
2608 class = class >> 16;
2609 /* if (class == PCI_CLASS_DISPLAY_VGA) {*/
2610 if ((class>>8) == PCI_BASE_CLASS_DISPLAY
||
2611 /* Number 9 GXE64Pro needs this */
2612 class == PCI_CLASS_NOT_DEFINED_VGA
)
2615 printk(KERN_INFO
"bttv: PCI display adapter: ");
2616 for (i
=0; i
<NR_CARDS
; i
++)
2618 if (vendor
==vbs
[i
].vendor
)
2621 if (vbs
[i
].device
!=device
)
2623 printk("%s.\n", vbs
[i
].name
);
2629 printk("UNKNOWN.\n");
2632 printk(KERN_ERR
"bttv: Unknown video memory base address.\n");
2635 pcibios_read_config_dword(bus
, devfn
, badr
, &vidadr
);
2636 if (vidadr
& PCI_BASE_ADDRESS_SPACE_IO
)
2638 printk(KERN_ERR
"bttv: Memory seems to be I/O memory.\n");
2639 printk(KERN_ERR
"bttv: Check entry for your card type in bttv.c vidbases struct.\n");
2642 vidadr
&= PCI_BASE_ADDRESS_MEM_MASK
;
2645 printk(KERN_ERR
"bttv: Memory @ 0, must be something wrong!\n");
2649 if (vendor
==PCI_VENDOR_ID_DEC
)
2650 if (device
==PCI_DEVICE_ID_DEC_TGA
)
2652 tga_type
= (readl((unsigned long)vidadr
) >> 12) & 0x0f;
2653 if (tga_type
!= 0 && tga_type
!= 1 && tga_type
!= 3)
2655 printk(KERN_ERR
"bttv: TGA type (0x%x) unrecognized!\n", tga_type
);
2658 vidadr
+=dec_offsets
[tga_type
];
2661 DEBUG(printk(KERN_DEBUG
"bttv: memory @ 0x%08x, ", vidadr
));
2662 DEBUG(printk(KERN_DEBUG
"devfn: 0x%04x.\n", devfn
));
2669 if (vidmem
< 0x1000)
2673 printk(KERN_INFO
"bttv: Video memory override: 0x%08x\n", vidadr
);
2676 for (i
=0; i
<BTTV_MAX
; i
++)
2677 bttvs
[i
].win
.vidadr
=vidadr
;
2684 #define TRITON_PCON 0x50
2685 #define TRITON_BUS_CONCURRENCY (1<<0)
2686 #define TRITON_STREAMING (1<<1)
2687 #define TRITON_WRITE_BURST (1<<2)
2688 #define TRITON_PEER_CONCURRENCY (1<<3)
2691 #if LINUX_VERSION_CODE >= 0x020100
2693 static void handle_chipset(void)
2695 struct pci_dev
*dev
= NULL
;
2697 /* Just in case some nut set this to something dangerous */
2699 triton1
=BT848_INT_ETBF
;
2701 while ((dev
= pci_find_device(PCI_VENDOR_ID_SI
, PCI_DEVICE_ID_SI_496
, dev
)))
2703 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
2704 printk(KERN_WARNING
"BT848 and SIS 85C496 chipset don't always work together.\n");
2709 while ((dev
= pci_find_device(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82441
, dev
)))
2712 pci_read_config_byte(dev
, 0x53, &b
);
2713 DEBUG(printk(KERN_INFO
"bttv: Host bridge: 82441FX Natoma, "));
2714 DEBUG(printk("bufcon=0x%02x\n",b
));
2717 while ((dev
= pci_find_device(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82437
, dev
)))
2722 printk(KERN_INFO
"bttv: Host bridge 82437FX Triton PIIX\n");
2723 triton1
=BT848_INT_ETBF
;
2726 /* The ETBF bit SHOULD make all this unnecessary */
2727 /* 430FX (Triton I) freezes with bus concurrency on -> switch it off */
2729 pci_read_config_byte(dev
, TRITON_PCON
, &b
);
2731 DEBUG(printk(KERN_DEBUG
"bttv: 82437FX: PCON: 0x%x\n",b
));
2732 if(!(b
& TRITON_BUS_CONCURRENCY
))
2734 printk(KERN_WARNING
"bttv: 82437FX: disabling bus concurrency\n");
2735 b
|= TRITON_BUS_CONCURRENCY
;
2737 if(b
& TRITON_PEER_CONCURRENCY
)
2739 printk(KERN_WARNING
"bttv: 82437FX: disabling peer concurrency\n");
2740 b
&= ~TRITON_PEER_CONCURRENCY
;
2742 if(!(b
& TRITON_STREAMING
))
2744 printk(KERN_WARNING
"bttv: 82437FX: disabling streaming\n");
2745 b
|= TRITON_STREAMING
;
2750 pci_write_config_byte(dev
, TRITON_PCON
, b
);
2751 printk(KERN_DEBUG
"bttv: 82437FX: PCON changed to: 0x%x\n",b
);
2757 static void handle_chipset(void)
2761 for (index
= 0; index
< 8; index
++)
2763 unsigned char bus
, devfn
;
2766 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
2768 if (!pcibios_find_device(PCI_VENDOR_ID_SI
,
2769 PCI_DEVICE_ID_SI_496
,
2770 index
, &bus
, &devfn
))
2772 printk(KERN_WARNING
"BT848 and SIS 85C496 chipset don't always work together.\n");
2775 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL
,
2776 PCI_DEVICE_ID_INTEL_82441
,
2777 index
, &bus
, &devfn
))
2779 pcibios_read_config_byte(bus
, devfn
, 0x53, &b
);
2780 DEBUG(printk(KERN_INFO
"bttv: Host bridge: 82441FX Natoma, "));
2781 DEBUG(printk("bufcon=0x%02x\n",b
));
2784 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82437
,
2785 index
, &bus
, &devfn
))
2787 printk(KERN_INFO
"bttv: Host bridge 82437FX Triton PIIX\n");
2788 triton1
=BT848_INT_ETBF
;
2791 /* The ETBF bit SHOULD make all this unnecessary */
2792 /* 430FX (Triton I) freezes with bus concurrency on -> switch it off */
2796 pcibios_read_config_byte(bus
, devfn
, TRITON_PCON
, &b
);
2798 DEBUG(printk(KERN_DEBUG
"bttv: 82437FX: PCON: 0x%x\n",b
));
2800 if(!(b
& TRITON_BUS_CONCURRENCY
))
2802 printk(KERN_WARNING
"bttv: 82437FX: disabling bus concurrency\n");
2803 b
|= TRITON_BUS_CONCURRENCY
;
2806 if(b
& TRITON_PEER_CONCURRENCY
)
2808 printk(KERN_WARNING
"bttv: 82437FX: disabling peer concurrency\n");
2809 b
&= ~TRITON_PEER_CONCURRENCY
;
2811 if(!(b
& TRITON_STREAMING
))
2813 printk(KERN_WARNING
"bttv: 82437FX: disabling streaming\n");
2814 b
|= TRITON_STREAMING
;
2819 pcibios_write_config_byte(bus
, devfn
, TRITON_PCON
, b
);
2820 printk(KERN_DEBUG
"bttv: 82437FX: PCON changed to: 0x%x\n",b
);
2829 static void init_tda8425(struct i2c_bus
*bus
)
2831 I2CWrite(bus
, I2C_TDA8425
, TDA8425_VL
, 0xFC, 1); /* volume left 0dB */
2832 I2CWrite(bus
, I2C_TDA8425
, TDA8425_VR
, 0xFC, 1); /* volume right 0dB */
2833 I2CWrite(bus
, I2C_TDA8425
, TDA8425_BA
, 0xF6, 1); /* bass 0dB */
2834 I2CWrite(bus
, I2C_TDA8425
, TDA8425_TR
, 0xF6, 1); /* treble 0dB */
2835 I2CWrite(bus
, I2C_TDA8425
, TDA8425_S1
, 0xCE, 1); /* mute off */
2838 static void init_tda9840(struct i2c_bus
*bus
)
2840 I2CWrite(bus
, I2C_TDA9840
, TDA9840_SW
, 0x2A, 1); /* Sound mode switching */
2843 static void init_tda9850(struct i2c_bus
*bus
)
2845 I2CWrite(bus
, I2C_TDA9850
, TDA9850_CON1
, 0x08, 1); /* noise threshold st */
2846 I2CWrite(bus
, I2C_TDA9850
, TDA9850_CON2
, 0x08, 1); /* noise threshold sap */
2847 I2CWrite(bus
, I2C_TDA9850
, TDA9850_CON3
, 0x40, 1); /* stereo mode */
2848 I2CWrite(bus
, I2C_TDA9850
, TDA9850_CON4
, 0x07, 1); /* 0 dB input gain?*/
2849 I2CWrite(bus
, I2C_TDA9850
, TDA9850_ALI1
, 0x10, 1); /* wideband alignment? */
2850 I2CWrite(bus
, I2C_TDA9850
, TDA9850_ALI2
, 0x10, 1); /* spectral alignment? */
2851 I2CWrite(bus
, I2C_TDA9850
, TDA9850_ALI3
, 0x03, 1);
2856 /* Figure out card and tuner type */
2858 static void idcard(int i
)
2860 struct bttv
*btv
= &bttvs
[i
];
2862 btwrite(0, BT848_GPIO_OUT_EN
);
2863 DEBUG(printk(KERN_DEBUG
"bttv%d: GPIO: 0x%08x\n", i
, btread(BT848_GPIO_DATA
)));
2865 /* Default the card to the user-selected one. */
2867 btv
->tuner_type
=-1; /* use default tuner type */
2869 /* If we were asked to auto-detect, then do so!
2870 Right now this will only recognize Miro, Hauppauge or STB
2872 if (btv
->type
== BTTV_UNKNOWN
)
2874 if (I2CRead(&(btv
->i2c
), I2C_HAUPEE
)>=0)
2877 btv
->type
=BTTV_HAUPPAUGE878
;
2879 btv
->type
=BTTV_HAUPPAUGE
;
2881 } else if (I2CRead(&(btv
->i2c
), I2C_STBEE
)>=0) {
2884 if (I2CRead(&(btv
->i2c
), I2C_VHX
)>=0) {
2887 if (I2CRead(&(btv
->i2c
), 0x80)>=0) /* check for msp34xx */
2888 btv
->type
= BTTV_MIROPRO
;
2890 btv
->type
=BTTV_MIRO
;
2894 /* board specific initialisations */
2895 if (btv
->type
== BTTV_MIRO
|| btv
->type
== BTTV_MIROPRO
) {
2896 /* auto detect tuner for MIRO cards */
2897 btv
->tuner_type
=((btread(BT848_GPIO_DATA
)>>10)-1)&7;
2899 if (btv
->type
== BTTV_HAUPPAUGE
|| btv
->type
== BTTV_HAUPPAUGE878
) {
2900 hauppauge_msp_reset(btv
);
2901 hauppauge_eeprom(&(btv
->i2c
));
2902 if (btv
->type
== BTTV_HAUPPAUGE878
) {
2903 /* all bt878 hauppauge boards use this ... */
2904 btv
->pll
.pll_ifreq
=28636363;
2905 btv
->pll
.pll_crystal
=BT848_IFORM_XT0
;
2908 if(btv
->type
==BTTV_AVERMEDIA98
)
2910 btv
->pll
.pll_ifreq
=28636363;
2911 btv
->pll
.pll_crystal
=BT848_IFORM_XT0
;
2914 if (btv
->have_tuner
&& btv
->tuner_type
!= -1)
2915 i2c_control_device(&(btv
->i2c
),
2917 TUNER_SET_TYPE
,&btv
->tuner_type
);
2920 if (I2CRead(&(btv
->i2c
), I2C_TDA9840
) >=0)
2922 btv
->audio_chip
= TDA9840
;
2923 printk(KERN_INFO
"bttv%d: audio chip: TDA9840\n", btv
->nr
);
2926 if (I2CRead(&(btv
->i2c
), I2C_TDA9850
) >=0)
2928 btv
->audio_chip
= TDA9850
;
2929 printk(KERN_INFO
"bttv%d: audio chip: TDA9850\n",btv
->nr
);
2932 if (I2CRead(&(btv
->i2c
), I2C_TDA8425
) >=0)
2934 btv
->audio_chip
= TDA8425
;
2935 printk(KERN_INFO
"bttv%d: audio chip: TDA8425\n",btv
->nr
);
2938 switch(btv
->audio_chip
)
2941 init_tda9850(&(btv
->i2c
));
2944 init_tda9840(&(btv
->i2c
));
2947 init_tda8425(&(btv
->i2c
));
2951 printk(KERN_INFO
"bttv%d: model: ",btv
->nr
);
2953 sprintf(btv
->video_dev
.name
,"BT%d",btv
->id
);
2958 strcat(btv
->video_dev
.name
,
2959 (btv
->type
== BTTV_MIRO
) ? "(Miro)" : "(Miro pro)");
2961 case BTTV_HAUPPAUGE
:
2962 strcat(btv
->video_dev
.name
,"(Hauppauge old)");
2964 case BTTV_HAUPPAUGE878
:
2965 strcat(btv
->video_dev
.name
,"(Hauppauge new)");
2968 strcat(btv
->video_dev
.name
,"(STB)");
2971 strcat(btv
->video_dev
.name
,"(Intel)");
2974 strcat(btv
->video_dev
.name
,"(Diamond)");
2976 case BTTV_AVERMEDIA
:
2977 strcat(btv
->video_dev
.name
,"(AVerMedia)");
2979 case BTTV_MATRIX_VISION
:
2980 strcat(btv
->video_dev
.name
,"(MATRIX-Vision)");
2982 case BTTV_AVERMEDIA98
:
2983 strcat(btv
->video_dev
.name
,"(AVerMedia TVCapture 98)");
2986 strcpy(btv
->video_dev
.name
,"BT848(Aimslab-VHX)");
2989 printk("%s\n",btv
->video_dev
.name
);
2990 audio(btv
, AUDIO_MUTE
);
2995 static void bt848_set_risc_jmps(struct bttv
*btv
)
2999 /* Sync to start of odd field */
3000 btv
->risc_jmp
[0]=BT848_RISC_SYNC
|BT848_RISC_RESYNC
|BT848_FIFO_STATUS_VRE
;
3003 /* Jump to odd vbi sub */
3004 btv
->risc_jmp
[2]=BT848_RISC_JUMP
|(0x5<<20);
3006 btv
->risc_jmp
[3]=virt_to_bus(btv
->vbi_odd
);
3008 btv
->risc_jmp
[3]=virt_to_bus(btv
->risc_jmp
+4);
3010 /* Jump to odd sub */
3011 btv
->risc_jmp
[4]=BT848_RISC_JUMP
|(0x6<<20);
3013 btv
->risc_jmp
[5]=virt_to_bus(btv
->risc_odd
);
3015 btv
->risc_jmp
[5]=virt_to_bus(btv
->risc_jmp
+6);
3018 /* Sync to start of even field */
3019 btv
->risc_jmp
[6]=BT848_RISC_SYNC
|BT848_RISC_RESYNC
|BT848_FIFO_STATUS_VRO
;
3022 /* Jump to even vbi sub */
3023 btv
->risc_jmp
[8]=BT848_RISC_JUMP
;
3025 btv
->risc_jmp
[9]=virt_to_bus(btv
->vbi_even
);
3027 btv
->risc_jmp
[9]=virt_to_bus(btv
->risc_jmp
+10);
3029 /* Jump to even sub */
3030 btv
->risc_jmp
[10]=BT848_RISC_JUMP
|(8<<20);
3032 btv
->risc_jmp
[11]=virt_to_bus(btv
->risc_even
);
3034 btv
->risc_jmp
[11]=virt_to_bus(btv
->risc_jmp
+12);
3036 btv
->risc_jmp
[12]=BT848_RISC_JUMP
;
3037 btv
->risc_jmp
[13]=virt_to_bus(btv
->risc_jmp
);
3039 /* enable cpaturing and DMA */
3040 btaor(flags
, ~0x0f, BT848_CAP_CTL
);
3047 static int init_bt848(int i
)
3049 struct bttv
*btv
= &bttvs
[i
];
3053 /* reset the bt848 */
3054 btwrite(0, BT848_SRESET
);
3055 DEBUG(printk(KERN_DEBUG
"bttv%d: bt848_mem: 0x%08x\n",i
,(unsigned int) btv
->bt848_mem
));
3057 /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
3058 btv
->win
.norm
=0; /* change this to 1 for NTSC, 2 for SECAM */
3059 btv
->win
.interlace
=1;
3062 btv
->win
.width
=768; /* 640 */
3063 btv
->win
.height
=576; /* 480 */
3064 btv
->win
.cropwidth
=768; /* 640 */
3065 btv
->win
.cropheight
=576; /* 480 */
3070 btv
->win
.color_fmt
=BT848_COLOR_FMT_RGB16
;
3071 btv
->win
.bpl
=1024*btv
->win
.bpp
;
3072 btv
->win
.swidth
=1024;
3073 btv
->win
.sheight
=768;
3086 btv
->field
=btv
->last_field
=0;
3087 /* cevans - prevents panic if initialization bails due to memory
3090 btv
->video_dev
.minor
= -1;
3091 btv
->vbi_dev
.minor
= -1;
3092 btv
->radio_dev
.minor
= -1;
3095 memcpy(&(btv
->i2c
),&bttv_i2c_bus_template
,sizeof(struct i2c_bus
));
3096 sprintf(btv
->i2c
.name
,"bt848-%d",i
);
3097 btv
->i2c
.data
= btv
;
3099 if (!(btv
->risc_odd
=(unsigned int *) kmalloc(RISCMEM_LEN
/2, GFP_KERNEL
)))
3101 if (!(btv
->risc_even
=(unsigned int *) kmalloc(RISCMEM_LEN
/2, GFP_KERNEL
)))
3103 if (!(btv
->risc_jmp
=(unsigned int *) kmalloc(2048, GFP_KERNEL
)))
3105 DEBUG(printk(KERN_DEBUG
"risc_jmp: %p\n",btv
->risc_jmp
));
3106 btv
->vbi_odd
=btv
->risc_jmp
+16;
3107 btv
->vbi_even
=btv
->vbi_odd
+256;
3108 btv
->bus_vbi_odd
=virt_to_bus(btv
->risc_jmp
+12);
3109 btv
->bus_vbi_even
=virt_to_bus(btv
->risc_jmp
+6);
3111 btwrite(virt_to_bus(btv
->risc_jmp
+2), BT848_RISC_STRT_ADD
);
3112 btv
->vbibuf
=(unsigned char *) vmalloc(VBIBUF_SIZE
);
3115 if (!(btv
->grisc
=(unsigned int *) kmalloc(32768, GFP_KERNEL
)))
3118 memset(btv
->vbibuf
, 0, VBIBUF_SIZE
); /* We don't want to return random
3119 memory to the user */
3123 bt848_muxsel(btv
, 1);
3124 bt848_set_winsize(btv
);
3126 /* btwrite(0, BT848_TDEC); */
3127 btwrite(0x10, BT848_COLOR_CTL
);
3128 btwrite(0x00, BT848_CAP_CTL
);
3129 btwrite(0xac, BT848_GPIO_DMA_CTL
);
3131 /* select direct input */
3132 btwrite(0x00, BT848_GPIO_REG_INP
);
3134 btwrite(BT848_IFORM_MUX1
| BT848_IFORM_XTAUTO
| BT848_IFORM_PAL_BDGHI
,
3137 btwrite(0xd8, BT848_CONTRAST_LO
);
3138 bt848_bright(btv
, 0x10);
3140 btwrite(0x20, BT848_E_VSCALE_HI
);
3141 btwrite(0x20, BT848_O_VSCALE_HI
);
3142 btwrite(/*BT848_ADC_SYNC_T|*/
3143 BT848_ADC_RESERVED
|BT848_ADC_CRUSH
, BT848_ADC
);
3145 btwrite(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
3146 btwrite(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
3148 btv
->picture
.colour
=254<<7;
3149 btv
->picture
.brightness
=128<<8;
3150 btv
->picture
.hue
=128<<8;
3151 btv
->picture
.contrast
=0xd8<<7;
3153 btwrite(0x00, BT848_E_SCLOOP
);
3154 btwrite(0x00, BT848_O_SCLOOP
);
3156 /* clear interrupt status */
3157 btwrite(0xfffffUL
, BT848_INT_STAT
);
3159 /* set interrupt mask */
3160 btwrite(btv
->triton1
|
3161 /*BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|
3162 BT848_INT_FDSR|BT848_INT_FTRGT|BT848_INT_FBUS|*/
3165 BT848_INT_RISCI
|BT848_INT_OCERR
|BT848_INT_VPRES
|
3166 BT848_INT_FMTCHG
|BT848_INT_HLOCK
,
3170 bt848_set_risc_jmps(btv
);
3173 * Now add the template and register the device unit.
3176 memcpy(&btv
->video_dev
,&bttv_template
, sizeof(bttv_template
));
3177 memcpy(&btv
->vbi_dev
,&vbi_template
, sizeof(vbi_template
));
3178 memcpy(&btv
->radio_dev
,&radio_template
,sizeof(radio_template
));
3182 if(video_register_device(&btv
->video_dev
,VFL_TYPE_GRABBER
)<0)
3184 if(video_register_device(&btv
->vbi_dev
,VFL_TYPE_VBI
)<0)
3186 video_unregister_device(&btv
->video_dev
);
3191 if(video_register_device(&btv
->radio_dev
, VFL_TYPE_RADIO
)<0)
3193 video_unregister_device(&btv
->vbi_dev
);
3194 video_unregister_device(&btv
->video_dev
);
3198 i2c_register_bus(&btv
->i2c
);
3203 static void bttv_irq(int irq
, void *dev_id
, struct pt_regs
* regs
)
3210 btv
=(struct bttv
*)dev_id
;
3214 /* get/clear interrupt status bits */
3215 stat
=btread(BT848_INT_STAT
);
3216 astat
=stat
&btread(BT848_INT_MASK
);
3219 btwrite(astat
,BT848_INT_STAT
);
3220 IDEBUG(printk ("bttv%d: astat %08x\n", btv
->nr
, astat
));
3221 IDEBUG(printk ("bttv%d: stat %08x\n", btv
->nr
, stat
));
3223 /* get device status bits */
3224 dstat
=btread(BT848_DSTATUS
);
3226 if (astat
&BT848_INT_FMTCHG
)
3228 IDEBUG(printk ("bttv%d: IRQ_FMTCHG\n", btv
->nr
));
3230 (dstat&BT848_DSTATUS_NUML) ? (~1) : (~0); */
3232 if (astat
&BT848_INT_VPRES
)
3234 IDEBUG(printk ("bttv%d: IRQ_VPRES\n", btv
->nr
));
3236 if (astat
&BT848_INT_VSYNC
)
3238 IDEBUG(printk ("bttv%d: IRQ_VSYNC\n", btv
->nr
));
3241 if (astat
&BT848_INT_SCERR
) {
3242 IDEBUG(printk ("bttv%d: IRQ_SCERR\n", btv
->nr
));
3245 wake_up_interruptible(&btv
->vbiq
);
3246 wake_up_interruptible(&btv
->capq
);
3249 if (astat
&BT848_INT_RISCI
)
3251 IDEBUG(printk ("bttv%d: IRQ_RISCI\n", btv
->nr
));
3253 /* captured VBI frame */
3257 /* inc vbi frame count for detecting drops */
3258 (*(u32
*)&(btv
->vbibuf
[VBIBUF_SIZE
- 4]))++;
3259 wake_up_interruptible(&btv
->vbiq
);
3262 /* captured full frame */
3265 wake_up_interruptible(&btv
->capq
);
3266 btv
->last_field
=btv
->field
;
3268 btv
->frame_stat
[btv
->grf
] = GBUFFER_DONE
;
3269 if ((--btv
->grabbing
))
3271 btv
->gfmt
= btv
->gfmt_next
;
3272 btv
->gwidth
= btv
->gwidth_next
;
3273 btv
->gheight
= btv
->gheight_next
;
3274 btv
->gro
= btv
->gro_next
;
3275 btv
->gre
= btv
->gre_next
;
3276 btv
->grf
= btv
->grf_next
;
3277 btv
->risc_jmp
[5]=btv
->gro
;
3278 btv
->risc_jmp
[11]=btv
->gre
;
3279 bt848_set_geo(btv
, btv
->gwidth
,
3283 bt848_set_risc_jmps(btv
);
3284 btand(~BT848_VSCALE_COMB
, BT848_E_VSCALE_HI
);
3285 btand(~BT848_VSCALE_COMB
, BT848_O_VSCALE_HI
);
3286 bt848_set_geo(btv
, btv
->win
.width
,
3288 btv
->win
.color_fmt
);
3290 wake_up_interruptible(&btv
->capq
);
3295 btv
->risc_jmp
[5]=btv
->gro
;
3296 btv
->risc_jmp
[11]=btv
->gre
;
3297 btv
->risc_jmp
[12]=BT848_RISC_JUMP
;
3298 bt848_set_geo(btv
, btv
->gwidth
, btv
->gheight
,
3302 if (astat
&BT848_INT_OCERR
)
3304 IDEBUG(printk ("bttv%d: IRQ_OCERR\n", btv
->nr
));
3306 if (astat
&BT848_INT_PABORT
)
3308 IDEBUG(printk ("bttv%d: IRQ_PABORT\n", btv
->nr
));
3310 if (astat
&BT848_INT_RIPERR
)
3312 IDEBUG(printk ("bttv%d: IRQ_RIPERR\n", btv
->nr
));
3314 if (astat
&BT848_INT_PPERR
)
3316 IDEBUG(printk ("bttv%d: IRQ_PPERR\n", btv
->nr
));
3318 if (astat
&BT848_INT_FDSR
)
3320 IDEBUG(printk ("bttv%d: IRQ_FDSR\n", btv
->nr
));
3322 if (astat
&BT848_INT_FTRGT
)
3324 IDEBUG(printk ("bttv%d: IRQ_FTRGT\n", btv
->nr
));
3326 if (astat
&BT848_INT_FBUS
)
3328 IDEBUG(printk ("bttv%d: IRQ_FBUS\n", btv
->nr
));
3330 if (astat
&BT848_INT_HLOCK
)
3332 if ((dstat
&BT848_DSTATUS_HLOC
) || (btv
->radio
))
3333 audio(btv
, AUDIO_ON
);
3335 audio(btv
, AUDIO_OFF
);
3338 if (astat
&BT848_INT_I2CDONE
)
3344 printk (KERN_WARNING
"bttv%d: irq loop %d\n",
3348 btwrite(0, BT848_INT_MASK
);
3350 "bttv%d: IRQ lockup, cleared int mask\n", btv
->nr
);
3358 * Scan for a Bt848 card, request the irq and map the io memory
3361 #if LINUX_VERSION_CODE >= 0x020100
3362 int configure_bt848(struct pci_dev
*dev
, int bttv_num
)
3365 unsigned char command
;
3368 btv
=&bttvs
[bttv_num
];
3371 btv
->bt848_mem
=NULL
;
3380 btv
->vbip
=VBIBUF_SIZE
;
3382 btv
->id
=dev
->device
;
3384 btv
->bt848_adr
=dev
->base_address
[0];
3386 btv
->i2c_command
= 0x83;
3388 btv
->i2c_command
=(I2C_TIMING
| BT848_I2C_SCL
| BT848_I2C_SDA
);
3390 if (remap
[bttv_num
])
3392 if (remap
[bttv_num
] < 0x1000)
3393 remap
[bttv_num
]<<=20;
3394 remap
[bttv_num
]&=PCI_BASE_ADDRESS_MEM_MASK
;
3395 printk(KERN_INFO
"bttv%d: remapping to : 0x%08x.\n",
3396 bttv_num
,remap
[bttv_num
]);
3397 remap
[bttv_num
]|=btv
->bt848_adr
&(~PCI_BASE_ADDRESS_MEM_MASK
);
3398 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_0
, remap
[bttv_num
]);
3399 pci_read_config_dword(dev
, PCI_BASE_ADDRESS_0
, &btv
->bt848_adr
);
3400 btv
->dev
->base_address
[0] = btv
->bt848_adr
;
3402 btv
->bt848_adr
&=PCI_BASE_ADDRESS_MEM_MASK
;
3403 pci_read_config_byte(dev
, PCI_CLASS_REVISION
, &btv
->revision
);
3404 printk(KERN_INFO
"bttv%d: Brooktree Bt%d (rev %d) ",
3405 bttv_num
,btv
->id
, btv
->revision
);
3406 printk("bus: %d, devfn: %d, ",dev
->bus
->number
, dev
->devfn
);
3407 printk("irq: %d, ",btv
->irq
);
3408 printk("memory: 0x%08x.\n", btv
->bt848_adr
);
3410 btv
->pll
.pll_crystal
= 0;
3411 btv
->pll
.pll_ifreq
= 0;
3412 btv
->pll
.pll_ofreq
= 0;
3413 btv
->pll
.pll_current
= 0;
3414 if (!(btv
->id
==848 && btv
->revision
==0x11)) {
3415 switch (pll
[btv
->nr
]) {
3420 /* 28 MHz crystal installed */
3421 btv
->pll
.pll_ifreq
=28636363;
3422 btv
->pll
.pll_crystal
=BT848_IFORM_XT0
;
3425 /* 35 MHz crystal installed */
3426 btv
->pll
.pll_ifreq
=35468950;
3427 btv
->pll
.pll_crystal
=BT848_IFORM_XT1
;
3432 btv
->bt848_mem
=ioremap(btv
->bt848_adr
, 0x1000);
3434 /* clear interrupt mask */
3435 btwrite(0, BT848_INT_MASK
);
3437 result
= request_irq(btv
->irq
, bttv_irq
,
3438 SA_SHIRQ
| SA_INTERRUPT
,"bttv",(void *)btv
);
3439 if (result
==-EINVAL
)
3441 printk(KERN_ERR
"bttv%d: Bad irq number or handler\n",
3447 printk(KERN_ERR
"bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num
,btv
->irq
);
3453 pci_set_master(dev
);
3455 btv
->triton1
=triton1
? BT848_INT_ETBF
: 0;
3456 if (triton1
&& btv
->id
>= 878)
3459 printk("bttv: Enabling 430FX compatibilty for bt878\n");
3460 pci_read_config_byte(dev
, BT878_DEVCTRL
, &command
);
3461 command
|=BT878_EN_TBFX
;
3462 pci_write_config_byte(dev
, BT878_DEVCTRL
, command
);
3463 pci_read_config_byte(dev
, BT878_DEVCTRL
, &command
);
3464 if (!(command
&BT878_EN_TBFX
))
3466 printk("bttv: 430FX compatibility could not be enabled\n");
3474 static int find_bt848(void)
3476 struct pci_dev
*dev
= pci_devices
;
3483 if (dev
->vendor
== PCI_VENDOR_ID_BROOKTREE
)
3484 if ((dev
->device
== PCI_DEVICE_ID_BT848
)||
3485 (dev
->device
== PCI_DEVICE_ID_BT849
)||
3486 (dev
->device
== PCI_DEVICE_ID_BT878
)||
3487 (dev
->device
== PCI_DEVICE_ID_BT879
))
3488 result
=configure_bt848(dev
,bttv_num
++);
3494 printk(KERN_INFO
"bttv: %d Bt8xx card(s) found.\n", bttv_num
);
3498 static int find_bt848(void)
3501 unsigned char command
, latency
;
3503 unsigned char bus
, devfn
;
3508 if (!pcibios_present())
3510 DEBUG(printk(KERN_DEBUG
"bttv%d: PCI-BIOS not present or not accessable!\n",bttv_num
));
3515 !pcibios_find_device(PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT849
,
3516 pci_index
, &bus
, &devfn
)
3517 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT848
,
3518 pci_index
, &bus
, &devfn
)
3519 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT878
,
3520 pci_index
, &bus
, &devfn
)
3521 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT879
,
3522 pci_index
, &bus
, &devfn
);
3525 btv
=&bttvs
[bttv_num
];
3529 btv
->bt848_mem
=NULL
;
3539 btv
->vbip
=VBIBUF_SIZE
;
3541 pcibios_read_config_word(btv
->bus
, btv
->devfn
, PCI_DEVICE_ID
,
3543 pcibios_read_config_byte(btv
->bus
, btv
->devfn
,
3544 PCI_INTERRUPT_LINE
, &btv
->irq
);
3545 pcibios_read_config_dword(btv
->bus
, btv
->devfn
, PCI_BASE_ADDRESS_0
,
3548 btv
->i2c_command
= 0x83;
3551 (I2C_TIMING
| BT848_I2C_SCL
| BT848_I2C_SDA
);
3553 if (remap
[bttv_num
])
3555 if (remap
[bttv_num
] < 0x1000)
3556 remap
[bttv_num
]<<=20;
3557 remap
[bttv_num
]&=PCI_BASE_ADDRESS_MEM_MASK
;
3558 printk(KERN_INFO
"bttv%d: remapping to : 0x%08x.\n",
3559 bttv_num
,remap
[bttv_num
]);
3560 remap
[bttv_num
]|=btv
->bt848_adr
&(~PCI_BASE_ADDRESS_MEM_MASK
);
3561 pcibios_write_config_dword(btv
->bus
, btv
->devfn
, PCI_BASE_ADDRESS_0
,
3563 pcibios_read_config_dword(btv
->bus
, btv
->devfn
, PCI_BASE_ADDRESS_0
,
3567 btv
->bt848_adr
&=PCI_BASE_ADDRESS_MEM_MASK
;
3568 pcibios_read_config_byte(btv
->bus
, btv
->devfn
, PCI_CLASS_REVISION
,
3570 printk(KERN_INFO
"bttv%d: Brooktree Bt%d (rev %d) ",
3571 bttv_num
,btv
->id
, btv
->revision
);
3572 printk("bus: %d, devfn: %d, ",
3573 btv
->bus
, btv
->devfn
);
3574 printk("irq: %d, ",btv
->irq
);
3575 printk("memory: 0x%08x.\n", btv
->bt848_adr
);
3577 btv
->pll
.pll_crystal
= 0;
3578 btv
->pll
.pll_ifreq
= 0;
3579 btv
->pll
.pll_ofreq
= 0;
3580 btv
->pll
.pll_current
= 0;
3581 if (!(btv
->id
==848 && btv
->revision
==0x11)) {
3582 switch (pll
[btv
->nr
]) {
3587 /* 28 MHz crystal installed */
3588 btv
->pll
.pll_ifreq
=28636363;
3589 btv
->pll
.pll_crystal
=BT848_IFORM_XT0
;
3592 /* 35 MHz crystal installed */
3593 btv
->pll
.pll_ifreq
=35468950;
3594 btv
->pll
.pll_crystal
=BT848_IFORM_XT1
;
3599 btv
->bt848_mem
=ioremap(btv
->bt848_adr
, 0x1000);
3601 result
= request_irq(btv
->irq
, bttv_irq
,
3602 SA_SHIRQ
| SA_INTERRUPT
,"bttv",(void *)btv
);
3603 if (result
==-EINVAL
)
3605 printk(KERN_ERR
"bttv%d: Bad irq number or handler\n",
3611 printk(KERN_ERR
"bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num
,btv
->irq
);
3617 /* Enable bus-mastering */
3618 pcibios_read_config_byte(btv
->bus
, btv
->devfn
, PCI_COMMAND
, &command
);
3619 command
|=PCI_COMMAND_MASTER
;
3620 pcibios_write_config_byte(btv
->bus
, btv
->devfn
, PCI_COMMAND
, command
);
3621 pcibios_read_config_byte(btv
->bus
, btv
->devfn
, PCI_COMMAND
, &command
);
3622 if (!(command
&PCI_COMMAND_MASTER
))
3624 printk(KERN_ERR
"bttv%d: PCI bus-mastering could not be enabled\n",bttv_num
);
3627 pcibios_read_config_byte(btv
->bus
, btv
->devfn
, PCI_LATENCY_TIMER
,
3632 pcibios_write_config_byte(btv
->bus
, btv
->devfn
,
3633 PCI_LATENCY_TIMER
, latency
);
3635 DEBUG(printk(KERN_DEBUG
"bttv%d: latency: %02x\n",
3636 bttv_num
, latency
));
3638 btv
->triton1
=triton1
? BT848_INT_ETBF
: 0;
3639 if (triton1
&& btv
->id
>= 878)
3642 printk("bttv: Enabling 430FX compatibilty for bt878\n");
3643 pcibios_read_config_byte(btv
->bus
, btv
->devfn
, BT878_DEVCTRL
, &command
);
3644 command
|=BT878_EN_TBFX
;
3645 pcibios_write_config_byte(btv
->bus
, btv
->devfn
, BT878_DEVCTRL
, command
);
3646 pcibios_read_config_byte(btv
->bus
, btv
->devfn
, BT878_DEVCTRL
, &command
);
3647 if (!(command
&BT878_EN_TBFX
))
3649 printk("bttv: 430FX compatibility could not be enabled\n");
3657 printk(KERN_INFO
"bttv: %d Bt8xx card(s) found.\n", bttv_num
);
3662 static void release_bttv(void)
3668 for (i
=0;i
<bttv_num
; i
++)
3672 /* turn off all capturing, DMA and IRQs */
3674 btand(~15, BT848_GPIO_DMA_CTL
);
3676 /* first disable interrupts before unmapping the memory! */
3677 btwrite(0, BT848_INT_MASK
);
3678 btwrite(0xffffffffUL
,BT848_INT_STAT
);
3679 btwrite(0x0, BT848_GPIO_OUT_EN
);
3681 /* unregister i2c_bus */
3682 i2c_unregister_bus((&btv
->i2c
));
3684 /* disable PCI bus-mastering */
3685 #if LINUX_VERSION_CODE >= 0x020100
3686 pci_read_config_byte(btv
->dev
, PCI_COMMAND
, &command
);
3687 /* Should this be &=~ ?? */
3688 command
&=~PCI_COMMAND_MASTER
;
3689 pci_write_config_byte(btv
->dev
, PCI_COMMAND
, command
);
3691 pcibios_read_config_byte(btv
->bus
, btv
->devfn
, PCI_COMMAND
, &command
);
3692 command
&=~PCI_COMMAND_MASTER
;
3693 pcibios_write_config_byte(btv
->bus
, btv
->devfn
, PCI_COMMAND
, command
);
3697 /* unmap and free memory */
3699 kfree((void *) btv
->grisc
);
3702 kfree((void *) btv
->risc_odd
);
3705 kfree((void *) btv
->risc_even
);
3707 DEBUG(printk(KERN_DEBUG
"free: risc_jmp: 0x%08x.\n", btv
->risc_jmp
));
3709 kfree((void *) btv
->risc_jmp
);
3711 DEBUG(printk(KERN_DEBUG
"bt848_vbibuf: 0x%08x.\n", btv
->vbibuf
));
3713 vfree((void *) btv
->vbibuf
);
3716 free_irq(btv
->irq
,btv
);
3717 DEBUG(printk(KERN_DEBUG
"bt848_mem: 0x%08x.\n", btv
->bt848_mem
));
3719 iounmap(btv
->bt848_mem
);
3721 if(btv
->video_dev
.minor
!=-1)
3722 video_unregister_device(&btv
->video_dev
);
3723 if(btv
->vbi_dev
.minor
!=-1)
3724 video_unregister_device(&btv
->vbi_dev
);
3725 if (radio
[btv
->nr
] && btv
->radio_dev
.minor
!= -1)
3726 video_unregister_device(&btv
->radio_dev
);
3732 int init_module(void)
3735 int init_bttv_cards(struct video_init
*unused
)
3744 /* initialize Bt848s */
3745 for (i
=0; i
<bttv_num
; i
++)
3747 if (init_bt848(i
)<0)
3761 void cleanup_module(void)
3771 * c-brace-imaginary-offset: 0
3772 * c-brace-offset: -8
3773 * c-argdecl-indent: 8
3774 * c-label-offset: -8
3775 * c-continued-statement-offset: 8
3776 * c-continued-brace-offset: 0
3777 * indent-tabs-mode: nil