2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of EMU10K1 MPU-401 in UART mode
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <sound/core.h>
26 #include <sound/emu10k1.h>
28 #define EMU10K1_MIDI_MODE_INPUT (1<<0)
29 #define EMU10K1_MIDI_MODE_OUTPUT (1<<1)
31 static inline unsigned char mpu401_read(emu10k1_t
*emu
, emu10k1_midi_t
*mpu
, int idx
)
34 return (unsigned char)snd_emu10k1_ptr_read(emu
, mpu
->port
+ idx
, 0);
36 return inb(emu
->port
+ mpu
->port
+ idx
);
39 static inline void mpu401_write(emu10k1_t
*emu
, emu10k1_midi_t
*mpu
, int data
, int idx
)
42 snd_emu10k1_ptr_write(emu
, mpu
->port
+ idx
, 0, data
);
44 outb(data
, emu
->port
+ mpu
->port
+ idx
);
47 #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
48 #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
49 #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
50 #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
52 #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
53 #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
55 #define MPU401_RESET 0xff
56 #define MPU401_ENTER_UART 0x3f
57 #define MPU401_ACK 0xfe
59 static void mpu401_clear_rx(emu10k1_t
*emu
, emu10k1_midi_t
*mpu
)
62 for (; timeout
> 0 && mpu401_input_avail(emu
, mpu
); timeout
--)
63 mpu401_read_data(emu
, mpu
);
64 #ifdef CONFIG_SND_DEBUG
66 snd_printk(KERN_ERR
"cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu
, mpu
));
74 static void do_emu10k1_midi_interrupt(emu10k1_t
*emu
, emu10k1_midi_t
*midi
, unsigned int status
)
78 if (midi
->rmidi
== NULL
) {
79 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
| midi
->rx_enable
);
83 spin_lock(&midi
->input_lock
);
84 if ((status
& midi
->ipr_rx
) && mpu401_input_avail(emu
, midi
)) {
85 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_INPUT
)) {
86 mpu401_clear_rx(emu
, midi
);
88 byte
= mpu401_read_data(emu
, midi
);
89 if (midi
->substream_input
)
90 snd_rawmidi_receive(midi
->substream_input
, &byte
, 1);
93 spin_unlock(&midi
->input_lock
);
95 spin_lock(&midi
->output_lock
);
96 if ((status
& midi
->ipr_tx
) && mpu401_output_ready(emu
, midi
)) {
97 if (midi
->substream_output
&&
98 snd_rawmidi_transmit(midi
->substream_output
, &byte
, 1) == 1) {
99 mpu401_write_data(emu
, midi
, byte
);
101 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
);
104 spin_unlock(&midi
->output_lock
);
107 static void snd_emu10k1_midi_interrupt(emu10k1_t
*emu
, unsigned int status
)
109 do_emu10k1_midi_interrupt(emu
, &emu
->midi
, status
);
112 static void snd_emu10k1_midi_interrupt2(emu10k1_t
*emu
, unsigned int status
)
114 do_emu10k1_midi_interrupt(emu
, &emu
->midi2
, status
);
117 static void snd_emu10k1_midi_cmd(emu10k1_t
* emu
, emu10k1_midi_t
*midi
, unsigned char cmd
, int ack
)
122 spin_lock_irqsave(&midi
->input_lock
, flags
);
123 mpu401_write_data(emu
, midi
, 0x00);
124 /* mpu401_clear_rx(emu, midi); */
126 mpu401_write_cmd(emu
, midi
, cmd
);
130 while (!ok
&& timeout
-- > 0) {
131 if (mpu401_input_avail(emu
, midi
)) {
132 if (mpu401_read_data(emu
, midi
) == MPU401_ACK
)
136 if (!ok
&& mpu401_read_data(emu
, midi
) == MPU401_ACK
)
141 spin_unlock_irqrestore(&midi
->input_lock
, flags
);
143 snd_printk(KERN_ERR
"midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
145 mpu401_read_stat(emu
, midi
),
146 mpu401_read_data(emu
, midi
));
149 static int snd_emu10k1_midi_input_open(snd_rawmidi_substream_t
* substream
)
152 emu10k1_midi_t
*midi
= (emu10k1_midi_t
*)substream
->rmidi
->private_data
;
156 snd_assert(emu
, return -ENXIO
);
157 spin_lock_irqsave(&midi
->open_lock
, flags
);
158 midi
->midi_mode
|= EMU10K1_MIDI_MODE_INPUT
;
159 midi
->substream_input
= substream
;
160 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_OUTPUT
)) {
161 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
162 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 1);
163 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_ENTER_UART
, 1);
165 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
170 static int snd_emu10k1_midi_output_open(snd_rawmidi_substream_t
* substream
)
173 emu10k1_midi_t
*midi
= (emu10k1_midi_t
*)substream
->rmidi
->private_data
;
177 snd_assert(emu
, return -ENXIO
);
178 spin_lock_irqsave(&midi
->open_lock
, flags
);
179 midi
->midi_mode
|= EMU10K1_MIDI_MODE_OUTPUT
;
180 midi
->substream_output
= substream
;
181 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_INPUT
)) {
182 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
183 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 1);
184 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_ENTER_UART
, 1);
186 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
191 static int snd_emu10k1_midi_input_close(snd_rawmidi_substream_t
* substream
)
194 emu10k1_midi_t
*midi
= (emu10k1_midi_t
*)substream
->rmidi
->private_data
;
198 snd_assert(emu
, return -ENXIO
);
199 spin_lock_irqsave(&midi
->open_lock
, flags
);
200 snd_emu10k1_intr_disable(emu
, midi
->rx_enable
);
201 midi
->midi_mode
&= ~EMU10K1_MIDI_MODE_INPUT
;
202 midi
->substream_input
= NULL
;
203 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_OUTPUT
)) {
204 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
205 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 0);
207 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
212 static int snd_emu10k1_midi_output_close(snd_rawmidi_substream_t
* substream
)
215 emu10k1_midi_t
*midi
= (emu10k1_midi_t
*)substream
->rmidi
->private_data
;
219 snd_assert(emu
, return -ENXIO
);
220 spin_lock_irqsave(&midi
->open_lock
, flags
);
221 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
);
222 midi
->midi_mode
&= ~EMU10K1_MIDI_MODE_OUTPUT
;
223 midi
->substream_output
= NULL
;
224 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_INPUT
)) {
225 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
226 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 0);
228 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
233 static void snd_emu10k1_midi_input_trigger(snd_rawmidi_substream_t
* substream
, int up
)
236 emu10k1_midi_t
*midi
= (emu10k1_midi_t
*)substream
->rmidi
->private_data
;
238 snd_assert(emu
, return);
241 snd_emu10k1_intr_enable(emu
, midi
->rx_enable
);
243 snd_emu10k1_intr_disable(emu
, midi
->rx_enable
);
246 static void snd_emu10k1_midi_output_trigger(snd_rawmidi_substream_t
* substream
, int up
)
249 emu10k1_midi_t
*midi
= (emu10k1_midi_t
*)substream
->rmidi
->private_data
;
253 snd_assert(emu
, return);
259 /* try to send some amount of bytes here before interrupts */
260 spin_lock_irqsave(&midi
->output_lock
, flags
);
262 if (mpu401_output_ready(emu
, midi
)) {
263 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_OUTPUT
) ||
264 snd_rawmidi_transmit(substream
, &byte
, 1) != 1) {
266 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
269 mpu401_write_data(emu
, midi
, byte
);
275 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
276 snd_emu10k1_intr_enable(emu
, midi
->tx_enable
);
278 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
);
286 static snd_rawmidi_ops_t snd_emu10k1_midi_output
=
288 .open
= snd_emu10k1_midi_output_open
,
289 .close
= snd_emu10k1_midi_output_close
,
290 .trigger
= snd_emu10k1_midi_output_trigger
,
293 static snd_rawmidi_ops_t snd_emu10k1_midi_input
=
295 .open
= snd_emu10k1_midi_input_open
,
296 .close
= snd_emu10k1_midi_input_close
,
297 .trigger
= snd_emu10k1_midi_input_trigger
,
300 static void snd_emu10k1_midi_free(snd_rawmidi_t
*rmidi
)
302 emu10k1_midi_t
*midi
= (emu10k1_midi_t
*)rmidi
->private_data
;
303 midi
->interrupt
= NULL
;
307 static int __devinit
emu10k1_midi_init(emu10k1_t
*emu
, emu10k1_midi_t
*midi
, int device
, char *name
)
309 snd_rawmidi_t
*rmidi
;
312 if ((err
= snd_rawmidi_new(emu
->card
, name
, device
, 1, 1, &rmidi
)) < 0)
315 spin_lock_init(&midi
->open_lock
);
316 spin_lock_init(&midi
->input_lock
);
317 spin_lock_init(&midi
->output_lock
);
318 strcpy(rmidi
->name
, name
);
319 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_emu10k1_midi_output
);
320 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_emu10k1_midi_input
);
321 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
|
322 SNDRV_RAWMIDI_INFO_INPUT
|
323 SNDRV_RAWMIDI_INFO_DUPLEX
;
324 rmidi
->private_data
= midi
;
325 rmidi
->private_free
= snd_emu10k1_midi_free
;
330 int __devinit
snd_emu10k1_midi(emu10k1_t
*emu
)
332 emu10k1_midi_t
*midi
= &emu
->midi
;
335 if ((err
= emu10k1_midi_init(emu
, midi
, 0, "EMU10K1 MPU-401 (UART)")) < 0)
338 midi
->tx_enable
= INTE_MIDITXENABLE
;
339 midi
->rx_enable
= INTE_MIDIRXENABLE
;
341 midi
->ipr_tx
= IPR_MIDITRANSBUFEMPTY
;
342 midi
->ipr_rx
= IPR_MIDIRECVBUFEMPTY
;
343 midi
->interrupt
= snd_emu10k1_midi_interrupt
;
347 int __devinit
snd_emu10k1_audigy_midi(emu10k1_t
*emu
)
349 emu10k1_midi_t
*midi
;
353 if ((err
= emu10k1_midi_init(emu
, midi
, 0, "Audigy MPU-401 (UART)")) < 0)
356 midi
->tx_enable
= INTE_MIDITXENABLE
;
357 midi
->rx_enable
= INTE_MIDIRXENABLE
;
358 midi
->port
= A_MUDATA1
;
359 midi
->ipr_tx
= IPR_MIDITRANSBUFEMPTY
;
360 midi
->ipr_rx
= IPR_MIDIRECVBUFEMPTY
;
361 midi
->interrupt
= snd_emu10k1_midi_interrupt
;
364 if ((err
= emu10k1_midi_init(emu
, midi
, 1, "Audigy MPU-401 #2")) < 0)
367 midi
->tx_enable
= INTE_A_MIDITXENABLE2
;
368 midi
->rx_enable
= INTE_A_MIDIRXENABLE2
;
369 midi
->port
= A_MUDATA2
;
370 midi
->ipr_tx
= IPR_A_MIDITRANSBUFEMPTY2
;
371 midi
->ipr_rx
= IPR_A_MIDIRECVBUFEMPTY2
;
372 midi
->interrupt
= snd_emu10k1_midi_interrupt2
;