xref: /openbmc/linux/sound/pci/emu10k1/emumpu401.c (revision 9fe856e47e1751204faf3d604c6d20ab24bd3b93)
11da177e4SLinus Torvalds /*
2c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
31da177e4SLinus Torvalds  *  Routines for control of EMU10K1 MPU-401 in UART mode
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds  *   (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
121da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4SLinus Torvalds  *   GNU General Public License for more details.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
171da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
181da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/time.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <sound/core.h>
251da177e4SLinus Torvalds #include <sound/emu10k1.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #define EMU10K1_MIDI_MODE_INPUT		(1<<0)
281da177e4SLinus Torvalds #define EMU10K1_MIDI_MODE_OUTPUT	(1<<1)
291da177e4SLinus Torvalds 
30eb4698f3STakashi Iwai static inline unsigned char mpu401_read(struct snd_emu10k1 *emu,
31eb4698f3STakashi Iwai 					struct snd_emu10k1_midi *mpu, int idx)
321da177e4SLinus Torvalds {
331da177e4SLinus Torvalds 	if (emu->audigy)
341da177e4SLinus Torvalds 		return (unsigned char)snd_emu10k1_ptr_read(emu, mpu->port + idx, 0);
351da177e4SLinus Torvalds 	else
361da177e4SLinus Torvalds 		return inb(emu->port + mpu->port + idx);
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
39eb4698f3STakashi Iwai static inline void mpu401_write(struct snd_emu10k1 *emu,
40eb4698f3STakashi Iwai 				struct snd_emu10k1_midi *mpu, int data, int idx)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	if (emu->audigy)
431da177e4SLinus Torvalds 		snd_emu10k1_ptr_write(emu, mpu->port + idx, 0, data);
441da177e4SLinus Torvalds 	else
451da177e4SLinus Torvalds 		outb(data, emu->port + mpu->port + idx);
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #define mpu401_write_data(emu, mpu, data)	mpu401_write(emu, mpu, data, 0)
491da177e4SLinus Torvalds #define mpu401_write_cmd(emu, mpu, data)	mpu401_write(emu, mpu, data, 1)
501da177e4SLinus Torvalds #define mpu401_read_data(emu, mpu)		mpu401_read(emu, mpu, 0)
511da177e4SLinus Torvalds #define mpu401_read_stat(emu, mpu)		mpu401_read(emu, mpu, 1)
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #define mpu401_input_avail(emu,mpu)	(!(mpu401_read_stat(emu,mpu) & 0x80))
541da177e4SLinus Torvalds #define mpu401_output_ready(emu,mpu)	(!(mpu401_read_stat(emu,mpu) & 0x40))
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #define MPU401_RESET		0xff
571da177e4SLinus Torvalds #define MPU401_ENTER_UART	0x3f
581da177e4SLinus Torvalds #define MPU401_ACK		0xfe
591da177e4SLinus Torvalds 
60eb4698f3STakashi Iwai static void mpu401_clear_rx(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *mpu)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	int timeout = 100000;
631da177e4SLinus Torvalds 	for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
641da177e4SLinus Torvalds 		mpu401_read_data(emu, mpu);
651da177e4SLinus Torvalds #ifdef CONFIG_SND_DEBUG
661da177e4SLinus Torvalds 	if (timeout <= 0)
671da177e4SLinus Torvalds 		snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds 
75eb4698f3STakashi Iwai static void do_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, unsigned int status)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	unsigned char byte;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	if (midi->rmidi == NULL) {
801da177e4SLinus Torvalds 		snd_emu10k1_intr_disable(emu, midi->tx_enable | midi->rx_enable);
811da177e4SLinus Torvalds 		return;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	spin_lock(&midi->input_lock);
851da177e4SLinus Torvalds 	if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
861da177e4SLinus Torvalds 		if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
871da177e4SLinus Torvalds 			mpu401_clear_rx(emu, midi);
881da177e4SLinus Torvalds 		} else {
891da177e4SLinus Torvalds 			byte = mpu401_read_data(emu, midi);
901da177e4SLinus Torvalds 			if (midi->substream_input)
911da177e4SLinus Torvalds 				snd_rawmidi_receive(midi->substream_input, &byte, 1);
921da177e4SLinus Torvalds 		}
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 	spin_unlock(&midi->input_lock);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	spin_lock(&midi->output_lock);
971da177e4SLinus Torvalds 	if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
981da177e4SLinus Torvalds 		if (midi->substream_output &&
991da177e4SLinus Torvalds 		    snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1001da177e4SLinus Torvalds 			mpu401_write_data(emu, midi, byte);
1011da177e4SLinus Torvalds 		} else {
1021da177e4SLinus Torvalds 			snd_emu10k1_intr_disable(emu, midi->tx_enable);
1031da177e4SLinus Torvalds 		}
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 	spin_unlock(&midi->output_lock);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108eb4698f3STakashi Iwai static void snd_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, unsigned int status)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	do_emu10k1_midi_interrupt(emu, &emu->midi, status);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
113eb4698f3STakashi Iwai static void snd_emu10k1_midi_interrupt2(struct snd_emu10k1 *emu, unsigned int status)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	do_emu10k1_midi_interrupt(emu, &emu->midi2, status);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
118b130807dSRandy Dunlap static int snd_emu10k1_midi_cmd(struct snd_emu10k1 * emu, struct snd_emu10k1_midi *midi, unsigned char cmd, int ack)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	unsigned long flags;
1211da177e4SLinus Torvalds 	int timeout, ok;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	spin_lock_irqsave(&midi->input_lock, flags);
1241da177e4SLinus Torvalds 	mpu401_write_data(emu, midi, 0x00);
1251da177e4SLinus Torvalds 	/* mpu401_clear_rx(emu, midi); */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	mpu401_write_cmd(emu, midi, cmd);
1281da177e4SLinus Torvalds 	if (ack) {
1291da177e4SLinus Torvalds 		ok = 0;
1301da177e4SLinus Torvalds 		timeout = 10000;
1311da177e4SLinus Torvalds 		while (!ok && timeout-- > 0) {
1321da177e4SLinus Torvalds 			if (mpu401_input_avail(emu, midi)) {
1331da177e4SLinus Torvalds 				if (mpu401_read_data(emu, midi) == MPU401_ACK)
1341da177e4SLinus Torvalds 					ok = 1;
1351da177e4SLinus Torvalds 			}
1361da177e4SLinus Torvalds 		}
1371da177e4SLinus Torvalds 		if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1381da177e4SLinus Torvalds 			ok = 1;
1391da177e4SLinus Torvalds 	} else {
1401da177e4SLinus Torvalds 		ok = 1;
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 	spin_unlock_irqrestore(&midi->input_lock, flags);
143b130807dSRandy Dunlap 	if (!ok) {
1441da177e4SLinus Torvalds 		snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1451da177e4SLinus Torvalds 			   cmd, emu->port,
1461da177e4SLinus Torvalds 			   mpu401_read_stat(emu, midi),
1471da177e4SLinus Torvalds 			   mpu401_read_data(emu, midi));
148b130807dSRandy Dunlap 		return 1;
149b130807dSRandy Dunlap 	}
150b130807dSRandy Dunlap 	return 0;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153eb4698f3STakashi Iwai static int snd_emu10k1_midi_input_open(struct snd_rawmidi_substream *substream)
1541da177e4SLinus Torvalds {
155eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
156eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
1571da177e4SLinus Torvalds 	unsigned long flags;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	emu = midi->emu;
160da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
161da3cec35STakashi Iwai 		return -ENXIO;
1621da177e4SLinus Torvalds 	spin_lock_irqsave(&midi->open_lock, flags);
1631da177e4SLinus Torvalds 	midi->midi_mode |= EMU10K1_MIDI_MODE_INPUT;
1641da177e4SLinus Torvalds 	midi->substream_input = substream;
1651da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
1661da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
167b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
168b130807dSRandy Dunlap 			goto error_out;
169b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
170b130807dSRandy Dunlap 			goto error_out;
1711da177e4SLinus Torvalds 	} else {
1721da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 	return 0;
175b130807dSRandy Dunlap 
176b130807dSRandy Dunlap error_out:
177b130807dSRandy Dunlap 	return -EIO;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
180eb4698f3STakashi Iwai static int snd_emu10k1_midi_output_open(struct snd_rawmidi_substream *substream)
1811da177e4SLinus Torvalds {
182eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
183eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
1841da177e4SLinus Torvalds 	unsigned long flags;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	emu = midi->emu;
187da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
188da3cec35STakashi Iwai 		return -ENXIO;
1891da177e4SLinus Torvalds 	spin_lock_irqsave(&midi->open_lock, flags);
1901da177e4SLinus Torvalds 	midi->midi_mode |= EMU10K1_MIDI_MODE_OUTPUT;
1911da177e4SLinus Torvalds 	midi->substream_output = substream;
1921da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
1931da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
194b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
195b130807dSRandy Dunlap 			goto error_out;
196b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
197b130807dSRandy Dunlap 			goto error_out;
1981da177e4SLinus Torvalds 	} else {
1991da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 	return 0;
202b130807dSRandy Dunlap 
203b130807dSRandy Dunlap error_out:
204b130807dSRandy Dunlap 	return -EIO;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207eb4698f3STakashi Iwai static int snd_emu10k1_midi_input_close(struct snd_rawmidi_substream *substream)
2081da177e4SLinus Torvalds {
209eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
210eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
2111da177e4SLinus Torvalds 	unsigned long flags;
212b130807dSRandy Dunlap 	int err = 0;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	emu = midi->emu;
215da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
216da3cec35STakashi Iwai 		return -ENXIO;
2171da177e4SLinus Torvalds 	spin_lock_irqsave(&midi->open_lock, flags);
2181da177e4SLinus Torvalds 	snd_emu10k1_intr_disable(emu, midi->rx_enable);
2191da177e4SLinus Torvalds 	midi->midi_mode &= ~EMU10K1_MIDI_MODE_INPUT;
2201da177e4SLinus Torvalds 	midi->substream_input = NULL;
2211da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
2221da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
223b130807dSRandy Dunlap 		err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
2241da177e4SLinus Torvalds 	} else {
2251da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
2261da177e4SLinus Torvalds 	}
227b130807dSRandy Dunlap 	return err;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
230eb4698f3STakashi Iwai static int snd_emu10k1_midi_output_close(struct snd_rawmidi_substream *substream)
2311da177e4SLinus Torvalds {
232eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
233eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
2341da177e4SLinus Torvalds 	unsigned long flags;
235b130807dSRandy Dunlap 	int err = 0;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	emu = midi->emu;
238da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
239da3cec35STakashi Iwai 		return -ENXIO;
2401da177e4SLinus Torvalds 	spin_lock_irqsave(&midi->open_lock, flags);
2411da177e4SLinus Torvalds 	snd_emu10k1_intr_disable(emu, midi->tx_enable);
2421da177e4SLinus Torvalds 	midi->midi_mode &= ~EMU10K1_MIDI_MODE_OUTPUT;
2431da177e4SLinus Torvalds 	midi->substream_output = NULL;
2441da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
2451da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
246b130807dSRandy Dunlap 		err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
2471da177e4SLinus Torvalds 	} else {
2481da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->open_lock, flags);
2491da177e4SLinus Torvalds 	}
250b130807dSRandy Dunlap 	return err;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
253eb4698f3STakashi Iwai static void snd_emu10k1_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
2541da177e4SLinus Torvalds {
255eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
256eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
2571da177e4SLinus Torvalds 	emu = midi->emu;
258da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
259da3cec35STakashi Iwai 		return;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (up)
2621da177e4SLinus Torvalds 		snd_emu10k1_intr_enable(emu, midi->rx_enable);
2631da177e4SLinus Torvalds 	else
2641da177e4SLinus Torvalds 		snd_emu10k1_intr_disable(emu, midi->rx_enable);
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
267eb4698f3STakashi Iwai static void snd_emu10k1_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
2681da177e4SLinus Torvalds {
269eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
270eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
2711da177e4SLinus Torvalds 	unsigned long flags;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	emu = midi->emu;
274da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
275da3cec35STakashi Iwai 		return;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (up) {
2781da177e4SLinus Torvalds 		int max = 4;
2791da177e4SLinus Torvalds 		unsigned char byte;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 		/* try to send some amount of bytes here before interrupts */
2821da177e4SLinus Torvalds 		spin_lock_irqsave(&midi->output_lock, flags);
2831da177e4SLinus Torvalds 		while (max > 0) {
2841da177e4SLinus Torvalds 			if (mpu401_output_ready(emu, midi)) {
2851da177e4SLinus Torvalds 				if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT) ||
2861da177e4SLinus Torvalds 				    snd_rawmidi_transmit(substream, &byte, 1) != 1) {
2871da177e4SLinus Torvalds 					/* no more data */
2881da177e4SLinus Torvalds 					spin_unlock_irqrestore(&midi->output_lock, flags);
2891da177e4SLinus Torvalds 					return;
2901da177e4SLinus Torvalds 				}
2911da177e4SLinus Torvalds 				mpu401_write_data(emu, midi, byte);
2921da177e4SLinus Torvalds 				max--;
2931da177e4SLinus Torvalds 			} else {
2941da177e4SLinus Torvalds 				break;
2951da177e4SLinus Torvalds 			}
2961da177e4SLinus Torvalds 		}
2971da177e4SLinus Torvalds 		spin_unlock_irqrestore(&midi->output_lock, flags);
2981da177e4SLinus Torvalds 		snd_emu10k1_intr_enable(emu, midi->tx_enable);
2991da177e4SLinus Torvalds 	} else {
3001da177e4SLinus Torvalds 		snd_emu10k1_intr_disable(emu, midi->tx_enable);
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds /*
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds  */
3071da177e4SLinus Torvalds 
308eb4698f3STakashi Iwai static struct snd_rawmidi_ops snd_emu10k1_midi_output =
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	.open =		snd_emu10k1_midi_output_open,
3111da177e4SLinus Torvalds 	.close =	snd_emu10k1_midi_output_close,
3121da177e4SLinus Torvalds 	.trigger =	snd_emu10k1_midi_output_trigger,
3131da177e4SLinus Torvalds };
3141da177e4SLinus Torvalds 
315eb4698f3STakashi Iwai static struct snd_rawmidi_ops snd_emu10k1_midi_input =
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	.open =		snd_emu10k1_midi_input_open,
3181da177e4SLinus Torvalds 	.close =	snd_emu10k1_midi_input_close,
3191da177e4SLinus Torvalds 	.trigger =	snd_emu10k1_midi_input_trigger,
3201da177e4SLinus Torvalds };
3211da177e4SLinus Torvalds 
322eb4698f3STakashi Iwai static void snd_emu10k1_midi_free(struct snd_rawmidi *rmidi)
3231da177e4SLinus Torvalds {
324*9fe856e4SJoe Perches 	struct snd_emu10k1_midi *midi = rmidi->private_data;
3251da177e4SLinus Torvalds 	midi->interrupt = NULL;
3261da177e4SLinus Torvalds 	midi->rmidi = NULL;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
329eb4698f3STakashi Iwai static int __devinit emu10k1_midi_init(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, int device, char *name)
3301da177e4SLinus Torvalds {
331eb4698f3STakashi Iwai 	struct snd_rawmidi *rmidi;
3321da177e4SLinus Torvalds 	int err;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
3351da177e4SLinus Torvalds 		return err;
3361da177e4SLinus Torvalds 	midi->emu = emu;
3371da177e4SLinus Torvalds 	spin_lock_init(&midi->open_lock);
3381da177e4SLinus Torvalds 	spin_lock_init(&midi->input_lock);
3391da177e4SLinus Torvalds 	spin_lock_init(&midi->output_lock);
3401da177e4SLinus Torvalds 	strcpy(rmidi->name, name);
3411da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1_midi_output);
3421da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1_midi_input);
3431da177e4SLinus Torvalds 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
3441da177e4SLinus Torvalds 	                     SNDRV_RAWMIDI_INFO_INPUT |
3451da177e4SLinus Torvalds 	                     SNDRV_RAWMIDI_INFO_DUPLEX;
3461da177e4SLinus Torvalds 	rmidi->private_data = midi;
3471da177e4SLinus Torvalds 	rmidi->private_free = snd_emu10k1_midi_free;
3481da177e4SLinus Torvalds 	midi->rmidi = rmidi;
3491da177e4SLinus Torvalds 	return 0;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352eb4698f3STakashi Iwai int __devinit snd_emu10k1_midi(struct snd_emu10k1 *emu)
3531da177e4SLinus Torvalds {
354eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = &emu->midi;
3551da177e4SLinus Torvalds 	int err;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	if ((err = emu10k1_midi_init(emu, midi, 0, "EMU10K1 MPU-401 (UART)")) < 0)
3581da177e4SLinus Torvalds 		return err;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	midi->tx_enable = INTE_MIDITXENABLE;
3611da177e4SLinus Torvalds 	midi->rx_enable = INTE_MIDIRXENABLE;
3621da177e4SLinus Torvalds 	midi->port = MUDATA;
3631da177e4SLinus Torvalds 	midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
3641da177e4SLinus Torvalds 	midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
3651da177e4SLinus Torvalds 	midi->interrupt = snd_emu10k1_midi_interrupt;
3661da177e4SLinus Torvalds 	return 0;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
369eb4698f3STakashi Iwai int __devinit snd_emu10k1_audigy_midi(struct snd_emu10k1 *emu)
3701da177e4SLinus Torvalds {
371eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi;
3721da177e4SLinus Torvalds 	int err;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	midi = &emu->midi;
3751da177e4SLinus Torvalds 	if ((err = emu10k1_midi_init(emu, midi, 0, "Audigy MPU-401 (UART)")) < 0)
3761da177e4SLinus Torvalds 		return err;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	midi->tx_enable = INTE_MIDITXENABLE;
3791da177e4SLinus Torvalds 	midi->rx_enable = INTE_MIDIRXENABLE;
3801da177e4SLinus Torvalds 	midi->port = A_MUDATA1;
3811da177e4SLinus Torvalds 	midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
3821da177e4SLinus Torvalds 	midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
3831da177e4SLinus Torvalds 	midi->interrupt = snd_emu10k1_midi_interrupt;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	midi = &emu->midi2;
3861da177e4SLinus Torvalds 	if ((err = emu10k1_midi_init(emu, midi, 1, "Audigy MPU-401 #2")) < 0)
3871da177e4SLinus Torvalds 		return err;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	midi->tx_enable = INTE_A_MIDITXENABLE2;
3901da177e4SLinus Torvalds 	midi->rx_enable = INTE_A_MIDIRXENABLE2;
3911da177e4SLinus Torvalds 	midi->port = A_MUDATA2;
3921da177e4SLinus Torvalds 	midi->ipr_tx = IPR_A_MIDITRANSBUFEMPTY2;
3931da177e4SLinus Torvalds 	midi->ipr_rx = IPR_A_MIDIRECVBUFEMPTY2;
3941da177e4SLinus Torvalds 	midi->interrupt = snd_emu10k1_midi_interrupt2;
3951da177e4SLinus Torvalds 	return 0;
3961da177e4SLinus Torvalds }
397