18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/delay.h>
78c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
88c2ecf20Sopenharmony_ci#include <linux/time.h>
98c2ecf20Sopenharmony_ci#include <sound/core.h>
108c2ecf20Sopenharmony_ci#include <sound/gus.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciextern int snd_gf1_synth_init(struct snd_gus_card * gus);
138c2ecf20Sopenharmony_ciextern void snd_gf1_synth_done(struct snd_gus_card * gus);
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci *  ok.. default interrupt handlers...
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic void snd_gf1_default_interrupt_handler_midi_out(struct snd_gus_card * gus)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x20);
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic void snd_gf1_default_interrupt_handler_midi_in(struct snd_gus_card * gus)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x80);
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic void snd_gf1_default_interrupt_handler_timer1(struct snd_gus_card * gus)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~4);
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic void snd_gf1_default_interrupt_handler_timer2(struct snd_gus_card * gus)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~8);
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic void snd_gf1_default_interrupt_handler_wave_and_volume(struct snd_gus_card * gus, struct snd_gus_voice * voice)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	snd_gf1_i_ctrl_stop(gus, 0x00);
428c2ecf20Sopenharmony_ci	snd_gf1_i_ctrl_stop(gus, 0x0d);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void snd_gf1_default_interrupt_handler_dma_write(struct snd_gus_card * gus)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, 0x41, 0x00);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic void snd_gf1_default_interrupt_handler_dma_read(struct snd_gus_card * gus)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, 0x49, 0x00);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_civoid snd_gf1_set_default_handlers(struct snd_gus_card * gus, unsigned int what)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	if (what & SNDRV_GF1_HANDLER_MIDI_OUT)
588c2ecf20Sopenharmony_ci		gus->gf1.interrupt_handler_midi_out = snd_gf1_default_interrupt_handler_midi_out;
598c2ecf20Sopenharmony_ci	if (what & SNDRV_GF1_HANDLER_MIDI_IN)
608c2ecf20Sopenharmony_ci		gus->gf1.interrupt_handler_midi_in = snd_gf1_default_interrupt_handler_midi_in;
618c2ecf20Sopenharmony_ci	if (what & SNDRV_GF1_HANDLER_TIMER1)
628c2ecf20Sopenharmony_ci		gus->gf1.interrupt_handler_timer1 = snd_gf1_default_interrupt_handler_timer1;
638c2ecf20Sopenharmony_ci	if (what & SNDRV_GF1_HANDLER_TIMER2)
648c2ecf20Sopenharmony_ci		gus->gf1.interrupt_handler_timer2 = snd_gf1_default_interrupt_handler_timer2;
658c2ecf20Sopenharmony_ci	if (what & SNDRV_GF1_HANDLER_VOICE) {
668c2ecf20Sopenharmony_ci		struct snd_gus_voice *voice;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci		voice = &gus->gf1.voices[what & 0xffff];
698c2ecf20Sopenharmony_ci		voice->handler_wave =
708c2ecf20Sopenharmony_ci		voice->handler_volume = snd_gf1_default_interrupt_handler_wave_and_volume;
718c2ecf20Sopenharmony_ci		voice->handler_effect = NULL;
728c2ecf20Sopenharmony_ci		voice->volume_change = NULL;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci	if (what & SNDRV_GF1_HANDLER_DMA_WRITE)
758c2ecf20Sopenharmony_ci		gus->gf1.interrupt_handler_dma_write = snd_gf1_default_interrupt_handler_dma_write;
768c2ecf20Sopenharmony_ci	if (what & SNDRV_GF1_HANDLER_DMA_READ)
778c2ecf20Sopenharmony_ci		gus->gf1.interrupt_handler_dma_read = snd_gf1_default_interrupt_handler_dma_read;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/*
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic void snd_gf1_clear_regs(struct snd_gus_card * gus)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	unsigned long flags;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
898c2ecf20Sopenharmony_ci	inb(GUSP(gus, IRQSTAT));
908c2ecf20Sopenharmony_ci	snd_gf1_write8(gus, 0x41, 0);	/* DRAM DMA Control Register */
918c2ecf20Sopenharmony_ci	snd_gf1_write8(gus, 0x45, 0);	/* Timer Control */
928c2ecf20Sopenharmony_ci	snd_gf1_write8(gus, 0x49, 0);	/* Sampling Control Register */
938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic void snd_gf1_look_regs(struct snd_gus_card * gus)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	unsigned long flags;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
1018c2ecf20Sopenharmony_ci	snd_gf1_look8(gus, 0x41);	/* DRAM DMA Control Register */
1028c2ecf20Sopenharmony_ci	snd_gf1_look8(gus, 0x49);	/* Sampling Control Register */
1038c2ecf20Sopenharmony_ci	inb(GUSP(gus, IRQSTAT));
1048c2ecf20Sopenharmony_ci	snd_gf1_read8(gus, 0x0f);	/* IRQ Source Register */
1058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/*
1098c2ecf20Sopenharmony_ci *  put selected GF1 voices to initial stage...
1108c2ecf20Sopenharmony_ci */
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_civoid snd_gf1_smart_stop_voice(struct snd_gus_card * gus, unsigned short voice)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	unsigned long flags;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
1178c2ecf20Sopenharmony_ci	snd_gf1_select_voice(gus, voice);
1188c2ecf20Sopenharmony_ci#if 0
1198c2ecf20Sopenharmony_ci	printk(KERN_DEBUG " -%i- smart stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME));
1208c2ecf20Sopenharmony_ci#endif
1218c2ecf20Sopenharmony_ci	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
1228c2ecf20Sopenharmony_ci	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
1238c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_civoid snd_gf1_stop_voice(struct snd_gus_card * gus, unsigned short voice)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	unsigned long flags;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
1318c2ecf20Sopenharmony_ci	snd_gf1_select_voice(gus, voice);
1328c2ecf20Sopenharmony_ci#if 0
1338c2ecf20Sopenharmony_ci	printk(KERN_DEBUG " -%i- stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME));
1348c2ecf20Sopenharmony_ci#endif
1358c2ecf20Sopenharmony_ci	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
1368c2ecf20Sopenharmony_ci	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
1378c2ecf20Sopenharmony_ci	if (gus->gf1.enh_mode)
1388c2ecf20Sopenharmony_ci		snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0);
1398c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
1408c2ecf20Sopenharmony_ci#if 0
1418c2ecf20Sopenharmony_ci	snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_VIBRATO);
1428c2ecf20Sopenharmony_ci	snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_TREMOLO);
1438c2ecf20Sopenharmony_ci#endif
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic void snd_gf1_clear_voices(struct snd_gus_card * gus, unsigned short v_min,
1478c2ecf20Sopenharmony_ci				 unsigned short v_max)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	unsigned long flags;
1508c2ecf20Sopenharmony_ci	unsigned int daddr;
1518c2ecf20Sopenharmony_ci	unsigned short i, w_16;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	daddr = gus->gf1.default_voice_address << 4;
1548c2ecf20Sopenharmony_ci	for (i = v_min; i <= v_max; i++) {
1558c2ecf20Sopenharmony_ci#if 0
1568c2ecf20Sopenharmony_ci		if (gus->gf1.syn_voices)
1578c2ecf20Sopenharmony_ci			gus->gf1.syn_voices[i].flags = ~VFLG_DYNAMIC;
1588c2ecf20Sopenharmony_ci#endif
1598c2ecf20Sopenharmony_ci		spin_lock_irqsave(&gus->reg_lock, flags);
1608c2ecf20Sopenharmony_ci		snd_gf1_select_voice(gus, i);
1618c2ecf20Sopenharmony_ci		snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);	/* Voice Control Register = voice stop */
1628c2ecf20Sopenharmony_ci		snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);	/* Volume Ramp Control Register = ramp off */
1638c2ecf20Sopenharmony_ci		if (gus->gf1.enh_mode)
1648c2ecf20Sopenharmony_ci			snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, gus->gf1.memory ? 0x02 : 0x82);	/* Deactivate voice */
1658c2ecf20Sopenharmony_ci		w_16 = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & 0x04;
1668c2ecf20Sopenharmony_ci		snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, 0x400);
1678c2ecf20Sopenharmony_ci		snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, daddr, w_16);
1688c2ecf20Sopenharmony_ci		snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, daddr, w_16);
1698c2ecf20Sopenharmony_ci		snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, 0);
1708c2ecf20Sopenharmony_ci		snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, 0);
1718c2ecf20Sopenharmony_ci		snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0);
1728c2ecf20Sopenharmony_ci		snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, 0);
1738c2ecf20Sopenharmony_ci		snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, daddr, w_16);
1748c2ecf20Sopenharmony_ci		snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, 7);
1758c2ecf20Sopenharmony_ci		if (gus->gf1.enh_mode) {
1768c2ecf20Sopenharmony_ci			snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0);
1778c2ecf20Sopenharmony_ci			snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME, 0);
1788c2ecf20Sopenharmony_ci			snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME_FINAL, 0);
1798c2ecf20Sopenharmony_ci		}
1808c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&gus->reg_lock, flags);
1818c2ecf20Sopenharmony_ci#if 0
1828c2ecf20Sopenharmony_ci		snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_VIBRATO);
1838c2ecf20Sopenharmony_ci		snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_TREMOLO);
1848c2ecf20Sopenharmony_ci#endif
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_civoid snd_gf1_stop_voices(struct snd_gus_card * gus, unsigned short v_min, unsigned short v_max)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	unsigned long flags;
1918c2ecf20Sopenharmony_ci	short i, ramp_ok;
1928c2ecf20Sopenharmony_ci	unsigned short ramp_end;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (!in_interrupt()) {	/* this can't be done in interrupt */
1958c2ecf20Sopenharmony_ci		for (i = v_min, ramp_ok = 0; i <= v_max; i++) {
1968c2ecf20Sopenharmony_ci			spin_lock_irqsave(&gus->reg_lock, flags);
1978c2ecf20Sopenharmony_ci			snd_gf1_select_voice(gus, i);
1988c2ecf20Sopenharmony_ci			ramp_end = snd_gf1_read16(gus, 9) >> 8;
1998c2ecf20Sopenharmony_ci			if (ramp_end > SNDRV_GF1_MIN_OFFSET) {
2008c2ecf20Sopenharmony_ci				ramp_ok++;
2018c2ecf20Sopenharmony_ci				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 20);	/* ramp rate */
2028c2ecf20Sopenharmony_ci				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET);	/* ramp start */
2038c2ecf20Sopenharmony_ci				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, ramp_end);	/* ramp end */
2048c2ecf20Sopenharmony_ci				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40);	/* ramp down */
2058c2ecf20Sopenharmony_ci				if (gus->gf1.enh_mode) {
2068c2ecf20Sopenharmony_ci					snd_gf1_delay(gus);
2078c2ecf20Sopenharmony_ci					snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40);
2088c2ecf20Sopenharmony_ci				}
2098c2ecf20Sopenharmony_ci			}
2108c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&gus->reg_lock, flags);
2118c2ecf20Sopenharmony_ci		}
2128c2ecf20Sopenharmony_ci		msleep_interruptible(50);
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci	snd_gf1_clear_voices(gus, v_min, v_max);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic void snd_gf1_alloc_voice_use(struct snd_gus_card * gus,
2188c2ecf20Sopenharmony_ci				    struct snd_gus_voice * pvoice,
2198c2ecf20Sopenharmony_ci				    int type, int client, int port)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	pvoice->use = 1;
2228c2ecf20Sopenharmony_ci	switch (type) {
2238c2ecf20Sopenharmony_ci	case SNDRV_GF1_VOICE_TYPE_PCM:
2248c2ecf20Sopenharmony_ci		gus->gf1.pcm_alloc_voices++;
2258c2ecf20Sopenharmony_ci		pvoice->pcm = 1;
2268c2ecf20Sopenharmony_ci		break;
2278c2ecf20Sopenharmony_ci	case SNDRV_GF1_VOICE_TYPE_SYNTH:
2288c2ecf20Sopenharmony_ci		pvoice->synth = 1;
2298c2ecf20Sopenharmony_ci		pvoice->client = client;
2308c2ecf20Sopenharmony_ci		pvoice->port = port;
2318c2ecf20Sopenharmony_ci		break;
2328c2ecf20Sopenharmony_ci	case SNDRV_GF1_VOICE_TYPE_MIDI:
2338c2ecf20Sopenharmony_ci		pvoice->midi = 1;
2348c2ecf20Sopenharmony_ci		pvoice->client = client;
2358c2ecf20Sopenharmony_ci		pvoice->port = port;
2368c2ecf20Sopenharmony_ci		break;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistruct snd_gus_voice *snd_gf1_alloc_voice(struct snd_gus_card * gus, int type, int client, int port)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	struct snd_gus_voice *pvoice;
2438c2ecf20Sopenharmony_ci	unsigned long flags;
2448c2ecf20Sopenharmony_ci	int idx;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->voice_alloc, flags);
2478c2ecf20Sopenharmony_ci	if (type == SNDRV_GF1_VOICE_TYPE_PCM) {
2488c2ecf20Sopenharmony_ci		if (gus->gf1.pcm_alloc_voices >= gus->gf1.pcm_channels) {
2498c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&gus->voice_alloc, flags);
2508c2ecf20Sopenharmony_ci			return NULL;
2518c2ecf20Sopenharmony_ci		}
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci	for (idx = 0; idx < 32; idx++) {
2548c2ecf20Sopenharmony_ci		pvoice = &gus->gf1.voices[idx];
2558c2ecf20Sopenharmony_ci		if (!pvoice->use) {
2568c2ecf20Sopenharmony_ci			snd_gf1_alloc_voice_use(gus, pvoice, type, client, port);
2578c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&gus->voice_alloc, flags);
2588c2ecf20Sopenharmony_ci			return pvoice;
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci	for (idx = 0; idx < 32; idx++) {
2628c2ecf20Sopenharmony_ci		pvoice = &gus->gf1.voices[idx];
2638c2ecf20Sopenharmony_ci		if (pvoice->midi && !pvoice->client) {
2648c2ecf20Sopenharmony_ci			snd_gf1_clear_voices(gus, pvoice->number, pvoice->number);
2658c2ecf20Sopenharmony_ci			snd_gf1_alloc_voice_use(gus, pvoice, type, client, port);
2668c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&gus->voice_alloc, flags);
2678c2ecf20Sopenharmony_ci			return pvoice;
2688c2ecf20Sopenharmony_ci		}
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->voice_alloc, flags);
2718c2ecf20Sopenharmony_ci	return NULL;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_civoid snd_gf1_free_voice(struct snd_gus_card * gus, struct snd_gus_voice *voice)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	unsigned long flags;
2778c2ecf20Sopenharmony_ci	void (*private_free)(struct snd_gus_voice *voice);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (voice == NULL || !voice->use)
2808c2ecf20Sopenharmony_ci		return;
2818c2ecf20Sopenharmony_ci	snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | voice->number);
2828c2ecf20Sopenharmony_ci	snd_gf1_clear_voices(gus, voice->number, voice->number);
2838c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->voice_alloc, flags);
2848c2ecf20Sopenharmony_ci	private_free = voice->private_free;
2858c2ecf20Sopenharmony_ci	voice->private_free = NULL;
2868c2ecf20Sopenharmony_ci	voice->private_data = NULL;
2878c2ecf20Sopenharmony_ci	if (voice->pcm)
2888c2ecf20Sopenharmony_ci		gus->gf1.pcm_alloc_voices--;
2898c2ecf20Sopenharmony_ci	voice->use = voice->pcm = 0;
2908c2ecf20Sopenharmony_ci	voice->sample_ops = NULL;
2918c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->voice_alloc, flags);
2928c2ecf20Sopenharmony_ci	if (private_free)
2938c2ecf20Sopenharmony_ci		private_free(voice);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci/*
2978c2ecf20Sopenharmony_ci *  call this function only by start of driver
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ciint snd_gf1_start(struct snd_gus_card * gus)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	unsigned long flags;
3038c2ecf20Sopenharmony_ci	unsigned int i;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);	/* reset GF1 */
3068c2ecf20Sopenharmony_ci	udelay(160);
3078c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);	/* disable IRQ & DAC */
3088c2ecf20Sopenharmony_ci	udelay(160);
3098c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_ALL);
3128c2ecf20Sopenharmony_ci	for (i = 0; i < 32; i++) {
3138c2ecf20Sopenharmony_ci		gus->gf1.voices[i].number = i;
3148c2ecf20Sopenharmony_ci		snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | i);
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	snd_gf1_uart_cmd(gus, 0x03);	/* huh.. this cleanup took me some time... */
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	if (gus->gf1.enh_mode) {	/* enhanced mode !!!! */
3208c2ecf20Sopenharmony_ci		snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);
3218c2ecf20Sopenharmony_ci		snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01);
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci	snd_gf1_clear_regs(gus);
3248c2ecf20Sopenharmony_ci	snd_gf1_select_active_voices(gus);
3258c2ecf20Sopenharmony_ci	snd_gf1_delay(gus);
3268c2ecf20Sopenharmony_ci	gus->gf1.default_voice_address = gus->gf1.memory > 0 ? 0 : 512 - 8;
3278c2ecf20Sopenharmony_ci	/* initialize LFOs & clear LFOs memory */
3288c2ecf20Sopenharmony_ci	if (gus->gf1.enh_mode && gus->gf1.memory) {
3298c2ecf20Sopenharmony_ci		gus->gf1.hw_lfo = 1;
3308c2ecf20Sopenharmony_ci		gus->gf1.default_voice_address += 1024;
3318c2ecf20Sopenharmony_ci	} else {
3328c2ecf20Sopenharmony_ci		gus->gf1.sw_lfo = 1;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci#if 0
3358c2ecf20Sopenharmony_ci	snd_gf1_lfo_init(gus);
3368c2ecf20Sopenharmony_ci#endif
3378c2ecf20Sopenharmony_ci	if (gus->gf1.memory > 0)
3388c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++)
3398c2ecf20Sopenharmony_ci			snd_gf1_poke(gus, gus->gf1.default_voice_address + i, 0);
3408c2ecf20Sopenharmony_ci	snd_gf1_clear_regs(gus);
3418c2ecf20Sopenharmony_ci	snd_gf1_clear_voices(gus, 0, 31);
3428c2ecf20Sopenharmony_ci	snd_gf1_look_regs(gus);
3438c2ecf20Sopenharmony_ci	udelay(160);
3448c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7);	/* Reset Register = IRQ enable, DAC enable */
3458c2ecf20Sopenharmony_ci	udelay(160);
3468c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7);	/* Reset Register = IRQ enable, DAC enable */
3478c2ecf20Sopenharmony_ci	if (gus->gf1.enh_mode) {	/* enhanced mode !!!! */
3488c2ecf20Sopenharmony_ci		snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);
3498c2ecf20Sopenharmony_ci		snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01);
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci	while ((snd_gf1_i_read8(gus, SNDRV_GF1_GB_VOICES_IRQ) & 0xc0) != 0xc0);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
3548c2ecf20Sopenharmony_ci	outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE));
3558c2ecf20Sopenharmony_ci	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
3568c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	snd_gf1_timers_init(gus);
3598c2ecf20Sopenharmony_ci	snd_gf1_look_regs(gus);
3608c2ecf20Sopenharmony_ci	snd_gf1_mem_init(gus);
3618c2ecf20Sopenharmony_ci	snd_gf1_mem_proc_init(gus);
3628c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_DEBUG
3638c2ecf20Sopenharmony_ci	snd_gus_irq_profile_init(gus);
3648c2ecf20Sopenharmony_ci#endif
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci#if 0
3678c2ecf20Sopenharmony_ci	if (gus->pnp_flag) {
3688c2ecf20Sopenharmony_ci		if (gus->chip.playback_fifo_size > 0)
3698c2ecf20Sopenharmony_ci			snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_RECORD_BASE_ADDR, gus->chip.playback_fifo_block->ptr >> 8);
3708c2ecf20Sopenharmony_ci		if (gus->chip.record_fifo_size > 0)
3718c2ecf20Sopenharmony_ci			snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_PLAY_BASE_ADDR, gus->chip.record_fifo_block->ptr >> 8);
3728c2ecf20Sopenharmony_ci		snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_SIZE, gus->chip.interwave_fifo_reg);
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci#endif
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return 0;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci/*
3808c2ecf20Sopenharmony_ci *  call this function only by shutdown of driver
3818c2ecf20Sopenharmony_ci */
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ciint snd_gf1_stop(struct snd_gus_card * gus)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, 0); /* stop all timers */
3868c2ecf20Sopenharmony_ci	snd_gf1_stop_voices(gus, 0, 31);		/* stop all voices */
3878c2ecf20Sopenharmony_ci	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);	/* disable IRQ & DAC */
3888c2ecf20Sopenharmony_ci	snd_gf1_timers_done(gus);
3898c2ecf20Sopenharmony_ci	snd_gf1_mem_done(gus);
3908c2ecf20Sopenharmony_ci#if 0
3918c2ecf20Sopenharmony_ci	snd_gf1_lfo_done(gus);
3928c2ecf20Sopenharmony_ci#endif
3938c2ecf20Sopenharmony_ci	return 0;
3948c2ecf20Sopenharmony_ci}
395