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