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