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