Import 2.1.77
[davej-history.git] / drivers / sound / uart401.c
blob7293e9047396abdd07db7703544beb2c820bb43e
1 /*
2 * sound/uart401.c
4 * MPU-401 UART driver (formerly uart401_midi.c)
5 */
6 /*
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 #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
23 int base;
24 int irq;
25 int *osp;
26 void (*midi_input_intr) (int dev, unsigned char data);
27 int opened, disabled;
28 volatile unsigned char input_byte;
29 int my_dev;
30 int share_irq;
32 uart401_devc;
34 static uart401_devc *detected_devc = NULL;
35 static uart401_devc *irq2devc[16] =
36 {NULL};
38 #define DATAPORT (devc->base)
39 #define COMDPORT (devc->base+1)
40 #define STATPORT (devc->base+1)
42 static int
43 uart401_status(uart401_devc * devc)
45 return inb(STATPORT);
47 #define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
48 #define output_ready(devc) (!(uart401_status(devc)&OUTPUT_READY))
49 static void
50 uart401_cmd(uart401_devc * devc, unsigned char cmd)
52 outb((cmd), COMDPORT);
54 static int
55 uart401_read(uart401_devc * devc)
57 return inb(DATAPORT);
59 static void
60 uart401_write(uart401_devc * devc, unsigned char byte)
62 outb((byte), DATAPORT);
65 #define OUTPUT_READY 0x40
66 #define INPUT_AVAIL 0x80
67 #define MPU_ACK 0xFE
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);
74 static void
75 uart401_input_loop(uart401_devc * devc)
77 while (input_avail(devc))
79 unsigned char c = uart401_read(devc);
81 if (c == MPU_ACK)
82 devc->input_byte = c;
83 else if (devc->opened & OPEN_READ && devc->midi_input_intr)
84 devc->midi_input_intr(devc->my_dev, c);
88 void
89 uart401intr(int irq, void *dev_id, struct pt_regs *dummy)
91 uart401_devc *devc;
93 if (irq < 1 || irq > 15)
94 return;
96 devc = irq2devc[irq];
98 if (devc == NULL)
99 return;
101 if (input_avail(devc))
102 uart401_input_loop(devc);
105 static int
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;
113 if (devc->opened)
115 return -EBUSY;
117 while (input_avail(devc))
118 uart401_read(devc);
120 devc->midi_input_intr = input;
121 devc->opened = mode;
122 enter_uart_mode(devc);
123 devc->disabled = 0;
125 return 0;
128 static void
129 uart401_close(int dev)
131 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
133 reset_uart401(devc);
134 devc->opened = 0;
137 static int
138 uart401_out(int dev, unsigned char midi_byte)
140 int timeout;
141 unsigned long flags;
142 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
144 if (devc->disabled)
145 return 1;
147 * Test for input since pending input seems to block the output.
150 save_flags(flags);
151 cli();
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");
168 devc->disabled = 1;
169 reset_uart401(devc);
170 enter_uart_mode(devc);
171 return 1;
173 uart401_write(devc, midi_byte);
174 return 1;
177 static int
178 uart401_start_read(int dev)
180 return 0;
183 static int
184 uart401_end_read(int dev)
186 return 0;
189 static void
190 uart401_kick(int dev)
194 static int
195 uart401_buffer_status(int dev)
197 return 0;
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},
207 &std_midi_synth,
208 {0},
209 uart401_open,
210 uart401_close,
211 NULL, /* ioctl */
212 uart401_out,
213 uart401_start_read,
214 uart401_end_read,
215 uart401_kick,
216 NULL,
217 uart401_buffer_status,
218 NULL
221 static void
222 enter_uart_mode(uart401_devc * devc)
224 int ok, timeout;
225 unsigned long flags;
227 save_flags(flags);
228 cli();
229 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
231 devc->input_byte = 0;
232 uart401_cmd(devc, UART_MODE_ON);
234 ok = 0;
235 for (timeout = 50000; timeout > 0 && !ok; timeout--)
236 if (devc->input_byte == MPU_ACK)
237 ok = 1;
238 else if (input_avail(devc))
239 if (uart401_read(devc) == MPU_ACK)
240 ok = 1;
242 restore_flags(flags);
245 void
246 attach_uart401(struct address_info *hw_config)
248 uart401_devc *devc;
249 char *name = "MPU-401 (UART) MIDI";
251 if (hw_config->name)
252 name = hw_config->name;
254 if (detected_devc == NULL)
255 return;
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)
261 sound_nblocks++;;
262 if (devc == NULL)
264 printk(KERN_WARNING "uart401: Can't allocate memory\n");
265 return;
267 memcpy((char *) devc, (char *) detected_devc, sizeof(uart401_devc));
268 detected_devc = NULL;
270 devc->irq = hw_config->irq;
271 if (devc->irq < 0)
273 devc->share_irq = 1;
274 devc->irq *= -1;
275 } else
276 devc->share_irq = 0;
278 if (devc->irq < 1 || devc->irq > 15)
279 return;
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);
285 devc->share_irq = 1;
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");
296 return;
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)
307 sound_nblocks++;;
308 if (midi_devs[devc->my_dev] == NULL)
310 printk("uart401: Failed to allocate memory\n");
311 sound_unload_mididev(devc->my_dev);
312 return;
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)
324 sound_nblocks++;
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);
330 return;
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;
338 sequencer_init();
339 devc->opened = 0;
342 static int
343 reset_uart401(uart401_devc * devc)
345 int ok, timeout, n;
348 * Send the RESET command. Try again if no success at the first time.
351 ok = 0;
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 */
367 ok = 1;
368 else if (input_avail(devc))
369 if (uart401_read(devc) == MPU_ACK)
370 ok = 1;
375 if (ok)
377 DEB(printk("Reset UART401 OK\n"));
378 } else
379 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
381 if (ok)
382 uart401_input_loop(devc); /*
383 * Flush input before enabling interrupts
386 return ok;
390 probe_uart401(struct address_info *hw_config)
392 int ok = 0;
393 unsigned long flags;
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))
403 return 0;
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;
409 devc->opened = 0;
410 devc->input_byte = 0;
411 devc->my_dev = 0;
412 devc->share_irq = 0;
414 save_flags(flags);
415 cli();
416 ok = reset_uart401(devc);
417 restore_flags(flags);
419 if (ok)
420 detected_devc = devc;
422 return ok;
425 void
426 unload_uart401(struct address_info *hw_config)
428 uart401_devc *devc;
430 int irq = hw_config->irq;
432 if (irq < 0)
434 irq *= -1;
436 if (irq < 1 || irq > 15)
437 return;
439 devc = irq2devc[irq];
440 if (devc == NULL)
441 return;
443 reset_uart401(devc);
444 release_region(hw_config->io_base, 4);
446 if (!devc->share_irq)
447 snd_release_irq(devc->irq);
449 if (devc)
450 devc = NULL;
451 sound_unload_mididev(hw_config->slots[4]);
454 #ifdef MODULE
456 int io = -1;
457 int irq = -1;
459 MODULE_PARM(io, "i");
460 MODULE_PARM(irq, "i");
461 struct address_info hw;
463 int
464 init_module(void)
466 /* Can be loaded either for module use or to provide functions
467 to others */
468 if (io != -1 && irq != -1)
470 printk("MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
471 hw.irq = irq;
472 hw.io_base = io;
473 if (probe_uart401(&hw) == 0)
474 return -ENODEV;
475 attach_uart401(&hw);
477 SOUND_LOCK;
478 return 0;
481 void
482 cleanup_module(void)
484 if (io != -1 && irq != -1)
486 unload_uart401(&hw);
488 /* FREE SYMTAB */
489 SOUND_LOCK_END;
492 #else
494 #endif
496 #endif
498 EXPORT_SYMBOL(attach_uart401);
499 EXPORT_SYMBOL(probe_uart401);
500 EXPORT_SYMBOL(unload_uart401);
501 EXPORT_SYMBOL(uart401intr);