File: /usr/src/linux/drivers/media/video/zr36067.c

1     #define DEBUGLEVEL 0
2     #define MAX_KMALLOC_MEM (128*1024)
3     
4     /*
5        Miro/Pinnacle Systems Inc. DC10/DC10plus and
6        Linux Media Labs LML33 video capture boards driver
7        now with IOMega BUZ support!
8        
9        Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
10     
11        Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
12        
13        Based on
14        
15         Miro DC10 driver
16         Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
17        
18         Iomega Buz driver version 1.0
19         Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
20     
21         buz.0.0.3
22         Copyright (C) 1998 Dave Perks <dperks@ibm.net>
23     
24         bttv - Bt848 frame grabber driver
25         Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
26                                & Marcus Metzler (mocm@thp.uni-koeln.de)
27     
28         
29         This program is free software; you can redistribute it and/or modify
30         it under the terms of the GNU General Public License as published by
31         the Free Software Foundation; either version 2 of the License, or
32         (at your option) any later version.
33     
34         This program is distributed in the hope that it will be useful,
35         but WITHOUT ANY WARRANTY; without even the implied warranty of
36         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37         GNU General Public License for more details.
38     
39         You should have received a copy of the GNU General Public License
40         along with this program; if not, write to the Free Software
41         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42     */
43     
44     #include <linux/config.h>
45     #include <linux/version.h>
46     #include <linux/init.h>
47     #include <linux/module.h>
48     #include <linux/delay.h>
49     #include <linux/errno.h>
50     #include <linux/fs.h>
51     #include <linux/kernel.h>
52     #include <linux/major.h>
53     #include <linux/slab.h>
54     #include <linux/mm.h>
55     #include <linux/pci.h>
56     #include <linux/signal.h>
57     #include <asm/io.h>
58     #include <asm/pgtable.h>
59     #include <asm/page.h>
60     #include <linux/sched.h>
61     #include <asm/segment.h>
62     #include <linux/types.h>
63     #include <linux/wrapper.h>
64     
65     #include <linux/spinlock.h>
66     #include <linux/vmalloc.h>
67     #include <linux/i2c-old.h>
68     #define     MAP_NR(x)       virt_to_page(x)
69     #define     ZORAN_HARDWARE  VID_HARDWARE_ZR36067
70     
71     #include <linux/videodev.h>
72     
73     #include <asm/uaccess.h>
74     #include <linux/proc_fs.h>
75     
76     #include "zoran.h"
77     #include <linux/video_decoder.h>
78     #include <linux/video_encoder.h>
79     
80     // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
81     #if !defined(CONFIG_BIGPHYS_AREA)
82     //#undef CONFIG_BIGPHYS_AREA
83     #define BUZ_USE_HIMEM
84     #endif
85     
86     #if defined(CONFIG_BIGPHYS_AREA)
87     #   include <linux/bigphysarea.h>
88     #endif
89     
90     #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | ZR36057_ISR_GIRQ1 | /* ZR36057_ISR_CodRepIRQ | */  ZR36057_ISR_JPEGRepIRQ )	// SW
91     //#define GPIO_MASK 0xcd
92     //#define GPIO_MASK 0x8d
93     
94     /*
95     DC10:
96     GPIO0 = /RESET ZR 36060
97     GPIO1 = VIDEO BUS DIRECTION (0: CAPTURE, 1: DISPLAY)
98     GPIO2 = VIDEO BUS ENABLE    (0: ON, 1: OFF)
99     GPIO3 = /SLEEP ZR 36060
100     GPIO4 = ADC7175 (video out) FREQUENCY (0: LCC/SAA7110, 1: 27 MHz (quarz))
101     GPIO5 = ZORAN FREQUENCY  (0: LCC and LCC2 from SAA7110,
102                               1: 27 and 13.5 MHz (quarz))
103     GPIO6 = /FRAME ZR 36060
104     GPIO7 = /RESET ADV7175 (video out)
105     (I think they lost the SAA7110 reset.....)
106     
107     GIRQ0 signals that ZR36060's DATERR# line is asserted.
108     GIRQ1 signals a vertical sync of the video signal (VS SAA7110)
109     
110       SAA7110A:
111        mode 0 - Composite
112        mode 1 - 
113        mode 2 - 
114        mode 3 - 
115        mode 4 - 
116        mode 5 - internal Composite (from PCTV)
117        mode 6 - 
118        mode 7 - S-Video
119     
120     BUZ:
121     GPIO0 = 1, take board out of reset
122     GPIO1 = 1, take JPEG codec out of sleep mode
123     GPIO3 = 1, deassert FRAME# to 36060
124     
125     GIRQ0 signals a vertical sync of the video signal
126     GIRQ1 signals that ZR36060's DATERR# line is asserted.
127     
128     SAA7111A
129     
130        In their infinite wisdom, the Iomega engineers decided to
131        use the same input line for composite and S-Video Color,
132        although there are two entries not connected at all!
133        Through this ingenious strike, it is not possible to
134        keep two running video sources connected at the same time
135        to Composite and S-VHS input!
136     
137        mode 0 - N/C
138        mode 1 - S-Video Y
139        mode 2 - noise or something I don't know
140        mode 3 - Composite and S-Video C
141        mode 4 - N/C
142        mode 5 - S-Video (gain C independently selectable of gain Y)
143        mode 6 - N/C
144        mode 7 - S-Video (gain C adapted to gain Y)
145     */
146     
147     #define MAJOR_VERSION 0		/* driver major version */
148     #define MINOR_VERSION 7		/* driver minor version */
149     
150     #define ZORAN_NAME    "zr36067"	/* name of the device */
151     
152     #define BUZ_ERR       KERN_ERR     ZORAN_NAME
153     #define BUZ_DEBUG     KERN_INFO    ZORAN_NAME
154     #define BUZ_INFO      KERN_INFO    ZORAN_NAME
155     #define BUZ_WARNING   KERN_WARNING ZORAN_NAME
156     
157     #if(DEBUGLEVEL>0)
158     #define DEBUG1(x...)    x
159     #else
160     #define DEBUG1(x...)
161     #endif
162     
163     #if(DEBUGLEVEL>1)
164     #define DEBUG2(x...)    x
165     #else
166     #define DEBUG2(x...)
167     #endif
168     
169     #if(DEBUGLEVEL>2)
170     #define DEBUG3(x...)    x
171     #else
172     #define DEBUG3(x...)
173     #endif
174     
175     #if(DEBUGLEVEL>3)
176     #define DEBUG4(x...)    x
177     #else
178     #define DEBUG4(x...)
179     #endif
180     
181     /* The parameters for this driver */
182     
183     /*
184        The video mem address of the video card.
185        The driver has a little database for some videocards
186        to determine it from there. If your video card is not in there
187        you have either to give it to the driver as a parameter
188        or set in in a VIDIOCSFBUF ioctl
189      */
190     
191     static unsigned long vidmem = 0;	/* Video memory base address */
192     
193     /* Special purposes only: */
194     
195     static int triton = 0;		/* 0=no, 1=yes */
196     static int natoma = 0;		/* 0=no, 1=yes */
197     
198     /*
199        Number and size of grab buffers for Video 4 Linux
200        The vast majority of applications should not need more than 2,
201        the very popular BTTV driver actually does ONLY have 2.
202        Time sensitive applications might need more, the maximum
203        is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
204     
205        The size is set so that the maximum possible request
206        can be satisfied. Decrease  it, if bigphys_area alloc'd
207        memory is low. If you don't have the bigphys_area patch,
208        set it to 128 KB. Will you allow only to grab small
209        images with V4L, but that's better than nothing.
210     
211        v4l_bufsize has to be given in KB !
212     
213     */
214     
215     static int v4l_nbufs = 2;
216     static int v4l_bufsize = 128;	/* Everybody should be able to work with this setting */
217     
218     /*
219        Default input and video norm at startup of the driver.
220     */
221     
222     static int default_input = 0;	/* 0=Composite, 1=S-VHS */
223     static int default_norm = 0;	/* 0=PAL, 1=NTSC 2=SECAM */
224     static int lock_norm = 0;	/* 1=Don't change TV standard (norm) */
225     
226     static int pass_through = 0;	/* 1=Pass through TV signal when device is not used */
227     				/* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
228     
229     static int lml33dpath = 0;	/* 1 will use digital path in capture mode instead of analog.
230     				   It can be used for picture adjustments using tool like xawtv
231     				   while watching image on TV monitor connected to the output.
232     				   However, due to absence of 75 Ohm load on Bt819 input, there
233     				   will be some image imperfections */
234     static int video_nr = -1;
235     
236     MODULE_PARM(vidmem, "i");
237     MODULE_PARM(triton, "i");
238     MODULE_PARM(natoma, "i");
239     MODULE_PARM(v4l_nbufs, "i");
240     MODULE_PARM(v4l_bufsize, "i");
241     MODULE_PARM(default_input, "i");
242     MODULE_PARM(default_norm, "i");
243     MODULE_PARM(lock_norm, "i");
244     MODULE_PARM(pass_through, "i");
245     MODULE_PARM(lml33dpath, "i");
246     MODULE_PARM(video_nr, "i");
247     
248     /* Anybody who uses more than four? */
249     #define BUZ_MAX 4
250     
251     static int zoran_num;		/* number of Buzs in use */
252     static struct zoran zoran[BUZ_MAX];
253     
254     /* forward references */
255     
256     static void v4l_fbuffer_free(struct zoran *zr);
257     static void jpg_fbuffer_free(struct zoran *zr);
258     static void zoran_feed_stat_com(struct zoran *zr);
259     
260     /*
261      *   Allocate the V4L grab buffers
262      *
263      *   These have to be pysically contiguous.
264      *   If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
265      *   else we try to allocate them with bigphysarea_alloc_pages
266      *   if the bigphysarea patch is present in the kernel,
267      *   else we try to use high memory (if the user has bootet
268      *   Linux with the necessary memory left over).
269      */
270     
271     static int v4l_fbuffer_alloc(struct zoran *zr)
272     {
273     	int i, off;
274     	unsigned char *mem;
275     
276     	for (i = 0; i < v4l_nbufs; i++) {
277     		if (zr->v4l_gbuf[i].fbuffer)
278     			printk(KERN_WARNING
279     			       "%s: v4l_fbuffer_alloc: buffer %d allready allocated ???\n",
280     			       zr->name, i);
281     
282     		//udelay(20);
283     		if (v4l_bufsize <= MAX_KMALLOC_MEM) {
284     			/* Use kmalloc */
285     
286     			mem =
287     			    (unsigned char *) kmalloc(v4l_bufsize,
288     						      GFP_KERNEL);
289     			if (mem == 0) {
290     				printk(KERN_ERR
291     				       "%s: kmalloc for V4L bufs failed\n",
292     				       zr->name);
293     				v4l_fbuffer_free(zr);
294     				return -ENOBUFS;
295     			}
296     			zr->v4l_gbuf[i].fbuffer = mem;
297     			zr->v4l_gbuf[i].fbuffer_phys = virt_to_phys(mem);
298     			zr->v4l_gbuf[i].fbuffer_bus = virt_to_bus(mem);
299     			for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
300     				mem_map_reserve(MAP_NR(mem + off));
301     			DEBUG1(printk
302     			       (KERN_INFO
303     				"%s: V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
304     				zr->name, i, (unsigned long) mem,
305     				virt_to_bus(mem)));
306     		} else {
307     #if defined(CONFIG_BIGPHYS_AREA)
308     			/* Use bigphysarea_alloc_pages */
309     
310     			int n = (v4l_bufsize + PAGE_SIZE - 1) / PAGE_SIZE;
311     			mem =
312     			    (unsigned char *) bigphysarea_alloc_pages(n, 0,
313     								      GFP_KERNEL);
314     			if (mem == 0) {
315     				printk(KERN_ERR
316     				       "%s: bigphysarea_alloc_pages for V4L bufs failed\n",
317     				       zr->name);
318     				v4l_fbuffer_free(zr);
319     				return -ENOBUFS;
320     			}
321     			zr->v4l_gbuf[i].fbuffer = mem;
322     			zr->v4l_gbuf[i].fbuffer_phys = virt_to_phys(mem);
323     			zr->v4l_gbuf[i].fbuffer_bus = virt_to_bus(mem);
324     			DEBUG1(printk
325     			       (KERN_INFO
326     				"%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
327     				zr->name, i, (unsigned) mem,
328     				(unsigned) virt_to_bus(mem)));
329     
330     			/* Zero out the allocated memory */
331     			memset(zr->v4l_gbuf[i].fbuffer, 0, v4l_bufsize);
332     #else
333     			/* No bigphysarea present, usage of high memory disabled,
334     			   but user wants buffers of more than MAX_KMALLOC_MEM */
335     			printk(KERN_ERR
336     			       "%s: No bigphysarea_patch present, usage of high memory disabled,\n",
337     			       zr->name);
338     			printk(KERN_ERR
339     			       "%s: sorry, could not allocate V4L buffers of size %d KB.\n",
340     			       zr->name, v4l_bufsize >> 10);
341     			return -ENOBUFS;
342     #endif
343     		}
344     	}
345     
346     	return 0;
347     }
348     
349     /* free the V4L grab buffers */
350     
351     static void v4l_fbuffer_free(struct zoran *zr)
352     {
353     	int i, off;
354     	unsigned char *mem;
355     
356     	for (i = 0; i < v4l_nbufs; i++) {
357     		if (!zr->v4l_gbuf[i].fbuffer)
358     			continue;
359     
360     		if (v4l_bufsize <= MAX_KMALLOC_MEM) {
361     			mem = zr->v4l_gbuf[i].fbuffer;
362     			for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
363     				mem_map_unreserve(MAP_NR(mem + off));
364     			kfree((void *) zr->v4l_gbuf[i].fbuffer);
365     		}
366     #if defined(CONFIG_BIGPHYS_AREA)
367     		else
368     			bigphysarea_free_pages((void *) zr->v4l_gbuf[i].
369     					       fbuffer);
370     #endif
371     		zr->v4l_gbuf[i].fbuffer = NULL;
372     	}
373     }
374     
375     /*
376      *   Allocate the MJPEG grab buffers.
377      *
378      *   If the requested buffer size is smaller than MAX_KMALLOC_MEM,
379      *   kmalloc is used to request a physically contiguous area,
380      *   else we allocate the memory in framgents with get_free_page.
381      *
382      *   If a Natoma chipset is present and this is a revision 1 zr36057,
383      *   each MJPEG buffer needs to be physically contiguous.
384      *   (RJ: This statement is from Dave Perks' original driver,
385      *   I could never check it because I have a zr36067)
386      *   The driver cares about this because it reduces the buffer
387      *   size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
388      *
389      *   RJ: The contents grab buffers needs never be accessed in the driver.
390      *       Therefore there is no need to allocate them with vmalloc in order
391      *       to get a contiguous virtual memory space.
392      *       I don't understand why many other drivers first allocate them with
393      *       vmalloc (which uses internally also get_free_page, but delivers you
394      *       virtual addresses) and then again have to make a lot of efforts
395      *       to get the physical address.
396      *
397      */
398     
399     static int jpg_fbuffer_alloc(struct zoran *zr)
400     {
401     	int i, j, off;		//alloc_contig;
402     	unsigned long mem;
403     
404     	/* Decide if we should alloc contiguous or fragmented memory */
405     	/* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
406     
407     	//alloc_contig = (zr->jpg_bufsize <= MAX_KMALLOC_MEM);
408     
409     	for (i = 0; i < zr->jpg_nbufs; i++) {
410     		if (zr->jpg_gbuf[i].frag_tab)
411     			printk(KERN_WARNING
412     			       "%s: jpg_fbuffer_alloc: buffer %d allready allocated ???\n",
413     			       zr->name, i);
414     
415     		/* Allocate fragment table for this buffer */
416     
417     		mem = get_free_page(GFP_KERNEL);
418     		if (mem == 0) {
419     			printk(KERN_ERR
420     			       "%s: jpg_fbuffer_alloc: get_free_page (frag_tab) failed for buffer %d\n",
421     			       zr->name, i);
422     			jpg_fbuffer_free(zr);
423     			return -ENOBUFS;
424     		}
425     		memset((void *) mem, 0, PAGE_SIZE);
426     		zr->jpg_gbuf[i].frag_tab = (u32 *) mem;
427     		zr->jpg_gbuf[i].frag_tab_bus = virt_to_bus((void *) mem);
428     
429     		//if (alloc_contig) {
430     		if (zr->need_contiguous) {
431     			mem = (unsigned long) kmalloc(zr->jpg_bufsize, GFP_KERNEL);
432     			if (mem == 0) {
433     				printk(KERN_ERR "%s: jpg_fbuffer_alloc: kmalloc failed for buffer %d\n",
434     				       zr->name, i);
435     				jpg_fbuffer_free(zr);
436     				return -ENOBUFS;
437     			}
438     			zr->jpg_gbuf[i].frag_tab[0] = virt_to_bus((void *) mem);
439     			zr->jpg_gbuf[i].frag_tab[1] =
440     			    ((zr->jpg_bufsize / 4) << 1) | 1;
441     			for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
442     				mem_map_reserve(MAP_NR(mem + off));
443     		} else {
444     			/* jpg_bufsize is allreay page aligned */
445     			for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) 
446     			{
447     				mem = get_free_page(GFP_KERNEL);
448     				if (mem == 0) {
449     					printk(KERN_ERR
450     					       "%s: jpg_fbuffer_alloc: get_free_page failed for buffer %d\n",
451     					       zr->name, i);
452     					jpg_fbuffer_free(zr);
453     					return -ENOBUFS;
454     				}
455     
456     				zr->jpg_gbuf[i].frag_tab[2 * j] =
457     				    virt_to_bus((void *) mem);
458     				zr->jpg_gbuf[i].frag_tab[2 * j + 1] =
459     				    (PAGE_SIZE / 4) << 1;
460     				mem_map_reserve(MAP_NR(mem));
461     			}
462     
463     			zr->jpg_gbuf[i].frag_tab[2 * j - 1] |= 1;
464     		}
465     	}
466     
467     	DEBUG1(printk
468     	       ("%s: jpg_fbuffer_alloc: %ld KB allocated\n", zr->name,
469     		(zr->jpg_nbufs * zr->jpg_bufsize) >> 10));
470     	zr->jpg_buffers_allocated = 1;
471     	return 0;
472     }
473     
474     /* free the MJPEG grab buffers */
475     static void jpg_fbuffer_free(struct zoran *zr)
476     {
477     	int i, j, off;		// alloc_contig;
478     	unsigned char *mem;
479     
480     	/* Decide if we should alloc contiguous or fragmented memory */
481     	/* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
482     
483     	//alloc_contig = (zr->jpg_bufsize <= MAX_KMALLOC_MEM);
484     
485     	for (i = 0; i < zr->jpg_nbufs; i++) {
486     		if (!zr->jpg_gbuf[i].frag_tab)
487     			continue;
488     
489     		//if (alloc_contig) {
490     		if (zr->need_contiguous) {
491     			if (zr->jpg_gbuf[i].frag_tab[0]) {
492     				mem =
493     				    (unsigned char *) bus_to_virt(zr->
494     								  jpg_gbuf
495     								  [i].
496     								  frag_tab
497     								  [0]);
498     				for (off = 0; off < zr->jpg_bufsize;
499     				     off += PAGE_SIZE)
500     					mem_map_unreserve(MAP_NR
501     							  (mem + off));
502     				kfree((void *) mem);
503     				zr->jpg_gbuf[i].frag_tab[0] = 0;
504     				zr->jpg_gbuf[i].frag_tab[1] = 0;
505     			}
506     		} else {
507     			for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
508     				if (!zr->jpg_gbuf[i].frag_tab[2 * j])
509     					break;
510     				mem_map_unreserve(MAP_NR
511     						  (bus_to_virt
512     						   (zr->jpg_gbuf[i].
513     						    frag_tab[2 * j])));
514     				free_page((unsigned long)
515     					  bus_to_virt(zr->jpg_gbuf[i].
516     						      frag_tab[2 * j]));
517     				zr->jpg_gbuf[i].frag_tab[2 * j] = 0;
518     				zr->jpg_gbuf[i].frag_tab[2 * j + 1] = 0;
519     			}
520     		}
521     
522     		free_page((unsigned long) zr->jpg_gbuf[i].frag_tab);
523     		zr->jpg_gbuf[i].frag_tab = NULL;
524     	}
525     	zr->jpg_buffers_allocated = 0;
526     }
527     
528     
529     /* ----------------------------------------------------------------------- */
530     
531     /* I2C functions                                                           */
532     
533     #define I2C_DELAY   10
534     
535     
536     /* software I2C functions */
537     
538     static void i2c_setlines(struct i2c_bus *bus, int ctrl, int data)
539     {
540     	struct zoran *zr = (struct zoran *) bus->data;
541     	btwrite((data << 1) | ctrl, ZR36057_I2CBR);
542     	udelay(I2C_DELAY);
543     }
544     
545     static int i2c_getdataline(struct i2c_bus *bus)
546     {
547     	struct zoran *zr = (struct zoran *) bus->data;
548     	return (btread(ZR36057_I2CBR) >> 1) & 1;
549     }
550     
551     static void attach_inform(struct i2c_bus *bus, int id)
552     {
553     	int i;
554     	struct zoran *zr = (struct zoran *) bus->data;
555     
556     	DEBUG1(printk(KERN_DEBUG "%s: i2c attach %02x\n", zr->name, id));
557     	for (i = 0; i < bus->devcount; i++) {
558     		if (strcmp(bus->devices[i]->name, "saa7110") == 0) {
559     			if (zr->revision < 2) {
560     				zr->card = DC10;
561     				sprintf(zr->name, "DC10[%u]", zr->id);
562     			} else {
563     				zr->card = DC10plus;
564     				sprintf(zr->name, "DC10plus[%u]", zr->id);
565     			}
566     			break;
567     		}
568     		if (strcmp(bus->devices[i]->name, "bt819") == 0) {
569     			zr->card = LML33;
570     			sprintf(zr->name, "LML33[%u]", zr->id);
571     			break;
572     		}
573     		if (strcmp(bus->devices[i]->name, "saa7111") == 0) {
574     			zr->card = BUZ;
575     			sprintf(zr->name, "Buz[%u]", zr->id);
576     			break;
577     		}
578     	}
579     }
580     
581     static void detach_inform(struct i2c_bus *bus, int id)
582     {
583     	DEBUG1(struct zoran *zr = (struct zoran *) bus->data);
584     	DEBUG1(printk(KERN_DEBUG "%s: i2c detach %02x\n", zr->name, id));
585     }
586     
587     static struct i2c_bus zoran_i2c_bus_template = {
588     	"zr36057",
589     	I2C_BUSID_BT848,
590     	NULL,
591     
592     	SPIN_LOCK_UNLOCKED,
593     
594     	attach_inform,
595     	detach_inform,
596     
597     	i2c_setlines,
598     	i2c_getdataline,
599     	NULL,
600     	NULL,
601     };
602     
603     /*
604      *   Set the registers for the size we have specified. Don't bother
605      *   trying to understand this without the ZR36057 manual in front of
606      *   you [AC].
607      *
608      *   PS: The manual is free for download in .pdf format from
609      *   www.zoran.com - nicely done those folks.
610      */
611     
612     static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
613     static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
614     
615     static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
616     static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
617     
618     static struct tvnorm *dc10norms[] = {
619     	&f50sqpixel,		/* PAL-BDGHI */
620     	&f60sqpixel,		/* NTSC */
621     	&f50sqpixel,		/* SECAM */
622     };
623     
624     static struct tvnorm *lml33norms[] = {
625     	&f50ccir601,		/* PAL-BDGHI */
626     	&f60ccir601,		/* NTSC      */
627     	NULL,			/* SECAM (not supported in LML33) */
628     };
629     
630     static struct tvnorm *buznorms[] = {
631     	&f50ccir601,		/* PAL-BDGHI */
632     	&f60ccir601,		/* NTSC      */
633     	NULL,			/* SECAM     */
634     };
635     
636     static struct tvnorm *unsupported[] = {
637     	NULL,			/* PAL-BDGHI */
638     	NULL,			/* NTSC      */
639     	NULL,			/* SECAM     */
640     };
641     
642     static struct tvnorm **cardnorms[] = {
643     	unsupported,		/* UNKNOWN  */
644     	dc10norms,		/* DC10     */
645     	dc10norms,		/* DC10plus */
646     	lml33norms,		/* LML33    */
647     	buznorms,		/* Buz      */
648     };
649     
650     static u32 cardvsync[] = {
651     	0,			/* UNKNOWN  */
652     	ZR36057_ISR_GIRQ1,	/* DC10     */
653     	ZR36057_ISR_GIRQ1,	/* DC10plus */
654     	ZR36057_ISR_GIRQ0,	/* LML33    */
655     	ZR36057_ISR_GIRQ0,	/* Buz      */
656     };
657     
658     static u32 cardjpegint[] = {
659     	0,			/* UNKNOWN  */
660     	ZR36057_ISR_GIRQ0,	/* DC10     */
661     	ZR36057_ISR_GIRQ0,	/* DC10plus */
662     	ZR36057_ISR_GIRQ1,	/* LML33    */
663     	ZR36057_ISR_GIRQ1,	/* Buz      */
664     };
665     
666     static int format2bpp(int format)
667     {
668     	int bpp;
669     
670     	/* Determine the number of bytes per pixel for the video format requested */
671     
672     	switch (format) {
673     
674     	case VIDEO_PALETTE_YUV422:
675     		bpp = 2;
676     		break;
677     
678     	case VIDEO_PALETTE_RGB555:
679     		bpp = 2;
680     		break;
681     
682     	case VIDEO_PALETTE_RGB565:
683     		bpp = 2;
684     		break;
685     
686     	case VIDEO_PALETTE_RGB24:
687     		bpp = 3;
688     		break;
689     
690     	case VIDEO_PALETTE_RGB32:
691     		bpp = 4;
692     		break;
693     
694     	default:
695     		bpp = 0;
696     	}
697     
698     	return bpp;
699     }
700     
701     static void zr36057_adjust_vfe(struct zoran *zr,
702     			       enum zoran_codec_mode mode)
703     {
704     	u32 reg;
705     	switch (mode) {
706     	case BUZ_MODE_MOTION_DECOMPRESS:
707     		btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
708     		reg = btread(ZR36057_VFEHCR);
709     		if (reg & (1 << 10)) {
710     			reg += ((1 << 10) | 1);
711     		}
712     		btwrite(reg, ZR36057_VFEHCR);
713     		break;
714     	case BUZ_MODE_MOTION_COMPRESS:
715     	case BUZ_MODE_IDLE:
716     	default:
717     		if (zr->params.norm == VIDEO_MODE_NTSC)
718     			btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
719     		else
720     			btor(ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
721     		reg = btread(ZR36057_VFEHCR);
722     		if (!(reg & (1 << 10))) {
723     			reg -= ((1 << 10) | 1);
724     		}
725     		btwrite(reg, ZR36057_VFEHCR);
726     		break;
727     	}
728     }
729     
730     /*
731      * set geometry
732      */
733     static void zr36057_set_vfe(struct zoran *zr, int video_width,
734     			    int video_height, unsigned int video_format)
735     {
736     	struct tvnorm *tvn;
737     	unsigned HStart, HEnd, VStart, VEnd;
738     	unsigned DispMode;
739     	unsigned VidWinWid, VidWinHt;
740     	unsigned hcrop1, hcrop2, vcrop1, vcrop2;
741     	unsigned Wa, We, Ha, He;
742     	unsigned X, Y, HorDcm, VerDcm;
743     	u32 reg;
744     	unsigned mask_line_size;
745     
746     	tvn = zr->timing;
747     
748     	Wa = tvn->Wa;
749     	Ha = tvn->Ha;
750     
751     	DEBUG1(printk (BUZ_INFO ": width = %d, height = %d\n", video_width, video_height));
752     
753     	if (zr->params.norm != VIDEO_MODE_PAL
754     	    && zr->params.norm != VIDEO_MODE_NTSC
755     	    && zr->params.norm != VIDEO_MODE_SECAM) {
756     		printk(KERN_ERR "%s: set_vfe: video_norm = %d not valid\n",
757     		       zr->name, zr->params.norm);
758     		return;
759     	}
760     	if (video_width < BUZ_MIN_WIDTH || video_height < BUZ_MIN_HEIGHT
761     	    || video_width > Wa || video_height > Ha) {
762     		printk(KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n",
763     		       zr->name, video_width, video_height);
764     		return;
765     	}
766     
767     	/* if window has more than half of active height,
768     	   switch on interlacing - we want the full information */
769     
770     	zr->video_interlace = (video_height > Ha / 2);
771     
772     	/**** zr36057 ****/
773     
774     	/* horizontal */
775     	VidWinWid = video_width;
776     	X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
777     	We = (VidWinWid * 64) / X;
778     	HorDcm = 64 - X;
779     	hcrop1 = 2 * ((tvn->Wa - We) / 4);
780     	hcrop2 = tvn->Wa - We - hcrop1;
781     	HStart = tvn->HStart | 1;
782     	if (zr->card == LML33)
783     		HStart += 62;
784     	if (zr->card == BUZ) {	//HStart += 67;
785     		HStart += 44;
786     	}
787     	HEnd = HStart + tvn->Wa - 1;
788     	HStart += hcrop1;
789     	HEnd -= hcrop2;
790     	reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
791     	    | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
792     	if (zr->card != BUZ)
793     		reg |= ZR36057_VFEHCR_HSPol;
794     	btwrite(reg, ZR36057_VFEHCR);
795     
796     	/* Vertical */
797     	DispMode = !zr->video_interlace;
798     	VidWinHt = DispMode ? video_height : video_height / 2;
799     	Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
800     	He = (VidWinHt * 64) / Y;
801     	VerDcm = 64 - Y;
802     	vcrop1 = (tvn->Ha / 2 - He) / 2;
803     	vcrop2 = tvn->Ha / 2 - He - vcrop1;
804     	VStart = tvn->VStart;
805     	VEnd = VStart + tvn->Ha / 2 - 1;
806     	VStart += vcrop1;
807     	VEnd -= vcrop2;
808     	reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
809     	    | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
810     	reg |= ZR36057_VFEVCR_VSPol;
811     	btwrite(reg, ZR36057_VFEVCR);
812     
813     	/* scaler and pixel format */
814     	reg = 0;
815     	reg |= (HorDcm << ZR36057_VFESPFR_HorDcm);
816     	reg |= (VerDcm << ZR36057_VFESPFR_VerDcm);
817     	reg |= (DispMode << ZR36057_VFESPFR_DispMode);
818     	reg |= ZR36057_VFESPFR_LittleEndian;
819     	/* RJ: I don't know, why the following has to be the opposite
820     	   of the corresponding ZR36060 setting, but only this way
821     	   we get the correct colors when uncompressing to the screen  */
822     	//reg |= ZR36057_VFESPFR_VCLKPol; /**/
823     	/* RJ: Don't know if that is needed for NTSC also */
824     	if (zr->params.norm != VIDEO_MODE_NTSC)
825     		reg |= ZR36057_VFESPFR_ExtFl;	// NEEDED!!!!!!! Wolfgang
826     	reg |= ZR36057_VFESPFR_TopField;
827     	switch (video_format) {
828     
829     	case VIDEO_PALETTE_YUV422:
830     		reg |= ZR36057_VFESPFR_YUV422;
831     		break;
832     
833     	case VIDEO_PALETTE_RGB555:
834     		reg |= ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ErrDif;
835     		break;
836     
837     	case VIDEO_PALETTE_RGB565:
838     		reg |= ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ErrDif;
839     		break;
840     
841     	case VIDEO_PALETTE_RGB24:
842     		reg |= ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_Pack24;
843     		break;
844     
845     	case VIDEO_PALETTE_RGB32:
846     		reg |= ZR36057_VFESPFR_RGB888;
847     		break;
848     
849     	default:
850     		printk(KERN_INFO "%s: Unknown color_fmt=%x\n", zr->name,
851     		       video_format);
852     		return;
853     
854     	}
855     	if (HorDcm >= 48) {
856     		reg |= 3 << ZR36057_VFESPFR_HFilter;	/* 5 tap filter */
857     	} else if (HorDcm >= 32) {
858     		reg |= 2 << ZR36057_VFESPFR_HFilter;	/* 4 tap filter */
859     	} else if (HorDcm >= 16) {
860     		reg |= 1 << ZR36057_VFESPFR_HFilter;	/* 3 tap filter */
861     	}
862     	btwrite(reg, ZR36057_VFESPFR);
863     
864     	/* display configuration */
865     
866     	reg = (16 << ZR36057_VDCR_MinPix)
867     	    | (VidWinHt << ZR36057_VDCR_VidWinHt)
868     	    | (VidWinWid << ZR36057_VDCR_VidWinWid);
869     	if (triton || zr->revision <= 1)
870     		reg &= ~ZR36057_VDCR_Triton;
871     	else
872     		reg |= ZR36057_VDCR_Triton;
873     	btwrite(reg, ZR36057_VDCR);
874     
875     	/* Write overlay clipping mask data, but don't enable overlay clipping */
876     	/* RJ: since this makes only sense on the screen, we use 
877     	   zr->window.width instead of video_width */
878     
879     	mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
880     	reg = virt_to_bus(zr->overlay_mask);
881     	btwrite(reg, ZR36057_MMTR);
882     	reg = virt_to_bus(zr->overlay_mask + mask_line_size);
883     	btwrite(reg, ZR36057_MMBR);
884     	reg = mask_line_size - (zr->window.width + 31) / 32;
885     	if (DispMode == 0)
886     		reg += mask_line_size;
887     	reg <<= ZR36057_OCR_MaskStride;
888     	btwrite(reg, ZR36057_OCR);
889     
890     	zr36057_adjust_vfe(zr, zr->codec_mode);
891     
892     }
893     
894     /*
895      * Switch overlay on or off
896      */
897     
898     static void zr36057_overlay(struct zoran *zr, int on)
899     {
900     	int fmt, bpp;
901     	u32 reg;
902     
903     	if (on) {
904     		/* do the necessary settings ... */
905     
906     		btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);	/* switch it off first */
907     
908     		switch (zr->buffer.depth) {
909     		case 15:
910     			fmt = VIDEO_PALETTE_RGB555;
911     			bpp = 2;
912     			break;
913     		case 16:
914     			fmt = VIDEO_PALETTE_RGB565;
915     			bpp = 2;
916     			break;
917     		case 24:
918     			fmt = VIDEO_PALETTE_RGB24;
919     			bpp = 3;
920     			break;
921     		case 32:
922     			fmt = VIDEO_PALETTE_RGB32;
923     			bpp = 4;
924     			break;
925     		default:
926     			fmt = 0;
927     			bpp = 0;
928     		}
929     
930     		zr36057_set_vfe(zr, zr->window.width, zr->window.height,
931     				fmt);
932     
933     		/* Start and length of each line MUST be 4-byte aligned.
934     		   This should be allready checked before the call to this routine.
935     		   All error messages are internal driver checking only! */
936     
937     		/* video display top and bottom registers */
938     
939     		reg =
940     		    (u32) zr->buffer.base + zr->window.x * bpp +
941     		    zr->window.y * zr->buffer.bytesperline;
942     		btwrite(reg, ZR36057_VDTR);
943     		if (reg & 3)
944     			printk(KERN_ERR
945     			       "%s: zr36057_overlay: video_address not aligned\n",
946     			       zr->name);
947     		if (zr->video_interlace)
948     			reg += zr->buffer.bytesperline;
949     		btwrite(reg, ZR36057_VDBR);
950     
951     		/* video stride, status, and frame grab register */
952     
953     		reg = zr->buffer.bytesperline - zr->window.width * bpp;
954     		if (zr->video_interlace)
955     			reg += zr->buffer.bytesperline;
956     		if (reg & 3)
957     			printk(KERN_ERR
958     			       "%s: zr36057_overlay: video_stride not aligned\n",
959     			       zr->name);
960     		reg = (reg << ZR36057_VSSFGR_DispStride);
961     		reg |= ZR36057_VSSFGR_VidOvf;	/* clear overflow status */
962     		btwrite(reg, ZR36057_VSSFGR);
963     
964     		/* Set overlay clipping */
965     
966     		if (zr->window.clipcount)
967     			btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
968     
969     		/* ... and switch it on */
970     
971     		btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
972     	} else {
973     		/* Switch it off */
974     
975     		btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
976     	}
977     }
978     
979     /*
980      * The overlay mask has one bit for each pixel on a scan line,
981      *  and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
982      */
983     static void write_overlay_mask(struct zoran *zr, struct video_clip *vp,
984     			       int count)
985     {
986     	unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
987     	u32 *mask;
988     	int x, y, width, height;
989     	unsigned i, j, k;
990     	u32 reg;
991     
992     	/* fill mask with one bits */
993     	memset(zr->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
994     	reg = 0;
995     
996     	for (i = 0; i < count; ++i) {
997     		/* pick up local copy of clip */
998     		x = vp[i].x;
999     		y = vp[i].y;
1000     		width = vp[i].width;
1001     		height = vp[i].height;
1002     
1003     		/* trim clips that extend beyond the window */
1004     		if (x < 0) {
1005     			width += x;
1006     			x = 0;
1007     		}
1008     		if (y < 0) {
1009     			height += y;
1010     			y = 0;
1011     		}
1012     		if (x + width > zr->window.width) {
1013     			width = zr->window.width - x;
1014     		}
1015     		if (y + height > zr->window.height) {
1016     			height = zr->window.height - y;
1017     		}
1018     
1019     		/* ignore degenerate clips */
1020     		if (height <= 0) {
1021     			continue;
1022     		}
1023     		if (width <= 0) {
1024     			continue;
1025     		}
1026     
1027     		/* apply clip for each scan line */
1028     		for (j = 0; j < height; ++j) {
1029     			/* reset bit for each pixel */
1030     			/* this can be optimized later if need be */
1031     			mask = zr->overlay_mask + (y + j) * mask_line_size;
1032     			for (k = 0; k < width; ++k) {
1033     				mask[(x + k) / 32] &=
1034     				    ~((u32) 1 << (x + k) % 32);
1035     			}
1036     		}
1037     	}
1038     }
1039     
1040     /* Enable/Disable uncompressed memory grabbing of the 36057 */
1041     
1042     static void zr36057_set_memgrab(struct zoran *zr, int mode)
1043     {
1044     	if (mode) {
1045     		if (btread(ZR36057_VSSFGR) &
1046     		    (ZR36057_VSSFGR_SnapShot | ZR36057_VSSFGR_FrameGrab))
1047     			printk(KERN_WARNING
1048     			       "%s: zr36057_set_memgrab_on with SnapShot or FrameGrab on ???\n",
1049     			       zr->name);
1050     
1051     		/* switch on VSync interrupts */
1052     
1053     		btwrite(IRQ_MASK, ZR36057_ISR);	// Clear Interrupts
1054     		btor(cardvsync[zr->card], ZR36057_ICR);	// SW
1055     
1056     		/* enable SnapShot */
1057     
1058     		btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
1059     
1060     		/* Set zr36057 video front end  and enable video */
1061     
1062     		zr36057_set_vfe(zr, zr->gwidth, zr->gheight, zr->gformat);
1063     
1064     		zr->v4l_memgrab_active = 1;
1065     	} else {
1066     		zr->v4l_memgrab_active = 0;
1067     
1068     		/* switch off VSync interrupts */
1069     
1070     		//btand(~ZR36057_ICR_GIRQ1, ZR36057_ICR); // SW
1071     
1072     		/* reenable grabbing to screen if it was running */
1073     
1074     		if (zr->v4l_overlay_active) {
1075     			zr36057_overlay(zr, 1);
1076     		} else {
1077     			btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
1078     			btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
1079     		}
1080     	}
1081     }
1082     
1083     static int wait_grab_pending(struct zoran *zr)
1084     {
1085     	unsigned long flags;
1086     
1087     	/* wait until all pending grabs are finished */
1088     
1089     	if (!zr->v4l_memgrab_active)
1090     		return 0;
1091     
1092     	while (zr->v4l_pend_tail != zr->v4l_pend_head) {
1093     		interruptible_sleep_on(&zr->v4l_capq);
1094     		if (signal_pending(current))
1095     			return -ERESTARTSYS;
1096     	}
1097     
1098     	spin_lock_irqsave(&zr->lock, flags);
1099     	zr36057_set_memgrab(zr, 0);
1100     	spin_unlock_irqrestore(&zr->lock, flags);
1101     
1102     	return 0;
1103     }
1104     
1105     /*
1106      *   V4L Buffer grabbing
1107      */
1108     
1109     static int v4l_grab(struct zoran *zr, struct video_mmap *mp)
1110     {
1111     	unsigned long flags;
1112     	int res, bpp;
1113     
1114     	/*
1115     	 * There is a long list of limitations to what is allowed to be grabbed
1116     	 * We don't output error messages here, since some programs (e.g. xawtv)
1117     	 * just try several settings to find out what is valid or not.
1118     	 */
1119     
1120     	/* No grabbing outside the buffer range! */
1121     
1122     	if (mp->frame >= v4l_nbufs || mp->frame < 0) {
1123     		DEBUG2(printk
1124     		       (KERN_ERR "%s: Can not grab frame %d\n", zr->name,
1125     			mp->frame));
1126     		return -EINVAL;
1127     	}
1128     
1129     	/* Check size and format of the grab wanted */
1130     
1131     	if (mp->height < BUZ_MIN_HEIGHT || mp->width < BUZ_MIN_WIDTH
1132     	    || mp->height > BUZ_MAX_HEIGHT || mp->width > BUZ_MAX_WIDTH) {
1133     		DEBUG2(printk
1134     		       (KERN_ERR "%s: Wrong frame size.\n", zr->name));
1135     		return -EINVAL;
1136     	}
1137     
1138     	bpp = format2bpp(mp->format);
1139     	if (bpp == 0) {
1140     		DEBUG2(printk
1141     		       (KERN_ERR "%s: Wrong bytes-per-pixel format\n",
1142     			zr->name));
1143     		return -EINVAL;
1144     	}
1145     
1146     	/* Check against available buffer size */
1147     
1148     	if (mp->height * mp->width * bpp > v4l_bufsize) {
1149     		DEBUG2(printk
1150     		       (KERN_ERR "%s: Video buffer size is too small\n",
1151     			zr->name));
1152     		return -EINVAL;
1153     	}
1154     
1155     	/* The video front end needs 4-byte alinged line sizes */
1156     
1157     	if ((bpp == 2 && (mp->width & 1)) || (bpp == 3 && (mp->width & 3))) {
1158     		DEBUG2(printk
1159     		       (KERN_ERR "%s: Wrong frame alingment\n", zr->name));
1160     		return -EINVAL;
1161     	}
1162     
1163     	/*
1164     	 * To minimize the time spent in the IRQ routine, we avoid setting up
1165     	 * the video front end there.
1166     	 * If this grab has different parameters from a running streaming capture
1167     	 * we stop the streaming capture and start it over again.
1168     	 */
1169     
1170     	if (zr->v4l_memgrab_active
1171     	    && (zr->gwidth != mp->width || zr->gheight != mp->height
1172     		|| zr->gformat != mp->format)) {
1173     		res = wait_grab_pending(zr);
1174     		if (res)
1175     			return res;
1176     	}
1177     	zr->gwidth = mp->width;
1178     	zr->gheight = mp->height;
1179     	zr->gformat = mp->format;
1180     	zr->gbpl = bpp * zr->gwidth;
1181     
1182     
1183     	spin_lock_irqsave(&zr->lock, flags);
1184     
1185     	/* make sure a grab isn't going on currently with this buffer */
1186     
1187     	switch (zr->v4l_gbuf[mp->frame].state) {
1188     
1189     	default:
1190     	case BUZ_STATE_PEND:
1191     		res = -EBUSY;	/* what are you doing? */
1192     		break;
1193     
1194     	case BUZ_STATE_USER:
1195     	case BUZ_STATE_DONE:
1196     		/* since there is at least one unused buffer there's room for at least one more pend[] entry */
1197     		zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] =
1198     		    mp->frame;
1199     		zr->v4l_gbuf[mp->frame].state = BUZ_STATE_PEND;
1200     		res = 0;
1201     		break;
1202     
1203     	}
1204     
1205     	/* put the 36057 into frame grabbing mode */
1206     
1207     	if (!res && !zr->v4l_memgrab_active)
1208     		zr36057_set_memgrab(zr, 1);
1209     
1210     	spin_unlock_irqrestore(&zr->lock, flags);
1211     	//DEBUG2(printk(KERN_INFO "%s: Frame grab 3...\n", zr->name));
1212     
1213     	return res;
1214     }
1215     
1216     /*
1217      * Sync on a V4L buffer
1218      */
1219     
1220     static int v4l_sync(struct zoran *zr, int frame)
1221     {
1222     	unsigned long flags;
1223     
1224     	/* check passed-in frame number */
1225     
1226     	if (frame >= v4l_nbufs || frame < 0) {
1227     		DEBUG1(printk(KERN_ERR "%s: v4l_sync: frame %d is invalid\n",
1228     		       zr->name, frame));
1229     		return -EINVAL;
1230     	}
1231     
1232     	/* Check if is buffer was queued at all */
1233     
1234     	if (zr->v4l_gbuf[frame].state == BUZ_STATE_USER) {
1235     		DEBUG1(printk(KERN_ERR
1236     		       "%s: v4l_sync: Attempt to sync on a buffer which was not queued?\n",
1237     		       zr->name));
1238     		return -EPROTO;
1239     	}
1240     
1241     	/* wait on this buffer to get ready */
1242     
1243     	while (zr->v4l_gbuf[frame].state == BUZ_STATE_PEND) {
1244     		interruptible_sleep_on(&zr->v4l_capq);
1245     		if (signal_pending(current))
1246     			return -ERESTARTSYS;
1247     	}
1248     
1249     	/* buffer should now be in BUZ_STATE_DONE */
1250     
1251     	if (zr->v4l_gbuf[frame].state != BUZ_STATE_DONE)
1252     		printk(KERN_ERR "%s: v4l_sync - internal error\n",
1253     		       zr->name);
1254     
1255     	/* Check if streaming capture has finished */
1256     
1257     	spin_lock_irqsave(&zr->lock, flags);
1258     
1259     	if (zr->v4l_pend_tail == zr->v4l_pend_head)
1260     		zr36057_set_memgrab(zr, 0);
1261     
1262     	spin_unlock_irqrestore(&zr->lock, flags);
1263     
1264     	return 0;
1265     }
1266     
1267     /*****************************************************************************
1268      *                                                                           *
1269      *  Set up the Buz-specific MJPEG part                                       *
1270      *                                                                           *
1271      *****************************************************************************/
1272     
1273     /*
1274     Wait til post office is no longer busy */
1275     static int post_office_wait(struct zoran *zr)
1276     {
1277     	u32 por;
1278     
1279     //      while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
1280     	while ((por = btread(ZR36057_POR)) & ZR36057_POR_POPen) {
1281     		/* wait for something to happen */
1282     	}
1283     	if ((por & ZR36057_POR_POTime) && zr->card != LML33
1284     	    && zr->card != BUZ) {
1285     		/* In LML33/BUZ \GWS line is not connected, so it has always timeout set */
1286     		printk(KERN_WARNING "%s: pop timeout %08x\n", zr->name, por);
1287     		return -1;
1288     	}
1289     	return 0;
1290     }
1291     
1292     static int post_office_write(struct zoran *zr, unsigned guest,
1293     			     unsigned reg, unsigned value)
1294     {
1295     	u32 por;
1296     
1297     	por =
1298     	    ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) |
1299     	    ((reg & 7) << 16) | (value & 0xFF);
1300     	btwrite(por, ZR36057_POR);
1301     	return post_office_wait(zr);
1302     }
1303     
1304     static int post_office_read(struct zoran *zr, unsigned guest, unsigned reg)
1305     {
1306     	u32 por;
1307     
1308     	por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
1309     	btwrite(por, ZR36057_POR);
1310     	if (post_office_wait(zr) < 0) {
1311     		return -1;
1312     	}
1313     	return btread(ZR36057_POR) & 0xFF;
1314     }
1315     
1316     static int zr36060_write_8(struct zoran *zr, unsigned reg, unsigned val)
1317     {
1318     	if (post_office_wait(zr)
1319     	    || post_office_write(zr, 0, 1, reg >> 8)
1320     	    || post_office_write(zr, 0, 2, reg)) {
1321     		return -1;
1322     	}
1323     	return post_office_write(zr, 0, 3, val);
1324     }
1325     
1326     static int zr36060_write_16(struct zoran *zr, unsigned reg, unsigned val)
1327     {
1328     	if (zr36060_write_8(zr, reg + 0, val >> 8)) {
1329     		return -1;
1330     	}
1331     	return zr36060_write_8(zr, reg + 1, val >> 0);
1332     }
1333     
1334     static int zr36060_write_24(struct zoran *zr, unsigned reg, unsigned val)
1335     {
1336     	if (zr36060_write_8(zr, reg + 0, val >> 16)) {
1337     		return -1;
1338     	}
1339     	return zr36060_write_16(zr, reg + 1, val >> 0);
1340     }
1341     
1342     static int zr36060_write_32(struct zoran *zr, unsigned reg, unsigned val)
1343     {
1344     	if (zr36060_write_16(zr, reg + 0, val >> 16)) {
1345     		return -1;
1346     	}
1347     	return zr36060_write_16(zr, reg + 2, val >> 0);
1348     }
1349     
1350     static u32 zr36060_read_8(struct zoran *zr, unsigned reg)
1351     {
1352     	if (post_office_wait(zr)
1353     	    || post_office_write(zr, 0, 1, reg >> 8)
1354     	    || post_office_write(zr, 0, 2, reg)) {
1355     		return -1;
1356     	}
1357     	return post_office_read(zr, 0, 3) & 0xFF;
1358     }
1359     
1360     /* ----------------------------------------------------------------------- */
1361     
1362     static void GPIO(struct zoran *zr, unsigned bit, unsigned value)
1363     {
1364     	u32 reg;
1365     	u32 mask;
1366     
1367     	mask = 1 << (24 + bit);
1368     	reg = btread(ZR36057_GPPGCR1) & ~mask;
1369     	if (value) {
1370     		reg |= mask;
1371     	}
1372     	btwrite(reg, ZR36057_GPPGCR1);
1373     	udelay(1);
1374     }
1375     
1376     
1377     static void zr36060_sleep(struct zoran *zr, int sleep)
1378     {
1379     	switch (zr->card) {
1380     	case DC10:
1381     	case DC10plus:
1382     		GPIO(zr, 3, !sleep);
1383     		break;
1384     	case BUZ:
1385     	case LML33:
1386     		GPIO(zr, 1, !sleep);
1387     		break;
1388     	default:
1389     		break;
1390     	}
1391     	if (!sleep)
1392     		udelay(500);
1393     	else
1394     		udelay(2);
1395     }
1396     
1397     static int zr36060_reset(struct zoran *zr)
1398     {
1399     	switch (zr->card) {
1400     	case DC10:
1401     	case DC10plus:
1402     		zr36060_sleep(zr, 0);
1403     		GPIO(zr, 0, 0);
1404     		udelay(2);
1405     		GPIO(zr, 0, 1);
1406     		udelay(2);
1407     		break;
1408     	case LML33:
1409     	case BUZ:
1410     		zr36060_sleep(zr, 0);
1411     		post_office_write(zr, 3, 0, 0);
1412     		udelay(2);
1413     	default:
1414     	}
1415     	return 0;
1416     }
1417     
1418     static void set_frame(struct zoran *zr, int val)
1419     {
1420     	switch (zr->card) {
1421     	case DC10:
1422     	case DC10plus:
1423     		GPIO(zr, 6, val);
1424     		break;
1425     	case LML33:
1426     	case BUZ:
1427     		GPIO(zr, 3, val);
1428     		break;
1429     	default:
1430     		break;
1431     	}
1432     }
1433     
1434     static void set_videobus_dir(struct zoran *zr, int val)
1435     {
1436     	switch (zr->card) {
1437     	case DC10:
1438     	case DC10plus:
1439     		GPIO(zr, 1, val);
1440     		break;
1441     	case LML33:
1442     		if (lml33dpath == 0)
1443     			GPIO(zr, 5, val);
1444     		else
1445     			GPIO(zr, 5, 1);
1446     		break;
1447     	case BUZ:
1448     	default:
1449     		break;
1450     	}
1451     }
1452     
1453     static void set_videobus_enable(struct zoran *zr, int val)
1454     {
1455     	switch (zr->card) {
1456     	case LML33:
1457     		GPIO(zr, 7, val);
1458     		break;
1459     	case DC10:
1460     	case DC10plus:
1461     	case BUZ:
1462     	default:
1463     		break;
1464     	}
1465     }
1466     
1467     static void zr36060_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1468     {
1469     	struct tvnorm *tvn;
1470     	u32 reg;
1471     	int size, blocks;
1472     
1473     	reg = (1 << 0)		/* CodeMstr */
1474     	    |(0 << 2)		/* CFIS=0 */
1475     	    |(0 << 6)		/* Endian=0 */
1476     	    |(0 << 7);		/* Code16=0 */
1477     	zr36060_write_8(zr, 0x002, reg);
1478     
1479     	switch (mode) {
1480     
1481     	case BUZ_MODE_MOTION_DECOMPRESS:
1482     	case BUZ_MODE_STILL_DECOMPRESS:
1483     		reg = 0x00;	/* Codec mode = decompression */
1484     		break;
1485     
1486     	case BUZ_MODE_MOTION_COMPRESS:
1487     	case BUZ_MODE_STILL_COMPRESS:
1488     	default:
1489     		reg = 0xa4;	/* Codec mode = compression with variable scale factor */
1490     		break;
1491     
1492     	}
1493     	zr36060_write_8(zr, 0x003, reg);
1494     
1495     	reg = 0x00;		/* reserved, mbz */
1496     	zr36060_write_8(zr, 0x004, reg);
1497     
1498     	/* code volume */
1499     
1500     	/* Target field size in pixels: */
1501     	tvn = zr->timing;
1502     	size =
1503     	    (tvn->Ha / 2) * (tvn->Wa) / (zr->params.HorDcm) /
1504     	    (zr->params.VerDcm);
1505     	blocks = size / 64;
1506     
1507     	/* Target compressed field size in bits: */
1508     	size = size * 16;	/* uncompressed size in bits */
1509     	size = size * zr->params.quality / 400;	/* quality = 100 is a compression ratio 1:4 */
1510     
1511     	/* Lower limit (arbitrary, 1 KB) */
1512     	if (size < 8192)
1513     		size = 8192;
1514     
1515     	/* Upper limit: 6/8 of the code buffers */
1516     	if (size * zr->params.field_per_buff > zr->jpg_bufsize * 6)
1517     		size = zr->jpg_bufsize * 6 / zr->params.field_per_buff;
1518     
1519     	reg = size * 4 / blocks;
1520     	if (reg > 0xf0)
1521     		reg = 0xf0;	/* 480 bits/block, does 0xff represents unlimited? */
1522     	zr36060_write_8(zr, 0x005, reg);
1523     
1524     	/* JPEG markers */
1525     	reg = (zr->params.jpeg_markers) & 0x38;	/* DRI, DQT, DHT */
1526     	if (zr->params.COM_len)
1527     		reg |= JPEG_MARKER_COM;
1528     	if (zr->params.APP_len)
1529     		reg |= JPEG_MARKER_APP;
1530     	zr36060_write_8(zr, 0x006, reg);
1531     
1532     	if (zr->card != LML33 && zr->card != BUZ) {
1533     		reg = (0 << 3)	/* EOAV=0 */
1534     		    |(0 << 2)	/* EOI=0 */
1535     		    |(0 << 1)	/* END=0 */
1536     		    |(1 << 0);	/* DATERR=1 */
1537     	} else {
1538     		reg = (0 << 3)	/* EOAV=0 */
1539     		    |(0 << 2)	/* EOI=0 */
1540     		    |(0 << 1)	/* END=0 */
1541     		    |(0 << 0);	/* DATERR=0 */
1542     	}
1543     	zr36060_write_8(zr, 0x007, reg);
1544     
1545     	reg = size;
1546     	zr36060_write_32(zr, 0x009, reg);
1547     
1548     	reg = (size * 10) / 11;
1549     	zr36060_write_32(zr, 0x00d, reg);	// Not needed for compr. with variable scale factor, just in case ...
1550     
1551     	/* how do we set initial SF as a function of quality parameter? */
1552     	reg = 0x0100;		/* SF=1.0 */
1553     	zr36060_write_16(zr, 0x011, reg);
1554     
1555     	reg = 0x00ffffff;	/* AF=max */
1556     	zr36060_write_24(zr, 0x013, reg);
1557     
1558     	reg = 0x0000;		/* test */
1559     	zr36060_write_16(zr, 0x024, reg);
1560     
1561     	//post_office_read(zr,1,0);
1562     }
1563     
1564     static void zr36060_set_video(struct zoran *zr, enum zoran_codec_mode mode)
1565     {
1566     	struct tvnorm *tvn;
1567     	u32 reg;
1568     	unsigned HStart;
1569     
1570     	tvn = zr->timing;
1571     
1572     	reg = (0 << 7)		/* Video8     */
1573     	    |(0 << 6)		/* Range      */
1574     	    |(0 << 3)		/* FlDet      */
1575     	    |(1 << 2)		/* FlVedge    */
1576     	    |(0 << 1)		/* FlExt      */
1577     	    |(0 << 0);		/* SyncMstr   */
1578     
1579     	if (mode != BUZ_MODE_STILL_DECOMPRESS) {
1580     		/* limit pixels to range 16..235 as per CCIR-601 */
1581     		reg |= (1 << 6);	/* Range=1 */
1582     	}
1583     	zr36060_write_8(zr, 0x030, reg);
1584     
1585     	switch (zr->card) {
1586     	case DC10:
1587     	case DC10plus:
1588     		reg = (0 << 7)	/* VCLKPol   */
1589     		    |(0 << 6)	/* PValPol   */
1590     		    |(0 << 5)	/* PoePol    */
1591     		    |(0 << 4)	/* SImgPol   */
1592     		    |(1 << 3)	/* BLPol     */
1593     		    |(1 << 2)	/* FlPol     */
1594     		    |(1 << 1)	/* HSPol     */
1595     		    |(1 << 0);	/* VSPol     */
1596     		break;
1597     	case LML33:
1598     		reg = (0 << 7)	/* VCLKPol=0 */
1599     		    |(0 << 6)	/* PValPol=0 */
1600     		    |(1 << 5)	/* PoePol=1 */
1601     		    |(0 << 4)	/* SImgPol=0 */
1602     		    |(0 << 3)	/* BLPol=0 */
1603     		    |(0 << 2)	/* FlPol=0 */
1604     		    |(0 << 1)	/* HSPol=0, sync on falling edge */
1605     		    |(1 << 0);	/* VSPol=1 */
1606     		break;
1607     	case BUZ:
1608     	default:
1609     		reg = (0 << 7)	/* VCLKPol=0 */
1610     		    |(0 << 6)	/* PValPol=0 */
1611     		    |(1 << 5)	/* PoePol=1 */
1612     		    |(0 << 4)	/* SImgPol=0 */
1613     		    |(0 << 3)	/* BLPol=0 */
1614     		    |(0 << 2)	/* FlPol=0 */
1615     		    |(1 << 1)	/* HSPol=0, sync on falling edge */
1616     		    |(1 << 0);	/* VSPol=1 */
1617     		break;
1618     	}
1619     	zr36060_write_8(zr, 0x031, reg);
1620     
1621     	switch (zr->params.HorDcm) {
1622     	default:
1623     	case 1:
1624     		reg = (0 << 0);
1625     		break;		/* HScale = 0 */
1626     
1627     	case 2:
1628     		reg = (1 << 0);
1629     		break;		/* HScale = 1 */
1630     
1631     	case 4:
1632     		reg = (2 << 0);
1633     		break;		/* HScale = 2 */
1634     	}
1635     	if (zr->params.VerDcm == 2)
1636     		reg |= (1 << 2);
1637     	zr36060_write_8(zr, 0x032, reg);
1638     
1639     	reg = 0x00;		/* BackY */
1640     	zr36060_write_8(zr, 0x033, reg);
1641     
1642     	reg = 0x80;		/* BackU */
1643     	zr36060_write_8(zr, 0x034, reg);
1644     
1645     	reg = 0x80;		/* BackV */
1646     	zr36060_write_8(zr, 0x035, reg);
1647     
1648     	/* sync generator */
1649     
1650     	reg = tvn->Ht - 1;	/* Vtotal */
1651     	zr36060_write_16(zr, 0x036, reg);
1652     
1653     	reg = tvn->Wt - 1;	/* Htotal */
1654     	zr36060_write_16(zr, 0x038, reg);
1655     
1656     	reg = 6 - 1;		/* VsyncSize */
1657     	zr36060_write_8(zr, 0x03a, reg);
1658     
1659     	//reg   = 30 - 1;               /* HsyncSize */
1660     	reg = (zr->params.norm == 1 ? 57 : 68);
1661     	zr36060_write_8(zr, 0x03b, reg);
1662     
1663     	reg = tvn->VStart - 1;	/* BVstart */
1664     	zr36060_write_8(zr, 0x03c, reg);
1665     
1666     	reg += tvn->Ha / 2;	/* BVend */
1667     	zr36060_write_16(zr, 0x03e, reg);
1668     
1669     	reg = tvn->HStart + 64 - 1;	/* BHstart */
1670     	zr36060_write_8(zr, 0x03d, reg);
1671     
1672     	reg += tvn->Wa;		/* BHend */
1673     	zr36060_write_16(zr, 0x040, reg);
1674     
1675     	/* active area */
1676     	reg = zr->params.img_y + tvn->VStart;	/* Vstart */
1677     	zr36060_write_16(zr, 0x042, reg);
1678     
1679     	reg += zr->params.img_height;	/* Vend */
1680     	zr36060_write_16(zr, 0x044, reg);
1681     
1682     	HStart = tvn->HStart;
1683     	if (zr->card == BUZ) {
1684     		HStart += 44;
1685     	} else {
1686     		HStart += 64;
1687     	}
1688     	reg = zr->params.img_x + HStart;	/* Hstart */
1689     	zr36060_write_16(zr, 0x046, reg);
1690     
1691     	reg += zr->params.img_width;	/* Hend */
1692     	zr36060_write_16(zr, 0x048, reg);
1693     
1694     	/* subimage area */
1695     	reg = tvn->VStart - 4;	/* SVstart */
1696     	zr36060_write_16(zr, 0x04a, reg);
1697     
1698     	reg += tvn->Ha / 2 + 8;	/* SVend */
1699     	zr36060_write_16(zr, 0x04c, reg);
1700     
1701     	reg = tvn->HStart + 64 - 4;	/* SHstart */
1702     	zr36060_write_16(zr, 0x04e, reg);
1703     
1704     	reg += tvn->Wa + 8;	/* SHend */
1705     	zr36060_write_16(zr, 0x050, reg);
1706     }
1707     
1708     static void zr36060_set_jpg_SOF(struct zoran *zr)
1709     {
1710     	u32 reg;
1711     
1712     
1713     	reg = 0xffc0;		/* SOF marker */
1714     	zr36060_write_16(zr, 0x060, reg);
1715     
1716     	reg = 17;		/* SOF length */
1717     	zr36060_write_16(zr, 0x062, reg);
1718     
1719     	reg = 8;		/* precision 8 bits */
1720     	zr36060_write_8(zr, 0x064, reg);
1721     
1722     	reg = zr->params.img_height / zr->params.VerDcm;	/* image height */
1723     	zr36060_write_16(zr, 0x065, reg);
1724     
1725     	reg = zr->params.img_width / zr->params.HorDcm;	/* image width */
1726     	zr36060_write_16(zr, 0x067, reg);
1727     
1728     	reg = 3;		/* 3 color components */
1729     	zr36060_write_8(zr, 0x069, reg);
1730     
1731     	reg = 0x002100;		/* Y component */
1732     	zr36060_write_24(zr, 0x06a, reg);
1733     
1734     	reg = 0x011101;		/* U component */
1735     	zr36060_write_24(zr, 0x06d, reg);
1736     
1737     	reg = 0x021101;		/* V component */
1738     	zr36060_write_24(zr, 0x070, reg);
1739     }
1740     
1741     static void zr36060_set_jpg_SOS(struct zoran *zr)
1742     {
1743     	u32 reg;
1744     
1745     
1746     	reg = 0xffda;		/* SOS marker */
1747     	zr36060_write_16(zr, 0x07a, reg);
1748     
1749     	reg = 12;		/* SOS length */
1750     	zr36060_write_16(zr, 0x07c, reg);
1751     
1752     	reg = 3;		/* 3 color components */
1753     	zr36060_write_8(zr, 0x07e, reg);
1754     
1755     	reg = 0x0000;		/* Y component */
1756     	zr36060_write_16(zr, 0x07f, reg);
1757     
1758     	reg = 0x0111;		/* U component */
1759     	zr36060_write_16(zr, 0x081, reg);
1760     
1761     	reg = 0x0211;		/* V component */
1762     	zr36060_write_16(zr, 0x083, reg);
1763     
1764     	reg = 0x003f00;		/* Start, end spectral scans */
1765     	zr36060_write_24(zr, 0x085, reg);
1766     }
1767     
1768     static void zr36060_set_jpg_DRI(struct zoran *zr)
1769     {
1770     	u32 reg;
1771     
1772     
1773     	reg = 0xffdd;		/* DRI marker */
1774     	zr36060_write_16(zr, 0x0c0, reg);
1775     
1776     	reg = 4;		/* DRI length */
1777     	zr36060_write_16(zr, 0x0c2, reg);
1778     
1779     	reg = 8;		/* length in MCUs */
1780     	zr36060_write_16(zr, 0x0c4, reg);
1781     }
1782     
1783     static void zr36060_set_jpg_DQT(struct zoran *zr)
1784     {
1785     	unsigned i;
1786     	unsigned adr;
1787     	static const u8 dqt[] = {
1788     		0xff, 0xdb,	/* DHT marker */
1789     		0x00, 0x84,	/* DHT length */
1790     		0x00,		/* table ID 0 */
1791     		0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
1792     		0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
1793     		0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
1794     		0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
1795     		0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
1796     		0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
1797     		0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
1798     		0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
1799     		0x01,		/* table ID 1 */
1800     		0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
1801     		0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
1802     		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1803     		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1804     		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1805     		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1806     		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1807     		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
1808     	};
1809     
1810     	/* write fixed quantitization tables */
1811     	adr = 0x0cc;
1812     	for (i = 0; i < sizeof(dqt); ++i) {
1813     		zr36060_write_8(zr, adr++, dqt[i]);
1814     	}
1815     }
1816     
1817     static void zr36060_set_jpg_DHT(struct zoran *zr)
1818     {
1819     	unsigned i;
1820     	unsigned adr;
1821     	static const u8 dht[] = {
1822     		0xff, 0xc4,	/* DHT marker */
1823     		0x01, 0xa2,	/* DHT length */
1824     		0x00,		/* table class 0, ID 0 */
1825     		0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,	/* # codes of length 1..8 */
1826     		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* # codes of length 8..16 */
1827     		0x00,		/* values for codes of length 2 */
1828     		0x01, 0x02, 0x03, 0x04, 0x05,	/* values for codes of length 3 */
1829     		0x06,		/* values for codes of length 4 */
1830     		0x07,		/* values for codes of length 5 */
1831     		0x08,		/* values for codes of length 6 */
1832     		0x09,		/* values for codes of length 7 */
1833     		0x0a,		/* values for codes of length 8 */
1834     		0x0b,		/* values for codes of length 9 */
1835     		0x01,		/* table class 0, ID 1 */
1836     		0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,	/* # codes of length 1..8 */
1837     		0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,	/* # codes of length 9..16 */
1838     		0x00, 0x01, 0x02,	/* values for codes of length 2 */
1839     		0x03,		/* values for codes of length 3 */
1840     		0x04,		/* values for codes of length 4 */
1841     		0x05,		/* values for codes of length 5 */
1842     		0x06,		/* values for codes of length 6 */
1843     		0x07,		/* values for codes of length 7 */
1844     		0x08,		/* values for codes of length 8 */
1845     		0x09,		/* values for codes of length 9 */
1846     		0x0a,		/* values for codes of length 10 */
1847     		0x0b,		/* values for codes of length 11 */
1848     		0x10,
1849     		0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
1850     		0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
1851     		0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
1852     		0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
1853     		0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
1854     		0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
1855     		0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
1856     		0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
1857     		0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
1858     		0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
1859     		0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
1860     		0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
1861     		0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
1862     		0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
1863     		0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
1864     		0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1865     		0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
1866     		0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
1867     		0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
1868     		0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
1869     		0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
1870     		0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
1871     		0xf9, 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
1872     		0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00,
1873     		0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
1874     		0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
1875     		0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,
1876     		0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23,
1877     		0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a,
1878     		0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
1879     		0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35,
1880     		0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
1881     		0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55,
1882     		0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65,
1883     		0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
1884     		0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84,
1885     		0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93,
1886     		0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2,
1887     		0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
1888     		0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
1889     		0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
1890     		0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
1891     		0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
1892     		0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5,
1893     		0xf6, 0xf7, 0xf8, 0xf9, 0xfa
1894     	};
1895     
1896     	/* write fixed Huffman tables */
1897     	adr = 0x1d4;
1898     	for (i = 0; i < sizeof(dht); ++i) {
1899     		zr36060_write_8(zr, adr++, dht[i]);
1900     	}
1901     }
1902     
1903     static void zr36060_set_jpg_APP(struct zoran *zr)
1904     {
1905     	unsigned adr;
1906     	int len, i;
1907     	u32 reg;
1908     
1909     
1910     	len = zr->params.APP_len;
1911     	if (len < 0)
1912     		len = 0;
1913     	if (len > 60)
1914     		len = 60;
1915     
1916     	i = zr->params.APPn;
1917     	if (i < 0)
1918     		i = 0;
1919     	if (i > 15)
1920     		i = 15;
1921     
1922     	reg = 0xffe0 + i;	/* APPn marker */
1923     	zr36060_write_16(zr, 0x380, reg);
1924     
1925     	reg = len + 2;		/* APPn len */
1926     	zr36060_write_16(zr, 0x382, reg);
1927     
1928     	/* write APPn data */
1929     	adr = 0x384;
1930     	for (i = 0; i < 60; i++) {
1931     		zr36060_write_8(zr, adr++,
1932     				(i < len ? zr->params.APP_data[i] : 0));
1933     	}
1934     }
1935     
1936     static void zr36060_set_jpg_COM(struct zoran *zr)
1937     {
1938     	unsigned adr;
1939     	int len, i;
1940     	u32 reg;
1941     
1942     
1943     	len = zr->params.COM_len;
1944     	if (len < 0)
1945     		len = 0;
1946     	if (len > 60)
1947     		len = 60;
1948     
1949     	reg = 0xfffe;		/* COM marker */
1950     	zr36060_write_16(zr, 0x3c0, reg);
1951     
1952     	reg = len + 2;		/* COM len */
1953     	zr36060_write_16(zr, 0x3c2, reg);
1954     
1955     	/* write COM data */
1956     	adr = 0x3c4;
1957     	for (i = 0; i < 60; i++) {
1958     		zr36060_write_8(zr, adr++,
1959     				(i < len ? zr->params.COM_data[i] : 0));
1960     	}
1961     }
1962     
1963     static void zr36060_set_cap(struct zoran *zr, enum zoran_codec_mode mode)
1964     {
1965     	unsigned i;
1966     	u32 reg;
1967     
1968     	zr36060_reset(zr);
1969     	mdelay(10);
1970     
1971     	reg = (0 << 7)		/* Load=0 */
1972     	    |(1 << 0);		/* SynRst=1 */
1973     	zr36060_write_8(zr, 0x000, reg);
1974     
1975     	zr36060_set_jpg(zr, mode);
1976     	zr36060_set_video(zr, mode);
1977     	zr36060_set_jpg_SOF(zr);
1978     	zr36060_set_jpg_SOS(zr);
1979     	zr36060_set_jpg_DRI(zr);
1980     	zr36060_set_jpg_DQT(zr);
1981     	zr36060_set_jpg_DHT(zr);
1982     	zr36060_set_jpg_APP(zr);
1983     	zr36060_set_jpg_COM(zr);
1984     
1985     	reg = (1 << 7)		/* Load=1 */
1986     	    |(1 << 0);		/* SynRst=0 */
1987     	zr36060_write_8(zr, 0x000, reg);
1988     
1989     	/* wait for codec to unbusy */
1990     	for (i = 0; i < 100000; ++i) {
1991     		reg = zr36060_read_8(zr, 0x001);
1992     		if ((reg & (1 << 7)) == 0) {
1993     			return;
1994     		}
1995     		//udelay(100);
1996     	}
1997     	printk(KERN_ERR "%sZR36060: stuck busy, statux=%02x\n", zr->name,
1998     	       reg);
1999     }
2000     
2001     static void init_jpeg_queue(struct zoran *zr)
2002     {
2003     	int i;
2004     	/* re-initialize DMA ring stuff */
2005     	zr->jpg_que_head = 0;
2006     	zr->jpg_dma_head = 0;
2007     	zr->jpg_dma_tail = 0;
2008     	zr->jpg_que_tail = 0;
2009     	zr->jpg_seq_num = 0;
2010     	zr->JPEG_error = 0;
2011     	zr->num_errors = 0;
2012     	zr->jpg_err_seq = 0;
2013     	zr->jpg_err_shift = 0;
2014     	zr->jpg_queued_num = 0;
2015     	for (i = 0; i < zr->jpg_nbufs; i++) {
2016     		zr->jpg_gbuf[i].state = BUZ_STATE_USER;	/* nothing going on */
2017     	}
2018     	for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
2019     		zr->stat_com[i] = 1;	/* mark as unavailable to zr36057 */
2020     	}
2021     }
2022     
2023     static void zr36057_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
2024     {
2025     	struct tvnorm *tvn;
2026     	u32 reg;
2027     
2028     	tvn = zr->timing;
2029     
2030     	/* assert P_Reset */
2031     	btwrite(0, ZR36057_JPC);
2032     
2033     	/* MJPEG compression mode */
2034     	switch (mode) {
2035     
2036     	case BUZ_MODE_MOTION_COMPRESS:
2037     	default:
2038     		reg = ZR36057_JMC_MJPGCmpMode;
2039     		break;
2040     
2041     	case BUZ_MODE_MOTION_DECOMPRESS:
2042     		reg = ZR36057_JMC_MJPGExpMode;
2043     		reg |= ZR36057_JMC_SyncMstr;
2044     		/* RJ: The following is experimental - improves the output to screen */
2045     		//if(zr->params.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
2046     		break;
2047     
2048     	case BUZ_MODE_STILL_COMPRESS:
2049     		reg = ZR36057_JMC_JPGCmpMode;
2050     		break;
2051     
2052     	case BUZ_MODE_STILL_DECOMPRESS:
2053     		reg = ZR36057_JMC_JPGExpMode;
2054     		break;
2055     
2056     	}
2057     	reg |= ZR36057_JMC_JPG;
2058     	if (zr->params.field_per_buff == 1)
2059     		reg |= ZR36057_JMC_Fld_per_buff;
2060     	btwrite(reg, ZR36057_JMC);
2061     
2062     	/* vertical */
2063     	btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
2064     	reg =
2065     	    (6 << ZR36057_VSP_VsyncSize) | (tvn->Ht << ZR36057_VSP_FrmTot);
2066     	btwrite(reg, ZR36057_VSP);
2067     	reg = ((zr->params.img_y + tvn->VStart) << ZR36057_FVAP_NAY)
2068     	    | (zr->params.img_height << ZR36057_FVAP_PAY);
2069     	btwrite(reg, ZR36057_FVAP);
2070     
2071     	/* horizontal */
2072     	btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
2073     	reg =
2074     	    ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) | (tvn->
2075     							     Wt <<
2076     							     ZR36057_HSP_LineTot);
2077     	btwrite(reg, ZR36057_HSP);
2078     	reg = ((zr->params.img_x + tvn->HStart + 4) << ZR36057_FHAP_NAX)
2079     	    | (zr->params.img_width << ZR36057_FHAP_PAX);
2080     	btwrite(reg, ZR36057_FHAP);
2081     
2082     	/* field process parameters */
2083     	if (zr->params.odd_even)
2084     		reg = ZR36057_FPP_Odd_Even;
2085     	else
2086     		reg = 0;
2087     	if (mode == BUZ_MODE_MOTION_DECOMPRESS && zr->card != LML33
2088     	    && zr->card != BUZ)
2089     		reg ^= ZR36057_FPP_Odd_Even;
2090     
2091     	btwrite(reg, ZR36057_FPP);
2092     
2093     	/* Set proper VCLK Polarity, else colors will be wrong during playback */
2094     	//btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
2095     
2096     	/* code base address */
2097     	reg = virt_to_bus(zr->stat_com);
2098     	btwrite(reg, ZR36057_JCBA);
2099     
2100     	/* FIFO threshold (FIFO is 160. double words) */
2101     	/* NOTE: decimal values here */
2102     	switch (mode) {
2103     
2104     	case BUZ_MODE_STILL_COMPRESS:
2105     	case BUZ_MODE_MOTION_COMPRESS:
2106     		reg = 140;
2107     		break;
2108     
2109     	case BUZ_MODE_STILL_DECOMPRESS:
2110     	case BUZ_MODE_MOTION_DECOMPRESS:
2111     		reg = 20;
2112     		break;
2113     
2114     	default:
2115     		reg = 80;
2116     		break;
2117     
2118     	}
2119     	btwrite(reg, ZR36057_JCFT);
2120     	zr36057_adjust_vfe(zr, mode);
2121     
2122     }
2123     
2124     #if (DEBUGLEVEL > 2)
2125     static void dump_guests(struct zoran *zr)
2126     {
2127     	int i, guest[8];
2128     
2129     	for (i = 1; i < 8; i++) {	// Don't read zr36060 here
2130     		guest[i] = post_office_read(zr, i, 0);
2131     	}
2132     
2133     	printk(KERN_INFO "%s: Guests:", zr->name);
2134     
2135     	for (i = 1; i < 8; i++) {
2136     		printk(" 0x%02x", guest[i]);
2137     	}
2138     	printk("\n");
2139     }
2140     
2141     static unsigned long get_time(void)
2142     {
2143     	struct timeval tv;
2144     	do_gettimeofday(&tv);
2145     	return (1000000 * tv.tv_sec + tv.tv_usec);
2146     }
2147     
2148     static void detect_guest_activity(struct zoran *zr)
2149     {
2150     	int timeout, i, j, res, guest[8], guest0[8], change[8][3];
2151     	unsigned long t0, t1;
2152     
2153     	dump_guests(zr);
2154     	printk(KERN_INFO "%s: Detecting guests activity, please wait...\n",
2155     	       zr->name);
2156     	for (i = 1; i < 8; i++) {	// Don't read zr36060 here
2157     		guest0[i] = guest[i] = post_office_read(zr, i, 0);
2158     	}
2159     
2160     	timeout = 0;
2161     	j = 0;
2162     	t0 = get_time();
2163     	while (timeout < 10000) {
2164     		udelay(10);
2165     		timeout++;
2166     		for (i = 1; (i < 8) && (j < 8); i++) {
2167     			res = post_office_read(zr, i, 0);
2168     			if (res != guest[i]) {
2169     				t1 = get_time();
2170     				change[j][0] = (t1 - t0);
2171     				t0 = t1;
2172     				change[j][1] = i;
2173     				change[j][2] = res;
2174     				j++;
2175     				guest[i] = res;
2176     			}
2177     		}
2178     		if (j >= 8)
2179     			break;
2180     	}
2181     	printk(KERN_INFO "%s: Guests:", zr->name);
2182     
2183     	for (i = 1; i < 8; i++) {
2184     		printk(" 0x%02x", guest0[i]);
2185     	}
2186     	printk("\n");
2187     	if (j == 0) {
2188     		printk(KERN_INFO "%s: No activity detected.\n", zr->name);
2189     		return;
2190     	}
2191     	for (i = 0; i < j; i++) {
2192     		printk(KERN_INFO "%s: %6d: %d => 0x%02x\n", zr->name,
2193     		       change[i][0], change[i][1], change[i][2]);
2194     	}
2195     }
2196     #endif
2197     
2198     static void print_interrupts(struct zoran *zr)
2199     {
2200     	int res, noerr;
2201     	noerr = 0;
2202     	printk(KERN_INFO "%s: interrupts received:", zr->name);
2203     	if ((res = zr->field_counter) < -1 || res > 1) {
2204     		printk(" FD:%d", res);
2205     	}
2206     	if ((res = zr->intr_counter_GIRQ1) != 0) {
2207     		printk(" GIRQ1:%d", res);
2208     		noerr++;
2209     	}
2210     	if ((res = zr->intr_counter_GIRQ0) != 0) {
2211     		printk(" GIRQ0:%d", res);
2212     		noerr++;
2213     	}
2214     	if ((res = zr->intr_counter_CodRepIRQ) != 0) {
2215     		printk(" CodRepIRQ:%d", res);
2216     		noerr++;
2217     	}
2218     	if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
2219     		printk(" JPEGRepIRQ:%d", res);
2220     		noerr++;
2221     	}
2222     	if (zr->JPEG_max_missed) {
2223     		printk(" JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
2224     		       zr->JPEG_min_missed);
2225     	}
2226     	if (zr->END_event_missed) {
2227     		printk(" ENDs missed: %d", zr->END_event_missed);
2228     	}
2229     	//if (zr->jpg_queued_num) {
2230     	printk(" queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
2231     	       zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
2232     	//}
2233     	if (!noerr) {
2234     		printk(": no interrupts detected.");
2235     	}
2236     	printk("\n");
2237     }
2238     
2239     static void clear_interrupt_counters(struct zoran *zr)
2240     {
2241     	zr->intr_counter_GIRQ1 = 0;
2242     	zr->intr_counter_GIRQ0 = 0;
2243     	zr->intr_counter_CodRepIRQ = 0;
2244     	zr->intr_counter_JPEGRepIRQ = 0;
2245     	zr->field_counter = 0;
2246     	zr->IRQ1_in = 0;
2247     	zr->IRQ1_out = 0;
2248     	zr->JPEG_in = 0;
2249     	zr->JPEG_out = 0;
2250     	zr->JPEG_0 = 0;
2251     	zr->JPEG_1 = 0;
2252     	zr->END_event_missed = 0;
2253     	zr->JPEG_missed = 0;
2254     	zr->JPEG_max_missed = 0;
2255     	zr->JPEG_min_missed = 0x7fffffff;
2256     }
2257     
2258     static u32 count_reset_interrupt(struct zoran *zr)
2259     {
2260     	u32 isr;
2261     	if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
2262     		if (isr & ZR36057_ISR_GIRQ1) {
2263     			btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
2264     			zr->intr_counter_GIRQ1++;
2265     		}
2266     		if (isr & ZR36057_ISR_GIRQ0) {
2267     			btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
2268     			zr->intr_counter_GIRQ0++;
2269     		}
2270     		if (isr & ZR36057_ISR_CodRepIRQ) {
2271     			btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
2272     			zr->intr_counter_CodRepIRQ++;
2273     		}
2274     		if (isr & ZR36057_ISR_JPEGRepIRQ) {
2275     			btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
2276     			zr->intr_counter_JPEGRepIRQ++;
2277     		}
2278     	}
2279     	return isr;
2280     }
2281     
2282     static void jpeg_start(struct zoran *zr)
2283     {
2284     	int reg;
2285     	zr->frame_num = 0;
2286     
2287     	btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);	// /P_Reset
2288     	btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);	// \CFlush
2289     	btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC);	// /CodTrnsEn
2290     	btwrite(IRQ_MASK, ZR36057_ISR);	// Clear IRQs
2291     	btwrite(IRQ_MASK | ZR36057_ICR_IntPinEn, ZR36057_ICR);	// Enable IRQs
2292     
2293     	set_frame(zr, 0);	// \FRAME
2294     
2295     	/* JPEG codec guest ID */
2296     	reg =
2297     	    (1 << ZR36057_JCGI_JPEGuestID) | (0 <<
2298     					      ZR36057_JCGI_JPEGuestReg);
2299     	btwrite(reg, ZR36057_JCGI);
2300     
2301     	btor(ZR36057_JPC_Active, ZR36057_JPC);	// /Active
2302     	btor(ZR36057_JMC_Go_en, ZR36057_JMC);	// /Go_en
2303     	udelay(30);
2304     	set_frame(zr, 1);	// /FRAME
2305     }
2306     
2307     static void zr36057_enable_jpg(struct zoran *zr,
2308     			       enum zoran_codec_mode mode)
2309     {
2310     	static int zero = 0;
2311     	static int one = 1;
2312     
2313     	zr->codec_mode = mode;
2314     	switch (mode) {
2315     
2316     	case BUZ_MODE_MOTION_COMPRESS:
2317     		set_videobus_enable(zr, 0);
2318     		set_videobus_dir(zr, 0);	// GPIO(zr, 1, 0);
2319     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
2320     				   DECODER_ENABLE_OUTPUT, &one);
2321     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
2322     				   ENCODER_SET_INPUT, &zero);
2323     		set_videobus_enable(zr, 1);
2324     		zr36060_sleep(zr, 0);
2325     		zr36060_set_cap(zr, mode);	// Load ZR36060
2326     		init_jpeg_queue(zr);
2327     		zr36057_set_jpg(zr, mode);	// \P_Reset, ... Video param, FIFO
2328     
2329     		clear_interrupt_counters(zr);
2330     		DEBUG1(printk
2331     		       (KERN_INFO "%s: enable_jpg MOTION_COMPRESS\n",
2332     			zr->name));
2333     		break;
2334     
2335     	case BUZ_MODE_MOTION_DECOMPRESS:
2336     		set_videobus_enable(zr, 0);
2337     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
2338     				   DECODER_ENABLE_OUTPUT, &zero);
2339     		set_videobus_dir(zr, 1);	// GPIO(zr, 1, 1);
2340     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
2341     				   ENCODER_SET_INPUT, &one);
2342     		set_videobus_enable(zr, 1);
2343     		zr36060_sleep(zr, 0);
2344     		zr36060_set_cap(zr, mode);	// Load ZR36060
2345     		init_jpeg_queue(zr);
2346     		zr36057_set_jpg(zr, mode);	// \P_Reset, ... Video param, FIFO
2347     
2348     		clear_interrupt_counters(zr);
2349     		DEBUG1(printk
2350     		       (KERN_INFO "%s: enable_jpg MOTION_DECOMPRESS\n",
2351     			zr->name));
2352     		break;
2353     
2354     	case BUZ_MODE_IDLE:
2355     	default:
2356     		/* shut down processing */
2357     		btand(~(cardjpegint[zr->card] | ZR36057_ICR_JPEGRepIRQ),
2358     		      ZR36057_ICR);
2359     		btwrite(cardjpegint[zr->card] | ZR36057_ICR_JPEGRepIRQ,
2360     			ZR36057_ISR);
2361     		btand(~ZR36057_JMC_Go_en, ZR36057_JMC);	// \Go_en
2362     
2363     		set_current_state(TASK_UNINTERRUPTIBLE);
2364     		schedule_timeout(HZ/20);
2365     
2366     		set_videobus_dir(zr, 0);	// GPIO(zr, 1, 0);
2367     		set_frame(zr, 1);	//GPIO(zr, 6, 1);     // /FRAME
2368     		btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);	// /CFlush
2369     		btwrite(0, ZR36057_JPC);	// \P_Reset,\CodTrnsEn,\Active
2370     		btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
2371     		btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
2372     		zr36060_reset(zr);
2373     		zr36060_sleep(zr, 1);
2374     		zr36057_adjust_vfe(zr, mode);
2375     		set_videobus_enable(zr, 0);
2376     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
2377     				   DECODER_ENABLE_OUTPUT, &one);
2378     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
2379     				   ENCODER_SET_INPUT, &zero);
2380     		set_videobus_enable(zr, 1);
2381     		DEBUG1(printk
2382     		       (KERN_INFO "%s: enable_jpg IDLE\n", zr->name));
2383     		break;
2384     
2385     	}
2386     }
2387     
2388     /*
2389      *   Queue a MJPEG buffer for capture/playback
2390      */
2391     
2392     static int jpg_qbuf(struct zoran *zr, int frame,
2393     		    enum zoran_codec_mode mode)
2394     {
2395     	unsigned long flags;
2396     	int res;
2397     
2398     	/* Check if buffers are allocated */
2399     
2400     	if (!zr->jpg_buffers_allocated) {
2401     		printk(KERN_ERR
2402     		       "%s: jpg_qbuf: buffers not yet allocated\n",
2403     		       zr->name);
2404     		return -ENOMEM;
2405     	}
2406     
2407     	/* Does the user want to stop streaming? */
2408     
2409     	if (frame < 0) {
2410     		if (zr->codec_mode == mode) {
2411     			zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
2412     			return 0;
2413     		} else {
2414     			printk(KERN_ERR
2415     			       "%s: jpg_qbuf - stop streaming but not in streaming mode\n",
2416     			       zr->name);
2417     			return -EINVAL;
2418     		}
2419     	}
2420     
2421     	/* No grabbing outside the buffer range! */
2422     
2423     	if (frame >= zr->jpg_nbufs) {
2424     		printk(KERN_ERR "%s: jpg_qbuf: buffer %d out of range\n",
2425     		       zr->name, frame);
2426     		return -EINVAL;
2427     	}
2428     
2429     	/* what is the codec mode right now? */
2430     
2431     	if (zr->codec_mode == BUZ_MODE_IDLE) {
2432     		/* Ok load up the zr36060 */
2433     		zr36057_enable_jpg(zr, mode);
2434     	} else if (zr->codec_mode != mode) {
2435     		/* wrong codec mode active - invalid */
2436     		printk(KERN_ERR "%s: jpg_qbuf - codec in wrong mode\n",
2437     		       zr->name);
2438     		return -EINVAL;
2439     	}
2440     
2441     	spin_lock_irqsave(&zr->lock, flags);
2442     
2443     	/* make sure a grab isn't going on currently with this buffer */
2444     
2445     	switch (zr->jpg_gbuf[frame].state) {
2446     
2447     	case BUZ_STATE_DONE:
2448     		DEBUG1(printk
2449     		       (KERN_WARNING
2450     			"%s: Warning: queing frame in BUZ_STATE_DONE state\n",
2451     			zr->name));
2452     	case BUZ_STATE_USER:
2453     		/* since there is at least one unused buffer there's room for at least one more pend[] entry */
2454     		zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = frame;
2455     		zr->jpg_gbuf[frame].state = BUZ_STATE_PEND;
2456     		zoran_feed_stat_com(zr);
2457     		res = 0;
2458     		break;
2459     
2460     	default:
2461     	case BUZ_STATE_DMA:
2462     	case BUZ_STATE_PEND:
2463     		res = -EBUSY;	/* what are you doing? */
2464     		break;
2465     
2466     	}
2467     
2468     	spin_unlock_irqrestore(&zr->lock, flags);
2469     
2470     	/* Start the zr36060 when the first frame is queued  */
2471     	if (zr->jpg_que_head == 1)
2472     		jpeg_start(zr);
2473     
2474     	return res;
2475     }
2476     
2477     /*
2478      *   Sync on a MJPEG buffer
2479      */
2480     
2481     static int jpg_sync(struct zoran *zr, struct zoran_sync *bs)
2482     {
2483     	unsigned long flags;
2484     	int frame, timeout;
2485     
2486     	if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS
2487     	    && zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
2488     		DEBUG1(printk(KERN_ERR
2489     		       "%s: BUZIOCSYNC: - codec not in streaming mode\n",
2490     		       zr->name));
2491     		return -EINVAL;
2492     	}
2493     	while (zr->jpg_que_tail == zr->jpg_dma_tail) {
2494     		if (zr->jpg_dma_tail == zr->jpg_dma_head)
2495     			break;
2496     		timeout =
2497     		    interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
2498     		if (!timeout) {
2499     			btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
2500     			udelay(1);
2501     			printk(KERN_ERR
2502     			       "%s: timeout: codec isr=0x%02x, csr=0x%02x\n",
2503     			       zr->name, zr36060_read_8(zr, 0x008),
2504     			       zr36060_read_8(zr, 0x001));
2505     			return -ETIME;
2506     		} else if (signal_pending(current))
2507     			return -ERESTARTSYS;
2508     	}
2509     
2510     	spin_lock_irqsave(&zr->lock, flags);
2511     
2512     	if (zr->jpg_dma_tail != zr->jpg_dma_head)
2513     		frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
2514     	else
2515     		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2516     	/* buffer should now be in BUZ_STATE_DONE */
2517     
2518     #if(DEBUGLEVEL > 0)
2519     	if (zr->jpg_gbuf[frame].state != BUZ_STATE_DONE)
2520     		printk(KERN_ERR "%s: jpg_sync - internal error\n",
2521     		       zr->name);
2522     #endif
2523     
2524     	*bs = zr->jpg_gbuf[frame].bs;
2525     	zr->jpg_gbuf[frame].state = BUZ_STATE_USER;
2526     
2527     	spin_unlock_irqrestore(&zr->lock, flags);
2528     
2529     	return 0;
2530     }
2531     
2532     /* when this is called the spinlock must be held */
2533     static void zoran_feed_stat_com(struct zoran *zr)
2534     {
2535     	/* move frames from pending queue to DMA */
2536     
2537     	int frame, i, max_stat_com;
2538     
2539     	max_stat_com =
2540     	    (zr->params.TmpDcm ==
2541     	     1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
2542     
2543     	while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com
2544     	       && zr->jpg_dma_head < zr->jpg_que_head) {
2545     
2546     		frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
2547     		if (zr->params.TmpDcm == 1) {
2548     			/* fill 1 stat_com entry */
2549     			i = (zr->jpg_dma_head -
2550     			     zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
2551     			if (!(zr->stat_com[i] & 1))
2552     				break;
2553     			zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
2554     		} else {
2555     			/* fill 2 stat_com entries */
2556     			i = ((zr->jpg_dma_head -
2557     			      zr->jpg_err_shift) & 1) * 2;
2558     			if (!(zr->stat_com[i] & 1))
2559     				break;
2560     			zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
2561     			zr->stat_com[i + 1] =
2562     			    zr->jpg_gbuf[frame].frag_tab_bus;
2563     		}
2564     		zr->jpg_gbuf[frame].state = BUZ_STATE_DMA;
2565     		zr->jpg_dma_head++;
2566     
2567     	}
2568     	if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
2569     		zr->jpg_queued_num++;
2570     }
2571     
2572     /* when this is called the spinlock must be held */
2573     static void zoran_reap_stat_com(struct zoran *zr)
2574     {
2575     	/* move frames from DMA queue to done queue */
2576     
2577     	int i;
2578     	u32 stat_com;
2579     	unsigned int seq;
2580     	unsigned int dif;
2581     	int frame;
2582     	struct zoran_gbuffer *gbuf;
2583     
2584     	/* In motion decompress we don't have a hardware frame counter,
2585     	   we just count the interrupts here */
2586     
2587     	if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
2588     		zr->jpg_seq_num++;
2589     	}
2590     	while (zr->jpg_dma_tail < zr->jpg_dma_head) {
2591     		if (zr->params.TmpDcm == 1)
2592     			i = (zr->jpg_dma_tail -
2593     			     zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
2594     		else
2595     			i = ((zr->jpg_dma_tail -
2596     			      zr->jpg_err_shift) & 1) * 2 + 1;
2597     
2598     		stat_com = zr->stat_com[i];
2599     
2600     		if ((stat_com & 1) == 0) {
2601     			return;
2602     		}
2603     		frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
2604     		gbuf = &zr->jpg_gbuf[frame];
2605     		get_fast_time(&gbuf->bs.timestamp);
2606     
2607     		if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
2608     			gbuf->bs.length = (stat_com & 0x7fffff) >> 1;
2609     
2610     			/* update sequence number with the help of the counter in stat_com */
2611     
2612     			seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
2613     			dif = (seq - zr->jpg_seq_num) & 0xff;
2614     			zr->jpg_seq_num += dif;
2615     		} else {
2616     			gbuf->bs.length = 0;
2617     		}
2618     		gbuf->bs.seq =
2619     		    zr->params.TmpDcm ==
2620     		    2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
2621     		gbuf->state = BUZ_STATE_DONE;
2622     
2623     		zr->jpg_dma_tail++;
2624     	}
2625     }
2626     
2627     static void error_handler(struct zoran *zr, u32 astat, u32 stat)
2628     {
2629     	/* This is JPEG error handling part */
2630     	if ((zr->codec_mode != BUZ_MODE_MOTION_COMPRESS)
2631     	    && (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS)) {
2632     		//printk(KERN_ERR "%s: Internal error: error handling request in mode %d\n", zr->name, zr->codec_mode);
2633     		return;
2634     	}
2635     	if ((stat & 1) == 0
2636     	    && zr->codec_mode == BUZ_MODE_MOTION_COMPRESS
2637     	    && zr->jpg_dma_tail - zr->jpg_que_tail >= zr->jpg_nbufs) {
2638     		/* No free buffers... */
2639     		zoran_reap_stat_com(zr);
2640     		zoran_feed_stat_com(zr);
2641     		wake_up_interruptible(&zr->jpg_capq);
2642     		zr->JPEG_missed = 0;
2643     		return;
2644     	}
2645     	if (zr->JPEG_error != 1) {
2646     		/*
2647     		 * First entry: error just happened during normal operation
2648     		 * 
2649     		 * In BUZ_MODE_MOTION_COMPRESS:
2650     		 * 
2651     		 * Possible glitch in TV signal. In this case we should
2652     		 * stop the codec and wait for good quality signal before
2653     		 * restarting it to avoid further problems
2654     		 * 
2655     		 * In BUZ_MODE_MOTION_DECOMPRESS:
2656     		 * 
2657     		 * Bad JPEG frame: we have to mark it as processed (codec crashed
2658     		 * and was not able to do it itself), and to remove it from queue.
2659     		 */
2660     		btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
2661     		udelay(1);
2662     		stat =
2663     		    stat | (post_office_read(zr, 7, 0) & 3) << 8 |
2664     		    zr36060_read_8(zr, 0x008);
2665     		btwrite(0, ZR36057_JPC);
2666     		btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
2667     		zr36060_reset(zr);
2668     		zr36060_sleep(zr, 1);
2669     		zr->JPEG_error = 1;
2670     		zr->num_errors++;
2671     		/* Report error */
2672     #if(DEBUGLEVEL > 1)
2673     		if (zr->num_errors <= 8) {
2674     			long frame;
2675     			frame =
2676     			    zr->jpg_pend[zr->
2677     					 jpg_dma_tail & BUZ_MASK_FRAME];
2678     			printk(KERN_ERR
2679     			       "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
2680     			       zr->name, stat, zr->last_isr,
2681     			       zr->jpg_que_tail, zr->jpg_dma_tail,
2682     			       zr->jpg_dma_head, zr->jpg_que_head,
2683     			       zr->jpg_seq_num, frame);
2684     			printk("stat_com frames:");
2685     			{
2686     				int i, j;
2687     				for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
2688     					for (i = 0; i < zr->jpg_nbufs; i++) {
2689     						if (zr->stat_com[j] ==
2690     						    zr->jpg_gbuf[i].
2691     						    frag_tab_bus) {
2692     							printk("% d->%d",
2693     							       j, i);
2694     						}
2695     					}
2696     				}
2697     				printk("\n");
2698     			}
2699     		}
2700     #endif
2701     		/* Find an entry in stat_com and rotate contents */
2702     		{
2703     			int i;
2704     
2705     			if (zr->params.TmpDcm == 1)
2706     				i = (zr->jpg_dma_tail -
2707     				     zr->
2708     				     jpg_err_shift) & BUZ_MASK_STAT_COM;
2709     			else
2710     				i = ((zr->jpg_dma_tail -
2711     				      zr->jpg_err_shift) & 1) * 2;
2712     			if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
2713     				/* Mimic zr36067 operation */
2714     				zr->stat_com[i] |= 1;
2715     				if (zr->params.TmpDcm != 1)
2716     					zr->stat_com[i + 1] |= 1;
2717     				/* Refill */
2718     				zoran_reap_stat_com(zr);
2719     				zoran_feed_stat_com(zr);
2720     				wake_up_interruptible(&zr->jpg_capq);
2721     				/* Find an entry in stat_com again after refill */
2722     				if (zr->params.TmpDcm == 1)
2723     					i = (zr->jpg_dma_tail -
2724     					     zr->
2725     					     jpg_err_shift) &
2726     					    BUZ_MASK_STAT_COM;
2727     				else
2728     					i = ((zr->jpg_dma_tail -
2729     					      zr->jpg_err_shift) & 1) * 2;
2730     			}
2731     			if (i) {
2732     				/* Rotate stat_comm entries to make current entry first */
2733     				int j;
2734     				u32 bus_addr[BUZ_NUM_STAT_COM];
2735     
2736     				memcpy(bus_addr, zr->stat_com,
2737     				       sizeof(bus_addr));
2738     				for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
2739     					zr->stat_com[j] =
2740     					    bus_addr[(i +
2741     						      j) &
2742     						     BUZ_MASK_STAT_COM];
2743     				}
2744     				zr->jpg_err_shift += i;
2745     				zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
2746     			}
2747     			if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
2748     				zr->jpg_err_seq = zr->jpg_seq_num;	/* + 1; */
2749     		}
2750     	}
2751     	/* Now the stat_comm buffer is ready for restart */
2752     	{
2753     		int status;
2754     
2755     		status = 0;
2756     		if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
2757     			i2c_control_device(&zr->i2c,
2758     					   I2C_DRIVERID_VIDEODECODER,
2759     					   DECODER_GET_STATUS, &status);
2760     		if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS
2761     		    || (status & DECODER_STATUS_GOOD)) {
2762     	    /********** RESTART code *************/
2763     			zr36060_reset(zr);
2764     			zr36060_set_cap(zr, zr->codec_mode);
2765     			zr36057_set_jpg(zr, zr->codec_mode);
2766     			jpeg_start(zr);
2767     #if(DEBUGLEVEL > 1)
2768     			if (zr->num_errors <= 8)
2769     				printk(KERN_INFO "%s: Restart\n",
2770     				       zr->name);
2771     #endif
2772     			zr->JPEG_missed = 0;
2773     			zr->JPEG_error = 2;
2774     	    /********** End RESTART code ***********/
2775     		}
2776     	}
2777     }
2778     
2779     static void zoran_irq(int irq, void *dev_id, struct pt_regs *regs)
2780     {
2781     	u32 stat, astat;
2782     	int count;
2783     	struct zoran *zr;
2784     	unsigned long flags;
2785     
2786     	zr = (struct zoran *) dev_id;
2787     	count = 0;
2788     
2789     	if (zr->testing) {
2790     		/* Testing interrupts */
2791     		spin_lock_irqsave(&zr->lock, flags);
2792     		while ((stat = count_reset_interrupt(zr))) {
2793     			if (count++ > 100) {
2794     				btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
2795     				printk(KERN_ERR
2796     				       "%s: IRQ lockup while testing, isr=0x%08x, cleared int mask\n",
2797     				       zr->name, stat);
2798     				wake_up_interruptible(&zr->test_q);
2799     			}
2800     		}
2801     		zr->last_isr = stat;
2802     		spin_unlock_irqrestore(&zr->lock, flags);
2803     		return;
2804     	}
2805     
2806     	spin_lock_irqsave(&zr->lock, flags);
2807     	while (1) {
2808     		/* get/clear interrupt status bits */
2809     		stat = count_reset_interrupt(zr);
2810     		astat = stat & IRQ_MASK;
2811     		if (!astat) {
2812     			break;
2813     		}
2814     		if (astat & cardvsync[zr->card]) {	// SW
2815     
2816     			if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS
2817     			    || zr->codec_mode ==
2818     			    BUZ_MODE_MOTION_COMPRESS) {
2819     				/* count missed interrupts */
2820     				zr->JPEG_missed++;
2821     			}
2822     			//post_office_read(zr,1,0);
2823     			/* Interrupts may still happen when zr->v4l_memgrab_active is switched off.
2824     			   We simply ignore them */
2825     
2826     			if (zr->v4l_memgrab_active) {
2827     
2828     				/* A lot more checks should be here ... */
2829     				if ((btread(ZR36057_VSSFGR) &
2830     				     ZR36057_VSSFGR_SnapShot) == 0)
2831     					printk(KERN_WARNING
2832     					       "%s: BuzIRQ with SnapShot off ???\n",
2833     					       zr->name);
2834     
2835     				if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
2836     					/* There is a grab on a frame going on, check if it has finished */
2837     
2838     					if ((btread(ZR36057_VSSFGR) &
2839     					     ZR36057_VSSFGR_FrameGrab) ==
2840     					    0) {
2841     						/* it is finished, notify the user */
2842     
2843     						zr->v4l_gbuf[zr->
2844     							     v4l_grab_frame].
2845     						    state = BUZ_STATE_DONE;
2846     						zr->v4l_grab_frame =
2847     						    NO_GRAB_ACTIVE;
2848     						zr->v4l_grab_seq++;
2849     						zr->v4l_pend_tail++;
2850     					}
2851     				}
2852     
2853     				if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
2854     					wake_up_interruptible(&zr->
2855     							      v4l_capq);
2856     
2857     				/* Check if there is another grab queued */
2858     
2859     				if (zr->v4l_grab_frame == NO_GRAB_ACTIVE
2860     				    && zr->v4l_pend_tail !=
2861     				    zr->v4l_pend_head) {
2862     
2863     					int frame =
2864     					    zr->v4l_pend[zr->
2865     							 v4l_pend_tail &
2866     							 V4L_MASK_FRAME];
2867     					u32 reg;
2868     
2869     					zr->v4l_grab_frame = frame;
2870     
2871     					/* Set zr36057 video front end and enable video */
2872     
2873     					/* Buffer address */
2874     
2875     					reg =
2876     					    zr->v4l_gbuf[frame].
2877     					    fbuffer_bus;
2878     					btwrite(reg, ZR36057_VDTR);
2879     					if (zr->video_interlace)
2880     						reg += zr->gbpl;
2881     					btwrite(reg, ZR36057_VDBR);
2882     
2883     					/* video stride, status, and frame grab register */
2884     
2885     					reg = 0;
2886     					if (zr->video_interlace)
2887     						reg += zr->gbpl;
2888     					reg =
2889     					    (reg <<
2890     					     ZR36057_VSSFGR_DispStride);
2891     					reg |= ZR36057_VSSFGR_VidOvf;
2892     					reg |= ZR36057_VSSFGR_SnapShot;
2893     					reg |= ZR36057_VSSFGR_FrameGrab;
2894     					btwrite(reg, ZR36057_VSSFGR);
2895     
2896     					btor(ZR36057_VDCR_VidEn,
2897     					     ZR36057_VDCR);
2898     				}
2899     			}
2900     		}
2901     #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
2902     		if (astat & ZR36057_ISR_CodRepIRQ) {
2903     			zr->intr_counter_CodRepIRQ++;
2904     			IDEBUG(printk
2905     			       (KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n",
2906     				zr->name));
2907     			btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
2908     		}
2909     #endif				/* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
2910     
2911     #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
2912     		if (astat & ZR36057_ISR_JPEGRepIRQ) {
2913     
2914     			if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS
2915     			    || zr->codec_mode ==
2916     			    BUZ_MODE_MOTION_COMPRESS) {
2917     #if(DEBUGLEVEL > 1)
2918     				if (!zr->frame_num || zr->JPEG_error) {
2919     					printk(KERN_INFO
2920     					       "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%d\n",
2921     					       zr->name, stat,
2922     					       zr->params.odd_even,
2923     					       zr->params.field_per_buff,
2924     					       zr->JPEG_missed);
2925     					{
2926     						char sc[] = "0000";
2927     						char sv[5];
2928     						int i;
2929     						strcpy(sv, sc);
2930     						for (i = 0; i < 4; i++) {
2931     							if (zr->
2932     							    stat_com[i] &
2933     							    1)
2934     								sv[i] =
2935     								    '1';
2936     						}
2937     						sv[4] = 0;
2938     						printk(KERN_INFO
2939     						       "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ld\n",
2940     						       zr->name, sv,
2941     						       zr->jpg_que_tail,
2942     						       zr->jpg_dma_tail,
2943     						       zr->jpg_dma_head,
2944     						       zr->jpg_que_head);
2945     					}
2946     				} else {
2947     					if (zr->JPEG_missed > zr->JPEG_max_missed)	// Get statistics
2948     						zr->JPEG_max_missed =
2949     						    zr->JPEG_missed;
2950     					if (zr->JPEG_missed <
2951     					    zr->JPEG_min_missed)
2952     						zr->JPEG_min_missed =
2953     						    zr->JPEG_missed;
2954     				}
2955     #endif
2956     #if(DEBUGLEVEL > 2)
2957     				if (zr->frame_num < 6) {
2958     					int i;
2959     					printk("%s: seq=%ld stat_com:",
2960     					       zr->name, zr->jpg_seq_num);
2961     					for (i = 0; i < 4; i++) {
2962     						printk(" %08x",
2963     						       zr->stat_com[i]);
2964     					}
2965     					printk("\n");
2966     				}
2967     #endif
2968     				zr->frame_num++;
2969     				zr->JPEG_missed = 0;
2970     				zr->JPEG_error = 0;
2971     				zoran_reap_stat_com(zr);
2972     				zoran_feed_stat_com(zr);
2973     				wake_up_interruptible(&zr->jpg_capq);
2974     			}	//else {
2975     			//      printk(KERN_ERR "%s: JPEG interrupt while not in motion (de)compress mode!\n", zr->name);
2976     			//}
2977     		}
2978     #endif				/* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
2979     
2980     		if ((astat & cardjpegint[zr->card])	/* DATERR interrupt received                 */
2981     		    ||zr->JPEG_missed > 25	/* Too many fields missed without processing */
2982     		    || zr->JPEG_error == 1	/* We are already in error processing        */
2983     		    || ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
2984     			&& (zr->
2985     			    frame_num & (zr->JPEG_missed >
2986     					 zr->params.field_per_buff)))
2987     		    /* fields missed during decompression */
2988     		    ) {
2989     			error_handler(zr, astat, stat);
2990     		}
2991     
2992     		count++;
2993     		if (count > 10) {
2994     			printk(KERN_WARNING "%s: irq loop %d\n", zr->name,
2995     			       count);
2996     			if (count > 20) {
2997     				btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
2998     				printk(KERN_ERR
2999     				       "%s: IRQ lockup, cleared int mask\n",
3000     				       zr->name);
3001     				break;
3002     			}
3003     		}
3004     		zr->last_isr = stat;
3005     	}
3006     	spin_unlock_irqrestore(&zr->lock, flags);
3007     }
3008     
3009     /* Check a zoran_params struct for correctness, insert default params */
3010     
3011     static int zoran_check_params(struct zoran *zr,
3012     			      struct zoran_params *params)
3013     {
3014     	int err = 0, err0 = 0;
3015     
3016     	/* insert constant params */
3017     
3018     	params->major_version = MAJOR_VERSION;
3019     	params->minor_version = MINOR_VERSION;
3020     
3021     	/* Check input and norm: must be set by calling VIDIOCSCHAN only! */
3022     
3023     	params->norm = zr->params.norm;
3024     	params->input = zr->params.input;
3025     
3026     	/* Check decimation, set default values for decimation = 1, 2, 4 */
3027     
3028     	switch (params->decimation) {
3029     	case 1:
3030     
3031     		params->HorDcm = 1;
3032     		params->VerDcm = 1;
3033     		params->TmpDcm = 1;
3034     		params->field_per_buff = 2;
3035     
3036     		params->img_x = 0;
3037     		params->img_y = 0;
3038     		params->img_width = zr->timing->Wa;
3039     		params->img_height = zr->timing->Ha / 2;
3040     		break;
3041     
3042     	case 2:
3043     
3044     		params->HorDcm = 2;
3045     		params->VerDcm = 1;
3046     		params->TmpDcm = 2;
3047     		params->field_per_buff = 1;
3048     
3049     		params->img_x = 8;
3050     		params->img_y = 0;
3051     		params->img_width = zr->timing->Wa;
3052     		params->img_height = zr->timing->Ha / 2;
3053     		break;
3054     
3055     	case 4:
3056     
3057     		params->HorDcm = 4;
3058     		params->VerDcm = 2;
3059     		params->TmpDcm = 2;
3060     		params->field_per_buff = 1;
3061     
3062     		params->img_x = 8;
3063     		params->img_y = 0;
3064     		params->img_width = zr->timing->Wa;
3065     		params->img_height = zr->timing->Ha / 2;
3066     		break;
3067     
3068     	case 0:
3069     
3070     		/* We have to check the data the user has set */
3071     
3072     		if (params->HorDcm != 1 && params->HorDcm != 2
3073     		    && params->HorDcm != 4)
3074     			err0++;
3075     		if (params->VerDcm != 1 && params->VerDcm != 2)
3076     			err0++;
3077     		if (params->TmpDcm != 1 && params->TmpDcm != 2)
3078     			err0++;
3079     		if (params->field_per_buff != 1
3080     		    && params->field_per_buff != 2)
3081     			err0++;
3082     
3083     		if (params->img_x < 0)
3084     			err0++;
3085     		if (params->img_y < 0)
3086     			err0++;
3087     		if (params->img_width < 0)
3088     			err0++;
3089     		if (params->img_height < 0)
3090     			err0++;
3091     		if (params->img_x + params->img_width > zr->timing->Wa)
3092     			err0++;
3093     		if (params->img_y + params->img_height >
3094     		    zr->timing->Ha / 2)
3095     			err0++;
3096     		if (params->HorDcm) {
3097     			if (params->img_width % (16 * params->HorDcm) != 0)
3098     				err0++;
3099     			if (params->img_height % (8 * params->VerDcm) != 0)
3100     				err0++;
3101     		}
3102     
3103     		if (err0) {
3104     			DEBUG1(printk(KERN_ERR
3105     			       "%s: SET PARAMS: error in params for decimation = 0\n",
3106     			       zr->name));
3107     			err++;
3108     		}
3109     		break;
3110     
3111     	default:
3112     		DEBUG1(printk(KERN_ERR
3113     		       "%s: SET PARAMS: decimation = %d, must be 0, 1, 2 or 4\n",
3114     		       zr->name, params->decimation));
3115     		err++;
3116     		break;
3117     	}
3118     
3119     	if (params->quality > 100)
3120     		params->quality = 100;
3121     	if (params->quality < 5)
3122     		params->quality = 5;
3123     
3124     	if (params->APPn < 0)
3125     		params->APPn = 0;
3126     	if (params->APPn > 15)
3127     		params->APPn = 15;
3128     	if (params->APP_len < 0)
3129     		params->APP_len = 0;
3130     	if (params->APP_len > 60)
3131     		params->APP_len = 60;
3132     	if (params->COM_len < 0)
3133     		params->COM_len = 0;
3134     	if (params->COM_len > 60)
3135     		params->COM_len = 60;
3136     
3137     	if (err)
3138     		return -EINVAL;
3139     
3140     	return 0;
3141     
3142     }
3143     static void zoran_open_init_params(struct zoran *zr)
3144     {
3145     	int i;
3146     
3147     	/* Per default, map the V4L Buffers */
3148     
3149     	zr->map_mjpeg_buffers = 0;
3150     
3151     	/* User must explicitly set a window */
3152     
3153     	zr->window_set = 0;
3154     
3155     	zr->window.x = 0;
3156     	zr->window.y = 0;
3157     	zr->window.width = 0;
3158     	zr->window.height = 0;
3159     	zr->window.chromakey = 0;
3160     	zr->window.flags = 0;
3161     	zr->window.clips = NULL;
3162     	zr->window.clipcount = 0;
3163     
3164     	zr->video_interlace = 0;
3165     
3166     	zr->v4l_memgrab_active = 0;
3167     	zr->v4l_overlay_active = 0;
3168     
3169     	zr->v4l_grab_frame = NO_GRAB_ACTIVE;
3170     	zr->v4l_grab_seq = 0;
3171     
3172     	zr->gwidth = 0;
3173     	zr->gheight = 0;
3174     	zr->gformat = 0;
3175     	zr->gbpl = 0;
3176     
3177     	/* DMA ring stuff for V4L */
3178     
3179     	zr->v4l_pend_tail = 0;
3180     	zr->v4l_pend_head = 0;
3181     	for (i = 0; i < v4l_nbufs; i++) {
3182     		zr->v4l_gbuf[i].state = BUZ_STATE_USER;	/* nothing going on */
3183     	}
3184     
3185     	/* Set necessary params and call zoran_check_params to set the defaults */
3186     
3187     	zr->params.decimation = 1;
3188     
3189     	zr->params.quality = 50;	/* default compression factor 8 */
3190     	if (zr->card != BUZ)
3191     		zr->params.odd_even = 1;
3192     	else
3193     		zr->params.odd_even = 0;
3194     
3195     	zr->params.APPn = 0;
3196     	zr->params.APP_len = 0;	/* No APPn marker */
3197     	for (i = 0; i < 60; i++)
3198     		zr->params.APP_data[i] = 0;
3199     
3200     	zr->params.COM_len = 0;	/* No COM marker */
3201     	for (i = 0; i < 60; i++)
3202     		zr->params.COM_data[i] = 0;
3203     
3204     	zr->params.VFIFO_FB = 0;
3205     
3206     	memset(zr->params.reserved, 0, sizeof(zr->params.reserved));
3207     
3208     	zr->params.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
3209     
3210     	i = zoran_check_params(zr, &zr->params);
3211     	if (i)
3212     		printk(KERN_ERR
3213     		       "%s: zoran_open_init_params internal error\n",
3214     		       zr->name);
3215     
3216     	clear_interrupt_counters(zr);
3217     	zr->testing = 0;
3218     }
3219     
3220     /*
3221      *   Open a zoran card. Right now the flags stuff is just playing
3222      */
3223     
3224     static int zoran_open(struct video_device *dev, int flags)
3225     {
3226     	struct zoran *zr = (struct zoran *) dev;
3227     	//int one = 1;
3228     
3229     	DEBUG1(printk
3230     	       (KERN_INFO "%s: zoran_open, %s pid=[%d]\n", zr->name,
3231     		current->comm, current->pid));
3232     
3233     	switch (flags) {
3234     
3235     	case 0:
3236     		if (zr->user > 1) {
3237     			DEBUG1(printk(KERN_WARNING
3238     			       "%s: zoran_open: Buz is allready in use\n",
3239     			       zr->name));
3240     			return -EBUSY;
3241     		}
3242     		zr->user++;
3243     
3244     		if (zr->user == 1 && v4l_fbuffer_alloc(zr) < 0) {
3245     			zr->user--;
3246     			printk(KERN_ERR
3247     			       "%s: zoran_open: v4l_fbuffer_alloc failed\n",
3248     			       zr->name);
3249     			return -ENOMEM;
3250     		}
3251     
3252     		/* default setup */
3253     
3254     		if (zr->user == 1) {	/* First device open */
3255     			zoran_open_init_params(zr);
3256     
3257     			zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
3258     
3259     			btwrite(IRQ_MASK, ZR36057_ISR);	// Clears interrupts
3260     			btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
3261     			dev->busy = 0;	/* Allow second open */
3262     		}
3263     
3264     		break;
3265     
3266     	default:
3267     		DEBUG1(printk(KERN_WARNING
3268     		       "%s: zoran_open: flags = 0x%x not yet supported\n",
3269     		       zr->name, flags));
3270     		return -EBUSY;
3271     		break;
3272     
3273     	}
3274     	MOD_INC_USE_COUNT;
3275     	return 0;
3276     }
3277     
3278     static void zoran_close(struct video_device *dev)
3279     {
3280     	struct zoran *zr = (struct zoran *) dev;
3281     	int zero = 0, two = 2;
3282     
3283     	DEBUG1(printk
3284     	       (KERN_INFO "%s: zoran_close, %s pid=[%d]\n", zr->name,
3285     		current->comm, current->pid));
3286     	/* Clean up JPEG process */
3287     
3288     	wake_up_interruptible(&zr->jpg_capq);
3289     	zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
3290     	jpg_fbuffer_free(zr);
3291     	zr->jpg_nbufs = 0;
3292     
3293     	if (zr->user == 1) {	/* Last process */
3294     		/* disable interrupts */
3295     		btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
3296     
3297     #if(DEBUGLEVEL > 1)
3298     		print_interrupts(zr);
3299     #endif
3300     		/* Overlay off */
3301     		wake_up_interruptible(&zr->v4l_capq);
3302     		zr36057_set_memgrab(zr, 0);
3303     		if (zr->v4l_overlay_active)
3304     			zr36057_overlay(zr, 0);
3305     		v4l_fbuffer_free(zr);
3306     
3307     		if (!pass_through) {	/* Switch to color bar */
3308     			set_videobus_enable(zr, 0);
3309     			i2c_control_device(&zr->i2c,
3310     					   I2C_DRIVERID_VIDEODECODER,
3311     					   DECODER_ENABLE_OUTPUT, &zero);
3312     			i2c_control_device(&zr->i2c,
3313     					   I2C_DRIVERID_VIDEOENCODER,
3314     					   ENCODER_SET_INPUT, &two);
3315     			set_videobus_enable(zr, 1);
3316     		}
3317     	}
3318     
3319     	zr->user--;
3320     
3321     	MOD_DEC_USE_COUNT;
3322     	DEBUG2(printk(KERN_INFO "%s: zoran_close done\n", zr->name));
3323     }
3324     
3325     
3326     static long zoran_read(struct video_device *dev, char *buf,
3327     		       unsigned long count, int nonblock)
3328     {
3329     	return -EINVAL;
3330     }
3331     
3332     static long zoran_write(struct video_device *dev, const char *buf,
3333     			unsigned long count, int nonblock)
3334     {
3335     	return -EINVAL;
3336     }
3337     
3338     /*
3339      *   ioctl routine
3340      */
3341     
3342     static int do_zoran_ioctl(struct zoran *zr, unsigned int cmd,
3343     		       void *arg)
3344     {
3345     	switch (cmd) {
3346     
3347     	case VIDIOCGCAP:
3348     		{
3349     			struct video_capability b;
3350     			DEBUG2(printk("%s: ioctl VIDIOCGCAP\n", zr->name));
3351     
3352     			strncpy(b.name, zr->video_dev.name,
3353     				sizeof(b.name));
3354     			b.type =
3355     			    VID_TYPE_CAPTURE | VID_TYPE_OVERLAY |
3356     			    VID_TYPE_CLIPPING | VID_TYPE_FRAMERAM |
3357     			    VID_TYPE_SCALES;
3358     			/* theoretically we could also flag VID_TYPE_SUBCAPTURE
3359     			   but this is not even implemented in the BTTV driver */
3360     
3361     			if (zr->card == DC10 || zr->card == DC10plus) {
3362     				b.channels = 3;	/* composite, svhs, internal */
3363     			} else {
3364     				b.channels = 2;	/* composite, svhs */
3365     			}
3366     			b.audios = 0;
3367     			b.maxwidth = BUZ_MAX_WIDTH;
3368     			b.maxheight = BUZ_MAX_HEIGHT;
3369     			b.minwidth = BUZ_MIN_WIDTH;
3370     			b.minheight = BUZ_MIN_HEIGHT;
3371     			if (copy_to_user(arg, &b, sizeof(b))) {
3372     				return -EFAULT;
3373     			}
3374     			return 0;
3375     		}
3376     		break;
3377     
3378     	case VIDIOCGCHAN:
3379     		{
3380     			struct video_channel v;
3381     
3382     			if (copy_from_user(&v, arg, sizeof(v))) {
3383     				return -EFAULT;
3384     			}
3385     			DEBUG2(printk
3386     			       ("%s: ioctl VIDIOCGCHAN for channel %d\n",
3387     				zr->name, v.channel));
3388     			switch (v.channel) {
3389     			case 0:
3390     				strcpy(v.name, "Composite");
3391     				break;
3392     			case 1:
3393     				strcpy(v.name, "SVHS");
3394     				break;
3395     			case 2:
3396     				if (zr->card == DC10
3397     				    || zr->card == DC10plus) {
3398     					strcpy(v.name, "Internal/comp");
3399     					break;
3400     				}
3401     			default:
3402     				DEBUG1(printk(KERN_ERR
3403     				       "%s: VIDIOCGCHAN on not existing channel %d\n",
3404     				       zr->name, v.channel));
3405     				return -EINVAL;
3406     			}
3407     			v.tuners = 0;
3408     			v.flags = 0;
3409     			v.type = VIDEO_TYPE_CAMERA;
3410     			v.norm = zr->params.norm;
3411     			if (copy_to_user(arg, &v, sizeof(v))) {
3412     				return -EFAULT;
3413     			}
3414     			return 0;
3415     		}
3416     		break;
3417     
3418     		/* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
3419     
3420     		 * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
3421     		 *                                 ^^^^^^^
3422     		 * The famos BTTV driver has it implemented with a struct video_channel argument
3423     		 * and we follow it for compatibility reasons
3424     		 *
3425     		 * BTW: this is the only way the user can set the norm!
3426     		 */
3427     
3428     	case VIDIOCSCHAN:
3429     		{
3430     			struct video_channel v;
3431     			int input;
3432     			int on, res;
3433     			int encoder_norm;
3434     
3435     			if (copy_from_user(&v, arg, sizeof(v))) {
3436     				return -EFAULT;
3437     			}
3438     
3439     			if (zr->codec_mode != BUZ_MODE_IDLE) {
3440     				if (v.norm != zr->params.norm
3441     				    || v.channel != zr->params.input) {
3442     					DEBUG1(printk(KERN_ERR
3443     					       "%s: VIDIOCSCHAN called while the card in capture/playback mode\n",
3444     					       zr->name));
3445     					return -EINVAL;
3446     				} else {
3447     					DEBUG1(printk(BUZ_WARNING
3448     					       "%s: Warning: VIDIOCSCHAN called while the card in capture/playback mode\n",
3449     					       zr->name));
3450     				}
3451     			}
3452     
3453     			DEBUG2(printk
3454     			       ("%s: ioctl VIDIOCSCHAN: channel=%d, norm=%d\n",
3455     				zr->name, v.channel, v.norm));
3456     			switch (v.channel) {
3457     			case 0:
3458     				if (zr->card == BUZ)
3459     					input = 3;
3460     				else
3461     					input = 0;
3462     				break;
3463     			case 1:
3464     				input = 7;
3465     				break;
3466     			case 2:
3467     				if (zr->card == DC10
3468     				    || zr->card == DC10plus) {
3469     					input = 5;
3470     					break;
3471     				}
3472     			default:
3473     				DEBUG1(printk(KERN_ERR
3474     				       "%s: VIDIOCSCHAN on not existing channel %d\n",
3475     				       zr->name, v.channel));
3476     				return -EINVAL;
3477     				break;
3478     			}
3479     
3480     			if (lock_norm && v.norm != zr->params.norm) {
3481     				if (lock_norm > 1) {
3482     					DEBUG1(printk(KERN_WARNING
3483     					       "%s: VIDIOCSCHAN: TV standard is locked, can not switch norm.\n",
3484     					       zr->name));
3485     					return -EINVAL;
3486     				} else {
3487     					DEBUG1(printk(KERN_WARNING
3488     					       "%s: VIDIOCSCHAN: TV standard is locked, norm was not changed.\n",
3489     					       zr->name));
3490     					v.norm = zr->params.norm;
3491     				}
3492     			}
3493     			
3494     			if(v.norm >= 2)
3495     				return -EINVAL;
3496     
3497     			if (!cardnorms[zr->card][v.norm]) {
3498     				DEBUG1(printk(KERN_ERR
3499     				       "%s: VIDIOCSCHAN with not supported norm %d\n",
3500     				       zr->name, v.norm));
3501     				return -EOPNOTSUPP;
3502     				break;
3503     			}
3504     			encoder_norm = v.norm;
3505     
3506     			zr->params.norm = v.norm;
3507     			zr->params.input = v.channel;
3508     			zr->timing = cardnorms[zr->card][zr->params.norm];
3509     
3510     			/* We switch overlay off and on since a change in the norm
3511     			   needs different VFE settings */
3512     
3513     			on = zr->v4l_overlay_active
3514     			    && !zr->v4l_memgrab_active;
3515     			if (on)
3516     				zr36057_overlay(zr, 0);
3517     
3518     			set_videobus_enable(zr, 0);
3519     			i2c_control_device(&zr->i2c,
3520     					   I2C_DRIVERID_VIDEODECODER,
3521     					   DECODER_SET_INPUT, &input);
3522     			i2c_control_device(&zr->i2c,
3523     					   I2C_DRIVERID_VIDEODECODER,
3524     					   DECODER_SET_NORM,
3525     					   &zr->params.norm);
3526     			i2c_control_device(&zr->i2c,
3527     					   I2C_DRIVERID_VIDEOENCODER,
3528     					   ENCODER_SET_NORM,
3529     					   &encoder_norm);
3530     			set_videobus_enable(zr, 1);
3531     
3532     			if (on)
3533     				zr36057_overlay(zr, 1);
3534     
3535     			/* Make sure the changes come into effect */
3536     			res = wait_grab_pending(zr);
3537     			if (res)
3538     				return res;
3539     
3540     			return 0;
3541     		}
3542     		break;
3543     
3544     	case VIDIOCGTUNER:
3545     		{
3546     			DEBUG1(printk(KERN_ERR
3547     			       "%s: ioctl VIDIOCGTUNER not supported\n",
3548     			       zr->name));
3549     			return -EINVAL;
3550     		}
3551     		break;
3552     
3553     	case VIDIOCSTUNER:
3554     		{
3555     			DEBUG1(printk(KERN_ERR
3556     			       "%s: ioctl VIDIOCSTUNER not supported\n",
3557     			       zr->name));
3558     			return -EINVAL;
3559     		}
3560     		break;
3561     
3562     	case VIDIOCGPICT:
3563     		{
3564     			struct video_picture p = zr->picture;
3565     
3566     			DEBUG2(printk
3567     			       ("%s: ioctl VIDIOCGPICT\n", zr->name));
3568     			p.depth = zr->buffer.depth;
3569     			switch (zr->buffer.depth) {
3570     			case 15:
3571     				p.palette = VIDEO_PALETTE_RGB555;
3572     				break;
3573     
3574     			case 16:
3575     				p.palette = VIDEO_PALETTE_RGB565;
3576     				break;
3577     
3578     			case 24:
3579     				p.palette = VIDEO_PALETTE_RGB24;
3580     				break;
3581     
3582     			case 32:
3583     				p.palette = VIDEO_PALETTE_RGB32;
3584     				break;
3585     			}
3586     
3587     			if (copy_to_user(arg, &p, sizeof(p))) {
3588     				return -EFAULT;
3589     			}
3590     			return 0;
3591     		}
3592     		break;
3593     
3594     	case VIDIOCSPICT:
3595     		{
3596     			struct video_picture p;
3597     
3598     			if (copy_from_user(&p, arg, sizeof(p))) {
3599     				return -EFAULT;
3600     			}
3601     			i2c_control_device(&zr->i2c,
3602     					   I2C_DRIVERID_VIDEODECODER,
3603     					   DECODER_SET_PICTURE, &p);
3604     			DEBUG2(printk
3605     			       ("%s: ioctl VIDIOCSPICT bri=%d hue=%d col=%d con=%d dep=%d pal=%d\n",
3606     				zr->name, p.brightness, p.hue, p.colour,
3607     				p.contrast, p.depth, p.palette));
3608     			/* The depth and palette values have no meaning to us,
3609     			   should we return  -EINVAL if they don't fit ? */
3610     			zr->picture = p;
3611     			return 0;
3612     		}
3613     		break;
3614     
3615     	case VIDIOCCAPTURE:
3616     		{
3617     			int v, res;
3618     
3619     			if (copy_from_user(&v, arg, sizeof(v))) {
3620     				return -EFAULT;
3621     			}
3622     			DEBUG2(printk
3623     			       ("%s: ioctl VIDIOCCAPTURE: %d\n", zr->name,
3624     				v));
3625     
3626     			/* If there is nothing to do, return immediatly */
3627     
3628     			if ((v && zr->v4l_overlay_active)
3629     			    || (!v && !zr->v4l_overlay_active))
3630     				return 0;
3631     
3632     			if (v == 0) {
3633     				zr->v4l_overlay_active = 0;
3634     				if (!zr->v4l_memgrab_active)
3635     					zr36057_overlay(zr, 0);
3636     				/* When a grab is running, the video simply won't be switched on any more */
3637     			} else {
3638     				if (!zr->buffer_set || !zr->window_set) {
3639     					DEBUG1(printk(KERN_ERR
3640     					       "%s: VIDIOCCAPTURE: buffer or window not set\n",
3641     					       zr->name));
3642     					return -EINVAL;
3643     				}
3644     				zr->v4l_overlay_active = 1;
3645     				if (!zr->v4l_memgrab_active)
3646     					zr36057_overlay(zr, 1);
3647     				/* When a grab is running, the video will be switched on when grab is finished */
3648     			}
3649     			/* Make sure the changes come into effect */
3650     			res = wait_grab_pending(zr);
3651     			if (res)
3652     				return res;
3653     			return 0;
3654     		}
3655     		break;
3656     
3657     	case VIDIOCGWIN:
3658     		{
3659     			DEBUG2(printk("%s: ioctl VIDIOCGWIN\n", zr->name));
3660     			if (copy_to_user
3661     			    (arg, &zr->window, sizeof(zr->window))) {
3662     				return -EFAULT;
3663     			}
3664     			return 0;
3665     		}
3666     		break;
3667     
3668     	case VIDIOCSWIN:
3669     		{
3670     			struct video_clip *vcp;
3671     			struct video_window vw;
3672     			struct tvnorm *tvn;
3673     			int on, end, res, Wa, Ha;
3674     
3675     			tvn = zr->timing;
3676     
3677     			Wa = tvn->Wa;
3678     			Ha = tvn->Ha;
3679     
3680     			if (copy_from_user(&vw, arg, sizeof(vw))) {
3681     				return -EFAULT;
3682     			}
3683     
3684     			DEBUG2(printk
3685     			       ("%s: ioctl VIDIOCSWIN: x=%d y=%d w=%d h=%d clipcount=%d\n",
3686     				zr->name, vw.x, vw.y, vw.width, vw.height,
3687     				vw.clipcount));
3688     
3689     			if (!zr->buffer_set) {
3690     				DEBUG1(printk(KERN_ERR
3691     				       "%s: VIDIOCSWIN: frame buffer has to be set first\n",
3692     				       zr->name));
3693     				return -EINVAL;
3694     			}
3695     
3696     			/*
3697     			 * The video front end needs 4-byte alinged line sizes, we correct that
3698     			 * silently here if necessary
3699     			 */
3700     
3701     			if (zr->buffer.depth == 15
3702     			    || zr->buffer.depth == 16) {
3703     				end = (vw.x + vw.width) & ~1;	/* round down */
3704     				vw.x = (vw.x + 1) & ~1;	/* round up */
3705     				vw.width = end - vw.x;
3706     			}
3707     
3708     			if (zr->buffer.depth == 24) {
3709     				end = (vw.x + vw.width) & ~3;	/* round down */
3710     				vw.x = (vw.x + 3) & ~3;	/* round up */
3711     				vw.width = end - vw.x;
3712     			}
3713     
3714     			if (vw.width > Wa)
3715     				vw.width = Wa;
3716     			if (vw.height > Ha)
3717     				vw.height = Ha;
3718     
3719     			/* Check for vaild parameters */
3720     			if (vw.width < BUZ_MIN_WIDTH
3721     			    || vw.height < BUZ_MIN_HEIGHT
3722     			    || vw.width > BUZ_MAX_WIDTH
3723     			    || vw.height > BUZ_MAX_HEIGHT) {
3724     				DEBUG1(printk(KERN_ERR
3725     				       "%s: VIDIOCSWIN: width = %d or height = %d invalid\n",
3726     				       zr->name, vw.width, vw.height));
3727     				return -EINVAL;
3728     			}
3729     
3730     			zr->window.x = vw.x;
3731     			zr->window.y = vw.y;
3732     			zr->window.width = vw.width;
3733     			zr->window.height = vw.height;
3734     			zr->window.chromakey = 0;
3735     			zr->window.flags = 0;	// RJ: Is this intended for interlace on/off ?
3736     			zr->window.clips = NULL;
3737     
3738     			/*
3739     			 * If an overlay is running, we have to switch it off
3740     			 * and switch it on again in order to get the new settings in effect.
3741     			 *
3742     			 * We also want to avoid that the overlay mask is written
3743     			 * when an overlay is running.
3744     			 */
3745     
3746     			on = zr->v4l_overlay_active
3747     			    && !zr->v4l_memgrab_active;
3748     			if (on)
3749     				zr36057_overlay(zr, 0);
3750     
3751     			/*
3752     			 *   Write the overlay mask if clips are wanted.
3753     			 */
3754     			 
3755     			if (vw.clipcount > 2048)
3756     				return -EINVAL;
3757     			if (vw.clipcount) {
3758     				vcp =
3759     				    vmalloc(sizeof(struct video_clip) *
3760     					    (vw.clipcount + 4));
3761     				if (vcp == NULL) {
3762     					printk(KERN_ERR
3763     					       "%s: zoran_ioctl: Alloc of clip mask failed\n",
3764     					       zr->name);
3765     					return -ENOMEM;
3766     				}
3767     				if (copy_from_user
3768     				    (vcp, vw.clips,
3769     				     sizeof(struct video_clip) *
3770     				     vw.clipcount)) {
3771     					vfree(vcp);
3772     					return -EFAULT;
3773     				}
3774     				write_overlay_mask(zr, vcp, vw.clipcount);
3775     				vfree(vcp);
3776     			}
3777     			zr->window.clipcount = vw.clipcount;
3778     	
3779     			if (on)
3780     				zr36057_overlay(zr, 1);
3781     			zr->window_set = 1;
3782     
3783     			/* Make sure the changes come into effect */
3784     			res = wait_grab_pending(zr);
3785     			if (res)
3786     				return res;
3787     
3788     			return 0;
3789     		}
3790     		break;
3791     
3792     	case VIDIOCGFBUF:
3793     		{
3794     			DEBUG2(printk
3795     			       ("%s: ioctl VIDIOCGFBUF\n", zr->name));
3796     			if (copy_to_user
3797     			    (arg, &zr->buffer, sizeof(zr->buffer))) {
3798     				return -EFAULT;
3799     			}
3800     			return 0;
3801     		}
3802     		break;
3803     
3804     	case VIDIOCSFBUF:
3805     		{
3806     			struct video_buffer v;
3807     
3808     			/* RJ: Isn't this too restrictive? As long as the user doesn't set
3809     			   the base address it shouldn't be too dangerous */
3810     
3811     			if (!capable(CAP_SYS_ADMIN)) {
3812     				DEBUG1(printk(KERN_ERR
3813     				       "%s: Only the superuser may issue VIDIOCSFBUF ioctl\n",
3814     				       zr->name));
3815     				return -EPERM;
3816     			}
3817     			if (copy_from_user(&v, arg, sizeof(v))) {
3818     				return -EFAULT;
3819     			}
3820     			DEBUG2(printk
3821     			       ("%s: ioctl VIDIOCSFBUF: base=0x%x w=%d h=%d depth=%d bpl=%d\n",
3822     				zr->name, (u32) v.base, v.width, v.height,
3823     				v.depth, v.bytesperline));
3824     			if (zr->v4l_overlay_active) {
3825     				/* Has the user gotten crazy ... ? */
3826     				DEBUG1(printk(KERN_ERR
3827     				       "%s: VIDIOCSFBUF not allowed when overlay active\n",
3828     				       zr->name));
3829     				return -EINVAL;
3830     			}
3831     			if (v.depth != 15 && v.depth != 16 && v.depth != 24
3832     			    && v.depth != 32) {
3833     				DEBUG1(printk(KERN_ERR
3834     				       "%s: VIDIOCSFBUF: depth=%d not supported\n",
3835     				       zr->name, v.depth));
3836     				return -EINVAL;
3837     			}
3838     			if (v.height <= 0 || v.width <= 0
3839     			    || v.bytesperline <= 0) {
3840     				DEBUG1(printk(KERN_ERR
3841     				       "%s: VIDIOCSFBUF: invalid height/width/bpl value\n",
3842     				       zr->name));
3843     				return -EINVAL;
3844     			}
3845     			if (v.bytesperline & 3) {
3846     				DEBUG1(printk(KERN_ERR
3847     				       "%s: VIDIOCSFBUF: bytesperline must be 4-byte aligned\n",
3848     				       zr->name));
3849     				return -EINVAL;
3850     			}
3851     			if (v.base) {
3852     				zr->buffer.base =
3853     				    (void *) ((unsigned long) v.base & ~3);
3854     			}
3855     			zr->buffer.height = v.height;
3856     			zr->buffer.width = v.width;
3857     			zr->buffer.depth = v.depth;
3858     			zr->buffer.bytesperline = v.bytesperline;
3859     
3860     			if (zr->buffer.base)
3861     				zr->buffer_set = 1;
3862     			zr->window_set = 0;	/* The user should set new window parameters */
3863     			return 0;
3864     		}
3865     		break;
3866     
3867     		/* RJ: what is VIDIOCKEY intended to do ??? */
3868     
3869     	case VIDIOCKEY:
3870     		{
3871     			/* Will be handled higher up .. */
3872     			DEBUG2(printk("%s: ioctl VIDIOCKEY\n", zr->name));
3873     			return 0;
3874     		}
3875     		break;
3876     
3877     	case VIDIOCGFREQ:
3878     		{
3879     			DEBUG1(printk(KERN_ERR
3880     			       "%s: ioctl VIDIOCGFREQ not supported\n",
3881     			       zr->name));
3882     			return -EINVAL;
3883     		}
3884     		break;
3885     
3886     	case VIDIOCSFREQ:
3887     		{
3888     			DEBUG1(printk(KERN_ERR
3889     			       "%s: ioctl VIDIOCSFREQ not supported\n",
3890     			       zr->name));
3891     			return -EINVAL;
3892     		}
3893     		break;
3894     
3895     	case VIDIOCGAUDIO:
3896     		{
3897     			DEBUG1(printk(KERN_ERR
3898     			       "%s: ioctl VIDIOCGAUDIO not supported\n",
3899     			       zr->name));
3900     			return -EINVAL;
3901     		}
3902     		break;
3903     
3904     	case VIDIOCSAUDIO:
3905     		{
3906     			DEBUG1(printk(KERN_ERR
3907     			       "%s: ioctl VIDIOCSAUDIO not supported\n",
3908     			       zr->name));
3909     			return -EINVAL;
3910     		}
3911     		break;
3912     
3913     	case VIDIOCSYNC:
3914     		{
3915     			int v;
3916     
3917     			if (copy_from_user(&v, arg, sizeof(v))) {
3918     				return -EFAULT;
3919     			}
3920     			DEBUG3(printk
3921     			       ("%s: ioctl VIDIOCSYNC %d\n", zr->name, v));
3922     			return v4l_sync(zr, v);
3923     		}
3924     		break;
3925     
3926     	case VIDIOCMCAPTURE:
3927     		{
3928     			struct video_mmap vm;
3929     
3930     			if (copy_from_user
3931     			    ((void *) &vm, (void *) arg, sizeof(vm))) {
3932     				return -EFAULT;
3933     			}
3934     			DEBUG2(printk
3935     			       ("%s: ioctl VIDIOCMCAPTURE frame=%d geom=%dx%d fmt=%d\n",
3936     				zr->name, vm.frame, vm.width, vm.height,
3937     				vm.format));
3938     			return v4l_grab(zr, &vm);
3939     		}
3940     		break;
3941     
3942     	case VIDIOCGMBUF:
3943     		{
3944     			struct video_mbuf vm;
3945     			int i;
3946     
3947     			DEBUG2(printk
3948     			       ("%s: ioctl VIDIOCGMBUF\n", zr->name));
3949     			vm.size = v4l_nbufs * v4l_bufsize;
3950     			vm.frames = v4l_nbufs;
3951     			for (i = 0; i < v4l_nbufs; i++) {
3952     				vm.offsets[i] = i * v4l_bufsize;
3953     			}
3954     
3955     			/* The next mmap will map the V4L buffers */
3956     			zr->map_mjpeg_buffers = 0;
3957     
3958     			if (copy_to_user(arg, &vm, sizeof(vm))) {
3959     				return -EFAULT;
3960     			}
3961     			return 0;
3962     		}
3963     		break;
3964     
3965     	case VIDIOCGUNIT:
3966     		{
3967     			struct video_unit vu;
3968     
3969     			DEBUG2(printk
3970     			       ("%s: ioctl VIDIOCGUNIT\n", zr->name));
3971     			vu.video = zr->video_dev.minor;
3972     			vu.vbi = VIDEO_NO_UNIT;
3973     			vu.radio = VIDEO_NO_UNIT;
3974     			vu.audio = VIDEO_NO_UNIT;
3975     			vu.teletext = VIDEO_NO_UNIT;
3976     			if (copy_to_user(arg, &vu, sizeof(vu))) {
3977     				return -EFAULT;
3978     			}
3979     			return 0;
3980     		}
3981     		break;
3982     
3983     		/*
3984     		 * RJ: In principal we could support subcaptures for V4L grabbing.
3985     		 *     Not even the famous BTTV driver has them, however.
3986     		 *     If there should be a strong demand, one could consider
3987     		 *     to implement them.
3988     		 */
3989     	case VIDIOCGCAPTURE:
3990     		{
3991     			DEBUG1(printk(KERN_ERR
3992     			       "%s: ioctl VIDIOCGCAPTURE not supported\n",
3993     			       zr->name));
3994     			return -EINVAL;
3995     		}
3996     		break;
3997     
3998     	case VIDIOCSCAPTURE:
3999     		{
4000     			DEBUG1(printk(KERN_ERR
4001     			       "%s: ioctl VIDIOCSCAPTURE not supported\n",
4002     			       zr->name));
4003     			return -EINVAL;
4004     		}
4005     		break;
4006     
4007     	case BUZIOC_G_PARAMS:
4008     		{
4009     			DEBUG2(printk
4010     			       ("%s: ioctl BUZIOC_G_PARAMS\n", zr->name));
4011     
4012     			if (copy_to_user
4013     			    (arg, &(zr->params), sizeof(zr->params))) {
4014     				return -EFAULT;
4015     			}
4016     			return 0;
4017     		}
4018     		break;
4019     
4020     	case BUZIOC_S_PARAMS:
4021     		{
4022     			struct zoran_params bp;
4023     			/* int input, on; */
4024     
4025     			if (zr->codec_mode != BUZ_MODE_IDLE) {
4026     				DEBUG1(printk(KERN_ERR
4027     				       "%s: BUZIOC_S_PARAMS called but Buz in capture/playback mode\n",
4028     				       zr->name));
4029     				return -EINVAL;
4030     			}
4031     
4032     			if (copy_from_user(&bp, arg, sizeof(bp))) {
4033     				return -EFAULT;
4034     			}
4035     			DEBUG2(printk
4036     			       ("%s: ioctl BUZIOC_S_PARAMS\n", zr->name));
4037     
4038     			/* Check the params first before overwriting our internal values */
4039     
4040     			if (zoran_check_params(zr, &bp))
4041     				return -EINVAL;
4042     
4043     			zr->params = bp;
4044     
4045     			/* Make changes of input and norm go into effect immediatly */
4046     
4047     			/* We switch overlay off and on since a change in the norm
4048     			   needs different VFE settings */
4049     
4050     			if (copy_to_user(arg, &bp, sizeof(bp))) {
4051     				return -EFAULT;
4052     			}
4053     			return 0;
4054     		}
4055     		break;
4056     
4057     	case BUZIOC_REQBUFS:
4058     		{
4059     			struct zoran_requestbuffers br;
4060     
4061     			if (zr->jpg_buffers_allocated) {
4062     				DEBUG1(printk(KERN_ERR
4063     				       "%s: BUZIOC_REQBUFS: buffers allready allocated\n",
4064     				       zr->name));
4065     				return -EINVAL;
4066     			}
4067     			if (copy_from_user(&br, arg, sizeof(br))) {
4068     				return -EFAULT;
4069     			}
4070     			DEBUG2(printk
4071     			       ("%s: ioctl BUZIOC_REQBUFS count = %lu size=%lu\n",
4072     				zr->name, br.count, br.size));
4073     
4074     			/* Enforce reasonable lower and upper limits */
4075     			if (br.count < 4)
4076     				br.count = 4;	/* Could be choosen smaller */
4077     			if (br.count > BUZ_MAX_FRAME)
4078     				br.count = BUZ_MAX_FRAME;
4079     			br.size = PAGE_ALIGN(br.size);
4080     			if (br.size < 8192)
4081     				br.size = 8192;	/* Arbitrary */
4082     			/* br.size is limited by 1 page for the stat_com tables to a Maximum of 2 MB */
4083     			if (br.size > (512 * 1024))
4084     				br.size = (512 * 1024);	/* 512 K should be enough */
4085     			if (zr->need_contiguous
4086     			    && br.size > MAX_KMALLOC_MEM)
4087     				br.size = MAX_KMALLOC_MEM;
4088     
4089     			zr->jpg_nbufs = br.count;
4090     			zr->jpg_bufsize = br.size;
4091     
4092     			if (jpg_fbuffer_alloc(zr))
4093     				return -ENOMEM;
4094     
4095     			/* The next mmap will map the MJPEG buffers */
4096     			zr->map_mjpeg_buffers = 1;
4097     
4098     			if (copy_to_user(arg, &br, sizeof(br))) {
4099     				return -EFAULT;
4100     			}
4101     			return 0;
4102     		}
4103     		break;
4104     
4105     	case BUZIOC_QBUF_CAPT:
4106     		{
4107     			int nb;
4108     
4109     			if (copy_from_user
4110     			    ((void *) &nb, (void *) arg, sizeof(int))) {
4111     				return -EFAULT;
4112     			}
4113     			DEBUG4(printk
4114     			       ("%s: ioctl BUZIOC_QBUF_CAPT %d\n",
4115     				zr->name, nb));
4116     			return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_COMPRESS);
4117     		}
4118     		break;
4119     
4120     	case BUZIOC_QBUF_PLAY:
4121     		{
4122     			int nb;
4123     
4124     			if (copy_from_user
4125     			    ((void *) &nb, (void *) arg, sizeof(int))) {
4126     				return -EFAULT;
4127     			}
4128     			DEBUG4(printk
4129     			       ("%s: ioctl BUZIOC_QBUF_PLAY %d\n",
4130     				zr->name, nb));
4131     			return jpg_qbuf(zr, nb,
4132     					BUZ_MODE_MOTION_DECOMPRESS);
4133     		}
4134     		break;
4135     
4136     	case BUZIOC_SYNC:
4137     		{
4138     			struct zoran_sync bs;
4139     			int res;
4140     
4141     			DEBUG4(printk
4142     			       ("%s: ioctl BUZIOC_SYNC\n", zr->name));
4143     			res = jpg_sync(zr, &bs);
4144     			if (copy_to_user(arg, &bs, sizeof(bs))) {
4145     				return -EFAULT;
4146     			}
4147     			return res;
4148     		}
4149     		break;
4150     
4151     	case BUZIOC_G_STATUS:
4152     		{
4153     			struct zoran_status bs;
4154     			int norm, input, status;
4155     			unsigned long timeout;
4156     
4157     			if (zr->codec_mode != BUZ_MODE_IDLE) {
4158     				DEBUG1(printk(KERN_ERR
4159     				       "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
4160     				       zr->name));
4161     				return -EINVAL;
4162     			}
4163     
4164     			if (copy_from_user(&bs, arg, sizeof(bs))) {
4165     				return -EFAULT;
4166     			}
4167     			DEBUG2(printk
4168     			       ("%s: ioctl BUZIOC_G_STATUS\n", zr->name));
4169     
4170     			switch (bs.input) {
4171     			case 0:
4172     				if (zr->card == BUZ)
4173     					input = 3;
4174     				else
4175     					input = 0;
4176     				break;
4177     			case 1:
4178     				input = 7;
4179     				break;
4180     			default:
4181     				DEBUG1(printk(KERN_ERR
4182     				       "%s: BUZIOC_G_STATUS on not existing input %d\n",
4183     				       zr->name, bs.input));
4184     				return -EINVAL;
4185     			}
4186     
4187     			/* Set video norm to VIDEO_MODE_AUTO */
4188     
4189     			norm = VIDEO_MODE_AUTO;
4190     			set_videobus_enable(zr, 0);
4191     			i2c_control_device(&zr->i2c,
4192     					   I2C_DRIVERID_VIDEODECODER,
4193     					   DECODER_SET_INPUT, &input);
4194     			i2c_control_device(&zr->i2c,
4195     					   I2C_DRIVERID_VIDEODECODER,
4196     					   DECODER_SET_NORM, &norm);
4197     			set_videobus_enable(zr, 1);
4198     
4199     			/* sleep 1 second */
4200     
4201     			timeout = jiffies + 1 * HZ;
4202     			while (jiffies < timeout)
4203     				schedule();
4204     
4205     			/* Get status of video decoder */
4206     
4207     			i2c_control_device(&zr->i2c,
4208     					   I2C_DRIVERID_VIDEODECODER,
4209     					   DECODER_GET_STATUS, &status);
4210     			bs.signal = (status & DECODER_STATUS_GOOD) ? 1 : 0;
4211     
4212     			if (status & DECODER_STATUS_NTSC)
4213     				bs.norm = VIDEO_MODE_NTSC;
4214     			else if (status & DECODER_STATUS_SECAM)
4215     				bs.norm = VIDEO_MODE_SECAM;
4216     			else
4217     				bs.norm = VIDEO_MODE_PAL;
4218     
4219     			bs.color = (status & DECODER_STATUS_COLOR) ? 1 : 0;
4220     
4221     			/* restore previous input and norm */
4222     			if (zr->card == BUZ)
4223     				input = zr->params.input == 0 ? 3 : 7;
4224     			else
4225     				input = zr->params.input == 0 ? 0 : 7;
4226     			set_videobus_enable(zr, 0);
4227     			i2c_control_device(&zr->i2c,
4228     					   I2C_DRIVERID_VIDEODECODER,
4229     					   DECODER_SET_INPUT, &input);
4230     			i2c_control_device(&zr->i2c,
4231     					   I2C_DRIVERID_VIDEODECODER,
4232     					   DECODER_SET_NORM,
4233     					   &zr->params.norm);
4234     			set_videobus_enable(zr, 1);
4235     
4236     			if (copy_to_user(arg, &bs, sizeof(bs))) {
4237     				return -EFAULT;
4238     			}
4239     			return 0;
4240     		}
4241     		break;
4242     
4243     	default:
4244     		DEBUG1(printk
4245     		       ("%s: UNKNOWN ioctl cmd: 0x%x\n", zr->name, cmd));
4246     		return -ENOIOCTLCMD;
4247     	}
4248     	return 0;
4249     }
4250     
4251     static int zoran_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
4252     {
4253     	struct zoran *zr = (struct zoran *) dev;
4254     	int err;
4255     	
4256     	down(&zr->sem);
4257     	err = do_zoran_ioctl(zr, cmd, arg);
4258     	up(&zr->sem);
4259     	
4260     	return err;
4261     }
4262     
4263     /*
4264      *   This maps the buffers to user space.
4265      *
4266      *   Depending on the state of zr->map_mjpeg_buffers
4267      *   the V4L or the MJPEG buffers are mapped
4268      *
4269      */
4270     
4271     static int do_zoran_mmap(struct zoran *zr, const char *adr,
4272     		      unsigned long size)
4273     {
4274     	unsigned long start = (unsigned long) adr;
4275     	unsigned long page, pos, todo, fraglen;
4276     	int i, j;
4277     
4278     	DEBUG2(printk
4279     	       (KERN_INFO "%s: mmap at 0x%08lx, size %lu\n", zr->name,
4280     		start, size));
4281     	if (zr->map_mjpeg_buffers) {
4282     		/* Map the MJPEG buffers */
4283     
4284     		if (!zr->jpg_buffers_allocated) {
4285     			DEBUG1(printk(KERN_ERR
4286     			       "%s: zoran_mmap(MJPEG): buffers not yet allocated\n",
4287     			       zr->name));
4288     			return -ENOMEM;
4289     		}
4290     
4291     		if (size > zr->jpg_nbufs * zr->jpg_bufsize) {
4292     			DEBUG1(printk(KERN_ERR
4293     			       "%s: zoran_mmap(MJPEG): Max size is %lu - you wanted %lu\n",
4294     			       zr->name, zr->jpg_nbufs * zr->jpg_bufsize,
4295     			       size));
4296     			return -EINVAL;
4297     		}
4298     
4299     		if (size != zr->jpg_nbufs * zr->jpg_bufsize)
4300     			DEBUG1(printk(KERN_WARNING
4301     			       "%s: zoran_mmap(MJPEG): Expected %lu - you wanted %lu\n",
4302     			       zr->name, zr->jpg_nbufs * zr->jpg_bufsize,
4303     			       size));
4304     
4305     		for (i = 0; i < zr->jpg_nbufs; i++) {
4306     			for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
4307     				fraglen =
4308     				    (zr->jpg_gbuf[i].
4309     				     frag_tab[2 * j + 1] & ~1) << 1;
4310     				todo = size;
4311     				if (todo > fraglen)
4312     					todo = fraglen;
4313     				pos =
4314     				    (unsigned long) zr->jpg_gbuf[i].
4315     				    frag_tab[2 * j];
4316     				page = virt_to_phys(bus_to_virt(pos));	/* should just be pos on i386 */
4317     				if (remap_page_range
4318     				    (start, page, todo, PAGE_SHARED)) {
4319     					printk(KERN_ERR
4320     					       "%s: zoran_mmap(V4L): remap_page_range failed\n",
4321     					       zr->name);
4322     					return -EAGAIN;
4323     				}
4324     				size -= todo;
4325     				start += todo;
4326     				if (size == 0)
4327     					break;
4328     				if (zr->jpg_gbuf[i].
4329     				    frag_tab[2 * j + 1] & 1)
4330     					break;	/* was last fragment */
4331     			}
4332     			if (size == 0)
4333     				break;
4334     		}
4335     	} else {
4336     		/* Map the V4L buffers */
4337     
4338     		if (size > v4l_nbufs * v4l_bufsize) {
4339     			DEBUG1(printk(KERN_ERR
4340     			       "%s: zoran_mmap(V4L): Max size is %d - you wanted %ld\n",
4341     			       zr->name, v4l_nbufs * v4l_bufsize, size));
4342     			return -EINVAL;
4343     		}
4344     
4345     		if (size != v4l_nbufs * v4l_bufsize)
4346     			DEBUG1(printk(KERN_WARNING
4347     			       "%s: zoran_mmap(V4L): Expected %d - you wanted %ld\n",
4348     			       zr->name, v4l_nbufs * v4l_bufsize, size));
4349     
4350     		for (i = 0; i < v4l_nbufs; i++) {
4351     			todo = size;
4352     			if (todo > v4l_bufsize)
4353     				todo = v4l_bufsize;
4354     			page = zr->v4l_gbuf[i].fbuffer_phys;
4355     			DEBUG2(printk
4356     			       ("V4L remap page range %d 0x%lx %ld to 0x%lx\n",
4357     				i, page, todo, start));
4358     			if (remap_page_range
4359     			    (start, page, todo, PAGE_SHARED)) {
4360     				printk(KERN_ERR
4361     				       "%s: zoran_mmap(V4L): remap_page_range failed\n",
4362     				       zr->name);
4363     				return -EAGAIN;
4364     			}
4365     			size -= todo;
4366     			start += todo;
4367     			if (size == 0)
4368     				break;
4369     		}
4370     	}
4371     	return 0;
4372     }
4373     
4374     static int zoran_mmap(struct video_device *dev, const char *adr,
4375     		      unsigned long size)
4376     {
4377     	int err;
4378     	struct zoran *zr = (struct zoran *) dev;
4379     	
4380     	down(&zr->sem);
4381     	err = do_zoran_mmap(zr, adr, size);
4382     	up(&zr->sem);
4383     		
4384     	return err;
4385     }
4386     
4387     static int zoran_init_done(struct video_device *dev)
4388     {
4389     	return 0;
4390     }
4391     
4392     static struct video_device zoran_template = {
4393     	THIS_MODULE,
4394     	ZORAN_NAME,
4395     	VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CLIPPING |
4396     	    VID_TYPE_FRAMERAM | VID_TYPE_SCALES | VID_TYPE_SUBCAPTURE,
4397     	ZORAN_HARDWARE,
4398     	zoran_open,
4399     	zoran_close,
4400     	zoran_read,
4401     	zoran_write,
4402     	NULL,
4403     	zoran_ioctl,
4404     	zoran_mmap,
4405     	zoran_init_done,
4406     	NULL,
4407     	0,
4408     	0
4409     };
4410     
4411     /*
4412      * initialize video front end
4413      */
4414     static void zr36057_init_vfe(struct zoran *zr)
4415     {
4416     	u32 reg;
4417     	reg = btread(ZR36057_VFESPFR);
4418     	reg |= ZR36057_VFESPFR_LittleEndian;
4419     	reg &= ~ZR36057_VFESPFR_VCLKPol;
4420     	reg |= ZR36057_VFESPFR_ExtFl;
4421     	reg |= ZR36057_VFESPFR_TopField;
4422     	btwrite(reg, ZR36057_VFESPFR);
4423     	reg = btread(ZR36057_VDCR);
4424     	if (triton || zr->revision <= 1)
4425     		reg &= ~ZR36057_VDCR_Triton;
4426     	else
4427     		reg |= ZR36057_VDCR_Triton;
4428     	btwrite(reg, ZR36057_VDCR);
4429     }
4430     
4431     static void test_interrupts(struct zoran *zr)
4432     {
4433     	int timeout, icr;
4434     
4435     	clear_interrupt_counters(zr);
4436     	zr->testing = 1;
4437     	icr = btread(ZR36057_ICR);
4438     	btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
4439     	timeout = interruptible_sleep_on_timeout(&zr->test_q, 1 * HZ);
4440     	btwrite(0, ZR36057_ICR);
4441     	btwrite(0x78000000, ZR36057_ISR);
4442     	zr->testing = 0;
4443     	printk(KERN_INFO "%s: Testing interrupts...\n", zr->name);
4444     	if (timeout) {
4445     		printk(": time spent: %d\n", 1 * HZ - timeout);
4446     	}
4447     	print_interrupts(zr);
4448     	btwrite(icr, ZR36057_ICR);
4449     }
4450     
4451     static int zr36057_init(int i)
4452     {
4453     	struct zoran *zr = &zoran[i];
4454     	unsigned long mem;
4455     	unsigned mem_needed;
4456     	int j;
4457     	int two = 2;
4458     	int zero = 0;
4459     
4460     	printk(KERN_INFO "%s: Initializing card[%d], zr=%x\n", zr->name, i, (int) zr);
4461     
4462     	/* default setup of all parameters which will persist beetween opens */
4463     
4464     	zr->user = 0;
4465     
4466     	init_waitqueue_head(&zr->v4l_capq);
4467     	init_waitqueue_head(&zr->jpg_capq);
4468     	init_waitqueue_head(&zr->test_q);
4469     
4470     	zr->map_mjpeg_buffers = 0;	/* Map V4L buffers by default */
4471     
4472     	zr->jpg_nbufs = 0;
4473     	zr->jpg_bufsize = 0;
4474     	zr->jpg_buffers_allocated = 0;
4475     
4476     	zr->buffer_set = 0;	/* Flag if frame buffer has been set */
4477     	zr->buffer.base = (void *) vidmem;
4478     	zr->buffer.width = 0;
4479     	zr->buffer.height = 0;
4480     	zr->buffer.depth = 0;
4481     	zr->buffer.bytesperline = 0;
4482     
4483     	zr->params.norm = default_norm = (default_norm < 3 ? default_norm : VIDEO_MODE_PAL);	/* Avoid nonsense settings from user */
4484     	if (!(zr->timing = cardnorms[zr->card][zr->params.norm])) {
4485     		printk(KERN_WARNING
4486     		       "%s: default TV statdard not supported by hardware. PAL will be used.\n",
4487     		       zr->name);
4488     		zr->params.norm = VIDEO_MODE_PAL;
4489     		zr->timing = cardnorms[zr->card][zr->params.norm];
4490     	}
4491     	zr->params.input = default_input = (default_input ? 1 : 0);	/* Avoid nonsense settings from user */
4492     	zr->video_interlace = 0;
4493     
4494     	/* Should the following be reset at every open ? */
4495     
4496     	zr->picture.colour = 32768;
4497     	zr->picture.brightness = 32768;
4498     	zr->picture.hue = 32768;
4499     	zr->picture.contrast = 32768;
4500     	zr->picture.whiteness = 0;
4501     	zr->picture.depth = 0;
4502     	zr->picture.palette = 0;
4503     
4504     	for (j = 0; j < VIDEO_MAX_FRAME; j++) {
4505     		zr->v4l_gbuf[i].fbuffer = 0;
4506     		zr->v4l_gbuf[i].fbuffer_phys = 0;
4507     		zr->v4l_gbuf[i].fbuffer_bus = 0;
4508     	}
4509     
4510     	zr->stat_com = 0;
4511     
4512     	/* default setup (will be repeated at every open) */
4513     
4514     	zoran_open_init_params(zr);
4515     
4516     	/* allocate memory *before* doing anything to the hardware in case allocation fails */
4517     
4518     	/* STAT_COM table and overlay mask */
4519     
4520     	mem_needed = (BUZ_NUM_STAT_COM + ((BUZ_MAX_WIDTH + 31) / 32) * BUZ_MAX_HEIGHT) * 4;
4521     	mem = (unsigned long) kmalloc(mem_needed, GFP_KERNEL);
4522     	if (!mem) {
4523     		printk(KERN_ERR "%s: zr36057_init: kmalloc (STAT_COM + ovl.mask) failed\n", zr->name);
4524     		return -ENOMEM;
4525     	}
4526     	memset((void *) mem, 0, mem_needed);
4527     
4528     	zr->stat_com = (u32 *) mem;
4529     	for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
4530     		zr->stat_com[j] = 1;	/* mark as unavailable to zr36057 */
4531     	}
4532     	zr->overlay_mask = (u32 *) (mem + BUZ_NUM_STAT_COM * 4);
4533     
4534     	/* Initialize zr->jpg_gbuf */
4535     
4536     	for (j = 0; j < BUZ_MAX_FRAME; j++) {
4537     		zr->jpg_gbuf[j].frag_tab = 0;
4538     		zr->jpg_gbuf[j].frag_tab_bus = 0;
4539     		zr->jpg_gbuf[j].state = BUZ_STATE_USER;
4540     		zr->jpg_gbuf[j].bs.frame = j;
4541     	}
4542     
4543     	/*
4544     	 *   Now add the template and register the device unit.
4545     	 */
4546     	memcpy(&zr->video_dev, &zoran_template, sizeof(zoran_template));
4547     	strcpy(zr->video_dev.name, zr->name);
4548     	if (video_register_device(&zr->video_dev, VFL_TYPE_GRABBER, video_nr) < 0) {
4549     		i2c_unregister_bus(&zr->i2c);
4550     		kfree((void *) zr->stat_com);
4551     		return -1;
4552     	}
4553     
4554     	/* Enable bus-mastering */
4555     	pci_set_master(zr->pci_dev);
4556     
4557     	if (zr->card == BUZ)
4558     		j = zr->params.input == 0 ? 3 : 7;
4559     	else
4560     		j = zr->params.input == 0 ? 0 : 7;
4561     	set_videobus_enable(zr, 0);
4562     	i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
4563     			   DECODER_SET_INPUT, &j);
4564     	i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
4565     			   DECODER_SET_NORM, &zr->params.norm);
4566     	i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
4567     			   ENCODER_SET_NORM, &zr->params.norm);
4568     	set_videobus_enable(zr, 1);
4569     
4570     	/* toggle JPEG codec sleep to sync PLL */
4571     	zr36060_sleep(zr, 1);
4572     	zr36060_sleep(zr, 0);
4573     
4574     	/* set individual interrupt enables (without GIRQ1)
4575     	   but don't global enable until zoran_open() */
4576     
4577     	//btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ1, ZR36057_ICR);  // SW
4578     	// It looks like using only JPEGRepIRQEn is not always reliable,
4579     	// may be when JPEG codec crashes it won't generate IRQ? So,
4580     	btwrite(IRQ_MASK, ZR36057_ICR);	// Enable Vsync interrupts too. SM
4581     
4582     	zr36057_init_vfe(zr);
4583     
4584     	zr->zoran_proc = NULL;
4585     	zr->initialized = 1;
4586     
4587     	zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
4588     #if (DEBUGLEVEL > 2)
4589     	detect_guest_activity(zr);
4590     #endif
4591     	test_interrupts(zr);
4592     	btwrite(IRQ_MASK, ZR36057_ICR);	// Enable Vsync interrupts too. SM
4593     	if (!pass_through) {
4594     		set_videobus_enable(zr, 0);
4595     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
4596     				   DECODER_ENABLE_OUTPUT, &zero);
4597     		i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
4598     				   ENCODER_SET_INPUT, &two);
4599     		set_videobus_enable(zr, 1);
4600     	}
4601     	return 0;
4602     }
4603     
4604     #include "zoran_procfs.c"
4605     
4606     static void release_dc10(void)
4607     {
4608     	u8 command;
4609     	int i;
4610     	struct zoran *zr;
4611     
4612     	for (i = 0; i < zoran_num; i++) {
4613     		zr = &zoran[i];
4614     
4615     		if (!zr->initialized)
4616     			continue;
4617     
4618     		/* unregister i2c_bus */
4619     		i2c_unregister_bus((&zr->i2c));
4620     
4621     		/* disable PCI bus-mastering */
4622     		pci_read_config_byte(zr->pci_dev, PCI_COMMAND, &command);
4623     		command &= ~PCI_COMMAND_MASTER;
4624     		pci_write_config_byte(zr->pci_dev, PCI_COMMAND, command);
4625     
4626     		/* put chip into reset */
4627     		btwrite(0, ZR36057_SPGPPCR);
4628     
4629     		free_irq(zr->pci_dev->irq, zr);
4630     
4631     		/* unmap and free memory */
4632     
4633     		kfree((void *) zr->stat_com);
4634     
4635     		zoran_proc_cleanup(i);
4636     		iounmap(zr->zr36057_mem);
4637     
4638     		video_unregister_device(&zr->video_dev);
4639     	}
4640     }
4641     
4642     /*
4643      *   Scan for a Buz card (actually for the PCI contoler ZR36057),
4644      *   request the irq and map the io memory
4645      */
4646     
4647     static int find_zr36057(void)
4648     {
4649     	unsigned char latency, need_latency;
4650     	struct zoran *zr;
4651     	struct pci_dev *dev = NULL;
4652     	int result;
4653     
4654     	zoran_num = 0;
4655     
4656     	while (zoran_num < BUZ_MAX
4657     	       && (dev =
4658     		   pci_find_device(PCI_VENDOR_ID_ZORAN,
4659     				   PCI_DEVICE_ID_ZORAN_36057,
4660     				   dev)) != NULL) {
4661     		zr = &zoran[zoran_num];
4662     		zr->pci_dev = dev;
4663     		zr->zr36057_mem = NULL;
4664     		zr->id = zoran_num;
4665     		sprintf(zr->name, "MJPEG[%u]", zr->id);
4666     
4667     		spin_lock_init(&zr->lock);
4668     		init_MUTEX(&zr->sem);
4669     
4670     		if (pci_enable_device(dev))
4671     			continue;
4672     
4673     		zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
4674     		pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION,
4675     				     &zr->revision);
4676     		if (zr->revision < 2) {
4677     			printk(KERN_INFO
4678     			       "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
4679     			       zr->name, zr->revision, zr->pci_dev->irq,
4680     			       zr->zr36057_adr);
4681     		} else {
4682     			unsigned short ss_vendor_id, ss_id;
4683     			ss_vendor_id = zr->pci_dev->subsystem_vendor;
4684     			ss_id = zr->pci_dev->subsystem_device;
4685     			printk(KERN_INFO
4686     			       "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
4687     			       zr->name, zr->revision, zr->pci_dev->irq,
4688     			       zr->zr36057_adr);
4689     			printk(KERN_INFO
4690     			       "%s: subsystem vendor=0x%04x id=0x%04x\n",
4691     			       zr->name, ss_vendor_id, ss_id);
4692     		}
4693     
4694     		zr->zr36057_mem = ioremap_nocache(zr->zr36057_adr, 0x1000);
4695     		if (!zr->zr36057_mem) {
4696     			printk(KERN_ERR "%s: ioremap failed\n", zr->name);
4697     			/* XXX handle error */
4698     		}
4699     
4700     		result = request_irq(zr->pci_dev->irq, zoran_irq, SA_SHIRQ | SA_INTERRUPT, zr->name, (void *) zr);
4701     		if (result < 0) {
4702     			if (result == -EINVAL) {
4703     				printk(KERN_ERR
4704     				       "%s: Bad irq number or handler\n",
4705     				       zr->name);
4706     			} else if (result == -EBUSY) {
4707     				printk(KERN_ERR
4708     				       "%s: IRQ %d busy, change your PnP config in BIOS\n",
4709     				       zr->name, zr->pci_dev->irq);
4710     			} else {
4711     				printk(KERN_ERR
4712     				       "%s: Can't assign irq, error code %d\n",
4713     				       zr->name, result);
4714     			}
4715     			iounmap(zr->zr36057_mem);
4716     			continue;
4717     		}
4718     
4719     		/* set PCI latency timer */
4720     		pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, &latency);
4721     		need_latency = zr->revision > 1 ? 32 : 48;
4722     		if (latency != need_latency) {
4723     			printk(KERN_INFO "%s: Changing PCI latency from %d to %d.\n",  zr->name, latency, need_latency);
4724     			pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,  need_latency);
4725     		}
4726     
4727     		btwrite(0, ZR36057_SPGPPCR);
4728     		mdelay(1);
4729     		btwrite(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
4730     		mdelay(1);
4731     
4732     		/* assert P_Reset */
4733     		btwrite(0, ZR36057_JPC);
4734     
4735     		/* set up GPIO direction - all output */
4736     		btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
4737     		btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
4738     
4739     		/* i2c */
4740     		memcpy(&zr->i2c, &zoran_i2c_bus_template,
4741     		       sizeof(struct i2c_bus));
4742     		strcpy(zr->i2c.name, zr->name);
4743     		zr->i2c.data = zr;
4744     		printk(KERN_INFO "%s: Initializing i2c bus...\n", zr->name);
4745     		if (i2c_register_bus(&zr->i2c) < 0) {
4746     			/* put chip into reset */
4747     			btwrite(0, ZR36057_SPGPPCR);
4748     			free_irq(zr->pci_dev->irq, zr);
4749     			iounmap(zr->zr36057_mem);
4750     			printk(KERN_ERR "%s: Can't initialize i2c bus\n", zr->name);
4751     			continue;
4752     		}
4753     
4754     		if (zr->card != DC10 && zr->card != DC10plus
4755     		    && zr->card != LML33 && zr->card != BUZ) {
4756     			/* unregister i2c_bus */
4757     			i2c_unregister_bus((&zr->i2c));
4758     			/* put chip into reset */
4759     			btwrite(0, ZR36057_SPGPPCR);
4760     			free_irq(zr->pci_dev->irq, zr);
4761     			iounmap(zr->zr36057_mem);
4762     			printk(KERN_ERR "%s: Card not supported\n",
4763     			       zr->name);
4764     			continue;
4765     		}
4766     		printk(KERN_INFO "%s card detected\n", zr->name);
4767     		if (zr->card == LML33) {
4768     			GPIO(zr, 2, 1);	// Set Composite input/output
4769     		}
4770     
4771     		/* reset JPEG codec */
4772     		zr36060_sleep(zr, 1);
4773     		zr36060_reset(zr);
4774     
4775     		/* video bus enabled */
4776     
4777     		/* display codec revision */
4778     		if (zr36060_read_8(zr, 0x022) == 0x33) {
4779     			printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)\n",
4780     			       zr->name, zr36060_read_8(zr, 0x023));
4781     		} else {
4782     			/* unregister i2c_bus */
4783     			i2c_unregister_bus((&zr->i2c));
4784     			/* put chip into reset */
4785     			btwrite(0, ZR36057_SPGPPCR);
4786     			free_irq(zr->pci_dev->irq, zr);
4787     			iounmap(zr->zr36057_mem);
4788     			printk(KERN_ERR "%s: Zoran ZR36060 not found\n",
4789     			       zr->name);
4790     			continue;
4791     		}
4792     
4793     		zoran_num++;
4794     	}
4795     	if (zoran_num == 0) {
4796     		printk(KERN_INFO "No known MJPEG cards found.\n");
4797     	}
4798     	return zoran_num;
4799     }
4800     
4801     static void handle_chipset(void)
4802     {
4803     	if(pci_pci_problems & PCIPCI_FAIL)
4804     		printk(KERN_WARNING "Chipset may not support reliable PCI-PCI DMA.\n");
4805     
4806     	if(pci_pci_problems & PCIPCI_TRITON)
4807     	{
4808     		printk(KERN_WARNING "Enabling Triton support.\n");
4809     		triton = 1;
4810     	}
4811     	
4812     	if(pci_pci_problems & PCIPCI_NATOMA)
4813     	{
4814     		printk(KERN_WARNING "Enabling Natoma workaround.\n");
4815     		natoma = 1;
4816     	}
4817     }
4818     
4819     static int init_dc10_cards(void)
4820     {
4821     	int i;
4822     
4823     	memset(zoran, 0, sizeof(zoran));
4824     	printk(KERN_INFO
4825     	       "Zoran ZR36060 + ZR36057/67 MJPEG board driver version %d.%d\n",
4826     	       MAJOR_VERSION, MINOR_VERSION);
4827     
4828     	/* Look for cards */
4829     
4830     	if (find_zr36057() < 0) {
4831     		return -EIO;
4832     	}
4833     	if (zoran_num == 0)
4834     		return 0;	//-ENXIO;
4835     
4836     	printk(KERN_INFO "MJPEG: %d card(s) found\n", zoran_num);
4837     
4838     	/* check the parameters we have been given, adjust if necessary */
4839     
4840     	if (v4l_nbufs < 0)
4841     		v4l_nbufs = 0;
4842     	if (v4l_nbufs > VIDEO_MAX_FRAME)
4843     		v4l_nbufs = VIDEO_MAX_FRAME;
4844     	/* The user specfies the in KB, we want them in byte (and page aligned) */
4845     	v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
4846     	if (v4l_bufsize < 32768)
4847     		v4l_bufsize = 32768;
4848     	/* 2 MB is arbitrary but sufficient for the maximum possible images */
4849     	if (v4l_bufsize > 2048 * 1024)
4850     		v4l_bufsize = 2048 * 1024;
4851     
4852     	printk(KERN_INFO "MJPEG: using %d V4L buffers of size %d KB\n",
4853     	       v4l_nbufs, v4l_bufsize >> 10);
4854     
4855     	/* Use parameter for vidmem or try to find a video card */
4856     
4857     	if (vidmem) {
4858     		printk(KERN_INFO
4859     		       "MJPEG: Using supplied video memory base address @ 0x%lx\n",
4860     		       vidmem);
4861     	}
4862     	/* check if we have a Triton or Natome chipset */
4863     
4864     	handle_chipset();
4865     
4866     	/* take care of Natoma chipset and a revision 1 zr36057 */
4867     
4868     	for (i = 0; i < zoran_num; i++) {
4869     		if (natoma && zoran[i].revision <= 1) {
4870     			zoran[i].need_contiguous = 1;
4871     			printk(KERN_INFO
4872     			       "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
4873     			       zoran[i].name);
4874     		} else {
4875     			zoran[i].need_contiguous = 0;
4876     		}
4877     	}
4878     
4879     	/* initialize the Buzs */
4880     
4881     	/* We have to know which ones must be released if an error occurs */
4882     	for (i = 0; i < zoran_num; i++)
4883     		zoran[i].initialized = 0;
4884     
4885     	for (i = 0; i < zoran_num; i++) {
4886     		if (zr36057_init(i) < 0) {
4887     			release_dc10();
4888     			return -EIO;
4889     		}
4890     		zoran_proc_init(i);
4891     	}
4892     
4893     	return 0;
4894     }
4895     
4896     static void unload_dc10_cards(void)
4897     {
4898     	release_dc10();
4899     }
4900     
4901     
4902     module_init(init_dc10_cards);
4903     module_exit(unload_dc10_cards);
4904