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
13 #include <linux/config.h>
14 #include <linux/module.h>
16 #include "sound_config.h"
17 #include "soundmodule.h"
19 #if (defined(CONFIG_UART401)||defined(CONFIG_MIDI)) || defined(MODULE)
21 typedef struct uart401_devc
26 void (*midi_input_intr
) (int dev
, unsigned char data
);
28 volatile unsigned char input_byte
;
34 static uart401_devc
*detected_devc
= NULL
;
35 static uart401_devc
*irq2devc
[16] =
38 #define DATAPORT (devc->base)
39 #define COMDPORT (devc->base+1)
40 #define STATPORT (devc->base+1)
43 uart401_status(uart401_devc
* devc
)
47 #define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
48 #define output_ready(devc) (!(uart401_status(devc)&OUTPUT_READY))
50 uart401_cmd(uart401_devc
* devc
, unsigned char cmd
)
52 outb((cmd
), COMDPORT
);
55 uart401_read(uart401_devc
* devc
)
60 uart401_write(uart401_devc
* devc
, unsigned char byte
)
62 outb((byte
), DATAPORT
);
65 #define OUTPUT_READY 0x40
66 #define INPUT_AVAIL 0x80
68 #define MPU_RESET 0xFF
69 #define UART_MODE_ON 0x3F
71 static int reset_uart401(uart401_devc
* devc
);
72 static void enter_uart_mode(uart401_devc
* devc
);
75 uart401_input_loop(uart401_devc
* devc
)
77 while (input_avail(devc
))
79 unsigned char c
= uart401_read(devc
);
83 else if (devc
->opened
& OPEN_READ
&& devc
->midi_input_intr
)
84 devc
->midi_input_intr(devc
->my_dev
, c
);
89 uart401intr(int irq
, void *dev_id
, struct pt_regs
*dummy
)
93 if (irq
< 1 || irq
> 15)
101 if (input_avail(devc
))
102 uart401_input_loop(devc
);
106 uart401_open(int dev
, int mode
,
107 void (*input
) (int dev
, unsigned char data
),
108 void (*output
) (int dev
)
111 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
117 while (input_avail(devc
))
120 devc
->midi_input_intr
= input
;
122 enter_uart_mode(devc
);
129 uart401_close(int dev
)
131 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
138 uart401_out(int dev
, unsigned char midi_byte
)
142 uart401_devc
*devc
= (uart401_devc
*) midi_devs
[dev
]->devc
;
147 * Test for input since pending input seems to block the output.
153 if (input_avail(devc
))
154 uart401_input_loop(devc
);
156 restore_flags(flags
);
159 * Sometimes it takes about 13000 loops before the output becomes ready
160 * (After reset). Normally it takes just about 10 loops.
163 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
165 if (!output_ready(devc
))
167 printk("MPU-401: Timeout - Device not responding\n");
170 enter_uart_mode(devc
);
173 uart401_write(devc
, midi_byte
);
178 uart401_start_read(int dev
)
184 uart401_end_read(int dev
)
190 uart401_kick(int dev
)
195 uart401_buffer_status(int dev
)
200 #define MIDI_SYNTH_NAME "MPU-401 UART"
201 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
202 #include "midi_synth.h"
204 static struct midi_operations uart401_operations
=
206 {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401
},
217 uart401_buffer_status
,
222 enter_uart_mode(uart401_devc
* devc
)
229 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
231 devc
->input_byte
= 0;
232 uart401_cmd(devc
, UART_MODE_ON
);
235 for (timeout
= 50000; timeout
> 0 && !ok
; timeout
--)
236 if (devc
->input_byte
== MPU_ACK
)
238 else if (input_avail(devc
))
239 if (uart401_read(devc
) == MPU_ACK
)
242 restore_flags(flags
);
246 attach_uart401(struct address_info
*hw_config
)
249 char *name
= "MPU-401 (UART) MIDI";
252 name
= hw_config
->name
;
254 if (detected_devc
== NULL
)
258 devc
= (uart401_devc
*) (sound_mem_blocks
[sound_nblocks
] = vmalloc(sizeof(uart401_devc
)));
259 sound_mem_sizes
[sound_nblocks
] = sizeof(uart401_devc
);
260 if (sound_nblocks
< 1024)
264 printk(KERN_WARNING
"uart401: Can't allocate memory\n");
267 memcpy((char *) devc
, (char *) detected_devc
, sizeof(uart401_devc
));
268 detected_devc
= NULL
;
270 devc
->irq
= hw_config
->irq
;
278 if (devc
->irq
< 1 || devc
->irq
> 15)
281 if (!devc
->share_irq
)
282 if (snd_set_irq_handler(devc
->irq
, uart401intr
, "MPU-401 UART", devc
->osp
) < 0)
284 printk(KERN_WARNING
"uart401: Failed to allocate IRQ%d\n", devc
->irq
);
287 irq2devc
[devc
->irq
] = devc
;
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");
298 conf_printf(name
, hw_config
);
300 std_midi_synth
.midi_dev
= devc
->my_dev
;
303 midi_devs
[devc
->my_dev
] = (struct midi_operations
*) (sound_mem_blocks
[sound_nblocks
] = vmalloc(sizeof(struct midi_operations
)));
304 sound_mem_sizes
[sound_nblocks
] = sizeof(struct midi_operations
);
306 if (sound_nblocks
< 1024)
308 if (midi_devs
[devc
->my_dev
] == NULL
)
310 printk("uart401: Failed to allocate memory\n");
311 sound_unload_mididev(devc
->my_dev
);
314 memcpy((char *) midi_devs
[devc
->my_dev
], (char *) &uart401_operations
,
315 sizeof(struct midi_operations
));
317 midi_devs
[devc
->my_dev
]->devc
= devc
;
320 midi_devs
[devc
->my_dev
]->converter
= (struct synth_operations
*) (sound_mem_blocks
[sound_nblocks
] = vmalloc(sizeof(struct synth_operations
)));
321 sound_mem_sizes
[sound_nblocks
] = sizeof(struct synth_operations
);
323 if (sound_nblocks
< 1024)
326 if (midi_devs
[devc
->my_dev
]->converter
== NULL
)
328 printk(KERN_WARNING
"uart401: Failed to allocate memory\n");
329 sound_unload_mididev(devc
->my_dev
);
332 memcpy((char *) midi_devs
[devc
->my_dev
]->converter
, (char *) &std_midi_synth
,
333 sizeof(struct synth_operations
));
335 strcpy(midi_devs
[devc
->my_dev
]->info
.name
, name
);
336 midi_devs
[devc
->my_dev
]->converter
->id
= "UART401";
337 hw_config
->slots
[4] = devc
->my_dev
;
343 reset_uart401(uart401_devc
* devc
)
348 * Send the RESET command. Try again if no success at the first time.
353 for (n
= 0; n
< 2 && !ok
; n
++)
355 for (timeout
= 30000; timeout
> 0 && !output_ready(devc
); timeout
--);
357 devc
->input_byte
= 0;
358 uart401_cmd(devc
, MPU_RESET
);
361 * Wait at least 25 msec. This method is not accurate so let's make the
362 * loop bit longer. Cannot sleep since this is called during boot.
365 for (timeout
= 50000; timeout
> 0 && !ok
; timeout
--)
366 if (devc
->input_byte
== MPU_ACK
) /* Interrupt */
368 else if (input_avail(devc
))
369 if (uart401_read(devc
) == MPU_ACK
)
377 DEB(printk("Reset UART401 OK\n"));
379 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
382 uart401_input_loop(devc
); /*
383 * Flush input before enabling interrupts
390 probe_uart401(struct address_info
*hw_config
)
395 static uart401_devc hw_info
;
396 uart401_devc
*devc
= &hw_info
;
398 DDB(printk("Entered probe_uart401()\n"));
400 detected_devc
= NULL
;
402 if (check_region(hw_config
->io_base
, 4))
405 devc
->base
= hw_config
->io_base
;
406 devc
->irq
= hw_config
->irq
;
407 devc
->osp
= hw_config
->osp
;
408 devc
->midi_input_intr
= NULL
;
410 devc
->input_byte
= 0;
416 ok
= reset_uart401(devc
);
417 restore_flags(flags
);
420 detected_devc
= devc
;
426 unload_uart401(struct address_info
*hw_config
)
430 int irq
= hw_config
->irq
;
436 if (irq
< 1 || irq
> 15)
439 devc
= irq2devc
[irq
];
444 release_region(hw_config
->io_base
, 4);
446 if (!devc
->share_irq
)
447 snd_release_irq(devc
->irq
);
451 sound_unload_mididev(hw_config
->slots
[4]);
459 MODULE_PARM(io
, "i");
460 MODULE_PARM(irq
, "i");
461 struct address_info hw
;
466 /* Can be loaded either for module use or to provide functions
468 if (io
!= -1 && irq
!= -1)
470 printk("MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
473 if (probe_uart401(&hw
) == 0)
484 if (io
!= -1 && irq
!= -1)
498 EXPORT_SYMBOL(attach_uart401
);
499 EXPORT_SYMBOL(probe_uart401
);
500 EXPORT_SYMBOL(unload_uart401
);
501 EXPORT_SYMBOL(uart401intr
);