xref: /openbmc/linux/sound/isa/gus/gus_main.c (revision 3213486f)
1 /*
2  *  Routines for Gravis UltraSound soundcards
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/ioport.h>
27 #include <linux/module.h>
28 #include <sound/core.h>
29 #include <sound/gus.h>
30 #include <sound/control.h>
31 
32 #include <asm/dma.h>
33 
34 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
35 MODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards");
36 MODULE_LICENSE("GPL");
37 
38 static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches);
39 
40 int snd_gus_use_inc(struct snd_gus_card * gus)
41 {
42 	if (!try_module_get(gus->card->module))
43 		return 0;
44 	return 1;
45 }
46 
47 void snd_gus_use_dec(struct snd_gus_card * gus)
48 {
49 	module_put(gus->card->module);
50 }
51 
52 static int snd_gus_joystick_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
53 {
54 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
55 	uinfo->count = 1;
56 	uinfo->value.integer.min = 0;
57 	uinfo->value.integer.max = 31;
58 	return 0;
59 }
60 
61 static int snd_gus_joystick_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
62 {
63 	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
64 
65 	ucontrol->value.integer.value[0] = gus->joystick_dac & 31;
66 	return 0;
67 }
68 
69 static int snd_gus_joystick_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
70 {
71 	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
72 	unsigned long flags;
73 	int change;
74 	unsigned char nval;
75 
76 	nval = ucontrol->value.integer.value[0] & 31;
77 	spin_lock_irqsave(&gus->reg_lock, flags);
78 	change = gus->joystick_dac != nval;
79 	gus->joystick_dac = nval;
80 	snd_gf1_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
81 	spin_unlock_irqrestore(&gus->reg_lock, flags);
82 	return change;
83 }
84 
85 static const struct snd_kcontrol_new snd_gus_joystick_control = {
86 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
87 	.name = "Joystick Speed",
88 	.info = snd_gus_joystick_info,
89 	.get = snd_gus_joystick_get,
90 	.put = snd_gus_joystick_put
91 };
92 
93 static void snd_gus_init_control(struct snd_gus_card *gus)
94 {
95 	int ret;
96 
97 	if (!gus->ace_flag) {
98 		ret =
99 			snd_ctl_add(gus->card,
100 					snd_ctl_new1(&snd_gus_joystick_control,
101 						gus));
102 		if (ret)
103 			snd_printk(KERN_ERR "gus: snd_ctl_add failed: %d\n",
104 					ret);
105 	}
106 }
107 
108 /*
109  *
110  */
111 
112 static int snd_gus_free(struct snd_gus_card *gus)
113 {
114 	if (gus->gf1.res_port2 == NULL)
115 		goto __hw_end;
116 	snd_gf1_stop(gus);
117 	snd_gus_init_dma_irq(gus, 0);
118       __hw_end:
119 	release_and_free_resource(gus->gf1.res_port1);
120 	release_and_free_resource(gus->gf1.res_port2);
121 	if (gus->gf1.irq >= 0)
122 		free_irq(gus->gf1.irq, (void *) gus);
123 	if (gus->gf1.dma1 >= 0) {
124 		disable_dma(gus->gf1.dma1);
125 		free_dma(gus->gf1.dma1);
126 	}
127 	if (!gus->equal_dma && gus->gf1.dma2 >= 0) {
128 		disable_dma(gus->gf1.dma2);
129 		free_dma(gus->gf1.dma2);
130 	}
131 	kfree(gus);
132 	return 0;
133 }
134 
135 static int snd_gus_dev_free(struct snd_device *device)
136 {
137 	struct snd_gus_card *gus = device->device_data;
138 	return snd_gus_free(gus);
139 }
140 
141 int snd_gus_create(struct snd_card *card,
142 		   unsigned long port,
143 		   int irq, int dma1, int dma2,
144 		   int timer_dev,
145 		   int voices,
146 		   int pcm_channels,
147 		   int effect,
148 		   struct snd_gus_card **rgus)
149 {
150 	struct snd_gus_card *gus;
151 	int err;
152 	static struct snd_device_ops ops = {
153 		.dev_free =	snd_gus_dev_free,
154 	};
155 
156 	*rgus = NULL;
157 	gus = kzalloc(sizeof(*gus), GFP_KERNEL);
158 	if (gus == NULL)
159 		return -ENOMEM;
160 	spin_lock_init(&gus->reg_lock);
161 	spin_lock_init(&gus->voice_alloc);
162 	spin_lock_init(&gus->active_voice_lock);
163 	spin_lock_init(&gus->event_lock);
164 	spin_lock_init(&gus->dma_lock);
165 	spin_lock_init(&gus->pcm_volume_level_lock);
166 	spin_lock_init(&gus->uart_cmd_lock);
167 	mutex_init(&gus->dma_mutex);
168 	gus->gf1.irq = -1;
169 	gus->gf1.dma1 = -1;
170 	gus->gf1.dma2 = -1;
171 	gus->card = card;
172 	gus->gf1.port = port;
173 	/* fill register variables for speedup */
174 	gus->gf1.reg_page = GUSP(gus, GF1PAGE);
175 	gus->gf1.reg_regsel = GUSP(gus, GF1REGSEL);
176 	gus->gf1.reg_data8 = GUSP(gus, GF1DATAHIGH);
177 	gus->gf1.reg_data16 = GUSP(gus, GF1DATALOW);
178 	gus->gf1.reg_irqstat = GUSP(gus, IRQSTAT);
179 	gus->gf1.reg_dram = GUSP(gus, DRAM);
180 	gus->gf1.reg_timerctrl = GUSP(gus, TIMERCNTRL);
181 	gus->gf1.reg_timerdata = GUSP(gus, TIMERDATA);
182 	/* allocate resources */
183 	if ((gus->gf1.res_port1 = request_region(port, 16, "GUS GF1 (Adlib/SB)")) == NULL) {
184 		snd_printk(KERN_ERR "gus: can't grab SB port 0x%lx\n", port);
185 		snd_gus_free(gus);
186 		return -EBUSY;
187 	}
188 	if ((gus->gf1.res_port2 = request_region(port + 0x100, 12, "GUS GF1 (Synth)")) == NULL) {
189 		snd_printk(KERN_ERR "gus: can't grab synth port 0x%lx\n", port + 0x100);
190 		snd_gus_free(gus);
191 		return -EBUSY;
192 	}
193 	if (irq >= 0 && request_irq(irq, snd_gus_interrupt, 0, "GUS GF1", (void *) gus)) {
194 		snd_printk(KERN_ERR "gus: can't grab irq %d\n", irq);
195 		snd_gus_free(gus);
196 		return -EBUSY;
197 	}
198 	gus->gf1.irq = irq;
199 	if (request_dma(dma1, "GUS - 1")) {
200 		snd_printk(KERN_ERR "gus: can't grab DMA1 %d\n", dma1);
201 		snd_gus_free(gus);
202 		return -EBUSY;
203 	}
204 	gus->gf1.dma1 = dma1;
205 	if (dma2 >= 0 && dma1 != dma2) {
206 		if (request_dma(dma2, "GUS - 2")) {
207 			snd_printk(KERN_ERR "gus: can't grab DMA2 %d\n", dma2);
208 			snd_gus_free(gus);
209 			return -EBUSY;
210 		}
211 		gus->gf1.dma2 = dma2;
212 	} else {
213 		gus->gf1.dma2 = gus->gf1.dma1;
214 		gus->equal_dma = 1;
215 	}
216 	gus->timer_dev = timer_dev;
217 	if (voices < 14)
218 		voices = 14;
219 	if (voices > 32)
220 		voices = 32;
221 	if (pcm_channels < 0)
222 		pcm_channels = 0;
223 	if (pcm_channels > 8)
224 		pcm_channels = 8;
225 	pcm_channels++;
226 	pcm_channels &= ~1;
227 	gus->gf1.effect = effect ? 1 : 0;
228 	gus->gf1.active_voices = voices;
229 	gus->gf1.pcm_channels = pcm_channels;
230 	gus->gf1.volume_ramp = 25;
231 	gus->gf1.smooth_pan = 1;
232 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops)) < 0) {
233 		snd_gus_free(gus);
234 		return err;
235 	}
236 	*rgus = gus;
237 	return 0;
238 }
239 
240 /*
241  *  Memory detection routine for plain GF1 soundcards
242  */
243 
244 static int snd_gus_detect_memory(struct snd_gus_card * gus)
245 {
246 	int l, idx, local;
247 	unsigned char d;
248 
249 	snd_gf1_poke(gus, 0L, 0xaa);
250 	snd_gf1_poke(gus, 1L, 0x55);
251 	if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) {
252 		snd_printk(KERN_ERR "plain GF1 card at 0x%lx without onboard DRAM?\n", gus->gf1.port);
253 		return -ENOMEM;
254 	}
255 	for (idx = 1, d = 0xab; idx < 4; idx++, d++) {
256 		local = idx << 18;
257 		snd_gf1_poke(gus, local, d);
258 		snd_gf1_poke(gus, local + 1, d + 1);
259 		if (snd_gf1_peek(gus, local) != d ||
260 		    snd_gf1_peek(gus, local + 1) != d + 1 ||
261 		    snd_gf1_peek(gus, 0L) != 0xaa)
262 			break;
263 	}
264 #if 1
265 	gus->gf1.memory = idx << 18;
266 #else
267 	gus->gf1.memory = 256 * 1024;
268 #endif
269 	for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) {
270 		gus->gf1.mem_alloc.banks_8[l].address =
271 		    gus->gf1.mem_alloc.banks_8[l].size = 0;
272 		gus->gf1.mem_alloc.banks_16[l].address = l << 18;
273 		gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0;
274 	}
275 	gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory;
276 	return 0;		/* some memory were detected */
277 }
278 
279 static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches)
280 {
281 	struct snd_card *card;
282 	unsigned long flags;
283 	int irq, dma1, dma2;
284 	static unsigned char irqs[16] =
285 		{0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
286 	static unsigned char dmas[8] =
287 		{6, 1, 0, 2, 0, 3, 4, 5};
288 
289 	if (snd_BUG_ON(!gus))
290 		return -EINVAL;
291 	card = gus->card;
292 	if (snd_BUG_ON(!card))
293 		return -EINVAL;
294 
295 	gus->mix_cntrl_reg &= 0xf8;
296 	gus->mix_cntrl_reg |= 0x01;	/* disable MIC, LINE IN, enable LINE OUT */
297 	if (gus->codec_flag || gus->ess_flag) {
298 		gus->mix_cntrl_reg &= ~1;	/* enable LINE IN */
299 		gus->mix_cntrl_reg |= 4;	/* enable MIC */
300 	}
301 	dma1 = gus->gf1.dma1;
302 	dma1 = abs(dma1);
303 	dma1 = dmas[dma1 & 7];
304 	dma2 = gus->gf1.dma2;
305 	dma2 = abs(dma2);
306 	dma2 = dmas[dma2 & 7];
307 	dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3);
308 
309 	if ((dma1 & 7) == 0 || (dma2 & 7) == 0) {
310 		snd_printk(KERN_ERR "Error! DMA isn't defined.\n");
311 		return -EINVAL;
312 	}
313 	irq = gus->gf1.irq;
314 	irq = abs(irq);
315 	irq = irqs[irq & 0x0f];
316 	if (irq == 0) {
317 		snd_printk(KERN_ERR "Error! IRQ isn't defined.\n");
318 		return -EINVAL;
319 	}
320 	irq |= 0x40;
321 #if 0
322 	card->mixer.mix_ctrl_reg |= 0x10;
323 #endif
324 
325 	spin_lock_irqsave(&gus->reg_lock, flags);
326 	outb(5, GUSP(gus, REGCNTRLS));
327 	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
328 	outb(0x00, GUSP(gus, IRQDMACNTRLREG));
329 	outb(0, GUSP(gus, REGCNTRLS));
330 	spin_unlock_irqrestore(&gus->reg_lock, flags);
331 
332 	udelay(100);
333 
334 	spin_lock_irqsave(&gus->reg_lock, flags);
335 	outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
336 	outb(dma1, GUSP(gus, IRQDMACNTRLREG));
337 	if (latches) {
338 		outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
339 		outb(irq, GUSP(gus, IRQDMACNTRLREG));
340 	}
341 	spin_unlock_irqrestore(&gus->reg_lock, flags);
342 
343 	udelay(100);
344 
345 	spin_lock_irqsave(&gus->reg_lock, flags);
346 	outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
347 	outb(dma1, GUSP(gus, IRQDMACNTRLREG));
348 	if (latches) {
349 		outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
350 		outb(irq, GUSP(gus, IRQDMACNTRLREG));
351 	}
352 	spin_unlock_irqrestore(&gus->reg_lock, flags);
353 
354 	snd_gf1_delay(gus);
355 
356 	if (latches)
357 		gus->mix_cntrl_reg |= 0x08;	/* enable latches */
358 	else
359 		gus->mix_cntrl_reg &= ~0x08;	/* disable latches */
360 	spin_lock_irqsave(&gus->reg_lock, flags);
361 	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
362 	outb(0, GUSP(gus, GF1PAGE));
363 	spin_unlock_irqrestore(&gus->reg_lock, flags);
364 
365 	return 0;
366 }
367 
368 static int snd_gus_check_version(struct snd_gus_card * gus)
369 {
370 	unsigned long flags;
371 	unsigned char val, rev;
372 	struct snd_card *card;
373 
374 	card = gus->card;
375 	spin_lock_irqsave(&gus->reg_lock, flags);
376 	outb(0x20, GUSP(gus, REGCNTRLS));
377 	val = inb(GUSP(gus, REGCNTRLS));
378 	rev = inb(GUSP(gus, BOARDVERSION));
379 	spin_unlock_irqrestore(&gus->reg_lock, flags);
380 	snd_printdd("GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev);
381 	strcpy(card->driver, "GUS");
382 	strcpy(card->longname, "Gravis UltraSound Classic (2.4)");
383 	if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) {
384 		if (rev >= 5 && rev <= 9) {
385 			gus->ics_flag = 1;
386 			if (rev == 5)
387 				gus->ics_flipped = 1;
388 			card->longname[27] = '3';
389 			card->longname[29] = rev == 5 ? '5' : '7';
390 		}
391 		if (rev >= 10 && rev != 255) {
392 			if (rev >= 10 && rev <= 11) {
393 				strcpy(card->driver, "GUS MAX");
394 				strcpy(card->longname, "Gravis UltraSound MAX");
395 				gus->max_flag = 1;
396 			} else if (rev == 0x30) {
397 				strcpy(card->driver, "GUS ACE");
398 				strcpy(card->longname, "Gravis UltraSound Ace");
399 				gus->ace_flag = 1;
400 			} else if (rev == 0x50) {
401 				strcpy(card->driver, "GUS Extreme");
402 				strcpy(card->longname, "Gravis UltraSound Extreme");
403 				gus->ess_flag = 1;
404 			} else {
405 				snd_printk(KERN_ERR "unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n", gus->gf1.port, rev, val);
406 				snd_printk(KERN_ERR "  please - report to <perex@perex.cz>\n");
407 			}
408 		}
409 	}
410 	strcpy(card->shortname, card->longname);
411 	gus->uart_enable = 1;	/* standard GUSes doesn't have midi uart trouble */
412 	snd_gus_init_control(gus);
413 	return 0;
414 }
415 
416 int snd_gus_initialize(struct snd_gus_card *gus)
417 {
418 	int err;
419 
420 	if (!gus->interwave) {
421 		if ((err = snd_gus_check_version(gus)) < 0) {
422 			snd_printk(KERN_ERR "version check failed\n");
423 			return err;
424 		}
425 		if ((err = snd_gus_detect_memory(gus)) < 0)
426 			return err;
427 	}
428 	if ((err = snd_gus_init_dma_irq(gus, 1)) < 0)
429 		return err;
430 	snd_gf1_start(gus);
431 	gus->initialized = 1;
432 	return 0;
433 }
434 
435   /* gus_io.c */
436 EXPORT_SYMBOL(snd_gf1_delay);
437 EXPORT_SYMBOL(snd_gf1_write8);
438 EXPORT_SYMBOL(snd_gf1_look8);
439 EXPORT_SYMBOL(snd_gf1_write16);
440 EXPORT_SYMBOL(snd_gf1_look16);
441 EXPORT_SYMBOL(snd_gf1_i_write8);
442 EXPORT_SYMBOL(snd_gf1_i_look8);
443 EXPORT_SYMBOL(snd_gf1_i_look16);
444 EXPORT_SYMBOL(snd_gf1_dram_addr);
445 EXPORT_SYMBOL(snd_gf1_write_addr);
446 EXPORT_SYMBOL(snd_gf1_poke);
447 EXPORT_SYMBOL(snd_gf1_peek);
448   /* gus_reset.c */
449 EXPORT_SYMBOL(snd_gf1_alloc_voice);
450 EXPORT_SYMBOL(snd_gf1_free_voice);
451 EXPORT_SYMBOL(snd_gf1_ctrl_stop);
452 EXPORT_SYMBOL(snd_gf1_stop_voice);
453   /* gus_mixer.c */
454 EXPORT_SYMBOL(snd_gf1_new_mixer);
455   /* gus_pcm.c */
456 EXPORT_SYMBOL(snd_gf1_pcm_new);
457   /* gus.c */
458 EXPORT_SYMBOL(snd_gus_use_inc);
459 EXPORT_SYMBOL(snd_gus_use_dec);
460 EXPORT_SYMBOL(snd_gus_create);
461 EXPORT_SYMBOL(snd_gus_initialize);
462   /* gus_irq.c */
463 EXPORT_SYMBOL(snd_gus_interrupt);
464   /* gus_uart.c */
465 EXPORT_SYMBOL(snd_gf1_rawmidi_new);
466   /* gus_dram.c */
467 EXPORT_SYMBOL(snd_gus_dram_write);
468 EXPORT_SYMBOL(snd_gus_dram_read);
469   /* gus_volume.c */
470 EXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw);
471 EXPORT_SYMBOL(snd_gf1_translate_freq);
472   /* gus_mem.c */
473 EXPORT_SYMBOL(snd_gf1_mem_alloc);
474 EXPORT_SYMBOL(snd_gf1_mem_xfree);
475 EXPORT_SYMBOL(snd_gf1_mem_free);
476 EXPORT_SYMBOL(snd_gf1_mem_lock);
477