]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Driver for Gravis UltraSound MAX soundcard | |
c1017a4c | 4 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
5 | */ |
6 | ||
1da177e4 | 7 | #include <linux/init.h> |
654aa661 | 8 | #include <linux/err.h> |
5e24c1c1 | 9 | #include <linux/isa.h> |
1da177e4 LT |
10 | #include <linux/delay.h> |
11 | #include <linux/time.h> | |
65a77217 | 12 | #include <linux/module.h> |
654aa661 | 13 | #include <asm/dma.h> |
1da177e4 LT |
14 | #include <sound/core.h> |
15 | #include <sound/gus.h> | |
61ef19d7 | 16 | #include <sound/wss.h> |
1da177e4 LT |
17 | #define SNDRV_LEGACY_FIND_FREE_IRQ |
18 | #define SNDRV_LEGACY_FIND_FREE_DMA | |
19 | #include <sound/initval.h> | |
20 | ||
c1017a4c | 21 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
1da177e4 LT |
22 | MODULE_DESCRIPTION("Gravis UltraSound MAX"); |
23 | MODULE_LICENSE("GPL"); | |
24 | MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound MAX}}"); | |
25 | ||
26 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ | |
27 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ | |
a67ff6a5 | 28 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ |
1da177e4 LT |
29 | static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */ |
30 | static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */ | |
31 | static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ | |
32 | static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ | |
33 | static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; | |
34 | /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ | |
35 | static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; | |
36 | static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; | |
37 | ||
38 | module_param_array(index, int, NULL, 0444); | |
39 | MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard."); | |
40 | module_param_array(id, charp, NULL, 0444); | |
41 | MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard."); | |
42 | module_param_array(enable, bool, NULL, 0444); | |
43 | MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard."); | |
e992ef57 | 44 | module_param_hw_array(port, long, ioport, NULL, 0444); |
1da177e4 | 45 | MODULE_PARM_DESC(port, "Port # for GUS MAX driver."); |
e992ef57 | 46 | module_param_hw_array(irq, int, irq, NULL, 0444); |
1da177e4 | 47 | MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver."); |
e992ef57 | 48 | module_param_hw_array(dma1, int, dma, NULL, 0444); |
1da177e4 | 49 | MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver."); |
e992ef57 | 50 | module_param_hw_array(dma2, int, dma, NULL, 0444); |
1da177e4 LT |
51 | MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver."); |
52 | module_param_array(joystick_dac, int, NULL, 0444); | |
53 | MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver."); | |
54 | module_param_array(channels, int, NULL, 0444); | |
55 | MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver."); | |
56 | module_param_array(pcm_channels, int, NULL, 0444); | |
57 | MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver."); | |
58 | ||
59 | struct snd_gusmax { | |
60 | int irq; | |
5e2da206 TI |
61 | struct snd_card *card; |
62 | struct snd_gus_card *gus; | |
7779f75f | 63 | struct snd_wss *wss; |
1da177e4 LT |
64 | unsigned short gus_status_reg; |
65 | unsigned short pcm_status_reg; | |
66 | }; | |
67 | ||
43bcd973 | 68 | #define PFX "gusmax: " |
1da177e4 | 69 | |
1bff292e | 70 | static int snd_gusmax_detect(struct snd_gus_card *gus) |
1da177e4 | 71 | { |
43bcd973 | 72 | unsigned char d; |
1da177e4 | 73 | |
43bcd973 TI |
74 | snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ |
75 | if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) { | |
76 | snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); | |
1da177e4 | 77 | return -ENODEV; |
43bcd973 | 78 | } |
1da177e4 LT |
79 | udelay(160); |
80 | snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ | |
81 | udelay(160); | |
43bcd973 TI |
82 | if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) { |
83 | snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); | |
1da177e4 | 84 | return -ENODEV; |
43bcd973 TI |
85 | } |
86 | ||
1da177e4 LT |
87 | return 0; |
88 | } | |
89 | ||
7d12e780 | 90 | static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id) |
1da177e4 | 91 | { |
c7bec5ab | 92 | struct snd_gusmax *maxcard = dev_id; |
1da177e4 LT |
93 | int loop, max = 5; |
94 | int handled = 0; | |
95 | ||
96 | do { | |
97 | loop = 0; | |
98 | if (inb(maxcard->gus_status_reg)) { | |
99 | handled = 1; | |
7d12e780 | 100 | snd_gus_interrupt(irq, maxcard->gus); |
1da177e4 LT |
101 | loop++; |
102 | } | |
103 | if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */ | |
104 | handled = 1; | |
7779f75f | 105 | snd_wss_interrupt(irq, maxcard->wss); |
1da177e4 LT |
106 | loop++; |
107 | } | |
108 | } while (loop && --max > 0); | |
109 | return IRQ_RETVAL(handled); | |
110 | } | |
111 | ||
1bff292e BP |
112 | static void snd_gusmax_init(int dev, struct snd_card *card, |
113 | struct snd_gus_card *gus) | |
1da177e4 LT |
114 | { |
115 | gus->equal_irq = 1; | |
116 | gus->codec_flag = 1; | |
117 | gus->joystick_dac = joystick_dac[dev]; | |
118 | /* init control register */ | |
119 | gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f; | |
120 | if (gus->gf1.dma1 > 3) | |
121 | gus->max_cntrl_val |= 0x10; | |
122 | if (gus->gf1.dma2 > 3) | |
123 | gus->max_cntrl_val |= 0x20; | |
124 | gus->max_cntrl_val |= 0x40; | |
125 | outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT)); | |
126 | } | |
127 | ||
1bff292e | 128 | static int snd_gusmax_mixer(struct snd_wss *chip) |
1da177e4 | 129 | { |
5e2da206 TI |
130 | struct snd_card *card = chip->card; |
131 | struct snd_ctl_elem_id id1, id2; | |
1da177e4 LT |
132 | int err; |
133 | ||
134 | memset(&id1, 0, sizeof(id1)); | |
135 | memset(&id2, 0, sizeof(id2)); | |
136 | id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
137 | /* reassign AUXA to SYNTHESIZER */ | |
138 | strcpy(id1.name, "Aux Playback Switch"); | |
139 | strcpy(id2.name, "Synth Playback Switch"); | |
140 | if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) | |
141 | return err; | |
142 | strcpy(id1.name, "Aux Playback Volume"); | |
143 | strcpy(id2.name, "Synth Playback Volume"); | |
144 | if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) | |
145 | return err; | |
146 | /* reassign AUXB to CD */ | |
147 | strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; | |
148 | strcpy(id2.name, "CD Playback Switch"); | |
149 | if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) | |
150 | return err; | |
151 | strcpy(id1.name, "Aux Playback Volume"); | |
152 | strcpy(id2.name, "CD Playback Volume"); | |
153 | if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) | |
154 | return err; | |
155 | #if 0 | |
156 | /* reassign Mono Input to MIC */ | |
157 | if (snd_mixer_group_rename(mixer, | |
158 | SNDRV_MIXER_IN_MONO, 0, | |
159 | SNDRV_MIXER_IN_MIC, 0) < 0) | |
160 | goto __error; | |
161 | if (snd_mixer_elem_rename(mixer, | |
162 | SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT, | |
163 | SNDRV_MIXER_IN_MIC, 0) < 0) | |
164 | goto __error; | |
165 | if (snd_mixer_elem_rename(mixer, | |
166 | "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1, | |
167 | "Mic Capture Volume", 0) < 0) | |
168 | goto __error; | |
169 | if (snd_mixer_elem_rename(mixer, | |
170 | "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1, | |
171 | "Mic Capture Switch", 0) < 0) | |
172 | goto __error; | |
173 | #endif | |
174 | return 0; | |
175 | } | |
176 | ||
5e2da206 | 177 | static void snd_gusmax_free(struct snd_card *card) |
1da177e4 | 178 | { |
9fe856e4 | 179 | struct snd_gusmax *maxcard = card->private_data; |
1da177e4 LT |
180 | |
181 | if (maxcard == NULL) | |
182 | return; | |
183 | if (maxcard->irq >= 0) | |
184 | free_irq(maxcard->irq, (void *)maxcard); | |
185 | } | |
186 | ||
1bff292e | 187 | static int snd_gusmax_match(struct device *pdev, unsigned int dev) |
5e24c1c1 TI |
188 | { |
189 | return enable[dev]; | |
190 | } | |
191 | ||
1bff292e | 192 | static int snd_gusmax_probe(struct device *pdev, unsigned int dev) |
1da177e4 LT |
193 | { |
194 | static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1}; | |
195 | static int possible_dmas[] = {5, 6, 7, 1, 3, -1}; | |
196 | int xirq, xdma1, xdma2, err; | |
5e2da206 TI |
197 | struct snd_card *card; |
198 | struct snd_gus_card *gus = NULL; | |
7779f75f | 199 | struct snd_wss *wss; |
1da177e4 LT |
200 | struct snd_gusmax *maxcard; |
201 | ||
4323cc4d TI |
202 | err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE, |
203 | sizeof(struct snd_gusmax), &card); | |
c95eadd2 TI |
204 | if (err < 0) |
205 | return err; | |
1da177e4 | 206 | card->private_free = snd_gusmax_free; |
9fe856e4 | 207 | maxcard = card->private_data; |
1da177e4 LT |
208 | maxcard->card = card; |
209 | maxcard->irq = -1; | |
210 | ||
211 | xirq = irq[dev]; | |
212 | if (xirq == SNDRV_AUTO_IRQ) { | |
213 | if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) { | |
43bcd973 TI |
214 | snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); |
215 | err = -EBUSY; | |
216 | goto _err; | |
1da177e4 LT |
217 | } |
218 | } | |
219 | xdma1 = dma1[dev]; | |
220 | if (xdma1 == SNDRV_AUTO_DMA) { | |
221 | if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) { | |
43bcd973 TI |
222 | snd_printk(KERN_ERR PFX "unable to find a free DMA1\n"); |
223 | err = -EBUSY; | |
224 | goto _err; | |
1da177e4 LT |
225 | } |
226 | } | |
227 | xdma2 = dma2[dev]; | |
228 | if (xdma2 == SNDRV_AUTO_DMA) { | |
229 | if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) { | |
43bcd973 TI |
230 | snd_printk(KERN_ERR PFX "unable to find a free DMA2\n"); |
231 | err = -EBUSY; | |
232 | goto _err; | |
1da177e4 LT |
233 | } |
234 | } | |
235 | ||
654aa661 TI |
236 | if (port[dev] != SNDRV_AUTO_PORT) { |
237 | err = snd_gus_create(card, | |
238 | port[dev], | |
239 | -xirq, xdma1, xdma2, | |
240 | 0, channels[dev], | |
241 | pcm_channels[dev], | |
242 | 0, &gus); | |
243 | } else { | |
244 | static unsigned long possible_ports[] = { | |
245 | 0x220, 0x230, 0x240, 0x250, 0x260 | |
246 | }; | |
247 | int i; | |
248 | for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { | |
249 | err = snd_gus_create(card, | |
250 | possible_ports[i], | |
251 | -xirq, xdma1, xdma2, | |
252 | 0, channels[dev], | |
253 | pcm_channels[dev], | |
254 | 0, &gus); | |
255 | if (err >= 0) { | |
256 | port[dev] = possible_ports[i]; | |
257 | break; | |
258 | } | |
259 | } | |
260 | } | |
261 | if (err < 0) | |
43bcd973 TI |
262 | goto _err; |
263 | ||
264 | if ((err = snd_gusmax_detect(gus)) < 0) | |
265 | goto _err; | |
266 | ||
1da177e4 LT |
267 | maxcard->gus_status_reg = gus->gf1.reg_irqstat; |
268 | maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2; | |
269 | snd_gusmax_init(dev, card, gus); | |
43bcd973 TI |
270 | if ((err = snd_gus_initialize(gus)) < 0) |
271 | goto _err; | |
272 | ||
1da177e4 | 273 | if (!gus->max_flag) { |
43bcd973 TI |
274 | snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port); |
275 | err = -ENODEV; | |
276 | goto _err; | |
1da177e4 LT |
277 | } |
278 | ||
88e24c3a | 279 | if (request_irq(xirq, snd_gusmax_interrupt, 0, "GUS MAX", (void *)maxcard)) { |
43bcd973 TI |
280 | snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq); |
281 | err = -EBUSY; | |
282 | goto _err; | |
1da177e4 LT |
283 | } |
284 | maxcard->irq = xirq; | |
285 | ||
7779f75f KH |
286 | err = snd_wss_create(card, |
287 | gus->gf1.port + 0x10c, -1, xirq, | |
288 | xdma2 < 0 ? xdma1 : xdma2, xdma1, | |
289 | WSS_HW_DETECT, | |
290 | WSS_HWSHARE_IRQ | | |
291 | WSS_HWSHARE_DMA1 | | |
292 | WSS_HWSHARE_DMA2, | |
293 | &wss); | |
294 | if (err < 0) | |
43bcd973 TI |
295 | goto _err; |
296 | ||
fa60c065 | 297 | err = snd_wss_pcm(wss, 0); |
7779f75f | 298 | if (err < 0) |
43bcd973 TI |
299 | goto _err; |
300 | ||
7779f75f KH |
301 | err = snd_wss_mixer(wss); |
302 | if (err < 0) | |
43bcd973 TI |
303 | goto _err; |
304 | ||
fa60c065 | 305 | err = snd_wss_timer(wss, 2); |
7779f75f | 306 | if (err < 0) |
43bcd973 TI |
307 | goto _err; |
308 | ||
1da177e4 | 309 | if (pcm_channels[dev] > 0) { |
db5abb3c | 310 | if ((err = snd_gf1_pcm_new(gus, 1, 1)) < 0) |
43bcd973 | 311 | goto _err; |
1da177e4 | 312 | } |
7779f75f KH |
313 | err = snd_gusmax_mixer(wss); |
314 | if (err < 0) | |
43bcd973 | 315 | goto _err; |
1da177e4 | 316 | |
db5abb3c | 317 | err = snd_gf1_rawmidi_new(gus, 0); |
7779f75f | 318 | if (err < 0) |
43bcd973 | 319 | goto _err; |
1da177e4 LT |
320 | |
321 | sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1); | |
322 | if (xdma2 >= 0) | |
323 | sprintf(card->longname + strlen(card->longname), "&%i", xdma2); | |
43bcd973 | 324 | |
7779f75f KH |
325 | err = snd_card_register(card); |
326 | if (err < 0) | |
43bcd973 | 327 | goto _err; |
1da177e4 LT |
328 | |
329 | maxcard->gus = gus; | |
7779f75f | 330 | maxcard->wss = wss; |
654aa661 | 331 | |
5e24c1c1 | 332 | dev_set_drvdata(pdev, card); |
1da177e4 | 333 | return 0; |
43bcd973 TI |
334 | |
335 | _err: | |
336 | snd_card_free(card); | |
337 | return err; | |
1da177e4 LT |
338 | } |
339 | ||
1bff292e | 340 | static int snd_gusmax_remove(struct device *devptr, unsigned int dev) |
1da177e4 | 341 | { |
5e24c1c1 | 342 | snd_card_free(dev_get_drvdata(devptr)); |
654aa661 | 343 | return 0; |
1da177e4 LT |
344 | } |
345 | ||
83c51c0a | 346 | #define DEV_NAME "gusmax" |
654aa661 | 347 | |
5e24c1c1 TI |
348 | static struct isa_driver snd_gusmax_driver = { |
349 | .match = snd_gusmax_match, | |
654aa661 | 350 | .probe = snd_gusmax_probe, |
1bff292e | 351 | .remove = snd_gusmax_remove, |
654aa661 TI |
352 | /* FIXME: suspend/resume */ |
353 | .driver = { | |
83c51c0a | 354 | .name = DEV_NAME |
654aa661 TI |
355 | }, |
356 | }; | |
357 | ||
a99e8c62 | 358 | module_isa_driver(snd_gusmax_driver, SNDRV_CARDS); |