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