ARM: shmobile: r8a7792: basic SoC support
[linux-2.6/btrfs-unstable.git] / sound / oss / uart401.c
blobdae4d4344407875bb627acbe25b115b5fb734a19
1 /*
2 * sound/oss/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/slab.h>
28 #include <linux/spinlock.h>
29 #include "sound_config.h"
31 #include "mpu401.h"
33 struct uart401_devc
35 int base;
36 int irq;
37 int *osp;
38 void (*midi_input_intr) (int dev, unsigned char data);
39 int opened, disabled;
40 volatile unsigned char input_byte;
41 int my_dev;
42 int share_irq;
43 spinlock_t lock;
46 #define DATAPORT (devc->base)
47 #define COMDPORT (devc->base+1)
48 #define STATPORT (devc->base+1)
50 static int uart401_status(struct 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(struct uart401_devc *devc, unsigned char cmd)
60 outb((cmd), COMDPORT);
63 static int uart401_read(struct uart401_devc *devc)
65 return inb(DATAPORT);
68 static void uart401_write(struct 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(struct uart401_devc *devc);
80 static void enter_uart_mode(struct uart401_devc *devc);
82 static void uart401_input_loop(struct 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 irqreturn_t uart401intr(int irq, void *dev_id)
101 struct uart401_devc *devc = dev_id;
103 if (devc == NULL)
105 printk(KERN_ERR "uart401: bad devc\n");
106 return IRQ_NONE;
109 if (input_avail(devc))
110 uart401_input_loop(devc);
111 return IRQ_HANDLED;
114 static int
115 uart401_open(int dev, int mode,
116 void (*input) (int dev, unsigned char data),
117 void (*output) (int dev)
120 struct uart401_devc *devc = (struct uart401_devc *)
121 midi_devs[dev]->devc;
123 if (devc->opened)
124 return -EBUSY;
126 /* Flush the UART */
128 while (input_avail(devc))
129 uart401_read(devc);
131 devc->midi_input_intr = input;
132 devc->opened = mode;
133 enter_uart_mode(devc);
134 devc->disabled = 0;
136 return 0;
139 static void uart401_close(int dev)
141 struct uart401_devc *devc = (struct uart401_devc *)
142 midi_devs[dev]->devc;
144 reset_uart401(devc);
145 devc->opened = 0;
148 static int uart401_out(int dev, unsigned char midi_byte)
150 int timeout;
151 unsigned long flags;
152 struct uart401_devc *devc = (struct uart401_devc *)
153 midi_devs[dev]->devc;
155 if (devc->disabled)
156 return 1;
158 * Test for input since pending input seems to block the output.
161 spin_lock_irqsave(&devc->lock,flags);
162 if (input_avail(devc))
163 uart401_input_loop(devc);
165 spin_unlock_irqrestore(&devc->lock,flags);
168 * Sometimes it takes about 13000 loops before the output becomes ready
169 * (After reset). Normally it takes just about 10 loops.
172 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
174 if (!output_ready(devc))
176 printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
177 devc->disabled = 1;
178 reset_uart401(devc);
179 enter_uart_mode(devc);
180 return 1;
182 uart401_write(devc, midi_byte);
183 return 1;
186 static inline int uart401_start_read(int dev)
188 return 0;
191 static inline int uart401_end_read(int dev)
193 return 0;
196 static inline void uart401_kick(int dev)
200 static inline int uart401_buffer_status(int dev)
202 return 0;
205 #define MIDI_SYNTH_NAME "MPU-401 UART"
206 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
207 #include "midi_synth.h"
209 static const struct midi_operations uart401_operations =
211 .owner = THIS_MODULE,
212 .info = {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
213 .converter = &std_midi_synth,
214 .in_info = {0},
215 .open = uart401_open,
216 .close = uart401_close,
217 .outputc = uart401_out,
218 .start_read = uart401_start_read,
219 .end_read = uart401_end_read,
220 .kick = uart401_kick,
221 .buffer_status = uart401_buffer_status,
224 static void enter_uart_mode(struct uart401_devc *devc)
226 int ok, timeout;
227 unsigned long flags;
229 spin_lock_irqsave(&devc->lock,flags);
230 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
232 devc->input_byte = 0;
233 uart401_cmd(devc, UART_MODE_ON);
235 ok = 0;
236 for (timeout = 50000; timeout > 0 && !ok; timeout--)
237 if (devc->input_byte == MPU_ACK)
238 ok = 1;
239 else if (input_avail(devc))
240 if (uart401_read(devc) == MPU_ACK)
241 ok = 1;
243 spin_unlock_irqrestore(&devc->lock,flags);
246 static int reset_uart401(struct uart401_devc *devc)
248 int ok, timeout, n;
251 * Send the RESET command. Try again if no success at the first time.
254 ok = 0;
256 for (n = 0; n < 2 && !ok; n++)
258 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
259 devc->input_byte = 0;
260 uart401_cmd(devc, MPU_RESET);
263 * Wait at least 25 msec. This method is not accurate so let's make the
264 * loop bit longer. Cannot sleep since this is called during boot.
267 for (timeout = 50000; timeout > 0 && !ok; timeout--)
269 if (devc->input_byte == MPU_ACK) /* Interrupt */
270 ok = 1;
271 else if (input_avail(devc))
273 if (uart401_read(devc) == MPU_ACK)
274 ok = 1;
279 /* Flush input before enabling interrupts */
280 if (ok)
281 uart401_input_loop(devc);
282 else
283 DDB(printk("Reset UART401 failed - No hardware detected.\n"));
285 return ok;
288 int probe_uart401(struct address_info *hw_config, struct module *owner)
290 struct uart401_devc *devc;
291 char *name = "MPU-401 (UART) MIDI";
292 int ok = 0;
293 unsigned long flags;
295 DDB(printk("Entered probe_uart401()\n"));
297 /* Default to "not found" */
298 hw_config->slots[4] = -1;
300 if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
301 printk(KERN_INFO "uart401: could not request_region(%d, 4)\n", hw_config->io_base);
302 return 0;
305 devc = kmalloc(sizeof(struct uart401_devc), GFP_KERNEL);
306 if (!devc) {
307 printk(KERN_WARNING "uart401: Can't allocate memory\n");
308 goto cleanup_region;
311 devc->base = hw_config->io_base;
312 devc->irq = hw_config->irq;
313 devc->osp = hw_config->osp;
314 devc->midi_input_intr = NULL;
315 devc->opened = 0;
316 devc->input_byte = 0;
317 devc->my_dev = 0;
318 devc->share_irq = 0;
319 spin_lock_init(&devc->lock);
321 spin_lock_irqsave(&devc->lock,flags);
322 ok = reset_uart401(devc);
323 spin_unlock_irqrestore(&devc->lock,flags);
325 if (!ok)
326 goto cleanup_devc;
328 if (hw_config->name)
329 name = hw_config->name;
331 if (devc->irq < 0) {
332 devc->share_irq = 1;
333 devc->irq *= -1;
334 } else
335 devc->share_irq = 0;
337 if (!devc->share_irq)
338 if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
339 printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
340 devc->share_irq = 1;
342 devc->my_dev = sound_alloc_mididev();
343 enter_uart_mode(devc);
345 if (devc->my_dev == -1) {
346 printk(KERN_INFO "uart401: Too many midi devices detected\n");
347 goto cleanup_irq;
349 conf_printf(name, hw_config);
350 midi_devs[devc->my_dev] = kmemdup(&uart401_operations,
351 sizeof(struct midi_operations),
352 GFP_KERNEL);
353 if (!midi_devs[devc->my_dev]) {
354 printk(KERN_ERR "uart401: Failed to allocate memory\n");
355 goto cleanup_unload_mididev;
358 if (owner)
359 midi_devs[devc->my_dev]->owner = owner;
361 midi_devs[devc->my_dev]->devc = devc;
362 midi_devs[devc->my_dev]->converter = kmemdup(&std_midi_synth,
363 sizeof(struct synth_operations),
364 GFP_KERNEL);
366 if (!midi_devs[devc->my_dev]->converter) {
367 printk(KERN_WARNING "uart401: Failed to allocate memory\n");
368 goto cleanup_midi_devs;
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 struct 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 kfree(midi_devs[devc->my_dev]->converter);
416 kfree(midi_devs[devc->my_dev]);
417 kfree(devc);
419 /* This kills midi_devs[x] */
420 sound_unload_mididev(hw_config->slots[4]);
423 EXPORT_SYMBOL(probe_uart401);
424 EXPORT_SYMBOL(unload_uart401);
425 EXPORT_SYMBOL(uart401intr);
427 static struct address_info cfg_mpu;
429 static int io = -1;
430 static int irq = -1;
432 module_param(io, int, 0444);
433 module_param(irq, int, 0444);
436 static int __init init_uart401(void)
438 cfg_mpu.irq = irq;
439 cfg_mpu.io_base = io;
441 /* Can be loaded either for module use or to provide functions
442 to others */
443 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
444 printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
445 if (!probe_uart401(&cfg_mpu, THIS_MODULE))
446 return -ENODEV;
449 return 0;
452 static void __exit cleanup_uart401(void)
454 if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
455 unload_uart401(&cfg_mpu);
458 module_init(init_uart401);
459 module_exit(cleanup_uart401);
461 #ifndef MODULE
462 static int __init setup_uart401(char *str)
464 /* io, irq */
465 int ints[3];
467 str = get_options(str, ARRAY_SIZE(ints), ints);
469 io = ints[1];
470 irq = ints[2];
472 return 1;
475 __setup("uart401=", setup_uart401);
476 #endif
477 MODULE_LICENSE("GPL");