xref: /kernel/linux/linux-5.10/sound/isa/gus/gus_main.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Routines for Gravis UltraSound soundcards
48c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/ioport.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <sound/core.h>
148c2ecf20Sopenharmony_ci#include <sound/gus.h>
158c2ecf20Sopenharmony_ci#include <sound/control.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/dma.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards");
218c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciint snd_gus_use_inc(struct snd_gus_card * gus)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	if (!try_module_get(gus->card->module))
288c2ecf20Sopenharmony_ci		return 0;
298c2ecf20Sopenharmony_ci	return 1;
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_civoid snd_gus_use_dec(struct snd_gus_card * gus)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	module_put(gus->card->module);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int snd_gus_joystick_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
408c2ecf20Sopenharmony_ci	uinfo->count = 1;
418c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
428c2ecf20Sopenharmony_ci	uinfo->value.integer.max = 31;
438c2ecf20Sopenharmony_ci	return 0;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int snd_gus_joystick_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = gus->joystick_dac & 31;
518c2ecf20Sopenharmony_ci	return 0;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int snd_gus_joystick_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
578c2ecf20Sopenharmony_ci	unsigned long flags;
588c2ecf20Sopenharmony_ci	int change;
598c2ecf20Sopenharmony_ci	unsigned char nval;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	nval = ucontrol->value.integer.value[0] & 31;
628c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
638c2ecf20Sopenharmony_ci	change = gus->joystick_dac != nval;
648c2ecf20Sopenharmony_ci	gus->joystick_dac = nval;
658c2ecf20Sopenharmony_ci	snd_gf1_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
668c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
678c2ecf20Sopenharmony_ci	return change;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_gus_joystick_control = {
718c2ecf20Sopenharmony_ci	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
728c2ecf20Sopenharmony_ci	.name = "Joystick Speed",
738c2ecf20Sopenharmony_ci	.info = snd_gus_joystick_info,
748c2ecf20Sopenharmony_ci	.get = snd_gus_joystick_get,
758c2ecf20Sopenharmony_ci	.put = snd_gus_joystick_put
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic void snd_gus_init_control(struct snd_gus_card *gus)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	if (!gus->ace_flag)
818c2ecf20Sopenharmony_ci		snd_ctl_add(gus->card, snd_ctl_new1(&snd_gus_joystick_control, gus));
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int snd_gus_free(struct snd_gus_card *gus)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	if (gus->gf1.res_port2 == NULL)
918c2ecf20Sopenharmony_ci		goto __hw_end;
928c2ecf20Sopenharmony_ci	snd_gf1_stop(gus);
938c2ecf20Sopenharmony_ci	snd_gus_init_dma_irq(gus, 0);
948c2ecf20Sopenharmony_ci      __hw_end:
958c2ecf20Sopenharmony_ci	release_and_free_resource(gus->gf1.res_port1);
968c2ecf20Sopenharmony_ci	release_and_free_resource(gus->gf1.res_port2);
978c2ecf20Sopenharmony_ci	if (gus->gf1.irq >= 0)
988c2ecf20Sopenharmony_ci		free_irq(gus->gf1.irq, (void *) gus);
998c2ecf20Sopenharmony_ci	if (gus->gf1.dma1 >= 0) {
1008c2ecf20Sopenharmony_ci		disable_dma(gus->gf1.dma1);
1018c2ecf20Sopenharmony_ci		free_dma(gus->gf1.dma1);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	if (!gus->equal_dma && gus->gf1.dma2 >= 0) {
1048c2ecf20Sopenharmony_ci		disable_dma(gus->gf1.dma2);
1058c2ecf20Sopenharmony_ci		free_dma(gus->gf1.dma2);
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci	kfree(gus);
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int snd_gus_dev_free(struct snd_device *device)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct snd_gus_card *gus = device->device_data;
1148c2ecf20Sopenharmony_ci	return snd_gus_free(gus);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciint snd_gus_create(struct snd_card *card,
1188c2ecf20Sopenharmony_ci		   unsigned long port,
1198c2ecf20Sopenharmony_ci		   int irq, int dma1, int dma2,
1208c2ecf20Sopenharmony_ci		   int timer_dev,
1218c2ecf20Sopenharmony_ci		   int voices,
1228c2ecf20Sopenharmony_ci		   int pcm_channels,
1238c2ecf20Sopenharmony_ci		   int effect,
1248c2ecf20Sopenharmony_ci		   struct snd_gus_card **rgus)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct snd_gus_card *gus;
1278c2ecf20Sopenharmony_ci	int err;
1288c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
1298c2ecf20Sopenharmony_ci		.dev_free =	snd_gus_dev_free,
1308c2ecf20Sopenharmony_ci	};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	*rgus = NULL;
1338c2ecf20Sopenharmony_ci	gus = kzalloc(sizeof(*gus), GFP_KERNEL);
1348c2ecf20Sopenharmony_ci	if (gus == NULL)
1358c2ecf20Sopenharmony_ci		return -ENOMEM;
1368c2ecf20Sopenharmony_ci	spin_lock_init(&gus->reg_lock);
1378c2ecf20Sopenharmony_ci	spin_lock_init(&gus->voice_alloc);
1388c2ecf20Sopenharmony_ci	spin_lock_init(&gus->active_voice_lock);
1398c2ecf20Sopenharmony_ci	spin_lock_init(&gus->event_lock);
1408c2ecf20Sopenharmony_ci	spin_lock_init(&gus->dma_lock);
1418c2ecf20Sopenharmony_ci	spin_lock_init(&gus->pcm_volume_level_lock);
1428c2ecf20Sopenharmony_ci	spin_lock_init(&gus->uart_cmd_lock);
1438c2ecf20Sopenharmony_ci	mutex_init(&gus->dma_mutex);
1448c2ecf20Sopenharmony_ci	gus->gf1.irq = -1;
1458c2ecf20Sopenharmony_ci	gus->gf1.dma1 = -1;
1468c2ecf20Sopenharmony_ci	gus->gf1.dma2 = -1;
1478c2ecf20Sopenharmony_ci	gus->card = card;
1488c2ecf20Sopenharmony_ci	gus->gf1.port = port;
1498c2ecf20Sopenharmony_ci	/* fill register variables for speedup */
1508c2ecf20Sopenharmony_ci	gus->gf1.reg_page = GUSP(gus, GF1PAGE);
1518c2ecf20Sopenharmony_ci	gus->gf1.reg_regsel = GUSP(gus, GF1REGSEL);
1528c2ecf20Sopenharmony_ci	gus->gf1.reg_data8 = GUSP(gus, GF1DATAHIGH);
1538c2ecf20Sopenharmony_ci	gus->gf1.reg_data16 = GUSP(gus, GF1DATALOW);
1548c2ecf20Sopenharmony_ci	gus->gf1.reg_irqstat = GUSP(gus, IRQSTAT);
1558c2ecf20Sopenharmony_ci	gus->gf1.reg_dram = GUSP(gus, DRAM);
1568c2ecf20Sopenharmony_ci	gus->gf1.reg_timerctrl = GUSP(gus, TIMERCNTRL);
1578c2ecf20Sopenharmony_ci	gus->gf1.reg_timerdata = GUSP(gus, TIMERDATA);
1588c2ecf20Sopenharmony_ci	/* allocate resources */
1598c2ecf20Sopenharmony_ci	if ((gus->gf1.res_port1 = request_region(port, 16, "GUS GF1 (Adlib/SB)")) == NULL) {
1608c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "gus: can't grab SB port 0x%lx\n", port);
1618c2ecf20Sopenharmony_ci		snd_gus_free(gus);
1628c2ecf20Sopenharmony_ci		return -EBUSY;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci	if ((gus->gf1.res_port2 = request_region(port + 0x100, 12, "GUS GF1 (Synth)")) == NULL) {
1658c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "gus: can't grab synth port 0x%lx\n", port + 0x100);
1668c2ecf20Sopenharmony_ci		snd_gus_free(gus);
1678c2ecf20Sopenharmony_ci		return -EBUSY;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci	if (irq >= 0 && request_irq(irq, snd_gus_interrupt, 0, "GUS GF1", (void *) gus)) {
1708c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "gus: can't grab irq %d\n", irq);
1718c2ecf20Sopenharmony_ci		snd_gus_free(gus);
1728c2ecf20Sopenharmony_ci		return -EBUSY;
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci	gus->gf1.irq = irq;
1758c2ecf20Sopenharmony_ci	card->sync_irq = irq;
1768c2ecf20Sopenharmony_ci	if (request_dma(dma1, "GUS - 1")) {
1778c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "gus: can't grab DMA1 %d\n", dma1);
1788c2ecf20Sopenharmony_ci		snd_gus_free(gus);
1798c2ecf20Sopenharmony_ci		return -EBUSY;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci	gus->gf1.dma1 = dma1;
1828c2ecf20Sopenharmony_ci	if (dma2 >= 0 && dma1 != dma2) {
1838c2ecf20Sopenharmony_ci		if (request_dma(dma2, "GUS - 2")) {
1848c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "gus: can't grab DMA2 %d\n", dma2);
1858c2ecf20Sopenharmony_ci			snd_gus_free(gus);
1868c2ecf20Sopenharmony_ci			return -EBUSY;
1878c2ecf20Sopenharmony_ci		}
1888c2ecf20Sopenharmony_ci		gus->gf1.dma2 = dma2;
1898c2ecf20Sopenharmony_ci	} else {
1908c2ecf20Sopenharmony_ci		gus->gf1.dma2 = gus->gf1.dma1;
1918c2ecf20Sopenharmony_ci		gus->equal_dma = 1;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci	gus->timer_dev = timer_dev;
1948c2ecf20Sopenharmony_ci	if (voices < 14)
1958c2ecf20Sopenharmony_ci		voices = 14;
1968c2ecf20Sopenharmony_ci	if (voices > 32)
1978c2ecf20Sopenharmony_ci		voices = 32;
1988c2ecf20Sopenharmony_ci	if (pcm_channels < 0)
1998c2ecf20Sopenharmony_ci		pcm_channels = 0;
2008c2ecf20Sopenharmony_ci	if (pcm_channels > 8)
2018c2ecf20Sopenharmony_ci		pcm_channels = 8;
2028c2ecf20Sopenharmony_ci	pcm_channels++;
2038c2ecf20Sopenharmony_ci	pcm_channels &= ~1;
2048c2ecf20Sopenharmony_ci	gus->gf1.effect = effect ? 1 : 0;
2058c2ecf20Sopenharmony_ci	gus->gf1.active_voices = voices;
2068c2ecf20Sopenharmony_ci	gus->gf1.pcm_channels = pcm_channels;
2078c2ecf20Sopenharmony_ci	gus->gf1.volume_ramp = 25;
2088c2ecf20Sopenharmony_ci	gus->gf1.smooth_pan = 1;
2098c2ecf20Sopenharmony_ci	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops)) < 0) {
2108c2ecf20Sopenharmony_ci		snd_gus_free(gus);
2118c2ecf20Sopenharmony_ci		return err;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci	*rgus = gus;
2148c2ecf20Sopenharmony_ci	return 0;
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/*
2188c2ecf20Sopenharmony_ci *  Memory detection routine for plain GF1 soundcards
2198c2ecf20Sopenharmony_ci */
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic int snd_gus_detect_memory(struct snd_gus_card * gus)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	int l, idx, local;
2248c2ecf20Sopenharmony_ci	unsigned char d;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	snd_gf1_poke(gus, 0L, 0xaa);
2278c2ecf20Sopenharmony_ci	snd_gf1_poke(gus, 1L, 0x55);
2288c2ecf20Sopenharmony_ci	if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) {
2298c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "plain GF1 card at 0x%lx without onboard DRAM?\n", gus->gf1.port);
2308c2ecf20Sopenharmony_ci		return -ENOMEM;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci	for (idx = 1, d = 0xab; idx < 4; idx++, d++) {
2338c2ecf20Sopenharmony_ci		local = idx << 18;
2348c2ecf20Sopenharmony_ci		snd_gf1_poke(gus, local, d);
2358c2ecf20Sopenharmony_ci		snd_gf1_poke(gus, local + 1, d + 1);
2368c2ecf20Sopenharmony_ci		if (snd_gf1_peek(gus, local) != d ||
2378c2ecf20Sopenharmony_ci		    snd_gf1_peek(gus, local + 1) != d + 1 ||
2388c2ecf20Sopenharmony_ci		    snd_gf1_peek(gus, 0L) != 0xaa)
2398c2ecf20Sopenharmony_ci			break;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci#if 1
2428c2ecf20Sopenharmony_ci	gus->gf1.memory = idx << 18;
2438c2ecf20Sopenharmony_ci#else
2448c2ecf20Sopenharmony_ci	gus->gf1.memory = 256 * 1024;
2458c2ecf20Sopenharmony_ci#endif
2468c2ecf20Sopenharmony_ci	for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) {
2478c2ecf20Sopenharmony_ci		gus->gf1.mem_alloc.banks_8[l].address =
2488c2ecf20Sopenharmony_ci		    gus->gf1.mem_alloc.banks_8[l].size = 0;
2498c2ecf20Sopenharmony_ci		gus->gf1.mem_alloc.banks_16[l].address = l << 18;
2508c2ecf20Sopenharmony_ci		gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0;
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci	gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory;
2538c2ecf20Sopenharmony_ci	return 0;		/* some memory were detected */
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	struct snd_card *card;
2598c2ecf20Sopenharmony_ci	unsigned long flags;
2608c2ecf20Sopenharmony_ci	int irq, dma1, dma2;
2618c2ecf20Sopenharmony_ci	static const unsigned char irqs[16] =
2628c2ecf20Sopenharmony_ci		{0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
2638c2ecf20Sopenharmony_ci	static const unsigned char dmas[8] =
2648c2ecf20Sopenharmony_ci		{6, 1, 0, 2, 0, 3, 4, 5};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!gus))
2678c2ecf20Sopenharmony_ci		return -EINVAL;
2688c2ecf20Sopenharmony_ci	card = gus->card;
2698c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!card))
2708c2ecf20Sopenharmony_ci		return -EINVAL;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	gus->mix_cntrl_reg &= 0xf8;
2738c2ecf20Sopenharmony_ci	gus->mix_cntrl_reg |= 0x01;	/* disable MIC, LINE IN, enable LINE OUT */
2748c2ecf20Sopenharmony_ci	if (gus->codec_flag || gus->ess_flag) {
2758c2ecf20Sopenharmony_ci		gus->mix_cntrl_reg &= ~1;	/* enable LINE IN */
2768c2ecf20Sopenharmony_ci		gus->mix_cntrl_reg |= 4;	/* enable MIC */
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci	dma1 = gus->gf1.dma1;
2798c2ecf20Sopenharmony_ci	dma1 = abs(dma1);
2808c2ecf20Sopenharmony_ci	dma1 = dmas[dma1 & 7];
2818c2ecf20Sopenharmony_ci	dma2 = gus->gf1.dma2;
2828c2ecf20Sopenharmony_ci	dma2 = abs(dma2);
2838c2ecf20Sopenharmony_ci	dma2 = dmas[dma2 & 7];
2848c2ecf20Sopenharmony_ci	dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if ((dma1 & 7) == 0 || (dma2 & 7) == 0) {
2878c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "Error! DMA isn't defined.\n");
2888c2ecf20Sopenharmony_ci		return -EINVAL;
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci	irq = gus->gf1.irq;
2918c2ecf20Sopenharmony_ci	irq = abs(irq);
2928c2ecf20Sopenharmony_ci	irq = irqs[irq & 0x0f];
2938c2ecf20Sopenharmony_ci	if (irq == 0) {
2948c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "Error! IRQ isn't defined.\n");
2958c2ecf20Sopenharmony_ci		return -EINVAL;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci	irq |= 0x40;
2988c2ecf20Sopenharmony_ci#if 0
2998c2ecf20Sopenharmony_ci	card->mixer.mix_ctrl_reg |= 0x10;
3008c2ecf20Sopenharmony_ci#endif
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
3038c2ecf20Sopenharmony_ci	outb(5, GUSP(gus, REGCNTRLS));
3048c2ecf20Sopenharmony_ci	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
3058c2ecf20Sopenharmony_ci	outb(0x00, GUSP(gus, IRQDMACNTRLREG));
3068c2ecf20Sopenharmony_ci	outb(0, GUSP(gus, REGCNTRLS));
3078c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	udelay(100);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
3128c2ecf20Sopenharmony_ci	outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
3138c2ecf20Sopenharmony_ci	outb(dma1, GUSP(gus, IRQDMACNTRLREG));
3148c2ecf20Sopenharmony_ci	if (latches) {
3158c2ecf20Sopenharmony_ci		outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
3168c2ecf20Sopenharmony_ci		outb(irq, GUSP(gus, IRQDMACNTRLREG));
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	udelay(100);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
3238c2ecf20Sopenharmony_ci	outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
3248c2ecf20Sopenharmony_ci	outb(dma1, GUSP(gus, IRQDMACNTRLREG));
3258c2ecf20Sopenharmony_ci	if (latches) {
3268c2ecf20Sopenharmony_ci		outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
3278c2ecf20Sopenharmony_ci		outb(irq, GUSP(gus, IRQDMACNTRLREG));
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	snd_gf1_delay(gus);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (latches)
3348c2ecf20Sopenharmony_ci		gus->mix_cntrl_reg |= 0x08;	/* enable latches */
3358c2ecf20Sopenharmony_ci	else
3368c2ecf20Sopenharmony_ci		gus->mix_cntrl_reg &= ~0x08;	/* disable latches */
3378c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
3388c2ecf20Sopenharmony_ci	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
3398c2ecf20Sopenharmony_ci	outb(0, GUSP(gus, GF1PAGE));
3408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	return 0;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic int snd_gus_check_version(struct snd_gus_card * gus)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	unsigned long flags;
3488c2ecf20Sopenharmony_ci	unsigned char val, rev;
3498c2ecf20Sopenharmony_ci	struct snd_card *card;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	card = gus->card;
3528c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
3538c2ecf20Sopenharmony_ci	outb(0x20, GUSP(gus, REGCNTRLS));
3548c2ecf20Sopenharmony_ci	val = inb(GUSP(gus, REGCNTRLS));
3558c2ecf20Sopenharmony_ci	rev = inb(GUSP(gus, BOARDVERSION));
3568c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
3578c2ecf20Sopenharmony_ci	snd_printdd("GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev);
3588c2ecf20Sopenharmony_ci	strcpy(card->driver, "GUS");
3598c2ecf20Sopenharmony_ci	strcpy(card->longname, "Gravis UltraSound Classic (2.4)");
3608c2ecf20Sopenharmony_ci	if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) {
3618c2ecf20Sopenharmony_ci		if (rev >= 5 && rev <= 9) {
3628c2ecf20Sopenharmony_ci			gus->ics_flag = 1;
3638c2ecf20Sopenharmony_ci			if (rev == 5)
3648c2ecf20Sopenharmony_ci				gus->ics_flipped = 1;
3658c2ecf20Sopenharmony_ci			card->longname[27] = '3';
3668c2ecf20Sopenharmony_ci			card->longname[29] = rev == 5 ? '5' : '7';
3678c2ecf20Sopenharmony_ci		}
3688c2ecf20Sopenharmony_ci		if (rev >= 10 && rev != 255) {
3698c2ecf20Sopenharmony_ci			if (rev >= 10 && rev <= 11) {
3708c2ecf20Sopenharmony_ci				strcpy(card->driver, "GUS MAX");
3718c2ecf20Sopenharmony_ci				strcpy(card->longname, "Gravis UltraSound MAX");
3728c2ecf20Sopenharmony_ci				gus->max_flag = 1;
3738c2ecf20Sopenharmony_ci			} else if (rev == 0x30) {
3748c2ecf20Sopenharmony_ci				strcpy(card->driver, "GUS ACE");
3758c2ecf20Sopenharmony_ci				strcpy(card->longname, "Gravis UltraSound Ace");
3768c2ecf20Sopenharmony_ci				gus->ace_flag = 1;
3778c2ecf20Sopenharmony_ci			} else if (rev == 0x50) {
3788c2ecf20Sopenharmony_ci				strcpy(card->driver, "GUS Extreme");
3798c2ecf20Sopenharmony_ci				strcpy(card->longname, "Gravis UltraSound Extreme");
3808c2ecf20Sopenharmony_ci				gus->ess_flag = 1;
3818c2ecf20Sopenharmony_ci			} else {
3828c2ecf20Sopenharmony_ci				snd_printk(KERN_ERR "unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n", gus->gf1.port, rev, val);
3838c2ecf20Sopenharmony_ci				snd_printk(KERN_ERR "  please - report to <perex@perex.cz>\n");
3848c2ecf20Sopenharmony_ci			}
3858c2ecf20Sopenharmony_ci		}
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci	strcpy(card->shortname, card->longname);
3888c2ecf20Sopenharmony_ci	gus->uart_enable = 1;	/* standard GUSes doesn't have midi uart trouble */
3898c2ecf20Sopenharmony_ci	snd_gus_init_control(gus);
3908c2ecf20Sopenharmony_ci	return 0;
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ciint snd_gus_initialize(struct snd_gus_card *gus)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	int err;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	if (!gus->interwave) {
3988c2ecf20Sopenharmony_ci		if ((err = snd_gus_check_version(gus)) < 0) {
3998c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "version check failed\n");
4008c2ecf20Sopenharmony_ci			return err;
4018c2ecf20Sopenharmony_ci		}
4028c2ecf20Sopenharmony_ci		if ((err = snd_gus_detect_memory(gus)) < 0)
4038c2ecf20Sopenharmony_ci			return err;
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	if ((err = snd_gus_init_dma_irq(gus, 1)) < 0)
4068c2ecf20Sopenharmony_ci		return err;
4078c2ecf20Sopenharmony_ci	snd_gf1_start(gus);
4088c2ecf20Sopenharmony_ci	gus->initialized = 1;
4098c2ecf20Sopenharmony_ci	return 0;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci  /* gus_io.c */
4138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_delay);
4148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_write8);
4158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_look8);
4168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_write16);
4178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_look16);
4188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_i_write8);
4198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_i_look8);
4208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_i_look16);
4218c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_dram_addr);
4228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_write_addr);
4238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_poke);
4248c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_peek);
4258c2ecf20Sopenharmony_ci  /* gus_reset.c */
4268c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_alloc_voice);
4278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_free_voice);
4288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_ctrl_stop);
4298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_stop_voice);
4308c2ecf20Sopenharmony_ci  /* gus_mixer.c */
4318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_new_mixer);
4328c2ecf20Sopenharmony_ci  /* gus_pcm.c */
4338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_pcm_new);
4348c2ecf20Sopenharmony_ci  /* gus.c */
4358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gus_use_inc);
4368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gus_use_dec);
4378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gus_create);
4388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gus_initialize);
4398c2ecf20Sopenharmony_ci  /* gus_irq.c */
4408c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gus_interrupt);
4418c2ecf20Sopenharmony_ci  /* gus_uart.c */
4428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_rawmidi_new);
4438c2ecf20Sopenharmony_ci  /* gus_dram.c */
4448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gus_dram_write);
4458c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gus_dram_read);
4468c2ecf20Sopenharmony_ci  /* gus_volume.c */
4478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw);
4488c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_translate_freq);
4498c2ecf20Sopenharmony_ci  /* gus_mem.c */
4508c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_mem_alloc);
4518c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_mem_xfree);
4528c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_mem_free);
4538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_gf1_mem_lock);
454