1 2 3#include <linux/init.h> 4#include <linux/interrupt.h> 5#include <linux/module.h> 6#include <linux/spinlock.h> 7/* Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl: 8 * added 6850 support, used with COVOX SoundMaster II and custom cards. 9 */ 10 11#include "sound_config.h" 12 13static int uart6850_base = 0x330; 14 15static int *uart6850_osp; 16 17#define DATAPORT (uart6850_base) 18#define COMDPORT (uart6850_base+1) 19#define STATPORT (uart6850_base+1) 20 21static int uart6850_status(void) 22{ 23 return inb(STATPORT); 24} 25 26#define input_avail() (uart6850_status()&INPUT_AVAIL) 27#define output_ready() (uart6850_status()&OUTPUT_READY) 28 29static void uart6850_cmd(unsigned char cmd) 30{ 31 outb(cmd, COMDPORT); 32} 33 34static int uart6850_read(void) 35{ 36 return inb(DATAPORT); 37} 38 39static void uart6850_write(unsigned char byte) 40{ 41 outb(byte, DATAPORT); 42} 43 44#define OUTPUT_READY 0x02 /* Mask for data ready Bit */ 45#define INPUT_AVAIL 0x01 /* Mask for Data Send Ready Bit */ 46 47#define UART_RESET 0x95 48#define UART_MODE_ON 0x03 49 50static int uart6850_opened; 51static int uart6850_irq; 52static int uart6850_detected; 53static int my_dev; 54static DEFINE_SPINLOCK(lock); 55 56static void (*midi_input_intr) (int dev, unsigned char data); 57static void poll_uart6850(unsigned long dummy); 58 59 60static DEFINE_TIMER(uart6850_timer, poll_uart6850, 0, 0); 61 62static void uart6850_input_loop(void) 63{ 64 int count = 10; 65 66 while (count) 67 { 68 /* 69 * Not timed out 70 */ 71 if (input_avail()) 72 { 73 unsigned char c = uart6850_read(); 74 count = 100; 75 if (uart6850_opened & OPEN_READ) 76 midi_input_intr(my_dev, c); 77 } 78 else 79 { 80 while (!input_avail() && count) 81 count--; 82 } 83 } 84} 85 86static irqreturn_t m6850intr(int irq, void *dev_id) 87{ 88 if (input_avail()) 89 uart6850_input_loop(); 90 return IRQ_HANDLED; 91} 92 93/* 94 * It looks like there is no input interrupts in the UART mode. Let's try 95 * polling. 96 */ 97 98static void poll_uart6850(unsigned long dummy) 99{ 100 unsigned long flags; 101 102 if (!(uart6850_opened & OPEN_READ)) 103 return; /* Device has been closed */ 104 105 spin_lock_irqsave(&lock,flags); 106 if (input_avail()) 107 uart6850_input_loop(); 108 109 uart6850_timer.expires = 1 + jiffies; 110 add_timer(&uart6850_timer); 111 112 /* 113 * Come back later 114 */ 115 116 spin_unlock_irqrestore(&lock,flags); 117} 118 119static int uart6850_open(int dev, int mode, 120 void (*input) (int dev, unsigned char data), 121 void (*output) (int dev) 122) 123{ 124 if (uart6850_opened) 125 { 126/* printk("Midi6850: Midi busy\n");*/ 127 return -EBUSY; 128 }; 129 130 uart6850_cmd(UART_RESET); 131 uart6850_input_loop(); 132 midi_input_intr = input; 133 uart6850_opened = mode; 134 poll_uart6850(0); /* 135 * Enable input polling 136 */ 137 138 return 0; 139} 140 141static void uart6850_close(int dev) 142{ 143 uart6850_cmd(UART_MODE_ON); 144 del_timer(&uart6850_timer); 145 uart6850_opened = 0; 146} 147 148static int uart6850_out(int dev, unsigned char midi_byte) 149{ 150 int timeout; 151 unsigned long flags; 152 153 /* 154 * Test for input since pending input seems to block the output. 155 */ 156 157 spin_lock_irqsave(&lock,flags); 158 159 if (input_avail()) 160 uart6850_input_loop(); 161 162 spin_unlock_irqrestore(&lock,flags); 163 164 /* 165 * Sometimes it takes about 13000 loops before the output becomes ready 166 * (After reset). Normally it takes just about 10 loops. 167 */ 168 169 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /* 170 * Wait 171 */ 172 if (!output_ready()) 173 { 174 printk(KERN_WARNING "Midi6850: Timeout\n"); 175 return 0; 176 } 177 uart6850_write(midi_byte); 178 return 1; 179} 180 181static inline int uart6850_command(int dev, unsigned char *midi_byte) 182{ 183 return 1; 184} 185 186static inline int uart6850_start_read(int dev) 187{ 188 return 0; 189} 190 191static inline int uart6850_end_read(int dev) 192{ 193 return 0; 194} 195 196static inline void uart6850_kick(int dev) 197{ 198} 199 200static inline int uart6850_buffer_status(int dev) 201{ 202 return 0; /* 203 * No data in buffers 204 */ 205} 206 207#define MIDI_SYNTH_NAME "6850 UART Midi" 208#define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT 209#include "midi_synth.h" 210 211static struct midi_operations uart6850_operations = 212{ 213 .owner = THIS_MODULE, 214 .info = {"6850 UART", 0, 0, SNDCARD_UART6850}, 215 .converter = &std_midi_synth, 216 .in_info = {0}, 217 .open = uart6850_open, 218 .close = uart6850_close, 219 .outputc = uart6850_out, 220 .start_read = uart6850_start_read, 221 .end_read = uart6850_end_read, 222 .kick = uart6850_kick, 223 .command = uart6850_command, 224 .buffer_status = uart6850_buffer_status 225}; 226 227 228static void __init attach_uart6850(struct address_info *hw_config) 229{ 230 int ok, timeout; 231 unsigned long flags; 232 233 if (!uart6850_detected) 234 return; 235 236 if ((my_dev = sound_alloc_mididev()) == -1) 237 { 238 printk(KERN_INFO "uart6850: Too many midi devices detected\n"); 239 return; 240 } 241 uart6850_base = hw_config->io_base; 242 uart6850_osp = hw_config->osp; 243 uart6850_irq = hw_config->irq; 244 245 spin_lock_irqsave(&lock,flags); 246 247 for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /* 248 * Wait 249 */ 250 uart6850_cmd(UART_MODE_ON); 251 ok = 1; 252 spin_unlock_irqrestore(&lock,flags); 253 254 conf_printf("6850 Midi Interface", hw_config); 255 256 std_midi_synth.midi_dev = my_dev; 257 hw_config->slots[4] = my_dev; 258 midi_devs[my_dev] = &uart6850_operations; 259 sequencer_init(); 260} 261 262static inline int reset_uart6850(void) 263{ 264 uart6850_read(); 265 return 1; /* 266 * OK 267 */ 268} 269 270static int __init probe_uart6850(struct address_info *hw_config) 271{ 272 int ok; 273 274 uart6850_osp = hw_config->osp; 275 uart6850_base = hw_config->io_base; 276 uart6850_irq = hw_config->irq; 277 278 if (request_irq(uart6850_irq, m6850intr, 0, "MIDI6850", NULL) < 0) 279 return 0; 280 281 ok = reset_uart6850(); 282 uart6850_detected = ok; 283 return ok; 284} 285 286static void __exit unload_uart6850(struct address_info *hw_config) 287{ 288 free_irq(hw_config->irq, NULL); 289 sound_unload_mididev(hw_config->slots[4]); 290} 291 292static struct address_info cfg_mpu; 293 294static int __initdata io = -1; 295static int __initdata irq = -1; 296 297module_param(io, int, 0); 298module_param(irq, int, 0); 299 300static int __init init_uart6850(void) 301{ 302 cfg_mpu.io_base = io; 303 cfg_mpu.irq = irq; 304 305 if (cfg_mpu.io_base == -1 || cfg_mpu.irq == -1) { 306 printk(KERN_INFO "uart6850: irq and io must be set.\n"); 307 return -EINVAL; 308 } 309 310 if (probe_uart6850(&cfg_mpu)) 311 return -ENODEV; 312 attach_uart6850(&cfg_mpu); 313 314 return 0; 315} 316 317static void __exit cleanup_uart6850(void) 318{ 319 unload_uart6850(&cfg_mpu); 320} 321 322module_init(init_uart6850); 323module_exit(cleanup_uart6850); 324 325#ifndef MODULE 326static int __init setup_uart6850(char *str) 327{ 328 /* io, irq */ 329 int ints[3]; 330 331 str = get_options(str, ARRAY_SIZE(ints), ints); 332 333 io = ints[1]; 334 irq = ints[2]; 335 336 return 1; 337} 338__setup("uart6850=", setup_uart6850); 339#endif 340MODULE_LICENSE("GPL"); 341