ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / zoran_device.c
1 /*
2  * Zoran zr36057/zr36067 PCI controller driver, for the
3  * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4  * Media Labs LML33/LML33R10.
5  *
6  * This part handles device access (PCI/I2C/codec/...)
7  * 
8  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9  *
10  * Currently maintained by:
11  *   Ronald Bultje    <rbultje@ronald.bitfreak.net>
12  *   Laurent Pinchart <laurent.pinchart@skynet.be>
13  *   Mailinglist      <mjpeg-users@lists.sf.net>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include <linux/config.h>
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/vmalloc.h>
35
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-algo-bit.h>
40 #include <linux/videodev.h>
41 #include <linux/spinlock.h>
42 #include <linux/sem.h>
43
44 #include <linux/pci.h>
45 #include <linux/video_decoder.h>
46 #include <linux/video_encoder.h>
47 #include <linux/delay.h>
48
49 #include <asm/io.h>
50
51 #include "videocodec.h"
52 #include "zoran.h"
53 #include "zoran_device.h"
54
55 #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | \
56                    ZR36057_ISR_GIRQ1 | \
57                    ZR36057_ISR_JPEGRepIRQ )
58
59 extern const struct zoran_format zoran_formats[];
60 extern const int zoran_num_formats;
61
62 extern int *zr_debug;
63
64 #define dprintk(num, format, args...) \
65         do { \
66                 if (*zr_debug >= num) \
67                         printk(format, ##args); \
68         } while (0)
69
70 static int lml33dpath = 0;      /* 1 will use digital path in capture
71                                  * mode instead of analog. It can be
72                                  * used for picture adjustments using
73                                  * tool like xawtv while watching image
74                                  * on TV monitor connected to the output.
75                                  * However, due to absence of 75 Ohm
76                                  * load on Bt819 input, there will be
77                                  * some image imperfections */
78
79 MODULE_PARM(lml33dpath, "i");
80 MODULE_PARM_DESC(lml33dpath,
81                  "Use digital path capture mode (on LML33 cards)");
82
83 /*
84  * General Purpose I/O and Guest bus access
85  */
86
87 /*
88  * This is a bit tricky. When a board lacks a GPIO function, the corresponding
89  * GPIO bit number in the card_info structure is set to 0.
90  */
91
92 void
93 GPIO (struct zoran *zr,
94       int           bit,
95       unsigned int  value)
96 {
97         u32 reg;
98         u32 mask;
99
100         /* Make sure the bit number is legal
101          * A bit number of -1 (lacking) gives a mask of 0,
102          * making it harmless */
103         mask = (1 << (24 + bit)) & 0xff000000;
104         reg = btread(ZR36057_GPPGCR1) & ~mask;
105         if (value) {
106                 reg |= mask;
107         }
108         btwrite(reg, ZR36057_GPPGCR1);
109         udelay(1);
110 }
111
112 /*
113  * Wait til post office is no longer busy
114  */
115
116 int
117 post_office_wait (struct zoran *zr)
118 {
119         u32 por;
120
121 //      while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
122         while ((por = btread(ZR36057_POR)) & ZR36057_POR_POPen) {
123                 /* wait for something to happen */
124         }
125         if ((por & ZR36057_POR_POTime) && !zr->card.gws_not_connected) {
126                 /* In LML33/BUZ \GWS line is not connected, so it has always timeout set */
127                 dprintk(1, KERN_INFO "%s: pop timeout %08x\n", ZR_DEVNAME(zr),
128                         por);
129                 return -1;
130         }
131
132         return 0;
133 }
134
135 int
136 post_office_write (struct zoran *zr,
137                    unsigned int  guest,
138                    unsigned int  reg,
139                    unsigned int  value)
140 {
141         u32 por;
142
143         por =
144             ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) |
145             ((reg & 7) << 16) | (value & 0xFF);
146         btwrite(por, ZR36057_POR);
147
148         return post_office_wait(zr);
149 }
150
151 int
152 post_office_read (struct zoran *zr,
153                   unsigned int  guest,
154                   unsigned int  reg)
155 {
156         u32 por;
157
158         por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
159         btwrite(por, ZR36057_POR);
160         if (post_office_wait(zr) < 0) {
161                 return -1;
162         }
163
164         return btread(ZR36057_POR) & 0xFF;
165 }
166
167 /*
168  * detect guests
169  */
170
171 static void
172 dump_guests (struct zoran *zr)
173 {
174         if (*zr_debug > 2) {
175                 int i, guest[8];
176
177                 for (i = 1; i < 8; i++) {       // Don't read jpeg codec here
178                         guest[i] = post_office_read(zr, i, 0);
179                 }
180
181                 printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
182
183                 for (i = 1; i < 8; i++) {
184                         printk(" 0x%02x", guest[i]);
185                 }
186                 printk("\n");
187         }
188 }
189
190 static inline unsigned long
191 get_time (void)
192 {
193         struct timeval tv;
194
195         do_gettimeofday(&tv);
196         return (1000000 * tv.tv_sec + tv.tv_usec);
197 }
198
199 void
200 detect_guest_activity (struct zoran *zr)
201 {
202         int timeout, i, j, res, guest[8], guest0[8], change[8][3];
203         unsigned long t0, t1;
204
205         dump_guests(zr);
206         printk(KERN_INFO "%s: Detecting guests activity, please wait...\n",
207                ZR_DEVNAME(zr));
208         for (i = 1; i < 8; i++) {       // Don't read jpeg codec here
209                 guest0[i] = guest[i] = post_office_read(zr, i, 0);
210         }
211
212         timeout = 0;
213         j = 0;
214         t0 = get_time();
215         while (timeout < 10000) {
216                 udelay(10);
217                 timeout++;
218                 for (i = 1; (i < 8) && (j < 8); i++) {
219                         res = post_office_read(zr, i, 0);
220                         if (res != guest[i]) {
221                                 t1 = get_time();
222                                 change[j][0] = (t1 - t0);
223                                 t0 = t1;
224                                 change[j][1] = i;
225                                 change[j][2] = res;
226                                 j++;
227                                 guest[i] = res;
228                         }
229                 }
230                 if (j >= 8)
231                         break;
232         }
233         printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
234
235         for (i = 1; i < 8; i++) {
236                 printk(" 0x%02x", guest0[i]);
237         }
238         printk("\n");
239         if (j == 0) {
240                 printk(KERN_INFO "%s: No activity detected.\n", ZR_DEVNAME(zr));
241                 return;
242         }
243         for (i = 0; i < j; i++) {
244                 printk(KERN_INFO "%s: %6d: %d => 0x%02x\n", ZR_DEVNAME(zr),
245                        change[i][0], change[i][1], change[i][2]);
246         }
247 }
248
249 /*
250  * JPEG Codec access
251  */
252
253 void
254 jpeg_codec_sleep (struct zoran *zr,
255                   int           sleep)
256 {
257         GPIO(zr, zr->card.gpio[GPIO_JPEG_SLEEP], !sleep);
258         if (!sleep) {
259                 dprintk(3,
260                         KERN_DEBUG
261                         "%s: jpeg_codec_sleep() - wake GPIO=0x%08x\n",
262                         ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
263                 udelay(500);
264         } else {
265                 dprintk(3,
266                         KERN_DEBUG
267                         "%s: jpeg_codec_sleep() - sleep GPIO=0x%08x\n",
268                         ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
269                 udelay(2);
270         }
271 }
272
273 int
274 jpeg_codec_reset (struct zoran *zr)
275 {
276         /* Take the codec out of sleep */
277         jpeg_codec_sleep(zr, 0);
278
279         if (zr->card.gpcs[GPCS_JPEG_RESET] != 0xff) {
280                 post_office_write(zr, zr->card.gpcs[GPCS_JPEG_RESET], 0,
281                                   0);
282                 udelay(2);
283         } else {
284                 GPIO(zr, zr->card.gpio[GPIO_JPEG_RESET], 0);
285                 udelay(2);
286                 GPIO(zr, zr->card.gpio[GPIO_JPEG_RESET], 1);
287                 udelay(2);
288         }
289
290         return 0;
291 }
292
293 /*
294  *   Set the registers for the size we have specified. Don't bother
295  *   trying to understand this without the ZR36057 manual in front of
296  *   you [AC].
297  *
298  *   PS: The manual is free for download in .pdf format from
299  *   www.zoran.com - nicely done those folks.
300  */
301
302 static void
303 zr36057_adjust_vfe (struct zoran          *zr,
304                     enum zoran_codec_mode  mode)
305 {
306         u32 reg;
307
308         switch (mode) {
309         case BUZ_MODE_MOTION_DECOMPRESS:
310                 btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
311                 reg = btread(ZR36057_VFEHCR);
312                 if ((reg & (1 << 10)) && zr->card.type != LML33R10) {
313                         reg += ((1 << 10) | 1);
314                 }
315                 btwrite(reg, ZR36057_VFEHCR);
316                 break;
317         case BUZ_MODE_MOTION_COMPRESS:
318         case BUZ_MODE_IDLE:
319         default:
320                 if (zr->norm == VIDEO_MODE_NTSC ||
321                     (zr->card.type == LML33R10 &&
322                      zr->norm == VIDEO_MODE_PAL))
323                         btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
324                 else
325                         btor(ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
326                 reg = btread(ZR36057_VFEHCR);
327                 if (!(reg & (1 << 10)) && zr->card.type != LML33R10) {
328                         reg -= ((1 << 10) | 1);
329                 }
330                 btwrite(reg, ZR36057_VFEHCR);
331                 break;
332         }
333 }
334
335 /*
336  * set geometry
337  */
338
339 static void
340 zr36057_set_vfe (struct zoran              *zr,
341                  int                        video_width,
342                  int                        video_height,
343                  const struct zoran_format *format)
344 {
345         struct tvnorm *tvn;
346         unsigned HStart, HEnd, VStart, VEnd;
347         unsigned DispMode;
348         unsigned VidWinWid, VidWinHt;
349         unsigned hcrop1, hcrop2, vcrop1, vcrop2;
350         unsigned Wa, We, Ha, He;
351         unsigned X, Y, HorDcm, VerDcm;
352         u32 reg;
353         unsigned mask_line_size;
354
355         tvn = zr->timing;
356
357         Wa = tvn->Wa;
358         Ha = tvn->Ha;
359
360         dprintk(2, KERN_INFO "%s: set_vfe() - width = %d, height = %d\n",
361                 ZR_DEVNAME(zr), video_width, video_height);
362
363         if (zr->norm != VIDEO_MODE_PAL &&
364             zr->norm != VIDEO_MODE_NTSC &&
365             zr->norm != VIDEO_MODE_SECAM) {
366                 dprintk(1,
367                         KERN_ERR "%s: set_vfe() - norm = %d not valid\n",
368                         ZR_DEVNAME(zr), zr->norm);
369                 return;
370         }
371         if (video_width < BUZ_MIN_WIDTH ||
372             video_height < BUZ_MIN_HEIGHT ||
373             video_width > Wa || video_height > Ha) {
374                 dprintk(1, KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n",
375                         ZR_DEVNAME(zr), video_width, video_height);
376                 return;
377         }
378
379         /**** zr36057 ****/
380
381         /* horizontal */
382         VidWinWid = video_width;
383         X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
384         We = (VidWinWid * 64) / X;
385         HorDcm = 64 - X;
386         hcrop1 = 2 * ((tvn->Wa - We) / 4);
387         hcrop2 = tvn->Wa - We - hcrop1;
388         HStart = tvn->HStart ? tvn->HStart : 1;
389         /* (Ronald) Original comment:
390          * "| 1 Doesn't have any effect, tested on both a DC10 and a DC10+"
391          * this is false. It inverses chroma values on the LML33R10 (so Cr
392          * suddenly is shown as Cb and reverse, really cool effect if you
393          * want to see blue faces, not useful otherwise). So don't use |1.
394          * However, the DC10 has '0' as HStart, but does need |1, so we
395          * use a dirty check...
396          */
397         HEnd = HStart + tvn->Wa - 1;
398         HStart += hcrop1;
399         HEnd -= hcrop2;
400         reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
401             | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
402         if (zr->card.vfe_pol.hsync_pol)
403                 reg |= ZR36057_VFEHCR_HSPol;
404         btwrite(reg, ZR36057_VFEHCR);
405
406         /* Vertical */
407         DispMode = !(video_height > BUZ_MAX_HEIGHT / 2);
408         VidWinHt = DispMode ? video_height : video_height / 2;
409         Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
410         He = (VidWinHt * 64) / Y;
411         VerDcm = 64 - Y;
412         vcrop1 = (tvn->Ha / 2 - He) / 2;
413         vcrop2 = tvn->Ha / 2 - He - vcrop1;
414         VStart = tvn->VStart;
415         VEnd = VStart + tvn->Ha / 2;    // - 1; FIXME SnapShot times out with -1 in 768*576 on the DC10 - LP
416         VStart += vcrop1;
417         VEnd -= vcrop2;
418         reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
419             | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
420         if (zr->card.vfe_pol.vsync_pol)
421                 reg |= ZR36057_VFEVCR_VSPol;
422         btwrite(reg, ZR36057_VFEVCR);
423
424         /* scaler and pixel format */
425         reg = 0;
426         reg |= (HorDcm << ZR36057_VFESPFR_HorDcm);
427         reg |= (VerDcm << ZR36057_VFESPFR_VerDcm);
428         reg |= (DispMode << ZR36057_VFESPFR_DispMode);
429         if (format->palette != VIDEO_PALETTE_YUV422)
430                 reg |= ZR36057_VFESPFR_LittleEndian;
431         /* RJ: I don't know, why the following has to be the opposite
432          * of the corresponding ZR36060 setting, but only this way
433          * we get the correct colors when uncompressing to the screen  */
434         //reg |= ZR36057_VFESPFR_VCLKPol; /**/
435         /* RJ: Don't know if that is needed for NTSC also */
436         if (zr->norm != VIDEO_MODE_NTSC)
437                 reg |= ZR36057_VFESPFR_ExtFl;   // NEEDED!!!!!!! Wolfgang
438         reg |= ZR36057_VFESPFR_TopField;
439         switch (format->palette) {
440
441         case VIDEO_PALETTE_YUV422:
442                 reg |= ZR36057_VFESPFR_YUV422;
443                 break;
444
445         case VIDEO_PALETTE_RGB555:
446                 reg |= ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ErrDif;
447                 break;
448
449         case VIDEO_PALETTE_RGB565:
450                 reg |= ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ErrDif;
451                 break;
452
453         case VIDEO_PALETTE_RGB24:
454                 reg |= ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_Pack24;
455                 break;
456
457         case VIDEO_PALETTE_RGB32:
458                 reg |= ZR36057_VFESPFR_RGB888;
459                 break;
460
461         default:
462                 dprintk(1,
463                         KERN_INFO "%s: set_vfe() - unknown color_fmt=%x\n",
464                         ZR_DEVNAME(zr), format->palette);
465                 return;
466
467         }
468         if (HorDcm >= 48) {
469                 reg |= 3 << ZR36057_VFESPFR_HFilter;    /* 5 tap filter */
470         } else if (HorDcm >= 32) {
471                 reg |= 2 << ZR36057_VFESPFR_HFilter;    /* 4 tap filter */
472         } else if (HorDcm >= 16) {
473                 reg |= 1 << ZR36057_VFESPFR_HFilter;    /* 3 tap filter */
474         }
475         btwrite(reg, ZR36057_VFESPFR);
476
477         /* display configuration */
478         reg = (16 << ZR36057_VDCR_MinPix)
479             | (VidWinHt << ZR36057_VDCR_VidWinHt)
480             | (VidWinWid << ZR36057_VDCR_VidWinWid);
481         if (pci_pci_problems & PCIPCI_TRITON)
482                 // || zr->revision < 1) // Revision 1 has also Triton support
483                 reg &= ~ZR36057_VDCR_Triton;
484         else
485                 reg |= ZR36057_VDCR_Triton;
486         btwrite(reg, ZR36057_VDCR);
487
488         /* (Ronald) don't write this if overlay_mask = NULL */
489         if (zr->overlay_mask) {
490                 /* Write overlay clipping mask data, but don't enable overlay clipping */
491                 /* RJ: since this makes only sense on the screen, we use 
492                  * zr->overlay_settings.width instead of video_width */
493
494                 mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
495                 reg = virt_to_bus(zr->overlay_mask);
496                 btwrite(reg, ZR36057_MMTR);
497                 reg = virt_to_bus(zr->overlay_mask + mask_line_size);
498                 btwrite(reg, ZR36057_MMBR);
499                 reg =
500                     mask_line_size - (zr->overlay_settings.width +
501                                       31) / 32;
502                 if (DispMode == 0)
503                         reg += mask_line_size;
504                 reg <<= ZR36057_OCR_MaskStride;
505                 btwrite(reg, ZR36057_OCR);
506         }
507
508         zr36057_adjust_vfe(zr, zr->codec_mode);
509 }
510
511 /*
512  * Switch overlay on or off
513  */
514
515 void
516 zr36057_overlay (struct zoran *zr,
517                  int           on)
518 {
519         u32 reg;
520
521         if (on) {
522                 /* do the necessary settings ... */
523                 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);       /* switch it off first */
524
525                 zr36057_set_vfe(zr,
526                                 zr->overlay_settings.width,
527                                 zr->overlay_settings.height,
528                                 zr->overlay_settings.format);
529
530                 /* Start and length of each line MUST be 4-byte aligned.
531                  * This should be allready checked before the call to this routine.
532                  * All error messages are internal driver checking only! */
533
534                 /* video display top and bottom registers */
535                 reg = (u32) zr->buffer.base +
536                     zr->overlay_settings.x *
537                     ((zr->overlay_settings.format->depth + 7) / 8) +
538                     zr->overlay_settings.y *
539                     zr->buffer.bytesperline;
540                 btwrite(reg, ZR36057_VDTR);
541                 if (reg & 3)
542                         dprintk(1,
543                                 KERN_ERR
544                                 "%s: zr36057_overlay() - video_address not aligned\n",
545                                 ZR_DEVNAME(zr));
546                 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
547                         reg += zr->buffer.bytesperline;
548                 btwrite(reg, ZR36057_VDBR);
549
550                 /* video stride, status, and frame grab register */
551                 reg = zr->buffer.bytesperline -
552                     zr->overlay_settings.width *
553                     ((zr->overlay_settings.format->depth + 7) / 8);
554                 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
555                         reg += zr->buffer.bytesperline;
556                 if (reg & 3)
557                         dprintk(1,
558                                 KERN_ERR
559                                 "%s: zr36057_overlay() - video_stride not aligned\n",
560                                 ZR_DEVNAME(zr));
561                 reg = (reg << ZR36057_VSSFGR_DispStride);
562                 reg |= ZR36057_VSSFGR_VidOvf;   /* clear overflow status */
563                 btwrite(reg, ZR36057_VSSFGR);
564
565                 /* Set overlay clipping */
566                 if (zr->overlay_settings.clipcount > 0)
567                         btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
568
569                 /* ... and switch it on */
570                 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
571         } else {
572                 /* Switch it off */
573                 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
574         }
575 }
576
577 /*
578  * The overlay mask has one bit for each pixel on a scan line,
579  *  and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
580  */
581
582 void
583 write_overlay_mask (struct file       *file,
584                     struct video_clip *vp,
585                     int                count)
586 {
587         struct zoran_fh *fh = file->private_data;
588         struct zoran *zr = fh->zr;
589         unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
590         u32 *mask;
591         int x, y, width, height;
592         unsigned i, j, k;
593         u32 reg;
594
595         /* fill mask with one bits */
596         memset(fh->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
597         reg = 0;
598
599         for (i = 0; i < count; ++i) {
600                 /* pick up local copy of clip */
601                 x = vp[i].x;
602                 y = vp[i].y;
603                 width = vp[i].width;
604                 height = vp[i].height;
605
606                 /* trim clips that extend beyond the window */
607                 if (x < 0) {
608                         width += x;
609                         x = 0;
610                 }
611                 if (y < 0) {
612                         height += y;
613                         y = 0;
614                 }
615                 if (x + width > fh->overlay_settings.width) {
616                         width = fh->overlay_settings.width - x;
617                 }
618                 if (y + height > fh->overlay_settings.height) {
619                         height = fh->overlay_settings.height - y;
620                 }
621
622                 /* ignore degenerate clips */
623                 if (height <= 0) {
624                         continue;
625                 }
626                 if (width <= 0) {
627                         continue;
628                 }
629
630                 /* apply clip for each scan line */
631                 for (j = 0; j < height; ++j) {
632                         /* reset bit for each pixel */
633                         /* this can be optimized later if need be */
634                         mask = fh->overlay_mask + (y + j) * mask_line_size;
635                         for (k = 0; k < width; ++k) {
636                                 mask[(x + k) / 32] &=
637                                     ~((u32) 1 << (x + k) % 32);
638                         }
639                 }
640         }
641 }
642
643 /* Enable/Disable uncompressed memory grabbing of the 36057 */
644
645 void
646 zr36057_set_memgrab (struct zoran *zr,
647                      int           mode)
648 {
649         if (mode) {
650                 if (btread(ZR36057_VSSFGR) &
651                     (ZR36057_VSSFGR_SnapShot | ZR36057_VSSFGR_FrameGrab))
652                         dprintk(1,
653                                 KERN_WARNING
654                                 "%s: zr36057_set_memgrab(1) with SnapShot or FrameGrab on!?\n",
655                                 ZR_DEVNAME(zr));
656
657                 /* switch on VSync interrupts */
658                 btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
659                 btor(zr->card.vsync_int, ZR36057_ICR);  // SW
660
661                 /* enable SnapShot */
662                 btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
663
664                 /* Set zr36057 video front end  and enable video */
665                 zr36057_set_vfe(zr, zr->v4l_settings.width,
666                                 zr->v4l_settings.height,
667                                 zr->v4l_settings.format);
668
669                 zr->v4l_memgrab_active = 1;
670         } else {
671                 zr->v4l_memgrab_active = 0;
672
673                 /* switch off VSync interrupts */
674                 btand(~zr->card.vsync_int, ZR36057_ICR);        // SW
675
676                 /* reenable grabbing to screen if it was running */
677                 if (zr->v4l_overlay_active) {
678                         zr36057_overlay(zr, 1);
679                 } else {
680                         btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
681                         btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
682                 }
683         }
684 }
685
686 int
687 wait_grab_pending (struct zoran *zr)
688 {
689         unsigned long flags;
690
691         /* wait until all pending grabs are finished */
692
693         if (!zr->v4l_memgrab_active)
694                 return 0;
695
696         while (zr->v4l_pend_tail != zr->v4l_pend_head) {
697                 interruptible_sleep_on(&zr->v4l_capq);
698                 if (signal_pending(current))
699                         return -ERESTARTSYS;
700         }
701
702         spin_lock_irqsave(&zr->spinlock, flags);
703         zr36057_set_memgrab(zr, 0);
704         spin_unlock_irqrestore(&zr->spinlock, flags);
705
706         return 0;
707 }
708
709 /*****************************************************************************
710  *                                                                           *
711  *  Set up the Buz-specific MJPEG part                                       *
712  *                                                                           *
713  *****************************************************************************/
714
715 static inline void
716 set_frame (struct zoran *zr,
717            int           val)
718 {
719         GPIO(zr, zr->card.gpio[GPIO_JPEG_FRAME], val);
720 }
721
722 static void
723 set_videobus_dir (struct zoran *zr,
724                   int           val)
725 {
726         switch (zr->card.type) {
727         case LML33:
728         case LML33R10:
729                 if (lml33dpath == 0)
730                         GPIO(zr, 5, val);
731                 else
732                         GPIO(zr, 5, 1);
733                 break;
734         default:
735                 GPIO(zr, zr->card.gpio[GPIO_VID_DIR],
736                      zr->card.gpio_pol[GPIO_VID_DIR] ? !val : val);
737                 break;
738         }
739 }
740
741 static void
742 init_jpeg_queue (struct zoran *zr)
743 {
744         int i;
745
746         /* re-initialize DMA ring stuff */
747         zr->jpg_que_head = 0;
748         zr->jpg_dma_head = 0;
749         zr->jpg_dma_tail = 0;
750         zr->jpg_que_tail = 0;
751         zr->jpg_seq_num = 0;
752         zr->JPEG_error = 0;
753         zr->num_errors = 0;
754         zr->jpg_err_seq = 0;
755         zr->jpg_err_shift = 0;
756         zr->jpg_queued_num = 0;
757         for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
758                 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
759         }
760         for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
761                 zr->stat_com[i] = 1;    /* mark as unavailable to zr36057 */
762         }
763 }
764
765 static void
766 zr36057_set_jpg (struct zoran          *zr,
767                  enum zoran_codec_mode  mode)
768 {
769         struct tvnorm *tvn;
770         u32 reg;
771
772         tvn = zr->timing;
773
774         /* assert P_Reset, disable code transfer, deassert Active */
775         btwrite(0, ZR36057_JPC);
776
777         /* MJPEG compression mode */
778         switch (mode) {
779
780         case BUZ_MODE_MOTION_COMPRESS:
781         default:
782                 reg = ZR36057_JMC_MJPGCmpMode;
783                 break;
784
785         case BUZ_MODE_MOTION_DECOMPRESS:
786                 reg = ZR36057_JMC_MJPGExpMode;
787                 reg |= ZR36057_JMC_SyncMstr;
788                 /* RJ: The following is experimental - improves the output to screen */
789                 //if(zr->jpg_settings.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
790                 break;
791
792         case BUZ_MODE_STILL_COMPRESS:
793                 reg = ZR36057_JMC_JPGCmpMode;
794                 break;
795
796         case BUZ_MODE_STILL_DECOMPRESS:
797                 reg = ZR36057_JMC_JPGExpMode;
798                 break;
799
800         }
801         reg |= ZR36057_JMC_JPG;
802         if (zr->jpg_settings.field_per_buff == 1)
803                 reg |= ZR36057_JMC_Fld_per_buff;
804         btwrite(reg, ZR36057_JMC);
805
806         /* vertical */
807         btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
808         reg = (6 << ZR36057_VSP_VsyncSize) |
809               (tvn->Ht << ZR36057_VSP_FrmTot);
810         btwrite(reg, ZR36057_VSP);
811         reg = ((zr->jpg_settings.img_y + tvn->VStart) << ZR36057_FVAP_NAY) |
812               (zr->jpg_settings.img_height << ZR36057_FVAP_PAY);
813         btwrite(reg, ZR36057_FVAP);
814
815         /* horizontal */
816         if (zr->card.vfe_pol.hsync_pol)
817                 btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
818         else
819                 btand(~ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);           
820         reg = ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) |
821               (tvn->Wt << ZR36057_HSP_LineTot);
822         btwrite(reg, ZR36057_HSP);
823         reg = ((zr->jpg_settings.img_x +
824                 tvn->HStart + 4) << ZR36057_FHAP_NAX) |
825               (zr->jpg_settings.img_width << ZR36057_FHAP_PAX);
826         btwrite(reg, ZR36057_FHAP);
827
828         /* field process parameters */
829         if (zr->jpg_settings.odd_even)
830                 reg = ZR36057_FPP_Odd_Even;
831         else
832                 reg = 0;
833
834         btwrite(reg, ZR36057_FPP);
835
836         /* Set proper VCLK Polarity, else colors will be wrong during playback */
837         //btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
838
839         /* code base address */
840         reg = virt_to_bus(zr->stat_com);
841         btwrite(reg, ZR36057_JCBA);
842
843         /* FIFO threshold (FIFO is 160. double words) */
844         /* NOTE: decimal values here */
845         switch (mode) {
846
847         case BUZ_MODE_STILL_COMPRESS:
848         case BUZ_MODE_MOTION_COMPRESS:
849                 if (zr->card.type != BUZ)
850                         reg = 140;
851                 else
852                         reg = 60;
853                 break;
854
855         case BUZ_MODE_STILL_DECOMPRESS:
856         case BUZ_MODE_MOTION_DECOMPRESS:
857                 reg = 20;
858                 break;
859
860         default:
861                 reg = 80;
862                 break;
863
864         }
865         btwrite(reg, ZR36057_JCFT);
866         zr36057_adjust_vfe(zr, mode);
867
868 }
869
870 void
871 print_interrupts (struct zoran *zr)
872 {
873         int res, noerr = 0;
874
875         printk(KERN_INFO "%s: interrupts received:", ZR_DEVNAME(zr));
876         if ((res = zr->field_counter) < -1 || res > 1) {
877                 printk(" FD:%d", res);
878         }
879         if ((res = zr->intr_counter_GIRQ1) != 0) {
880                 printk(" GIRQ1:%d", res);
881                 noerr++;
882         }
883         if ((res = zr->intr_counter_GIRQ0) != 0) {
884                 printk(" GIRQ0:%d", res);
885                 noerr++;
886         }
887         if ((res = zr->intr_counter_CodRepIRQ) != 0) {
888                 printk(" CodRepIRQ:%d", res);
889                 noerr++;
890         }
891         if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
892                 printk(" JPEGRepIRQ:%d", res);
893                 noerr++;
894         }
895         if (zr->JPEG_max_missed) {
896                 printk(" JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
897                        zr->JPEG_min_missed);
898         }
899         if (zr->END_event_missed) {
900                 printk(" ENDs missed: %d", zr->END_event_missed);
901         }
902         //if (zr->jpg_queued_num) {
903         printk(" queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
904                zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
905         //}
906         if (!noerr) {
907                 printk(": no interrupts detected.");
908         }
909         printk("\n");
910 }
911
912 void
913 clear_interrupt_counters (struct zoran *zr)
914 {
915         zr->intr_counter_GIRQ1 = 0;
916         zr->intr_counter_GIRQ0 = 0;
917         zr->intr_counter_CodRepIRQ = 0;
918         zr->intr_counter_JPEGRepIRQ = 0;
919         zr->field_counter = 0;
920         zr->IRQ1_in = 0;
921         zr->IRQ1_out = 0;
922         zr->JPEG_in = 0;
923         zr->JPEG_out = 0;
924         zr->JPEG_0 = 0;
925         zr->JPEG_1 = 0;
926         zr->END_event_missed = 0;
927         zr->JPEG_missed = 0;
928         zr->JPEG_max_missed = 0;
929         zr->JPEG_min_missed = 0x7fffffff;
930 }
931
932 static u32
933 count_reset_interrupt (struct zoran *zr)
934 {
935         u32 isr;
936
937         if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
938                 if (isr & ZR36057_ISR_GIRQ1) {
939                         btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
940                         zr->intr_counter_GIRQ1++;
941                 }
942                 if (isr & ZR36057_ISR_GIRQ0) {
943                         btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
944                         zr->intr_counter_GIRQ0++;
945                 }
946                 if (isr & ZR36057_ISR_CodRepIRQ) {
947                         btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
948                         zr->intr_counter_CodRepIRQ++;
949                 }
950                 if (isr & ZR36057_ISR_JPEGRepIRQ) {
951                         btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
952                         zr->intr_counter_JPEGRepIRQ++;
953                 }
954         }
955         return isr;
956 }
957
958 /* hack */
959 extern void zr36016_write (struct videocodec *codec,
960                            u16                reg,
961                            u32                val);
962
963 void
964 jpeg_start (struct zoran *zr)
965 {
966         int reg;
967
968         zr->frame_num = 0;
969
970         /* deassert P_reset, disable code transfer, deassert Active */
971         btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
972         /* stop flushing the internal code buffer */
973         btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
974         /* enable code transfer */
975         btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC);
976
977         /* clear IRQs */
978         btwrite(IRQ_MASK, ZR36057_ISR);
979         /* enable the JPEG IRQs */
980         btwrite(zr->card.jpeg_int |
981                         ZR36057_ICR_JPEGRepIRQ |
982                         ZR36057_ICR_IntPinEn,
983                 ZR36057_ICR);
984
985         set_frame(zr, 0);       // \FRAME
986
987         /* set the JPEG codec guest ID */
988         reg = (zr->card.gpcs[1] << ZR36057_JCGI_JPEGuestID) |
989                (0 << ZR36057_JCGI_JPEGuestReg);
990         btwrite(reg, ZR36057_JCGI);
991
992         if (zr->card.video_vfe == CODEC_TYPE_ZR36016 &&
993             zr->card.video_codec == CODEC_TYPE_ZR36050) {
994                 /* Enable processing on the ZR36016 */
995                 if (zr->vfe)
996                         zr36016_write(zr->vfe, 0, 1);
997
998                 /* load the address of the GO register in the ZR36050 latch */
999                 post_office_write(zr, 0, 0, 0);
1000         }
1001
1002         /* assert Active */
1003         btor(ZR36057_JPC_Active, ZR36057_JPC);
1004
1005         /* enable the Go generation */
1006         btor(ZR36057_JMC_Go_en, ZR36057_JMC);
1007         udelay(30);
1008
1009         set_frame(zr, 1);       // /FRAME
1010
1011         dprintk(3, KERN_DEBUG "%s: jpeg_start\n", ZR_DEVNAME(zr));
1012 }
1013
1014 void
1015 zr36057_enable_jpg (struct zoran          *zr,
1016                     enum zoran_codec_mode  mode)
1017 {
1018         static int zero = 0;
1019         static int one = 1;
1020         struct vfe_settings cap;
1021         int field_size =
1022             zr->jpg_buffers.buffer_size / zr->jpg_settings.field_per_buff;
1023
1024         zr->codec_mode = mode;
1025
1026         cap.x = zr->jpg_settings.img_x;
1027         cap.y = zr->jpg_settings.img_y;
1028         cap.width = zr->jpg_settings.img_width;
1029         cap.height = zr->jpg_settings.img_height;
1030         cap.decimation =
1031             zr->jpg_settings.HorDcm | (zr->jpg_settings.VerDcm << 8);
1032         cap.quality = zr->jpg_settings.jpg_comp.quality;
1033
1034         switch (mode) {
1035
1036         case BUZ_MODE_MOTION_COMPRESS:
1037                 /* In motion compress mode, the decoder output must be enabled, and
1038                  * the video bus direction set to input.
1039                  */
1040                 set_videobus_dir(zr, 0);
1041                 decoder_command(zr, DECODER_ENABLE_OUTPUT, &one);
1042                 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1043
1044                 /* Take the JPEG codec and the VFE out of sleep */
1045                 jpeg_codec_sleep(zr, 0);
1046                 /* Setup the JPEG codec */
1047                 zr->codec->control(zr->codec, CODEC_S_JPEG_TDS_BYTE,
1048                                    sizeof(int), &field_size);
1049                 zr->codec->set_video(zr->codec, zr->timing, &cap,
1050                                      &zr->card.vfe_pol);
1051                 zr->codec->set_mode(zr->codec, CODEC_DO_COMPRESSION);
1052
1053                 /* Setup the VFE */
1054                 if (zr->vfe) {
1055                         zr->vfe->control(zr->vfe, CODEC_S_JPEG_TDS_BYTE,
1056                                          sizeof(int), &field_size);
1057                         zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1058                                            &zr->card.vfe_pol);
1059                         zr->vfe->set_mode(zr->vfe, CODEC_DO_COMPRESSION);
1060                 }
1061
1062                 init_jpeg_queue(zr);
1063                 zr36057_set_jpg(zr, mode);      // \P_Reset, ... Video param, FIFO
1064
1065                 clear_interrupt_counters(zr);
1066                 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_COMPRESS)\n",
1067                         ZR_DEVNAME(zr));
1068                 break;
1069
1070         case BUZ_MODE_MOTION_DECOMPRESS:
1071                 /* In motion decompression mode, the decoder output must be disabled, and
1072                  * the video bus direction set to output.
1073                  */
1074                 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1075                 set_videobus_dir(zr, 1);
1076                 encoder_command(zr, ENCODER_SET_INPUT, &one);
1077
1078                 /* Take the JPEG codec and the VFE out of sleep */
1079                 jpeg_codec_sleep(zr, 0);
1080                 /* Setup the VFE */
1081                 if (zr->vfe) {
1082                         zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1083                                            &zr->card.vfe_pol);
1084                         zr->vfe->set_mode(zr->vfe, CODEC_DO_EXPANSION);
1085                 }
1086                 /* Setup the JPEG codec */
1087                 zr->codec->set_video(zr->codec, zr->timing, &cap,
1088                                      &zr->card.vfe_pol);
1089                 zr->codec->set_mode(zr->codec, CODEC_DO_EXPANSION);
1090
1091                 init_jpeg_queue(zr);
1092                 zr36057_set_jpg(zr, mode);      // \P_Reset, ... Video param, FIFO
1093
1094                 clear_interrupt_counters(zr);
1095                 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_DECOMPRESS)\n",
1096                         ZR_DEVNAME(zr));
1097                 break;
1098
1099         case BUZ_MODE_IDLE:
1100         default:
1101                 /* shut down processing */
1102                 btand(~(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ),
1103                       ZR36057_ICR);
1104                 btwrite(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ,
1105                         ZR36057_ISR);
1106                 btand(~ZR36057_JMC_Go_en, ZR36057_JMC); // \Go_en
1107
1108                 current->state = TASK_UNINTERRUPTIBLE;
1109                 schedule_timeout(HZ / 20);
1110
1111                 set_videobus_dir(zr, 0);
1112                 set_frame(zr, 1);       // /FRAME
1113                 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);      // /CFlush
1114                 btwrite(0, ZR36057_JPC);        // \P_Reset,\CodTrnsEn,\Active
1115                 btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1116                 btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1117                 jpeg_codec_reset(zr);
1118                 jpeg_codec_sleep(zr, 1);
1119                 zr36057_adjust_vfe(zr, mode);
1120
1121                 decoder_command(zr, DECODER_ENABLE_OUTPUT, &one);
1122                 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1123
1124                 dprintk(2, KERN_INFO "%s: enable_jpg(IDLE)\n", ZR_DEVNAME(zr));
1125                 break;
1126
1127         }
1128 }
1129
1130 /* when this is called the spinlock must be held */
1131 void
1132 zoran_feed_stat_com (struct zoran *zr)
1133 {
1134         /* move frames from pending queue to DMA */
1135
1136         int frame, i, max_stat_com;
1137
1138         max_stat_com =
1139             (zr->jpg_settings.TmpDcm ==
1140              1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1141
1142         while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com &&
1143                zr->jpg_dma_head < zr->jpg_que_head) {
1144
1145                 frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1146                 if (zr->jpg_settings.TmpDcm == 1) {
1147                         /* fill 1 stat_com entry */
1148                         i = (zr->jpg_dma_head -
1149                              zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1150                         if (!(zr->stat_com[i] & 1))
1151                                 break;
1152                         zr->stat_com[i] =
1153                             zr->jpg_buffers.buffer[frame].frag_tab_bus;
1154                 } else {
1155                         /* fill 2 stat_com entries */
1156                         i = ((zr->jpg_dma_head -
1157                               zr->jpg_err_shift) & 1) * 2;
1158                         if (!(zr->stat_com[i] & 1))
1159                                 break;
1160                         zr->stat_com[i] =
1161                             zr->jpg_buffers.buffer[frame].frag_tab_bus;
1162                         zr->stat_com[i + 1] =
1163                             zr->jpg_buffers.buffer[frame].frag_tab_bus;
1164                 }
1165                 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_DMA;
1166                 zr->jpg_dma_head++;
1167
1168         }
1169         if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1170                 zr->jpg_queued_num++;
1171 }
1172
1173 /* when this is called the spinlock must be held */
1174 static void
1175 zoran_reap_stat_com (struct zoran *zr)
1176 {
1177         /* move frames from DMA queue to done queue */
1178
1179         int i;
1180         u32 stat_com;
1181         unsigned int seq;
1182         unsigned int dif;
1183         struct zoran_jpg_buffer *buffer;
1184         int frame;
1185
1186         /* In motion decompress we don't have a hardware frame counter,
1187          * we just count the interrupts here */
1188
1189         if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1190                 zr->jpg_seq_num++;
1191         }
1192         while (zr->jpg_dma_tail < zr->jpg_dma_head) {
1193                 if (zr->jpg_settings.TmpDcm == 1)
1194                         i = (zr->jpg_dma_tail -
1195                              zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1196                 else
1197                         i = ((zr->jpg_dma_tail -
1198                               zr->jpg_err_shift) & 1) * 2 + 1;
1199
1200                 stat_com = zr->stat_com[i];
1201
1202                 if ((stat_com & 1) == 0) {
1203                         return;
1204                 }
1205                 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1206                 buffer = &zr->jpg_buffers.buffer[frame];
1207                 do_gettimeofday(&buffer->bs.timestamp);
1208
1209                 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1210                         buffer->bs.length = (stat_com & 0x7fffff) >> 1;
1211
1212                         /* update sequence number with the help of the counter in stat_com */
1213
1214                         seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
1215                         dif = (seq - zr->jpg_seq_num) & 0xff;
1216                         zr->jpg_seq_num += dif;
1217                 } else {
1218                         buffer->bs.length = 0;
1219                 }
1220                 buffer->bs.seq =
1221                     zr->jpg_settings.TmpDcm ==
1222                     2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1223                 buffer->state = BUZ_STATE_DONE;
1224
1225                 zr->jpg_dma_tail++;
1226         }
1227 }
1228
1229 static void
1230 error_handler (struct zoran *zr,
1231                u32           astat,
1232                u32           stat)
1233 {
1234         /* This is JPEG error handling part */
1235         if ((zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) &&
1236             (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS)) {
1237                 //dprintk(1, KERN_ERR "%s: Internal error: error handling request in mode %d\n", ZR_DEVNAME(zr), zr->codec_mode);
1238                 return;
1239         }
1240
1241         if ((stat & 1) == 0 &&
1242             zr->codec_mode == BUZ_MODE_MOTION_COMPRESS &&
1243             zr->jpg_dma_tail - zr->jpg_que_tail >=
1244              zr->jpg_buffers.num_buffers) {
1245                 /* No free buffers... */
1246                 zoran_reap_stat_com(zr);
1247                 zoran_feed_stat_com(zr);
1248                 wake_up_interruptible(&zr->jpg_capq);
1249                 zr->JPEG_missed = 0;
1250                 return;
1251         }
1252
1253         if (zr->JPEG_error != 1) {
1254                 /*
1255                  * First entry: error just happened during normal operation
1256                  * 
1257                  * In BUZ_MODE_MOTION_COMPRESS:
1258                  * 
1259                  * Possible glitch in TV signal. In this case we should
1260                  * stop the codec and wait for good quality signal before
1261                  * restarting it to avoid further problems
1262                  * 
1263                  * In BUZ_MODE_MOTION_DECOMPRESS:
1264                  * 
1265                  * Bad JPEG frame: we have to mark it as processed (codec crashed
1266                  * and was not able to do it itself), and to remove it from queue.
1267                  */
1268                 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1269                 udelay(1);
1270                 stat = stat | (post_office_read(zr, 7, 0) & 3) << 8;
1271                 btwrite(0, ZR36057_JPC);
1272                 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1273                 jpeg_codec_reset(zr);
1274                 jpeg_codec_sleep(zr, 1);
1275                 zr->JPEG_error = 1;
1276                 zr->num_errors++;
1277
1278                 /* Report error */
1279                 if (*zr_debug > 1 && zr->num_errors <= 8) {
1280                         long frame;
1281                         frame =
1282                             zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1283                         printk(KERN_ERR
1284                                "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
1285                                ZR_DEVNAME(zr), stat, zr->last_isr,
1286                                zr->jpg_que_tail, zr->jpg_dma_tail,
1287                                zr->jpg_dma_head, zr->jpg_que_head,
1288                                zr->jpg_seq_num, frame);
1289                         printk("stat_com frames:");
1290                         {
1291                                 int i, j;
1292                                 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1293                                         for (i = 0;
1294                                              i < zr->jpg_buffers.num_buffers;
1295                                              i++) {
1296                                                 if (zr->stat_com[j] ==
1297                                                     zr->jpg_buffers.
1298                                                     buffer[i].
1299                                                     frag_tab_bus) {
1300                                                         printk("% d->%d",
1301                                                                j, i);
1302                                                 }
1303                                         }
1304                                 }
1305                                 printk("\n");
1306                         }
1307                 }
1308
1309                 /* Find an entry in stat_com and rotate contents */
1310                 {
1311                         int i;
1312
1313                         if (zr->jpg_settings.TmpDcm == 1)
1314                                 i = (zr->jpg_dma_tail -
1315                                      zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1316                         else
1317                                 i = ((zr->jpg_dma_tail -
1318                                       zr->jpg_err_shift) & 1) * 2;
1319                         if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1320                                 /* Mimic zr36067 operation */
1321                                 zr->stat_com[i] |= 1;
1322                                 if (zr->jpg_settings.TmpDcm != 1)
1323                                         zr->stat_com[i + 1] |= 1;
1324                                 /* Refill */
1325                                 zoran_reap_stat_com(zr);
1326                                 zoran_feed_stat_com(zr);
1327                                 wake_up_interruptible(&zr->jpg_capq);
1328                                 /* Find an entry in stat_com again after refill */
1329                                 if (zr->jpg_settings.TmpDcm == 1)
1330                                         i = (zr->jpg_dma_tail -
1331                                              zr->jpg_err_shift) &
1332                                             BUZ_MASK_STAT_COM;
1333                                 else
1334                                         i = ((zr->jpg_dma_tail -
1335                                               zr->jpg_err_shift) & 1) * 2;
1336                         }
1337                         if (i) {
1338                                 /* Rotate stat_comm entries to make current entry first */
1339                                 int j;
1340                                 u32 bus_addr[BUZ_NUM_STAT_COM];
1341
1342                                 memcpy(bus_addr, zr->stat_com,
1343                                        sizeof(bus_addr));
1344                                 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1345                                         zr->stat_com[j] =
1346                                             bus_addr[(i + j) &
1347                                                      BUZ_MASK_STAT_COM];
1348                                 }
1349                                 zr->jpg_err_shift += i;
1350                                 zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
1351                         }
1352                         if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
1353                                 zr->jpg_err_seq = zr->jpg_seq_num;      /* + 1; */
1354                 }
1355         }
1356
1357         /* Now the stat_comm buffer is ready for restart */
1358         do {
1359                 int status, mode;
1360
1361                 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1362                         decoder_command(zr, DECODER_GET_STATUS, &status);
1363                         mode = CODEC_DO_COMPRESSION;
1364                 } else {
1365                         status = 0;
1366                         mode = CODEC_DO_EXPANSION;
1367                 }
1368                 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1369                     (status & DECODER_STATUS_GOOD)) {
1370                         /********** RESTART code *************/
1371                         jpeg_codec_reset(zr);
1372                         zr->codec->set_mode(zr->codec, mode);
1373                         zr36057_set_jpg(zr, zr->codec_mode);
1374                         jpeg_start(zr);
1375
1376                         if (zr->num_errors <= 8)
1377                                 dprintk(2, KERN_INFO "%s: Restart\n",
1378                                         ZR_DEVNAME(zr));
1379
1380                         zr->JPEG_missed = 0;
1381                         zr->JPEG_error = 2;
1382                         /********** End RESTART code ***********/
1383                 }
1384         } while (0);
1385 }
1386
1387 irqreturn_t
1388 zoran_irq (int             irq,
1389            void           *dev_id,
1390            struct pt_regs *regs)
1391 {
1392         u32 stat, astat;
1393         int count;
1394         struct zoran *zr;
1395         unsigned long flags;
1396
1397         zr = (struct zoran *) dev_id;
1398         count = 0;
1399
1400         if (zr->testing) {
1401                 /* Testing interrupts */
1402                 spin_lock_irqsave(&zr->spinlock, flags);
1403                 while ((stat = count_reset_interrupt(zr))) {
1404                         if (count++ > 100) {
1405                                 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1406                                 dprintk(1,
1407                                         KERN_ERR
1408                                         "%s: IRQ lockup while testing, isr=0x%08x, cleared int mask\n",
1409                                         ZR_DEVNAME(zr), stat);
1410                                 wake_up_interruptible(&zr->test_q);
1411                         }
1412                 }
1413                 zr->last_isr = stat;
1414                 spin_unlock_irqrestore(&zr->spinlock, flags);
1415                 return IRQ_HANDLED;
1416         }
1417
1418         spin_lock_irqsave(&zr->spinlock, flags);
1419         while (1) {
1420                 /* get/clear interrupt status bits */
1421                 stat = count_reset_interrupt(zr);
1422                 astat = stat & IRQ_MASK;
1423                 if (!astat) {
1424                         break;
1425                 }
1426                 dprintk(4,
1427                         KERN_DEBUG
1428                         "zoran_irq: astat: 0x%08x, mask: 0x%08x\n",
1429                         astat, btread(ZR36057_ICR));
1430                 if (astat & zr->card.vsync_int) {       // SW
1431
1432                         if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1433                             zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1434                                 /* count missed interrupts */
1435                                 zr->JPEG_missed++;
1436                         }
1437                         //post_office_read(zr,1,0);
1438                         /* Interrupts may still happen when
1439                          * zr->v4l_memgrab_active is switched off.
1440                          * We simply ignore them */
1441
1442                         if (zr->v4l_memgrab_active) {
1443
1444                                 /* A lot more checks should be here ... */
1445                                 if ((btread(ZR36057_VSSFGR) &
1446                                      ZR36057_VSSFGR_SnapShot) == 0)
1447                                         dprintk(1,
1448                                                 KERN_WARNING
1449                                                 "%s: BuzIRQ with SnapShot off ???\n",
1450                                                 ZR_DEVNAME(zr));
1451
1452                                 if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1453                                         /* There is a grab on a frame going on, check if it has finished */
1454
1455                                         if ((btread(ZR36057_VSSFGR) &
1456                                              ZR36057_VSSFGR_FrameGrab) ==
1457                                             0) {
1458                                                 /* it is finished, notify the user */
1459
1460                                                 zr->v4l_buffers.buffer[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1461                                                 zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.seq = zr->v4l_grab_seq;
1462                                                 do_gettimeofday(&zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.timestamp);
1463                                                 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1464                                                 zr->v4l_pend_tail++;
1465                                         }
1466                                 }
1467
1468                                 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1469                                         wake_up_interruptible(&zr->v4l_capq);
1470
1471                                 /* Check if there is another grab queued */
1472
1473                                 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1474                                     zr->v4l_pend_tail != zr->v4l_pend_head) {
1475
1476                                         int frame = zr->v4l_pend[zr->v4l_pend_tail &
1477                                                          V4L_MASK_FRAME];
1478                                         u32 reg;
1479
1480                                         zr->v4l_grab_frame = frame;
1481
1482                                         /* Set zr36057 video front end and enable video */
1483
1484                                         /* Buffer address */
1485
1486                                         reg =
1487                                             zr->v4l_buffers.buffer[frame].
1488                                             fbuffer_bus;
1489                                         btwrite(reg, ZR36057_VDTR);
1490                                         if (zr->v4l_settings.height >
1491                                             BUZ_MAX_HEIGHT / 2)
1492                                                 reg +=
1493                                                     zr->v4l_settings.
1494                                                     bytesperline;
1495                                         btwrite(reg, ZR36057_VDBR);
1496
1497                                         /* video stride, status, and frame grab register */
1498                                         reg = 0;
1499                                         if (zr->v4l_settings.height >
1500                                             BUZ_MAX_HEIGHT / 2)
1501                                                 reg +=
1502                                                     zr->v4l_settings.
1503                                                     bytesperline;
1504                                         reg =
1505                                             (reg <<
1506                                              ZR36057_VSSFGR_DispStride);
1507                                         reg |= ZR36057_VSSFGR_VidOvf;
1508                                         reg |= ZR36057_VSSFGR_SnapShot;
1509                                         reg |= ZR36057_VSSFGR_FrameGrab;
1510                                         btwrite(reg, ZR36057_VSSFGR);
1511
1512                                         btor(ZR36057_VDCR_VidEn,
1513                                              ZR36057_VDCR);
1514                                 }
1515                         }
1516
1517                         /* even if we don't grab, we do want to increment
1518                          * the sequence counter to see lost frames */
1519                         zr->v4l_grab_seq++;
1520                 }
1521 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
1522                 if (astat & ZR36057_ISR_CodRepIRQ) {
1523                         zr->intr_counter_CodRepIRQ++;
1524                         IDEBUG(printk
1525                                (KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n",
1526                                 ZR_DEVNAME(zr)));
1527                         btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
1528                 }
1529 #endif                          /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
1530
1531 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
1532                 if (astat & ZR36057_ISR_JPEGRepIRQ) {
1533
1534                         if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1535                             zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1536                                 if (*zr_debug > 1 &&
1537                                     (!zr->frame_num || zr->JPEG_error)) {
1538                                         printk(KERN_INFO
1539                                                "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%d\n",
1540                                                ZR_DEVNAME(zr), stat,
1541                                                zr->jpg_settings.odd_even,
1542                                                zr->jpg_settings.
1543                                                field_per_buff,
1544                                                zr->JPEG_missed);
1545                                         {
1546                                                 char sc[] = "0000";
1547                                                 char sv[5];
1548                                                 int i;
1549                                                 strcpy(sv, sc);
1550                                                 for (i = 0; i < 4; i++) {
1551                                                         if (zr->stat_com[i] & 1)
1552                                                                 sv[i] = '1';
1553                                                 }
1554                                                 sv[4] = 0;
1555                                                 printk(KERN_INFO
1556                                                        "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ld\n",
1557                                                        ZR_DEVNAME(zr), sv,
1558                                                        zr->jpg_que_tail,
1559                                                        zr->jpg_dma_tail,
1560                                                        zr->jpg_dma_head,
1561                                                        zr->jpg_que_head);
1562                                         }
1563                                 } else {
1564                                         if (zr->JPEG_missed > zr->JPEG_max_missed)      // Get statistics
1565                                                 zr->JPEG_max_missed =
1566                                                     zr->JPEG_missed;
1567                                         if (zr->JPEG_missed <
1568                                             zr->JPEG_min_missed)
1569                                                 zr->JPEG_min_missed =
1570                                                     zr->JPEG_missed;
1571                                 }
1572
1573                                 if (*zr_debug > 2 && zr->frame_num < 6) {
1574                                         int i;
1575                                         printk("%s: seq=%ld stat_com:",
1576                                                ZR_DEVNAME(zr), zr->jpg_seq_num);
1577                                         for (i = 0; i < 4; i++) {
1578                                                 printk(" %08x",
1579                                                        zr->stat_com[i]);
1580                                         }
1581                                         printk("\n");
1582                                 }
1583                                 zr->frame_num++;
1584                                 zr->JPEG_missed = 0;
1585                                 zr->JPEG_error = 0;
1586                                 zoran_reap_stat_com(zr);
1587                                 zoran_feed_stat_com(zr);
1588                                 wake_up_interruptible(&zr->jpg_capq);
1589                         } /*else {
1590                               dprintk(1,
1591                                         KERN_ERR
1592                                         "%s: JPEG interrupt while not in motion (de)compress mode!\n",
1593                                         ZR_DEVNAME(zr));
1594                         }*/
1595                 }
1596 #endif                          /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
1597
1598                 /* DATERR, too many fields missed, error processing */
1599                 if ((astat & zr->card.jpeg_int) ||
1600                     zr->JPEG_missed > 25 ||
1601                     zr->JPEG_error == 1 ||
1602                     ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) &&
1603                      (zr->frame_num & (zr->JPEG_missed >
1604                                        zr->jpg_settings.field_per_buff)))) {
1605                         error_handler(zr, astat, stat);
1606                 }
1607
1608                 count++;
1609                 if (count > 10) {
1610                         dprintk(2, KERN_WARNING "%s: irq loop %d\n",
1611                                 ZR_DEVNAME(zr), count);
1612                         if (count > 20) {
1613                                 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1614                                 dprintk(2,
1615                                         KERN_ERR
1616                                         "%s: IRQ lockup, cleared int mask\n",
1617                                         ZR_DEVNAME(zr));
1618                                 break;
1619                         }
1620                 }
1621                 zr->last_isr = stat;
1622         }
1623         spin_unlock_irqrestore(&zr->spinlock, flags);
1624
1625         return IRQ_HANDLED;
1626 }
1627
1628 void
1629 zoran_set_pci_master (struct zoran *zr,
1630                       int           set_master)
1631 {
1632         if (set_master) {
1633                 pci_set_master(zr->pci_dev);
1634         } else {
1635                 u16 command;
1636
1637                 pci_read_config_word(zr->pci_dev, PCI_COMMAND, &command);
1638                 command &= ~PCI_COMMAND_MASTER;
1639                 pci_write_config_word(zr->pci_dev, PCI_COMMAND, command);
1640         }
1641 }
1642
1643 void
1644 zoran_init_hardware (struct zoran *zr)
1645 {
1646         int j, zero = 0;
1647
1648         /* Enable bus-mastering */
1649         zoran_set_pci_master(zr, 1);
1650
1651         /* Initialize the board */
1652         if (zr->card.init) {
1653                 zr->card.init(zr);
1654         }
1655
1656         j = zr->card.input[zr->input].muxsel;
1657
1658         decoder_command(zr, 0, NULL);
1659         decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1660         decoder_command(zr, DECODER_SET_INPUT, &j);
1661
1662         encoder_command(zr, 0, NULL);
1663         encoder_command(zr, ENCODER_SET_NORM, &zr->norm);
1664         encoder_command(zr, ENCODER_SET_INPUT, &zero);
1665
1666         /* toggle JPEG codec sleep to sync PLL */
1667         jpeg_codec_sleep(zr, 1);
1668         jpeg_codec_sleep(zr, 0);
1669
1670         /* set individual interrupt enables (without GIRQ1)
1671          * but don't global enable until zoran_open() */
1672
1673         //btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ1, ZR36057_ICR);  // SW
1674         // It looks like using only JPEGRepIRQEn is not always reliable,
1675         // may be when JPEG codec crashes it won't generate IRQ? So,
1676          /*CP*/                 //        btwrite(IRQ_MASK, ZR36057_ICR); // Enable Vsync interrupts too. SM    WHY ? LP
1677             zr36057_init_vfe(zr);
1678
1679         zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1680
1681         btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
1682 }
1683
1684 void
1685 zr36057_restart (struct zoran *zr)
1686 {
1687         btwrite(0, ZR36057_SPGPPCR);
1688         mdelay(1);
1689         btor(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
1690         mdelay(1);
1691
1692         /* assert P_Reset */
1693         btwrite(0, ZR36057_JPC);
1694         /* set up GPIO direction - all output */
1695         btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
1696
1697         /* set up GPIO pins and guest bus timing */
1698         btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
1699 }
1700
1701 /*
1702  * initialize video front end
1703  */
1704
1705 void
1706 zr36057_init_vfe (struct zoran *zr)
1707 {
1708         u32 reg;
1709
1710         reg = btread(ZR36057_VFESPFR);
1711         reg |= ZR36057_VFESPFR_LittleEndian;
1712         reg &= ~ZR36057_VFESPFR_VCLKPol;
1713         reg |= ZR36057_VFESPFR_ExtFl;
1714         reg |= ZR36057_VFESPFR_TopField;
1715         btwrite(reg, ZR36057_VFESPFR);
1716         reg = btread(ZR36057_VDCR);
1717         if (pci_pci_problems & PCIPCI_TRITON)
1718                 // || zr->revision < 1) // Revision 1 has also Triton support
1719                 reg &= ~ZR36057_VDCR_Triton;
1720         else
1721                 reg |= ZR36057_VDCR_Triton;
1722         btwrite(reg, ZR36057_VDCR);
1723 }
1724
1725 /*
1726  * Interface to decoder and encoder chips using i2c bus
1727  */
1728
1729 int
1730 decoder_command (struct zoran *zr,
1731                  int           cmd,
1732                  void         *data)
1733 {
1734         if (zr->decoder == NULL)
1735                 return -EIO;
1736
1737         if (zr->card.type == LML33 &&
1738             (cmd == DECODER_SET_NORM || DECODER_SET_INPUT)) {
1739                 int res;
1740
1741                 // Bt819 needs to reset its FIFO buffer using #FRST pin and
1742                 // LML33 card uses GPIO(7) for that.
1743                 GPIO(zr, 7, 0);
1744                 res = zr->decoder->driver->command(zr->decoder, cmd, data);
1745                 // Pull #FRST high.
1746                 GPIO(zr, 7, 1);
1747                 return res;
1748         } else
1749                 return zr->decoder->driver->command(zr->decoder, cmd,
1750                                                     data);
1751 }
1752
1753 int
1754 encoder_command (struct zoran *zr,
1755                  int           cmd,
1756                  void         *data)
1757 {
1758         if (zr->encoder == NULL)
1759                 return -1;
1760
1761         return zr->encoder->driver->command(zr->encoder, cmd, data);
1762 }