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
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
23 #include <linux/init.h>
24 #include <linux/module.h>
26 #include "sound_config.h"
30 typedef struct uart401_devc
35 void (*midi_input_intr
) (int dev
, unsigned char data
);
37 volatile unsigned char input_byte
;
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
)
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
)
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
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
)
85 while (input_avail(devc
) && --work_limit
)
87 unsigned char c
= uart401_read(devc
);
91 else if (devc
->opened
& OPEN_READ
&& devc
->midi_input_intr
)
92 devc
->midi_input_intr(devc
->my_dev
, c
);
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
;
104 printk(KERN_ERR
"uart401: bad devc\n");
108 if (input_avail(devc
))
109 uart401_input_loop(devc
);
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
;
125 while (input_avail(devc
))
128 devc
->midi_input_intr
= input
;
130 enter_uart_mode(devc
);
136 static void uart401_close(int dev
)
138 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
144 static int uart401_out(int dev
, unsigned char midi_byte
)
148 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
153 * Test for input since pending input seems to block the output.
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");
176 enter_uart_mode(devc
);
179 uart401_write(devc
, midi_byte
);
183 static inline int uart401_start_read(int dev
)
188 static inline int uart401_end_read(int dev
)
193 static inline void uart401_kick(int dev
)
197 static inline int uart401_buffer_status(int dev
)
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
=
209 info
: {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401
},
210 converter
: &std_midi_synth
,
213 close
: uart401_close
,
214 outputc
: uart401_out
,
215 start_read
: uart401_start_read
,
216 end_read
: uart401_end_read
,
218 buffer_status
: uart401_buffer_status
,
221 static void enter_uart_mode(uart401_devc
* devc
)
228 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
230 devc
->input_byte
= 0;
231 uart401_cmd(devc
, UART_MODE_ON
);
234 for (timeout
= 50000; timeout
> 0 && !ok
; timeout
--)
235 if (devc
->input_byte
== MPU_ACK
)
237 else if (input_avail(devc
))
238 if (uart401_read(devc
) == MPU_ACK
)
241 restore_flags(flags
);
244 void attach_uart401(struct address_info
*hw_config
, struct module
*owner
)
247 char *name
= "MPU-401 (UART) MIDI";
251 name
= hw_config
->name
;
253 if (detected_devc
== NULL
)
257 devc
= (uart401_devc
*) kmalloc(sizeof(uart401_devc
), GFP_KERNEL
);
260 printk(KERN_WARNING
"uart401: Can't allocate memory\n");
263 memcpy((char *) devc
, (char *) detected_devc
, sizeof(uart401_devc
));
264 detected_devc
= NULL
;
266 devc
->irq
= hw_config
->irq
;
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
);
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");
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
);
306 memcpy((char *) midi_devs
[devc
->my_dev
], (char *) &uart401_operations
,
307 sizeof(struct midi_operations
));
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
]);
319 sound_unload_mididev(devc
->my_dev
);
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
;
333 static int reset_uart401(uart401_devc
* devc
)
338 * Send the RESET command. Try again if no success at the first time.
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 */
358 else if (input_avail(devc
))
360 if (uart401_read(devc
) == MPU_ACK
)
369 DEB(printk("Reset UART401 OK\n"));
372 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
375 uart401_input_loop(devc
); /*
376 * Flush input before enabling interrupts
382 int probe_uart401(struct address_info
*hw_config
)
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))
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
;
403 devc
->input_byte
= 0;
409 ok
= reset_uart401(devc
);
410 restore_flags(flags
);
413 detected_devc
= devc
;
418 void unload_uart401(struct address_info
*hw_config
)
421 int n
=hw_config
->slots
[4];
424 if(n
==-1 || midi_devs
[n
]==NULL
)
427 /* Not allocated (erm ??) */
429 devc
= midi_devs
[hw_config
->slots
[4]]->devc
;
434 release_region(hw_config
->io_base
, 4);
436 if (!devc
->share_irq
)
437 free_irq(devc
->irq
, devc
);
440 kfree(midi_devs
[devc
->my_dev
]->converter
);
441 kfree(midi_devs
[devc
->my_dev
]);
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)
466 cfg_mpu
.io_base
= io
;
468 /* Can be loaded either for module use or to provide functions
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)
474 attach_uart401(&cfg_mpu
, THIS_MODULE
);
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
);
490 static int __init
setup_uart401(char *str
)
495 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
503 __setup("uart401=", setup_uart401
);