xref: /linux/sound/isa/gus/gusmax.c (revision 55d0969c451159cff86949b38c39171cab962069)
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 
21 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
22 MODULE_DESCRIPTION("Gravis UltraSound MAX");
23 MODULE_LICENSE("GPL");
24 
25 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
26 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
27 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;	/* Enable this card */
28 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x230,0x240,0x250,0x260 */
29 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 2,3,5,9,11,12,15 */
30 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 1,3,5,6,7 */
31 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 1,3,5,6,7 */
32 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
33 				/* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
34 static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
35 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
36 
37 module_param_array(index, int, NULL, 0444);
38 MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
39 module_param_array(id, charp, NULL, 0444);
40 MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
41 module_param_array(enable, bool, NULL, 0444);
42 MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
43 module_param_hw_array(port, long, ioport, NULL, 0444);
44 MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
45 module_param_hw_array(irq, int, irq, NULL, 0444);
46 MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
47 module_param_hw_array(dma1, int, dma, NULL, 0444);
48 MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
49 module_param_hw_array(dma2, int, dma, NULL, 0444);
50 MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
51 module_param_array(joystick_dac, int, NULL, 0444);
52 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
53 module_param_array(channels, int, NULL, 0444);
54 MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
55 module_param_array(pcm_channels, int, NULL, 0444);
56 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
57 
58 struct 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 
67 static 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, 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(160);
78 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);	/* release reset */
79 	udelay(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 
89 static 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(maxcard->gus_status_reg)) {
98 			handled = 1;
99 			snd_gus_interrupt(irq, maxcard->gus);
100 			loop++;
101 		}
102 		if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
103 			handled = 1;
104 			snd_wss_interrupt(irq, maxcard->wss);
105 			loop++;
106 		}
107 	} while (loop && --max > 0);
108 	return IRQ_RETVAL(handled);
109 }
110 
111 static 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(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
125 }
126 
127 static 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 	strcpy(id1.name, "Aux Playback Switch");
138 	strcpy(id2.name, "Synth Playback Switch");
139 	err = snd_ctl_rename_id(card, &id1, &id2);
140 	if (err < 0)
141 		return err;
142 	strcpy(id1.name, "Aux Playback Volume");
143 	strcpy(id2.name, "Synth Playback Volume");
144 	err = snd_ctl_rename_id(card, &id1, &id2);
145 	if (err < 0)
146 		return err;
147 	/* reassign AUXB to CD */
148 	strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
149 	strcpy(id2.name, "CD Playback Switch");
150 	err = snd_ctl_rename_id(card, &id1, &id2);
151 	if (err < 0)
152 		return err;
153 	strcpy(id1.name, "Aux Playback Volume");
154 	strcpy(id2.name, "CD Playback Volume");
155 	err = snd_ctl_rename_id(card, &id1, &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 
180 static int snd_gusmax_match(struct device *pdev, unsigned int dev)
181 {
182 	return enable[dev];
183 }
184 
185 static 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(pdev, index[dev], id[dev], THIS_MODULE,
196 				sizeof(struct snd_gusmax), &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(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(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(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[dev],
231 				     -xirq, xdma1, xdma2,
232 				     0, channels[dev],
233 				     pcm_channels[dev],
234 				     0, &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 					     possible_ports[i],
243 					     -xirq, xdma1, xdma2,
244 					     0, channels[dev],
245 					     pcm_channels[dev],
246 					     0, &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(card->dev, xirq, snd_gusmax_interrupt, 0,
273 			     "GUS MAX", (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 			     gus->gf1.port + 0x10c, -1, xirq,
282 			     xdma2 < 0 ? xdma1 : xdma2, xdma1,
283 			     WSS_HW_DETECT,
284 			     WSS_HWSHARE_IRQ |
285 			     WSS_HWSHARE_DMA1 |
286 			     WSS_HWSHARE_DMA2,
287 			     &wss);
288 	if (err < 0)
289 		return err;
290 
291 	err = snd_wss_pcm(wss, 0);
292 	if (err < 0)
293 		return err;
294 
295 	err = snd_wss_mixer(wss);
296 	if (err < 0)
297 		return err;
298 
299 	err = snd_wss_timer(wss, 2);
300 	if (err < 0)
301 		return err;
302 
303 	if (pcm_channels[dev] > 0) {
304 		err = snd_gf1_pcm_new(gus, 1, 1);
305 		if (err < 0)
306 			return err;
307 	}
308 	err = snd_gusmax_mixer(wss);
309 	if (err < 0)
310 		return err;
311 
312 	err = snd_gf1_rawmidi_new(gus, 0);
313 	if (err < 0)
314 		return err;
315 
316 	sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
317 	if (xdma2 >= 0)
318 		sprintf(card->longname + strlen(card->longname), "&%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(pdev, card);
328 	return 0;
329 }
330 
331 #define DEV_NAME "gusmax"
332 
333 static 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 
342 module_isa_driver(snd_gusmax_driver, SNDRV_CARDS);
343