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"
27 #include "soundmodule.h"
31 typedef struct uart401_devc
36 void (*midi_input_intr
) (int dev
, unsigned char data
);
38 volatile unsigned char input_byte
;
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
)
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
)
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
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
)
86 while (input_avail(devc
) && --work_limit
)
88 unsigned char c
= uart401_read(devc
);
92 else if (devc
->opened
& OPEN_READ
&& devc
->midi_input_intr
)
93 devc
->midi_input_intr(devc
->my_dev
, c
);
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
;
105 printk(KERN_ERR
"uart401: bad devc\n");
109 if (input_avail(devc
))
110 uart401_input_loop(devc
);
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
;
126 while (input_avail(devc
))
129 devc
->midi_input_intr
= input
;
131 enter_uart_mode(devc
);
137 static void uart401_close(int dev
)
139 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
145 static int uart401_out(int dev
, unsigned char midi_byte
)
149 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
154 * Test for input since pending input seems to block the output.
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");
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 struct midi_operations uart401_operations
=
210 "MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401
222 uart401_buffer_status
,
226 static void enter_uart_mode(uart401_devc
* devc
)
233 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
235 devc
->input_byte
= 0;
236 uart401_cmd(devc
, UART_MODE_ON
);
239 for (timeout
= 50000; timeout
> 0 && !ok
; timeout
--)
240 if (devc
->input_byte
== MPU_ACK
)
242 else if (input_avail(devc
))
243 if (uart401_read(devc
) == MPU_ACK
)
246 restore_flags(flags
);
249 void attach_uart401(struct address_info
*hw_config
)
252 char *name
= "MPU-401 (UART) MIDI";
256 name
= hw_config
->name
;
258 if (detected_devc
== NULL
)
262 devc
= (uart401_devc
*) kmalloc(sizeof(uart401_devc
), GFP_KERNEL
);
265 printk(KERN_WARNING
"uart401: Can't allocate memory\n");
268 memcpy((char *) devc
, (char *) detected_devc
, sizeof(uart401_devc
));
269 detected_devc
= NULL
;
271 devc
->irq
= hw_config
->irq
;
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
);
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");
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
);
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
]);
321 sound_unload_mididev(devc
->my_dev
);
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
;
335 static int reset_uart401(uart401_devc
* devc
)
340 * Send the RESET command. Try again if no success at the first time.
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 */
360 else if (input_avail(devc
))
362 if (uart401_read(devc
) == MPU_ACK
)
371 DEB(printk("Reset UART401 OK\n"));
374 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
377 uart401_input_loop(devc
); /*
378 * Flush input before enabling interrupts
384 int probe_uart401(struct address_info
*hw_config
)
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))
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
;
405 devc
->input_byte
= 0;
411 ok
= reset_uart401(devc
);
412 restore_flags(flags
);
415 detected_devc
= devc
;
420 void unload_uart401(struct address_info
*hw_config
)
423 int n
=hw_config
->slots
[4];
426 if(n
==-1 || midi_devs
[n
]==NULL
)
429 /* Not allocated (erm ??) */
431 devc
= midi_devs
[hw_config
->slots
[4]]->devc
;
436 release_region(hw_config
->io_base
, 4);
438 if (!devc
->share_irq
)
439 free_irq(devc
->irq
, devc
);
442 kfree(midi_devs
[devc
->my_dev
]->converter
);
443 kfree(midi_devs
[devc
->my_dev
]);
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)
468 cfg_mpu
.io_base
= io
;
470 /* Can be loaded either for module use or to provide functions
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)
476 attach_uart401(&cfg_mpu
);
482 static void __exit
cleanup_uart401(void)
484 if (cfg_mpu
.io_base
!= -1 && cfg_mpu
.irq
!= -1)
485 unload_uart401(&cfg_mpu
);
489 module_init(init_uart401
);
490 module_exit(cleanup_uart401
);
493 static int __init
setup_uart401(char *str
)
498 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
506 __setup("uart401=", setup_uart401
);