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