summaryrefslogtreecommitdiffstats
path: root/sys/i386/isa/sound/mad16_sb_midi.c
blob: d1b3ad0167cc89e80e31fb3db04bd82aa1a75adb (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * sound/mad16_sb_midi.c
 * 
 * The low level driver for MAD16 SoundBlaster-DS-chip-based MIDI.
 * 
 * Copyright by Hannu Savolainen 1993, Aaron Ucko 1995
 * 
 * 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 defined(CONFIG_MAD16) && defined(CONFIG_MIDI)

#define sbc_base mad16_sb_base
#include <i386/isa/sound/sb_card.h>

static int      input_opened = 0;
static int      my_dev;
static int      mad16_sb_base = 0x220;
static int      mad16_sb_irq = 0;
static int      mad16_sb_dsp_ok = 0;
static sound_os_info *midi_osp;

int             mad16_sb_midi_mode = NORMAL_MIDI;
int             mad16_sb_midi_busy = 0;

int             mad16_sb_duplex_midi = 0;
volatile int    mad16_sb_intr_active = 0;

void            (*midi_input_intr) (int dev, unsigned char data);

static void     mad16_sb_midi_init(int model);

static int
mad16_sb_dsp_command(unsigned char val)
{
	int             i;
	unsigned long   limit;

	limit = get_time() + hz / 10;	/* The timeout is 0.1 secods */

	/*
	 * Note! the i<500000 is an emergency exit. The
	 * mad16_sb_dsp_command() is sometimes called while interrupts are
	 * disabled. This means that the timer is disabled also. However the
	 * timeout situation is a abnormal condition. Normally the DSP should
	 * be ready to accept commands after just couple of loops.
	 */

	for (i = 0; i < 500000 && get_time() < limit; i++) {
		if ((inb(DSP_STATUS) & 0x80) == 0) {
			outb(DSP_COMMAND, val);
			return 1;
		}
	}

	printf("MAD16 (SBP mode): DSP Command(%x) Timeout.\n", val);
	printf("IRQ conflict???\n");
	return 0;
}

void
mad16_sbintr(int irq)
{
	int             status;

	unsigned long   flags;
	unsigned char   data;

	status = inb(DSP_DATA_AVAIL);	/* Clear interrupt */

	flags = splhigh();

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

	splx(flags);
}

static int
mad16_sb_reset_dsp(void)
{
	int             loopc;

	outb(DSP_RESET, 1);
	DELAY(10);
	outb(DSP_RESET, 0);
	DELAY(30);

	for (loopc = 0; loopc < 100 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++)
		DELAY(10);
		/* Wait for data available status */

	if (inb(DSP_READ) != 0xAA)
		return 0;	/* Sorry */

	return 1;
}

int
mad16_sb_dsp_detect(struct address_info * hw_config)
{
	mad16_sb_base = hw_config->io_base;
	mad16_sb_irq = hw_config->irq;
	midi_osp = hw_config->osp;

	if (mad16_sb_dsp_ok)
		return 0;	/* Already initialized */
	if (!mad16_sb_reset_dsp())
		return 0;

	return 1;		/* Detected */
}

void
mad16_sb_dsp_init(struct address_info * hw_config)
/*
 * this function now just verifies the reported version and calls
 * mad16_sb_midi_init -- everything else is done elsewhere
 */
{

	midi_osp = hw_config->osp;
	if (snd_set_irq_handler(mad16_sb_irq, mad16_sbintr, midi_osp) < 0) {
		printf("MAD16 SB MIDI: IRQ not free\n");
		return;
	}

	conf_printf("MAD16 MIDI (SB mode)", hw_config);
	mad16_sb_midi_init(2);

	mad16_sb_dsp_ok = 1;
	return;
}

static int
mad16_sb_midi_open(int dev, int mode,
		   void (*input) (int dev, unsigned char data),
		   void (*output) (int dev)
)
{

	if (!mad16_sb_dsp_ok) {
		printf("MAD16_SB Error: MIDI hardware not installed\n");
		return -(ENXIO);
	}
	if (mad16_sb_midi_busy)
		return -(EBUSY);

	if (mode != OPEN_WRITE && !mad16_sb_duplex_midi) {
		if (num_midis == 1)
			printf("MAD16 (SBP mode): Midi input not currently supported\n");
		return -(EPERM);
	}
	mad16_sb_midi_mode = NORMAL_MIDI;
	if (mode != OPEN_WRITE) {
		if (mad16_sb_intr_active)
			return -(EBUSY);
		mad16_sb_midi_mode = UART_MIDI;
	}
	if (mad16_sb_midi_mode == UART_MIDI) {
		mad16_sb_reset_dsp();

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

		input_opened = 1;
		midi_input_intr = input;
	}
	mad16_sb_midi_busy = 1;

	return 0;
}

static void
mad16_sb_midi_close(int dev)
{
	if (mad16_sb_midi_mode == UART_MIDI) {
		mad16_sb_reset_dsp();	/* The only way to kill the UART mode */
	}
	mad16_sb_intr_active = 0;
	mad16_sb_midi_busy = 0;
	input_opened = 0;
}

static int
mad16_sb_midi_out(int dev, unsigned char midi_byte)
{
	unsigned long   flags;

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

	return 1;
}

static int
mad16_sb_midi_start_read(int dev)
{
	if (mad16_sb_midi_mode != UART_MIDI) {
		printf("MAD16 (SBP mode): MIDI input not implemented.\n");
		return -(EPERM);
	}
	return 0;
}

static int
mad16_sb_midi_end_read(int dev)
{
	if (mad16_sb_midi_mode == UART_MIDI) {
		mad16_sb_reset_dsp();
		mad16_sb_intr_active = 0;
	}
	return 0;
}

static int
mad16_sb_midi_ioctl(int dev, unsigned cmd, ioctl_arg arg)
{
	return -(EPERM);
}

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

static struct midi_operations mad16_sb_midi_operations =
{
	{"MAD16 (SBP mode)", 0, 0, SNDCARD_MAD16},
	&std_midi_synth,
	{0},
	mad16_sb_midi_open,
	mad16_sb_midi_close,
	mad16_sb_midi_ioctl,
	mad16_sb_midi_out,
	mad16_sb_midi_start_read,
	mad16_sb_midi_end_read,
	NULL,			/* Kick */
	NULL,			/* command */
	NULL,			/* buffer_status */
	NULL
};

static void
mad16_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++] = &mad16_sb_midi_operations;
}

#endif
OpenPOWER on IntegriCloud