Import 2.3.18pre1
[davej-history.git] / drivers / sound / uart6850.c
blob13dcb5fc22362a846ae630bcfe9d2a31d4a4aace
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.
13 * Alan Cox: Updated for new modular code. Removed snd_* irq handling. Now
14 * uses native linux resources
16 * Status: Testing required
19 #include <linux/config.h>
20 #include <linux/module.h>
22 /* Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl:
23 * added 6850 support, used with COVOX SoundMaster II and custom cards.
26 #include "sound_config.h"
28 #ifdef CONFIG_SOUND_UART6850
29 #ifdef CONFIG_MIDI
30 #include "soundmodule.h"
32 static int uart6850_base = 0x330;
34 static int *uart6850_osp;
36 #define DATAPORT (uart6850_base)
37 #define COMDPORT (uart6850_base+1)
38 #define STATPORT (uart6850_base+1)
40 static int uart6850_status(void)
42 return inb(STATPORT);
45 #define input_avail() (uart6850_status()&INPUT_AVAIL)
46 #define output_ready() (uart6850_status()&OUTPUT_READY)
48 static void uart6850_cmd(unsigned char cmd)
50 outb(cmd, COMDPORT);
53 static int uart6850_read(void)
55 return inb(DATAPORT);
58 static void uart6850_write(unsigned char byte)
60 outb(byte, DATAPORT);
63 #define OUTPUT_READY 0x02 /* Mask for data ready Bit */
64 #define INPUT_AVAIL 0x01 /* Mask for Data Send Ready Bit */
66 #define UART_RESET 0x95
67 #define UART_MODE_ON 0x03
69 static int uart6850_opened = 0;
70 static int uart6850_irq;
71 static int uart6850_detected = 0;
72 static int my_dev;
74 static int reset_uart6850(void);
75 static void (*midi_input_intr) (int dev, unsigned char data);
76 static void poll_uart6850(unsigned long dummy);
79 static struct timer_list uart6850_timer = {
80 NULL, NULL, 0, 0, poll_uart6850
83 static void uart6850_input_loop(void)
85 int count = 10;
87 while (count)
90 * Not timed out
92 if (input_avail())
94 unsigned char c = uart6850_read();
95 count = 100;
96 if (uart6850_opened & OPEN_READ)
97 midi_input_intr(my_dev, c);
99 else
101 while (!input_avail() && count)
102 count--;
107 void m6850intr(int irq, void *dev_id, struct pt_regs *dummy)
109 if (input_avail())
110 uart6850_input_loop();
114 * It looks like there is no input interrupts in the UART mode. Let's try
115 * polling.
118 static void poll_uart6850(unsigned long dummy)
120 unsigned long flags;
122 if (!(uart6850_opened & OPEN_READ))
123 return; /* Device has been closed */
125 save_flags(flags);
126 cli();
128 if (input_avail())
129 uart6850_input_loop();
131 uart6850_timer.expires = 1 + jiffies;
132 add_timer(&uart6850_timer);
135 * Come back later
138 restore_flags(flags);
141 static int uart6850_open(int dev, int mode,
142 void (*input) (int dev, unsigned char data),
143 void (*output) (int dev)
146 if (uart6850_opened)
148 /* printk("Midi6850: Midi busy\n");*/
149 return -EBUSY;
152 MOD_INC_USE_COUNT;
153 uart6850_cmd(UART_RESET);
154 uart6850_input_loop();
155 midi_input_intr = input;
156 uart6850_opened = mode;
157 poll_uart6850(0); /*
158 * Enable input polling
161 return 0;
164 static void uart6850_close(int dev)
166 uart6850_cmd(UART_MODE_ON);
167 del_timer(&uart6850_timer);
168 uart6850_opened = 0;
169 MOD_DEC_USE_COUNT;
172 static int uart6850_out(int dev, unsigned char midi_byte)
174 int timeout;
175 unsigned long flags;
178 * Test for input since pending input seems to block the output.
181 save_flags(flags);
182 cli();
184 if (input_avail())
185 uart6850_input_loop();
187 restore_flags(flags);
190 * Sometimes it takes about 13000 loops before the output becomes ready
191 * (After reset). Normally it takes just about 10 loops.
194 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
195 * Wait
197 if (!output_ready())
199 printk(KERN_WARNING "Midi6850: Timeout\n");
200 return 0;
202 uart6850_write(midi_byte);
203 return 1;
206 static int uart6850_command(int dev, unsigned char *midi_byte)
208 return 1;
211 static int uart6850_start_read(int dev)
213 return 0;
216 static int uart6850_end_read(int dev)
218 return 0;
221 static void uart6850_kick(int dev)
225 static int uart6850_buffer_status(int dev)
227 return 0; /*
228 * No data in buffers
232 #define MIDI_SYNTH_NAME "6850 UART Midi"
233 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
234 #include "midi_synth.h"
236 static struct midi_operations uart6850_operations =
238 {"6850 UART", 0, 0, SNDCARD_UART6850},
239 &std_midi_synth,
240 {0},
241 uart6850_open,
242 uart6850_close,
243 NULL, /* ioctl */
244 uart6850_out,
245 uart6850_start_read,
246 uart6850_end_read,
247 uart6850_kick,
248 uart6850_command,
249 uart6850_buffer_status
253 void attach_uart6850(struct address_info *hw_config)
255 int ok, timeout;
256 unsigned long flags;
258 if ((my_dev = sound_alloc_mididev()) == -1)
260 printk(KERN_INFO "uart6850: Too many midi devices detected\n");
261 return;
263 uart6850_base = hw_config->io_base;
264 uart6850_osp = hw_config->osp;
265 uart6850_irq = hw_config->irq;
267 if (!uart6850_detected)
269 sound_unload_mididev(my_dev);
270 return;
272 save_flags(flags);
273 cli();
275 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
276 * Wait
278 uart6850_cmd(UART_MODE_ON);
279 ok = 1;
280 restore_flags(flags);
282 conf_printf("6850 Midi Interface", hw_config);
284 std_midi_synth.midi_dev = my_dev;
285 hw_config->slots[4] = my_dev;
286 midi_devs[my_dev] = &uart6850_operations;
287 sequencer_init();
290 static int reset_uart6850(void)
292 uart6850_read();
293 return 1; /*
294 * OK
299 int probe_uart6850(struct address_info *hw_config)
301 int ok = 0;
303 uart6850_osp = hw_config->osp;
304 uart6850_base = hw_config->io_base;
305 uart6850_irq = hw_config->irq;
307 if (request_irq(uart6850_irq, m6850intr, 0, "MIDI6850", NULL) < 0)
308 return 0;
310 ok = reset_uart6850();
311 uart6850_detected = ok;
312 return ok;
315 void unload_uart6850(struct address_info *hw_config)
317 free_irq(hw_config->irq, NULL);
318 sound_unload_mididev(hw_config->slots[4]);
322 #ifdef MODULE
324 int io = -1;
325 int irq = -1;
327 MODULE_PARM(io,"i");
328 MODULE_PARM(irq,"i");
330 EXPORT_NO_SYMBOLS;
332 struct address_info cfg;
334 int init_module(void)
336 if (io == -1 || irq == -1)
338 printk(KERN_INFO "uart6850: irq and io must be set.\n");
339 return -EINVAL;
341 cfg.io_base = io;
342 cfg.irq = irq;
344 if (probe_uart6850(&cfg))
345 return -ENODEV;
347 SOUND_LOCK;
348 return 0;
351 void cleanup_module(void)
353 unload_uart6850(&cfg);
354 SOUND_LOCK_END;
356 #endif
357 #endif
358 #endif