18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) by Uros Bizjak <uros@kss-loka.si>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Midi synth routines for OPL2/OPL3/OPL4 FM
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#undef DEBUG_ALLOC
98c2ecf20Sopenharmony_ci#undef DEBUG_MIDI
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "opl3_voice.h"
128c2ecf20Sopenharmony_ci#include <sound/asoundef.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic void snd_opl3_note_off_unsafe(void *p, int note, int vel,
158c2ecf20Sopenharmony_ci				     struct snd_midi_channel *chan);
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * The next table looks magical, but it certainly is not. Its values have
188c2ecf20Sopenharmony_ci * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
198c2ecf20Sopenharmony_ci * for i=0. This log-table converts a linear volume-scaling (0..127) to a
208c2ecf20Sopenharmony_ci * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
218c2ecf20Sopenharmony_ci * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
228c2ecf20Sopenharmony_ci * volume -8 it was implemented as a table because it is only 128 bytes and
238c2ecf20Sopenharmony_ci * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic const char opl3_volume_table[128] =
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	-63, -48, -40, -35, -32, -29, -27, -26,
298c2ecf20Sopenharmony_ci	-24, -23, -21, -20, -19, -18, -18, -17,
308c2ecf20Sopenharmony_ci	-16, -15, -15, -14, -13, -13, -12, -12,
318c2ecf20Sopenharmony_ci	-11, -11, -10, -10, -10, -9, -9, -8,
328c2ecf20Sopenharmony_ci	-8, -8, -7, -7, -7, -6, -6, -6,
338c2ecf20Sopenharmony_ci	-5, -5, -5, -5, -4, -4, -4, -4,
348c2ecf20Sopenharmony_ci	-3, -3, -3, -3, -2, -2, -2, -2,
358c2ecf20Sopenharmony_ci	-2, -1, -1, -1, -1, 0, 0, 0,
368c2ecf20Sopenharmony_ci	0, 0, 0, 1, 1, 1, 1, 1,
378c2ecf20Sopenharmony_ci	1, 2, 2, 2, 2, 2, 2, 2,
388c2ecf20Sopenharmony_ci	3, 3, 3, 3, 3, 3, 3, 4,
398c2ecf20Sopenharmony_ci	4, 4, 4, 4, 4, 4, 4, 5,
408c2ecf20Sopenharmony_ci	5, 5, 5, 5, 5, 5, 5, 5,
418c2ecf20Sopenharmony_ci	6, 6, 6, 6, 6, 6, 6, 6,
428c2ecf20Sopenharmony_ci	6, 7, 7, 7, 7, 7, 7, 7,
438c2ecf20Sopenharmony_ci	7, 7, 7, 8, 8, 8, 8, 8
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_civoid snd_opl3_calc_volume(unsigned char *volbyte, int vel,
478c2ecf20Sopenharmony_ci			  struct snd_midi_channel *chan)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	int oldvol, newvol, n;
508c2ecf20Sopenharmony_ci	int volume;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
538c2ecf20Sopenharmony_ci	if (volume > 127)
548c2ecf20Sopenharmony_ci		volume = 127;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	newvol = opl3_volume_table[volume] + oldvol;
598c2ecf20Sopenharmony_ci	if (newvol > OPL3_TOTAL_LEVEL_MASK)
608c2ecf20Sopenharmony_ci		newvol = OPL3_TOTAL_LEVEL_MASK;
618c2ecf20Sopenharmony_ci	else if (newvol < 0)
628c2ecf20Sopenharmony_ci		newvol = 0;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	*volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*
708c2ecf20Sopenharmony_ci * Converts the note frequency to block and fnum values for the FM chip
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_cistatic const short opl3_note_table[16] =
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	305, 323,	/* for pitch bending, -2 semitones */
758c2ecf20Sopenharmony_ci	343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
768c2ecf20Sopenharmony_ci	686, 726	/* for pitch bending, +2 semitones */
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
808c2ecf20Sopenharmony_ci				int note, struct snd_midi_channel *chan)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	int block = ((note / 12) & 0x07) - 1;
838c2ecf20Sopenharmony_ci	int idx = (note % 12) + 2;
848c2ecf20Sopenharmony_ci	int freq;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (chan->midi_pitchbend) {
878c2ecf20Sopenharmony_ci		int pitchbend = chan->midi_pitchbend;
888c2ecf20Sopenharmony_ci		int segment;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci		if (pitchbend < -0x2000)
918c2ecf20Sopenharmony_ci			pitchbend = -0x2000;
928c2ecf20Sopenharmony_ci		if (pitchbend > 0x1FFF)
938c2ecf20Sopenharmony_ci			pitchbend = 0x1FFF;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		segment = pitchbend / 0x1000;
968c2ecf20Sopenharmony_ci		freq = opl3_note_table[idx+segment];
978c2ecf20Sopenharmony_ci		freq += ((opl3_note_table[idx+segment+1] - freq) *
988c2ecf20Sopenharmony_ci			 (pitchbend % 0x1000)) / 0x1000;
998c2ecf20Sopenharmony_ci	} else {
1008c2ecf20Sopenharmony_ci		freq = opl3_note_table[idx];
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	*fnum = (unsigned char) freq;
1048c2ecf20Sopenharmony_ci	*blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
1058c2ecf20Sopenharmony_ci		((block << 2) & OPL3_BLOCKNUM_MASK);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#ifdef DEBUG_ALLOC
1108c2ecf20Sopenharmony_cistatic void debug_alloc(struct snd_opl3 *opl3, char *s, int voice) {
1118c2ecf20Sopenharmony_ci	int i;
1128c2ecf20Sopenharmony_ci	char *str = "x.24";
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "time %.5i: %s [%.2i]: ", opl3->use_time, s, voice);
1158c2ecf20Sopenharmony_ci	for (i = 0; i < opl3->max_voices; i++)
1168c2ecf20Sopenharmony_ci		printk(KERN_CONT "%c", *(str + opl3->voices[i].state + 1));
1178c2ecf20Sopenharmony_ci	printk(KERN_CONT "\n");
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci#endif
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/*
1228c2ecf20Sopenharmony_ci * Get a FM voice (channel) to play a note on.
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_cistatic int opl3_get_voice(struct snd_opl3 *opl3, int instr_4op,
1258c2ecf20Sopenharmony_ci			  struct snd_midi_channel *chan) {
1268c2ecf20Sopenharmony_ci	int chan_4op_1;		/* first voice for 4op instrument */
1278c2ecf20Sopenharmony_ci	int chan_4op_2;		/* second voice for 4op instrument */
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	struct snd_opl3_voice *vp, *vp2;
1308c2ecf20Sopenharmony_ci	unsigned int voice_time;
1318c2ecf20Sopenharmony_ci	int i;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#ifdef DEBUG_ALLOC
1348c2ecf20Sopenharmony_ci	char *alloc_type[3] = { "FREE     ", "CHEAP    ", "EXPENSIVE" };
1358c2ecf20Sopenharmony_ci#endif
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/* This is our "allocation cost" table */
1388c2ecf20Sopenharmony_ci	enum {
1398c2ecf20Sopenharmony_ci		FREE = 0, CHEAP, EXPENSIVE, END
1408c2ecf20Sopenharmony_ci	};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* Keeps track of what we are finding */
1438c2ecf20Sopenharmony_ci	struct best {
1448c2ecf20Sopenharmony_ci		unsigned int time;
1458c2ecf20Sopenharmony_ci		int voice;
1468c2ecf20Sopenharmony_ci	} best[END];
1478c2ecf20Sopenharmony_ci	struct best *bp;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	for (i = 0; i < END; i++) {
1508c2ecf20Sopenharmony_ci		best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */
1518c2ecf20Sopenharmony_ci		best[i].voice = -1;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* Look through all the channels for the most suitable. */
1558c2ecf20Sopenharmony_ci	for (i = 0; i < opl3->max_voices; i++) {
1568c2ecf20Sopenharmony_ci		vp = &opl3->voices[i];
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL)
1598c2ecf20Sopenharmony_ci		  /* skip unavailable channels, allocated by
1608c2ecf20Sopenharmony_ci		     drum voices or by bounded 4op voices) */
1618c2ecf20Sopenharmony_ci			continue;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		voice_time = vp->time;
1648c2ecf20Sopenharmony_ci		bp = best;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci		chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
1678c2ecf20Sopenharmony_ci		chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
1688c2ecf20Sopenharmony_ci		if (instr_4op) {
1698c2ecf20Sopenharmony_ci			/* allocate 4op voice */
1708c2ecf20Sopenharmony_ci			/* skip channels unavailable to 4op instrument */
1718c2ecf20Sopenharmony_ci			if (!chan_4op_1)
1728c2ecf20Sopenharmony_ci				continue;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci			if (vp->state)
1758c2ecf20Sopenharmony_ci				/* kill one voice, CHEAP */
1768c2ecf20Sopenharmony_ci				bp++;
1778c2ecf20Sopenharmony_ci			/* get state of bounded 2op channel
1788c2ecf20Sopenharmony_ci			   to be allocated for 4op instrument */
1798c2ecf20Sopenharmony_ci			vp2 = &opl3->voices[i + 3];
1808c2ecf20Sopenharmony_ci			if (vp2->state == SNDRV_OPL3_ST_ON_2OP) {
1818c2ecf20Sopenharmony_ci				/* kill two voices, EXPENSIVE */
1828c2ecf20Sopenharmony_ci				bp++;
1838c2ecf20Sopenharmony_ci				voice_time = (voice_time > vp->time) ?
1848c2ecf20Sopenharmony_ci					voice_time : vp->time;
1858c2ecf20Sopenharmony_ci			}
1868c2ecf20Sopenharmony_ci		} else {
1878c2ecf20Sopenharmony_ci			/* allocate 2op voice */
1888c2ecf20Sopenharmony_ci			if ((chan_4op_1) || (chan_4op_2))
1898c2ecf20Sopenharmony_ci				/* use bounded channels for 2op, CHEAP */
1908c2ecf20Sopenharmony_ci				bp++;
1918c2ecf20Sopenharmony_ci			else if (vp->state)
1928c2ecf20Sopenharmony_ci				/* kill one voice on 2op channel, CHEAP */
1938c2ecf20Sopenharmony_ci				bp++;
1948c2ecf20Sopenharmony_ci			/* raise kill cost to EXPENSIVE for all channels */
1958c2ecf20Sopenharmony_ci			if (vp->state)
1968c2ecf20Sopenharmony_ci				bp++;
1978c2ecf20Sopenharmony_ci		}
1988c2ecf20Sopenharmony_ci		if (voice_time < bp->time) {
1998c2ecf20Sopenharmony_ci			bp->time = voice_time;
2008c2ecf20Sopenharmony_ci			bp->voice = i;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	for (i = 0; i < END; i++) {
2058c2ecf20Sopenharmony_ci		if (best[i].voice >= 0) {
2068c2ecf20Sopenharmony_ci#ifdef DEBUG_ALLOC
2078c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "%s %iop allocation on voice %i\n",
2088c2ecf20Sopenharmony_ci			       alloc_type[i], instr_4op ? 4 : 2,
2098c2ecf20Sopenharmony_ci			       best[i].voice);
2108c2ecf20Sopenharmony_ci#endif
2118c2ecf20Sopenharmony_ci			return best[i].voice;
2128c2ecf20Sopenharmony_ci		}
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci	/* not found */
2158c2ecf20Sopenharmony_ci	return -1;
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/* ------------------------------ */
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/*
2218c2ecf20Sopenharmony_ci * System timer interrupt function
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_civoid snd_opl3_timer_func(struct timer_list *t)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	struct snd_opl3 *opl3 = from_timer(opl3, t, tlist);
2278c2ecf20Sopenharmony_ci	unsigned long flags;
2288c2ecf20Sopenharmony_ci	int again = 0;
2298c2ecf20Sopenharmony_ci	int i;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&opl3->voice_lock, flags);
2328c2ecf20Sopenharmony_ci	for (i = 0; i < opl3->max_voices; i++) {
2338c2ecf20Sopenharmony_ci		struct snd_opl3_voice *vp = &opl3->voices[i];
2348c2ecf20Sopenharmony_ci		if (vp->state > 0 && vp->note_off_check) {
2358c2ecf20Sopenharmony_ci			if (vp->note_off == jiffies)
2368c2ecf20Sopenharmony_ci				snd_opl3_note_off_unsafe(opl3, vp->note, 0,
2378c2ecf20Sopenharmony_ci							 vp->chan);
2388c2ecf20Sopenharmony_ci			else
2398c2ecf20Sopenharmony_ci				again++;
2408c2ecf20Sopenharmony_ci		}
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&opl3->voice_lock, flags);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	spin_lock_irqsave(&opl3->sys_timer_lock, flags);
2458c2ecf20Sopenharmony_ci	if (again)
2468c2ecf20Sopenharmony_ci		mod_timer(&opl3->tlist, jiffies + 1);	/* invoke again */
2478c2ecf20Sopenharmony_ci	else
2488c2ecf20Sopenharmony_ci		opl3->sys_timer_status = 0;
2498c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/*
2538c2ecf20Sopenharmony_ci * Start system timer
2548c2ecf20Sopenharmony_ci */
2558c2ecf20Sopenharmony_cistatic void snd_opl3_start_timer(struct snd_opl3 *opl3)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	unsigned long flags;
2588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&opl3->sys_timer_lock, flags);
2598c2ecf20Sopenharmony_ci	if (! opl3->sys_timer_status) {
2608c2ecf20Sopenharmony_ci		mod_timer(&opl3->tlist, jiffies + 1);
2618c2ecf20Sopenharmony_ci		opl3->sys_timer_status = 1;
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci/* ------------------------------ */
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic const int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
2708c2ecf20Sopenharmony_ci	0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
2718c2ecf20Sopenharmony_ci};
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci/*
2748c2ecf20Sopenharmony_ci * Start a note.
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_civoid snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct snd_opl3 *opl3;
2798c2ecf20Sopenharmony_ci	int instr_4op;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	int voice;
2828c2ecf20Sopenharmony_ci	struct snd_opl3_voice *vp, *vp2;
2838c2ecf20Sopenharmony_ci	unsigned short connect_mask;
2848c2ecf20Sopenharmony_ci	unsigned char connection;
2858c2ecf20Sopenharmony_ci	unsigned char vol_op[4];
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	int extra_prg = 0;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	unsigned short reg_side;
2908c2ecf20Sopenharmony_ci	unsigned char op_offset;
2918c2ecf20Sopenharmony_ci	unsigned char voice_offset;
2928c2ecf20Sopenharmony_ci	unsigned short opl3_reg;
2938c2ecf20Sopenharmony_ci	unsigned char reg_val;
2948c2ecf20Sopenharmony_ci	unsigned char prg, bank;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	int key = note;
2978c2ecf20Sopenharmony_ci	unsigned char fnum, blocknum;
2988c2ecf20Sopenharmony_ci	int i;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	struct fm_patch *patch;
3018c2ecf20Sopenharmony_ci	struct fm_instrument *fm;
3028c2ecf20Sopenharmony_ci	unsigned long flags;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	opl3 = p;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
3078c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i\n",
3088c2ecf20Sopenharmony_ci		   chan->number, chan->midi_program, note, vel);
3098c2ecf20Sopenharmony_ci#endif
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/* in SYNTH mode, application takes care of voices */
3128c2ecf20Sopenharmony_ci	/* in SEQ mode, drum voice numbers are notes on drum channel */
3138c2ecf20Sopenharmony_ci	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
3148c2ecf20Sopenharmony_ci		if (chan->drum_channel) {
3158c2ecf20Sopenharmony_ci			/* percussion instruments are located in bank 128 */
3168c2ecf20Sopenharmony_ci			bank = 128;
3178c2ecf20Sopenharmony_ci			prg = note;
3188c2ecf20Sopenharmony_ci		} else {
3198c2ecf20Sopenharmony_ci			bank = chan->gm_bank_select;
3208c2ecf20Sopenharmony_ci			prg = chan->midi_program;
3218c2ecf20Sopenharmony_ci		}
3228c2ecf20Sopenharmony_ci	} else {
3238c2ecf20Sopenharmony_ci		/* Prepare for OSS mode */
3248c2ecf20Sopenharmony_ci		if (chan->number >= MAX_OPL3_VOICES)
3258c2ecf20Sopenharmony_ci			return;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci		/* OSS instruments are located in bank 127 */
3288c2ecf20Sopenharmony_ci		bank = 127;
3298c2ecf20Sopenharmony_ci		prg = chan->midi_program;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	spin_lock_irqsave(&opl3->voice_lock, flags);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	if (use_internal_drums) {
3358c2ecf20Sopenharmony_ci		snd_opl3_drum_switch(opl3, note, vel, 1, chan);
3368c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&opl3->voice_lock, flags);
3378c2ecf20Sopenharmony_ci		return;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci __extra_prg:
3418c2ecf20Sopenharmony_ci	patch = snd_opl3_find_patch(opl3, prg, bank, 0);
3428c2ecf20Sopenharmony_ci	if (!patch) {
3438c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&opl3->voice_lock, flags);
3448c2ecf20Sopenharmony_ci		return;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	fm = &patch->inst;
3488c2ecf20Sopenharmony_ci	switch (patch->type) {
3498c2ecf20Sopenharmony_ci	case FM_PATCH_OPL2:
3508c2ecf20Sopenharmony_ci		instr_4op = 0;
3518c2ecf20Sopenharmony_ci		break;
3528c2ecf20Sopenharmony_ci	case FM_PATCH_OPL3:
3538c2ecf20Sopenharmony_ci		if (opl3->hardware >= OPL3_HW_OPL3) {
3548c2ecf20Sopenharmony_ci			instr_4op = 1;
3558c2ecf20Sopenharmony_ci			break;
3568c2ecf20Sopenharmony_ci		}
3578c2ecf20Sopenharmony_ci		fallthrough;
3588c2ecf20Sopenharmony_ci	default:
3598c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&opl3->voice_lock, flags);
3608c2ecf20Sopenharmony_ci		return;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
3638c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "  --> OPL%i instrument: %s\n",
3648c2ecf20Sopenharmony_ci		   instr_4op ? 3 : 2, patch->name);
3658c2ecf20Sopenharmony_ci#endif
3668c2ecf20Sopenharmony_ci	/* in SYNTH mode, application takes care of voices */
3678c2ecf20Sopenharmony_ci	/* in SEQ mode, allocate voice on free OPL3 channel */
3688c2ecf20Sopenharmony_ci	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
3698c2ecf20Sopenharmony_ci		voice = opl3_get_voice(opl3, instr_4op, chan);
3708c2ecf20Sopenharmony_ci	} else {
3718c2ecf20Sopenharmony_ci		/* remap OSS voice */
3728c2ecf20Sopenharmony_ci		voice = snd_opl3_oss_map[chan->number];
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (voice < 0) {
3768c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&opl3->voice_lock, flags);
3778c2ecf20Sopenharmony_ci		return;
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	if (voice < MAX_OPL2_VOICES) {
3818c2ecf20Sopenharmony_ci		/* Left register block for voices 0 .. 8 */
3828c2ecf20Sopenharmony_ci		reg_side = OPL3_LEFT;
3838c2ecf20Sopenharmony_ci		voice_offset = voice;
3848c2ecf20Sopenharmony_ci		connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
3858c2ecf20Sopenharmony_ci	} else {
3868c2ecf20Sopenharmony_ci		/* Right register block for voices 9 .. 17 */
3878c2ecf20Sopenharmony_ci		reg_side = OPL3_RIGHT;
3888c2ecf20Sopenharmony_ci		voice_offset = voice - MAX_OPL2_VOICES;
3898c2ecf20Sopenharmony_ci		connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* kill voice on channel */
3938c2ecf20Sopenharmony_ci	vp = &opl3->voices[voice];
3948c2ecf20Sopenharmony_ci	if (vp->state > 0) {
3958c2ecf20Sopenharmony_ci		opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
3968c2ecf20Sopenharmony_ci		reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
3978c2ecf20Sopenharmony_ci		opl3->command(opl3, opl3_reg, reg_val);
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci	if (instr_4op) {
4008c2ecf20Sopenharmony_ci		vp2 = &opl3->voices[voice + 3];
4018c2ecf20Sopenharmony_ci		if (vp2->state > 0) {
4028c2ecf20Sopenharmony_ci			opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
4038c2ecf20Sopenharmony_ci					       voice_offset + 3);
4048c2ecf20Sopenharmony_ci			reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
4058c2ecf20Sopenharmony_ci			opl3->command(opl3, opl3_reg, reg_val);
4068c2ecf20Sopenharmony_ci		}
4078c2ecf20Sopenharmony_ci	}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/* set connection register */
4108c2ecf20Sopenharmony_ci	if (instr_4op) {
4118c2ecf20Sopenharmony_ci		if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
4128c2ecf20Sopenharmony_ci			opl3->connection_reg |= connect_mask;
4138c2ecf20Sopenharmony_ci			/* set connection bit */
4148c2ecf20Sopenharmony_ci			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
4158c2ecf20Sopenharmony_ci			opl3->command(opl3, opl3_reg, opl3->connection_reg);
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci	} else {
4188c2ecf20Sopenharmony_ci		if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
4198c2ecf20Sopenharmony_ci			opl3->connection_reg &= ~connect_mask;
4208c2ecf20Sopenharmony_ci			/* clear connection bit */
4218c2ecf20Sopenharmony_ci			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
4228c2ecf20Sopenharmony_ci			opl3->command(opl3, opl3_reg, opl3->connection_reg);
4238c2ecf20Sopenharmony_ci		}
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
4278c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "  --> setting OPL3 connection: 0x%x\n",
4288c2ecf20Sopenharmony_ci		   opl3->connection_reg);
4298c2ecf20Sopenharmony_ci#endif
4308c2ecf20Sopenharmony_ci	/*
4318c2ecf20Sopenharmony_ci	 * calculate volume depending on connection
4328c2ecf20Sopenharmony_ci	 * between FM operators (see include/opl3.h)
4338c2ecf20Sopenharmony_ci	 */
4348c2ecf20Sopenharmony_ci	for (i = 0; i < (instr_4op ? 4 : 2); i++)
4358c2ecf20Sopenharmony_ci		vol_op[i] = fm->op[i].ksl_level;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	connection = fm->feedback_connection[0] & 0x01;
4388c2ecf20Sopenharmony_ci	if (instr_4op) {
4398c2ecf20Sopenharmony_ci		connection <<= 1;
4408c2ecf20Sopenharmony_ci		connection |= fm->feedback_connection[1] & 0x01;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		snd_opl3_calc_volume(&vol_op[3], vel, chan);
4438c2ecf20Sopenharmony_ci		switch (connection) {
4448c2ecf20Sopenharmony_ci		case 0x03:
4458c2ecf20Sopenharmony_ci			snd_opl3_calc_volume(&vol_op[2], vel, chan);
4468c2ecf20Sopenharmony_ci			fallthrough;
4478c2ecf20Sopenharmony_ci		case 0x02:
4488c2ecf20Sopenharmony_ci			snd_opl3_calc_volume(&vol_op[0], vel, chan);
4498c2ecf20Sopenharmony_ci			break;
4508c2ecf20Sopenharmony_ci		case 0x01:
4518c2ecf20Sopenharmony_ci			snd_opl3_calc_volume(&vol_op[1], vel, chan);
4528c2ecf20Sopenharmony_ci		}
4538c2ecf20Sopenharmony_ci	} else {
4548c2ecf20Sopenharmony_ci		snd_opl3_calc_volume(&vol_op[1], vel, chan);
4558c2ecf20Sopenharmony_ci		if (connection)
4568c2ecf20Sopenharmony_ci			snd_opl3_calc_volume(&vol_op[0], vel, chan);
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	/* Program the FM voice characteristics */
4608c2ecf20Sopenharmony_ci	for (i = 0; i < (instr_4op ? 4 : 2); i++) {
4618c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
4628c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG "  --> programming operator %i\n", i);
4638c2ecf20Sopenharmony_ci#endif
4648c2ecf20Sopenharmony_ci		op_offset = snd_opl3_regmap[voice_offset][i];
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci		/* Set OPL3 AM_VIB register of requested voice/operator */
4678c2ecf20Sopenharmony_ci		reg_val = fm->op[i].am_vib;
4688c2ecf20Sopenharmony_ci		opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
4698c2ecf20Sopenharmony_ci		opl3->command(opl3, opl3_reg, reg_val);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci		/* Set OPL3 KSL_LEVEL register of requested voice/operator */
4728c2ecf20Sopenharmony_ci		reg_val = vol_op[i];
4738c2ecf20Sopenharmony_ci		opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
4748c2ecf20Sopenharmony_ci		opl3->command(opl3, opl3_reg, reg_val);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci		/* Set OPL3 ATTACK_DECAY register of requested voice/operator */
4778c2ecf20Sopenharmony_ci		reg_val = fm->op[i].attack_decay;
4788c2ecf20Sopenharmony_ci		opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
4798c2ecf20Sopenharmony_ci		opl3->command(opl3, opl3_reg, reg_val);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci		/* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
4828c2ecf20Sopenharmony_ci		reg_val = fm->op[i].sustain_release;
4838c2ecf20Sopenharmony_ci		opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
4848c2ecf20Sopenharmony_ci		opl3->command(opl3, opl3_reg, reg_val);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci		/* Select waveform */
4878c2ecf20Sopenharmony_ci		reg_val = fm->op[i].wave_select;
4888c2ecf20Sopenharmony_ci		opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
4898c2ecf20Sopenharmony_ci		opl3->command(opl3, opl3_reg, reg_val);
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* Set operator feedback and 2op inter-operator connection */
4938c2ecf20Sopenharmony_ci	reg_val = fm->feedback_connection[0];
4948c2ecf20Sopenharmony_ci	/* Set output voice connection */
4958c2ecf20Sopenharmony_ci	reg_val |= OPL3_STEREO_BITS;
4968c2ecf20Sopenharmony_ci	if (chan->gm_pan < 43)
4978c2ecf20Sopenharmony_ci		reg_val &= ~OPL3_VOICE_TO_RIGHT;
4988c2ecf20Sopenharmony_ci	if (chan->gm_pan > 85)
4998c2ecf20Sopenharmony_ci		reg_val &= ~OPL3_VOICE_TO_LEFT;
5008c2ecf20Sopenharmony_ci	opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
5018c2ecf20Sopenharmony_ci	opl3->command(opl3, opl3_reg, reg_val);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (instr_4op) {
5048c2ecf20Sopenharmony_ci		/* Set 4op inter-operator connection */
5058c2ecf20Sopenharmony_ci		reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
5068c2ecf20Sopenharmony_ci		/* Set output voice connection */
5078c2ecf20Sopenharmony_ci		reg_val |= OPL3_STEREO_BITS;
5088c2ecf20Sopenharmony_ci		if (chan->gm_pan < 43)
5098c2ecf20Sopenharmony_ci			reg_val &= ~OPL3_VOICE_TO_RIGHT;
5108c2ecf20Sopenharmony_ci		if (chan->gm_pan > 85)
5118c2ecf20Sopenharmony_ci			reg_val &= ~OPL3_VOICE_TO_LEFT;
5128c2ecf20Sopenharmony_ci		opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
5138c2ecf20Sopenharmony_ci				       voice_offset + 3);
5148c2ecf20Sopenharmony_ci		opl3->command(opl3, opl3_reg, reg_val);
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	/*
5188c2ecf20Sopenharmony_ci	 * Special treatment of percussion notes for fm:
5198c2ecf20Sopenharmony_ci	 * Requested pitch is really program, and pitch for
5208c2ecf20Sopenharmony_ci	 * device is whatever was specified in the patch library.
5218c2ecf20Sopenharmony_ci	 */
5228c2ecf20Sopenharmony_ci	if (fm->fix_key)
5238c2ecf20Sopenharmony_ci		note = fm->fix_key;
5248c2ecf20Sopenharmony_ci	/*
5258c2ecf20Sopenharmony_ci	 * use transpose if defined in patch library
5268c2ecf20Sopenharmony_ci	 */
5278c2ecf20Sopenharmony_ci	if (fm->trnsps)
5288c2ecf20Sopenharmony_ci		note += (fm->trnsps - 64);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* Set OPL3 FNUM_LOW register of requested voice */
5338c2ecf20Sopenharmony_ci	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
5348c2ecf20Sopenharmony_ci	opl3->command(opl3, opl3_reg, fnum);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	opl3->voices[voice].keyon_reg = blocknum;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	/* Set output sound flag */
5398c2ecf20Sopenharmony_ci	blocknum |= OPL3_KEYON_BIT;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
5428c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "  --> trigger voice %i\n", voice);
5438c2ecf20Sopenharmony_ci#endif
5448c2ecf20Sopenharmony_ci	/* Set OPL3 KEYON_BLOCK register of requested voice */
5458c2ecf20Sopenharmony_ci	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
5468c2ecf20Sopenharmony_ci	opl3->command(opl3, opl3_reg, blocknum);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* kill note after fixed duration (in centiseconds) */
5498c2ecf20Sopenharmony_ci	if (fm->fix_dur) {
5508c2ecf20Sopenharmony_ci		opl3->voices[voice].note_off = jiffies +
5518c2ecf20Sopenharmony_ci			(fm->fix_dur * HZ) / 100;
5528c2ecf20Sopenharmony_ci		snd_opl3_start_timer(opl3);
5538c2ecf20Sopenharmony_ci		opl3->voices[voice].note_off_check = 1;
5548c2ecf20Sopenharmony_ci	} else
5558c2ecf20Sopenharmony_ci		opl3->voices[voice].note_off_check = 0;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	/* get extra pgm, but avoid possible loops */
5588c2ecf20Sopenharmony_ci	extra_prg = (extra_prg) ? 0 : fm->modes;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	/* do the bookkeeping */
5618c2ecf20Sopenharmony_ci	vp->time = opl3->use_time++;
5628c2ecf20Sopenharmony_ci	vp->note = key;
5638c2ecf20Sopenharmony_ci	vp->chan = chan;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	if (instr_4op) {
5668c2ecf20Sopenharmony_ci		vp->state = SNDRV_OPL3_ST_ON_4OP;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci		vp2 = &opl3->voices[voice + 3];
5698c2ecf20Sopenharmony_ci		vp2->time = opl3->use_time++;
5708c2ecf20Sopenharmony_ci		vp2->note = key;
5718c2ecf20Sopenharmony_ci		vp2->chan = chan;
5728c2ecf20Sopenharmony_ci		vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
5738c2ecf20Sopenharmony_ci	} else {
5748c2ecf20Sopenharmony_ci		if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
5758c2ecf20Sopenharmony_ci			/* 4op killed by 2op, release bounded voice */
5768c2ecf20Sopenharmony_ci			vp2 = &opl3->voices[voice + 3];
5778c2ecf20Sopenharmony_ci			vp2->time = opl3->use_time++;
5788c2ecf20Sopenharmony_ci			vp2->state = SNDRV_OPL3_ST_OFF;
5798c2ecf20Sopenharmony_ci		}
5808c2ecf20Sopenharmony_ci		vp->state = SNDRV_OPL3_ST_ON_2OP;
5818c2ecf20Sopenharmony_ci	}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci#ifdef DEBUG_ALLOC
5848c2ecf20Sopenharmony_ci	debug_alloc(opl3, "note on ", voice);
5858c2ecf20Sopenharmony_ci#endif
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	/* allocate extra program if specified in patch library */
5888c2ecf20Sopenharmony_ci	if (extra_prg) {
5898c2ecf20Sopenharmony_ci		if (extra_prg > 128) {
5908c2ecf20Sopenharmony_ci			bank = 128;
5918c2ecf20Sopenharmony_ci			/* percussions start at 35 */
5928c2ecf20Sopenharmony_ci			prg = extra_prg - 128 + 35 - 1;
5938c2ecf20Sopenharmony_ci		} else {
5948c2ecf20Sopenharmony_ci			bank = 0;
5958c2ecf20Sopenharmony_ci			prg = extra_prg - 1;
5968c2ecf20Sopenharmony_ci		}
5978c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
5988c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG " *** allocating extra program\n");
5998c2ecf20Sopenharmony_ci#endif
6008c2ecf20Sopenharmony_ci		goto __extra_prg;
6018c2ecf20Sopenharmony_ci	}
6028c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&opl3->voice_lock, flags);
6038c2ecf20Sopenharmony_ci}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_cistatic void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	unsigned short reg_side;
6088c2ecf20Sopenharmony_ci	unsigned char voice_offset;
6098c2ecf20Sopenharmony_ci	unsigned short opl3_reg;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	struct snd_opl3_voice *vp, *vp2;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
6148c2ecf20Sopenharmony_ci		return;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	vp = &opl3->voices[voice];
6178c2ecf20Sopenharmony_ci	if (voice < MAX_OPL2_VOICES) {
6188c2ecf20Sopenharmony_ci		/* Left register block for voices 0 .. 8 */
6198c2ecf20Sopenharmony_ci		reg_side = OPL3_LEFT;
6208c2ecf20Sopenharmony_ci		voice_offset = voice;
6218c2ecf20Sopenharmony_ci	} else {
6228c2ecf20Sopenharmony_ci		/* Right register block for voices 9 .. 17 */
6238c2ecf20Sopenharmony_ci		reg_side = OPL3_RIGHT;
6248c2ecf20Sopenharmony_ci		voice_offset = voice - MAX_OPL2_VOICES;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	/* kill voice */
6288c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
6298c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "  --> kill voice %i\n", voice);
6308c2ecf20Sopenharmony_ci#endif
6318c2ecf20Sopenharmony_ci	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
6328c2ecf20Sopenharmony_ci	/* clear Key ON bit */
6338c2ecf20Sopenharmony_ci	opl3->command(opl3, opl3_reg, vp->keyon_reg);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* do the bookkeeping */
6368c2ecf20Sopenharmony_ci	vp->time = opl3->use_time++;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
6398c2ecf20Sopenharmony_ci		vp2 = &opl3->voices[voice + 3];
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci		vp2->time = opl3->use_time++;
6428c2ecf20Sopenharmony_ci		vp2->state = SNDRV_OPL3_ST_OFF;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci	vp->state = SNDRV_OPL3_ST_OFF;
6458c2ecf20Sopenharmony_ci#ifdef DEBUG_ALLOC
6468c2ecf20Sopenharmony_ci	debug_alloc(opl3, "note off", voice);
6478c2ecf20Sopenharmony_ci#endif
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci/*
6528c2ecf20Sopenharmony_ci * Release a note in response to a midi note off.
6538c2ecf20Sopenharmony_ci */
6548c2ecf20Sopenharmony_cistatic void snd_opl3_note_off_unsafe(void *p, int note, int vel,
6558c2ecf20Sopenharmony_ci				     struct snd_midi_channel *chan)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci  	struct snd_opl3 *opl3;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	int voice;
6608c2ecf20Sopenharmony_ci	struct snd_opl3_voice *vp;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	opl3 = p;
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
6658c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
6668c2ecf20Sopenharmony_ci		   chan->number, chan->midi_program, note);
6678c2ecf20Sopenharmony_ci#endif
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
6708c2ecf20Sopenharmony_ci		if (chan->drum_channel && use_internal_drums) {
6718c2ecf20Sopenharmony_ci			snd_opl3_drum_switch(opl3, note, vel, 0, chan);
6728c2ecf20Sopenharmony_ci			return;
6738c2ecf20Sopenharmony_ci		}
6748c2ecf20Sopenharmony_ci		/* this loop will hopefully kill all extra voices, because
6758c2ecf20Sopenharmony_ci		   they are grouped by the same channel and note values */
6768c2ecf20Sopenharmony_ci		for (voice = 0; voice < opl3->max_voices; voice++) {
6778c2ecf20Sopenharmony_ci			vp = &opl3->voices[voice];
6788c2ecf20Sopenharmony_ci			if (vp->state > 0 && vp->chan == chan && vp->note == note) {
6798c2ecf20Sopenharmony_ci				snd_opl3_kill_voice(opl3, voice);
6808c2ecf20Sopenharmony_ci			}
6818c2ecf20Sopenharmony_ci		}
6828c2ecf20Sopenharmony_ci	} else {
6838c2ecf20Sopenharmony_ci		/* remap OSS voices */
6848c2ecf20Sopenharmony_ci		if (chan->number < MAX_OPL3_VOICES) {
6858c2ecf20Sopenharmony_ci			voice = snd_opl3_oss_map[chan->number];
6868c2ecf20Sopenharmony_ci			snd_opl3_kill_voice(opl3, voice);
6878c2ecf20Sopenharmony_ci		}
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_civoid snd_opl3_note_off(void *p, int note, int vel,
6928c2ecf20Sopenharmony_ci		       struct snd_midi_channel *chan)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	struct snd_opl3 *opl3 = p;
6958c2ecf20Sopenharmony_ci	unsigned long flags;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	spin_lock_irqsave(&opl3->voice_lock, flags);
6988c2ecf20Sopenharmony_ci	snd_opl3_note_off_unsafe(p, note, vel, chan);
6998c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&opl3->voice_lock, flags);
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci/*
7038c2ecf20Sopenharmony_ci * key pressure change
7048c2ecf20Sopenharmony_ci */
7058c2ecf20Sopenharmony_civoid snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
7088c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n",
7098c2ecf20Sopenharmony_ci		   chan->number, chan->midi_program);
7108c2ecf20Sopenharmony_ci#endif
7118c2ecf20Sopenharmony_ci}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci/*
7148c2ecf20Sopenharmony_ci * terminate note
7158c2ecf20Sopenharmony_ci */
7168c2ecf20Sopenharmony_civoid snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
7198c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n",
7208c2ecf20Sopenharmony_ci		   chan->number, chan->midi_program);
7218c2ecf20Sopenharmony_ci#endif
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
7258c2ecf20Sopenharmony_ci{
7268c2ecf20Sopenharmony_ci	unsigned short reg_side;
7278c2ecf20Sopenharmony_ci	unsigned char voice_offset;
7288c2ecf20Sopenharmony_ci	unsigned short opl3_reg;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	unsigned char fnum, blocknum;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	struct snd_opl3_voice *vp;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
7358c2ecf20Sopenharmony_ci		return;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	vp = &opl3->voices[voice];
7388c2ecf20Sopenharmony_ci	if (vp->chan == NULL)
7398c2ecf20Sopenharmony_ci		return; /* not allocated? */
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	if (voice < MAX_OPL2_VOICES) {
7428c2ecf20Sopenharmony_ci		/* Left register block for voices 0 .. 8 */
7438c2ecf20Sopenharmony_ci		reg_side = OPL3_LEFT;
7448c2ecf20Sopenharmony_ci		voice_offset = voice;
7458c2ecf20Sopenharmony_ci	} else {
7468c2ecf20Sopenharmony_ci		/* Right register block for voices 9 .. 17 */
7478c2ecf20Sopenharmony_ci		reg_side = OPL3_RIGHT;
7488c2ecf20Sopenharmony_ci		voice_offset = voice - MAX_OPL2_VOICES;
7498c2ecf20Sopenharmony_ci	}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	/* Set OPL3 FNUM_LOW register of requested voice */
7548c2ecf20Sopenharmony_ci	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
7558c2ecf20Sopenharmony_ci	opl3->command(opl3, opl3_reg, fnum);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	vp->keyon_reg = blocknum;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	/* Set output sound flag */
7608c2ecf20Sopenharmony_ci	blocknum |= OPL3_KEYON_BIT;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	/* Set OPL3 KEYON_BLOCK register of requested voice */
7638c2ecf20Sopenharmony_ci	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
7648c2ecf20Sopenharmony_ci	opl3->command(opl3, opl3_reg, blocknum);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	vp->time = opl3->use_time++;
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci/*
7708c2ecf20Sopenharmony_ci * Update voice pitch controller
7718c2ecf20Sopenharmony_ci */
7728c2ecf20Sopenharmony_cistatic void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	int voice;
7758c2ecf20Sopenharmony_ci	struct snd_opl3_voice *vp;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	unsigned long flags;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	spin_lock_irqsave(&opl3->voice_lock, flags);
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
7828c2ecf20Sopenharmony_ci		for (voice = 0; voice < opl3->max_voices; voice++) {
7838c2ecf20Sopenharmony_ci			vp = &opl3->voices[voice];
7848c2ecf20Sopenharmony_ci			if (vp->state > 0 && vp->chan == chan) {
7858c2ecf20Sopenharmony_ci				snd_opl3_update_pitch(opl3, voice);
7868c2ecf20Sopenharmony_ci			}
7878c2ecf20Sopenharmony_ci		}
7888c2ecf20Sopenharmony_ci	} else {
7898c2ecf20Sopenharmony_ci		/* remap OSS voices */
7908c2ecf20Sopenharmony_ci		if (chan->number < MAX_OPL3_VOICES) {
7918c2ecf20Sopenharmony_ci			voice = snd_opl3_oss_map[chan->number];
7928c2ecf20Sopenharmony_ci			snd_opl3_update_pitch(opl3, voice);
7938c2ecf20Sopenharmony_ci		}
7948c2ecf20Sopenharmony_ci	}
7958c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&opl3->voice_lock, flags);
7968c2ecf20Sopenharmony_ci}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci/*
7998c2ecf20Sopenharmony_ci * Deal with a controller type event.  This includes all types of
8008c2ecf20Sopenharmony_ci * control events, not just the midi controllers
8018c2ecf20Sopenharmony_ci */
8028c2ecf20Sopenharmony_civoid snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
8038c2ecf20Sopenharmony_ci{
8048c2ecf20Sopenharmony_ci  	struct snd_opl3 *opl3;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	opl3 = p;
8078c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
8088c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
8098c2ecf20Sopenharmony_ci		   type, chan->number, chan->midi_program);
8108c2ecf20Sopenharmony_ci#endif
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	switch (type) {
8138c2ecf20Sopenharmony_ci	case MIDI_CTL_MSB_MODWHEEL:
8148c2ecf20Sopenharmony_ci		if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
8158c2ecf20Sopenharmony_ci			opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
8168c2ecf20Sopenharmony_ci		else
8178c2ecf20Sopenharmony_ci			opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
8188c2ecf20Sopenharmony_ci		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
8198c2ecf20Sopenharmony_ci				 opl3->drum_reg);
8208c2ecf20Sopenharmony_ci		break;
8218c2ecf20Sopenharmony_ci	case MIDI_CTL_E2_TREMOLO_DEPTH:
8228c2ecf20Sopenharmony_ci		if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
8238c2ecf20Sopenharmony_ci			opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
8248c2ecf20Sopenharmony_ci		else
8258c2ecf20Sopenharmony_ci			opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
8268c2ecf20Sopenharmony_ci		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
8278c2ecf20Sopenharmony_ci				 opl3->drum_reg);
8288c2ecf20Sopenharmony_ci		break;
8298c2ecf20Sopenharmony_ci	case MIDI_CTL_PITCHBEND:
8308c2ecf20Sopenharmony_ci		snd_opl3_pitch_ctrl(opl3, chan);
8318c2ecf20Sopenharmony_ci		break;
8328c2ecf20Sopenharmony_ci	}
8338c2ecf20Sopenharmony_ci}
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci/*
8368c2ecf20Sopenharmony_ci * NRPN events
8378c2ecf20Sopenharmony_ci */
8388c2ecf20Sopenharmony_civoid snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
8398c2ecf20Sopenharmony_ci		   struct snd_midi_channel_set *chset)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
8428c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
8438c2ecf20Sopenharmony_ci		   chan->number, chan->midi_program);
8448c2ecf20Sopenharmony_ci#endif
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci/*
8488c2ecf20Sopenharmony_ci * receive sysex
8498c2ecf20Sopenharmony_ci */
8508c2ecf20Sopenharmony_civoid snd_opl3_sysex(void *p, unsigned char *buf, int len,
8518c2ecf20Sopenharmony_ci		    int parsed, struct snd_midi_channel_set *chset)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci#ifdef DEBUG_MIDI
8548c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "SYSEX\n");
8558c2ecf20Sopenharmony_ci#endif
8568c2ecf20Sopenharmony_ci}
857