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