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