xref: /openbmc/linux/sound/pci/emu10k1/emumpu401.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
41da177e4SLinus Torvalds  *  Routines for control of EMU10K1 MPU-401 in UART mode
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/time.h>
81da177e4SLinus Torvalds #include <linux/init.h>
91da177e4SLinus Torvalds #include <sound/core.h>
101da177e4SLinus Torvalds #include <sound/emu10k1.h>
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #define EMU10K1_MIDI_MODE_INPUT		(1<<0)
131da177e4SLinus Torvalds #define EMU10K1_MIDI_MODE_OUTPUT	(1<<1)
141da177e4SLinus Torvalds 
mpu401_read(struct snd_emu10k1 * emu,struct snd_emu10k1_midi * mpu,int idx)15eb4698f3STakashi Iwai static inline unsigned char mpu401_read(struct snd_emu10k1 *emu,
16eb4698f3STakashi Iwai 					struct snd_emu10k1_midi *mpu, int idx)
171da177e4SLinus Torvalds {
181da177e4SLinus Torvalds 	if (emu->audigy)
191da177e4SLinus Torvalds 		return (unsigned char)snd_emu10k1_ptr_read(emu, mpu->port + idx, 0);
201da177e4SLinus Torvalds 	else
211da177e4SLinus Torvalds 		return inb(emu->port + mpu->port + idx);
221da177e4SLinus Torvalds }
231da177e4SLinus Torvalds 
mpu401_write(struct snd_emu10k1 * emu,struct snd_emu10k1_midi * mpu,int data,int idx)24eb4698f3STakashi Iwai static inline void mpu401_write(struct snd_emu10k1 *emu,
25eb4698f3STakashi Iwai 				struct snd_emu10k1_midi *mpu, int data, int idx)
261da177e4SLinus Torvalds {
271da177e4SLinus Torvalds 	if (emu->audigy)
281da177e4SLinus Torvalds 		snd_emu10k1_ptr_write(emu, mpu->port + idx, 0, data);
291da177e4SLinus Torvalds 	else
301da177e4SLinus Torvalds 		outb(data, emu->port + mpu->port + idx);
311da177e4SLinus Torvalds }
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define mpu401_write_data(emu, mpu, data)	mpu401_write(emu, mpu, data, 0)
341da177e4SLinus Torvalds #define mpu401_write_cmd(emu, mpu, data)	mpu401_write(emu, mpu, data, 1)
351da177e4SLinus Torvalds #define mpu401_read_data(emu, mpu)		mpu401_read(emu, mpu, 0)
361da177e4SLinus Torvalds #define mpu401_read_stat(emu, mpu)		mpu401_read(emu, mpu, 1)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define mpu401_input_avail(emu,mpu)	(!(mpu401_read_stat(emu,mpu) & 0x80))
391da177e4SLinus Torvalds #define mpu401_output_ready(emu,mpu)	(!(mpu401_read_stat(emu,mpu) & 0x40))
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define MPU401_RESET		0xff
421da177e4SLinus Torvalds #define MPU401_ENTER_UART	0x3f
431da177e4SLinus Torvalds #define MPU401_ACK		0xfe
441da177e4SLinus Torvalds 
mpu401_clear_rx(struct snd_emu10k1 * emu,struct snd_emu10k1_midi * mpu)45eb4698f3STakashi Iwai static void mpu401_clear_rx(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *mpu)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	int timeout = 100000;
481da177e4SLinus Torvalds 	for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
491da177e4SLinus Torvalds 		mpu401_read_data(emu, mpu);
501da177e4SLinus Torvalds #ifdef CONFIG_SND_DEBUG
511da177e4SLinus Torvalds 	if (timeout <= 0)
526f002b02STakashi Iwai 		dev_err(emu->card->dev,
536f002b02STakashi Iwai 			"cmd: clear rx timeout (status = 0x%x)\n",
546f002b02STakashi Iwai 			mpu401_read_stat(emu, mpu));
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /*
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds 
do_emu10k1_midi_interrupt(struct snd_emu10k1 * emu,struct snd_emu10k1_midi * midi,unsigned int status)62eb4698f3STakashi Iwai static void do_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, unsigned int status)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	unsigned char byte;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	if (midi->rmidi == NULL) {
671da177e4SLinus Torvalds 		snd_emu10k1_intr_disable(emu, midi->tx_enable | midi->rx_enable);
681da177e4SLinus Torvalds 		return;
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	spin_lock(&midi->input_lock);
721da177e4SLinus Torvalds 	if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
731da177e4SLinus Torvalds 		if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
741da177e4SLinus Torvalds 			mpu401_clear_rx(emu, midi);
751da177e4SLinus Torvalds 		} else {
761da177e4SLinus Torvalds 			byte = mpu401_read_data(emu, midi);
771da177e4SLinus Torvalds 			if (midi->substream_input)
781da177e4SLinus Torvalds 				snd_rawmidi_receive(midi->substream_input, &byte, 1);
791da177e4SLinus Torvalds 		}
801da177e4SLinus Torvalds 	}
811da177e4SLinus Torvalds 	spin_unlock(&midi->input_lock);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	spin_lock(&midi->output_lock);
841da177e4SLinus Torvalds 	if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
851da177e4SLinus Torvalds 		if (midi->substream_output &&
861da177e4SLinus Torvalds 		    snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
871da177e4SLinus Torvalds 			mpu401_write_data(emu, midi, byte);
881da177e4SLinus Torvalds 		} else {
891da177e4SLinus Torvalds 			snd_emu10k1_intr_disable(emu, midi->tx_enable);
901da177e4SLinus Torvalds 		}
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 	spin_unlock(&midi->output_lock);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
snd_emu10k1_midi_interrupt(struct snd_emu10k1 * emu,unsigned int status)95eb4698f3STakashi Iwai static void snd_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, unsigned int status)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	do_emu10k1_midi_interrupt(emu, &emu->midi, status);
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
snd_emu10k1_midi_interrupt2(struct snd_emu10k1 * emu,unsigned int status)100eb4698f3STakashi Iwai static void snd_emu10k1_midi_interrupt2(struct snd_emu10k1 *emu, unsigned int status)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	do_emu10k1_midi_interrupt(emu, &emu->midi2, status);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
snd_emu10k1_midi_cmd(struct snd_emu10k1 * emu,struct snd_emu10k1_midi * midi,unsigned char cmd,int ack)105b130807dSRandy Dunlap static int snd_emu10k1_midi_cmd(struct snd_emu10k1 * emu, struct snd_emu10k1_midi *midi, unsigned char cmd, int ack)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	int timeout, ok;
1081da177e4SLinus Torvalds 
109*67192cc0SOswald Buddenhagen 	spin_lock_irq(&midi->input_lock);
1101da177e4SLinus Torvalds 	mpu401_write_data(emu, midi, 0x00);
1111da177e4SLinus Torvalds 	/* mpu401_clear_rx(emu, midi); */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	mpu401_write_cmd(emu, midi, cmd);
1141da177e4SLinus Torvalds 	if (ack) {
1151da177e4SLinus Torvalds 		ok = 0;
1161da177e4SLinus Torvalds 		timeout = 10000;
1171da177e4SLinus Torvalds 		while (!ok && timeout-- > 0) {
1181da177e4SLinus Torvalds 			if (mpu401_input_avail(emu, midi)) {
1191da177e4SLinus Torvalds 				if (mpu401_read_data(emu, midi) == MPU401_ACK)
1201da177e4SLinus Torvalds 					ok = 1;
1211da177e4SLinus Torvalds 			}
1221da177e4SLinus Torvalds 		}
1231da177e4SLinus Torvalds 		if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1241da177e4SLinus Torvalds 			ok = 1;
1251da177e4SLinus Torvalds 	} else {
1261da177e4SLinus Torvalds 		ok = 1;
1271da177e4SLinus Torvalds 	}
128*67192cc0SOswald Buddenhagen 	spin_unlock_irq(&midi->input_lock);
129b130807dSRandy Dunlap 	if (!ok) {
1306f002b02STakashi Iwai 		dev_err(emu->card->dev,
1316f002b02STakashi Iwai 			"midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1321da177e4SLinus Torvalds 			   cmd, emu->port,
1331da177e4SLinus Torvalds 			   mpu401_read_stat(emu, midi),
1341da177e4SLinus Torvalds 			   mpu401_read_data(emu, midi));
135b130807dSRandy Dunlap 		return 1;
136b130807dSRandy Dunlap 	}
137b130807dSRandy Dunlap 	return 0;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
snd_emu10k1_midi_input_open(struct snd_rawmidi_substream * substream)140eb4698f3STakashi Iwai static int snd_emu10k1_midi_input_open(struct snd_rawmidi_substream *substream)
1411da177e4SLinus Torvalds {
142eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
143eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	emu = midi->emu;
146da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
147da3cec35STakashi Iwai 		return -ENXIO;
148*67192cc0SOswald Buddenhagen 	spin_lock_irq(&midi->open_lock);
1491da177e4SLinus Torvalds 	midi->midi_mode |= EMU10K1_MIDI_MODE_INPUT;
1501da177e4SLinus Torvalds 	midi->substream_input = substream;
1511da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
152*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
153b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
154b130807dSRandy Dunlap 			goto error_out;
155b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
156b130807dSRandy Dunlap 			goto error_out;
1571da177e4SLinus Torvalds 	} else {
158*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
1591da177e4SLinus Torvalds 	}
1601da177e4SLinus Torvalds 	return 0;
161b130807dSRandy Dunlap 
162b130807dSRandy Dunlap error_out:
163b130807dSRandy Dunlap 	return -EIO;
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
snd_emu10k1_midi_output_open(struct snd_rawmidi_substream * substream)166eb4698f3STakashi Iwai static int snd_emu10k1_midi_output_open(struct snd_rawmidi_substream *substream)
1671da177e4SLinus Torvalds {
168eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
169eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	emu = midi->emu;
172da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
173da3cec35STakashi Iwai 		return -ENXIO;
174*67192cc0SOswald Buddenhagen 	spin_lock_irq(&midi->open_lock);
1751da177e4SLinus Torvalds 	midi->midi_mode |= EMU10K1_MIDI_MODE_OUTPUT;
1761da177e4SLinus Torvalds 	midi->substream_output = substream;
1771da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
178*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
179b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
180b130807dSRandy Dunlap 			goto error_out;
181b130807dSRandy Dunlap 		if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
182b130807dSRandy Dunlap 			goto error_out;
1831da177e4SLinus Torvalds 	} else {
184*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 	return 0;
187b130807dSRandy Dunlap 
188b130807dSRandy Dunlap error_out:
189b130807dSRandy Dunlap 	return -EIO;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
snd_emu10k1_midi_input_close(struct snd_rawmidi_substream * substream)192eb4698f3STakashi Iwai static int snd_emu10k1_midi_input_close(struct snd_rawmidi_substream *substream)
1931da177e4SLinus Torvalds {
194eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
195eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
196b130807dSRandy Dunlap 	int err = 0;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	emu = midi->emu;
199da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
200da3cec35STakashi Iwai 		return -ENXIO;
201*67192cc0SOswald Buddenhagen 	spin_lock_irq(&midi->open_lock);
2021da177e4SLinus Torvalds 	snd_emu10k1_intr_disable(emu, midi->rx_enable);
2031da177e4SLinus Torvalds 	midi->midi_mode &= ~EMU10K1_MIDI_MODE_INPUT;
2041da177e4SLinus Torvalds 	midi->substream_input = NULL;
2051da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
206*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
207b130807dSRandy Dunlap 		err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
2081da177e4SLinus Torvalds 	} else {
209*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
2101da177e4SLinus Torvalds 	}
211b130807dSRandy Dunlap 	return err;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
snd_emu10k1_midi_output_close(struct snd_rawmidi_substream * substream)214eb4698f3STakashi Iwai static int snd_emu10k1_midi_output_close(struct snd_rawmidi_substream *substream)
2151da177e4SLinus Torvalds {
216eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
217eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
218b130807dSRandy Dunlap 	int err = 0;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	emu = midi->emu;
221da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
222da3cec35STakashi Iwai 		return -ENXIO;
223*67192cc0SOswald Buddenhagen 	spin_lock_irq(&midi->open_lock);
2241da177e4SLinus Torvalds 	snd_emu10k1_intr_disable(emu, midi->tx_enable);
2251da177e4SLinus Torvalds 	midi->midi_mode &= ~EMU10K1_MIDI_MODE_OUTPUT;
2261da177e4SLinus Torvalds 	midi->substream_output = NULL;
2271da177e4SLinus Torvalds 	if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
228*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
229b130807dSRandy Dunlap 		err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
2301da177e4SLinus Torvalds 	} else {
231*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->open_lock);
2321da177e4SLinus Torvalds 	}
233b130807dSRandy Dunlap 	return err;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
snd_emu10k1_midi_input_trigger(struct snd_rawmidi_substream * substream,int up)236eb4698f3STakashi Iwai static void snd_emu10k1_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
2371da177e4SLinus Torvalds {
238eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
239eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
2401da177e4SLinus Torvalds 	emu = midi->emu;
241da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
242da3cec35STakashi Iwai 		return;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	if (up)
2451da177e4SLinus Torvalds 		snd_emu10k1_intr_enable(emu, midi->rx_enable);
2461da177e4SLinus Torvalds 	else
2471da177e4SLinus Torvalds 		snd_emu10k1_intr_disable(emu, midi->rx_enable);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
snd_emu10k1_midi_output_trigger(struct snd_rawmidi_substream * substream,int up)250eb4698f3STakashi Iwai static void snd_emu10k1_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
2511da177e4SLinus Torvalds {
252eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
253eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	emu = midi->emu;
256da3cec35STakashi Iwai 	if (snd_BUG_ON(!emu))
257da3cec35STakashi Iwai 		return;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	if (up) {
2601da177e4SLinus Torvalds 		int max = 4;
2611da177e4SLinus Torvalds 		unsigned char byte;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 		/* try to send some amount of bytes here before interrupts */
264*67192cc0SOswald Buddenhagen 		spin_lock_irq(&midi->output_lock);
2651da177e4SLinus Torvalds 		while (max > 0) {
2661da177e4SLinus Torvalds 			if (mpu401_output_ready(emu, midi)) {
2671da177e4SLinus Torvalds 				if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT) ||
2681da177e4SLinus Torvalds 				    snd_rawmidi_transmit(substream, &byte, 1) != 1) {
2691da177e4SLinus Torvalds 					/* no more data */
270*67192cc0SOswald Buddenhagen 					spin_unlock_irq(&midi->output_lock);
2711da177e4SLinus Torvalds 					return;
2721da177e4SLinus Torvalds 				}
2731da177e4SLinus Torvalds 				mpu401_write_data(emu, midi, byte);
2741da177e4SLinus Torvalds 				max--;
2751da177e4SLinus Torvalds 			} else {
2761da177e4SLinus Torvalds 				break;
2771da177e4SLinus Torvalds 			}
2781da177e4SLinus Torvalds 		}
279*67192cc0SOswald Buddenhagen 		spin_unlock_irq(&midi->output_lock);
2801da177e4SLinus Torvalds 		snd_emu10k1_intr_enable(emu, midi->tx_enable);
2811da177e4SLinus Torvalds 	} else {
2821da177e4SLinus Torvalds 		snd_emu10k1_intr_disable(emu, midi->tx_enable);
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds /*
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds  */
2891da177e4SLinus Torvalds 
290485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_emu10k1_midi_output =
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	.open =		snd_emu10k1_midi_output_open,
2931da177e4SLinus Torvalds 	.close =	snd_emu10k1_midi_output_close,
2941da177e4SLinus Torvalds 	.trigger =	snd_emu10k1_midi_output_trigger,
2951da177e4SLinus Torvalds };
2961da177e4SLinus Torvalds 
297485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_emu10k1_midi_input =
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	.open =		snd_emu10k1_midi_input_open,
3001da177e4SLinus Torvalds 	.close =	snd_emu10k1_midi_input_close,
3011da177e4SLinus Torvalds 	.trigger =	snd_emu10k1_midi_input_trigger,
3021da177e4SLinus Torvalds };
3031da177e4SLinus Torvalds 
snd_emu10k1_midi_free(struct snd_rawmidi * rmidi)304eb4698f3STakashi Iwai static void snd_emu10k1_midi_free(struct snd_rawmidi *rmidi)
3051da177e4SLinus Torvalds {
3069fe856e4SJoe Perches 	struct snd_emu10k1_midi *midi = rmidi->private_data;
3071da177e4SLinus Torvalds 	midi->interrupt = NULL;
3081da177e4SLinus Torvalds 	midi->rmidi = NULL;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
emu10k1_midi_init(struct snd_emu10k1 * emu,struct snd_emu10k1_midi * midi,int device,char * name)311e23e7a14SBill Pemberton static int emu10k1_midi_init(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, int device, char *name)
3121da177e4SLinus Torvalds {
313eb4698f3STakashi Iwai 	struct snd_rawmidi *rmidi;
3141da177e4SLinus Torvalds 	int err;
3151da177e4SLinus Torvalds 
31612bda107STakashi Iwai 	err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi);
31712bda107STakashi Iwai 	if (err < 0)
3181da177e4SLinus Torvalds 		return err;
3191da177e4SLinus Torvalds 	midi->emu = emu;
3201da177e4SLinus Torvalds 	spin_lock_init(&midi->open_lock);
3211da177e4SLinus Torvalds 	spin_lock_init(&midi->input_lock);
3221da177e4SLinus Torvalds 	spin_lock_init(&midi->output_lock);
3231da177e4SLinus Torvalds 	strcpy(rmidi->name, name);
3241da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1_midi_output);
3251da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1_midi_input);
3261da177e4SLinus Torvalds 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
3271da177e4SLinus Torvalds 	                     SNDRV_RAWMIDI_INFO_INPUT |
3281da177e4SLinus Torvalds 	                     SNDRV_RAWMIDI_INFO_DUPLEX;
3291da177e4SLinus Torvalds 	rmidi->private_data = midi;
3301da177e4SLinus Torvalds 	rmidi->private_free = snd_emu10k1_midi_free;
3311da177e4SLinus Torvalds 	midi->rmidi = rmidi;
3321da177e4SLinus Torvalds 	return 0;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
snd_emu10k1_midi(struct snd_emu10k1 * emu)335e23e7a14SBill Pemberton int snd_emu10k1_midi(struct snd_emu10k1 *emu)
3361da177e4SLinus Torvalds {
337eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi = &emu->midi;
3381da177e4SLinus Torvalds 	int err;
3391da177e4SLinus Torvalds 
34012bda107STakashi Iwai 	err = emu10k1_midi_init(emu, midi, 0, "EMU10K1 MPU-401 (UART)");
34112bda107STakashi Iwai 	if (err < 0)
3421da177e4SLinus Torvalds 		return err;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	midi->tx_enable = INTE_MIDITXENABLE;
3451da177e4SLinus Torvalds 	midi->rx_enable = INTE_MIDIRXENABLE;
3461da177e4SLinus Torvalds 	midi->port = MUDATA;
3471da177e4SLinus Torvalds 	midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
3481da177e4SLinus Torvalds 	midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
3491da177e4SLinus Torvalds 	midi->interrupt = snd_emu10k1_midi_interrupt;
3501da177e4SLinus Torvalds 	return 0;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
snd_emu10k1_audigy_midi(struct snd_emu10k1 * emu)353e23e7a14SBill Pemberton int snd_emu10k1_audigy_midi(struct snd_emu10k1 *emu)
3541da177e4SLinus Torvalds {
355eb4698f3STakashi Iwai 	struct snd_emu10k1_midi *midi;
3561da177e4SLinus Torvalds 	int err;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	midi = &emu->midi;
35912bda107STakashi Iwai 	err = emu10k1_midi_init(emu, midi, 0, "Audigy MPU-401 (UART)");
36012bda107STakashi Iwai 	if (err < 0)
3611da177e4SLinus Torvalds 		return err;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	midi->tx_enable = INTE_MIDITXENABLE;
3641da177e4SLinus Torvalds 	midi->rx_enable = INTE_MIDIRXENABLE;
3651da177e4SLinus Torvalds 	midi->port = A_MUDATA1;
3661da177e4SLinus Torvalds 	midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
3671da177e4SLinus Torvalds 	midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
3681da177e4SLinus Torvalds 	midi->interrupt = snd_emu10k1_midi_interrupt;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	midi = &emu->midi2;
37112bda107STakashi Iwai 	err = emu10k1_midi_init(emu, midi, 1, "Audigy MPU-401 #2");
37212bda107STakashi Iwai 	if (err < 0)
3731da177e4SLinus Torvalds 		return err;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	midi->tx_enable = INTE_A_MIDITXENABLE2;
3761da177e4SLinus Torvalds 	midi->rx_enable = INTE_A_MIDIRXENABLE2;
3771da177e4SLinus Torvalds 	midi->port = A_MUDATA2;
3781da177e4SLinus Torvalds 	midi->ipr_tx = IPR_A_MIDITRANSBUFEMPTY2;
3791da177e4SLinus Torvalds 	midi->ipr_rx = IPR_A_MIDIRECVBUFEMPTY2;
3801da177e4SLinus Torvalds 	midi->interrupt = snd_emu10k1_midi_interrupt2;
3811da177e4SLinus Torvalds 	return 0;
3821da177e4SLinus Torvalds }
383