/*
 *  GM/GS/XG midi module.
 *
 *  Copyright (C) 1999 Steve Ratcliffe
 *
 *  Based on awe_wave.c by Takashi Iwai
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
/*
 * This module is used to keep track of the current midi state.
 * It can be used for drivers that are required to emulate midi when
 * the hardware doesn't.
 *
 * It was written for a AWE64 driver, but there should be no AWE specific
 * code in here.  If there is it should be reported as a bug.
 */

#define SND_MAIN_OBJECT_FILE
#include "driver.h"
#include "seq_kernel.h"
#include "seq_midi_emul.h"

#ifdef MODULE_PARM		/* hey - we have new 2.1.18+ kernel... */
MODULE_AUTHOR("Takashi Iwai / Steve Ratcliffe");
MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI emulation.");
MODULE_SUPPORTED_DEVICE("sound");
#endif

/* Prototypes for static functions */
static void note_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, int note, int vel);
static void do_control(snd_midi_op_t *ops, void *private,
		       snd_midi_channel_set_t *chset, snd_midi_channel_t *chan,
		       int control, int value);
static void rpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset);
static void nrpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset);
static void sysex(snd_midi_op_t *ops, void *private, unsigned char *sysex, int len, snd_midi_channel_set_t *chset);
static void pre_note_proc(int client, snd_midi_channel_t *chan,
	 snd_seq_event_t *ev);
static void all_sounds_off(snd_midi_op_t *ops, void *private, snd_midi_channel_t *chan);
static void all_notes_off(snd_midi_op_t *ops, void *private, snd_midi_channel_t *chan);


/*
 * Process an event in a driver independant way.  This means dealing
 * with RPN, NRPN, SysEx etc that are defined for common midi applications
 * such as GM, GS and XG.
 * There modes that this module will run in are:
 *   Generic MIDI - no interpretation at all, it will just save current values
 *                  of controlers etc.
 *   GM - You can use all gm_ prefixed elements of chan.  Controls, RPN, NRPN,
 *        SysEx will be interpreded as defined in General Midi.
 *   GS - You can use all gs_ prefixed elements of chan. Codes for GS will be
 *        interpreted.
 *   XG - You can use all xg_ prefixed elements of chan.  Codes for XG will
 *        be interpreted.
 */
void
snd_midi_process_event(snd_midi_op_t *ops,
		       snd_seq_event_t *ev, snd_midi_channel_set_t *chanset)
{
	snd_midi_channel_t *chan;
	void *drv;

	if (ev == NULL || chanset == NULL) {
		snd_printd("ev or chanbase NULL (snd_midi_process_event)\n");
		return;
	}
	if (chanset->channels == NULL)
		return;
	if (ev->dest.channel >= chanset->max_channels) {
		snd_printd("dest channel is %d, max is %d\n", ev->dest.channel,
			chanset->max_channels);
		return;
	}

	chan = chanset->channels + ev->dest.channel;
	drv  = chanset->private_data;

	/*
	 * First deal with EVENT_NOTE, this is converted into a NOTE_ON
	 * and the corresponding NOTE_OFF, which is queued.
	 */
	if (ev->type == SND_SEQ_EVENT_NOTE)
		pre_note_proc(chanset->client, chan, ev);

	/* Make sure that we don't have a note on that should really be
	 * a note off */
	if (ev->type == SND_SEQ_EVENT_NOTEON && ev->data.note.velocity == 0)
		ev->type = SND_SEQ_EVENT_NOTEOFF;

	/* Make sure the note is within array range */
	if (ev->type == SND_SEQ_EVENT_NOTEON ||
	    ev->type == SND_SEQ_EVENT_NOTEOFF ||
	    ev->type == SND_SEQ_EVENT_KEYPRESS) {
		if (ev->data.note.note >= 128)
			return;
	}

	switch (ev->type) {
	case SND_SEQ_EVENT_NOTEON:
		chan->note[ev->data.note.note] = SND_MIDI_NOTE_ON;
		ops->note_on(drv, ev->data.note.note, ev->data.note.velocity, chan);
		break;
	case SND_SEQ_EVENT_NOTEOFF:
		note_off(ops, drv, chan, ev->data.note.note, ev->data.note.velocity);
		break;
	case SND_SEQ_EVENT_KEYPRESS:
		ops->key_press(drv, ev->data.note.note, ev->data.note.velocity, chan);
		break;
	case SND_SEQ_EVENT_CONTROLLER:
		do_control(ops, drv, chanset, chan,
			   ev->data.control.param, ev->data.control.value);
		break;
	case SND_SEQ_EVENT_PGMCHANGE:
		chan->midi_program = ev->data.control.value;
		break;
	case SND_SEQ_EVENT_PITCHBEND:
		chan->midi_pitchbend = ev->data.control.value;
		ops->control(drv, SND_MCTL_PITCHBEND, chan);
		break;
	case SND_SEQ_EVENT_CHANPRESS:
		chan->midi_pressure = ev->data.control.value;
		ops->control(drv, SND_MCTL_CHAN_PRESSURE, chan);
		break;
	case SND_SEQ_EVENT_CONTROL14:
		/* Best guess is that this is any of the 14 bit controller values */
		if (ev->data.control.param >= 0 && ev->data.control.param < 32) {
			/* set low part first */
			chan->control[ev->data.control.param + 32] =
				ev->data.control.value & 0x7f;
			do_control(ops, drv, chanset, chan,
				   ev->data.control.param,
				   ((ev->data.control.value>>7) & 0x7f));
		} else
			do_control(ops, drv, chanset, chan,
				   ev->data.control.param,
				   ev->data.control.value);
		break;
	case SND_SEQ_EVENT_NONREGPARAM:
		/* Break it back into its controler values */
		chan->param_type = SND_MIDI_PARAM_TYPE_NONREGISTERED;
		chan->control[SND_MCTL_MSB_DATA_ENTRY]
			= (ev->data.control.value >> 7) & 0x7f;
		chan->control[SND_MCTL_LSB_DATA_ENTRY]
			= ev->data.control.value & 0x7f;
		chan->control[SND_MCTL_NONREG_PARM_NUM_MSB]
			= (ev->data.control.param >> 7) & 0x7f;
		chan->control[SND_MCTL_NONREG_PARM_NUM_LSB]
			= ev->data.control.param & 0x7f;
		nrpn(ops, drv, chan, chanset);
		break;
	case SND_SEQ_EVENT_REGPARAM:
		/* Break it back into its controler values */
		chan->param_type = SND_MIDI_PARAM_TYPE_REGISTERED;
		chan->control[SND_MCTL_MSB_DATA_ENTRY]
			= (ev->data.control.value >> 7) & 0x7f;
		chan->control[SND_MCTL_LSB_DATA_ENTRY]
			= ev->data.control.value & 0x7f;
		chan->control[SND_MCTL_REGIST_PARM_NUM_MSB]
			= (ev->data.control.param >> 7) & 0x7f;
		chan->control[SND_MCTL_REGIST_PARM_NUM_LSB]
			= ev->data.control.param & 0x7f;
		rpn(ops, drv, chan, chanset);
		break;
	case SND_SEQ_EVENT_SYSEX:
		sysex(ops, drv, (unsigned char*)ev->data.ext.ptr,
		      ev->data.ext.len, chanset);
		break;
	case SND_SEQ_EVENT_SONGPOS:
	case SND_SEQ_EVENT_SONGSEL:
	case SND_SEQ_EVENT_CLOCK:
	case SND_SEQ_EVENT_START:
	case SND_SEQ_EVENT_CONTINUE:
	case SND_SEQ_EVENT_STOP:
	case SND_SEQ_EVENT_QFRAME:
	case SND_SEQ_EVENT_TEMPO:
	case SND_SEQ_EVENT_TIMESIGN:
	case SND_SEQ_EVENT_KEYSIGN:
		goto not_yet;
	case SND_SEQ_EVENT_HEARTBEAT:
		break;
	case SND_SEQ_EVENT_CLIENT_START:
	case SND_SEQ_EVENT_CLIENT_EXIT:
	case SND_SEQ_EVENT_CLIENT_CHANGE:
	case SND_SEQ_EVENT_PORT_START:
	case SND_SEQ_EVENT_PORT_EXIT:
	case SND_SEQ_EVENT_PORT_CHANGE:
	case SND_SEQ_EVENT_SAMPLE:
	case SND_SEQ_EVENT_SAMPLE_START:
	case SND_SEQ_EVENT_SAMPLE_STOP:
	case SND_SEQ_EVENT_SAMPLE_FREQ:
	case SND_SEQ_EVENT_SAMPLE_VOLUME:
	case SND_SEQ_EVENT_SAMPLE_LOOP:
	case SND_SEQ_EVENT_SAMPLE_POSITION:
	case SND_SEQ_EVENT_ECHO:
	not_yet:
	default:
		snd_printd("Unimplemented event %d\n", ev->type);
		break;
	}
}


/*
 * release note
 */
static void
note_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, int note, int vel)
{
	if (chan->gm_hold) {
		/* Hold this note until pedal is turned off */
		chan->note[note] |= SND_MIDI_NOTE_RELEASED;
	} else if (chan->note[note] & SND_MIDI_NOTE_SUSTENUTO) {
		/* Mark this note as release; it will be turned off when sustenuto
		 * is turned off */
		chan->note[note] |= SND_MIDI_NOTE_RELEASED;
	} else {
		chan->note[note] = 0;
		ops->note_off(drv, note, vel, chan);
	}
}

/*
 * Do all driver independant operations for this controler and pass
 * events that need to take place immediately to the driver.
 */
static void
do_control(snd_midi_op_t *ops, void *drv, snd_midi_channel_set_t *chset,
	   snd_midi_channel_t *chan, int control, int value)
{
	int  i;

	/* Switches */
	if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) {
		/* These are all switches; either off or on so set to 0 or 127 */
		value = (value >= 64)? 127: 0;
	}
	chan->control[control] = value;

	switch (control) {
	case SND_MCTL_SUSTAIN:
		if (value == 0) {
			/* Sustain has been released, turn off held notes */
			for (i = 0; i < 128; i++) {
				if (chan->note[i] & SND_MIDI_NOTE_RELEASED) {
					chan->note[i] = SND_MIDI_NOTE_OFF;
					ops->note_off(drv, i, 0, chan);
				}
			}
		}
		break;
	case SND_MCTL_PORTAMENTO:
		break;
	case SND_MCTL_SUSTENUTO:
		if (value) {
			/* Mark each note that is currently held down */
			for (i = 0; i < 128; i++) {
				if (chan->note[i] & SND_MIDI_NOTE_ON)
					chan->note[i] |= SND_MIDI_NOTE_SUSTENUTO;
			}
		} else {
			/* release all notes that were held */
			for (i = 0; i < 128; i++) {
				if (chan->note[i] & SND_MIDI_NOTE_SUSTENUTO) {
					chan->note[i] &= ~SND_MIDI_NOTE_SUSTENUTO;
					if (chan->note[i] & SND_MIDI_NOTE_RELEASED) {
						chan->note[i] = SND_MIDI_NOTE_OFF;
						ops->note_off(drv, i, 0, chan);
					}
				}
			}
		}
		break;
	case SND_MCTL_MSB_DATA_ENTRY:
		chan->control[SND_MCTL_LSB_DATA_ENTRY] = 0;
		/* go through here */
	case SND_MCTL_LSB_DATA_ENTRY:
		if (chan->param_type == SND_MIDI_PARAM_TYPE_REGISTERED)
			rpn(ops, drv, chan, chset);
		else
			nrpn(ops, drv, chan, chset);
		break;
	case SND_MCTL_REGIST_PARM_NUM_LSB:
	case SND_MCTL_REGIST_PARM_NUM_MSB:
		chan->param_type = SND_MIDI_PARAM_TYPE_REGISTERED;
		break;
	case SND_MCTL_NONREG_PARM_NUM_LSB:
	case SND_MCTL_NONREG_PARM_NUM_MSB:
		chan->param_type = SND_MIDI_PARAM_TYPE_NONREGISTERED;
		break;

	case SND_MCTL_ALL_SOUNDS_OFF:
		all_sounds_off(ops, drv, chan);
		break;

	case SND_MCTL_ALL_NOTES_OFF:
		all_notes_off(ops, drv, chan);
		break;

	case SND_MCTL_SOFT_PEDAL:
	case SND_MCTL_LEGATO_FOOTSWITCH:
	case SND_MCTL_HOLD2:
	case SND_MCTL_SC1_SOUND_VARIATION:
	case SND_MCTL_SC2_TIMBRE:
	case SND_MCTL_SC3_RELEASE_TIME:
	case SND_MCTL_SC4_ATTACK_TIME:
	case SND_MCTL_SC5_BRIGHTNESS:
	case SND_MCTL_E1_REVERB_DEPTH:
	case SND_MCTL_E2_TREMOLO_DEPTH:
	case SND_MCTL_E3_CHORUS_DEPTH:
	case SND_MCTL_E4_DETUNE_DEPTH:
	case SND_MCTL_E5_PHASER_DEPTH:
		goto notyet;
	notyet:
	default:
		if (ops->control)
			ops->control(drv, control, chan);
		break;
	}
}


/*
 * intialize the MIDI status
 */
void
snd_midi_channel_set_clear(snd_midi_channel_set_t *chset)
{
	int i;

	chset->midi_mode = SND_MIDI_MODE_GM;
	chset->gs_master_volume = 127;

	for (i = 0; i < chset->max_channels; i++) {
		snd_midi_channel_t *chan = chset->channels + i;
		memset(chan->note, 0, sizeof(chan->note));
		memset(chan->control, 0, sizeof(chan->control));
		chan->midi_aftertouch = 0;
		chan->midi_pressure = 0;
		chan->midi_program = 0;
		chan->midi_pitchbend = 0;
		chan->gm_volume = 127;
		chan->gm_expression = 127;
		chan->gm_pan = 64;
		chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
		chan->gm_rpn_fine_tuning = 0;
		chan->gm_rpn_coarse_tuning = 0;
		if (i == 9)
			chan->drum_channel = 1;
		else
			chan->drum_channel = 0;
	}
}


/*
 * Process a rpn message.
 */
static void
rpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan,
    snd_midi_channel_set_t *chset)
{
	int type;
	int val;

	if (chset->midi_mode != SND_MIDI_MODE_NONE) {
		type = (chan->control[SND_MCTL_REGIST_PARM_NUM_MSB] << 8) |
			chan->control[SND_MCTL_REGIST_PARM_NUM_LSB];
		val = (chan->control[SND_MCTL_MSB_DATA_ENTRY] << 7) |
			chan->control[SND_MCTL_LSB_DATA_ENTRY];

		switch (type) {
		case 0x0000: /* Pitch bend sensitivity */
			/* MSB only / 1 semitone per 128 */
			chan->gm_rpn_pitch_bend_range = val;
			break;
					
		case 0x0001: /* fine tuning: */
			/* MSB/LSB, 8192=center, 100/8192 cent step */
			chan->gm_rpn_fine_tuning = val - 8192;
			break;

		case 0x0002: /* coarse tuning */
			/* MSB only / 8192=center, 1 semitone per 128 */
			chan->gm_rpn_coarse_tuning = val - 8192;
			break;

		case 0x7F7F: /* "lock-in" RPN */
			/* ignored */
			break;
		}
	}
	/* should call nrpn or rpn callback here.. */
}

/*
 * Process an nrpn message.
 */
static void
nrpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan,
     snd_midi_channel_set_t *chset)
{
	/* parse XG NRPNs here if possible */
	ops->nrpn(drv, chan, chset);
}


/*
 * convert channel parameter in GS sysex
 */
static int
get_channel(unsigned char cmd)
{
	int p = cmd & 0x0f;
	if (p == 0)
		p = 9;
	else if (p < 10)
		p--;
	return p;
}


/*
 * Process a sysex message.
 */
static void
sysex(snd_midi_op_t *ops, void *private, unsigned char *buf, int len, snd_midi_channel_set_t *chset)
{
	/* GM on */
	static unsigned char gm_on_macro[] = {
		0x7e,0x7f,0x09,0x01,
	};
	/* XG on */
	static unsigned char xg_on_macro[] = {
		0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
	};
	/* GS prefix
	 * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
	 * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
	 * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
	 * master vol:  XX=0x00, YY=0x04, ZZ=0-127
	 */
	static unsigned char gs_pfx_macro[] = {
		0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
	};

	int parsed = SND_MIDI_SYSEX_NOT_PARSED;

	if (len <= 0 || buf[0] != 0xf0)
		return;
	/* skip first byte */
	buf++;
	len--;

	/* GM on */
	if (len >= sizeof(gm_on_macro) &&
	    memcmp(buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
		if (chset->midi_mode != SND_MIDI_MODE_GS &&
		    chset->midi_mode != SND_MIDI_MODE_XG) {
			chset->midi_mode = SND_MIDI_MODE_GM;
			/*init_midi_status(chset);*/
			parsed = SND_MIDI_SYSEX_GM_ON;
		}
	}

	/* GS macros */
	else if (len >= 8 &&
		 memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
		if (chset->midi_mode != SND_MIDI_MODE_GS &&
		    chset->midi_mode != SND_MIDI_MODE_XG)
			chset->midi_mode = SND_MIDI_MODE_GS;

		if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) {
			/* GS reset */
			parsed = SND_MIDI_SYSEX_GS_RESET;
			/*init_midi_status(chset);*/
		}

		else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) {
			/* drum pattern */
			int p = get_channel(buf[5]);
			if (p < chset->max_channels) {
				parsed = SND_MIDI_SYSEX_GS_DRUM_CHANNEL;
				if (buf[7])
					chset->channels[p].drum_channel = 1;
				else
					chset->channels[p].drum_channel = 0;
			}

		} else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) {
			/* program */
			int p = get_channel(buf[5]);
			if (p < chset->max_channels &&
			    ! chset->channels[p].drum_channel) {
				parsed = SND_MIDI_SYSEX_GS_DRUM_CHANNEL;
				chset->channels[p].midi_program = buf[7];
			}

		} else if (buf[5] == 0x01 && buf[6] == 0x30) {
			/* reverb mode */
			parsed = SND_MIDI_SYSEX_GS_CHORUS_MODE;
			chset->gs_reverb_mode = buf[7];

		} else if (buf[5] == 0x01 && buf[6] == 0x38) {
			/* chorus mode */
			parsed = SND_MIDI_SYSEX_GS_REVERB_MODE;
			chset->gs_chorus_mode = buf[7];

		} else if (buf[5] == 0x00 && buf[6] == 0x04) {
			/* master volume */
			parsed = SND_MIDI_SYSEX_GS_REVERB_MODE;
			chset->gs_master_volume = buf[7];

		}
	}

	/* XG on */
	else if (len >= sizeof(xg_on_macro) &&
		 memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
		int i;
		chset->midi_mode = SND_MIDI_MODE_XG;
		parsed = SND_MIDI_SYSEX_XG_ON;
		/* reset CC#0 for drums */
		for (i = 0; i < chset->max_channels; i++) {
			if (chset->channels[i].drum_channel)
				chset->channels[i].control[SND_MCTL_MSB_BANK] = 127;
			else
				chset->channels[i].control[SND_MCTL_MSB_BANK] = 0;
		}
	}

	ops->sysex(private, buf - 1, len + 1, parsed, chset);
}

/*
 * Deal with the NOTE event.  This is converted into a note on event
 * and a corresponding note off event.
 */
static void
pre_note_proc(int client, snd_midi_channel_t *chan, snd_seq_event_t *ev)
{
	snd_seq_event_t offev;

	/* Convert this one */
	ev->type = SND_SEQ_EVENT_NOTEON;

	/* Enqueue the note off first, to make sure */
	memcpy(&offev, ev, sizeof(offev));
	switch (offev.flags & SND_SEQ_TIME_STAMP_MASK) {
	case SND_SEQ_TIME_STAMP_TICK:
		offev.time.tick = offev.data.note.duration;
		break;
	case SND_SEQ_TIME_STAMP_REAL:
		/* unit for duration is ms */
		offev.time.real.tv_sec = ev->data.note.duration / 1000;
		offev.time.real.tv_nsec =
			1000000 * (ev->data.note.duration % 1000);
		break;
	}

	offev.flags = (offev.flags & ~SND_SEQ_TIME_MODE_MASK) | SND_SEQ_TIME_MODE_REL;
	offev.type = SND_SEQ_EVENT_NOTEOFF;
	snd_seq_kernel_client_enqueue(client, &offev, 1);
}

/*
 * all sound off
 */
static void
all_sounds_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan)
{
	int n;

	if (! ops->note_terminate)
		return;
	for (n = 0; n < 128; n++) {
		if (chan->note[n]) {
			ops->note_terminate(drv, n, chan);
			chan->note[n] = 0;
		}
	}
}

/*
 * all notes off
 */
static void
all_notes_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan)
{
	int n;

	if (! ops->note_off)
		return;
	for (n = 0; n < 128; n++) {
		if (chan->note[n] == SND_MIDI_NOTE_ON)
			note_off(ops, drv, chan, n, 0);
	}
}

/*
 * Initialise a single midi channel control block.
 */
void snd_midi_channel_init(snd_midi_channel_t *p, int n)
{
	if (!p)
		return;

	memset(p, 0, sizeof(snd_midi_channel_t));
	p->private = NULL;
	p->number = n;

	p->gm_volume = 127;
	p->gm_expression = 127;
	p->gm_pan = 64;
	p->gm_rpn_pitch_bend_range = 256; /* 2 octaves */

	if (n == 9)
		p->drum_channel = 1;	/* Default ch 10 as drums */
}

/*
 * Allocate and initialise a set of midi channel control blocks.
 */
snd_midi_channel_t *snd_midi_channel_init_set(int n)
{
	snd_midi_channel_t *chan;
	int  i;

	chan = snd_malloc(n * sizeof(snd_midi_channel_t));
	if (chan) {
		for (i = 0; i < n; i++)
			snd_midi_channel_init(chan+i, i);
	}

	return chan;
}

/*
 * Allocate and initialise a midi channel set.
 */
snd_midi_channel_set_t *snd_midi_channel_alloc_set(int n)
{
	snd_midi_channel_set_t *chset;

	chset = snd_malloc(sizeof(*chset));
	chset->channels = snd_midi_channel_init_set(n);

	chset->private_data = NULL;
	chset->max_channels = n;
	return chset;
}

/*
 * Free a midi channel set.
 */
void snd_midi_channel_free_set(snd_midi_channel_set_t *chset)
{
	if (chset == NULL)
		return;
	if (chset->channels != NULL)
		snd_free(chset->channels, sizeof(snd_midi_channel_t) *
					  chset->max_channels);
	snd_free(chset, sizeof(*chset));
}

#ifndef LINUX_2_1
extern struct symbol_table snd_symbol_table_seq_midi_emul_export;
#endif

int init_module(void)
{
#ifndef LINUX_2_1
	if (register_symtab(&snd_symbol_table_seq_midi_emul_export) < 0)
		return -ENOMEM;
#endif
	return 0;
}


void cleanup_module(void)
{
}

