~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/media/video/bttv-driver.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.