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