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