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