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