xref: /linux/sound/core/seq/seq_midi_emul.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
1 /*
2  *  GM/GS/XG midi module.
3  *
4  *  Copyright (C) 1999 Steve Ratcliffe
5  *
6  *  Based on awe_wave.c by Takashi Iwai
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */
23 /*
24  * This module is used to keep track of the current midi state.
25  * It can be used for drivers that are required to emulate midi when
26  * the hardware doesn't.
27  *
28  * It was written for a AWE64 driver, but there should be no AWE specific
29  * code in here.  If there is it should be reported as a bug.
30  */
31 
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/module.h>
36 #include <sound/core.h>
37 #include <sound/seq_kernel.h>
38 #include <sound/seq_midi_emul.h>
39 #include <sound/initval.h>
40 #include <sound/asoundef.h>
41 
42 MODULE_AUTHOR("Takashi Iwai / Steve Ratcliffe");
43 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI emulation.");
44 MODULE_LICENSE("GPL");
45 
46 /* Prototypes for static functions */
47 static void note_off(struct snd_midi_op *ops, void *drv,
48 		     struct snd_midi_channel *chan,
49 		     int note, int vel);
50 static void do_control(struct snd_midi_op *ops, void *private,
51 		       struct snd_midi_channel_set *chset,
52 		       struct snd_midi_channel *chan,
53 		       int control, int value);
54 static void rpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
55 		struct snd_midi_channel_set *chset);
56 static void nrpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
57 		 struct snd_midi_channel_set *chset);
58 static void sysex(struct snd_midi_op *ops, void *private, unsigned char *sysex,
59 		  int len, struct snd_midi_channel_set *chset);
60 static void all_sounds_off(struct snd_midi_op *ops, void *private,
61 			   struct snd_midi_channel *chan);
62 static void all_notes_off(struct snd_midi_op *ops, void *private,
63 			  struct snd_midi_channel *chan);
64 static void snd_midi_reset_controllers(struct snd_midi_channel *chan);
65 static void reset_all_channels(struct snd_midi_channel_set *chset);
66 
67 
68 /*
69  * Process an event in a driver independent way.  This means dealing
70  * with RPN, NRPN, SysEx etc that are defined for common midi applications
71  * such as GM, GS and XG.
72  * There modes that this module will run in are:
73  *   Generic MIDI - no interpretation at all, it will just save current values
74  *                  of controllers etc.
75  *   GM - You can use all gm_ prefixed elements of chan.  Controls, RPN, NRPN,
76  *        SysEx will be interpreded as defined in General Midi.
77  *   GS - You can use all gs_ prefixed elements of chan. Codes for GS will be
78  *        interpreted.
79  *   XG - You can use all xg_ prefixed elements of chan.  Codes for XG will
80  *        be interpreted.
81  */
82 void
83 snd_midi_process_event(struct snd_midi_op *ops,
84 		       struct snd_seq_event *ev,
85 		       struct snd_midi_channel_set *chanset)
86 {
87 	struct snd_midi_channel *chan;
88 	void *drv;
89 	int dest_channel = 0;
90 
91 	if (ev == NULL || chanset == NULL) {
92 		pr_debug("ALSA: seq_midi_emul: ev or chanbase NULL (snd_midi_process_event)\n");
93 		return;
94 	}
95 	if (chanset->channels == NULL)
96 		return;
97 
98 	if (snd_seq_ev_is_channel_type(ev)) {
99 		dest_channel = ev->data.note.channel;
100 		if (dest_channel >= chanset->max_channels) {
101 			pr_debug("ALSA: seq_midi_emul: dest channel is %d, max is %d\n",
102 				   dest_channel, chanset->max_channels);
103 			return;
104 		}
105 	}
106 
107 	chan = chanset->channels + dest_channel;
108 	drv  = chanset->private_data;
109 
110 	/* EVENT_NOTE should be processed before queued */
111 	if (ev->type == SNDRV_SEQ_EVENT_NOTE)
112 		return;
113 
114 	/* Make sure that we don't have a note on that should really be
115 	 * a note off */
116 	if (ev->type == SNDRV_SEQ_EVENT_NOTEON && ev->data.note.velocity == 0)
117 		ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
118 
119 	/* Make sure the note is within array range */
120 	if (ev->type == SNDRV_SEQ_EVENT_NOTEON ||
121 	    ev->type == SNDRV_SEQ_EVENT_NOTEOFF ||
122 	    ev->type == SNDRV_SEQ_EVENT_KEYPRESS) {
123 		if (ev->data.note.note >= 128)
124 			return;
125 	}
126 
127 	switch (ev->type) {
128 	case SNDRV_SEQ_EVENT_NOTEON:
129 		if (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON) {
130 			if (ops->note_off)
131 				ops->note_off(drv, ev->data.note.note, 0, chan);
132 		}
133 		chan->note[ev->data.note.note] = SNDRV_MIDI_NOTE_ON;
134 		if (ops->note_on)
135 			ops->note_on(drv, ev->data.note.note, ev->data.note.velocity, chan);
136 		break;
137 	case SNDRV_SEQ_EVENT_NOTEOFF:
138 		if (! (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON))
139 			break;
140 		if (ops->note_off)
141 			note_off(ops, drv, chan, ev->data.note.note, ev->data.note.velocity);
142 		break;
143 	case SNDRV_SEQ_EVENT_KEYPRESS:
144 		if (ops->key_press)
145 			ops->key_press(drv, ev->data.note.note, ev->data.note.velocity, chan);
146 		break;
147 	case SNDRV_SEQ_EVENT_CONTROLLER:
148 		do_control(ops, drv, chanset, chan,
149 			   ev->data.control.param, ev->data.control.value);
150 		break;
151 	case SNDRV_SEQ_EVENT_PGMCHANGE:
152 		chan->midi_program = ev->data.control.value;
153 		break;
154 	case SNDRV_SEQ_EVENT_PITCHBEND:
155 		chan->midi_pitchbend = ev->data.control.value;
156 		if (ops->control)
157 			ops->control(drv, MIDI_CTL_PITCHBEND, chan);
158 		break;
159 	case SNDRV_SEQ_EVENT_CHANPRESS:
160 		chan->midi_pressure = ev->data.control.value;
161 		if (ops->control)
162 			ops->control(drv, MIDI_CTL_CHAN_PRESSURE, chan);
163 		break;
164 	case SNDRV_SEQ_EVENT_CONTROL14:
165 		/* Best guess is that this is any of the 14 bit controller values */
166 		if (ev->data.control.param < 32) {
167 			/* set low part first */
168 			chan->control[ev->data.control.param + 32] =
169 				ev->data.control.value & 0x7f;
170 			do_control(ops, drv, chanset, chan,
171 				   ev->data.control.param,
172 				   ((ev->data.control.value>>7) & 0x7f));
173 		} else
174 			do_control(ops, drv, chanset, chan,
175 				   ev->data.control.param,
176 				   ev->data.control.value);
177 		break;
178 	case SNDRV_SEQ_EVENT_NONREGPARAM:
179 		/* Break it back into its controller values */
180 		chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
181 		chan->control[MIDI_CTL_MSB_DATA_ENTRY]
182 			= (ev->data.control.value >> 7) & 0x7f;
183 		chan->control[MIDI_CTL_LSB_DATA_ENTRY]
184 			= ev->data.control.value & 0x7f;
185 		chan->control[MIDI_CTL_NONREG_PARM_NUM_MSB]
186 			= (ev->data.control.param >> 7) & 0x7f;
187 		chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB]
188 			= ev->data.control.param & 0x7f;
189 		nrpn(ops, drv, chan, chanset);
190 		break;
191 	case SNDRV_SEQ_EVENT_REGPARAM:
192 		/* Break it back into its controller values */
193 		chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
194 		chan->control[MIDI_CTL_MSB_DATA_ENTRY]
195 			= (ev->data.control.value >> 7) & 0x7f;
196 		chan->control[MIDI_CTL_LSB_DATA_ENTRY]
197 			= ev->data.control.value & 0x7f;
198 		chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB]
199 			= (ev->data.control.param >> 7) & 0x7f;
200 		chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB]
201 			= ev->data.control.param & 0x7f;
202 		rpn(ops, drv, chan, chanset);
203 		break;
204 	case SNDRV_SEQ_EVENT_SYSEX:
205 		if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
206 			unsigned char sysexbuf[64];
207 			int len;
208 			len = snd_seq_expand_var_event(ev, sizeof(sysexbuf), sysexbuf, 1, 0);
209 			if (len > 0)
210 				sysex(ops, drv, sysexbuf, len, chanset);
211 		}
212 		break;
213 	case SNDRV_SEQ_EVENT_SONGPOS:
214 	case SNDRV_SEQ_EVENT_SONGSEL:
215 	case SNDRV_SEQ_EVENT_CLOCK:
216 	case SNDRV_SEQ_EVENT_START:
217 	case SNDRV_SEQ_EVENT_CONTINUE:
218 	case SNDRV_SEQ_EVENT_STOP:
219 	case SNDRV_SEQ_EVENT_QFRAME:
220 	case SNDRV_SEQ_EVENT_TEMPO:
221 	case SNDRV_SEQ_EVENT_TIMESIGN:
222 	case SNDRV_SEQ_EVENT_KEYSIGN:
223 		goto not_yet;
224 	case SNDRV_SEQ_EVENT_SENSING:
225 		break;
226 	case SNDRV_SEQ_EVENT_CLIENT_START:
227 	case SNDRV_SEQ_EVENT_CLIENT_EXIT:
228 	case SNDRV_SEQ_EVENT_CLIENT_CHANGE:
229 	case SNDRV_SEQ_EVENT_PORT_START:
230 	case SNDRV_SEQ_EVENT_PORT_EXIT:
231 	case SNDRV_SEQ_EVENT_PORT_CHANGE:
232 	case SNDRV_SEQ_EVENT_ECHO:
233 	not_yet:
234 	default:
235 		/*pr_debug("ALSA: seq_midi_emul: Unimplemented event %d\n", ev->type);*/
236 		break;
237 	}
238 }
239 EXPORT_SYMBOL(snd_midi_process_event);
240 
241 
242 /*
243  * release note
244  */
245 static void
246 note_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
247 	 int note, int vel)
248 {
249 	if (chan->gm_hold) {
250 		/* Hold this note until pedal is turned off */
251 		chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
252 	} else if (chan->note[note] & SNDRV_MIDI_NOTE_SOSTENUTO) {
253 		/* Mark this note as release; it will be turned off when sostenuto
254 		 * is turned off */
255 		chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
256 	} else {
257 		chan->note[note] = 0;
258 		if (ops->note_off)
259 			ops->note_off(drv, note, vel, chan);
260 	}
261 }
262 
263 /*
264  * Do all driver independent operations for this controller and pass
265  * events that need to take place immediately to the driver.
266  */
267 static void
268 do_control(struct snd_midi_op *ops, void *drv, struct snd_midi_channel_set *chset,
269 	   struct snd_midi_channel *chan, int control, int value)
270 {
271 	int  i;
272 
273 	if (control >= ARRAY_SIZE(chan->control))
274 		return;
275 
276 	/* Switches */
277 	if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) {
278 		/* These are all switches; either off or on so set to 0 or 127 */
279 		value = (value >= 64)? 127: 0;
280 	}
281 	chan->control[control] = value;
282 
283 	switch (control) {
284 	case MIDI_CTL_SUSTAIN:
285 		if (value == 0) {
286 			/* Sustain has been released, turn off held notes */
287 			for (i = 0; i < 128; i++) {
288 				if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
289 					chan->note[i] = SNDRV_MIDI_NOTE_OFF;
290 					if (ops->note_off)
291 						ops->note_off(drv, i, 0, chan);
292 				}
293 			}
294 		}
295 		break;
296 	case MIDI_CTL_PORTAMENTO:
297 		break;
298 	case MIDI_CTL_SOSTENUTO:
299 		if (value) {
300 			/* Mark each note that is currently held down */
301 			for (i = 0; i < 128; i++) {
302 				if (chan->note[i] & SNDRV_MIDI_NOTE_ON)
303 					chan->note[i] |= SNDRV_MIDI_NOTE_SOSTENUTO;
304 			}
305 		} else {
306 			/* release all notes that were held */
307 			for (i = 0; i < 128; i++) {
308 				if (chan->note[i] & SNDRV_MIDI_NOTE_SOSTENUTO) {
309 					chan->note[i] &= ~SNDRV_MIDI_NOTE_SOSTENUTO;
310 					if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
311 						chan->note[i] = SNDRV_MIDI_NOTE_OFF;
312 						if (ops->note_off)
313 							ops->note_off(drv, i, 0, chan);
314 					}
315 				}
316 			}
317 		}
318 		break;
319 	case MIDI_CTL_MSB_DATA_ENTRY:
320 		chan->control[MIDI_CTL_LSB_DATA_ENTRY] = 0;
321 		/* fall through */
322 	case MIDI_CTL_LSB_DATA_ENTRY:
323 		if (chan->param_type == SNDRV_MIDI_PARAM_TYPE_REGISTERED)
324 			rpn(ops, drv, chan, chset);
325 		else
326 			nrpn(ops, drv, chan, chset);
327 		break;
328 	case MIDI_CTL_REGIST_PARM_NUM_LSB:
329 	case MIDI_CTL_REGIST_PARM_NUM_MSB:
330 		chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
331 		break;
332 	case MIDI_CTL_NONREG_PARM_NUM_LSB:
333 	case MIDI_CTL_NONREG_PARM_NUM_MSB:
334 		chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
335 		break;
336 
337 	case MIDI_CTL_ALL_SOUNDS_OFF:
338 		all_sounds_off(ops, drv, chan);
339 		break;
340 
341 	case MIDI_CTL_ALL_NOTES_OFF:
342 		all_notes_off(ops, drv, chan);
343 		break;
344 
345 	case MIDI_CTL_MSB_BANK:
346 		if (chset->midi_mode == SNDRV_MIDI_MODE_XG) {
347 			if (value == 127)
348 				chan->drum_channel = 1;
349 			else
350 				chan->drum_channel = 0;
351 		}
352 		break;
353 	case MIDI_CTL_LSB_BANK:
354 		break;
355 
356 	case MIDI_CTL_RESET_CONTROLLERS:
357 		snd_midi_reset_controllers(chan);
358 		break;
359 
360 	case MIDI_CTL_SOFT_PEDAL:
361 	case MIDI_CTL_LEGATO_FOOTSWITCH:
362 	case MIDI_CTL_HOLD2:
363 	case MIDI_CTL_SC1_SOUND_VARIATION:
364 	case MIDI_CTL_SC2_TIMBRE:
365 	case MIDI_CTL_SC3_RELEASE_TIME:
366 	case MIDI_CTL_SC4_ATTACK_TIME:
367 	case MIDI_CTL_SC5_BRIGHTNESS:
368 	case MIDI_CTL_E1_REVERB_DEPTH:
369 	case MIDI_CTL_E2_TREMOLO_DEPTH:
370 	case MIDI_CTL_E3_CHORUS_DEPTH:
371 	case MIDI_CTL_E4_DETUNE_DEPTH:
372 	case MIDI_CTL_E5_PHASER_DEPTH:
373 		goto notyet;
374 	notyet:
375 	default:
376 		if (ops->control)
377 			ops->control(drv, control, chan);
378 		break;
379 	}
380 }
381 
382 
383 /*
384  * initialize the MIDI status
385  */
386 void
387 snd_midi_channel_set_clear(struct snd_midi_channel_set *chset)
388 {
389 	int i;
390 
391 	chset->midi_mode = SNDRV_MIDI_MODE_GM;
392 	chset->gs_master_volume = 127;
393 
394 	for (i = 0; i < chset->max_channels; i++) {
395 		struct snd_midi_channel *chan = chset->channels + i;
396 		memset(chan->note, 0, sizeof(chan->note));
397 
398 		chan->midi_aftertouch = 0;
399 		chan->midi_pressure = 0;
400 		chan->midi_program = 0;
401 		chan->midi_pitchbend = 0;
402 		snd_midi_reset_controllers(chan);
403 		chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
404 		chan->gm_rpn_fine_tuning = 0;
405 		chan->gm_rpn_coarse_tuning = 0;
406 
407 		if (i == 9)
408 			chan->drum_channel = 1;
409 		else
410 			chan->drum_channel = 0;
411 	}
412 }
413 EXPORT_SYMBOL(snd_midi_channel_set_clear);
414 
415 /*
416  * Process a rpn message.
417  */
418 static void
419 rpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
420     struct snd_midi_channel_set *chset)
421 {
422 	int type;
423 	int val;
424 
425 	if (chset->midi_mode != SNDRV_MIDI_MODE_NONE) {
426 		type = (chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB] << 8) |
427 			chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB];
428 		val = (chan->control[MIDI_CTL_MSB_DATA_ENTRY] << 7) |
429 			chan->control[MIDI_CTL_LSB_DATA_ENTRY];
430 
431 		switch (type) {
432 		case 0x0000: /* Pitch bend sensitivity */
433 			/* MSB only / 1 semitone per 128 */
434 			chan->gm_rpn_pitch_bend_range = val;
435 			break;
436 
437 		case 0x0001: /* fine tuning: */
438 			/* MSB/LSB, 8192=center, 100/8192 cent step */
439 			chan->gm_rpn_fine_tuning = val - 8192;
440 			break;
441 
442 		case 0x0002: /* coarse tuning */
443 			/* MSB only / 8192=center, 1 semitone per 128 */
444 			chan->gm_rpn_coarse_tuning = val - 8192;
445 			break;
446 
447 		case 0x7F7F: /* "lock-in" RPN */
448 			/* ignored */
449 			break;
450 		}
451 	}
452 	/* should call nrpn or rpn callback here.. */
453 }
454 
455 /*
456  * Process an nrpn message.
457  */
458 static void
459 nrpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
460      struct snd_midi_channel_set *chset)
461 {
462 	/* parse XG NRPNs here if possible */
463 	if (ops->nrpn)
464 		ops->nrpn(drv, chan, chset);
465 }
466 
467 
468 /*
469  * convert channel parameter in GS sysex
470  */
471 static int
472 get_channel(unsigned char cmd)
473 {
474 	int p = cmd & 0x0f;
475 	if (p == 0)
476 		p = 9;
477 	else if (p < 10)
478 		p--;
479 	return p;
480 }
481 
482 
483 /*
484  * Process a sysex message.
485  */
486 static void
487 sysex(struct snd_midi_op *ops, void *private, unsigned char *buf, int len,
488       struct snd_midi_channel_set *chset)
489 {
490 	/* GM on */
491 	static unsigned char gm_on_macro[] = {
492 		0x7e,0x7f,0x09,0x01,
493 	};
494 	/* XG on */
495 	static unsigned char xg_on_macro[] = {
496 		0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
497 	};
498 	/* GS prefix
499 	 * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
500 	 * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
501 	 * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
502 	 * master vol:  XX=0x00, YY=0x04, ZZ=0-127
503 	 */
504 	static unsigned char gs_pfx_macro[] = {
505 		0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
506 	};
507 
508 	int parsed = SNDRV_MIDI_SYSEX_NOT_PARSED;
509 
510 	if (len <= 0 || buf[0] != 0xf0)
511 		return;
512 	/* skip first byte */
513 	buf++;
514 	len--;
515 
516 	/* GM on */
517 	if (len >= (int)sizeof(gm_on_macro) &&
518 	    memcmp(buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
519 		if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
520 		    chset->midi_mode != SNDRV_MIDI_MODE_XG) {
521 			chset->midi_mode = SNDRV_MIDI_MODE_GM;
522 			reset_all_channels(chset);
523 			parsed = SNDRV_MIDI_SYSEX_GM_ON;
524 		}
525 	}
526 
527 	/* GS macros */
528 	else if (len >= 8 &&
529 		 memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
530 		if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
531 		    chset->midi_mode != SNDRV_MIDI_MODE_XG)
532 			chset->midi_mode = SNDRV_MIDI_MODE_GS;
533 
534 		if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) {
535 			/* GS reset */
536 			parsed = SNDRV_MIDI_SYSEX_GS_RESET;
537 			reset_all_channels(chset);
538 		}
539 
540 		else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) {
541 			/* drum pattern */
542 			int p = get_channel(buf[5]);
543 			if (p < chset->max_channels) {
544 				parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
545 				if (buf[7])
546 					chset->channels[p].drum_channel = 1;
547 				else
548 					chset->channels[p].drum_channel = 0;
549 			}
550 
551 		} else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) {
552 			/* program */
553 			int p = get_channel(buf[5]);
554 			if (p < chset->max_channels &&
555 			    ! chset->channels[p].drum_channel) {
556 				parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
557 				chset->channels[p].midi_program = buf[7];
558 			}
559 
560 		} else if (buf[5] == 0x01 && buf[6] == 0x30) {
561 			/* reverb mode */
562 			parsed = SNDRV_MIDI_SYSEX_GS_REVERB_MODE;
563 			chset->gs_reverb_mode = buf[7];
564 
565 		} else if (buf[5] == 0x01 && buf[6] == 0x38) {
566 			/* chorus mode */
567 			parsed = SNDRV_MIDI_SYSEX_GS_CHORUS_MODE;
568 			chset->gs_chorus_mode = buf[7];
569 
570 		} else if (buf[5] == 0x00 && buf[6] == 0x04) {
571 			/* master volume */
572 			parsed = SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME;
573 			chset->gs_master_volume = buf[7];
574 
575 		}
576 	}
577 
578 	/* XG on */
579 	else if (len >= (int)sizeof(xg_on_macro) &&
580 		 memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
581 		int i;
582 		chset->midi_mode = SNDRV_MIDI_MODE_XG;
583 		parsed = SNDRV_MIDI_SYSEX_XG_ON;
584 		/* reset CC#0 for drums */
585 		for (i = 0; i < chset->max_channels; i++) {
586 			if (chset->channels[i].drum_channel)
587 				chset->channels[i].control[MIDI_CTL_MSB_BANK] = 127;
588 			else
589 				chset->channels[i].control[MIDI_CTL_MSB_BANK] = 0;
590 		}
591 	}
592 
593 	if (ops->sysex)
594 		ops->sysex(private, buf - 1, len + 1, parsed, chset);
595 }
596 
597 /*
598  * all sound off
599  */
600 static void
601 all_sounds_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan)
602 {
603 	int n;
604 
605 	if (! ops->note_terminate)
606 		return;
607 	for (n = 0; n < 128; n++) {
608 		if (chan->note[n]) {
609 			ops->note_terminate(drv, n, chan);
610 			chan->note[n] = 0;
611 		}
612 	}
613 }
614 
615 /*
616  * all notes off
617  */
618 static void
619 all_notes_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan)
620 {
621 	int n;
622 
623 	if (! ops->note_off)
624 		return;
625 	for (n = 0; n < 128; n++) {
626 		if (chan->note[n] == SNDRV_MIDI_NOTE_ON)
627 			note_off(ops, drv, chan, n, 0);
628 	}
629 }
630 
631 /*
632  * Initialise a single midi channel control block.
633  */
634 static void snd_midi_channel_init(struct snd_midi_channel *p, int n)
635 {
636 	if (p == NULL)
637 		return;
638 
639 	memset(p, 0, sizeof(struct snd_midi_channel));
640 	p->private = NULL;
641 	p->number = n;
642 
643 	snd_midi_reset_controllers(p);
644 	p->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
645 	p->gm_rpn_fine_tuning = 0;
646 	p->gm_rpn_coarse_tuning = 0;
647 
648 	if (n == 9)
649 		p->drum_channel = 1;	/* Default ch 10 as drums */
650 }
651 
652 /*
653  * Allocate and initialise a set of midi channel control blocks.
654  */
655 static struct snd_midi_channel *snd_midi_channel_init_set(int n)
656 {
657 	struct snd_midi_channel *chan;
658 	int  i;
659 
660 	chan = kmalloc_array(n, sizeof(struct snd_midi_channel), GFP_KERNEL);
661 	if (chan) {
662 		for (i = 0; i < n; i++)
663 			snd_midi_channel_init(chan+i, i);
664 	}
665 
666 	return chan;
667 }
668 
669 /*
670  * reset all midi channels
671  */
672 static void
673 reset_all_channels(struct snd_midi_channel_set *chset)
674 {
675 	int ch;
676 	for (ch = 0; ch < chset->max_channels; ch++) {
677 		struct snd_midi_channel *chan = chset->channels + ch;
678 		snd_midi_reset_controllers(chan);
679 		chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
680 		chan->gm_rpn_fine_tuning = 0;
681 		chan->gm_rpn_coarse_tuning = 0;
682 
683 		if (ch == 9)
684 			chan->drum_channel = 1;
685 		else
686 			chan->drum_channel = 0;
687 	}
688 }
689 
690 
691 /*
692  * Allocate and initialise a midi channel set.
693  */
694 struct snd_midi_channel_set *snd_midi_channel_alloc_set(int n)
695 {
696 	struct snd_midi_channel_set *chset;
697 
698 	chset = kmalloc(sizeof(*chset), GFP_KERNEL);
699 	if (chset) {
700 		chset->channels = snd_midi_channel_init_set(n);
701 		chset->private_data = NULL;
702 		chset->max_channels = n;
703 	}
704 	return chset;
705 }
706 EXPORT_SYMBOL(snd_midi_channel_alloc_set);
707 
708 /*
709  * Reset the midi controllers on a particular channel to default values.
710  */
711 static void snd_midi_reset_controllers(struct snd_midi_channel *chan)
712 {
713 	memset(chan->control, 0, sizeof(chan->control));
714 	chan->gm_volume = 127;
715 	chan->gm_expression = 127;
716 	chan->gm_pan = 64;
717 }
718 
719 
720 /*
721  * Free a midi channel set.
722  */
723 void snd_midi_channel_free_set(struct snd_midi_channel_set *chset)
724 {
725 	if (chset == NULL)
726 		return;
727 	kfree(chset->channels);
728 	kfree(chset);
729 }
730 EXPORT_SYMBOL(snd_midi_channel_free_set);
731