- pre4:
[davej-history.git] / drivers / sound / uart401.c
blob0b7eb3297a0a787d8d1c8c4c2bef94bc9a4d06e7
1 /*
2 * sound/uart401.c
4 * MPU-401 UART driver (formerly uart401_midi.c)
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
13 * Changes:
14 * Alan Cox Reformatted, removed sound_mem usage, use normal Linux
15 * interrupt allocation. Protect against bogus unload
16 * Fixed to allow IRQ > 15
17 * Christoph Hellwig Adapted to module_init/module_exit
19 * Status:
20 * Untested
23 #include <linux/init.h>
24 #include <linux/module.h>
26 #include "sound_config.h"
28 #include "mpu401.h"
30 typedef struct uart401_devc
32 int base;
33 int irq;
34 int *osp;
35 void (*midi_input_intr) (int dev, unsigned char data);
36 int opened, disabled;
37 volatile unsigned char input_byte;
38 int my_dev;
39 int share_irq;
41 uart401_devc;
43 static uart401_devc *detected_devc = NULL;
45 #define DATAPORT (devc->base)
46 #define COMDPORT (devc->base+1)
47 #define STATPORT (devc->base+1)
49 static int uart401_status(uart401_devc * devc)
51 return inb(STATPORT);
54 #define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
55 #define output_ready(devc) (!(uart401_status(devc)&OUTPUT_READY))
57 static void uart401_cmd(uart401_devc * devc, unsigned char cmd)
59 outb((cmd), COMDPORT);
62 static int uart401_read(uart401_devc * devc)
64 return inb(DATAPORT);
67 static void uart401_write(uart401_devc * devc, unsigned char byte)
69 outb((byte), DATAPORT);
72 #define OUTPUT_READY 0x40
73 #define INPUT_AVAIL 0x80
74 #define MPU_ACK 0xFE
75 #define MPU_RESET 0xFF
76 #define UART_MODE_ON 0x3F
78 static int reset_uart401(uart401_devc * devc);
79 static void enter_uart_mode(uart401_devc * devc);
81 static void uart401_input_loop(uart401_devc * devc)
83 int work_limit=30000;
85 while (input_avail(devc) && --work_limit)
87 unsigned char c = uart401_read(devc);
89 if (c == MPU_ACK)
90 devc->input_byte = c;
91 else if (devc->opened & OPEN_READ && devc->midi_input_intr)
92 devc->midi_input_intr(devc->my_dev, c);
94 if(work_limit==0)
95 printk(KERN_WARNING "Too much work in interrupt on uart401 (0x%X). UART jabbering ??\n", devc->base);
98 void uart401intr(int irq, void *dev_id, struct pt_regs *dummy)
100 uart401_devc *devc = dev_id;
102 if (devc == NULL)
104 printk(KERN_ERR "uart401: bad devc\n");
105 return;
108 if (input_avail(devc))
109 uart401_input_loop(devc);
112 static int
113 uart401_open(int dev, int mode,
114 void (*input) (int dev, unsigned char data),
115 void (*output) (int dev)
118 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
120 if (devc->opened)
121 return -EBUSY;
123 /* Flush the UART */
125 while (input_avail(devc))
126 uart401_read(devc);
128 devc->midi_input_intr = input;
129 devc->opened = mode;
130 enter_uart_mode(devc);
131 devc->disabled = 0;
133 return 0;
136 static void uart401_close(int dev)
138 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
140 reset_uart401(devc);
141 devc->opened = 0;
144 static int uart401_out(int dev, unsigned char midi_byte)
146 int timeout;
147 unsigned long flags;
148 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
150 if (devc->disabled)
151 return 1;
153 * Test for input since pending input seems to block the output.
156 save_flags(flags);
157 cli();
159 if (input_avail(devc))
160 uart401_input_loop(devc);
162 restore_flags(flags);
165 * Sometimes it takes about 13000 loops before the output becomes ready
166 * (After reset). Normally it takes just about 10 loops.
169 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
171 if (!output_ready(devc))
173 printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
174 devc->disabled = 1;
175 reset_uart401(devc);
176 enter_uart_mode(devc);
177 return 1;
179 uart401_write(devc, midi_byte);
180 return 1;
183 static inline int uart401_start_read(int dev)
185 return 0;
188 static inline int uart401_end_read(int dev)
190 return 0;
193 static inline void uart401_kick(int dev)
197 static inline int uart401_buffer_status(int dev)
199 return 0;
202 #define MIDI_SYNTH_NAME "MPU-401 UART"
203 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
204 #include "midi_synth.h"
206 static struct midi_operations uart401_operations =
208 owner: THIS_MODULE,
209 info: {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
210 converter: &std_midi_synth,
211 in_info: {0},
212 open: uart401_open,
213 close: uart401_close,
214 outputc: uart401_out,
215 start_read: uart401_start_read,
216 end_read: uart401_end_read,
217 kick: uart401_kick,
218 buffer_status: uart401_buffer_status,
221 static void enter_uart_mode(uart401_devc * devc)
223 int ok, timeout;
224 unsigned long flags;
226 save_flags(flags);
227 cli();
228 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
230 devc->input_byte = 0;
231 uart401_cmd(devc, UART_MODE_ON);
233 ok = 0;
234 for (timeout = 50000; timeout > 0 && !ok; timeout--)
235 if (devc->input_byte == MPU_ACK)
236 ok = 1;
237 else if (input_avail(devc))
238 if (uart401_read(devc) == MPU_ACK)
239 ok = 1;
241 restore_flags(flags);
244 void attach_uart401(struct address_info *hw_config, struct module *owner)
246 uart401_devc *devc;
247 char *name = "MPU-401 (UART) MIDI";
250 if (hw_config->name)
251 name = hw_config->name;
253 if (detected_devc == NULL)
254 return;
257 devc = (uart401_devc *) kmalloc(sizeof(uart401_devc), GFP_KERNEL);
258 if (devc == NULL)
260 printk(KERN_WARNING "uart401: Can't allocate memory\n");
261 return;
263 memcpy((char *) devc, (char *) detected_devc, sizeof(uart401_devc));
264 detected_devc = NULL;
266 devc->irq = hw_config->irq;
267 if (devc->irq < 0)
269 devc->share_irq = 1;
270 devc->irq *= -1;
272 else
273 devc->share_irq = 0;
275 if (!devc->share_irq)
277 if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0)
279 printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
280 devc->share_irq = 1;
283 devc->my_dev = sound_alloc_mididev();
285 request_region(hw_config->io_base, 4, "MPU-401 UART");
286 enter_uart_mode(devc);
288 if (devc->my_dev == -1)
290 printk(KERN_INFO "uart401: Too many midi devices detected\n");
291 kfree(devc);
292 return;
294 conf_printf(name, hw_config);
296 std_midi_synth.midi_dev = devc->my_dev;
297 midi_devs[devc->my_dev] = (struct midi_operations *)kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
298 if (midi_devs[devc->my_dev] == NULL)
300 printk(KERN_ERR "uart401: Failed to allocate memory\n");
301 sound_unload_mididev(devc->my_dev);
302 kfree(devc);
303 devc=NULL;
304 return;
306 memcpy((char *) midi_devs[devc->my_dev], (char *) &uart401_operations,
307 sizeof(struct midi_operations));
309 if (owner)
310 midi_devs[devc->my_dev]->owner = owner;
312 midi_devs[devc->my_dev]->devc = devc;
313 midi_devs[devc->my_dev]->converter = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
314 if (midi_devs[devc->my_dev]->converter == NULL)
316 printk(KERN_WARNING "uart401: Failed to allocate memory\n");
317 kfree(midi_devs[devc->my_dev]);
318 kfree(devc);
319 sound_unload_mididev(devc->my_dev);
320 devc=NULL;
321 return;
323 memcpy((char *) midi_devs[devc->my_dev]->converter, (char *) &std_midi_synth,
324 sizeof(struct synth_operations));
326 strcpy(midi_devs[devc->my_dev]->info.name, name);
327 midi_devs[devc->my_dev]->converter->id = "UART401";
328 hw_config->slots[4] = devc->my_dev;
329 sequencer_init();
330 devc->opened = 0;
333 static int reset_uart401(uart401_devc * devc)
335 int ok, timeout, n;
338 * Send the RESET command. Try again if no success at the first time.
341 ok = 0;
343 for (n = 0; n < 2 && !ok; n++)
345 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
346 devc->input_byte = 0;
347 uart401_cmd(devc, MPU_RESET);
350 * Wait at least 25 msec. This method is not accurate so let's make the
351 * loop bit longer. Cannot sleep since this is called during boot.
354 for (timeout = 50000; timeout > 0 && !ok; timeout--)
356 if (devc->input_byte == MPU_ACK) /* Interrupt */
357 ok = 1;
358 else if (input_avail(devc))
360 if (uart401_read(devc) == MPU_ACK)
361 ok = 1;
367 if (ok)
369 DEB(printk("Reset UART401 OK\n"));
371 else
372 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
374 if (ok)
375 uart401_input_loop(devc); /*
376 * Flush input before enabling interrupts
379 return ok;
382 int probe_uart401(struct address_info *hw_config)
384 int ok = 0;
385 unsigned long flags;
386 static uart401_devc hw_info;
387 uart401_devc *devc = &hw_info;
389 DDB(printk("Entered probe_uart401()\n"));
391 /* Default to "not found" */
392 hw_config->slots[4] = -1;
393 detected_devc = NULL;
395 if (check_region(hw_config->io_base, 4))
396 return 0;
398 devc->base = hw_config->io_base;
399 devc->irq = hw_config->irq;
400 devc->osp = hw_config->osp;
401 devc->midi_input_intr = NULL;
402 devc->opened = 0;
403 devc->input_byte = 0;
404 devc->my_dev = 0;
405 devc->share_irq = 0;
407 save_flags(flags);
408 cli();
409 ok = reset_uart401(devc);
410 restore_flags(flags);
412 if (ok)
413 detected_devc = devc;
415 return ok;
418 void unload_uart401(struct address_info *hw_config)
420 uart401_devc *devc;
421 int n=hw_config->slots[4];
423 /* Not set up */
424 if(n==-1 || midi_devs[n]==NULL)
425 return;
427 /* Not allocated (erm ??) */
429 devc = midi_devs[hw_config->slots[4]]->devc;
430 if (devc == NULL)
431 return;
433 reset_uart401(devc);
434 release_region(hw_config->io_base, 4);
436 if (!devc->share_irq)
437 free_irq(devc->irq, devc);
438 if (devc)
440 kfree(midi_devs[devc->my_dev]->converter);
441 kfree(midi_devs[devc->my_dev]);
442 kfree(devc);
443 devc = NULL;
445 /* This kills midi_devs[x] */
446 sound_unload_mididev(hw_config->slots[4]);
449 EXPORT_SYMBOL(attach_uart401);
450 EXPORT_SYMBOL(probe_uart401);
451 EXPORT_SYMBOL(unload_uart401);
452 EXPORT_SYMBOL(uart401intr);
454 static struct address_info cfg_mpu;
456 static int __initdata io = -1;
457 static int __initdata irq = -1;
459 MODULE_PARM(io, "i");
460 MODULE_PARM(irq, "i");
463 static int __init init_uart401(void)
465 cfg_mpu.irq = irq;
466 cfg_mpu.io_base = io;
468 /* Can be loaded either for module use or to provide functions
469 to others */
470 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
471 printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
472 if (probe_uart401(&cfg_mpu) == 0)
473 return -ENODEV;
474 attach_uart401(&cfg_mpu, THIS_MODULE);
477 return 0;
480 static void __exit cleanup_uart401(void)
482 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
483 unload_uart401(&cfg_mpu);
486 module_init(init_uart401);
487 module_exit(cleanup_uart401);
489 #ifndef MODULE
490 static int __init setup_uart401(char *str)
492 /* io, irq */
493 int ints[3];
495 str = get_options(str, ARRAY_SIZE(ints), ints);
497 io = ints[1];
498 irq = ints[2];
500 return 1;
503 __setup("uart401=", setup_uart401);
504 #endif