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