Remove redundant panic.
[dragonfly.git] / sys / dev / sound / isa / i386 / pas2 / pas2_midi.c
1 /*
2  * sound/pas2_midi.c
3  * 
4  * The low level driver for the PAS Midi Interface.
5  * 
6  * Copyright by Hannu Savolainen 1993
7  * 
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.
15  * 
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
26  * SUCH DAMAGE.
27  * 
28  */
29
30 #include <i386/isa/sound/sound_config.h>
31
32 #if defined(CONFIG_PAS) && defined(CONFIG_MIDI)
33 #include <i386/isa/sound/pas_hw.h>
34
35 static int      midi_busy = 0, input_opened = 0;
36 static int      my_dev;
37 static volatile int ofifo_bytes = 0;
38
39 static u_char tmp_queue[256];
40 static volatile int qlen;
41 static volatile u_char qhead, qtail;
42
43 static void     (*midi_input_intr) (int dev, u_char data);
44
45 static int
46 pas_midi_open(int dev, int mode,
47               void (*input) (int dev, u_char data),
48               void (*output) (int dev)
49 )
50 {
51         int             err;
52         u_long   flags;
53         u_char   ctrl;
54
55
56         if (midi_busy) {
57                 printf("PAS2: Midi busy\n");
58                 return -(EBUSY);
59         }
60         /*
61          * Reset input and output FIFO pointers
62          */
63         pas_write(M_C_RESET_INPUT_FIFO | M_C_RESET_OUTPUT_FIFO,
64                   MIDI_CONTROL);
65
66         flags = splhigh();
67
68         if ((err = pas_set_intr(I_M_MIDI_IRQ_ENABLE)) < 0)
69                 return err;
70
71         /*
72          * Enable input available and output FIFO empty interrupts
73          */
74
75         ctrl = 0;
76         input_opened = 0;
77         midi_input_intr = input;
78
79         if (mode == OPEN_READ || mode == OPEN_READWRITE) {
80                 ctrl |= M_C_ENA_INPUT_IRQ;      /* Enable input */
81                 input_opened = 1;
82         }
83         if (mode == OPEN_WRITE || mode == OPEN_READWRITE) {
84                 ctrl |= M_C_ENA_OUTPUT_IRQ |    /* Enable output */
85                         M_C_ENA_OUTPUT_HALF_IRQ;
86         }
87         pas_write(ctrl,
88                   MIDI_CONTROL);
89
90         /*
91          * Acknowledge any pending interrupts
92          */
93
94         pas_write(0xff, MIDI_STATUS);
95         ofifo_bytes = 0;
96
97         splx(flags);
98
99         midi_busy = 1;
100         qlen = qhead = qtail = 0;
101         return 0;
102 }
103
104 static void
105 pas_midi_close(int dev)
106 {
107
108         /*
109          * Reset FIFO pointers, disable intrs
110          */
111         pas_write(M_C_RESET_INPUT_FIFO | M_C_RESET_OUTPUT_FIFO, MIDI_CONTROL);
112
113         pas_remove_intr(I_M_MIDI_IRQ_ENABLE);
114         midi_busy = 0;
115 }
116
117 static int
118 dump_to_midi(u_char midi_byte)
119 {
120         int             fifo_space, x;
121
122         fifo_space = ((x = pas_read(MIDI_FIFO_STATUS)) >> 4) & 0x0f;
123
124         if (fifo_space == 15 || (fifo_space < 2 && ofifo_bytes > 13)) { /* Fifo full */
125                 return 0;       /* Upper layer will call again */
126         }
127         ofifo_bytes++;
128
129         pas_write(midi_byte, MIDI_DATA);
130
131         return 1;
132 }
133
134 static int
135 pas_midi_out(int dev, u_char midi_byte)
136 {
137
138         u_long   flags;
139
140         /*
141          * Drain the local queue first
142          */
143
144         flags = splhigh();
145
146         while (qlen && dump_to_midi(tmp_queue[qhead])) {
147                 qlen--;
148                 qhead++;
149         }
150
151         splx(flags);
152
153         /*
154          * Output the byte if the local queue is empty.
155          */
156
157         if (!qlen)
158                 if (dump_to_midi(midi_byte))
159                         return 1;       /* OK */
160
161         /*
162          * Put to the local queue
163          */
164
165         if (qlen >= 256)
166                 return 0;       /* Local queue full */
167
168         flags = splhigh();
169
170         tmp_queue[qtail] = midi_byte;
171         qlen++;
172         qtail++;
173
174         splx(flags);
175
176         return 1;
177 }
178
179 static int
180 pas_midi_start_read(int dev)
181 {
182         return 0;
183 }
184
185 static int
186 pas_midi_end_read(int dev)
187 {
188         return 0;
189 }
190
191 static int
192 pas_midi_ioctl(int dev, u_int cmd, ioctl_arg arg)
193 {
194         return -(EINVAL);
195 }
196
197 static void
198 pas_midi_kick(int dev)
199 {
200         ofifo_bytes = 0;
201 }
202
203 static int
204 pas_buffer_status(int dev)
205 {
206         return qlen;
207 }
208
209 #define MIDI_SYNTH_NAME "Pro Audio Spectrum Midi"
210 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
211 #include <i386/isa/sound/midi_synth.h>
212
213 static struct midi_operations pas_midi_operations =
214 {
215         {"Pro Audio Spectrum", 0, 0, SNDCARD_PAS},
216         &std_midi_synth,
217         {0},
218         pas_midi_open,
219         pas_midi_close,
220         pas_midi_ioctl,
221         pas_midi_out,
222         pas_midi_start_read,
223         pas_midi_end_read,
224         pas_midi_kick,
225         NULL,                   /* command */
226         pas_buffer_status,
227         NULL
228 };
229
230 void
231 pas_midi_init()
232 {
233         if (num_midis >= MAX_MIDI_DEV) {
234                 printf("Sound: Too many midi devices detected\n");
235                 return;
236         }
237         std_midi_synth.midi_dev = my_dev = num_midis;
238         midi_devs[num_midis++] = &pas_midi_operations;
239         return;
240 }
241
242 void
243 pas_midi_interrupt(void)
244 {
245         u_char   stat;
246         int             i, incount;
247         u_long   flags;
248
249         stat = pas_read(MIDI_STATUS);
250
251         if (stat & M_S_INPUT_AVAIL) {   /* Input byte available */
252                 incount = pas_read(MIDI_FIFO_STATUS) & 0x0f;    /* Input FIFO count */
253                 if (!incount)
254                         incount = 16;
255
256                 for (i = 0; i < incount; i++)
257                         if (input_opened) {
258                                 midi_input_intr(my_dev, pas_read(MIDI_DATA));
259                         } else
260                                 pas_read(MIDI_DATA);    /* Flush */
261         }
262         if (stat & (M_S_OUTPUT_EMPTY | M_S_OUTPUT_HALF_EMPTY)) {
263                 if (!(stat & M_S_OUTPUT_EMPTY)) {
264                         ofifo_bytes = 8;
265                 } else {
266                         ofifo_bytes = 0;
267                 }
268
269                 flags = splhigh();
270
271                 while (qlen && dump_to_midi(tmp_queue[qhead])) {
272                         qlen--;
273                         qhead++;
274                 }
275
276                 splx(flags);
277         }
278         if (stat & M_S_OUTPUT_OVERRUN) {
279                 printf("MIDI output overrun %x,%x,%d \n", pas_read(MIDI_FIFO_STATUS), stat, ofifo_bytes);
280                 ofifo_bytes = 100;
281         }
282         pas_write(stat, MIDI_STATUS);   /* Acknowledge interrupts */
283 }
284
285 #endif