2 * sound/mad16_sb_midi.c
4 * The low level driver for MAD16 SoundBlaster-DS-chip-based MIDI.
6 * Copyright by Hannu Savolainen 1993, Aaron Ucko 1995
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
30 #include <i386/isa/sound/sound_config.h>
32 #if defined(CONFIG_MAD16) && defined(CONFIG_MIDI)
34 #define sbc_base mad16_sb_base
35 #include <i386/isa/sound/sb_card.h>
37 static int input_opened = 0;
39 static int mad16_sb_base = 0x220;
40 static int mad16_sb_irq = 0;
41 static int mad16_sb_dsp_ok = 0;
42 static sound_os_info *midi_osp;
44 int mad16_sb_midi_mode = NORMAL_MIDI;
45 int mad16_sb_midi_busy = 0;
47 int mad16_sb_duplex_midi = 0;
48 volatile int mad16_sb_intr_active = 0;
50 void (*midi_input_intr) (int dev, unsigned char data);
52 static void mad16_sb_midi_init(int model);
55 mad16_sb_dsp_command(unsigned char val)
60 limit = get_time() + hz / 10; /* The timeout is 0.1 secods */
63 * Note! the i<500000 is an emergency exit. The
64 * mad16_sb_dsp_command() is sometimes called while interrupts are
65 * disabled. This means that the timer is disabled also. However the
66 * timeout situation is a abnormal condition. Normally the DSP should
67 * be ready to accept commands after just couple of loops.
70 for (i = 0; i < 500000 && get_time() < limit; i++) {
71 if ((inb(DSP_STATUS) & 0x80) == 0) {
72 outb(DSP_COMMAND, val);
77 printf("MAD16 (SBP mode): DSP Command(%x) Timeout.\n", val);
78 printf("IRQ conflict???\n");
90 status = inb(DSP_DATA_AVAIL); /* Clear interrupt */
96 midi_input_intr(my_dev, data);
102 mad16_sb_reset_dsp(void)
111 for (loopc = 0; loopc < 100 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++)
113 /* Wait for data available status */
115 if (inb(DSP_READ) != 0xAA)
116 return 0; /* Sorry */
122 mad16_sb_dsp_detect(struct address_info * hw_config)
124 mad16_sb_base = hw_config->io_base;
125 mad16_sb_irq = hw_config->irq;
126 midi_osp = hw_config->osp;
129 return 0; /* Already initialized */
130 if (!mad16_sb_reset_dsp())
133 return 1; /* Detected */
137 mad16_sb_dsp_init(struct address_info * hw_config)
139 * this function now just verifies the reported version and calls
140 * mad16_sb_midi_init -- everything else is done elsewhere
144 midi_osp = hw_config->osp;
145 if (snd_set_irq_handler(mad16_sb_irq, mad16_sbintr, midi_osp) < 0) {
146 printf("MAD16 SB MIDI: IRQ not free\n");
150 conf_printf("MAD16 MIDI (SB mode)", hw_config);
151 mad16_sb_midi_init(2);
158 mad16_sb_midi_open(int dev, int mode,
159 void (*input) (int dev, unsigned char data),
160 void (*output) (int dev)
164 if (!mad16_sb_dsp_ok) {
165 printf("MAD16_SB Error: MIDI hardware not installed\n");
168 if (mad16_sb_midi_busy)
171 if (mode != OPEN_WRITE && !mad16_sb_duplex_midi) {
173 printf("MAD16 (SBP mode): Midi input not currently supported\n");
176 mad16_sb_midi_mode = NORMAL_MIDI;
177 if (mode != OPEN_WRITE) {
178 if (mad16_sb_intr_active)
180 mad16_sb_midi_mode = UART_MIDI;
182 if (mad16_sb_midi_mode == UART_MIDI) {
183 mad16_sb_reset_dsp();
185 if (!mad16_sb_dsp_command(0x35))
186 return -(EIO); /* Enter the UART mode */
187 mad16_sb_intr_active = 1;
190 midi_input_intr = input;
192 mad16_sb_midi_busy = 1;
198 mad16_sb_midi_close(int dev)
200 if (mad16_sb_midi_mode == UART_MIDI) {
201 mad16_sb_reset_dsp(); /* The only way to kill the UART mode */
203 mad16_sb_intr_active = 0;
204 mad16_sb_midi_busy = 0;
209 mad16_sb_midi_out(int dev, unsigned char midi_byte)
213 if (mad16_sb_midi_mode == NORMAL_MIDI) {
215 if (mad16_sb_dsp_command(0x38))
216 mad16_sb_dsp_command(midi_byte);
218 printf("MAD16_SB Error: Unable to send a MIDI byte\n");
221 mad16_sb_dsp_command(midi_byte); /* UART write */
227 mad16_sb_midi_start_read(int dev)
229 if (mad16_sb_midi_mode != UART_MIDI) {
230 printf("MAD16 (SBP mode): MIDI input not implemented.\n");
237 mad16_sb_midi_end_read(int dev)
239 if (mad16_sb_midi_mode == UART_MIDI) {
240 mad16_sb_reset_dsp();
241 mad16_sb_intr_active = 0;
247 mad16_sb_midi_ioctl(int dev, unsigned cmd, ioctl_arg arg)
252 #define MIDI_SYNTH_NAME "pseudo-SoundBlaster Midi"
253 #define MIDI_SYNTH_CAPS 0
254 #include <i386/isa/sound/midi_synth.h>
256 static struct midi_operations mad16_sb_midi_operations =
258 {"MAD16 (SBP mode)", 0, 0, SNDCARD_MAD16},
265 mad16_sb_midi_start_read,
266 mad16_sb_midi_end_read,
269 NULL, /* buffer_status */
274 mad16_sb_midi_init(int model)
276 if (num_midis >= MAX_MIDI_DEV) {
277 printf("Sound: Too many midi devices detected\n");
280 std_midi_synth.midi_dev = num_midis;
282 midi_devs[num_midis++] = &mad16_sb_midi_operations;