patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / oss / sscape.c
1 /*
2  * sound/sscape.c
3  *
4  * Low level driver for Ensoniq SoundScape
5  *
6  *
7  * Copyright (C) by Hannu Savolainen 1993-1997
8  *
9  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10  * Version 2 (June 1991). See the "COPYING" file distributed with this software
11  * for more info.
12  *
13  *
14  * Thomas Sailer        : ioctl code reworked (vmalloc/vfree removed)
15  * Sergey Smitienko     : ensoniq p'n'p support
16  * Christoph Hellwig    : adapted to module_init/module_exit
17  * Bartlomiej Zolnierkiewicz : added __init to attach_sscape()
18  * Chris Rankin         : Specify that this module owns the coprocessor
19  * Arnaldo C. de Melo   : added missing restore_flags in sscape_pnp_upload_file
20  */
21
22 #include <linux/init.h>
23 #include <linux/module.h>
24
25 #include "sound_config.h"
26 #include "sound_firmware.h"
27
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/signal.h>
31 #include <linux/fcntl.h>
32 #include <linux/ctype.h>
33 #include <linux/stddef.h>
34 #include <linux/kmod.h>
35 #include <asm/dma.h>
36 #include <asm/io.h>
37 #include <linux/wait.h>
38 #include <linux/slab.h>
39 #include <linux/ioport.h>
40 #include <linux/delay.h>
41 #include <linux/proc_fs.h>
42 #include <linux/spinlock.h>
43
44 #include "coproc.h"
45
46 #include "ad1848.h"
47 #include "mpu401.h"
48
49 /*
50  *    I/O ports
51  */
52 #define MIDI_DATA       0
53 #define MIDI_CTRL       1
54 #define HOST_CTRL       2
55 #define TX_READY        0x02
56 #define RX_READY        0x01
57 #define HOST_DATA       3
58 #define ODIE_ADDR       4
59 #define ODIE_DATA       5
60
61 /*
62  *    Indirect registers
63  */
64
65 #define GA_INTSTAT_REG  0
66 #define GA_INTENA_REG   1
67 #define GA_DMAA_REG     2
68 #define GA_DMAB_REG     3
69 #define GA_INTCFG_REG   4
70 #define GA_DMACFG_REG   5
71 #define GA_CDCFG_REG    6
72 #define GA_SMCFGA_REG   7
73 #define GA_SMCFGB_REG   8
74 #define GA_HMCTL_REG    9
75
76 /*
77  * DMA channel identifiers (A and B)
78  */
79
80 #define SSCAPE_DMA_A    0
81 #define SSCAPE_DMA_B    1
82
83 #define PORT(name)      (devc->base+name)
84
85 /*
86  * Host commands recognized by the OBP microcode
87  */
88  
89 #define CMD_GEN_HOST_ACK        0x80
90 #define CMD_GEN_MPU_ACK         0x81
91 #define CMD_GET_BOARD_TYPE      0x82
92 #define CMD_SET_CONTROL         0x88    /* Old firmware only */
93 #define CMD_GET_CONTROL         0x89    /* Old firmware only */
94 #define CTL_MASTER_VOL          0
95 #define CTL_MIC_MODE            2
96 #define CTL_SYNTH_VOL           4
97 #define CTL_WAVE_VOL            7
98 #define CMD_SET_EXTMIDI         0x8a
99 #define CMD_GET_EXTMIDI         0x8b
100 #define CMD_SET_MT32            0x8c
101 #define CMD_GET_MT32            0x8d
102
103 #define CMD_ACK                 0x80
104
105 #define IC_ODIE                 1
106 #define IC_OPUS                 2
107
108 typedef struct sscape_info
109 {
110         int     base, irq, dma;
111         
112         int     codec, codec_irq;       /* required to setup pnp cards*/
113         int     codec_type;
114         int     ic_type;
115         char*   raw_buf;
116         unsigned long   raw_buf_phys;
117         int     buffsize;               /* -------------------------- */
118         spinlock_t lock;
119         int     ok;     /* Properly detected */
120         int     failed;
121         int     dma_allocated;
122         int     codec_audiodev;
123         int     opened;
124         int     *osp;
125         int     my_audiodev;
126 } sscape_info;
127
128 static struct sscape_info adev_info = {
129         0
130 };
131
132 static struct sscape_info *devc = &adev_info;
133 static int sscape_mididev = -1;
134
135 /* Some older cards have assigned interrupt bits differently than new ones */
136 static char valid_interrupts_old[] = {
137         9, 7, 5, 15
138 };
139
140 static char valid_interrupts_new[] = {
141         9, 5, 7, 10
142 };
143
144 static char *valid_interrupts = valid_interrupts_new;
145
146 /*
147  *      See the bottom of the driver. This can be set by spea =0/1.
148  */
149  
150 #ifdef REVEAL_SPEA
151 static char old_hardware = 1;
152 #else
153 static char old_hardware;
154 #endif
155
156 static void sleep(unsigned howlong)
157 {
158         current->state = TASK_INTERRUPTIBLE;
159         schedule_timeout(howlong);
160 }
161
162 static unsigned char sscape_read(struct sscape_info *devc, int reg)
163 {
164         unsigned long flags;
165         unsigned char val;
166
167         spin_lock_irqsave(&devc->lock,flags);
168         outb(reg, PORT(ODIE_ADDR));
169         val = inb(PORT(ODIE_DATA));
170         spin_unlock_irqrestore(&devc->lock,flags);
171         return val;
172 }
173
174 static void sscape_write(struct sscape_info *devc, int reg, int data)
175 {
176         unsigned long flags;
177
178         spin_lock_irqsave(&devc->lock,flags);
179         outb(reg, PORT(ODIE_ADDR));
180         outb(data, PORT(ODIE_DATA));
181         spin_unlock_irqrestore(&devc->lock,flags);
182 }
183
184 static unsigned char sscape_pnp_read_codec(sscape_info* devc, unsigned char reg)
185 {
186         unsigned char res;
187         unsigned long flags;
188
189         spin_lock_irqsave(&devc->lock,flags);
190         outb( reg, devc -> codec);
191         res = inb (devc -> codec + 1);
192         spin_unlock_irqrestore(&devc->lock,flags);
193         return res;
194
195 }
196
197 static void sscape_pnp_write_codec(sscape_info* devc, unsigned char reg, unsigned char data)
198 {
199         unsigned long flags;
200         
201         spin_lock_irqsave(&devc->lock,flags);
202         outb( reg, devc -> codec);
203         outb( data, devc -> codec + 1);
204         spin_unlock_irqrestore(&devc->lock,flags);
205 }
206
207 static void host_open(struct sscape_info *devc)
208 {
209         outb((0x00), PORT(HOST_CTRL));  /* Put the board to the host mode */
210 }
211
212 static void host_close(struct sscape_info *devc)
213 {
214         outb((0x03), PORT(HOST_CTRL));  /* Put the board to the MIDI mode */
215 }
216
217 static int host_write(struct sscape_info *devc, unsigned char *data, int count)
218 {
219         unsigned long flags;
220         int i, timeout_val;
221
222         spin_lock_irqsave(&devc->lock,flags);
223         /*
224          * Send the command and data bytes
225          */
226
227         for (i = 0; i < count; i++)
228         {
229                 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
230                         if (inb(PORT(HOST_CTRL)) & TX_READY)
231                                 break;
232
233                 if (timeout_val <= 0)
234                 {
235                                 spin_unlock_irqrestore(&devc->lock,flags);
236                             return 0;
237                 }
238                 outb(data[i], PORT(HOST_DATA));
239         }
240         spin_unlock_irqrestore(&devc->lock,flags);
241         return 1;
242 }
243
244 static int host_read(struct sscape_info *devc)
245 {
246         unsigned long flags;
247         int timeout_val;
248         unsigned char data;
249
250         spin_lock_irqsave(&devc->lock,flags);
251         /*
252          * Read a byte
253          */
254
255         for (timeout_val = 10000; timeout_val > 0; timeout_val--)
256                 if (inb(PORT(HOST_CTRL)) & RX_READY)
257                         break;
258
259         if (timeout_val <= 0)
260         {
261                 spin_unlock_irqrestore(&devc->lock,flags);
262                 return -1;
263         }
264         data = inb(PORT(HOST_DATA));
265         spin_unlock_irqrestore(&devc->lock,flags);
266         return data;
267 }
268
269 #if 0 /* unused */
270 static int host_command1(struct sscape_info *devc, int cmd)
271 {
272         unsigned char buf[10];
273         buf[0] = (unsigned char) (cmd & 0xff);
274         return host_write(devc, buf, 1);
275 }
276 #endif /* unused */
277
278
279 static int host_command2(struct sscape_info *devc, int cmd, int parm1)
280 {
281         unsigned char buf[10];
282
283         buf[0] = (unsigned char) (cmd & 0xff);
284         buf[1] = (unsigned char) (parm1 & 0xff);
285
286         return host_write(devc, buf, 2);
287 }
288
289 static int host_command3(struct sscape_info *devc, int cmd, int parm1, int parm2)
290 {
291         unsigned char buf[10];
292
293         buf[0] = (unsigned char) (cmd & 0xff);
294         buf[1] = (unsigned char) (parm1 & 0xff);
295         buf[2] = (unsigned char) (parm2 & 0xff);
296         return host_write(devc, buf, 3);
297 }
298
299 static void set_mt32(struct sscape_info *devc, int value)
300 {
301         host_open(devc);
302         host_command2(devc, CMD_SET_MT32, value ? 1 : 0);
303         if (host_read(devc) != CMD_ACK)
304         {
305                 /* printk( "SNDSCAPE: Setting MT32 mode failed\n"); */
306         }
307         host_close(devc);
308 }
309
310 static void set_control(struct sscape_info *devc, int ctrl, int value)
311 {
312         host_open(devc);
313         host_command3(devc, CMD_SET_CONTROL, ctrl, value);
314         if (host_read(devc) != CMD_ACK)
315         {
316                 /* printk( "SNDSCAPE: Setting control (%d) failed\n",  ctrl); */
317         }
318         host_close(devc);
319 }
320
321 static void do_dma(struct sscape_info *devc, int dma_chan, unsigned long buf, int blk_size, int mode)
322 {
323         unsigned char temp;
324
325         if (dma_chan != SSCAPE_DMA_A)
326         {
327                 printk(KERN_WARNING "soundscape: Tried to use DMA channel  != A. Why?\n");
328                 return;
329         }
330         audio_devs[devc->codec_audiodev]->flags &= ~DMA_AUTOMODE;
331         DMAbuf_start_dma(devc->codec_audiodev, buf, blk_size, mode);
332         audio_devs[devc->codec_audiodev]->flags |= DMA_AUTOMODE;
333
334         temp = devc->dma << 4;  /* Setup DMA channel select bits */
335         if (devc->dma <= 3)
336                 temp |= 0x80;   /* 8 bit DMA channel */
337
338         temp |= 1;              /* Trigger DMA */
339         sscape_write(devc, GA_DMAA_REG, temp);
340         temp &= 0xfe;           /* Clear DMA trigger */
341         sscape_write(devc, GA_DMAA_REG, temp);
342 }
343
344 static int verify_mpu(struct sscape_info *devc)
345 {
346         /*
347          * The SoundScape board could be in three modes (MPU, 8250 and host).
348          * If the card is not in the MPU mode, enabling the MPU driver will
349          * cause infinite loop (the driver believes that there is always some
350          * received data in the buffer.
351          *
352          * Detect this by looking if there are more than 10 received MIDI bytes
353          * (0x00) in the buffer.
354          */
355
356         int i;
357
358         for (i = 0; i < 10; i++)
359         {
360                 if (inb(devc->base + HOST_CTRL) & 0x80)
361                         return 1;
362
363                 if (inb(devc->base) != 0x00)
364                         return 1;
365         }
366         printk(KERN_WARNING "SoundScape: The device is not in the MPU-401 mode\n");
367         return 0;
368 }
369
370 static int sscape_coproc_open(void *dev_info, int sub_device)
371 {
372         if (sub_device == COPR_MIDI)
373         {
374                 set_mt32(devc, 0);
375                 if (!verify_mpu(devc))
376                         return -EIO;
377         }
378         return 0;
379 }
380
381 static void sscape_coproc_close(void *dev_info, int sub_device)
382 {
383         struct sscape_info *devc = dev_info;
384         unsigned long   flags;
385
386         spin_lock_irqsave(&devc->lock,flags);
387         if (devc->dma_allocated)
388         {
389                 sscape_write(devc, GA_DMAA_REG, 0x20);  /* DMA channel disabled */
390                 devc->dma_allocated = 0;
391         }
392         spin_unlock_irqrestore(&devc->lock,flags);
393         return;
394 }
395
396 static void sscape_coproc_reset(void *dev_info)
397 {
398 }
399
400 static int sscape_download_boot(struct sscape_info *devc, unsigned char *block, int size, int flag)
401 {
402         unsigned long flags;
403         unsigned char temp;
404         volatile int done, timeout_val;
405         static unsigned char codec_dma_bits;
406
407         if (flag & CPF_FIRST)
408         {
409                 /*
410                  * First block. Have to allocate DMA and to reset the board
411                  * before continuing.
412                  */
413
414                 spin_lock_irqsave(&devc->lock,flags);
415                 codec_dma_bits = sscape_read(devc, GA_CDCFG_REG);
416
417                 if (devc->dma_allocated == 0)
418                         devc->dma_allocated = 1;
419
420                 spin_unlock_irqrestore(&devc->lock,flags);
421
422                 sscape_write(devc, GA_HMCTL_REG, 
423                         (temp = sscape_read(devc, GA_HMCTL_REG)) & 0x3f);       /*Reset */
424
425                 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
426                         sscape_read(devc, GA_HMCTL_REG);        /* Delay */
427
428                 /* Take board out of reset */
429                 sscape_write(devc, GA_HMCTL_REG,
430                         (temp = sscape_read(devc, GA_HMCTL_REG)) | 0x80);
431         }
432         /*
433          * Transfer one code block using DMA
434          */
435         if (audio_devs[devc->codec_audiodev]->dmap_out->raw_buf == NULL)
436         {
437                 printk(KERN_WARNING "soundscape: DMA buffer not available\n");
438                 return 0;
439         }
440         memcpy(audio_devs[devc->codec_audiodev]->dmap_out->raw_buf, block, size);
441
442         spin_lock_irqsave(&devc->lock,flags);
443         
444         /******** INTERRUPTS DISABLED NOW ********/
445         
446         do_dma(devc, SSCAPE_DMA_A,
447                audio_devs[devc->codec_audiodev]->dmap_out->raw_buf_phys,
448                size, DMA_MODE_WRITE);
449
450         /*
451          * Wait until transfer completes.
452          */
453         
454         done = 0;
455         timeout_val = 30;
456         while (!done && timeout_val-- > 0)
457         {
458                 int resid;
459
460                 if (HZ / 50)
461                         sleep(HZ / 50);
462                 clear_dma_ff(devc->dma);
463                 if ((resid = get_dma_residue(devc->dma)) == 0)
464                         done = 1;
465         }
466
467         spin_unlock_irqrestore(&devc->lock,flags);
468         if (!done)
469                 return 0;
470
471         if (flag & CPF_LAST)
472         {
473                 /*
474                  * Take the board out of reset
475                  */
476                 outb((0x00), PORT(HOST_CTRL));
477                 outb((0x00), PORT(MIDI_CTRL));
478
479                 temp = sscape_read(devc, GA_HMCTL_REG);
480                 temp |= 0x40;
481                 sscape_write(devc, GA_HMCTL_REG, temp); /* Kickstart the board */
482
483                 /*
484                  * Wait until the ODB wakes up
485                  */
486                 spin_lock_irqsave(&devc->lock,flags);
487                 done = 0;
488                 timeout_val = 5 * HZ;
489                 while (!done && timeout_val-- > 0)
490                 {
491                         unsigned char x;
492                         
493                         sleep(1);
494                         x = inb(PORT(HOST_DATA));
495                         if (x == 0xff || x == 0xfe)             /* OBP startup acknowledge */
496                         {
497                                 DDB(printk("Soundscape: Acknowledge = %x\n", x));
498                                 done = 1;
499                         }
500                 }
501                 sscape_write(devc, GA_CDCFG_REG, codec_dma_bits);
502
503                 spin_unlock_irqrestore(&devc->lock,flags);
504                 if (!done)
505                 {
506                         printk(KERN_ERR "soundscape: The OBP didn't respond after code download\n");
507                         return 0;
508                 }
509                 spin_lock_irqsave(&devc->lock,flags);
510                 done = 0;
511                 timeout_val = 5 * HZ;
512                 while (!done && timeout_val-- > 0)
513                 {
514                         sleep(1);
515                         if (inb(PORT(HOST_DATA)) == 0xfe)       /* Host startup acknowledge */
516                                 done = 1;
517                 }
518                 spin_unlock_irqrestore(&devc->lock,flags);
519                 if (!done)
520                 {
521                         printk(KERN_ERR "soundscape: OBP Initialization failed.\n");
522                         return 0;
523                 }
524                 printk(KERN_INFO "SoundScape board initialized OK\n");
525                 set_control(devc, CTL_MASTER_VOL, 100);
526                 set_control(devc, CTL_SYNTH_VOL, 100);
527
528 #ifdef SSCAPE_DEBUG3
529                 /*
530                  * Temporary debugging aid. Print contents of the registers after
531                  * downloading the code.
532                  */
533                 {
534                         int i;
535
536                         for (i = 0; i < 13; i++)
537                                 printk("I%d = %02x (new value)\n", i, sscape_read(devc, i));
538                 }
539 #endif
540
541         }
542         return 1;
543 }
544
545 static int download_boot_block(void *dev_info, copr_buffer * buf)
546 {
547         if (buf->len <= 0 || buf->len > sizeof(buf->data))
548                 return -EINVAL;
549
550         if (!sscape_download_boot(devc, buf->data, buf->len, buf->flags))
551         {
552                 printk(KERN_ERR "soundscape: Unable to load microcode block to the OBP.\n");
553                 return -EIO;
554         }
555         return 0;
556 }
557
558 static int sscape_coproc_ioctl(void *dev_info, unsigned int cmd, void __user *arg, int local)
559 {
560         copr_buffer *buf;
561         int err;
562
563         switch (cmd) 
564         {
565                 case SNDCTL_COPR_RESET:
566                         sscape_coproc_reset(dev_info);
567                         return 0;
568
569                 case SNDCTL_COPR_LOAD:
570                         buf = (copr_buffer *) vmalloc(sizeof(copr_buffer));
571                         if (buf == NULL)
572                                 return -ENOSPC;
573                         if (copy_from_user(buf, arg, sizeof(copr_buffer))) 
574                         {
575                                 vfree(buf);
576                                 return -EFAULT;
577                         }
578                         err = download_boot_block(dev_info, buf);
579                         vfree(buf);
580                         return err;
581                 
582                 default:
583                         return -EINVAL;
584         }
585 }
586
587 static coproc_operations sscape_coproc_operations =
588 {
589         "SoundScape M68K",
590         THIS_MODULE,
591         sscape_coproc_open,
592         sscape_coproc_close,
593         sscape_coproc_ioctl,
594         sscape_coproc_reset,
595         &adev_info
596 };
597
598 static int sscape_detected;
599 static int sscape_is_pnp;
600
601 void __init attach_sscape(struct address_info *hw_config)
602 {
603 #ifndef SSCAPE_REGS
604         /*
605          * Config register values for Spea/V7 Media FX and Ensoniq S-2000.
606          * These values are card
607          * dependent. If you have another SoundScape based card, you have to
608          * find the correct values. Do the following:
609          *  - Compile this driver with SSCAPE_DEBUG1 defined.
610          *  - Shut down and power off your machine.
611          *  - Boot with DOS so that the SSINIT.EXE program is run.
612          *  - Warm boot to {Linux|SYSV|BSD} and write down the lines displayed
613          *    when detecting the SoundScape.
614          *  - Modify the following list to use the values printed during boot.
615          *    Undefine the SSCAPE_DEBUG1
616          */
617 #define SSCAPE_REGS { \
618 /* I0 */        0x00, \
619 /* I1 */        0xf0, /* Note! Ignored. Set always to 0xf0 */ \
620 /* I2 */        0x20, /* Note! Ignored. Set always to 0x20 */ \
621 /* I3 */        0x20, /* Note! Ignored. Set always to 0x20 */ \
622 /* I4 */        0xf5, /* Ignored */ \
623 /* I5 */        0x10, \
624 /* I6 */        0x00, \
625 /* I7 */        0x2e, /* I7 MEM config A. Likely to vary between models */ \
626 /* I8 */        0x00, /* I8 MEM config B. Likely to vary between models */ \
627 /* I9 */        0x40 /* Ignored */ \
628         }
629 #endif
630
631         unsigned long   flags;
632         static unsigned char regs[10] = SSCAPE_REGS;
633
634         int i, irq_bits = 0xff;
635
636         if (sscape_detected != hw_config->io_base)
637                 return;
638
639         request_region(devc->base + 2, 6, "SoundScape");
640         if (old_hardware)
641         {
642                 valid_interrupts = valid_interrupts_old;
643                 conf_printf("Ensoniq SoundScape (old)", hw_config);
644         }
645         else
646                 conf_printf("Ensoniq SoundScape", hw_config);
647
648         for (i = 0; i < sizeof(valid_interrupts); i++)
649         {
650                 if (hw_config->irq == valid_interrupts[i])
651                 {
652                         irq_bits = i;
653                         break;
654                 }
655         }
656         if (hw_config->irq > 15 || (regs[4] = irq_bits == 0xff))
657         {
658                 printk(KERN_ERR "Invalid IRQ%d\n", hw_config->irq);
659                 return;
660         }
661         
662         if (!sscape_is_pnp) {
663         
664                 spin_lock_irqsave(&devc->lock,flags);
665             for (i = 1; i < 10; i++)
666             {
667                 switch (i)
668                 {
669                         case 1: /* Host interrupt enable */
670                                 sscape_write(devc, i, 0xf0);    /* All interrupts enabled */
671                                 break;
672
673                         case 2: /* DMA A status/trigger register */
674                         case 3: /* DMA B status/trigger register */
675                                 sscape_write(devc, i, 0x20);    /* DMA channel disabled */
676                                 break;
677
678                         case 4: /* Host interrupt config reg */
679                                 sscape_write(devc, i, 0xf0 | (irq_bits << 2) | irq_bits);
680                                 break;
681
682                         case 5: /* Don't destroy CD-ROM DMA config bits (0xc0) */
683                                 sscape_write(devc, i, (regs[i] & 0x3f) | (sscape_read(devc, i) & 0xc0));
684                                 break;
685
686                         case 6: /* CD-ROM config (WSS codec actually) */
687                                 sscape_write(devc, i, regs[i]);
688                                 break;
689
690                         case 9: /* Master control reg. Don't modify CR-ROM bits. Disable SB emul */
691                                 sscape_write(devc, i, (sscape_read(devc, i) & 0xf0) | 0x08);
692                                 break;
693
694                         default:
695                                 sscape_write(devc, i, regs[i]);
696                 }
697             }
698                 spin_unlock_irqrestore(&devc->lock,flags);
699         }
700 #ifdef SSCAPE_DEBUG2
701         /*
702          * Temporary debugging aid. Print contents of the registers after
703          * changing them.
704          */
705         {
706                 int i;
707
708                 for (i = 0; i < 13; i++)
709                         printk("I%d = %02x (new value)\n", i, sscape_read(devc, i));
710         }
711 #endif
712
713         if (probe_mpu401(hw_config))
714                 hw_config->always_detect = 1;
715         hw_config->name = "SoundScape";
716
717         hw_config->irq *= -1;   /* Negative value signals IRQ sharing */
718         attach_mpu401(hw_config, THIS_MODULE);
719         hw_config->irq *= -1;   /* Restore it */
720
721         if (hw_config->slots[1] != -1)  /* The MPU driver installed itself */
722         {
723                 sscape_mididev = hw_config->slots[1];
724                 midi_devs[hw_config->slots[1]]->coproc = &sscape_coproc_operations;
725         }
726         sscape_write(devc, GA_INTENA_REG, 0x80);        /* Master IRQ enable */
727         devc->ok = 1;
728         devc->failed = 0;
729 }
730
731 static int detect_ga(sscape_info * devc)
732 {
733         unsigned char save;
734
735         DDB(printk("Entered Soundscape detect_ga(%x)\n", devc->base));
736
737         if (check_region(devc->base, 8))
738                 return 0;
739
740         /*
741          * First check that the address register of "ODIE" is
742          * there and that it has exactly 4 writable bits.
743          * First 4 bits
744          */
745         
746         if ((save = inb(PORT(ODIE_ADDR))) & 0xf0)
747         {
748                 DDB(printk("soundscape: Detect error A\n"));
749                 return 0;
750         }
751         outb((0x00), PORT(ODIE_ADDR));
752         if (inb(PORT(ODIE_ADDR)) != 0x00)
753         {
754                 DDB(printk("soundscape: Detect error B\n"));
755                 return 0;
756         }
757         outb((0xff), PORT(ODIE_ADDR));
758         if (inb(PORT(ODIE_ADDR)) != 0x0f)
759         {
760                 DDB(printk("soundscape: Detect error C\n"));
761                 return 0;
762         }
763         outb((save), PORT(ODIE_ADDR));
764
765         /*
766          * Now verify that some indirect registers return zero on some bits.
767          * This may break the driver with some future revisions of "ODIE" but...
768          */
769
770         if (sscape_read(devc, 0) & 0x0c)
771         {
772                 DDB(printk("soundscape: Detect error D (%x)\n", sscape_read(devc, 0)));
773                 return 0;
774         }
775         if (sscape_read(devc, 1) & 0x0f)
776         {
777                 DDB(printk("soundscape: Detect error E\n"));
778                 return 0;
779         }
780         if (sscape_read(devc, 5) & 0x0f)
781         {
782                 DDB(printk("soundscape: Detect error F\n"));
783                 return 0;
784         }
785         return 1;
786 }
787
788 static  int sscape_read_host_ctrl(sscape_info* devc)
789 {
790         return host_read(devc);
791 }
792
793 static  void sscape_write_host_ctrl2(sscape_info *devc, int a, int b)
794 {
795         host_command2(devc, a, b);
796 }
797
798 static int sscape_alloc_dma(sscape_info *devc)
799 {
800         char *start_addr, *end_addr;
801         int dma_pagesize;
802         int sz, size;
803         struct page *page;
804
805         if (devc->raw_buf != NULL) return 0;    /* Already done */
806         dma_pagesize = (devc->dma < 4) ? (64 * 1024) : (128 * 1024);
807         devc->raw_buf = NULL;
808         devc->buffsize = 8192*4;
809         if (devc->buffsize > dma_pagesize) devc->buffsize = dma_pagesize;
810         start_addr = NULL;
811         /*
812          * Now loop until we get a free buffer. Try to get smaller buffer if
813          * it fails. Don't accept smaller than 8k buffer for performance
814          * reasons.
815          */
816         while (start_addr == NULL && devc->buffsize > PAGE_SIZE) {
817                 for (sz = 0, size = PAGE_SIZE; size < devc->buffsize; sz++, size <<= 1);
818                 devc->buffsize = PAGE_SIZE * (1 << sz);
819                 start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA, sz);
820                 if (start_addr == NULL) devc->buffsize /= 2;
821         }
822
823         if (start_addr == NULL) {
824                 printk(KERN_ERR "sscape pnp init error: Couldn't allocate DMA buffer\n");
825                 return 0;
826         } else {
827                 /* make some checks */
828                 end_addr = start_addr + devc->buffsize - 1;             
829                 /* now check if it fits into the same dma-pagesize */
830
831                 if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
832                     || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
833                         printk(KERN_ERR "sscape pnp: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, devc->buffsize);
834                         return 0;
835                 }
836         }
837         devc->raw_buf = start_addr;
838         devc->raw_buf_phys = virt_to_bus(start_addr);
839
840         for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
841                 SetPageReserved(page);
842         return 1;
843 }
844
845 static void sscape_free_dma(sscape_info *devc)
846 {
847         int sz, size;
848         unsigned long start_addr, end_addr;
849         struct page *page;
850
851         if (devc->raw_buf == NULL) return;
852         for (sz = 0, size = PAGE_SIZE; size < devc->buffsize; sz++, size <<= 1);
853         start_addr = (unsigned long) devc->raw_buf;
854         end_addr = start_addr + devc->buffsize;
855
856         for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
857                 ClearPageReserved(page);
858
859         free_pages((unsigned long) devc->raw_buf, sz);
860         devc->raw_buf = NULL;
861 }
862
863 /* Intel version !!!!!!!!! */
864
865 static int sscape_start_dma(int chan, unsigned long physaddr, int count, int dma_mode)
866 {
867         unsigned long flags;
868
869         flags = claim_dma_lock();
870         disable_dma(chan);
871         clear_dma_ff(chan);
872         set_dma_mode(chan, dma_mode);
873         set_dma_addr(chan, physaddr);
874         set_dma_count(chan, count);
875         enable_dma(chan);
876         release_dma_lock(flags);
877         return 0;
878 }
879
880 static void sscape_pnp_start_dma(sscape_info* devc, int arg )
881 {
882         int reg;
883         if (arg == 0) reg = 2;
884         else reg = 3;
885
886         sscape_write(devc, reg, sscape_read( devc, reg) | 0x01);
887         sscape_write(devc, reg, sscape_read( devc, reg) & 0xFE);
888 }
889
890 static int sscape_pnp_wait_dma (sscape_info* devc, int arg )
891 {
892         int             reg;
893         unsigned long   i;
894         unsigned char   d;
895
896         if (arg == 0) reg = 2;
897         else reg = 3;
898
899         sleep ( 1 );
900         i = 0;
901         do {
902                 d = sscape_read(devc, reg) & 1;
903                 if ( d == 1)  break;
904                 i++;
905         } while (i < 500000);
906         d = sscape_read(devc, reg) & 1; 
907         return d;
908 }
909
910 static  int     sscape_pnp_alloc_dma(sscape_info* devc)
911 {
912         /* printk(KERN_INFO "sscape: requesting dma\n"); */
913         if (request_dma(devc -> dma, "sscape")) return 0;
914         /* printk(KERN_INFO "sscape: dma channel allocated\n"); */
915         if (!sscape_alloc_dma(devc)) {
916                 free_dma(devc -> dma);
917                 return 0;
918         };
919         return 1;
920 }
921
922 static  void    sscape_pnp_free_dma(sscape_info* devc)
923 {
924         sscape_free_dma( devc);
925         free_dma(devc -> dma ); 
926         /* printk(KERN_INFO "sscape: dma released\n"); */
927 }
928
929 static  int     sscape_pnp_upload_file(sscape_info* devc, char* fn)
930 {       
931         int             done = 0;
932         int             timeout_val;
933         char*           data,*dt;
934         int             len,l;
935         unsigned long   flags;
936
937         sscape_write( devc, 9, sscape_read(devc, 9 )  & 0x3F );
938         sscape_write( devc, 2, (devc -> dma << 4) | 0x80 );
939         sscape_write( devc, 3, 0x20 );
940         sscape_write( devc, 9, sscape_read( devc, 9 )  | 0x80 );
941         
942         len = mod_firmware_load(fn, &data);
943         if (len == 0) {
944                     printk(KERN_ERR "sscape: file not found: %s\n", fn);
945                     return 0;
946         }
947         dt = data;
948         spin_lock_irqsave(&devc->lock,flags);
949         while ( len > 0 ) {
950                 if (len > devc -> buffsize) l = devc->buffsize;
951                 else l = len;
952                 len -= l;               
953                 memcpy(devc->raw_buf, dt, l); dt += l;
954                 sscape_start_dma(devc->dma, devc->raw_buf_phys, l, 0x48);
955                 sscape_pnp_start_dma ( devc, 0 );
956                 if (sscape_pnp_wait_dma ( devc, 0 ) == 0) {
957                         spin_unlock_irqrestore(&devc->lock,flags);
958                         return 0;
959                 }
960         }
961         
962         spin_unlock_irqrestore(&devc->lock,flags);
963         vfree(data);
964         
965         outb(0, devc -> base + 2);
966         outb(0, devc -> base);
967
968         sscape_write ( devc, 9, sscape_read( devc, 9 ) | 0x40);
969
970         timeout_val = 5 * HZ; 
971         while (!done && timeout_val-- > 0)
972         {
973                 unsigned char x;
974                 sleep(1);
975                 x = inb( devc -> base + 3);
976                 if (x == 0xff || x == 0xfe)             /* OBP startup acknowledge */
977                 {
978                         //printk(KERN_ERR "Soundscape: Acknowledge = %x\n", x);
979                         done = 1;
980                 }
981         }
982         timeout_val = 5 * HZ;
983         done = 0;
984         while (!done && timeout_val-- > 0)
985         {
986                 unsigned char x;
987                 sleep(1);
988                 x = inb( devc -> base + 3);
989                 if (x == 0xfe)          /* OBP startup acknowledge */
990                 {
991                         //printk(KERN_ERR "Soundscape: Acknowledge = %x\n", x);
992                         done = 1;
993                 }
994         }
995
996         if ( !done ) printk(KERN_ERR "soundscape: OBP Initialization failed.\n");
997
998         sscape_write( devc, 2, devc->ic_type == IC_ODIE ? 0x70 : 0x40);
999         sscape_write( devc, 3, (devc -> dma << 4) + 0x80);
1000         return 1;
1001 }
1002
1003 static void __init sscape_pnp_init_hw(sscape_info* devc)
1004 {       
1005         unsigned char midi_irq = 0, sb_irq = 0;
1006         unsigned i;
1007         static  char code_file_name[23] = "/sndscape/sndscape.cox";
1008         
1009         int sscape_sb_enable            = 0;
1010         int sscape_joystic_enable       = 0x7f;
1011         int sscape_mic_enable           = 0;
1012         int sscape_ext_midi             = 0;            
1013
1014         if ( !sscape_pnp_alloc_dma(devc) ) {
1015                 printk(KERN_ERR "sscape: faild to allocate dma\n");
1016                 return;
1017         }
1018
1019         for (i = 0; i < 4; i++) {
1020                 if ( devc -> irq   == valid_interrupts[i] ) 
1021                         midi_irq = i;
1022                 if ( devc -> codec_irq == valid_interrupts[i] ) 
1023                         sb_irq = i;
1024         }
1025
1026         sscape_write( devc, 5, 0x50);
1027         sscape_write( devc, 7, 0x2e);
1028         sscape_write( devc, 8, 0x00);
1029
1030         sscape_write( devc, 2, devc->ic_type == IC_ODIE ? 0x70 : 0x40);
1031         sscape_write( devc, 3, ( devc -> dma << 4) | 0x80);
1032
1033         if ( sscape_sb_enable )
1034                 sscape_write (devc, 4, 0xF0 | (sb_irq << 2) | midi_irq);
1035         else    
1036                 sscape_write (devc, 4, 0xF0 | (midi_irq<<2) | midi_irq);
1037
1038         i = 0x10; //sscape_read(devc, 9) & (devc->ic_type == IC_ODIE ? 0xf0 : 0xc0);
1039         if ( sscape_sb_enable )
1040                 i |= devc->ic_type == IC_ODIE ? 0x05 : 0x07;        
1041         if (sscape_joystic_enable) i |= 8;
1042         
1043         sscape_write (devc, 9, i);
1044         sscape_write (devc, 6, 0x80);
1045         sscape_write (devc, 1, 0x80);
1046
1047         if (devc -> codec_type == 2) {
1048                 sscape_pnp_write_codec( devc, 0x0C, 0x50);
1049                 sscape_pnp_write_codec( devc, 0x10, sscape_pnp_read_codec( devc, 0x10) & 0x3F);
1050                 sscape_pnp_write_codec( devc, 0x11, sscape_pnp_read_codec( devc, 0x11) | 0xC0);
1051                 sscape_pnp_write_codec( devc, 29, 0x20);
1052         }
1053
1054         if (sscape_pnp_upload_file(devc, "/sndscape/scope.cod") == 0 ) {
1055                 printk(KERN_ERR "sscape: faild to upload file /sndscape/scope.cod\n");
1056                 sscape_pnp_free_dma(devc);
1057                 return;
1058         }
1059
1060         i = sscape_read_host_ctrl( devc );
1061         
1062         if ( (i & 0x0F) >  7 ) {
1063                 printk(KERN_ERR "sscape: scope.cod faild\n");
1064                 sscape_pnp_free_dma(devc);
1065                 return;
1066         }
1067         if ( i & 0x10 ) sscape_write( devc, 7, 0x2F);
1068         code_file_name[21] = (char) ( i & 0x0F) + 0x30;
1069         if (sscape_pnp_upload_file( devc, code_file_name) == 0) {
1070                 printk(KERN_ERR "sscape: faild to upload file %s\n", code_file_name);
1071                 sscape_pnp_free_dma(devc);
1072                 return;
1073         }
1074         
1075         if (devc->ic_type != IC_ODIE) {
1076                 sscape_pnp_write_codec( devc, 10, (sscape_pnp_read_codec(devc, 10) & 0x7f) |
1077                  ( sscape_mic_enable == 0 ? 0x00 : 0x80) );
1078         }
1079         sscape_write_host_ctrl2( devc, 0x84, 0x64 );  /* MIDI volume */
1080         sscape_write_host_ctrl2( devc, 0x86, 0x64 );  /* MIDI volume?? */
1081         sscape_write_host_ctrl2( devc, 0x8A, sscape_ext_midi);
1082
1083         sscape_pnp_write_codec ( devc, 6, 0x3f ); //WAV_VOL
1084         sscape_pnp_write_codec ( devc, 7, 0x3f ); //WAV_VOL
1085         sscape_pnp_write_codec ( devc, 2, 0x1F ); //WD_CDXVOLL
1086         sscape_pnp_write_codec ( devc, 3, 0x1F ); //WD_CDXVOLR
1087
1088         if (devc -> codec_type == 1) {
1089                 sscape_pnp_write_codec ( devc, 4, 0x1F );
1090                 sscape_pnp_write_codec ( devc, 5, 0x1F );
1091                 sscape_write_host_ctrl2( devc, 0x88, sscape_mic_enable);
1092         } else {
1093                 int t;
1094                 sscape_pnp_write_codec ( devc, 0x10, 0x1F << 1);
1095                 sscape_pnp_write_codec ( devc, 0x11, 0xC0 | (0x1F << 1));
1096
1097                 t = sscape_pnp_read_codec( devc, 0x00) & 0xDF;
1098                 if ( (sscape_mic_enable == 0)) t |= 0;
1099                 else t |= 0x20;
1100                 sscape_pnp_write_codec ( devc, 0x00, t);
1101                 t = sscape_pnp_read_codec( devc, 0x01) & 0xDF;
1102                 if ( (sscape_mic_enable == 0) ) t |= 0;
1103                 else t |= 0x20;
1104                 sscape_pnp_write_codec ( devc, 0x01, t);
1105                 sscape_pnp_write_codec ( devc, 0x40 | 29 , 0x20);
1106                 outb(0, devc -> codec);
1107         }
1108         if (devc -> ic_type == IC_OPUS ) {
1109                 int i = sscape_read( devc, 9 );
1110                 sscape_write( devc, 9, i | 3 );
1111                 sscape_write( devc, 3, 0x40);
1112
1113                 if (check_region(0x228, 1)) {
1114                             outb(0, 0x228);
1115                             release_region(0x228,1);
1116                 }
1117                 sscape_write( devc, 3, (devc -> dma << 4) | 0x80);
1118                 sscape_write( devc, 9, i );
1119         }
1120         
1121         host_close ( devc );
1122         sscape_pnp_free_dma(devc);
1123 }
1124
1125 static int __init detect_sscape_pnp(sscape_info* devc)
1126 {
1127         long     i, irq_bits = 0xff;
1128         unsigned int d;
1129
1130         DDB(printk("Entered detect_sscape_pnp(%x)\n", devc->base));
1131
1132         if (check_region(devc->base, 8)) {
1133                 printk(KERN_ERR "detect_sscape_pnp: port %x is not free\n", devc->base);        
1134                 return 0;
1135         }
1136                 
1137         if (check_region(devc->codec, 2)) {
1138                 printk(KERN_ERR "detect_sscape_pnp: port %x is not free\n", devc->codec);       
1139                 return 0;
1140         }
1141
1142         if ( (inb( devc -> base + 2) & 0x78) != 0) return 0;
1143
1144         d = inb ( devc -> base + 4) & 0xF0;
1145         if (  (d & 0x80) != 0)  return 0;
1146         
1147         if (d == 0) {
1148                         devc->codec_type = 1;
1149                         devc->ic_type = IC_ODIE;
1150         }
1151         else if ( (d & 0x60) != 0) {
1152                         devc->codec_type = 2;
1153                         devc->ic_type = IC_OPUS;
1154         }
1155         else if ( (d & 0x40) != 0) {
1156                         devc->codec_type = 2;
1157                         devc->ic_type = IC_ODIE;
1158         } 
1159         else return 0;
1160         
1161         sscape_is_pnp = 1;
1162                 
1163         outb(0xFA, devc -> base+4);
1164         if  ((inb( devc -> base+4) & 0x9F) != 0x0A)
1165                 return 0;
1166         outb(0xFE, devc -> base+4);
1167         if  ( (inb(devc -> base+4) & 0x9F) != 0x0E)
1168                 return 0;
1169         if  ( (inb(devc -> base+5) & 0x9F) != 0x0E)
1170                 return 0;
1171
1172         if (devc->codec_type == 2) {
1173                 if (devc -> codec != devc -> base + 8)
1174                         printk("soundscape warning: incorrect codec port specified\n");
1175                 devc -> codec = devc -> base + 8;
1176                 d = 0x10 | (sscape_read(devc, 9)  & 0xCF);
1177                 sscape_write(devc, 9, d);
1178                 sscape_write(devc, 6, 0x80);
1179         } else {
1180                 //todo: check codec is not base + 8
1181         }
1182
1183         d  = (sscape_read(devc, 9) & 0x3F) | 0xC0;
1184         sscape_write(devc, 9, d);
1185
1186         for (i = 0; i < 550000; i++)
1187                 if ( !(inb(devc -> codec) & 0x80) ) break;
1188
1189         d = inb(devc -> codec);
1190         if (d & 0x80)
1191                 return 0;
1192         if ( inb(devc -> codec + 2) == 0xFF)
1193                 return 0;
1194
1195         sscape_write(devc, 9, sscape_read(devc, 9)  & 0x3F );
1196
1197         d  = inb(devc -> codec) & 0x80;
1198         if ( d == 0) {
1199                 printk(KERN_INFO "soundscape: hardware detected\n");
1200                 valid_interrupts = valid_interrupts_new;
1201         } else  {
1202                 printk(KERN_INFO "soundscape: board looks like media fx\n");
1203                 valid_interrupts = valid_interrupts_old;
1204                 old_hardware = 1;
1205         }
1206
1207         sscape_write( devc, 9, 0xC0 | (sscape_read(devc, 9)  & 0x3F) );
1208
1209         for (i = 0; i < 550000; i++)
1210                 if ( !(inb(devc -> codec) & 0x80)) 
1211                         break;
1212                 
1213         sscape_pnp_init_hw(devc);
1214
1215         for (i = 0; i < sizeof(valid_interrupts); i++)
1216         {
1217                 if (devc->codec_irq == valid_interrupts[i]) {
1218                         irq_bits = i;
1219                         break;
1220                 }
1221         }       
1222         sscape_write(devc, GA_INTENA_REG, 0x00);
1223         sscape_write(devc, GA_DMACFG_REG, 0x50);
1224         sscape_write(devc, GA_DMAA_REG, 0x70);
1225         sscape_write(devc, GA_DMAB_REG, 0x20);
1226         sscape_write(devc, GA_INTCFG_REG, 0xf0);
1227         sscape_write(devc, GA_CDCFG_REG, 0x89 | (devc->dma << 4) | (irq_bits << 1));
1228
1229         sscape_pnp_write_codec( devc, 0, sscape_pnp_read_codec( devc, 0) | 0x20);
1230         sscape_pnp_write_codec( devc, 0, sscape_pnp_read_codec( devc, 1) | 0x20);
1231
1232         return 1;       
1233 }
1234
1235 static int __init probe_sscape(struct address_info *hw_config)
1236 {
1237
1238         if (sscape_detected != 0 && sscape_detected != hw_config->io_base)
1239                 return 0;
1240
1241         devc->base = hw_config->io_base;
1242         devc->irq = hw_config->irq;
1243         devc->dma = hw_config->dma;
1244         devc->osp = hw_config->osp;
1245
1246 #ifdef SSCAPE_DEBUG1
1247         /*
1248          * Temporary debugging aid. Print contents of the registers before
1249          * changing them.
1250          */
1251         {
1252                 int i;
1253
1254                 for (i = 0; i < 13; i++)
1255                         printk("I%d = %02x (old value)\n", i, sscape_read(devc, i));
1256         }
1257 #endif
1258         devc->failed = 1;
1259
1260         if (!detect_ga(devc)) {
1261                 if (detect_sscape_pnp(devc)) {                  
1262                         sscape_detected = hw_config->io_base;
1263                         return 1;
1264                 } 
1265                 else return 0;
1266         }
1267
1268         if (old_hardware)       /* Check that it's really an old Spea/Reveal card. */
1269         {
1270                 unsigned char   tmp;
1271                 int             cc;
1272
1273                 if (!((tmp = sscape_read(devc, GA_HMCTL_REG)) & 0xc0))
1274                 {
1275                         sscape_write(devc, GA_HMCTL_REG, tmp | 0x80);
1276                         for (cc = 0; cc < 200000; ++cc)
1277                                 inb(devc->base + ODIE_ADDR);
1278                 }
1279         }
1280         sscape_detected = hw_config->io_base;
1281         return 1;
1282 }
1283
1284 static int __init probe_ss_ms_sound(struct address_info *hw_config)
1285 {
1286         int i, irq_bits = 0xff;
1287         int ad_flags = 0;
1288         
1289         if (devc->failed)
1290         {
1291                 printk(KERN_ERR "soundscape: Card not detected\n");
1292                 return 0;
1293         }
1294         if (devc->ok == 0)
1295         {
1296                 printk(KERN_ERR "soundscape: Invalid initialization order.\n");
1297                 return 0;
1298         }
1299         for (i = 0; i < sizeof(valid_interrupts); i++)
1300         {
1301                 if (hw_config->irq == valid_interrupts[i])
1302                 {
1303                         irq_bits = i;
1304                         break;
1305                 }
1306         }
1307         if (hw_config->irq > 15 || irq_bits == 0xff)
1308         {
1309                 printk(KERN_ERR "soundscape: Invalid MSS IRQ%d\n", hw_config->irq);
1310                 return 0;
1311         }
1312         
1313         if (!sscape_is_pnp) {
1314                 if (old_hardware)
1315                         ad_flags = 0x12345677;  /* Tell that we may have a CS4248 chip (Spea-V7 Media FX) */
1316                 return ad1848_detect(hw_config->io_base, &ad_flags, hw_config->osp);
1317         } 
1318         else {
1319                 if (old_hardware)
1320                         ad_flags = 0x12345677;  /* Tell that we may have a CS4248 chip (Spea-V7 Media FX) */
1321                 else
1322                         ad_flags = 0x87654321;  /* Tell that we have a soundscape pnp with 1845 chip */
1323                 return ad1848_detect(hw_config->io_base, &ad_flags, hw_config->osp);
1324         }
1325 }
1326
1327 static void __init attach_ss_ms_sound(struct address_info *hw_config)
1328 {
1329         /*
1330          * This routine configures the SoundScape card for use with the
1331          * Win Sound System driver. The AD1848 codec interface uses the CD-ROM
1332          * config registers of the "ODIE".
1333          */
1334
1335         int i, irq_bits = 0xff;
1336
1337                 
1338         if (!sscape_is_pnp)  /*pnp is already setup*/
1339         {
1340                 /*
1341                  * Setup the DMA polarity.
1342                  */
1343                 sscape_write(devc, GA_DMACFG_REG, 0x50);
1344         
1345                 /*
1346                  * Take the gate-array off of the DMA channel.
1347                  */
1348                 sscape_write(devc, GA_DMAB_REG, 0x20);
1349         
1350                 /*
1351                  * Init the AD1848 (CD-ROM) config reg.
1352                  */
1353                 for (i = 0; i < sizeof(valid_interrupts); i++)
1354                 {
1355                         if (hw_config->irq == valid_interrupts[i])
1356                         {
1357                                 irq_bits = i;
1358                                 break;
1359                         }
1360                 }       
1361                 sscape_write(devc, GA_CDCFG_REG, 0x89 | (hw_config->dma << 4) | (irq_bits << 1));
1362         }
1363         
1364         if (hw_config->irq == devc->irq)
1365                 printk(KERN_WARNING "soundscape: Warning! The WSS mode can't share IRQ with MIDI\n");
1366                                 
1367         hw_config->slots[0] = ad1848_init(
1368                         sscape_is_pnp ? "SoundScape" : "SoundScape PNP",
1369                         hw_config->io_base,
1370                         hw_config->irq,
1371                         hw_config->dma,
1372                         hw_config->dma,
1373                         0,
1374                         devc->osp,
1375                         THIS_MODULE);
1376
1377                                           
1378         if (hw_config->slots[0] != -1)  /* The AD1848 driver installed itself */
1379         {
1380                 audio_devs[hw_config->slots[0]]->coproc = &sscape_coproc_operations;
1381                 devc->codec_audiodev = hw_config->slots[0];
1382                 devc->my_audiodev = hw_config->slots[0];
1383
1384                 /* Set proper routings here (what are they) */
1385                 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_LINE);
1386         }
1387                 
1388 #ifdef SSCAPE_DEBUG5
1389         /*
1390          * Temporary debugging aid. Print contents of the registers
1391          * after the AD1848 device has been initialized.
1392          */
1393         {
1394                 int i;
1395
1396                 for (i = 0; i < 13; i++)
1397                         printk("I%d = %02x\n", i, sscape_read(devc, i));
1398         }
1399 #endif
1400
1401 }
1402
1403 static void __exit unload_sscape(struct address_info *hw_config)
1404 {
1405         release_region(devc->base + 2, 6);
1406         unload_mpu401(hw_config);
1407 }
1408
1409 static void __exit unload_ss_ms_sound(struct address_info *hw_config)
1410 {
1411         ad1848_unload(hw_config->io_base,
1412                       hw_config->irq,
1413                       devc->dma,
1414                       devc->dma,
1415                       0);
1416         sound_unload_audiodev(hw_config->slots[0]);
1417 }
1418
1419 static struct address_info cfg;
1420 static struct address_info cfg_mpu;
1421
1422 static int __initdata spea = -1;
1423 static int __initdata mss = 0;
1424 static int __initdata dma = -1;
1425 static int __initdata irq = -1;
1426 static int __initdata io = -1;
1427 static int __initdata mpu_irq = -1;
1428 static int __initdata mpu_io = -1;
1429
1430 MODULE_PARM(dma, "i");
1431 MODULE_PARM(irq, "i");
1432 MODULE_PARM(io, "i");
1433 MODULE_PARM(spea, "i");         /* spea=0/1 set the old_hardware */
1434 MODULE_PARM(mpu_irq, "i");
1435 MODULE_PARM(mpu_io, "i");
1436 MODULE_PARM(mss, "i");
1437
1438 static int __init init_sscape(void)
1439 {
1440         printk(KERN_INFO "Soundscape driver Copyright (C) by Hannu Savolainen 1993-1996\n");
1441         
1442         cfg.irq = irq;
1443         cfg.dma = dma;
1444         cfg.io_base = io;
1445
1446         cfg_mpu.irq = mpu_irq;
1447         cfg_mpu.io_base = mpu_io;
1448         /* WEH - Try to get right dma channel */
1449         cfg_mpu.dma = dma;
1450         
1451         devc->codec = cfg.io_base;
1452         devc->codec_irq = cfg.irq;
1453         devc->codec_type = 0;
1454         devc->ic_type = 0;
1455         devc->raw_buf = NULL;
1456         spin_lock_init(&devc->lock);
1457
1458         if (cfg.dma == -1 || cfg.irq == -1 || cfg.io_base == -1) {
1459                 printk(KERN_ERR "DMA, IRQ, and IO port must be specified.\n");
1460                 return -EINVAL;
1461         }
1462         
1463         if (cfg_mpu.irq == -1 && cfg_mpu.io_base != -1) {
1464                 printk(KERN_ERR "MPU_IRQ must be specified if MPU_IO is set.\n");
1465                 return -EINVAL;
1466         }
1467         
1468         if(spea != -1) {
1469                 old_hardware = spea;
1470                 printk(KERN_INFO "Forcing %s hardware support.\n",
1471                         spea?"new":"old");
1472         }       
1473         if (probe_sscape(&cfg_mpu) == 0)
1474                 return -ENODEV;
1475
1476         attach_sscape(&cfg_mpu);
1477         
1478         mss = probe_ss_ms_sound(&cfg);
1479
1480         if (mss)
1481                 attach_ss_ms_sound(&cfg);
1482
1483         return 0;
1484 }
1485
1486 static void __exit cleanup_sscape(void)
1487 {
1488         if (mss)
1489                 unload_ss_ms_sound(&cfg);
1490         unload_sscape(&cfg_mpu);
1491 }
1492
1493 module_init(init_sscape);
1494 module_exit(cleanup_sscape);
1495
1496 #ifndef MODULE
1497 static int __init setup_sscape(char *str)
1498 {
1499         /* io, irq, dma, mpu_io, mpu_irq */
1500         int ints[6];
1501         
1502         str = get_options(str, ARRAY_SIZE(ints), ints);
1503         
1504         io      = ints[1];
1505         irq     = ints[2];
1506         dma     = ints[3];
1507         mpu_io  = ints[4];
1508         mpu_irq = ints[5];
1509
1510         return 1;
1511 }
1512
1513 __setup("sscape=", setup_sscape);
1514 #endif
1515 MODULE_LICENSE("GPL");