Import 2.1.77
[davej-history.git] / drivers / sound / uart6850.c
blobc3e75857bb19781e310930665d513b08dac50bd2
1 /*
2 * sound/uart6850.c
3 */
4 /*
5 * Copyright (C) by Hannu Savolainen 1993-1997
7 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
8 * Version 2 (June 1991). See the "COPYING" file distributed with this software
9 * for more info.
10 * Extended by Alan Cox for Red Hat Software. Now a loadable MIDI driver.
11 * 28/4/97 - (C) Copyright Alan Cox. Released under the GPL version 2.
14 #include <linux/config.h>
15 #include <linux/module.h>
17 /* Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl:
18 * added 6850 support, used with COVOX SoundMaster II and custom cards.
21 #include "sound_config.h"
22 #include "soundmodule.h"
23 #if defined(CONFIG_UART6850) && defined(CONFIG_MIDI) || defined(MODULE)
25 static int uart6850_base = 0x330;
27 static int *uart6850_osp;
29 #define DATAPORT (uart6850_base)
30 #define COMDPORT (uart6850_base+1)
31 #define STATPORT (uart6850_base+1)
33 static int
34 uart6850_status(void)
36 return inb(STATPORT);
38 #define input_avail() (uart6850_status()&INPUT_AVAIL)
39 #define output_ready() (uart6850_status()&OUTPUT_READY)
40 static void
41 uart6850_cmd(unsigned char cmd)
43 outb((cmd), COMDPORT);
45 static int
46 uart6850_read(void)
48 return inb(DATAPORT);
50 static void
51 uart6850_write(unsigned char byte)
53 outb((byte), DATAPORT);
56 #define OUTPUT_READY 0x02 /* Mask for data ready Bit */
57 #define INPUT_AVAIL 0x01 /* Mask for Data Send Ready Bit */
59 #define UART_RESET 0x95
60 #define UART_MODE_ON 0x03
62 static int uart6850_opened = 0;
63 static int uart6850_irq;
64 static int uart6850_detected = 0;
65 static int my_dev;
67 static int reset_uart6850(void);
68 static void (*midi_input_intr) (int dev, unsigned char data);
69 static void poll_uart6850(unsigned long dummy);
72 static struct timer_list uart6850_timer =
73 {NULL, NULL, 0, 0, poll_uart6850};
75 static void
76 uart6850_input_loop(void)
78 int count;
80 count = 10;
82 while (count) /*
83 * Not timed out
85 if (input_avail())
87 unsigned char c = uart6850_read();
89 count = 100;
91 if (uart6850_opened & OPEN_READ)
92 midi_input_intr(my_dev, c);
93 } else
94 while (!input_avail() && count)
95 count--;
98 void
99 m6850intr(int irq, void *dev_id, struct pt_regs *dummy)
101 if (input_avail())
102 uart6850_input_loop();
106 * It looks like there is no input interrupts in the UART mode. Let's try
107 * polling.
110 static void
111 poll_uart6850(unsigned long dummy)
113 unsigned long flags;
115 if (!(uart6850_opened & OPEN_READ))
116 return; /* Device has been closed */
118 save_flags(flags);
119 cli();
121 if (input_avail())
122 uart6850_input_loop();
126 uart6850_timer.expires = (1) + jiffies;
127 add_timer(&uart6850_timer);
128 }; /*
129 * Come back later
132 restore_flags(flags);
135 static int
136 uart6850_open(int dev, int mode,
137 void (*input) (int dev, unsigned char data),
138 void (*output) (int dev)
141 if (uart6850_opened)
143 printk("Midi6850: Midi busy\n");
144 return -EBUSY;
147 MOD_INC_USE_COUNT;
148 uart6850_cmd(UART_RESET);
150 uart6850_input_loop();
152 midi_input_intr = input;
153 uart6850_opened = mode;
154 poll_uart6850(0); /*
155 * Enable input polling
158 return 0;
161 static void
162 uart6850_close(int dev)
164 uart6850_cmd(UART_MODE_ON);
166 del_timer(&uart6850_timer);;
167 uart6850_opened = 0;
169 MOD_DEC_USE_COUNT;
172 static int
173 uart6850_out(int dev, unsigned char midi_byte)
175 int timeout;
176 unsigned long flags;
179 * Test for input since pending input seems to block the output.
182 save_flags(flags);
183 cli();
185 if (input_avail())
186 uart6850_input_loop();
188 restore_flags(flags);
191 * Sometimes it takes about 13000 loops before the output becomes ready
192 * (After reset). Normally it takes just about 10 loops.
195 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
196 * Wait
199 if (!output_ready())
201 printk("Midi6850: Timeout\n");
202 return 0;
204 uart6850_write(midi_byte);
205 return 1;
208 static int
209 uart6850_command(int dev, unsigned char *midi_byte)
211 return 1;
214 static int
215 uart6850_start_read(int dev)
217 return 0;
220 static int
221 uart6850_end_read(int dev)
223 return 0;
226 static void
227 uart6850_kick(int dev)
231 static int
232 uart6850_buffer_status(int dev)
234 return 0; /*
235 * No data in buffers
239 #define MIDI_SYNTH_NAME "6850 UART Midi"
240 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
241 #include "midi_synth.h"
243 static struct midi_operations uart6850_operations =
245 {"6850 UART", 0, 0, SNDCARD_UART6850},
246 &std_midi_synth,
247 {0},
248 uart6850_open,
249 uart6850_close,
250 NULL, /* ioctl */
251 uart6850_out,
252 uart6850_start_read,
253 uart6850_end_read,
254 uart6850_kick,
255 uart6850_command,
256 uart6850_buffer_status
260 void
261 attach_uart6850(struct address_info *hw_config)
263 int ok, timeout;
264 unsigned long flags;
266 if ((my_dev = sound_alloc_mididev()) == -1)
268 printk(KERN_INFO "uart6850: Too many midi devices detected\n");
269 return;
271 uart6850_base = hw_config->io_base;
272 uart6850_osp = hw_config->osp;
273 uart6850_irq = hw_config->irq;
275 if (!uart6850_detected)
277 sound_unload_mididev(my_dev);
278 return;
280 save_flags(flags);
281 cli();
283 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
284 * Wait
286 uart6850_cmd(UART_MODE_ON);
288 ok = 1;
290 restore_flags(flags);
292 conf_printf("6850 Midi Interface", hw_config);
294 std_midi_synth.midi_dev = my_dev;
295 hw_config->slots[4] = my_dev;
296 midi_devs[my_dev] = &uart6850_operations;
297 sequencer_init();
300 static int
301 reset_uart6850(void)
303 uart6850_read();
304 return 1; /*
305 * OK
311 probe_uart6850(struct address_info *hw_config)
313 int ok = 0;
315 uart6850_osp = hw_config->osp;
316 uart6850_base = hw_config->io_base;
317 uart6850_irq = hw_config->irq;
319 if (snd_set_irq_handler(uart6850_irq, m6850intr, "MIDI6850", uart6850_osp) < 0)
320 return 0;
322 ok = reset_uart6850();
324 uart6850_detected = ok;
325 return ok;
328 void
329 unload_uart6850(struct address_info *hw_config)
331 snd_release_irq(hw_config->irq);
332 sound_unload_mididev(hw_config->slots[4]);
336 #ifdef MODULE
338 int io = -1;
339 int irq = -1;
341 struct address_info cfg;
343 int
344 init_module(void)
346 if (io == -1 || irq == -1)
348 printk("uart6850: irq and io must be set.\n");
349 return -EINVAL;
351 cfg.io_base = io;
352 cfg.irq = irq;
354 if (probe_uart6850(&cfg))
355 return -ENODEV;
357 SOUND_LOCK;
358 return 0;
361 void
362 cleanup_module(void)
364 unload_uart6850(&cfg);
365 SOUND_LOCK_END;
367 #endif
368 #endif