ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / oss / opl3sa.c
1 /*
2  * sound/opl3sa.c
3  *
4  * Low level driver for Yamaha YMF701B aka OPL3-SA chip
5  * 
6  *
7  *
8  * Copyright (C) by Hannu Savolainen 1993-1997
9  *
10  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
11  * Version 2 (June 1991). See the "COPYING" file distributed with this software
12  * for more info.
13  *
14  * Changes:
15  *      Alan Cox                Modularisation
16  *      Christoph Hellwig       Adapted to module_init/module_exit
17  *      Arnaldo C. de Melo      got rid of attach_uart401
18  *
19  * FIXME:
20  *      Check for install of mpu etc is wrong, should check result of the mss stuff
21  */
22
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/spinlock.h>
26
27 #undef  SB_OK
28
29 #include "sound_config.h"
30
31 #include "ad1848.h"
32 #include "mpu401.h"
33
34 #ifdef SB_OK
35 #include "sb.h"
36 static int sb_initialized;
37 #endif
38
39 static int kilroy_was_here;     /* Don't detect twice */
40 static int mpu_initialized;
41 static spinlock_t lock=SPIN_LOCK_UNLOCKED;
42 static int *opl3sa_osp;
43
44 static unsigned char opl3sa_read(int addr)
45 {
46         unsigned long flags;
47         unsigned char tmp;
48
49         spin_lock_irqsave(&lock,flags);
50         outb((0x1d), 0xf86);    /* password */
51         outb(((unsigned char) addr), 0xf86);    /* address */
52         tmp = inb(0xf87);       /* data */
53         spin_unlock_irqrestore(&lock,flags);
54
55         return tmp;
56 }
57
58 static void opl3sa_write(int addr, int data)
59 {
60         unsigned long flags;
61
62         spin_lock_irqsave(&lock,flags);
63         outb((0x1d), 0xf86);    /* password */
64         outb(((unsigned char) addr), 0xf86);    /* address */
65         outb(((unsigned char) data), 0xf87);    /* data */
66         spin_unlock_irqrestore(&lock,flags);
67 }
68
69 static int __init opl3sa_detect(void)
70 {
71         int tmp;
72
73         if (((tmp = opl3sa_read(0x01)) & 0xc4) != 0x04)
74         {
75                 DDB(printk("OPL3-SA detect error 1 (%x)\n", opl3sa_read(0x01)));
76                 /* return 0; */
77         }
78
79         /*
80          * Check that the password feature has any effect
81          */
82         
83         if (inb(0xf87) == tmp)
84         {
85                 DDB(printk("OPL3-SA detect failed 2 (%x/%x)\n", tmp, inb(0xf87)));
86                 return 0;
87         }
88         tmp = (opl3sa_read(0x04) & 0xe0) >> 5;
89
90         if (tmp != 0 && tmp != 1)
91         {
92                 DDB(printk("OPL3-SA detect failed 3 (%d)\n", tmp));
93                 return 0;
94         }
95         DDB(printk("OPL3-SA mode %x detected\n", tmp));
96
97         opl3sa_write(0x01, 0x00);       /* Disable MSS */
98         opl3sa_write(0x02, 0x00);       /* Disable SB */
99         opl3sa_write(0x03, 0x00);       /* Disable MPU */
100
101         return 1;
102 }
103
104 /*
105  *    Probe and attach routines for the Windows Sound System mode of
106  *     OPL3-SA
107  */
108
109 static int __init probe_opl3sa_wss(struct address_info *hw_config)
110 {
111         int ret;
112         unsigned char tmp = 0x24;       /* WSS enable */
113
114         if (check_region(0xf86, 2))     /* Control port is busy */
115                 return 0;
116         /*
117          * Check if the IO port returns valid signature. The original MS Sound
118          * system returns 0x04 while some cards (OPL3-SA for example)
119          * return 0x00.
120          */
121
122         if (check_region(hw_config->io_base, 8))
123         {
124                 printk(KERN_ERR "OPL3-SA: MSS I/O port conflict (%x)\n", hw_config->io_base);
125                 return 0;
126         }
127         opl3sa_osp = hw_config->osp;
128
129         if (!opl3sa_detect())
130         {
131                 printk(KERN_ERR "OSS: OPL3-SA chip not found\n");
132                 return 0;
133         }
134         
135         switch (hw_config->io_base)
136         {
137                 case 0x530:
138                         tmp |= 0x00;
139                         break;
140                 case 0xe80:
141                         tmp |= 0x08;
142                         break;
143                 case 0xf40:
144                         tmp |= 0x10;
145                         break;
146                 case 0x604:
147                         tmp |= 0x18;
148                         break;
149                 default:
150                         printk(KERN_ERR "OSS: Unsupported OPL3-SA/WSS base %x\n", hw_config->io_base);
151                   return 0;
152         }
153
154         opl3sa_write(0x01, tmp);        /* WSS setup register */
155         kilroy_was_here = 1;
156
157         ret = probe_ms_sound(hw_config);
158         if (ret)
159                 request_region(0xf86, 2, "OPL3-SA");
160
161         return ret;
162 }
163
164 static void __init attach_opl3sa_wss(struct address_info *hw_config)
165 {
166         int nm = num_mixers;
167
168         /* FIXME */
169         attach_ms_sound(hw_config, THIS_MODULE);
170         if (num_mixers > nm)    /* A mixer was installed */
171         {
172                 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
173                 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
174                 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
175         }
176 }
177
178
179 static int __init probe_opl3sa_mpu(struct address_info *hw_config)
180 {
181         unsigned char conf;
182         static signed char irq_bits[] = {
183                 -1, -1, -1, -1, -1, 1, -1, 2, -1, 3, 4
184         };
185
186         if (!kilroy_was_here)
187                 return 0;       /* OPL3-SA has not been detected earlier */
188
189         if (mpu_initialized)
190         {
191                 DDB(printk("OPL3-SA: MPU mode already initialized\n"));
192                 return 0;
193         }
194         if (hw_config->irq > 10)
195         {
196                 printk(KERN_ERR "OPL3-SA: Bad MPU IRQ %d\n", hw_config->irq);
197                 return 0;
198         }
199         if (irq_bits[hw_config->irq] == -1)
200         {
201                 printk(KERN_ERR "OPL3-SA: Bad MPU IRQ %d\n", hw_config->irq);
202                 return 0;
203         }
204         switch (hw_config->io_base)
205         {
206                 case 0x330:
207                         conf = 0x00;
208                         break;
209                 case 0x332:
210                         conf = 0x20;
211                         break;
212                 case 0x334:
213                         conf = 0x40;
214                         break;
215                 case 0x300:
216                         conf = 0x60;
217                         break;
218                 default:
219                         return 0;       /* Invalid port */
220         }
221
222         conf |= 0x83;           /* MPU & OPL3 (synth) & game port enable */
223         conf |= irq_bits[hw_config->irq] << 2;
224
225         opl3sa_write(0x03, conf);
226
227         mpu_initialized = 1;
228         hw_config->name = "OPL3-SA (MPU401)";
229
230         return probe_uart401(hw_config, THIS_MODULE);
231 }
232
233 static void __exit unload_opl3sa_wss(struct address_info *hw_config)
234 {
235         int dma2 = hw_config->dma2;
236
237         if (dma2 == -1)
238                 dma2 = hw_config->dma;
239
240         release_region(0xf86, 2);
241         release_region(hw_config->io_base, 4);
242
243         ad1848_unload(hw_config->io_base + 4,
244                       hw_config->irq,
245                       hw_config->dma,
246                       dma2,
247                       0);
248         sound_unload_audiodev(hw_config->slots[0]);
249 }
250
251 static inline void __exit unload_opl3sa_mpu(struct address_info *hw_config)
252 {
253         unload_uart401(hw_config);
254 }
255
256 #ifdef SB_OK
257 static inline void __exit unload_opl3sa_sb(struct address_info *hw_config)
258 {
259         sb_dsp_unload(hw_config);
260 }
261 #endif
262
263 static int found_mpu;
264
265 static struct address_info cfg;
266 static struct address_info cfg_mpu;
267
268 static int __initdata io        = -1;
269 static int __initdata irq       = -1;
270 static int __initdata dma       = -1;
271 static int __initdata dma2      = -1;
272 static int __initdata mpu_io    = -1;
273 static int __initdata mpu_irq   = -1;
274
275 MODULE_PARM(io,"i");
276 MODULE_PARM(irq,"i");
277 MODULE_PARM(dma,"i");
278 MODULE_PARM(dma2,"i");
279 MODULE_PARM(mpu_io,"i");
280 MODULE_PARM(mpu_irq,"i");
281
282 static int __init init_opl3sa(void)
283 {
284         if (io == -1 || irq == -1 || dma == -1) {
285                 printk(KERN_ERR "opl3sa: dma, irq and io must be set.\n");
286                 return -EINVAL;
287         }
288
289         cfg.io_base = io;
290         cfg.irq = irq;
291         cfg.dma = dma;
292         cfg.dma2 = dma2;
293         
294         cfg_mpu.io_base = mpu_io;
295         cfg_mpu.irq = mpu_irq;
296
297         if (probe_opl3sa_wss(&cfg) == 0)
298                 return -ENODEV;
299
300         found_mpu=probe_opl3sa_mpu(&cfg_mpu);
301
302         attach_opl3sa_wss(&cfg);
303         return 0;
304 }
305
306 static void __exit cleanup_opl3sa(void)
307 {
308         if(found_mpu)
309                 unload_opl3sa_mpu(&cfg_mpu);
310         unload_opl3sa_wss(&cfg);
311 }
312
313 module_init(init_opl3sa);
314 module_exit(cleanup_opl3sa);
315
316 #ifndef MODULE
317 static int __init setup_opl3sa(char *str)
318 {
319         /* io, irq, dma, dma2, mpu_io, mpu_irq */
320         int ints[7];
321         
322         str = get_options(str, ARRAY_SIZE(ints), ints);
323         
324         io      = ints[1];
325         irq     = ints[2];
326         dma     = ints[3];
327         dma2    = ints[4];
328         mpu_io  = ints[5];
329         mpu_irq = ints[6];
330
331         return 1;
332 }
333
334 __setup("opl3sa=", setup_opl3sa);
335 #endif
336 MODULE_LICENSE("GPL");