blob: 63311cdb4a1a3944b3f721bc7c7b48cb0862a9a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Gravis UltraSound MAX soundcard
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/delay.h>
26#include <linux/time.h>
27#include <linux/moduleparam.h>
28#include <sound/core.h>
29#include <sound/gus.h>
30#include <sound/cs4231.h>
31#define SNDRV_LEGACY_AUTO_PROBE
32#define SNDRV_LEGACY_FIND_FREE_IRQ
33#define SNDRV_LEGACY_FIND_FREE_DMA
34#include <sound/initval.h>
35
36MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
37MODULE_DESCRIPTION("Gravis UltraSound MAX");
38MODULE_LICENSE("GPL");
39MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound MAX}}");
40
41static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
42static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
43static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
44static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */
45static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */
46static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
47static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
48static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
49 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
50static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
51static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
52
53module_param_array(index, int, NULL, 0444);
54MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
55module_param_array(id, charp, NULL, 0444);
56MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
57module_param_array(enable, bool, NULL, 0444);
58MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
59module_param_array(port, long, NULL, 0444);
60MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
61module_param_array(irq, int, NULL, 0444);
62MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
63module_param_array(dma1, int, NULL, 0444);
64MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
65module_param_array(dma2, int, NULL, 0444);
66MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
67module_param_array(joystick_dac, int, NULL, 0444);
68MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
69module_param_array(channels, int, NULL, 0444);
70MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
71module_param_array(pcm_channels, int, NULL, 0444);
72MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
73
74struct snd_gusmax {
75 int irq;
Takashi Iwai5e2da202005-11-17 14:36:44 +010076 struct snd_card *card;
77 struct snd_gus_card *gus;
78 struct snd_cs4231 *cs4231;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned short gus_status_reg;
80 unsigned short pcm_status_reg;
81};
82
Takashi Iwai5e2da202005-11-17 14:36:44 +010083static struct snd_card *snd_gusmax_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Takashi Iwai43bcd972005-09-05 17:19:20 +020085#define PFX "gusmax: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwai5e2da202005-11-17 14:36:44 +010087static int __init snd_gusmax_detect(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Takashi Iwai43bcd972005-09-05 17:19:20 +020089 unsigned char d;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwai43bcd972005-09-05 17:19:20 +020091 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
92 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
93 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -ENODEV;
Takashi Iwai43bcd972005-09-05 17:19:20 +020095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 udelay(160);
97 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
98 udelay(160);
Takashi Iwai43bcd972005-09-05 17:19:20 +020099 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
100 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -ENODEV;
Takashi Iwai43bcd972005-09-05 17:19:20 +0200102 }
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
105}
106
107static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id, struct pt_regs *regs)
108{
109 struct snd_gusmax *maxcard = (struct snd_gusmax *) dev_id;
110 int loop, max = 5;
111 int handled = 0;
112
113 do {
114 loop = 0;
115 if (inb(maxcard->gus_status_reg)) {
116 handled = 1;
117 snd_gus_interrupt(irq, maxcard->gus, regs);
118 loop++;
119 }
120 if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
121 handled = 1;
122 snd_cs4231_interrupt(irq, maxcard->cs4231, regs);
123 loop++;
124 }
125 } while (loop && --max > 0);
126 return IRQ_RETVAL(handled);
127}
128
Takashi Iwai5e2da202005-11-17 14:36:44 +0100129static void __init snd_gusmax_init(int dev, struct snd_card *card, struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 gus->equal_irq = 1;
132 gus->codec_flag = 1;
133 gus->joystick_dac = joystick_dac[dev];
134 /* init control register */
135 gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f;
136 if (gus->gf1.dma1 > 3)
137 gus->max_cntrl_val |= 0x10;
138 if (gus->gf1.dma2 > 3)
139 gus->max_cntrl_val |= 0x20;
140 gus->max_cntrl_val |= 0x40;
141 outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
142}
143
144#define CS4231_PRIVATE( left, right, shift, mute ) \
145 ((left << 24)|(right << 16)|(shift<<8)|mute)
146
Takashi Iwai5e2da202005-11-17 14:36:44 +0100147static int __init snd_gusmax_mixer(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100149 struct snd_card *card = chip->card;
150 struct snd_ctl_elem_id id1, id2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int err;
152
153 memset(&id1, 0, sizeof(id1));
154 memset(&id2, 0, sizeof(id2));
155 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
156 /* reassign AUXA to SYNTHESIZER */
157 strcpy(id1.name, "Aux Playback Switch");
158 strcpy(id2.name, "Synth Playback Switch");
159 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
160 return err;
161 strcpy(id1.name, "Aux Playback Volume");
162 strcpy(id2.name, "Synth Playback Volume");
163 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
164 return err;
165 /* reassign AUXB to CD */
166 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
167 strcpy(id2.name, "CD Playback Switch");
168 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
169 return err;
170 strcpy(id1.name, "Aux Playback Volume");
171 strcpy(id2.name, "CD Playback Volume");
172 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
173 return err;
174#if 0
175 /* reassign Mono Input to MIC */
176 if (snd_mixer_group_rename(mixer,
177 SNDRV_MIXER_IN_MONO, 0,
178 SNDRV_MIXER_IN_MIC, 0) < 0)
179 goto __error;
180 if (snd_mixer_elem_rename(mixer,
181 SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT,
182 SNDRV_MIXER_IN_MIC, 0) < 0)
183 goto __error;
184 if (snd_mixer_elem_rename(mixer,
185 "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1,
186 "Mic Capture Volume", 0) < 0)
187 goto __error;
188 if (snd_mixer_elem_rename(mixer,
189 "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1,
190 "Mic Capture Switch", 0) < 0)
191 goto __error;
192#endif
193 return 0;
194}
195
Takashi Iwai5e2da202005-11-17 14:36:44 +0100196static void snd_gusmax_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 struct snd_gusmax *maxcard = (struct snd_gusmax *)card->private_data;
199
200 if (maxcard == NULL)
201 return;
202 if (maxcard->irq >= 0)
203 free_irq(maxcard->irq, (void *)maxcard);
204}
205
206static int __init snd_gusmax_probe(int dev)
207{
208 static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
209 static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
210 int xirq, xdma1, xdma2, err;
Takashi Iwai5e2da202005-11-17 14:36:44 +0100211 struct snd_card *card;
212 struct snd_gus_card *gus = NULL;
213 struct snd_cs4231 *cs4231;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct snd_gusmax *maxcard;
215
216 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
217 sizeof(struct snd_gusmax));
218 if (card == NULL)
219 return -ENOMEM;
220 card->private_free = snd_gusmax_free;
221 maxcard = (struct snd_gusmax *)card->private_data;
222 maxcard->card = card;
223 maxcard->irq = -1;
224
225 xirq = irq[dev];
226 if (xirq == SNDRV_AUTO_IRQ) {
227 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200228 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
229 err = -EBUSY;
230 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232 }
233 xdma1 = dma1[dev];
234 if (xdma1 == SNDRV_AUTO_DMA) {
235 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200236 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
237 err = -EBUSY;
238 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 }
241 xdma2 = dma2[dev];
242 if (xdma2 == SNDRV_AUTO_DMA) {
243 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200244 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
245 err = -EBUSY;
246 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 }
249
250 if ((err = snd_gus_create(card,
251 port[dev],
252 -xirq, xdma1, xdma2,
253 0, channels[dev],
254 pcm_channels[dev],
Takashi Iwai43bcd972005-09-05 17:19:20 +0200255 0, &gus)) < 0)
256 goto _err;
257
258 if ((err = snd_gusmax_detect(gus)) < 0)
259 goto _err;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 maxcard->gus_status_reg = gus->gf1.reg_irqstat;
262 maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
263 snd_gusmax_init(dev, card, gus);
Takashi Iwai43bcd972005-09-05 17:19:20 +0200264 if ((err = snd_gus_initialize(gus)) < 0)
265 goto _err;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!gus->max_flag) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200268 snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port);
269 err = -ENODEV;
270 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 if (request_irq(xirq, snd_gusmax_interrupt, SA_INTERRUPT, "GUS MAX", (void *)maxcard)) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200274 snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
275 err = -EBUSY;
276 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 maxcard->irq = xirq;
279
280 if ((err = snd_cs4231_create(card,
281 gus->gf1.port + 0x10c, -1, xirq,
282 xdma2 < 0 ? xdma1 : xdma2, xdma1,
283 CS4231_HW_DETECT,
284 CS4231_HWSHARE_IRQ |
285 CS4231_HWSHARE_DMA1 |
286 CS4231_HWSHARE_DMA2,
Takashi Iwai43bcd972005-09-05 17:19:20 +0200287 &cs4231)) < 0)
288 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Takashi Iwai43bcd972005-09-05 17:19:20 +0200290 if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
291 goto _err;
292
293 if ((err = snd_cs4231_mixer(cs4231)) < 0)
294 goto _err;
295
296 if ((err = snd_cs4231_timer(cs4231, 2, NULL)) < 0)
297 goto _err;
298
299 if (pcm_channels[dev] > 0) {
300 if ((err = snd_gf1_pcm_new(gus, 1, 1, NULL)) < 0)
301 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Takashi Iwai43bcd972005-09-05 17:19:20 +0200303 if ((err = snd_gusmax_mixer(cs4231)) < 0)
304 goto _err;
305
306 if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
307 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
310 if (xdma2 >= 0)
311 sprintf(card->longname + strlen(card->longname), "&%i", xdma2);
Takashi Iwai43bcd972005-09-05 17:19:20 +0200312
313 if ((err = snd_card_set_generic_dev(card)) < 0)
314 goto _err;
315
316 if ((err = snd_card_register(card)) < 0)
317 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 maxcard->gus = gus;
320 maxcard->cs4231 = cs4231;
321 snd_gusmax_cards[dev] = card;
322 return 0;
Takashi Iwai43bcd972005-09-05 17:19:20 +0200323
324 _err:
325 snd_card_free(card);
326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329static int __init snd_gusmax_legacy_auto_probe(unsigned long xport)
330{
331 static int dev;
332 int res;
333
334 for ( ; dev < SNDRV_CARDS; dev++) {
335 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
336 continue;
337 port[dev] = xport;
338 res = snd_gusmax_probe(dev);
339 if (res < 0)
340 port[dev] = SNDRV_AUTO_PORT;
341 return res;
342 }
343 return -ENODEV;
344}
345
346static int __init alsa_card_gusmax_init(void)
347{
348 static unsigned long possible_ports[] = {0x220, 0x230, 0x240, 0x250, 0x260, -1};
349 int dev, cards, i;
350
351 for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev] > 0; dev++) {
352 if (port[dev] == SNDRV_AUTO_PORT)
353 continue;
354 if (snd_gusmax_probe(dev) >= 0)
355 cards++;
356 }
357 i = snd_legacy_auto_probe(possible_ports, snd_gusmax_legacy_auto_probe);
358 if (i > 0)
359 cards += i;
360
361 if (!cards) {
362#ifdef MODULE
363 printk(KERN_ERR "GUS MAX soundcard not found or device busy\n");
364#endif
365 return -ENODEV;
366 }
367 return 0;
368}
369
370static void __exit alsa_card_gusmax_exit(void)
371{
372 int idx;
373
374 for (idx = 0; idx < SNDRV_CARDS; idx++)
375 snd_card_free(snd_gusmax_cards[idx]);
376}
377
378module_init(alsa_card_gusmax_init)
379module_exit(alsa_card_gusmax_exit)