aboutsummaryrefslogtreecommitdiffstats
path: root/sound/pci/emu10k1/emumpu401.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/pci/emu10k1/emumpu401.c')
-rw-r--r--sound/pci/emu10k1/emumpu401.c374
1 files changed, 374 insertions, 0 deletions
diff --git a/sound/pci/emu10k1/emumpu401.c b/sound/pci/emu10k1/emumpu401.c
new file mode 100644
index 000000000000..eb57458a9665
--- /dev/null
+++ b/sound/pci/emu10k1/emumpu401.c
@@ -0,0 +1,374 @@
1/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of EMU10K1 MPU-401 in UART mode
4 *
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
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>
27
28#define EMU10K1_MIDI_MODE_INPUT (1<<0)
29#define EMU10K1_MIDI_MODE_OUTPUT (1<<1)
30
31static inline unsigned char mpu401_read(emu10k1_t *emu, emu10k1_midi_t *mpu, int idx)
32{
33 if (emu->audigy)
34 return (unsigned char)snd_emu10k1_ptr_read(emu, mpu->port + idx, 0);
35 else
36 return inb(emu->port + mpu->port + idx);
37}
38
39static inline void mpu401_write(emu10k1_t *emu, emu10k1_midi_t *mpu, int data, int idx)
40{
41 if (emu->audigy)
42 snd_emu10k1_ptr_write(emu, mpu->port + idx, 0, data);
43 else
44 outb(data, emu->port + mpu->port + idx);
45}
46
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)
51
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))
54
55#define MPU401_RESET 0xff
56#define MPU401_ENTER_UART 0x3f
57#define MPU401_ACK 0xfe
58
59static void mpu401_clear_rx(emu10k1_t *emu, emu10k1_midi_t *mpu)
60{
61 int timeout = 100000;
62 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
63 mpu401_read_data(emu, mpu);
64#ifdef CONFIG_SND_DEBUG
65 if (timeout <= 0)
66 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
67#endif
68}
69
70/*
71
72 */
73
74static void do_emu10k1_midi_interrupt(emu10k1_t *emu, emu10k1_midi_t *midi, unsigned int status)
75{
76 unsigned char byte;
77
78 if (midi->rmidi == NULL) {
79 snd_emu10k1_intr_disable(emu, midi->tx_enable | midi->rx_enable);
80 return;
81 }
82
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);
87 } else {
88 byte = mpu401_read_data(emu, midi);
89 if (midi->substream_input)
90 snd_rawmidi_receive(midi->substream_input, &byte, 1);
91 }
92 }
93 spin_unlock(&midi->input_lock);
94
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);
100 } else {
101 snd_emu10k1_intr_disable(emu, midi->tx_enable);
102 }
103 }
104 spin_unlock(&midi->output_lock);
105}
106
107static void snd_emu10k1_midi_interrupt(emu10k1_t *emu, unsigned int status)
108{
109 do_emu10k1_midi_interrupt(emu, &emu->midi, status);
110}
111
112static void snd_emu10k1_midi_interrupt2(emu10k1_t *emu, unsigned int status)
113{
114 do_emu10k1_midi_interrupt(emu, &emu->midi2, status);
115}
116
117static void snd_emu10k1_midi_cmd(emu10k1_t * emu, emu10k1_midi_t *midi, unsigned char cmd, int ack)
118{
119 unsigned long flags;
120 int timeout, ok;
121
122 spin_lock_irqsave(&midi->input_lock, flags);
123 mpu401_write_data(emu, midi, 0x00);
124 /* mpu401_clear_rx(emu, midi); */
125
126 mpu401_write_cmd(emu, midi, cmd);
127 if (ack) {
128 ok = 0;
129 timeout = 10000;
130 while (!ok && timeout-- > 0) {
131 if (mpu401_input_avail(emu, midi)) {
132 if (mpu401_read_data(emu, midi) == MPU401_ACK)
133 ok = 1;
134 }
135 }
136 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
137 ok = 1;
138 } else {
139 ok = 1;
140 }
141 spin_unlock_irqrestore(&midi->input_lock, flags);
142 if (!ok)
143 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
144 cmd, emu->port,
145 mpu401_read_stat(emu, midi),
146 mpu401_read_data(emu, midi));
147}
148
149static int snd_emu10k1_midi_input_open(snd_rawmidi_substream_t * substream)
150{
151 emu10k1_t *emu;
152 emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
153 unsigned long flags;
154
155 emu = midi->emu;
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);
164 } else {
165 spin_unlock_irqrestore(&midi->open_lock, flags);
166 }
167 return 0;
168}
169
170static int snd_emu10k1_midi_output_open(snd_rawmidi_substream_t * substream)
171{
172 emu10k1_t *emu;
173 emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
174 unsigned long flags;
175
176 emu = midi->emu;
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);
185 } else {
186 spin_unlock_irqrestore(&midi->open_lock, flags);
187 }
188 return 0;
189}
190
191static int snd_emu10k1_midi_input_close(snd_rawmidi_substream_t * substream)
192{
193 emu10k1_t *emu;
194 emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
195 unsigned long flags;
196
197 emu = midi->emu;
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);
206 } else {
207 spin_unlock_irqrestore(&midi->open_lock, flags);
208 }
209 return 0;
210}
211
212static int snd_emu10k1_midi_output_close(snd_rawmidi_substream_t * substream)
213{
214 emu10k1_t *emu;
215 emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
216 unsigned long flags;
217
218 emu = midi->emu;
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);
227 } else {
228 spin_unlock_irqrestore(&midi->open_lock, flags);
229 }
230 return 0;
231}
232
233static void snd_emu10k1_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
234{
235 emu10k1_t *emu;
236 emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
237 emu = midi->emu;
238 snd_assert(emu, return);
239
240 if (up)
241 snd_emu10k1_intr_enable(emu, midi->rx_enable);
242 else
243 snd_emu10k1_intr_disable(emu, midi->rx_enable);
244}
245
246static void snd_emu10k1_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
247{
248 emu10k1_t *emu;
249 emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
250 unsigned long flags;
251
252 emu = midi->emu;
253 snd_assert(emu, return);
254
255 if (up) {
256 int max = 4;
257 unsigned char byte;
258
259 /* try to send some amount of bytes here before interrupts */
260 spin_lock_irqsave(&midi->output_lock, flags);
261 while (max > 0) {
262 if (mpu401_output_ready(emu, midi)) {
263 if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT) ||
264 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
265 /* no more data */
266 spin_unlock_irqrestore(&midi->output_lock, flags);
267 return;
268 }
269 mpu401_write_data(emu, midi, byte);
270 max--;
271 } else {
272 break;
273 }
274 }
275 spin_unlock_irqrestore(&midi->output_lock, flags);
276 snd_emu10k1_intr_enable(emu, midi->tx_enable);
277 } else {
278 snd_emu10k1_intr_disable(emu, midi->tx_enable);
279 }
280}
281
282/*
283
284 */
285
286static snd_rawmidi_ops_t snd_emu10k1_midi_output =
287{
288 .open = snd_emu10k1_midi_output_open,
289 .close = snd_emu10k1_midi_output_close,
290 .trigger = snd_emu10k1_midi_output_trigger,
291};
292
293static snd_rawmidi_ops_t snd_emu10k1_midi_input =
294{
295 .open = snd_emu10k1_midi_input_open,
296 .close = snd_emu10k1_midi_input_close,
297 .trigger = snd_emu10k1_midi_input_trigger,
298};
299
300static void snd_emu10k1_midi_free(snd_rawmidi_t *rmidi)
301{
302 emu10k1_midi_t *midi = (emu10k1_midi_t *)rmidi->private_data;
303 midi->interrupt = NULL;
304 midi->rmidi = NULL;
305}
306
307static int __devinit emu10k1_midi_init(emu10k1_t *emu, emu10k1_midi_t *midi, int device, char *name)
308{
309 snd_rawmidi_t *rmidi;
310 int err;
311
312 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
313 return err;
314 midi->emu = emu;
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;
326 midi->rmidi = rmidi;
327 return 0;
328}
329
330int __devinit snd_emu10k1_midi(emu10k1_t *emu)
331{
332 emu10k1_midi_t *midi = &emu->midi;
333 int err;
334
335 if ((err = emu10k1_midi_init(emu, midi, 0, "EMU10K1 MPU-401 (UART)")) < 0)
336 return err;
337
338 midi->tx_enable = INTE_MIDITXENABLE;
339 midi->rx_enable = INTE_MIDIRXENABLE;
340 midi->port = MUDATA;
341 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
342 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
343 midi->interrupt = snd_emu10k1_midi_interrupt;
344 return 0;
345}
346
347int __devinit snd_emu10k1_audigy_midi(emu10k1_t *emu)
348{
349 emu10k1_midi_t *midi;
350 int err;
351
352 midi = &emu->midi;
353 if ((err = emu10k1_midi_init(emu, midi, 0, "Audigy MPU-401 (UART)")) < 0)
354 return err;
355
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;
362
363 midi = &emu->midi2;
364 if ((err = emu10k1_midi_init(emu, midi, 1, "Audigy MPU-401 #2")) < 0)
365 return err;
366
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;
373 return 0;
374}