File: /usr/src/linux/drivers/media/video/zr36120.c

1     /*
2         zr36120.c - Zoran 36120/36125 based framegrabbers
3     
4         Copyright (C) 1998-1999 Pauline Middelink <middelin@polyware.nl>
5     
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.
10     
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.
15     
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.
19     */
20     
21     #include <linux/module.h>
22     #include <linux/delay.h>
23     #include <linux/init.h>
24     #include <linux/errno.h>
25     #include <linux/fs.h>
26     #include <linux/kernel.h>
27     #include <linux/major.h>
28     #include <linux/slab.h>
29     #include <linux/vmalloc.h>
30     #include <linux/mm.h>
31     #include <linux/pci.h>
32     #include <linux/signal.h>
33     #include <asm/io.h>
34     #include <asm/pgtable.h>
35     #include <asm/page.h>
36     #include <linux/sched.h>
37     #include <linux/video_decoder.h>
38     #include <asm/segment.h>
39     
40     #include <linux/version.h>
41     #include <asm/uaccess.h>
42     
43     #include "tuner.h"
44     #include "zr36120.h"
45     #include "zr36120_mem.h"
46     
47     /* mark an required function argument unused - lintism */
48     #define	UNUSED(x)	(void)(x)
49     
50     /* sensible default */
51     #ifndef CARDTYPE
52     #define CARDTYPE 0
53     #endif
54     
55     /* Anybody who uses more than four? */
56     #define ZORAN_MAX 4
57     
58     static unsigned int triton1=0;			/* triton1 chipset? */
59     static unsigned int cardtype[ZORAN_MAX]={ [ 0 ... ZORAN_MAX-1 ] = CARDTYPE };
60     static int video_nr = -1;
61     static int vbi_nr = -1;
62     
63     MODULE_AUTHOR("Pauline Middelink <middelin@polyware.nl>");
64     MODULE_DESCRIPTION("Zoran ZR36120 based framegrabber");
65     MODULE_PARM(triton1,"i");
66     MODULE_PARM(cardtype,"1-" __MODULE_STRING(ZORAN_MAX) "i");
67     MODULE_PARM(video_nr,"i");
68     MODULE_PARM(vbi_nr,"i");
69     
70     static int zoran_cards;
71     static struct zoran zorans[ZORAN_MAX];
72     
73     /*
74      * the meaning of each element can be found in zr36120.h
75      * Determining the value of gpdir/gpval can be tricky. The
76      * best way is to run the card under the original software
77      * and read the values from the general purpose registers
78      * 0x28 and 0x2C. How you do that is left as an exercise
79      * to the impatient reader :)
80      */
81     #define T 1	/* to seperate the bools from the ints */
82     #define F 0
83     static struct tvcard tvcards[] = {
84     	/* reported working by <middelin@polyware.nl> */
85     /*0*/	{ "Trust Victor II",
86     	  2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
87     	/* reported working by <Michael.Paxton@aihw.gov.au>  */
88     /*1*/   { "Aitech WaveWatcher TV-PCI",
89     	  3, 0, T, F, T, T, 0x7F, 0x80, { 1, TUNER(3), SVHS(6) }, { 0 } },
90     	/* reported working by ? */
91     /*2*/	{ "Genius Video Wonder PCI Video Capture Card",
92     	  2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
93     	/* reported working by <Pascal.Gabriel@wanadoo.fr> */
94     /*3*/	{ "Guillemot Maxi-TV PCI",
95     	  2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
96     	/* reported working by "Craig Whitmore <lennon@igrin.co.nz> */
97     /*4*/	{ "Quadrant Buster",
98     	  3, 3, T, F, T, T, 0x7F, 0x80, { SVHS(1), TUNER(2), 3 }, { 1, 2, 3 } },
99     	/* a debug entry which has all inputs mapped */
100     /*5*/	{ "ZR36120 based framegrabber (all inputs enabled)",
101     	  6, 0, T, T, T, T, 0x7F, 0x80, { 1, 2, 3, 4, 5, 6 }, { 0 } }
102     };
103     #undef T
104     #undef F
105     #define NRTVCARDS (sizeof(tvcards)/sizeof(tvcards[0]))
106     
107     #ifdef __sparc__
108     #define	ENDIANESS	0
109     #else
110     #define	ENDIANESS	ZORAN_VFEC_LE
111     #endif
112     
113     static struct { const char name[8]; uint mode; uint bpp; } palette2fmt[] = {
114     /* n/a     */	{ "n/a",     0, 0 },
115     /* GREY    */	{ "GRAY",    0, 0 },
116     /* HI240   */	{ "HI240",   0, 0 },
117     /* RGB565  */	{ "RGB565",  ZORAN_VFEC_RGB_RGB565|ENDIANESS, 2 },
118     /* RGB24   */	{ "RGB24",   ZORAN_VFEC_RGB_RGB888|ENDIANESS|ZORAN_VFEC_PACK24, 3 },
119     /* RGB32   */	{ "RGB32",   ZORAN_VFEC_RGB_RGB888|ENDIANESS, 4 },
120     /* RGB555  */	{ "RGB555",  ZORAN_VFEC_RGB_RGB555|ENDIANESS, 2 },
121     /* YUV422  */	{ "YUV422",  ZORAN_VFEC_RGB_YUV422|ENDIANESS, 2 },
122     /* YUYV    */	{ "YUYV",    0, 0 },
123     /* UYVY    */	{ "UYVY",    0, 0 },
124     /* YUV420  */	{ "YUV420",  0, 0 },
125     /* YUV411  */	{ "YUV411",  0, 0 },
126     /* RAW     */	{ "RAW",     0, 0 },
127     /* YUV422P */	{ "YUV422P", 0, 0 },
128     /* YUV411P */	{ "YUV411P", 0, 0 }};
129     #define NRPALETTES (sizeof(palette2fmt)/sizeof(palette2fmt[0]))
130     #undef ENDIANESS
131     
132     /* ----------------------------------------------------------------------- */
133     /* ZORAN chipset detector                                                 */
134     /* shamelessly stolen from bttv.c                                         */
135     /* Reason for beeing here: we need to detect if we are running on a        */
136     /* Triton based chipset, and if so, enable a certain bit                   */
137     /* ----------------------------------------------------------------------- */
138     static
139     void __init handle_chipset(void)
140     {
141     	struct pci_dev *dev = NULL;
142     
143     	/* Just in case some nut set this to something dangerous */
144     	if (triton1)
145     		triton1 = ZORAN_VDC_TRICOM;
146     
147     	while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437, dev)))
148     	{
149     		printk(KERN_INFO "zoran: Host bridge 82437FX Triton PIIX\n");
150     		triton1 = ZORAN_VDC_TRICOM;
151     	}
152     }
153     
154     /* ----------------------------------------------------------------------- */
155     /* ZORAN functions							   */
156     /* ----------------------------------------------------------------------- */
157     
158     static void zoran_set_geo(struct zoran* ztv, struct vidinfo* i);
159     
160     #if 0 /* unused */
161     static
162     void zoran_dump(struct zoran *ztv)
163     {
164     	char	str[256];
165     	char	*p=str; /* shut up, gcc! */
166     	int	i;
167     
168     	for (i=0; i<0x60; i+=4) {
169     		if ((i % 16) == 0) {
170     			if (i) printk("%s\n",str);
171     			p = str;
172     			p+= sprintf(str, KERN_DEBUG "       %04x: ",i);
173     		}
174     		p += sprintf(p, "%08x ",zrread(i));
175     	}
176     }
177     #endif /* unused */
178     
179     static
180     void reap_states(struct zoran* ztv)
181     {
182     	/* count frames */
183     	ztv->fieldnr++;
184     
185     	/*
186     	 * Are we busy at all?
187     	 * This depends on if there is a workqueue AND the
188     	 * videotransfer is enabled on the chip...
189     	 */
190     	if (ztv->workqueue && (zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
191     	{
192     		struct vidinfo* newitem;
193     
194     		/* did we get a complete frame? */
195     		if (zrread(ZORAN_VSTR) & ZORAN_VSTR_GRAB)
196     			return;
197     
198     DEBUG(printk(CARD_DEBUG "completed %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
199     
200     		/* we are done with this buffer, tell everyone */
201     		ztv->workqueue->status = FBUFFER_DONE;
202     		ztv->workqueue->fieldnr = ztv->fieldnr;
203     		/* not good, here for BTTV_FIELDNR reasons */
204     		ztv->lastfieldnr = ztv->fieldnr;
205     
206     		switch (ztv->workqueue->kindof) {
207     		 case FBUFFER_GRAB:
208     			wake_up_interruptible(&ztv->grabq);
209     			break;
210     		 case FBUFFER_VBI:
211     			wake_up_interruptible(&ztv->vbiq);
212     			break;
213     		 default:
214     			printk(CARD_INFO "somebody killed the workqueue (kindof=%d)!\n",CARD,ztv->workqueue->kindof);
215     		}
216     
217     		/* item completed, skip to next item in queue */
218     		write_lock(&ztv->lock);
219     		newitem = ztv->workqueue->next;
220     		ztv->workqueue->next = 0;	/* mark completed */
221     		ztv->workqueue = newitem;
222     		write_unlock(&ztv->lock);
223     	}
224     
225     	/*
226     	 * ok, so it seems we have nothing in progress right now.
227     	 * Lets see if we can find some work.
228     	 */
229     	if (ztv->workqueue)
230     	{
231     		struct vidinfo* newitem;
232     again:
233     
234     DEBUG(printk(CARD_DEBUG "starting %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
235     
236     		/* loadup the frame settings */
237     		read_lock(&ztv->lock);
238     		zoran_set_geo(ztv,ztv->workqueue);
239     		read_unlock(&ztv->lock);
240     
241     		switch (ztv->workqueue->kindof) {
242     		 case FBUFFER_GRAB:
243     		 case FBUFFER_VBI:
244     			zrand(~ZORAN_OCR_OVLEN, ZORAN_OCR);
245     			zror(ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
246     			zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
247     
248     			/* start single-shot grab */
249     			zror(ZORAN_VSTR_GRAB, ZORAN_VSTR);
250     			break;
251     		 default:
252     			printk(CARD_INFO "what is this doing on the queue? (kindof=%d)\n",CARD,ztv->workqueue->kindof);
253     			write_lock(&ztv->lock);
254     			newitem = ztv->workqueue->next;
255     			ztv->workqueue->next = 0;
256     			ztv->workqueue = newitem;
257     			write_unlock(&ztv->lock);
258     			if (newitem)
259     				goto again;	/* yeah, sure.. */
260     		}
261     		/* bye for now */
262     		return;
263     	}
264     DEBUG(printk(CARD_DEBUG "nothing in queue\n",CARD));
265     
266     	/*
267     	 * What? Even the workqueue is empty? Am i really here
268     	 * for nothing? Did i come all that way to... do nothing?
269     	 */
270     
271     	/* do we need to overlay? */
272     	if (test_bit(STATE_OVERLAY, &ztv->state))
273     	{
274     		/* are we already overlaying? */
275     		if (!(zrread(ZORAN_OCR) & ZORAN_OCR_OVLEN) ||
276     		    !(zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
277     		{
278     DEBUG(printk(CARD_DEBUG "starting overlay\n",CARD));
279     
280     			read_lock(&ztv->lock);
281     			zoran_set_geo(ztv,&ztv->overinfo);
282     			read_unlock(&ztv->lock);
283     
284     			zror(ZORAN_OCR_OVLEN, ZORAN_OCR);
285     			zrand(~ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
286     			zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
287     		}
288     
289     		/*
290     		 * leave overlaying on, but turn interrupts off.
291     		 */
292     		zrand(~ZORAN_ICR_EN,ZORAN_ICR);
293     		return;
294     	}
295     
296     	/* do we have any VBI idle time processing? */
297     	if (test_bit(STATE_VBI, &ztv->state))
298     	{
299     		struct vidinfo* item;
300     		struct vidinfo* lastitem;
301     
302     		/* protect the workqueue */
303     		write_lock(&ztv->lock);
304     		lastitem = ztv->workqueue;
305     		if (lastitem)
306     			while (lastitem->next) lastitem = lastitem->next;
307     		for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
308     			if (item->next == 0 && item->status == FBUFFER_FREE)
309     			{
310     DEBUG(printk(CARD_DEBUG "%p added to queue\n",CARD,item));
311     				item->status = FBUFFER_BUSY;
312     				if (!lastitem)
313     					ztv->workqueue = item;
314     				else 
315     					lastitem->next = item;
316     				lastitem = item;
317     			}
318     		write_unlock(&ztv->lock);
319     		if (ztv->workqueue)
320     			goto again;	/* hey, _i_ graduated :) */
321     	}
322     
323     	/*
324     	 * Then we must be realy IDLE
325     	 */
326     DEBUG(printk(CARD_DEBUG "turning off\n",CARD));
327     	/* nothing further to do, disable DMA and further IRQs */
328     	zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
329     	zrand(~ZORAN_ICR_EN,ZORAN_ICR);
330     }
331     
332     static
333     void zoran_irq(int irq, void *dev_id, struct pt_regs * regs)
334     {
335     	u32 stat,estat;
336     	int count = 0;
337     	struct zoran *ztv = (struct zoran *)dev_id;
338     
339     	UNUSED(irq); UNUSED(regs);
340     	for (;;) {
341     		/* get/clear interrupt status bits */
342     		stat=zrread(ZORAN_ISR);
343     		estat=stat & zrread(ZORAN_ICR);
344     		if (!estat)
345     			return;
346     		zrwrite(estat,ZORAN_ISR);
347     		IDEBUG(printk(CARD_DEBUG "estat %08x\n",CARD,estat));
348     		IDEBUG(printk(CARD_DEBUG " stat %08x\n",CARD,stat));
349     
350     		if (estat & ZORAN_ISR_CODE)
351     		{
352     			IDEBUG(printk(CARD_DEBUG "CodReplIRQ\n",CARD));
353     		}
354     		if (estat & ZORAN_ISR_GIRQ0)
355     		{
356     			IDEBUG(printk(CARD_DEBUG "GIRQ0\n",CARD));
357     			if (!ztv->card->usegirq1)
358     				reap_states(ztv);
359     		}
360     		if (estat & ZORAN_ISR_GIRQ1)
361     		{
362     			IDEBUG(printk(CARD_DEBUG "GIRQ1\n",CARD));
363     			if (ztv->card->usegirq1)
364     				reap_states(ztv);
365     		}
366     
367     		count++;
368     		if (count > 10)
369     			printk(CARD_ERR "irq loop %d (%x)\n",CARD,count,estat);
370     		if (count > 20)
371     		{
372     			zrwrite(0, ZORAN_ICR);
373     			printk(CARD_ERR "IRQ lockup, cleared int mask\n",CARD);
374     		}
375     	}
376     }
377     
378     static
379     int zoran_muxsel(struct zoran* ztv, int channel, int norm)
380     {
381     	int	rv;
382     
383     	/* set the new video norm */
384     	rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
385     	if (rv)
386     		return rv;
387     	ztv->norm = norm;
388     
389     	/* map the given channel to the cards decoder's channel */
390     	channel = ztv->card->video_mux[channel] & CHANNEL_MASK;
391     
392     	/* set the new channel */
393     	rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &channel);
394     	return rv;
395     }
396     
397     /* Tell the interrupt handler what to to.  */
398     static
399     void zoran_cap(struct zoran* ztv, int on)
400     {
401     DEBUG(printk(CARD_DEBUG "zoran_cap(%d) state=%x\n",CARD,on,ztv->state));
402     
403     	if (on) {
404     		ztv->running = 1;
405     
406     		/*
407     		 * turn interrupts (back) on. The DMA will be enabled
408     		 * inside the irq handler when it detects a restart.
409     		 */
410     		zror(ZORAN_ICR_EN,ZORAN_ICR);
411     	}
412     	else {
413     		/*
414     		 * turn both interrupts and DMA off
415     		 */
416     		zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
417     		zrand(~ZORAN_ICR_EN,ZORAN_ICR);
418     
419     		ztv->running = 0;
420     	}
421     }
422     
423     static ulong dmask[] = {
424     	0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8,
425     	0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80,
426     	0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
427     	0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000,
428     	0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000,
429     	0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
430     	0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000,
431     	0xF0000000, 0xE0000000, 0xC0000000, 0x80000000
432     };
433     
434     static
435     void zoran_built_overlay(struct zoran* ztv, int count, struct video_clip *vcp)
436     {
437     	ulong*	mtop;
438     	int	ystep = (ztv->vidXshift + ztv->vidWidth+31)/32;	/* next DWORD */
439     	int	i;
440     
441     DEBUG(printk(KERN_DEBUG "       overlay at %p, ystep=%d, clips=%d\n",ztv->overinfo.overlay,ystep,count));
442     
443     	for (i=0; i<count; i++) {
444     		struct video_clip *vp = vcp+i;
445     		UNUSED(vp);
446     DEBUG(printk(KERN_DEBUG "       %d: clip(%d,%d,%d,%d)\n", i,vp->x,vp->y,vp->width,vp->height));
447     	}
448     
449     	/*
450     	 * activate the visible portion of the screen
451     	 * Note we take some shortcuts here, because we
452     	 * know the width can never be < 32. (I.e. a DWORD)
453     	 * We also assume the overlay starts somewhere in
454     	 * the FIRST dword.
455     	 */
456     	{
457     		int start = ztv->vidXshift;
458     		ulong firstd = dmask[start];
459     		ulong lastd = ~dmask[(start + ztv->overinfo.w) & 31];
460     		mtop = ztv->overinfo.overlay;
461     		for (i=0; i<ztv->overinfo.h; i++) {
462     			int w = ztv->vidWidth;
463     			ulong* line = mtop;
464     			if (start & 31) {
465     				*line++ = firstd;
466     				w -= 32-(start&31);
467     			}
468     			memset(line, ~0, w/8);
469     			if (w & 31)
470     				line[w/32] = lastd;
471     			mtop += ystep;
472     		}
473     	}
474     
475     	/* process clipping regions */
476     	for (i=0; i<count; i++) {
477     		int h;
478     		if (vcp->x < 0 || (uint)vcp->x > ztv->overinfo.w ||
479     		    vcp->y < 0 || vcp->y > ztv->overinfo.h ||
480     		    vcp->width < 0 || (uint)(vcp->x+vcp->width) > ztv->overinfo.w ||
481     		    vcp->height < 0 || (vcp->y+vcp->height) > ztv->overinfo.h)
482     		{
483     			DEBUG(printk(CARD_DEBUG "illegal 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));
484     			if (vcp->x < 0) vcp->x = 0;
485     			if ((uint)vcp->x > ztv->overinfo.w) vcp->x = ztv->overinfo.w;
486     			if (vcp->y < 0) vcp->y = 0;
487     			if (vcp->y > ztv->overinfo.h) vcp->y = ztv->overinfo.h;
488     			if (vcp->width < 0) vcp->width = 0;
489     			if ((uint)(vcp->x+vcp->width) > ztv->overinfo.w) vcp->width = ztv->overinfo.w - vcp->x;
490     			if (vcp->height < 0) vcp->height = 0;
491     			if (vcp->y+vcp->height > ztv->overinfo.h) vcp->height = ztv->overinfo.h - vcp->y;
492     //			continue;
493     		}
494     
495     		mtop = &ztv->overinfo.overlay[vcp->y*ystep];
496     		for (h=0; h<=vcp->height; h++) {
497     			int w;
498     			int x = ztv->vidXshift + vcp->x;
499     			for (w=0; w<=vcp->width; w++) {
500     				clear_bit(x&31, &mtop[x/32]);
501     				x++;
502     			}
503     			mtop += ystep;
504     		}
505     		++vcp;
506     	}
507     
508     	mtop = ztv->overinfo.overlay;
509     	zrwrite(virt_to_bus(mtop), ZORAN_MTOP);
510     	zrwrite(virt_to_bus(mtop+ystep), ZORAN_MBOT);
511     	zraor((ztv->vidInterlace*ystep)<<0,~ZORAN_OCR_MASKSTRIDE,ZORAN_OCR);
512     }
513     
514     struct tvnorm 
515     {
516     	u16 Wt, Wa, Ht, Ha, HStart, VStart;
517     };
518     
519     static struct tvnorm tvnorms[] = {
520     	/* PAL-BDGHI */
521     /*	{ 864, 720, 625, 576, 131, 21 },*/
522     /*00*/	{ 864, 768, 625, 576, 81, 17 },
523     	/* NTSC */
524     /*01*/	{ 858, 720, 525, 480, 121, 10 },
525     	/* SECAM */
526     /*02*/	{ 864, 720, 625, 576, 131, 21 },
527     	/* BW50 */
528     /*03*/	{ 864, 720, 625, 576, 131, 21 },
529     	/* BW60 */
530     /*04*/	{ 858, 720, 525, 480, 121, 10 }
531     };
532     #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
533     
534     /*
535      * Program the chip for a setup as described in the vidinfo struct.
536      *
537      * Side-effects: calculates vidXshift, vidInterlace,
538      * vidHeight, vidWidth which are used in a later stage
539      * to calculate the overlay mask
540      *
541      * This is an internal function, as such it does not check the
542      * validity of the struct members... Spectaculair crashes will
543      * follow /very/ quick when you're wrong and the chip right :)
544      */
545     static
546     void zoran_set_geo(struct zoran* ztv, struct vidinfo* i)
547     {
548     	ulong	top, bot;
549     	int	stride;
550     	int	winWidth, winHeight;
551     	int	maxWidth, maxHeight, maxXOffset, maxYOffset;
552     	long	vfec;
553     
554     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));
555     
556     	/*
557     	 * make sure the DMA transfers are inhibited during our
558     	 * reprogramming of the chip
559     	 */
560     	zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
561     
562     	maxWidth = tvnorms[ztv->norm].Wa;
563     	maxHeight = tvnorms[ztv->norm].Ha/2;
564     	maxXOffset = tvnorms[ztv->norm].HStart;
565     	maxYOffset = tvnorms[ztv->norm].VStart;
566     
567     	/* setup vfec register (keep ExtFl,TopField and VCLKPol settings) */
568     	vfec = (zrread(ZORAN_VFEC) & (ZORAN_VFEC_EXTFL|ZORAN_VFEC_TOPFIELD|ZORAN_VFEC_VCLKPOL)) |
569     	       (palette2fmt[i->format].mode & (ZORAN_VFEC_RGB|ZORAN_VFEC_ERRDIF|ZORAN_VFEC_LE|ZORAN_VFEC_PACK24));
570     
571     	/*
572     	 * Set top, bottom ptrs. Since these must be DWORD aligned,
573     	 * possible adjust the x and the width of the window.
574     	 * so the endposition stay the same. The vidXshift will make
575     	 * sure we are not writing pixels before the requested x.
576     	 */
577     	ztv->vidXshift = 0;
578     	winWidth = i->w;
579     	if (winWidth < 0)
580     		winWidth = -winWidth;
581     	top = i->busadr + i->x*i->bpp + i->y*i->bpl;
582     	if (top & 3) {
583     		ztv->vidXshift = (top & 3) / i->bpp;
584     		winWidth += ztv->vidXshift;
585     		DEBUG(printk(KERN_DEBUG "       window-x shifted %d pixels left\n",ztv->vidXshift));
586     		top &= ~3;
587     	}
588     
589     	/*
590     	 * bottom points to next frame but in interleaved mode we want
591     	 * to 'mix' the 2 frames to one capture, so 'bot' points to one
592     	 * (physical) line below the top line.
593     	 */
594     	bot = top + i->bpl;
595     	zrwrite(top,ZORAN_VTOP);
596     	zrwrite(bot,ZORAN_VBOT);
597     
598     	/*
599     	 * Make sure the winWidth is DWORD aligned too,
600     	 * thereby automaticly making sure the stride to the
601     	 * next line is DWORD aligned too (as required by spec).
602     	 */
603     	if ((winWidth*i->bpp) & 3) {
604     DEBUG(printk(KERN_DEBUG "       window-width enlarged by %d pixels\n",(winWidth*i->bpp) & 3));
605     		winWidth += (winWidth*i->bpp) & 3;
606     	}
607     
608     	/* determine the DispMode and stride */
609     	if (i->h >= 0 && i->h <= maxHeight) {
610     		/* single frame grab suffices for this height. */
611     		vfec |= ZORAN_VFEC_DISPMOD;
612     		ztv->vidInterlace = 0;
613     		stride = i->bpl - (winWidth*i->bpp);
614     		winHeight = i->h;
615     	}
616     	else {
617     		/* interleaving needed for this height */
618     		ztv->vidInterlace = 1;
619     		stride = i->bpl*2 - (winWidth*i->bpp);
620     		winHeight = i->h/2;
621     	}
622     	if (winHeight < 0)	/* can happen for VBI! */
623     		winHeight = -winHeight;
624     
625     	/* safety net, sometimes bpl is too short??? */
626     	if (stride<0) {
627     DEBUG(printk(CARD_DEBUG "WARNING stride = %d\n",CARD,stride));
628     		stride = 0;
629     	}
630     
631     	zraor((winHeight<<12)|(winWidth<<0),~(ZORAN_VDC_VIDWINHT|ZORAN_VDC_VIDWINWID), ZORAN_VDC);
632     	zraor(stride<<16,~ZORAN_VSTR_DISPSTRIDE,ZORAN_VSTR);
633     
634     	/* remember vidWidth, vidHeight for overlay calculations */
635     	ztv->vidWidth = winWidth;
636     	ztv->vidHeight = winHeight;
637     DEBUG(printk(KERN_DEBUG "       top=%08lx, bottom=%08lx\n",top,bot));
638     DEBUG(printk(KERN_DEBUG "       winWidth=%d, winHeight=%d\n",winWidth,winHeight));
639     DEBUG(printk(KERN_DEBUG "       maxWidth=%d, maxHeight=%d\n",maxWidth,maxHeight));
640     DEBUG(printk(KERN_DEBUG "       stride=%d\n",stride));
641     
642     	/*
643     	 * determine horizontal scales and crops
644     	 */
645     	if (i->w < 0) {
646     		int Hstart = 1;
647     		int Hend = Hstart + winWidth;
648     DEBUG(printk(KERN_DEBUG "       Y: scale=0, start=%d, end=%d\n", Hstart, Hend));
649     		zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
650     	}
651     	else {
652     		int Wa = maxWidth;
653     		int X = (winWidth*64+Wa-1)/Wa;
654     		int We = winWidth*64/X;
655     		int HorDcm = 64-X;
656     		int hcrop1 = 2*(Wa-We)/4;
657     		/*
658     		 * BUGFIX: Juha Nurmela <junki@qn-lpr2-165.quicknet.inet.fi> 
659     		 * found the solution to the color phase shift.
660     		 * See ChangeLog for the full explanation)
661     		 */
662     		int Hstart = (maxXOffset + hcrop1) | 1;
663     		int Hend = Hstart + We - 1;
664     
665     DEBUG(printk(KERN_DEBUG "       X: scale=%d, start=%d, end=%d\n", HorDcm, Hstart, Hend));
666     
667     		zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
668     		vfec |= HorDcm<<14;
669     
670     		if (HorDcm<16)
671     			vfec |= ZORAN_VFEC_HFILTER_1; /* no filter */
672     		else if (HorDcm<32)
673     			vfec |= ZORAN_VFEC_HFILTER_3; /* 3 tap filter */
674     		else if (HorDcm<48)
675     			vfec |= ZORAN_VFEC_HFILTER_4; /* 4 tap filter */
676     		else	vfec |= ZORAN_VFEC_HFILTER_5; /* 5 tap filter */
677     	}
678     
679     	/*
680     	 * Determine vertical scales and crops
681     	 *
682     	 * when height is negative, we want to read starting at line 0
683     	 * One day someone might need access to these lines...
684     	 */
685     	if (i->h < 0) {
686     		int Vstart = 0;
687     		int Vend = Vstart + winHeight;
688     DEBUG(printk(KERN_DEBUG "       Y: scale=0, start=%d, end=%d\n", Vstart, Vend));
689     		zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
690     	}
691     	else {
692     		int Ha = maxHeight;
693     		int Y = (winHeight*64+Ha-1)/Ha;
694     		int He = winHeight*64/Y;
695     		int VerDcm = 64-Y;
696     		int vcrop1 = 2*(Ha-He)/4;
697     		int Vstart = maxYOffset + vcrop1;
698     		int Vend = Vstart + He - 1;
699     
700     DEBUG(printk(KERN_DEBUG "       Y: scale=%d, start=%d, end=%d\n", VerDcm, Vstart, Vend));
701     		zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
702     		vfec |= VerDcm<<8;
703     	}
704     
705     DEBUG(printk(KERN_DEBUG "       F: format=%d(=%s)\n",i->format,palette2fmt[i->format].name));
706     
707     	/* setup the requested format */
708     	zrwrite(vfec, ZORAN_VFEC);
709     }
710     
711     static
712     void zoran_common_open(struct zoran* ztv, int flags)
713     {
714     	UNUSED(flags);
715     
716     	/* already opened? */
717     	if (ztv->users++ != 0)
718     		return;
719     
720     	/* unmute audio */
721     	/* /what/ audio? */
722     
723     	ztv->state = 0;
724     
725     	/* setup the encoder to the initial values */
726     	ztv->picture.colour=254<<7;
727     	ztv->picture.brightness=128<<8;
728     	ztv->picture.hue=128<<8;
729     	ztv->picture.contrast=216<<7;
730     	i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &ztv->picture);
731     
732     	/* default to the composite input since my camera is there */
733     	zoran_muxsel(ztv, 0, VIDEO_MODE_PAL);
734     }
735     
736     static
737     void zoran_common_close(struct zoran* ztv)
738     {
739     	if (--ztv->users != 0)
740     		return;
741     
742     	/* mute audio */
743     	/* /what/ audio? */
744     
745     	/* stop the chip */
746     	zoran_cap(ztv, 0);
747     }
748     
749     /*
750      * Open a zoran card. Right now the flags are just a hack
751      */
752     static int zoran_open(struct video_device *dev, int flags)
753     {
754     	struct zoran *ztv = (struct zoran*)dev;
755     	struct vidinfo* item;
756     	char* pos;
757     
758     	DEBUG(printk(CARD_DEBUG "open(dev,%d)\n",CARD,flags));
759     
760     	/*********************************************
761     	 * We really should be doing lazy allocing...
762     	 *********************************************/
763     	/* allocate a frame buffer */
764     	if (!ztv->fbuffer)
765     		ztv->fbuffer = bmalloc(ZORAN_MAX_FBUFSIZE);
766     	if (!ztv->fbuffer) {
767     		/* could not get a buffer, bail out */
768     		return -ENOBUFS;
769     	}
770     	/* at this time we _always_ have a framebuffer */
771     	memset(ztv->fbuffer,0,ZORAN_MAX_FBUFSIZE);
772     
773     	if (!ztv->overinfo.overlay)
774     		ztv->overinfo.overlay = (void*)kmalloc(1024*1024/8, GFP_KERNEL);
775     	if (!ztv->overinfo.overlay) {
776     		/* could not get an overlay buffer, bail out */
777     		bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
778     		return -ENOBUFS;
779     	}
780     	/* at this time we _always_ have a overlay */
781     
782     	/* clear buffer status, and give them a DMAable address */
783     	pos = ztv->fbuffer;
784     	for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
785     	{
786     		item->status = FBUFFER_FREE;
787     		item->memadr = pos;
788     		item->busadr = virt_to_bus(pos);
789     		pos += ZORAN_MAX_FBUFFER;
790     	}
791     
792     	/* do the common part of all open's */
793     	zoran_common_open(ztv, flags);
794     
795     	return 0;
796     }
797     
798     static
799     void zoran_close(struct video_device* dev)
800     {
801     	struct zoran *ztv = (struct zoran*)dev;
802     
803     	DEBUG(printk(CARD_DEBUG "close(dev)\n",CARD));
804     
805     	/* driver specific closure */
806     	clear_bit(STATE_OVERLAY, &ztv->state);
807     
808     	zoran_common_close(ztv);
809     
810             /*
811              *      This is sucky but right now I can't find a good way to
812              *      be sure its safe to free the buffer. We wait 5-6 fields
813              *      which is more than sufficient to be sure.
814              */
815             current->state = TASK_UNINTERRUPTIBLE;
816             schedule_timeout(HZ/10);        /* Wait 1/10th of a second */
817     
818     	/* free the allocated framebuffer */
819     	if (ztv->fbuffer)
820     		bfree( ztv->fbuffer, ZORAN_MAX_FBUFSIZE );
821     	ztv->fbuffer = 0;
822     	if (ztv->overinfo.overlay)
823     		kfree( ztv->overinfo.overlay );
824     	ztv->overinfo.overlay = 0;
825     
826     }
827     
828     /*
829      * This read function could be used reentrant in a SMP situation.
830      *
831      * This is made possible by the spinlock which is kept till we
832      * found and marked a buffer for our own use. The lock must
833      * be released as soon as possible to prevent lock contention.
834      */
835     static
836     long zoran_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
837     {
838     	struct zoran *ztv = (struct zoran*)dev;
839     	unsigned long max;
840     	struct vidinfo* unused = 0;
841     	struct vidinfo* done = 0;
842     
843     	DEBUG(printk(CARD_DEBUG "zoran_read(%p,%ld,%d)\n",CARD,buf,count,nonblock));
844     
845     	/* find ourself a free or completed buffer */
846     	for (;;) {
847     		struct vidinfo* item;
848     
849     		write_lock_irq(&ztv->lock);
850     		for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
851     		{
852     			if (!unused && item->status == FBUFFER_FREE)
853     				unused = item;
854     			if (!done && item->status == FBUFFER_DONE)
855     				done = item;
856     		}
857     		if (done || unused)
858     			break;
859     
860     		/* no more free buffers, wait for them. */
861     		write_unlock_irq(&ztv->lock);
862     		if (nonblock)
863     			return -EWOULDBLOCK;
864     		interruptible_sleep_on(&ztv->grabq);
865     		if (signal_pending(current))
866     			return -EINTR;
867     	}
868     
869     	/* Do we have 'ready' data? */
870     	if (!done) {
871     		/* no? than this will take a while... */
872     		if (nonblock) {
873     			write_unlock_irq(&ztv->lock);
874     			return -EWOULDBLOCK;
875     		}
876     
877     		/* mark the unused buffer as wanted */
878     		unused->status = FBUFFER_BUSY;
879     		unused->w = 320;
880     		unused->h = 240;
881     		unused->format = VIDEO_PALETTE_RGB24;
882     		unused->bpp = palette2fmt[unused->format].bpp;
883     		unused->bpl = unused->w * unused->bpp;
884     		unused->next = 0;
885     		{ /* add to tail of queue */
886     		  struct vidinfo* oldframe = ztv->workqueue;
887     		  if (!oldframe) ztv->workqueue = unused;
888     		  else {
889     		    while (oldframe->next) oldframe = oldframe->next;
890     		    oldframe->next = unused;
891     		  }
892     		}
893     		write_unlock_irq(&ztv->lock);
894     
895     		/* tell the state machine we want it filled /NOW/ */
896     		zoran_cap(ztv, 1);
897     
898     		/* wait till this buffer gets grabbed */
899     		while (unused->status == FBUFFER_BUSY) {
900     			interruptible_sleep_on(&ztv->grabq);
901     			/* see if a signal did it */
902     			if (signal_pending(current))
903     				return -EINTR;
904     		}
905     		done = unused;
906     	}
907     	else
908     		write_unlock_irq(&ztv->lock);
909     
910     	/* Yes! we got data! */
911     	max = done->bpl * done->h;
912     	if (count > max)
913     		count = max;
914     	if (copy_to_user((void*)buf, done->memadr, count))
915     		count = -EFAULT;
916     
917     	/* keep the engine running */
918     	done->status = FBUFFER_FREE;
919     //	zoran_cap(ztv,1);
920     
921     	/* tell listeners this buffer became free */
922     	wake_up_interruptible(&ztv->grabq);
923     
924     	/* goodbye */
925     	DEBUG(printk(CARD_DEBUG "zoran_read() returns %lu\n",CARD,count));
926     	return count;
927     }
928     
929     static
930     long zoran_write(struct video_device* dev, const char* buf, unsigned long count, int nonblock)
931     {
932     	struct zoran *ztv = (struct zoran *)dev;
933     	UNUSED(ztv); UNUSED(dev); UNUSED(buf); UNUSED(count); UNUSED(nonblock);
934     	DEBUG(printk(CARD_DEBUG "zoran_write\n",CARD));
935     	return -EINVAL;
936     }
937     
938     #if LINUX_VERSION_CODE >= 0x020100
939     static
940     unsigned int zoran_poll(struct video_device *dev, struct file *file, poll_table *wait)
941     {
942     	struct zoran *ztv = (struct zoran *)dev;
943     	struct vidinfo* item;
944     	unsigned int mask = 0;
945     
946     	poll_wait(file, &ztv->grabq, wait);
947     
948     	for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
949     		if (item->status == FBUFFER_DONE)
950     		{
951     			mask |= (POLLIN | POLLRDNORM);
952     			break;
953     		}
954     
955     	DEBUG(printk(CARD_DEBUG "zoran_poll()=%x\n",CARD,mask));
956     
957     	return mask;
958     }
959     #endif
960     
961     /* append a new clipregion to the vector of video_clips */
962     static
963     void new_clip(struct video_window* vw, struct video_clip* vcp, int x, int y, int w, int h)
964     {
965     	vcp[vw->clipcount].x = x;
966     	vcp[vw->clipcount].y = y;
967     	vcp[vw->clipcount].width = w;
968     	vcp[vw->clipcount].height = h;
969     	vw->clipcount++;
970     }
971     
972     static
973     int zoran_ioctl(struct video_device* dev, unsigned int cmd, void *arg)
974     {
975     	struct zoran* ztv = (struct zoran*)dev;
976     
977     	switch (cmd) {
978     	 case VIDIOCGCAP:
979     	 {
980     		struct video_capability c;
981     		DEBUG(printk(CARD_DEBUG "VIDIOCGCAP\n",CARD));
982     
983     		strcpy(c.name,ztv->video_dev.name);
984     		c.type = VID_TYPE_CAPTURE|
985     			 VID_TYPE_OVERLAY|
986     			 VID_TYPE_CLIPPING|
987     			 VID_TYPE_FRAMERAM|
988     			 VID_TYPE_SCALES;
989     		if (ztv->have_tuner)
990     			c.type |= VID_TYPE_TUNER;
991     		if (ztv->have_decoder) {
992     			c.channels = ztv->card->video_inputs;
993     			c.audios = ztv->card->audio_inputs;
994     		} else
995     			/* no decoder -> no channels */
996     			c.channels = c.audios = 0;
997     		c.maxwidth = 768;
998     		c.maxheight = 576;
999     		c.minwidth = 32;
1000     		c.minheight = 32;
1001     		if (copy_to_user(arg,&c,sizeof(c)))
1002     			return -EFAULT;
1003     		break;
1004     	 }
1005     
1006     	 case VIDIOCGCHAN:
1007     	 {
1008     		struct video_channel v;
1009     		int mux;
1010     		if (copy_from_user(&v, arg,sizeof(v)))
1011     			return -EFAULT;
1012     		DEBUG(printk(CARD_DEBUG "VIDIOCGCHAN(%d)\n",CARD,v.channel));
1013     		v.flags=VIDEO_VC_AUDIO
1014     #ifdef VIDEO_VC_NORM
1015     			|VIDEO_VC_NORM
1016     #endif
1017     			;
1018     		v.tuners=0;
1019     		v.type=VIDEO_TYPE_CAMERA;
1020     #ifdef I_EXPECT_POSSIBLE_NORMS_IN_THE_API
1021     		v.norm=VIDEO_MODE_PAL|
1022     		       VIDEO_MODE_NTSC|
1023     		       VIDEO_MODE_SECAM;
1024     #else
1025     		v.norm=VIDEO_MODE_PAL;
1026     #endif
1027     		/* too many inputs? no decoder -> no channels */
1028     		if (!ztv->have_decoder || v.channel < 0 ||  v.channel >= ztv->card->video_inputs)
1029     			return -EINVAL;
1030     
1031     		/* now determine the name of the channel */
1032     		mux = ztv->card->video_mux[v.channel];
1033     		if (mux & IS_TUNER) {
1034     			/* lets assume only one tuner, yes? */
1035     			strcpy(v.name,"Television");
1036     			v.type = VIDEO_TYPE_TV;
1037     			if (ztv->have_tuner) {
1038     				v.flags |= VIDEO_VC_TUNER;
1039     				v.tuners = 1;
1040     			}
1041     		}
1042     		else if (mux & IS_SVHS)
1043     			sprintf(v.name,"S-Video-%d",v.channel);
1044     		else
1045     			sprintf(v.name,"CVBS-%d",v.channel);
1046     
1047     		if (copy_to_user(arg,&v,sizeof(v)))
1048     			return -EFAULT;
1049     		break;
1050     	 }
1051     	 case VIDIOCSCHAN:
1052     	 {	/* set video channel */
1053     		struct video_channel v;
1054     		if (copy_from_user(&v, arg,sizeof(v)))
1055     			return -EFAULT;
1056     		DEBUG(printk(CARD_DEBUG "VIDIOCSCHAN(%d,%d)\n",CARD,v.channel,v.norm));
1057     
1058     		/* too many inputs? no decoder -> no channels */
1059     		if (!ztv->have_decoder || v.channel >= ztv->card->video_inputs || v.channel < 0)
1060     			return -EINVAL;
1061     
1062     		if (v.norm != VIDEO_MODE_PAL &&
1063     		    v.norm != VIDEO_MODE_NTSC &&
1064     		    v.norm != VIDEO_MODE_SECAM &&
1065     		    v.norm != VIDEO_MODE_AUTO)
1066     			return -EOPNOTSUPP;
1067     
1068     		/* make it happen, nr1! */
1069     		return zoran_muxsel(ztv,v.channel,v.norm);
1070     	 }
1071     
1072     	 case VIDIOCGTUNER:
1073     	 {
1074     		struct video_tuner v;
1075     		if (copy_from_user(&v, arg,sizeof(v)))
1076     			return -EFAULT;
1077     		DEBUG(printk(CARD_DEBUG "VIDIOCGTUNER(%d)\n",CARD,v.tuner));
1078     
1079     		/* Only no or one tuner for now */
1080     		if (!ztv->have_tuner || v.tuner)
1081     			return -EINVAL;
1082     
1083     		strcpy(v.name,"Television");
1084     		v.rangelow  = 0;
1085     		v.rangehigh = ~0;
1086     		v.flags     = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1087     		v.mode      = ztv->norm;
1088     		v.signal    = 0xFFFF; /* unknown */
1089     
1090     		if (copy_to_user(arg,&v,sizeof(v)))
1091     			return -EFAULT;
1092     		break;
1093     	 }
1094     	 case VIDIOCSTUNER:
1095     	 {
1096     		struct video_tuner v;
1097     		if (copy_from_user(&v, arg, sizeof(v)))
1098     			return -EFAULT;
1099     		DEBUG(printk(CARD_DEBUG "VIDIOCSTUNER(%d,%d)\n",CARD,v.tuner,v.mode));
1100     
1101     		/* Only no or one tuner for now */
1102     		if (!ztv->have_tuner || v.tuner)
1103     			return -EINVAL;
1104     
1105     		/* and it only has certain valid modes */
1106     		if( v.mode != VIDEO_MODE_PAL &&
1107     		    v.mode != VIDEO_MODE_NTSC &&
1108     		    v.mode != VIDEO_MODE_SECAM)
1109     			return -EOPNOTSUPP;
1110     
1111     		/* engage! */
1112     		return zoran_muxsel(ztv,v.tuner,v.mode);
1113     	 }
1114     
1115     	 case VIDIOCGPICT:
1116     	 {
1117     		struct video_picture p = ztv->picture;
1118     		DEBUG(printk(CARD_DEBUG "VIDIOCGPICT\n",CARD));
1119     		p.depth = ztv->depth;
1120     		switch (p.depth) {
1121     		 case  8: p.palette=VIDEO_PALETTE_YUV422;
1122     			  break;
1123     		 case 15: p.palette=VIDEO_PALETTE_RGB555;
1124     			  break;
1125     		 case 16: p.palette=VIDEO_PALETTE_RGB565;
1126     			  break;
1127     		 case 24: p.palette=VIDEO_PALETTE_RGB24;
1128     			  break;
1129     		 case 32: p.palette=VIDEO_PALETTE_RGB32;
1130     			  break;
1131     		}
1132     		if (copy_to_user(arg, &p, sizeof(p)))
1133     			return -EFAULT;
1134     		break;
1135     	 }
1136     	 case VIDIOCSPICT:
1137     	 {
1138     		struct video_picture p;
1139     		if (copy_from_user(&p, arg,sizeof(p)))
1140     			return -EFAULT;
1141     		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     
1143     		/* depth must match with framebuffer */
1144     		if (p.depth != ztv->depth)
1145     			return -EINVAL;
1146     
1147     		/* check if palette matches this bpp */
1148     		if (p.palette>NRPALETTES ||
1149     		    palette2fmt[p.palette].bpp != ztv->overinfo.bpp)
1150     			return -EINVAL;
1151     
1152     		write_lock_irq(&ztv->lock);
1153     		ztv->overinfo.format = p.palette;
1154     		ztv->picture = p;
1155     		write_unlock_irq(&ztv->lock);
1156     
1157     		/* tell the decoder */
1158     		i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
1159     		break;
1160     	 }
1161     
1162     	 case VIDIOCGWIN:
1163     	 {
1164     		struct video_window vw;
1165     		DEBUG(printk(CARD_DEBUG "VIDIOCGWIN\n",CARD));
1166     		read_lock(&ztv->lock);
1167     		vw.x      = ztv->overinfo.x;
1168     		vw.y      = ztv->overinfo.y;
1169     		vw.width  = ztv->overinfo.w;
1170     		vw.height = ztv->overinfo.h;
1171     		vw.chromakey= 0;
1172     		vw.flags  = 0;
1173     		if (ztv->vidInterlace)
1174     			vw.flags|=VIDEO_WINDOW_INTERLACE;
1175     		read_unlock(&ztv->lock);
1176     		if (copy_to_user(arg,&vw,sizeof(vw)))
1177     			return -EFAULT;
1178     		break;
1179     	 }
1180     	 case VIDIOCSWIN:
1181     	 {
1182     		struct video_window vw;
1183     		struct video_clip *vcp;
1184     		int on;
1185     		if (copy_from_user(&vw,arg,sizeof(vw)))
1186     			return -EFAULT;
1187     		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));
1188     
1189     		if (vw.flags)
1190     			return -EINVAL;
1191     
1192     		if (vw.clipcount <0 || vw.clipcount>256)
1193     			return -EDOM;   /* Too many! */
1194     
1195     		/*
1196     		 *      Do any clips.
1197     		 */
1198     		vcp = vmalloc(sizeof(struct video_clip)*(vw.clipcount+4));
1199     		if (vcp==NULL)
1200     			return -ENOMEM;
1201     		if (vw.clipcount && copy_from_user(vcp,vw.clips,sizeof(struct video_clip)*vw.clipcount)) {
1202     			vfree(vcp);
1203     			return -EFAULT;
1204     		}
1205     
1206     		on = ztv->running;
1207     		if (on)
1208     			zoran_cap(ztv, 0);
1209     
1210     		/*
1211     		 * strange, it seems xawtv sometimes calls us with 0
1212     		 * width and/or height. Ignore these values
1213     		 */
1214     		if (vw.x == 0)
1215     			vw.x = ztv->overinfo.x;
1216     		if (vw.y == 0)
1217     			vw.y = ztv->overinfo.y;
1218     
1219     		/* by now we are committed to the new data... */
1220     		write_lock_irq(&ztv->lock);
1221     		ztv->overinfo.x = vw.x;
1222     		ztv->overinfo.y = vw.y;
1223     		ztv->overinfo.w = vw.width;
1224     		ztv->overinfo.h = vw.height;
1225     		write_unlock_irq(&ztv->lock);
1226     
1227     		/*
1228     		 *      Impose display clips
1229     		 */
1230     		if (vw.x+vw.width > ztv->swidth)
1231     			new_clip(&vw, vcp, ztv->swidth-vw.x, 0, vw.width-1, vw.height-1);
1232     		if (vw.y+vw.height > ztv->sheight)
1233     			new_clip(&vw, vcp, 0, ztv->sheight-vw.y, vw.width-1, vw.height-1);
1234     
1235     		/* built the requested clipping zones */
1236     		zoran_set_geo(ztv, &ztv->overinfo);
1237     		zoran_built_overlay(ztv, vw.clipcount, vcp);
1238     		vfree(vcp);
1239     
1240     		/* if we were on, restart the video engine */
1241     		if (on)
1242     			zoran_cap(ztv, 1);
1243     		break;
1244     	 }
1245     
1246     	 case VIDIOCCAPTURE:
1247     	 {
1248     		int v;
1249     		if (get_user(v, (int *)arg))
1250     			return -EFAULT;
1251     		DEBUG(printk(CARD_DEBUG "VIDIOCCAPTURE(%d)\n",CARD,v));
1252     
1253     		if (v==0) {
1254     			clear_bit(STATE_OVERLAY, &ztv->state);
1255     			zoran_cap(ztv, 1);
1256     		}
1257     		else {
1258     			/* is VIDIOCSFBUF, VIDIOCSWIN done? */
1259     			if (ztv->overinfo.busadr==0 || ztv->overinfo.w==0 || ztv->overinfo.h==0)
1260     				return -EINVAL;
1261     
1262     			set_bit(STATE_OVERLAY, &ztv->state);
1263     			zoran_cap(ztv, 1);
1264     		}
1265     		break;
1266     	 }
1267     
1268     	 case VIDIOCGFBUF:
1269     	 {
1270     		struct video_buffer v;
1271     		DEBUG(printk(CARD_DEBUG "VIDIOCGFBUF\n",CARD));
1272     		read_lock(&ztv->lock);
1273     		v.base   = (void *)ztv->overinfo.busadr;
1274     		v.height = ztv->sheight;
1275     		v.width  = ztv->swidth;
1276     		v.depth  = ztv->depth;
1277     		v.bytesperline = ztv->overinfo.bpl;
1278     		read_unlock(&ztv->lock);
1279     		if(copy_to_user(arg, &v,sizeof(v)))
1280     			return -EFAULT;
1281     		break;
1282     	 }
1283     	 case VIDIOCSFBUF:
1284     	 {
1285     		struct video_buffer v;
1286     #if LINUX_VERSION_CODE >= 0x020100
1287     			if(!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_ADMIN))
1288     #else
1289     			if(!suser())
1290     #endif
1291     			return -EPERM;
1292     		if (copy_from_user(&v, arg,sizeof(v)))
1293     			return -EFAULT;
1294     		DEBUG(printk(CARD_DEBUG "VIDIOCSFBUF(%p,%d,%d,%d,%d)\n",CARD,v.base, v.width,v.height,v.depth,v.bytesperline));
1295     
1296     		if (v.depth!=15 && v.depth!=16 && v.depth!=24 && v.depth!=32)
1297     			return -EINVAL;
1298     		if (v.bytesperline<1)
1299     			return -EINVAL;
1300     		if (ztv->running)
1301     			return -EBUSY;
1302     		write_lock_irq(&ztv->lock);
1303     		ztv->overinfo.busadr  = (ulong)v.base;
1304     		ztv->sheight      = v.height;
1305     		ztv->swidth       = v.width;
1306     		ztv->depth        = v.depth;		/* bits per pixel */
1307     		ztv->overinfo.bpp = ((v.depth+1)&0x38)/8;/* bytes per pixel */
1308     		ztv->overinfo.bpl = v.bytesperline;	/* bytes per line */
1309     		write_unlock_irq(&ztv->lock);
1310     		break;
1311     	 }
1312     
1313     	 case VIDIOCKEY:
1314     	 {
1315     		/* Will be handled higher up .. */
1316     		break;
1317     	 }
1318     
1319     	 case VIDIOCSYNC:
1320     	 {
1321     		int i;
1322     		if (get_user(i, (int *) arg))
1323     			return -EFAULT;
1324     		DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d)\n",CARD,i));
1325     		if (i<0 || i>ZORAN_MAX_FBUFFERS)
1326     			return -EINVAL;
1327     		switch (ztv->grabinfo[i].status) {
1328     		 case FBUFFER_FREE:
1329     			return -EINVAL;
1330     		 case FBUFFER_BUSY:
1331     			/* wait till this buffer gets grabbed */
1332     			while (ztv->grabinfo[i].status == FBUFFER_BUSY) {
1333     				interruptible_sleep_on(&ztv->grabq);
1334     				/* see if a signal did it */
1335     				if (signal_pending(current))
1336     					return -EINTR;
1337     			}
1338     			/* don't fall through; a DONE buffer is not UNUSED */
1339     			break;
1340     		 case FBUFFER_DONE:
1341     			ztv->grabinfo[i].status = FBUFFER_FREE;
1342     			/* tell ppl we have a spare buffer */
1343     			wake_up_interruptible(&ztv->grabq);
1344     			break;
1345     		}
1346     		DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d) returns\n",CARD,i));
1347     		break;
1348     	 }
1349     
1350     	 case VIDIOCMCAPTURE:
1351     	 {
1352     		struct video_mmap vm;
1353     		struct vidinfo* frame;
1354     		if (copy_from_user(&vm,arg,sizeof(vm)))
1355     			return -EFAULT;
1356     		DEBUG(printk(CARD_DEBUG "VIDIOCMCAPTURE(%d,(%d,%d),%d)\n",CARD,vm.frame,vm.width,vm.height,vm.format));
1357     		if (vm.frame<0 || vm.frame>ZORAN_MAX_FBUFFERS ||
1358     		    vm.width<32 || vm.width>768 ||
1359     		    vm.height<32 || vm.height>576 ||
1360     		    vm.format>NRPALETTES ||
1361     		    palette2fmt[vm.format].mode == 0)
1362     			return -EINVAL;
1363     
1364     		/* we are allowed to take over UNUSED and DONE buffers */
1365     		frame = &ztv->grabinfo[vm.frame];
1366     		if (frame->status == FBUFFER_BUSY)
1367     			return -EBUSY;
1368     
1369     		/* setup the other parameters if they are given */
1370     		write_lock_irq(&ztv->lock);
1371     		frame->w = vm.width;
1372     		frame->h = vm.height;
1373     		frame->format = vm.format;
1374     		frame->bpp = palette2fmt[frame->format].bpp;
1375     		frame->bpl = frame->w*frame->bpp;
1376     		frame->status = FBUFFER_BUSY;
1377     		frame->next = 0;
1378     		{ /* add to tail of queue */
1379     		  struct vidinfo* oldframe = ztv->workqueue;
1380     		  if (!oldframe) ztv->workqueue = frame;
1381     		  else {
1382     		    while (oldframe->next) oldframe = oldframe->next;
1383     		    oldframe->next = frame;
1384     		  }
1385     		}
1386     		write_unlock_irq(&ztv->lock);
1387     		zoran_cap(ztv, 1);
1388     		break;
1389     	 }
1390     
1391     	 case VIDIOCGMBUF:
1392     	 {
1393     		struct video_mbuf mb;
1394     		int i;
1395     		DEBUG(printk(CARD_DEBUG "VIDIOCGMBUF\n",CARD));
1396     		mb.size = ZORAN_MAX_FBUFSIZE;
1397     		mb.frames = ZORAN_MAX_FBUFFERS;
1398     		for (i=0; i<ZORAN_MAX_FBUFFERS; i++)
1399     			mb.offsets[i] = i*ZORAN_MAX_FBUFFER;
1400     		if(copy_to_user(arg, &mb,sizeof(mb)))
1401     			return -EFAULT;
1402     		break;
1403     	 }
1404     
1405     	 case VIDIOCGUNIT:
1406     	 {
1407     		struct video_unit vu;
1408     		DEBUG(printk(CARD_DEBUG "VIDIOCGUNIT\n",CARD));
1409     		vu.video = ztv->video_dev.minor;
1410     		vu.vbi = ztv->vbi_dev.minor;
1411     		vu.radio = VIDEO_NO_UNIT;
1412     		vu.audio = VIDEO_NO_UNIT;
1413     		vu.teletext = VIDEO_NO_UNIT;
1414     		if(copy_to_user(arg, &vu,sizeof(vu)))
1415     			return -EFAULT;
1416     		break;
1417     	 }
1418     
1419     	 case VIDIOCGFREQ:
1420     	 {
1421     		unsigned long v = ztv->tuner_freq;
1422     		if (copy_to_user(arg,&v,sizeof(v)))
1423     			return -EFAULT;
1424     		DEBUG(printk(CARD_DEBUG "VIDIOCGFREQ\n",CARD));
1425     		break;
1426     	 }
1427     	 case VIDIOCSFREQ:
1428     	 {
1429     		unsigned long v;
1430     		if (copy_from_user(&v, arg, sizeof(v)))
1431     			return -EFAULT;
1432     		DEBUG(printk(CARD_DEBUG "VIDIOCSFREQ\n",CARD));
1433     
1434     		if (ztv->have_tuner) {
1435     			int fixme = v;
1436     			if (i2c_control_device(&(ztv->i2c), I2C_DRIVERID_TUNER, TUNER_SET_TVFREQ, &fixme) < 0)
1437     				return -EAGAIN;
1438     		}
1439     		ztv->tuner_freq = v;
1440     		break;
1441     	 }
1442     
1443     	 /* Why isn't this in the API?
1444     	  * And why doesn't it take a buffer number?
1445     	 case BTTV_FIELDNR: 
1446     	 {
1447     		unsigned long v = ztv->lastfieldnr;
1448     		if (copy_to_user(arg,&v,sizeof(v)))
1449     			return -EFAULT;
1450     		DEBUG(printk(CARD_DEBUG "BTTV_FIELDNR\n",CARD));
1451     		break;
1452     	 }
1453     	 */
1454     
1455     	 default:
1456     		return -ENOIOCTLCMD;
1457     	}
1458     	return 0;
1459     }
1460     
1461     static
1462     int zoran_mmap(struct video_device* dev, const char* adr, unsigned long size)
1463     {
1464     	struct zoran* ztv = (struct zoran*)dev;
1465     	unsigned long start = (unsigned long)adr;
1466     	unsigned long pos;
1467     
1468     	DEBUG(printk(CARD_DEBUG "zoran_mmap(0x%p,%ld)\n",CARD,adr,size));
1469     
1470     	/* sanity checks */
1471     	if (size > ZORAN_MAX_FBUFSIZE || !ztv->fbuffer)
1472     		return -EINVAL;
1473     
1474     	/* start mapping the whole shabang to user memory */
1475     	pos = (unsigned long)ztv->fbuffer;
1476     	while (size>0) {
1477     		unsigned long page = virt_to_phys((void*)pos);
1478     		if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
1479     			return -EAGAIN;
1480     		start += PAGE_SIZE;
1481     		pos += PAGE_SIZE;
1482     		size -= PAGE_SIZE;
1483     	}
1484     	return 0;
1485     }
1486     
1487     static struct video_device zr36120_template=
1488     {
1489     	owner:		THIS_MODULE,
1490     	name:		"UNSET",
1491     	type:		VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY,
1492     	hardware:	VID_HARDWARE_ZR36120,
1493     	open:		zoran_open,
1494     	close:		zoran_close,
1495     	read:		zoran_read,
1496     	write:		zoran_write,
1497     	poll:		zoran_poll,
1498     	ioctl:		zoran_ioctl,
1499     	mmap:		zoran_mmap,
1500     	minor:		-1,
1501     };
1502     
1503     static
1504     int vbi_open(struct video_device *dev, int flags)
1505     {
1506     	struct zoran *ztv = (struct zoran*)dev->priv;
1507     	struct vidinfo* item;
1508     
1509     	DEBUG(printk(CARD_DEBUG "vbi_open(dev,%d)\n",CARD,flags));
1510     
1511     	/*
1512     	 * During VBI device open, we continiously grab VBI-like
1513     	 * data in the vbi buffer when we have nothing to do.
1514     	 * Only when there is an explicit request for VBI data
1515     	 * (read call) we /force/ a read.
1516     	 */
1517     
1518     	/* allocate buffers */
1519     	for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1520     	{
1521     		item->status = FBUFFER_FREE;
1522     
1523     		/* alloc */
1524     		if (!item->memadr) {
1525     			item->memadr = bmalloc(ZORAN_VBI_BUFSIZE);
1526     			if (!item->memadr) {
1527     				/* could not get a buffer, bail out */
1528     				while (item != ztv->readinfo) {
1529     					item--;
1530     					bfree(item->memadr, ZORAN_VBI_BUFSIZE);
1531     					item->memadr = 0;
1532     					item->busadr = 0;
1533     				}
1534     				return -ENOBUFS;
1535     			}
1536     		}
1537     
1538     		/* determine the DMAable address */
1539     		item->busadr = virt_to_bus(item->memadr);
1540     	}
1541     
1542     	/* do the common part of all open's */
1543     	zoran_common_open(ztv, flags);
1544     
1545     	set_bit(STATE_VBI, &ztv->state);
1546     	/* start read-ahead */
1547     	zoran_cap(ztv, 1);
1548     
1549     	return 0;
1550     }
1551     
1552     static
1553     void vbi_close(struct video_device *dev)
1554     {
1555     	struct zoran *ztv = (struct zoran*)dev->priv;
1556     	struct vidinfo* item;
1557     
1558     	DEBUG(printk(CARD_DEBUG "vbi_close(dev)\n",CARD));
1559     
1560     	/* driver specific closure */
1561     	clear_bit(STATE_VBI, &ztv->state);
1562     
1563     	zoran_common_close(ztv);
1564     
1565             /*
1566              *      This is sucky but right now I can't find a good way to
1567              *      be sure its safe to free the buffer. We wait 5-6 fields
1568              *      which is more than sufficient to be sure.
1569              */
1570             current->state = TASK_UNINTERRUPTIBLE;
1571             schedule_timeout(HZ/10);        /* Wait 1/10th of a second */
1572     
1573     	for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1574     	{
1575     		if (item->memadr)
1576     			bfree(item->memadr, ZORAN_VBI_BUFSIZE);
1577     		item->memadr = 0;
1578     	}
1579     
1580     }
1581     
1582     /*
1583      * This read function could be used reentrant in a SMP situation.
1584      *
1585      * This is made possible by the spinlock which is kept till we
1586      * found and marked a buffer for our own use. The lock must
1587      * be released as soon as possible to prevent lock contention.
1588      */
1589     static
1590     long vbi_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
1591     {
1592     	struct zoran *ztv = (struct zoran*)dev->priv;
1593     	unsigned long max;
1594     	struct vidinfo* unused = 0;
1595     	struct vidinfo* done = 0;
1596     
1597     	DEBUG(printk(CARD_DEBUG "vbi_read(0x%p,%ld,%d)\n",CARD,buf,count,nonblock));
1598     
1599     	/* find ourself a free or completed buffer */
1600     	for (;;) {
1601     		struct vidinfo* item;
1602     
1603     		write_lock_irq(&ztv->lock);
1604     		for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++) {
1605     			if (!unused && item->status == FBUFFER_FREE)
1606     				unused = item;
1607     			if (!done && item->status == FBUFFER_DONE)
1608     				done = item;
1609     		}
1610     		if (done || unused)
1611     			break;
1612     
1613     		/* no more free buffers, wait for them. */
1614     		write_unlock_irq(&ztv->lock);
1615     		if (nonblock)
1616     			return -EWOULDBLOCK;
1617     		interruptible_sleep_on(&ztv->vbiq);
1618     		if (signal_pending(current))
1619     			return -EINTR;
1620     	}
1621     
1622     	/* Do we have 'ready' data? */
1623     	if (!done) {
1624     		/* no? than this will take a while... */
1625     		if (nonblock) {
1626     			write_unlock_irq(&ztv->lock);
1627     			return -EWOULDBLOCK;
1628     		}
1629     		
1630     		/* mark the unused buffer as wanted */
1631     		unused->status = FBUFFER_BUSY;
1632     		unused->next = 0;
1633     		{ /* add to tail of queue */
1634     		  struct vidinfo* oldframe = ztv->workqueue;
1635     		  if (!oldframe) ztv->workqueue = unused;
1636     		  else {
1637     		    while (oldframe->next) oldframe = oldframe->next;
1638     		    oldframe->next = unused;
1639     		  }
1640     		}
1641     		write_unlock_irq(&ztv->lock);
1642     
1643     		/* tell the state machine we want it filled /NOW/ */
1644     		zoran_cap(ztv, 1);
1645     
1646     		/* wait till this buffer gets grabbed */
1647     		while (unused->status == FBUFFER_BUSY) {
1648     			interruptible_sleep_on(&ztv->vbiq);
1649     			/* see if a signal did it */
1650     			if (signal_pending(current))
1651     				return -EINTR;
1652     		}
1653     		done = unused;
1654     	}
1655     	else
1656     		write_unlock_irq(&ztv->lock);
1657     
1658     	/* Yes! we got data! */
1659     	max = done->bpl * -done->h;
1660     	if (count > max)
1661     		count = max;
1662     
1663     	/* check if the user gave us enough room to write the data */
1664     	if (!access_ok(VERIFY_WRITE, buf, count)) {
1665     		count = -EFAULT;
1666     		goto out;
1667     	}
1668     
1669     	/*
1670     	 * Now transform/strip the data from YUV to Y-only
1671     	 * NB. Assume the Y is in the LSB of the YUV data.
1672     	 */
1673     	{
1674     	unsigned char* optr = buf;
1675     	unsigned char* eptr = buf+count;
1676     
1677     	/* are we beeing accessed from an old driver? */
1678     	if (count == 2*19*2048) {
1679     		/*
1680     		 * Extreme HACK, old VBI programs expect 2048 points
1681     		 * of data, and we only got 864 orso. Double each 
1682     		 * datapoint and clear the rest of the line.
1683     		 * This way we have appear to have a
1684     		 * sample_frequency of 29.5 Mc.
1685     		 */
1686     		int x,y;
1687     		unsigned char* iptr = done->memadr+1;
1688     		for (y=done->h; optr<eptr && y<0; y++)
1689     		{
1690     			/* copy to doubled data to userland */
1691     			for (x=0; optr+1<eptr && x<-done->w; x++)
1692     			{
1693     				unsigned char a = iptr[x*2];
1694     				*optr++ = a;
1695     				*optr++ = a;
1696     			}
1697     			/* and clear the rest of the line */
1698     			for (x*=2; optr<eptr && x<done->bpl; x++)
1699     				*optr++ = 0;
1700     			/* next line */
1701     			iptr += done->bpl;
1702     		}
1703     	}
1704     	else {
1705     		/*
1706     		 * Other (probably newer) programs asked
1707     		 * us what geometry we are using, and are
1708     		 * reading the correct size.
1709     		 */
1710     		int x,y;
1711     		unsigned char* iptr = done->memadr+1;
1712     		for (y=done->h; optr<eptr && y<0; y++)
1713     		{
1714     			/* copy to doubled data to userland */
1715     			for (x=0; optr<eptr && x<-done->w; x++)
1716     				*optr++ = iptr[x*2];
1717     			/* and clear the rest of the line */
1718     			for (;optr<eptr && x<done->bpl; x++)
1719     				*optr++ = 0;
1720     			/* next line */
1721     			iptr += done->bpl;
1722     		}
1723     	}
1724     
1725     	/* API compliance:
1726     	 * place the framenumber (half fieldnr) in the last long
1727     	 */
1728     	((ulong*)eptr)[-1] = done->fieldnr/2;
1729     	}
1730     
1731     	/* keep the engine running */
1732     	done->status = FBUFFER_FREE;
1733     	zoran_cap(ztv, 1);
1734     
1735     	/* tell listeners this buffer just became free */
1736     	wake_up_interruptible(&ztv->vbiq);
1737     
1738     	/* goodbye */
1739     out:
1740     	DEBUG(printk(CARD_DEBUG "vbi_read() returns %lu\n",CARD,count));
1741     	return count;
1742     }
1743     
1744     #if LINUX_VERSION_CODE >= 0x020100
1745     static
1746     unsigned int vbi_poll(struct video_device *dev, struct file *file, poll_table *wait)
1747     {
1748     	struct zoran *ztv = (struct zoran*)dev->priv;
1749     	struct vidinfo* item;
1750     	unsigned int mask = 0;
1751     
1752     	poll_wait(file, &ztv->vbiq, wait);
1753     
1754     	for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1755     		if (item->status == FBUFFER_DONE)
1756     		{
1757     			mask |= (POLLIN | POLLRDNORM);
1758     			break;
1759     		}
1760     
1761     	DEBUG(printk(CARD_DEBUG "vbi_poll()=%x\n",CARD,mask));
1762     
1763     	return mask;
1764     }
1765     #endif
1766     
1767     static
1768     int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
1769     {
1770     	struct zoran* ztv = (struct zoran*)dev->priv;
1771     
1772     	switch (cmd) {
1773     	 case VIDIOCGVBIFMT:
1774     	 {
1775     		struct vbi_format f;
1776     		DEBUG(printk(CARD_DEBUG "VIDIOCGVBIINFO\n",CARD));
1777     		f.sampling_rate = 14750000UL;
1778     		f.samples_per_line = -ztv->readinfo[0].w;
1779     		f.sample_format = VIDEO_PALETTE_RAW;
1780     		f.start[0] = f.start[1] = ztv->readinfo[0].y;
1781     		f.start[1] += 312;
1782     		f.count[0] = f.count[1] = -ztv->readinfo[0].h;
1783     		f.flags = VBI_INTERLACED;
1784     		if (copy_to_user(arg,&f,sizeof(f)))
1785     			return -EFAULT;
1786     		break;
1787     	 }
1788     	 case VIDIOCSVBIFMT:
1789     	 {
1790     		struct vbi_format f;
1791     		int i;
1792     		if (copy_from_user(&f, arg,sizeof(f)))
1793     			return -EFAULT;
1794     		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));
1795     
1796     		/* lots of parameters are fixed... (PAL) */
1797     		if (f.sampling_rate != 14750000UL ||
1798     		    f.samples_per_line > 864 ||
1799     		    f.sample_format != VIDEO_PALETTE_RAW ||
1800     		    f.start[0] < 0 ||
1801     		    f.start[0] != f.start[1]-312 ||
1802     		    f.count[0] != f.count[1] ||
1803     		    f.start[0]+f.count[0] >= 288 ||
1804     		    f.flags != VBI_INTERLACED)
1805     			return -EINVAL;
1806     
1807     		write_lock_irq(&ztv->lock);
1808     		ztv->readinfo[0].y = f.start[0];
1809     		ztv->readinfo[0].w = -f.samples_per_line;
1810     		ztv->readinfo[0].h = -f.count[0];
1811     		ztv->readinfo[0].bpl = f.samples_per_line*ztv->readinfo[0].bpp;
1812     		for (i=1; i<ZORAN_VBI_BUFFERS; i++)
1813     			ztv->readinfo[i] = ztv->readinfo[i];
1814     		write_unlock_irq(&ztv->lock);
1815     		break;
1816     	 }
1817     	 default:
1818     		return -ENOIOCTLCMD;
1819     	}
1820     	return 0;
1821     }
1822     
1823     static struct video_device vbi_template=
1824     {
1825     	owner:		THIS_MODULE,
1826     	name:		"UNSET",
1827     	type:		VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
1828     	hardware:	VID_HARDWARE_ZR36120,
1829     	open:		vbi_open,
1830     	close:		vbi_close,
1831     	read:		vbi_read,
1832     	write:		zoran_write,
1833     	poll:		vbi_poll,
1834     	ioctl:		vbi_ioctl,
1835     	minor:		-1,
1836     };
1837     
1838     /*
1839      *      Scan for a Zoran chip, request the irq and map the io memory
1840      */
1841     static
1842     int __init find_zoran(void)
1843     {
1844     	int result;
1845     	struct zoran *ztv;
1846     	struct pci_dev *dev = NULL;
1847     	unsigned char revision;
1848     	int zoran_num=0;
1849     
1850     	while ((dev = pci_find_device(PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120, dev)))
1851     	{
1852     		/* Ok, a ZR36120/ZR36125 found! */
1853     		ztv = &zorans[zoran_num];
1854     		ztv->dev = dev;
1855     
1856     		if (pci_enable_device(dev))
1857     			return -EIO;
1858     
1859     		pci_read_config_byte(dev, PCI_CLASS_REVISION, &revision);
1860     		printk(KERN_INFO "zoran: Zoran %x (rev %d) ",
1861     			dev->device, revision);
1862     		printk("bus: %d, devfn: %d, irq: %d, ",
1863     			dev->bus->number, dev->devfn, dev->irq);
1864     		printk("memory: 0x%08lx.\n", ztv->zoran_adr);
1865     
1866     		ztv->zoran_mem = ioremap(ztv->zoran_adr, 0x1000);
1867     		DEBUG(printk(KERN_DEBUG "zoran: mapped-memory at 0x%p\n",ztv->zoran_mem));
1868     
1869     		result = request_irq(dev->irq, zoran_irq,
1870     			SA_SHIRQ|SA_INTERRUPT,"zoran",(void *)ztv);
1871     		if (result==-EINVAL)
1872     		{
1873     			iounmap(ztv->zoran_mem);
1874     			printk(KERN_ERR "zoran: Bad irq number or handler\n");
1875     			return -EINVAL;
1876     		}
1877     		if (result==-EBUSY)
1878     			printk(KERN_ERR "zoran: IRQ %d busy, change your PnP config in BIOS\n",dev->irq);
1879     		if (result < 0) {
1880     			iounmap(ztv->zoran_mem);
1881     			return result;
1882     		}
1883     		/* Enable bus-mastering */
1884     		pci_set_master(dev);
1885     
1886     		zoran_num++;
1887     	}
1888     	if(zoran_num)
1889     		printk(KERN_INFO "zoran: %d Zoran card(s) found.\n",zoran_num);
1890     	return zoran_num;
1891     }
1892     
1893     static
1894     int __init init_zoran(int card)
1895     {
1896     	struct zoran *ztv = &zorans[card];
1897     	int	i;
1898     
1899     	/* if the given cardtype valid? */
1900     	if (cardtype[card]>=NRTVCARDS) {
1901     		printk(KERN_INFO "invalid cardtype(%d) detected\n",cardtype[card]);
1902     		return -1;
1903     	}
1904     
1905     	/* reset the zoran */
1906     	zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
1907     	udelay(10);
1908     	zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
1909     	udelay(10);
1910     
1911     	/* zoran chip specific details */
1912     	ztv->card = tvcards+cardtype[card];	/* point to the selected card */
1913     	ztv->norm = 0;				/* PAL */
1914     	ztv->tuner_freq = 0;
1915     
1916     	/* videocard details */
1917     	ztv->swidth = 800;
1918     	ztv->sheight = 600;
1919     	ztv->depth = 16;
1920     
1921     	/* State details */
1922     	ztv->fbuffer = 0;
1923     	ztv->overinfo.kindof = FBUFFER_OVERLAY;
1924     	ztv->overinfo.status = FBUFFER_FREE;
1925     	ztv->overinfo.x = 0;
1926     	ztv->overinfo.y = 0;
1927     	ztv->overinfo.w = 768; /* 640 */
1928     	ztv->overinfo.h = 576; /* 480 */
1929     	ztv->overinfo.format = VIDEO_PALETTE_RGB565;
1930     	ztv->overinfo.bpp = palette2fmt[ztv->overinfo.format].bpp;
1931     	ztv->overinfo.bpl = ztv->overinfo.bpp*ztv->swidth;
1932     	ztv->overinfo.busadr = 0;
1933     	ztv->overinfo.memadr = 0;
1934     	ztv->overinfo.overlay = 0;
1935     	for (i=0; i<ZORAN_MAX_FBUFFERS; i++) {
1936     		ztv->grabinfo[i] = ztv->overinfo;
1937     		ztv->grabinfo[i].kindof = FBUFFER_GRAB;
1938     	}
1939     	init_waitqueue_head(&ztv->grabq);
1940     
1941     	/* VBI details */
1942     	ztv->readinfo[0] = ztv->overinfo;
1943     	ztv->readinfo[0].kindof = FBUFFER_VBI;
1944     	ztv->readinfo[0].w = -864;
1945     	ztv->readinfo[0].h = -38;
1946     	ztv->readinfo[0].format = VIDEO_PALETTE_YUV422;
1947     	ztv->readinfo[0].bpp = palette2fmt[ztv->readinfo[0].format].bpp;
1948     	ztv->readinfo[0].bpl = 1024*ztv->readinfo[0].bpp;
1949     	for (i=1; i<ZORAN_VBI_BUFFERS; i++)
1950     		ztv->readinfo[i] = ztv->readinfo[0];
1951     	init_waitqueue_head(&ztv->vbiq);
1952     
1953     	/* maintenance data */
1954     	ztv->have_decoder = 0;
1955     	ztv->have_tuner = 0;
1956     	ztv->tuner_type = 0;
1957     	ztv->running = 0;
1958     	ztv->users = 0;
1959     	ztv->lock = RW_LOCK_UNLOCKED;
1960     	ztv->workqueue = 0;
1961     	ztv->fieldnr = 0;
1962     	ztv->lastfieldnr = 0;
1963     
1964     	if (triton1)
1965     		zrand(~ZORAN_VDC_TRICOM, ZORAN_VDC);
1966     
1967     	/* external FL determines TOP frame */
1968     	zror(ZORAN_VFEC_EXTFL, ZORAN_VFEC); 
1969     
1970     	/* set HSpol */
1971     	if (ztv->card->hsync_pos)
1972     		zrwrite(ZORAN_VFEH_HSPOL, ZORAN_VFEH);
1973     	/* set VSpol */
1974     	if (ztv->card->vsync_pos)
1975     		zrwrite(ZORAN_VFEV_VSPOL, ZORAN_VFEV);
1976     
1977     	/* Set the proper General Purpuse register bits */
1978     	/* implicit: no softreset, 0 waitstates */
1979     	zrwrite(ZORAN_PCI_SOFTRESET|(ztv->card->gpdir<<0),ZORAN_PCI);
1980     	/* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
1981     	zrwrite(ztv->card->gpval<<24,ZORAN_GUEST);
1982     
1983     	/* clear interrupt status */
1984     	zrwrite(~0, ZORAN_ISR);
1985     
1986     	/*
1987     	 * i2c template
1988     	 */
1989     	ztv->i2c = zoran_i2c_bus_template;
1990     	sprintf(ztv->i2c.name,"zoran-%d",card);
1991     	ztv->i2c.data = ztv;
1992     
1993     	/*
1994     	 * Now add the template and register the device unit
1995     	 */
1996     	ztv->video_dev = zr36120_template;
1997     	strcpy(ztv->video_dev.name, ztv->i2c.name);
1998     	ztv->video_dev.priv = ztv;
1999     	if (video_register_device(&ztv->video_dev, VFL_TYPE_GRABBER, video_nr) < 0)
2000     		return -1;
2001     
2002     	ztv->vbi_dev = vbi_template;
2003     	strcpy(ztv->vbi_dev.name, ztv->i2c.name);
2004     	ztv->vbi_dev.priv = ztv;
2005     	if (video_register_device(&ztv->vbi_dev, VFL_TYPE_VBI, vbi_nr) < 0) {
2006     		video_unregister_device(&ztv->video_dev);
2007     		return -1;
2008     	}
2009     	i2c_register_bus(&ztv->i2c);
2010     
2011     	/* set interrupt mask - the PIN enable will be set later */
2012     	zrwrite(ZORAN_ICR_GIRQ0|ZORAN_ICR_GIRQ1|ZORAN_ICR_CODE, ZORAN_ICR);
2013     
2014     	printk(KERN_INFO "%s: installed %s\n",ztv->i2c.name,ztv->card->name);
2015     	return 0;
2016     }
2017     
2018     static
2019     void __exit release_zoran(int max)
2020     {
2021     	struct zoran *ztv;
2022     	int i;
2023     
2024     	for (i=0;i<max; i++) 
2025     	{
2026     		ztv = &zorans[i];
2027     
2028     		/* turn off all capturing, DMA and IRQs */
2029     		/* reset the zoran */
2030     		zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
2031     		udelay(10);
2032     		zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
2033     		udelay(10);
2034     
2035     		/* first disable interrupts before unmapping the memory! */
2036     		zrwrite(0, ZORAN_ICR);
2037     		zrwrite(0xffffffffUL,ZORAN_ISR);
2038     
2039     		/* free it */
2040     		free_irq(ztv->dev->irq,ztv);
2041      
2042         		/* unregister i2c_bus */
2043     		i2c_unregister_bus((&ztv->i2c));
2044     
2045     		/* unmap and free memory */
2046     		if (ztv->zoran_mem)
2047     			iounmap(ztv->zoran_mem);
2048     
2049     		video_unregister_device(&ztv->video_dev);
2050     		video_unregister_device(&ztv->vbi_dev);
2051     	}
2052     }
2053     
2054     void __exit zr36120_exit(void)
2055     {
2056     	release_zoran(zoran_cards);
2057     }
2058     
2059     int __init zr36120_init(void)
2060     {
2061     	int	card;
2062      
2063     	handle_chipset();
2064     	zoran_cards = find_zoran();
2065     	if (zoran_cards<0)
2066     		/* no cards found, no need for a driver */
2067     		return -EIO;
2068     
2069     	/* initialize Zorans */
2070     	for (card=0; card<zoran_cards; card++) {
2071     		if (init_zoran(card)<0) {
2072     			/* only release the zorans we have registered */
2073     			release_zoran(card);
2074     			return -EIO;
2075     		} 
2076     	}
2077     	return 0;
2078     }
2079     
2080     module_init(zr36120_init);
2081     module_exit(zr36120_exit);
2082