Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[pandora-kernel.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 #include "sound_config.h"
30 #include "sb_mixer.h"
31 #include "sb.h"
32 #ifdef CONFIG_PNP
33 #include <linux/pnp.h>
34 #endif /* CONFIG_PNP */
35 #include "sb_card.h"
36
37 MODULE_DESCRIPTION("OSS Soundblaster ISA PnP and legacy sound driver");
38 MODULE_LICENSE("GPL");
39
40 extern void *smw_free;
41
42 static int __initdata mpu_io    = 0;
43 static int __initdata io        = -1;
44 static int __initdata irq       = -1;
45 static int __initdata dma       = -1;
46 static int __initdata dma16     = -1;
47 static int __initdata type      = 0; /* Can set this to a specific card type */
48 static int __initdata esstype   = 0; /* ESS chip type */
49 static int __initdata acer      = 0; /* Do acer notebook init? */
50 static int __initdata sm_games  = 0; /* Logitech soundman games? */
51
52 static struct sb_card_config *legacy = NULL;
53
54 #ifdef CONFIG_PNP
55 static int pnp_registered;
56 static int __initdata pnp       = 1;
57 /*
58 static int __initdata uart401   = 0;
59 */
60 #else
61 static int __initdata pnp       = 0;
62 #endif
63
64 module_param(io, int, 000);
65 MODULE_PARM_DESC(io,       "Soundblaster i/o base address (0x220,0x240,0x260,0x280)");
66 module_param(irq, int, 000);
67 MODULE_PARM_DESC(irq,      "IRQ (5,7,9,10)");
68 module_param(dma, int, 000);
69 MODULE_PARM_DESC(dma,      "8-bit DMA channel (0,1,3)");
70 module_param(dma16, int, 000);
71 MODULE_PARM_DESC(dma16,    "16-bit DMA channel (5,6,7)");
72 module_param(mpu_io, int, 000);
73 MODULE_PARM_DESC(mpu_io,   "MPU base address");
74 module_param(type, int, 000);
75 MODULE_PARM_DESC(type,     "You can set this to specific card type (doesn't " \
76                  "work with pnp)");
77 module_param(sm_games, int, 000);
78 MODULE_PARM_DESC(sm_games, "Enable support for Logitech soundman games " \
79                  "(doesn't work with pnp)");
80 module_param(esstype, int, 000);
81 MODULE_PARM_DESC(esstype,  "ESS chip type (doesn't work with pnp)");
82 module_param(acer, int, 000);
83 MODULE_PARM_DESC(acer,     "Set this to detect cards in some ACER notebooks "\
84                  "(doesn't work with pnp)");
85
86 #ifdef CONFIG_PNP
87 module_param(pnp, int, 000);
88 MODULE_PARM_DESC(pnp,     "Went set to 0 will disable detection using PnP. "\
89                   "Default is 1.\n");
90 /* Not done yet.... */
91 /*
92 module_param(uart401, int, 000);
93 MODULE_PARM_DESC(uart401,  "When set to 1, will attempt to detect and enable"\
94                  "the mpu on some clones");
95 */
96 #endif /* CONFIG_PNP */
97
98 /* OSS subsystem card registration shared by PnP and legacy routines */
99 static int sb_register_oss(struct sb_card_config *scc, struct sb_module_options *sbmo)
100 {
101         if (!request_region(scc->conf.io_base, 16, "soundblaster")) {
102                 printk(KERN_ERR "sb: ports busy.\n");
103                 kfree(scc);
104                 return -EBUSY;
105         }
106
107         if (!sb_dsp_detect(&scc->conf, 0, 0, sbmo)) {
108                 release_region(scc->conf.io_base, 16);
109                 printk(KERN_ERR "sb: Failed DSP Detect.\n");
110                 kfree(scc);
111                 return -ENODEV;
112         }
113         if(!sb_dsp_init(&scc->conf, THIS_MODULE)) {
114                 printk(KERN_ERR "sb: Failed DSP init.\n");
115                 kfree(scc);
116                 return -ENODEV;
117         }
118         if(scc->mpucnf.io_base > 0) {
119                 scc->mpu = 1;
120                 printk(KERN_INFO "sb: Turning on MPU\n");
121                 if(!probe_sbmpu(&scc->mpucnf, THIS_MODULE))
122                         scc->mpu = 0;
123         }
124
125         return 1;
126 }
127
128 static void sb_unload(struct sb_card_config *scc)
129 {
130         sb_dsp_unload(&scc->conf, 0);
131         if(scc->mpu)
132                 unload_sbmpu(&scc->mpucnf);
133         kfree(scc);
134 }
135
136 /* Register legacy card with OSS subsystem */
137 static int __init sb_init_legacy(void)
138 {
139         struct sb_module_options sbmo = {0};
140
141         if((legacy = kmalloc(sizeof(struct sb_card_config), GFP_KERNEL)) == NULL) {
142                 printk(KERN_ERR "sb: Error: Could not allocate memory\n");
143                 return -ENOMEM;
144         }
145         memset(legacy, 0, sizeof(struct sb_card_config));
146
147         legacy->conf.io_base      = io;
148         legacy->conf.irq          = irq;
149         legacy->conf.dma          = dma;
150         legacy->conf.dma2         = dma16;
151         legacy->conf.card_subtype = type;
152
153         legacy->mpucnf.io_base = mpu_io;
154         legacy->mpucnf.irq     = -1;
155         legacy->mpucnf.dma     = -1;
156         legacy->mpucnf.dma2    = -1;
157
158         sbmo.esstype  = esstype;
159         sbmo.sm_games = sm_games;
160         sbmo.acer     = acer;
161
162         return sb_register_oss(legacy, &sbmo);
163 }
164
165 #ifdef CONFIG_PNP
166
167 /* Populate the OSS subsystem structures with information from PnP */
168 static void sb_dev2cfg(struct pnp_dev *dev, struct sb_card_config *scc)
169 {
170         scc->conf.io_base   = -1;
171         scc->conf.irq       = -1;
172         scc->conf.dma       = -1;
173         scc->conf.dma2      = -1;
174         scc->mpucnf.io_base = -1;
175         scc->mpucnf.irq     = -1;
176         scc->mpucnf.dma     = -1;
177         scc->mpucnf.dma2    = -1;
178
179         /* All clones layout their PnP tables differently and some use
180            different logical devices for the MPU */
181         if(!strncmp("CTL",scc->card_id,3)) {
182                 scc->conf.io_base   = pnp_port_start(dev,0);
183                 scc->conf.irq       = pnp_irq(dev,0);
184                 scc->conf.dma       = pnp_dma(dev,0);
185                 scc->conf.dma2      = pnp_dma(dev,1);
186                 scc->mpucnf.io_base = pnp_port_start(dev,1);
187                 return;
188         }
189         if(!strncmp("tBA",scc->card_id,3)) {
190                 scc->conf.io_base   = pnp_port_start(dev,0);
191                 scc->conf.irq       = pnp_irq(dev,0);
192                 scc->conf.dma       = pnp_dma(dev,0);
193                 scc->conf.dma2      = pnp_dma(dev,1);
194                 return;
195         }
196         if(!strncmp("ESS",scc->card_id,3)) {
197                 scc->conf.io_base   = pnp_port_start(dev,0);
198                 scc->conf.irq       = pnp_irq(dev,0);
199                 scc->conf.dma       = pnp_dma(dev,0);
200                 scc->conf.dma2      = pnp_dma(dev,1);
201                 scc->mpucnf.io_base = pnp_port_start(dev,2);
202                 return;
203         }
204         if(!strncmp("CMI",scc->card_id,3)) {
205                 scc->conf.io_base = pnp_port_start(dev,0);
206                 scc->conf.irq     = pnp_irq(dev,0);
207                 scc->conf.dma     = pnp_dma(dev,0);
208                 scc->conf.dma2    = pnp_dma(dev,1);
209                 return;
210         }
211         if(!strncmp("RWB",scc->card_id,3)) {
212                 scc->conf.io_base = pnp_port_start(dev,0);
213                 scc->conf.irq     = pnp_irq(dev,0);
214                 scc->conf.dma     = pnp_dma(dev,0);
215                 return;
216         }
217         if(!strncmp("ALS",scc->card_id,3)) {
218                 if(!strncmp("ALS0007",scc->card_id,7)) {
219                         scc->conf.io_base = pnp_port_start(dev,0);
220                         scc->conf.irq     = pnp_irq(dev,0);
221                         scc->conf.dma     = pnp_dma(dev,0);
222                 } else {
223                         scc->conf.io_base = pnp_port_start(dev,0);
224                         scc->conf.irq     = pnp_irq(dev,0);
225                         scc->conf.dma     = pnp_dma(dev,1);
226                         scc->conf.dma2    = pnp_dma(dev,0);
227                 }
228                 return;
229         }
230         if(!strncmp("RTL",scc->card_id,3)) {
231                 scc->conf.io_base = pnp_port_start(dev,0);
232                 scc->conf.irq     = pnp_irq(dev,0);
233                 scc->conf.dma     = pnp_dma(dev,1);
234                 scc->conf.dma2    = pnp_dma(dev,0);
235         }
236 }
237
238 static unsigned int sb_pnp_devices;
239
240 /* Probe callback function for the PnP API */
241 static int sb_pnp_probe(struct pnp_card_link *card, const struct pnp_card_device_id *card_id)
242 {
243         struct sb_card_config *scc;
244         struct sb_module_options sbmo = {0}; /* Default to 0 for PnP */
245         struct pnp_dev *dev = pnp_request_card_device(card, card_id->devs[0].id, NULL);
246         
247         if(!dev){
248                 return -EBUSY;
249         }
250
251         if((scc = kmalloc(sizeof(struct sb_card_config), GFP_KERNEL)) == NULL) {
252                 printk(KERN_ERR "sb: Error: Could not allocate memory\n");
253                 return -ENOMEM;
254         }
255         memset(scc, 0, sizeof(struct sb_card_config));
256
257         printk(KERN_INFO "sb: PnP: Found Card Named = \"%s\", Card PnP id = " \
258                "%s, Device PnP id = %s\n", card->card->name, card_id->id,
259                dev->id->id);
260
261         scc->card_id = card_id->id;
262         scc->dev_id = dev->id->id;
263         sb_dev2cfg(dev, scc);
264
265         printk(KERN_INFO "sb: PnP:      Detected at: io=0x%x, irq=%d, " \
266                "dma=%d, dma16=%d\n", scc->conf.io_base, scc->conf.irq,
267                scc->conf.dma, scc->conf.dma2);
268
269         pnp_set_card_drvdata(card, scc);
270         sb_pnp_devices++;
271
272         return sb_register_oss(scc, &sbmo);
273 }
274
275 static void sb_pnp_remove(struct pnp_card_link *card)
276 {
277         struct sb_card_config *scc = pnp_get_card_drvdata(card);
278
279         if(!scc)
280                 return;
281
282         printk(KERN_INFO "sb: PnP: Removing %s\n", scc->card_id);
283
284         sb_unload(scc);
285 }
286
287 static struct pnp_card_driver sb_pnp_driver = {
288         .name          = "OSS SndBlstr", /* 16 character limit */
289         .id_table      = sb_pnp_card_table,
290         .probe         = sb_pnp_probe,
291         .remove        = sb_pnp_remove,
292 };
293 MODULE_DEVICE_TABLE(pnp_card, sb_pnp_card_table);
294 #endif /* CONFIG_PNP */
295
296 static void __init_or_module sb_unregister_all(void)
297 {
298 #ifdef CONFIG_PNP
299         if (pnp_registered)
300                 pnp_unregister_card_driver(&sb_pnp_driver);
301 #endif
302 }
303
304 static int __init sb_init(void)
305 {
306         int lres = 0;
307         int pres = 0;
308
309         printk(KERN_INFO "sb: Init: Starting Probe...\n");
310
311         if(io != -1 && irq != -1 && dma != -1) {
312                 printk(KERN_INFO "sb: Probing legacy card with io=%x, "\
313                        "irq=%d, dma=%d, dma16=%d\n",io, irq, dma, dma16);
314                 lres = sb_init_legacy();
315         } else if((io != -1 || irq != -1 || dma != -1) ||
316                   (!pnp && (io == -1 && irq == -1 && dma == -1)))
317                 printk(KERN_ERR "sb: Error: At least io, irq, and dma "\
318                        "must be set for legacy cards.\n");
319
320 #ifdef CONFIG_PNP
321         if(pnp) {
322                 int err = pnp_register_card_driver(&sb_pnp_driver);
323                 if (!err)
324                         pnp_registered = 1;
325                 pres = sb_pnp_devices;
326         }
327 #endif
328         printk(KERN_INFO "sb: Init: Done\n");
329
330         /* If either PnP or Legacy registered a card then return
331          * success */
332         if (pres == 0 && lres <= 0) {
333                 sb_unregister_all();
334                 return -ENODEV;
335         }
336         return 0;
337 }
338
339 static void __exit sb_exit(void)
340 {
341         printk(KERN_INFO "sb: Unloading...\n");
342
343         /* Unload legacy card */
344         if (legacy) {
345                 printk (KERN_INFO "sb: Unloading legacy card\n");
346                 sb_unload(legacy);
347         }
348
349         sb_unregister_all();
350
351         vfree(smw_free);
352         smw_free = NULL;
353 }
354
355 module_init(sb_init);
356 module_exit(sb_exit);