aboutsummaryrefslogtreecommitdiff
path: root/sys/i386/isa/sound/sb_midi.c
blob: a1d50bdc1357b9537c48fd32f5d400dc83321341 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * sound/sb_dsp.c
 * 
 * The low level driver for the SoundBlaster DS chips.
 * 
 * Copyright by Hannu Savolainen 1993
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer. 2.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

#include <i386/isa/sound/sound_config.h>

#if (NSND > 0) && defined(CONFIG_SB) && defined(CONFIG_MIDI)

#include <i386/isa/sound/sbcard.h>
#undef SB_TEST_IRQ

/*
 * The DSP channel can be used either for input or output. Variable
 * 'sb_irq_mode' will be set when the program calls read or write first time
 * after open. Current version doesn't support mode changes without closing
 * and reopening the device. Support for this feature may be implemented in a
 * future version of this driver.
 */

extern int      sb_dsp_ok;	/* Set to 1 atfer successful initialization */
extern int      sbc_base;

extern int      sb_midi_mode;
extern int      sb_midi_busy;	/* 1 if the process has output to MIDI */
extern int      sb_dsp_busy;
extern int      sb_dsp_highspeed;

extern volatile int sb_irq_mode;
extern int      sb_duplex_midi;
extern int      sb_intr_active;
int             input_opened = 0;
static int      my_dev;

extern sound_os_info *sb_osp;

void            (*midi_input_intr) (int dev, u_char data);

static int
sb_midi_open(int dev, int mode, void (*input) (int dev, u_char data),
	     void (*output) (int dev))
{
    int             ret;

    if (!sb_dsp_ok) {
	printf("SB Error: MIDI hardware not installed\n");
	return -(ENXIO);
    }
    if (sb_midi_busy)
	return -(EBUSY);

    if (mode != OPEN_WRITE && !sb_duplex_midi) {
	if (num_midis == 1)
	    printf("SoundBlaster: Midi input not currently supported\n");
	return -(EPERM);
    }
    sb_midi_mode = NORMAL_MIDI;
    if (mode != OPEN_WRITE) {
	if (sb_dsp_busy || sb_intr_active)
	    return -(EBUSY);
	sb_midi_mode = UART_MIDI;
    }
    if (sb_dsp_highspeed) {
	printf("SB Error: Midi output not possible during stereo or high speed audio\n");
	return -(EBUSY);
    }
    if (sb_midi_mode == UART_MIDI) {
	sb_irq_mode = IMODE_MIDI;

	sb_reset_dsp();

	if (!sb_dsp_command(0x35))
	    return -(EIO);	/* Enter the UART mode */
	sb_intr_active = 1;

	input_opened = 1;
	midi_input_intr = input;
    }
    sb_midi_busy = 1;

    return 0;
}

static void
sb_midi_close(int dev)
{
    if (sb_midi_mode == UART_MIDI) {
	sb_reset_dsp();	/* The only way to kill the UART mode */
    }
    sb_intr_active = 0;
    sb_midi_busy = 0;
    input_opened = 0;
}

static int
sb_midi_out(int dev, u_char midi_byte)
{
    u_long   flags;

    if (sb_midi_mode == NORMAL_MIDI) {
	flags = splhigh();
	if (sb_dsp_command(0x38))
	    sb_dsp_command(midi_byte);
	else
	    printf("SB Error: Unable to send a MIDI byte\n");
	splx(flags);
    } else
	sb_dsp_command(midi_byte);	/* UART write */

    return 1;
}

static int
sb_midi_start_read(int dev)
{
    if (sb_midi_mode != UART_MIDI) {
	printf("SoundBlaster: MIDI input not implemented.\n");
	return -(EPERM);
    }
    return 0;
}

static int
sb_midi_end_read(int dev)
{
    if (sb_midi_mode == UART_MIDI) {
	sb_reset_dsp();
	sb_intr_active = 0;
    }
    return 0;
}

static int
sb_midi_ioctl(int dev, u_int cmd, ioctl_arg arg)
{
    return -(EPERM);
}

void
sb_midi_interrupt(int dummy)
{
    u_long   flags;
    u_char   data;

    flags = splhigh();

    data = inb(DSP_READ);
    if (input_opened)
	midi_input_intr(my_dev, data);

    splx(flags);
}

#define MIDI_SYNTH_NAME	"SoundBlaster Midi"
#define MIDI_SYNTH_CAPS	0
#include <i386/isa/sound/midi_synth.h>

static struct midi_operations sb_midi_operations =
{
	{"SoundBlaster", 0, 0, SNDCARD_SB},
	&std_midi_synth,
	{0},
	sb_midi_open,
	sb_midi_close,
	sb_midi_ioctl,
	sb_midi_out,
	sb_midi_start_read,
	sb_midi_end_read,
	NULL,			/* Kick */
	NULL,			/* command */
	NULL,			/* buffer_status */
	NULL
};

void
sb_midi_init(int model)
{
    if (num_midis >= MAX_MIDI_DEV) {
	printf("Sound: Too many midi devices detected\n");
	return;
    }
    std_midi_synth.midi_dev = num_midis;
    my_dev = num_midis;
    midi_devs[num_midis++] = &sb_midi_operations;
}

#endif