[ALSA] Nest sound devices
[linux-2.6-block.git] / sound / isa / gus / gusextreme.c
CommitLineData
1da177e4
LT
1/*
2 * Driver for Gravis UltraSound Extreme 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>
1da177e4 23#include <linux/init.h>
654aa661
TI
24#include <linux/err.h>
25#include <linux/platform_device.h>
1da177e4
LT
26#include <linux/delay.h>
27#include <linux/time.h>
28#include <linux/moduleparam.h>
654aa661 29#include <asm/dma.h>
1da177e4
LT
30#include <sound/core.h>
31#include <sound/gus.h>
32#include <sound/es1688.h>
33#include <sound/mpu401.h>
34#include <sound/opl3.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
40MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
41MODULE_DESCRIPTION("Gravis UltraSound Extreme");
42MODULE_LICENSE("GPL");
43MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound Extreme}}");
44
45static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
46static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
47static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
48static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
49static long gf1_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x210,0x220,0x230,0x240,0x250,0x260,0x270 */
50static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x300,0x310,0x320 */
51static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
52static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
53static int gf1_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */
54static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
55static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
56static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
57 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
58static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
59static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
60
61module_param_array(index, int, NULL, 0444);
62MODULE_PARM_DESC(index, "Index value for GUS Extreme soundcard.");
63module_param_array(id, charp, NULL, 0444);
64MODULE_PARM_DESC(id, "ID string for GUS Extreme soundcard.");
65module_param_array(enable, bool, NULL, 0444);
66MODULE_PARM_DESC(enable, "Enable GUS Extreme soundcard.");
67module_param_array(port, long, NULL, 0444);
68MODULE_PARM_DESC(port, "Port # for GUS Extreme driver.");
69module_param_array(gf1_port, long, NULL, 0444);
70MODULE_PARM_DESC(gf1_port, "GF1 port # for GUS Extreme driver (optional).");
71module_param_array(mpu_port, long, NULL, 0444);
72MODULE_PARM_DESC(mpu_port, "MPU-401 port # for GUS Extreme driver.");
73module_param_array(irq, int, NULL, 0444);
74MODULE_PARM_DESC(irq, "IRQ # for GUS Extreme driver.");
75module_param_array(mpu_irq, int, NULL, 0444);
76MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for GUS Extreme driver.");
77module_param_array(gf1_irq, int, NULL, 0444);
78MODULE_PARM_DESC(gf1_irq, "GF1 IRQ # for GUS Extreme driver.");
79module_param_array(dma8, int, NULL, 0444);
80MODULE_PARM_DESC(dma8, "8-bit DMA # for GUS Extreme driver.");
81module_param_array(dma1, int, NULL, 0444);
82MODULE_PARM_DESC(dma1, "GF1 DMA # for GUS Extreme driver.");
83module_param_array(joystick_dac, int, NULL, 0444);
84MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS Extreme driver.");
85module_param_array(channels, int, NULL, 0444);
86MODULE_PARM_DESC(channels, "GF1 channels for GUS Extreme driver.");
87module_param_array(pcm_channels, int, NULL, 0444);
88MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS Extreme driver.");
89
1da177e4 90
43bcd973 91#define PFX "gusextreme: "
1da177e4
LT
92
93static int __init snd_gusextreme_detect(int dev,
5e2da206
TI
94 struct snd_card *card,
95 struct snd_gus_card * gus,
96 struct snd_es1688 *es1688)
1da177e4
LT
97{
98 unsigned long flags;
43bcd973 99 unsigned char d;
1da177e4
LT
100
101 /*
102 * This is main stuff - enable access to GF1 chip...
103 * I'm not sure, if this will work for card which have
104 * ES1688 chip in another place than 0x220.
105 *
106 * I used reverse-engineering in DOSEMU. [--jk]
107 *
108 * ULTRINIT.EXE:
109 * 0x230 = 0,2,3
110 * 0x240 = 2,0,1
111 * 0x250 = 2,0,3
112 * 0x260 = 2,2,1
113 */
114
115 spin_lock_irqsave(&es1688->mixer_lock, flags);
116 snd_es1688_mixer_write(es1688, 0x40, 0x0b); /* don't change!!! */
117 spin_unlock_irqrestore(&es1688->mixer_lock, flags);
118 spin_lock_irqsave(&es1688->reg_lock, flags);
119 outb(gf1_port[dev] & 0x040 ? 2 : 0, ES1688P(es1688, INIT1));
120 outb(0, 0x201);
121 outb(gf1_port[dev] & 0x020 ? 2 : 0, ES1688P(es1688, INIT1));
122 outb(0, 0x201);
123 outb(gf1_port[dev] & 0x010 ? 3 : 1, ES1688P(es1688, INIT1));
124 spin_unlock_irqrestore(&es1688->reg_lock, flags);
125
126 udelay(100);
127
128 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
43bcd973
TI
129 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
130 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
1da177e4 131 return -EIO;
43bcd973 132 }
1da177e4
LT
133 udelay(160);
134 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
135 udelay(160);
43bcd973
TI
136 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
137 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
1da177e4 138 return -EIO;
43bcd973 139 }
1da177e4
LT
140 return 0;
141}
142
5e2da206 143static void __init snd_gusextreme_init(int dev, struct snd_gus_card * gus)
1da177e4
LT
144{
145 gus->joystick_dac = joystick_dac[dev];
146}
147
5e2da206 148static int __init snd_gusextreme_mixer(struct snd_es1688 *chip)
1da177e4 149{
5e2da206
TI
150 struct snd_card *card = chip->card;
151 struct snd_ctl_elem_id id1, id2;
1da177e4
LT
152 int err;
153
154 memset(&id1, 0, sizeof(id1));
155 memset(&id2, 0, sizeof(id2));
156 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
157 /* reassign AUX to SYNTHESIZER */
158 strcpy(id1.name, "Aux Playback Volume");
159 strcpy(id2.name, "Synth Playback Volume");
160 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
161 return err;
162 /* reassign Master Playback Switch to Synth Playback Switch */
163 strcpy(id1.name, "Master Playback Switch");
164 strcpy(id2.name, "Synth Playback Switch");
165 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
166 return err;
167 return 0;
168}
169
654aa661 170static int __init snd_gusextreme_probe(struct platform_device *pdev)
1da177e4 171{
654aa661 172 int dev = pdev->id;
1da177e4
LT
173 static int possible_ess_irqs[] = {5, 9, 10, 7, -1};
174 static int possible_ess_dmas[] = {1, 3, 0, -1};
175 static int possible_gf1_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
176 static int possible_gf1_dmas[] = {5, 6, 7, 1, 3, -1};
177 int xgf1_irq, xgf1_dma, xess_irq, xmpu_irq, xess_dma;
5e2da206 178 struct snd_card *card;
5e2da206
TI
179 struct snd_gus_card *gus;
180 struct snd_es1688 *es1688;
181 struct snd_opl3 *opl3;
1da177e4
LT
182 int err;
183
184 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
185 if (card == NULL)
186 return -ENOMEM;
1da177e4
LT
187
188 xgf1_irq = gf1_irq[dev];
189 if (xgf1_irq == SNDRV_AUTO_IRQ) {
190 if ((xgf1_irq = snd_legacy_find_free_irq(possible_gf1_irqs)) < 0) {
43bcd973 191 snd_printk(KERN_ERR PFX "unable to find a free IRQ for GF1\n");
1da177e4
LT
192 err = -EBUSY;
193 goto out;
194 }
195 }
196 xess_irq = irq[dev];
197 if (xess_irq == SNDRV_AUTO_IRQ) {
198 if ((xess_irq = snd_legacy_find_free_irq(possible_ess_irqs)) < 0) {
43bcd973 199 snd_printk(KERN_ERR PFX "unable to find a free IRQ for ES1688\n");
1da177e4
LT
200 err = -EBUSY;
201 goto out;
202 }
203 }
204 if (mpu_port[dev] == SNDRV_AUTO_PORT)
205 mpu_port[dev] = 0;
206 xmpu_irq = mpu_irq[dev];
207 if (xmpu_irq > 15)
208 xmpu_irq = -1;
209 xgf1_dma = dma1[dev];
210 if (xgf1_dma == SNDRV_AUTO_DMA) {
211 if ((xgf1_dma = snd_legacy_find_free_dma(possible_gf1_dmas)) < 0) {
43bcd973 212 snd_printk(KERN_ERR PFX "unable to find a free DMA for GF1\n");
1da177e4
LT
213 err = -EBUSY;
214 goto out;
215 }
216 }
217 xess_dma = dma8[dev];
218 if (xess_dma == SNDRV_AUTO_DMA) {
219 if ((xess_dma = snd_legacy_find_free_dma(possible_ess_dmas)) < 0) {
43bcd973 220 snd_printk(KERN_ERR PFX "unable to find a free DMA for ES1688\n");
1da177e4
LT
221 err = -EBUSY;
222 goto out;
223 }
224 }
225
654aa661
TI
226 if (port[dev] != SNDRV_AUTO_PORT) {
227 err = snd_es1688_create(card, port[dev], mpu_port[dev],
228 xess_irq, xmpu_irq, xess_dma,
229 ES1688_HW_1688, &es1688);
230 } else {
231 /* auto-probe legacy ports */
232 static unsigned long possible_ports[] = {0x220, 0x240, 0x260};
233 int i;
234 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
235 err = snd_es1688_create(card,
236 possible_ports[i],
237 mpu_port[dev],
238 xess_irq, xmpu_irq, xess_dma,
239 ES1688_HW_1688, &es1688);
240 if (err >= 0) {
241 port[dev] = possible_ports[i];
242 break;
243 }
244 }
245 }
246 if (err < 0)
1da177e4 247 goto out;
654aa661 248
1da177e4
LT
249 if (gf1_port[dev] < 0)
250 gf1_port[dev] = port[dev] + 0x20;
251 if ((err = snd_gus_create(card,
252 gf1_port[dev],
253 xgf1_irq,
254 xgf1_dma,
255 -1,
256 0, channels[dev],
257 pcm_channels[dev], 0,
258 &gus)) < 0)
259 goto out;
260
261 if ((err = snd_gusextreme_detect(dev, card, gus, es1688)) < 0)
262 goto out;
263
264 snd_gusextreme_init(dev, gus);
265 if ((err = snd_gus_initialize(gus)) < 0)
266 goto out;
267
268 if (!gus->ess_flag) {
43bcd973 269 snd_printk(KERN_ERR PFX "GUS Extreme soundcard was not detected at 0x%lx\n", gus->gf1.port);
1da177e4
LT
270 err = -ENODEV;
271 goto out;
272 }
273 if ((err = snd_es1688_pcm(es1688, 0, NULL)) < 0)
274 goto out;
275
276 if ((err = snd_es1688_mixer(es1688)) < 0)
277 goto out;
278
279 snd_component_add(card, "ES1688");
280 if (pcm_channels[dev] > 0) {
281 if ((err = snd_gf1_pcm_new(gus, 1, 1, NULL)) < 0)
282 goto out;
283 }
284 if ((err = snd_gf1_new_mixer(gus)) < 0)
285 goto out;
286
287 if ((err = snd_gusextreme_mixer(es1688)) < 0)
288 goto out;
289
290 if (snd_opl3_create(card, es1688->port, es1688->port + 2,
291 OPL3_HW_OPL3, 0, &opl3) < 0) {
43bcd973 292 printk(KERN_ERR PFX "gusextreme: opl3 not detected at 0x%lx\n", es1688->port);
1da177e4
LT
293 } else {
294 if ((err = snd_opl3_hwdep_new(opl3, 0, 2, NULL)) < 0)
295 goto out;
296 }
297
298 if (es1688->mpu_port >= 0x300 &&
299 (err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
300 es1688->mpu_port, 0,
301 xmpu_irq,
302 SA_INTERRUPT,
303 NULL)) < 0)
304 goto out;
305
306 sprintf(card->longname, "Gravis UltraSound Extreme at 0x%lx, irq %i&%i, dma %i&%i",
307 es1688->port, xgf1_irq, xess_irq, xgf1_dma, xess_dma);
43bcd973 308
654aa661 309 snd_card_set_dev(card, &pdev->dev);
43bcd973 310
1da177e4
LT
311 if ((err = snd_card_register(card)) < 0)
312 goto out;
313
654aa661 314 platform_set_drvdata(pdev, card);
1da177e4
LT
315 return 0;
316
317 out:
318 snd_card_free(card);
319 return err;
320}
321
654aa661 322static int snd_gusextreme_remove(struct platform_device *devptr)
1da177e4 323{
654aa661
TI
324 snd_card_free(platform_get_drvdata(devptr));
325 platform_set_drvdata(devptr, NULL);
326 return 0;
1da177e4
LT
327}
328
654aa661
TI
329#define GUSEXTREME_DRIVER "snd_gusextreme"
330
331static struct platform_driver snd_gusextreme_driver = {
332 .probe = snd_gusextreme_probe,
333 .remove = snd_gusextreme_remove,
334 /* FIXME: suspend/resume */
335 .driver = {
336 .name = GUSEXTREME_DRIVER
337 },
338};
339
1da177e4
LT
340static int __init alsa_card_gusextreme_init(void)
341{
654aa661
TI
342 int i, cards, err;
343
344 err = platform_driver_register(&snd_gusextreme_driver);
345 if (err < 0)
346 return err;
1da177e4 347
654aa661
TI
348 cards = 0;
349 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
350 struct platform_device *device;
351 device = platform_device_register_simple(GUSEXTREME_DRIVER,
352 i, NULL, 0);
353 if (IS_ERR(device)) {
354 err = PTR_ERR(device);
355 goto errout;
356 }
357 cards++;
358 }
1da177e4
LT
359 if (!cards) {
360#ifdef MODULE
361 printk(KERN_ERR "GUS Extreme soundcard not found or device busy\n");
362#endif
654aa661
TI
363 err = -ENODEV;
364 goto errout;
1da177e4
LT
365 }
366 return 0;
654aa661
TI
367
368 errout:
369 platform_driver_unregister(&snd_gusextreme_driver);
370 return err;
1da177e4
LT
371}
372
373static void __exit alsa_card_gusextreme_exit(void)
374{
654aa661 375 platform_driver_unregister(&snd_gusextreme_driver);
1da177e4
LT
376}
377
378module_init(alsa_card_gusextreme_init)
379module_exit(alsa_card_gusextreme_exit)