blob: 950c6bcd6b7d9945775cfdefb9bfb9958ca56d5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Takashi Iwaieb4698f2005-11-17 14:50:13 +010031static inline unsigned char mpu401_read(struct snd_emu10k1 *emu,
32 struct snd_emu10k1_midi *mpu, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 if (emu->audigy)
35 return (unsigned char)snd_emu10k1_ptr_read(emu, mpu->port + idx, 0);
36 else
37 return inb(emu->port + mpu->port + idx);
38}
39
Takashi Iwaieb4698f2005-11-17 14:50:13 +010040static inline void mpu401_write(struct snd_emu10k1 *emu,
41 struct snd_emu10k1_midi *mpu, int data, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 if (emu->audigy)
44 snd_emu10k1_ptr_write(emu, mpu->port + idx, 0, data);
45 else
46 outb(data, emu->port + mpu->port + idx);
47}
48
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)
53
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))
56
57#define MPU401_RESET 0xff
58#define MPU401_ENTER_UART 0x3f
59#define MPU401_ACK 0xfe
60
Takashi Iwaieb4698f2005-11-17 14:50:13 +010061static void mpu401_clear_rx(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 int timeout = 100000;
64 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
65 mpu401_read_data(emu, mpu);
66#ifdef CONFIG_SND_DEBUG
67 if (timeout <= 0)
68 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
69#endif
70}
71
72/*
73
74 */
75
Takashi Iwaieb4698f2005-11-17 14:50:13 +010076static void do_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 unsigned char byte;
79
80 if (midi->rmidi == NULL) {
81 snd_emu10k1_intr_disable(emu, midi->tx_enable | midi->rx_enable);
82 return;
83 }
84
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);
89 } else {
90 byte = mpu401_read_data(emu, midi);
91 if (midi->substream_input)
92 snd_rawmidi_receive(midi->substream_input, &byte, 1);
93 }
94 }
95 spin_unlock(&midi->input_lock);
96
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);
102 } else {
103 snd_emu10k1_intr_disable(emu, midi->tx_enable);
104 }
105 }
106 spin_unlock(&midi->output_lock);
107}
108
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100109static void snd_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 do_emu10k1_midi_interrupt(emu, &emu->midi, status);
112}
113
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100114static void snd_emu10k1_midi_interrupt2(struct snd_emu10k1 *emu, unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 do_emu10k1_midi_interrupt(emu, &emu->midi2, status);
117}
118
Randy Dunlapb1308072006-07-04 14:25:26 +0200119static int snd_emu10k1_midi_cmd(struct snd_emu10k1 * emu, struct snd_emu10k1_midi *midi, unsigned char cmd, int ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned long flags;
122 int timeout, ok;
123
124 spin_lock_irqsave(&midi->input_lock, flags);
125 mpu401_write_data(emu, midi, 0x00);
126 /* mpu401_clear_rx(emu, midi); */
127
128 mpu401_write_cmd(emu, midi, cmd);
129 if (ack) {
130 ok = 0;
131 timeout = 10000;
132 while (!ok && timeout-- > 0) {
133 if (mpu401_input_avail(emu, midi)) {
134 if (mpu401_read_data(emu, midi) == MPU401_ACK)
135 ok = 1;
136 }
137 }
138 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
139 ok = 1;
140 } else {
141 ok = 1;
142 }
143 spin_unlock_irqrestore(&midi->input_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +0200144 if (!ok) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
146 cmd, emu->port,
147 mpu401_read_stat(emu, midi),
148 mpu401_read_data(emu, midi));
Randy Dunlapb1308072006-07-04 14:25:26 +0200149 return 1;
150 }
151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100154static int snd_emu10k1_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100156 struct snd_emu10k1 *emu;
157 struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned long flags;
159
160 emu = midi->emu;
161 snd_assert(emu, return -ENXIO);
162 spin_lock_irqsave(&midi->open_lock, flags);
163 midi->midi_mode |= EMU10K1_MIDI_MODE_INPUT;
164 midi->substream_input = substream;
165 if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
166 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +0200167 if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
168 goto error_out;
169 if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
170 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } else {
172 spin_unlock_irqrestore(&midi->open_lock, flags);
173 }
174 return 0;
Randy Dunlapb1308072006-07-04 14:25:26 +0200175
176error_out:
177 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100180static int snd_emu10k1_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100182 struct snd_emu10k1 *emu;
183 struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned long flags;
185
186 emu = midi->emu;
187 snd_assert(emu, return -ENXIO);
188 spin_lock_irqsave(&midi->open_lock, flags);
189 midi->midi_mode |= EMU10K1_MIDI_MODE_OUTPUT;
190 midi->substream_output = substream;
191 if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
192 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +0200193 if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
194 goto error_out;
195 if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
196 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } else {
198 spin_unlock_irqrestore(&midi->open_lock, flags);
199 }
200 return 0;
Randy Dunlapb1308072006-07-04 14:25:26 +0200201
202error_out:
203 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100206static int snd_emu10k1_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100208 struct snd_emu10k1 *emu;
209 struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long flags;
Randy Dunlapb1308072006-07-04 14:25:26 +0200211 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 emu = midi->emu;
214 snd_assert(emu, return -ENXIO);
215 spin_lock_irqsave(&midi->open_lock, flags);
216 snd_emu10k1_intr_disable(emu, midi->rx_enable);
217 midi->midi_mode &= ~EMU10K1_MIDI_MODE_INPUT;
218 midi->substream_input = NULL;
219 if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
220 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +0200221 err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 } else {
223 spin_unlock_irqrestore(&midi->open_lock, flags);
224 }
Randy Dunlapb1308072006-07-04 14:25:26 +0200225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100228static int snd_emu10k1_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100230 struct snd_emu10k1 *emu;
231 struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 unsigned long flags;
Randy Dunlapb1308072006-07-04 14:25:26 +0200233 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 emu = midi->emu;
236 snd_assert(emu, return -ENXIO);
237 spin_lock_irqsave(&midi->open_lock, flags);
238 snd_emu10k1_intr_disable(emu, midi->tx_enable);
239 midi->midi_mode &= ~EMU10K1_MIDI_MODE_OUTPUT;
240 midi->substream_output = NULL;
241 if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
242 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +0200243 err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } else {
245 spin_unlock_irqrestore(&midi->open_lock, flags);
246 }
Randy Dunlapb1308072006-07-04 14:25:26 +0200247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100250static void snd_emu10k1_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100252 struct snd_emu10k1 *emu;
253 struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 emu = midi->emu;
255 snd_assert(emu, return);
256
257 if (up)
258 snd_emu10k1_intr_enable(emu, midi->rx_enable);
259 else
260 snd_emu10k1_intr_disable(emu, midi->rx_enable);
261}
262
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100263static void snd_emu10k1_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100265 struct snd_emu10k1 *emu;
266 struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned long flags;
268
269 emu = midi->emu;
270 snd_assert(emu, return);
271
272 if (up) {
273 int max = 4;
274 unsigned char byte;
275
276 /* try to send some amount of bytes here before interrupts */
277 spin_lock_irqsave(&midi->output_lock, flags);
278 while (max > 0) {
279 if (mpu401_output_ready(emu, midi)) {
280 if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT) ||
281 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
282 /* no more data */
283 spin_unlock_irqrestore(&midi->output_lock, flags);
284 return;
285 }
286 mpu401_write_data(emu, midi, byte);
287 max--;
288 } else {
289 break;
290 }
291 }
292 spin_unlock_irqrestore(&midi->output_lock, flags);
293 snd_emu10k1_intr_enable(emu, midi->tx_enable);
294 } else {
295 snd_emu10k1_intr_disable(emu, midi->tx_enable);
296 }
297}
298
299/*
300
301 */
302
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100303static struct snd_rawmidi_ops snd_emu10k1_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 .open = snd_emu10k1_midi_output_open,
306 .close = snd_emu10k1_midi_output_close,
307 .trigger = snd_emu10k1_midi_output_trigger,
308};
309
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100310static struct snd_rawmidi_ops snd_emu10k1_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 .open = snd_emu10k1_midi_input_open,
313 .close = snd_emu10k1_midi_input_close,
314 .trigger = snd_emu10k1_midi_input_trigger,
315};
316
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100317static void snd_emu10k1_midi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100319 struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 midi->interrupt = NULL;
321 midi->rmidi = NULL;
322}
323
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100324static int __devinit emu10k1_midi_init(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, int device, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100326 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int err;
328
329 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
330 return err;
331 midi->emu = emu;
332 spin_lock_init(&midi->open_lock);
333 spin_lock_init(&midi->input_lock);
334 spin_lock_init(&midi->output_lock);
335 strcpy(rmidi->name, name);
336 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1_midi_output);
337 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1_midi_input);
338 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
339 SNDRV_RAWMIDI_INFO_INPUT |
340 SNDRV_RAWMIDI_INFO_DUPLEX;
341 rmidi->private_data = midi;
342 rmidi->private_free = snd_emu10k1_midi_free;
343 midi->rmidi = rmidi;
344 return 0;
345}
346
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100347int __devinit snd_emu10k1_midi(struct snd_emu10k1 *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100349 struct snd_emu10k1_midi *midi = &emu->midi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int err;
351
352 if ((err = emu10k1_midi_init(emu, midi, 0, "EMU10K1 MPU-401 (UART)")) < 0)
353 return err;
354
355 midi->tx_enable = INTE_MIDITXENABLE;
356 midi->rx_enable = INTE_MIDIRXENABLE;
357 midi->port = MUDATA;
358 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
359 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
360 midi->interrupt = snd_emu10k1_midi_interrupt;
361 return 0;
362}
363
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100364int __devinit snd_emu10k1_audigy_midi(struct snd_emu10k1 *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100366 struct snd_emu10k1_midi *midi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int err;
368
369 midi = &emu->midi;
370 if ((err = emu10k1_midi_init(emu, midi, 0, "Audigy MPU-401 (UART)")) < 0)
371 return err;
372
373 midi->tx_enable = INTE_MIDITXENABLE;
374 midi->rx_enable = INTE_MIDIRXENABLE;
375 midi->port = A_MUDATA1;
376 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
377 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
378 midi->interrupt = snd_emu10k1_midi_interrupt;
379
380 midi = &emu->midi2;
381 if ((err = emu10k1_midi_init(emu, midi, 1, "Audigy MPU-401 #2")) < 0)
382 return err;
383
384 midi->tx_enable = INTE_A_MIDITXENABLE2;
385 midi->rx_enable = INTE_A_MIDIRXENABLE2;
386 midi->port = A_MUDATA2;
387 midi->ipr_tx = IPR_A_MIDITRANSBUFEMPTY2;
388 midi->ipr_rx = IPR_A_MIDIRECVBUFEMPTY2;
389 midi->interrupt = snd_emu10k1_midi_interrupt2;
390 return 0;
391}