[PATCH] m32r: Remove a warning in m32r_sio.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / drivers / serial-u16550.c
blob1a7fbefe4740de3e013a262fbbf34e85956c76fd
1 /*
2 * serial.c
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
4 * Isaku Yamahata <yamahata@private.email.ne.jp>,
5 * George Hansper <ghansper@apana.org.au>,
6 * Hannu Savolainen
8 * This code is based on the code from ALSA 0.5.9, but heavily rewritten.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Sat Mar 31 17:27:57 PST 2001 tim.mann@compaq.com
25 * Added support for the Midiator MS-124T and for the MS-124W in
26 * Single Addressed (S/A) or Multiple Burst (M/B) mode, with
27 * power derived either parasitically from the serial port or
28 * from a separate power supply.
30 * More documentation can be found in serial-u16550.txt.
33 #include <sound/driver.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/err.h>
37 #include <linux/platform_device.h>
38 #include <linux/slab.h>
39 #include <linux/ioport.h>
40 #include <linux/moduleparam.h>
41 #include <sound/core.h>
42 #include <sound/rawmidi.h>
43 #include <sound/initval.h>
45 #include <linux/serial_reg.h>
47 #include <asm/io.h>
49 MODULE_DESCRIPTION("MIDI serial u16550");
50 MODULE_LICENSE("GPL");
51 MODULE_SUPPORTED_DEVICE("{{ALSA, MIDI serial u16550}}");
53 #define SNDRV_SERIAL_SOUNDCANVAS 0 /* Roland Soundcanvas; F5 NN selects part */
54 #define SNDRV_SERIAL_MS124T 1 /* Midiator MS-124T */
55 #define SNDRV_SERIAL_MS124W_SA 2 /* Midiator MS-124W in S/A mode */
56 #define SNDRV_SERIAL_MS124W_MB 3 /* Midiator MS-124W in M/B mode */
57 #define SNDRV_SERIAL_GENERIC 4 /* Generic Interface */
58 #define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC
59 static char *adaptor_names[] = {
60 "Soundcanvas",
61 "MS-124T",
62 "MS-124W S/A",
63 "MS-124W M/B",
64 "Generic"
67 #define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */
68 #define SNDRV_SERIAL_DROPBUFF 1 /* Non-blocking discard operation */
70 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
71 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
72 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
73 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */
74 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,4,5,7,9,10,11,14,15 */
75 static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */
76 static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */
77 static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
78 static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
79 static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
80 static int droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
82 module_param_array(index, int, NULL, 0444);
83 MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
84 module_param_array(id, charp, NULL, 0444);
85 MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
86 module_param_array(enable, bool, NULL, 0444);
87 MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
88 module_param_array(port, long, NULL, 0444);
89 MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
90 module_param_array(irq, int, NULL, 0444);
91 MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
92 module_param_array(speed, int, NULL, 0444);
93 MODULE_PARM_DESC(speed, "Speed in bauds.");
94 module_param_array(base, int, NULL, 0444);
95 MODULE_PARM_DESC(base, "Base for divisor in bauds.");
96 module_param_array(outs, int, NULL, 0444);
97 MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
98 module_param_array(ins, int, NULL, 0444);
99 MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
100 module_param_array(droponfull, bool, NULL, 0444);
101 MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
103 module_param_array(adaptor, int, NULL, 0444);
104 MODULE_PARM_DESC(adaptor, "Type of adaptor.");
106 /*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/ /* Address outs as 0-3 instead of bitmap */
108 #define SNDRV_SERIAL_MAX_OUTS 16 /* max 64, min 16 */
109 #define SNDRV_SERIAL_MAX_INS 16 /* max 64, min 16 */
111 #define TX_BUFF_SIZE (1<<15) /* Must be 2^n */
112 #define TX_BUFF_MASK (TX_BUFF_SIZE - 1)
114 #define SERIAL_MODE_NOT_OPENED (0)
115 #define SERIAL_MODE_INPUT_OPEN (1 << 0)
116 #define SERIAL_MODE_OUTPUT_OPEN (1 << 1)
117 #define SERIAL_MODE_INPUT_TRIGGERED (1 << 2)
118 #define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3)
120 typedef struct _snd_uart16550 {
121 struct snd_card *card;
122 struct snd_rawmidi *rmidi;
123 struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS];
124 struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS];
126 int filemode; //open status of file
128 spinlock_t open_lock;
130 int irq;
132 unsigned long base;
133 struct resource *res_base;
135 unsigned int speed;
136 unsigned int speed_base;
137 unsigned char divisor;
139 unsigned char old_divisor_lsb;
140 unsigned char old_divisor_msb;
141 unsigned char old_line_ctrl_reg;
143 // parameter for using of write loop
144 short int fifo_limit; //used in uart16550
145 short int fifo_count; //used in uart16550
147 // type of adaptor
148 int adaptor;
150 // inputs
151 int prev_in;
152 unsigned char rstatus;
154 // outputs
155 int prev_out;
156 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
158 // write buffer and its writing/reading position
159 unsigned char tx_buff[TX_BUFF_SIZE];
160 int buff_in_count;
161 int buff_in;
162 int buff_out;
163 int drop_on_full;
165 // wait timer
166 unsigned int timer_running:1;
167 struct timer_list buffer_timer;
169 } snd_uart16550_t;
171 static struct platform_device *devices[SNDRV_CARDS];
173 static inline void snd_uart16550_add_timer(snd_uart16550_t *uart)
175 if (! uart->timer_running) {
176 /* timer 38600bps * 10bit * 16byte */
177 uart->buffer_timer.expires = jiffies + (HZ+255)/256;
178 uart->timer_running = 1;
179 add_timer(&uart->buffer_timer);
183 static inline void snd_uart16550_del_timer(snd_uart16550_t *uart)
185 if (uart->timer_running) {
186 del_timer(&uart->buffer_timer);
187 uart->timer_running = 0;
191 /* This macro is only used in snd_uart16550_io_loop */
192 static inline void snd_uart16550_buffer_output(snd_uart16550_t *uart)
194 unsigned short buff_out = uart->buff_out;
195 if( uart->buff_in_count > 0 ) {
196 outb(uart->tx_buff[buff_out], uart->base + UART_TX);
197 uart->fifo_count++;
198 buff_out++;
199 buff_out &= TX_BUFF_MASK;
200 uart->buff_out = buff_out;
201 uart->buff_in_count--;
205 /* This loop should be called with interrupts disabled
206 * We don't want to interrupt this,
207 * as we're already handling an interrupt
209 static void snd_uart16550_io_loop(snd_uart16550_t * uart)
211 unsigned char c, status;
212 int substream;
214 /* recall previous stream */
215 substream = uart->prev_in;
217 /* Read Loop */
218 while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
219 /* while receive data ready */
220 c = inb(uart->base + UART_RX);
222 /* keep track of last status byte */
223 if (c & 0x80) {
224 uart->rstatus = c;
227 /* handle stream switch */
228 if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
229 if (uart->rstatus == 0xf5) {
230 if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
231 substream = c - 1;
232 if (c != 0xf5)
233 uart->rstatus = 0; /* prevent future bytes from being interpreted as streams */
235 else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && (uart->midi_input[substream] != NULL)) {
236 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
238 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && (uart->midi_input[substream] != NULL)) {
239 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
242 if (status & UART_LSR_OE)
243 snd_printk("%s: Overrun on device at 0x%lx\n",
244 uart->rmidi->name, uart->base);
247 /* remember the last stream */
248 uart->prev_in = substream;
250 /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
251 buffer is never filled. */
252 /* Check write status */
253 if (status & UART_LSR_THRE) {
254 uart->fifo_count = 0;
256 if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
257 || uart->adaptor == SNDRV_SERIAL_GENERIC) {
258 /* Can't use FIFO, must send only when CTS is true */
259 status = inb(uart->base + UART_MSR);
260 while( (uart->fifo_count == 0) && (status & UART_MSR_CTS) &&
261 (uart->buff_in_count > 0) ) {
262 snd_uart16550_buffer_output(uart);
263 status = inb( uart->base + UART_MSR );
265 } else {
266 /* Write loop */
267 while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
268 && uart->buff_in_count > 0) /* Do we want to? */
269 snd_uart16550_buffer_output(uart);
271 if (uart->irq < 0 && uart->buff_in_count > 0)
272 snd_uart16550_add_timer(uart);
275 /* NOTES ON SERVICING INTERUPTS
276 * ---------------------------
277 * After receiving a interrupt, it is important to indicate to the UART that
278 * this has been done.
279 * For a Rx interrupt, this is done by reading the received byte.
280 * For a Tx interrupt this is done by either:
281 * a) Writing a byte
282 * b) Reading the IIR
283 * It is particularly important to read the IIR if a Tx interrupt is received
284 * when there is no data in tx_buff[], as in this case there no other
285 * indication that the interrupt has been serviced, and it remains outstanding
286 * indefinitely. This has the curious side effect that and no further interrupts
287 * will be generated from this device AT ALL!!.
288 * It is also desirable to clear outstanding interrupts when the device is
289 * opened/closed.
292 * Note that some devices need OUT2 to be set before they will generate
293 * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
295 static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id, struct pt_regs *regs)
297 snd_uart16550_t *uart;
299 uart = (snd_uart16550_t *) dev_id;
300 spin_lock(&uart->open_lock);
301 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
302 spin_unlock(&uart->open_lock);
303 return IRQ_NONE;
305 inb(uart->base + UART_IIR); /* indicate to the UART that the interrupt has been serviced */
306 snd_uart16550_io_loop(uart);
307 spin_unlock(&uart->open_lock);
308 return IRQ_HANDLED;
311 /* When the polling mode, this function calls snd_uart16550_io_loop. */
312 static void snd_uart16550_buffer_timer(unsigned long data)
314 unsigned long flags;
315 snd_uart16550_t *uart;
317 uart = (snd_uart16550_t *)data;
318 spin_lock_irqsave(&uart->open_lock, flags);
319 snd_uart16550_del_timer(uart);
320 snd_uart16550_io_loop(uart);
321 spin_unlock_irqrestore(&uart->open_lock, flags);
325 * this method probes, if an uart sits on given port
326 * return 0 if found
327 * return negative error if not found
329 static int __init snd_uart16550_detect(snd_uart16550_t *uart)
331 unsigned long io_base = uart->base;
332 int ok;
333 unsigned char c;
335 /* Do some vague tests for the presence of the uart */
336 if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
337 return -ENODEV; /* Not configured */
340 uart->res_base = request_region(io_base, 8, "Serial MIDI");
341 if (uart->res_base == NULL) {
342 snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
343 return -EBUSY;
346 ok = 1; /* uart detected unless one of the following tests should fail */
347 /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
348 outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
349 c = inb(io_base + UART_IER);
350 /* The top four bits of the IER should always == 0 */
351 if ((c & 0xf0) != 0)
352 ok = 0; /* failed */
354 outb(0xaa, io_base + UART_SCR);
355 /* Write arbitrary data into the scratch reg */
356 c = inb(io_base + UART_SCR);
357 /* If it comes back, it's OK */
358 if (c != 0xaa)
359 ok = 0; /* failed */
361 outb(0x55, io_base + UART_SCR);
362 /* Write arbitrary data into the scratch reg */
363 c = inb(io_base + UART_SCR);
364 /* If it comes back, it's OK */
365 if (c != 0x55)
366 ok = 0; /* failed */
368 return ok;
371 static void snd_uart16550_do_open(snd_uart16550_t * uart)
373 char byte;
375 /* Initialize basic variables */
376 uart->buff_in_count = 0;
377 uart->buff_in = 0;
378 uart->buff_out = 0;
379 uart->fifo_limit = 1;
380 uart->fifo_count = 0;
381 uart->timer_running = 0;
383 outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */
384 | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */
385 | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */
386 | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */
387 /* NOTE: interrupt generated after T=(time)4-bytes
388 * if less than UART_FCR_TRIGGER bytes received
390 ,uart->base + UART_FCR); /* FIFO Control Register */
392 if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
393 uart->fifo_limit = 16;
394 if (uart->divisor != 0) {
395 uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
396 outb(UART_LCR_DLAB /* Divisor latch access bit */
397 ,uart->base + UART_LCR); /* Line Control Register */
398 uart->old_divisor_lsb = inb(uart->base + UART_DLL);
399 uart->old_divisor_msb = inb(uart->base + UART_DLM);
401 outb(uart->divisor
402 ,uart->base + UART_DLL); /* Divisor Latch Low */
403 outb(0
404 ,uart->base + UART_DLM); /* Divisor Latch High */
405 /* DLAB is reset to 0 in next outb() */
407 /* Set serial parameters (parity off, etc) */
408 outb(UART_LCR_WLEN8 /* 8 data-bits */
409 | 0 /* 1 stop-bit */
410 | 0 /* parity off */
411 | 0 /* DLAB = 0 */
412 ,uart->base + UART_LCR); /* Line Control Register */
414 switch (uart->adaptor) {
415 default:
416 outb(UART_MCR_RTS /* Set Request-To-Send line active */
417 | UART_MCR_DTR /* Set Data-Terminal-Ready line active */
418 | UART_MCR_OUT2 /* Set OUT2 - not always required, but when
419 * it is, it is ESSENTIAL for enabling interrupts
421 ,uart->base + UART_MCR); /* Modem Control Register */
422 break;
423 case SNDRV_SERIAL_MS124W_SA:
424 case SNDRV_SERIAL_MS124W_MB:
425 /* MS-124W can draw power from RTS and DTR if they
426 are in opposite states. */
427 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
428 uart->base + UART_MCR);
429 break;
430 case SNDRV_SERIAL_MS124T:
431 /* MS-124T can draw power from RTS and/or DTR (preferably
432 both) if they are both asserted. */
433 outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
434 uart->base + UART_MCR);
435 break;
438 if (uart->irq < 0) {
439 byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
440 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
442 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
443 byte = UART_IER_RDI /* Enable Receiver data interrupt */
444 | UART_IER_MSI /* Enable Modem status interrupt */
446 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
447 byte = UART_IER_RDI /* Enable Receiver data interrupt */
448 | UART_IER_MSI /* Enable Modem status interrupt */
449 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
451 } else {
452 byte = UART_IER_RDI /* Enable Receiver data interrupt */
453 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
456 outb(byte, uart->base + UART_IER); /* Interupt enable Register */
458 inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */
459 inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */
460 inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */
463 static void snd_uart16550_do_close(snd_uart16550_t * uart)
465 if (uart->irq < 0)
466 snd_uart16550_del_timer(uart);
468 /* NOTE: may need to disable interrupts before de-registering out handler.
469 * For now, the consequences are harmless.
472 outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */
473 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
474 ,uart->base + UART_IER); /* Interupt enable Register */
476 switch (uart->adaptor) {
477 default:
478 outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */
479 |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */
480 |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */
481 ,uart->base + UART_MCR); /* Modem Control Register */
482 break;
483 case SNDRV_SERIAL_MS124W_SA:
484 case SNDRV_SERIAL_MS124W_MB:
485 /* MS-124W can draw power from RTS and DTR if they
486 are in opposite states; leave it powered. */
487 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
488 uart->base + UART_MCR);
489 break;
490 case SNDRV_SERIAL_MS124T:
491 /* MS-124T can draw power from RTS and/or DTR (preferably
492 both) if they are both asserted; leave it powered. */
493 outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
494 uart->base + UART_MCR);
495 break;
498 inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
500 /* Restore old divisor */
501 if (uart->divisor != 0) {
502 outb(UART_LCR_DLAB /* Divisor latch access bit */
503 ,uart->base + UART_LCR); /* Line Control Register */
504 outb(uart->old_divisor_lsb
505 ,uart->base + UART_DLL); /* Divisor Latch Low */
506 outb(uart->old_divisor_msb
507 ,uart->base + UART_DLM); /* Divisor Latch High */
508 /* Restore old LCR (data bits, stop bits, parity, DLAB) */
509 outb(uart->old_line_ctrl_reg
510 ,uart->base + UART_LCR); /* Line Control Register */
514 static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
516 unsigned long flags;
517 snd_uart16550_t *uart = substream->rmidi->private_data;
519 spin_lock_irqsave(&uart->open_lock, flags);
520 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
521 snd_uart16550_do_open(uart);
522 uart->filemode |= SERIAL_MODE_INPUT_OPEN;
523 uart->midi_input[substream->number] = substream;
524 spin_unlock_irqrestore(&uart->open_lock, flags);
525 return 0;
528 static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
530 unsigned long flags;
531 snd_uart16550_t *uart = substream->rmidi->private_data;
533 spin_lock_irqsave(&uart->open_lock, flags);
534 uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
535 uart->midi_input[substream->number] = NULL;
536 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
537 snd_uart16550_do_close(uart);
538 spin_unlock_irqrestore(&uart->open_lock, flags);
539 return 0;
542 static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream, int up)
544 unsigned long flags;
545 snd_uart16550_t *uart = substream->rmidi->private_data;
547 spin_lock_irqsave(&uart->open_lock, flags);
548 if (up) {
549 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
550 } else {
551 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
553 spin_unlock_irqrestore(&uart->open_lock, flags);
556 static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
558 unsigned long flags;
559 snd_uart16550_t *uart = substream->rmidi->private_data;
561 spin_lock_irqsave(&uart->open_lock, flags);
562 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
563 snd_uart16550_do_open(uart);
564 uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
565 uart->midi_output[substream->number] = substream;
566 spin_unlock_irqrestore(&uart->open_lock, flags);
567 return 0;
570 static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
572 unsigned long flags;
573 snd_uart16550_t *uart = substream->rmidi->private_data;
575 spin_lock_irqsave(&uart->open_lock, flags);
576 uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
577 uart->midi_output[substream->number] = NULL;
578 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
579 snd_uart16550_do_close(uart);
580 spin_unlock_irqrestore(&uart->open_lock, flags);
581 return 0;
584 static inline int snd_uart16550_buffer_can_write( snd_uart16550_t *uart, int Num )
586 if( uart->buff_in_count + Num < TX_BUFF_SIZE )
587 return 1;
588 else
589 return 0;
592 static inline int snd_uart16550_write_buffer(snd_uart16550_t *uart, unsigned char byte)
594 unsigned short buff_in = uart->buff_in;
595 if( uart->buff_in_count < TX_BUFF_SIZE ) {
596 uart->tx_buff[buff_in] = byte;
597 buff_in++;
598 buff_in &= TX_BUFF_MASK;
599 uart->buff_in = buff_in;
600 uart->buff_in_count++;
601 if (uart->irq < 0) /* polling mode */
602 snd_uart16550_add_timer(uart);
603 return 1;
604 } else
605 return 0;
608 static int snd_uart16550_output_byte(snd_uart16550_t *uart, struct snd_rawmidi_substream *substream, unsigned char midi_byte)
610 if (uart->buff_in_count == 0 /* Buffer empty? */
611 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
612 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
613 (uart->fifo_count == 0 /* FIFO empty? */
614 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
616 /* Tx Buffer Empty - try to write immediately */
617 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
618 /* Transmitter holding register (and Tx FIFO) empty */
619 uart->fifo_count = 1;
620 outb(midi_byte, uart->base + UART_TX);
621 } else {
622 if (uart->fifo_count < uart->fifo_limit) {
623 uart->fifo_count++;
624 outb(midi_byte, uart->base + UART_TX);
625 } else {
626 /* Cannot write (buffer empty) - put char in buffer */
627 snd_uart16550_write_buffer(uart, midi_byte);
630 } else {
631 if( !snd_uart16550_write_buffer(uart, midi_byte) ) {
632 snd_printk("%s: Buffer overrun on device at 0x%lx\n",
633 uart->rmidi->name, uart->base);
634 return 0;
638 return 1;
641 static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
643 unsigned long flags;
644 unsigned char midi_byte, addr_byte;
645 snd_uart16550_t *uart = substream->rmidi->private_data;
646 char first;
647 static unsigned long lasttime=0;
649 /* Interupts are disabled during the updating of the tx_buff,
650 * since it is 'bad' to have two processes updating the same
651 * variables (ie buff_in & buff_out)
654 spin_lock_irqsave(&uart->open_lock, flags);
656 if (uart->irq < 0) //polling
657 snd_uart16550_io_loop(uart);
659 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
660 while (1) {
661 /* buffer full? */
662 /* in this mode we need two bytes of space */
663 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
664 break;
665 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
666 break;
667 #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
668 /* select exactly one of the four ports */
669 addr_byte = (1 << (substream->number + 4)) | 0x08;
670 #else
671 /* select any combination of the four ports */
672 addr_byte = (substream->number << 4) | 0x08;
673 /* ...except none */
674 if (addr_byte == 0x08) addr_byte = 0xf8;
675 #endif
676 snd_uart16550_output_byte(uart, substream, addr_byte);
677 /* send midi byte */
678 snd_uart16550_output_byte(uart, substream, midi_byte);
680 } else {
681 first = 0;
682 while( 1 == snd_rawmidi_transmit_peek(substream, &midi_byte, 1) ) {
683 /* Also send F5 after 3 seconds with no data to handle device disconnect */
684 if (first == 0 && (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
685 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
686 (uart->prev_out != substream->number || jiffies-lasttime > 3*HZ)) {
688 if( snd_uart16550_buffer_can_write( uart, 3 ) ) {
689 /* Roland Soundcanvas part selection */
690 /* If this substream of the data is different previous
691 substream in this uart, send the change part event */
692 uart->prev_out = substream->number;
693 /* change part */
694 snd_uart16550_output_byte(uart, substream, 0xf5);
695 /* data */
696 snd_uart16550_output_byte(uart, substream, uart->prev_out + 1);
697 /* If midi_byte is a data byte, send the previous status byte */
698 if ((midi_byte < 0x80) && (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS))
699 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
700 } else if( !uart->drop_on_full )
701 break;
705 /* send midi byte */
706 if( !snd_uart16550_output_byte(uart, substream, midi_byte) && !uart->drop_on_full )
707 break;
709 if (midi_byte >= 0x80 && midi_byte < 0xf0)
710 uart->prev_status[uart->prev_out] = midi_byte;
711 first = 1;
713 snd_rawmidi_transmit_ack( substream, 1 );
715 lasttime = jiffies;
717 spin_unlock_irqrestore(&uart->open_lock, flags);
720 static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream, int up)
722 unsigned long flags;
723 snd_uart16550_t *uart = substream->rmidi->private_data;
725 spin_lock_irqsave(&uart->open_lock, flags);
726 if (up) {
727 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
728 } else {
729 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
731 spin_unlock_irqrestore(&uart->open_lock, flags);
732 if (up)
733 snd_uart16550_output_write(substream);
736 static struct snd_rawmidi_ops snd_uart16550_output =
738 .open = snd_uart16550_output_open,
739 .close = snd_uart16550_output_close,
740 .trigger = snd_uart16550_output_trigger,
743 static struct snd_rawmidi_ops snd_uart16550_input =
745 .open = snd_uart16550_input_open,
746 .close = snd_uart16550_input_close,
747 .trigger = snd_uart16550_input_trigger,
750 static int snd_uart16550_free(snd_uart16550_t *uart)
752 if (uart->irq >= 0)
753 free_irq(uart->irq, (void *)uart);
754 release_and_free_resource(uart->res_base);
755 kfree(uart);
756 return 0;
759 static int snd_uart16550_dev_free(struct snd_device *device)
761 snd_uart16550_t *uart = device->device_data;
762 return snd_uart16550_free(uart);
765 static int __init snd_uart16550_create(struct snd_card *card,
766 unsigned long iobase,
767 int irq,
768 unsigned int speed,
769 unsigned int base,
770 int adaptor,
771 int droponfull,
772 snd_uart16550_t **ruart)
774 static struct snd_device_ops ops = {
775 .dev_free = snd_uart16550_dev_free,
777 snd_uart16550_t *uart;
778 int err;
781 if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
782 return -ENOMEM;
783 uart->adaptor = adaptor;
784 uart->card = card;
785 spin_lock_init(&uart->open_lock);
786 uart->irq = -1;
787 uart->base = iobase;
788 uart->drop_on_full = droponfull;
790 if ((err = snd_uart16550_detect(uart)) <= 0) {
791 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
792 snd_uart16550_free(uart);
793 return -ENODEV;
796 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
797 if (request_irq(irq, snd_uart16550_interrupt,
798 SA_INTERRUPT, "Serial MIDI", (void *) uart)) {
799 snd_printk("irq %d busy. Using Polling.\n", irq);
800 } else {
801 uart->irq = irq;
804 uart->divisor = base / speed;
805 uart->speed = base / (unsigned int)uart->divisor;
806 uart->speed_base = base;
807 uart->prev_out = -1;
808 uart->prev_in = 0;
809 uart->rstatus = 0;
810 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
811 init_timer(&uart->buffer_timer);
812 uart->buffer_timer.function = snd_uart16550_buffer_timer;
813 uart->buffer_timer.data = (unsigned long)uart;
814 uart->timer_running = 0;
816 /* Register device */
817 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
818 snd_uart16550_free(uart);
819 return err;
822 switch (uart->adaptor) {
823 case SNDRV_SERIAL_MS124W_SA:
824 case SNDRV_SERIAL_MS124W_MB:
825 /* MS-124W can draw power from RTS and DTR if they
826 are in opposite states. */
827 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
828 break;
829 case SNDRV_SERIAL_MS124T:
830 /* MS-124T can draw power from RTS and/or DTR (preferably
831 both) if they are asserted. */
832 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
833 break;
834 default:
835 break;
838 if (ruart)
839 *ruart = uart;
841 return 0;
844 static void __init snd_uart16550_substreams(struct snd_rawmidi_str *stream)
846 struct list_head *list;
848 list_for_each(list, &stream->substreams) {
849 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
850 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
854 static int __init snd_uart16550_rmidi(snd_uart16550_t *uart, int device, int outs, int ins, struct snd_rawmidi **rmidi)
856 struct snd_rawmidi *rrawmidi;
857 int err;
859 if ((err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device, outs, ins, &rrawmidi)) < 0)
860 return err;
861 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_uart16550_input);
862 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_uart16550_output);
863 strcpy(rrawmidi->name, "Serial MIDI");
864 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
865 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
866 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
867 SNDRV_RAWMIDI_INFO_INPUT |
868 SNDRV_RAWMIDI_INFO_DUPLEX;
869 rrawmidi->private_data = uart;
870 if (rmidi)
871 *rmidi = rrawmidi;
872 return 0;
875 static int __init snd_serial_probe(struct platform_device *devptr)
877 struct snd_card *card;
878 snd_uart16550_t *uart;
879 int err;
880 int dev = devptr->id;
882 switch (adaptor[dev]) {
883 case SNDRV_SERIAL_SOUNDCANVAS:
884 ins[dev] = 1;
885 break;
886 case SNDRV_SERIAL_MS124T:
887 case SNDRV_SERIAL_MS124W_SA:
888 outs[dev] = 1;
889 ins[dev] = 1;
890 break;
891 case SNDRV_SERIAL_MS124W_MB:
892 outs[dev] = 16;
893 ins[dev] = 1;
894 break;
895 case SNDRV_SERIAL_GENERIC:
896 break;
897 default:
898 snd_printk("Adaptor type is out of range 0-%d (%d)\n",
899 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
900 return -ENODEV;
903 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
904 snd_printk("Count of outputs is out of range 1-%d (%d)\n",
905 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
906 return -ENODEV;
909 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
910 snd_printk("Count of inputs is out of range 1-%d (%d)\n",
911 SNDRV_SERIAL_MAX_INS, ins[dev]);
912 return -ENODEV;
915 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
916 if (card == NULL)
917 return -ENOMEM;
919 strcpy(card->driver, "Serial");
920 strcpy(card->shortname, "Serial MIDI (UART16550A)");
922 if ((err = snd_uart16550_create(card,
923 port[dev],
924 irq[dev],
925 speed[dev],
926 base[dev],
927 adaptor[dev],
928 droponfull[dev],
929 &uart)) < 0)
930 goto _err;
932 if ((err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi)) < 0)
933 goto _err;
935 sprintf(card->longname, "%s at 0x%lx, irq %d speed %d div %d outs %d ins %d adaptor %s droponfull %d",
936 card->shortname,
937 uart->base,
938 uart->irq,
939 uart->speed,
940 (int)uart->divisor,
941 outs[dev],
942 ins[dev],
943 adaptor_names[uart->adaptor],
944 uart->drop_on_full);
946 snd_card_set_dev(card, &devptr->dev);
948 if ((err = snd_card_register(card)) < 0)
949 goto _err;
951 platform_set_drvdata(devptr, card);
952 return 0;
954 _err:
955 snd_card_free(card);
956 return err;
959 static int snd_serial_remove(struct platform_device *devptr)
961 snd_card_free(platform_get_drvdata(devptr));
962 platform_set_drvdata(devptr, NULL);
963 return 0;
966 #define SND_SERIAL_DRIVER "snd_serial_u16550"
968 static struct platform_driver snd_serial_driver = {
969 .probe = snd_serial_probe,
970 .remove = snd_serial_remove,
971 .driver = {
972 .name = SND_SERIAL_DRIVER
976 static void __init_or_module snd_serial_unregister_all(void)
978 int i;
980 for (i = 0; i < ARRAY_SIZE(devices); ++i)
981 platform_device_unregister(devices[i]);
982 platform_driver_unregister(&snd_serial_driver);
985 static int __init alsa_card_serial_init(void)
987 int i, cards, err;
989 if ((err = platform_driver_register(&snd_serial_driver)) < 0)
990 return err;
992 cards = 0;
993 for (i = 0; i < SNDRV_CARDS; i++) {
994 struct platform_device *device;
995 if (! enable[i])
996 continue;
997 device = platform_device_register_simple(SND_SERIAL_DRIVER,
998 i, NULL, 0);
999 if (IS_ERR(device)) {
1000 err = PTR_ERR(device);
1001 goto errout;
1003 devices[i] = device;
1004 cards++;
1006 if (! cards) {
1007 #ifdef MODULE
1008 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
1009 #endif
1010 err = -ENODEV;
1011 goto errout;
1013 return 0;
1015 errout:
1016 snd_serial_unregister_all();
1017 return err;
1020 static void __exit alsa_card_serial_exit(void)
1022 snd_serial_unregister_all();
1025 module_init(alsa_card_serial_init)
1026 module_exit(alsa_card_serial_exit)