/*
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
 *  Routines for control of MPU-401 in UART mode
 *
 *  MPU-401 supports UART mode which isn't capable generate transmit
 *  interrupts thus output is done via polling.
 *
 *   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/mpu401.h"

static void snd_mpu401_uart_output_write(snd_rawmidi_t * rmidi);

/*

 */

void snd_mpu401_uart_interrupt(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	mpu401_t *mpu;
	int max = 128;
	unsigned char byte, status;

	mpu = (mpu401_t *) rmidi->private_data;
	if (!mpu) {
		inb(MPU401D(mpu));	/* ack interrupt only */
		return;
	}
	while (max-- > 0) {
		spin_lock_irqsave(&mpu->input_lock, flags);
		status = inb(MPU401C(mpu));
		if (status == 0xff) {
			spin_unlock_irqrestore(&mpu->input_lock, flags);
			break;	/* wrong value, MPU isn't connected */
		}
		if (!(status & 0x80)) {
			byte = inb(MPU401D(mpu));
#if 0
			snd_printk("interrupt!! - rx byte = 0x%x\n", byte);
#endif
			spin_unlock_irqrestore(&mpu->input_lock, flags);
			if (mpu->mode & MPU401_MODE_INPUT_TRIGGER)
				rmidi->input.data(rmidi, &byte, 1);
		} else {
			spin_unlock_irqrestore(&mpu->input_lock, flags);
			break;
		}
	}
	/* ok. for better Tx performance try do some output when input is done */
	if (mpu->mode & MPU401_MODE_OUTPUT)
		snd_mpu401_uart_output_write(rmidi);
}

/*

 */

static void snd_mpu401_uart_cmd(mpu401_t * mpu, unsigned char cmd, int ack)
{
	unsigned long flags;
	int timeout, ok;

	spin_lock_irqsave(&mpu->input_lock, flags);
	outb(0x00, MPU401D(mpu));
	for (timeout = 100000; timeout > 0 && !(inb(MPU401C(mpu)) & 0x80); timeout--)
		inb(MPU401D(mpu));
#ifdef CONFIG_SND_DEBUG
	if (timeout <= 0)
		snd_printk("snd_mpu401_uart_cmd: clear rx timeout (status = 0x%x)\n", inb(MPU401C(mpu)));
#endif
	/* ok. standard MPU-401 initialization */
	if (mpu->hardware != MPU401_HW_SB) {
		for (timeout = 1000; timeout > 0 && inb(MPU401C(mpu)) & 0x40; timeout--)
			snd_delay(1);
#ifdef CONFIG_SND_DEBUG
		if (!timeout)
			snd_printk("snd_mpu401_uart_cmd: tx timeout (status = 0x%x)\n", inb(MPU401C(mpu)));
#endif
	}
	outb(cmd, MPU401C(mpu));
	if (ack) {
		ok = 0;
		timeout = 10000;
		while (!ok && timeout-- > 0) {
			if (!(inb(MPU401C(mpu)) & 0x80)) {
				if (inb(MPU401D(mpu)) == 0xfe)
					ok = 1;
			}
		}
	} else {
		ok = 1;
	}
	spin_unlock_irqrestore(&mpu->input_lock, flags);
	if (!ok) {
		snd_printk("snd_mpu401_uart_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n", cmd, mpu->port, inb(MPU401C(mpu)), inb(MPU401D(mpu)));
	}
#if 0
	snd_printk("snd_mpu401_uart_cmd: 0x%x at 0x%x (status = 0x%x, data = 0x%x)!!!\n", cmd, mpu->port, inb(MPU401C(mpu)), inb(MPU401D(mpu)));
#endif
}

static int snd_mpu401_uart_input_open(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	mpu401_t *mpu;

	mpu = (mpu401_t *) rmidi->private_data;
#if 0
	snd_printk("[0x%x] MPU-401 command port - 0x%x\n", MPU401C(mpu), inb(MPU401C(mpu)));
	snd_printk("[0x%x] MPU-401 data port - 0x%x\n", MPU401D(mpu), inb(MPU401D(mpu)));
#endif
	spin_lock_irqsave(&mpu->open_lock, flags);
	mpu->mode |= MPU401_MODE_INPUT;
	if (mpu->open_input)
		mpu->open_input(mpu);
	if (!(mpu->mode & MPU401_MODE_OUTPUT)) {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
		snd_mpu401_uart_cmd(mpu, 0xff, 1);	/* reset */
		snd_mpu401_uart_cmd(mpu, 0x3f, 1);	/* enter UART mode */
	} else {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
	}
	return 0;
}

static int snd_mpu401_uart_output_open(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	mpu401_t *mpu;

	mpu = (mpu401_t *) rmidi->private_data;
	spin_lock_irqsave(&mpu->open_lock, flags);
	mpu->mode |= MPU401_MODE_OUTPUT;
	if (mpu->open_output)
		mpu->open_output(mpu);
	if (!(mpu->mode & MPU401_MODE_INPUT)) {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
		snd_mpu401_uart_cmd(mpu, 0xff, 1);	/* reset */
		snd_mpu401_uart_cmd(mpu, 0x3f, 1);	/* enter UART mode */
	} else {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
	}
	return 0;
}

static int snd_mpu401_uart_input_close(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	mpu401_t *mpu;

	mpu = (mpu401_t *) rmidi->private_data;
	spin_lock_irqsave(&mpu->open_lock, flags);
	mpu->mode &= ~MPU401_MODE_INPUT;
	if (mpu->close_input)
		mpu->close_input(mpu);
	if (!(mpu->mode & MPU401_MODE_OUTPUT)) {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
		snd_mpu401_uart_cmd(mpu, 0xff, 0);	/* reset */
	} else {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
	}
	return 0;
}

static int snd_mpu401_uart_output_close(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	mpu401_t *mpu;

	mpu = (mpu401_t *) rmidi->private_data;
	spin_lock_irqsave(&mpu->open_lock, flags);
	if (mpu->close_output)
		mpu->close_output(mpu);
	mpu->mode &= ~MPU401_MODE_OUTPUT;
	if (!(mpu->mode & MPU401_MODE_INPUT)) {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
		snd_mpu401_uart_cmd(mpu, 0xff, 0);	/* reset */
	} else {
		spin_unlock_irqrestore(&mpu->open_lock, flags);
	}
	return 0;
}

static void snd_mpu401_uart_input_trigger(snd_rawmidi_t * rmidi, int up)
{
	unsigned long flags;
	mpu401_t *mpu;

	mpu = (mpu401_t *) rmidi->private_data;
	spin_lock_irqsave(&mpu->input_lock, flags);
	if (up)
		mpu->mode |= MPU401_MODE_INPUT_TRIGGER;
	else
		mpu->mode &= ~MPU401_MODE_INPUT_TRIGGER;
	spin_unlock_irqrestore(&mpu->input_lock, flags);
}

/*
 *  Tx FIFO sizes:
 *    CS4237B			- 16 bytes
 *    AudioDrive ES1688         - 12 bytes
 *    S3 SonicVibes             -  8 bytes
 *    SoundBlaster AWE 64       -  2 bytes (ugly hardware)
 */

static void snd_mpu401_uart_output_write(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	mpu401_t *mpu;
	unsigned char byte;
	int max = 256, timeout;

	mpu = (mpu401_t *) rmidi->private_data;
	while (max > 0) {
		for (timeout = 100; timeout > 0; timeout--) {
			if (!(inb(MPU401C(mpu)) & 0x40))
				break;
		}
		spin_lock_irqsave(&mpu->output_lock, flags);
		if (!(inb(MPU401C(mpu)) & 0x40)) {
			if (!(mpu->mode & MPU401_MODE_OUTPUT) ||
			    rmidi->output.data(rmidi, &byte, 1) != 1) {
				spin_unlock_irqrestore(&mpu->output_lock, flags);
				return;
			}
			outb(byte, MPU401D(mpu));
			spin_unlock_irqrestore(&mpu->output_lock, flags);
#if 0
			snd_printk("mpu401_uart: tx = 0x%x\n", byte);
#endif
			max--;
		} else {
			spin_unlock_irqrestore(&mpu->output_lock, flags);
#if 0
			snd_printd("snd_mpu401_uart_output_write: timeout - max = %i (%i)\n", max, 256 - max);
#endif
			return;
		}
	}
}

/*

 */

static struct snd_stru_rawmidi_direction_hw snd_mpu401_uart_output =
{
	SND_RAWMIDI_HW_POLL,		/* flags */
	NULL,				/* private_data */
	NULL,				/* private_free */
	snd_mpu401_uart_output_open,	/* open */
	snd_mpu401_uart_output_close,	/* close */
	NULL,				/* trigger */
	{snd_mpu401_uart_output_write},	/* io.write */
	NULL,				/* abort */
};

static struct snd_stru_rawmidi_direction_hw snd_mpu401_uart_input =
{
	0,				/* flags */
	NULL,				/* private_data */
	NULL,				/* private_free */
	snd_mpu401_uart_input_open,	/* open */
	snd_mpu401_uart_input_close,	/* close */
	snd_mpu401_uart_input_trigger,	/* trigger */
	{NULL},				/* io.read */
	NULL,				/* abort */
};

static void snd_mpu401_uart_free(void *private_data)
{
	snd_kfree(private_data);
}

snd_rawmidi_t *snd_mpu401_uart_new_device(snd_card_t * card,
					  unsigned short hardware,
					  unsigned short port,
					  unsigned short irq)
{
	mpu401_t *mpu;
	snd_rawmidi_t *rmidi;

	if ((rmidi = snd_rawmidi_new_device(card, "MPU-401U")) == NULL)
		return NULL;
	mpu = snd_kcalloc(sizeof(mpu401_t), GFP_KERNEL);
	if (!mpu) {
		snd_rawmidi_free(rmidi);
		return NULL;
	}
	mpu->open_lock = SPIN_LOCK_UNLOCKED;
	mpu->input_lock = SPIN_LOCK_UNLOCKED;
	mpu->output_lock = SPIN_LOCK_UNLOCKED;
	mpu->hardware = hardware;
	mpu->port = port;
	mpu->irq = irq;
	strcpy(rmidi->name, "MPU-401 (UART)");
	memcpy(&rmidi->output.hw, &snd_mpu401_uart_output, sizeof(snd_mpu401_uart_output));
	memcpy(&rmidi->input.hw, &snd_mpu401_uart_input, sizeof(snd_mpu401_uart_input));
	rmidi->info_flags |= SND_RAWMIDI_INFO_OUTPUT |
	                     SND_RAWMIDI_INFO_INPUT |
	                     SND_RAWMIDI_INFO_DUPLEX;
	rmidi->private_data = mpu;
	rmidi->private_free = snd_mpu401_uart_free;
	return rmidi;
}

EXPORT_SYMBOL(snd_mpu401_uart_interrupt);
EXPORT_SYMBOL(snd_mpu401_uart_new_device);

/*
 *  INIT part
 */

int init_module(void)
{
	return 0;
}

void cleanup_module(void)
{
}
