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