ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / stradis.c
1 /* 
2  * stradis.c - stradis 4:2:2 mpeg decoder driver
3  *
4  * Stradis 4:2:2 MPEG-2 Decoder Driver
5  * Copyright (C) 1999 Nathan Laredo <laredo@gnu.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/kernel.h>
27 #include <linux/major.h>
28 #include <linux/slab.h>
29 #include <linux/mm.h>
30 #include <linux/init.h>
31 #include <linux/poll.h>
32 #include <linux/pci.h>
33 #include <linux/signal.h>
34 #include <asm/io.h>
35 #include <linux/ioport.h>
36 #include <asm/pgtable.h>
37 #include <asm/page.h>
38 #include <linux/sched.h>
39 #include <asm/types.h>
40 #include <linux/types.h>
41 #include <linux/interrupt.h>
42 #include <asm/uaccess.h>
43 #include <linux/vmalloc.h>
44 #include <linux/videodev.h>
45
46 #include "saa7146.h"
47 #include "saa7146reg.h"
48 #include "ibmmpeg2.h"
49 #include "saa7121.h"
50 #include "cs8420.h"
51
52 #define DEBUG(x)                /* debug driver */
53 #undef  IDEBUG                  /* debug irq handler */
54 #undef  MDEBUG                  /* debug memory management */
55
56 #define SAA7146_MAX 6
57
58 static struct saa7146 saa7146s[SAA7146_MAX];
59
60 static int saa_num = 0;         /* number of SAA7146s in use */
61
62 static int video_nr = -1;
63 MODULE_PARM(video_nr,"i");
64 MODULE_LICENSE("GPL");
65
66
67 #define nDebNormal      0x00480000
68 #define nDebNoInc       0x00480000
69 #define nDebVideo       0xd0480000
70 #define nDebAudio       0xd0400000
71 #define nDebDMA         0x02c80000
72
73 #define oDebNormal      0x13c80000
74 #define oDebNoInc       0x13c80000
75 #define oDebVideo       0xd1080000
76 #define oDebAudio       0xd1080000
77 #define oDebDMA         0x03080000
78
79 #define NewCard         (saa->boardcfg[3])
80 #define ChipControl     (saa->boardcfg[1])
81 #define NTSCFirstActive (saa->boardcfg[4])
82 #define PALFirstActive  (saa->boardcfg[5])
83 #define NTSCLastActive  (saa->boardcfg[54])
84 #define PALLastActive   (saa->boardcfg[55])
85 #define Have2MB         (saa->boardcfg[18] & 0x40)
86 #define HaveCS8420      (saa->boardcfg[18] & 0x04)
87 #define IBMMPEGCD20     (saa->boardcfg[18] & 0x20)
88 #define HaveCS3310      (saa->boardcfg[18] & 0x01)
89 #define CS3310MaxLvl    ((saa->boardcfg[30] << 8) | saa->boardcfg[31])
90 #define HaveCS4341      (saa->boardcfg[40] == 2)
91 #define SDIType         (saa->boardcfg[27])
92 #define CurrentMode     (saa->boardcfg[2])
93
94 #define debNormal       (NewCard ? nDebNormal : oDebNormal)
95 #define debNoInc        (NewCard ? nDebNoInc : oDebNoInc)
96 #define debVideo        (NewCard ? nDebVideo : oDebVideo)
97 #define debAudio        (NewCard ? nDebAudio : oDebAudio)
98 #define debDMA          (NewCard ? nDebDMA : oDebDMA)
99
100 #ifdef DEBUG
101 int stradis_driver(void)        /* for the benefit of ksymoops */
102 {
103         return 1;
104 }
105 #endif
106
107 #ifdef USE_RESCUE_EEPROM_SDM275
108 static unsigned char rescue_eeprom[64] = {
109 0x00,0x01,0x04,0x13,0x26,0x0f,0x10,0x00,0x00,0x00,0x43,0x63,0x22,0x01,0x29,0x15,0x73,0x00,0x1f, 'd', 'e', 'c', 'x', 'l', 'd', 'v', 'a',0x02,0x00,0x01,0x00,0xcc,0xa4,0x63,0x09,0xe2,0x10,0x00,0x0a,0x00,0x02,0x02, 'd', 'e', 'c', 'x', 'l', 'a',0x00,0x00,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
110 };
111 #endif
112
113 /* ----------------------------------------------------------------------- */
114 /* Hardware I2C functions */
115 static void I2CWipe(struct saa7146 *saa)
116 {
117         int i;
118         /* set i2c to ~=100kHz, abort transfer, clear busy */
119         saawrite(0x600 | SAA7146_I2C_ABORT, SAA7146_I2C_STATUS);
120         saawrite((SAA7146_MC2_UPLD_I2C << 16) |
121                  SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
122         /* wait for i2c registers to be programmed */
123         for (i = 0; i < 1000 &&
124              !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
125                 schedule();
126         saawrite(0x600, SAA7146_I2C_STATUS);
127         saawrite((SAA7146_MC2_UPLD_I2C << 16) |
128                  SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
129         /* wait for i2c registers to be programmed */
130         for (i = 0; i < 1000 &&
131              !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
132                 schedule();
133         saawrite(0x600, SAA7146_I2C_STATUS);
134         saawrite((SAA7146_MC2_UPLD_I2C << 16) |
135                  SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
136         /* wait for i2c registers to be programmed */
137         for (i = 0; i < 1000 &&
138              !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
139                 schedule();
140 }
141
142 /* read I2C */
143 static int I2CRead(struct saa7146 *saa, unsigned char addr,
144                    unsigned char subaddr, int dosub)
145 {
146         int i;
147
148         if (saaread(SAA7146_I2C_STATUS) & 0x3c)
149                 I2CWipe(saa);
150         for (i = 0; i < 1000 &&
151              (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
152                 schedule();
153         if (i == 1000)
154                 I2CWipe(saa);
155         if (dosub)
156                 saawrite(((addr & 0xfe) << 24) | (((addr | 1) & 0xff) << 8) |
157                   ((subaddr & 0xff) << 16) | 0xed, SAA7146_I2C_TRANSFER);
158         else
159                 saawrite(((addr & 0xfe) << 24) | (((addr | 1) & 0xff) << 16) |
160                          0xf1, SAA7146_I2C_TRANSFER);
161         saawrite((SAA7146_MC2_UPLD_I2C << 16) |
162                  SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
163         /* wait for i2c registers to be programmed */
164         for (i = 0; i < 1000 &&
165              !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
166                 schedule();
167         /* wait for valid data */
168         for (i = 0; i < 1000 &&
169              (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
170                 schedule();
171         if (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_ERR)
172                 return -1;
173         if (i == 1000) 
174                 printk("i2c setup read timeout\n");
175         saawrite(0x41, SAA7146_I2C_TRANSFER);
176         saawrite((SAA7146_MC2_UPLD_I2C << 16) |
177                  SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
178         /* wait for i2c registers to be programmed */
179         for (i = 0; i < 1000 &&
180              !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
181                 schedule();
182         /* wait for valid data */
183         for (i = 0; i < 1000 &&
184              (saaread(SAA7146_I2C_TRANSFER) & SAA7146_I2C_BUSY); i++)
185                 schedule();
186         if (saaread(SAA7146_I2C_TRANSFER) & SAA7146_I2C_ERR)
187                 return -1;
188         if (i == 1000) 
189                 printk("i2c read timeout\n");
190         return ((saaread(SAA7146_I2C_TRANSFER) >> 24) & 0xff);
191 }
192
193 /* set both to write both bytes, reset it to write only b1 */
194
195 static int I2CWrite(struct saa7146 *saa, unsigned char addr, unsigned char b1,
196                     unsigned char b2, int both)
197 {
198         int i;
199         u32 data;
200
201         if (saaread(SAA7146_I2C_STATUS) & 0x3c)
202                 I2CWipe(saa);
203         for (i = 0; i < 1000 &&
204              (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
205                 schedule();
206         if (i == 1000)
207                 I2CWipe(saa);
208         data = ((addr & 0xfe) << 24) | ((b1 & 0xff) << 16);
209         if (both)
210                 data |= ((b2 & 0xff) << 8) | 0xe5;
211         else
212                 data |= 0xd1;
213         saawrite(data, SAA7146_I2C_TRANSFER);
214         saawrite((SAA7146_MC2_UPLD_I2C << 16) | SAA7146_MC2_UPLD_I2C,
215                  SAA7146_MC2);
216         return 0;
217 }
218
219 static void attach_inform(struct saa7146 *saa, int id)
220 {
221         int i;
222
223         DEBUG(printk(KERN_DEBUG "stradis%d: i2c: device found=%02x\n", saa->nr, id));
224         if (id == 0xa0) { /* we have rev2 or later board, fill in info */
225                 for (i = 0; i < 64; i++)
226                         saa->boardcfg[i] = I2CRead(saa, 0xa0, i, 1);
227 #ifdef USE_RESCUE_EEPROM_SDM275
228                 if (saa->boardcfg[0] != 0) {
229                         printk("stradis%d: WARNING: EEPROM STORED VALUES HAVE BEEN IGNORED\n", saa->nr);
230                         for (i = 0; i < 64; i++)
231                                 saa->boardcfg[i] = rescue_eeprom[i];
232                 }
233 #endif
234                 printk("stradis%d: config =", saa->nr);
235                 for (i = 0; i < 51; i++) {
236                         printk(" %02x",saa->boardcfg[i]);
237                 }
238                 printk("\n");
239         }
240 }
241
242 static void I2CBusScan(struct saa7146 *saa)
243 {
244         int i;
245         for (i = 0; i < 0xff; i += 2)
246                 if ((I2CRead(saa, i, 0, 0)) >= 0)
247                         attach_inform(saa, i);
248 }
249
250 static int debiwait_maxwait = 0;
251
252 static int wait_for_debi_done(struct saa7146 *saa)
253 {
254         int i;
255
256         /* wait for registers to be programmed */
257         for (i = 0; i < 100000 &&
258              !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_DEBI); i++)
259                 saaread(SAA7146_MC2);
260         /* wait for transfer to complete */
261         for (i = 0; i < 500000 &&
262              (saaread(SAA7146_PSR) & SAA7146_PSR_DEBI_S); i++)
263                 saaread(SAA7146_MC2);
264         if (i > debiwait_maxwait)
265                 printk("wait-for-debi-done maxwait: %d\n",
266                         debiwait_maxwait = i);
267         
268         if (i == 500000)
269                 return -1;
270         return 0;
271 }
272
273 static int debiwrite(struct saa7146 *saa, u32 config, int addr,
274                       u32 val, int count)
275 {
276         u32 cmd;
277         if (count <= 0 || count > 32764)
278                 return -1;
279         if (wait_for_debi_done(saa) < 0)
280                 return -1;
281         saawrite(config, SAA7146_DEBI_CONFIG);
282         if (count <= 4)         /* immediate transfer */
283                 saawrite(val, SAA7146_DEBI_AD);
284         else                    /* block transfer */
285                 saawrite(virt_to_bus(saa->dmadebi), SAA7146_DEBI_AD);
286         saawrite((cmd = (count << 17) | (addr & 0xffff)), SAA7146_DEBI_COMMAND);
287         saawrite((SAA7146_MC2_UPLD_DEBI << 16) | SAA7146_MC2_UPLD_DEBI,
288                  SAA7146_MC2);
289         return 0;
290 }
291
292 static u32 debiread(struct saa7146 *saa, u32 config, int addr, int count)
293 {
294         u32 result = 0;
295
296         if (count > 32764 || count <= 0)
297                 return 0;
298         if (wait_for_debi_done(saa) < 0)
299                 return 0;
300         saawrite(virt_to_bus(saa->dmadebi), SAA7146_DEBI_AD);
301         saawrite((count << 17) | 0x10000 | (addr & 0xffff),
302                  SAA7146_DEBI_COMMAND);
303         saawrite(config, SAA7146_DEBI_CONFIG);
304         saawrite((SAA7146_MC2_UPLD_DEBI << 16) | SAA7146_MC2_UPLD_DEBI,
305                  SAA7146_MC2);
306         if (count > 4)          /* not an immediate transfer */
307                 return count;
308         wait_for_debi_done(saa);
309         result = saaread(SAA7146_DEBI_AD);
310         if (count == 1)
311                 result &= 0xff;
312         if (count == 2)
313                 result &= 0xffff;
314         if (count == 3)
315                 result &= 0xffffff;
316         return result;
317 }
318
319 #if 0 /* unused */
320 /* MUST be a multiple of 8 bytes and 8-byte aligned and < 32768 bytes */
321 /* data copied into saa->dmadebi buffer, caller must re-enable interrupts */
322 static void ibm_block_dram_read(struct saa7146 *saa, int address, int bytes)
323 {
324         int i, j;
325         u32 *buf;
326         buf = (u32 *) saa->dmadebi;
327         if (bytes > 0x7000)
328                 bytes = 0x7000;
329         saawrite(0, SAA7146_IER);       /* disable interrupts */
330         for (i=0; i < 10000 &&
331                 (debiread(saa, debNormal, IBM_MP2_DRAM_CMD_STAT, 2)
332                 & 0x8000); i++)
333                 saaread(SAA7146_MC2);
334         if (i == 10000)
335                 printk(KERN_ERR "stradis%d: dram_busy never cleared\n",
336                         saa->nr);
337         debiwrite(saa, debNormal, IBM_MP2_SRC_ADDR, (address<<16) |
338                 (address>>16), 4);
339         debiwrite(saa, debNormal, IBM_MP2_BLOCK_SIZE, bytes, 2);
340         debiwrite(saa, debNormal, IBM_MP2_CMD_STAT, 0x8a10, 2);
341         for (j = 0; j < bytes/4; j++) {
342                 for (i = 0; i < 10000 &&
343                         (!(debiread(saa, debNormal, IBM_MP2_DRAM_CMD_STAT, 2)
344                         & 0x4000)); i++)
345                         saaread(SAA7146_MC2);
346                 if (i == 10000)
347                         printk(KERN_ERR "stradis%d: dram_ready never set\n",
348                                 saa->nr);
349                 buf[j] = debiread(saa, debNormal, IBM_MP2_DRAM_DATA, 4);
350         }
351 }
352 #endif /* unused */
353
354 static void do_irq_send_data(struct saa7146 *saa)
355 {
356         int split, audbytes, vidbytes;
357
358         saawrite(SAA7146_PSR_PIN1, SAA7146_IER);
359         /* if special feature mode in effect, disable audio sending */
360         if (saa->playmode != VID_PLAY_NORMAL)
361                 saa->audtail = saa->audhead = 0;
362         if (saa->audhead <= saa->audtail)
363                 audbytes = saa->audtail - saa->audhead;
364         else
365                 audbytes = 65536 - (saa->audhead - saa->audtail);
366         if (saa->vidhead <= saa->vidtail)
367                 vidbytes = saa->vidtail - saa->vidhead;
368         else
369                 vidbytes = 524288 - (saa->vidhead - saa->vidtail);
370         if (audbytes == 0 && vidbytes == 0 && saa->osdtail == saa->osdhead) {
371                 saawrite(0, SAA7146_IER);
372                 return;
373         }
374         /* if at least 1 block audio waiting and audio fifo isn't full */
375         if (audbytes >= 2048 && (debiread(saa, debNormal,
376                 IBM_MP2_AUD_FIFO, 2) & 0xff) < 60) {
377                 if (saa->audhead > saa->audtail)
378                         split = 65536 - saa->audhead;
379                 else
380                         split = 0;
381                 audbytes = 2048;
382                 if (split > 0 && split < 2048) {
383                         memcpy(saa->dmadebi, saa->audbuf + saa->audhead,
384                                 split);
385                         saa->audhead = 0;
386                         audbytes -= split;
387                 } else
388                         split = 0;
389                 memcpy(saa->dmadebi + split, saa->audbuf + saa->audhead,
390                         audbytes);
391                 saa->audhead += audbytes;
392                 saa->audhead &= 0xffff;
393                 debiwrite(saa, debAudio, (NewCard? IBM_MP2_AUD_FIFO :
394                           IBM_MP2_AUD_FIFOW), 0, 2048);
395                 wake_up_interruptible(&saa->audq);
396         /* if at least 1 block video waiting and video fifo isn't full */
397         } else if (vidbytes >= 30720 && (debiread(saa, debNormal,
398                 IBM_MP2_FIFO, 2)) < 16384) {
399                 if (saa->vidhead > saa->vidtail)
400                         split = 524288 - saa->vidhead;
401                 else
402                         split = 0;
403                 vidbytes = 30720;
404                 if (split > 0 && split < 30720) {
405                         memcpy(saa->dmadebi, saa->vidbuf + saa->vidhead,
406                                 split);
407                         saa->vidhead = 0;
408                         vidbytes -= split;
409                 } else
410                         split = 0;
411                 memcpy(saa->dmadebi + split, saa->vidbuf + saa->vidhead,
412                         vidbytes);
413                 saa->vidhead += vidbytes;
414                 saa->vidhead &= 0x7ffff;
415                 debiwrite(saa, debVideo, (NewCard ? IBM_MP2_FIFO :
416                           IBM_MP2_FIFOW), 0, 30720);
417                 wake_up_interruptible(&saa->vidq);
418         }
419         saawrite(SAA7146_PSR_DEBI_S | SAA7146_PSR_PIN1, SAA7146_IER);
420 }
421
422 static void send_osd_data(struct saa7146 *saa)
423 {
424         int size = saa->osdtail - saa->osdhead;
425         if (size > 30720)
426                 size = 30720;
427         /* ensure some multiple of 8 bytes is transferred */
428         size = 8 * ((size + 8)>>3);
429         if (size) {
430                 debiwrite(saa, debNormal, IBM_MP2_OSD_ADDR,
431                           (saa->osdhead>>3), 2);
432                 memcpy(saa->dmadebi, &saa->osdbuf[saa->osdhead], size);
433                 saa->osdhead += size;
434                 /* block transfer of next 8 bytes to ~32k bytes */
435                 debiwrite(saa, debNormal, IBM_MP2_OSD_DATA, 0, size);
436         }
437         if (saa->osdhead >= saa->osdtail) {
438                 saa->osdhead = saa->osdtail = 0;
439                 debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
440         }
441 }
442
443 static irqreturn_t saa7146_irq(int irq, void *dev_id, struct pt_regs *regs)
444 {
445         struct saa7146 *saa = (struct saa7146 *) dev_id;
446         u32 stat, astat;
447         int count;
448         int handled = 0;
449
450         count = 0;
451         while (1) {
452                 /* get/clear interrupt status bits */
453                 stat = saaread(SAA7146_ISR);
454                 astat = stat & saaread(SAA7146_IER);
455                 if (!astat)
456                         break;
457                 handled = 1;
458                 saawrite(astat, SAA7146_ISR);
459                 if (astat & SAA7146_PSR_DEBI_S) {
460                         do_irq_send_data(saa);
461                 }
462                 if (astat & SAA7146_PSR_PIN1) {
463                         int istat;
464                         /* the following read will trigger DEBI_S */
465                         istat = debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
466                         if (istat & 1) {
467                                 saawrite(0, SAA7146_IER);
468                                 send_osd_data(saa);
469                                 saawrite(SAA7146_PSR_DEBI_S |
470                                          SAA7146_PSR_PIN1, SAA7146_IER);
471                         }
472                         if (istat & 0x20) {     /* Video Start */
473                                 saa->vidinfo.frame_count++;
474                         }
475                         if (istat & 0x400) {    /* Picture Start */
476                                 /* update temporal reference */
477                         }
478                         if (istat & 0x200) {    /* Picture Resolution Change */
479                                 /* read new resolution */
480                         }
481                         if (istat & 0x100) {    /* New User Data found */
482                                 /* read new user data */
483                         }
484                         if (istat & 0x1000) {   /* new GOP/SMPTE */
485                                 /* read new SMPTE */
486                         }
487                         if (istat & 0x8000) {   /* Sequence Start Code */
488                                 /* reset frame counter, load sizes */
489                                 saa->vidinfo.frame_count = 0;
490                                 saa->vidinfo.h_size = 704;
491                                 saa->vidinfo.v_size = 480;
492 #if 0
493                                 if (saa->endmarkhead != saa->endmarktail) {
494                                         saa->audhead = 
495                                                 saa->endmark[saa->endmarkhead];
496                                         saa->endmarkhead++;
497                                         if (saa->endmarkhead >= MAX_MARKS)
498                                                 saa->endmarkhead = 0;
499                                 }
500 #endif
501                         }
502                         if (istat & 0x4000) {   /* Sequence Error Code */
503                                 if (saa->endmarkhead != saa->endmarktail) {
504                                         saa->audhead = 
505                                                 saa->endmark[saa->endmarkhead];
506                                         saa->endmarkhead++;
507                                         if (saa->endmarkhead >= MAX_MARKS)
508                                                 saa->endmarkhead = 0;
509                                 }
510                         }
511                 }
512 #ifdef IDEBUG
513                 if (astat & SAA7146_PSR_PPEF) {
514                         IDEBUG(printk("stradis%d irq: PPEF\n", saa->nr));
515                 }
516                 if (astat & SAA7146_PSR_PABO) {
517                         IDEBUG(printk("stradis%d irq: PABO\n", saa->nr));
518                 }
519                 if (astat & SAA7146_PSR_PPED) {
520                         IDEBUG(printk("stradis%d irq: PPED\n", saa->nr));
521                 }
522                 if (astat & SAA7146_PSR_RPS_I1) {
523                         IDEBUG(printk("stradis%d irq: RPS_I1\n", saa->nr));
524                 }
525                 if (astat & SAA7146_PSR_RPS_I0) {
526                         IDEBUG(printk("stradis%d irq: RPS_I0\n", saa->nr));
527                 }
528                 if (astat & SAA7146_PSR_RPS_LATE1) {
529                         IDEBUG(printk("stradis%d irq: RPS_LATE1\n", saa->nr));
530                 }
531                 if (astat & SAA7146_PSR_RPS_LATE0) {
532                         IDEBUG(printk("stradis%d irq: RPS_LATE0\n", saa->nr));
533                 }
534                 if (astat & SAA7146_PSR_RPS_E1) {
535                         IDEBUG(printk("stradis%d irq: RPS_E1\n", saa->nr));
536                 }
537                 if (astat & SAA7146_PSR_RPS_E0) {
538                         IDEBUG(printk("stradis%d irq: RPS_E0\n", saa->nr));
539                 }
540                 if (astat & SAA7146_PSR_RPS_TO1) {
541                         IDEBUG(printk("stradis%d irq: RPS_TO1\n", saa->nr));
542                 }
543                 if (astat & SAA7146_PSR_RPS_TO0) {
544                         IDEBUG(printk("stradis%d irq: RPS_TO0\n", saa->nr));
545                 }
546                 if (astat & SAA7146_PSR_UPLD) {
547                         IDEBUG(printk("stradis%d irq: UPLD\n", saa->nr));
548                 }
549                 if (astat & SAA7146_PSR_DEBI_E) {
550                         IDEBUG(printk("stradis%d irq: DEBI_E\n", saa->nr));
551                 }
552                 if (astat & SAA7146_PSR_I2C_S) {
553                         IDEBUG(printk("stradis%d irq: I2C_S\n", saa->nr));
554                 }
555                 if (astat & SAA7146_PSR_I2C_E) {
556                         IDEBUG(printk("stradis%d irq: I2C_E\n", saa->nr));
557                 }
558                 if (astat & SAA7146_PSR_A2_IN) {
559                         IDEBUG(printk("stradis%d irq: A2_IN\n", saa->nr));
560                 }
561                 if (astat & SAA7146_PSR_A2_OUT) {
562                         IDEBUG(printk("stradis%d irq: A2_OUT\n", saa->nr));
563                 }
564                 if (astat & SAA7146_PSR_A1_IN) {
565                         IDEBUG(printk("stradis%d irq: A1_IN\n", saa->nr));
566                 }
567                 if (astat & SAA7146_PSR_A1_OUT) {
568                         IDEBUG(printk("stradis%d irq: A1_OUT\n", saa->nr));
569                 }
570                 if (astat & SAA7146_PSR_AFOU) {
571                         IDEBUG(printk("stradis%d irq: AFOU\n", saa->nr));
572                 }
573                 if (astat & SAA7146_PSR_V_PE) {
574                         IDEBUG(printk("stradis%d irq: V_PE\n", saa->nr));
575                 }
576                 if (astat & SAA7146_PSR_VFOU) {
577                         IDEBUG(printk("stradis%d irq: VFOU\n", saa->nr));
578                 }
579                 if (astat & SAA7146_PSR_FIDA) {
580                         IDEBUG(printk("stradis%d irq: FIDA\n", saa->nr));
581                 }
582                 if (astat & SAA7146_PSR_FIDB) {
583                         IDEBUG(printk("stradis%d irq: FIDB\n", saa->nr));
584                 }
585                 if (astat & SAA7146_PSR_PIN3) {
586                         IDEBUG(printk("stradis%d irq: PIN3\n", saa->nr));
587                 }
588                 if (astat & SAA7146_PSR_PIN2) {
589                         IDEBUG(printk("stradis%d irq: PIN2\n", saa->nr));
590                 }
591                 if (astat & SAA7146_PSR_PIN0) {
592                         IDEBUG(printk("stradis%d irq: PIN0\n", saa->nr));
593                 }
594                 if (astat & SAA7146_PSR_ECS) {
595                         IDEBUG(printk("stradis%d irq: ECS\n", saa->nr));
596                 }
597                 if (astat & SAA7146_PSR_EC3S) {
598                         IDEBUG(printk("stradis%d irq: EC3S\n", saa->nr));
599                 }
600                 if (astat & SAA7146_PSR_EC0S) {
601                         IDEBUG(printk("stradis%d irq: EC0S\n", saa->nr));
602                 }
603 #endif
604                 count++;
605                 if (count > 15)
606                         printk(KERN_WARNING "stradis%d: irq loop %d\n",
607                                saa->nr, count);
608                 if (count > 20) {
609                         saawrite(0, SAA7146_IER);
610                         printk(KERN_ERR
611                                "stradis%d: IRQ loop cleared\n", saa->nr);
612                 }
613         }
614         return IRQ_RETVAL(handled);
615 }
616
617 static int ibm_send_command(struct saa7146 *saa,
618                             int command, int data, int chain)
619 {
620         int i;
621
622         if (chain)
623                 debiwrite(saa, debNormal, IBM_MP2_COMMAND, (command << 1) | 1, 2);
624         else
625                 debiwrite(saa, debNormal, IBM_MP2_COMMAND, command << 1, 2);
626         debiwrite(saa, debNormal, IBM_MP2_CMD_DATA, data, 2);
627         debiwrite(saa, debNormal, IBM_MP2_CMD_STAT, 1, 2);
628         for (i = 0; i < 100 &&
629              (debiread(saa, debNormal, IBM_MP2_CMD_STAT, 2) & 1); i++)
630                 schedule();
631         if (i == 100)
632                 return -1;
633         return 0;
634 }
635
636 static void cs4341_setlevel(struct saa7146 *saa, int left, int right)
637 {
638         I2CWrite(saa, 0x22, 0x03, left > 94 ? 94 : left, 2);
639         I2CWrite(saa, 0x22, 0x04, right > 94 ? 94 : right, 2);
640 }
641
642 static void initialize_cs4341(struct saa7146 *saa)
643 {
644         int i;
645         for (i = 0; i < 200; i++) {
646                 /* auto mute off, power on, no de-emphasis */
647                 /* I2S data up to 24-bit 64xFs internal SCLK */
648                 I2CWrite(saa, 0x22, 0x01, 0x11, 2);
649                 /* ATAPI mixer settings */
650                 I2CWrite(saa, 0x22, 0x02, 0x49, 2);
651                 /* attenuation left 3db */
652                 I2CWrite(saa, 0x22, 0x03, 0x00, 2);
653                 /* attenuation right 3db */
654                 I2CWrite(saa, 0x22, 0x04, 0x00, 2);
655                 I2CWrite(saa, 0x22, 0x01, 0x10, 2);
656                 if (I2CRead(saa, 0x22, 0x02, 1) == 0x49)
657                         break;
658                 schedule();
659         }
660         printk("stradis%d: CS4341 initialized (%d)\n", saa->nr, i);
661         return;
662 }
663
664 static void initialize_cs8420(struct saa7146 *saa, int pro)
665 {
666         int i;
667         u8 *sequence;
668         if (pro)
669                 sequence = mode8420pro;
670         else
671                 sequence = mode8420con;
672         for (i = 0; i < INIT8420LEN; i++)
673                 I2CWrite(saa, 0x20, init8420[i * 2],
674                          init8420[i * 2 + 1], 2);
675         for (i = 0; i < MODE8420LEN; i++)
676                 I2CWrite(saa, 0x20, sequence[i * 2],
677                          sequence[i * 2 + 1], 2);
678         printk("stradis%d: CS8420 initialized\n", saa->nr);
679 }
680
681 static void initialize_saa7121(struct saa7146 *saa, int dopal)
682 {
683         int i, mod;
684         u8 *sequence;
685         if (dopal)
686                 sequence = init7121pal;
687         else
688                 sequence = init7121ntsc;
689         mod = saaread(SAA7146_PSR) & 0x08;
690         /* initialize PAL/NTSC video encoder */
691         for (i = 0; i < INIT7121LEN; i++) {
692                 if (NewCard) {  /* handle new card encoder differences */
693                         if (sequence[i*2] == 0x3a)
694                                 I2CWrite(saa, 0x88, 0x3a, 0x13, 2);
695                         else if (sequence[i*2] == 0x6b)
696                                 I2CWrite(saa, 0x88, 0x6b, 0x20, 2);
697                         else if (sequence[i*2] == 0x6c)
698                                 I2CWrite(saa, 0x88, 0x6c,
699                                          dopal ? 0x09 : 0xf5, 2);
700                         else if (sequence[i*2] == 0x6d)
701                                 I2CWrite(saa, 0x88, 0x6d,
702                                          dopal ? 0x20 : 0x00, 2);
703                         else if (sequence[i*2] == 0x7a)
704                                 I2CWrite(saa, 0x88, 0x7a,
705                                          dopal ? (PALFirstActive - 1) :
706                                          (NTSCFirstActive - 4), 2);
707                         else if (sequence[i*2] == 0x7b)
708                                 I2CWrite(saa, 0x88, 0x7b,
709                                          dopal ? PALLastActive :
710                                          NTSCLastActive, 2);
711                         else I2CWrite(saa, 0x88, sequence[i * 2],
712                                  sequence[i * 2 + 1], 2);
713                 } else {
714                         if (sequence[i*2] == 0x6b && mod)
715                                 I2CWrite(saa, 0x88, 0x6b, 
716                                         (sequence[i * 2 + 1] ^ 0x09), 2);
717                         else if (sequence[i*2] == 0x7a)
718                                 I2CWrite(saa, 0x88, 0x7a,
719                                          dopal ? (PALFirstActive - 1) :
720                                          (NTSCFirstActive - 4), 2);
721                         else if (sequence[i*2] == 0x7b)
722                                 I2CWrite(saa, 0x88, 0x7b,
723                                          dopal ? PALLastActive :
724                                          NTSCLastActive, 2);
725                         else
726                                 I2CWrite(saa, 0x88, sequence[i * 2],
727                                          sequence[i * 2 + 1], 2);
728                 }
729         }
730 }
731
732 static void set_genlock_offset(struct saa7146 *saa, int noffset)
733 {
734         int nCode;
735         int PixelsPerLine = 858;
736         if (CurrentMode == VIDEO_MODE_PAL)
737                 PixelsPerLine = 864;
738         if (noffset > 500)
739                 noffset = 500;
740         else if (noffset < -500)
741                 noffset = -500;
742         nCode = noffset + 0x100;
743         if (nCode == 1)
744                 nCode = 0x401;
745         else if (nCode < 1) nCode = 0x400 + PixelsPerLine + nCode;
746         debiwrite(saa, debNormal, XILINX_GLDELAY, nCode, 2);
747 }
748
749 static void set_out_format(struct saa7146 *saa, int mode)
750 {
751         initialize_saa7121(saa, (mode == VIDEO_MODE_NTSC ? 0 : 1));
752         saa->boardcfg[2] = mode;
753         /* do not adjust analog video parameters here, use saa7121 init */
754         /* you will affect the SDI output on the new card */
755         if (mode == VIDEO_MODE_PAL) {           /* PAL */
756                 debiwrite(saa, debNormal, XILINX_CTL0, 0x0808, 2);
757                 mdelay(50);
758                 saawrite(0x012002c0, SAA7146_NUM_LINE_BYTE1);
759                 if (NewCard) {
760                         debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
761                                   0xe100, 2);
762                         mdelay(50);
763                 }
764                 debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
765                           NewCard ? 0xe500: 0x6500, 2);
766                 debiwrite(saa, debNormal, IBM_MP2_DISP_DLY,
767                           (1 << 8) |
768                           (NewCard ? PALFirstActive : PALFirstActive-6), 2);
769         } else {                /* NTSC */
770                 debiwrite(saa, debNormal, XILINX_CTL0, 0x0800, 2);
771                 mdelay(50);
772                 saawrite(0x00f002c0, SAA7146_NUM_LINE_BYTE1);
773                 debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
774                           NewCard ? 0xe100: 0x6100, 2);
775                 debiwrite(saa, debNormal, IBM_MP2_DISP_DLY,
776                           (1 << 8) |
777                           (NewCard ? NTSCFirstActive : NTSCFirstActive-6), 2);
778         }
779 }
780
781
782 /* Intialize bitmangler to map from a byte value to the mangled word that
783  * must be output to program the Xilinx part through the DEBI port.
784  * Xilinx Data Bit->DEBI Bit: 0->15 1->7 2->6 3->12 4->11 5->2 6->1 7->0
785  * transfer FPGA code, init IBM chip, transfer IBM microcode
786  * rev2 card mangles: 0->7 1->6 2->5 3->4 4->3 5->2 6->1 7->0
787  */
788 static u16 bitmangler[256];
789
790 static int initialize_fpga(struct video_code *bitdata)
791 {
792         int i, num, startindex, failure = 0, loadtwo, loadfile = 0;
793         u16 *dmabuf;
794         u8 *newdma;
795         struct saa7146 *saa;
796
797         /* verify fpga code */
798         for (startindex = 0; startindex < bitdata->datasize; startindex++)
799                 if (bitdata->data[startindex] == 255)
800                         break;
801         if (startindex == bitdata->datasize) {
802                 printk(KERN_INFO "stradis: bad fpga code\n");
803                 return -1;
804         }
805         /* initialize all detected cards */
806         for (num = 0; num < saa_num; num++) {
807                 saa = &saa7146s[num];
808                 if (saa->boardcfg[0] > 20)
809                                 continue;       /* card was programmed */
810                 loadtwo = (saa->boardcfg[18] & 0x10);
811                 if (!NewCard)   /* we have an old board */
812                         for (i = 0; i < 256; i++)
813                             bitmangler[i] = ((i & 0x01) << 15) |
814                                 ((i & 0x02) << 6) | ((i & 0x04) << 4) |
815                                 ((i & 0x08) << 9) | ((i & 0x10) << 7) |
816                                 ((i & 0x20) >> 3) | ((i & 0x40) >> 5) |
817                                 ((i & 0x80) >> 7);
818                 else    /* else we have a new board */
819                         for (i = 0; i < 256; i++)
820                             bitmangler[i] = ((i & 0x01) << 7) |
821                                 ((i & 0x02) << 5) | ((i & 0x04) << 3) |
822                                 ((i & 0x08) << 1) | ((i & 0x10) >> 1) |
823                                 ((i & 0x20) >> 3) | ((i & 0x40) >> 5) |
824                                 ((i & 0x80) >> 7);
825
826                 dmabuf = (u16 *) saa->dmadebi;
827                 newdma = (u8 *) saa->dmadebi;
828                 if (NewCard) {  /* SDM2xxx */
829                         if (!strncmp(bitdata->loadwhat, "decoder2", 8))
830                                 continue;       /* fpga not for this card */
831                         if (!strncmp(&saa->boardcfg[42],
832                                      bitdata->loadwhat, 8)) {
833                                 loadfile = 1;
834                         } else if (loadtwo && !strncmp(&saa->boardcfg[19],
835                                    bitdata->loadwhat, 8)) {
836                                 loadfile = 2;
837                         } else if (!saa->boardcfg[42] &&        /* special */
838                                    !strncmp("decxl", bitdata->loadwhat, 8)) {
839                                 loadfile = 1;
840                         } else
841                                 continue;       /* fpga not for this card */
842                         if (loadfile != 1 && loadfile != 2) {
843                                 continue;       /* skip to next card */
844                         }
845                         if (saa->boardcfg[0] && loadfile == 1 )
846                                 continue;       /* skip to next card */
847                         if (saa->boardcfg[0] != 1 && loadfile == 2)
848                                 continue;       /* skip to next card */
849                         saa->boardcfg[0]++;     /* mark fpga handled */
850                         printk("stradis%d: loading %s\n", saa->nr,
851                                 bitdata->loadwhat);
852                         if (loadtwo && loadfile == 2)
853                                 goto send_fpga_stuff;
854                         /* turn on the Audio interface to set PROG low */
855                         saawrite(0x00400040, SAA7146_GPIO_CTRL);
856                         saaread(SAA7146_PSR);   /* ensure posted write */
857                         /* wait for everyone to reset */
858                         mdelay(10);
859                         saawrite(0x00400000, SAA7146_GPIO_CTRL);
860                 } else {        /* original card */
861                         if (strncmp(bitdata->loadwhat, "decoder2", 8))
862                                 continue;       /* fpga not for this card */
863                         /* Pull the Xilinx PROG signal WS3 low */
864                         saawrite(0x02000200, SAA7146_MC1);
865                         /* Turn on the Audio interface so can set PROG low */
866                         saawrite(0x000000c0, SAA7146_ACON1);
867                         /* Pull the Xilinx INIT signal (GPIO2) low */
868                         saawrite(0x00400000, SAA7146_GPIO_CTRL);
869                         /* Make sure everybody resets */
870                         saaread(SAA7146_PSR);   /* ensure posted write */
871                         mdelay(10);
872                         /* Release the Xilinx PROG signal */
873                         saawrite(0x00000000, SAA7146_ACON1);
874                         /* Turn off the Audio interface */
875                         saawrite(0x02000000, SAA7146_MC1);
876                 }
877                 /* Release Xilinx INIT signal (WS2) */
878                 saawrite(0x00000000, SAA7146_GPIO_CTRL);
879                 /* Wait for the INIT to go High */
880                 for (i = 0; i < 10000 &&
881                      !(saaread(SAA7146_PSR) & SAA7146_PSR_PIN2); i++)
882                         schedule();
883                 if (i == 1000) {
884                         printk(KERN_INFO "stradis%d: no fpga INIT\n", saa->nr);
885                         return -1;
886                 }
887 send_fpga_stuff:
888                 if (NewCard) {
889                         for (i = startindex; i < bitdata->datasize; i++)
890                                 newdma[i - startindex] =
891                                         bitmangler[bitdata->data[i]];
892                         debiwrite(saa, 0x01420000, 0, 0,
893                                 ((bitdata->datasize - startindex) + 5));
894                         if (loadtwo) {
895                                 if (loadfile == 1) {
896                                         printk("stradis%d: "
897                                                 "awaiting 2nd FPGA bitfile\n",
898                                                 saa->nr);
899                                         continue;       /* skip to next card */
900                                 }
901
902                         }
903                 } else {
904                         for (i = startindex; i < bitdata->datasize; i++)
905                                 dmabuf[i - startindex] =
906                                         bitmangler[bitdata->data[i]];
907                         debiwrite(saa, 0x014a0000, 0, 0,
908                                 ((bitdata->datasize - startindex) + 5) * 2);
909                 }
910                 for (i = 0; i < 1000 &&
911                      !(saaread(SAA7146_PSR) & SAA7146_PSR_PIN2); i++)
912                         schedule();
913                 if (i == 1000) {
914                         printk(KERN_INFO "stradis%d: FPGA load failed\n",
915                                saa->nr);
916                         failure++;
917                         continue;
918                 }
919                 if (!NewCard) {
920                         /* Pull the Xilinx INIT signal (GPIO2) low */
921                         saawrite(0x00400000, SAA7146_GPIO_CTRL);
922                         saaread(SAA7146_PSR);   /* ensure posted write */
923                         mdelay(2);
924                         saawrite(0x00000000, SAA7146_GPIO_CTRL);
925                         mdelay(2);
926                 }
927                 printk(KERN_INFO "stradis%d: FPGA Loaded\n", saa->nr);
928                 saa->boardcfg[0] = 26;  /* mark fpga programmed */
929                 /* set VXCO to its lowest frequency */
930                 debiwrite(saa, debNormal, XILINX_PWM, 0, 2);
931                 if (NewCard) {
932                         /* mute CS3310 */
933                         if (HaveCS3310)
934                                 debiwrite(saa, debNormal, XILINX_CS3310_CMPLT,
935                                           0, 2);
936                         /* set VXCO to PWM mode, release reset, blank on */
937                         debiwrite(saa, debNormal, XILINX_CTL0, 0xffc4, 2);
938                         mdelay(10);
939                         /* unmute CS3310 */
940                         if (HaveCS3310)
941                                 debiwrite(saa, debNormal, XILINX_CTL0,
942                                           0x2020, 2);
943                 }
944                 /* set source Black */
945                 debiwrite(saa, debNormal, XILINX_CTL0, 0x1707, 2);
946                 saa->boardcfg[4] = 22;  /* set NTSC First Active Line */
947                 saa->boardcfg[5] = 23;  /* set PAL First Active Line */
948                 saa->boardcfg[54] = 2;  /* set NTSC Last Active Line - 256 */
949                 saa->boardcfg[55] = 54; /* set PAL Last Active Line - 256 */
950                 set_out_format(saa, VIDEO_MODE_NTSC);
951                 mdelay(50);
952                 /* begin IBM chip init */
953                 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 4, 2);
954                 saaread(SAA7146_PSR);   /* wait for reset */
955                 mdelay(5);
956                 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0, 2);
957                 debiread(saa, debNormal, IBM_MP2_CHIP_CONTROL, 2);
958                 debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0x10, 2);
959                 debiwrite(saa, debNormal, IBM_MP2_CMD_ADDR, 0, 2);
960                 debiwrite(saa, debNormal, IBM_MP2_CHIP_MODE, 0x2e, 2);
961                 if (NewCard) {
962                         mdelay(5);
963                         /* set i2s rate converter to 48KHz */
964                         debiwrite(saa, debNormal, 0x80c0, 6, 2);
965                         /* we must init CS8420 first since rev b pulls i2s */
966                         /* master clock low and CS4341 needs i2s master to */
967                         /* run the i2c port. */
968                         if (HaveCS8420) {
969                                 /* 0=consumer, 1=pro */
970                                 initialize_cs8420(saa, 0);
971                         }
972                         mdelay(5);
973                         if (HaveCS4341)
974                                 initialize_cs4341(saa);
975                 }
976                 debiwrite(saa, debNormal, IBM_MP2_INFC_CTL, 0x48, 2);
977                 debiwrite(saa, debNormal, IBM_MP2_BEEP_CTL, 0xa000, 2);
978                 debiwrite(saa, debNormal, IBM_MP2_DISP_LBOR, 0, 2);
979                 debiwrite(saa, debNormal, IBM_MP2_DISP_TBOR, 0, 2);
980                 if (NewCard)
981                         set_genlock_offset(saa, 0);
982                 debiwrite(saa, debNormal, IBM_MP2_FRNT_ATTEN, 0, 2);
983 #if 0
984                 /* enable genlock */
985                 debiwrite(saa, debNormal, XILINX_CTL0, 0x8000, 2);
986 #else
987                 /* disable genlock */
988                 debiwrite(saa, debNormal, XILINX_CTL0, 0x8080, 2);
989 #endif
990         }
991         return failure;
992 }
993
994 static int do_ibm_reset(struct saa7146 *saa)
995 {
996         /* failure if decoder not previously programmed */
997         if (saa->boardcfg[0] < 37)
998                 return -EIO;
999         /* mute CS3310 */
1000         if (HaveCS3310)
1001                 debiwrite(saa, debNormal, XILINX_CS3310_CMPLT, 0, 2);
1002         /* disable interrupts */
1003         saawrite(0, SAA7146_IER);
1004         saa->audhead = saa->audtail = 0;
1005         saa->vidhead = saa->vidtail = 0;
1006         /* tristate debi bus, disable debi transfers */
1007         saawrite(0x00880000, SAA7146_MC1);
1008         /* ensure posted write */
1009         saaread(SAA7146_MC1);
1010         mdelay(50);
1011         /* re-enable debi transfers */
1012         saawrite(0x00880088, SAA7146_MC1);
1013         /* set source Black */
1014         debiwrite(saa, debNormal, XILINX_CTL0, 0x1707, 2);
1015         /* begin IBM chip init */
1016         set_out_format(saa, CurrentMode);
1017         debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 4, 2);
1018         saaread(SAA7146_PSR);   /* wait for reset */
1019         mdelay(5);
1020         debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0, 2);
1021         debiread(saa, debNormal, IBM_MP2_CHIP_CONTROL, 2);
1022         debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, ChipControl, 2);
1023         debiwrite(saa, debNormal, IBM_MP2_CHIP_MODE, 0x2e, 2);
1024         if (NewCard) {
1025                 mdelay(5);
1026                 /* set i2s rate converter to 48KHz */
1027                 debiwrite(saa, debNormal, 0x80c0, 6, 2);
1028                 /* we must init CS8420 first since rev b pulls i2s */
1029                 /* master clock low and CS4341 needs i2s master to */
1030                 /* run the i2c port. */
1031                 if (HaveCS8420) {
1032                         /* 0=consumer, 1=pro */
1033                         initialize_cs8420(saa, 1);
1034                 }
1035                 mdelay(5);
1036                 if (HaveCS4341)
1037                         initialize_cs4341(saa);
1038         }
1039         debiwrite(saa, debNormal, IBM_MP2_INFC_CTL, 0x48, 2);
1040         debiwrite(saa, debNormal, IBM_MP2_BEEP_CTL, 0xa000, 2);
1041         debiwrite(saa, debNormal, IBM_MP2_DISP_LBOR, 0, 2);
1042         debiwrite(saa, debNormal, IBM_MP2_DISP_TBOR, 0, 2);
1043         if (NewCard)
1044                 set_genlock_offset(saa, 0);
1045         debiwrite(saa, debNormal, IBM_MP2_FRNT_ATTEN, 0, 2);
1046         debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0x2000, 2);
1047         debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4552, 2);
1048         if (ibm_send_command(saa, IBM_MP2_CONFIG_DECODER,
1049                 (ChipControl == 0x43 ? 0xe800 : 0xe000), 1)) {
1050                 printk(KERN_ERR "stradis%d: IBM config failed\n", saa->nr);
1051         }
1052         if (HaveCS3310) {
1053                 int i = CS3310MaxLvl;
1054                 debiwrite(saa, debNormal, XILINX_CS3310_CMPLT, ((i<<8)|i), 2);
1055         }
1056         /* start video decoder */
1057         debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, ChipControl, 2);
1058         /* 256k vid, 3520 bytes aud */
1059         debiwrite(saa, debNormal, IBM_MP2_RB_THRESHOLD, 0x4037, 2);
1060         debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4573, 2);
1061         ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
1062         /* enable buffer threshold irq */
1063         debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
1064         /* clear pending interrupts */
1065         debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
1066         debiwrite(saa, debNormal, XILINX_CTL0, 0x1711, 2);
1067         return 0;
1068 }
1069
1070 /* load the decoder microcode */
1071 static int initialize_ibmmpeg2(struct video_code *microcode)
1072 {
1073         int i, num;
1074         struct saa7146 *saa;
1075
1076         for (num = 0; num < saa_num; num++) {
1077                 saa = &saa7146s[num];
1078                 /* check that FPGA is loaded */
1079                 debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0xa55a, 2);
1080                 if ((i = debiread(saa, debNormal, IBM_MP2_OSD_SIZE, 2)) !=
1081                      0xa55a) {
1082                         printk(KERN_INFO "stradis%d: %04x != 0xa55a\n",
1083                                 saa->nr, i);
1084 #if 0
1085                         return -1;
1086 #endif
1087                 }
1088                 if (!strncmp(microcode->loadwhat, "decoder.vid", 11)) {
1089                         if (saa->boardcfg[0] > 27)
1090                                 continue;       /* skip to next card */
1091                         /* load video control store */
1092                         saa->boardcfg[1] = 0x13;  /* no-sync default */
1093                         debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 1, 2);
1094                         debiwrite(saa, debNormal, IBM_MP2_PROC_IADDR, 0, 2);
1095                         for (i = 0; i < microcode->datasize / 2; i++)
1096                                 debiwrite(saa, debNormal, IBM_MP2_PROC_IDATA,
1097                                         (microcode->data[i * 2] << 8) |
1098                                          microcode->data[i * 2 + 1], 2);
1099                         debiwrite(saa, debNormal, IBM_MP2_PROC_IADDR, 0, 2);
1100                         debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 0, 2);
1101                         debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
1102                                   ChipControl, 2);
1103                         saa->boardcfg[0] = 28;
1104                 }
1105                 if (!strncmp(microcode->loadwhat, "decoder.aud", 11)) {
1106                         if (saa->boardcfg[0] > 35)
1107                                 continue;       /* skip to next card */
1108                         /* load audio control store */
1109                         debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 1, 2);
1110                         debiwrite(saa, debNormal, IBM_MP2_AUD_IADDR, 0, 2);
1111                         for (i = 0; i < microcode->datasize; i++)
1112                                 debiwrite(saa, debNormal, IBM_MP2_AUD_IDATA,
1113                                         microcode->data[i], 1);
1114                         debiwrite(saa, debNormal, IBM_MP2_AUD_IADDR, 0, 2);
1115                         debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 0, 2);
1116                         debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0x2000, 2);
1117                         debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4552, 2);
1118                         if (ibm_send_command(saa, IBM_MP2_CONFIG_DECODER,
1119                             0xe000, 1)) {
1120                                 printk(KERN_ERR
1121                                        "stradis%d: IBM config failed\n",
1122                                        saa->nr);
1123                                 return -1;
1124                         }
1125                         /* set PWM to center value */
1126                         if (NewCard) {
1127                                 debiwrite(saa, debNormal, XILINX_PWM,
1128                                           saa->boardcfg[14] +
1129                                           (saa->boardcfg[13]<<8), 2);
1130                         } else
1131                                 debiwrite(saa, debNormal, XILINX_PWM,
1132                                           0x46, 2);
1133                         if (HaveCS3310) {
1134                                 i = CS3310MaxLvl;
1135                                 debiwrite(saa, debNormal,
1136                                         XILINX_CS3310_CMPLT, ((i<<8)|i), 2);
1137                         }
1138                         printk(KERN_INFO
1139                                "stradis%d: IBM MPEGCD%d Initialized\n",
1140                                saa->nr, 18 + (debiread(saa, debNormal,
1141                                IBM_MP2_CHIP_CONTROL, 2) >> 12));
1142                         /* start video decoder */
1143                         debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
1144                                 ChipControl, 2);
1145                         debiwrite(saa, debNormal, IBM_MP2_RB_THRESHOLD,
1146                                 0x4037, 2);     /* 256k vid, 3520 bytes aud */
1147                         debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4573, 2);
1148                         ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
1149                         /* enable buffer threshold irq */
1150                         debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
1151                         debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
1152                         /* enable gpio irq */
1153                         saawrite(0x00002000, SAA7146_GPIO_CTRL);
1154                         /* enable decoder output to HPS */
1155                         debiwrite(saa, debNormal, XILINX_CTL0, 0x1711, 2);
1156                         saa->boardcfg[0] = 37;
1157                 }
1158         }
1159         return 0;
1160 }
1161
1162 static u32 palette2fmt[] =
1163 {                               /* some of these YUV translations are wrong */
1164   0xffffffff, 0x86000000, 0x87000000, 0x80000000, 0x8100000, 0x82000000,
1165   0x83000000, 0x00000000, 0x03000000, 0x03000000, 0x0a00000, 0x03000000,
1166   0x06000000, 0x00000000, 0x03000000, 0x0a000000, 0x0300000
1167 };
1168 static int bpp2fmt[4] =
1169 {
1170         VIDEO_PALETTE_HI240, VIDEO_PALETTE_RGB565, VIDEO_PALETTE_RGB24,
1171         VIDEO_PALETTE_RGB32
1172 };
1173
1174 /* I wish I could find a formula to calculate these... */
1175 static u32 h_prescale[64] =
1176 {
1177   0x10000000, 0x18040202, 0x18080000, 0x380c0606, 0x38100204, 0x38140808,
1178   0x38180000, 0x381c0000, 0x3820161c, 0x38242a3b, 0x38281230, 0x382c4460,
1179   0x38301040, 0x38340080, 0x38380000, 0x383c0000, 0x3840fefe, 0x3844ee9f,
1180   0x3848ee9f, 0x384cee9f, 0x3850ee9f, 0x38542a3b, 0x38581230, 0x385c0000,
1181   0x38600000, 0x38640000, 0x38680000, 0x386c0000, 0x38700000, 0x38740000,
1182   0x38780000, 0x387c0000, 0x30800000, 0x38840000, 0x38880000, 0x388c0000,
1183   0x38900000, 0x38940000, 0x38980000, 0x389c0000, 0x38a00000, 0x38a40000,
1184   0x38a80000, 0x38ac0000, 0x38b00000, 0x38b40000, 0x38b80000, 0x38bc0000,
1185   0x38c00000, 0x38c40000, 0x38c80000, 0x38cc0000, 0x38d00000, 0x38d40000,
1186   0x38d80000, 0x38dc0000, 0x38e00000, 0x38e40000, 0x38e80000, 0x38ec0000,
1187   0x38f00000, 0x38f40000, 0x38f80000, 0x38fc0000,
1188 };
1189 static u32 v_gain[64] =
1190 {
1191   0x016000ff, 0x016100ff, 0x016100ff, 0x016200ff, 0x016200ff, 0x016200ff,
1192   0x016200ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff,
1193   0x016300ff, 0x016300ff, 0x016300ff, 0x016400ff, 0x016400ff, 0x016400ff,
1194   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1195   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1196   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1197   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1198   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1199   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1200   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1201   0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1202 };
1203
1204
1205 static void saa7146_set_winsize(struct saa7146 *saa)
1206 {
1207         u32 format;
1208         int offset, yacl, ysci;
1209         saa->win.color_fmt = format =
1210             (saa->win.depth == 15) ? palette2fmt[VIDEO_PALETTE_RGB555] :
1211             palette2fmt[bpp2fmt[(saa->win.bpp - 1) & 3]];
1212         offset = saa->win.x * saa->win.bpp + saa->win.y * saa->win.bpl;
1213         saawrite(saa->win.vidadr + offset, SAA7146_BASE_EVEN1);
1214         saawrite(saa->win.vidadr + offset + saa->win.bpl, SAA7146_BASE_ODD1);
1215         saawrite(saa->win.bpl * 2, SAA7146_PITCH1);
1216         saawrite(saa->win.vidadr + saa->win.bpl * saa->win.sheight,
1217                  SAA7146_PROT_ADDR1);
1218         saawrite(0, SAA7146_PAGE1);
1219         saawrite(format|0x60, SAA7146_CLIP_FORMAT_CTRL);
1220         offset = (704 / (saa->win.width - 1)) & 0x3f;
1221         saawrite(h_prescale[offset], SAA7146_HPS_H_PRESCALE);
1222         offset = (720896 / saa->win.width) / (offset + 1);
1223         saawrite((offset<<12)|0x0c, SAA7146_HPS_H_SCALE);
1224         if (CurrentMode == VIDEO_MODE_NTSC) {
1225                 yacl = /*(480 / saa->win.height - 1) & 0x3f*/ 0;
1226                 ysci = 1024 - (saa->win.height * 1024 / 480);
1227         } else {
1228                 yacl = /*(576 / saa->win.height - 1) & 0x3f*/ 0;
1229                 ysci = 1024 - (saa->win.height * 1024 / 576);
1230         }
1231         saawrite((1<<31)|(ysci<<21)|(yacl<<15), SAA7146_HPS_V_SCALE);
1232         saawrite(v_gain[yacl], SAA7146_HPS_V_GAIN);
1233         saawrite(((SAA7146_MC2_UPLD_DMA1 | SAA7146_MC2_UPLD_HPS_V |
1234                    SAA7146_MC2_UPLD_HPS_H) << 16) | (SAA7146_MC2_UPLD_DMA1 |
1235                    SAA7146_MC2_UPLD_HPS_V | SAA7146_MC2_UPLD_HPS_H),
1236                    SAA7146_MC2);
1237 }
1238
1239 /* clip_draw_rectangle(cm,x,y,w,h) -- handle clipping an area
1240  * bitmap is fixed width, 128 bytes (1024 pixels represented) 
1241  * arranged most-sigificant-bit-left in 32-bit words 
1242  * based on saa7146 clipping hardware, it swaps bytes if LE 
1243  * much of this makes up for egcs brain damage -- so if you
1244  * are wondering "why did he do this?" it is because the C
1245  * was adjusted to generate the optimal asm output without
1246  * writing non-portable __asm__ directives.
1247  */
1248
1249 static void clip_draw_rectangle(u32 *clipmap, int x, int y, int w, int h)
1250 {
1251         register int startword, endword;
1252         register u32 bitsleft, bitsright;
1253         u32 *temp;
1254         if (x < 0) {
1255                 w += x;
1256                 x = 0;
1257         }
1258         if (y < 0) {
1259                 h += y;
1260                 y = 0;
1261         }
1262         if (w <= 0 || h <= 0 || x > 1023 || y > 639)
1263                 return;         /* throw away bad clips */
1264         if (x + w > 1024)
1265                 w = 1024 - x;
1266         if (y + h > 640)
1267                 h = 640 - y;
1268         startword = (x >> 5);
1269         endword = ((x + w) >> 5);
1270         bitsleft = (0xffffffff >> (x & 31));
1271         bitsright = (0xffffffff << (~((x + w) - (endword<<5))));
1272         temp = &clipmap[(y<<5) + startword];
1273         w = endword - startword;
1274         if (!w) {
1275                 bitsleft |= bitsright;
1276                 for (y = 0; y < h; y++) {
1277                         *temp |= bitsleft;
1278                         temp += 32;
1279                 }
1280         } else {
1281                 for (y = 0; y < h; y++) {
1282                         *temp++ |= bitsleft;
1283                         for (x = 1; x < w; x++)
1284                                 *temp++ = 0xffffffff;
1285                         *temp |= bitsright;
1286                         temp += (32 - w);
1287                 }
1288         }
1289 }
1290
1291 static void make_clip_tab(struct saa7146 *saa, struct video_clip *cr, int ncr)
1292 {
1293         int i, width, height;
1294         u32 *clipmap;
1295
1296         clipmap = saa->dmavid2;
1297         if((width=saa->win.width)>1023)
1298                 width = 1023;           /* sanity check */
1299         if((height=saa->win.height)>640)
1300                 height = 639;           /* sanity check */
1301         if (ncr > 0) {  /* rectangles pased */
1302                 /* convert rectangular clips to a bitmap */
1303                 memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
1304                 for (i = 0; i < ncr; i++)
1305                         clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
1306                                 cr[i].width, cr[i].height);
1307         }
1308         /* clip against viewing window AND screen 
1309            so we do not have to rely on the user program
1310          */
1311         clip_draw_rectangle(clipmap,(saa->win.x+width>saa->win.swidth) ?
1312                 (saa->win.swidth-saa->win.x) : width, 0, 1024, 768);
1313         clip_draw_rectangle(clipmap,0,(saa->win.y+height>saa->win.sheight) ?
1314                 (saa->win.sheight-saa->win.y) : height,1024,768);
1315         if (saa->win.x<0)
1316                 clip_draw_rectangle(clipmap, 0, 0, -(saa->win.x), 768);
1317         if (saa->win.y<0)
1318                 clip_draw_rectangle(clipmap, 0, 0, 1024, -(saa->win.y));
1319 }
1320
1321 static int saa_ioctl(struct inode *inode, struct file *file,
1322                      unsigned int cmd, unsigned long argl)
1323 {
1324         struct saa7146 *saa = file->private_data;
1325         void *arg = (void *)argl;
1326
1327         switch (cmd) {
1328         case VIDIOCGCAP:
1329                 {
1330                         struct video_capability b;
1331                         strcpy(b.name, saa->video_dev.name);
1332                         b.type = VID_TYPE_CAPTURE |
1333                             VID_TYPE_OVERLAY |
1334                             VID_TYPE_CLIPPING |
1335                             VID_TYPE_FRAMERAM |
1336                             VID_TYPE_SCALES;
1337                         b.channels = 1;
1338                         b.audios = 1;
1339                         b.maxwidth = 768;
1340                         b.maxheight = 576;
1341                         b.minwidth = 32;
1342                         b.minheight = 32;
1343                         if (copy_to_user(arg, &b, sizeof(b)))
1344                                 return -EFAULT;
1345                         return 0;
1346                 }
1347         case VIDIOCGPICT:
1348                 {
1349                         struct video_picture p = saa->picture;
1350                         if (saa->win.depth == 8)
1351                                 p.palette = VIDEO_PALETTE_HI240;
1352                         if (saa->win.depth == 15)
1353                                 p.palette = VIDEO_PALETTE_RGB555;
1354                         if (saa->win.depth == 16)
1355                                 p.palette = VIDEO_PALETTE_RGB565;
1356                         if (saa->win.depth == 24)
1357                                 p.palette = VIDEO_PALETTE_RGB24;
1358                         if (saa->win.depth == 32)
1359                                 p.palette = VIDEO_PALETTE_RGB32;
1360                         if (copy_to_user(arg, &p, sizeof(p)))
1361                                 return -EFAULT;
1362                         return 0;
1363                 }
1364         case VIDIOCSPICT:
1365                 {
1366                         struct video_picture p;
1367                         u32 format;
1368                         if (copy_from_user(&p, arg, sizeof(p)))
1369                                 return -EFAULT;
1370                         if (p.palette < sizeof(palette2fmt) / sizeof(u32)) {
1371                                 format = palette2fmt[p.palette];
1372                                 saa->win.color_fmt = format;
1373                                 saawrite(format|0x60, SAA7146_CLIP_FORMAT_CTRL);
1374                         }
1375                         saawrite(((p.brightness & 0xff00) << 16) |
1376                                  ((p.contrast & 0xfe00) << 7) |
1377                              ((p.colour & 0xfe00) >> 9), SAA7146_BCS_CTRL);
1378                         saa->picture = p;
1379                         /* upload changed registers */
1380                         saawrite(((SAA7146_MC2_UPLD_HPS_H |
1381                                  SAA7146_MC2_UPLD_HPS_V) << 16) |
1382                                 SAA7146_MC2_UPLD_HPS_H | SAA7146_MC2_UPLD_HPS_V,
1383                                  SAA7146_MC2);
1384                         return 0;
1385                 }
1386         case VIDIOCSWIN:
1387                 {
1388                         struct video_window vw;
1389                         struct video_clip *vcp = NULL;
1390
1391                         if (copy_from_user(&vw, arg, sizeof(vw)))
1392                                 return -EFAULT;
1393
1394                         if (vw.flags || vw.width < 16 || vw.height < 16) {      /* stop capture */
1395                                 saawrite((SAA7146_MC1_TR_E_1 << 16), SAA7146_MC1);
1396                                 return -EINVAL;
1397                         }
1398                         if (saa->win.bpp < 4) {         /* 32-bit align start and adjust width */
1399                                 int i = vw.x;
1400                                 vw.x = (vw.x + 3) & ~3;
1401                                 i = vw.x - i;
1402                                 vw.width -= i;
1403                         }
1404                         saa->win.x = vw.x;
1405                         saa->win.y = vw.y;
1406                         saa->win.width = vw.width;
1407                         if (saa->win.width > 768)
1408                                 saa->win.width = 768;
1409                         saa->win.height = vw.height;
1410                         if (CurrentMode == VIDEO_MODE_NTSC) {
1411                                 if (saa->win.height > 480)
1412                                         saa->win.height = 480;
1413                         } else {
1414                                 if (saa->win.height > 576)
1415                                         saa->win.height = 576;
1416                         }
1417
1418                         /* stop capture */
1419                         saawrite((SAA7146_MC1_TR_E_1 << 16), SAA7146_MC1);
1420                         saa7146_set_winsize(saa);
1421
1422                         /*
1423                          *    Do any clips.
1424                          */
1425                         if (vw.clipcount < 0) {
1426                                 if (copy_from_user(saa->dmavid2, vw.clips,
1427                                                    VIDEO_CLIPMAP_SIZE))
1428                                         return -EFAULT;
1429                         }
1430                         else if (vw.clipcount > 16384) {
1431                                 return -EINVAL;
1432                         } else if (vw.clipcount > 0) {
1433                                 if ((vcp = vmalloc(sizeof(struct video_clip) *
1434                                                 (vw.clipcount))) == NULL)
1435                                          return -ENOMEM;
1436                                 if (copy_from_user(vcp, vw.clips,
1437                                               sizeof(struct video_clip) *
1438                                                    vw.clipcount)) {
1439                                         vfree(vcp);
1440                                         return -EFAULT;
1441                                 }
1442                         } else  /* nothing clipped */
1443                                 memset(saa->dmavid2, 0, VIDEO_CLIPMAP_SIZE);
1444                         make_clip_tab(saa, vcp, vw.clipcount);
1445                         if (vw.clipcount > 0)
1446                                 vfree(vcp);
1447
1448                         /* start capture & clip dma if we have an address */
1449                         if ((saa->cap & 3) && saa->win.vidadr != 0)
1450                                 saawrite(((SAA7146_MC1_TR_E_1 |
1451                                         SAA7146_MC1_TR_E_2) << 16) | 0xffff,
1452                                         SAA7146_MC1);
1453                         return 0;
1454                 }
1455         case VIDIOCGWIN:
1456                 {
1457                         struct video_window vw;
1458                         vw.x = saa->win.x;
1459                         vw.y = saa->win.y;
1460                         vw.width = saa->win.width;
1461                         vw.height = saa->win.height;
1462                         vw.chromakey = 0;
1463                         vw.flags = 0;
1464                         if (copy_to_user(arg, &vw, sizeof(vw)))
1465                                 return -EFAULT;
1466                         return 0;
1467                 }
1468         case VIDIOCCAPTURE:
1469                 {
1470                         int v;
1471                         if (copy_from_user(&v, arg, sizeof(v)))
1472                                 return -EFAULT;
1473                         if (v == 0) {
1474                                 saa->cap &= ~1;
1475                                 saawrite((SAA7146_MC1_TR_E_1 << 16),
1476                                          SAA7146_MC1);
1477                         } else {
1478                                 if (saa->win.vidadr == 0 || saa->win.width == 0
1479                                     || saa->win.height == 0)
1480                                         return -EINVAL;
1481                                 saa->cap |= 1;
1482                                 saawrite((SAA7146_MC1_TR_E_1 << 16) | 0xffff,
1483                                          SAA7146_MC1);
1484                         }
1485                         return 0;
1486                 }
1487         case VIDIOCGFBUF:
1488                 {
1489                         struct video_buffer v;
1490                         v.base = (void *) saa->win.vidadr;
1491                         v.height = saa->win.sheight;
1492                         v.width = saa->win.swidth;
1493                         v.depth = saa->win.depth;
1494                         v.bytesperline = saa->win.bpl;
1495                         if (copy_to_user(arg, &v, sizeof(v)))
1496                                 return -EFAULT;
1497                         return 0;
1498
1499                 }
1500         case VIDIOCSFBUF:
1501                 {
1502                         struct video_buffer v;
1503                         if (!capable(CAP_SYS_ADMIN))
1504                                 return -EPERM;
1505                         if (copy_from_user(&v, arg, sizeof(v)))
1506                                 return -EFAULT;
1507                         if (v.depth != 8 && v.depth != 15 && v.depth != 16 &&
1508                         v.depth != 24 && v.depth != 32 && v.width > 16 &&
1509                             v.height > 16 && v.bytesperline > 16)
1510                                 return -EINVAL;
1511                         if (v.base)
1512                                 saa->win.vidadr = (unsigned long) v.base;
1513                         saa->win.sheight = v.height;
1514                         saa->win.swidth = v.width;
1515                         saa->win.bpp = ((v.depth + 7) & 0x38) / 8;
1516                         saa->win.depth = v.depth;
1517                         saa->win.bpl = v.bytesperline;
1518
1519                         DEBUG(printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1520                                      v.base, v.width, v.height, saa->win.bpp, saa->win.bpl));
1521                         saa7146_set_winsize(saa);
1522                         return 0;
1523                 }
1524         case VIDIOCKEY:
1525                 {
1526                         /* Will be handled higher up .. */
1527                         return 0;
1528                 }
1529
1530         case VIDIOCGAUDIO:
1531                 {
1532                         struct video_audio v;
1533                         v = saa->audio_dev;
1534                         v.flags &= ~(VIDEO_AUDIO_MUTE | VIDEO_AUDIO_MUTABLE);
1535                         v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
1536                         strcpy(v.name, "MPEG");
1537                         v.mode = VIDEO_SOUND_STEREO;
1538                         if (copy_to_user(arg, &v, sizeof(v)))
1539                                 return -EFAULT;
1540                         return 0;
1541                 }
1542         case VIDIOCSAUDIO:
1543                 {
1544                         struct video_audio v;
1545                         int i;
1546                         if (copy_from_user(&v, arg, sizeof(v)))
1547                                 return -EFAULT;
1548                         i = (~(v.volume>>8))&0xff;
1549                         if (!HaveCS4341) {
1550                                 if (v.flags & VIDEO_AUDIO_MUTE) {
1551                                         debiwrite(saa, debNormal,
1552                                                 IBM_MP2_FRNT_ATTEN,
1553                                                 0xffff, 2);
1554                                 }
1555                                 if (!(v.flags & VIDEO_AUDIO_MUTE))
1556                                         debiwrite(saa, debNormal,
1557                                                 IBM_MP2_FRNT_ATTEN,
1558                                                   0x0000, 2);
1559                                 if (v.flags & VIDEO_AUDIO_VOLUME)
1560                                         debiwrite(saa, debNormal,
1561                                                 IBM_MP2_FRNT_ATTEN,
1562                                                 (i<<8)|i, 2);
1563                         } else {
1564                                 if (v.flags & VIDEO_AUDIO_MUTE)
1565                                         cs4341_setlevel(saa, 0xff, 0xff);
1566                                 if (!(v.flags & VIDEO_AUDIO_MUTE))
1567                                         cs4341_setlevel(saa, 0, 0);
1568                                 if (v.flags & VIDEO_AUDIO_VOLUME)
1569                                         cs4341_setlevel(saa, i, i);
1570                         }
1571                         saa->audio_dev = v;
1572                         return 0;
1573                 }
1574
1575         case VIDIOCGUNIT:
1576                 {
1577                         struct video_unit vu;
1578                         vu.video = saa->video_dev.minor;
1579                         vu.vbi = VIDEO_NO_UNIT;
1580                         vu.radio = VIDEO_NO_UNIT;
1581                         vu.audio = VIDEO_NO_UNIT;
1582                         vu.teletext = VIDEO_NO_UNIT;
1583                         if (copy_to_user((void *) arg, (void *) &vu, sizeof(vu)))
1584                                 return -EFAULT;
1585                         return 0;
1586                 }
1587         case VIDIOCSPLAYMODE:
1588                 {
1589                         struct video_play_mode pmode;
1590                         if (copy_from_user((void *) &pmode, arg,
1591                                 sizeof(struct video_play_mode)))
1592                                 return -EFAULT;
1593                         switch (pmode.mode) {
1594                                 case VID_PLAY_VID_OUT_MODE:
1595                                         if (pmode.p1 != VIDEO_MODE_NTSC &&
1596                                                 pmode.p1 != VIDEO_MODE_PAL)
1597                                                 return -EINVAL;
1598                                         set_out_format(saa, pmode.p1);
1599                                         return 0;
1600                                 case VID_PLAY_GENLOCK:
1601                                         debiwrite(saa, debNormal,
1602                                                   XILINX_CTL0,
1603                                                   (pmode.p1 ? 0x8000 : 0x8080),
1604                                                   2);
1605                                         if (NewCard)
1606                                                 set_genlock_offset(saa,
1607                                                         pmode.p2);
1608                                         return 0;
1609                                 case VID_PLAY_NORMAL:
1610                                         debiwrite(saa, debNormal,
1611                                                 IBM_MP2_CHIP_CONTROL,
1612                                                 ChipControl, 2);
1613                                         ibm_send_command(saa,
1614                                                 IBM_MP2_PLAY, 0, 0);
1615                                         saa->playmode = pmode.mode;
1616                                         return 0;
1617                                 case VID_PLAY_PAUSE:
1618                                         /* IBM removed the PAUSE command */
1619                                         /* they say use SINGLE_FRAME now */
1620                                 case VID_PLAY_SINGLE_FRAME:
1621                                         ibm_send_command(saa,
1622                                                         IBM_MP2_SINGLE_FRAME,
1623                                                         0, 0);
1624                                         if (saa->playmode == pmode.mode) {
1625                                                 debiwrite(saa, debNormal,
1626                                                         IBM_MP2_CHIP_CONTROL,
1627                                                         ChipControl, 2);
1628                                         }
1629                                         saa->playmode = pmode.mode;
1630                                         return 0;
1631                                 case VID_PLAY_FAST_FORWARD:
1632                                         ibm_send_command(saa,
1633                                                 IBM_MP2_FAST_FORWARD, 0, 0);
1634                                         saa->playmode = pmode.mode;
1635                                         return 0;
1636                                 case VID_PLAY_SLOW_MOTION:
1637                                         ibm_send_command(saa,
1638                                                 IBM_MP2_SLOW_MOTION,
1639                                                 pmode.p1, 0);
1640                                         saa->playmode = pmode.mode;
1641                                         return 0;
1642                                 case VID_PLAY_IMMEDIATE_NORMAL:
1643                                         /* ensure transfers resume */
1644                                         debiwrite(saa, debNormal,
1645                                                 IBM_MP2_CHIP_CONTROL,
1646                                                 ChipControl, 2);
1647                                         ibm_send_command(saa,
1648                                                 IBM_MP2_IMED_NORM_PLAY, 0, 0);
1649                                         saa->playmode = VID_PLAY_NORMAL;
1650                                         return 0;
1651                                 case VID_PLAY_SWITCH_CHANNELS:
1652                                         saa->audhead = saa->audtail = 0;
1653                                         saa->vidhead = saa->vidtail = 0;
1654                                         ibm_send_command(saa,
1655                                                 IBM_MP2_FREEZE_FRAME, 0, 1);
1656                                         ibm_send_command(saa,
1657                                                 IBM_MP2_RESET_AUD_RATE, 0, 1);
1658                                         debiwrite(saa, debNormal,
1659                                                 IBM_MP2_CHIP_CONTROL, 0, 2);
1660                                         ibm_send_command(saa,
1661                                                 IBM_MP2_CHANNEL_SWITCH, 0, 1);
1662                                         debiwrite(saa, debNormal,
1663                                                 IBM_MP2_CHIP_CONTROL,
1664                                                 ChipControl, 2);
1665                                         ibm_send_command(saa,
1666                                                 IBM_MP2_PLAY, 0, 0);
1667                                         saa->playmode = VID_PLAY_NORMAL;
1668                                         return 0;
1669                                 case VID_PLAY_FREEZE_FRAME:
1670                                         ibm_send_command(saa,
1671                                                 IBM_MP2_FREEZE_FRAME, 0, 0);
1672                                         saa->playmode = pmode.mode;
1673                                         return 0;
1674                                 case VID_PLAY_STILL_MODE:
1675                                         ibm_send_command(saa,
1676                                                 IBM_MP2_SET_STILL_MODE, 0, 0);
1677                                         saa->playmode = pmode.mode;
1678                                         return 0;
1679                                 case VID_PLAY_MASTER_MODE:
1680                                         if (pmode.p1 == VID_PLAY_MASTER_NONE)
1681                                                 saa->boardcfg[1] = 0x13;
1682                                         else if (pmode.p1 ==
1683                                                 VID_PLAY_MASTER_VIDEO)
1684                                                 saa->boardcfg[1] = 0x23;
1685                                         else if (pmode.p1 ==
1686                                                 VID_PLAY_MASTER_AUDIO)
1687                                                 saa->boardcfg[1] = 0x43;
1688                                         else
1689                                                 return -EINVAL;
1690                                         debiwrite(saa, debNormal,
1691                                                 IBM_MP2_CHIP_CONTROL,
1692                                                 ChipControl, 2);
1693                                         return 0;
1694                                 case VID_PLAY_ACTIVE_SCANLINES:
1695                                         if (CurrentMode == VIDEO_MODE_PAL) {
1696                                                 if (pmode.p1 < 1 ||
1697                                                         pmode.p2 > 625)
1698                                                         return -EINVAL;
1699                                                 saa->boardcfg[5] = pmode.p1;
1700                                                 saa->boardcfg[55] = (pmode.p1 +
1701                                                         (pmode.p2/2) - 1) &     
1702                                                         0xff;
1703                                         } else {
1704                                                 if (pmode.p1 < 4 ||
1705                                                         pmode.p2 > 525)
1706                                                         return -EINVAL;
1707                                                 saa->boardcfg[4] = pmode.p1;
1708                                                 saa->boardcfg[54] = (pmode.p1 +
1709                                                         (pmode.p2/2) - 4) &
1710                                                         0xff;
1711                                         }
1712                                         set_out_format(saa, CurrentMode);
1713                                 case VID_PLAY_RESET:
1714                                         return do_ibm_reset(saa);
1715                                 case VID_PLAY_END_MARK:
1716                                         if (saa->endmarktail <  
1717                                                 saa->endmarkhead) {
1718                                                 if (saa->endmarkhead -
1719                                                         saa->endmarktail < 2)
1720                                                         return -ENOSPC;
1721                                         } else if (saa->endmarkhead <=
1722                                                 saa->endmarktail) {
1723                                                 if (saa->endmarktail -
1724                                                         saa->endmarkhead >
1725                                                         (MAX_MARKS - 2))
1726                                                         return -ENOSPC;
1727                                         } else
1728                                                 return -ENOSPC;
1729                                         saa->endmark[saa->endmarktail] =
1730                                                 saa->audtail;
1731                                         saa->endmarktail++;
1732                                         if (saa->endmarktail >= MAX_MARKS)
1733                                                 saa->endmarktail = 0;
1734                         }
1735                         return -EINVAL;
1736                 }
1737         case VIDIOCSWRITEMODE:
1738                 {
1739                         int mode;
1740                         if (copy_from_user((void *) &mode, arg, sizeof(int)))
1741                                  return -EFAULT;
1742                         if (mode == VID_WRITE_MPEG_AUD ||
1743                             mode == VID_WRITE_MPEG_VID ||
1744                             mode == VID_WRITE_CC ||
1745                             mode == VID_WRITE_TTX ||
1746                             mode == VID_WRITE_OSD) {
1747                                 saa->writemode = mode;
1748                                 return 0;
1749                         }
1750                         return -EINVAL;
1751                 }
1752         case VIDIOCSMICROCODE:
1753                 {
1754                         struct video_code ucode;
1755                         __u8 *udata;
1756                         int i;
1757                         if (copy_from_user((void *) &ucode, arg,
1758                             sizeof(ucode)))
1759                                 return -EFAULT;
1760                         if (ucode.datasize > 65536 || ucode.datasize < 1024 ||
1761                             strncmp(ucode.loadwhat, "dec", 3))
1762                                 return -EINVAL;
1763                         if ((udata = vmalloc(ucode.datasize)) == NULL)
1764                                 return -ENOMEM;
1765                         if (copy_from_user((void *) udata, ucode.data,
1766                             ucode.datasize)) {
1767                                 vfree(udata);
1768                                 return -EFAULT;
1769                         }
1770                         ucode.data = udata;
1771                         if (!strncmp(ucode.loadwhat, "decoder.aud", 11)
1772                                 || !strncmp(ucode.loadwhat, "decoder.vid", 11))
1773                                 i = initialize_ibmmpeg2(&ucode);
1774                         else
1775                                 i = initialize_fpga(&ucode);
1776                         vfree(udata);
1777                         if (i)
1778                                 return -EINVAL;
1779                         return 0;
1780
1781                 }
1782         case VIDIOCGCHAN:       /* this makes xawtv happy */
1783                 {
1784                         struct video_channel v;
1785                         if (copy_from_user(&v, arg, sizeof(v)))
1786                                 return -EFAULT;
1787                         v.flags = VIDEO_VC_AUDIO;
1788                         v.tuners = 0;
1789                         v.type = VID_TYPE_MPEG_DECODER;
1790                         v.norm = CurrentMode;
1791                         strcpy(v.name, "MPEG2");
1792                         if (copy_to_user(arg, &v, sizeof(v)))
1793                                 return -EFAULT;
1794                         return 0;
1795                 }
1796         case VIDIOCSCHAN:       /* this makes xawtv happy */
1797                 {
1798                         struct video_channel v;
1799                         if (copy_from_user(&v, arg, sizeof(v)))
1800                                 return -EFAULT;
1801                         /* do nothing */
1802                         return 0;
1803                 }
1804         default:
1805                 return -ENOIOCTLCMD;
1806         }
1807         return 0;
1808 }
1809
1810 static int saa_mmap(struct file *file, struct vm_area_struct *vma)
1811 {
1812         struct saa7146 *saa = file->private_data;
1813         printk(KERN_DEBUG "stradis%d: saa_mmap called\n", saa->nr);
1814         return -EINVAL;
1815 }
1816
1817 static ssize_t saa_read(struct file *file, char *buf,
1818                         size_t count, loff_t *ppos)
1819 {
1820         return -EINVAL;
1821 }
1822
1823 static ssize_t saa_write(struct file *file, const char *buf,
1824                          size_t count, loff_t *ppos)
1825 {
1826         struct saa7146 *saa = file->private_data;
1827         unsigned long todo = count;
1828         int blocksize, split;
1829         unsigned long flags;
1830
1831         while (todo > 0) {
1832                 if (saa->writemode == VID_WRITE_MPEG_AUD) {
1833                         spin_lock_irqsave(&saa->lock, flags);
1834                         if (saa->audhead <= saa->audtail)
1835                                 blocksize = 65536-(saa->audtail - saa->audhead);
1836                         else
1837                                 blocksize = saa->audhead - saa->audtail;
1838                         spin_unlock_irqrestore(&saa->lock, flags);
1839                         if (blocksize < 16384) {
1840                                 saawrite(SAA7146_PSR_DEBI_S |
1841                                          SAA7146_PSR_PIN1, SAA7146_IER);
1842                                 saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
1843                                 /* wait for buffer space to open */
1844                                 interruptible_sleep_on(&saa->audq);
1845                         }
1846                         spin_lock_irqsave(&saa->lock, flags);
1847                         if (saa->audhead <= saa->audtail) {
1848                                 blocksize = 65536-(saa->audtail - saa->audhead);
1849                                 split = 65536 - saa->audtail;
1850                         } else {
1851                                 blocksize = saa->audhead - saa->audtail;
1852                                 split = 65536;
1853                         }
1854                         spin_unlock_irqrestore(&saa->lock, flags);
1855                         blocksize--;
1856                         if (blocksize > todo)
1857                                 blocksize = todo;
1858                         /* double check that we really have space */
1859                         if (!blocksize)
1860                                 return -ENOSPC;
1861                         if (split < blocksize) {
1862                                 if (copy_from_user(saa->audbuf +
1863                                         saa->audtail, buf, split)) 
1864                                         return -EFAULT;
1865                                 buf += split;
1866                                 todo -= split;
1867                                 blocksize -= split;
1868                                 saa->audtail = 0;
1869                         }
1870                         if (copy_from_user(saa->audbuf + saa->audtail, buf,
1871                                 blocksize)) 
1872                                 return -EFAULT;
1873                         saa->audtail += blocksize;
1874                         todo -= blocksize;
1875                         buf += blocksize;
1876                         saa->audtail &= 0xffff;
1877                 } else if (saa->writemode == VID_WRITE_MPEG_VID) {
1878                         spin_lock_irqsave(&saa->lock, flags);
1879                         if (saa->vidhead <= saa->vidtail)
1880                                 blocksize=524288-(saa->vidtail - saa->vidhead);
1881                         else
1882                                 blocksize = saa->vidhead - saa->vidtail;
1883                         spin_unlock_irqrestore(&saa->lock, flags);
1884                         if (blocksize < 65536) {
1885                                 saawrite(SAA7146_PSR_DEBI_S |
1886                                          SAA7146_PSR_PIN1, SAA7146_IER);
1887                                 saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
1888                                 /* wait for buffer space to open */
1889                                 interruptible_sleep_on(&saa->vidq);
1890                         }
1891                         spin_lock_irqsave(&saa->lock, flags);
1892                         if (saa->vidhead <= saa->vidtail) {
1893                                 blocksize=524288-(saa->vidtail - saa->vidhead);
1894                                 split = 524288 - saa->vidtail;
1895                         } else {
1896                                 blocksize = saa->vidhead - saa->vidtail;
1897                                 split = 524288;
1898                         }
1899                         spin_unlock_irqrestore(&saa->lock, flags);
1900                         blocksize--;
1901                         if (blocksize > todo)
1902                                 blocksize = todo;
1903                         /* double check that we really have space */
1904                         if (!blocksize)
1905                                 return -ENOSPC;
1906                         if (split < blocksize) {
1907                                 if (copy_from_user(saa->vidbuf +
1908                                         saa->vidtail, buf, split)) 
1909                                         return -EFAULT;
1910                                 buf += split;
1911                                 todo -= split;
1912                                 blocksize -= split;
1913                                 saa->vidtail = 0;
1914                         }
1915                         if (copy_from_user(saa->vidbuf + saa->vidtail, buf,
1916                                 blocksize)) 
1917                                 return -EFAULT;
1918                         saa->vidtail += blocksize;
1919                         todo -= blocksize;
1920                         buf += blocksize;
1921                         saa->vidtail &= 0x7ffff;
1922                 } else if (saa->writemode == VID_WRITE_OSD) {
1923                         if (count > 131072)
1924                                 return -ENOSPC;
1925                         if (copy_from_user(saa->osdbuf, buf, count))
1926                                 return -EFAULT;
1927                         buf += count;
1928                         saa->osdhead = 0;
1929                         saa->osdtail = count;
1930                         debiwrite(saa, debNormal, IBM_MP2_OSD_ADDR, 0, 2);
1931                         debiwrite(saa, debNormal, IBM_MP2_OSD_LINK_ADDR, 0, 2);
1932                         debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00d, 2);
1933                         debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
1934                                   debiread(saa, debNormal,
1935                                   IBM_MP2_DISP_MODE, 2) | 1, 2);
1936                         /* trigger osd data transfer */
1937                         saawrite(SAA7146_PSR_DEBI_S |
1938                                  SAA7146_PSR_PIN1, SAA7146_IER);
1939                         saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
1940                 }
1941         }
1942         return count;
1943 }
1944
1945 static int saa_open(struct inode *inode, struct file *file)
1946 {
1947         struct saa7146 *saa = NULL;
1948         unsigned int minor = iminor(inode);
1949         int i;
1950
1951         for (i = 0; i < SAA7146_MAX; i++) {
1952                 if (saa7146s[i].video_dev.minor == minor) {
1953                         saa = &saa7146s[i];
1954                 }
1955         }
1956         if (saa == NULL) {
1957                 return -ENODEV;
1958         }
1959         file->private_data = saa;
1960
1961         //saa->video_dev.busy = 0; /* old hack to support multiple open */
1962         saa->user++;
1963         if (saa->user > 1)
1964                 return 0;       /* device open already, don't reset */
1965         saa->writemode = VID_WRITE_MPEG_VID;    /* default to video */
1966         return 0;
1967 }
1968
1969 static int saa_release(struct inode *inode, struct file *file)
1970 {
1971         struct saa7146 *saa = file->private_data;
1972         saa->user--;
1973         //saa->video_dev.busy = 0; /* old hack to support multiple open */
1974         if (saa->user > 0)      /* still someone using device */
1975                 return 0;
1976         saawrite(0x007f0000, SAA7146_MC1);      /* stop all overlay dma */
1977         return 0;
1978 }
1979
1980 static struct file_operations saa_fops =
1981 {
1982         .owner          = THIS_MODULE,
1983         .open           = saa_open,
1984         .release        = saa_release,
1985         .ioctl          = saa_ioctl,
1986         .read           = saa_read,
1987         .llseek         = no_llseek,
1988         .write          = saa_write,
1989         .mmap           = saa_mmap,
1990 };
1991
1992 /* template for video_device-structure */
1993 static struct video_device saa_template =
1994 {
1995         .name           = "SAA7146A",
1996         .type           = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY,
1997         .hardware       = VID_HARDWARE_SAA7146,
1998         .fops           = &saa_fops,
1999         .minor          = -1,
2000 };
2001
2002 static int configure_saa7146(struct pci_dev *dev, int num)
2003 {
2004         int result;
2005         struct saa7146 *saa;
2006
2007         saa = &saa7146s[num];
2008         
2009         saa->endmarkhead = saa->endmarktail = 0;
2010         saa->win.x = saa->win.y = 0;
2011         saa->win.width = saa->win.cropwidth = 720;
2012         saa->win.height = saa->win.cropheight = 480;
2013         saa->win.cropx = saa->win.cropy = 0;
2014         saa->win.bpp = 2;
2015         saa->win.depth = 16;
2016         saa->win.color_fmt = palette2fmt[VIDEO_PALETTE_RGB565];
2017         saa->win.bpl = 1024 * saa->win.bpp;
2018         saa->win.swidth = 1024;
2019         saa->win.sheight = 768;
2020         saa->picture.brightness = 32768;
2021         saa->picture.contrast = 38768;
2022         saa->picture.colour = 32768;
2023         saa->cap = 0;
2024         saa->dev = dev;
2025         saa->nr = num;
2026         saa->playmode = VID_PLAY_NORMAL;
2027         memset(saa->boardcfg, 0, 64);   /* clear board config area */
2028         saa->saa7146_mem = NULL;
2029         saa->dmavid1 = saa->dmavid2 = saa->dmavid3 = saa->dmaa1in =
2030             saa->dmaa1out = saa->dmaa2in = saa->dmaa2out =
2031             saa->pagevid1 = saa->pagevid2 = saa->pagevid3 = saa->pagea1in =
2032             saa->pagea1out = saa->pagea2in = saa->pagea2out =
2033             saa->pagedebi = saa->dmaRPS1 = saa->dmaRPS2 = saa->pageRPS1 =
2034             saa->pageRPS2 = NULL;
2035         saa->audbuf = saa->vidbuf = saa->osdbuf = saa->dmadebi = NULL;
2036         saa->audhead = saa->vidtail = 0;
2037
2038         init_waitqueue_head(&saa->i2cq);
2039         init_waitqueue_head(&saa->audq);
2040         init_waitqueue_head(&saa->debiq);
2041         init_waitqueue_head(&saa->vidq);
2042         spin_lock_init(&saa->lock);
2043
2044         if (pci_enable_device(dev))
2045                 return -EIO;
2046         
2047         saa->id = dev->device;
2048         saa->irq = dev->irq;
2049         saa->video_dev.minor = -1;
2050         saa->saa7146_adr = pci_resource_start(dev, 0);
2051         pci_read_config_byte(dev, PCI_CLASS_REVISION, &saa->revision);
2052
2053         saa->saa7146_mem = ioremap(saa->saa7146_adr, 0x200);
2054         if (!saa->saa7146_mem)
2055                 return -EIO;
2056
2057         memcpy(&saa->video_dev, &saa_template, sizeof(saa_template));
2058         saawrite(0, SAA7146_IER);       /* turn off all interrupts */
2059         result = request_irq(saa->irq, saa7146_irq,
2060                        SA_SHIRQ | SA_INTERRUPT, "stradis", (void *) saa);
2061         if (result == -EINVAL)
2062                 printk(KERN_ERR "stradis%d: Bad irq number or handler\n",
2063                        num);
2064         if (result == -EBUSY)
2065                 printk(KERN_ERR "stradis%d: IRQ %ld busy, change your PnP"
2066                        " config in BIOS\n", num, saa->irq);
2067         if (result < 0) {
2068                 iounmap(saa->saa7146_mem);
2069                 return result;
2070         }
2071         pci_set_master(dev);
2072         if (video_register_device(&saa->video_dev, VFL_TYPE_GRABBER, video_nr) < 0) {
2073                 iounmap(saa->saa7146_mem);
2074                 return -1;
2075         }
2076         return 0;
2077 }
2078
2079 static int init_saa7146(int i)
2080 {
2081         struct saa7146 *saa = &saa7146s[i];
2082
2083         saa->user = 0;
2084         /* reset the saa7146 */
2085         saawrite(0xffff0000, SAA7146_MC1);
2086         mdelay(5);
2087         /* enable debi and i2c transfers and pins */
2088         saawrite(((SAA7146_MC1_EDP | SAA7146_MC1_EI2C |
2089                    SAA7146_MC1_TR_E_DEBI) << 16) | 0xffff, SAA7146_MC1);
2090         /* ensure proper state of chip */
2091         saawrite(0x00000000, SAA7146_PAGE1);
2092         saawrite(0x00f302c0, SAA7146_NUM_LINE_BYTE1);
2093         saawrite(0x00000000, SAA7146_PAGE2);
2094         saawrite(0x01400080, SAA7146_NUM_LINE_BYTE2);
2095         saawrite(0x00000000, SAA7146_DD1_INIT);
2096         saawrite(0x00000000, SAA7146_DD1_STREAM_B);
2097         saawrite(0x00000000, SAA7146_DD1_STREAM_A);
2098         saawrite(0x00000000, SAA7146_BRS_CTRL);
2099         saawrite(0x80400040, SAA7146_BCS_CTRL);
2100         saawrite(0x0000e000 /*| (1<<29)*/, SAA7146_HPS_CTRL);
2101         saawrite(0x00000060, SAA7146_CLIP_FORMAT_CTRL);
2102         saawrite(0x00000000, SAA7146_ACON1);
2103         saawrite(0x00000000, SAA7146_ACON2);
2104         saawrite(0x00000600, SAA7146_I2C_STATUS);
2105         saawrite(((SAA7146_MC2_UPLD_D1_B | SAA7146_MC2_UPLD_D1_A |
2106                    SAA7146_MC2_UPLD_BRS | SAA7146_MC2_UPLD_HPS_H |
2107                    SAA7146_MC2_UPLD_HPS_V | SAA7146_MC2_UPLD_DMA2 |
2108            SAA7146_MC2_UPLD_DMA1 | SAA7146_MC2_UPLD_I2C) << 16) | 0xffff,
2109                  SAA7146_MC2);
2110         /* setup arbitration control registers */
2111         saawrite(0x1412121a, SAA7146_PCI_BT_V1);
2112
2113         /* allocate 32k dma buffer + 4k for page table */
2114         if ((saa->dmadebi = kmalloc(32768 + 4096, GFP_KERNEL)) == NULL) {
2115                 printk(KERN_ERR "stradis%d: debi kmalloc failed\n", i);
2116                 return -1;
2117         }
2118 #if 0
2119         saa->pagedebi = saa->dmadebi + 32768;   /* top 4k is for mmu */
2120         saawrite(virt_to_bus(saa->pagedebi) /*|0x800 */ , SAA7146_DEBI_PAGE);
2121         for (i = 0; i < 12; i++)        /* setup mmu page table */
2122                 saa->pagedebi[i] = virt_to_bus((saa->dmadebi + i * 4096));
2123 #endif
2124         saa->audhead = saa->vidhead = saa->osdhead = 0;
2125         saa->audtail = saa->vidtail = saa->osdtail = 0;
2126         if (saa->vidbuf == NULL)
2127                 if ((saa->vidbuf = vmalloc(524288)) == NULL) {
2128                         printk(KERN_ERR "stradis%d: malloc failed\n", saa->nr);
2129                         return -ENOMEM;
2130                 }
2131         if (saa->audbuf == NULL)
2132                 if ((saa->audbuf = vmalloc(65536)) == NULL) {
2133                         printk(KERN_ERR "stradis%d: malloc failed\n", saa->nr);
2134                         vfree(saa->vidbuf);
2135                         saa->vidbuf = NULL;
2136                         return -ENOMEM;
2137                 }
2138         if (saa->osdbuf == NULL)
2139                 if ((saa->osdbuf = vmalloc(131072)) == NULL) {
2140                         printk(KERN_ERR "stradis%d: malloc failed\n", saa->nr);
2141                         vfree(saa->vidbuf);
2142                         vfree(saa->audbuf);
2143                         saa->vidbuf = saa->audbuf = NULL;
2144                         return -ENOMEM;
2145                 }
2146         /* allocate 81920 byte buffer for clipping */
2147         if ((saa->dmavid2 = kmalloc(VIDEO_CLIPMAP_SIZE, GFP_KERNEL)) == NULL) {
2148                 printk(KERN_ERR "stradis%d: clip kmalloc failed\n", saa->nr);
2149                 vfree(saa->vidbuf);
2150                 vfree(saa->audbuf);
2151                 vfree(saa->osdbuf);
2152                 saa->vidbuf = saa->audbuf = saa->osdbuf = NULL;
2153                 saa->dmavid2 = NULL;
2154                 return -1;
2155         }
2156         memset(saa->dmavid2, 0x00, VIDEO_CLIPMAP_SIZE); /* clip everything */
2157         /* setup clipping registers */
2158         saawrite(virt_to_bus(saa->dmavid2), SAA7146_BASE_EVEN2);
2159         saawrite(virt_to_bus(saa->dmavid2) + 128, SAA7146_BASE_ODD2);
2160         saawrite(virt_to_bus(saa->dmavid2) + VIDEO_CLIPMAP_SIZE,
2161                 SAA7146_PROT_ADDR2);
2162         saawrite(256, SAA7146_PITCH2);
2163         saawrite(4, SAA7146_PAGE2); /* dma direction: read, no byteswap */
2164         saawrite(((SAA7146_MC2_UPLD_DMA2) << 16) | SAA7146_MC2_UPLD_DMA2,
2165                  SAA7146_MC2);
2166         I2CBusScan(saa);
2167         return 0;
2168 }
2169
2170 static void release_saa(void)
2171 {
2172         u8 command;
2173         int i;
2174         struct saa7146 *saa;
2175
2176         for (i = 0; i < saa_num; i++) {
2177                 saa = &saa7146s[i];
2178
2179                 /* turn off all capturing, DMA and IRQs */
2180                 saawrite(0xffff0000, SAA7146_MC1);      /* reset chip */
2181                 saawrite(0, SAA7146_MC2);
2182                 saawrite(0, SAA7146_IER);
2183                 saawrite(0xffffffffUL, SAA7146_ISR);
2184
2185                 /* disable PCI bus-mastering */
2186                 pci_read_config_byte(saa->dev, PCI_COMMAND, &command);
2187                 command &= ~PCI_COMMAND_MASTER;
2188                 pci_write_config_byte(saa->dev, PCI_COMMAND, command);
2189
2190                 /* unmap and free memory */
2191                 saa->audhead = saa->audtail = saa->osdhead = 0;
2192                 saa->vidhead = saa->vidtail = saa->osdtail = 0;
2193                 if (saa->vidbuf)
2194                         vfree(saa->vidbuf);
2195                 if (saa->audbuf)
2196                         vfree(saa->audbuf);
2197                 if (saa->osdbuf)
2198                         vfree(saa->osdbuf);
2199                 if (saa->dmavid2)
2200                         kfree((void *) saa->dmavid2);
2201                 saa->audbuf = saa->vidbuf = saa->osdbuf = NULL;
2202                 saa->dmavid2 = NULL;
2203                 if (saa->dmadebi)
2204                         kfree((void *) saa->dmadebi);
2205                 if (saa->dmavid1)
2206                         kfree((void *) saa->dmavid1);
2207                 if (saa->dmavid2)
2208                         kfree((void *) saa->dmavid2);
2209                 if (saa->dmavid3)
2210                         kfree((void *) saa->dmavid3);
2211                 if (saa->dmaa1in)
2212                         kfree((void *) saa->dmaa1in);
2213                 if (saa->dmaa1out)
2214                         kfree((void *) saa->dmaa1out);
2215                 if (saa->dmaa2in)
2216                         kfree((void *) saa->dmaa2in);
2217                 if (saa->dmaa2out)
2218                         kfree((void *) saa->dmaa2out);
2219                 if (saa->dmaRPS1)
2220                         kfree((void *) saa->dmaRPS1);
2221                 if (saa->dmaRPS2)
2222                         kfree((void *) saa->dmaRPS2);
2223                 free_irq(saa->irq, saa);
2224                 if (saa->saa7146_mem)
2225                         iounmap(saa->saa7146_mem);
2226                 if (saa->video_dev.minor != -1)
2227                         video_unregister_device(&saa->video_dev);
2228         }
2229 }
2230
2231
2232 static int __init stradis_init (void)
2233 {
2234         struct pci_dev *dev = NULL;
2235         int result = 0, i;
2236
2237         saa_num = 0;
2238
2239         while ((dev = pci_find_device(PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, dev))) {
2240                 if (!dev->subsystem_vendor)
2241                         printk(KERN_INFO "stradis%d: rev1 decoder\n", saa_num);
2242                 else
2243                         printk(KERN_INFO "stradis%d: SDM2xx found\n", saa_num); 
2244                 result = configure_saa7146(dev, saa_num++);
2245                 if (result)
2246                         return result;
2247         }
2248         if (saa_num)
2249                 printk(KERN_INFO "stradis: %d card(s) found.\n", saa_num);
2250         else
2251                 return -EINVAL;
2252         for (i = 0; i < saa_num; i++)
2253                 if (init_saa7146(i) < 0) {
2254                         release_saa();
2255                         return -EIO;
2256                 }
2257         return 0;
2258 }
2259
2260
2261 static void __exit stradis_exit (void)
2262 {
2263         release_saa();
2264         printk(KERN_INFO "stradis: module cleanup complete\n");
2265 }
2266
2267
2268 module_init(stradis_init);
2269 module_exit(stradis_exit);
2270