initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / media / video / zr36120.c
blob9b55341baf4099dfa522b9cf3ada64003f7026ce
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/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>
39 #include <asm/uaccess.h>
41 #include "tuner.h"
42 #include "zr36120.h"
43 #include "zr36120_mem.h"
45 /* mark an required function argument unused - lintism */
46 #define UNUSED(x) (void)(x)
48 /* sensible default */
49 #ifndef CARDTYPE
50 #define CARDTYPE 0
51 #endif
53 /* Anybody who uses more than four? */
54 #define ZORAN_MAX 4
56 static unsigned int triton1=0; /* triton1 chipset? */
57 static unsigned int cardtype[ZORAN_MAX]={ [ 0 ... ZORAN_MAX-1 ] = CARDTYPE };
58 static int video_nr = -1;
59 static int vbi_nr = -1;
61 static struct pci_device_id zr36120_pci_tbl[] = {
62 { PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120,
63 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
64 { 0 }
66 MODULE_DEVICE_TABLE(pci, zr36120_pci_tbl);
68 MODULE_AUTHOR("Pauline Middelink <middelin@polyware.nl>");
69 MODULE_DESCRIPTION("Zoran ZR36120 based framegrabber");
70 MODULE_LICENSE("GPL");
72 MODULE_PARM(triton1,"i");
73 MODULE_PARM(cardtype,"1-" __MODULE_STRING(ZORAN_MAX) "i");
74 MODULE_PARM(video_nr,"i");
75 MODULE_PARM(vbi_nr,"i");
77 static int zoran_cards;
78 static struct zoran zorans[ZORAN_MAX];
81 * the meaning of each element can be found in zr36120.h
82 * Determining the value of gpdir/gpval can be tricky. The
83 * best way is to run the card under the original software
84 * and read the values from the general purpose registers
85 * 0x28 and 0x2C. How you do that is left as an exercise
86 * to the impatient reader :)
88 #define T 1 /* to separate the bools from the ints */
89 #define F 0
90 static struct tvcard tvcards[] = {
91 /* reported working by <middelin@polyware.nl> */
92 /*0*/ { "Trust Victor II",
93 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
94 /* reported working by <Michael.Paxton@aihw.gov.au> */
95 /*1*/ { "Aitech WaveWatcher TV-PCI",
96 3, 0, T, F, T, T, 0x7F, 0x80, { 1, TUNER(3), SVHS(6) }, { 0 } },
97 /* reported working by ? */
98 /*2*/ { "Genius Video Wonder PCI Video Capture Card",
99 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
100 /* reported working by <Pascal.Gabriel@wanadoo.fr> */
101 /*3*/ { "Guillemot Maxi-TV PCI",
102 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
103 /* reported working by "Craig Whitmore <lennon@igrin.co.nz> */
104 /*4*/ { "Quadrant Buster",
105 3, 3, T, F, T, T, 0x7F, 0x80, { SVHS(1), TUNER(2), 3 }, { 1, 2, 3 } },
106 /* a debug entry which has all inputs mapped */
107 /*5*/ { "ZR36120 based framegrabber (all inputs enabled)",
108 6, 0, T, T, T, T, 0x7F, 0x80, { 1, 2, 3, 4, 5, 6 }, { 0 } }
110 #undef T
111 #undef F
112 #define NRTVCARDS (sizeof(tvcards)/sizeof(tvcards[0]))
114 #ifdef __sparc__
115 #define ENDIANESS 0
116 #else
117 #define ENDIANESS ZORAN_VFEC_LE
118 #endif
120 static struct { const char name[8]; uint mode; uint bpp; } palette2fmt[] = {
121 /* n/a */ { "n/a", 0, 0 },
122 /* GREY */ { "GRAY", 0, 0 },
123 /* HI240 */ { "HI240", 0, 0 },
124 /* RGB565 */ { "RGB565", ZORAN_VFEC_RGB_RGB565|ENDIANESS, 2 },
125 /* RGB24 */ { "RGB24", ZORAN_VFEC_RGB_RGB888|ENDIANESS|ZORAN_VFEC_PACK24, 3 },
126 /* RGB32 */ { "RGB32", ZORAN_VFEC_RGB_RGB888|ENDIANESS, 4 },
127 /* RGB555 */ { "RGB555", ZORAN_VFEC_RGB_RGB555|ENDIANESS, 2 },
128 /* YUV422 */ { "YUV422", ZORAN_VFEC_RGB_YUV422|ENDIANESS, 2 },
129 /* YUYV */ { "YUYV", 0, 0 },
130 /* UYVY */ { "UYVY", 0, 0 },
131 /* YUV420 */ { "YUV420", 0, 0 },
132 /* YUV411 */ { "YUV411", 0, 0 },
133 /* RAW */ { "RAW", 0, 0 },
134 /* YUV422P */ { "YUV422P", 0, 0 },
135 /* YUV411P */ { "YUV411P", 0, 0 }};
136 #define NRPALETTES (sizeof(palette2fmt)/sizeof(palette2fmt[0]))
137 #undef ENDIANESS
139 /* ----------------------------------------------------------------------- */
140 /* ZORAN chipset detector */
141 /* shamelessly stolen from bttv.c */
142 /* Reason for beeing here: we need to detect if we are running on a */
143 /* Triton based chipset, and if so, enable a certain bit */
144 /* ----------------------------------------------------------------------- */
145 static
146 void __init handle_chipset(void)
148 struct pci_dev *dev = NULL;
150 /* Just in case some nut set this to something dangerous */
151 if (triton1)
152 triton1 = ZORAN_VDC_TRICOM;
154 while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437, dev)))
156 printk(KERN_INFO "zoran: Host bridge 82437FX Triton PIIX\n");
157 triton1 = ZORAN_VDC_TRICOM;
161 /* ----------------------------------------------------------------------- */
162 /* ZORAN functions */
163 /* ----------------------------------------------------------------------- */
165 static void zoran_set_geo(struct zoran* ztv, struct vidinfo* i);
167 #if 0 /* unused */
168 static
169 void zoran_dump(struct zoran *ztv)
171 char str[256];
172 char *p=str; /* shut up, gcc! */
173 int i;
175 for (i=0; i<0x60; i+=4) {
176 if ((i % 16) == 0) {
177 if (i) printk("%s\n",str);
178 p = str;
179 p+= sprintf(str, KERN_DEBUG " %04x: ",i);
181 p += sprintf(p, "%08x ",zrread(i));
184 #endif /* unused */
186 static
187 void reap_states(struct zoran* ztv)
189 /* count frames */
190 ztv->fieldnr++;
193 * Are we busy at all?
194 * This depends on if there is a workqueue AND the
195 * videotransfer is enabled on the chip...
197 if (ztv->workqueue && (zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
199 struct vidinfo* newitem;
201 /* did we get a complete frame? */
202 if (zrread(ZORAN_VSTR) & ZORAN_VSTR_GRAB)
203 return;
205 DEBUG(printk(CARD_DEBUG "completed %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
207 /* we are done with this buffer, tell everyone */
208 ztv->workqueue->status = FBUFFER_DONE;
209 ztv->workqueue->fieldnr = ztv->fieldnr;
210 /* not good, here for BTTV_FIELDNR reasons */
211 ztv->lastfieldnr = ztv->fieldnr;
213 switch (ztv->workqueue->kindof) {
214 case FBUFFER_GRAB:
215 wake_up_interruptible(&ztv->grabq);
216 break;
217 case FBUFFER_VBI:
218 wake_up_interruptible(&ztv->vbiq);
219 break;
220 default:
221 printk(CARD_INFO "somebody killed the workqueue (kindof=%d)!\n",CARD,ztv->workqueue->kindof);
224 /* item completed, skip to next item in queue */
225 write_lock(&ztv->lock);
226 newitem = ztv->workqueue->next;
227 ztv->workqueue->next = 0; /* mark completed */
228 ztv->workqueue = newitem;
229 write_unlock(&ztv->lock);
233 * ok, so it seems we have nothing in progress right now.
234 * Lets see if we can find some work.
236 if (ztv->workqueue)
238 struct vidinfo* newitem;
239 again:
241 DEBUG(printk(CARD_DEBUG "starting %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
243 /* loadup the frame settings */
244 read_lock(&ztv->lock);
245 zoran_set_geo(ztv,ztv->workqueue);
246 read_unlock(&ztv->lock);
248 switch (ztv->workqueue->kindof) {
249 case FBUFFER_GRAB:
250 case FBUFFER_VBI:
251 zrand(~ZORAN_OCR_OVLEN, ZORAN_OCR);
252 zror(ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
253 zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
255 /* start single-shot grab */
256 zror(ZORAN_VSTR_GRAB, ZORAN_VSTR);
257 break;
258 default:
259 printk(CARD_INFO "what is this doing on the queue? (kindof=%d)\n",CARD,ztv->workqueue->kindof);
260 write_lock(&ztv->lock);
261 newitem = ztv->workqueue->next;
262 ztv->workqueue->next = 0;
263 ztv->workqueue = newitem;
264 write_unlock(&ztv->lock);
265 if (newitem)
266 goto again; /* yeah, sure.. */
268 /* bye for now */
269 return;
271 DEBUG(printk(CARD_DEBUG "nothing in queue\n",CARD));
274 * What? Even the workqueue is empty? Am i really here
275 * for nothing? Did i come all that way to... do nothing?
278 /* do we need to overlay? */
279 if (test_bit(STATE_OVERLAY, &ztv->state))
281 /* are we already overlaying? */
282 if (!(zrread(ZORAN_OCR) & ZORAN_OCR_OVLEN) ||
283 !(zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
285 DEBUG(printk(CARD_DEBUG "starting overlay\n",CARD));
287 read_lock(&ztv->lock);
288 zoran_set_geo(ztv,&ztv->overinfo);
289 read_unlock(&ztv->lock);
291 zror(ZORAN_OCR_OVLEN, ZORAN_OCR);
292 zrand(~ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
293 zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
297 * leave overlaying on, but turn interrupts off.
299 zrand(~ZORAN_ICR_EN,ZORAN_ICR);
300 return;
303 /* do we have any VBI idle time processing? */
304 if (test_bit(STATE_VBI, &ztv->state))
306 struct vidinfo* item;
307 struct vidinfo* lastitem;
309 /* protect the workqueue */
310 write_lock(&ztv->lock);
311 lastitem = ztv->workqueue;
312 if (lastitem)
313 while (lastitem->next) lastitem = lastitem->next;
314 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
315 if (item->next == 0 && item->status == FBUFFER_FREE)
317 DEBUG(printk(CARD_DEBUG "%p added to queue\n",CARD,item));
318 item->status = FBUFFER_BUSY;
319 if (!lastitem)
320 ztv->workqueue = item;
321 else
322 lastitem->next = item;
323 lastitem = item;
325 write_unlock(&ztv->lock);
326 if (ztv->workqueue)
327 goto again; /* hey, _i_ graduated :) */
331 * Then we must be realy IDLE
333 DEBUG(printk(CARD_DEBUG "turning off\n",CARD));
334 /* nothing further to do, disable DMA and further IRQs */
335 zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
336 zrand(~ZORAN_ICR_EN,ZORAN_ICR);
339 static
340 void zoran_irq(int irq, void *dev_id, struct pt_regs * regs)
342 u32 stat,estat;
343 int count = 0;
344 struct zoran *ztv = (struct zoran *)dev_id;
346 UNUSED(irq); UNUSED(regs);
347 for (;;) {
348 /* get/clear interrupt status bits */
349 stat=zrread(ZORAN_ISR);
350 estat=stat & zrread(ZORAN_ICR);
351 if (!estat)
352 return;
353 zrwrite(estat,ZORAN_ISR);
354 IDEBUG(printk(CARD_DEBUG "estat %08x\n",CARD,estat));
355 IDEBUG(printk(CARD_DEBUG " stat %08x\n",CARD,stat));
357 if (estat & ZORAN_ISR_CODE)
359 IDEBUG(printk(CARD_DEBUG "CodReplIRQ\n",CARD));
361 if (estat & ZORAN_ISR_GIRQ0)
363 IDEBUG(printk(CARD_DEBUG "GIRQ0\n",CARD));
364 if (!ztv->card->usegirq1)
365 reap_states(ztv);
367 if (estat & ZORAN_ISR_GIRQ1)
369 IDEBUG(printk(CARD_DEBUG "GIRQ1\n",CARD));
370 if (ztv->card->usegirq1)
371 reap_states(ztv);
374 count++;
375 if (count > 10)
376 printk(CARD_ERR "irq loop %d (%x)\n",CARD,count,estat);
377 if (count > 20)
379 zrwrite(0, ZORAN_ICR);
380 printk(CARD_ERR "IRQ lockup, cleared int mask\n",CARD);
385 static
386 int zoran_muxsel(struct zoran* ztv, int channel, int norm)
388 int rv;
390 /* set the new video norm */
391 rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
392 if (rv)
393 return rv;
394 ztv->norm = norm;
396 /* map the given channel to the cards decoder's channel */
397 channel = ztv->card->video_mux[channel] & CHANNEL_MASK;
399 /* set the new channel */
400 rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &channel);
401 return rv;
404 /* Tell the interrupt handler what to to. */
405 static
406 void zoran_cap(struct zoran* ztv, int on)
408 DEBUG(printk(CARD_DEBUG "zoran_cap(%d) state=%x\n",CARD,on,ztv->state));
410 if (on) {
411 ztv->running = 1;
414 * turn interrupts (back) on. The DMA will be enabled
415 * inside the irq handler when it detects a restart.
417 zror(ZORAN_ICR_EN,ZORAN_ICR);
419 else {
421 * turn both interrupts and DMA off
423 zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
424 zrand(~ZORAN_ICR_EN,ZORAN_ICR);
426 ztv->running = 0;
430 static ulong dmask[] = {
431 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8,
432 0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80,
433 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
434 0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000,
435 0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000,
436 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
437 0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000,
438 0xF0000000, 0xE0000000, 0xC0000000, 0x80000000
441 static
442 void zoran_built_overlay(struct zoran* ztv, int count, struct video_clip *vcp)
444 ulong* mtop;
445 int ystep = (ztv->vidXshift + ztv->vidWidth+31)/32; /* next DWORD */
446 int i;
448 DEBUG(printk(KERN_DEBUG " overlay at %p, ystep=%d, clips=%d\n",ztv->overinfo.overlay,ystep,count));
450 for (i=0; i<count; i++) {
451 struct video_clip *vp = vcp+i;
452 UNUSED(vp);
453 DEBUG(printk(KERN_DEBUG " %d: clip(%d,%d,%d,%d)\n", i,vp->x,vp->y,vp->width,vp->height));
457 * activate the visible portion of the screen
458 * Note we take some shortcuts here, because we
459 * know the width can never be < 32. (I.e. a DWORD)
460 * We also assume the overlay starts somewhere in
461 * the FIRST dword.
464 int start = ztv->vidXshift;
465 ulong firstd = dmask[start];
466 ulong lastd = ~dmask[(start + ztv->overinfo.w) & 31];
467 mtop = ztv->overinfo.overlay;
468 for (i=0; i<ztv->overinfo.h; i++) {
469 int w = ztv->vidWidth;
470 ulong* line = mtop;
471 if (start & 31) {
472 *line++ = firstd;
473 w -= 32-(start&31);
475 memset(line, ~0, w/8);
476 if (w & 31)
477 line[w/32] = lastd;
478 mtop += ystep;
482 /* process clipping regions */
483 for (i=0; i<count; i++) {
484 int h;
485 if (vcp->x < 0 || (uint)vcp->x > ztv->overinfo.w ||
486 vcp->y < 0 || vcp->y > ztv->overinfo.h ||
487 vcp->width < 0 || (uint)(vcp->x+vcp->width) > ztv->overinfo.w ||
488 vcp->height < 0 || (vcp->y+vcp->height) > ztv->overinfo.h)
490 DEBUG(printk(CARD_DEBUG "invalid clipzone (%d,%d,%d,%d) not in (0,0,%d,%d), adapting\n",CARD,vcp->x,vcp->y,vcp->width,vcp->height,ztv->overinfo.w,ztv->overinfo.h));
491 if (vcp->x < 0) vcp->x = 0;
492 if ((uint)vcp->x > ztv->overinfo.w) vcp->x = ztv->overinfo.w;
493 if (vcp->y < 0) vcp->y = 0;
494 if (vcp->y > ztv->overinfo.h) vcp->y = ztv->overinfo.h;
495 if (vcp->width < 0) vcp->width = 0;
496 if ((uint)(vcp->x+vcp->width) > ztv->overinfo.w) vcp->width = ztv->overinfo.w - vcp->x;
497 if (vcp->height < 0) vcp->height = 0;
498 if (vcp->y+vcp->height > ztv->overinfo.h) vcp->height = ztv->overinfo.h - vcp->y;
499 // continue;
502 mtop = &ztv->overinfo.overlay[vcp->y*ystep];
503 for (h=0; h<=vcp->height; h++) {
504 int w;
505 int x = ztv->vidXshift + vcp->x;
506 for (w=0; w<=vcp->width; w++) {
507 clear_bit(x&31, &mtop[x/32]);
508 x++;
510 mtop += ystep;
512 ++vcp;
515 mtop = ztv->overinfo.overlay;
516 zrwrite(virt_to_bus(mtop), ZORAN_MTOP);
517 zrwrite(virt_to_bus(mtop+ystep), ZORAN_MBOT);
518 zraor((ztv->vidInterlace*ystep)<<0,~ZORAN_OCR_MASKSTRIDE,ZORAN_OCR);
521 struct tvnorm
523 u16 Wt, Wa, Ht, Ha, HStart, VStart;
526 static struct tvnorm tvnorms[] = {
527 /* PAL-BDGHI */
528 /* { 864, 720, 625, 576, 131, 21 },*/
529 /*00*/ { 864, 768, 625, 576, 81, 17 },
530 /* NTSC */
531 /*01*/ { 858, 720, 525, 480, 121, 10 },
532 /* SECAM */
533 /*02*/ { 864, 720, 625, 576, 131, 21 },
534 /* BW50 */
535 /*03*/ { 864, 720, 625, 576, 131, 21 },
536 /* BW60 */
537 /*04*/ { 858, 720, 525, 480, 121, 10 }
539 #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
542 * Program the chip for a setup as described in the vidinfo struct.
544 * Side-effects: calculates vidXshift, vidInterlace,
545 * vidHeight, vidWidth which are used in a later stage
546 * to calculate the overlay mask
548 * This is an internal function, as such it does not check the
549 * validity of the struct members... Spectaculair crashes will
550 * follow /very/ quick when you're wrong and the chip right :)
552 static
553 void zoran_set_geo(struct zoran* ztv, struct vidinfo* i)
555 ulong top, bot;
556 int stride;
557 int winWidth, winHeight;
558 int maxWidth, maxHeight, maxXOffset, maxYOffset;
559 long vfec;
561 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 * make sure the DMA transfers are inhibited during our
565 * reprogramming of the chip
567 zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
569 maxWidth = tvnorms[ztv->norm].Wa;
570 maxHeight = tvnorms[ztv->norm].Ha/2;
571 maxXOffset = tvnorms[ztv->norm].HStart;
572 maxYOffset = tvnorms[ztv->norm].VStart;
574 /* setup vfec register (keep ExtFl,TopField and VCLKPol settings) */
575 vfec = (zrread(ZORAN_VFEC) & (ZORAN_VFEC_EXTFL|ZORAN_VFEC_TOPFIELD|ZORAN_VFEC_VCLKPOL)) |
576 (palette2fmt[i->format].mode & (ZORAN_VFEC_RGB|ZORAN_VFEC_ERRDIF|ZORAN_VFEC_LE|ZORAN_VFEC_PACK24));
579 * Set top, bottom ptrs. Since these must be DWORD aligned,
580 * possible adjust the x and the width of the window.
581 * so the endposition stay the same. The vidXshift will make
582 * sure we are not writing pixels before the requested x.
584 ztv->vidXshift = 0;
585 winWidth = i->w;
586 if (winWidth < 0)
587 winWidth = -winWidth;
588 top = i->busadr + i->x*i->bpp + i->y*i->bpl;
589 if (top & 3) {
590 ztv->vidXshift = (top & 3) / i->bpp;
591 winWidth += ztv->vidXshift;
592 DEBUG(printk(KERN_DEBUG " window-x shifted %d pixels left\n",ztv->vidXshift));
593 top &= ~3;
597 * bottom points to next frame but in interleaved mode we want
598 * to 'mix' the 2 frames to one capture, so 'bot' points to one
599 * (physical) line below the top line.
601 bot = top + i->bpl;
602 zrwrite(top,ZORAN_VTOP);
603 zrwrite(bot,ZORAN_VBOT);
606 * Make sure the winWidth is DWORD aligned too,
607 * thereby automaticly making sure the stride to the
608 * next line is DWORD aligned too (as required by spec).
610 if ((winWidth*i->bpp) & 3) {
611 DEBUG(printk(KERN_DEBUG " window-width enlarged by %d pixels\n",(winWidth*i->bpp) & 3));
612 winWidth += (winWidth*i->bpp) & 3;
615 /* determine the DispMode and stride */
616 if (i->h >= 0 && i->h <= maxHeight) {
617 /* single frame grab suffices for this height. */
618 vfec |= ZORAN_VFEC_DISPMOD;
619 ztv->vidInterlace = 0;
620 stride = i->bpl - (winWidth*i->bpp);
621 winHeight = i->h;
623 else {
624 /* interleaving needed for this height */
625 ztv->vidInterlace = 1;
626 stride = i->bpl*2 - (winWidth*i->bpp);
627 winHeight = i->h/2;
629 if (winHeight < 0) /* can happen for VBI! */
630 winHeight = -winHeight;
632 /* safety net, sometimes bpl is too short??? */
633 if (stride<0) {
634 DEBUG(printk(CARD_DEBUG "WARNING stride = %d\n",CARD,stride));
635 stride = 0;
638 zraor((winHeight<<12)|(winWidth<<0),~(ZORAN_VDC_VIDWINHT|ZORAN_VDC_VIDWINWID), ZORAN_VDC);
639 zraor(stride<<16,~ZORAN_VSTR_DISPSTRIDE,ZORAN_VSTR);
641 /* remember vidWidth, vidHeight for overlay calculations */
642 ztv->vidWidth = winWidth;
643 ztv->vidHeight = winHeight;
644 DEBUG(printk(KERN_DEBUG " top=%08lx, bottom=%08lx\n",top,bot));
645 DEBUG(printk(KERN_DEBUG " winWidth=%d, winHeight=%d\n",winWidth,winHeight));
646 DEBUG(printk(KERN_DEBUG " maxWidth=%d, maxHeight=%d\n",maxWidth,maxHeight));
647 DEBUG(printk(KERN_DEBUG " stride=%d\n",stride));
650 * determine horizontal scales and crops
652 if (i->w < 0) {
653 int Hstart = 1;
654 int Hend = Hstart + winWidth;
655 DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Hstart, Hend));
656 zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
658 else {
659 int Wa = maxWidth;
660 int X = (winWidth*64+Wa-1)/Wa;
661 int We = winWidth*64/X;
662 int HorDcm = 64-X;
663 int hcrop1 = 2*(Wa-We)/4;
665 * BUGFIX: Juha Nurmela <junki@qn-lpr2-165.quicknet.inet.fi>
666 * found the solution to the color phase shift.
667 * See ChangeLog for the full explanation)
669 int Hstart = (maxXOffset + hcrop1) | 1;
670 int Hend = Hstart + We - 1;
672 DEBUG(printk(KERN_DEBUG " X: scale=%d, start=%d, end=%d\n", HorDcm, Hstart, Hend));
674 zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
675 vfec |= HorDcm<<14;
677 if (HorDcm<16)
678 vfec |= ZORAN_VFEC_HFILTER_1; /* no filter */
679 else if (HorDcm<32)
680 vfec |= ZORAN_VFEC_HFILTER_3; /* 3 tap filter */
681 else if (HorDcm<48)
682 vfec |= ZORAN_VFEC_HFILTER_4; /* 4 tap filter */
683 else vfec |= ZORAN_VFEC_HFILTER_5; /* 5 tap filter */
687 * Determine vertical scales and crops
689 * when height is negative, we want to read starting at line 0
690 * One day someone might need access to these lines...
692 if (i->h < 0) {
693 int Vstart = 0;
694 int Vend = Vstart + winHeight;
695 DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Vstart, Vend));
696 zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
698 else {
699 int Ha = maxHeight;
700 int Y = (winHeight*64+Ha-1)/Ha;
701 int He = winHeight*64/Y;
702 int VerDcm = 64-Y;
703 int vcrop1 = 2*(Ha-He)/4;
704 int Vstart = maxYOffset + vcrop1;
705 int Vend = Vstart + He - 1;
707 DEBUG(printk(KERN_DEBUG " Y: scale=%d, start=%d, end=%d\n", VerDcm, Vstart, Vend));
708 zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
709 vfec |= VerDcm<<8;
712 DEBUG(printk(KERN_DEBUG " F: format=%d(=%s)\n",i->format,palette2fmt[i->format].name));
714 /* setup the requested format */
715 zrwrite(vfec, ZORAN_VFEC);
718 static
719 void zoran_common_open(struct zoran* ztv, int flags)
721 UNUSED(flags);
723 /* already opened? */
724 if (ztv->users++ != 0)
725 return;
727 /* unmute audio */
728 /* /what/ audio? */
730 ztv->state = 0;
732 /* setup the encoder to the initial values */
733 ztv->picture.colour=254<<7;
734 ztv->picture.brightness=128<<8;
735 ztv->picture.hue=128<<8;
736 ztv->picture.contrast=216<<7;
737 i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &ztv->picture);
739 /* default to the composite input since my camera is there */
740 zoran_muxsel(ztv, 0, VIDEO_MODE_PAL);
743 static
744 void zoran_common_close(struct zoran* ztv)
746 if (--ztv->users != 0)
747 return;
749 /* mute audio */
750 /* /what/ audio? */
752 /* stop the chip */
753 zoran_cap(ztv, 0);
757 * Open a zoran card. Right now the flags are just a hack
759 static int zoran_open(struct video_device *dev, int flags)
761 struct zoran *ztv = (struct zoran*)dev;
762 struct vidinfo* item;
763 char* pos;
765 DEBUG(printk(CARD_DEBUG "open(dev,%d)\n",CARD,flags));
767 /*********************************************
768 * We really should be doing lazy allocing...
769 *********************************************/
770 /* allocate a frame buffer */
771 if (!ztv->fbuffer)
772 ztv->fbuffer = bmalloc(ZORAN_MAX_FBUFSIZE);
773 if (!ztv->fbuffer) {
774 /* could not get a buffer, bail out */
775 return -ENOBUFS;
777 /* at this time we _always_ have a framebuffer */
778 memset(ztv->fbuffer,0,ZORAN_MAX_FBUFSIZE);
780 if (!ztv->overinfo.overlay)
781 ztv->overinfo.overlay = (void*)kmalloc(1024*1024/8, GFP_KERNEL);
782 if (!ztv->overinfo.overlay) {
783 /* could not get an overlay buffer, bail out */
784 bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
785 return -ENOBUFS;
787 /* at this time we _always_ have a overlay */
789 /* clear buffer status, and give them a DMAable address */
790 pos = ztv->fbuffer;
791 for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
793 item->status = FBUFFER_FREE;
794 item->memadr = pos;
795 item->busadr = virt_to_bus(pos);
796 pos += ZORAN_MAX_FBUFFER;
799 /* do the common part of all open's */
800 zoran_common_open(ztv, flags);
802 return 0;
805 static
806 void zoran_close(struct video_device* dev)
808 struct zoran *ztv = (struct zoran*)dev;
810 DEBUG(printk(CARD_DEBUG "close(dev)\n",CARD));
812 /* driver specific closure */
813 clear_bit(STATE_OVERLAY, &ztv->state);
815 zoran_common_close(ztv);
818 * This is sucky but right now I can't find a good way to
819 * be sure its safe to free the buffer. We wait 5-6 fields
820 * which is more than sufficient to be sure.
822 current->state = TASK_UNINTERRUPTIBLE;
823 schedule_timeout(HZ/10); /* Wait 1/10th of a second */
825 /* free the allocated framebuffer */
826 if (ztv->fbuffer)
827 bfree( ztv->fbuffer, ZORAN_MAX_FBUFSIZE );
828 ztv->fbuffer = 0;
829 if (ztv->overinfo.overlay)
830 kfree( ztv->overinfo.overlay );
831 ztv->overinfo.overlay = 0;
836 * This read function could be used reentrant in a SMP situation.
838 * This is made possible by the spinlock which is kept till we
839 * found and marked a buffer for our own use. The lock must
840 * be released as soon as possible to prevent lock contention.
842 static
843 long zoran_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
845 struct zoran *ztv = (struct zoran*)dev;
846 unsigned long max;
847 struct vidinfo* unused = 0;
848 struct vidinfo* done = 0;
850 DEBUG(printk(CARD_DEBUG "zoran_read(%p,%ld,%d)\n",CARD,buf,count,nonblock));
852 /* find ourself a free or completed buffer */
853 for (;;) {
854 struct vidinfo* item;
856 write_lock_irq(&ztv->lock);
857 for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
859 if (!unused && item->status == FBUFFER_FREE)
860 unused = item;
861 if (!done && item->status == FBUFFER_DONE)
862 done = item;
864 if (done || unused)
865 break;
867 /* no more free buffers, wait for them. */
868 write_unlock_irq(&ztv->lock);
869 if (nonblock)
870 return -EWOULDBLOCK;
871 interruptible_sleep_on(&ztv->grabq);
872 if (signal_pending(current))
873 return -EINTR;
876 /* Do we have 'ready' data? */
877 if (!done) {
878 /* no? than this will take a while... */
879 if (nonblock) {
880 write_unlock_irq(&ztv->lock);
881 return -EWOULDBLOCK;
884 /* mark the unused buffer as wanted */
885 unused->status = FBUFFER_BUSY;
886 unused->w = 320;
887 unused->h = 240;
888 unused->format = VIDEO_PALETTE_RGB24;
889 unused->bpp = palette2fmt[unused->format].bpp;
890 unused->bpl = unused->w * unused->bpp;
891 unused->next = 0;
892 { /* add to tail of queue */
893 struct vidinfo* oldframe = ztv->workqueue;
894 if (!oldframe) ztv->workqueue = unused;
895 else {
896 while (oldframe->next) oldframe = oldframe->next;
897 oldframe->next = unused;
900 write_unlock_irq(&ztv->lock);
902 /* tell the state machine we want it filled /NOW/ */
903 zoran_cap(ztv, 1);
905 /* wait till this buffer gets grabbed */
906 while (unused->status == FBUFFER_BUSY) {
907 interruptible_sleep_on(&ztv->grabq);
908 /* see if a signal did it */
909 if (signal_pending(current))
910 return -EINTR;
912 done = unused;
914 else
915 write_unlock_irq(&ztv->lock);
917 /* Yes! we got data! */
918 max = done->bpl * done->h;
919 if (count > max)
920 count = max;
921 if (copy_to_user((void*)buf, done->memadr, count))
922 count = -EFAULT;
924 /* keep the engine running */
925 done->status = FBUFFER_FREE;
926 // zoran_cap(ztv,1);
928 /* tell listeners this buffer became free */
929 wake_up_interruptible(&ztv->grabq);
931 /* goodbye */
932 DEBUG(printk(CARD_DEBUG "zoran_read() returns %lu\n",CARD,count));
933 return count;
936 static
937 long zoran_write(struct video_device* dev, const char* buf, unsigned long count, int nonblock)
939 struct zoran *ztv = (struct zoran *)dev;
940 UNUSED(ztv); UNUSED(dev); UNUSED(buf); UNUSED(count); UNUSED(nonblock);
941 DEBUG(printk(CARD_DEBUG "zoran_write\n",CARD));
942 return -EINVAL;
945 static
946 unsigned int zoran_poll(struct video_device *dev, struct file *file, poll_table *wait)
948 struct zoran *ztv = (struct zoran *)dev;
949 struct vidinfo* item;
950 unsigned int mask = 0;
952 poll_wait(file, &ztv->grabq, wait);
954 for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
955 if (item->status == FBUFFER_DONE)
957 mask |= (POLLIN | POLLRDNORM);
958 break;
961 DEBUG(printk(CARD_DEBUG "zoran_poll()=%x\n",CARD,mask));
963 return mask;
966 /* append a new clipregion to the vector of video_clips */
967 static
968 void new_clip(struct video_window* vw, struct video_clip* vcp, int x, int y, int w, int h)
970 vcp[vw->clipcount].x = x;
971 vcp[vw->clipcount].y = y;
972 vcp[vw->clipcount].width = w;
973 vcp[vw->clipcount].height = h;
974 vw->clipcount++;
977 static
978 int zoran_ioctl(struct video_device* dev, unsigned int cmd, void *arg)
980 struct zoran* ztv = (struct zoran*)dev;
982 switch (cmd) {
983 case VIDIOCGCAP:
985 struct video_capability c;
986 DEBUG(printk(CARD_DEBUG "VIDIOCGCAP\n",CARD));
988 strcpy(c.name,ztv->video_dev.name);
989 c.type = VID_TYPE_CAPTURE|
990 VID_TYPE_OVERLAY|
991 VID_TYPE_CLIPPING|
992 VID_TYPE_FRAMERAM|
993 VID_TYPE_SCALES;
994 if (ztv->have_tuner)
995 c.type |= VID_TYPE_TUNER;
996 if (ztv->have_decoder) {
997 c.channels = ztv->card->video_inputs;
998 c.audios = ztv->card->audio_inputs;
999 } else
1000 /* no decoder -> no channels */
1001 c.channels = c.audios = 0;
1002 c.maxwidth = 768;
1003 c.maxheight = 576;
1004 c.minwidth = 32;
1005 c.minheight = 32;
1006 if (copy_to_user(arg,&c,sizeof(c)))
1007 return -EFAULT;
1008 break;
1011 case VIDIOCGCHAN:
1013 struct video_channel v;
1014 int mux;
1015 if (copy_from_user(&v, arg,sizeof(v)))
1016 return -EFAULT;
1017 DEBUG(printk(CARD_DEBUG "VIDIOCGCHAN(%d)\n",CARD,v.channel));
1018 v.flags=VIDEO_VC_AUDIO
1019 #ifdef VIDEO_VC_NORM
1020 |VIDEO_VC_NORM
1021 #endif
1023 v.tuners=0;
1024 v.type=VIDEO_TYPE_CAMERA;
1025 #ifdef I_EXPECT_POSSIBLE_NORMS_IN_THE_API
1026 v.norm=VIDEO_MODE_PAL|
1027 VIDEO_MODE_NTSC|
1028 VIDEO_MODE_SECAM;
1029 #else
1030 v.norm=VIDEO_MODE_PAL;
1031 #endif
1032 /* too many inputs? no decoder -> no channels */
1033 if (!ztv->have_decoder || v.channel < 0 || v.channel >= ztv->card->video_inputs)
1034 return -EINVAL;
1036 /* now determine the name of the channel */
1037 mux = ztv->card->video_mux[v.channel];
1038 if (mux & IS_TUNER) {
1039 /* lets assume only one tuner, yes? */
1040 strcpy(v.name,"Television");
1041 v.type = VIDEO_TYPE_TV;
1042 if (ztv->have_tuner) {
1043 v.flags |= VIDEO_VC_TUNER;
1044 v.tuners = 1;
1047 else if (mux & IS_SVHS)
1048 sprintf(v.name,"S-Video-%d",v.channel);
1049 else
1050 sprintf(v.name,"CVBS-%d",v.channel);
1052 if (copy_to_user(arg,&v,sizeof(v)))
1053 return -EFAULT;
1054 break;
1056 case VIDIOCSCHAN:
1057 { /* set video channel */
1058 struct video_channel v;
1059 if (copy_from_user(&v, arg,sizeof(v)))
1060 return -EFAULT;
1061 DEBUG(printk(CARD_DEBUG "VIDIOCSCHAN(%d,%d)\n",CARD,v.channel,v.norm));
1063 /* too many inputs? no decoder -> no channels */
1064 if (!ztv->have_decoder || v.channel >= ztv->card->video_inputs || v.channel < 0)
1065 return -EINVAL;
1067 if (v.norm != VIDEO_MODE_PAL &&
1068 v.norm != VIDEO_MODE_NTSC &&
1069 v.norm != VIDEO_MODE_SECAM &&
1070 v.norm != VIDEO_MODE_AUTO)
1071 return -EOPNOTSUPP;
1073 /* make it happen, nr1! */
1074 return zoran_muxsel(ztv,v.channel,v.norm);
1077 case VIDIOCGTUNER:
1079 struct video_tuner v;
1080 if (copy_from_user(&v, arg,sizeof(v)))
1081 return -EFAULT;
1082 DEBUG(printk(CARD_DEBUG "VIDIOCGTUNER(%d)\n",CARD,v.tuner));
1084 /* Only no or one tuner for now */
1085 if (!ztv->have_tuner || v.tuner)
1086 return -EINVAL;
1088 strcpy(v.name,"Television");
1089 v.rangelow = 0;
1090 v.rangehigh = ~0;
1091 v.flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1092 v.mode = ztv->norm;
1093 v.signal = 0xFFFF; /* unknown */
1095 if (copy_to_user(arg,&v,sizeof(v)))
1096 return -EFAULT;
1097 break;
1099 case VIDIOCSTUNER:
1101 struct video_tuner v;
1102 if (copy_from_user(&v, arg, sizeof(v)))
1103 return -EFAULT;
1104 DEBUG(printk(CARD_DEBUG "VIDIOCSTUNER(%d,%d)\n",CARD,v.tuner,v.mode));
1106 /* Only no or one tuner for now */
1107 if (!ztv->have_tuner || v.tuner)
1108 return -EINVAL;
1110 /* and it only has certain valid modes */
1111 if( v.mode != VIDEO_MODE_PAL &&
1112 v.mode != VIDEO_MODE_NTSC &&
1113 v.mode != VIDEO_MODE_SECAM)
1114 return -EOPNOTSUPP;
1116 /* engage! */
1117 return zoran_muxsel(ztv,v.tuner,v.mode);
1120 case VIDIOCGPICT:
1122 struct video_picture p = ztv->picture;
1123 DEBUG(printk(CARD_DEBUG "VIDIOCGPICT\n",CARD));
1124 p.depth = ztv->depth;
1125 switch (p.depth) {
1126 case 8: p.palette=VIDEO_PALETTE_YUV422;
1127 break;
1128 case 15: p.palette=VIDEO_PALETTE_RGB555;
1129 break;
1130 case 16: p.palette=VIDEO_PALETTE_RGB565;
1131 break;
1132 case 24: p.palette=VIDEO_PALETTE_RGB24;
1133 break;
1134 case 32: p.palette=VIDEO_PALETTE_RGB32;
1135 break;
1137 if (copy_to_user(arg, &p, sizeof(p)))
1138 return -EFAULT;
1139 break;
1141 case VIDIOCSPICT:
1143 struct video_picture p;
1144 if (copy_from_user(&p, arg,sizeof(p)))
1145 return -EFAULT;
1146 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));
1148 /* depth must match with framebuffer */
1149 if (p.depth != ztv->depth)
1150 return -EINVAL;
1152 /* check if palette matches this bpp */
1153 if (p.palette>NRPALETTES ||
1154 palette2fmt[p.palette].bpp != ztv->overinfo.bpp)
1155 return -EINVAL;
1157 write_lock_irq(&ztv->lock);
1158 ztv->overinfo.format = p.palette;
1159 ztv->picture = p;
1160 write_unlock_irq(&ztv->lock);
1162 /* tell the decoder */
1163 i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
1164 break;
1167 case VIDIOCGWIN:
1169 struct video_window vw;
1170 DEBUG(printk(CARD_DEBUG "VIDIOCGWIN\n",CARD));
1171 read_lock(&ztv->lock);
1172 vw.x = ztv->overinfo.x;
1173 vw.y = ztv->overinfo.y;
1174 vw.width = ztv->overinfo.w;
1175 vw.height = ztv->overinfo.h;
1176 vw.chromakey= 0;
1177 vw.flags = 0;
1178 if (ztv->vidInterlace)
1179 vw.flags|=VIDEO_WINDOW_INTERLACE;
1180 read_unlock(&ztv->lock);
1181 if (copy_to_user(arg,&vw,sizeof(vw)))
1182 return -EFAULT;
1183 break;
1185 case VIDIOCSWIN:
1187 struct video_window vw;
1188 struct video_clip *vcp;
1189 int on;
1190 if (copy_from_user(&vw,arg,sizeof(vw)))
1191 return -EFAULT;
1192 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));
1194 if (vw.flags)
1195 return -EINVAL;
1197 if (vw.clipcount <0 || vw.clipcount>256)
1198 return -EDOM; /* Too many! */
1201 * Do any clips.
1203 vcp = vmalloc(sizeof(struct video_clip)*(vw.clipcount+4));
1204 if (vcp==NULL)
1205 return -ENOMEM;
1206 if (vw.clipcount && copy_from_user(vcp,vw.clips,sizeof(struct video_clip)*vw.clipcount)) {
1207 vfree(vcp);
1208 return -EFAULT;
1211 on = ztv->running;
1212 if (on)
1213 zoran_cap(ztv, 0);
1216 * strange, it seems xawtv sometimes calls us with 0
1217 * width and/or height. Ignore these values
1219 if (vw.x == 0)
1220 vw.x = ztv->overinfo.x;
1221 if (vw.y == 0)
1222 vw.y = ztv->overinfo.y;
1224 /* by now we are committed to the new data... */
1225 write_lock_irq(&ztv->lock);
1226 ztv->overinfo.x = vw.x;
1227 ztv->overinfo.y = vw.y;
1228 ztv->overinfo.w = vw.width;
1229 ztv->overinfo.h = vw.height;
1230 write_unlock_irq(&ztv->lock);
1233 * Impose display clips
1235 if (vw.x+vw.width > ztv->swidth)
1236 new_clip(&vw, vcp, ztv->swidth-vw.x, 0, vw.width-1, vw.height-1);
1237 if (vw.y+vw.height > ztv->sheight)
1238 new_clip(&vw, vcp, 0, ztv->sheight-vw.y, vw.width-1, vw.height-1);
1240 /* built the requested clipping zones */
1241 zoran_set_geo(ztv, &ztv->overinfo);
1242 zoran_built_overlay(ztv, vw.clipcount, vcp);
1243 vfree(vcp);
1245 /* if we were on, restart the video engine */
1246 if (on)
1247 zoran_cap(ztv, 1);
1248 break;
1251 case VIDIOCCAPTURE:
1253 int v;
1254 if (get_user(v, (int *)arg))
1255 return -EFAULT;
1256 DEBUG(printk(CARD_DEBUG "VIDIOCCAPTURE(%d)\n",CARD,v));
1258 if (v==0) {
1259 clear_bit(STATE_OVERLAY, &ztv->state);
1260 zoran_cap(ztv, 1);
1262 else {
1263 /* is VIDIOCSFBUF, VIDIOCSWIN done? */
1264 if (ztv->overinfo.busadr==0 || ztv->overinfo.w==0 || ztv->overinfo.h==0)
1265 return -EINVAL;
1267 set_bit(STATE_OVERLAY, &ztv->state);
1268 zoran_cap(ztv, 1);
1270 break;
1273 case VIDIOCGFBUF:
1275 struct video_buffer v;
1276 DEBUG(printk(CARD_DEBUG "VIDIOCGFBUF\n",CARD));
1277 read_lock(&ztv->lock);
1278 v.base = (void *)ztv->overinfo.busadr;
1279 v.height = ztv->sheight;
1280 v.width = ztv->swidth;
1281 v.depth = ztv->depth;
1282 v.bytesperline = ztv->overinfo.bpl;
1283 read_unlock(&ztv->lock);
1284 if(copy_to_user(arg, &v,sizeof(v)))
1285 return -EFAULT;
1286 break;
1288 case VIDIOCSFBUF:
1290 struct video_buffer v;
1291 if(!capable(CAP_SYS_ADMIN))
1292 return -EPERM;
1293 if (copy_from_user(&v, arg,sizeof(v)))
1294 return -EFAULT;
1295 DEBUG(printk(CARD_DEBUG "VIDIOCSFBUF(%p,%d,%d,%d,%d)\n",CARD,v.base, v.width,v.height,v.depth,v.bytesperline));
1297 if (v.depth!=15 && v.depth!=16 && v.depth!=24 && v.depth!=32)
1298 return -EINVAL;
1299 if (v.bytesperline<1)
1300 return -EINVAL;
1301 if (ztv->running)
1302 return -EBUSY;
1303 write_lock_irq(&ztv->lock);
1304 ztv->overinfo.busadr = (ulong)v.base;
1305 ztv->sheight = v.height;
1306 ztv->swidth = v.width;
1307 ztv->depth = v.depth; /* bits per pixel */
1308 ztv->overinfo.bpp = ((v.depth+1)&0x38)/8;/* bytes per pixel */
1309 ztv->overinfo.bpl = v.bytesperline; /* bytes per line */
1310 write_unlock_irq(&ztv->lock);
1311 break;
1314 case VIDIOCKEY:
1316 /* Will be handled higher up .. */
1317 break;
1320 case VIDIOCSYNC:
1322 int i;
1323 if (get_user(i, (int *) arg))
1324 return -EFAULT;
1325 DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d)\n",CARD,i));
1326 if (i<0 || i>ZORAN_MAX_FBUFFERS)
1327 return -EINVAL;
1328 switch (ztv->grabinfo[i].status) {
1329 case FBUFFER_FREE:
1330 return -EINVAL;
1331 case FBUFFER_BUSY:
1332 /* wait till this buffer gets grabbed */
1333 while (ztv->grabinfo[i].status == FBUFFER_BUSY) {
1334 interruptible_sleep_on(&ztv->grabq);
1335 /* see if a signal did it */
1336 if (signal_pending(current))
1337 return -EINTR;
1339 /* don't fall through; a DONE buffer is not UNUSED */
1340 break;
1341 case FBUFFER_DONE:
1342 ztv->grabinfo[i].status = FBUFFER_FREE;
1343 /* tell ppl we have a spare buffer */
1344 wake_up_interruptible(&ztv->grabq);
1345 break;
1347 DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d) returns\n",CARD,i));
1348 break;
1351 case VIDIOCMCAPTURE:
1353 struct video_mmap vm;
1354 struct vidinfo* frame;
1355 if (copy_from_user(&vm,arg,sizeof(vm)))
1356 return -EFAULT;
1357 DEBUG(printk(CARD_DEBUG "VIDIOCMCAPTURE(%d,(%d,%d),%d)\n",CARD,vm.frame,vm.width,vm.height,vm.format));
1358 if (vm.frame<0 || vm.frame>ZORAN_MAX_FBUFFERS ||
1359 vm.width<32 || vm.width>768 ||
1360 vm.height<32 || vm.height>576 ||
1361 vm.format>NRPALETTES ||
1362 palette2fmt[vm.format].mode == 0)
1363 return -EINVAL;
1365 /* we are allowed to take over UNUSED and DONE buffers */
1366 frame = &ztv->grabinfo[vm.frame];
1367 if (frame->status == FBUFFER_BUSY)
1368 return -EBUSY;
1370 /* setup the other parameters if they are given */
1371 write_lock_irq(&ztv->lock);
1372 frame->w = vm.width;
1373 frame->h = vm.height;
1374 frame->format = vm.format;
1375 frame->bpp = palette2fmt[frame->format].bpp;
1376 frame->bpl = frame->w*frame->bpp;
1377 frame->status = FBUFFER_BUSY;
1378 frame->next = 0;
1379 { /* add to tail of queue */
1380 struct vidinfo* oldframe = ztv->workqueue;
1381 if (!oldframe) ztv->workqueue = frame;
1382 else {
1383 while (oldframe->next) oldframe = oldframe->next;
1384 oldframe->next = frame;
1387 write_unlock_irq(&ztv->lock);
1388 zoran_cap(ztv, 1);
1389 break;
1392 case VIDIOCGMBUF:
1394 struct video_mbuf mb;
1395 int i;
1396 DEBUG(printk(CARD_DEBUG "VIDIOCGMBUF\n",CARD));
1397 mb.size = ZORAN_MAX_FBUFSIZE;
1398 mb.frames = ZORAN_MAX_FBUFFERS;
1399 for (i=0; i<ZORAN_MAX_FBUFFERS; i++)
1400 mb.offsets[i] = i*ZORAN_MAX_FBUFFER;
1401 if(copy_to_user(arg, &mb,sizeof(mb)))
1402 return -EFAULT;
1403 break;
1406 case VIDIOCGUNIT:
1408 struct video_unit vu;
1409 DEBUG(printk(CARD_DEBUG "VIDIOCGUNIT\n",CARD));
1410 vu.video = ztv->video_dev.minor;
1411 vu.vbi = ztv->vbi_dev.minor;
1412 vu.radio = VIDEO_NO_UNIT;
1413 vu.audio = VIDEO_NO_UNIT;
1414 vu.teletext = VIDEO_NO_UNIT;
1415 if(copy_to_user(arg, &vu,sizeof(vu)))
1416 return -EFAULT;
1417 break;
1420 case VIDIOCGFREQ:
1422 unsigned long v = ztv->tuner_freq;
1423 if (copy_to_user(arg,&v,sizeof(v)))
1424 return -EFAULT;
1425 DEBUG(printk(CARD_DEBUG "VIDIOCGFREQ\n",CARD));
1426 break;
1428 case VIDIOCSFREQ:
1430 unsigned long v;
1431 if (copy_from_user(&v, arg, sizeof(v)))
1432 return -EFAULT;
1433 DEBUG(printk(CARD_DEBUG "VIDIOCSFREQ\n",CARD));
1435 if (ztv->have_tuner) {
1436 int fixme = v;
1437 if (i2c_control_device(&(ztv->i2c), I2C_DRIVERID_TUNER, TUNER_SET_TVFREQ, &fixme) < 0)
1438 return -EAGAIN;
1440 ztv->tuner_freq = v;
1441 break;
1444 /* Why isn't this in the API?
1445 * And why doesn't it take a buffer number?
1446 case BTTV_FIELDNR:
1448 unsigned long v = ztv->lastfieldnr;
1449 if (copy_to_user(arg,&v,sizeof(v)))
1450 return -EFAULT;
1451 DEBUG(printk(CARD_DEBUG "BTTV_FIELDNR\n",CARD));
1452 break;
1456 default:
1457 return -ENOIOCTLCMD;
1459 return 0;
1462 static
1463 int zoran_mmap(struct vm_area_struct *vma, struct video_device* dev, const char* adr, unsigned long size)
1465 struct zoran* ztv = (struct zoran*)dev;
1466 unsigned long start = (unsigned long)adr;
1467 unsigned long pos;
1469 DEBUG(printk(CARD_DEBUG "zoran_mmap(0x%p,%ld)\n",CARD,adr,size));
1471 /* sanity checks */
1472 if (size > ZORAN_MAX_FBUFSIZE || !ztv->fbuffer)
1473 return -EINVAL;
1475 /* start mapping the whole shabang to user memory */
1476 pos = (unsigned long)ztv->fbuffer;
1477 while (size>0) {
1478 unsigned long page = virt_to_phys((void*)pos);
1479 if (remap_page_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1480 return -EAGAIN;
1481 start += PAGE_SIZE;
1482 pos += PAGE_SIZE;
1483 size -= PAGE_SIZE;
1485 return 0;
1488 static struct video_device zr36120_template=
1490 .owner = THIS_MODULE,
1491 .name = "UNSET",
1492 .type = VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY,
1493 .hardware = VID_HARDWARE_ZR36120,
1494 .open = zoran_open,
1495 .close = zoran_close,
1496 .read = zoran_read,
1497 .write = zoran_write,
1498 .poll = zoran_poll,
1499 .ioctl = zoran_ioctl,
1500 .mmap = zoran_mmap,
1501 .minor = -1,
1504 static
1505 int vbi_open(struct video_device *dev, int flags)
1507 struct zoran *ztv = (struct zoran*)dev->priv;
1508 struct vidinfo* item;
1510 DEBUG(printk(CARD_DEBUG "vbi_open(dev,%d)\n",CARD,flags));
1513 * During VBI device open, we continiously grab VBI-like
1514 * data in the vbi buffer when we have nothing to do.
1515 * Only when there is an explicit request for VBI data
1516 * (read call) we /force/ a read.
1519 /* allocate buffers */
1520 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1522 item->status = FBUFFER_FREE;
1524 /* alloc */
1525 if (!item->memadr) {
1526 item->memadr = bmalloc(ZORAN_VBI_BUFSIZE);
1527 if (!item->memadr) {
1528 /* could not get a buffer, bail out */
1529 while (item != ztv->readinfo) {
1530 item--;
1531 bfree(item->memadr, ZORAN_VBI_BUFSIZE);
1532 item->memadr = 0;
1533 item->busadr = 0;
1535 return -ENOBUFS;
1539 /* determine the DMAable address */
1540 item->busadr = virt_to_bus(item->memadr);
1543 /* do the common part of all open's */
1544 zoran_common_open(ztv, flags);
1546 set_bit(STATE_VBI, &ztv->state);
1547 /* start read-ahead */
1548 zoran_cap(ztv, 1);
1550 return 0;
1553 static
1554 void vbi_close(struct video_device *dev)
1556 struct zoran *ztv = (struct zoran*)dev->priv;
1557 struct vidinfo* item;
1559 DEBUG(printk(CARD_DEBUG "vbi_close(dev)\n",CARD));
1561 /* driver specific closure */
1562 clear_bit(STATE_VBI, &ztv->state);
1564 zoran_common_close(ztv);
1567 * This is sucky but right now I can't find a good way to
1568 * be sure its safe to free the buffer. We wait 5-6 fields
1569 * which is more than sufficient to be sure.
1571 current->state = TASK_UNINTERRUPTIBLE;
1572 schedule_timeout(HZ/10); /* Wait 1/10th of a second */
1574 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1576 if (item->memadr)
1577 bfree(item->memadr, ZORAN_VBI_BUFSIZE);
1578 item->memadr = 0;
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 __put_user(a, optr++);
1696 __put_user(a, optr++);
1698 /* and clear the rest of the line */
1699 for (x*=2; optr<eptr && x<done->bpl; x++)
1700 __put_user(0, optr++);
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 __put_user(iptr[x*2], optr++);
1718 /* and clear the rest of the line */
1719 for (;optr<eptr && x<done->bpl; x++)
1720 __put_user(0, optr++);
1721 /* next line */
1722 iptr += done->bpl;
1726 /* API compliance:
1727 * place the framenumber (half fieldnr) in the last long
1729 __put_user(done->fieldnr/2, ((ulong*)eptr)[-1]);
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 static
1746 unsigned int vbi_poll(struct video_device *dev, struct file *file, poll_table *wait)
1748 struct zoran *ztv = (struct zoran*)dev->priv;
1749 struct vidinfo* item;
1750 unsigned int mask = 0;
1752 poll_wait(file, &ztv->vbiq, wait);
1754 for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
1755 if (item->status == FBUFFER_DONE)
1757 mask |= (POLLIN | POLLRDNORM);
1758 break;
1761 DEBUG(printk(CARD_DEBUG "vbi_poll()=%x\n",CARD,mask));
1763 return mask;
1766 static
1767 int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
1769 struct zoran* ztv = (struct zoran*)dev->priv;
1771 switch (cmd) {
1772 case VIDIOCGVBIFMT:
1774 struct vbi_format f;
1775 DEBUG(printk(CARD_DEBUG "VIDIOCGVBIINFO\n",CARD));
1776 f.sampling_rate = 14750000UL;
1777 f.samples_per_line = -ztv->readinfo[0].w;
1778 f.sample_format = VIDEO_PALETTE_RAW;
1779 f.start[0] = f.start[1] = ztv->readinfo[0].y;
1780 f.start[1] += 312;
1781 f.count[0] = f.count[1] = -ztv->readinfo[0].h;
1782 f.flags = VBI_INTERLACED;
1783 if (copy_to_user(arg,&f,sizeof(f)))
1784 return -EFAULT;
1785 break;
1787 case VIDIOCSVBIFMT:
1789 struct vbi_format f;
1790 int i;
1791 if (copy_from_user(&f, arg,sizeof(f)))
1792 return -EFAULT;
1793 DEBUG(printk(CARD_DEBUG "VIDIOCSVBIINFO(%d,%d,%d,%d,%d,%d,%d,%x)\n",CARD,f.sampling_rate,f.samples_per_line,f.sample_format,f.start[0],f.start[1],f.count[0],f.count[1],f.flags));
1795 /* lots of parameters are fixed... (PAL) */
1796 if (f.sampling_rate != 14750000UL ||
1797 f.samples_per_line > 864 ||
1798 f.sample_format != VIDEO_PALETTE_RAW ||
1799 f.start[0] < 0 ||
1800 f.start[0] != f.start[1]-312 ||
1801 f.count[0] != f.count[1] ||
1802 f.start[0]+f.count[0] >= 288 ||
1803 f.flags != VBI_INTERLACED)
1804 return -EINVAL;
1806 write_lock_irq(&ztv->lock);
1807 ztv->readinfo[0].y = f.start[0];
1808 ztv->readinfo[0].w = -f.samples_per_line;
1809 ztv->readinfo[0].h = -f.count[0];
1810 ztv->readinfo[0].bpl = f.samples_per_line*ztv->readinfo[0].bpp;
1811 for (i=1; i<ZORAN_VBI_BUFFERS; i++)
1812 ztv->readinfo[i] = ztv->readinfo[i];
1813 write_unlock_irq(&ztv->lock);
1814 break;
1816 default:
1817 return -ENOIOCTLCMD;
1819 return 0;
1822 static struct video_device vbi_template=
1824 .owner = THIS_MODULE,
1825 .name = "UNSET",
1826 .type = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
1827 .hardware = VID_HARDWARE_ZR36120,
1828 .open = vbi_open,
1829 .close = vbi_close,
1830 .read = vbi_read,
1831 .write = zoran_write,
1832 .poll = vbi_poll,
1833 .ioctl = vbi_ioctl,
1834 .minor = -1,
1838 * Scan for a Zoran chip, request the irq and map the io memory
1840 static
1841 int __init find_zoran(void)
1843 int result;
1844 struct zoran *ztv;
1845 struct pci_dev *dev = NULL;
1846 unsigned char revision;
1847 int zoran_num=0;
1849 while ((dev = pci_find_device(PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120, dev)))
1851 /* Ok, a ZR36120/ZR36125 found! */
1852 ztv = &zorans[zoran_num];
1853 ztv->dev = dev;
1855 if (pci_enable_device(dev))
1856 return -EIO;
1858 pci_read_config_byte(dev, PCI_CLASS_REVISION, &revision);
1859 printk(KERN_INFO "zoran: Zoran %x (rev %d) ",
1860 dev->device, revision);
1861 printk("bus: %d, devfn: %d, irq: %d, ",
1862 dev->bus->number, dev->devfn, dev->irq);
1863 printk("memory: 0x%08lx.\n", ztv->zoran_adr);
1865 ztv->zoran_mem = ioremap(ztv->zoran_adr, 0x1000);
1866 DEBUG(printk(KERN_DEBUG "zoran: mapped-memory at 0x%p\n",ztv->zoran_mem));
1868 result = request_irq(dev->irq, zoran_irq,
1869 SA_SHIRQ|SA_INTERRUPT,"zoran",(void *)ztv);
1870 if (result==-EINVAL)
1872 iounmap(ztv->zoran_mem);
1873 printk(KERN_ERR "zoran: Bad irq number or handler\n");
1874 return -EINVAL;
1876 if (result==-EBUSY)
1877 printk(KERN_ERR "zoran: IRQ %d busy, change your PnP config in BIOS\n",dev->irq);
1878 if (result < 0) {
1879 iounmap(ztv->zoran_mem);
1880 return result;
1882 /* Enable bus-mastering */
1883 pci_set_master(dev);
1885 zoran_num++;
1887 if(zoran_num)
1888 printk(KERN_INFO "zoran: %d Zoran card(s) found.\n",zoran_num);
1889 return zoran_num;
1892 static
1893 int __init init_zoran(int card)
1895 struct zoran *ztv = &zorans[card];
1896 int i;
1898 /* if the given cardtype valid? */
1899 if (cardtype[card]>=NRTVCARDS) {
1900 printk(KERN_INFO "invalid cardtype(%d) detected\n",cardtype[card]);
1901 return -1;
1904 /* reset the zoran */
1905 zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
1906 udelay(10);
1907 zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
1908 udelay(10);
1910 /* zoran chip specific details */
1911 ztv->card = tvcards+cardtype[card]; /* point to the selected card */
1912 ztv->norm = 0; /* PAL */
1913 ztv->tuner_freq = 0;
1915 /* videocard details */
1916 ztv->swidth = 800;
1917 ztv->sheight = 600;
1918 ztv->depth = 16;
1920 /* State details */
1921 ztv->fbuffer = 0;
1922 ztv->overinfo.kindof = FBUFFER_OVERLAY;
1923 ztv->overinfo.status = FBUFFER_FREE;
1924 ztv->overinfo.x = 0;
1925 ztv->overinfo.y = 0;
1926 ztv->overinfo.w = 768; /* 640 */
1927 ztv->overinfo.h = 576; /* 480 */
1928 ztv->overinfo.format = VIDEO_PALETTE_RGB565;
1929 ztv->overinfo.bpp = palette2fmt[ztv->overinfo.format].bpp;
1930 ztv->overinfo.bpl = ztv->overinfo.bpp*ztv->swidth;
1931 ztv->overinfo.busadr = 0;
1932 ztv->overinfo.memadr = 0;
1933 ztv->overinfo.overlay = 0;
1934 for (i=0; i<ZORAN_MAX_FBUFFERS; i++) {
1935 ztv->grabinfo[i] = ztv->overinfo;
1936 ztv->grabinfo[i].kindof = FBUFFER_GRAB;
1938 init_waitqueue_head(&ztv->grabq);
1940 /* VBI details */
1941 ztv->readinfo[0] = ztv->overinfo;
1942 ztv->readinfo[0].kindof = FBUFFER_VBI;
1943 ztv->readinfo[0].w = -864;
1944 ztv->readinfo[0].h = -38;
1945 ztv->readinfo[0].format = VIDEO_PALETTE_YUV422;
1946 ztv->readinfo[0].bpp = palette2fmt[ztv->readinfo[0].format].bpp;
1947 ztv->readinfo[0].bpl = 1024*ztv->readinfo[0].bpp;
1948 for (i=1; i<ZORAN_VBI_BUFFERS; i++)
1949 ztv->readinfo[i] = ztv->readinfo[0];
1950 init_waitqueue_head(&ztv->vbiq);
1952 /* maintenance data */
1953 ztv->have_decoder = 0;
1954 ztv->have_tuner = 0;
1955 ztv->tuner_type = 0;
1956 ztv->running = 0;
1957 ztv->users = 0;
1958 ztv->lock = RW_LOCK_UNLOCKED;
1959 ztv->workqueue = 0;
1960 ztv->fieldnr = 0;
1961 ztv->lastfieldnr = 0;
1963 if (triton1)
1964 zrand(~ZORAN_VDC_TRICOM, ZORAN_VDC);
1966 /* external FL determines TOP frame */
1967 zror(ZORAN_VFEC_EXTFL, ZORAN_VFEC);
1969 /* set HSpol */
1970 if (ztv->card->hsync_pos)
1971 zrwrite(ZORAN_VFEH_HSPOL, ZORAN_VFEH);
1972 /* set VSpol */
1973 if (ztv->card->vsync_pos)
1974 zrwrite(ZORAN_VFEV_VSPOL, ZORAN_VFEV);
1976 /* Set the proper General Purpuse register bits */
1977 /* implicit: no softreset, 0 waitstates */
1978 zrwrite(ZORAN_PCI_SOFTRESET|(ztv->card->gpdir<<0),ZORAN_PCI);
1979 /* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
1980 zrwrite(ztv->card->gpval<<24,ZORAN_GUEST);
1982 /* clear interrupt status */
1983 zrwrite(~0, ZORAN_ISR);
1986 * i2c template
1988 ztv->i2c = zoran_i2c_bus_template;
1989 sprintf(ztv->i2c.name,"zoran-%d",card);
1990 ztv->i2c.data = ztv;
1993 * Now add the template and register the device unit
1995 ztv->video_dev = zr36120_template;
1996 strcpy(ztv->video_dev.name, ztv->i2c.name);
1997 ztv->video_dev.priv = ztv;
1998 if (video_register_device(&ztv->video_dev, VFL_TYPE_GRABBER, video_nr) < 0)
1999 return -1;
2001 ztv->vbi_dev = vbi_template;
2002 strcpy(ztv->vbi_dev.name, ztv->i2c.name);
2003 ztv->vbi_dev.priv = ztv;
2004 if (video_register_device(&ztv->vbi_dev, VFL_TYPE_VBI, vbi_nr) < 0) {
2005 video_unregister_device(&ztv->video_dev);
2006 return -1;
2008 i2c_register_bus(&ztv->i2c);
2010 /* set interrupt mask - the PIN enable will be set later */
2011 zrwrite(ZORAN_ICR_GIRQ0|ZORAN_ICR_GIRQ1|ZORAN_ICR_CODE, ZORAN_ICR);
2013 printk(KERN_INFO "%s: installed %s\n",ztv->i2c.name,ztv->card->name);
2014 return 0;
2017 static
2018 void release_zoran(int max)
2020 struct zoran *ztv;
2021 int i;
2023 for (i=0;i<max; i++)
2025 ztv = &zorans[i];
2027 /* turn off all capturing, DMA and IRQs */
2028 /* reset the zoran */
2029 zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
2030 udelay(10);
2031 zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
2032 udelay(10);
2034 /* first disable interrupts before unmapping the memory! */
2035 zrwrite(0, ZORAN_ICR);
2036 zrwrite(0xffffffffUL,ZORAN_ISR);
2038 /* free it */
2039 free_irq(ztv->dev->irq,ztv);
2041 /* unregister i2c_bus */
2042 i2c_unregister_bus((&ztv->i2c));
2044 /* unmap and free memory */
2045 if (ztv->zoran_mem)
2046 iounmap(ztv->zoran_mem);
2048 video_unregister_device(&ztv->video_dev);
2049 video_unregister_device(&ztv->vbi_dev);
2053 void __exit zr36120_exit(void)
2055 release_zoran(zoran_cards);
2058 int __init zr36120_init(void)
2060 int card;
2062 handle_chipset();
2063 zoran_cards = find_zoran();
2064 if (zoran_cards<0)
2065 /* no cards found, no need for a driver */
2066 return -EIO;
2068 /* initialize Zorans */
2069 for (card=0; card<zoran_cards; card++) {
2070 if (init_zoran(card)<0) {
2071 /* only release the zorans we have registered */
2072 release_zoran(card);
2073 return -EIO;
2076 return 0;
2079 module_init(zr36120_init);
2080 module_exit(zr36120_exit);