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