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