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

source code of linux/sound/isa/gus/gusmax.c