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