xref: /openbmc/linux/sound/drivers/opl3/opl3_midi.c (revision 92a2c6b2)
1 /*
2  *  Copyright (c) by Uros Bizjak <uros@kss-loka.si>
3  *
4  *  Midi synth routines for OPL2/OPL3/OPL4 FM
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 #undef DEBUG_ALLOC
23 #undef DEBUG_MIDI
24 
25 #include "opl3_voice.h"
26 #include <sound/asoundef.h>
27 
28 extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
29 
30 extern bool use_internal_drums;
31 
32 static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
33 				     struct snd_midi_channel *chan);
34 /*
35  * The next table looks magical, but it certainly is not. Its values have
36  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
37  * for i=0. This log-table converts a linear volume-scaling (0..127) to a
38  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
39  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
40  * volume -8 it was implemented as a table because it is only 128 bytes and
41  * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
42  */
43 
44 static char opl3_volume_table[128] =
45 {
46 	-63, -48, -40, -35, -32, -29, -27, -26,
47 	-24, -23, -21, -20, -19, -18, -18, -17,
48 	-16, -15, -15, -14, -13, -13, -12, -12,
49 	-11, -11, -10, -10, -10, -9, -9, -8,
50 	-8, -8, -7, -7, -7, -6, -6, -6,
51 	-5, -5, -5, -5, -4, -4, -4, -4,
52 	-3, -3, -3, -3, -2, -2, -2, -2,
53 	-2, -1, -1, -1, -1, 0, 0, 0,
54 	0, 0, 0, 1, 1, 1, 1, 1,
55 	1, 2, 2, 2, 2, 2, 2, 2,
56 	3, 3, 3, 3, 3, 3, 3, 4,
57 	4, 4, 4, 4, 4, 4, 4, 5,
58 	5, 5, 5, 5, 5, 5, 5, 5,
59 	6, 6, 6, 6, 6, 6, 6, 6,
60 	6, 7, 7, 7, 7, 7, 7, 7,
61 	7, 7, 7, 8, 8, 8, 8, 8
62 };
63 
64 void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
65 			  struct snd_midi_channel *chan)
66 {
67 	int oldvol, newvol, n;
68 	int volume;
69 
70 	volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
71 	if (volume > 127)
72 		volume = 127;
73 
74 	oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
75 
76 	newvol = opl3_volume_table[volume] + oldvol;
77 	if (newvol > OPL3_TOTAL_LEVEL_MASK)
78 		newvol = OPL3_TOTAL_LEVEL_MASK;
79 	else if (newvol < 0)
80 		newvol = 0;
81 
82 	n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
83 
84 	*volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
85 }
86 
87 /*
88  * Converts the note frequency to block and fnum values for the FM chip
89  */
90 static short opl3_note_table[16] =
91 {
92 	305, 323,	/* for pitch bending, -2 semitones */
93 	343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
94 	686, 726	/* for pitch bending, +2 semitones */
95 };
96 
97 static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
98 				int note, struct snd_midi_channel *chan)
99 {
100 	int block = ((note / 12) & 0x07) - 1;
101 	int idx = (note % 12) + 2;
102 	int freq;
103 
104 	if (chan->midi_pitchbend) {
105 		int pitchbend = chan->midi_pitchbend;
106 		int segment;
107 
108 		if (pitchbend > 0x1FFF)
109 			pitchbend = 0x1FFF;
110 
111 		segment = pitchbend / 0x1000;
112 		freq = opl3_note_table[idx+segment];
113 		freq += ((opl3_note_table[idx+segment+1] - freq) *
114 			 (pitchbend % 0x1000)) / 0x1000;
115 	} else {
116 		freq = opl3_note_table[idx];
117 	}
118 
119 	*fnum = (unsigned char) freq;
120 	*blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
121 		((block << 2) & OPL3_BLOCKNUM_MASK);
122 }
123 
124 
125 #ifdef DEBUG_ALLOC
126 static void debug_alloc(struct snd_opl3 *opl3, char *s, int voice) {
127 	int i;
128 	char *str = "x.24";
129 
130 	printk(KERN_DEBUG "time %.5i: %s [%.2i]: ", opl3->use_time, s, voice);
131 	for (i = 0; i < opl3->max_voices; i++)
132 		printk("%c", *(str + opl3->voices[i].state + 1));
133 	printk("\n");
134 }
135 #endif
136 
137 /*
138  * Get a FM voice (channel) to play a note on.
139  */
140 static int opl3_get_voice(struct snd_opl3 *opl3, int instr_4op,
141 			  struct snd_midi_channel *chan) {
142 	int chan_4op_1;		/* first voice for 4op instrument */
143 	int chan_4op_2;		/* second voice for 4op instrument */
144 
145 	struct snd_opl3_voice *vp, *vp2;
146 	unsigned int voice_time;
147 	int i;
148 
149 #ifdef DEBUG_ALLOC
150 	char *alloc_type[3] = { "FREE     ", "CHEAP    ", "EXPENSIVE" };
151 #endif
152 
153 	/* This is our "allocation cost" table */
154 	enum {
155 		FREE = 0, CHEAP, EXPENSIVE, END
156 	};
157 
158 	/* Keeps track of what we are finding */
159 	struct best {
160 		unsigned int time;
161 		int voice;
162 	} best[END];
163 	struct best *bp;
164 
165 	for (i = 0; i < END; i++) {
166 		best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */
167 		best[i].voice = -1;
168 	}
169 
170 	/* Look through all the channels for the most suitable. */
171 	for (i = 0; i < opl3->max_voices; i++) {
172 		vp = &opl3->voices[i];
173 
174 		if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL)
175 		  /* skip unavailable channels, allocated by
176 		     drum voices or by bounded 4op voices) */
177 			continue;
178 
179 		voice_time = vp->time;
180 		bp = best;
181 
182 		chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
183 		chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
184 		if (instr_4op) {
185 			/* allocate 4op voice */
186 			/* skip channels unavailable to 4op instrument */
187 			if (!chan_4op_1)
188 				continue;
189 
190 			if (vp->state)
191 				/* kill one voice, CHEAP */
192 				bp++;
193 			/* get state of bounded 2op channel
194 			   to be allocated for 4op instrument */
195 			vp2 = &opl3->voices[i + 3];
196 			if (vp2->state == SNDRV_OPL3_ST_ON_2OP) {
197 				/* kill two voices, EXPENSIVE */
198 				bp++;
199 				voice_time = (voice_time > vp->time) ?
200 					voice_time : vp->time;
201 			}
202 		} else {
203 			/* allocate 2op voice */
204 			if ((chan_4op_1) || (chan_4op_2))
205 				/* use bounded channels for 2op, CHEAP */
206 				bp++;
207 			else if (vp->state)
208 				/* kill one voice on 2op channel, CHEAP */
209 				bp++;
210 			/* raise kill cost to EXPENSIVE for all channels */
211 			if (vp->state)
212 				bp++;
213 		}
214 		if (voice_time < bp->time) {
215 			bp->time = voice_time;
216 			bp->voice = i;
217 		}
218 	}
219 
220 	for (i = 0; i < END; i++) {
221 		if (best[i].voice >= 0) {
222 #ifdef DEBUG_ALLOC
223 			printk(KERN_DEBUG "%s %iop allocation on voice %i\n",
224 			       alloc_type[i], instr_4op ? 4 : 2,
225 			       best[i].voice);
226 #endif
227 			return best[i].voice;
228 		}
229 	}
230 	/* not found */
231 	return -1;
232 }
233 
234 /* ------------------------------ */
235 
236 /*
237  * System timer interrupt function
238  */
239 void snd_opl3_timer_func(unsigned long data)
240 {
241 
242 	struct snd_opl3 *opl3 = (struct snd_opl3 *)data;
243 	unsigned long flags;
244 	int again = 0;
245 	int i;
246 
247 	spin_lock_irqsave(&opl3->voice_lock, flags);
248 	for (i = 0; i < opl3->max_voices; i++) {
249 		struct snd_opl3_voice *vp = &opl3->voices[i];
250 		if (vp->state > 0 && vp->note_off_check) {
251 			if (vp->note_off == jiffies)
252 				snd_opl3_note_off_unsafe(opl3, vp->note, 0,
253 							 vp->chan);
254 			else
255 				again++;
256 		}
257 	}
258 	spin_unlock_irqrestore(&opl3->voice_lock, flags);
259 
260 	spin_lock_irqsave(&opl3->sys_timer_lock, flags);
261 	if (again)
262 		mod_timer(&opl3->tlist, jiffies + 1);	/* invoke again */
263 	else
264 		opl3->sys_timer_status = 0;
265 	spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
266 }
267 
268 /*
269  * Start system timer
270  */
271 static void snd_opl3_start_timer(struct snd_opl3 *opl3)
272 {
273 	unsigned long flags;
274 	spin_lock_irqsave(&opl3->sys_timer_lock, flags);
275 	if (! opl3->sys_timer_status) {
276 		mod_timer(&opl3->tlist, jiffies + 1);
277 		opl3->sys_timer_status = 1;
278 	}
279 	spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
280 }
281 
282 /* ------------------------------ */
283 
284 
285 static int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
286 	0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
287 };
288 
289 /*
290  * Start a note.
291  */
292 void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
293 {
294 	struct snd_opl3 *opl3;
295 	int instr_4op;
296 
297 	int voice;
298 	struct snd_opl3_voice *vp, *vp2;
299 	unsigned short connect_mask;
300 	unsigned char connection;
301 	unsigned char vol_op[4];
302 
303 	int extra_prg = 0;
304 
305 	unsigned short reg_side;
306 	unsigned char op_offset;
307 	unsigned char voice_offset;
308 	unsigned short opl3_reg;
309 	unsigned char reg_val;
310 	unsigned char prg, bank;
311 
312 	int key = note;
313 	unsigned char fnum, blocknum;
314 	int i;
315 
316 	struct fm_patch *patch;
317 	struct fm_instrument *fm;
318 	unsigned long flags;
319 
320 	opl3 = p;
321 
322 #ifdef DEBUG_MIDI
323 	snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i\n",
324 		   chan->number, chan->midi_program, note, vel);
325 #endif
326 
327 	/* in SYNTH mode, application takes care of voices */
328 	/* in SEQ mode, drum voice numbers are notes on drum channel */
329 	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
330 		if (chan->drum_channel) {
331 			/* percussion instruments are located in bank 128 */
332 			bank = 128;
333 			prg = note;
334 		} else {
335 			bank = chan->gm_bank_select;
336 			prg = chan->midi_program;
337 		}
338 	} else {
339 		/* Prepare for OSS mode */
340 		if (chan->number >= MAX_OPL3_VOICES)
341 			return;
342 
343 		/* OSS instruments are located in bank 127 */
344 		bank = 127;
345 		prg = chan->midi_program;
346 	}
347 
348 	spin_lock_irqsave(&opl3->voice_lock, flags);
349 
350 	if (use_internal_drums) {
351 		snd_opl3_drum_switch(opl3, note, vel, 1, chan);
352 		spin_unlock_irqrestore(&opl3->voice_lock, flags);
353 		return;
354 	}
355 
356  __extra_prg:
357 	patch = snd_opl3_find_patch(opl3, prg, bank, 0);
358 	if (!patch) {
359 		spin_unlock_irqrestore(&opl3->voice_lock, flags);
360 		return;
361 	}
362 
363 	fm = &patch->inst;
364 	switch (patch->type) {
365 	case FM_PATCH_OPL2:
366 		instr_4op = 0;
367 		break;
368 	case FM_PATCH_OPL3:
369 		if (opl3->hardware >= OPL3_HW_OPL3) {
370 			instr_4op = 1;
371 			break;
372 		}
373 	default:
374 		spin_unlock_irqrestore(&opl3->voice_lock, flags);
375 		return;
376 	}
377 #ifdef DEBUG_MIDI
378 	snd_printk(KERN_DEBUG "  --> OPL%i instrument: %s\n",
379 		   instr_4op ? 3 : 2, patch->name);
380 #endif
381 	/* in SYNTH mode, application takes care of voices */
382 	/* in SEQ mode, allocate voice on free OPL3 channel */
383 	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
384 		voice = opl3_get_voice(opl3, instr_4op, chan);
385 	} else {
386 		/* remap OSS voice */
387 		voice = snd_opl3_oss_map[chan->number];
388 	}
389 
390 	if (voice < 0) {
391 		spin_unlock_irqrestore(&opl3->voice_lock, flags);
392 		return;
393 	}
394 
395 	if (voice < MAX_OPL2_VOICES) {
396 		/* Left register block for voices 0 .. 8 */
397 		reg_side = OPL3_LEFT;
398 		voice_offset = voice;
399 		connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
400 	} else {
401 		/* Right register block for voices 9 .. 17 */
402 		reg_side = OPL3_RIGHT;
403 		voice_offset = voice - MAX_OPL2_VOICES;
404 		connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
405 	}
406 
407 	/* kill voice on channel */
408 	vp = &opl3->voices[voice];
409 	if (vp->state > 0) {
410 		opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
411 		reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
412 		opl3->command(opl3, opl3_reg, reg_val);
413 	}
414 	if (instr_4op) {
415 		vp2 = &opl3->voices[voice + 3];
416 		if (vp->state > 0) {
417 			opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
418 					       voice_offset + 3);
419 			reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
420 			opl3->command(opl3, opl3_reg, reg_val);
421 		}
422 	}
423 
424 	/* set connection register */
425 	if (instr_4op) {
426 		if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
427 			opl3->connection_reg |= connect_mask;
428 			/* set connection bit */
429 			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
430 			opl3->command(opl3, opl3_reg, opl3->connection_reg);
431 		}
432 	} else {
433 		if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
434 			opl3->connection_reg &= ~connect_mask;
435 			/* clear connection bit */
436 			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
437 			opl3->command(opl3, opl3_reg, opl3->connection_reg);
438 		}
439 	}
440 
441 #ifdef DEBUG_MIDI
442 	snd_printk(KERN_DEBUG "  --> setting OPL3 connection: 0x%x\n",
443 		   opl3->connection_reg);
444 #endif
445 	/*
446 	 * calculate volume depending on connection
447 	 * between FM operators (see include/opl3.h)
448 	 */
449 	for (i = 0; i < (instr_4op ? 4 : 2); i++)
450 		vol_op[i] = fm->op[i].ksl_level;
451 
452 	connection = fm->feedback_connection[0] & 0x01;
453 	if (instr_4op) {
454 		connection <<= 1;
455 		connection |= fm->feedback_connection[1] & 0x01;
456 
457 		snd_opl3_calc_volume(&vol_op[3], vel, chan);
458 		switch (connection) {
459 		case 0x03:
460 			snd_opl3_calc_volume(&vol_op[2], vel, chan);
461 			/* fallthru */
462 		case 0x02:
463 			snd_opl3_calc_volume(&vol_op[0], vel, chan);
464 			break;
465 		case 0x01:
466 			snd_opl3_calc_volume(&vol_op[1], vel, chan);
467 		}
468 	} else {
469 		snd_opl3_calc_volume(&vol_op[1], vel, chan);
470 		if (connection)
471 			snd_opl3_calc_volume(&vol_op[0], vel, chan);
472 	}
473 
474 	/* Program the FM voice characteristics */
475 	for (i = 0; i < (instr_4op ? 4 : 2); i++) {
476 #ifdef DEBUG_MIDI
477 		snd_printk(KERN_DEBUG "  --> programming operator %i\n", i);
478 #endif
479 		op_offset = snd_opl3_regmap[voice_offset][i];
480 
481 		/* Set OPL3 AM_VIB register of requested voice/operator */
482 		reg_val = fm->op[i].am_vib;
483 		opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
484 		opl3->command(opl3, opl3_reg, reg_val);
485 
486 		/* Set OPL3 KSL_LEVEL register of requested voice/operator */
487 		reg_val = vol_op[i];
488 		opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
489 		opl3->command(opl3, opl3_reg, reg_val);
490 
491 		/* Set OPL3 ATTACK_DECAY register of requested voice/operator */
492 		reg_val = fm->op[i].attack_decay;
493 		opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
494 		opl3->command(opl3, opl3_reg, reg_val);
495 
496 		/* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
497 		reg_val = fm->op[i].sustain_release;
498 		opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
499 		opl3->command(opl3, opl3_reg, reg_val);
500 
501 		/* Select waveform */
502 		reg_val = fm->op[i].wave_select;
503 		opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
504 		opl3->command(opl3, opl3_reg, reg_val);
505 	}
506 
507 	/* Set operator feedback and 2op inter-operator connection */
508 	reg_val = fm->feedback_connection[0];
509 	/* Set output voice connection */
510 	reg_val |= OPL3_STEREO_BITS;
511 	if (chan->gm_pan < 43)
512 		reg_val &= ~OPL3_VOICE_TO_RIGHT;
513 	if (chan->gm_pan > 85)
514 		reg_val &= ~OPL3_VOICE_TO_LEFT;
515 	opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
516 	opl3->command(opl3, opl3_reg, reg_val);
517 
518 	if (instr_4op) {
519 		/* Set 4op inter-operator connection */
520 		reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
521 		/* Set output voice connection */
522 		reg_val |= OPL3_STEREO_BITS;
523 		if (chan->gm_pan < 43)
524 			reg_val &= ~OPL3_VOICE_TO_RIGHT;
525 		if (chan->gm_pan > 85)
526 			reg_val &= ~OPL3_VOICE_TO_LEFT;
527 		opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
528 				       voice_offset + 3);
529 		opl3->command(opl3, opl3_reg, reg_val);
530 	}
531 
532 	/*
533 	 * Special treatment of percussion notes for fm:
534 	 * Requested pitch is really program, and pitch for
535 	 * device is whatever was specified in the patch library.
536 	 */
537 	if (fm->fix_key)
538 		note = fm->fix_key;
539 	/*
540 	 * use transpose if defined in patch library
541 	 */
542 	if (fm->trnsps)
543 		note += (fm->trnsps - 64);
544 
545 	snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
546 
547 	/* Set OPL3 FNUM_LOW register of requested voice */
548 	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
549 	opl3->command(opl3, opl3_reg, fnum);
550 
551 	opl3->voices[voice].keyon_reg = blocknum;
552 
553 	/* Set output sound flag */
554 	blocknum |= OPL3_KEYON_BIT;
555 
556 #ifdef DEBUG_MIDI
557 	snd_printk(KERN_DEBUG "  --> trigger voice %i\n", voice);
558 #endif
559 	/* Set OPL3 KEYON_BLOCK register of requested voice */
560 	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
561 	opl3->command(opl3, opl3_reg, blocknum);
562 
563 	/* kill note after fixed duration (in centiseconds) */
564 	if (fm->fix_dur) {
565 		opl3->voices[voice].note_off = jiffies +
566 			(fm->fix_dur * HZ) / 100;
567 		snd_opl3_start_timer(opl3);
568 		opl3->voices[voice].note_off_check = 1;
569 	} else
570 		opl3->voices[voice].note_off_check = 0;
571 
572 	/* get extra pgm, but avoid possible loops */
573 	extra_prg = (extra_prg) ? 0 : fm->modes;
574 
575 	/* do the bookkeeping */
576 	vp->time = opl3->use_time++;
577 	vp->note = key;
578 	vp->chan = chan;
579 
580 	if (instr_4op) {
581 		vp->state = SNDRV_OPL3_ST_ON_4OP;
582 
583 		vp2 = &opl3->voices[voice + 3];
584 		vp2->time = opl3->use_time++;
585 		vp2->note = key;
586 		vp2->chan = chan;
587 		vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
588 	} else {
589 		if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
590 			/* 4op killed by 2op, release bounded voice */
591 			vp2 = &opl3->voices[voice + 3];
592 			vp2->time = opl3->use_time++;
593 			vp2->state = SNDRV_OPL3_ST_OFF;
594 		}
595 		vp->state = SNDRV_OPL3_ST_ON_2OP;
596 	}
597 
598 #ifdef DEBUG_ALLOC
599 	debug_alloc(opl3, "note on ", voice);
600 #endif
601 
602 	/* allocate extra program if specified in patch library */
603 	if (extra_prg) {
604 		if (extra_prg > 128) {
605 			bank = 128;
606 			/* percussions start at 35 */
607 			prg = extra_prg - 128 + 35 - 1;
608 		} else {
609 			bank = 0;
610 			prg = extra_prg - 1;
611 		}
612 #ifdef DEBUG_MIDI
613 		snd_printk(KERN_DEBUG " *** allocating extra program\n");
614 #endif
615 		goto __extra_prg;
616 	}
617 	spin_unlock_irqrestore(&opl3->voice_lock, flags);
618 }
619 
620 static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
621 {
622 	unsigned short reg_side;
623 	unsigned char voice_offset;
624 	unsigned short opl3_reg;
625 
626 	struct snd_opl3_voice *vp, *vp2;
627 
628 	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
629 		return;
630 
631 	vp = &opl3->voices[voice];
632 	if (voice < MAX_OPL2_VOICES) {
633 		/* Left register block for voices 0 .. 8 */
634 		reg_side = OPL3_LEFT;
635 		voice_offset = voice;
636 	} else {
637 		/* Right register block for voices 9 .. 17 */
638 		reg_side = OPL3_RIGHT;
639 		voice_offset = voice - MAX_OPL2_VOICES;
640 	}
641 
642 	/* kill voice */
643 #ifdef DEBUG_MIDI
644 	snd_printk(KERN_DEBUG "  --> kill voice %i\n", voice);
645 #endif
646 	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
647 	/* clear Key ON bit */
648 	opl3->command(opl3, opl3_reg, vp->keyon_reg);
649 
650 	/* do the bookkeeping */
651 	vp->time = opl3->use_time++;
652 
653 	if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
654 		vp2 = &opl3->voices[voice + 3];
655 
656 		vp2->time = opl3->use_time++;
657 		vp2->state = SNDRV_OPL3_ST_OFF;
658 	}
659 	vp->state = SNDRV_OPL3_ST_OFF;
660 #ifdef DEBUG_ALLOC
661 	debug_alloc(opl3, "note off", voice);
662 #endif
663 
664 }
665 
666 /*
667  * Release a note in response to a midi note off.
668  */
669 static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
670 				     struct snd_midi_channel *chan)
671 {
672   	struct snd_opl3 *opl3;
673 
674 	int voice;
675 	struct snd_opl3_voice *vp;
676 
677 	opl3 = p;
678 
679 #ifdef DEBUG_MIDI
680 	snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
681 		   chan->number, chan->midi_program, note);
682 #endif
683 
684 	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
685 		if (chan->drum_channel && use_internal_drums) {
686 			snd_opl3_drum_switch(opl3, note, vel, 0, chan);
687 			return;
688 		}
689 		/* this loop will hopefully kill all extra voices, because
690 		   they are grouped by the same channel and note values */
691 		for (voice = 0; voice < opl3->max_voices; voice++) {
692 			vp = &opl3->voices[voice];
693 			if (vp->state > 0 && vp->chan == chan && vp->note == note) {
694 				snd_opl3_kill_voice(opl3, voice);
695 			}
696 		}
697 	} else {
698 		/* remap OSS voices */
699 		if (chan->number < MAX_OPL3_VOICES) {
700 			voice = snd_opl3_oss_map[chan->number];
701 			snd_opl3_kill_voice(opl3, voice);
702 		}
703 	}
704 }
705 
706 void snd_opl3_note_off(void *p, int note, int vel,
707 		       struct snd_midi_channel *chan)
708 {
709 	struct snd_opl3 *opl3 = p;
710 	unsigned long flags;
711 
712 	spin_lock_irqsave(&opl3->voice_lock, flags);
713 	snd_opl3_note_off_unsafe(p, note, vel, chan);
714 	spin_unlock_irqrestore(&opl3->voice_lock, flags);
715 }
716 
717 /*
718  * key pressure change
719  */
720 void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
721 {
722   	struct snd_opl3 *opl3;
723 
724 	opl3 = p;
725 #ifdef DEBUG_MIDI
726 	snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n",
727 		   chan->number, chan->midi_program);
728 #endif
729 }
730 
731 /*
732  * terminate note
733  */
734 void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
735 {
736   	struct snd_opl3 *opl3;
737 
738 	opl3 = p;
739 #ifdef DEBUG_MIDI
740 	snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n",
741 		   chan->number, chan->midi_program);
742 #endif
743 }
744 
745 static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
746 {
747 	unsigned short reg_side;
748 	unsigned char voice_offset;
749 	unsigned short opl3_reg;
750 
751 	unsigned char fnum, blocknum;
752 
753 	struct snd_opl3_voice *vp;
754 
755 	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
756 		return;
757 
758 	vp = &opl3->voices[voice];
759 	if (vp->chan == NULL)
760 		return; /* not allocated? */
761 
762 	if (voice < MAX_OPL2_VOICES) {
763 		/* Left register block for voices 0 .. 8 */
764 		reg_side = OPL3_LEFT;
765 		voice_offset = voice;
766 	} else {
767 		/* Right register block for voices 9 .. 17 */
768 		reg_side = OPL3_RIGHT;
769 		voice_offset = voice - MAX_OPL2_VOICES;
770 	}
771 
772 	snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
773 
774 	/* Set OPL3 FNUM_LOW register of requested voice */
775 	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
776 	opl3->command(opl3, opl3_reg, fnum);
777 
778 	vp->keyon_reg = blocknum;
779 
780 	/* Set output sound flag */
781 	blocknum |= OPL3_KEYON_BIT;
782 
783 	/* Set OPL3 KEYON_BLOCK register of requested voice */
784 	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
785 	opl3->command(opl3, opl3_reg, blocknum);
786 
787 	vp->time = opl3->use_time++;
788 }
789 
790 /*
791  * Update voice pitch controller
792  */
793 static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
794 {
795 	int voice;
796 	struct snd_opl3_voice *vp;
797 
798 	unsigned long flags;
799 
800 	spin_lock_irqsave(&opl3->voice_lock, flags);
801 
802 	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
803 		for (voice = 0; voice < opl3->max_voices; voice++) {
804 			vp = &opl3->voices[voice];
805 			if (vp->state > 0 && vp->chan == chan) {
806 				snd_opl3_update_pitch(opl3, voice);
807 			}
808 		}
809 	} else {
810 		/* remap OSS voices */
811 		if (chan->number < MAX_OPL3_VOICES) {
812 			voice = snd_opl3_oss_map[chan->number];
813 			snd_opl3_update_pitch(opl3, voice);
814 		}
815 	}
816 	spin_unlock_irqrestore(&opl3->voice_lock, flags);
817 }
818 
819 /*
820  * Deal with a controller type event.  This includes all types of
821  * control events, not just the midi controllers
822  */
823 void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
824 {
825   	struct snd_opl3 *opl3;
826 
827 	opl3 = p;
828 #ifdef DEBUG_MIDI
829 	snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
830 		   type, chan->number, chan->midi_program);
831 #endif
832 
833 	switch (type) {
834 	case MIDI_CTL_MSB_MODWHEEL:
835 		if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
836 			opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
837 		else
838 			opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
839 		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
840 				 opl3->drum_reg);
841 		break;
842 	case MIDI_CTL_E2_TREMOLO_DEPTH:
843 		if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
844 			opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
845 		else
846 			opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
847 		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
848 				 opl3->drum_reg);
849 		break;
850 	case MIDI_CTL_PITCHBEND:
851 		snd_opl3_pitch_ctrl(opl3, chan);
852 		break;
853 	}
854 }
855 
856 /*
857  * NRPN events
858  */
859 void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
860 		   struct snd_midi_channel_set *chset)
861 {
862   	struct snd_opl3 *opl3;
863 
864 	opl3 = p;
865 #ifdef DEBUG_MIDI
866 	snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
867 		   chan->number, chan->midi_program);
868 #endif
869 }
870 
871 /*
872  * receive sysex
873  */
874 void snd_opl3_sysex(void *p, unsigned char *buf, int len,
875 		    int parsed, struct snd_midi_channel_set *chset)
876 {
877   	struct snd_opl3 *opl3;
878 
879 	opl3 = p;
880 #ifdef DEBUG_MIDI
881 	snd_printk(KERN_DEBUG "SYSEX\n");
882 #endif
883 }
884