patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / oss / sb_card.c
1 /*
2  * sound/oss/sb_card.c
3  *
4  * Detection routine for the ISA Sound Blaster and compatable sound
5  * cards.
6  *
7  * This file is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
8  * Version 2 (June 1991). See the "COPYING" file distributed with this
9  * software for more info.
10  *
11  * This is a complete rewrite of the detection routines. This was
12  * prompted by the PnP API change during v2.5 and the ugly state the
13  * code was in.
14  *
15  * Copyright (C) by Paul Laufer 2002. Based on code originally by
16  * Hannu Savolainen which was modified by many others over the
17  * years. Authors specifically mentioned in the previous version were:
18  * Daniel Stone, Alessandro Zummo, Jeff Garzik, Arnaldo Carvalho de
19  * Melo, Daniel Church, and myself.
20  *
21  * 02-05-2003 Original Release, Paul Laufer <paul@laufernet.com>
22  * 02-07-2003 Bug made it into first release. Take two.
23  */
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #ifdef CONFIG_MCA
30 #include <linux/mca.h>
31 #endif /* CONFIG_MCA */
32 #include "sound_config.h"
33 #include "sb_mixer.h"
34 #include "sb.h"
35 #ifdef CONFIG_PNP
36 #include <linux/pnp.h>
37 #endif /* CONFIG_PNP */
38 #include "sb_card.h"
39
40 MODULE_DESCRIPTION("OSS Soundblaster ISA PnP and legacy sound driver");
41 MODULE_LICENSE("GPL");
42
43 extern void *smw_free;
44
45 static int __initdata mpu_io    = 0;
46 static int __initdata io        = -1;
47 static int __initdata irq       = -1;
48 static int __initdata dma       = -1;
49 static int __initdata dma16     = -1;
50 static int __initdata type      = 0; /* Can set this to a specific card type */
51 static int __initdata esstype   = 0; /* ESS chip type */
52 static int __initdata acer      = 0; /* Do acer notebook init? */
53 static int __initdata sm_games  = 0; /* Logitech soundman games? */
54
55 struct sb_card_config *legacy = NULL;
56
57 #ifdef CONFIG_PNP
58 static int __initdata pnp       = 1;
59 /*
60 static int __initdata uart401   = 0;
61 */
62 #else
63 static int __initdata pnp       = 0;
64 #endif
65
66 module_param(io, int, 000);
67 MODULE_PARM_DESC(io,       "Soundblaster i/o base address (0x220,0x240,0x260,0x280)");
68 module_param(irq, int, 000);
69 MODULE_PARM_DESC(irq,      "IRQ (5,7,9,10)");
70 module_param(dma, int, 000);
71 MODULE_PARM_DESC(dma,      "8-bit DMA channel (0,1,3)");
72 module_param(dma16, int, 000);
73 MODULE_PARM_DESC(dma16,    "16-bit DMA channel (5,6,7)");
74 module_param(mpu_io, int, 000);
75 MODULE_PARM_DESC(mpu_io,   "MPU base address");
76 module_param(type, int, 000);
77 MODULE_PARM_DESC(type,     "You can set this to specific card type (doesn't " \
78                  "work with pnp)");
79 module_param(sm_games, int, 000);
80 MODULE_PARM_DESC(sm_games, "Enable support for Logitech soundman games " \
81                  "(doesn't work with pnp)");
82 module_param(esstype, int, 000);
83 MODULE_PARM_DESC(esstype,  "ESS chip type (doesn't work with pnp)");
84 module_param(acer, int, 000);
85 MODULE_PARM_DESC(acer,     "Set this to detect cards in some ACER notebooks "\
86                  "(doesn't work with pnp)");
87
88 #ifdef CONFIG_PNP
89 module_param(pnp, int, 000);
90 MODULE_PARM_DESC(pnp,     "Went set to 0 will disable detection using PnP. "\
91                   "Default is 1.\n");
92 /* Not done yet.... */
93 /*
94 module_param(uart401, int, 000);
95 MODULE_PARM_DESC(uart401,  "When set to 1, will attempt to detect and enable"\
96                  "the mpu on some clones");
97 */
98 #endif /* CONFIG_PNP */
99
100 /* OSS subsystem card registration shared by PnP and legacy routines */
101 static int sb_register_oss(struct sb_card_config *scc, struct sb_module_options *sbmo)
102 {
103         if(!sb_dsp_detect(&scc->conf, 0, 0, sbmo)) {
104                 printk(KERN_ERR "sb: Failed DSP Detect.\n");
105                 kfree(scc);
106                 return -ENODEV;
107         }
108         if(!sb_dsp_init(&scc->conf, THIS_MODULE)) {
109                 printk(KERN_ERR "sb: Failed DSP init.\n");
110                 kfree(scc);
111                 return -ENODEV;
112         }
113         if(scc->mpucnf.io_base > 0) {
114                 scc->mpu = 1;
115                 printk(KERN_INFO "sb: Turning on MPU\n");
116                 if(!probe_sbmpu(&scc->mpucnf, THIS_MODULE))
117                         scc->mpu = 0;
118         }
119
120         return 1;
121 }
122
123 static void sb_unload(struct sb_card_config *scc)
124 {
125         sb_dsp_unload(&scc->conf, 0);
126         if(scc->mpu)
127                 unload_sbmpu(&scc->mpucnf);
128         kfree(scc);
129 }
130
131 /* Register legacy card with OSS subsystem */
132 static int sb_init_legacy(void)
133 {
134         struct sb_module_options sbmo = {0};
135
136         if((legacy = kmalloc(sizeof(struct sb_card_config), GFP_KERNEL)) == NULL) {
137                 printk(KERN_ERR "sb: Error: Could not allocate memory\n");
138                 return -ENOMEM;
139         }
140         memset(legacy, 0, sizeof(struct sb_card_config));
141
142         legacy->conf.io_base      = io;
143         legacy->conf.irq          = irq;
144         legacy->conf.dma          = dma;
145         legacy->conf.dma2         = dma16;
146         legacy->conf.card_subtype = type;
147
148         legacy->mpucnf.io_base = mpu_io;
149         legacy->mpucnf.irq     = -1;
150         legacy->mpucnf.dma     = -1;
151         legacy->mpucnf.dma2    = -1;
152
153         sbmo.esstype  = esstype;
154         sbmo.sm_games = sm_games;
155         sbmo.acer     = acer;
156
157         return sb_register_oss(legacy, &sbmo);
158 }
159
160 #ifdef CONFIG_PNP
161
162 /* Populate the OSS subsystem structures with information from PnP */
163 static void sb_dev2cfg(struct pnp_dev *dev, struct sb_card_config *scc)
164 {
165         scc->conf.io_base   = -1;
166         scc->conf.irq       = -1;
167         scc->conf.dma       = -1;
168         scc->conf.dma2      = -1;
169         scc->mpucnf.io_base = -1;
170         scc->mpucnf.irq     = -1;
171         scc->mpucnf.dma     = -1;
172         scc->mpucnf.dma2    = -1;
173
174         /* All clones layout their PnP tables differently and some use
175            different logical devices for the MPU */
176         if(!strncmp("CTL",scc->card_id,3)) {
177                 scc->conf.io_base   = pnp_port_start(dev,0);
178                 scc->conf.irq       = pnp_irq(dev,0);
179                 scc->conf.dma       = pnp_dma(dev,0);
180                 scc->conf.dma2      = pnp_dma(dev,1);
181                 scc->mpucnf.io_base = pnp_port_start(dev,1);
182                 return;
183         }
184         if(!strncmp("tBA",scc->card_id,3)) {
185                 scc->conf.io_base   = pnp_port_start(dev,0);
186                 scc->conf.irq       = pnp_irq(dev,0);
187                 scc->conf.dma       = pnp_dma(dev,0);
188                 scc->conf.dma2      = pnp_dma(dev,1);
189                 return;
190         }
191         if(!strncmp("ESS",scc->card_id,3)) {
192                 scc->conf.io_base   = pnp_port_start(dev,0);
193                 scc->conf.irq       = pnp_irq(dev,0);
194                 scc->conf.dma       = pnp_dma(dev,0);
195                 scc->conf.dma2      = pnp_dma(dev,1);
196                 scc->mpucnf.io_base = pnp_port_start(dev,2);
197                 return;
198         }
199         if(!strncmp("CMI",scc->card_id,3)) {
200                 scc->conf.io_base = pnp_port_start(dev,0);
201                 scc->conf.irq     = pnp_irq(dev,0);
202                 scc->conf.dma     = pnp_dma(dev,0);
203                 scc->conf.dma2    = pnp_dma(dev,1);
204                 return;
205         }
206         if(!strncmp("RWB",scc->card_id,3)) {
207                 scc->conf.io_base = pnp_port_start(dev,0);
208                 scc->conf.irq     = pnp_irq(dev,0);
209                 scc->conf.dma     = pnp_dma(dev,0);
210                 return;
211         }
212         if(!strncmp("ALS",scc->card_id,3)) {
213                 if(!strncmp("ALS0007",scc->card_id,7)) {
214                         scc->conf.io_base = pnp_port_start(dev,0);
215                         scc->conf.irq     = pnp_irq(dev,0);
216                         scc->conf.dma     = pnp_dma(dev,0);
217                 } else {
218                         scc->conf.io_base = pnp_port_start(dev,0);
219                         scc->conf.irq     = pnp_irq(dev,0);
220                         scc->conf.dma     = pnp_dma(dev,1);
221                         scc->conf.dma2    = pnp_dma(dev,0);
222                 }
223                 return;
224         }
225         if(!strncmp("RTL",scc->card_id,3)) {
226                 scc->conf.io_base = pnp_port_start(dev,0);
227                 scc->conf.irq     = pnp_irq(dev,0);
228                 scc->conf.dma     = pnp_dma(dev,1);
229                 scc->conf.dma2    = pnp_dma(dev,0);
230         }
231 }
232
233 /* Probe callback function for the PnP API */
234 static int sb_pnp_probe(struct pnp_card_link *card, const struct pnp_card_device_id *card_id)
235 {
236         struct sb_card_config *scc;
237         struct sb_module_options sbmo = {0}; /* Default to 0 for PnP */
238         struct pnp_dev *dev = pnp_request_card_device(card, card_id->devs[0].id, NULL);
239         
240         if(!dev){
241                 return -EBUSY;
242         }
243
244         if((scc = kmalloc(sizeof(struct sb_card_config), GFP_KERNEL)) == NULL) {
245                 printk(KERN_ERR "sb: Error: Could not allocate memory\n");
246                 return -ENOMEM;
247         }
248         memset(scc, 0, sizeof(struct sb_card_config));
249
250         printk(KERN_INFO "sb: PnP: Found Card Named = \"%s\", Card PnP id = " \
251                "%s, Device PnP id = %s\n", card->card->name, card_id->id,
252                dev->id->id);
253
254         scc->card_id = card_id->id;
255         scc->dev_id = dev->id->id;
256         sb_dev2cfg(dev, scc);
257
258         printk(KERN_INFO "sb: PnP:      Detected at: io=0x%x, irq=%d, " \
259                "dma=%d, dma16=%d\n", scc->conf.io_base, scc->conf.irq,
260                scc->conf.dma, scc->conf.dma2);
261
262         pnp_set_card_drvdata(card, scc);
263
264         return sb_register_oss(scc, &sbmo);
265 }
266
267 static void sb_pnp_remove(struct pnp_card_link *card)
268 {
269         struct sb_card_config *scc = pnp_get_card_drvdata(card);
270
271         if(!scc)
272                 return;
273
274         printk(KERN_INFO "sb: PnP: Removing %s\n", scc->card_id);
275
276         sb_unload(scc);
277 }
278
279 static struct pnp_card_driver sb_pnp_driver = {
280         .name          = "OSS SndBlstr", /* 16 character limit */
281         .id_table      = sb_pnp_card_table,
282         .probe         = sb_pnp_probe,
283         .remove        = sb_pnp_remove,
284 };
285 #endif /* CONFIG_PNP */
286
287 static int __init sb_init(void)
288 {
289         int lres = 0;
290         int pres = 0;
291
292         printk(KERN_INFO "sb: Init: Starting Probe...\n");
293
294         if(io != -1 && irq != -1 && dma != -1) {
295                 printk(KERN_INFO "sb: Probing legacy card with io=%x, "\
296                        "irq=%d, dma=%d, dma16=%d\n",io, irq, dma, dma16);
297                 lres = sb_init_legacy();
298         } else if((io != -1 || irq != -1 || dma != -1) ||
299                   (!pnp && (io == -1 && irq == -1 && dma == -1)))
300                 printk(KERN_ERR "sb: Error: At least io, irq, and dma "\
301                        "must be set for legacy cards.\n");
302
303 #ifdef CONFIG_PNP
304         if(pnp) {
305                 pres = pnp_register_card_driver(&sb_pnp_driver);
306         }
307 #endif
308         printk(KERN_INFO "sb: Init: Done\n");
309
310         /* If either PnP or Legacy registered a card then return
311          * success */
312         return (pres > 0 || lres > 0) ? 0 : -ENODEV;
313 }
314
315 static void __exit sb_exit(void)
316 {
317         printk(KERN_INFO "sb: Unloading...\n");
318
319         /* Unload legacy card */
320         if (legacy) {
321                 printk (KERN_INFO "sb: Unloading legacy card\n");
322                 sb_unload(legacy);
323         }
324
325 #ifdef CONFIG_PNP
326         pnp_unregister_card_driver(&sb_pnp_driver);
327 #endif
328
329         if (smw_free) {
330                 vfree(smw_free);
331                 smw_free = NULL;
332         }
333 }
334
335 module_init(sb_init);
336 module_exit(sb_exit);