ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / wavefront / wavefront.c
1 /*
2  *  ALSA card-level driver for Turtle Beach Wavefront cards 
3  *                                              (Maui,Tropez,Tropez+)
4  *
5  *  Copyright (c) 1997-1999 by Paul Barton-Davis <pbd@op.net>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/pnp.h>
27 #include <sound/core.h>
28 #define SNDRV_GET_ID
29 #include <sound/initval.h>
30 #include <sound/opl3.h>
31 #include <sound/snd_wavefront.h>
32
33 #define chip_t cs4231_t
34
35 MODULE_AUTHOR("Paul Barton-Davis <pbd@op.net>");
36 MODULE_DESCRIPTION("Turtle Beach Wavefront");
37 MODULE_LICENSE("GPL");
38 MODULE_CLASSES("{sound}");
39 MODULE_DEVICES("{{Turtle Beach,Maui/Tropez/Tropez+}}");
40
41 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;          /* Index 0-MAX */
42 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;           /* ID for this card */
43 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;      /* Enable this card */
44 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
45 static long cs4232_pcm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
46 static int cs4232_pcm_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,11,12,15 */
47 static long cs4232_mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
48 static int cs4232_mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 9,11,12,15 */
49 static long ics2115_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
50 static int ics2115_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 2,9,11,12,15 */
51 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;      /* PnP setup */
52 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;           /* 0,1,3,5,6,7 */
53 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;           /* 0,1,3,5,6,7 */
54 static int use_cs4232_midi[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; 
55
56 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
57 MODULE_PARM_DESC(index, "Index value for WaveFront soundcard.");
58 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
59 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
60 MODULE_PARM_DESC(id, "ID string for WaveFront soundcard.");
61 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
62 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
63 MODULE_PARM_DESC(enable, "Enable WaveFront soundcard.");
64 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
65 #ifdef CONFIG_PNP
66 MODULE_PARM(isapnp, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
67 MODULE_PARM_DESC(isapnp, "ISA PnP detection for WaveFront soundcards.");
68 MODULE_PARM_SYNTAX(isapnp, SNDRV_ISAPNP_DESC);
69 #endif
70 MODULE_PARM(cs4232_pcm_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
71 MODULE_PARM_DESC(cs4232_pcm_port, "Port # for CS4232 PCM interface.");
72 MODULE_PARM_SYNTAX(cs4232_pcm_port, SNDRV_PORT12_DESC);
73 MODULE_PARM(cs4232_pcm_irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
74 MODULE_PARM_DESC(cs4232_pcm_irq, "IRQ # for CS4232 PCM interface.");
75 MODULE_PARM_SYNTAX(cs4232_pcm_irq, SNDRV_ENABLED ",allows:{{5},{7},{9},{11},{12},{15}},dialog:list");
76 MODULE_PARM(dma1, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
77 MODULE_PARM_DESC(dma1, "DMA1 # for CS4232 PCM interface.");
78 MODULE_PARM_SYNTAX(dma1, SNDRV_DMA_DESC);
79 MODULE_PARM(dma2, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
80 MODULE_PARM_DESC(dma2, "DMA2 # for CS4232 PCM interface.");
81 MODULE_PARM_SYNTAX(dma2, SNDRV_DMA_DESC);
82 MODULE_PARM(cs4232_mpu_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
83 MODULE_PARM_DESC(cs4232_mpu_port, "port # for CS4232 MPU-401 interface.");
84 MODULE_PARM_SYNTAX(cs4232_mpu_port, SNDRV_PORT12_DESC);
85 MODULE_PARM(cs4232_mpu_irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
86 MODULE_PARM_DESC(cs4232_mpu_irq, "IRQ # for CS4232 MPU-401 interface.");
87 MODULE_PARM_SYNTAX(cs4232_mpu_irq, SNDRV_ENABLED ",allows:{{9},{11},{12},{15}},dialog:list");
88 MODULE_PARM(ics2115_irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
89 MODULE_PARM_DESC(ics2115_irq, "IRQ # for ICS2115.");
90 MODULE_PARM_SYNTAX(ics2115_irq, SNDRV_ENABLED ",allows:{{9},{11},{12},{15}},dialog:list");
91 MODULE_PARM(ics2115_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
92 MODULE_PARM_DESC(ics2115_port, "Port # for ICS2115.");
93 MODULE_PARM_SYNTAX(ics2115_port, SNDRV_PORT12_DESC);
94 MODULE_PARM(fm_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
95 MODULE_PARM_DESC(fm_port, "FM port #.");
96 MODULE_PARM_SYNTAX(fm_port, SNDRV_PORT12_DESC);
97 MODULE_PARM(use_cs4232_midi, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
98 MODULE_PARM_DESC(use_cs4232_midi, "Use CS4232 MPU-401 interface (inaccessibly located inside your computer)");
99 MODULE_PARM_SYNTAX(use_cs4232_midi, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
100
101 static snd_card_t *snd_wavefront_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
102
103 #ifdef CONFIG_PNP
104
105 static struct pnp_card_device_id snd_wavefront_pnpids[] = {
106         /* Tropez */
107         { .id = "CSC7532", .devs = { { "CSC0000" }, { "CSC0010" }, { "PnPb006" }, { "CSC0004" } } },
108         /* Tropez+ */
109         { .id = "CSC7632", .devs = { { "CSC0000" }, { "CSC0010" }, { "PnPb006" }, { "CSC0004" } } },
110         { .id = "" }
111 };
112
113 MODULE_DEVICE_TABLE(pnp_card, snd_wavefront_pnpids);
114
115 static int __devinit
116 snd_wavefront_pnp (int dev, snd_wavefront_card_t *acard, struct pnp_card_link *card,
117                    const struct pnp_card_device_id *id)
118 {
119         struct pnp_dev *pdev;
120         struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
121         int err;
122
123         if (!cfg)
124                 return -ENOMEM;
125
126         /* Check for each logical device. */
127
128         /* CS4232 chip (aka "windows sound system") is logical device 0 */
129
130         acard->wss = pnp_request_card_device(card, id->devs[0].id, NULL);
131         if (acard->wss == NULL) {
132                 kfree(cfg);
133                 return -EBUSY;
134         }
135
136         /* there is a game port at logical device 1, but we ignore it completely */
137
138         /* the control interface is logical device 2, but we ignore it
139            completely. in fact, nobody even seems to know what it
140            does.
141         */
142
143         /* Only configure the CS4232 MIDI interface if its been
144            specifically requested. It is logical device 3.
145         */
146
147         if (use_cs4232_midi[dev]) {
148                 acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL);
149                 if (acard->mpu == NULL) {
150                         kfree(cfg);
151                         return -EBUSY;
152                 }
153         }
154
155         /* The ICS2115 synth is logical device 4 */
156
157         acard->synth = pnp_request_card_device(card, id->devs[3].id, NULL);
158         if (acard->synth == NULL) {
159                 kfree(cfg);
160                 return -EBUSY;
161         }
162
163         /* PCM/FM initialization */
164
165         pdev = acard->wss;
166
167         pnp_init_resource_table(cfg);
168
169         /* An interesting note from the Tropez+ FAQ:
170
171            Q. [Ports] Why is the base address of the WSS I/O ports off by 4?
172
173            A. WSS I/O requires a block of 8 I/O addresses ("ports"). Of these, the first
174            4 are used to identify and configure the board. With the advent of PnP,
175            these first 4 addresses have become obsolete, and software applications
176            only use the last 4 addresses to control the codec chip. Therefore, the
177            base address setting "skips past" the 4 unused addresses.
178
179         */
180
181         if (cs4232_pcm_port[dev] != SNDRV_AUTO_PORT)
182                 pnp_resource_change(&cfg->port_resource[0], cs4232_pcm_port[dev], 4);
183         if (fm_port[dev] != SNDRV_AUTO_PORT)
184                 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
185         if (dma1[dev] != SNDRV_AUTO_DMA)
186                 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
187         if (dma2[dev] != SNDRV_AUTO_DMA)
188                 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
189         if (cs4232_pcm_irq[dev] != SNDRV_AUTO_IRQ)
190                 pnp_resource_change(&cfg->irq_resource[0], cs4232_pcm_irq[dev], 1);
191
192         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
193                 snd_printk(KERN_ERR "PnP WSS the requested resources are invalid, using auto config\n");
194         err = pnp_activate_dev(pdev);
195         if (err < 0) {
196                 snd_printk(KERN_ERR "PnP WSS pnp configure failure\n");
197                 kfree(cfg);
198                 return err;
199         }
200
201         cs4232_pcm_port[dev] = pnp_port_start(pdev, 0);
202         fm_port[dev] = pnp_port_start(pdev, 1);
203         dma1[dev] = pnp_dma(pdev, 0);
204         dma2[dev] = pnp_dma(pdev, 1);
205         cs4232_pcm_irq[dev] = pnp_irq(pdev, 0);
206
207         /* Synth initialization */
208
209         pdev = acard->synth;
210         
211         pnp_init_resource_table(cfg);
212
213         if (ics2115_port[dev] != SNDRV_AUTO_PORT) {
214                 pnp_resource_change(&cfg->port_resource[0], ics2115_port[dev], 16);
215         }
216                 
217         if (ics2115_port[dev] != SNDRV_AUTO_IRQ) {
218                 pnp_resource_change(&cfg->irq_resource[0], ics2115_irq[dev], 1);
219         }
220
221         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
222                 snd_printk(KERN_ERR "PnP ICS2115 the requested resources are invalid, using auto config\n");
223         err = pnp_activate_dev(pdev);
224         if (err < 0) {
225                 snd_printk(KERN_ERR "PnP ICS2115 pnp configure failure\n");
226                 kfree(cfg);
227                 return err;
228         }
229
230         ics2115_port[dev] = pnp_port_start(pdev, 0);
231         ics2115_irq[dev] = pnp_irq(pdev, 0);
232
233         /* CS4232 MPU initialization. Configure this only if
234            explicitly requested, since its physically inaccessible and
235            consumes another IRQ.
236         */
237
238         if (use_cs4232_midi[dev]) {
239
240                 pdev = acard->mpu;
241
242                 pnp_init_resource_table(cfg);
243
244                 if (cs4232_mpu_port[dev] != SNDRV_AUTO_PORT)
245                         pnp_resource_change(&cfg->port_resource[0], cs4232_mpu_port[dev], 2);
246                 if (cs4232_mpu_irq[dev] != SNDRV_AUTO_IRQ)
247                         pnp_resource_change(&cfg->port_resource[0], cs4232_mpu_irq[dev], 1);
248
249                 if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
250                         snd_printk(KERN_ERR "PnP MPU401 the requested resources are invalid, using auto config\n");
251                 err = pnp_activate_dev(pdev);
252                 if (err < 0) {
253                         snd_printk(KERN_ERR "PnP MPU401 pnp configure failure\n");
254                         cs4232_mpu_port[dev] = SNDRV_AUTO_PORT;
255                 } else {
256                         cs4232_mpu_port[dev] = pnp_port_start(pdev, 0);
257                         cs4232_mpu_irq[dev] = pnp_irq(pdev, 0);
258                 }
259
260                 snd_printk ("CS4232 MPU: port=0x%lx, irq=%i\n", 
261                             cs4232_mpu_port[dev], 
262                             cs4232_mpu_irq[dev]);
263         }
264
265         snd_printdd ("CS4232: pcm port=0x%lx, fm port=0x%lx, dma1=%i, dma2=%i, irq=%i\nICS2115: port=0x%lx, irq=%i\n", 
266                     cs4232_pcm_port[dev], 
267                     fm_port[dev],
268                     dma1[dev], 
269                     dma2[dev], 
270                     cs4232_pcm_irq[dev],
271                     ics2115_port[dev], 
272                     ics2115_irq[dev]);
273         
274         kfree(cfg);
275         return 0;
276 }
277
278 #endif /* CONFIG_PNP */
279
280 static irqreturn_t snd_wavefront_ics2115_interrupt(int irq, 
281                                             void *dev_id, 
282                                             struct pt_regs *regs)
283 {
284         snd_wavefront_card_t *acard;
285
286         acard = (snd_wavefront_card_t *) dev_id;
287
288         if (acard == NULL) 
289                 return IRQ_NONE;
290
291         if (acard->wavefront.interrupts_are_midi) {
292                 snd_wavefront_midi_interrupt (acard);
293         } else {
294                 snd_wavefront_internal_interrupt (acard);
295         }
296         return IRQ_HANDLED;
297 }
298
299 snd_hwdep_t * __devinit
300 snd_wavefront_new_synth (snd_card_t *card,
301                          int hw_dev,
302                          snd_wavefront_card_t *acard)
303 {
304         snd_hwdep_t *wavefront_synth;
305
306         if (snd_wavefront_detect (acard) < 0) {
307                 return NULL;
308         }
309
310         if (snd_wavefront_start (&acard->wavefront) < 0) {
311                 return NULL;
312         }
313
314         if (snd_hwdep_new(card, "WaveFront", hw_dev, &wavefront_synth) < 0)
315                 return NULL;
316         strcpy (wavefront_synth->name, 
317                 "WaveFront (ICS2115) wavetable synthesizer");
318         wavefront_synth->ops.open = snd_wavefront_synth_open;
319         wavefront_synth->ops.release = snd_wavefront_synth_release;
320         wavefront_synth->ops.ioctl = snd_wavefront_synth_ioctl;
321
322         return wavefront_synth;
323 }
324
325 snd_hwdep_t * __devinit
326 snd_wavefront_new_fx (snd_card_t *card,
327                       int hw_dev,
328                       snd_wavefront_card_t *acard,
329                       unsigned long port)
330
331 {
332         snd_hwdep_t *fx_processor;
333
334         if (snd_wavefront_fx_start (&acard->wavefront)) {
335                 snd_printk ("cannot initialize YSS225 FX processor");
336                 return NULL;
337         }
338
339         if (snd_hwdep_new (card, "YSS225", hw_dev, &fx_processor) < 0)
340                 return NULL;
341         sprintf (fx_processor->name, "YSS225 FX Processor at 0x%lx", port);
342         fx_processor->ops.open = snd_wavefront_fx_open;
343         fx_processor->ops.release = snd_wavefront_fx_release;
344         fx_processor->ops.ioctl = snd_wavefront_fx_ioctl;
345         
346         return fx_processor;
347 }
348
349 static snd_wavefront_mpu_id internal_id = internal_mpu;
350 static snd_wavefront_mpu_id external_id = external_mpu;
351
352 snd_rawmidi_t * __devinit
353 snd_wavefront_new_midi (snd_card_t *card,
354                         int midi_dev,
355                         snd_wavefront_card_t *acard,
356                         unsigned long port,
357                         snd_wavefront_mpu_id mpu)
358
359 {
360         snd_rawmidi_t *rmidi;
361         static int first = 1;
362
363         if (first) {
364                 first = 0;
365                 acard->wavefront.midi.base = port;
366                 if (snd_wavefront_midi_start (acard)) {
367                         snd_printk ("cannot initialize MIDI interface\n");
368                         return NULL;
369                 }
370         }
371
372         if (snd_rawmidi_new (card, "WaveFront MIDI", midi_dev, 1, 1, &rmidi) < 0)
373                 return NULL;
374
375         if (mpu == internal_mpu) {
376                 strcpy(rmidi->name, "WaveFront MIDI (Internal)");
377                 rmidi->private_data = &internal_id;
378         } else {
379                 strcpy(rmidi->name, "WaveFront MIDI (External)");
380                 rmidi->private_data = &external_id;
381         }
382
383         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_wavefront_midi_output);
384         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_wavefront_midi_input);
385
386         rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
387                              SNDRV_RAWMIDI_INFO_INPUT |
388                              SNDRV_RAWMIDI_INFO_DUPLEX;
389
390         return rmidi;
391 }
392
393 static void
394 snd_wavefront_free(snd_card_t *card)
395 {
396         snd_wavefront_card_t *acard = (snd_wavefront_card_t *)card->private_data;
397         
398         if (acard) {
399                 if (acard->wavefront.res_base != NULL) {
400                         release_resource(acard->wavefront.res_base);
401                         kfree_nocheck(acard->wavefront.res_base);
402                 }
403                 if (acard->wavefront.irq > 0)
404                         free_irq(acard->wavefront.irq, (void *)acard);
405         }
406 }
407
408 static int __devinit
409 snd_wavefront_probe (int dev, struct pnp_card_link *pcard,
410                      const struct pnp_card_device_id *pid)
411 {
412         snd_card_t *card;
413         snd_wavefront_card_t *acard;
414         cs4231_t *chip;
415         snd_hwdep_t *wavefront_synth;
416         snd_rawmidi_t *ics2115_internal_rmidi = NULL;
417         snd_rawmidi_t *ics2115_external_rmidi = NULL;
418         snd_hwdep_t *fx_processor;
419         int hw_dev = 0, midi_dev = 0, err;
420
421 #ifdef CONFIG_PNP
422         if (!isapnp[dev]) {
423 #endif
424                 if (cs4232_pcm_port[dev] == SNDRV_AUTO_PORT) {
425                         snd_printk("specify CS4232 port\n");
426                         return -EINVAL;
427                 }
428                 if (ics2115_port[dev] == SNDRV_AUTO_PORT) {
429                         snd_printk("specify ICS2115 port\n");
430                         return -ENODEV;
431                 }
432 #ifdef CONFIG_PNP
433         }
434 #endif
435         card = snd_card_new (index[dev], 
436                              id[dev],
437                              THIS_MODULE,
438                              sizeof(snd_wavefront_card_t));
439
440         if (card == NULL) {
441                 return -ENOMEM;
442         }
443         acard = (snd_wavefront_card_t *)card->private_data;
444         acard->wavefront.irq = -1;
445         spin_lock_init(&acard->wavefront.irq_lock);
446         init_waitqueue_head(&acard->wavefront.interrupt_sleeper);
447         spin_lock_init(&acard->wavefront.midi.open);
448         spin_lock_init(&acard->wavefront.midi.virtual);
449         card->private_free = snd_wavefront_free;
450
451 #ifdef CONFIG_PNP
452         if (isapnp[dev]) {
453                 if (snd_wavefront_pnp (dev, acard, pcard, pid) < 0) {
454                         if (cs4232_pcm_port[dev] == SNDRV_AUTO_PORT) {
455                                 snd_printk ("isapnp detection failed\n");
456                                 snd_card_free (card);
457                                 return -ENODEV;
458                         }
459                 }
460                 snd_card_set_dev(card, &pcard->card->dev);
461         }
462 #endif /* CONFIG_PNP */
463
464         /* --------- PCM --------------- */
465
466         if ((err = snd_cs4231_create (card,
467                                       cs4232_pcm_port[dev],
468                                       -1,
469                                       cs4232_pcm_irq[dev],
470                                       dma1[dev],
471                                       dma2[dev],
472                                       CS4231_HW_DETECT, 0, &chip)) < 0) {
473                 snd_card_free(card);
474                 snd_printk ("can't allocate CS4231 device\n");
475                 return err;
476         }
477
478         if ((err = snd_cs4231_pcm (chip, 0, NULL)) < 0) {
479                 snd_card_free(card);
480                 return err;
481         }
482         if ((err = snd_cs4231_timer (chip, 0, NULL)) < 0) {
483                 snd_card_free(card);
484                 return err;
485         }
486
487         /* ---------- OPL3 synth --------- */
488
489         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
490                 opl3_t *opl3;
491
492                 if ((err = snd_opl3_create(card,
493                                            fm_port[dev],
494                                            fm_port[dev] + 2,
495                                            OPL3_HW_OPL3_CS,
496                                            0, &opl3)) < 0) {
497                         snd_printk ("can't allocate or detect OPL3 synth\n");
498                         snd_card_free(card);
499                         return err;
500                 }
501
502                 if ((err = snd_opl3_hwdep_new(opl3, hw_dev, 1, NULL)) < 0) {
503                         snd_card_free(card);
504                         return err;
505                 }
506                 hw_dev++;
507         }
508
509         /* ------- ICS2115 Wavetable synth ------- */
510
511         if ((acard->wavefront.res_base = request_region(ics2115_port[dev], 16, "ICS2115")) == NULL) {
512                 snd_printk("unable to grab ICS2115 i/o region 0x%lx-0x%lx\n", ics2115_port[dev], ics2115_port[dev] + 16 - 1);
513                 snd_card_free(card);
514                 return -EBUSY;
515         }
516         if (request_irq(ics2115_irq[dev], snd_wavefront_ics2115_interrupt, SA_INTERRUPT, "ICS2115", (void *)acard)) {
517                 snd_printk("unable to use ICS2115 IRQ %d\n", ics2115_irq[dev]);
518                 snd_card_free(card);
519                 return -EBUSY;
520         }
521         
522         acard->wavefront.irq = ics2115_irq[dev];
523         acard->wavefront.base = ics2115_port[dev];
524
525         if ((wavefront_synth = snd_wavefront_new_synth (card, hw_dev, acard)) == NULL) {
526                 snd_printk ("can't create WaveFront synth device\n");
527                 snd_card_free(card);
528                 return -ENOMEM;
529         }
530
531         strcpy (wavefront_synth->name, "ICS2115 Wavetable MIDI Synthesizer");
532         wavefront_synth->iface = SNDRV_HWDEP_IFACE_ICS2115;
533         hw_dev++;
534
535         /* --------- Mixer ------------ */
536
537         if ((err = snd_cs4231_mixer(chip)) < 0) {
538                 snd_printk ("can't allocate mixer device\n");
539                 snd_card_free(card);
540                 return err;
541         }
542
543         /* -------- CS4232 MPU-401 interface -------- */
544
545         if (cs4232_mpu_port[dev] > 0 && cs4232_mpu_port[dev] != SNDRV_AUTO_PORT) {
546                 if ((err = snd_mpu401_uart_new(card, midi_dev, MPU401_HW_CS4232,
547                                                cs4232_mpu_port[dev], 0,
548                                                cs4232_mpu_irq[dev],
549                                                SA_INTERRUPT,
550                                                NULL)) < 0) {
551                         snd_printk ("can't allocate CS4232 MPU-401 device\n");
552                         snd_card_free(card);
553                         return err;
554                 }
555                 midi_dev++;
556         }
557
558         /* ------ ICS2115 internal MIDI ------------ */
559
560         if (ics2115_port[dev] > 0 && ics2115_port[dev] != SNDRV_AUTO_PORT) {
561                 ics2115_internal_rmidi = 
562                         snd_wavefront_new_midi (card, 
563                                                 midi_dev,
564                                                 acard,
565                                                 ics2115_port[dev],
566                                                 internal_mpu);
567                 if (ics2115_internal_rmidi == NULL) {
568                         snd_printk ("can't setup ICS2115 internal MIDI device\n");
569                         snd_card_free(card);
570                         return -ENOMEM;
571                 }
572                 midi_dev++;
573         }
574
575         /* ------ ICS2115 external MIDI ------------ */
576
577         if (ics2115_port[dev] > 0 && ics2115_port[dev] != SNDRV_AUTO_PORT) {
578                 ics2115_external_rmidi = 
579                         snd_wavefront_new_midi (card, 
580                                                 midi_dev,
581                                                 acard,
582                                                 ics2115_port[dev],
583                                                 external_mpu);
584                 if (ics2115_external_rmidi == NULL) {
585                         snd_printk ("can't setup ICS2115 external MIDI device\n");
586                         snd_card_free(card);
587                         return -ENOMEM;
588                 }
589                 midi_dev++;
590         }
591
592         /* FX processor for Tropez+ */
593
594         if (acard->wavefront.has_fx) {
595                 fx_processor = snd_wavefront_new_fx (card,
596                                                      hw_dev,
597                                                      acard,
598                                                      ics2115_port[dev]);
599                 if (fx_processor == NULL) {
600                         snd_printk ("can't setup FX device\n");
601                         snd_card_free(card);
602                         return -ENOMEM;
603                 }
604
605                 hw_dev++;
606
607                 strcpy(card->driver, "Tropez+");
608                 strcpy(card->shortname, "Turtle Beach Tropez+");
609         } else {
610                 /* Need a way to distinguish between Maui and Tropez */
611                 strcpy(card->driver, "WaveFront");
612                 strcpy(card->shortname, "Turtle Beach WaveFront");
613         }
614
615         /* ----- Register the card --------- */
616
617         /* Not safe to include "Turtle Beach" in longname, due to 
618            length restrictions
619         */
620
621         sprintf(card->longname, "%s PCM 0x%lx irq %d dma %d",
622                 card->driver,
623                 chip->port,
624                 cs4232_pcm_irq[dev],
625                 dma1[dev]);
626
627         if (dma2[dev] >= 0 && dma2[dev] < 8)
628                 sprintf(card->longname + strlen(card->longname), "&%d", dma2[dev]);
629
630         if (cs4232_mpu_port[dev] > 0 && cs4232_mpu_port[dev] != SNDRV_AUTO_PORT) {
631                 sprintf (card->longname + strlen (card->longname), 
632                          " MPU-401 0x%lx irq %d",
633                          cs4232_mpu_port[dev],
634                          cs4232_mpu_irq[dev]);
635         }
636
637         sprintf (card->longname + strlen (card->longname), 
638                  " SYNTH 0x%lx irq %d",
639                  ics2115_port[dev],
640                  ics2115_irq[dev]);
641
642         if ((err = snd_card_register(card)) < 0) {
643                 snd_card_free(card);
644                 return err;
645         }
646         if (pcard)
647                 pnp_set_card_drvdata(pcard, card);
648         else
649                 snd_wavefront_legacy[dev] = card;
650         return 0;
651 }       
652
653 #ifdef CONFIG_PNP
654
655 static int __devinit snd_wavefront_pnp_detect(struct pnp_card_link *card,
656                                               const struct pnp_card_device_id *id)
657 {
658         static int dev;
659         int res;
660
661         for ( ; dev < SNDRV_CARDS; dev++) {
662                 if (!enable[dev] || !isapnp[dev])
663                         continue;
664                 res = snd_wavefront_probe(dev, card, id);
665                 if (res < 0)
666                         return res;
667                 dev++;
668                 return 0;
669         }
670
671         return -ENODEV;
672 }
673
674 static void __devexit snd_wavefront_pnp_remove(struct pnp_card_link * pcard)
675 {
676         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
677
678         snd_card_disconnect(card);
679         snd_card_free_in_thread(card);
680 }
681
682 static struct pnp_card_driver wavefront_pnpc_driver = {
683         .flags          = PNP_DRIVER_RES_DISABLE,
684         .name           = "wavefront",
685         .id_table       = snd_wavefront_pnpids,
686         .probe          = snd_wavefront_pnp_detect,
687         .remove         = __devexit_p(snd_wavefront_pnp_remove),
688 };
689
690 #endif /* CONFIG_PNP */
691
692 static int __init alsa_card_wavefront_init(void)
693 {
694         int cards = 0;
695         int dev;
696         for (dev = 0; dev < SNDRV_CARDS; dev++) {
697                 if (!enable[dev])
698                         continue;
699 #ifdef CONFIG_PNP
700                 if (isapnp[dev])
701                         continue;
702 #endif
703                 if (snd_wavefront_probe(dev, NULL, NULL) >= 0)
704                         cards++;
705         }
706 #ifdef CONFIG_PNP
707         cards += pnp_register_card_driver(&wavefront_pnpc_driver);
708 #endif
709         if (!cards) {
710 #ifdef CONFIG_PNP
711                 pnp_unregister_card_driver(&wavefront_pnpc_driver);
712 #endif
713 #ifdef MODULE
714                 printk (KERN_ERR "No WaveFront cards found or devices busy\n");
715 #endif
716                 return -ENODEV;
717         }
718         return 0;
719 }
720
721 static void __exit alsa_card_wavefront_exit(void)
722 {
723         int idx;
724
725 #ifdef CONFIG_PNP
726         pnp_unregister_card_driver(&wavefront_pnpc_driver);
727 #endif
728         for (idx = 0; idx < SNDRV_CARDS; idx++)
729                 snd_card_free(snd_wavefront_legacy[idx]);
730 }
731
732 module_init(alsa_card_wavefront_init)
733 module_exit(alsa_card_wavefront_exit)
734
735 #ifndef MODULE
736
737 /* format is: snd-wavefront=enable,index,id,isapnp,
738                             cs4232_pcm_port,cs4232_pcm_irq,
739                             cs4232_mpu_port,cs4232_mpu_irq,
740                             ics2115_port,ics2115_irq,
741                             fm_port,
742                             dma1,dma2,
743                             use_cs4232_midi */
744
745 static int __init alsa_card_wavefront_setup(char *str)
746 {
747         static unsigned __initdata nr_dev = 0;
748
749         if (nr_dev >= SNDRV_CARDS)
750                 return 0;
751         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
752                get_option(&str,&index[nr_dev]) == 2 &&
753                get_id(&str,&id[nr_dev]) == 2 &&
754                get_option(&str,&isapnp[nr_dev]) == 2 &&
755                get_option_long(&str,&cs4232_pcm_port[nr_dev]) == 2 &&
756                get_option(&str,&cs4232_pcm_irq[nr_dev]) == 2 &&
757                get_option_long(&str,&cs4232_mpu_port[nr_dev]) == 2 &&
758                get_option(&str,&cs4232_mpu_irq[nr_dev]) == 2 &&
759                get_option_long(&str,&ics2115_port[nr_dev]) == 2 &&
760                get_option(&str,&ics2115_irq[nr_dev]) == 2 &&
761                get_option_long(&str,&fm_port[nr_dev]) == 2 &&
762                get_option(&str,&dma1[nr_dev]) == 2 &&
763                get_option(&str,&dma2[nr_dev]) == 2 &&
764                get_option(&str,&use_cs4232_midi[nr_dev]) == 2);
765         nr_dev++;
766         return 1;
767 }
768
769 __setup("snd-wavefront=", alsa_card_wavefront_setup);
770
771 #endif /* ifndef MODULE */