4 * The low level driver for the MPU-401 UART emulation of the SB16.
6 * Copyright by Hannu Savolainen 1993
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 2.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/i386/isa/sound/sb16_midi.c,v 1.20 1999/12/27 04:37:18 tanimura Exp $
32 #include <i386/isa/sound/sound_config.h>
33 #include <i386/isa/sound/sbcard.h>
35 #if defined(CONFIG_SB) && defined(CONFIG_SB16) && defined(CONFIG_MIDI)
40 #define DATAPORT (sb16midi_base)
41 #define COMDPORT (sb16midi_base+0x100)
42 #define STATPORT (sb16midi_base+0x100)
44 #define DATAPORT (sb16midi_base)
45 #define COMDPORT (sb16midi_base+1)
46 #define STATPORT (sb16midi_base+1)
49 extern sound_os_info *sb_osp;
51 #define sb16midi_status() inb( STATPORT)
52 #define input_avail() (!(sb16midi_status()&INPUT_AVAIL))
53 #define output_ready() (!(sb16midi_status()&OUTPUT_READY))
54 #define sb16midi_cmd(cmd) outb( COMDPORT, cmd)
55 #define sb16midi_read() inb( DATAPORT)
56 #define sb16midi_write(byte) outb( DATAPORT, byte)
58 #define OUTPUT_READY 0x40
59 #define INPUT_AVAIL 0x80
61 #define MPU_RESET 0xFF
62 #define UART_MODE_ON 0x3F
64 static int sb16midi_opened = 0;
66 static int sb16midi_base = 0x80d2;
68 static int sb16midi_base = 0x330;
70 static int sb16midi_detected = 0;
74 static int reset_sb16midi(void);
75 static void (*midi_input_intr) (int dev, u_char data);
76 static volatile u_char input_byte;
79 sb16midi_input_loop(void)
81 while (input_avail()) {
82 u_char c = sb16midi_read();
86 else if (sb16midi_opened & OPEN_READ && midi_input_intr)
87 midi_input_intr(my_dev, c);
92 sb16midiintr(int unit)
95 sb16midi_input_loop();
99 sb16midi_open(int dev, int mode,
100 void (*input) (int dev, u_char data),
101 void (*output) (int dev)
104 if (sb16midi_opened) {
107 sb16midi_input_loop();
109 midi_input_intr = input;
110 sb16midi_opened = mode;
116 sb16midi_close(int dev)
122 sb16midi_out(int dev, u_char midi_byte)
128 * Test for input since pending input seems to block the output.
134 sb16midi_input_loop();
139 * Sometimes it takes about 13000 loops before the output becomes
140 * ready (After reset). Normally it takes just about 10 loops.
143 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /* Wait */
145 if (!output_ready()) {
146 printf("MPU-401: Timeout\n");
149 sb16midi_write(midi_byte);
154 sb16midi_start_read(int dev)
160 sb16midi_end_read(int dev)
166 sb16midi_ioctl(int dev, u_int cmd, ioctl_arg arg)
172 sb16midi_kick(int dev)
177 sb16midi_buffer_status(int dev)
179 return 0; /* No data in buffers */
182 #define MIDI_SYNTH_NAME "SoundBlaster 16 Midi"
183 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
184 #include <i386/isa/sound/midi_synth.h>
186 static struct midi_operations sb16midi_operations =
188 {"SoundBlaster 16 Midi", 0, 0, SNDCARD_SB16MIDI},
199 sb16midi_buffer_status,
205 attach_sb16midi(struct address_info * hw_config)
210 sb16midi_base = hw_config->io_base;
212 if (!sb16midi_detected)
216 for (timeout = 30000; timeout < 0 && !output_ready(); timeout--); /* Wait */
218 sb16midi_cmd(UART_MODE_ON);
221 for (timeout = 50000; timeout > 0 && !ok; timeout--)
222 if (input_byte == MPU_ACK)
224 else if (input_avail())
225 if (sb16midi_read() == MPU_ACK)
230 if (num_midis >= MAX_MIDI_DEV) {
231 printf("Sound: Too many midi devices detected\n");
235 conf_printf("SoundBlaster MPU-401", hw_config);
236 std_midi_synth.midi_dev = my_dev = num_midis;
237 midi_devs[num_midis++] = &sb16midi_operations;
247 * Send the RESET command. Try again if no success at the first time.
250 if (inb(STATPORT) == 0xff)
255 /* flags = splhigh(); */
257 for (n = 0; n < 2 && !ok; n++) {
258 for (timeout = 30000; timeout < 0 && !output_ready(); timeout--); /* Wait */
260 sb16midi_cmd(MPU_RESET); /* Send MPU-401 RESET Command */
263 * Wait at least 25 msec. This method is not accurate so
264 * let's make the loop bit longer. Cannot sleep since this is
265 * called during boot.
268 for (timeout = 50000; timeout > 0 && !ok; timeout--)
269 if (input_byte == MPU_ACK) /* Interrupt */
271 else if (input_avail())
272 if (sb16midi_read() == MPU_ACK)
279 sb16midi_input_loop(); /* Flush input before enabling
289 probe_sb16midi(struct address_info * hw_config)
292 struct address_info *sb_config;
295 return 0; /* Not a SB16 */
297 if (!(sb_config = sound_getconf(SNDCARD_SB))) {
298 printf("SB16 Error: Plain SB not configured\n");
302 sb16midi_base = hw_config->io_base;
304 ok = reset_sb16midi();
306 sb16midi_detected = ok;