Merge from vendor branch AWK:
[dragonfly.git] / sys / dev / sound / isa / i386 / uart / uart6850.c
1 /*
2  * sound/uart6850.c
3  * 
4  * Copyright by Hannu Savolainen 1993
5  * 
6  * Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl: added 6850 support, used
7  * with COVOX SoundMaster II and custom cards.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met: 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer. 2.
13  * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  * 
29  */
30
31 #include <i386/isa/sound/sound_config.h>
32
33 #if     NSND > 0
34
35 #if 1
36 /* #if defined(CONFIG_UART6850) && defined(CONFIG_MIDI) */
37
38 #define DATAPORT   (uart6850_base)      /* * * Midi6850 Data I/O Port on IBM */
39 #define COMDPORT   (uart6850_base+1)    /* * * Midi6850 Command Port on IBM   */
40 #define STATPORT   (uart6850_base+1)    /* * * Midi6850 Status Port on IBM   */
41
42 #define uart6850_status()               inb( STATPORT)
43 #define input_avail()           (uart6850_status()&INPUT_AVAIL)
44 #define output_ready()          (uart6850_status()&OUTPUT_READY)
45 #define uart6850_cmd(cmd)       outb( COMDPORT,  cmd)
46 #define uart6850_read()         inb( DATAPORT)
47 #define uart6850_write(byte)    outb( DATAPORT,  byte)
48
49 #define OUTPUT_READY    0x02    /* * * Mask for Data Read Ready Bit   */
50 #define INPUT_AVAIL     0x01    /* * * Mask for Data Send Ready Bit   */
51
52 #define UART_RESET      0x95    /* * * 6850 Total Reset Command   */
53 #define UART_MODE_ON    0x03    /* * * 6850 Send/Receive UART Mode   */
54
55 static int      uart6850_opened = 0;
56 static int      uart6850_base = 0x330;
57 static int      uart6850_irq;
58 static int      uart6850_detected = 0;
59 static int      my_dev;
60
61 static int      reset_uart6850(void);
62 static void     (*midi_input_intr) (int dev, u_char data);
63 static void     poll_uart6850(void *dummy);
64
65
66 static sound_os_info *uart6850_osp;
67
68 static void
69 uart6850_input_loop(void)
70 {
71     int             count;
72
73     count = 10;
74
75     while (count)               /* Not timed out */
76         if (input_avail()) {
77             u_char   c = uart6850_read();
78
79             count = 100;
80
81             if (uart6850_opened & OPEN_READ)
82                 midi_input_intr(my_dev, c);
83             } else
84                 while (!input_avail() && count)
85                     count--;
86 }
87
88 void
89 m6850intr(int irq)
90 {
91     if (input_avail())
92         uart6850_input_loop();
93 }
94
95 /*
96  * It looks like there is no input interrupts in the UART mode. Let's try
97  * polling.
98  */
99
100 static void
101 poll_uart6850(void * dummy)
102 {
103     u_long   flags;
104
105     if (!(uart6850_opened & OPEN_READ))
106         return;         /* Device has been closed */
107
108     flags = splhigh();
109
110     if (input_avail())
111         uart6850_input_loop();
112
113
114     timeout( poll_uart6850, 0, 1);      /* Come back later */
115
116     splx(flags);
117 }
118
119 static int
120 uart6850_open(int dev, int mode,
121               void (*input) (int dev, u_char data),
122               void (*output) (int dev)
123 )
124 {
125     if (uart6850_opened) {
126         printf("Midi6850: Midi busy\n");
127         return -(EBUSY);
128     }
129     uart6850_cmd(UART_RESET);
130
131     uart6850_input_loop();
132
133     midi_input_intr = input;
134     uart6850_opened = mode;
135     poll_uart6850(0);   /* Enable input polling */
136
137     return 0;
138 }
139
140 static void
141 uart6850_close(int dev)
142 {
143     uart6850_cmd(UART_MODE_ON);
144
145     uart6850_opened = 0;
146 }
147
148 static int
149 uart6850_out(int dev, u_char midi_byte)
150 {
151     int             timeout;
152     u_long   flags;
153
154     /*
155      * Test for input since pending input seems to block the output.
156      */
157
158     flags = splhigh();
159
160     if (input_avail())
161         uart6850_input_loop();
162
163     splx(flags);
164
165     /*
166      * Sometimes it takes about 13000 loops before the output becomes
167      * ready (After reset). Normally it takes just about 10 loops.
168      */
169
170     for (timeout = 30000; timeout > 0 && !output_ready(); timeout--);   /* Wait */
171
172     if (!output_ready()) {
173         printf("Midi6850: Timeout\n");
174         return 0;
175     }
176     uart6850_write(midi_byte);
177     return 1;
178 }
179
180 static int
181 uart6850_command(int dev, u_char *midi_byte)
182 {
183     return 1;
184 }
185
186 static int
187 uart6850_start_read(int dev)
188 {
189     return 0;
190 }
191
192 static int
193 uart6850_end_read(int dev)
194 {
195     return 0;
196 }
197
198 static int
199 uart6850_ioctl(int dev, u_int cmd, ioctl_arg arg)
200 {
201     return -(EINVAL);
202 }
203
204 static void
205 uart6850_kick(int dev)
206 {
207 }
208
209 static int
210 uart6850_buffer_status(int dev)
211 {
212     return 0;           /* No data in buffers */
213 }
214
215 #define MIDI_SYNTH_NAME "6850 UART Midi"
216 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
217 #include <i386/isa/sound/midi_synth.h>
218
219 static struct midi_operations uart6850_operations =
220 {
221         {"6850 UART", 0, 0, SNDCARD_UART6850},
222         &std_midi_synth,
223         {0},
224         uart6850_open,
225         uart6850_close,
226         uart6850_ioctl,
227         uart6850_out,
228         uart6850_start_read,
229         uart6850_end_read,
230         uart6850_kick,
231         uart6850_command,
232         uart6850_buffer_status
233 };
234
235
236 void
237 attach_uart6850(struct address_info * hw_config)
238 {
239     int             ok, timeout;
240     u_long   flags;
241
242     if (num_midis >= MAX_MIDI_DEV) {
243         printf("Sound: Too many midi devices detected\n");
244         return ;
245     }
246     uart6850_base = hw_config->io_base;
247     uart6850_osp = hw_config->osp;
248     uart6850_irq = hw_config->irq;
249
250     if (!uart6850_detected)
251         return ;
252
253     flags = splhigh();
254
255     for (timeout = 30000; timeout < 0 && !output_ready(); timeout--);   /* Wait */
256     uart6850_cmd(UART_MODE_ON);
257
258     ok = 1;
259
260     splx(flags);
261
262     conf_printf("6850 Midi Interface", hw_config);
263
264     std_midi_synth.midi_dev = my_dev = num_midis;
265     midi_devs[num_midis++] = &uart6850_operations;
266     return ;
267 }
268
269 static int
270 reset_uart6850(void)
271 {
272     uart6850_read();
273     return 1;           /* OK */
274 }
275
276
277 int
278 probe_uart6850(struct address_info * hw_config)
279 {
280     int             ok = 0;
281
282     uart6850_osp = hw_config->osp;
283     uart6850_base = hw_config->io_base;
284     uart6850_irq = hw_config->irq;
285
286     if (snd_set_irq_handler(uart6850_irq, m6850intr, uart6850_osp) < 0)
287         return 0;
288
289     ok = reset_uart6850();
290
291     uart6850_detected = ok;
292     return ok;
293 }
294
295 #endif
296 #endif