Merge with 2.4.0-test3-pre4.
[linux-2.6/linux-mips.git] / drivers / sound / uart401.c
blob71721dd3e74d0358e560f9673beb0dd22e9e61a1
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"
27 #include "soundmodule.h"
29 #include "mpu401.h"
31 typedef struct uart401_devc
33 int base;
34 int irq;
35 int *osp;
36 void (*midi_input_intr) (int dev, unsigned char data);
37 int opened, disabled;
38 volatile unsigned char input_byte;
39 int my_dev;
40 int share_irq;
42 uart401_devc;
44 static uart401_devc *detected_devc = NULL;
46 #define DATAPORT (devc->base)
47 #define COMDPORT (devc->base+1)
48 #define STATPORT (devc->base+1)
50 static int uart401_status(uart401_devc * devc)
52 return inb(STATPORT);
55 #define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
56 #define output_ready(devc) (!(uart401_status(devc)&OUTPUT_READY))
58 static void uart401_cmd(uart401_devc * devc, unsigned char cmd)
60 outb((cmd), COMDPORT);
63 static int uart401_read(uart401_devc * devc)
65 return inb(DATAPORT);
68 static void uart401_write(uart401_devc * devc, unsigned char byte)
70 outb((byte), DATAPORT);
73 #define OUTPUT_READY 0x40
74 #define INPUT_AVAIL 0x80
75 #define MPU_ACK 0xFE
76 #define MPU_RESET 0xFF
77 #define UART_MODE_ON 0x3F
79 static int reset_uart401(uart401_devc * devc);
80 static void enter_uart_mode(uart401_devc * devc);
82 static void uart401_input_loop(uart401_devc * devc)
84 int work_limit=30000;
86 while (input_avail(devc) && --work_limit)
88 unsigned char c = uart401_read(devc);
90 if (c == MPU_ACK)
91 devc->input_byte = c;
92 else if (devc->opened & OPEN_READ && devc->midi_input_intr)
93 devc->midi_input_intr(devc->my_dev, c);
95 if(work_limit==0)
96 printk(KERN_WARNING "Too much work in interrupt on uart401 (0x%X). UART jabbering ??\n", devc->base);
99 void uart401intr(int irq, void *dev_id, struct pt_regs *dummy)
101 uart401_devc *devc = dev_id;
103 if (devc == NULL)
105 printk(KERN_ERR "uart401: bad devc\n");
106 return;
109 if (input_avail(devc))
110 uart401_input_loop(devc);
113 static int
114 uart401_open(int dev, int mode,
115 void (*input) (int dev, unsigned char data),
116 void (*output) (int dev)
119 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
121 if (devc->opened)
122 return -EBUSY;
124 /* Flush the UART */
126 while (input_avail(devc))
127 uart401_read(devc);
129 devc->midi_input_intr = input;
130 devc->opened = mode;
131 enter_uart_mode(devc);
132 devc->disabled = 0;
134 return 0;
137 static void uart401_close(int dev)
139 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
141 reset_uart401(devc);
142 devc->opened = 0;
145 static int uart401_out(int dev, unsigned char midi_byte)
147 int timeout;
148 unsigned long flags;
149 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
151 if (devc->disabled)
152 return 1;
154 * Test for input since pending input seems to block the output.
157 save_flags(flags);
158 cli();
160 if (input_avail(devc))
161 uart401_input_loop(devc);
163 restore_flags(flags);
166 * Sometimes it takes about 13000 loops before the output becomes ready
167 * (After reset). Normally it takes just about 10 loops.
170 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
172 if (!output_ready(devc))
174 printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
175 devc->disabled = 1;
176 reset_uart401(devc);
177 enter_uart_mode(devc);
178 return 1;
180 uart401_write(devc, midi_byte);
181 return 1;
184 static inline int uart401_start_read(int dev)
186 return 0;
189 static inline int uart401_end_read(int dev)
191 return 0;
194 static inline void uart401_kick(int dev)
198 static inline int uart401_buffer_status(int dev)
200 return 0;
203 #define MIDI_SYNTH_NAME "MPU-401 UART"
204 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
205 #include "midi_synth.h"
207 static struct midi_operations uart401_operations =
210 "MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401
212 &std_midi_synth,
213 {0},
214 uart401_open,
215 uart401_close,
216 NULL, /* ioctl */
217 uart401_out,
218 uart401_start_read,
219 uart401_end_read,
220 uart401_kick,
221 NULL,
222 uart401_buffer_status,
223 NULL
226 static void enter_uart_mode(uart401_devc * devc)
228 int ok, timeout;
229 unsigned long flags;
231 save_flags(flags);
232 cli();
233 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
235 devc->input_byte = 0;
236 uart401_cmd(devc, UART_MODE_ON);
238 ok = 0;
239 for (timeout = 50000; timeout > 0 && !ok; timeout--)
240 if (devc->input_byte == MPU_ACK)
241 ok = 1;
242 else if (input_avail(devc))
243 if (uart401_read(devc) == MPU_ACK)
244 ok = 1;
246 restore_flags(flags);
249 void attach_uart401(struct address_info *hw_config)
251 uart401_devc *devc;
252 char *name = "MPU-401 (UART) MIDI";
255 if (hw_config->name)
256 name = hw_config->name;
258 if (detected_devc == NULL)
259 return;
262 devc = (uart401_devc *) kmalloc(sizeof(uart401_devc), GFP_KERNEL);
263 if (devc == NULL)
265 printk(KERN_WARNING "uart401: Can't allocate memory\n");
266 return;
268 memcpy((char *) devc, (char *) detected_devc, sizeof(uart401_devc));
269 detected_devc = NULL;
271 devc->irq = hw_config->irq;
272 if (devc->irq < 0)
274 devc->share_irq = 1;
275 devc->irq *= -1;
277 else
278 devc->share_irq = 0;
280 if (!devc->share_irq)
282 if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0)
284 printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
285 devc->share_irq = 1;
288 devc->my_dev = sound_alloc_mididev();
290 request_region(hw_config->io_base, 4, "MPU-401 UART");
291 enter_uart_mode(devc);
293 if (devc->my_dev == -1)
295 printk(KERN_INFO "uart401: Too many midi devices detected\n");
296 kfree(devc);
297 return;
299 conf_printf(name, hw_config);
301 std_midi_synth.midi_dev = devc->my_dev;
302 midi_devs[devc->my_dev] = (struct midi_operations *)kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
303 if (midi_devs[devc->my_dev] == NULL)
305 printk(KERN_ERR "uart401: Failed to allocate memory\n");
306 sound_unload_mididev(devc->my_dev);
307 kfree(devc);
308 devc=NULL;
309 return;
311 memcpy((char *) midi_devs[devc->my_dev], (char *) &uart401_operations,
312 sizeof(struct midi_operations));
314 midi_devs[devc->my_dev]->devc = devc;
315 midi_devs[devc->my_dev]->converter = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
316 if (midi_devs[devc->my_dev]->converter == NULL)
318 printk(KERN_WARNING "uart401: Failed to allocate memory\n");
319 kfree(midi_devs[devc->my_dev]);
320 kfree(devc);
321 sound_unload_mididev(devc->my_dev);
322 devc=NULL;
323 return;
325 memcpy((char *) midi_devs[devc->my_dev]->converter, (char *) &std_midi_synth,
326 sizeof(struct synth_operations));
328 strcpy(midi_devs[devc->my_dev]->info.name, name);
329 midi_devs[devc->my_dev]->converter->id = "UART401";
330 hw_config->slots[4] = devc->my_dev;
331 sequencer_init();
332 devc->opened = 0;
335 static int reset_uart401(uart401_devc * devc)
337 int ok, timeout, n;
340 * Send the RESET command. Try again if no success at the first time.
343 ok = 0;
345 for (n = 0; n < 2 && !ok; n++)
347 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
348 devc->input_byte = 0;
349 uart401_cmd(devc, MPU_RESET);
352 * Wait at least 25 msec. This method is not accurate so let's make the
353 * loop bit longer. Cannot sleep since this is called during boot.
356 for (timeout = 50000; timeout > 0 && !ok; timeout--)
358 if (devc->input_byte == MPU_ACK) /* Interrupt */
359 ok = 1;
360 else if (input_avail(devc))
362 if (uart401_read(devc) == MPU_ACK)
363 ok = 1;
369 if (ok)
371 DEB(printk("Reset UART401 OK\n"));
373 else
374 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
376 if (ok)
377 uart401_input_loop(devc); /*
378 * Flush input before enabling interrupts
381 return ok;
384 int probe_uart401(struct address_info *hw_config)
386 int ok = 0;
387 unsigned long flags;
388 static uart401_devc hw_info;
389 uart401_devc *devc = &hw_info;
391 DDB(printk("Entered probe_uart401()\n"));
393 /* Default to "not found" */
394 hw_config->slots[4] = -1;
395 detected_devc = NULL;
397 if (check_region(hw_config->io_base, 4))
398 return 0;
400 devc->base = hw_config->io_base;
401 devc->irq = hw_config->irq;
402 devc->osp = hw_config->osp;
403 devc->midi_input_intr = NULL;
404 devc->opened = 0;
405 devc->input_byte = 0;
406 devc->my_dev = 0;
407 devc->share_irq = 0;
409 save_flags(flags);
410 cli();
411 ok = reset_uart401(devc);
412 restore_flags(flags);
414 if (ok)
415 detected_devc = devc;
417 return ok;
420 void unload_uart401(struct address_info *hw_config)
422 uart401_devc *devc;
423 int n=hw_config->slots[4];
425 /* Not set up */
426 if(n==-1 || midi_devs[n]==NULL)
427 return;
429 /* Not allocated (erm ??) */
431 devc = midi_devs[hw_config->slots[4]]->devc;
432 if (devc == NULL)
433 return;
435 reset_uart401(devc);
436 release_region(hw_config->io_base, 4);
438 if (!devc->share_irq)
439 free_irq(devc->irq, devc);
440 if (devc)
442 kfree(midi_devs[devc->my_dev]->converter);
443 kfree(midi_devs[devc->my_dev]);
444 kfree(devc);
445 devc = NULL;
447 /* This kills midi_devs[x] */
448 sound_unload_mididev(hw_config->slots[4]);
451 EXPORT_SYMBOL(attach_uart401);
452 EXPORT_SYMBOL(probe_uart401);
453 EXPORT_SYMBOL(unload_uart401);
454 EXPORT_SYMBOL(uart401intr);
456 static struct address_info cfg_mpu;
458 static int __initdata io = -1;
459 static int __initdata irq = -1;
461 MODULE_PARM(io, "i");
462 MODULE_PARM(irq, "i");
465 static int __init init_uart401(void)
467 cfg_mpu.irq = irq;
468 cfg_mpu.io_base = io;
470 /* Can be loaded either for module use or to provide functions
471 to others */
472 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
473 printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
474 if (probe_uart401(&cfg_mpu) == 0)
475 return -ENODEV;
476 attach_uart401(&cfg_mpu);
478 SOUND_LOCK;
479 return 0;
482 static void __exit cleanup_uart401(void)
484 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
485 unload_uart401(&cfg_mpu);
486 SOUND_LOCK_END;
489 module_init(init_uart401);
490 module_exit(cleanup_uart401);
492 #ifndef MODULE
493 static int __init setup_uart401(char *str)
495 /* io, irq */
496 int ints[3];
498 str = get_options(str, ARRAY_SIZE(ints), ints);
500 io = ints[1];
501 irq = ints[2];
503 return 1;
506 __setup("uart401=", setup_uart401);
507 #endif