ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / oss / mad16.c
1 /*
2  * Copyright (C) by Hannu Savolainen 1993-1997
3  *
4  * mad16.c
5  *
6  * Initialization code for OPTi MAD16 compatible audio chips. Including
7  *
8  *      OPTi 82C928     MAD16           (replaced by C929)
9  *      OAK OTI-601D    Mozart
10  *      OAK OTI-605     Mozart          (later version with MPU401 Midi)
11  *      OPTi 82C929     MAD16 Pro
12  *      OPTi 82C930
13  *      OPTi 82C924
14  *
15  * These audio interface chips don't produce sound themselves. They just
16  * connect some other components (OPL-[234] and a WSS compatible codec)
17  * to the PC bus and perform I/O, DMA and IRQ address decoding. There is
18  * also a UART for the MPU-401 mode (not 82C928/Mozart).
19  * The Mozart chip appears to be compatible with the 82C928, although later
20  * issues of the card, using the OTI-605 chip, have an MPU-401 compatible Midi
21  * port. This port is configured differently to that of the OPTi audio chips.
22  *
23  *      Changes
24  *      
25  *      Alan Cox                Clean up, added module selections.
26  *
27  *      A. Wik                  Added support for Opti924 PnP.
28  *                              Improved debugging support.     16-May-1998
29  *                              Fixed bug.                      16-Jun-1998
30  *
31  *      Torsten Duwe            Made Opti924 PnP support non-destructive
32  *                                                              23-Dec-1998
33  *
34  *      Paul Grayson            Added support for Midi on later Mozart cards.
35  *                                                              25-Nov-1999
36  *      Christoph Hellwig       Adapted to module_init/module_exit.
37  *      Arnaldo C. de Melo      got rid of attach_uart401       21-Sep-2000
38  *
39  *      Pavel Rabel             Clean up                           Nov-2000
40  */
41
42 #include <linux/config.h>
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/gameport.h>
46 #include <linux/spinlock.h>
47 #include "sound_config.h"
48
49 #include "ad1848.h"
50 #include "sb.h"
51 #include "mpu401.h"
52
53 static int      mad16_conf;
54 static int      mad16_cdsel;
55 static struct gameport gameport;
56 static spinlock_t lock=SPIN_LOCK_UNLOCKED;
57 static int      already_initialized;
58
59 #define C928    1
60 #define MOZART  2
61 #define C929    3
62 #define C930    4
63 #define C924    5
64
65 /*
66  *    Registers
67  *
68  *      The MAD16 occupies I/O ports 0xf8d to 0xf93 (fixed locations).
69  *      All ports are inactive by default. They can be activated by
70  *      writing 0xE2 or 0xE3 to the password register. The password is valid
71  *      only until the next I/O read or write.
72  *
73  *      82C930 uses 0xE4 as the password and indirect addressing to access
74  *      the config registers.
75  */
76
77 #define MC0_PORT        0xf8c   /* Dummy port */
78 #define MC1_PORT        0xf8d   /* SB address, CD-ROM interface type, joystick */
79 #define MC2_PORT        0xf8e   /* CD-ROM address, IRQ, DMA, plus OPL4 bit */
80 #define MC3_PORT        0xf8f
81 #define PASSWD_REG      0xf8f
82 #define MC4_PORT        0xf90
83 #define MC5_PORT        0xf91
84 #define MC6_PORT        0xf92
85 #define MC7_PORT        0xf93
86 #define MC8_PORT        0xf94
87 #define MC9_PORT        0xf95
88 #define MC10_PORT       0xf96
89 #define MC11_PORT       0xf97
90 #define MC12_PORT       0xf98
91
92 static int      board_type = C928;
93
94 static int     *mad16_osp;
95 static int      c931_detected;  /* minor differences from C930 */
96 static char     c924pnp;        /* "     "           "    C924 */
97 static int      debug;          /* debugging output */
98
99 #ifdef DDB
100 #undef DDB
101 #endif
102 #define DDB(x) do {if (debug) x;} while (0)
103
104 static unsigned char mad_read(int port)
105 {
106         unsigned long flags;
107         unsigned char tmp;
108
109         spin_lock_irqsave(&lock,flags);
110
111         switch (board_type)     /* Output password */
112         {
113                 case C928:
114                 case MOZART:
115                         outb((0xE2), PASSWD_REG);
116                         break;
117
118                 case C929:
119                         outb((0xE3), PASSWD_REG);
120                         break;
121
122                 case C930:
123                         /* outb(( 0xE4),  PASSWD_REG); */
124                         break;
125
126                 case C924:
127                         /* the c924 has its ports relocated by -128 if
128                            PnP is enabled  -aw */
129                         if (!c924pnp)
130                                 outb((0xE5), PASSWD_REG); else
131                                 outb((0xE5), PASSWD_REG - 0x80);
132                         break;
133         }
134
135         if (board_type == C930)
136         {
137                 outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
138                 tmp = inb(0xe0f);       /* Read from data reg */
139         }
140         else
141                 if (!c924pnp)
142                         tmp = inb(port); else
143                         tmp = inb(port-0x80);
144         spin_unlock_irqrestore(&lock,flags);
145
146         return tmp;
147 }
148
149 static void mad_write(int port, int value)
150 {
151         unsigned long   flags;
152
153         spin_lock_irqsave(&lock,flags);
154
155         switch (board_type)     /* Output password */
156         {
157                 case C928:
158                 case MOZART:
159                         outb((0xE2), PASSWD_REG);
160                         break;
161
162                 case C929:
163                         outb((0xE3), PASSWD_REG);
164                         break;
165
166                 case C930:
167                         /* outb(( 0xE4),  PASSWD_REG); */
168                         break;
169
170                 case C924:
171                         if (!c924pnp)
172                                 outb((0xE5), PASSWD_REG); else
173                                 outb((0xE5), PASSWD_REG - 0x80);
174                         break;
175         }
176
177         if (board_type == C930)
178         {
179                 outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
180                 outb(((unsigned char) (value & 0xff)), 0xe0f);
181         }
182         else
183                 if (!c924pnp)
184                         outb(((unsigned char) (value & 0xff)), port); else
185                         outb(((unsigned char) (value & 0xff)), port-0x80);
186         spin_unlock_irqrestore(&lock,flags);
187 }
188
189 static int __init detect_c930(void)
190 {
191         unsigned char   tmp = mad_read(MC1_PORT);
192
193         if ((tmp & 0x06) != 0x06)
194         {
195                 DDB(printk("Wrong C930 signature (%x)\n", tmp));
196                 /* return 0; */
197         }
198         mad_write(MC1_PORT, 0);
199
200         if (mad_read(MC1_PORT) != 0x06)
201         {
202                 DDB(printk("Wrong C930 signature2 (%x)\n", tmp));
203                 /* return 0; */
204         }
205         mad_write(MC1_PORT, tmp);       /* Restore bits */
206
207         mad_write(MC7_PORT, 0);
208         if ((tmp = mad_read(MC7_PORT)) != 0)
209         {
210                 DDB(printk("MC7 not writable (%x)\n", tmp));
211                 return 0;
212         }
213         mad_write(MC7_PORT, 0xcb);
214         if ((tmp = mad_read(MC7_PORT)) != 0xcb)
215         {
216                 DDB(printk("MC7 not writable2 (%x)\n", tmp));
217                 return 0;
218         }
219
220         tmp = mad_read(MC0_PORT+18);
221         if (tmp == 0xff || tmp == 0x00)
222                 return 1;
223         /* We probably have a C931 */
224         DDB(printk("Detected C931 config=0x%02x\n", tmp));
225         c931_detected = 1;
226
227         /*
228          * We cannot configure the chip if it is in PnP mode.
229          * If we have a CSN assigned (bit 8 in MC13) we first try
230          * a software reset, then a software power off, finally
231          * Clearing PnP mode. The last option is not
232          * Bit 8 in MC13 
233          */
234         if ((mad_read(MC0_PORT+13) & 0x80) == 0)
235                 return 1;
236
237         /* Software reset */
238         mad_write(MC9_PORT, 0x02);
239         mad_write(MC9_PORT, 0x00);
240
241         if ((mad_read(MC0_PORT+13) & 0x80) == 0)
242                 return 1;
243         
244         /* Power off, and on again */
245         mad_write(MC9_PORT, 0xc2);
246         mad_write(MC9_PORT, 0xc0);
247
248         if ((mad_read(MC0_PORT+13) & 0x80) == 0)
249                 return 1;
250         
251 #if 0   
252         /* Force off PnP mode. This is not recommended because
253          * the PnP bios will not recognize the chip on the next
254          * warm boot and may assignd different resources to other
255          * PnP/PCI cards.
256          */
257         mad_write(MC0_PORT+17, 0x04);
258 #endif
259         return 1;
260 }
261
262 static int __init detect_mad16(void)
263 {
264         unsigned char tmp, tmp2, bit;
265         int i, port;
266
267         /*
268          * Check that reading a register doesn't return bus float (0xff)
269          * when the card is accessed using password. This may fail in case
270          * the card is in low power mode. Normally at least the power saving
271          * mode bit should be 0.
272          */
273
274         if ((tmp = mad_read(MC1_PORT)) == 0xff)
275         {
276                 DDB(printk("MC1_PORT returned 0xff\n"));
277                 return 0;
278         }
279         for (i = 0xf8d; i <= 0xf98; i++)
280                 if (!c924pnp)
281                         DDB(printk("Port %0x (init value) = %0x\n", i, mad_read(i)));
282                 else
283                         DDB(printk("Port %0x (init value) = %0x\n", i-0x80, mad_read(i)));
284
285         if (board_type == C930)
286                 return detect_c930();
287
288         /*
289          * Now check that the gate is closed on first I/O after writing
290          * the password. (This is how a MAD16 compatible card works).
291          */
292
293         if ((tmp2 = inb(MC1_PORT)) == tmp)      /* It didn't close */
294         {
295                 DDB(printk("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
296                 return 0;
297         }
298
299         bit  = (c924pnp) ?     0x20 : 0x80;
300         port = (c924pnp) ? MC2_PORT : MC1_PORT;
301
302         tmp = mad_read(port);
303         mad_write(port, tmp ^ bit);     /* Toggle a bit */
304         if ((tmp2 = mad_read(port)) != (tmp ^ bit))     /* Compare the bit */
305         {
306                 mad_write(port, tmp);   /* Restore */
307                 DDB(printk("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
308                 return 0;
309         }
310         mad_write(port, tmp);   /* Restore */
311         return 1;               /* Bingo */
312 }
313
314 static int __init wss_init(struct address_info *hw_config)
315 {
316         int ad_flags = 0;
317
318         /*
319          *    Verify the WSS parameters
320          */
321
322         if (check_region(hw_config->io_base, 8))
323         {
324                 printk(KERN_ERR "MSS: I/O port conflict\n");
325                 return 0;
326         }
327         if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
328                 return 0;
329         /*
330          * Check if the IO port returns valid signature. The original MS Sound
331          * system returns 0x04 while some cards (AudioTrix Pro for example)
332          * return 0x00.
333          */
334
335         if ((inb(hw_config->io_base + 3) & 0x3f) != 0x04 &&
336             (inb(hw_config->io_base + 3) & 0x3f) != 0x00)
337         {
338                 DDB(printk("No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, inb(hw_config->io_base + 3)));
339                 return 0;
340         }
341         if (hw_config->irq > 11)
342         {
343                 printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
344                 return 0;
345         }
346         if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
347         {
348                 printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
349                 return 0;
350         }
351         /*
352          * Check that DMA0 is not in use with a 8 bit board.
353          */
354
355         if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
356         {
357                 printk("MSS: Can't use DMA0 with a 8 bit card/slot\n");
358                 return 0;
359         }
360         if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
361                 printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
362         return 1;
363 }
364
365 static int __init init_c930(struct address_info *hw_config)
366 {
367         unsigned char cfg = 0;
368
369         cfg |= (0x0f & mad16_conf);
370
371         if(c931_detected)
372         {
373                 /* Bit 0 has reversd meaning. Bits 1 and 2 sese
374                    reversed on write.
375                    Support only IDE cdrom. IDE port programmed
376                    somewhere else. */
377                 cfg =  (cfg & 0x09) ^ 0x07;
378         }
379
380         switch (hw_config->io_base)
381         {
382                 case 0x530:
383                         cfg |= 0x00;
384                         break;
385                 case 0xe80:
386                         cfg |= 0x10;
387                         break;
388                 case 0xf40:
389                         cfg |= 0x20;
390                         break;
391                 case 0x604:
392                         cfg |= 0x30;
393                         break;
394                 default:
395                         printk(KERN_ERR "MAD16: Invalid codec port %x\n", hw_config->io_base);
396                         return 0;
397         }
398         mad_write(MC1_PORT, cfg);
399
400         /* MC2 is CD configuration. Don't touch it. */
401
402         mad_write(MC3_PORT, 0); /* Disable SB mode IRQ and DMA */
403
404         /* bit 2 of MC4 reverses it's meaning between the C930
405            and the C931. */
406         cfg = c931_detected ? 0x04 : 0x00;
407
408         if(mad16_cdsel & 0x20)
409                 mad_write(MC4_PORT, 0x62|cfg);  /* opl4 */
410         else
411                 mad_write(MC4_PORT, 0x52|cfg);  /* opl3 */
412
413         mad_write(MC5_PORT, 0x3C);      /* Init it into mode2 */
414         mad_write(MC6_PORT, 0x02);      /* Enable WSS, Disable MPU and SB */
415         mad_write(MC7_PORT, 0xCB);
416         mad_write(MC10_PORT, 0x11);
417
418         return wss_init(hw_config);
419 }
420
421 static int __init chip_detect(void)
422 {
423         int i;
424
425         /*
426          *    Then try to detect with the old password
427          */
428         board_type = C924;
429
430         DDB(printk("Detect using password = 0xE5\n"));
431         
432         if (detect_mad16()) {
433                 return 1;
434         }
435         
436         board_type = C928;
437
438         DDB(printk("Detect using password = 0xE2\n"));
439
440         if (detect_mad16())
441         {
442                 unsigned char model;
443
444                 if (((model = mad_read(MC3_PORT)) & 0x03) == 0x03) {
445                         DDB(printk("mad16.c: Mozart detected\n"));
446                         board_type = MOZART;
447                 } else {
448                         DDB(printk("mad16.c: 82C928 detected???\n"));
449                         board_type = C928;
450                 }
451                 return 1;
452         }
453
454         board_type = C929;
455
456         DDB(printk("Detect using password = 0xE3\n"));
457
458         if (detect_mad16())
459         {
460                 DDB(printk("mad16.c: 82C929 detected\n"));
461                 return 1;
462         }
463
464         if (inb(PASSWD_REG) != 0xff)
465                 return 0;
466
467         /*
468          * First relocate MC# registers to 0xe0e/0xe0f, disable password 
469          */
470
471         outb((0xE4), PASSWD_REG);
472         outb((0x80), PASSWD_REG);
473
474         board_type = C930;
475
476         DDB(printk("Detect using password = 0xE4\n"));
477
478         for (i = 0xf8d; i <= 0xf93; i++)
479                 DDB(printk("port %03x = %02x\n", i, mad_read(i)));
480
481         if(detect_mad16()) {
482                 DDB(printk("mad16.c: 82C930 detected\n"));
483                 return 1;
484         }
485
486         /* The C931 has the password reg at F8D */
487         outb((0xE4), 0xF8D);
488         outb((0x80), 0xF8D);
489         DDB(printk("Detect using password = 0xE4 for C931\n"));
490
491         if (detect_mad16()) {
492                 return 1;
493         }
494
495         board_type = C924;
496         c924pnp++;
497         DDB(printk("Detect using password = 0xE5 (again), port offset -0x80\n"));
498         if (detect_mad16()) {
499                 DDB(printk("mad16.c: 82C924 PnP detected\n"));
500                 return 1;
501         }
502         
503         c924pnp=0;
504
505         return 0;
506 }
507
508 static int __init probe_mad16(struct address_info *hw_config)
509 {
510         int i;
511         static int valid_ports[] = 
512         {
513                 0x530, 0xe80, 0xf40, 0x604
514         };
515         unsigned char tmp;
516         unsigned char cs4231_mode = 0;
517
518         int ad_flags = 0;
519
520         if (already_initialized)
521                 return 0;
522
523         mad16_osp = hw_config->osp;
524
525         /*
526          *    Check that all ports return 0xff (bus float) when no password
527          *      is written to the password register.
528          */
529
530         DDB(printk("--- Detecting MAD16 / Mozart ---\n"));
531         if (!chip_detect())
532                 return 0;
533
534         if (board_type == C930)
535                 return init_c930(hw_config);
536
537
538         for (i = 0xf8d; i <= 0xf93; i++) {
539                 if (!c924pnp)
540                         DDB(printk("port %03x = %02x\n", i, mad_read(i)));
541                 else
542                         DDB(printk("port %03x = %02x\n", i-0x80, mad_read(i)));
543         }
544
545 /*
546  * Set the WSS address
547  */
548
549         tmp = (mad_read(MC1_PORT) & 0x0f) | 0x80;       /* Enable WSS, Disable SB */
550
551         for (i = 0; i < 5; i++)
552         {
553                 if (i > 3)      /* Not a valid port */
554                 {
555                         printk(KERN_ERR "MAD16/Mozart: Bad WSS base address 0x%x\n", hw_config->io_base);
556                         return 0;
557                 }
558                 if (valid_ports[i] == hw_config->io_base)
559                 {
560                         tmp |= i << 4;  /* WSS port select bits */
561                         break;
562                 }
563         }
564
565         /*
566          * Set optional CD-ROM and joystick settings.
567          */
568
569         tmp &= ~0x0f;
570         tmp |= (mad16_conf & 0x0f);     /* CD-ROM and joystick bits */
571         mad_write(MC1_PORT, tmp);
572
573         tmp = mad16_cdsel;
574         mad_write(MC2_PORT, tmp);
575         mad_write(MC3_PORT, 0xf0);      /* Disable SB */
576
577         if (board_type == C924) /* Specific C924 init values */
578         {
579                 mad_write(MC4_PORT, 0xA0);
580                 mad_write(MC5_PORT, 0x05);
581                 mad_write(MC6_PORT, 0x03);
582         }
583         if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
584                 return 0;
585
586         if (ad_flags & (AD_F_CS4231 | AD_F_CS4248))
587                 cs4231_mode = 0x02;     /* CS4248/CS4231 sync delay switch */
588
589         if (board_type == C929)
590         {
591                 mad_write(MC4_PORT, 0xa2);
592                 mad_write(MC5_PORT, 0xA5 | cs4231_mode);
593                 mad_write(MC6_PORT, 0x03);      /* Disable MPU401 */
594         }
595         else
596         {
597                 mad_write(MC4_PORT, 0x02);
598                 mad_write(MC5_PORT, 0x30 | cs4231_mode);
599         }
600
601         for (i = 0xf8d; i <= 0xf93; i++) {
602                 if (!c924pnp)
603                         DDB(printk("port %03x after init = %02x\n", i, mad_read(i)));
604                 else
605                         DDB(printk("port %03x after init = %02x\n", i-0x80, mad_read(i)));
606         }
607         wss_init(hw_config);
608
609         return 1;
610 }
611
612 static void __init attach_mad16(struct address_info *hw_config)
613 {
614
615         static signed char     interrupt_bits[12] = {
616                 -1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
617         };
618         signed char bits;
619
620         static char     dma_bits[4] = {
621                 1, 2, 0, 3
622         };
623
624         int config_port = hw_config->io_base + 0, version_port = hw_config->io_base + 3;
625         int ad_flags = 0, dma = hw_config->dma, dma2 = hw_config->dma2;
626         unsigned char dma2_bit = 0;
627
628         already_initialized = 1;
629
630         if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
631                 return;
632
633         /*
634          * Set the IRQ and DMA addresses.
635          */
636         
637         if (board_type == C930 || c924pnp)
638                 interrupt_bits[5] = 0x28;       /* Also IRQ5 is possible on C930 */
639
640         bits = interrupt_bits[hw_config->irq];
641         if (bits == -1)
642                 return;
643
644         outb((bits | 0x40), config_port);
645         if ((inb(version_port) & 0x40) == 0)
646                 printk(KERN_ERR "[IRQ Conflict?]\n");
647
648         /*
649          * Handle the capture DMA channel
650          */
651
652         if (ad_flags & AD_F_CS4231 && dma2 != -1 && dma2 != dma)
653         {
654                 if (!((dma == 0 && dma2 == 1) ||
655                         (dma == 1 && dma2 == 0) ||
656                         (dma == 3 && dma2 == 0)))
657                 {               /* Unsupported combination. Try to swap channels */
658                         int tmp = dma;
659
660                         dma = dma2;
661                         dma2 = tmp;
662                 }
663                 if ((dma == 0 && dma2 == 1) || (dma == 1 && dma2 == 0) ||
664                         (dma == 3 && dma2 == 0))
665                 {
666                         dma2_bit = 0x04;        /* Enable capture DMA */
667                 }
668                 else
669                 {
670                         printk("MAD16: Invalid capture DMA\n");
671                         dma2 = dma;
672                 }
673         }
674         else dma2 = dma;
675
676         outb((bits | dma_bits[dma] | dma2_bit), config_port);   /* Write IRQ+DMA setup */
677
678         hw_config->slots[0] = ad1848_init("mad16 WSS", hw_config->io_base + 4,
679                                           hw_config->irq,
680                                           dma,
681                                           dma2, 0,
682                                           hw_config->osp,
683                                           THIS_MODULE);
684         request_region(hw_config->io_base, 4, "mad16 WSS config");
685 }
686
687 static int __init probe_mad16_mpu(struct address_info *hw_config)
688 {
689         static int mpu_attached;
690         unsigned char tmp;
691
692         if (!already_initialized)       /* The MSS port must be initialized first */
693                 return 0;
694
695         if (mpu_attached)               /* Don't let them call this twice */
696                 return 0;
697         mpu_attached = 1;
698
699         if (board_type < C929)  /* Early chip. No MPU support. Just SB MIDI */
700         {
701
702 #ifdef CONFIG_MAD16_OLDCARD
703
704                 tmp = mad_read(MC3_PORT);
705
706                 /* 
707                  * MAD16 SB base is defined by the WSS base. It cannot be changed 
708                  * alone.
709                  * Ignore configured I/O base. Use the active setting. 
710                  */
711
712                 if (mad_read(MC1_PORT) & 0x20)
713                         hw_config->io_base = 0x240;
714                 else
715                         hw_config->io_base = 0x220;
716
717                 switch (hw_config->irq)
718                 {
719                         case 5:
720                                 tmp = (tmp & 0x3f) | 0x80;
721                                 break;
722                         case 7:
723                                 tmp = (tmp & 0x3f);
724                                 break;
725                         case 11:
726                                 tmp = (tmp & 0x3f) | 0x40;
727                                 break;
728                         default:
729                                 printk(KERN_ERR "mad16/Mozart: Invalid MIDI IRQ\n");
730                                 return 0;
731                 }
732
733                 mad_write(MC3_PORT, tmp | 0x04);
734                 hw_config->driver_use_1 = SB_MIDI_ONLY;
735                 if (!sb_dsp_detect(hw_config, 0, 0, NULL))
736                         return 0;
737
738                 if (mad_read(MC1_PORT) & 0x20)
739                         hw_config->io_base = 0x240;
740                 else
741                         hw_config->io_base = 0x220;
742
743                 hw_config->name = "Mad16/Mozart";
744                 sb_dsp_init(hw_config, THIS_MODULE);
745                 return 1;
746 #else
747                 /* assuming all later Mozart cards are identified as
748                  * either 82C928 or Mozart. If so, following code attempts
749                  * to set MPU register. TODO - add probing
750                  */
751
752                 tmp = mad_read(MC8_PORT);
753
754                 switch (hw_config->irq)
755                 {
756                         case 5:
757                                 tmp |= 0x08;
758                                 break;
759                         case 7:
760                                 tmp |= 0x10;
761                                 break;
762                         case 9:
763                                 tmp |= 0x18;
764                                 break;
765                         case 10:
766                                 tmp |= 0x20;
767                                 break;
768                         case 11:
769                                 tmp |= 0x28;
770                                 break;
771                         default:
772                                 printk(KERN_ERR "mad16/MOZART: invalid mpu_irq\n");
773                                 return 0;
774                 }
775
776                 switch (hw_config->io_base)
777                 {
778                         case 0x300:
779                                 tmp |= 0x01;
780                                 break;
781                         case 0x310:
782                                 tmp |= 0x03;
783                                 break;
784                         case 0x320:
785                                 tmp |= 0x05;
786                                 break;
787                         case 0x330:
788                                 tmp |= 0x07;
789                                 break;
790                         default:
791                                 printk(KERN_ERR "mad16/MOZART: invalid mpu_io\n");
792                                 return 0;
793                 }
794
795                 mad_write(MC8_PORT, tmp);       /* write MPU port parameters */
796                 goto probe_401;
797 #endif
798         }
799         tmp = mad_read(MC6_PORT) & 0x83;
800         tmp |= 0x80;            /* MPU-401 enable */
801
802         /* Set the MPU base bits */
803
804         switch (hw_config->io_base)
805         {
806                 case 0x300:
807                         tmp |= 0x60;
808                         break;
809                 case 0x310:
810                         tmp |= 0x40;
811                         break;
812                 case 0x320:
813                         tmp |= 0x20;
814                         break;
815                 case 0x330:
816                         tmp |= 0x00;
817                         break;
818                 default:
819                         printk(KERN_ERR "MAD16: Invalid MIDI port 0x%x\n", hw_config->io_base);
820                         return 0;
821         }
822
823         /* Set the MPU IRQ bits */
824
825         switch (hw_config->irq)
826         {
827                 case 5:
828                         tmp |= 0x10;
829                         break;
830                 case 7:
831                         tmp |= 0x18;
832                         break;
833                 case 9:
834                         tmp |= 0x00;
835                         break;
836                 case 10:
837                         tmp |= 0x08;
838                         break;
839                 default:
840                         printk(KERN_ERR "MAD16: Invalid MIDI IRQ %d\n", hw_config->irq);
841                         break;
842         }
843                         
844         mad_write(MC6_PORT, tmp);       /* Write MPU401 config */
845
846 #ifndef CONFIG_MAD16_OLDCARD
847 probe_401:
848 #endif
849         hw_config->driver_use_1 = SB_MIDI_ONLY;
850         hw_config->name = "Mad16/Mozart";
851         return probe_uart401(hw_config, THIS_MODULE);
852 }
853
854 static void __exit unload_mad16(struct address_info *hw_config)
855 {
856         ad1848_unload(hw_config->io_base + 4,
857                         hw_config->irq,
858                         hw_config->dma,
859                         hw_config->dma2, 0);
860         release_region(hw_config->io_base, 4);
861         sound_unload_audiodev(hw_config->slots[0]);
862 }
863
864 static void __exit unload_mad16_mpu(struct address_info *hw_config)
865 {
866 #ifdef CONFIG_MAD16_OLDCARD
867         if (board_type < C929)  /* Early chip. No MPU support. Just SB MIDI */
868         {
869                 sb_dsp_unload(hw_config, 0);
870                 return;
871         }
872 #endif
873
874         unload_uart401(hw_config);
875 }
876
877 static struct address_info cfg;
878 static struct address_info cfg_mpu;
879
880 static int found_mpu;
881
882 static int __initdata mpu_io = 0;
883 static int __initdata mpu_irq = 0;
884 static int __initdata io = -1;
885 static int __initdata dma = -1;
886 static int __initdata dma16 = -1; /* Set this for modules that need it */
887 static int __initdata irq = -1;
888 static int __initdata cdtype = 0;
889 static int __initdata cdirq = 0;
890 static int __initdata cdport = 0x340;
891 static int __initdata cddma = -1;
892 static int __initdata opl4 = 0;
893 static int __initdata joystick = 0;
894
895 MODULE_PARM(mpu_io, "i");
896 MODULE_PARM(mpu_irq, "i");
897 MODULE_PARM(io,"i");
898 MODULE_PARM(dma,"i");
899 MODULE_PARM(dma16,"i");
900 MODULE_PARM(irq,"i");
901 MODULE_PARM(cdtype,"i");
902 MODULE_PARM(cdirq,"i");
903 MODULE_PARM(cdport,"i");
904 MODULE_PARM(cddma,"i");
905 MODULE_PARM(opl4,"i");
906 MODULE_PARM(joystick,"i");
907 MODULE_PARM(debug,"i");
908
909 static int __initdata dma_map[2][8] =
910 {
911         {0x03, -1, -1, -1, -1, 0x00, 0x01, 0x02},
912         {0x03, -1, 0x01, 0x00, -1, -1, -1, -1}
913 };
914
915 static int __initdata irq_map[16] =
916 {
917         0x00, -1, -1, 0x0A,
918         -1, 0x04, -1, 0x08,
919         -1, 0x10, 0x14, 0x18,
920         -1, -1, -1, -1
921 };
922
923 static int __init init_mad16(void)
924 {
925         int dmatype = 0;
926
927         printk(KERN_INFO "MAD16 audio driver Copyright (C) by Hannu Savolainen 1993-1996\n");
928
929         printk(KERN_INFO "CDROM ");
930         switch (cdtype)
931         {
932                 case 0x00:
933                         printk("Disabled");
934                         cdirq = 0;
935                         break;
936                 case 0x02:
937                         printk("Sony CDU31A");
938                         dmatype = 1;
939                         if(cddma == -1) cddma = 3;
940                         break;
941                 case 0x04:
942                         printk("Mitsumi");
943                         dmatype = 0;
944                         if(cddma == -1) cddma = 5;
945                         break;
946                 case 0x06:
947                         printk("Panasonic Lasermate");
948                         dmatype = 1;
949                         if(cddma == -1) cddma = 3;
950                         break;
951                 case 0x08:
952                         printk("Secondary IDE");
953                         dmatype = 0;
954                         if(cddma == -1) cddma = 5;
955                         break;
956                 case 0x0A:
957                         printk("Primary IDE");
958                         dmatype = 0;
959                         if(cddma == -1) cddma = 5;
960                         break;
961                 default:
962                         printk("\n");
963                         printk(KERN_ERR "Invalid CDROM type\n");
964                         return -EINVAL;
965         }
966
967         /*
968          *    Build the config words
969          */
970
971         mad16_conf = (joystick ^ 1) | cdtype;
972         mad16_cdsel = 0;
973         if (opl4)
974                 mad16_cdsel |= 0x20;
975
976         if(cdtype){
977                 if (cddma > 7 || cddma < 0 || dma_map[dmatype][cddma] == -1)
978                 {
979                         printk("\n");
980                         printk(KERN_ERR "Invalid CDROM DMA\n");
981                         return -EINVAL;
982                 }
983                 if (cddma)
984                         printk(", DMA %d", cddma);
985                 else
986                         printk(", no DMA");
987
988                 if (!cdirq)
989                         printk(", no IRQ");
990                 else if (cdirq < 0 || cdirq > 15 || irq_map[cdirq] == -1)
991                 {
992                         printk(", invalid IRQ (disabling)");
993                         cdirq = 0;
994                 }
995                 else printk(", IRQ %d", cdirq);
996
997                 mad16_cdsel |= dma_map[dmatype][cddma];
998
999                 if (cdtype < 0x08)
1000                 {
1001                         switch (cdport)
1002                         {
1003                                 case 0x340:
1004                                         mad16_cdsel |= 0x00;
1005                                         break;
1006                                 case 0x330:
1007                                         mad16_cdsel |= 0x40;
1008                                         break;
1009                                 case 0x360:
1010                                         mad16_cdsel |= 0x80;
1011                                         break;
1012                                 case 0x320:
1013                                         mad16_cdsel |= 0xC0;
1014                                         break;
1015                                 default:
1016                                         printk(KERN_ERR "Unknown CDROM I/O base %d\n", cdport);
1017                                         return -EINVAL;
1018                         }
1019                 }
1020                 mad16_cdsel |= irq_map[cdirq];
1021         }
1022
1023         printk(".\n");
1024
1025         cfg.io_base = io;
1026         cfg.irq = irq;
1027         cfg.dma = dma;
1028         cfg.dma2 = dma16;
1029
1030         if (cfg.io_base == -1 || cfg.dma == -1 || cfg.irq == -1) {
1031                 printk(KERN_ERR "I/O, DMA and irq are mandatory\n");
1032                 return -EINVAL;
1033         }
1034         
1035         if (!probe_mad16(&cfg))
1036                 return -ENODEV;
1037
1038         cfg_mpu.io_base = mpu_io;
1039         cfg_mpu.irq = mpu_irq;
1040
1041         attach_mad16(&cfg);
1042
1043         found_mpu = probe_mad16_mpu(&cfg_mpu);
1044
1045         if (joystick == 1) {
1046                 /* register gameport */
1047                 if (!request_region(0x201, 1, "mad16 gameport"))
1048                         printk(KERN_ERR "mad16: gameport address 0x201 already in use\n");
1049                 else {
1050                         printk(KERN_ERR "mad16: gameport enabled at 0x201\n");
1051                         gameport.io = 0x201;
1052                         gameport_register_port(&gameport);
1053                 }
1054         }
1055         else printk(KERN_ERR "mad16: gameport disabled.\n");
1056         return 0;
1057 }
1058
1059 static void __exit cleanup_mad16(void)
1060 {
1061         if (found_mpu)
1062                 unload_mad16_mpu(&cfg_mpu);
1063         if (gameport.io) {
1064                 /* the gameport was initialized so we must free it up */
1065                 gameport_unregister_port(&gameport);
1066                 gameport.io = 0;
1067                 release_region(0x201, 1);
1068         }
1069         unload_mad16(&cfg);
1070 }
1071
1072 module_init(init_mad16);
1073 module_exit(cleanup_mad16);
1074
1075 #ifndef MODULE
1076 static int __init setup_mad16(char *str)
1077 {
1078         /* io, irq */
1079         int ints[8];
1080
1081         str = get_options(str, ARRAY_SIZE(ints), ints);
1082
1083         io       = ints[1];
1084         irq      = ints[2];
1085         dma      = ints[3];
1086         dma16    = ints[4];
1087         mpu_io   = ints[5];
1088         mpu_irq  = ints[6];
1089         joystick = ints[7];
1090
1091         return 1;
1092 }
1093
1094 __setup("mad16=", setup_mad16);
1095 #endif
1096 MODULE_LICENSE("GPL");