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
18 * Arnaldo C. de Melo got rid of check_region
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include "sound_config.h"
33 typedef struct uart401_devc
38 void (*midi_input_intr
) (int dev
, unsigned char data
);
40 volatile unsigned char input_byte
;
47 #define DATAPORT (devc->base)
48 #define COMDPORT (devc->base+1)
49 #define STATPORT (devc->base+1)
51 static int uart401_status(uart401_devc
* devc
)
56 #define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
57 #define output_ready(devc) (!(uart401_status(devc)&OUTPUT_READY))
59 static void uart401_cmd(uart401_devc
* devc
, unsigned char cmd
)
61 outb((cmd
), COMDPORT
);
64 static int uart401_read(uart401_devc
* devc
)
69 static void uart401_write(uart401_devc
* devc
, unsigned char byte
)
71 outb((byte
), DATAPORT
);
74 #define OUTPUT_READY 0x40
75 #define INPUT_AVAIL 0x80
77 #define MPU_RESET 0xFF
78 #define UART_MODE_ON 0x3F
80 static int reset_uart401(uart401_devc
* devc
);
81 static void enter_uart_mode(uart401_devc
* devc
);
83 static void uart401_input_loop(uart401_devc
* devc
)
87 while (input_avail(devc
) && --work_limit
)
89 unsigned char c
= uart401_read(devc
);
93 else if (devc
->opened
& OPEN_READ
&& devc
->midi_input_intr
)
94 devc
->midi_input_intr(devc
->my_dev
, c
);
97 printk(KERN_WARNING
"Too much work in interrupt on uart401 (0x%X). UART jabbering ??\n", devc
->base
);
100 irqreturn_t
uart401intr(int irq
, void *dev_id
)
102 uart401_devc
*devc
= dev_id
;
106 printk(KERN_ERR
"uart401: bad devc\n");
110 if (input_avail(devc
))
111 uart401_input_loop(devc
);
116 uart401_open(int dev
, int mode
,
117 void (*input
) (int dev
, unsigned char data
),
118 void (*output
) (int dev
)
121 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
128 while (input_avail(devc
))
131 devc
->midi_input_intr
= input
;
133 enter_uart_mode(devc
);
139 static void uart401_close(int dev
)
141 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
147 static int uart401_out(int dev
, unsigned char midi_byte
)
151 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
156 * Test for input since pending input seems to block the output.
159 spin_lock_irqsave(&devc
->lock
,flags
);
160 if (input_avail(devc
))
161 uart401_input_loop(devc
);
163 spin_unlock_irqrestore(&devc
->lock
,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");
177 enter_uart_mode(devc
);
180 uart401_write(devc
, midi_byte
);
184 static inline int uart401_start_read(int dev
)
189 static inline int uart401_end_read(int dev
)
194 static inline void uart401_kick(int dev
)
198 static inline int uart401_buffer_status(int dev
)
203 #define MIDI_SYNTH_NAME "MPU-401 UART"
204 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
205 #include "midi_synth.h"
207 static const struct midi_operations uart401_operations
=
209 .owner
= THIS_MODULE
,
210 .info
= {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401
},
211 .converter
= &std_midi_synth
,
213 .open
= uart401_open
,
214 .close
= uart401_close
,
215 .outputc
= uart401_out
,
216 .start_read
= uart401_start_read
,
217 .end_read
= uart401_end_read
,
218 .kick
= uart401_kick
,
219 .buffer_status
= uart401_buffer_status
,
222 static void enter_uart_mode(uart401_devc
* devc
)
227 spin_lock_irqsave(&devc
->lock
,flags
);
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 spin_unlock_irqrestore(&devc
->lock
,flags
);
244 static int reset_uart401(uart401_devc
* devc
)
249 * Send the RESET command. Try again if no success at the first time.
254 for (n
= 0; n
< 2 && !ok
; n
++)
256 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
257 devc
->input_byte
= 0;
258 uart401_cmd(devc
, MPU_RESET
);
261 * Wait at least 25 msec. This method is not accurate so let's make the
262 * loop bit longer. Cannot sleep since this is called during boot.
265 for (timeout
= 50000; timeout
> 0 && !ok
; timeout
--)
267 if (devc
->input_byte
== MPU_ACK
) /* Interrupt */
269 else if (input_avail(devc
))
271 if (uart401_read(devc
) == MPU_ACK
)
277 /* Flush input before enabling interrupts */
279 uart401_input_loop(devc
);
281 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
286 int probe_uart401(struct address_info
*hw_config
, struct module
*owner
)
289 char *name
= "MPU-401 (UART) MIDI";
293 DDB(printk("Entered probe_uart401()\n"));
295 /* Default to "not found" */
296 hw_config
->slots
[4] = -1;
298 if (!request_region(hw_config
->io_base
, 4, "MPU-401 UART")) {
299 printk(KERN_INFO
"uart401: could not request_region(%d, 4)\n", hw_config
->io_base
);
303 devc
= kmalloc(sizeof(uart401_devc
), GFP_KERNEL
);
305 printk(KERN_WARNING
"uart401: Can't allocate memory\n");
309 devc
->base
= hw_config
->io_base
;
310 devc
->irq
= hw_config
->irq
;
311 devc
->osp
= hw_config
->osp
;
312 devc
->midi_input_intr
= NULL
;
314 devc
->input_byte
= 0;
317 spin_lock_init(&devc
->lock
);
319 spin_lock_irqsave(&devc
->lock
,flags
);
320 ok
= reset_uart401(devc
);
321 spin_unlock_irqrestore(&devc
->lock
,flags
);
327 name
= hw_config
->name
;
335 if (!devc
->share_irq
)
336 if (request_irq(devc
->irq
, uart401intr
, 0, "MPU-401 UART", devc
) < 0) {
337 printk(KERN_WARNING
"uart401: Failed to allocate IRQ%d\n", devc
->irq
);
340 devc
->my_dev
= sound_alloc_mididev();
341 enter_uart_mode(devc
);
343 if (devc
->my_dev
== -1) {
344 printk(KERN_INFO
"uart401: Too many midi devices detected\n");
347 conf_printf(name
, hw_config
);
348 midi_devs
[devc
->my_dev
] = kmemdup(&uart401_operations
,
349 sizeof(struct midi_operations
),
351 if (!midi_devs
[devc
->my_dev
]) {
352 printk(KERN_ERR
"uart401: Failed to allocate memory\n");
353 goto cleanup_unload_mididev
;
357 midi_devs
[devc
->my_dev
]->owner
= owner
;
359 midi_devs
[devc
->my_dev
]->devc
= devc
;
360 midi_devs
[devc
->my_dev
]->converter
= kmemdup(&std_midi_synth
,
361 sizeof(struct synth_operations
),
364 if (!midi_devs
[devc
->my_dev
]->converter
) {
365 printk(KERN_WARNING
"uart401: Failed to allocate memory\n");
366 goto cleanup_midi_devs
;
368 strcpy(midi_devs
[devc
->my_dev
]->info
.name
, name
);
369 midi_devs
[devc
->my_dev
]->converter
->id
= "UART401";
370 midi_devs
[devc
->my_dev
]->converter
->midi_dev
= devc
->my_dev
;
373 midi_devs
[devc
->my_dev
]->converter
->owner
= owner
;
375 hw_config
->slots
[4] = devc
->my_dev
;
380 kfree(midi_devs
[devc
->my_dev
]);
381 cleanup_unload_mididev
:
382 sound_unload_mididev(devc
->my_dev
);
384 if (!devc
->share_irq
)
385 free_irq(devc
->irq
, devc
);
389 release_region(hw_config
->io_base
, 4);
393 void unload_uart401(struct address_info
*hw_config
)
396 int n
=hw_config
->slots
[4];
399 if(n
==-1 || midi_devs
[n
]==NULL
)
402 /* Not allocated (erm ??) */
404 devc
= midi_devs
[hw_config
->slots
[4]]->devc
;
409 release_region(hw_config
->io_base
, 4);
411 if (!devc
->share_irq
)
412 free_irq(devc
->irq
, devc
);
415 kfree(midi_devs
[devc
->my_dev
]->converter
);
416 kfree(midi_devs
[devc
->my_dev
]);
420 /* This kills midi_devs[x] */
421 sound_unload_mididev(hw_config
->slots
[4]);
424 EXPORT_SYMBOL(probe_uart401
);
425 EXPORT_SYMBOL(unload_uart401
);
426 EXPORT_SYMBOL(uart401intr
);
428 static struct address_info cfg_mpu
;
433 module_param(io
, int, 0444);
434 module_param(irq
, int, 0444);
437 static int __init
init_uart401(void)
440 cfg_mpu
.io_base
= io
;
442 /* Can be loaded either for module use or to provide functions
444 if (cfg_mpu
.io_base
!= -1 && cfg_mpu
.irq
!= -1) {
445 printk(KERN_INFO
"MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
446 if (!probe_uart401(&cfg_mpu
, THIS_MODULE
))
453 static void __exit
cleanup_uart401(void)
455 if (cfg_mpu
.io_base
!= -1 && cfg_mpu
.irq
!= -1)
456 unload_uart401(&cfg_mpu
);
459 module_init(init_uart401
);
460 module_exit(cleanup_uart401
);
463 static int __init
setup_uart401(char *str
)
468 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
476 __setup("uart401=", setup_uart401
);
478 MODULE_LICENSE("GPL");