Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / buz.c
1 #define MAX_KMALLOC_MEM (512*1024)
2 /*
3    buz - Iomega Buz driver version 1.0
4
5    Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
6
7    based on
8
9    buz.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
10
11    and
12
13    bttv - Bt848 frame grabber driver
14
15    Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
16    & Marcus Metzler (mocm@thp.uni-koeln.de)
17
18    This program is free software; you can redistribute it and/or modify
19    it under the terms of the GNU General Public License as published by
20    the Free Software Foundation; either version 2 of the License, or
21    (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26    GNU General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31  */
32
33 #include <linux/module.h>
34 #include <linux/delay.h>
35 #include <linux/errno.h>
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/malloc.h>
40 #include <linux/mm.h>
41 #include <linux/pci.h>
42 #include <linux/signal.h>
43 #include <asm/io.h>
44 #include <asm/pgtable.h>
45 #include <asm/page.h>
46 #include <linux/sched.h>
47 #include <asm/segment.h>
48 #include <linux/types.h>
49 #include <linux/wrapper.h>
50 #include <linux/spinlock.h>
51 #include <linux/vmalloc.h>
52
53 #include <linux/videodev.h>
54
55 #include <linux/version.h>
56 #include <asm/uaccess.h>
57
58 #include <linux/i2c-old.h>
59 #include "buz.h"
60 #include <linux/video_decoder.h>
61 #include <linux/video_encoder.h>
62
63 #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | /* ZR36057_ISR_GIRQ1 | ZR36057_ISR_CodRepIRQ | */ ZR36057_ISR_JPEGRepIRQ )
64 #define GPIO_MASK 0xdf
65
66 /*
67  
68  BUZ
69  
70    GPIO0 = 1, take board out of reset
71    GPIO1 = 1, take JPEG codec out of sleep mode
72    GPIO3 = 1, deassert FRAME# to 36060
73    
74
75    GIRQ0 signals a vertical sync of the video signal
76    GIRQ1 signals that ZR36060's DATERR# line is asserted.
77
78    SAA7111A
79
80    In their infinite wisdom, the Iomega engineers decided to
81    use the same input line for composite and S-Video Color,
82    although there are two entries not connected at all!
83    Through this ingenious strike, it is not possible to
84    keep two running video sources connected at the same time
85    to Composite and S-VHS input!
86
87    mode 0 - N/C
88    mode 1 - S-Video Y
89    mode 2 - noise or something I don't know
90    mode 3 - Composite and S-Video C
91    mode 4 - N/C
92    mode 5 - S-Video (gain C independently selectable of gain Y)
93    mode 6 - N/C
94    mode 7 - S-Video (gain C adapted to gain Y)
95  */
96
97 #define MAJOR_VERSION 1         /* driver major version */
98 #define MINOR_VERSION 0         /* driver minor version */
99
100 #define BUZ_NAME      "Iomega BUZ V-1.0"        /* name of the driver */
101
102 #define DEBUG(x)                /* Debug driver */
103 #define IDEBUG(x)               /* Debug interrupt handler */
104 #define IOCTL_DEBUG(x)
105
106
107 /* The parameters for this driver */
108
109 /*
110    The video mem address of the video card.
111    The driver has a little database for some videocards
112    to determine it from there. If your video card is not in there
113    you have either to give it to the driver as a parameter
114    or set in in a VIDIOCSFBUF ioctl
115  */
116
117 static unsigned long vidmem = 0;        /* Video memory base address */
118
119 /* Special purposes only: */
120
121 static int triton = 0;          /* 0=no, 1=yes */
122 static int natoma = 0;          /* 0=no, 1=yes */
123
124 /*
125    Number and size of grab buffers for Video 4 Linux
126    The vast majority of applications should not need more than 2,
127    the very popular BTTV driver actually does ONLY have 2.
128    Time sensitive applications might need more, the maximum
129    is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
130
131    The size is set so that the maximum possible request
132    can be satisfied. Decrease  it, if bigphys_area alloc'd
133    memory is low. If you don't have the bigphys_area patch,
134    set it to 128 KB. Will you allow only to grab small
135    images with V4L, but that's better than nothing.
136
137    v4l_bufsize has to be given in KB !
138
139  */
140
141 static int v4l_nbufs = 2;
142 static int v4l_bufsize = 128;   /* Everybody should be able to work with this setting */
143
144 /*
145    Default input and video norm at startup of the driver.
146  */
147
148 static int default_input = 0;   /* 0=Composite, 1=S-VHS */
149 static int default_norm = 0;    /* 0=PAL, 1=NTSC */
150
151 MODULE_PARM(vidmem, "i");
152 MODULE_PARM(triton, "i");
153 MODULE_PARM(natoma, "i");
154 MODULE_PARM(v4l_nbufs, "i");
155 MODULE_PARM(v4l_bufsize, "i");
156 MODULE_PARM(default_input, "i");
157 MODULE_PARM(default_norm, "i");
158
159 /* Anybody who uses more than four? */
160 #define BUZ_MAX 4
161
162 static int zoran_num;           /* number of Buzs in use */
163 static struct zoran zoran[BUZ_MAX];
164
165 /* forward references */
166
167 static void v4l_fbuffer_free(struct zoran *zr);
168 static void jpg_fbuffer_free(struct zoran *zr);
169 static void zoran_feed_stat_com(struct zoran *zr);
170
171
172
173 /*
174  *   Allocate the V4L grab buffers
175  *
176  *   These have to be pysically contiguous.
177  *   If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
178  */
179
180 static int v4l_fbuffer_alloc(struct zoran *zr)
181 {
182         int i, off;
183         unsigned char *mem;
184
185         for (i = 0; i < v4l_nbufs; i++) {
186                 if (zr->v4l_gbuf[i].fbuffer)
187                         printk(KERN_WARNING "%s: v4l_fbuffer_alloc: buffer %d allready allocated ?\n", zr->name, i);
188
189                 if (v4l_bufsize <= MAX_KMALLOC_MEM) {
190                         /* Use kmalloc */
191
192                         mem = (unsigned char *) kmalloc(v4l_bufsize, GFP_KERNEL);
193                         if (mem == 0) {
194                                 printk(KERN_ERR "%s: kmalloc for V4L bufs failed\n", zr->name);
195                                 v4l_fbuffer_free(zr);
196                                 return -ENOBUFS;
197                         }
198                         zr->v4l_gbuf[i].fbuffer = mem;
199                         zr->v4l_gbuf[i].fbuffer_phys = virt_to_phys(mem);
200                         zr->v4l_gbuf[i].fbuffer_bus = virt_to_bus(mem);
201                         for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
202                                 mem_map_reserve(virt_to_page(mem + off));
203                         DEBUG(printk(BUZ_INFO ": V4L frame %d mem 0x%x (bus: 0x%x=%d)\n", i, mem, virt_to_bus(mem), virt_to_bus(mem)));
204                 } else {
205                         v4l_fbuffer_free(zr);
206                         return -ENOBUFS;
207                 }
208         }
209
210         return 0;
211 }
212
213 /* free the V4L grab buffers */
214 static void v4l_fbuffer_free(struct zoran *zr)
215 {
216         int i, off;
217         unsigned char *mem;
218
219         for (i = 0; i < v4l_nbufs; i++) {
220                 if (!zr->v4l_gbuf[i].fbuffer)
221                         continue;
222
223                 mem = zr->v4l_gbuf[i].fbuffer;
224                 for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
225                         mem_map_unreserve(virt_to_page(mem + off));
226                 kfree((void *) zr->v4l_gbuf[i].fbuffer);
227                 zr->v4l_gbuf[i].fbuffer = NULL;
228         }
229 }
230
231 /*
232  *   Allocate the MJPEG grab buffers.
233  *
234  *   If the requested buffer size is smaller than MAX_KMALLOC_MEM,
235  *   kmalloc is used to request a physically contiguous area,
236  *   else we allocate the memory in framgents with get_free_page.
237  *
238  *   If a Natoma chipset is present and this is a revision 1 zr36057,
239  *   each MJPEG buffer needs to be physically contiguous.
240  *   (RJ: This statement is from Dave Perks' original driver,
241  *   I could never check it because I have a zr36067)
242  *   The driver cares about this because it reduces the buffer
243  *   size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
244  *
245  *   RJ: The contents grab buffers needs never be accessed in the driver.
246  *       Therefore there is no need to allocate them with vmalloc in order
247  *       to get a contiguous virtual memory space.
248  *       I don't understand why many other drivers first allocate them with
249  *       vmalloc (which uses internally also get_free_page, but delivers you
250  *       virtual addresses) and then again have to make a lot of efforts
251  *       to get the physical address.
252  *
253  */
254
255 static int jpg_fbuffer_alloc(struct zoran *zr)
256 {
257         int i, j, off, alloc_contig;
258         unsigned long mem;
259
260         /* Decide if we should alloc contiguous or fragmented memory */
261         /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
262
263         alloc_contig = (zr->jpg_bufsize < MAX_KMALLOC_MEM);
264
265         for (i = 0; i < zr->jpg_nbufs; i++) {
266                 if (zr->jpg_gbuf[i].frag_tab)
267                         printk(KERN_WARNING "%s: jpg_fbuffer_alloc: buffer %d allready allocated ???\n", zr->name, i);
268
269                 /* Allocate fragment table for this buffer */
270
271                 mem = get_free_page(GFP_KERNEL);
272                 if (mem == 0) {
273                         printk(KERN_ERR "%s: jpg_fbuffer_alloc: get_free_page (frag_tab) failed for buffer %d\n", zr->name, i);
274                         jpg_fbuffer_free(zr);
275                         return -ENOBUFS;
276                 }
277                 memset((void *) mem, 0, PAGE_SIZE);
278                 zr->jpg_gbuf[i].frag_tab = (u32 *) mem;
279                 zr->jpg_gbuf[i].frag_tab_bus = virt_to_bus((void *) mem);
280
281                 if (alloc_contig) {
282                         mem = (unsigned long) kmalloc(zr->jpg_bufsize, GFP_KERNEL);
283                         if (mem == 0) {
284                                 jpg_fbuffer_free(zr);
285                                 return -ENOBUFS;
286                         }
287                         zr->jpg_gbuf[i].frag_tab[0] = virt_to_bus((void *) mem);
288                         zr->jpg_gbuf[i].frag_tab[1] = ((zr->jpg_bufsize / 4) << 1) | 1;
289                         for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
290                                 mem_map_reserve(virt_to_page(mem + off));
291                 } else {
292                         /* jpg_bufsize is alreay page aligned */
293                         for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
294                                 mem = get_free_page(GFP_KERNEL);
295                                 if (mem == 0) {
296                                         jpg_fbuffer_free(zr);
297                                         return -ENOBUFS;
298                                 }
299                                 zr->jpg_gbuf[i].frag_tab[2 * j] = virt_to_bus((void *) mem);
300                                 zr->jpg_gbuf[i].frag_tab[2 * j + 1] = (PAGE_SIZE / 4) << 1;
301                                 mem_map_reserve(virt_to_page(mem));
302                         }
303
304                         zr->jpg_gbuf[i].frag_tab[2 * j - 1] |= 1;
305                 }
306         }
307
308         DEBUG(printk("jpg_fbuffer_alloc: %d KB allocated\n",
309                      (zr->jpg_nbufs * zr->jpg_bufsize) >> 10));
310         zr->jpg_buffers_allocated = 1;
311         return 0;
312 }
313
314 /* free the MJPEG grab buffers */
315 static void jpg_fbuffer_free(struct zoran *zr)
316 {
317         int i, j, off, alloc_contig;
318         unsigned char *mem;
319
320         /* Decide if we should alloc contiguous or fragmented memory */
321         /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
322
323         alloc_contig = (zr->jpg_bufsize < MAX_KMALLOC_MEM);
324
325         for (i = 0; i < zr->jpg_nbufs; i++) {
326                 if (!zr->jpg_gbuf[i].frag_tab)
327                         continue;
328
329                 if (alloc_contig) {
330                         if (zr->jpg_gbuf[i].frag_tab[0]) {
331                                 mem = (unsigned char *) bus_to_virt(zr->jpg_gbuf[i].frag_tab[0]);
332                                 for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
333                                         mem_map_unreserve(virt_to_page(mem + off));
334                                 kfree((void *) mem);
335                                 zr->jpg_gbuf[i].frag_tab[0] = 0;
336                                 zr->jpg_gbuf[i].frag_tab[1] = 0;
337                         }
338                 } else {
339                         for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
340                                 if (!zr->jpg_gbuf[i].frag_tab[2 * j])
341                                         break;
342                                 mem_map_unreserve(virt_to_page(bus_to_virt(zr->jpg_gbuf[i].frag_tab[2 * j])));
343                                 free_page((unsigned long) bus_to_virt(zr->jpg_gbuf[i].frag_tab[2 * j]));
344                                 zr->jpg_gbuf[i].frag_tab[2 * j] = 0;
345                                 zr->jpg_gbuf[i].frag_tab[2 * j + 1] = 0;
346                         }
347                 }
348
349                 free_page((unsigned long) zr->jpg_gbuf[i].frag_tab);
350                 zr->jpg_gbuf[i].frag_tab = NULL;
351         }
352         zr->jpg_buffers_allocated = 0;
353 }
354
355
356 /* ----------------------------------------------------------------------- */
357
358 /* I2C functions                                                           */
359
360 #define I2C_DELAY   10
361
362
363 /* software I2C functions */
364
365 static void i2c_setlines(struct i2c_bus *bus, int ctrl, int data)
366 {
367         struct zoran *zr = (struct zoran *) bus->data;
368         btwrite((data << 1) | ctrl, ZR36057_I2CBR);
369         btread(ZR36057_I2CBR);
370         udelay(I2C_DELAY);
371 }
372
373 static int i2c_getdataline(struct i2c_bus *bus)
374 {
375         struct zoran *zr = (struct zoran *) bus->data;
376         return (btread(ZR36057_I2CBR) >> 1) & 1;
377 }
378
379 static void attach_inform(struct i2c_bus *bus, int id)
380 {
381         DEBUG(struct zoran *zr = (struct zoran *) bus->data);
382         DEBUG(printk(BUZ_DEBUG "-%u: i2c attach %02x\n", zr->id, id));
383 }
384
385 static void detach_inform(struct i2c_bus *bus, int id)
386 {
387         DEBUG(struct zoran *zr = (struct zoran *) bus->data);
388         DEBUG(printk(BUZ_DEBUG "-%u: i2c detach %02x\n", zr->id, id));
389 }
390
391 static struct i2c_bus zoran_i2c_bus_template =
392 {
393         "zr36057",
394         I2C_BUSID_BT848,
395         NULL,
396
397         SPIN_LOCK_UNLOCKED,
398
399         attach_inform,
400         detach_inform,
401
402         i2c_setlines,
403         i2c_getdataline,
404         NULL,
405         NULL,
406 };
407
408
409 /* ----------------------------------------------------------------------- */
410
411 static void GPIO(struct zoran *zr, unsigned bit, unsigned value)
412 {
413         u32 reg;
414         u32 mask;
415
416         mask = 1 << (24 + bit);
417         reg = btread(ZR36057_GPPGCR1) & ~mask;
418         if (value) {
419                 reg |= mask;
420         }
421         btwrite(reg, ZR36057_GPPGCR1);
422         /* Stop any PCI posting on the GPIO bus */
423         btread(ZR36057_I2CBR);
424 }
425
426
427 /*
428  *   Set the registers for the size we have specified. Don't bother
429  *   trying to understand this without the ZR36057 manual in front of
430  *   you [AC].
431  *
432  *   PS: The manual is free for download in .pdf format from
433  *   www.zoran.com - nicely done those folks.
434  */
435
436 struct tvnorm {
437         u16 Wt, Wa, Ht, Ha, HStart, VStart;
438 };
439
440 static struct tvnorm tvnorms[] =
441 {
442    /* PAL-BDGHI */
443         {864, 720, 625, 576, 31, 16},
444    /* NTSC */
445         {858, 720, 525, 480, 21, 8},
446 };
447 #define TVNORMS (sizeof(tvnorms) / sizeof(tvnorm))
448
449 static int format2bpp(int format)
450 {
451         int bpp;
452
453         /* Determine the number of bytes per pixel for the video format requested */
454
455         switch (format) {
456
457         case VIDEO_PALETTE_YUV422:
458                 bpp = 2;
459                 break;
460
461         case VIDEO_PALETTE_RGB555:
462                 bpp = 2;
463                 break;
464
465         case VIDEO_PALETTE_RGB565:
466                 bpp = 2;
467                 break;
468
469         case VIDEO_PALETTE_RGB24:
470                 bpp = 3;
471                 break;
472
473         case VIDEO_PALETTE_RGB32:
474                 bpp = 4;
475                 break;
476
477         default:
478                 bpp = 0;
479         }
480
481         return bpp;
482 }
483
484 /*
485  * set geometry
486  */
487 static void zr36057_set_vfe(struct zoran *zr, int video_width, int video_height,
488                             unsigned int video_format)
489 {
490         struct tvnorm *tvn;
491         unsigned HStart, HEnd, VStart, VEnd;
492         unsigned DispMode;
493         unsigned VidWinWid, VidWinHt;
494         unsigned hcrop1, hcrop2, vcrop1, vcrop2;
495         unsigned Wa, We, Ha, He;
496         unsigned X, Y, HorDcm, VerDcm;
497         u32 reg;
498         unsigned mask_line_size;
499
500         if (zr->params.norm < 0 || zr->params.norm > 1) {
501                 printk(KERN_ERR "%s: set_vfe: video_norm = %d not valid\n", zr->name,  zr->params.norm);
502                 return;
503         }
504         if (video_width < BUZ_MIN_WIDTH || video_height < BUZ_MIN_HEIGHT) {
505                 printk(KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n", zr->name, video_width, video_height);
506                 return;
507         }
508         tvn = &tvnorms[zr->params.norm];
509
510         Wa = tvn->Wa;
511         Ha = tvn->Ha;
512
513         /* if window has more than half of active height,
514            switch on interlacing - we want the full information */
515
516         zr->video_interlace = (video_height > Ha / 2);
517
518 /**** zr36057 ****/
519
520         /* horizontal */
521         VidWinWid = video_width;
522         X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
523         We = (VidWinWid * 64) / X;
524         HorDcm = 64 - X;
525         hcrop1 = 2 * ((tvn->Wa - We) / 4);
526         hcrop2 = tvn->Wa - We - hcrop1;
527         HStart = tvn->HStart | 1;
528         HEnd = HStart + tvn->Wa - 1;
529         HStart += hcrop1;
530         HEnd -= hcrop2;
531         reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
532             | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
533         reg |= ZR36057_VFEHCR_HSPol;
534         btwrite(reg, ZR36057_VFEHCR);
535
536         /* Vertical */
537         DispMode = !zr->video_interlace;
538         VidWinHt = DispMode ? video_height : video_height / 2;
539         Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
540         He = (VidWinHt * 64) / Y;
541         VerDcm = 64 - Y;
542         vcrop1 = (tvn->Ha / 2 - He) / 2;
543         vcrop2 = tvn->Ha / 2 - He - vcrop1;
544         VStart = tvn->VStart;
545         VEnd = VStart + tvn->Ha / 2 - 1;
546         VStart += vcrop1;
547         VEnd -= vcrop2;
548         reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
549             | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
550         reg |= ZR36057_VFEVCR_VSPol;
551         btwrite(reg, ZR36057_VFEVCR);
552
553         /* scaler and pixel format */
554         reg = 0                 // ZR36057_VFESPFR_ExtFl /* Trying to live without ExtFl */
555              | (HorDcm << ZR36057_VFESPFR_HorDcm)
556             | (VerDcm << ZR36057_VFESPFR_VerDcm)
557             | (DispMode << ZR36057_VFESPFR_DispMode)
558             | ZR36057_VFESPFR_LittleEndian;
559         /* RJ: I don't know, why the following has to be the opposite
560            of the corresponding ZR36060 setting, but only this way
561            we get the correct colors when uncompressing to the screen  */
562         reg |= ZR36057_VFESPFR_VCLKPol;
563         /* RJ: Don't know if that is needed for NTSC also */
564         reg |= ZR36057_VFESPFR_TopField;
565         switch (video_format) {
566
567         case VIDEO_PALETTE_YUV422:
568                 reg |= ZR36057_VFESPFR_YUV422;
569                 break;
570
571         case VIDEO_PALETTE_RGB555:
572                 reg |= ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ErrDif;
573                 break;
574
575         case VIDEO_PALETTE_RGB565:
576                 reg |= ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ErrDif;
577                 break;
578
579         case VIDEO_PALETTE_RGB24:
580                 reg |= ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_Pack24;
581                 break;
582
583         case VIDEO_PALETTE_RGB32:
584                 reg |= ZR36057_VFESPFR_RGB888;
585                 break;
586
587         default:
588                 printk(KERN_INFO "%s: Unknown color_fmt=%x\n", zr->name, video_format);
589                 return;
590
591         }
592         if (HorDcm >= 48) {
593                 reg |= 3 << ZR36057_VFESPFR_HFilter;    /* 5 tap filter */
594         } else if (HorDcm >= 32) {
595                 reg |= 2 << ZR36057_VFESPFR_HFilter;    /* 4 tap filter */
596         } else if (HorDcm >= 16) {
597                 reg |= 1 << ZR36057_VFESPFR_HFilter;    /* 3 tap filter */
598         }
599         btwrite(reg, ZR36057_VFESPFR);
600
601         /* display configuration */
602
603         reg = (16 << ZR36057_VDCR_MinPix)
604             | (VidWinHt << ZR36057_VDCR_VidWinHt)
605             | (VidWinWid << ZR36057_VDCR_VidWinWid);
606         if (triton)
607                 reg &= ~ZR36057_VDCR_Triton;
608         else
609                 reg |= ZR36057_VDCR_Triton;
610         btwrite(reg, ZR36057_VDCR);
611
612         /* Write overlay clipping mask data, but don't enable overlay clipping */
613         /* RJ: since this makes only sense on the screen, we use 
614            zr->window.width instead of video_width */
615
616         mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
617         reg = virt_to_bus(zr->overlay_mask);
618         btwrite(reg, ZR36057_MMTR);
619         reg = virt_to_bus(zr->overlay_mask + mask_line_size);
620         btwrite(reg, ZR36057_MMBR);
621         reg = mask_line_size - (zr->window.width + 31) / 32;
622         if (DispMode == 0)
623                 reg += mask_line_size;
624         reg <<= ZR36057_OCR_MaskStride;
625         btwrite(reg, ZR36057_OCR);
626
627 }
628
629 /*
630  * Switch overlay on or off
631  */
632
633 static void zr36057_overlay(struct zoran *zr, int on)
634 {
635         int fmt, bpp;
636         u32 reg;
637
638         if (on) {
639                 /* do the necessary settings ... */
640
641                 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);       /* switch it off first */
642
643                 switch (zr->buffer.depth) {
644                 case 15:
645                         fmt = VIDEO_PALETTE_RGB555;
646                         bpp = 2;
647                         break;
648                 case 16:
649                         fmt = VIDEO_PALETTE_RGB565;
650                         bpp = 2;
651                         break;
652                 case 24:
653                         fmt = VIDEO_PALETTE_RGB24;
654                         bpp = 3;
655                         break;
656                 case 32:
657                         fmt = VIDEO_PALETTE_RGB32;
658                         bpp = 4;
659                         break;
660                 default:
661                         fmt = 0;
662                         bpp = 0;
663                 }
664
665                 zr36057_set_vfe(zr, zr->window.width, zr->window.height, fmt);
666
667                 /* Start and length of each line MUST be 4-byte aligned.
668                    This should be allready checked before the call to this routine.
669                    All error messages are internal driver checking only! */
670
671                 /* video display top and bottom registers */
672
673                 reg = (u32) zr->buffer.base
674                     + zr->window.x * bpp
675                     + zr->window.y * zr->buffer.bytesperline;
676                 btwrite(reg, ZR36057_VDTR);
677                 if (reg & 3)
678                         printk(KERN_ERR "%s: zr36057_overlay: video_address not aligned\n", zr->name);
679                 if (zr->video_interlace)
680                         reg += zr->buffer.bytesperline;
681                 btwrite(reg, ZR36057_VDBR);
682
683                 /* video stride, status, and frame grab register */
684
685                 reg = zr->buffer.bytesperline - zr->window.width * bpp;
686                 if (zr->video_interlace)
687                         reg += zr->buffer.bytesperline;
688                 if (reg & 3)
689                         printk(KERN_ERR "%s: zr36057_overlay: video_stride not aligned\n", zr->name);
690                 reg = (reg << ZR36057_VSSFGR_DispStride);
691                 reg |= ZR36057_VSSFGR_VidOvf;   /* clear overflow status */
692                 btwrite(reg, ZR36057_VSSFGR);
693
694                 /* Set overlay clipping */
695
696                 if (zr->window.clipcount)
697                         btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
698
699                 /* ... and switch it on */
700
701                 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
702         } else {
703                 /* Switch it off */
704
705                 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
706         }
707 }
708
709 /*
710  * The overlay mask has one bit for each pixel on a scan line,
711  *  and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
712  */
713 static void write_overlay_mask(struct zoran *zr, struct video_clip *vp, int count)
714 {
715         unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
716         u32 *mask;
717         int x, y, width, height;
718         unsigned i, j, k;
719         u32 reg;
720
721         /* fill mask with one bits */
722         memset(zr->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
723         reg = 0;
724
725         for (i = 0; i < count; ++i) {
726                 /* pick up local copy of clip */
727                 x = vp[i].x;
728                 y = vp[i].y;
729                 width = vp[i].width;
730                 height = vp[i].height;
731
732                 /* trim clips that extend beyond the window */
733                 if (x < 0) {
734                         width += x;
735                         x = 0;
736                 }
737                 if (y < 0) {
738                         height += y;
739                         y = 0;
740                 }
741                 if (x + width > zr->window.width) {
742                         width = zr->window.width - x;
743                 }
744                 if (y + height > zr->window.height) {
745                         height = zr->window.height - y;
746                 }
747                 /* ignore degenerate clips */
748                 if (height <= 0) {
749                         continue;
750                 }
751                 if (width <= 0) {
752                         continue;
753                 }
754                 /* apply clip for each scan line */
755                 for (j = 0; j < height; ++j) {
756                         /* reset bit for each pixel */
757                         /* this can be optimized later if need be */
758                         mask = zr->overlay_mask + (y + j) * mask_line_size;
759                         for (k = 0; k < width; ++k) {
760                                 mask[(x + k) / 32] &= ~((u32) 1 << (x + k) % 32);
761                         }
762                 }
763         }
764 }
765
766 /* Enable/Disable uncompressed memory grabbing of the 36057 */
767
768 static void zr36057_set_memgrab(struct zoran *zr, int mode)
769 {
770         if (mode) {
771                 if (btread(ZR36057_VSSFGR) & (ZR36057_VSSFGR_SnapShot | ZR36057_VSSFGR_FrameGrab))
772                         printk(KERN_WARNING "%s: zr36057_set_memgrab_on with SnapShot or FrameGrab on ???\n", zr->name);
773
774                 /* switch on VSync interrupts */
775
776                 btwrite(IRQ_MASK, ZR36057_ISR);         // Clear Interrupts
777
778                 btor(ZR36057_ICR_GIRQ0, ZR36057_ICR);
779
780                 /* enable SnapShot */
781
782                 btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
783
784                 /* Set zr36057 video front end  and enable video */
785
786 #ifdef XAWTV_HACK
787                 zr36057_set_vfe(zr, zr->gwidth > 720 ? 720 : zr->gwidth, zr->gheight, zr->gformat);
788 #else
789                 zr36057_set_vfe(zr, zr->gwidth, zr->gheight, zr->gformat);
790 #endif
791
792                 zr->v4l_memgrab_active = 1;
793         } else {
794                 zr->v4l_memgrab_active = 0;
795
796                 /* switch off VSync interrupts */
797
798                 btand(~ZR36057_ICR_GIRQ0, ZR36057_ICR);
799
800                 /* reenable grabbing to screen if it was running */
801
802                 if (zr->v4l_overlay_active) {
803                         zr36057_overlay(zr, 1);
804                 } else {
805                         btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
806                         btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
807                 }
808         }
809 }
810
811 static int wait_grab_pending(struct zoran *zr)
812 {
813         unsigned long flags;
814
815         /* wait until all pending grabs are finished */
816
817         if (!zr->v4l_memgrab_active)
818                 return 0;
819
820         while (zr->v4l_pend_tail != zr->v4l_pend_head) {
821                 interruptible_sleep_on(&zr->v4l_capq);
822                 if (signal_pending(current))
823                         return -ERESTARTSYS;
824         }
825
826         spin_lock_irqsave(&zr->lock, flags);
827         zr36057_set_memgrab(zr, 0);
828         spin_unlock_irqrestore(&zr->lock, flags);
829
830         return 0;
831 }
832
833 /*
834  *   V4L Buffer grabbing
835  */
836
837 static int v4l_grab(struct zoran *zr, struct video_mmap *mp)
838 {
839         unsigned long flags;
840         int res, bpp;
841
842         /*
843          * There is a long list of limitations to what is allowed to be grabbed
844          * We don't output error messages her, since some programs (e.g. xawtv)
845          * just try several settings to find out what is valid or not.
846          */
847
848         /* No grabbing outside the buffer range! */
849
850         if (mp->frame >= v4l_nbufs || mp->frame < 0)
851                 return -EINVAL;
852
853         /* Check size and format of the grab wanted */
854
855         if (mp->height < BUZ_MIN_HEIGHT || mp->width < BUZ_MIN_WIDTH)
856                 return -EINVAL;
857         if (mp->height > BUZ_MAX_HEIGHT || mp->width > BUZ_MAX_WIDTH)
858                 return -EINVAL;
859
860         bpp = format2bpp(mp->format);
861         if (bpp == 0)
862                 return -EINVAL;
863
864         /* Check against available buffer size */
865
866         if (mp->height * mp->width * bpp > v4l_bufsize)
867                 return -EINVAL;
868
869         /* The video front end needs 4-byte alinged line sizes */
870
871         if ((bpp == 2 && (mp->width & 1)) || (bpp == 3 && (mp->width & 3)))
872                 return -EINVAL;
873
874         /*
875          * To minimize the time spent in the IRQ routine, we avoid setting up
876          * the video front end there.
877          * If this grab has different parameters from a running streaming capture
878          * we stop the streaming capture and start it over again.
879          */
880
881         if (zr->v4l_memgrab_active &&
882             (zr->gwidth != mp->width || zr->gheight != mp->height || zr->gformat != mp->format)) {
883                 res = wait_grab_pending(zr);
884                 if (res)
885                         return res;
886         }
887         zr->gwidth = mp->width;
888         zr->gheight = mp->height;
889         zr->gformat = mp->format;
890         zr->gbpl = bpp * zr->gwidth;
891
892
893         spin_lock_irqsave(&zr->lock, flags);
894
895         /* make sure a grab isn't going on currently with this buffer */
896
897         switch (zr->v4l_gbuf[mp->frame].state) {
898
899         default:
900         case BUZ_STATE_PEND:
901                 res = -EBUSY;   /* what are you doing? */
902                 break;
903
904         case BUZ_STATE_USER:
905         case BUZ_STATE_DONE:
906                 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
907                 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = mp->frame;
908                 zr->v4l_gbuf[mp->frame].state = BUZ_STATE_PEND;
909                 res = 0;
910                 break;
911
912         }
913
914         /* put the 36057 into frame grabbing mode */
915
916         if (!res && !zr->v4l_memgrab_active)
917                 zr36057_set_memgrab(zr, 1);
918
919         spin_unlock_irqrestore(&zr->lock, flags);
920
921         return res;
922 }
923
924 /*
925  * Sync on a V4L buffer
926  */
927
928 static int v4l_sync(struct zoran *zr, int frame)
929 {
930         unsigned long flags;
931
932
933         /* check passed-in frame number */
934         if (frame >= v4l_nbufs || frame < 0) {
935                 printk(KERN_ERR "%s: v4l_sync: frame %d is invalid\n", zr->name, frame);
936                 return -EINVAL;
937         }
938         /* Check if is buffer was queued at all */
939
940         if (zr->v4l_gbuf[frame].state == BUZ_STATE_USER) {
941 //              printk(KERN_ERR "%s: v4l_sync: Trying to sync on a buffer which was not queued?\n", zr->name);
942                 return -EINVAL;
943         }
944         /* wait on this buffer to get ready */
945
946         while (zr->v4l_gbuf[frame].state == BUZ_STATE_PEND) {
947                 interruptible_sleep_on(&zr->v4l_capq);
948                 if (signal_pending(current))
949                         return -ERESTARTSYS;
950         }
951
952         /* buffer should now be in BUZ_STATE_DONE */
953
954         if (zr->v4l_gbuf[frame].state != BUZ_STATE_DONE)
955                 printk(KERN_ERR "%s: v4l_sync - internal error\n", zr->name);
956
957         /* Check if streaming capture has finished */
958
959         spin_lock_irqsave(&zr->lock, flags);
960
961         if (zr->v4l_pend_tail == zr->v4l_pend_head)
962                 zr36057_set_memgrab(zr, 0);
963
964         spin_unlock_irqrestore(&zr->lock, flags);
965
966         return 0;
967 }
968 /*****************************************************************************
969  *                                                                           *
970  *  Set up the Buz-specific MJPEG part                                       *
971  *                                                                           *
972  *****************************************************************************/
973
974 /*
975  *      Wait til post office is no longer busy 
976  */
977  
978 static int post_office_wait(struct zoran *zr)
979 {
980         u32 por;
981         u32 ct=0;
982
983         while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
984                 ct++;
985                 if(ct>100000)
986                 {
987                         printk(KERN_ERR "%s: timeout on post office.\n", zr->name);
988                         return -1;
989                 }
990                 /* wait for something to happen */
991         }
992         if ((por & ZR36057_POR_POPen) != 0) {
993                 printk(KERN_WARNING "%s: pop pending %08x\n", zr->name, por);
994                 return -1;
995         }
996         if ((por & (ZR36057_POR_POTime | ZR36057_POR_POPen)) != 0) {
997                 printk(KERN_WARNING "%s: pop timeout %08x\n", zr->name, por);
998                 return -1;
999         }
1000         return 0;
1001 }
1002
1003 static int post_office_write(struct zoran *zr, unsigned guest, unsigned reg, unsigned value)
1004 {
1005         u32 por;
1006
1007         post_office_wait(zr);
1008         por = ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16) | (value & 0xFF);
1009         btwrite(por, ZR36057_POR);
1010         return post_office_wait(zr);
1011 }
1012
1013 static int post_office_read(struct zoran *zr, unsigned guest, unsigned reg)
1014 {
1015         u32 por;
1016
1017         post_office_wait(zr);
1018         por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
1019         btwrite(por, ZR36057_POR);
1020         if (post_office_wait(zr) < 0) {
1021                 return -1;
1022         }
1023         return btread(ZR36057_POR) & 0xFF;
1024 }
1025
1026 static int zr36060_write_8(struct zoran *zr, unsigned reg, unsigned val)
1027 {
1028         if (post_office_wait(zr)
1029             || post_office_write(zr, 0, 1, reg >> 8)
1030             || post_office_write(zr, 0, 2, reg)) {
1031                 return -1;
1032         }
1033         return post_office_write(zr, 0, 3, val);
1034 }
1035
1036 static int zr36060_write_16(struct zoran *zr, unsigned reg, unsigned val)
1037 {
1038         if (zr36060_write_8(zr, reg + 0, val >> 8)) {
1039                 return -1;
1040         }
1041         return zr36060_write_8(zr, reg + 1, val >> 0);
1042 }
1043
1044 static int zr36060_write_24(struct zoran *zr, unsigned reg, unsigned val)
1045 {
1046         if (zr36060_write_8(zr, reg + 0, val >> 16)) {
1047                 return -1;
1048         }
1049         return zr36060_write_16(zr, reg + 1, val >> 0);
1050 }
1051
1052 static int zr36060_write_32(struct zoran *zr, unsigned reg, unsigned val)
1053 {
1054         if (zr36060_write_16(zr, reg + 0, val >> 16)) {
1055                 return -1;
1056         }
1057         return zr36060_write_16(zr, reg + 2, val >> 0);
1058 }
1059
1060 static u32 zr36060_read_8(struct zoran *zr, unsigned reg)
1061 {
1062         if (post_office_wait(zr)
1063             || post_office_write(zr, 0, 1, reg >> 8)
1064             || post_office_write(zr, 0, 2, reg)) {
1065                 return -1;
1066         }
1067         return post_office_read(zr, 0, 3) & 0xFF;
1068 }
1069
1070 static int zr36060_reset(struct zoran *zr)
1071 {
1072         return post_office_write(zr, 3, 0, 0);
1073 }
1074
1075 static void zr36060_sleep(struct zoran *zr, int sleep)
1076 {
1077         GPIO(zr, 1, !sleep);
1078 }
1079
1080
1081 static void zr36060_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1082 {
1083         struct tvnorm *tvn;
1084         u32 reg;
1085         int size;
1086
1087         reg = (1 << 0)          /* CodeMstr */
1088             |(0 << 2)           /* CFIS=0 */
1089             |(0 << 6)           /* Endian=0 */
1090             |(0 << 7);          /* Code16=0 */
1091         zr36060_write_8(zr, 0x002, reg);
1092
1093         switch (mode) {
1094
1095         case BUZ_MODE_MOTION_DECOMPRESS:
1096         case BUZ_MODE_STILL_DECOMPRESS:
1097                 reg = 0x00;     /* Codec mode = decompression */
1098                 break;
1099
1100         case BUZ_MODE_MOTION_COMPRESS:
1101         case BUZ_MODE_STILL_COMPRESS:
1102         default:
1103                 reg = 0xa4;     /* Codec mode = compression with variable scale factor */
1104                 break;
1105
1106         }
1107         zr36060_write_8(zr, 0x003, reg);
1108
1109         reg = 0x00;             /* reserved, mbz */
1110         zr36060_write_8(zr, 0x004, reg);
1111
1112         reg = 0xff;             /* 510 bits/block */
1113         zr36060_write_8(zr, 0x005, reg);
1114
1115         /* JPEG markers */
1116         reg = (zr->params.jpeg_markers) & 0x38; /* DRI, DQT, DHT */
1117         if (zr->params.COM_len)
1118                 reg |= JPEG_MARKER_COM;
1119         if (zr->params.APP_len)
1120                 reg |= JPEG_MARKER_APP;
1121         zr36060_write_8(zr, 0x006, reg);
1122
1123         reg = (0 << 3)          /* DATERR=0 */
1124             |(0 << 2)           /* END=0 */
1125             |(0 << 1)           /* EOI=0 */
1126             |(0 << 0);          /* EOAV=0 */
1127         zr36060_write_8(zr, 0x007, reg);
1128
1129         /* code volume */
1130
1131         /* Target field size in pixels: */
1132         tvn = &tvnorms[zr->params.norm];
1133         size = (tvn->Ha / 2) * (tvn->Wa) / (zr->params.HorDcm) / (zr->params.VerDcm);
1134
1135         /* Target compressed field size in bits: */
1136         size = size * 16;       /* uncompressed size in bits */
1137         size = size * zr->params.quality / 400; /* quality = 100 is a compression ratio 1:4 */
1138
1139         /* Lower limit (arbitrary, 1 KB) */
1140         if (size < 8192)
1141                 size = 8192;
1142
1143         /* Upper limit: 7/8 of the code buffers */
1144         if (size * zr->params.field_per_buff > zr->jpg_bufsize * 7)
1145                 size = zr->jpg_bufsize * 7 / zr->params.field_per_buff;
1146
1147         reg = size;
1148         zr36060_write_32(zr, 0x009, reg);
1149
1150         /* how do we set initial SF as a function of quality parameter? */
1151         reg = 0x0100;           /* SF=1.0 */
1152         zr36060_write_16(zr, 0x011, reg);
1153
1154         reg = 0x00ffffff;       /* AF=max */
1155         zr36060_write_24(zr, 0x013, reg);
1156
1157         reg = 0x0000;           /* test */
1158         zr36060_write_16(zr, 0x024, reg);
1159 }
1160
1161 static void zr36060_set_video(struct zoran *zr, enum zoran_codec_mode mode)
1162 {
1163         struct tvnorm *tvn;
1164         u32 reg;
1165
1166         reg = (0 << 7)          /* Video8=0 */
1167             |(0 << 6)           /* Range=0 */
1168             |(0 << 3)           /* FlDet=0 */
1169             |(1 << 2)           /* FlVedge=1 */
1170             |(0 << 1)           /* FlExt=0 */
1171             |(0 << 0);          /* SyncMstr=0 */
1172
1173         /* According to ZR36067 documentation, FlDet should correspond
1174            to the odd_even flag of the ZR36067 */
1175         if (zr->params.odd_even)
1176                 reg |= (1 << 3);
1177
1178         if (mode != BUZ_MODE_STILL_DECOMPRESS) {
1179                 /* limit pixels to range 16..235 as per CCIR-601 */
1180                 reg |= (1 << 6);        /* Range=1 */
1181         }
1182         zr36060_write_8(zr, 0x030, reg);
1183
1184         reg = (0 << 7)          /* VCLKPol=0 */
1185             |(0 << 6)           /* PValPol=0 */
1186             |(1 << 5)           /* PoePol=1 */
1187             |(0 << 4)           /* SImgPol=0 */
1188             |(0 << 3)           /* BLPol=0 */
1189             |(0 << 2)           /* FlPol=0 */
1190             |(0 << 1)           /* HSPol=0, sync on falling edge */
1191             |(1 << 0);          /* VSPol=1 */
1192         zr36060_write_8(zr, 0x031, reg);
1193
1194         switch (zr->params.HorDcm) {
1195         default:
1196         case 1:
1197                 reg = (0 << 0);
1198                 break;          /* HScale = 0 */
1199
1200         case 2:
1201                 reg = (1 << 0);
1202                 break;          /* HScale = 1 */
1203
1204         case 4:
1205                 reg = (2 << 0);
1206                 break;          /* HScale = 2 */
1207         }
1208         if (zr->params.VerDcm == 2)
1209                 reg |= (1 << 2);
1210         zr36060_write_8(zr, 0x032, reg);
1211
1212         reg = 0x80;             /* BackY */
1213         zr36060_write_8(zr, 0x033, reg);
1214
1215         reg = 0xe0;             /* BackU */
1216         zr36060_write_8(zr, 0x034, reg);
1217
1218         reg = 0xe0;             /* BackV */
1219         zr36060_write_8(zr, 0x035, reg);
1220
1221         /* sync generator */
1222
1223         tvn = &tvnorms[zr->params.norm];
1224
1225         reg = tvn->Ht - 1;      /* Vtotal */
1226         zr36060_write_16(zr, 0x036, reg);
1227
1228         reg = tvn->Wt - 1;      /* Htotal */
1229         zr36060_write_16(zr, 0x038, reg);
1230
1231         reg = 6 - 1;            /* VsyncSize */
1232         zr36060_write_8(zr, 0x03a, reg);
1233
1234         reg = 100 - 1;          /* HsyncSize */
1235         zr36060_write_8(zr, 0x03b, reg);
1236
1237         reg = tvn->VStart - 1;  /* BVstart */
1238         zr36060_write_8(zr, 0x03c, reg);
1239
1240         reg += tvn->Ha / 2;     /* BVend */
1241         zr36060_write_16(zr, 0x03e, reg);
1242
1243         reg = tvn->HStart - 1;  /* BHstart */
1244         zr36060_write_8(zr, 0x03d, reg);
1245
1246         reg += tvn->Wa;         /* BHend */
1247         zr36060_write_16(zr, 0x040, reg);
1248
1249         /* active area */
1250         reg = zr->params.img_y + tvn->VStart;   /* Vstart */
1251         zr36060_write_16(zr, 0x042, reg);
1252
1253         reg += zr->params.img_height;   /* Vend */
1254         zr36060_write_16(zr, 0x044, reg);
1255
1256         reg = zr->params.img_x + tvn->HStart;   /* Hstart */
1257         zr36060_write_16(zr, 0x046, reg);
1258
1259         reg += zr->params.img_width;    /* Hend */
1260         zr36060_write_16(zr, 0x048, reg);
1261
1262         /* subimage area */
1263         reg = zr->params.img_y + tvn->VStart;   /* SVstart */
1264         zr36060_write_16(zr, 0x04a, reg);
1265
1266         reg += zr->params.img_height;   /* SVend */
1267         zr36060_write_16(zr, 0x04c, reg);
1268
1269         reg = zr->params.img_x + tvn->HStart;   /* SHstart */
1270         zr36060_write_16(zr, 0x04e, reg);
1271
1272         reg += zr->params.img_width;    /* SHend */
1273         zr36060_write_16(zr, 0x050, reg);
1274 }
1275
1276 static void zr36060_set_jpg_SOF(struct zoran *zr)
1277 {
1278         u32 reg;
1279
1280
1281         reg = 0xffc0;           /* SOF marker */
1282         zr36060_write_16(zr, 0x060, reg);
1283
1284         reg = 17;               /* SOF length */
1285         zr36060_write_16(zr, 0x062, reg);
1286
1287         reg = 8;                /* precision 8 bits */
1288         zr36060_write_8(zr, 0x064, reg);
1289
1290         reg = zr->params.img_height / zr->params.VerDcm;        /* image height */
1291         zr36060_write_16(zr, 0x065, reg);
1292
1293         reg = zr->params.img_width / zr->params.HorDcm; /* image width */
1294         zr36060_write_16(zr, 0x067, reg);
1295
1296         reg = 3;                /* 3 color components */
1297         zr36060_write_8(zr, 0x069, reg);
1298
1299         reg = 0x002100;         /* Y component */
1300         zr36060_write_24(zr, 0x06a, reg);
1301
1302         reg = 0x011101;         /* U component */
1303         zr36060_write_24(zr, 0x06d, reg);
1304
1305         reg = 0x021101;         /* V component */
1306         zr36060_write_24(zr, 0x070, reg);
1307 }
1308
1309 static void zr36060_set_jpg_SOS(struct zoran *zr)
1310 {
1311         u32 reg;
1312
1313
1314         reg = 0xffda;           /* SOS marker */
1315         zr36060_write_16(zr, 0x07a, reg);
1316
1317         reg = 12;               /* SOS length */
1318         zr36060_write_16(zr, 0x07c, reg);
1319
1320         reg = 3;                /* 3 color components */
1321         zr36060_write_8(zr, 0x07e, reg);
1322
1323         reg = 0x0000;           /* Y component */
1324         zr36060_write_16(zr, 0x07f, reg);
1325
1326         reg = 0x0111;           /* U component */
1327         zr36060_write_16(zr, 0x081, reg);
1328
1329         reg = 0x0211;           /* V component */
1330         zr36060_write_16(zr, 0x083, reg);
1331
1332         reg = 0x003f00;         /* Start, end spectral scans */
1333         zr36060_write_24(zr, 0x085, reg);
1334 }
1335
1336 static void zr36060_set_jpg_DRI(struct zoran *zr)
1337 {
1338         u32 reg;
1339
1340
1341         reg = 0xffdd;           /* DRI marker */
1342         zr36060_write_16(zr, 0x0c0, reg);
1343
1344         reg = 4;                /* DRI length */
1345         zr36060_write_16(zr, 0x0c2, reg);
1346
1347         reg = 8;                /* length in MCUs */
1348         zr36060_write_16(zr, 0x0c4, reg);
1349 }
1350
1351 static void zr36060_set_jpg_DQT(struct zoran *zr)
1352 {
1353         unsigned i;
1354         unsigned adr;
1355         static const u8 dqt[] =
1356         {
1357                 0xff, 0xdb,     /* DHT marker */
1358                 0x00, 0x84,     /* DHT length */
1359                 0x00,           /* table ID 0 */
1360                 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
1361                 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
1362                 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
1363                 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
1364                 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
1365                 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
1366                 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
1367                 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
1368                 0x01,           /* table ID 1 */
1369                 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
1370                 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
1371                 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1372                 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1373                 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1374                 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1375                 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1376                 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
1377         };
1378
1379         /* write fixed quantitization tables */
1380         adr = 0x0cc;
1381         for (i = 0; i < sizeof(dqt); ++i) {
1382                 zr36060_write_8(zr, adr++, dqt[i]);
1383         }
1384 }
1385
1386 static void zr36060_set_jpg_DHT(struct zoran *zr)
1387 {
1388         unsigned i;
1389         unsigned adr;
1390         static const u8 dht[] =
1391         {
1392                 0xff, 0xc4,     /* DHT marker */
1393                 0x01, 0xa2,     /* DHT length */
1394                 0x00,           /* table class 0, ID 0 */
1395                 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,         /* # codes of length 1..8 */
1396                 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,         /* # codes of length 8..16 */
1397                 0x00,           /* values for codes of length 2 */
1398                 0x01, 0x02, 0x03, 0x04, 0x05,   /* values for codes of length 3 */
1399                 0x06,           /* values for codes of length 4 */
1400                 0x07,           /* values for codes of length 5 */
1401                 0x08,           /* values for codes of length 6 */
1402                 0x09,           /* values for codes of length 7 */
1403                 0x0a,           /* values for codes of length 8 */
1404                 0x0b,           /* values for codes of length 9 */
1405                 0x01,           /* table class 0, ID 1 */
1406                 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,         /* # codes of length 1..8 */
1407                 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,         /* # codes of length 9..16 */
1408                 0x00, 0x01, 0x02,       /* values for codes of length 2 */
1409                 0x03,           /* values for codes of length 3 */
1410                 0x04,           /* values for codes of length 4 */
1411                 0x05,           /* values for codes of length 5 */
1412                 0x06,           /* values for codes of length 6 */
1413                 0x07,           /* values for codes of length 7 */
1414                 0x08,           /* values for codes of length 8 */
1415                 0x09,           /* values for codes of length 9 */
1416                 0x0a,           /* values for codes of length 10 */
1417                 0x0b,           /* values for codes of length 11 */
1418                 0x10,
1419                 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
1420                 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
1421                 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
1422                 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
1423                 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
1424                 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
1425                 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
1426                 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
1427                 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
1428                 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
1429                 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
1430                 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
1431                 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
1432                 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
1433                 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
1434                 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1435                 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
1436                 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
1437                 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
1438                 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
1439                 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
1440                 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
1441                 0xf9, 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
1442                 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00,
1443                 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
1444                 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
1445                 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,
1446                 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23,
1447                 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a,
1448                 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
1449                 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35,
1450                 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
1451                 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55,
1452                 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65,
1453                 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
1454                 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84,
1455                 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93,
1456                 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2,
1457                 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
1458                 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
1459                 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
1460                 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
1461                 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
1462                 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5,
1463                 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
1464         };
1465
1466         /* write fixed Huffman tables */
1467         adr = 0x1d4;
1468         for (i = 0; i < sizeof(dht); ++i) {
1469                 zr36060_write_8(zr, adr++, dht[i]);
1470         }
1471 }
1472
1473 static void zr36060_set_jpg_APP(struct zoran *zr)
1474 {
1475         unsigned adr;
1476         int len, i;
1477         u32 reg;
1478
1479
1480         len = zr->params.APP_len;
1481         if (len < 0)
1482                 len = 0;
1483         if (len > 60)
1484                 len = 60;
1485
1486         i = zr->params.APPn;
1487         if (i < 0)
1488                 i = 0;
1489         if (i > 15)
1490                 i = 15;
1491
1492         reg = 0xffe0 + i;       /* APPn marker */
1493         zr36060_write_16(zr, 0x380, reg);
1494
1495         reg = len + 2;          /* APPn len */
1496         zr36060_write_16(zr, 0x382, reg);
1497
1498         /* write APPn data */
1499         adr = 0x384;
1500         for (i = 0; i < 60; i++) {
1501                 zr36060_write_8(zr, adr++, (i < len ? zr->params.APP_data[i] : 0));
1502         }
1503 }
1504
1505 static void zr36060_set_jpg_COM(struct zoran *zr)
1506 {
1507         unsigned adr;
1508         int len, i;
1509         u32 reg;
1510
1511
1512         len = zr->params.COM_len;
1513         if (len < 0)
1514                 len = 0;
1515         if (len > 60)
1516                 len = 60;
1517
1518         reg = 0xfffe;           /* COM marker */
1519         zr36060_write_16(zr, 0x3c0, reg);
1520
1521         reg = len + 2;          /* COM len */
1522         zr36060_write_16(zr, 0x3c2, reg);
1523
1524         /* write COM data */
1525         adr = 0x3c4;
1526         for (i = 0; i < 60; i++) {
1527                 zr36060_write_8(zr, adr++, (i < len ? zr->params.COM_data[i] : 0));
1528         }
1529 }
1530
1531 static void zr36060_set_cap(struct zoran *zr, enum zoran_codec_mode mode)
1532 {
1533         unsigned i;
1534         u32 reg;
1535
1536         zr36060_reset(zr);
1537         mdelay(10);
1538
1539         reg = (0 << 7)          /* Load=0 */
1540             |(1 << 0);          /* SynRst=1 */
1541         zr36060_write_8(zr, 0x000, reg);
1542
1543         zr36060_set_jpg(zr, mode);
1544         zr36060_set_video(zr, mode);
1545         zr36060_set_jpg_SOF(zr);
1546         zr36060_set_jpg_SOS(zr);
1547         zr36060_set_jpg_DRI(zr);
1548         zr36060_set_jpg_DQT(zr);
1549         zr36060_set_jpg_DHT(zr);
1550         zr36060_set_jpg_APP(zr);
1551         zr36060_set_jpg_COM(zr);
1552
1553         reg = (1 << 7)          /* Load=1 */
1554             |(0 << 0);          /* SynRst=0 */
1555         zr36060_write_8(zr, 0x000, reg);
1556
1557         /* wait for codec to unbusy */
1558         for (i = 0; i < 1000; ++i) {
1559                 reg = zr36060_read_8(zr, 0x001);
1560                 if ((reg & (1 << 7)) == 0) {
1561                         DEBUG(printk(KERN_DEBUG "060: loaded, loops=%u\n", i));
1562                         return;
1563                 }
1564                 udelay(1000);
1565         }
1566         printk(KERN_INFO "060: stuck busy, statux=%02x\n", reg);
1567 }
1568
1569 static void zr36057_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1570 {
1571         struct tvnorm *tvn;
1572         u32 reg;
1573         int i;
1574
1575         tvn = &tvnorms[zr->params.norm];
1576
1577         /* assert P_Reset */
1578         btwrite(0, ZR36057_JPC);
1579
1580         /* re-initialize DMA ring stuff */
1581         zr->jpg_que_head = 0;
1582         zr->jpg_dma_head = 0;
1583         zr->jpg_dma_tail = 0;
1584         zr->jpg_que_tail = 0;
1585         zr->jpg_seq_num = 0;
1586         for (i = 0; i < BUZ_NUM_STAT_COM; ++i) {
1587                 zr->stat_com[i] = 1;    /* mark as unavailable to zr36057 */
1588         }
1589         for (i = 0; i < zr->jpg_nbufs; i++) {
1590                 zr->jpg_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
1591         }
1592
1593         /* MJPEG compression mode */
1594         switch (mode) {
1595
1596         case BUZ_MODE_MOTION_COMPRESS:
1597         default:
1598                 reg = ZR36057_JMC_MJPGCmpMode;
1599                 break;
1600
1601         case BUZ_MODE_MOTION_DECOMPRESS:
1602                 reg = ZR36057_JMC_MJPGExpMode;
1603                 reg |= ZR36057_JMC_SyncMstr;
1604                 /* RJ: The following is experimental - improves the output to screen */
1605                 if (zr->params.VFIFO_FB)
1606                         reg |= ZR36057_JMC_VFIFO_FB;
1607                 break;
1608
1609         case BUZ_MODE_STILL_COMPRESS:
1610                 reg = ZR36057_JMC_JPGCmpMode;
1611                 break;
1612
1613         case BUZ_MODE_STILL_DECOMPRESS:
1614                 reg = ZR36057_JMC_JPGExpMode;
1615                 break;
1616
1617         }
1618         reg |= ZR36057_JMC_JPG;
1619         if (zr->params.field_per_buff == 1)
1620                 reg |= ZR36057_JMC_Fld_per_buff;
1621         btwrite(reg, ZR36057_JMC);
1622
1623         /* vertical */
1624         btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
1625         reg = (6 << ZR36057_VSP_VsyncSize) | (tvn->Ht << ZR36057_VSP_FrmTot);
1626         btwrite(reg, ZR36057_VSP);
1627         reg = ((zr->params.img_y + tvn->VStart) << ZR36057_FVAP_NAY)
1628             | (zr->params.img_height << ZR36057_FVAP_PAY);
1629         btwrite(reg, ZR36057_FVAP);
1630
1631         /* horizontal */
1632         btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
1633         reg = ((tvn->Wt - 100) << ZR36057_HSP_HsyncStart) | (tvn->Wt << ZR36057_HSP_LineTot);
1634         btwrite(reg, ZR36057_HSP);
1635         reg = ((zr->params.img_x + tvn->HStart) << ZR36057_FHAP_NAX)
1636             | (zr->params.img_width << ZR36057_FHAP_PAX);
1637         btwrite(reg, ZR36057_FHAP);
1638
1639         /* field process parameters */
1640         if (zr->params.odd_even)
1641                 reg = ZR36057_FPP_Odd_Even;
1642         else
1643                 reg = 0;
1644         btwrite(reg, ZR36057_FPP);
1645
1646         /* Set proper VCLK Polarity, else colors will be wrong during playback */
1647         btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
1648
1649         /* code base address and FIFO threshold */
1650         reg = virt_to_bus(zr->stat_com);
1651         btwrite(reg, ZR36057_JCBA);
1652         reg = 0x50;
1653         btwrite(reg, ZR36057_JCFT);
1654
1655         /* JPEG codec guest ID */
1656         reg = (1 << ZR36057_JCGI_JPEGuestID) | (0 << ZR36057_JCGI_JPEGuestReg);
1657         btwrite(reg, ZR36057_JCGI);
1658
1659         /* Code transfer guest ID */
1660         reg = (0 << ZR36057_MCTCR_CodGuestID) | (3 << ZR36057_MCTCR_CodGuestReg);
1661         reg |= ZR36057_MCTCR_CFlush;
1662         btwrite(reg, ZR36057_MCTCR);
1663
1664         /* deassert P_Reset */
1665         btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
1666 }
1667
1668 static void zr36057_enable_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1669 {
1670         static int zero = 0;
1671         static int one = 1;
1672
1673         switch (mode) {
1674
1675         case BUZ_MODE_MOTION_COMPRESS:
1676                 zr36060_set_cap(zr, mode);
1677                 zr36057_set_jpg(zr, mode);
1678                 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &one);
1679                 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &zero);
1680
1681                 /* deassert P_Reset, assert Code transfer enable */
1682                 btwrite(IRQ_MASK, ZR36057_ISR);
1683                 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1684                 break;
1685
1686         case BUZ_MODE_MOTION_DECOMPRESS:
1687                 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &zero);
1688                 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &one);
1689                 zr36060_set_cap(zr, mode);
1690                 zr36057_set_jpg(zr, mode);
1691
1692                 /* deassert P_Reset, assert Code transfer enable */
1693                 btwrite(IRQ_MASK, ZR36057_ISR);
1694                 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1695                 break;
1696
1697         case BUZ_MODE_IDLE:
1698         default:
1699                 /* shut down processing */
1700                 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1701                 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
1702                 btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1703                 btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1704                 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1705                 btwrite(0, ZR36057_ISR);
1706                 zr36060_reset(zr);
1707                 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &one);
1708                 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &zero);
1709                 break;
1710
1711         }
1712         zr->codec_mode = mode;
1713 }
1714
1715 /*
1716  *   Queue a MJPEG buffer for capture/playback
1717  */
1718
1719 static int jpg_qbuf(struct zoran *zr, int frame, enum zoran_codec_mode mode)
1720 {
1721         unsigned long flags;
1722         int res;
1723
1724         /* Check if buffers are allocated */
1725
1726         if (!zr->jpg_buffers_allocated) {
1727                 printk(KERN_ERR "%s: jpg_qbuf: buffers not yet allocated\n", zr->name);
1728                 return -ENOMEM;
1729         }
1730         /* Does the user want to stop streaming? */
1731
1732         if (frame < 0) {
1733                 if (zr->codec_mode == mode) {
1734                         zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1735                         return 0;
1736                 } else {
1737                         printk(KERN_ERR "%s: jpg_qbuf - stop streaming but not in streaming mode\n", zr->name);
1738                         return -EINVAL;
1739                 }
1740         }
1741         /* No grabbing outside the buffer range! */
1742
1743         if (frame >= zr->jpg_nbufs) {
1744                 printk(KERN_ERR "%s: jpg_qbuf: buffer %d out of range\n", zr->name, frame);
1745                 return -EINVAL;
1746         }
1747         /* what is the codec mode right now? */
1748
1749         if (zr->codec_mode == BUZ_MODE_IDLE) {
1750                 /* Ok load up the zr36060 and go */
1751                 zr36057_enable_jpg(zr, mode);
1752         } else if (zr->codec_mode != mode) {
1753                 /* wrong codec mode active - invalid */
1754                 printk(KERN_ERR "%s: jpg_qbuf - codec in wrong mode\n", zr->name);
1755                 return -EINVAL;
1756         }
1757         spin_lock_irqsave(&zr->lock, flags);
1758
1759         /* make sure a grab isn't going on currently with this buffer */
1760
1761         switch (zr->jpg_gbuf[frame].state) {
1762
1763         default:
1764         case BUZ_STATE_DMA:
1765         case BUZ_STATE_PEND:
1766         case BUZ_STATE_DONE:
1767                 res = -EBUSY;   /* what are you doing? */
1768                 break;
1769
1770         case BUZ_STATE_USER:
1771                 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
1772                 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = frame;
1773                 zr->jpg_gbuf[frame].state = BUZ_STATE_PEND;
1774                 zoran_feed_stat_com(zr);
1775                 res = 0;
1776                 break;
1777
1778         }
1779
1780         spin_unlock_irqrestore(&zr->lock, flags);
1781
1782         /* Start the zr36060 when the first frame is queued  */
1783         if (zr->jpg_que_head == 1) {
1784                 btor(ZR36057_JMC_Go_en, ZR36057_JMC);
1785                 btwrite(ZR36057_JPC_P_Reset | ZR36057_JPC_CodTrnsEn | ZR36057_JPC_Active, ZR36057_JPC);
1786         }
1787         return res;
1788 }
1789
1790 /*
1791  *   Sync on a MJPEG buffer
1792  */
1793
1794 static int jpg_sync(struct zoran *zr, struct zoran_sync *bs)
1795 {
1796         unsigned long flags;
1797         int frame;
1798
1799         if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1800             zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1801                 return -EINVAL;
1802         }
1803         while (zr->jpg_que_tail == zr->jpg_dma_tail) {
1804                 interruptible_sleep_on(&zr->jpg_capq);
1805                 if (signal_pending(current))
1806                         return -ERESTARTSYS;
1807         }
1808
1809         spin_lock_irqsave(&zr->lock, flags);
1810
1811         frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1812
1813         /* buffer should now be in BUZ_STATE_DONE */
1814
1815         if (zr->jpg_gbuf[frame].state != BUZ_STATE_DONE)
1816                 printk(KERN_ERR "%s: jpg_sync - internal error\n", zr->name);
1817
1818         *bs = zr->jpg_gbuf[frame].bs;
1819         zr->jpg_gbuf[frame].state = BUZ_STATE_USER;
1820
1821         spin_unlock_irqrestore(&zr->lock, flags);
1822
1823         return 0;
1824 }
1825
1826 /* when this is called the spinlock must be held */
1827 static void zoran_feed_stat_com(struct zoran *zr)
1828 {
1829         /* move frames from pending queue to DMA */
1830
1831         int frame, i, max_stat_com;
1832
1833         max_stat_com = (zr->params.TmpDcm == 1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1834
1835         while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com
1836                && zr->jpg_dma_head != zr->jpg_que_head) {
1837
1838                 frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1839                 if (zr->params.TmpDcm == 1) {
1840                         /* fill 1 stat_com entry */
1841                         i = zr->jpg_dma_head & BUZ_MASK_STAT_COM;
1842                         zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
1843                 } else {
1844                         /* fill 2 stat_com entries */
1845                         i = (zr->jpg_dma_head & 1) * 2;
1846                         zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
1847                         zr->stat_com[i + 1] = zr->jpg_gbuf[frame].frag_tab_bus;
1848                 }
1849                 zr->jpg_gbuf[frame].state = BUZ_STATE_DMA;
1850                 zr->jpg_dma_head++;
1851
1852         }
1853 }
1854
1855 /* when this is called the spinlock must be held */
1856 static void zoran_reap_stat_com(struct zoran *zr)
1857 {
1858         /* move frames from DMA queue to done queue */
1859
1860         int i;
1861         u32 stat_com;
1862         unsigned int seq;
1863         unsigned int dif;
1864         int frame;
1865         struct zoran_gbuffer *gbuf;
1866
1867         /* In motion decompress we don't have a hardware frame counter,
1868            we just count the interrupts here */
1869
1870         if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1871                 zr->jpg_seq_num++;
1872
1873         while (zr->jpg_dma_tail != zr->jpg_dma_head) {
1874                 if (zr->params.TmpDcm == 1)
1875                         i = zr->jpg_dma_tail & BUZ_MASK_STAT_COM;
1876                 else
1877                         i = (zr->jpg_dma_tail & 1) * 2 + 1;
1878
1879                 stat_com = zr->stat_com[i];
1880
1881                 if ((stat_com & 1) == 0) {
1882                         return;
1883                 }
1884                 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1885                 gbuf = &zr->jpg_gbuf[frame];
1886                 get_fast_time(&gbuf->bs.timestamp);
1887
1888                 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1889                         gbuf->bs.length = (stat_com & 0x7fffff) >> 1;
1890
1891                         /* update sequence number with the help of the counter in stat_com */
1892
1893                         seq = stat_com >> 24;
1894                         dif = (seq - zr->jpg_seq_num) & 0xff;
1895                         zr->jpg_seq_num += dif;
1896                 } else {
1897                         gbuf->bs.length = 0;
1898                 }
1899                 gbuf->bs.seq = zr->params.TmpDcm == 2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1900                 gbuf->state = BUZ_STATE_DONE;
1901
1902                 zr->jpg_dma_tail++;
1903         }
1904 }
1905
1906 static void zoran_irq(int irq, void *dev_id, struct pt_regs *regs)
1907 {
1908         u32 stat, astat;
1909         int count;
1910         struct zoran *zr;
1911         unsigned long flags;
1912
1913         zr = (struct zoran *) dev_id;
1914         count = 0;
1915
1916         spin_lock_irqsave(&zr->lock, flags);
1917         while (1) {
1918                 /* get/clear interrupt status bits */
1919                 stat = btread(ZR36057_ISR);
1920                 astat = stat & IRQ_MASK;
1921                 if (!astat) {
1922                         break;
1923                 }
1924                 btwrite(astat, ZR36057_ISR);
1925                 IDEBUG(printk(BUZ_DEBUG "-%u: astat %08x stat %08x\n", zr->id, astat, stat));
1926
1927 #if (IRQ_MASK & ZR36057_ISR_GIRQ0)
1928                 if (astat & ZR36057_ISR_GIRQ0) {
1929
1930                         /* Interrupts may still happen when zr->v4l_memgrab_active is switched off.
1931                            We simply ignore them */
1932
1933                         if (zr->v4l_memgrab_active) {
1934
1935 /* A lot more checks should be here ... */
1936                                 if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot) == 0)
1937                                         printk(KERN_WARNING "%s: BuzIRQ with SnapShot off ???\n", zr->name);
1938
1939                                 if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1940                                         /* There is a grab on a frame going on, check if it has finished */
1941
1942                                         if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_FrameGrab) == 0) {
1943                                                 /* it is finished, notify the user */
1944
1945                                                 zr->v4l_gbuf[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1946                                                 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1947                                                 zr->v4l_grab_seq++;
1948                                                 zr->v4l_pend_tail++;
1949                                         }
1950                                 }
1951                                 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1952                                         wake_up_interruptible(&zr->v4l_capq);
1953
1954                                 /* Check if there is another grab queued */
1955
1956                                 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1957                                     zr->v4l_pend_tail != zr->v4l_pend_head) {
1958
1959                                         int frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
1960                                         u32 reg;
1961
1962                                         zr->v4l_grab_frame = frame;
1963
1964                                         /* Set zr36057 video front end and enable video */
1965
1966                                         /* Buffer address */
1967
1968                                         reg = zr->v4l_gbuf[frame].fbuffer_bus;
1969                                         btwrite(reg, ZR36057_VDTR);
1970                                         if (zr->video_interlace)
1971                                                 reg += zr->gbpl;
1972                                         btwrite(reg, ZR36057_VDBR);
1973
1974                                         /* video stride, status, and frame grab register */
1975
1976 #ifdef XAWTV_HACK
1977                                         reg = (zr->gwidth > 720) ? ((zr->gwidth & ~3) - 720) * zr->gbpl / zr->gwidth : 0;
1978 #else
1979                                         reg = 0;
1980 #endif
1981                                         if (zr->video_interlace)
1982                                                 reg += zr->gbpl;
1983                                         reg = (reg << ZR36057_VSSFGR_DispStride);
1984                                         reg |= ZR36057_VSSFGR_VidOvf;
1985                                         reg |= ZR36057_VSSFGR_SnapShot;
1986                                         reg |= ZR36057_VSSFGR_FrameGrab;
1987                                         btwrite(reg, ZR36057_VSSFGR);
1988
1989                                         btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
1990                                 }
1991                         }
1992                 }
1993 #endif                          /* (IRQ_MASK & ZR36057_ISR_GIRQ0) */
1994
1995 #if (IRQ_MASK & ZR36057_ISR_GIRQ1)
1996                 if (astat & ZR36057_ISR_GIRQ1) {
1997                         unsigned csr = zr36060_read_8(zr, 0x001);
1998                         unsigned isr = zr36060_read_8(zr, 0x008);
1999
2000                         IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_GIRQ1 60_code=%02x 60_intr=%02x\n",
2001                                       zr->name, csr, isr));
2002
2003                         btand(~ZR36057_ICR_GIRQ1, ZR36057_ICR);
2004                         zoran_reap_stat_com(zr);
2005                         zoran_feed_stat_com(zr);
2006                 }
2007 #endif                          /* (IRQ_MASK & ZR36057_ISR_GIRQ1) */
2008
2009 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
2010                 if (astat & ZR36057_ISR_CodRepIRQ) {
2011                         IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n", zr->name));
2012                         btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
2013                 }
2014 #endif                          /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
2015
2016 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
2017                 if ((astat & ZR36057_ISR_JPEGRepIRQ) &&
2018                     (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
2019                      zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)) {
2020                         zoran_reap_stat_com(zr);
2021                         zoran_feed_stat_com(zr);
2022                         wake_up_interruptible(&zr->jpg_capq);
2023                 }
2024 #endif                          /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
2025
2026                 count++;
2027                 if (count > 10) {
2028                         printk(KERN_WARNING "%s: irq loop %d\n", zr->name, count);
2029                         if (count > 20) {
2030                                 btwrite(0, ZR36057_ICR);
2031                                 printk(KERN_ERR  "%s: IRQ lockup, cleared int mask\n", zr->name);
2032                                 break;
2033                         }
2034                 }
2035         }
2036         spin_unlock_irqrestore(&zr->lock, flags);
2037 }
2038
2039 /* Check a zoran_params struct for correctness, insert default params */
2040
2041 static int zoran_check_params(struct zoran *zr, struct zoran_params *params)
2042 {
2043         int err = 0, err0 = 0;
2044
2045         /* insert constant params */
2046
2047         params->major_version = MAJOR_VERSION;
2048         params->minor_version = MINOR_VERSION;
2049
2050         /* Check input and norm */
2051
2052         if (params->input != 0 && params->input != 1) {
2053                 err++;
2054         }
2055         if (params->norm != VIDEO_MODE_PAL && params->norm != VIDEO_MODE_NTSC) {
2056                 err++;
2057         }
2058         /* Check decimation, set default values for decimation = 1, 2, 4 */
2059
2060         switch (params->decimation) {
2061         case 1:
2062
2063                 params->HorDcm = 1;
2064                 params->VerDcm = 1;
2065                 params->TmpDcm = 1;
2066                 params->field_per_buff = 2;
2067
2068                 params->img_x = 0;
2069                 params->img_y = 0;
2070                 params->img_width = 720;
2071                 params->img_height = tvnorms[params->norm].Ha / 2;
2072                 break;
2073
2074         case 2:
2075
2076                 params->HorDcm = 2;
2077                 params->VerDcm = 1;
2078                 params->TmpDcm = 2;
2079                 params->field_per_buff = 1;
2080
2081                 params->img_x = 8;
2082                 params->img_y = 0;
2083                 params->img_width = 704;
2084                 params->img_height = tvnorms[params->norm].Ha / 2;
2085                 break;
2086
2087         case 4:
2088
2089                 params->HorDcm = 4;
2090                 params->VerDcm = 2;
2091                 params->TmpDcm = 2;
2092                 params->field_per_buff = 1;
2093
2094                 params->img_x = 8;
2095                 params->img_y = 0;
2096                 params->img_width = 704;
2097                 params->img_height = tvnorms[params->norm].Ha / 2;
2098                 break;
2099
2100         case 0:
2101
2102                 /* We have to check the data the user has set */
2103
2104                 if (params->HorDcm != 1 && params->HorDcm != 2 && params->HorDcm != 4)
2105                         err0++;
2106                 if (params->VerDcm != 1 && params->VerDcm != 2)
2107                         err0++;
2108                 if (params->TmpDcm != 1 && params->TmpDcm != 2)
2109                         err0++;
2110                 if (params->field_per_buff != 1 && params->field_per_buff != 2)
2111                         err0++;
2112
2113                 if (params->img_x < 0)
2114                         err0++;
2115                 if (params->img_y < 0)
2116                         err0++;
2117                 if (params->img_width < 0)
2118                         err0++;
2119                 if (params->img_height < 0)
2120                         err0++;
2121                 if (params->img_x + params->img_width > 720)
2122                         err0++;
2123                 if (params->img_y + params->img_height > tvnorms[params->norm].Ha / 2)
2124                         err0++;
2125                 if (params->img_width % (16 * params->HorDcm) != 0)
2126                         err0++;
2127                 if (params->img_height % (8 * params->VerDcm) != 0)
2128                         err0++;
2129
2130                 if (err0) {
2131                         err++;
2132                 }
2133                 break;
2134
2135         default:
2136                 err++;
2137                 break;
2138         }
2139
2140         if (params->quality > 100)
2141                 params->quality = 100;
2142         if (params->quality < 5)
2143                 params->quality = 5;
2144
2145         if (params->APPn < 0)
2146                 params->APPn = 0;
2147         if (params->APPn > 15)
2148                 params->APPn = 15;
2149         if (params->APP_len < 0)
2150                 params->APP_len = 0;
2151         if (params->APP_len > 60)
2152                 params->APP_len = 60;
2153         if (params->COM_len < 0)
2154                 params->COM_len = 0;
2155         if (params->COM_len > 60)
2156                 params->COM_len = 60;
2157
2158         if (err)
2159                 return -EINVAL;
2160
2161         return 0;
2162
2163 }
2164 static void zoran_open_init_params(struct zoran *zr)
2165 {
2166         int i;
2167
2168         /* Per default, map the V4L Buffers */
2169
2170         zr->map_mjpeg_buffers = 0;
2171
2172         /* User must explicitly set a window */
2173
2174         zr->window_set = 0;
2175
2176         zr->window.x = 0;
2177         zr->window.y = 0;
2178         zr->window.width = 0;
2179         zr->window.height = 0;
2180         zr->window.chromakey = 0;
2181         zr->window.flags = 0;
2182         zr->window.clips = NULL;
2183         zr->window.clipcount = 0;
2184
2185         zr->video_interlace = 0;
2186
2187         zr->v4l_memgrab_active = 0;
2188         zr->v4l_overlay_active = 0;
2189
2190         zr->v4l_grab_frame = NO_GRAB_ACTIVE;
2191         zr->v4l_grab_seq = 0;
2192
2193         zr->gwidth = 0;
2194         zr->gheight = 0;
2195         zr->gformat = 0;
2196         zr->gbpl = 0;
2197
2198         /* DMA ring stuff for V4L */
2199
2200         zr->v4l_pend_tail = 0;
2201         zr->v4l_pend_head = 0;
2202         for (i = 0; i < v4l_nbufs; i++) {
2203                 zr->v4l_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
2204         }
2205
2206         /* Set necessary params and call zoran_check_params to set the defaults */
2207
2208         zr->params.decimation = 1;
2209
2210         zr->params.quality = 50;        /* default compression factor 8 */
2211         zr->params.odd_even = 1;
2212
2213         zr->params.APPn = 0;
2214         zr->params.APP_len = 0; /* No APPn marker */
2215         for (i = 0; i < 60; i++)
2216                 zr->params.APP_data[i] = 0;
2217
2218         zr->params.COM_len = 0; /* No COM marker */
2219         for (i = 0; i < 60; i++)
2220                 zr->params.COM_data[i] = 0;
2221
2222         zr->params.VFIFO_FB = 0;
2223
2224         memset(zr->params.reserved, 0, sizeof(zr->params.reserved));
2225
2226         zr->params.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
2227
2228         i = zoran_check_params(zr, &zr->params);
2229         if (i)
2230                 printk(KERN_ERR "%s: zoran_open_init_params internal error\n", zr->name);
2231 }
2232
2233 /*
2234  *   Open a buz card. Right now the flags stuff is just playing
2235  */
2236
2237 static int zoran_open(struct video_device *dev, int flags)
2238 {
2239         struct zoran *zr = (struct zoran *) dev;
2240
2241         DEBUG(printk(KERN_INFO ": zoran_open\n"));
2242
2243         switch (flags) {
2244
2245         case 0:
2246                 if (zr->user)
2247                         return -EBUSY;
2248                 zr->user++;
2249
2250                 if (v4l_fbuffer_alloc(zr) < 0) {
2251                         zr->user--;
2252                         return -ENOMEM;
2253                 }
2254                 /* default setup */
2255
2256                 zoran_open_init_params(zr);
2257
2258                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
2259
2260                 btwrite(IRQ_MASK, ZR36057_ISR);         // Clears interrupts
2261
2262                 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
2263
2264                 break;
2265
2266         default:
2267                 return -EBUSY;
2268
2269         }
2270         MOD_INC_USE_COUNT;
2271         return 0;
2272 }
2273
2274 static void zoran_close(struct video_device *dev)
2275 {
2276         struct zoran *zr = (struct zoran *) dev;
2277
2278         DEBUG(printk(KERN_INFO ": zoran_close\n"));
2279         
2280         /* disable interrupts */
2281         btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
2282
2283         /* wake up sleeping beauties */
2284         wake_up_interruptible(&zr->v4l_capq);
2285         wake_up_interruptible(&zr->jpg_capq);
2286
2287         zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
2288         zr36057_set_memgrab(zr, 0);
2289         if (zr->v4l_overlay_active)
2290                 zr36057_overlay(zr, 0);
2291
2292         zr->user--;
2293
2294         v4l_fbuffer_free(zr);
2295         jpg_fbuffer_free(zr);
2296         zr->jpg_nbufs = 0;
2297
2298         MOD_DEC_USE_COUNT;
2299         DEBUG(printk(KERN_INFO ": zoran_close done\n"));
2300 }
2301
2302
2303 static long zoran_read(struct video_device *dev, char *buf, unsigned long count, int nonblock)
2304 {
2305         return -EINVAL;
2306 }
2307
2308 static long zoran_write(struct video_device *dev, const char *buf, unsigned long count, int nonblock)
2309 {
2310         return -EINVAL;
2311 }
2312
2313 /*
2314  *   ioctl routine
2315  */
2316
2317
2318 static int zoran_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2319 {
2320         struct zoran *zr = (struct zoran *) dev;
2321
2322         switch (cmd) {
2323
2324         case VIDIOCGCAP:
2325                 {
2326                         struct video_capability b;
2327                         IOCTL_DEBUG(printk("buz ioctl VIDIOCGCAP\n"));
2328                         strncpy(b.name, zr->video_dev.name, sizeof(b.name));
2329                         b.type = VID_TYPE_CAPTURE |
2330                             VID_TYPE_OVERLAY |
2331                             VID_TYPE_CLIPPING |
2332                             VID_TYPE_FRAMERAM |
2333                             VID_TYPE_SCALES;
2334                         /* theoretically we could also flag VID_TYPE_SUBCAPTURE
2335                            but this is not even implemented in the BTTV driver */
2336
2337                         b.channels = 2;         /* composite, svhs */
2338                         b.audios = 0;
2339                         b.maxwidth = BUZ_MAX_WIDTH;
2340                         b.maxheight = BUZ_MAX_HEIGHT;
2341                         b.minwidth = BUZ_MIN_WIDTH;
2342                         b.minheight = BUZ_MIN_HEIGHT;
2343                         if (copy_to_user(arg, &b, sizeof(b))) {
2344                                 return -EFAULT;
2345                         }
2346                         return 0;
2347                 }
2348
2349         case VIDIOCGCHAN:
2350                 {
2351                         struct video_channel v;
2352
2353                         if (copy_from_user(&v, arg, sizeof(v))) {
2354                                 return -EFAULT;
2355                         }
2356                         IOCTL_DEBUG(printk("buz ioctl VIDIOCGCHAN for channel %d\n", v.channel));
2357                         switch (v.channel) {
2358                         case 0:
2359                                 strcpy(v.name, "Composite");
2360                                 break;
2361                         case 1:
2362                                 strcpy(v.name, "SVHS");
2363                                 break;
2364                         default:
2365                                 return -EINVAL;
2366                         }
2367                         v.tuners = 0;
2368                         v.flags = 0;
2369                         v.type = VIDEO_TYPE_CAMERA;
2370                         v.norm = zr->params.norm;
2371                         if (copy_to_user(arg, &v, sizeof(v))) {
2372                                 return -EFAULT;
2373                         }
2374                         return 0;
2375                 }
2376
2377                 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2378
2379                  * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2380                  *                                 ^^^^^^^
2381                  * The famos BTTV driver has it implemented with a struct video_channel argument
2382                  * and we follow it for compatibility reasons
2383                  *
2384                  * BTW: this is the only way the user can set the norm!
2385                  */
2386
2387         case VIDIOCSCHAN:
2388                 {
2389                         struct video_channel v;
2390                         int input;
2391                         int on, res;
2392
2393                         if (copy_from_user(&v, arg, sizeof(v))) {
2394                                 return -EFAULT;
2395                         }
2396                         IOCTL_DEBUG(printk("buz ioctl VIDIOCSCHAN: channel=%d, norm=%d\n", v.channel, v.norm));
2397                         switch (v.channel) {
2398                         case 0:
2399                                 input = 3;
2400                                 break;
2401                         case 1:
2402                                 input = 7;
2403                                 break;
2404                         default:
2405                                 return -EINVAL;
2406                         }
2407
2408                         if (v.norm != VIDEO_MODE_PAL
2409                             && v.norm != VIDEO_MODE_NTSC) {
2410                                 return -EINVAL;
2411                         }
2412                         zr->params.norm = v.norm;
2413                         zr->params.input = v.channel;
2414
2415                         /* We switch overlay off and on since a change in the norm
2416                            needs different VFE settings */
2417
2418                         on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2419                         if (on)
2420                                 zr36057_overlay(zr, 0);
2421
2422                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2423                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2424                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
2425
2426                         if (on)
2427                                 zr36057_overlay(zr, 1);
2428
2429                         /* Make sure the changes come into effect */
2430                         res = wait_grab_pending(zr);
2431                         if (res)
2432                                 return res;
2433
2434                         return 0;
2435                 }
2436
2437         case VIDIOCGTUNER:
2438         case VIDIOCSTUNER:
2439                         return -EINVAL;
2440
2441         case VIDIOCGPICT:
2442                 {
2443                         struct video_picture p = zr->picture;
2444
2445                         IOCTL_DEBUG(printk("buz ioctl VIDIOCGPICT\n"));
2446                         p.depth = zr->buffer.depth;
2447                         switch (zr->buffer.depth) {
2448                         case 15:
2449                                 p.palette = VIDEO_PALETTE_RGB555;
2450                                 break;
2451
2452                         case 16:
2453                                 p.palette = VIDEO_PALETTE_RGB565;
2454                                 break;
2455
2456                         case 24:
2457                                 p.palette = VIDEO_PALETTE_RGB24;
2458                                 break;
2459
2460                         case 32:
2461                                 p.palette = VIDEO_PALETTE_RGB32;
2462                                 break;
2463                         }
2464
2465                         if (copy_to_user(arg, &p, sizeof(p))) {
2466                                 return -EFAULT;
2467                         }
2468                         return 0;
2469                 }
2470
2471         case VIDIOCSPICT:
2472                 {
2473                         struct video_picture p;
2474
2475                         if (copy_from_user(&p, arg, sizeof(p))) {
2476                                 return -EFAULT;
2477                         }
2478                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
2479                         IOCTL_DEBUG(printk("buz ioctl VIDIOCSPICT bri=%d hue=%d col=%d con=%d dep=%d pal=%d\n",
2480                                            p.brightness, p.hue, p.colour, p.contrast, p.depth, p.palette));
2481                         /* The depth and palette values have no meaning to us,
2482                            should we return  -EINVAL if they don't fit ? */
2483                         zr->picture = p;
2484                         return 0;
2485                 }
2486
2487         case VIDIOCCAPTURE:
2488                 {
2489                         int v, res;
2490
2491                         if (copy_from_user(&v, arg, sizeof(v))) {
2492                                 return -EFAULT;
2493                         }
2494                         IOCTL_DEBUG(printk("buz ioctl VIDIOCCAPTURE: %d\n", v));
2495                         /* If there is nothing to do, return immediatly */
2496
2497                         if ((v && zr->v4l_overlay_active) || (!v && !zr->v4l_overlay_active))
2498                                 return 0;
2499
2500                         if (v == 0) {
2501                                 zr->v4l_overlay_active = 0;
2502                                 if (!zr->v4l_memgrab_active)
2503                                         zr36057_overlay(zr, 0);
2504                                 /* When a grab is running, the video simply won't be switched on any more */
2505                         } else {
2506                                 if (!zr->buffer_set || !zr->window_set) {
2507                                         return -EINVAL;
2508                                 }
2509                                 zr->v4l_overlay_active = 1;
2510                                 if (!zr->v4l_memgrab_active)
2511                                         zr36057_overlay(zr, 1);
2512                                 /* When a grab is running, the video will be switched on when grab is finished */
2513                         }
2514                         /* Make sure the changes come into effect */
2515                         res = wait_grab_pending(zr);
2516                         if (res)
2517                                 return res;
2518                         return 0;
2519                 }
2520
2521         case VIDIOCGWIN:
2522                 {
2523                         IOCTL_DEBUG(printk("buz ioctl VIDIOCGWIN\n"));
2524                         if (copy_to_user(arg, &zr->window, sizeof(zr->window))) {
2525                                 return -EFAULT;
2526                         }
2527                         return 0;
2528                 }
2529
2530         case VIDIOCSWIN:
2531                 {
2532                         struct video_clip *vcp;
2533                         struct video_window vw;
2534                         int on, end, res;
2535
2536                         if (copy_from_user(&vw, arg, sizeof(vw))) {
2537                                 return -EFAULT;
2538                         }
2539                         IOCTL_DEBUG(printk("buz ioctl VIDIOCSWIN: x=%d y=%d w=%d h=%d clipcount=%d\n", vw.x, vw.y, vw.width, vw.height, vw.clipcount));
2540                         if (!zr->buffer_set) {
2541                                 return -EINVAL;
2542                         }
2543                         /*
2544                          * The video front end needs 4-byte alinged line sizes, we correct that
2545                          * silently here if necessary
2546                          */
2547
2548                         if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
2549                                 end = (vw.x + vw.width) & ~1;   /* round down */
2550                                 vw.x = (vw.x + 1) & ~1;         /* round up */
2551                                 vw.width = end - vw.x;
2552                         }
2553                         if (zr->buffer.depth == 24) {
2554                                 end = (vw.x + vw.width) & ~3;   /* round down */
2555                                 vw.x = (vw.x + 3) & ~3;         /* round up */
2556                                 vw.width = end - vw.x;
2557                         }
2558 #if 0
2559                         // At least xawtv seems to care about the following - just leave it away
2560                         /*
2561                          * Also corrected silently (as long as window fits at all):
2562                          * video not fitting the screen
2563                          */
2564 #if 0
2565                         if (vw.x < 0 || vw.y < 0 || vw.x + vw.width > zr->buffer.width ||
2566                             vw.y + vw.height > zr->buffer.height) {
2567                                 printk(BUZ_ERR ": VIDIOCSWIN: window does not fit frame buffer: %dx%d+%d*%d\n",
2568                                        vw.width, vw.height, vw.x, vw.y);
2569                                 return -EINVAL;
2570                         }
2571 #else
2572                         if (vw.x < 0)
2573                                 vw.x = 0;
2574                         if (vw.y < 0)
2575                                 vw.y = 0;
2576                         if (vw.x + vw.width > zr->buffer.width)
2577                                 vw.width = zr->buffer.width - vw.x;
2578                         if (vw.y + vw.height > zr->buffer.height)
2579                                 vw.height = zr->buffer.height - vw.y;
2580 #endif
2581 #endif
2582
2583                         /* Check for vaild parameters */
2584                         if (vw.width < BUZ_MIN_WIDTH || vw.height < BUZ_MIN_HEIGHT ||
2585                             vw.width > BUZ_MAX_WIDTH || vw.height > BUZ_MAX_HEIGHT) {
2586                                 return -EINVAL;
2587                         }
2588 #ifdef XAWTV_HACK
2589                         if (vw.width > 720)
2590                                 vw.width = 720;
2591 #endif
2592
2593                         zr->window.x = vw.x;
2594                         zr->window.y = vw.y;
2595                         zr->window.width = vw.width;
2596                         zr->window.height = vw.height;
2597                         zr->window.chromakey = 0;
2598                         zr->window.flags = 0;   // RJ: Is this intended for interlace on/off ?
2599
2600                         zr->window.clips = NULL;
2601                         zr->window.clipcount = vw.clipcount;
2602
2603                         /*
2604                          * If an overlay is running, we have to switch it off
2605                          * and switch it on again in order to get the new settings in effect.
2606                          *
2607                          * We also want to avoid that the overlay mask is written
2608                          * when an overlay is running.
2609                          */
2610
2611                         on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2612                         if (on)
2613                                 zr36057_overlay(zr, 0);
2614
2615                         /*
2616                          *   Write the overlay mask if clips are wanted.
2617                          */
2618                         if (vw.clipcount) {
2619                                 vcp = vmalloc(sizeof(struct video_clip) * (vw.clipcount + 4));
2620                                 if (vcp == NULL) {
2621                                         return -ENOMEM;
2622                                 }
2623                                 if (copy_from_user(vcp, vw.clips, sizeof(struct video_clip) * vw.clipcount)) {
2624                                         vfree(vcp);
2625                                         return -EFAULT;
2626                                 }
2627                                 write_overlay_mask(zr, vcp, vw.clipcount);
2628                                 vfree(vcp);
2629                         }
2630                         if (on)
2631                                 zr36057_overlay(zr, 1);
2632                         zr->window_set = 1;
2633
2634                         /* Make sure the changes come into effect */
2635                         res = wait_grab_pending(zr);
2636                         if (res)
2637                                 return res;
2638
2639                         return 0;
2640                 }
2641
2642         case VIDIOCGFBUF:
2643                 {
2644                         IOCTL_DEBUG(printk("buz ioctl VIDIOCGFBUF\n"));
2645                         if (copy_to_user(arg, &zr->buffer, sizeof(zr->buffer))) {
2646                                 return -EFAULT;
2647                         }
2648                         return 0;
2649                 }
2650
2651         case VIDIOCSFBUF:
2652                 {
2653                         struct video_buffer v;
2654
2655                         if (!capable(CAP_SYS_ADMIN)
2656                         || !capable(CAP_SYS_RAWIO))
2657                                 return -EPERM;
2658
2659                         if (copy_from_user(&v, arg, sizeof(v)))
2660                                 return -EFAULT;
2661                         
2662                         IOCTL_DEBUG(printk("buz ioctl VIDIOCSFBUF: base=0x%x w=%d h=%d depth=%d bpl=%d\n", (u32) v.base, v.width, v.height, v.depth, v.bytesperline));
2663                         if (zr->v4l_overlay_active) {
2664                                 /* Has the user gotten crazy ... ? */
2665                                 return -EINVAL;
2666                         }
2667                         if (v.depth != 15
2668                             && v.depth != 16
2669                             && v.depth != 24
2670                             && v.depth != 32) {
2671                                 return -EINVAL;
2672                         }
2673                         if (v.height <= 0 || v.width <= 0 || v.bytesperline <= 0) {
2674                                 return -EINVAL;
2675                         }
2676                         if (v.bytesperline & 3) {
2677                                 return -EINVAL;
2678                         }
2679                         if (v.base) {
2680                                 zr->buffer.base = (void *) ((unsigned long) v.base & ~3);
2681                         }
2682                         zr->buffer.height = v.height;
2683                         zr->buffer.width = v.width;
2684                         zr->buffer.depth = v.depth;
2685                         zr->buffer.bytesperline = v.bytesperline;
2686
2687                         if (zr->buffer.base)
2688                                 zr->buffer_set = 1;
2689                         zr->window_set = 0;     /* The user should set new window parameters */
2690                         return 0;
2691                 }
2692
2693                 /* RJ: what is VIDIOCKEY intended to do ??? */
2694
2695         case VIDIOCGFREQ:
2696         case VIDIOCSFREQ:
2697         case VIDIOCGAUDIO:
2698         case VIDIOCSAUDIO:
2699                 return -EINVAL;
2700                 
2701         case VIDIOCSYNC:
2702                 {
2703                         int v;
2704
2705                         if (copy_from_user(&v, arg, sizeof(v))) {
2706                                 return -EFAULT;
2707                         }
2708                         IOCTL_DEBUG(printk("buz ioctl VIDIOCSYNC %d\n", v));
2709                         return v4l_sync(zr, v);
2710                 }
2711
2712         case VIDIOCMCAPTURE:
2713                 {
2714                         struct video_mmap vm;
2715
2716                         if (copy_from_user((void *) &vm, (void *) arg, sizeof(vm))) {
2717                                 return -EFAULT;
2718                         }
2719                         IOCTL_DEBUG(printk("buz ioctl VIDIOCMCAPTURE frame=%d geom=%dx%d fmt=%d\n",
2720                                vm.frame, vm.height, vm.width, vm.format));
2721                         return v4l_grab(zr, &vm);
2722                 }
2723
2724         case VIDIOCGMBUF:
2725                 {
2726                         struct video_mbuf vm;
2727                         int i;
2728
2729                         IOCTL_DEBUG(printk("buz ioctl VIDIOCGMBUF\n"));
2730                 
2731                         vm.size = v4l_nbufs * v4l_bufsize;
2732                         vm.frames = v4l_nbufs;
2733                         for (i = 0; i < v4l_nbufs; i++) {
2734                                 vm.offsets[i] = i * v4l_bufsize;
2735                         }
2736
2737                         /* The next mmap will map the V4L buffers */
2738                         zr->map_mjpeg_buffers = 0;
2739
2740                         if (copy_to_user(arg, &vm, sizeof(vm))) {
2741                                 return -EFAULT;
2742                         }
2743                         return 0;
2744                 }
2745
2746         case VIDIOCGUNIT:
2747                 {
2748                         struct video_unit vu;
2749
2750                         IOCTL_DEBUG(printk("buz ioctl VIDIOCGUNIT\n"));
2751                         vu.video = zr->video_dev.minor;
2752                         vu.vbi = VIDEO_NO_UNIT;
2753                         vu.radio = VIDEO_NO_UNIT;
2754                         vu.audio = VIDEO_NO_UNIT;
2755                         vu.teletext = VIDEO_NO_UNIT;
2756                         if (copy_to_user(arg, &vu, sizeof(vu)))
2757                                 return -EFAULT;
2758                         return 0;
2759                 }
2760
2761                 /*
2762                  * RJ: In principal we could support subcaptures for V4L grabbing.
2763                  *     Not even the famous BTTV driver has them, however.
2764                  *     If there should be a strong demand, one could consider
2765                  *     to implement them.
2766                  */
2767         case VIDIOCGCAPTURE:
2768         case VIDIOCSCAPTURE:
2769                         return -EINVAL;
2770
2771         case BUZIOC_G_PARAMS:
2772                 {
2773                         IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_PARAMS\n"));
2774                         if (copy_to_user(arg, &(zr->params), sizeof(zr->params))) 
2775                                 return -EFAULT;
2776                         return 0;
2777                 }
2778
2779         case BUZIOC_S_PARAMS:
2780                 {
2781                         struct zoran_params bp;
2782                         int input, on;
2783
2784                         if (zr->codec_mode != BUZ_MODE_IDLE) {
2785                                 return -EINVAL;
2786                         }
2787                         if (copy_from_user(&bp, arg, sizeof(bp))) {
2788                                 return -EFAULT;
2789                         }
2790                         IOCTL_DEBUG(printk("buz ioctl BUZIOC_S_PARAMS\n"));
2791                         
2792                         /* Check the params first before overwriting our internal values */
2793
2794                         if (zoran_check_params(zr, &bp))
2795                                 return -EINVAL;
2796
2797                         zr->params = bp;
2798
2799                         /* Make changes of input and norm go into effect immediatly */
2800
2801                         /* We switch overlay off and on since a change in the norm
2802                            needs different VFE settings */
2803
2804                         on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2805                         if (on)
2806                                 zr36057_overlay(zr, 0);
2807
2808                         input = zr->params.input == 0 ? 3 : 7;
2809                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2810                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2811                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
2812
2813                         if (on)
2814                                 zr36057_overlay(zr, 1);
2815
2816                         if (copy_to_user(arg, &bp, sizeof(bp))) {
2817                                 return -EFAULT;
2818                         }
2819                         return 0;
2820                 }
2821
2822         case BUZIOC_REQBUFS:
2823                 {
2824                         struct zoran_requestbuffers br;
2825
2826                         if (zr->jpg_buffers_allocated) {
2827                                 return -EINVAL;
2828                         }
2829                         if (copy_from_user(&br, arg, sizeof(br))) {
2830                                 return -EFAULT;
2831                         }
2832                         IOCTL_DEBUG(printk("buz ioctl BUZIOC_REQBUFS count = %lu size=%lu\n",
2833                                            br.count, br.size));
2834                         /* Enforce reasonable lower and upper limits */
2835                         if (br.count < 4)
2836                                 br.count = 4;   /* Could be choosen smaller */
2837                         if (br.count > BUZ_MAX_FRAME)
2838                                 br.count = BUZ_MAX_FRAME;
2839                         br.size = PAGE_ALIGN(br.size);
2840                         if (br.size < 8192)
2841                                 br.size = 8192;         /* Arbitrary */
2842                         /* br.size is limited by 1 page for the stat_com tables to a Maximum of 2 MB */
2843                         if (br.size > (512 * 1024))
2844                                 br.size = (512 * 1024);         /* 512 K should be enough */
2845                         if (zr->need_contiguous && br.size > MAX_KMALLOC_MEM)
2846                                 br.size = MAX_KMALLOC_MEM;
2847
2848                         zr->jpg_nbufs = br.count;
2849                         zr->jpg_bufsize = br.size;
2850
2851                         if (jpg_fbuffer_alloc(zr))
2852                                 return -ENOMEM;
2853
2854                         /* The next mmap will map the MJPEG buffers */
2855                         zr->map_mjpeg_buffers = 1;
2856
2857                         if (copy_to_user(arg, &br, sizeof(br))) {
2858                                 return -EFAULT;
2859                         }
2860                         return 0;
2861                 }
2862
2863         case BUZIOC_QBUF_CAPT:
2864                 {
2865                         int nb;
2866
2867                         if (copy_from_user((void *) &nb, (void *) arg, sizeof(int))) {
2868                                 return -EFAULT;
2869                         }
2870                         IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_CAPT %d\n", nb));
2871                         return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_COMPRESS);
2872                 }
2873
2874         case BUZIOC_QBUF_PLAY:
2875                 {
2876                         int nb;
2877
2878                         if (copy_from_user((void *) &nb, (void *) arg, sizeof(int))) {
2879                                 return -EFAULT;
2880                         }
2881                         IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_PLAY %d\n", nb));
2882                         return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_DECOMPRESS);
2883                 }
2884
2885         case BUZIOC_SYNC:
2886                 {
2887                         struct zoran_sync bs;
2888                         int res;
2889
2890                         IOCTL_DEBUG(printk("buz ioctl BUZIOC_SYNC\n"));
2891                         res = jpg_sync(zr, &bs);
2892                         if (copy_to_user(arg, &bs, sizeof(bs))) {
2893                                 return -EFAULT;
2894                         }
2895                         return res;
2896                 }
2897
2898         case BUZIOC_G_STATUS:
2899                 {
2900                         struct zoran_status bs;
2901                         int norm, input, status;
2902
2903                         if (zr->codec_mode != BUZ_MODE_IDLE) {
2904                                 return -EINVAL;
2905                         }
2906                         if (copy_from_user(&bs, arg, sizeof(bs))) {
2907                                 return -EFAULT;
2908                         }
2909                         IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_STATUS\n"));
2910                         switch (bs.input) {
2911                         case 0:
2912                                 input = 3;
2913                                 break;
2914                         case 1:
2915                                 input = 7;
2916                                 break;
2917                         default:
2918                                 return -EINVAL;
2919                         }
2920
2921                         /* Set video norm to VIDEO_MODE_AUTO */
2922
2923                         norm = VIDEO_MODE_AUTO;
2924                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2925                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
2926
2927                         /* sleep 1 second */
2928
2929                         schedule_timeout(HZ);
2930                         
2931                         /* Get status of video decoder */
2932
2933                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_GET_STATUS, &status);
2934                         bs.signal = (status & DECODER_STATUS_GOOD) ? 1 : 0;
2935                         bs.norm = (status & DECODER_STATUS_NTSC) ? VIDEO_MODE_NTSC : VIDEO_MODE_PAL;
2936                         bs.color = (status & DECODER_STATUS_COLOR) ? 1 : 0;
2937
2938                         /* restore previous input and norm */
2939                         input = zr->params.input == 0 ? 3 : 7;
2940                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2941                         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2942
2943                         if (copy_to_user(arg, &bs, sizeof(bs))) {
2944                                 return -EFAULT;
2945                         }
2946                         return 0;
2947                 }
2948
2949         default:
2950                     return -ENOIOCTLCMD;
2951
2952         }
2953         return 0;
2954 }
2955
2956
2957 /*
2958  *   This maps the buffers to user space.
2959  *
2960  *   Depending on the state of zr->map_mjpeg_buffers
2961  *   the V4L or the MJPEG buffers are mapped
2962  *
2963  */
2964
2965 static int zoran_mmap(struct video_device *dev, const char *adr, unsigned long size)
2966 {
2967         struct zoran *zr = (struct zoran *) dev;
2968         unsigned long start = (unsigned long) adr;
2969         unsigned long page, pos, todo, fraglen;
2970         int i, j;
2971
2972         if (zr->map_mjpeg_buffers) {
2973                 /* Map the MJPEG buffers */
2974
2975                 if (!zr->jpg_buffers_allocated) {
2976                         return -ENOMEM;
2977                 }
2978                 if (size > zr->jpg_nbufs * zr->jpg_bufsize) {
2979                         return -EINVAL;
2980                 }
2981
2982                 for (i = 0; i < zr->jpg_nbufs; i++) {
2983                         for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
2984                                 fraglen = (zr->jpg_gbuf[i].frag_tab[2 * j + 1] & ~1) << 1;
2985                                 todo = size;
2986                                 if (todo > fraglen)
2987                                         todo = fraglen;
2988                                 pos = (unsigned long) zr->jpg_gbuf[i].frag_tab[2 * j];
2989                                 page = virt_to_phys(bus_to_virt(pos));  /* should just be pos on i386 */
2990                                 if (remap_page_range(start, page, todo, PAGE_SHARED)) {
2991                                         printk(KERN_ERR "%s: zoran_mmap(V4L): remap_page_range failed\n", zr->name);
2992                                         return -EAGAIN;
2993                                 }
2994                                 size -= todo;
2995                                 start += todo;
2996                                 if (size == 0)
2997                                         break;
2998                                 if (zr->jpg_gbuf[i].frag_tab[2 * j + 1] & 1)
2999                                         break;  /* was last fragment */
3000                         }
3001                         if (size == 0)
3002                                 break;
3003                 }
3004         } else {
3005                 /* Map the V4L buffers */
3006
3007                 if (size > v4l_nbufs * v4l_bufsize) {
3008                         return -EINVAL;
3009                 }
3010
3011                 for (i = 0; i < v4l_nbufs; i++) {
3012                         todo = size;
3013                         if (todo > v4l_bufsize)
3014                                 todo = v4l_bufsize;
3015                         page = zr->v4l_gbuf[i].fbuffer_phys;
3016                         DEBUG(printk("V4L remap page range %d 0x%x %d to 0x%x\n", i, page, todo, start));
3017                         if (remap_page_range(start, page, todo, PAGE_SHARED)) {
3018                                 printk(KERN_ERR "%s: zoran_mmap(V4L): remap_page_range failed\n", zr->name);
3019                                 return -EAGAIN;
3020                         }
3021                         size -= todo;
3022                         start += todo;
3023                         if (size == 0)
3024                                 break;
3025                 }
3026         }
3027         return 0;
3028 }
3029
3030 static struct video_device zoran_template =
3031 {
3032         name:           BUZ_NAME,
3033         type:           VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CLIPPING | VID_TYPE_FRAMERAM |
3034                         VID_TYPE_SCALES | VID_TYPE_SUBCAPTURE,
3035         hardware:       VID_HARDWARE_ZR36067,
3036         open:           zoran_open,
3037         close:          zoran_close,
3038         read:           zoran_read,
3039         write:          zoran_write,
3040         ioctl:          zoran_ioctl,
3041         mmap:           zoran_mmap,
3042 };
3043
3044 static int zr36057_init(int i)
3045 {
3046         struct zoran *zr = &zoran[i];
3047         unsigned long mem;
3048         unsigned mem_needed;
3049         int j;
3050         int rev;
3051
3052         /* reset zr36057 */
3053         btwrite(0, ZR36057_SPGPPCR);
3054         mdelay(10);
3055
3056         /* default setup of all parameters which will persist beetween opens */
3057
3058         zr->user = 0;
3059         
3060         init_waitqueue_head(&zr->v4l_capq);
3061         init_waitqueue_head(&zr->jpg_capq);
3062
3063         zr->map_mjpeg_buffers = 0;      /* Map V4L buffers by default */
3064
3065         zr->jpg_nbufs = 0;
3066         zr->jpg_bufsize = 0;
3067         zr->jpg_buffers_allocated = 0;
3068
3069         zr->buffer_set = 0;     /* Flag if frame buffer has been set */
3070         zr->buffer.base = (void *) vidmem;
3071         zr->buffer.width = 0;
3072         zr->buffer.height = 0;
3073         zr->buffer.depth = 0;
3074         zr->buffer.bytesperline = 0;
3075
3076         zr->params.norm = default_norm ? 1 : 0; /* Avoid nonsense settings from user */
3077         zr->params.input = default_input ? 1 : 0;       /* Avoid nonsense settings from user */
3078         zr->video_interlace = 0;
3079
3080         /* Should the following be reset at every open ? */
3081
3082         zr->picture.colour = 32768;
3083         zr->picture.brightness = 32768;
3084         zr->picture.hue = 32768;
3085         zr->picture.contrast = 32768;
3086         zr->picture.whiteness = 0;
3087         zr->picture.depth = 0;
3088         zr->picture.palette = 0;
3089
3090         for (j = 0; j < VIDEO_MAX_FRAME; j++) {
3091                 zr->v4l_gbuf[i].fbuffer = 0;
3092                 zr->v4l_gbuf[i].fbuffer_phys = 0;
3093                 zr->v4l_gbuf[i].fbuffer_bus = 0;
3094         }
3095
3096         zr->stat_com = 0;
3097
3098         /* default setup (will be repeated at every open) */
3099
3100         zoran_open_init_params(zr);
3101
3102         /* allocate memory *before* doing anything to the hardware in case allocation fails */
3103
3104         /* STAT_COM table and overlay mask */
3105
3106         mem_needed = (BUZ_NUM_STAT_COM + ((BUZ_MAX_WIDTH + 31) / 32) * BUZ_MAX_HEIGHT) * 4;
3107         mem = (unsigned long) kmalloc(mem_needed, GFP_KERNEL);
3108         if (!mem) {
3109                 return -ENOMEM;
3110         }
3111         memset((void *) mem, 0, mem_needed);
3112
3113         zr->stat_com = (u32 *) mem;
3114         for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
3115                 zr->stat_com[j] = 1;    /* mark as unavailable to zr36057 */
3116         }
3117         zr->overlay_mask = (u32 *) (mem + BUZ_NUM_STAT_COM * 4);
3118
3119         /* Initialize zr->jpg_gbuf */
3120
3121         for (j = 0; j < BUZ_MAX_FRAME; j++) {
3122                 zr->jpg_gbuf[j].frag_tab = 0;
3123                 zr->jpg_gbuf[j].frag_tab_bus = 0;
3124                 zr->jpg_gbuf[j].state = BUZ_STATE_USER;
3125                 zr->jpg_gbuf[j].bs.frame = j;
3126         }
3127
3128         /* take zr36057 out of reset now */
3129         btwrite(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
3130         mdelay(10);
3131
3132         /* stop all DMA processes */
3133         btwrite(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
3134         btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
3135         /* assert P_Reset */
3136         btwrite(0, ZR36057_JPC);
3137
3138         switch(zr->board)
3139         {
3140                 case BOARD_BUZ:
3141         
3142                         /* set up GPIO direction */
3143                         btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
3144
3145                         /* Set up guest bus timing - Guests 0..3 Tdur=12, Trec=3 */
3146                         btwrite((GPIO_MASK << 24) | 0x8888, ZR36057_GPPGCR1);
3147                         mdelay(10);
3148
3149                         /* reset video decoder */
3150
3151                         GPIO(zr, 0, 0);
3152                         mdelay(10);
3153                         GPIO(zr, 0, 1);
3154                         mdelay(10);
3155
3156                         /* reset JPEG codec */
3157                         zr36060_sleep(zr, 0);
3158                         mdelay(10);
3159                         zr36060_reset(zr);
3160                         mdelay(10);
3161         
3162                         /* display codec revision */
3163                         if ((rev=zr36060_read_8(zr, 0x022)) == 0x33) {
3164                                 printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)\n",
3165                                zr->name, zr36060_read_8(zr, 0x023));
3166                         } else {
3167                                 printk(KERN_ERR "%s: Zoran ZR36060 not found (Rev=%d)\n", zr->name, rev);
3168                                 kfree((void *) zr->stat_com);
3169                                 return -1;
3170                         }
3171                         break;
3172                         
3173                 case BOARD_LML33:
3174 //                      btwrite(btread(ZR36057_SPGPPCR)&~ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3175 //                      udelay(100);
3176 //                      btwrite(btread(ZR36057_SPGPPCR)|ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3177 //                      udelay(1000);
3178
3179                         /*
3180                          *      Set up the GPIO direction
3181                          */
3182                         btwrite(btread(ZR36057_SPGPPCR_SoftReset)|0 , ZR36057_SPGPPCR);
3183                         /* Set up guest bus timing - Guests 0..2 Tdur=12, Trec=3 */
3184                         btwrite(0xFF00F888, ZR36057_GPPGCR1);
3185                         mdelay(10);
3186                         GPIO(zr, 5, 0);         /* Analog video bypass */
3187                         udelay(3000);
3188                         GPIO(zr, 0, 0);         /* Reset 819 */
3189                         udelay(3000);
3190                         GPIO(zr, 0, 1);         /* 819 back */
3191                         udelay(3000);
3192                         /* reset JPEG codec */
3193                         zr36060_sleep(zr, 0);
3194                         udelay(3000);
3195                         zr36060_reset(zr);
3196                         udelay(3000);
3197
3198                         /* display codec revision */
3199                         if ((rev=zr36060_read_8(zr, 0x022)) == 0x33) {
3200                                 printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)\n",
3201                                zr->name, zr36060_read_8(zr, 0x023));
3202                         } else {
3203                                 printk(KERN_ERR "%s: Zoran ZR36060 not found (rev=%d)\n", zr->name, rev);
3204                                 kfree((void *) zr->stat_com);
3205                                 return -1;
3206                         }
3207                         break;
3208         }
3209         /* i2c */
3210         memcpy(&zr->i2c, &zoran_i2c_bus_template, sizeof(struct i2c_bus));
3211         sprintf(zr->i2c.name, "zoran%u", zr->id);
3212         zr->i2c.data = zr;
3213         if (i2c_register_bus(&zr->i2c) < 0) {
3214                 kfree((void *) zr->stat_com);
3215                 return -1;
3216         }
3217         /*
3218          *   Now add the template and register the device unit.
3219          */
3220         memcpy(&zr->video_dev, &zoran_template, sizeof(zoran_template));
3221         sprintf(zr->video_dev.name, "zoran%u", zr->id);
3222         if (video_register_device(&zr->video_dev, VFL_TYPE_GRABBER) < 0) {
3223                 i2c_unregister_bus(&zr->i2c);
3224                 kfree((void *) zr->stat_com);
3225                 return -1;
3226         }
3227         /* toggle JPEG codec sleep to sync PLL */
3228         zr36060_sleep(zr, 1);
3229         mdelay(10);
3230         zr36060_sleep(zr, 0);
3231         mdelay(10);
3232
3233         /* Enable bus-mastering */
3234         pci_set_master(zr->pci_dev);
3235
3236         j = zr->params.input == 0 ? 3 : 7;
3237         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &j);
3238         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
3239         i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
3240
3241         /* set individual interrupt enables (without GIRQ0)
3242            but don't global enable until zoran_open() */
3243
3244         btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ0, ZR36057_ICR);
3245
3246         if(request_irq(zr->pci_dev->irq, zoran_irq,
3247                SA_SHIRQ | SA_INTERRUPT, zr->name, (void *) zr)<0)
3248         {
3249                 printk(KERN_ERR "%s: Can't assign irq.\n", zr->name);
3250                 video_unregister_device(&zr->video_dev);
3251                 i2c_unregister_bus(&zr->i2c);
3252                 kfree((void *) zr->stat_com);
3253                 return -1;
3254         }
3255         zr->initialized = 1;
3256         return 0;
3257 }
3258
3259
3260
3261 static void release_zoran(void)
3262 {
3263         u8 command;
3264         int i;
3265         struct zoran *zr;
3266
3267         for (i = 0; i < zoran_num; i++) {
3268                 zr = &zoran[i];
3269
3270                 if (!zr->initialized)
3271                         continue;
3272
3273                 /* unregister i2c_bus */
3274                 i2c_unregister_bus((&zr->i2c));
3275
3276                 /* disable PCI bus-mastering */
3277                 pci_read_config_byte(zr->pci_dev, PCI_COMMAND, &command);
3278                 command &= ~PCI_COMMAND_MASTER;
3279                 pci_write_config_byte(zr->pci_dev, PCI_COMMAND, command);
3280
3281                 /* put chip into reset */
3282                 btwrite(0, ZR36057_SPGPPCR);
3283
3284                 free_irq(zr->pci_dev->irq, zr);
3285
3286                 /* unmap and free memory */
3287
3288                 kfree((void *) zr->stat_com);
3289
3290                 iounmap(zr->zr36057_mem);
3291
3292                 video_unregister_device(&zr->video_dev);
3293         }
3294 }
3295
3296 /*
3297  *   Scan for a Buz card (actually for the PCI controller ZR36057),
3298  *   request the irq and map the io memory
3299  */
3300
3301 static int find_zr36057(void)
3302 {
3303         unsigned char latency;
3304         struct zoran *zr;
3305         struct pci_dev *dev = NULL;
3306
3307         zoran_num = 0;
3308
3309         while (zoran_num < BUZ_MAX
3310                && (dev = pci_find_device(PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
3311                 zr = &zoran[zoran_num];
3312                 zr->pci_dev = dev;
3313                 zr->zr36057_mem = NULL;
3314                 zr->id = zoran_num;
3315                 sprintf(zr->name, "zoran%u", zr->id);
3316
3317                 spin_lock_init(&zr->lock);
3318
3319                 if (pci_enable_device(dev))
3320                         continue;
3321
3322                 zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
3323                 pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION, &zr->revision);
3324                 if (zr->revision < 2) {
3325                         printk(KERN_INFO "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
3326                                zr->name, zr->revision, zr->pci_dev->irq, zr->zr36057_adr);
3327                 } else {
3328                         unsigned short ss_vendor_id, ss_id;
3329
3330                         ss_vendor_id = zr->pci_dev->subsystem_vendor;
3331                         ss_id = zr->pci_dev->subsystem_device;
3332                         printk(KERN_INFO "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
3333                                zr->name, zr->revision, zr->pci_dev->irq, zr->zr36057_adr);
3334                         printk(KERN_INFO "%s: subsystem vendor=0x%04x id=0x%04x\n",
3335                                zr->name, ss_vendor_id, ss_id);
3336                         if(ss_vendor_id==0xFF10 && ss_id == 0xDE41)
3337                         {
3338                                 zr->board = BOARD_LML33;
3339                                 printk(KERN_INFO "%s: LML33 detected.\n", zr->name);
3340                         }
3341                 }
3342
3343                 zr->zr36057_mem = ioremap(zr->zr36057_adr, 0x1000);
3344                 if (!zr->zr36057_mem) {
3345                         printk(KERN_ERR "%s: ioremap failed\n", zr->name);
3346                         /* XXX handle error */
3347                 }
3348
3349                 /* set PCI latency timer */
3350                 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, &latency);
3351                 if (latency != 48) {
3352                         printk(KERN_INFO "%s: Changing PCI latency from %d to 48.\n", zr->name, latency);
3353                         latency = 48;
3354                         pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, latency);
3355                 }
3356                 zoran_num++;
3357         }
3358         if (zoran_num == 0)
3359                 printk(KERN_INFO "zoran: no cards found.\n");
3360
3361         return zoran_num;
3362 }
3363
3364 static void handle_chipset(void)
3365 {
3366         if(pci_pci_problems&PCIPCI_FAIL)
3367         {
3368                 printk(KERN_WARNING "buz: This configuration is known to have PCI to PCI DMA problems\n");
3369                 printk(KERN_WARNING "buz: You may not be able to use overlay mode.\n");
3370         }
3371                         
3372
3373         if(pci_pci_problems&PCIPCI_TRITON)
3374         {
3375                 printk("buz: Enabling Triton support.\n");
3376                 triton = 1;
3377         }
3378         
3379         if(pci_pci_problems&PCIPCI_NATOMA)
3380         {
3381                 printk("buz: Enabling Natoma workaround.\n");
3382                 natoma = 1;
3383         }
3384 }
3385
3386 #ifdef MODULE
3387 int init_module(void)
3388 #else
3389 int init_zoran_cards(struct video_init *unused)
3390 #endif
3391 {
3392         int i;
3393
3394
3395         printk(KERN_INFO "Zoran driver 1.00 (c) 1999 Rainer Johanni, Dave Perks.\n");
3396
3397         /* Look for Buz cards */
3398
3399         if (find_zr36057() <= 0) {
3400                 return -EIO;
3401         }
3402         printk(KERN_INFO"zoran: %d zoran card(s) found\n", zoran_num);
3403
3404         if (zoran_num == 0)
3405                 return -ENXIO;
3406
3407         
3408         /* check the parameters we have been given, adjust if necessary */
3409
3410         if (v4l_nbufs < 0)
3411                 v4l_nbufs = 0;
3412         if (v4l_nbufs > VIDEO_MAX_FRAME)
3413                 v4l_nbufs = VIDEO_MAX_FRAME;
3414         /* The user specfies the in KB, we want them in byte (and page aligned) */
3415         v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
3416         if (v4l_bufsize < 32768)
3417                 v4l_bufsize = 32768;
3418         /* 2 MB is arbitrary but sufficient for the maximum possible images */
3419         if (v4l_bufsize > 2048 * 1024)
3420                 v4l_bufsize = 2048 * 1024;
3421
3422         printk(KERN_INFO "zoran: using %d V4L buffers of size %d KB\n", v4l_nbufs, v4l_bufsize >> 10);
3423
3424         /* Use parameter for vidmem or try to find a video card */
3425
3426         if (vidmem) {
3427                 printk(KERN_INFO "zoran: Using supplied video memory base address @ 0x%lx\n", vidmem);
3428         }
3429
3430         /* check if we have a Triton or Natome chipset */
3431
3432         handle_chipset();
3433
3434         /* take care of Natoma chipset and a revision 1 zr36057 */
3435
3436         for (i = 0; i < zoran_num; i++) {
3437                 if (natoma && zoran[i].revision <= 1) {
3438                         zoran[i].need_contiguous = 1;
3439                         printk(KERN_INFO "%s: ZR36057/Natome bug, max. buffer size is 128K\n", zoran[i].name);
3440                 } else {
3441                         zoran[i].need_contiguous = 0;
3442                 }
3443         }
3444
3445         /* initialize the Buzs */
3446
3447         /* We have to know which ones must be released if an error occurs */
3448         for (i = 0; i < zoran_num; i++)
3449                 zoran[i].initialized = 0;
3450
3451         for (i = 0; i < zoran_num; i++) {
3452                 if (zr36057_init(i) < 0) {
3453                         release_zoran();
3454                         return -EIO;
3455                 }
3456         }
3457
3458         return 0;
3459 }
3460
3461
3462
3463 #ifdef MODULE
3464
3465 void cleanup_module(void)
3466 {
3467         release_zoran();
3468 }
3469
3470 #endif