/*
 *  Routines for Trident 4DWave NX/DX soundcards - Synthesizer
 *  Copyright (c) by Scott McNab <jedi@tartarus.uwa.edu.au>
 *
 *
 *   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.
 *
 */

#define SND_MAIN_OBJECT_FILE
#include "../../include/driver.h"
#include "../../include/trident.h"
#include "../../include/seq_device.h"

static void snd_trident_synth_use_inc(trident_t * trident)
{
	MOD_INC_USE_COUNT;
}

static void snd_trident_synth_use_dec(trident_t * trident)
{
	MOD_DEC_USE_COUNT;
}

/*
 * Sample handling operations
 */

static void sample_start(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position);
static void sample_stop(trident_t * trident, snd_trident_voice_t * voice, snd_seq_stop_mode_t mode);
static void sample_freq(trident_t * trident, snd_trident_voice_t * voice, snd_seq_frequency_t freq);
static void sample_volume(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_volume * volume);
static void sample_loop(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_loop * loop);
static void sample_pos(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position);
static void sample_private1(trident_t * trident, snd_trident_voice_t * voice, unsigned char *data);

static snd_trident_sample_ops_t sample_ops =
{
	sample_start,
	sample_stop,
	sample_freq,
	sample_volume,
	sample_loop,
	sample_pos,
	sample_private1
};

static void snd_trident_simple_init(snd_trident_voice_t * voice)
{
	//voice->handler_wave = interrupt_wave;
	//voice->handler_volume = interrupt_volume;
	//voice->handler_effect = interrupt_effect;
	//voice->volume_change = NULL;
	voice->sample_ops = &sample_ops;
}

static void sample_start(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position)
{
	simple_instrument_t *simple;
	snd_seq_kinstr_t *instr;
	unsigned long flags;
	unsigned int loop_start, loop_end, sample_start, sample_end;

	instr = snd_seq_instr_find(trident->synth.ilist, &voice->instr, 0, 1);
	if (instr == NULL)
		return;
	voice->instr = instr->instr;	/* copy ID to speedup aliases */
	simple = KINSTR_DATA(instr);

	spin_lock_irqsave(&trident->reg_lock, flags);
	voice->EC = 0;		/* fixme: do something with envelopes later */
	voice->CTRL = 0;
	voice->Alpha_FMS = 0;

	loop_start = simple->loop_start >> 4;
	loop_end = simple->loop_end >> 4;
	sample_start = simple->start >> 4;
	sample_end = simple->size;

	if (simple->format & SIMPLE_WAVE_16BIT)
		voice->CTRL |= 8;
	if (simple->format & SIMPLE_WAVE_STEREO)
		voice->CTRL |= 4;
	if (!(simple->format & SIMPLE_WAVE_UNSIGNED))
		voice->CTRL |= 2;

	/* fixme: handle looped samples proplely here.. */
	if (simple->format & SIMPLE_WAVE_LOOP) {
		voice->CTRL |= 1;
		voice->LBA = virt_to_bus(simple->address.ptr + loop_start);
		voice->CSO = 0;	//simple->start;

		voice->ESO = loop_end - loop_start - 1;
	} else {
		voice->LBA = virt_to_bus(simple->address.ptr);
		voice->CSO = sample_start;
		voice->ESO = sample_end - 1;
	}

	if (voice->flags & SND_TRIDENT_VFLG_RUNNING) {
		snd_trident_stop_voice(trident, voice->number);
		voice->flags &= ~SND_TRIDENT_VFLG_RUNNING;
	}
	snd_trident_write_voice_regs(trident, voice->number,
				     voice->LBA, voice->CSO, voice->ESO,
				     voice->Delta, voice->Alpha_FMS,
				     ((voice->FMC & 0x03) << 14) | ((voice->RVol & 0x7f) << 7) | (voice->CVol & 0x7f),
				     voice->GVSel, voice->Pan, voice->Vol,
				     voice->CTRL, voice->EC);
	snd_trident_start_voice(trident, voice->number);
	voice->flags |= SND_TRIDENT_VFLG_RUNNING;
	spin_unlock_irqrestore(&trident->reg_lock, flags);
	snd_seq_instr_free_use(trident->synth.ilist, instr);
}

static void sample_stop(trident_t * trident, snd_trident_voice_t * voice, snd_seq_stop_mode_t mode)
{
	unsigned long flags;

	if (!(voice->flags & SND_TRIDENT_VFLG_RUNNING))
		return;

	switch (mode) {
	default:
		spin_lock_irqsave(&trident->reg_lock, flags);
		snd_trident_stop_voice(trident, voice->number);
		voice->flags &= ~SND_TRIDENT_VFLG_RUNNING;
		spin_unlock_irqrestore(&trident->reg_lock, flags);
		break;
	case SAMPLE_STOP_LOOP:	/* disable loop only */
		voice->CTRL &= ~1;
		spin_lock_irqsave(&trident->reg_lock, flags);
		outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
		outw((((voice->CTRL << 12) | (voice->EC & 0x0fff)) & 0xffff), CH_GVSEL_PAN_VOL_CTRL_EC);
		spin_unlock_irqrestore(&trident->reg_lock, flags);
		break;
	}
}

static void sample_freq(trident_t * trident, snd_trident_voice_t * voice, snd_seq_frequency_t freq)
{
	unsigned long flags;
	freq >>= 4;

	spin_lock_irqsave(&trident->reg_lock, flags);
	if (freq == 44100)
		voice->Delta = 0xeb3;
	else if (freq == 8000)
		voice->Delta = 0x2ab;
	else if (freq == 48000)
		voice->Delta = 0x1000;
	else
		voice->Delta = (((freq << 12) + freq) / 48000) & 0x0000ffff;

	outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
	if (trident->isNX) {
		outb((unsigned char) voice->Delta, TRID_REG(trident, CH_NX_DELTA_CSO + 3));
		outb((unsigned char) (voice->Delta >> 8), TRID_REG(trident, CH_NX_DELTA_ESO + 3));
	} else {
		outw((unsigned short) voice->Delta, TRID_REG(trident, CH_DX_ESO_DELTA));
	}

	spin_unlock_irqrestore(&trident->reg_lock, flags);
}

static void sample_volume(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_volume * volume)
{
	unsigned long flags;
	unsigned short value;

	spin_lock_irqsave(&trident->reg_lock, flags);
	voice->GVSel = 0;	/* use global music volume */
	voice->FMC = 0x03;	/* fixme: can we do something useful with FMC? */
	if (volume->volume >= 0) {
		volume->volume &= 0x3fff;
		/* fixme: need correct volume translation here (linear->log) */
		voice->Vol = (0x3fff - volume->volume) >> 6;
		voice->RVol = 0;
		voice->CVol = 0;
	}
	if (volume->lr >= 0) {
		volume->lr &= 0x3fff;
		/* fixme: need correct pan value here, for now just hack left/right */
		if (volume->lr >= 0x2000)
			voice->Pan = 0x3f;	/* attenuate left (pan right) */
		else
			voice->Pan = 0x7f;	/* attenuate right (pan left) */
	}
	outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
	value = (voice->GVSel << 15) | ((voice->Pan & 0x0000007f) << 8) | (voice->Vol & 0x000000ff);
	outw(value, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
	value = ((voice->FMC & 0x03) << 14) | ((voice->RVol & 0x7f) << 7) | (voice->CVol & 0x7f);
	outw(value, TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
	spin_unlock_irqrestore(&trident->reg_lock, flags);
}

static void sample_loop(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_loop * loop)
{
	unsigned long flags;
	simple_instrument_t *simple;
	snd_seq_kinstr_t *instr;
	unsigned int loop_start, loop_end;

	instr = snd_seq_instr_find(trident->synth.ilist, &voice->instr, 0, 1);
	if (instr == NULL)
		return;
	voice->instr = instr->instr;	/* copy ID to speedup aliases */
	simple = KINSTR_DATA(instr);

	loop_start = simple->loop_start >> 4;
	loop_end = simple->loop_end >> 4;

	spin_lock_irqsave(&trident->reg_lock, flags);
	/* fixme: correct for hacked loop */
	voice->LBA = virt_to_bus(simple->address.ptr + loop_start);
	voice->CSO = 0;
	voice->ESO = loop_end - loop_start - 1;

	outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
	outb((voice->LBA >> 16), TRID_REG(trident, CH_LBA + 2));
	outw((voice->LBA & 0xffff), TRID_REG(trident, CH_LBA));
	if (trident->isNX) {
		outb((voice->ESO >> 16), TRID_REG(trident, CH_NX_DELTA_ESO + 2));
		outw((voice->ESO & 0xffff), TRID_REG(trident, CH_NX_DELTA_ESO));
		outb((voice->CSO >> 16), TRID_REG(trident, CH_NX_DELTA_CSO + 2));
		outw((voice->CSO & 0xffff), TRID_REG(trident, CH_NX_DELTA_CSO));
	} else {
		outw((voice->ESO & 0xffff), TRID_REG(trident, CH_DX_ESO_DELTA + 2));
		outw((voice->CSO & 0xffff), TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
	}

	spin_unlock_irqrestore(&trident->reg_lock, flags);
	snd_seq_instr_free_use(trident->synth.ilist, instr);
}

static void sample_pos(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position)
{
	unsigned long flags;
	simple_instrument_t *simple;
	snd_seq_kinstr_t *instr;

	instr = snd_seq_instr_find(trident->synth.ilist, &voice->instr, 0, 1);
	if (instr == NULL)
		return;
	voice->instr = instr->instr;	/* copy ID to speedup aliases */
	simple = KINSTR_DATA(instr);

	spin_lock_irqsave(&trident->reg_lock, flags);

	/* fixme: adjustment for hacked looping.. */
	if (simple->format & SIMPLE_WAVE_LOOP) {
		voice->CSO = (position - simple->loop_start) >> 4;

	} else {
		voice->CSO = position >> 4;
	}

	outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
	if (trident->isNX) {
		outw((voice->CSO & 0xffff), TRID_REG(trident, CH_NX_DELTA_CSO));
		outb((voice->CSO >> 16), TRID_REG(trident, CH_NX_DELTA_CSO + 2));
	} else {
		outw((voice->CSO & 0xffff), TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
	}

	spin_unlock_irqrestore(&trident->reg_lock, flags);
	snd_seq_instr_free_use(trident->synth.ilist, instr);
}

static void sample_private1(trident_t * trident, snd_trident_voice_t * voice, unsigned char *data)
{
}

/*
 * Memory management / sample loading
 */

static int snd_trident_simple_put_sample(void *private_data, simple_instrument_t * instr,
					 char *data, long len, int atomic)
{
	trident_t *trident = (trident_t *) private_data;
	unsigned char *block = NULL;
	int size = instr->size;

	if (instr->format & SIMPLE_WAVE_BACKWARD ||
	    instr->format & SIMPLE_WAVE_BIDIR ||
	    instr->format & SIMPLE_WAVE_ULAW)
		return -EINVAL;	/* not supported */

	if (instr->format & SIMPLE_WAVE_16BIT)
		size <<= 1;
	if (instr->format & SIMPLE_WAVE_STEREO)
		size <<= 1;

	if (trident->synth.current_size + size > trident->synth.max_size)
		return -ENOMEM;

	if (verify_area(VERIFY_READ, data, size))
		return -EFAULT;

	block = (unsigned char *) snd_kmalloc(size, GFP_KERNEL | TRIDENT_GFP_FLAGS);
	if (block == NULL)
		return -ENOMEM;

	copy_from_user(block, data, size);

	instr->address.ptr = block;
	trident->synth.current_size += size;
	return 0;
}

static int snd_trident_simple_get_sample(void *private_data, simple_instrument_t * instr,
					 char *data, long len, int atomic)
{
	//trident_t *trident = (trident_t *)private_data;
	int size = instr->size;

	if (instr->format & SIMPLE_WAVE_16BIT)
		size <<= 1;
	if (instr->format & SIMPLE_WAVE_STEREO)
		size <<= 1;

	if (verify_area(VERIFY_WRITE, data, size))
		return -EFAULT;

	copy_to_user(data, instr->address.ptr, size);

	return 0;
}

static int snd_trident_simple_remove_sample(void *private_data, simple_instrument_t * instr,
					    int atomic)
{
	trident_t *trident = (trident_t *) private_data;
	int size = instr->size;

	snd_kfree(instr->address.ptr);

	if (instr->format & SIMPLE_WAVE_16BIT)
		size <<= 1;
	if (instr->format & SIMPLE_WAVE_STEREO)
		size <<= 1;

	trident->synth.current_size -= size;
	if (trident->synth.current_size < 0)	/* shouldnt need this check... */
		trident->synth.current_size = 0;

	return 0;
}

static void select_instrument(trident_t * trident, snd_trident_voice_t * v)
{
	snd_seq_kinstr_t *instr;
	instr = snd_seq_instr_find(trident->synth.ilist, &v->instr, 0, 1);
	if (instr != NULL) {
		if (instr->ops) {
			if (instr->ops->instr_type == snd_seq_simple_id)
				snd_trident_simple_init(v);
		}
		snd_seq_instr_free_use(trident->synth.ilist, instr);
	}
}

/*

 */

static void event_sample(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_stop)
		v->sample_ops->sample_stop(p->trident, v, SAMPLE_STOP_IMMEDIATELY);
	v->instr.std = ev->data.sample.std;
	v->instr.bank = ev->data.sample.bank;
	v->instr.prg = ev->data.sample.prg;
	select_instrument(p->trident, v);
}

static void event_cluster(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_stop)
		v->sample_ops->sample_stop(p->trident, v, SAMPLE_STOP_IMMEDIATELY);
	v->instr.cluster = ev->data.cluster.cluster;
	select_instrument(p->trident, v);
}

static void event_start(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_start)
		v->sample_ops->sample_start(p->trident, v, ev->data.position);
}

static void event_stop(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_stop)
		v->sample_ops->sample_stop(p->trident, v, ev->data.stop_mode);
}

static void event_freq(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_freq)
		v->sample_ops->sample_freq(p->trident, v, ev->data.frequency);
}

static void event_volume(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_volume)
		v->sample_ops->sample_volume(p->trident, v, &ev->data.volume);
}

static void event_loop(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_loop)
		v->sample_ops->sample_loop(p->trident, v, &ev->data.loop);
}

static void event_position(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_pos)
		v->sample_ops->sample_pos(p->trident, v, ev->data.position);
}

static void event_private1(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
{
	if (v->sample_ops && v->sample_ops->sample_private1)
		v->sample_ops->sample_private1(p->trident, v, (unsigned char *) &ev->data.raw8);
}

typedef void (trident_sample_event_handler_t) (snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v);

static trident_sample_event_handler_t *trident_sample_event_handlers[9] =
{
	event_sample,
	event_cluster,
	event_start,
	event_stop,
	event_freq,
	event_volume,
	event_loop,
	event_position,
	event_private1
};

static void snd_trident_sample_event(snd_seq_event_t * ev, snd_trident_port_t * p)
{
	int idx, voice;
	trident_t *trident = p->trident;
	snd_trident_voice_t *v;
	unsigned long flags;

	idx = ev->type - SND_SEQ_EVENT_SAMPLE;
	if (idx < 0 || idx > 8)
		return;
	for (voice = 0; voice < 64; voice++) {
		v = &trident->synth.voices[voice];
		if (v->use && v->client == ev->source.client &&
		    v->port == ev->source.port &&
		    v->index == ev->dest.channel) {
			spin_lock_irqsave(&trident->event_lock, flags);
			trident_sample_event_handlers[idx] (ev, p, v);
			spin_unlock_irqrestore(&trident->event_lock, flags);
			return;
		}
	}
}

/*

 */

static void snd_trident_synth_free_voices(trident_t * trident, int client, int port)
{
	int idx;
	snd_trident_voice_t *voice;

	for (idx = 0; idx < 32; idx++) {
		voice = &trident->synth.voices[idx];
		if (voice->use && voice->client == client && voice->port == port)
			snd_trident_free_voice(trident, voice);
	}
}

static int snd_trident_synth_use(void *private_data, snd_seq_port_subscribe_t * info)
{
	snd_trident_port_t *port = (snd_trident_port_t *) private_data;
	trident_t *trident = port->trident;
	snd_trident_voice_t *voice;
	int idx;
	unsigned long flags;

	if (info->synth_voices + info->midi_voices > 64)
		return -EINVAL;
	spin_lock_irqsave(&trident->reg_lock, flags);
	for (idx = 0; idx < info->synth_voices; idx++) {
		voice = snd_trident_alloc_voice(trident, SND_TRIDENT_VOICE_TYPE_SYNTH, info->sender.client, info->sender.port);
		if (voice == NULL) {
			snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
			spin_unlock_irqrestore(&trident->reg_lock, flags);
			return -EBUSY;
		}
		voice->index = idx;
		voice->Vol = 0xff;
	}
	for (idx = 0; idx < info->midi_voices; idx++) {
		port->midi_has_voices = 1;
		voice = snd_trident_alloc_voice(trident, SND_TRIDENT_VOICE_TYPE_MIDI, info->sender.client, info->sender.port);
		if (voice == NULL) {
			snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
			spin_unlock_irqrestore(&trident->reg_lock, flags);
			return -EBUSY;
		}
		voice->Vol = 0xff;
	}
	snd_trident_synth_use_inc(trident);
	spin_unlock_irqrestore(&trident->reg_lock, flags);
	return 0;
}

static int snd_trident_synth_unuse(void *private_data, snd_seq_port_subscribe_t * info)
{
	snd_trident_port_t *port = (snd_trident_port_t *) private_data;
	trident_t *trident = port->trident;
	unsigned long flags;

	spin_lock_irqsave(&trident->reg_lock, flags);
	snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
	snd_trident_synth_use_dec(trident);
	spin_unlock_irqrestore(&trident->reg_lock, flags);
	return 0;
}

/*

 */

static void snd_trident_synth_free_private_instruments(snd_trident_port_t * p, int client)
{
	snd_seq_instr_free_t ifree;

	memset(&ifree, 0, sizeof(ifree));
	ifree.cmd = SND_SEQ_INSTR_FREE_CMD_PRIVATE;
	snd_seq_instr_list_free_cond(p->trident->synth.ilist, &ifree, client, 0);
}

int snd_trident_synth_event_input(snd_seq_event_t * ev, int direct, void *private_data, int atomic, int hop)
{
	snd_trident_port_t *p = (snd_trident_port_t *) private_data;

	if (p == NULL)
		return -EINVAL;
	if (ev->type >= SND_SEQ_EVENT_SAMPLE &&
	    ev->type <= SND_SEQ_EVENT_SAMPLE_POSITION) {
		snd_trident_sample_event(ev, p);
		return 0;
	}
	if (ev->source.client == SND_SEQ_CLIENT_SYSTEM &&
	    ev->source.port == SND_SEQ_PORT_SYSTEM_ANNOUNCE) {
		if (ev->type == SND_SEQ_EVENT_CLIENT_EXIT) {
			snd_trident_synth_free_private_instruments(p, ev->data.addr.client);
			return 0;
		}
	}
	if (direct) {
		if (ev->type >= SND_SEQ_EVENT_INSTR_BEGIN) {
			snd_seq_instr_event(&p->trident->synth.simple_ops.kops,
					    p->trident->synth.ilist, ev,
					p->trident->synth.seq_client, atomic, hop);
			return 0;
		}
	}
	return 0;
}

static void snd_trident_synth_instr_notify(void *private_data,
					   snd_seq_kinstr_t * instr,
					   int what)
{
	int idx;
	trident_t *trident = (trident_t *) private_data;
	snd_trident_voice_t *pvoice;
	unsigned long flags;

	spin_lock_irqsave(&trident->event_lock, flags);
	for (idx = 0; idx < 64; idx++) {
		pvoice = &trident->synth.voices[idx];
		if (pvoice->use && !memcmp(&pvoice->instr, &instr->instr, sizeof(pvoice->instr))) {
			if (pvoice->sample_ops && pvoice->sample_ops->sample_stop) {
				pvoice->sample_ops->sample_stop(trident, pvoice, SAMPLE_STOP_IMMEDIATELY);
			} else {
				snd_trident_stop_voice(trident, pvoice->number);
				pvoice->flags &= ~SND_TRIDENT_VFLG_RUNNING;
			}
		}
	}
	spin_unlock_irqrestore(&trident->event_lock, flags);
}

/*

 */

static void snd_trident_synth_free_port(void *private_data)
{
	snd_trident_port_t *p = (snd_trident_port_t *) private_data;

	if (p)
		snd_midi_channel_free_set(p->chset);
}

static int snd_trident_synth_create_port(trident_t * trident, int idx)
{
	snd_trident_port_t *p;
	snd_seq_port_callback_t callbacks;
	char name[32];
	int result;

	p = &trident->synth.seq_ports[idx];
	p->chset = snd_midi_channel_alloc_set(16);
	if (p->chset == NULL)
		return -ENOMEM;
	p->chset->private_data = p;
	p->trident = trident;
	p->client = trident->synth.seq_client;

	memset(&callbacks, 0, sizeof(callbacks));
	callbacks.use = snd_trident_synth_use;
	callbacks.unuse = snd_trident_synth_unuse;
	callbacks.event_input = snd_trident_synth_event_input;
	callbacks.private_free = snd_trident_synth_free_port;
	callbacks.private_data = p;

	sprintf(name, "%s port %i", trident->isNX ? "4DWave-NX" : "Trident 4DWave-DX", idx);
	p->chset->port = snd_seq_event_port_attach(trident->synth.seq_client,
						   &callbacks,
						   SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE,
						   SND_SEQ_PORT_TYPE_MIDI_GENERIC |
						   SND_SEQ_PORT_TYPE_MIDI_GM |
						   SND_SEQ_PORT_TYPE_MIDI_GS |
						   SND_SEQ_PORT_TYPE_DIRECT_SAMPLE |
						   SND_SEQ_PORT_TYPE_SYNTH,
						   name);
	if (p->chset->port < 0) {
		result = p->chset->port;
		snd_trident_synth_free_port(p);
		return result;
	}
	p->port = p->chset->port;
	return 0;
}

/*

 */

static int snd_trident_synth_new_device(snd_card_t * card, int device, char *name,
					char *id, void *arg, int size,
					snd_seq_dev_entry_t * result)
{
	trident_t *trident = (trident_t *) arg;
	int client, i;
	snd_seq_client_callback_t callbacks;
	snd_seq_client_info_t cinfo;
	snd_seq_port_subscribe_t sub;
	snd_simple_ops_t *simpleops;

	*result = NULL;

	trident->synth.seq_client = -1;

	/* allocate new client */
	memset(&callbacks, 0, sizeof(callbacks));
	callbacks.private_data = trident;
	callbacks.allow_output = callbacks.allow_input = 1;
	client = trident->synth.seq_client =
	    snd_seq_create_kernel_client(trident->card, 1, &callbacks);
	if (client < 0)
		return client;

	/* change name of client */
	memset(&cinfo, 0, sizeof(cinfo));
	cinfo.client = client;
	cinfo.type = KERNEL_CLIENT;
	sprintf(cinfo.name, trident->isNX ? "Trident 4DWave-NX" : "Trident 4DWave-DX");
	strcpy(cinfo.group, SND_SEQ_GROUP_DEVICE);
	snd_seq_kernel_client_ctl(client, SND_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);

	for (i = 0; i < 4; i++)
		snd_trident_synth_create_port(trident, i);

	trident->synth.ilist = snd_seq_instr_list_new();
	if (trident->synth.ilist == NULL) {
		snd_seq_delete_kernel_client(client);
		trident->synth.seq_client = -1;
		return -ENOMEM;
	}
	trident->synth.ilist->flags = SND_SEQ_INSTR_FLG_DIRECT;

	simpleops = &trident->synth.simple_ops;
	snd_seq_simple_init(simpleops, trident, NULL);
	simpleops->put_sample = snd_trident_simple_put_sample;
	simpleops->get_sample = snd_trident_simple_get_sample;
	simpleops->remove_sample = snd_trident_simple_remove_sample;
	simpleops->notify = snd_trident_synth_instr_notify;

	memset(&sub, 0, sizeof(sub));
	sub.sender.queue = 0;
	sub.sender.client = SND_SEQ_CLIENT_SYSTEM;
	sub.sender.port = SND_SEQ_PORT_SYSTEM_ANNOUNCE;
	sub.dest.client = client;
	sub.dest.port = 0;
	snd_seq_kernel_client_ctl(client, SND_SEQ_IOCTL_SUBSCRIBE_PORT, &sub);

	*result = (snd_seq_dev_entry_t) 1;
	return 0;
}

static int snd_trident_synth_delete_device(snd_card_t * card, int device, void *arg,
				     int size, snd_seq_dev_entry_t entry)
{
	trident_t *trident = (trident_t *) arg;

	if (trident->synth.seq_client >= 0) {
		snd_seq_delete_kernel_client(trident->synth.seq_client);
		trident->synth.seq_client = -1;
	}
	if (trident->synth.ilist)
		snd_seq_instr_list_free(&trident->synth.ilist);
	return 0;
}

int init_module(void)
{
	static snd_seq_dev_ops_t ops =
	{
		snd_trident_synth_new_device,
		snd_trident_synth_delete_device
	};

	return snd_seq_device_register_driver(SND_SEQ_DEV_TRIDENT, &ops);
}

void cleanup_module(void)
{
	snd_seq_device_unregister_driver(SND_SEQ_DEV_TRIDENT);
}
