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(struct snd_emu10k1
*emu
,
32 struct snd_emu10k1_midi
*mpu
, int idx
)
35 return (unsigned char)snd_emu10k1_ptr_read(emu
, mpu
->port
+ idx
, 0);
37 return inb(emu
->port
+ mpu
->port
+ idx
);
40 static inline void mpu401_write(struct snd_emu10k1
*emu
,
41 struct snd_emu10k1_midi
*mpu
, int data
, int idx
)
44 snd_emu10k1_ptr_write(emu
, mpu
->port
+ idx
, 0, data
);
46 outb(data
, emu
->port
+ mpu
->port
+ idx
);
49 #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
50 #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
51 #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
52 #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
54 #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
55 #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
57 #define MPU401_RESET 0xff
58 #define MPU401_ENTER_UART 0x3f
59 #define MPU401_ACK 0xfe
61 static void mpu401_clear_rx(struct snd_emu10k1
*emu
, struct snd_emu10k1_midi
*mpu
)
64 for (; timeout
> 0 && mpu401_input_avail(emu
, mpu
); timeout
--)
65 mpu401_read_data(emu
, mpu
);
66 #ifdef CONFIG_SND_DEBUG
68 snd_printk(KERN_ERR
"cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu
, mpu
));
76 static void do_emu10k1_midi_interrupt(struct snd_emu10k1
*emu
, struct snd_emu10k1_midi
*midi
, unsigned int status
)
80 if (midi
->rmidi
== NULL
) {
81 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
| midi
->rx_enable
);
85 spin_lock(&midi
->input_lock
);
86 if ((status
& midi
->ipr_rx
) && mpu401_input_avail(emu
, midi
)) {
87 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_INPUT
)) {
88 mpu401_clear_rx(emu
, midi
);
90 byte
= mpu401_read_data(emu
, midi
);
91 if (midi
->substream_input
)
92 snd_rawmidi_receive(midi
->substream_input
, &byte
, 1);
95 spin_unlock(&midi
->input_lock
);
97 spin_lock(&midi
->output_lock
);
98 if ((status
& midi
->ipr_tx
) && mpu401_output_ready(emu
, midi
)) {
99 if (midi
->substream_output
&&
100 snd_rawmidi_transmit(midi
->substream_output
, &byte
, 1) == 1) {
101 mpu401_write_data(emu
, midi
, byte
);
103 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
);
106 spin_unlock(&midi
->output_lock
);
109 static void snd_emu10k1_midi_interrupt(struct snd_emu10k1
*emu
, unsigned int status
)
111 do_emu10k1_midi_interrupt(emu
, &emu
->midi
, status
);
114 static void snd_emu10k1_midi_interrupt2(struct snd_emu10k1
*emu
, unsigned int status
)
116 do_emu10k1_midi_interrupt(emu
, &emu
->midi2
, status
);
119 static void snd_emu10k1_midi_cmd(struct snd_emu10k1
* emu
, struct snd_emu10k1_midi
*midi
, unsigned char cmd
, int ack
)
124 spin_lock_irqsave(&midi
->input_lock
, flags
);
125 mpu401_write_data(emu
, midi
, 0x00);
126 /* mpu401_clear_rx(emu, midi); */
128 mpu401_write_cmd(emu
, midi
, cmd
);
132 while (!ok
&& timeout
-- > 0) {
133 if (mpu401_input_avail(emu
, midi
)) {
134 if (mpu401_read_data(emu
, midi
) == MPU401_ACK
)
138 if (!ok
&& mpu401_read_data(emu
, midi
) == MPU401_ACK
)
143 spin_unlock_irqrestore(&midi
->input_lock
, flags
);
145 snd_printk(KERN_ERR
"midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
147 mpu401_read_stat(emu
, midi
),
148 mpu401_read_data(emu
, midi
));
151 static int snd_emu10k1_midi_input_open(struct snd_rawmidi_substream
*substream
)
153 struct snd_emu10k1
*emu
;
154 struct snd_emu10k1_midi
*midi
= (struct snd_emu10k1_midi
*)substream
->rmidi
->private_data
;
158 snd_assert(emu
, return -ENXIO
);
159 spin_lock_irqsave(&midi
->open_lock
, flags
);
160 midi
->midi_mode
|= EMU10K1_MIDI_MODE_INPUT
;
161 midi
->substream_input
= substream
;
162 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_OUTPUT
)) {
163 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
164 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 1);
165 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_ENTER_UART
, 1);
167 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
172 static int snd_emu10k1_midi_output_open(struct snd_rawmidi_substream
*substream
)
174 struct snd_emu10k1
*emu
;
175 struct snd_emu10k1_midi
*midi
= (struct snd_emu10k1_midi
*)substream
->rmidi
->private_data
;
179 snd_assert(emu
, return -ENXIO
);
180 spin_lock_irqsave(&midi
->open_lock
, flags
);
181 midi
->midi_mode
|= EMU10K1_MIDI_MODE_OUTPUT
;
182 midi
->substream_output
= substream
;
183 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_INPUT
)) {
184 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
185 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 1);
186 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_ENTER_UART
, 1);
188 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
193 static int snd_emu10k1_midi_input_close(struct snd_rawmidi_substream
*substream
)
195 struct snd_emu10k1
*emu
;
196 struct snd_emu10k1_midi
*midi
= (struct snd_emu10k1_midi
*)substream
->rmidi
->private_data
;
200 snd_assert(emu
, return -ENXIO
);
201 spin_lock_irqsave(&midi
->open_lock
, flags
);
202 snd_emu10k1_intr_disable(emu
, midi
->rx_enable
);
203 midi
->midi_mode
&= ~EMU10K1_MIDI_MODE_INPUT
;
204 midi
->substream_input
= NULL
;
205 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_OUTPUT
)) {
206 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
207 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 0);
209 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
214 static int snd_emu10k1_midi_output_close(struct snd_rawmidi_substream
*substream
)
216 struct snd_emu10k1
*emu
;
217 struct snd_emu10k1_midi
*midi
= (struct snd_emu10k1_midi
*)substream
->rmidi
->private_data
;
221 snd_assert(emu
, return -ENXIO
);
222 spin_lock_irqsave(&midi
->open_lock
, flags
);
223 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
);
224 midi
->midi_mode
&= ~EMU10K1_MIDI_MODE_OUTPUT
;
225 midi
->substream_output
= NULL
;
226 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_INPUT
)) {
227 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
228 snd_emu10k1_midi_cmd(emu
, midi
, MPU401_RESET
, 0);
230 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
235 static void snd_emu10k1_midi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
237 struct snd_emu10k1
*emu
;
238 struct snd_emu10k1_midi
*midi
= (struct snd_emu10k1_midi
*)substream
->rmidi
->private_data
;
240 snd_assert(emu
, return);
243 snd_emu10k1_intr_enable(emu
, midi
->rx_enable
);
245 snd_emu10k1_intr_disable(emu
, midi
->rx_enable
);
248 static void snd_emu10k1_midi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
250 struct snd_emu10k1
*emu
;
251 struct snd_emu10k1_midi
*midi
= (struct snd_emu10k1_midi
*)substream
->rmidi
->private_data
;
255 snd_assert(emu
, return);
261 /* try to send some amount of bytes here before interrupts */
262 spin_lock_irqsave(&midi
->output_lock
, flags
);
264 if (mpu401_output_ready(emu
, midi
)) {
265 if (!(midi
->midi_mode
& EMU10K1_MIDI_MODE_OUTPUT
) ||
266 snd_rawmidi_transmit(substream
, &byte
, 1) != 1) {
268 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
271 mpu401_write_data(emu
, midi
, byte
);
277 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
278 snd_emu10k1_intr_enable(emu
, midi
->tx_enable
);
280 snd_emu10k1_intr_disable(emu
, midi
->tx_enable
);
288 static struct snd_rawmidi_ops snd_emu10k1_midi_output
=
290 .open
= snd_emu10k1_midi_output_open
,
291 .close
= snd_emu10k1_midi_output_close
,
292 .trigger
= snd_emu10k1_midi_output_trigger
,
295 static struct snd_rawmidi_ops snd_emu10k1_midi_input
=
297 .open
= snd_emu10k1_midi_input_open
,
298 .close
= snd_emu10k1_midi_input_close
,
299 .trigger
= snd_emu10k1_midi_input_trigger
,
302 static void snd_emu10k1_midi_free(struct snd_rawmidi
*rmidi
)
304 struct snd_emu10k1_midi
*midi
= (struct snd_emu10k1_midi
*)rmidi
->private_data
;
305 midi
->interrupt
= NULL
;
309 static int __devinit
emu10k1_midi_init(struct snd_emu10k1
*emu
, struct snd_emu10k1_midi
*midi
, int device
, char *name
)
311 struct snd_rawmidi
*rmidi
;
314 if ((err
= snd_rawmidi_new(emu
->card
, name
, device
, 1, 1, &rmidi
)) < 0)
317 spin_lock_init(&midi
->open_lock
);
318 spin_lock_init(&midi
->input_lock
);
319 spin_lock_init(&midi
->output_lock
);
320 strcpy(rmidi
->name
, name
);
321 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_emu10k1_midi_output
);
322 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_emu10k1_midi_input
);
323 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
|
324 SNDRV_RAWMIDI_INFO_INPUT
|
325 SNDRV_RAWMIDI_INFO_DUPLEX
;
326 rmidi
->private_data
= midi
;
327 rmidi
->private_free
= snd_emu10k1_midi_free
;
332 int __devinit
snd_emu10k1_midi(struct snd_emu10k1
*emu
)
334 struct snd_emu10k1_midi
*midi
= &emu
->midi
;
337 if ((err
= emu10k1_midi_init(emu
, midi
, 0, "EMU10K1 MPU-401 (UART)")) < 0)
340 midi
->tx_enable
= INTE_MIDITXENABLE
;
341 midi
->rx_enable
= INTE_MIDIRXENABLE
;
343 midi
->ipr_tx
= IPR_MIDITRANSBUFEMPTY
;
344 midi
->ipr_rx
= IPR_MIDIRECVBUFEMPTY
;
345 midi
->interrupt
= snd_emu10k1_midi_interrupt
;
349 int __devinit
snd_emu10k1_audigy_midi(struct snd_emu10k1
*emu
)
351 struct snd_emu10k1_midi
*midi
;
355 if ((err
= emu10k1_midi_init(emu
, midi
, 0, "Audigy MPU-401 (UART)")) < 0)
358 midi
->tx_enable
= INTE_MIDITXENABLE
;
359 midi
->rx_enable
= INTE_MIDIRXENABLE
;
360 midi
->port
= A_MUDATA1
;
361 midi
->ipr_tx
= IPR_MIDITRANSBUFEMPTY
;
362 midi
->ipr_rx
= IPR_MIDIRECVBUFEMPTY
;
363 midi
->interrupt
= snd_emu10k1_midi_interrupt
;
366 if ((err
= emu10k1_midi_init(emu
, midi
, 1, "Audigy MPU-401 #2")) < 0)
369 midi
->tx_enable
= INTE_A_MIDITXENABLE2
;
370 midi
->rx_enable
= INTE_A_MIDIRXENABLE2
;
371 midi
->port
= A_MUDATA2
;
372 midi
->ipr_tx
= IPR_A_MIDITRANSBUFEMPTY2
;
373 midi
->ipr_rx
= IPR_A_MIDIRECVBUFEMPTY2
;
374 midi
->interrupt
= snd_emu10k1_midi_interrupt2
;