patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / isa / sb / sb16.c
1 /*
2  *  Driver for SoundBlaster 16/AWE32/AWE64 soundcards
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <asm/dma.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/pnp.h>
27 #include <linux/moduleparam.h>
28 #include <sound/core.h>
29 #include <sound/sb.h>
30 #include <sound/sb16_csp.h>
31 #include <sound/mpu401.h>
32 #include <sound/opl3.h>
33 #include <sound/emu8000.h>
34 #include <sound/seq_device.h>
35 #define SNDRV_LEGACY_AUTO_PROBE
36 #define SNDRV_LEGACY_FIND_FREE_IRQ
37 #define SNDRV_LEGACY_FIND_FREE_DMA
38 #include <sound/initval.h>
39
40 #define chip_t sb_t
41
42 #ifdef SNDRV_SBAWE
43 #define PFX "sbawe: "
44 #else
45 #define PFX "sb16: "
46 #endif
47
48 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
49 MODULE_LICENSE("GPL");
50 MODULE_CLASSES("{sound}");
51 #ifndef SNDRV_SBAWE
52 MODULE_DESCRIPTION("Sound Blaster 16");
53 MODULE_DEVICES("{{Creative Labs,SB 16},"
54                 "{Creative Labs,SB Vibra16S},"
55                 "{Creative Labs,SB Vibra16C},"
56                 "{Creative Labs,SB Vibra16CL},"
57                 "{Creative Labs,SB Vibra16X}}");
58 #else
59 MODULE_DESCRIPTION("Sound Blaster AWE");
60 MODULE_DEVICES("{{Creative Labs,SB AWE 32},"
61                 "{Creative Labs,SB AWE 64},"
62                 "{Creative Labs,SB AWE 64 Gold}}");
63 #endif
64
65 #if 0
66 #define SNDRV_DEBUG_IRQ
67 #endif
68
69 #if defined(SNDRV_SBAWE) && (defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE)))
70 #define SNDRV_SBAWE_EMU8000
71 #endif
72
73 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
74 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
75 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
76 #ifdef CONFIG_PNP
77 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
78 #endif
79 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x220,0x240,0x260,0x280 */
80 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x330,0x300 */
81 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
82 #ifdef SNDRV_SBAWE_EMU8000
83 static long awe_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
84 #endif
85 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 5,7,9,10 */
86 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
87 static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;      /* 5,6,7 */
88 static int mic_agc[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
89 #ifdef CONFIG_SND_SB16_CSP
90 static int csp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
91 #endif
92 #ifdef SNDRV_SBAWE_EMU8000
93 static int seq_ports[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
94 #endif
95 static int boot_devs;
96
97 module_param_array(index, int, boot_devs, 0444);
98 MODULE_PARM_DESC(index, "Index value for SoundBlaster 16 soundcard.");
99 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
100 module_param_array(id, charp, boot_devs, 0444);
101 MODULE_PARM_DESC(id, "ID string for SoundBlaster 16 soundcard.");
102 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
103 module_param_array(enable, bool, boot_devs, 0444);
104 MODULE_PARM_DESC(enable, "Enable SoundBlaster 16 soundcard.");
105 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
106 #ifdef CONFIG_PNP
107 module_param_array(isapnp, bool, boot_devs, 0444);
108 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
109 MODULE_PARM_SYNTAX(isapnp, SNDRV_ISAPNP_DESC);
110 #endif
111 module_param_array(port, long, boot_devs, 0444);
112 MODULE_PARM_DESC(port, "Port # for SB16 driver.");
113 MODULE_PARM_SYNTAX(port, SNDRV_ENABLED ",allows:{{0x220},{0x240},{0x260},{0x280}},dialog:list");
114 module_param_array(mpu_port, long, boot_devs, 0444);
115 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for SB16 driver.");
116 MODULE_PARM_SYNTAX(mpu_port, SNDRV_ENABLED ",allows:{{0x330},{0x300}},dialog:list");
117 module_param_array(fm_port, long, boot_devs, 0444);
118 MODULE_PARM_DESC(fm_port, "FM port # for SB16 PnP driver.");
119 MODULE_PARM_SYNTAX(fm_port, SNDRV_ENABLED ",allows:{{0x388},{0x38c},{0x390},{0x394}},dialog:list");
120 #ifdef SNDRV_SBAWE_EMU8000
121 module_param_array(awe_port, long, boot_devs, 0444);
122 MODULE_PARM_DESC(awe_port, "AWE port # for SB16 PnP driver.");
123 MODULE_PARM_SYNTAX(awe_port, SNDRV_ENABLED ",allows:{{0x620},{0x640},{0x660},{0x680}},dialog:list");
124 #endif
125 module_param_array(irq, int, boot_devs, 0444);
126 MODULE_PARM_DESC(irq, "IRQ # for SB16 driver.");
127 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
128 module_param_array(dma8, int, boot_devs, 0444);
129 MODULE_PARM_DESC(dma8, "8-bit DMA # for SB16 driver.");
130 MODULE_PARM_SYNTAX(dma8, SNDRV_DMA8_DESC);
131 module_param_array(dma16, int, boot_devs, 0444);
132 MODULE_PARM_DESC(dma16, "16-bit DMA # for SB16 driver.");
133 MODULE_PARM_SYNTAX(dma16, SNDRV_DMA16_DESC);
134 module_param_array(mic_agc, int, boot_devs, 0444);
135 MODULE_PARM_DESC(mic_agc, "Mic Auto-Gain-Control switch.");
136 MODULE_PARM_SYNTAX(mic_agc, SNDRV_ENABLED "," SNDRV_BOOLEAN_TRUE_DESC);
137 #ifdef CONFIG_SND_SB16_CSP
138 module_param_array(csp, int, boot_devs, 0444);
139 MODULE_PARM_DESC(csp, "ASP/CSP chip support.");
140 MODULE_PARM_SYNTAX(csp, SNDRV_ENABLED "," SNDRV_ENABLE_DESC);
141 #endif
142 #ifdef SNDRV_SBAWE_EMU8000
143 module_param_array(seq_ports, int, boot_devs, 0444);
144 MODULE_PARM_DESC(seq_ports, "Number of sequencer ports for WaveTable synth.");
145 MODULE_PARM_SYNTAX(seq_ports, SNDRV_ENABLED ",allows:{{0,8}},skill:advanced");
146 #endif
147
148 struct snd_card_sb16 {
149         struct resource *fm_res;        /* used to block FM i/o region for legacy cards */
150 #ifdef CONFIG_PNP
151         int dev_no;
152         struct pnp_dev *dev;
153 #ifdef SNDRV_SBAWE_EMU8000
154         struct pnp_dev *devwt;
155 #endif
156 #endif
157 };
158
159 static snd_card_t *snd_sb16_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
160
161 #ifdef CONFIG_PNP
162
163 static struct pnp_card_device_id snd_sb16_pnpids[] = {
164 #ifndef SNDRV_SBAWE
165         /* Sound Blaster 16 PnP */
166         { .id = "CTL0024", .devs = { { "CTL0031" } } },
167         /* Sound Blaster 16 PnP */
168         { .id = "CTL0025", .devs = { { "CTL0031" } } },
169         /* Sound Blaster 16 PnP */
170         { .id = "CTL0026", .devs = { { "CTL0031" } } },
171         /* Sound Blaster 16 PnP */
172         { .id = "CTL0027", .devs = { { "CTL0031" } } },
173         /* Sound Blaster 16 PnP */
174         { .id = "CTL0028", .devs = { { "CTL0031" } } },
175         /* Sound Blaster 16 PnP */
176         { .id = "CTL0029", .devs = { { "CTL0031" } } },
177         /* Sound Blaster 16 PnP */
178         { .id = "CTL002a", .devs = { { "CTL0031" } } },
179         /* Sound Blaster 16 PnP */
180         /* Note: This card has also a CTL0051:StereoEnhance device!!! */
181         { .id = "CTL002b", .devs = { { "CTL0031" } } },
182         /* Sound Blaster 16 PnP */
183         { .id = "CTL002c", .devs = { { "CTL0031" } } },
184         /* Sound Blaster Vibra16S */
185         { .id = "CTL0051", .devs = { { "CTL0001" } } },
186         /* Sound Blaster Vibra16C */
187         { .id = "CTL0070", .devs = { { "CTL0001" } } },
188         /* Sound Blaster Vibra16CL - added by ctm@ardi.com */
189         { .id = "CTL0080", .devs = { { "CTL0041" } } },
190         /* Sound Blaster 16 'value' PnP. It says model ct4130 on the pcb, */
191         /* but ct4131 on a sticker on the board.. */
192         { .id = "CTL0086", .devs = { { "CTL0041" } } },
193         /* Sound Blaster Vibra16X */
194         { .id = "CTL00f0", .devs = { { "CTL0043" } } },
195 #else  /* SNDRV_SBAWE defined */
196         /* Sound Blaster AWE 32 PnP */
197         { .id = "CTL0035", .devs = { { "CTL0031" }, { "CTL0021" } } },
198         /* Sound Blaster AWE 32 PnP */
199         { .id = "CTL0039", .devs = { { "CTL0031" }, { "CTL0021" } } },
200         /* Sound Blaster AWE 32 PnP */
201         { .id = "CTL0042", .devs = { { "CTL0031" }, { "CTL0021" } } },
202         /* Sound Blaster AWE 32 PnP */
203         { .id = "CTL0043", .devs = { { "CTL0031" }, { "CTL0021" } } },
204         /* Sound Blaster AWE 32 PnP */
205         /* Note: This card has also a CTL0051:StereoEnhance device!!! */
206         { .id = "CTL0044", .devs = { { "CTL0031" }, { "CTL0021" } } },
207         /* Sound Blaster AWE 32 PnP */
208         /* Note: This card has also a CTL0051:StereoEnhance device!!! */
209         { .id = "CTL0045", .devs = { { "CTL0031" }, { "CTL0021" } } },
210         /* Sound Blaster AWE 32 PnP */
211         { .id = "CTL0046", .devs = { { "CTL0031" }, { "CTL0021" } } },
212         /* Sound Blaster AWE 32 PnP */
213         { .id = "CTL0047", .devs = { { "CTL0031" }, { "CTL0021" } } },
214         /* Sound Blaster AWE 32 PnP */
215         { .id = "CTL0048", .devs = { { "CTL0031" }, { "CTL0021" } } },
216         /* Sound Blaster AWE 32 PnP */
217         { .id = "CTL0054", .devs = { { "CTL0031" }, { "CTL0021" } } },
218         /* Sound Blaster AWE 32 PnP */
219         { .id = "CTL009a", .devs = { { "CTL0041" }, { "CTL0021" } } },
220         /* Sound Blaster AWE 32 PnP */
221         { .id = "CTL009c", .devs = { { "CTL0041" }, { "CTL0021" } } },
222         /* Sound Blaster 32 PnP */
223         { .id = "CTL009f", .devs = { { "CTL0041" }, { "CTL0021" } } },
224         /* Sound Blaster AWE 64 PnP */
225         { .id = "CTL009d", .devs = { { "CTL0042" }, { "CTL0022" } } },
226         /* Sound Blaster AWE 64 PnP Gold */
227         { .id = "CTL009e", .devs = { { "CTL0044" }, { "CTL0023" } } },
228         /* Sound Blaster AWE 64 PnP Gold */
229         { .id = "CTL00b2", .devs = { { "CTL0044" }, { "CTL0023" } } },
230         /* Sound Blaster AWE 64 PnP */
231         { .id = "CTL00c1", .devs = { { "CTL0042" }, { "CTL0022" } } },
232         /* Sound Blaster AWE 64 PnP */
233         { .id = "CTL00c3", .devs = { { "CTL0045" }, { "CTL0022" } } },
234         /* Sound Blaster AWE 64 PnP */
235         { .id = "CTL00c5", .devs = { { "CTL0045" }, { "CTL0022" } } },
236         /* Sound Blaster AWE 64 PnP */
237         { .id = "CTL00c7", .devs = { { "CTL0045" }, { "CTL0022" } } },
238         /* Sound Blaster AWE 64 PnP */
239         { .id = "CTL00e4", .devs = { { "CTL0045" }, { "CTL0022" } } },
240         /* Sound Blaster AWE 64 PnP */
241         { .id = "CTL00e9", .devs = { { "CTL0045" }, { "CTL0022" } } },
242         /* Sound Blaster 16 PnP (AWE) */
243         { .id = "CTL00ed", .devs = { { "CTL0041" }, { "CTL0070" } } },
244         /* Generic entries */
245         { .id = "CTLXXXX" , .devs = { { "CTL0031" }, { "CTL0021" } } },
246         { .id = "CTLXXXX" , .devs = { { "CTL0041" }, { "CTL0021" } } },
247         { .id = "CTLXXXX" , .devs = { { "CTL0042" }, { "CTL0022" } } },
248         { .id = "CTLXXXX" , .devs = { { "CTL0044" }, { "CTL0023" } } },
249         { .id = "CTLXXXX" , .devs = { { "CTL0045" }, { "CTL0022" } } },
250 #endif /* SNDRV_SBAWE */
251         /* Sound Blaster 16 PnP (Virtual PC 2004)*/
252         { .id = "tBA03b0", .devs = { { "PNPb003" } } },
253         { .id = "", }
254 };
255
256 MODULE_DEVICE_TABLE(pnp_card, snd_sb16_pnpids);
257
258 #endif /* CONFIG_PNP */
259
260 #ifdef SNDRV_SBAWE_EMU8000
261 #define DRIVER_NAME     "snd-card-sbawe"
262 #else
263 #define DRIVER_NAME     "snd-card-sb16"
264 #endif
265
266 #ifdef CONFIG_PNP
267
268 static int __devinit snd_card_sb16_pnp(int dev, struct snd_card_sb16 *acard,
269                                        struct pnp_card_link *card,
270                                        const struct pnp_card_device_id *id)
271 {
272         struct pnp_dev *pdev;
273         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
274         int err;
275
276         if (!cfg) 
277                 return -ENOMEM; 
278         acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
279         if (acard->dev == NULL) { 
280                 kfree(cfg); 
281                 return -ENODEV; 
282         } 
283 #ifdef SNDRV_SBAWE_EMU8000
284         acard->devwt = pnp_request_card_device(card, id->devs[1].id, acard->dev);
285 #endif
286         /* Audio initialization */
287         pdev = acard->dev;
288
289         pnp_init_resource_table(cfg); 
290          
291         /* override resources */ 
292
293         if (port[dev] != SNDRV_AUTO_PORT)
294                 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
295         if (mpu_port[dev] != SNDRV_AUTO_PORT)
296                 pnp_resource_change(&cfg->port_resource[1], mpu_port[dev], 2);
297         if (fm_port[dev] != SNDRV_AUTO_PORT)
298                 pnp_resource_change(&cfg->port_resource[2], fm_port[dev], 4);
299         if (dma8[dev] != SNDRV_AUTO_DMA)
300                 pnp_resource_change(&cfg->dma_resource[0], dma8[dev], 1);
301         if (dma16[dev] != SNDRV_AUTO_DMA)
302                 pnp_resource_change(&cfg->dma_resource[1], dma16[dev], 1);
303         if (irq[dev] != SNDRV_AUTO_IRQ)
304                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
305         if (pnp_manual_config_dev(pdev, cfg, 0) < 0) 
306                 snd_printk(KERN_ERR PFX "AUDIO the requested resources are invalid, using auto config\n"); 
307         err = pnp_activate_dev(pdev); 
308         if (err < 0) { 
309                 snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n"); 
310                 kfree(cfg);
311                 return err; 
312         } 
313         port[dev] = pnp_port_start(pdev, 0);
314         mpu_port[dev] = pnp_port_start(pdev, 1);
315         fm_port[dev] = pnp_port_start(pdev, 2);
316         dma8[dev] = pnp_dma(pdev, 0);
317         dma16[dev] = pnp_dma(pdev, 1);
318         irq[dev] = pnp_irq(pdev, 0);
319         snd_printdd("pnp SB16: port=0x%lx, mpu port=0x%lx, fm port=0x%lx\n",
320                         port[dev], mpu_port[dev], fm_port[dev]);
321         snd_printdd("pnp SB16: dma1=%i, dma2=%i, irq=%i\n",
322                         dma8[dev], dma16[dev], irq[dev]);
323 #ifdef SNDRV_SBAWE_EMU8000
324         /* WaveTable initialization */
325         pdev = acard->devwt;
326         if (pdev != NULL) {
327                 pnp_init_resource_table(cfg); 
328          
329                 /* override resources */ 
330
331                 if (awe_port[dev] != SNDRV_AUTO_PORT) {
332                         pnp_resource_change(&cfg->port_resource[0], awe_port[dev], 4);
333                         pnp_resource_change(&cfg->port_resource[1], awe_port[dev] + 0x400, 4);
334                         pnp_resource_change(&cfg->port_resource[2], awe_port[dev] + 0x800, 4);
335                 }
336                 if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0) 
337                         snd_printk(KERN_ERR PFX "WaveTable the requested resources are invalid, using auto config\n"); 
338                 err = pnp_activate_dev(pdev); 
339                 if (err < 0) { 
340                         goto __wt_error; 
341                 } 
342                 awe_port[dev] = pnp_port_start(pdev, 0);
343                 snd_printdd("pnp SB16: wavetable port=0x%lx\n", pnp_port_start(pdev, 0));
344         } else {
345 __wt_error:
346                 if (pdev) {
347                         pnp_release_card_device(pdev);
348                         snd_printk(KERN_ERR PFX "WaveTable pnp configure failure\n");
349                 }
350                 acard->devwt = NULL;
351                 awe_port[dev] = -1;
352         }
353 #endif
354         kfree(cfg);
355         return 0;
356 }
357
358 #endif /* CONFIG_PNP */
359
360 static void snd_sb16_free(snd_card_t *card)
361 {
362         struct snd_card_sb16 *acard = (struct snd_card_sb16 *)card->private_data;
363         
364         if (acard == NULL)
365                 return;
366         if (acard->fm_res) {
367                 release_resource(acard->fm_res);
368                 kfree_nocheck(acard->fm_res);
369         }
370 }
371
372 static int __init snd_sb16_probe(int dev,
373                                  struct pnp_card_link *pcard,
374                                  const struct pnp_card_device_id *pid)
375 {
376         static int possible_irqs[] = {5, 9, 10, 7, -1};
377         static int possible_dmas8[] = {1, 3, 0, -1};
378         static int possible_dmas16[] = {5, 6, 7, -1};
379         int xirq, xdma8, xdma16;
380         sb_t *chip;
381         snd_card_t *card;
382         struct snd_card_sb16 *acard;
383         opl3_t *opl3;
384         snd_hwdep_t *synth = NULL;
385 #ifdef CONFIG_SND_SB16_CSP
386         snd_hwdep_t *xcsp = NULL;
387 #endif
388         unsigned long flags;
389         int err;
390
391         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
392                             sizeof(struct snd_card_sb16));
393         if (card == NULL)
394                 return -ENOMEM;
395         acard = (struct snd_card_sb16 *) card->private_data;
396         card->private_free = snd_sb16_free;
397 #ifdef CONFIG_PNP
398         if (isapnp[dev]) {
399                 if ((err = snd_card_sb16_pnp(dev, acard, pcard, pid))) {
400                         snd_card_free(card);
401                         return err;
402                 }
403                 snd_card_set_dev(card, &pcard->card->dev);
404         }
405 #endif
406
407         xirq = irq[dev];
408         xdma8 = dma8[dev];
409         xdma16 = dma16[dev];
410 #ifdef CONFIG_PNP
411         if (!isapnp[dev]) {
412 #endif
413         if (xirq == SNDRV_AUTO_IRQ) {
414                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
415                         snd_card_free(card);
416                         snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
417                         return -EBUSY;
418                 }
419         }
420         if (xdma8 == SNDRV_AUTO_DMA) {
421                 if ((xdma8 = snd_legacy_find_free_dma(possible_dmas8)) < 0) {
422                         snd_card_free(card);
423                         snd_printk(KERN_ERR PFX "unable to find a free 8-bit DMA\n");
424                         return -EBUSY;
425                 }
426         }
427         if (xdma16 == SNDRV_AUTO_DMA) {
428                 if ((xdma16 = snd_legacy_find_free_dma(possible_dmas16)) < 0) {
429                         snd_card_free(card);
430                         snd_printk(KERN_ERR PFX "unable to find a free 16-bit DMA\n");
431                         return -EBUSY;
432                 }
433         }
434         /* non-PnP FM port address is hardwired with base port address */
435         fm_port[dev] = port[dev];
436         /* block the 0x388 port to avoid PnP conflicts */
437         acard->fm_res = request_region(0x388, 4, "SoundBlaster FM");
438 #ifdef SNDRV_SBAWE_EMU8000
439         /* non-PnP AWE port address is hardwired with base port address */
440         awe_port[dev] = port[dev] + 0x400;
441 #endif
442 #ifdef CONFIG_PNP
443         }
444 #endif
445
446         if ((err = snd_sbdsp_create(card,
447                                     port[dev],
448                                     xirq,
449                                     snd_sb16dsp_interrupt,
450                                     xdma8,
451                                     xdma16,
452                                     SB_HW_AUTO,
453                                     &chip)) < 0) {
454                 snd_card_free(card);
455                 return err;
456         }
457         if (chip->hardware != SB_HW_16) {
458                 snd_card_free(card);
459                 snd_printdd("SB 16 chip was not detected at 0x%lx\n", port[dev]);
460                 return -ENODEV;
461         }
462         chip->mpu_port = mpu_port[dev];
463 #ifdef CONFIG_PNP
464         if (!isapnp[dev] && (err = snd_sb16dsp_configure(chip)) < 0) {
465 #else
466         if ((err = snd_sb16dsp_configure(chip)) < 0) {
467 #endif
468                 snd_card_free(card);
469                 return -ENXIO;
470         }
471         if ((err = snd_sb16dsp_pcm(chip, 0, NULL)) < 0) {
472                 snd_card_free(card);
473                 return -ENXIO;
474         }
475
476         strcpy(card->driver,
477 #ifdef SNDRV_SBAWE_EMU8000
478                         awe_port[dev] > 0 ? "SB AWE" :
479 #endif
480                         "SB16");
481         strcpy(card->shortname, chip->name);
482         sprintf(card->longname, "%s at 0x%lx, irq %i, dma ",
483                 chip->name,
484                 chip->port,
485                 xirq);
486         if (xdma8 >= 0)
487                 sprintf(card->longname + strlen(card->longname), "%d", xdma8);
488         if (xdma16 >= 0)
489                 sprintf(card->longname + strlen(card->longname), "%s%d",
490                         xdma8 >= 0 ? "&" : "", xdma16);
491
492         if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
493                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_SB,
494                                                chip->mpu_port, 0,
495                                                xirq, 0, &chip->rmidi)) < 0) {
496                         snd_card_free(card);
497                         return -ENXIO;
498                 }
499                 chip->rmidi_callback = snd_mpu401_uart_interrupt;
500         }
501
502 #ifdef SNDRV_SBAWE_EMU8000
503         if (awe_port[dev] == SNDRV_AUTO_PORT)
504                 awe_port[dev] = 0; /* disable */
505 #endif
506
507         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
508                 if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
509                                     OPL3_HW_OPL3,
510                                     acard->fm_res != NULL || fm_port[dev] == port[dev],
511                                     &opl3) < 0) {
512                         snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n",
513                                    fm_port[dev], fm_port[dev] + 2);
514                 } else {
515 #ifdef SNDRV_SBAWE_EMU8000
516                         int seqdev = awe_port[dev] > 0 ? 2 : 1;
517 #else
518                         int seqdev = 1;
519 #endif
520                         if ((err = snd_opl3_hwdep_new(opl3, 0, seqdev, &synth)) < 0) {
521                                 snd_card_free(card);
522                                 return -ENXIO;
523                         }
524                 }
525         }
526
527         if ((err = snd_sbmixer_new(chip)) < 0) {
528                 snd_card_free(card);
529                 return -ENXIO;
530         }
531
532 #ifdef CONFIG_SND_SB16_CSP
533         /* CSP chip on SB16ASP/AWE32 */
534         if ((chip->hardware == SB_HW_16) && csp[dev]) {
535                 snd_sb_csp_new(chip, synth != NULL ? 1 : 0, &xcsp);
536                 if (xcsp) {
537                         chip->csp = xcsp->private_data;
538                         chip->hardware = SB_HW_16CSP;
539                 } else {
540                         snd_printk(KERN_INFO PFX "warning - CSP chip not detected on soundcard #%i\n", dev + 1);
541                 }
542         }
543 #endif
544 #ifdef SNDRV_SBAWE_EMU8000
545         if (awe_port[dev] > 0) {
546                 if (snd_emu8000_new(card, 1, awe_port[dev],
547                                     seq_ports[dev], NULL) < 0) {
548                         snd_printk(KERN_ERR PFX "fatal error - EMU-8000 synthesizer not detected at 0x%lx\n", awe_port[dev]);
549                         snd_card_free(card);
550                         return -ENXIO;
551                 }
552         }
553 #endif
554
555         /* setup Mic AGC */
556         spin_lock_irqsave(&chip->mixer_lock, flags);
557         snd_sbmixer_write(chip, SB_DSP4_MIC_AGC,
558                 (snd_sbmixer_read(chip, SB_DSP4_MIC_AGC) & 0x01) |
559                 (mic_agc[dev] ? 0x00 : 0x01));
560         spin_unlock_irqrestore(&chip->mixer_lock, flags);
561
562         if ((err = snd_card_register(card)) < 0) {
563                 snd_card_free(card);
564                 return err;
565         }
566         if (pcard)
567                 pnp_set_card_drvdata(pcard, card);
568         else
569                 snd_sb16_legacy[dev] = card;
570         return 0;
571 }
572
573 static int __init snd_sb16_probe_legacy_port(unsigned long xport)
574 {
575         static int dev;
576         int res;
577
578         for ( ; dev < SNDRV_CARDS; dev++) {
579                 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
580                         continue;
581 #ifdef CONFIG_PNP
582                 if (isapnp[dev])
583                         continue;
584 #endif
585                 port[dev] = xport;
586                 res = snd_sb16_probe(dev, NULL, NULL);
587                 if (res < 0)
588                         port[dev] = SNDRV_AUTO_PORT;
589                 return res;
590         }
591         return -ENODEV;
592 }
593
594 #ifdef CONFIG_PNP
595
596 static int __devinit snd_sb16_pnp_detect(struct pnp_card_link *card,
597                                          const struct pnp_card_device_id *id)
598 {
599         static int dev;
600         int res;
601
602         for ( ; dev < SNDRV_CARDS; dev++) {
603                 if (!enable[dev] || !isapnp[dev])
604                         continue;
605                 res = snd_sb16_probe(dev, card, id);
606                 if (res < 0)
607                         return res;
608                 dev++;
609                 return 0;
610         }
611
612         return -ENODEV;
613 }
614
615 static void __devexit snd_sb16_pnp_remove(struct pnp_card_link * pcard)
616 {
617         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
618
619         snd_card_disconnect(card);
620         snd_card_free_in_thread(card);
621 }
622
623 static struct pnp_card_driver sb16_pnpc_driver = {
624         .flags = PNP_DRIVER_RES_DISABLE,
625         .name = "sb16",
626         .id_table = snd_sb16_pnpids,
627         .probe = snd_sb16_pnp_detect,
628         .remove = __devexit_p(snd_sb16_pnp_remove),
629 };
630
631 #endif /* CONFIG_PNP */
632
633 static int __init alsa_card_sb16_init(void)
634 {
635         int dev, cards = 0, i;
636         static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280, -1};
637
638         /* legacy non-auto cards at first */
639         for (dev = 0; dev < SNDRV_CARDS; dev++) {
640                 if (!enable[dev] || port[dev] == SNDRV_AUTO_PORT)
641                         continue;
642 #ifdef CONFIG_PNP
643                 if (isapnp[dev])
644                         continue;
645 #endif
646                 if (!snd_sb16_probe(dev, NULL, NULL)) {
647                         cards++;
648                         continue;
649                 }
650 #ifdef MODULE
651                 snd_printk(KERN_ERR "Sound Blaster 16+ soundcard #%i not found at 0x%lx or device busy\n", dev, port[dev]);
652 #endif
653         }
654         /* legacy auto configured cards */
655         i = snd_legacy_auto_probe(possible_ports, snd_sb16_probe_legacy_port);
656         if (i > 0)
657                 cards += i;
658
659 #ifdef CONFIG_PNP
660         /* PnP cards at last */
661         i = pnp_register_card_driver(&sb16_pnpc_driver);
662         if (i >0)
663                 cards += i;
664 #endif
665
666         if (!cards) {
667 #ifdef CONFIG_PNP
668                 pnp_unregister_card_driver(&sb16_pnpc_driver);
669 #endif
670 #ifdef MODULE
671                 snd_printk(KERN_ERR "Sound Blaster 16 soundcard not found or device busy\n");
672 #ifdef SNDRV_SBAWE_EMU8000
673                 snd_printk(KERN_ERR "In case, if you have non-AWE card, try snd-sb16 module\n");
674 #else
675                 snd_printk(KERN_ERR "In case, if you have AWE card, try snd-sbawe module\n");
676 #endif
677 #endif
678                 return -ENODEV;
679         }
680         return 0;
681 }
682
683 static void __exit alsa_card_sb16_exit(void)
684 {
685         int dev;
686
687 #ifdef CONFIG_PNP
688         /* PnP cards first */
689         pnp_unregister_card_driver(&sb16_pnpc_driver);
690 #endif
691         for (dev = 0; dev < SNDRV_CARDS; dev++)
692                 snd_card_free(snd_sb16_legacy[dev]);
693 }
694
695 module_init(alsa_card_sb16_init)
696 module_exit(alsa_card_sb16_exit)