ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / gus / gusclassic.c
1 /*
2  *  Driver for Gravis UltraSound Classic 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 #define SNDRV_LEGACY_AUTO_PROBE
30 #define SNDRV_LEGACY_FIND_FREE_IRQ
31 #define SNDRV_LEGACY_FIND_FREE_DMA
32 #define SNDRV_GET_ID
33 #include <sound/initval.h>
34
35 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
36 MODULE_DESCRIPTION("Gravis UltraSound Classic");
37 MODULE_LICENSE("GPL");
38 MODULE_CLASSES("{sound}");
39 MODULE_DEVICES("{{Gravis,UltraSound Classic}}");
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;        /* 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_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
54 MODULE_PARM_DESC(index, "Index value for GUS Classic soundcard.");
55 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
56 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
57 MODULE_PARM_DESC(id, "ID string for GUS Classic soundcard.");
58 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
59 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
60 MODULE_PARM_DESC(enable, "Enable GUS Classic soundcard.");
61 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
62 MODULE_PARM(port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
63 MODULE_PARM_DESC(port, "Port # for GUS Classic driver.");
64 MODULE_PARM_SYNTAX(port, SNDRV_ENABLED ",allows:{{0x220,0x260,0x10}},dialog:list");
65 MODULE_PARM(irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
66 MODULE_PARM_DESC(irq, "IRQ # for GUS Classic driver.");
67 MODULE_PARM_SYNTAX(irq, SNDRV_ENABLED ",allows:{{3},{5},{9},{11},{12},{15}},dialog:list");
68 MODULE_PARM(dma1, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
69 MODULE_PARM_DESC(dma1, "DMA1 # for GUS Classic driver.");
70 MODULE_PARM_SYNTAX(dma1, SNDRV_ENABLED ",allows:{{1},{3},{5},{6},{7}},dialog:list");
71 MODULE_PARM(dma2, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
72 MODULE_PARM_DESC(dma2, "DMA2 # for GUS Classic driver.");
73 MODULE_PARM_SYNTAX(dma2, SNDRV_ENABLED ",allows:{{1},{3},{5},{6},{7}},dialog:list");
74 MODULE_PARM(joystick_dac, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
75 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS Classic driver.");
76 MODULE_PARM_SYNTAX(joystick_dac, SNDRV_ENABLED ",allows:{{0,31}}");
77 MODULE_PARM(channels, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
78 MODULE_PARM_DESC(channels, "GF1 channels for GUS Classic driver.");
79 MODULE_PARM_SYNTAX(channels,  SNDRV_ENABLED ",allows:{{14,32}}");
80 MODULE_PARM(pcm_channels, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
81 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS Classic driver.");
82 MODULE_PARM_SYNTAX(pcm_channels, SNDRV_ENABLED ",allows:{{2,16}}");
83
84 static snd_card_t *snd_gusclassic_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
85
86
87 static int __init snd_gusclassic_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
120         return 0;
121 }
122
123 static void __init snd_gusclassic_init(int dev, snd_gus_card_t * gus)
124 {
125         gus->equal_irq = 0;
126         gus->codec_flag = 0;
127         gus->max_flag = 0;
128         gus->joystick_dac = joystick_dac[dev];
129 }
130
131 static int __init snd_gusclassic_probe(int dev)
132 {
133         static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, 4, -1};
134         static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
135         int xirq, xdma1, xdma2;
136         snd_card_t *card;
137         struct snd_gusclassic *guscard;
138         snd_gus_card_t *gus = NULL;
139         int err;
140
141         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
142         if (card == NULL)
143                 return -ENOMEM;
144         guscard = (struct snd_gusclassic *)card->private_data;
145         if (pcm_channels[dev] < 2)
146                 pcm_channels[dev] = 2;
147
148         xirq = irq[dev];
149         if (xirq == SNDRV_AUTO_IRQ) {
150                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
151                         snd_card_free(card);
152                         snd_printk("unable to find a free IRQ\n");
153                         return -EBUSY;
154                 }
155         }
156         xdma1 = dma1[dev];
157         if (xdma1 == SNDRV_AUTO_DMA) {
158                 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
159                         snd_card_free(card);
160                         snd_printk("unable to find a free DMA1\n");
161                         return -EBUSY;
162                 }
163         }
164         xdma2 = dma2[dev];
165         if (xdma2 == SNDRV_AUTO_DMA) {
166                 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
167                         snd_card_free(card);
168                         snd_printk("unable to find a free DMA2\n");
169                         return -EBUSY;
170                 }
171         }
172
173
174         if ((err = snd_gus_create(card,
175                                   port[dev],
176                                   xirq, xdma1, xdma2,
177                                   0, channels[dev], pcm_channels[dev],
178                                   0, &gus)) < 0) {
179                 snd_card_free(card);
180                 return err;
181         }
182         if ((err = snd_gusclassic_detect(gus)) < 0) {
183                 snd_card_free(card);
184                 return err;
185         }
186         snd_gusclassic_init(dev, gus);
187         if ((err = snd_gus_initialize(gus)) < 0) {
188                 snd_card_free(card);
189                 return err;
190         }
191         if (gus->max_flag || gus->ess_flag) {
192                 snd_printdd("GUS Classic or ACE soundcard was not detected at 0x%lx\n", gus->gf1.port);
193                 snd_card_free(card);
194                 return -ENODEV;
195         }
196         if ((err = snd_gf1_new_mixer(gus)) < 0) {
197                 snd_card_free(card);
198                 return err;
199         }
200         if ((err = snd_gf1_pcm_new(gus, 0, 0, NULL)) < 0) {
201                 snd_card_free(card);
202                 return err;
203         }
204         if (!gus->ace_flag) {
205                 if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0) {
206                         snd_card_free(card);
207                         return err;
208                 }
209         }
210         sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %d, dma %d", gus->gf1.port, xirq, xdma1);
211         if (dma2 >= 0)
212                 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
213         if ((err = snd_card_register(card)) < 0) {
214                 snd_card_free(card);
215                 return err;
216         }
217         snd_gusclassic_cards[dev] = card;
218         return 0;
219 }
220
221 static int __init snd_gusclassic_legacy_auto_probe(unsigned long xport)
222 {
223         static int dev;
224         int res;
225
226         for ( ; dev < SNDRV_CARDS; dev++) {
227                 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
228                         continue;
229                 port[dev] = xport;
230                 res = snd_gusclassic_probe(dev);
231                 if (res < 0)
232                         port[dev] = SNDRV_AUTO_PORT;
233                 return res;
234         }
235         return -ENODEV;
236 }
237
238 static int __init alsa_card_gusclassic_init(void)
239 {
240         static unsigned long possible_ports[] = {0x220, 0x230, 0x240, 0x250, 0x260, -1};
241         int dev, cards, i;
242
243         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
244                 if (port[dev] == SNDRV_AUTO_PORT)
245                         continue;
246                 if (snd_gusclassic_probe(dev) >= 0)
247                         cards++;
248         }
249         i = snd_legacy_auto_probe(possible_ports, snd_gusclassic_legacy_auto_probe);
250         if (i > 0)
251                 cards += i;
252
253         if (!cards) {
254 #ifdef MODULE
255                 printk(KERN_ERR "GUS Classic soundcard not found or device busy\n");
256 #endif
257                 return -ENODEV;
258         }
259         return 0;
260 }
261
262 static void __exit alsa_card_gusclassic_exit(void)
263 {
264         int idx;
265         
266         for (idx = 0; idx < SNDRV_CARDS; idx++)
267                 snd_card_free(snd_gusclassic_cards[idx]);
268 }
269
270 module_init(alsa_card_gusclassic_init)
271 module_exit(alsa_card_gusclassic_exit)
272
273 #ifndef MODULE
274
275 /* format is: snd-gusclassic=enable,index,id,
276                              port,irq,
277                              dma1,dma2,
278                              joystick_dac,
279                              channels,pcm_channels */
280
281 static int __init alsa_card_gusclassic_setup(char *str)
282 {
283         static unsigned __initdata nr_dev = 0;
284
285         if (nr_dev >= SNDRV_CARDS)
286                 return 0;
287         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
288                get_option(&str,&index[nr_dev]) == 2 &&
289                get_id(&str,&id[nr_dev]) == 2 &&
290                get_option_long(&str,&port[nr_dev]) == 2 &&
291                get_option(&str,&irq[nr_dev]) == 2 &&
292                get_option(&str,&dma1[nr_dev]) == 2 &&
293                get_option(&str,&dma2[nr_dev]) == 2 &&
294                get_option(&str,&joystick_dac[nr_dev]) == 2 &&
295                get_option(&str,&channels[nr_dev]) == 2 &&
296                get_option(&str,&pcm_channels[nr_dev]) == 2);
297         nr_dev++;
298         return 1;
299 }
300
301 __setup("snd-gusclassic=", alsa_card_gusclassic_setup);
302
303 #endif /* ifndef MODULE */