patch-2_6_7-vs1_9_1_12
[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 <linux/moduleparam.h>
28 #include <sound/core.h>
29 #include <sound/gus.h>
30 #define SNDRV_LEGACY_AUTO_PROBE
31 #define SNDRV_LEGACY_FIND_FREE_IRQ
32 #define SNDRV_LEGACY_FIND_FREE_DMA
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 static int boot_devs;
53
54 module_param_array(index, int, boot_devs, 0444);
55 MODULE_PARM_DESC(index, "Index value for GUS Classic soundcard.");
56 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
57 module_param_array(id, charp, boot_devs, 0444);
58 MODULE_PARM_DESC(id, "ID string for GUS Classic soundcard.");
59 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
60 module_param_array(enable, bool, boot_devs, 0444);
61 MODULE_PARM_DESC(enable, "Enable GUS Classic soundcard.");
62 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
63 module_param_array(port, long, boot_devs, 0444);
64 MODULE_PARM_DESC(port, "Port # for GUS Classic driver.");
65 MODULE_PARM_SYNTAX(port, SNDRV_ENABLED ",allows:{{0x220,0x260,0x10}},dialog:list");
66 module_param_array(irq, int, boot_devs, 0444);
67 MODULE_PARM_DESC(irq, "IRQ # for GUS Classic driver.");
68 MODULE_PARM_SYNTAX(irq, SNDRV_ENABLED ",allows:{{3},{5},{9},{11},{12},{15}},dialog:list");
69 module_param_array(dma1, int, boot_devs, 0444);
70 MODULE_PARM_DESC(dma1, "DMA1 # for GUS Classic driver.");
71 MODULE_PARM_SYNTAX(dma1, SNDRV_ENABLED ",allows:{{1},{3},{5},{6},{7}},dialog:list");
72 module_param_array(dma2, int, boot_devs, 0444);
73 MODULE_PARM_DESC(dma2, "DMA2 # for GUS Classic driver.");
74 MODULE_PARM_SYNTAX(dma2, SNDRV_ENABLED ",allows:{{1},{3},{5},{6},{7}},dialog:list");
75 module_param_array(joystick_dac, int, boot_devs, 0444);
76 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS Classic driver.");
77 MODULE_PARM_SYNTAX(joystick_dac, SNDRV_ENABLED ",allows:{{0,31}}");
78 module_param_array(channels, int, boot_devs, 0444);
79 MODULE_PARM_DESC(channels, "GF1 channels for GUS Classic driver.");
80 MODULE_PARM_SYNTAX(channels,  SNDRV_ENABLED ",allows:{{14,32}}");
81 module_param_array(pcm_channels, int, boot_devs, 0444);
82 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS Classic driver.");
83 MODULE_PARM_SYNTAX(pcm_channels, SNDRV_ENABLED ",allows:{{2,16}}");
84
85 static snd_card_t *snd_gusclassic_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
86
87
88 static int __init snd_gusclassic_detect(snd_gus_card_t * gus)
89 {
90         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);   /* reset GF1 */
91 #ifdef CONFIG_SND_DEBUG_DETECT
92         {
93                 unsigned char d;
94
95                 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
96                         snd_printk("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
97                         return -ENODEV;
98                 }
99         }
100 #else
101         if ((snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET) & 0x07) != 0)
102                 return -ENODEV;
103 #endif
104         udelay(160);
105         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);   /* release reset */
106         udelay(160);
107 #ifdef CONFIG_SND_DEBUG_DETECT
108         {
109                 unsigned char d;
110
111                 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
112                         snd_printk("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
113                         return -ENODEV;
114                 }
115         }
116 #else
117         if ((snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET) & 0x07) != 1)
118                 return -ENODEV;
119 #endif
120
121         return 0;
122 }
123
124 static void __init snd_gusclassic_init(int dev, snd_gus_card_t * gus)
125 {
126         gus->equal_irq = 0;
127         gus->codec_flag = 0;
128         gus->max_flag = 0;
129         gus->joystick_dac = joystick_dac[dev];
130 }
131
132 static int __init snd_gusclassic_probe(int dev)
133 {
134         static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, 4, -1};
135         static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
136         int xirq, xdma1, xdma2;
137         snd_card_t *card;
138         struct snd_gusclassic *guscard;
139         snd_gus_card_t *gus = NULL;
140         int err;
141
142         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
143         if (card == NULL)
144                 return -ENOMEM;
145         guscard = (struct snd_gusclassic *)card->private_data;
146         if (pcm_channels[dev] < 2)
147                 pcm_channels[dev] = 2;
148
149         xirq = irq[dev];
150         if (xirq == SNDRV_AUTO_IRQ) {
151                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
152                         snd_card_free(card);
153                         snd_printk("unable to find a free IRQ\n");
154                         return -EBUSY;
155                 }
156         }
157         xdma1 = dma1[dev];
158         if (xdma1 == SNDRV_AUTO_DMA) {
159                 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
160                         snd_card_free(card);
161                         snd_printk("unable to find a free DMA1\n");
162                         return -EBUSY;
163                 }
164         }
165         xdma2 = dma2[dev];
166         if (xdma2 == SNDRV_AUTO_DMA) {
167                 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
168                         snd_card_free(card);
169                         snd_printk("unable to find a free DMA2\n");
170                         return -EBUSY;
171                 }
172         }
173
174
175         if ((err = snd_gus_create(card,
176                                   port[dev],
177                                   xirq, xdma1, xdma2,
178                                   0, channels[dev], pcm_channels[dev],
179                                   0, &gus)) < 0) {
180                 snd_card_free(card);
181                 return err;
182         }
183         if ((err = snd_gusclassic_detect(gus)) < 0) {
184                 snd_card_free(card);
185                 return err;
186         }
187         snd_gusclassic_init(dev, gus);
188         if ((err = snd_gus_initialize(gus)) < 0) {
189                 snd_card_free(card);
190                 return err;
191         }
192         if (gus->max_flag || gus->ess_flag) {
193                 snd_printdd("GUS Classic or ACE soundcard was not detected at 0x%lx\n", gus->gf1.port);
194                 snd_card_free(card);
195                 return -ENODEV;
196         }
197         if ((err = snd_gf1_new_mixer(gus)) < 0) {
198                 snd_card_free(card);
199                 return err;
200         }
201         if ((err = snd_gf1_pcm_new(gus, 0, 0, NULL)) < 0) {
202                 snd_card_free(card);
203                 return err;
204         }
205         if (!gus->ace_flag) {
206                 if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0) {
207                         snd_card_free(card);
208                         return err;
209                 }
210         }
211         sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %d, dma %d", gus->gf1.port, xirq, xdma1);
212         if (dma2 >= 0)
213                 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
214         if ((err = snd_card_register(card)) < 0) {
215                 snd_card_free(card);
216                 return err;
217         }
218         snd_gusclassic_cards[dev] = card;
219         return 0;
220 }
221
222 static int __init snd_gusclassic_legacy_auto_probe(unsigned long xport)
223 {
224         static int dev;
225         int res;
226
227         for ( ; dev < SNDRV_CARDS; dev++) {
228                 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
229                         continue;
230                 port[dev] = xport;
231                 res = snd_gusclassic_probe(dev);
232                 if (res < 0)
233                         port[dev] = SNDRV_AUTO_PORT;
234                 return res;
235         }
236         return -ENODEV;
237 }
238
239 static int __init alsa_card_gusclassic_init(void)
240 {
241         static unsigned long possible_ports[] = {0x220, 0x230, 0x240, 0x250, 0x260, -1};
242         int dev, cards, i;
243
244         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
245                 if (port[dev] == SNDRV_AUTO_PORT)
246                         continue;
247                 if (snd_gusclassic_probe(dev) >= 0)
248                         cards++;
249         }
250         i = snd_legacy_auto_probe(possible_ports, snd_gusclassic_legacy_auto_probe);
251         if (i > 0)
252                 cards += i;
253
254         if (!cards) {
255 #ifdef MODULE
256                 printk(KERN_ERR "GUS Classic soundcard not found or device busy\n");
257 #endif
258                 return -ENODEV;
259         }
260         return 0;
261 }
262
263 static void __exit alsa_card_gusclassic_exit(void)
264 {
265         int idx;
266         
267         for (idx = 0; idx < SNDRV_CARDS; idx++)
268                 snd_card_free(snd_gusclassic_cards[idx]);
269 }
270
271 module_init(alsa_card_gusclassic_init)
272 module_exit(alsa_card_gusclassic_exit)