]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/isa/gus/gusmax.c
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
[net-next-2.6.git] / sound / isa / gus / gusmax.c
CommitLineData
1da177e4
LT
1/*
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>
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/cs4231.h>
1da177e4
LT
33#define SNDRV_LEGACY_FIND_FREE_IRQ
34#define SNDRV_LEGACY_FIND_FREE_DMA
35#include <sound/initval.h>
36
37MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
38MODULE_DESCRIPTION("Gravis UltraSound MAX");
39MODULE_LICENSE("GPL");
40MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound MAX}}");
41
42static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
43static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
44static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
45static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */
46static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */
47static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
48static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
49static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
50 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
51static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
52static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
53
54module_param_array(index, int, NULL, 0444);
55MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
56module_param_array(id, charp, NULL, 0444);
57MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
58module_param_array(enable, bool, NULL, 0444);
59MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
60module_param_array(port, long, NULL, 0444);
61MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
62module_param_array(irq, int, NULL, 0444);
63MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
64module_param_array(dma1, int, NULL, 0444);
65MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
66module_param_array(dma2, int, NULL, 0444);
67MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
68module_param_array(joystick_dac, int, NULL, 0444);
69MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
70module_param_array(channels, int, NULL, 0444);
71MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
72module_param_array(pcm_channels, int, NULL, 0444);
73MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
74
f7a9275d
CL
75static struct platform_device *devices[SNDRV_CARDS];
76
1da177e4
LT
77struct snd_gusmax {
78 int irq;
5e2da206
TI
79 struct snd_card *card;
80 struct snd_gus_card *gus;
81 struct snd_cs4231 *cs4231;
1da177e4
LT
82 unsigned short gus_status_reg;
83 unsigned short pcm_status_reg;
84};
85
43bcd973 86#define PFX "gusmax: "
1da177e4 87
5e2da206 88static int __init snd_gusmax_detect(struct snd_gus_card * gus)
1da177e4 89{
43bcd973 90 unsigned char d;
1da177e4 91
43bcd973
TI
92 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
93 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
94 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
1da177e4 95 return -ENODEV;
43bcd973 96 }
1da177e4
LT
97 udelay(160);
98 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
99 udelay(160);
43bcd973
TI
100 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
101 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
1da177e4 102 return -ENODEV;
43bcd973
TI
103 }
104
1da177e4
LT
105 return 0;
106}
107
7d12e780 108static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id)
1da177e4
LT
109{
110 struct snd_gusmax *maxcard = (struct snd_gusmax *) dev_id;
111 int loop, max = 5;
112 int handled = 0;
113
114 do {
115 loop = 0;
116 if (inb(maxcard->gus_status_reg)) {
117 handled = 1;
7d12e780 118 snd_gus_interrupt(irq, maxcard->gus);
1da177e4
LT
119 loop++;
120 }
121 if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
122 handled = 1;
7d12e780 123 snd_cs4231_interrupt(irq, maxcard->cs4231);
1da177e4
LT
124 loop++;
125 }
126 } while (loop && --max > 0);
127 return IRQ_RETVAL(handled);
128}
129
5e2da206 130static void __init snd_gusmax_init(int dev, struct snd_card *card, struct snd_gus_card * gus)
1da177e4
LT
131{
132 gus->equal_irq = 1;
133 gus->codec_flag = 1;
134 gus->joystick_dac = joystick_dac[dev];
135 /* init control register */
136 gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f;
137 if (gus->gf1.dma1 > 3)
138 gus->max_cntrl_val |= 0x10;
139 if (gus->gf1.dma2 > 3)
140 gus->max_cntrl_val |= 0x20;
141 gus->max_cntrl_val |= 0x40;
142 outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
143}
144
145#define CS4231_PRIVATE( left, right, shift, mute ) \
146 ((left << 24)|(right << 16)|(shift<<8)|mute)
147
5e2da206 148static int __init snd_gusmax_mixer(struct snd_cs4231 *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 AUXA to SYNTHESIZER */
158 strcpy(id1.name, "Aux Playback Switch");
159 strcpy(id2.name, "Synth Playback Switch");
160 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
161 return err;
162 strcpy(id1.name, "Aux Playback Volume");
163 strcpy(id2.name, "Synth Playback Volume");
164 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
165 return err;
166 /* reassign AUXB to CD */
167 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
168 strcpy(id2.name, "CD Playback Switch");
169 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
170 return err;
171 strcpy(id1.name, "Aux Playback Volume");
172 strcpy(id2.name, "CD Playback Volume");
173 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
174 return err;
175#if 0
176 /* reassign Mono Input to MIC */
177 if (snd_mixer_group_rename(mixer,
178 SNDRV_MIXER_IN_MONO, 0,
179 SNDRV_MIXER_IN_MIC, 0) < 0)
180 goto __error;
181 if (snd_mixer_elem_rename(mixer,
182 SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT,
183 SNDRV_MIXER_IN_MIC, 0) < 0)
184 goto __error;
185 if (snd_mixer_elem_rename(mixer,
186 "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1,
187 "Mic Capture Volume", 0) < 0)
188 goto __error;
189 if (snd_mixer_elem_rename(mixer,
190 "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1,
191 "Mic Capture Switch", 0) < 0)
192 goto __error;
193#endif
194 return 0;
195}
196
5e2da206 197static void snd_gusmax_free(struct snd_card *card)
1da177e4
LT
198{
199 struct snd_gusmax *maxcard = (struct snd_gusmax *)card->private_data;
200
201 if (maxcard == NULL)
202 return;
203 if (maxcard->irq >= 0)
204 free_irq(maxcard->irq, (void *)maxcard);
205}
206
654aa661 207static int __init snd_gusmax_probe(struct platform_device *pdev)
1da177e4 208{
654aa661 209 int dev = pdev->id;
1da177e4
LT
210 static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
211 static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
212 int xirq, xdma1, xdma2, err;
5e2da206
TI
213 struct snd_card *card;
214 struct snd_gus_card *gus = NULL;
215 struct snd_cs4231 *cs4231;
1da177e4
LT
216 struct snd_gusmax *maxcard;
217
218 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
219 sizeof(struct snd_gusmax));
220 if (card == NULL)
221 return -ENOMEM;
222 card->private_free = snd_gusmax_free;
223 maxcard = (struct snd_gusmax *)card->private_data;
224 maxcard->card = card;
225 maxcard->irq = -1;
226
227 xirq = irq[dev];
228 if (xirq == SNDRV_AUTO_IRQ) {
229 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
43bcd973
TI
230 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
231 err = -EBUSY;
232 goto _err;
1da177e4
LT
233 }
234 }
235 xdma1 = dma1[dev];
236 if (xdma1 == SNDRV_AUTO_DMA) {
237 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
43bcd973
TI
238 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
239 err = -EBUSY;
240 goto _err;
1da177e4
LT
241 }
242 }
243 xdma2 = dma2[dev];
244 if (xdma2 == SNDRV_AUTO_DMA) {
245 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
43bcd973
TI
246 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
247 err = -EBUSY;
248 goto _err;
1da177e4
LT
249 }
250 }
251
654aa661
TI
252 if (port[dev] != SNDRV_AUTO_PORT) {
253 err = snd_gus_create(card,
254 port[dev],
255 -xirq, xdma1, xdma2,
256 0, channels[dev],
257 pcm_channels[dev],
258 0, &gus);
259 } else {
260 static unsigned long possible_ports[] = {
261 0x220, 0x230, 0x240, 0x250, 0x260
262 };
263 int i;
264 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
265 err = snd_gus_create(card,
266 possible_ports[i],
267 -xirq, xdma1, xdma2,
268 0, channels[dev],
269 pcm_channels[dev],
270 0, &gus);
271 if (err >= 0) {
272 port[dev] = possible_ports[i];
273 break;
274 }
275 }
276 }
277 if (err < 0)
43bcd973
TI
278 goto _err;
279
280 if ((err = snd_gusmax_detect(gus)) < 0)
281 goto _err;
282
1da177e4
LT
283 maxcard->gus_status_reg = gus->gf1.reg_irqstat;
284 maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
285 snd_gusmax_init(dev, card, gus);
43bcd973
TI
286 if ((err = snd_gus_initialize(gus)) < 0)
287 goto _err;
288
1da177e4 289 if (!gus->max_flag) {
43bcd973
TI
290 snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port);
291 err = -ENODEV;
292 goto _err;
1da177e4
LT
293 }
294
65ca68b3 295 if (request_irq(xirq, snd_gusmax_interrupt, IRQF_DISABLED, "GUS MAX", (void *)maxcard)) {
43bcd973
TI
296 snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
297 err = -EBUSY;
298 goto _err;
1da177e4
LT
299 }
300 maxcard->irq = xirq;
301
302 if ((err = snd_cs4231_create(card,
303 gus->gf1.port + 0x10c, -1, xirq,
304 xdma2 < 0 ? xdma1 : xdma2, xdma1,
305 CS4231_HW_DETECT,
306 CS4231_HWSHARE_IRQ |
307 CS4231_HWSHARE_DMA1 |
308 CS4231_HWSHARE_DMA2,
43bcd973
TI
309 &cs4231)) < 0)
310 goto _err;
311
312 if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
313 goto _err;
314
315 if ((err = snd_cs4231_mixer(cs4231)) < 0)
316 goto _err;
317
318 if ((err = snd_cs4231_timer(cs4231, 2, NULL)) < 0)
319 goto _err;
320
1da177e4 321 if (pcm_channels[dev] > 0) {
43bcd973
TI
322 if ((err = snd_gf1_pcm_new(gus, 1, 1, NULL)) < 0)
323 goto _err;
1da177e4 324 }
43bcd973
TI
325 if ((err = snd_gusmax_mixer(cs4231)) < 0)
326 goto _err;
1da177e4 327
43bcd973
TI
328 if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
329 goto _err;
1da177e4
LT
330
331 sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
332 if (xdma2 >= 0)
333 sprintf(card->longname + strlen(card->longname), "&%i", xdma2);
43bcd973 334
654aa661 335 snd_card_set_dev(card, &pdev->dev);
43bcd973
TI
336
337 if ((err = snd_card_register(card)) < 0)
338 goto _err;
1da177e4
LT
339
340 maxcard->gus = gus;
341 maxcard->cs4231 = cs4231;
654aa661
TI
342
343 platform_set_drvdata(pdev, card);
1da177e4 344 return 0;
43bcd973
TI
345
346 _err:
347 snd_card_free(card);
348 return err;
1da177e4
LT
349}
350
654aa661 351static int snd_gusmax_remove(struct platform_device *devptr)
1da177e4 352{
654aa661
TI
353 snd_card_free(platform_get_drvdata(devptr));
354 platform_set_drvdata(devptr, NULL);
355 return 0;
1da177e4
LT
356}
357
654aa661
TI
358#define GUSMAX_DRIVER "snd_gusmax"
359
360static struct platform_driver snd_gusmax_driver = {
361 .probe = snd_gusmax_probe,
362 .remove = snd_gusmax_remove,
363 /* FIXME: suspend/resume */
364 .driver = {
365 .name = GUSMAX_DRIVER
366 },
367};
368
f7a9275d
CL
369static void __init_or_module snd_gusmax_unregister_all(void)
370{
371 int i;
372
373 for (i = 0; i < ARRAY_SIZE(devices); ++i)
374 platform_device_unregister(devices[i]);
375 platform_driver_unregister(&snd_gusmax_driver);
376}
377
1da177e4
LT
378static int __init alsa_card_gusmax_init(void)
379{
654aa661
TI
380 int i, cards, err;
381
382 err = platform_driver_register(&snd_gusmax_driver);
383 if (err < 0)
384 return err;
1da177e4 385
654aa661 386 cards = 0;
8278ca8f 387 for (i = 0; i < SNDRV_CARDS; i++) {
654aa661 388 struct platform_device *device;
8278ca8f
TI
389 if (! enable[i])
390 continue;
654aa661
TI
391 device = platform_device_register_simple(GUSMAX_DRIVER,
392 i, NULL, 0);
d0ac642d
RH
393 if (IS_ERR(device))
394 continue;
dcccdd93
RH
395 if (!platform_get_drvdata(device)) {
396 platform_device_unregister(device);
397 continue;
398 }
f7a9275d 399 devices[i] = device;
654aa661
TI
400 cards++;
401 }
1da177e4
LT
402 if (!cards) {
403#ifdef MODULE
404 printk(KERN_ERR "GUS MAX soundcard not found or device busy\n");
405#endif
d0ac642d
RH
406 snd_gusmax_unregister_all();
407 return -ENODEV;
1da177e4
LT
408 }
409 return 0;
410}
411
412static void __exit alsa_card_gusmax_exit(void)
413{
f7a9275d 414 snd_gusmax_unregister_all();
1da177e4
LT
415}
416
417module_init(alsa_card_gusmax_init)
418module_exit(alsa_card_gusmax_exit)