2 zr36120.c - Zoran 36120/36125 based framegrabbers
4 Copyright (C) 1998-1999 Pauline Middelink <middelin@polyware.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/major.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
31 #include <linux/pci.h>
32 #include <linux/signal.h>
33 #include <linux/wait.h>
35 #include <asm/pgtable.h>
37 #include <linux/sched.h>
38 #include <linux/video_decoder.h>
40 #include <asm/uaccess.h>
44 #include "zr36120_mem.h"
46 /* mark an required function argument unused - lintism */
47 #define UNUSED(x) (void)(x)
49 /* sensible default */
54 /* Anybody who uses more than four? */
57 static unsigned int triton1=0; /* triton1 chipset? */
58 static unsigned int cardtype[ZORAN_MAX]={ [ 0 ... ZORAN_MAX-1 ] = CARDTYPE };
59 static int video_nr = -1;
60 static int vbi_nr = -1;
62 static struct pci_device_id zr36120_pci_tbl[] = {
63 { PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120,
64 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
67 MODULE_DEVICE_TABLE(pci, zr36120_pci_tbl);
69 MODULE_AUTHOR("Pauline Middelink <middelin@polyware.nl>");
70 MODULE_DESCRIPTION("Zoran ZR36120 based framegrabber");
71 MODULE_LICENSE("GPL");
73 module_param(triton1, uint, 0);
74 module_param_array(cardtype, uint, NULL, 0);
75 module_param(video_nr, int, 0);
76 module_param(vbi_nr, int, 0);
78 static int zoran_cards;
79 static struct zoran zorans[ZORAN_MAX];
82 * the meaning of each element can be found in zr36120.h
83 * Determining the value of gpdir/gpval can be tricky. The
84 * best way is to run the card under the original software
85 * and read the values from the general purpose registers
86 * 0x28 and 0x2C. How you do that is left as an exercise
87 * to the impatient reader :)
89 #define T 1 /* to separate the bools from the ints */
91 static struct tvcard tvcards[] = {
92 /* reported working by <middelin@polyware.nl> */
93 /*0*/ { "Trust Victor II",
94 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
95 /* reported working by <Michael.Paxton@aihw.gov.au> */
96 /*1*/ { "Aitech WaveWatcher TV-PCI",
97 3, 0, T, F, T, T, 0x7F, 0x80, { 1, TUNER(3), SVHS(6) }, { 0 } },
98 /* reported working by ? */
99 /*2*/ { "Genius Video Wonder PCI Video Capture Card",
100 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
101 /* reported working by <Pascal.Gabriel@wanadoo.fr> */
102 /*3*/ { "Guillemot Maxi-TV PCI",
103 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
104 /* reported working by "Craig Whitmore <lennon@igrin.co.nz> */
105 /*4*/ { "Quadrant Buster",
106 3, 3, T, F, T, T, 0x7F, 0x80, { SVHS(1), TUNER(2), 3 }, { 1, 2, 3 } },
107 /* a debug entry which has all inputs mapped */
108 /*5*/ { "ZR36120 based framegrabber (all inputs enabled)",
109 6, 0, T, T, T, T, 0x7F, 0x80, { 1, 2, 3, 4, 5, 6 }, { 0 } }
113 #define NRTVCARDS (sizeof(tvcards)/sizeof(tvcards[0]))
118 #define ENDIANESS ZORAN_VFEC_LE
121 static struct { const char name[8]; uint mode; uint bpp; } palette2fmt[] = {
122 /* n/a */ { "n/a", 0, 0 },
123 /* GREY */ { "GRAY", 0, 0 },
124 /* HI240 */ { "HI240", 0, 0 },
125 /* RGB565 */ { "RGB565", ZORAN_VFEC_RGB_RGB565|ENDIANESS, 2 },
126 /* RGB24 */ { "RGB24", ZORAN_VFEC_RGB_RGB888|ENDIANESS|ZORAN_VFEC_PACK24, 3 },
127 /* RGB32 */ { "RGB32", ZORAN_VFEC_RGB_RGB888|ENDIANESS, 4 },
128 /* RGB555 */ { "RGB555", ZORAN_VFEC_RGB_RGB555|ENDIANESS, 2 },
129 /* YUV422 */ { "YUV422", ZORAN_VFEC_RGB_YUV422|ENDIANESS, 2 },
130 /* YUYV */ { "YUYV", 0, 0 },
131 /* UYVY */ { "UYVY", 0, 0 },
132 /* YUV420 */ { "YUV420", 0, 0 },
133 /* YUV411 */ { "YUV411", 0, 0 },
134 /* RAW */ { "RAW", 0, 0 },
135 /* YUV422P */ { "YUV422P", 0, 0 },
136 /* YUV411P */ { "YUV411P", 0, 0 }};
137 #define NRPALETTES (sizeof(palette2fmt)/sizeof(palette2fmt[0]))
140 /* ----------------------------------------------------------------------- */
141 /* ZORAN chipset detector */
142 /* shamelessly stolen from bttv.c */
143 /* Reason for beeing here: we need to detect if we are running on a */
144 /* Triton based chipset, and if so, enable a certain bit */
145 /* ----------------------------------------------------------------------- */
147 void __init handle_chipset(void)
149 /* Just in case some nut set this to something dangerous */
151 triton1 = ZORAN_VDC_TRICOM;
153 if (pci_pci_problems & PCIPCI_TRITON) {
154 printk(KERN_INFO "zoran: Host bridge 82437FX Triton PIIX\n");
155 triton1 = ZORAN_VDC_TRICOM;
159 /* ----------------------------------------------------------------------- */
160 /* ZORAN functions */
161 /* ----------------------------------------------------------------------- */
163 static void zoran_set_geo(struct zoran* ztv, struct vidinfo* i);
167 void zoran_dump(struct zoran *ztv)
170 char *p=str; /* shut up, gcc! */
173 for (i=0; i<0x60; i+=4) {
175 if (i) printk("%s\n",str);
177 p+= sprintf(str, KERN_DEBUG " %04x: ",i);
179 p += sprintf(p, "%08x ",zrread(i));
185 void reap_states(struct zoran* ztv)
191 * Are we busy at all?
192 * This depends on if there is a workqueue AND the
193 * videotransfer is enabled on the chip...
195 if (ztv->workqueue && (zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
197 struct vidinfo* newitem;
199 /* did we get a complete frame? */
200 if (zrread(ZORAN_VSTR) & ZORAN_VSTR_GRAB)
203 DEBUG(printk(CARD_DEBUG "completed %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
205 /* we are done with this buffer, tell everyone */
206 ztv->workqueue->status = FBUFFER_DONE;
207 ztv->workqueue->fieldnr = ztv->fieldnr;
208 /* not good, here for BTTV_FIELDNR reasons */
209 ztv->lastfieldnr = ztv->fieldnr;
211 switch (ztv->workqueue->kindof) {
213 wake_up_interruptible(&ztv->grabq);
216 wake_up_interruptible(&ztv->vbiq);
219 printk(CARD_INFO "somebody killed the workqueue (kindof=%d)!\n",CARD,ztv->workqueue->kindof);
222 /* item completed, skip to next item in queue */
223 write_lock(&ztv->lock);
224 newitem = ztv->workqueue->next;
225 ztv->workqueue->next = 0; /* mark completed */
226 ztv->workqueue = newitem;
227 write_unlock(&ztv->lock);
231 * ok, so it seems we have nothing in progress right now.
232 * Lets see if we can find some work.
236 struct vidinfo* newitem;
239 DEBUG(printk(CARD_DEBUG "starting %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
241 /* loadup the frame settings */
242 read_lock(&ztv->lock);
243 zoran_set_geo(ztv,ztv->workqueue);
244 read_unlock(&ztv->lock);
246 switch (ztv->workqueue->kindof) {
249 zrand(~ZORAN_OCR_OVLEN, ZORAN_OCR);
250 zror(ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
251 zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
253 /* start single-shot grab */
254 zror(ZORAN_VSTR_GRAB, ZORAN_VSTR);
257 printk(CARD_INFO "what is this doing on the queue? (kindof=%d)\n",CARD,ztv->workqueue->kindof);
258 write_lock(&ztv->lock);
259 newitem = ztv->workqueue->next;
260 ztv->workqueue->next = 0;
261 ztv->workqueue = newitem;
262 write_unlock(&ztv->lock);
264 goto again; /* yeah, sure.. */
269 DEBUG(printk(CARD_DEBUG "nothing in queue\n",CARD));
272 * What? Even the workqueue is empty? Am i really here
273 * for nothing? Did i come all that way to... do nothing?
276 /* do we need to overlay? */
277 if (test_bit(STATE_OVERLAY, &ztv->state))
279 /* are we already overlaying? */
280 if (!(zrread(ZORAN_OCR) & ZORAN_OCR_OVLEN) ||
281 !(zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
283 DEBUG(printk(CARD_DEBUG "starting overlay\n",CARD));
285 read_lock(&ztv->lock);
286 zoran_set_geo(ztv,&ztv->overinfo);
287 read_unlock(&ztv->lock);
289 zror(ZORAN_OCR_OVLEN, ZORAN_OCR);
290 zrand(~ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
291 zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
295 * leave overlaying on, but turn interrupts off.
297 zrand(~ZORAN_ICR_EN,ZORAN_ICR);
301 /* do we have any VBI idle time processing? */
302 if (test_bit(STATE_VBI, &ztv->state))
304 struct vidinfo* item;
305 struct vidinfo* lastitem;
307 /* protect the workqueue */
308 write_lock(&ztv->lock);
309 lastitem = ztv->workqueue;
311 while (lastitem->next) lastitem = lastitem->next;
312 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
313 if (item->next == 0 && item->status == FBUFFER_FREE)
315 DEBUG(printk(CARD_DEBUG "%p added to queue\n",CARD,item));
316 item->status = FBUFFER_BUSY;
318 ztv->workqueue = item;
320 lastitem->next = item;
323 write_unlock(&ztv->lock);
325 goto again; /* hey, _i_ graduated :) */
329 * Then we must be realy IDLE
331 DEBUG(printk(CARD_DEBUG "turning off\n",CARD));
332 /* nothing further to do, disable DMA and further IRQs */
333 zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
334 zrand(~ZORAN_ICR_EN,ZORAN_ICR);
338 void zoran_irq(int irq, void *dev_id, struct pt_regs * regs)
342 struct zoran *ztv = dev_id;
344 UNUSED(irq); UNUSED(regs);
346 /* get/clear interrupt status bits */
347 stat=zrread(ZORAN_ISR);
348 estat=stat & zrread(ZORAN_ICR);
351 zrwrite(estat,ZORAN_ISR);
352 IDEBUG(printk(CARD_DEBUG "estat %08x\n",CARD,estat));
353 IDEBUG(printk(CARD_DEBUG " stat %08x\n",CARD,stat));
355 if (estat & ZORAN_ISR_CODE)
357 IDEBUG(printk(CARD_DEBUG "CodReplIRQ\n",CARD));
359 if (estat & ZORAN_ISR_GIRQ0)
361 IDEBUG(printk(CARD_DEBUG "GIRQ0\n",CARD));
362 if (!ztv->card->usegirq1)
365 if (estat & ZORAN_ISR_GIRQ1)
367 IDEBUG(printk(CARD_DEBUG "GIRQ1\n",CARD));
368 if (ztv->card->usegirq1)
374 printk(CARD_ERR "irq loop %d (%x)\n",CARD,count,estat);
377 zrwrite(0, ZORAN_ICR);
378 printk(CARD_ERR "IRQ lockup, cleared int mask\n",CARD);
384 int zoran_muxsel(struct zoran* ztv, int channel, int norm)
388 /* set the new video norm */
389 rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
394 /* map the given channel to the cards decoder's channel */
395 channel = ztv->card->video_mux[channel] & CHANNEL_MASK;
397 /* set the new channel */
398 rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &channel);
402 /* Tell the interrupt handler what to to. */
404 void zoran_cap(struct zoran* ztv, int on)
406 DEBUG(printk(CARD_DEBUG "zoran_cap(%d) state=%x\n",CARD,on,ztv->state));
412 * turn interrupts (back) on. The DMA will be enabled
413 * inside the irq handler when it detects a restart.
415 zror(ZORAN_ICR_EN,ZORAN_ICR);
419 * turn both interrupts and DMA off
421 zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
422 zrand(~ZORAN_ICR_EN,ZORAN_ICR);
428 static ulong dmask[] = {
429 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8,
430 0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80,
431 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
432 0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000,
433 0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000,
434 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
435 0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000,
436 0xF0000000, 0xE0000000, 0xC0000000, 0x80000000
440 void zoran_built_overlay(struct zoran* ztv, int count, struct video_clip *vcp)
443 int ystep = (ztv->vidXshift + ztv->vidWidth+31)/32; /* next DWORD */
446 DEBUG(printk(KERN_DEBUG " overlay at %p, ystep=%d, clips=%d\n",ztv->overinfo.overlay,ystep,count));
448 for (i=0; i<count; i++) {
449 struct video_clip *vp = vcp+i;
451 DEBUG(printk(KERN_DEBUG " %d: clip(%d,%d,%d,%d)\n", i,vp->x,vp->y,vp->width,vp->height));
455 * activate the visible portion of the screen
456 * Note we take some shortcuts here, because we
457 * know the width can never be < 32. (I.e. a DWORD)
458 * We also assume the overlay starts somewhere in
462 int start = ztv->vidXshift;
463 ulong firstd = dmask[start];
464 ulong lastd = ~dmask[(start + ztv->overinfo.w) & 31];
465 mtop = ztv->overinfo.overlay;
466 for (i=0; i<ztv->overinfo.h; i++) {
467 int w = ztv->vidWidth;
473 memset(line, ~0, w/8);
480 /* process clipping regions */
481 for (i=0; i<count; i++) {
483 if (vcp->x < 0 || (uint)vcp->x > ztv->overinfo.w ||
484 vcp->y < 0 || vcp->y > ztv->overinfo.h ||
485 vcp->width < 0 || (uint)(vcp->x+vcp->width) > ztv->overinfo.w ||
486 vcp->height < 0 || (vcp->y+vcp->height) > ztv->overinfo.h)
488 DEBUG(printk(CARD_DEBUG "invalid clipzone (%d,%d,%d,%d) not in (0,0,%d,%d), adapting\n",CARD,vcp->x,vcp->y,vcp->width,vcp->height,ztv->overinfo.w,ztv->overinfo.h));
489 if (vcp->x < 0) vcp->x = 0;
490 if ((uint)vcp->x > ztv->overinfo.w) vcp->x = ztv->overinfo.w;
491 if (vcp->y < 0) vcp->y = 0;
492 if (vcp->y > ztv->overinfo.h) vcp->y = ztv->overinfo.h;
493 if (vcp->width < 0) vcp->width = 0;
494 if ((uint)(vcp->x+vcp->width) > ztv->overinfo.w) vcp->width = ztv->overinfo.w - vcp->x;
495 if (vcp->height < 0) vcp->height = 0;
496 if (vcp->y+vcp->height > ztv->overinfo.h) vcp->height = ztv->overinfo.h - vcp->y;
500 mtop = &ztv->overinfo.overlay[vcp->y*ystep];
501 for (h=0; h<=vcp->height; h++) {
503 int x = ztv->vidXshift + vcp->x;
504 for (w=0; w<=vcp->width; w++) {
505 clear_bit(x&31, &mtop[x/32]);
513 mtop = ztv->overinfo.overlay;
514 zrwrite(virt_to_bus(mtop), ZORAN_MTOP);
515 zrwrite(virt_to_bus(mtop+ystep), ZORAN_MBOT);
516 zraor((ztv->vidInterlace*ystep)<<0,~ZORAN_OCR_MASKSTRIDE,ZORAN_OCR);
521 u16 Wt, Wa, Ht, Ha, HStart, VStart;
524 static struct tvnorm tvnorms[] = {
526 /* { 864, 720, 625, 576, 131, 21 },*/
527 /*00*/ { 864, 768, 625, 576, 81, 17 },
529 /*01*/ { 858, 720, 525, 480, 121, 10 },
531 /*02*/ { 864, 720, 625, 576, 131, 21 },
533 /*03*/ { 864, 720, 625, 576, 131, 21 },
535 /*04*/ { 858, 720, 525, 480, 121, 10 }
537 #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
540 * Program the chip for a setup as described in the vidinfo struct.
542 * Side-effects: calculates vidXshift, vidInterlace,
543 * vidHeight, vidWidth which are used in a later stage
544 * to calculate the overlay mask
546 * This is an internal function, as such it does not check the
547 * validity of the struct members... Spectaculair crashes will
548 * follow /very/ quick when you're wrong and the chip right :)
551 void zoran_set_geo(struct zoran* ztv, struct vidinfo* i)
555 int winWidth, winHeight;
556 int maxWidth, maxHeight, maxXOffset, maxYOffset;
559 DEBUG(printk(CARD_DEBUG "set_geo(rect=(%d,%d,%d,%d), norm=%d, format=%d, bpp=%d, bpl=%d, busadr=%lx, overlay=%p)\n",CARD,i->x,i->y,i->w,i->h,ztv->norm,i->format,i->bpp,i->bpl,i->busadr,i->overlay));
562 * make sure the DMA transfers are inhibited during our
563 * reprogramming of the chip
565 zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
567 maxWidth = tvnorms[ztv->norm].Wa;
568 maxHeight = tvnorms[ztv->norm].Ha/2;
569 maxXOffset = tvnorms[ztv->norm].HStart;
570 maxYOffset = tvnorms[ztv->norm].VStart;
572 /* setup vfec register (keep ExtFl,TopField and VCLKPol settings) */
573 vfec = (zrread(ZORAN_VFEC) & (ZORAN_VFEC_EXTFL|ZORAN_VFEC_TOPFIELD|ZORAN_VFEC_VCLKPOL)) |
574 (palette2fmt[i->format].mode & (ZORAN_VFEC_RGB|ZORAN_VFEC_ERRDIF|ZORAN_VFEC_LE|ZORAN_VFEC_PACK24));
577 * Set top, bottom ptrs. Since these must be DWORD aligned,
578 * possible adjust the x and the width of the window.
579 * so the endposition stay the same. The vidXshift will make
580 * sure we are not writing pixels before the requested x.
585 winWidth = -winWidth;
586 top = i->busadr + i->x*i->bpp + i->y*i->bpl;
588 ztv->vidXshift = (top & 3) / i->bpp;
589 winWidth += ztv->vidXshift;
590 DEBUG(printk(KERN_DEBUG " window-x shifted %d pixels left\n",ztv->vidXshift));
595 * bottom points to next frame but in interleaved mode we want
596 * to 'mix' the 2 frames to one capture, so 'bot' points to one
597 * (physical) line below the top line.
600 zrwrite(top,ZORAN_VTOP);
601 zrwrite(bot,ZORAN_VBOT);
604 * Make sure the winWidth is DWORD aligned too,
605 * thereby automaticly making sure the stride to the
606 * next line is DWORD aligned too (as required by spec).
608 if ((winWidth*i->bpp) & 3) {
609 DEBUG(printk(KERN_DEBUG " window-width enlarged by %d pixels\n",(winWidth*i->bpp) & 3));
610 winWidth += (winWidth*i->bpp) & 3;
613 /* determine the DispMode and stride */
614 if (i->h >= 0 && i->h <= maxHeight) {
615 /* single frame grab suffices for this height. */
616 vfec |= ZORAN_VFEC_DISPMOD;
617 ztv->vidInterlace = 0;
618 stride = i->bpl - (winWidth*i->bpp);
622 /* interleaving needed for this height */
623 ztv->vidInterlace = 1;
624 stride = i->bpl*2 - (winWidth*i->bpp);
627 if (winHeight < 0) /* can happen for VBI! */
628 winHeight = -winHeight;
630 /* safety net, sometimes bpl is too short??? */
632 DEBUG(printk(CARD_DEBUG "WARNING stride = %d\n",CARD,stride));
636 zraor((winHeight<<12)|(winWidth<<0),~(ZORAN_VDC_VIDWINHT|ZORAN_VDC_VIDWINWID), ZORAN_VDC);
637 zraor(stride<<16,~ZORAN_VSTR_DISPSTRIDE,ZORAN_VSTR);
639 /* remember vidWidth, vidHeight for overlay calculations */
640 ztv->vidWidth = winWidth;
641 ztv->vidHeight = winHeight;
642 DEBUG(printk(KERN_DEBUG " top=%08lx, bottom=%08lx\n",top,bot));
643 DEBUG(printk(KERN_DEBUG " winWidth=%d, winHeight=%d\n",winWidth,winHeight));
644 DEBUG(printk(KERN_DEBUG " maxWidth=%d, maxHeight=%d\n",maxWidth,maxHeight));
645 DEBUG(printk(KERN_DEBUG " stride=%d\n",stride));
648 * determine horizontal scales and crops
652 int Hend = Hstart + winWidth;
653 DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Hstart, Hend));
654 zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
658 int X = (winWidth*64+Wa-1)/Wa;
659 int We = winWidth*64/X;
661 int hcrop1 = 2*(Wa-We)/4;
663 * BUGFIX: Juha Nurmela <junki@qn-lpr2-165.quicknet.inet.fi>
664 * found the solution to the color phase shift.
665 * See ChangeLog for the full explanation)
667 int Hstart = (maxXOffset + hcrop1) | 1;
668 int Hend = Hstart + We - 1;
670 DEBUG(printk(KERN_DEBUG " X: scale=%d, start=%d, end=%d\n", HorDcm, Hstart, Hend));
672 zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
676 vfec |= ZORAN_VFEC_HFILTER_1; /* no filter */
678 vfec |= ZORAN_VFEC_HFILTER_3; /* 3 tap filter */
680 vfec |= ZORAN_VFEC_HFILTER_4; /* 4 tap filter */
681 else vfec |= ZORAN_VFEC_HFILTER_5; /* 5 tap filter */
685 * Determine vertical scales and crops
687 * when height is negative, we want to read starting at line 0
688 * One day someone might need access to these lines...
692 int Vend = Vstart + winHeight;
693 DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Vstart, Vend));
694 zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
698 int Y = (winHeight*64+Ha-1)/Ha;
699 int He = winHeight*64/Y;
701 int vcrop1 = 2*(Ha-He)/4;
702 int Vstart = maxYOffset + vcrop1;
703 int Vend = Vstart + He - 1;
705 DEBUG(printk(KERN_DEBUG " Y: scale=%d, start=%d, end=%d\n", VerDcm, Vstart, Vend));
706 zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
710 DEBUG(printk(KERN_DEBUG " F: format=%d(=%s)\n",i->format,palette2fmt[i->format].name));
712 /* setup the requested format */
713 zrwrite(vfec, ZORAN_VFEC);
717 void zoran_common_open(struct zoran* ztv, int flags)
721 /* already opened? */
722 if (ztv->users++ != 0)
730 /* setup the encoder to the initial values */
731 ztv->picture.colour=254<<7;
732 ztv->picture.brightness=128<<8;
733 ztv->picture.hue=128<<8;
734 ztv->picture.contrast=216<<7;
735 i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &ztv->picture);
737 /* default to the composite input since my camera is there */
738 zoran_muxsel(ztv, 0, VIDEO_MODE_PAL);
742 void zoran_common_close(struct zoran* ztv)
744 if (--ztv->users != 0)
755 * Open a zoran card. Right now the flags are just a hack
757 static int zoran_open(struct video_device *dev, int flags)
759 struct zoran *ztv = (struct zoran*)dev;
760 struct vidinfo* item;
763 DEBUG(printk(CARD_DEBUG "open(dev,%d)\n",CARD,flags));
765 /*********************************************
766 * We really should be doing lazy allocing...
767 *********************************************/
768 /* allocate a frame buffer */
770 ztv->fbuffer = bmalloc(ZORAN_MAX_FBUFSIZE);
772 /* could not get a buffer, bail out */
775 /* at this time we _always_ have a framebuffer */
776 memset(ztv->fbuffer,0,ZORAN_MAX_FBUFSIZE);
778 if (!ztv->overinfo.overlay)
779 ztv->overinfo.overlay = kmalloc(1024*1024/8, GFP_KERNEL);
780 if (!ztv->overinfo.overlay) {
781 /* could not get an overlay buffer, bail out */
782 bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
785 /* at this time we _always_ have a overlay */
787 /* clear buffer status, and give them a DMAable address */
789 for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
791 item->status = FBUFFER_FREE;
793 item->busadr = virt_to_bus(pos);
794 pos += ZORAN_MAX_FBUFFER;
797 /* do the common part of all open's */
798 zoran_common_open(ztv, flags);
804 void zoran_close(struct video_device* dev)
806 struct zoran *ztv = (struct zoran*)dev;
808 DEBUG(printk(CARD_DEBUG "close(dev)\n",CARD));
810 /* driver specific closure */
811 clear_bit(STATE_OVERLAY, &ztv->state);
813 zoran_common_close(ztv);
816 * This is sucky but right now I can't find a good way to
817 * be sure its safe to free the buffer. We wait 5-6 fields
818 * which is more than sufficient to be sure.
820 msleep(100); /* Wait 1/10th of a second */
822 /* free the allocated framebuffer */
823 bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
825 kfree(ztv->overinfo.overlay);
826 ztv->overinfo.overlay = 0;
831 * This read function could be used reentrant in a SMP situation.
833 * This is made possible by the spinlock which is kept till we
834 * found and marked a buffer for our own use. The lock must
835 * be released as soon as possible to prevent lock contention.
838 long zoran_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
840 struct zoran *ztv = (struct zoran*)dev;
842 struct vidinfo* unused = 0;
843 struct vidinfo* done = 0;
845 DEBUG(printk(CARD_DEBUG "zoran_read(%p,%ld,%d)\n",CARD,buf,count,nonblock));
847 /* find ourself a free or completed buffer */
849 struct vidinfo* item;
851 write_lock_irq(&ztv->lock);
852 for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
854 if (!unused && item->status == FBUFFER_FREE)
856 if (!done && item->status == FBUFFER_DONE)
862 /* no more free buffers, wait for them. */
863 write_unlock_irq(&ztv->lock);
866 interruptible_sleep_on(&ztv->grabq);
867 if (signal_pending(current))
871 /* Do we have 'ready' data? */
873 /* no? than this will take a while... */
875 write_unlock_irq(&ztv->lock);
879 /* mark the unused buffer as wanted */
880 unused->status = FBUFFER_BUSY;
883 unused->format = VIDEO_PALETTE_RGB24;
884 unused->bpp = palette2fmt[unused->format].bpp;
885 unused->bpl = unused->w * unused->bpp;
887 { /* add to tail of queue */
888 struct vidinfo* oldframe = ztv->workqueue;
889 if (!oldframe) ztv->workqueue = unused;
891 while (oldframe->next) oldframe = oldframe->next;
892 oldframe->next = unused;
895 write_unlock_irq(&ztv->lock);
897 /* tell the state machine we want it filled /NOW/ */
900 /* wait till this buffer gets grabbed */
901 wait_event_interruptible(ztv->grabq,
902 (unused->status != FBUFFER_BUSY));
903 /* see if a signal did it */
904 if (signal_pending(current))
909 write_unlock_irq(&ztv->lock);
911 /* Yes! we got data! */
912 max = done->bpl * done->h;
915 if (copy_to_user((void*)buf, done->memadr, count))
918 /* keep the engine running */
919 done->status = FBUFFER_FREE;
922 /* tell listeners this buffer became free */
923 wake_up_interruptible(&ztv->grabq);
926 DEBUG(printk(CARD_DEBUG "zoran_read() returns %lu\n",CARD,count));
931 long zoran_write(struct video_device* dev, const char* buf, unsigned long count, int nonblock)
933 struct zoran *ztv = (struct zoran *)dev;
934 UNUSED(ztv); UNUSED(dev); UNUSED(buf); UNUSED(count); UNUSED(nonblock);
935 DEBUG(printk(CARD_DEBUG "zoran_write\n",CARD));
940 unsigned int zoran_poll(struct video_device *dev, struct file *file, poll_table *wait)
942 struct zoran *ztv = (struct zoran *)dev;
943 struct vidinfo* item;
944 unsigned int mask = 0;
946 poll_wait(file, &ztv->grabq, wait);
948 for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
949 if (item->status == FBUFFER_DONE)
951 mask |= (POLLIN | POLLRDNORM);
955 DEBUG(printk(CARD_DEBUG "zoran_poll()=%x\n",CARD,mask));
960 /* append a new clipregion to the vector of video_clips */
962 void new_clip(struct video_window* vw, struct video_clip* vcp, int x, int y, int w, int h)
964 vcp[vw->clipcount].x = x;
965 vcp[vw->clipcount].y = y;
966 vcp[vw->clipcount].width = w;
967 vcp[vw->clipcount].height = h;
972 int zoran_ioctl(struct video_device* dev, unsigned int cmd, void *arg)
974 struct zoran* ztv = (struct zoran*)dev;
979 struct video_capability c;
980 DEBUG(printk(CARD_DEBUG "VIDIOCGCAP\n",CARD));
982 strcpy(c.name,ztv->video_dev.name);
983 c.type = VID_TYPE_CAPTURE|
989 c.type |= VID_TYPE_TUNER;
990 if (ztv->have_decoder) {
991 c.channels = ztv->card->video_inputs;
992 c.audios = ztv->card->audio_inputs;
994 /* no decoder -> no channels */
995 c.channels = c.audios = 0;
1000 if (copy_to_user(arg,&c,sizeof(c)))
1007 struct video_channel v;
1009 if (copy_from_user(&v, arg,sizeof(v)))
1011 DEBUG(printk(CARD_DEBUG "VIDIOCGCHAN(%d)\n",CARD,v.channel));
1012 v.flags=VIDEO_VC_AUDIO
1013 #ifdef VIDEO_VC_NORM
1018 v.type=VIDEO_TYPE_CAMERA;
1019 #ifdef I_EXPECT_POSSIBLE_NORMS_IN_THE_API
1020 v.norm=VIDEO_MODE_PAL|
1024 v.norm=VIDEO_MODE_PAL;
1026 /* too many inputs? no decoder -> no channels */
1027 if (!ztv->have_decoder || v.channel < 0 || v.channel >= ztv->card->video_inputs)
1030 /* now determine the name of the channel */
1031 mux = ztv->card->video_mux[v.channel];
1032 if (mux & IS_TUNER) {
1033 /* lets assume only one tuner, yes? */
1034 strcpy(v.name,"Television");
1035 v.type = VIDEO_TYPE_TV;
1036 if (ztv->have_tuner) {
1037 v.flags |= VIDEO_VC_TUNER;
1041 else if (mux & IS_SVHS)
1042 sprintf(v.name,"S-Video-%d",v.channel);
1044 sprintf(v.name,"CVBS-%d",v.channel);
1046 if (copy_to_user(arg,&v,sizeof(v)))
1051 { /* set video channel */
1052 struct video_channel v;
1053 if (copy_from_user(&v, arg,sizeof(v)))
1055 DEBUG(printk(CARD_DEBUG "VIDIOCSCHAN(%d,%d)\n",CARD,v.channel,v.norm));
1057 /* too many inputs? no decoder -> no channels */
1058 if (!ztv->have_decoder || v.channel >= ztv->card->video_inputs || v.channel < 0)
1061 if (v.norm != VIDEO_MODE_PAL &&
1062 v.norm != VIDEO_MODE_NTSC &&
1063 v.norm != VIDEO_MODE_SECAM &&
1064 v.norm != VIDEO_MODE_AUTO)
1067 /* make it happen, nr1! */
1068 return zoran_muxsel(ztv,v.channel,v.norm);
1073 struct video_tuner v;
1074 if (copy_from_user(&v, arg,sizeof(v)))
1076 DEBUG(printk(CARD_DEBUG "VIDIOCGTUNER(%d)\n",CARD,v.tuner));
1078 /* Only no or one tuner for now */
1079 if (!ztv->have_tuner || v.tuner)
1082 strcpy(v.name,"Television");
1085 v.flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1087 v.signal = 0xFFFF; /* unknown */
1089 if (copy_to_user(arg,&v,sizeof(v)))
1095 struct video_tuner v;
1096 if (copy_from_user(&v, arg, sizeof(v)))
1098 DEBUG(printk(CARD_DEBUG "VIDIOCSTUNER(%d,%d)\n",CARD,v.tuner,v.mode));
1100 /* Only no or one tuner for now */
1101 if (!ztv->have_tuner || v.tuner)
1104 /* and it only has certain valid modes */
1105 if( v.mode != VIDEO_MODE_PAL &&
1106 v.mode != VIDEO_MODE_NTSC &&
1107 v.mode != VIDEO_MODE_SECAM)
1111 return zoran_muxsel(ztv,v.tuner,v.mode);
1116 struct video_picture p = ztv->picture;
1117 DEBUG(printk(CARD_DEBUG "VIDIOCGPICT\n",CARD));
1118 p.depth = ztv->depth;
1120 case 8: p.palette=VIDEO_PALETTE_YUV422;
1122 case 15: p.palette=VIDEO_PALETTE_RGB555;
1124 case 16: p.palette=VIDEO_PALETTE_RGB565;
1126 case 24: p.palette=VIDEO_PALETTE_RGB24;
1128 case 32: p.palette=VIDEO_PALETTE_RGB32;
1131 if (copy_to_user(arg, &p, sizeof(p)))
1137 struct video_picture p;
1138 if (copy_from_user(&p, arg,sizeof(p)))
1140 DEBUG(printk(CARD_DEBUG "VIDIOCSPICT(%d,%d,%d,%d,%d,%d,%d)\n",CARD,p.brightness,p.hue,p.colour,p.contrast,p.whiteness,p.depth,p.palette));
1142 /* depth must match with framebuffer */
1143 if (p.depth != ztv->depth)
1146 /* check if palette matches this bpp */
1147 if (p.palette>NRPALETTES ||
1148 palette2fmt[p.palette].bpp != ztv->overinfo.bpp)
1151 write_lock_irq(&ztv->lock);
1152 ztv->overinfo.format = p.palette;
1154 write_unlock_irq(&ztv->lock);
1156 /* tell the decoder */
1157 i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
1163 struct video_window vw;
1164 DEBUG(printk(CARD_DEBUG "VIDIOCGWIN\n",CARD));
1165 read_lock(&ztv->lock);
1166 vw.x = ztv->overinfo.x;
1167 vw.y = ztv->overinfo.y;
1168 vw.width = ztv->overinfo.w;
1169 vw.height = ztv->overinfo.h;
1172 if (ztv->vidInterlace)
1173 vw.flags|=VIDEO_WINDOW_INTERLACE;
1174 read_unlock(&ztv->lock);
1175 if (copy_to_user(arg,&vw,sizeof(vw)))
1181 struct video_window vw;
1182 struct video_clip *vcp;
1184 if (copy_from_user(&vw,arg,sizeof(vw)))
1186 DEBUG(printk(CARD_DEBUG "VIDIOCSWIN(%d,%d,%d,%d,%x,%d)\n",CARD,vw.x,vw.y,vw.width,vw.height,vw.flags,vw.clipcount));
1191 if (vw.clipcount <0 || vw.clipcount>256)
1192 return -EDOM; /* Too many! */
1197 vcp = vmalloc(sizeof(struct video_clip)*(vw.clipcount+4));
1200 if (vw.clipcount && copy_from_user(vcp,vw.clips,sizeof(struct video_clip)*vw.clipcount)) {
1210 * strange, it seems xawtv sometimes calls us with 0
1211 * width and/or height. Ignore these values
1214 vw.x = ztv->overinfo.x;
1216 vw.y = ztv->overinfo.y;
1218 /* by now we are committed to the new data... */
1219 write_lock_irq(&ztv->lock);
1220 ztv->overinfo.x = vw.x;
1221 ztv->overinfo.y = vw.y;
1222 ztv->overinfo.w = vw.width;
1223 ztv->overinfo.h = vw.height;
1224 write_unlock_irq(&ztv->lock);
1227 * Impose display clips
1229 if (vw.x+vw.width > ztv->swidth)
1230 new_clip(&vw, vcp, ztv->swidth-vw.x, 0, vw.width-1, vw.height-1);
1231 if (vw.y+vw.height > ztv->sheight)
1232 new_clip(&vw, vcp, 0, ztv->sheight-vw.y, vw.width-1, vw.height-1);
1234 /* built the requested clipping zones */
1235 zoran_set_geo(ztv, &ztv->overinfo);
1236 zoran_built_overlay(ztv, vw.clipcount, vcp);
1239 /* if we were on, restart the video engine */
1248 if (get_user(v, (int *)arg))
1250 DEBUG(printk(CARD_DEBUG "VIDIOCCAPTURE(%d)\n",CARD,v));
1253 clear_bit(STATE_OVERLAY, &ztv->state);
1257 /* is VIDIOCSFBUF, VIDIOCSWIN done? */
1258 if (ztv->overinfo.busadr==0 || ztv->overinfo.w==0 || ztv->overinfo.h==0)
1261 set_bit(STATE_OVERLAY, &ztv->state);
1269 struct video_buffer v;
1270 DEBUG(printk(CARD_DEBUG "VIDIOCGFBUF\n",CARD));
1271 read_lock(&ztv->lock);
1272 v.base = (void *)ztv->overinfo.busadr;
1273 v.height = ztv->sheight;
1274 v.width = ztv->swidth;
1275 v.depth = ztv->depth;
1276 v.bytesperline = ztv->overinfo.bpl;
1277 read_unlock(&ztv->lock);
1278 if(copy_to_user(arg, &v,sizeof(v)))
1284 struct video_buffer v;
1285 if(!capable(CAP_SYS_ADMIN))
1287 if (copy_from_user(&v, arg,sizeof(v)))
1289 DEBUG(printk(CARD_DEBUG "VIDIOCSFBUF(%p,%d,%d,%d,%d)\n",CARD,v.base, v.width,v.height,v.depth,v.bytesperline));
1291 if (v.depth!=15 && v.depth!=16 && v.depth!=24 && v.depth!=32)
1293 if (v.bytesperline<1)
1297 write_lock_irq(&ztv->lock);
1298 ztv->overinfo.busadr = (ulong)v.base;
1299 ztv->sheight = v.height;
1300 ztv->swidth = v.width;
1301 ztv->depth = v.depth; /* bits per pixel */
1302 ztv->overinfo.bpp = ((v.depth+1)&0x38)/8;/* bytes per pixel */
1303 ztv->overinfo.bpl = v.bytesperline; /* bytes per line */
1304 write_unlock_irq(&ztv->lock);
1310 /* Will be handled higher up .. */
1317 if (get_user(i, (int *) arg))
1319 DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d)\n",CARD,i));
1320 if (i<0 || i>ZORAN_MAX_FBUFFERS)
1322 switch (ztv->grabinfo[i].status) {
1326 /* wait till this buffer gets grabbed */
1327 wait_event_interruptible(ztv->grabq,
1328 (ztv->grabinfo[i].status != FBUFFER_BUSY));
1329 /* see if a signal did it */
1330 if (signal_pending(current))
1332 /* don't fall through; a DONE buffer is not UNUSED */
1335 ztv->grabinfo[i].status = FBUFFER_FREE;
1336 /* tell ppl we have a spare buffer */
1337 wake_up_interruptible(&ztv->grabq);
1340 DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d) returns\n",CARD,i));
1344 case VIDIOCMCAPTURE:
1346 struct video_mmap vm;
1347 struct vidinfo* frame;
1348 if (copy_from_user(&vm,arg,sizeof(vm)))
1350 DEBUG(printk(CARD_DEBUG "VIDIOCMCAPTURE(%d,(%d,%d),%d)\n",CARD,vm.frame,vm.width,vm.height,vm.format));
1351 if (vm.frame<0 || vm.frame>ZORAN_MAX_FBUFFERS ||
1352 vm.width<32 || vm.width>768 ||
1353 vm.height<32 || vm.height>576 ||
1354 vm.format>NRPALETTES ||
1355 palette2fmt[vm.format].mode == 0)
1358 /* we are allowed to take over UNUSED and DONE buffers */
1359 frame = &ztv->grabinfo[vm.frame];
1360 if (frame->status == FBUFFER_BUSY)
1363 /* setup the other parameters if they are given */
1364 write_lock_irq(&ztv->lock);
1365 frame->w = vm.width;
1366 frame->h = vm.height;
1367 frame->format = vm.format;
1368 frame->bpp = palette2fmt[frame->format].bpp;
1369 frame->bpl = frame->w*frame->bpp;
1370 frame->status = FBUFFER_BUSY;
1372 { /* add to tail of queue */
1373 struct vidinfo* oldframe = ztv->workqueue;
1374 if (!oldframe) ztv->workqueue = frame;
1376 while (oldframe->next) oldframe = oldframe->next;
1377 oldframe->next = frame;
1380 write_unlock_irq(&ztv->lock);
1387 struct video_mbuf mb;
1389 DEBUG(printk(CARD_DEBUG "VIDIOCGMBUF\n",CARD));
1390 mb.size = ZORAN_MAX_FBUFSIZE;
1391 mb.frames = ZORAN_MAX_FBUFFERS;
1392 for (i=0; i<ZORAN_MAX_FBUFFERS; i++)
1393 mb.offsets[i] = i*ZORAN_MAX_FBUFFER;
1394 if(copy_to_user(arg, &mb,sizeof(mb)))
1401 struct video_unit vu;
1402 DEBUG(printk(CARD_DEBUG "VIDIOCGUNIT\n",CARD));
1403 vu.video = ztv->video_dev.minor;
1404 vu.vbi = ztv->vbi_dev.minor;
1405 vu.radio = VIDEO_NO_UNIT;
1406 vu.audio = VIDEO_NO_UNIT;
1407 vu.teletext = VIDEO_NO_UNIT;
1408 if(copy_to_user(arg, &vu,sizeof(vu)))
1415 unsigned long v = ztv->tuner_freq;
1416 if (copy_to_user(arg,&v,sizeof(v)))
1418 DEBUG(printk(CARD_DEBUG "VIDIOCGFREQ\n",CARD));
1424 if (copy_from_user(&v, arg, sizeof(v)))
1426 DEBUG(printk(CARD_DEBUG "VIDIOCSFREQ\n",CARD));
1428 if (ztv->have_tuner) {
1430 if (i2c_control_device(&(ztv->i2c), I2C_DRIVERID_TUNER, TUNER_SET_TVFREQ, &fixme) < 0)
1433 ztv->tuner_freq = v;
1437 /* Why isn't this in the API?
1438 * And why doesn't it take a buffer number?
1441 unsigned long v = ztv->lastfieldnr;
1442 if (copy_to_user(arg,&v,sizeof(v)))
1444 DEBUG(printk(CARD_DEBUG "BTTV_FIELDNR\n",CARD));
1450 return -ENOIOCTLCMD;
1456 int zoran_mmap(struct vm_area_struct *vma, struct video_device* dev, const char* adr, unsigned long size)
1458 struct zoran* ztv = (struct zoran*)dev;
1459 unsigned long start = (unsigned long)adr;
1462 DEBUG(printk(CARD_DEBUG "zoran_mmap(0x%p,%ld)\n",CARD,adr,size));
1465 if (size > ZORAN_MAX_FBUFSIZE || !ztv->fbuffer)
1468 /* start mapping the whole shabang to user memory */
1469 pos = (unsigned long)ztv->fbuffer;
1471 unsigned long pfn = virt_to_phys((void*)pos) >> PAGE_SHIFT;
1472 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
1481 static struct video_device zr36120_template=
1483 .owner = THIS_MODULE,
1485 .type = VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY,
1486 .hardware = VID_HARDWARE_ZR36120,
1488 .close = zoran_close,
1490 .write = zoran_write,
1492 .ioctl = zoran_ioctl,
1493 .compat_ioctl = v4l_compat_ioctl32,
1499 int vbi_open(struct video_device *dev, int flags)
1501 struct zoran *ztv = dev->priv;
1502 struct vidinfo* item;
1504 DEBUG(printk(CARD_DEBUG "vbi_open(dev,%d)\n",CARD,flags));
1507 * During VBI device open, we continiously grab VBI-like
1508 * data in the vbi buffer when we have nothing to do.
1509 * Only when there is an explicit request for VBI data
1510 * (read call) we /force/ a read.
1513 /* allocate buffers */
1514 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1516 item->status = FBUFFER_FREE;
1519 if (!item->memadr) {
1520 item->memadr = bmalloc(ZORAN_VBI_BUFSIZE);
1521 if (!item->memadr) {
1522 /* could not get a buffer, bail out */
1523 while (item != ztv->readinfo) {
1525 bfree(item->memadr, ZORAN_VBI_BUFSIZE);
1533 /* determine the DMAable address */
1534 item->busadr = virt_to_bus(item->memadr);
1537 /* do the common part of all open's */
1538 zoran_common_open(ztv, flags);
1540 set_bit(STATE_VBI, &ztv->state);
1541 /* start read-ahead */
1548 void vbi_close(struct video_device *dev)
1550 struct zoran *ztv = dev->priv;
1551 struct vidinfo* item;
1553 DEBUG(printk(CARD_DEBUG "vbi_close(dev)\n",CARD));
1555 /* driver specific closure */
1556 clear_bit(STATE_VBI, &ztv->state);
1558 zoran_common_close(ztv);
1561 * This is sucky but right now I can't find a good way to
1562 * be sure its safe to free the buffer. We wait 5-6 fields
1563 * which is more than sufficient to be sure.
1565 msleep(100); /* Wait 1/10th of a second */
1567 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1570 bfree(item->memadr, ZORAN_VBI_BUFSIZE);
1577 * This read function could be used reentrant in a SMP situation.
1579 * This is made possible by the spinlock which is kept till we
1580 * found and marked a buffer for our own use. The lock must
1581 * be released as soon as possible to prevent lock contention.
1584 long vbi_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
1586 struct zoran *ztv = dev->priv;
1588 struct vidinfo* unused = 0;
1589 struct vidinfo* done = 0;
1591 DEBUG(printk(CARD_DEBUG "vbi_read(0x%p,%ld,%d)\n",CARD,buf,count,nonblock));
1593 /* find ourself a free or completed buffer */
1595 struct vidinfo* item;
1597 write_lock_irq(&ztv->lock);
1598 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++) {
1599 if (!unused && item->status == FBUFFER_FREE)
1601 if (!done && item->status == FBUFFER_DONE)
1607 /* no more free buffers, wait for them. */
1608 write_unlock_irq(&ztv->lock);
1610 return -EWOULDBLOCK;
1611 interruptible_sleep_on(&ztv->vbiq);
1612 if (signal_pending(current))
1616 /* Do we have 'ready' data? */
1618 /* no? than this will take a while... */
1620 write_unlock_irq(&ztv->lock);
1621 return -EWOULDBLOCK;
1624 /* mark the unused buffer as wanted */
1625 unused->status = FBUFFER_BUSY;
1627 { /* add to tail of queue */
1628 struct vidinfo* oldframe = ztv->workqueue;
1629 if (!oldframe) ztv->workqueue = unused;
1631 while (oldframe->next) oldframe = oldframe->next;
1632 oldframe->next = unused;
1635 write_unlock_irq(&ztv->lock);
1637 /* tell the state machine we want it filled /NOW/ */
1640 /* wait till this buffer gets grabbed */
1641 wait_event_interruptible(ztv->vbiq,
1642 (unused->status != FBUFFER_BUSY));
1643 /* see if a signal did it */
1644 if (signal_pending(current))
1649 write_unlock_irq(&ztv->lock);
1651 /* Yes! we got data! */
1652 max = done->bpl * -done->h;
1656 /* check if the user gave us enough room to write the data */
1657 if (!access_ok(VERIFY_WRITE, buf, count)) {
1663 * Now transform/strip the data from YUV to Y-only
1664 * NB. Assume the Y is in the LSB of the YUV data.
1667 unsigned char* optr = buf;
1668 unsigned char* eptr = buf+count;
1670 /* are we beeing accessed from an old driver? */
1671 if (count == 2*19*2048) {
1673 * Extreme HACK, old VBI programs expect 2048 points
1674 * of data, and we only got 864 orso. Double each
1675 * datapoint and clear the rest of the line.
1676 * This way we have appear to have a
1677 * sample_frequency of 29.5 Mc.
1680 unsigned char* iptr = done->memadr+1;
1681 for (y=done->h; optr<eptr && y<0; y++)
1683 /* copy to doubled data to userland */
1684 for (x=0; optr+1<eptr && x<-done->w; x++)
1686 unsigned char a = iptr[x*2];
1687 __put_user(a, optr++);
1688 __put_user(a, optr++);
1690 /* and clear the rest of the line */
1691 for (x*=2; optr<eptr && x<done->bpl; x++)
1692 __put_user(0, optr++);
1699 * Other (probably newer) programs asked
1700 * us what geometry we are using, and are
1701 * reading the correct size.
1704 unsigned char* iptr = done->memadr+1;
1705 for (y=done->h; optr<eptr && y<0; y++)
1707 /* copy to doubled data to userland */
1708 for (x=0; optr<eptr && x<-done->w; x++)
1709 __put_user(iptr[x*2], optr++);
1710 /* and clear the rest of the line */
1711 for (;optr<eptr && x<done->bpl; x++)
1712 __put_user(0, optr++);
1719 * place the framenumber (half fieldnr) in the last long
1721 __put_user(done->fieldnr/2, ((ulong*)eptr)[-1]);
1724 /* keep the engine running */
1725 done->status = FBUFFER_FREE;
1728 /* tell listeners this buffer just became free */
1729 wake_up_interruptible(&ztv->vbiq);
1733 DEBUG(printk(CARD_DEBUG "vbi_read() returns %lu\n",CARD,count));
1738 unsigned int vbi_poll(struct video_device *dev, struct file *file, poll_table *wait)
1740 struct zoran *ztv = dev->priv;
1741 struct vidinfo* item;
1742 unsigned int mask = 0;
1744 poll_wait(file, &ztv->vbiq, wait);
1746 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1747 if (item->status == FBUFFER_DONE)
1749 mask |= (POLLIN | POLLRDNORM);
1753 DEBUG(printk(CARD_DEBUG "vbi_poll()=%x\n",CARD,mask));
1759 int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
1761 struct zoran* ztv = dev->priv;
1766 struct vbi_format f;
1767 DEBUG(printk(CARD_DEBUG "VIDIOCGVBIINFO\n",CARD));
1768 f.sampling_rate = 14750000UL;
1769 f.samples_per_line = -ztv->readinfo[0].w;
1770 f.sample_format = VIDEO_PALETTE_RAW;
1771 f.start[0] = f.start[1] = ztv->readinfo[0].y;
1773 f.count[0] = f.count[1] = -ztv->readinfo[0].h;
1774 f.flags = VBI_INTERLACED;
1775 if (copy_to_user(arg,&f,sizeof(f)))
1781 struct vbi_format f;
1783 if (copy_from_user(&f, arg,sizeof(f)))
1785 DEBUG(printk(CARD_DEBUG "VIDIOCSVBIINFO(%d,%d,%d,%d,%d,%d,%d,%x)\n",CARD,f.sampling_rate,f.samples_per_line,f.sample_format,f.start[0],f.start[1],f.count[0],f.count[1],f.flags));
1787 /* lots of parameters are fixed... (PAL) */
1788 if (f.sampling_rate != 14750000UL ||
1789 f.samples_per_line > 864 ||
1790 f.sample_format != VIDEO_PALETTE_RAW ||
1792 f.start[0] != f.start[1]-312 ||
1793 f.count[0] != f.count[1] ||
1794 f.start[0]+f.count[0] >= 288 ||
1795 f.flags != VBI_INTERLACED)
1798 write_lock_irq(&ztv->lock);
1799 ztv->readinfo[0].y = f.start[0];
1800 ztv->readinfo[0].w = -f.samples_per_line;
1801 ztv->readinfo[0].h = -f.count[0];
1802 ztv->readinfo[0].bpl = f.samples_per_line*ztv->readinfo[0].bpp;
1803 for (i=1; i<ZORAN_VBI_BUFFERS; i++)
1804 ztv->readinfo[i] = ztv->readinfo[i];
1805 write_unlock_irq(&ztv->lock);
1809 return -ENOIOCTLCMD;
1814 static struct video_device vbi_template=
1816 .owner = THIS_MODULE,
1818 .type = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
1819 .hardware = VID_HARDWARE_ZR36120,
1823 .write = zoran_write,
1830 * Scan for a Zoran chip, request the irq and map the io memory
1833 int __init find_zoran(void)
1837 struct pci_dev *dev = NULL;
1838 unsigned char revision;
1841 while ((dev = pci_find_device(PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120, dev)))
1843 /* Ok, a ZR36120/ZR36125 found! */
1844 ztv = &zorans[zoran_num];
1847 if (pci_enable_device(dev))
1850 pci_read_config_byte(dev, PCI_CLASS_REVISION, &revision);
1851 printk(KERN_INFO "zoran: Zoran %x (rev %d) ",
1852 dev->device, revision);
1853 printk("bus: %d, devfn: %d, irq: %d, ",
1854 dev->bus->number, dev->devfn, dev->irq);
1855 printk("memory: 0x%08lx.\n", ztv->zoran_adr);
1857 ztv->zoran_mem = ioremap(ztv->zoran_adr, 0x1000);
1858 DEBUG(printk(KERN_DEBUG "zoran: mapped-memory at 0x%p\n",ztv->zoran_mem));
1860 result = request_irq(dev->irq, zoran_irq,
1861 SA_SHIRQ|SA_INTERRUPT,"zoran", ztv);
1862 if (result==-EINVAL)
1864 iounmap(ztv->zoran_mem);
1865 printk(KERN_ERR "zoran: Bad irq number or handler\n");
1869 printk(KERN_ERR "zoran: IRQ %d busy, change your PnP config in BIOS\n",dev->irq);
1871 iounmap(ztv->zoran_mem);
1874 /* Enable bus-mastering */
1875 pci_set_master(dev);
1880 printk(KERN_INFO "zoran: %d Zoran card(s) found.\n",zoran_num);
1885 int __init init_zoran(int card)
1887 struct zoran *ztv = &zorans[card];
1890 /* if the given cardtype valid? */
1891 if (cardtype[card]>=NRTVCARDS) {
1892 printk(KERN_INFO "invalid cardtype(%d) detected\n",cardtype[card]);
1896 /* reset the zoran */
1897 zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
1899 zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
1902 /* zoran chip specific details */
1903 ztv->card = tvcards+cardtype[card]; /* point to the selected card */
1904 ztv->norm = 0; /* PAL */
1905 ztv->tuner_freq = 0;
1907 /* videocard details */
1914 ztv->overinfo.kindof = FBUFFER_OVERLAY;
1915 ztv->overinfo.status = FBUFFER_FREE;
1916 ztv->overinfo.x = 0;
1917 ztv->overinfo.y = 0;
1918 ztv->overinfo.w = 768; /* 640 */
1919 ztv->overinfo.h = 576; /* 480 */
1920 ztv->overinfo.format = VIDEO_PALETTE_RGB565;
1921 ztv->overinfo.bpp = palette2fmt[ztv->overinfo.format].bpp;
1922 ztv->overinfo.bpl = ztv->overinfo.bpp*ztv->swidth;
1923 ztv->overinfo.busadr = 0;
1924 ztv->overinfo.memadr = 0;
1925 ztv->overinfo.overlay = 0;
1926 for (i=0; i<ZORAN_MAX_FBUFFERS; i++) {
1927 ztv->grabinfo[i] = ztv->overinfo;
1928 ztv->grabinfo[i].kindof = FBUFFER_GRAB;
1930 init_waitqueue_head(&ztv->grabq);
1933 ztv->readinfo[0] = ztv->overinfo;
1934 ztv->readinfo[0].kindof = FBUFFER_VBI;
1935 ztv->readinfo[0].w = -864;
1936 ztv->readinfo[0].h = -38;
1937 ztv->readinfo[0].format = VIDEO_PALETTE_YUV422;
1938 ztv->readinfo[0].bpp = palette2fmt[ztv->readinfo[0].format].bpp;
1939 ztv->readinfo[0].bpl = 1024*ztv->readinfo[0].bpp;
1940 for (i=1; i<ZORAN_VBI_BUFFERS; i++)
1941 ztv->readinfo[i] = ztv->readinfo[0];
1942 init_waitqueue_head(&ztv->vbiq);
1944 /* maintenance data */
1945 ztv->have_decoder = 0;
1946 ztv->have_tuner = 0;
1947 ztv->tuner_type = 0;
1950 rwlock_init(&ztv->lock);
1953 ztv->lastfieldnr = 0;
1956 zrand(~ZORAN_VDC_TRICOM, ZORAN_VDC);
1958 /* external FL determines TOP frame */
1959 zror(ZORAN_VFEC_EXTFL, ZORAN_VFEC);
1962 if (ztv->card->hsync_pos)
1963 zrwrite(ZORAN_VFEH_HSPOL, ZORAN_VFEH);
1965 if (ztv->card->vsync_pos)
1966 zrwrite(ZORAN_VFEV_VSPOL, ZORAN_VFEV);
1968 /* Set the proper General Purpuse register bits */
1969 /* implicit: no softreset, 0 waitstates */
1970 zrwrite(ZORAN_PCI_SOFTRESET|(ztv->card->gpdir<<0),ZORAN_PCI);
1971 /* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
1972 zrwrite(ztv->card->gpval<<24,ZORAN_GUEST);
1974 /* clear interrupt status */
1975 zrwrite(~0, ZORAN_ISR);
1980 ztv->i2c = zoran_i2c_bus_template;
1981 sprintf(ztv->i2c.name,"zoran-%d",card);
1982 ztv->i2c.data = ztv;
1985 * Now add the template and register the device unit
1987 ztv->video_dev = zr36120_template;
1988 strcpy(ztv->video_dev.name, ztv->i2c.name);
1989 ztv->video_dev.priv = ztv;
1990 if (video_register_device(&ztv->video_dev, VFL_TYPE_GRABBER, video_nr) < 0)
1993 ztv->vbi_dev = vbi_template;
1994 strcpy(ztv->vbi_dev.name, ztv->i2c.name);
1995 ztv->vbi_dev.priv = ztv;
1996 if (video_register_device(&ztv->vbi_dev, VFL_TYPE_VBI, vbi_nr) < 0) {
1997 video_unregister_device(&ztv->video_dev);
2000 i2c_register_bus(&ztv->i2c);
2002 /* set interrupt mask - the PIN enable will be set later */
2003 zrwrite(ZORAN_ICR_GIRQ0|ZORAN_ICR_GIRQ1|ZORAN_ICR_CODE, ZORAN_ICR);
2005 printk(KERN_INFO "%s: installed %s\n",ztv->i2c.name,ztv->card->name);
2010 void release_zoran(int max)
2015 for (i=0;i<max; i++)
2019 /* turn off all capturing, DMA and IRQs */
2020 /* reset the zoran */
2021 zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
2023 zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
2026 /* first disable interrupts before unmapping the memory! */
2027 zrwrite(0, ZORAN_ICR);
2028 zrwrite(0xffffffffUL,ZORAN_ISR);
2031 free_irq(ztv->dev->irq,ztv);
2033 /* unregister i2c_bus */
2034 i2c_unregister_bus((&ztv->i2c));
2036 /* unmap and free memory */
2038 iounmap(ztv->zoran_mem);
2040 video_unregister_device(&ztv->video_dev);
2041 video_unregister_device(&ztv->vbi_dev);
2045 void __exit zr36120_exit(void)
2047 release_zoran(zoran_cards);
2050 int __init zr36120_init(void)
2055 zoran_cards = find_zoran();
2057 /* no cards found, no need for a driver */
2060 /* initialize Zorans */
2061 for (card=0; card<zoran_cards; card++) {
2062 if (init_zoran(card)<0) {
2063 /* only release the zorans we have registered */
2064 release_zoran(card);
2071 module_init(zr36120_init);
2072 module_exit(zr36120_exit);