* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / sound / pas2_midi.c
blobd029d4c4d65d86052c50e2ef826a0d096387c493
1 /*
2 * sound/pas2_midi.c
4 * The low level driver for the PAS Midi Interface.
5 */
6 /*
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
13 #include <linux/config.h>
16 #include "sound_config.h"
18 #ifdef CONFIG_PAS
19 #ifdef CONFIG_MIDI
21 static int midi_busy = 0, input_opened = 0;
22 static int my_dev;
24 int pas2_mididev=-1;
26 static unsigned char tmp_queue[256];
27 static volatile int qlen;
28 static volatile unsigned char qhead, qtail;
30 static void (*midi_input_intr) (int dev, unsigned char data);
32 static int pas_midi_open(int dev, int mode,
33 void (*input) (int dev, unsigned char data),
34 void (*output) (int dev)
37 int err;
38 unsigned long flags;
39 unsigned char ctrl;
42 if (midi_busy)
43 return -EBUSY;
46 * Reset input and output FIFO pointers
48 pas_write(0x20 | 0x40,
49 0x178b);
51 save_flags(flags);
52 cli();
54 if ((err = pas_set_intr(0x10)) < 0)
56 restore_flags(flags);
57 return err;
60 * Enable input available and output FIFO empty interrupts
63 ctrl = 0;
64 input_opened = 0;
65 midi_input_intr = input;
67 if (mode == OPEN_READ || mode == OPEN_READWRITE)
69 ctrl |= 0x04; /* Enable input */
70 input_opened = 1;
72 if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
74 ctrl |= 0x08 | 0x10; /* Enable output */
76 pas_write(ctrl, 0x178b);
79 * Acknowledge any pending interrupts
82 pas_write(0xff, 0x1B88);
84 restore_flags(flags);
86 midi_busy = 1;
87 qlen = qhead = qtail = 0;
88 return 0;
91 static void pas_midi_close(int dev)
95 * Reset FIFO pointers, disable intrs
97 pas_write(0x20 | 0x40, 0x178b);
99 pas_remove_intr(0x10);
100 midi_busy = 0;
103 static int dump_to_midi(unsigned char midi_byte)
105 int fifo_space, x;
107 fifo_space = ((x = pas_read(0x1B89)) >> 4) & 0x0f;
110 * The MIDI FIFO space register and it's documentation is nonunderstandable.
111 * There seem to be no way to differentiate between buffer full and buffer
112 * empty situations. For this reason we don't never write the buffer
113 * completely full. In this way we can assume that 0 (or is it 15)
114 * means that the buffer is empty.
117 if (fifo_space < 2 && fifo_space != 0) /* Full (almost) */
118 return 0; /* Ask upper layers to retry after some time */
120 pas_write(midi_byte, 0x178A);
122 return 1;
125 static int pas_midi_out(int dev, unsigned char midi_byte)
128 unsigned long flags;
131 * Drain the local queue first
134 save_flags(flags);
135 cli();
137 while (qlen && dump_to_midi(tmp_queue[qhead]))
139 qlen--;
140 qhead++;
143 restore_flags(flags);
146 * Output the byte if the local queue is empty.
149 if (!qlen)
150 if (dump_to_midi(midi_byte))
151 return 1;
154 * Put to the local queue
157 if (qlen >= 256)
158 return 0; /* Local queue full */
160 save_flags(flags);
161 cli();
163 tmp_queue[qtail] = midi_byte;
164 qlen++;
165 qtail++;
167 restore_flags(flags);
169 return 1;
172 static int pas_midi_start_read(int dev)
174 return 0;
177 static int pas_midi_end_read(int dev)
179 return 0;
182 static void pas_midi_kick(int dev)
186 static int pas_buffer_status(int dev)
188 return qlen;
191 #define MIDI_SYNTH_NAME "Pro Audio Spectrum Midi"
192 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
193 #include "midi_synth.h"
195 static struct midi_operations pas_midi_operations =
197 {"Pro Audio Spectrum", 0, 0, SNDCARD_PAS},
198 &std_midi_synth,
199 {0},
200 pas_midi_open,
201 pas_midi_close,
202 NULL,
203 pas_midi_out,
204 pas_midi_start_read,
205 pas_midi_end_read,
206 pas_midi_kick,
207 NULL,
208 pas_buffer_status,
209 NULL
212 void pas_midi_init(void)
214 int dev = sound_alloc_mididev();
216 if (dev == -1)
218 printk(KERN_WARNING "pas_midi_init: Too many midi devices detected\n");
219 return;
221 std_midi_synth.midi_dev = my_dev = dev;
222 midi_devs[dev] = &pas_midi_operations;
223 pas2_mididev = dev;
224 sequencer_init();
227 void pas_midi_interrupt(void)
229 unsigned char stat;
230 int i, incount;
231 unsigned long flags;
233 stat = pas_read(0x1B88);
235 if (stat & 0x04) /* Input data available */
237 incount = pas_read(0x1B89) & 0x0f; /* Input FIFO size */
238 if (!incount)
239 incount = 16;
241 for (i = 0; i < incount; i++)
242 if (input_opened)
244 midi_input_intr(my_dev, pas_read(0x178A));
245 } else
246 pas_read(0x178A); /* Flush */
248 if (stat & (0x08 | 0x10))
250 save_flags(flags);
251 cli();
253 while (qlen && dump_to_midi(tmp_queue[qhead]))
255 qlen--;
256 qhead++;
259 restore_flags(flags);
261 if (stat & 0x40)
263 printk(KERN_WARNING "MIDI output overrun %x,%x\n", pas_read(0x1B89), stat);
265 pas_write(stat, 0x1B88); /* Acknowledge interrupts */
268 #endif
269 #endif