Import 2.3.50
[davej-history.git] / drivers / sound / uart6850.c
blob6540df0f6bc468467ba05873e323efb57e0ea87e
1 /*
2 * sound/uart6850.c
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
15 * Christoph Hellwig: Adapted to module_init/module_exit
17 * Status: Testing required
21 #include <linux/init.h>
22 #include <linux/module.h>
24 /* Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl:
25 * added 6850 support, used with COVOX SoundMaster II and custom cards.
28 #include "sound_config.h"
29 #include "soundmodule.h"
31 static int uart6850_base = 0x330;
33 static int *uart6850_osp;
35 #define DATAPORT (uart6850_base)
36 #define COMDPORT (uart6850_base+1)
37 #define STATPORT (uart6850_base+1)
39 static int uart6850_status(void)
41 return inb(STATPORT);
44 #define input_avail() (uart6850_status()&INPUT_AVAIL)
45 #define output_ready() (uart6850_status()&OUTPUT_READY)
47 static void uart6850_cmd(unsigned char cmd)
49 outb(cmd, COMDPORT);
52 static int uart6850_read(void)
54 return inb(DATAPORT);
57 static void uart6850_write(unsigned char byte)
59 outb(byte, DATAPORT);
62 #define OUTPUT_READY 0x02 /* Mask for data ready Bit */
63 #define INPUT_AVAIL 0x01 /* Mask for Data Send Ready Bit */
65 #define UART_RESET 0x95
66 #define UART_MODE_ON 0x03
68 static int uart6850_opened = 0;
69 static int uart6850_irq;
70 static int uart6850_detected = 0;
71 static int my_dev;
73 static int reset_uart6850(void);
74 static void (*midi_input_intr) (int dev, unsigned char data);
75 static void poll_uart6850(unsigned long dummy);
78 static struct timer_list uart6850_timer = {
79 NULL, NULL, 0, 0, poll_uart6850
82 static void uart6850_input_loop(void)
84 int count = 10;
86 while (count)
89 * Not timed out
91 if (input_avail())
93 unsigned char c = uart6850_read();
94 count = 100;
95 if (uart6850_opened & OPEN_READ)
96 midi_input_intr(my_dev, c);
98 else
100 while (!input_avail() && count)
101 count--;
106 void m6850intr(int irq, void *dev_id, struct pt_regs *dummy)
108 if (input_avail())
109 uart6850_input_loop();
113 * It looks like there is no input interrupts in the UART mode. Let's try
114 * polling.
117 static void poll_uart6850(unsigned long dummy)
119 unsigned long flags;
121 if (!(uart6850_opened & OPEN_READ))
122 return; /* Device has been closed */
124 save_flags(flags);
125 cli();
127 if (input_avail())
128 uart6850_input_loop();
130 uart6850_timer.expires = 1 + jiffies;
131 add_timer(&uart6850_timer);
134 * Come back later
137 restore_flags(flags);
140 static int uart6850_open(int dev, int mode,
141 void (*input) (int dev, unsigned char data),
142 void (*output) (int dev)
145 if (uart6850_opened)
147 /* printk("Midi6850: Midi busy\n");*/
148 return -EBUSY;
151 MOD_INC_USE_COUNT;
152 uart6850_cmd(UART_RESET);
153 uart6850_input_loop();
154 midi_input_intr = input;
155 uart6850_opened = mode;
156 poll_uart6850(0); /*
157 * Enable input polling
160 return 0;
163 static void uart6850_close(int dev)
165 uart6850_cmd(UART_MODE_ON);
166 del_timer(&uart6850_timer);
167 uart6850_opened = 0;
168 MOD_DEC_USE_COUNT;
171 static int uart6850_out(int dev, unsigned char midi_byte)
173 int timeout;
174 unsigned long flags;
177 * Test for input since pending input seems to block the output.
180 save_flags(flags);
181 cli();
183 if (input_avail())
184 uart6850_input_loop();
186 restore_flags(flags);
189 * Sometimes it takes about 13000 loops before the output becomes ready
190 * (After reset). Normally it takes just about 10 loops.
193 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
194 * Wait
196 if (!output_ready())
198 printk(KERN_WARNING "Midi6850: Timeout\n");
199 return 0;
201 uart6850_write(midi_byte);
202 return 1;
205 static inline int uart6850_command(int dev, unsigned char *midi_byte)
207 return 1;
210 static inline int uart6850_start_read(int dev)
212 return 0;
215 static inline int uart6850_end_read(int dev)
217 return 0;
220 static inline void uart6850_kick(int dev)
224 static inline int uart6850_buffer_status(int dev)
226 return 0; /*
227 * No data in buffers
231 #define MIDI_SYNTH_NAME "6850 UART Midi"
232 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
233 #include "midi_synth.h"
235 static struct midi_operations uart6850_operations =
237 {"6850 UART", 0, 0, SNDCARD_UART6850},
238 &std_midi_synth,
239 {0},
240 uart6850_open,
241 uart6850_close,
242 NULL, /* ioctl */
243 uart6850_out,
244 uart6850_start_read,
245 uart6850_end_read,
246 uart6850_kick,
247 uart6850_command,
248 uart6850_buffer_status
252 static void __init attach_uart6850(struct address_info *hw_config)
254 int ok, timeout;
255 unsigned long flags;
257 if ((my_dev = sound_alloc_mididev()) == -1)
259 printk(KERN_INFO "uart6850: Too many midi devices detected\n");
260 return;
262 uart6850_base = hw_config->io_base;
263 uart6850_osp = hw_config->osp;
264 uart6850_irq = hw_config->irq;
266 if (!uart6850_detected)
268 sound_unload_mididev(my_dev);
269 return;
271 save_flags(flags);
272 cli();
274 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /*
275 * Wait
277 uart6850_cmd(UART_MODE_ON);
278 ok = 1;
279 restore_flags(flags);
281 conf_printf("6850 Midi Interface", hw_config);
283 std_midi_synth.midi_dev = my_dev;
284 hw_config->slots[4] = my_dev;
285 midi_devs[my_dev] = &uart6850_operations;
286 sequencer_init();
289 static int reset_uart6850(void)
291 uart6850_read();
292 return 1; /*
293 * OK
298 static int __init probe_uart6850(struct address_info *hw_config)
300 int ok = 0;
302 uart6850_osp = hw_config->osp;
303 uart6850_base = hw_config->io_base;
304 uart6850_irq = hw_config->irq;
306 if (request_irq(uart6850_irq, m6850intr, 0, "MIDI6850", NULL) < 0)
307 return 0;
309 ok = reset_uart6850();
310 uart6850_detected = ok;
311 return ok;
314 static void __exit unload_uart6850(struct address_info *hw_config)
316 free_irq(hw_config->irq, NULL);
317 sound_unload_mididev(hw_config->slots[4]);
320 static struct address_info cfg_mpu;
322 static int __initdata io = -1;
323 static int __initdata irq = -1;
325 MODULE_PARM(io,"i");
326 MODULE_PARM(irq,"i");
328 static int __init init_uart6850(void)
330 cfg_mpu.io_base = io;
331 cfg_mpu.irq = irq;
333 if (cfg_mpu.io_base == -1 || cfg_mpu.irq == -1) {
334 printk(KERN_INFO "uart6850: irq and io must be set.\n");
335 return -EINVAL;
338 if (probe_uart6850(&cfg_mpu))
339 return -ENODEV;
341 SOUND_LOCK;
342 return 0;
345 static void __exit cleanup_uart6850(void)
347 unload_uart6850(&cfg_mpu);
348 SOUND_LOCK_END;
351 module_init(init_uart6850);
352 module_exit(cleanup_uart6850);
354 #ifndef MODULE
355 static int __init setup_uart6850(char *str)
357 /* io, irq */
358 int ints[3];
360 str = get_options(str, ARRAY_SIZE(ints), ints);
362 io = ints[1];
363 irq = ints[2];
365 return 1;
367 __setup("uart6850=", setup_uart6850);
368 #endif