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