Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / sound / oss / uart401.c
blob86b3fd6f00699aa317452fbbe40c2240703597a8
1 /*
2 * sound/uart401.c
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
11 * for more info.
13 * Changes:
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
20 * Status:
21 * Untested
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/spinlock.h>
28 #include "sound_config.h"
30 #include "mpu401.h"
32 typedef struct uart401_devc
34 int base;
35 int irq;
36 int *osp;
37 void (*midi_input_intr) (int dev, unsigned char data);
38 int opened, disabled;
39 volatile unsigned char input_byte;
40 int my_dev;
41 int share_irq;
42 spinlock_t lock;
44 uart401_devc;
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)
52 return inb(STATPORT);
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)
65 return inb(DATAPORT);
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
75 #define MPU_ACK 0xFE
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)
84 int work_limit=30000;
86 while (input_avail(devc) && --work_limit)
88 unsigned char c = uart401_read(devc);
90 if (c == MPU_ACK)
91 devc->input_byte = c;
92 else if (devc->opened & OPEN_READ && devc->midi_input_intr)
93 devc->midi_input_intr(devc->my_dev, c);
95 if(work_limit==0)
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;
103 if (devc == NULL)
105 printk(KERN_ERR "uart401: bad devc\n");
106 return;
109 if (input_avail(devc))
110 uart401_input_loop(devc);
113 static int
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;
121 if (devc->opened)
122 return -EBUSY;
124 /* Flush the UART */
126 while (input_avail(devc))
127 uart401_read(devc);
129 devc->midi_input_intr = input;
130 devc->opened = mode;
131 enter_uart_mode(devc);
132 devc->disabled = 0;
134 return 0;
137 static void uart401_close(int dev)
139 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
141 reset_uart401(devc);
142 devc->opened = 0;
145 static int uart401_out(int dev, unsigned char midi_byte)
147 int timeout;
148 unsigned long flags;
149 uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
151 if (devc->disabled)
152 return 1;
154 * Test for input since pending input seems to block the output.
157 spin_lock_irqsave(&devc->lock,flags);
158 if (input_avail(devc))
159 uart401_input_loop(devc);
161 spin_unlock_irqrestore(&devc->lock,flags);
164 * Sometimes it takes about 13000 loops before the output becomes ready
165 * (After reset). Normally it takes just about 10 loops.
168 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
170 if (!output_ready(devc))
172 printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
173 devc->disabled = 1;
174 reset_uart401(devc);
175 enter_uart_mode(devc);
176 return 1;
178 uart401_write(devc, midi_byte);
179 return 1;
182 static inline int uart401_start_read(int dev)
184 return 0;
187 static inline int uart401_end_read(int dev)
189 return 0;
192 static inline void uart401_kick(int dev)
196 static inline int uart401_buffer_status(int dev)
198 return 0;
201 #define MIDI_SYNTH_NAME "MPU-401 UART"
202 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
203 #include "midi_synth.h"
205 static const struct midi_operations uart401_operations =
207 owner: THIS_MODULE,
208 info: {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
209 converter: &std_midi_synth,
210 in_info: {0},
211 open: uart401_open,
212 close: uart401_close,
213 outputc: uart401_out,
214 start_read: uart401_start_read,
215 end_read: uart401_end_read,
216 kick: uart401_kick,
217 buffer_status: uart401_buffer_status,
220 static void enter_uart_mode(uart401_devc * devc)
222 int ok, timeout;
223 unsigned long flags;
225 spin_lock_irqsave(&devc->lock,flags);
226 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
228 devc->input_byte = 0;
229 uart401_cmd(devc, UART_MODE_ON);
231 ok = 0;
232 for (timeout = 50000; timeout > 0 && !ok; timeout--)
233 if (devc->input_byte == MPU_ACK)
234 ok = 1;
235 else if (input_avail(devc))
236 if (uart401_read(devc) == MPU_ACK)
237 ok = 1;
239 spin_unlock_irqrestore(&devc->lock,flags);
242 static int reset_uart401(uart401_devc * devc)
244 int ok, timeout, n;
247 * Send the RESET command. Try again if no success at the first time.
250 ok = 0;
252 for (n = 0; n < 2 && !ok; n++)
254 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
255 devc->input_byte = 0;
256 uart401_cmd(devc, MPU_RESET);
259 * Wait at least 25 msec. This method is not accurate so let's make the
260 * loop bit longer. Cannot sleep since this is called during boot.
263 for (timeout = 50000; timeout > 0 && !ok; timeout--)
265 if (devc->input_byte == MPU_ACK) /* Interrupt */
266 ok = 1;
267 else if (input_avail(devc))
269 if (uart401_read(devc) == MPU_ACK)
270 ok = 1;
276 if (ok)
278 DEB(printk("Reset UART401 OK\n"));
280 else
281 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
283 if (ok)
284 uart401_input_loop(devc); /*
285 * Flush input before enabling interrupts
288 return ok;
291 int probe_uart401(struct address_info *hw_config, struct module *owner)
293 uart401_devc *devc;
294 char *name = "MPU-401 (UART) MIDI";
295 int ok = 0;
296 unsigned long flags;
298 DDB(printk("Entered probe_uart401()\n"));
300 /* Default to "not found" */
301 hw_config->slots[4] = -1;
303 if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
304 printk(KERN_INFO "uart401: could not request_region(%d, 4)\n", hw_config->io_base);
305 return 0;
308 devc = kmalloc(sizeof(uart401_devc), GFP_KERNEL);
309 if (!devc) {
310 printk(KERN_WARNING "uart401: Can't allocate memory\n");
311 goto cleanup_region;
314 devc->base = hw_config->io_base;
315 devc->irq = hw_config->irq;
316 devc->osp = hw_config->osp;
317 devc->midi_input_intr = NULL;
318 devc->opened = 0;
319 devc->input_byte = 0;
320 devc->my_dev = 0;
321 devc->share_irq = 0;
322 spin_lock_init(&devc->lock);
324 spin_lock_irqsave(&devc->lock,flags);
325 ok = reset_uart401(devc);
326 spin_unlock_irqrestore(&devc->lock,flags);
328 if (!ok)
329 goto cleanup_devc;
331 if (hw_config->name)
332 name = hw_config->name;
334 if (devc->irq < 0) {
335 devc->share_irq = 1;
336 devc->irq *= -1;
337 } else
338 devc->share_irq = 0;
340 if (!devc->share_irq)
341 if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
342 printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
343 devc->share_irq = 1;
345 devc->my_dev = sound_alloc_mididev();
346 enter_uart_mode(devc);
348 if (devc->my_dev == -1) {
349 printk(KERN_INFO "uart401: Too many midi devices detected\n");
350 goto cleanup_irq;
352 conf_printf(name, hw_config);
353 midi_devs[devc->my_dev] = kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
354 if (!midi_devs[devc->my_dev]) {
355 printk(KERN_ERR "uart401: Failed to allocate memory\n");
356 goto cleanup_unload_mididev;
358 memcpy(midi_devs[devc->my_dev], &uart401_operations, sizeof(struct midi_operations));
360 if (owner)
361 midi_devs[devc->my_dev]->owner = owner;
363 midi_devs[devc->my_dev]->devc = devc;
364 midi_devs[devc->my_dev]->converter = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
365 if (!midi_devs[devc->my_dev]->converter) {
366 printk(KERN_WARNING "uart401: Failed to allocate memory\n");
367 goto cleanup_midi_devs;
369 memcpy(midi_devs[devc->my_dev]->converter, &std_midi_synth, sizeof(struct synth_operations));
370 strcpy(midi_devs[devc->my_dev]->info.name, name);
371 midi_devs[devc->my_dev]->converter->id = "UART401";
372 midi_devs[devc->my_dev]->converter->midi_dev = devc->my_dev;
374 if (owner)
375 midi_devs[devc->my_dev]->converter->owner = owner;
377 hw_config->slots[4] = devc->my_dev;
378 sequencer_init();
379 devc->opened = 0;
380 return 1;
381 cleanup_midi_devs:
382 kfree(midi_devs[devc->my_dev]);
383 cleanup_unload_mididev:
384 sound_unload_mididev(devc->my_dev);
385 cleanup_irq:
386 if (!devc->share_irq)
387 free_irq(devc->irq, devc);
388 cleanup_devc:
389 kfree(devc);
390 cleanup_region:
391 release_region(hw_config->io_base, 4);
392 return 0;
395 void unload_uart401(struct address_info *hw_config)
397 uart401_devc *devc;
398 int n=hw_config->slots[4];
400 /* Not set up */
401 if(n==-1 || midi_devs[n]==NULL)
402 return;
404 /* Not allocated (erm ??) */
406 devc = midi_devs[hw_config->slots[4]]->devc;
407 if (devc == NULL)
408 return;
410 reset_uart401(devc);
411 release_region(hw_config->io_base, 4);
413 if (!devc->share_irq)
414 free_irq(devc->irq, devc);
415 if (devc)
417 kfree(midi_devs[devc->my_dev]->converter);
418 kfree(midi_devs[devc->my_dev]);
419 kfree(devc);
420 devc = NULL;
422 /* This kills midi_devs[x] */
423 sound_unload_mididev(hw_config->slots[4]);
426 EXPORT_SYMBOL(probe_uart401);
427 EXPORT_SYMBOL(unload_uart401);
428 EXPORT_SYMBOL(uart401intr);
430 static struct address_info cfg_mpu;
432 static int __initdata io = -1;
433 static int __initdata irq = -1;
435 MODULE_PARM(io, "i");
436 MODULE_PARM(irq, "i");
439 static int __init init_uart401(void)
441 cfg_mpu.irq = irq;
442 cfg_mpu.io_base = io;
444 /* Can be loaded either for module use or to provide functions
445 to others */
446 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
447 printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
448 if (!probe_uart401(&cfg_mpu, THIS_MODULE))
449 return -ENODEV;
452 return 0;
455 static void __exit cleanup_uart401(void)
457 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
458 unload_uart401(&cfg_mpu);
461 module_init(init_uart401);
462 module_exit(cleanup_uart401);
464 #ifndef MODULE
465 static int __init setup_uart401(char *str)
467 /* io, irq */
468 int ints[3];
470 str = get_options(str, ARRAY_SIZE(ints), ints);
472 io = ints[1];
473 irq = ints[2];
475 return 1;
478 __setup("uart401=", setup_uart401);
479 #endif
480 MODULE_LICENSE("GPL");