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