1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
5 * Isaku Yamahata <yamahata@private.email.ne.jp>,
6 * George Hansper <ghansper@apana.org.au>,
9 * This code is based on the code from ALSA 0.5.9, but heavily rewritten.
11 * Sat Mar 31 17:27:57 PST 2001 tim.mann@compaq.com
12 * Added support for the Midiator MS-124T and for the MS-124W in
13 * Single Addressed (S/A) or Multiple Burst (M/B) mode, with
14 * power derived either parasitically from the serial port or
15 * from a separate power supply.
17 * More documentation can be found in serial-u16550.txt.
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/ioport.h>
26 #include <linux/module.h>
28 #include <sound/core.h>
29 #include <sound/rawmidi.h>
30 #include <sound/initval.h>
32 #include <linux/serial_reg.h>
33 #include <linux/jiffies.h>
35 MODULE_DESCRIPTION("MIDI serial u16550");
36 MODULE_LICENSE("GPL");
38 #define SNDRV_SERIAL_SOUNDCANVAS 0 /* Roland Soundcanvas; F5 NN selects part */
39 #define SNDRV_SERIAL_MS124T 1 /* Midiator MS-124T */
40 #define SNDRV_SERIAL_MS124W_SA 2 /* Midiator MS-124W in S/A mode */
41 #define SNDRV_SERIAL_MS124W_MB 3 /* Midiator MS-124W in M/B mode */
42 #define SNDRV_SERIAL_GENERIC 4 /* Generic Interface */
43 #define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC
44 static const char * const adaptor_names[] = {
52 #define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */
53 #define SNDRV_SERIAL_DROPBUFF 1 /* Non-blocking discard operation */
55 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
56 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
57 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
58 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */
59 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,4,5,7,9,10,11,14,15 */
60 static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */
61 static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */
62 static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
63 static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
64 static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
65 static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
67 module_param_array(index, int, NULL, 0444);
68 MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
69 module_param_array(id, charp, NULL, 0444);
70 MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
71 module_param_array(enable, bool, NULL, 0444);
72 MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
73 module_param_hw_array(port, long, ioport, NULL, 0444);
74 MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
75 module_param_hw_array(irq, int, irq, NULL, 0444);
76 MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
77 module_param_array(speed, int, NULL, 0444);
78 MODULE_PARM_DESC(speed, "Speed in bauds.");
79 module_param_array(base, int, NULL, 0444);
80 MODULE_PARM_DESC(base, "Base for divisor in bauds.");
81 module_param_array(outs, int, NULL, 0444);
82 MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
83 module_param_array(ins, int, NULL, 0444);
84 MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
85 module_param_array(droponfull, bool, NULL, 0444);
86 MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
88 module_param_array(adaptor, int, NULL, 0444);
89 MODULE_PARM_DESC(adaptor, "Type of adaptor.");
91 /*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/ /* Address outs as 0-3 instead of bitmap */
93 #define SNDRV_SERIAL_MAX_OUTS 16 /* max 64, min 16 */
94 #define SNDRV_SERIAL_MAX_INS 16 /* max 64, min 16 */
96 #define TX_BUFF_SIZE (1<<15) /* Must be 2^n */
97 #define TX_BUFF_MASK (TX_BUFF_SIZE - 1)
99 #define SERIAL_MODE_NOT_OPENED (0)
100 #define SERIAL_MODE_INPUT_OPEN (1 << 0)
101 #define SERIAL_MODE_OUTPUT_OPEN (1 << 1)
102 #define SERIAL_MODE_INPUT_TRIGGERED (1 << 2)
103 #define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3)
105 struct snd_uart16550 {
106 struct snd_card *card;
107 struct snd_rawmidi *rmidi;
108 struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS];
109 struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS];
111 int filemode; /* open status of file */
113 spinlock_t open_lock;
118 struct resource *res_base;
121 unsigned int speed_base;
122 unsigned char divisor;
124 unsigned char old_divisor_lsb;
125 unsigned char old_divisor_msb;
126 unsigned char old_line_ctrl_reg;
128 /* parameter for using of write loop */
129 short int fifo_limit; /* used in uart16550 */
130 short int fifo_count; /* used in uart16550 */
132 /* type of adaptor */
137 unsigned char rstatus;
141 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
143 /* write buffer and its writing/reading position */
144 unsigned char tx_buff[TX_BUFF_SIZE];
151 unsigned int timer_running:1;
152 struct timer_list buffer_timer;
156 static struct platform_device *devices[SNDRV_CARDS];
158 static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
160 if (!uart->timer_running) {
161 /* timer 38600bps * 10bit * 16byte */
162 mod_timer(&uart->buffer_timer, jiffies + (HZ + 255) / 256);
163 uart->timer_running = 1;
167 static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
169 if (uart->timer_running) {
170 del_timer(&uart->buffer_timer);
171 uart->timer_running = 0;
175 /* This macro is only used in snd_uart16550_io_loop */
176 static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
178 unsigned short buff_out = uart->buff_out;
179 if (uart->buff_in_count > 0) {
180 outb(uart->tx_buff[buff_out], uart->base + UART_TX);
183 buff_out &= TX_BUFF_MASK;
184 uart->buff_out = buff_out;
185 uart->buff_in_count--;
189 /* This loop should be called with interrupts disabled
190 * We don't want to interrupt this,
191 * as we're already handling an interrupt
193 static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
195 unsigned char c, status;
198 /* recall previous stream */
199 substream = uart->prev_in;
202 while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
203 /* while receive data ready */
204 c = inb(uart->base + UART_RX);
206 /* keep track of last status byte */
210 /* handle stream switch */
211 if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
212 if (uart->rstatus == 0xf5) {
213 if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
216 /* prevent future bytes from being
217 interpreted as streams */
219 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN)
220 && uart->midi_input[substream])
221 snd_rawmidi_receive(uart->midi_input[substream],
223 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) &&
224 uart->midi_input[substream])
225 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
227 if (status & UART_LSR_OE)
228 snd_printk(KERN_WARNING
229 "%s: Overrun on device at 0x%lx\n",
230 uart->rmidi->name, uart->base);
233 /* remember the last stream */
234 uart->prev_in = substream;
236 /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
237 buffer is never filled. */
238 /* Check write status */
239 if (status & UART_LSR_THRE)
240 uart->fifo_count = 0;
241 if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
242 || uart->adaptor == SNDRV_SERIAL_GENERIC) {
243 /* Can't use FIFO, must send only when CTS is true */
244 status = inb(uart->base + UART_MSR);
245 while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
246 uart->buff_in_count > 0) {
247 snd_uart16550_buffer_output(uart);
248 status = inb(uart->base + UART_MSR);
252 while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
253 && uart->buff_in_count > 0) /* Do we want to? */
254 snd_uart16550_buffer_output(uart);
256 if (uart->irq < 0 && uart->buff_in_count > 0)
257 snd_uart16550_add_timer(uart);
260 /* NOTES ON SERVICING INTERUPTS
261 * ---------------------------
262 * After receiving a interrupt, it is important to indicate to the UART that
263 * this has been done.
264 * For a Rx interrupt, this is done by reading the received byte.
265 * For a Tx interrupt this is done by either:
268 * It is particularly important to read the IIR if a Tx interrupt is received
269 * when there is no data in tx_buff[], as in this case there no other
270 * indication that the interrupt has been serviced, and it remains outstanding
271 * indefinitely. This has the curious side effect that and no further interrupts
272 * will be generated from this device AT ALL!!.
273 * It is also desirable to clear outstanding interrupts when the device is
277 * Note that some devices need OUT2 to be set before they will generate
278 * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
280 static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
282 struct snd_uart16550 *uart;
285 spin_lock(&uart->open_lock);
286 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
287 spin_unlock(&uart->open_lock);
290 /* indicate to the UART that the interrupt has been serviced */
291 inb(uart->base + UART_IIR);
292 snd_uart16550_io_loop(uart);
293 spin_unlock(&uart->open_lock);
297 /* When the polling mode, this function calls snd_uart16550_io_loop. */
298 static void snd_uart16550_buffer_timer(struct timer_list *t)
301 struct snd_uart16550 *uart;
303 uart = from_timer(uart, t, buffer_timer);
304 spin_lock_irqsave(&uart->open_lock, flags);
305 snd_uart16550_del_timer(uart);
306 snd_uart16550_io_loop(uart);
307 spin_unlock_irqrestore(&uart->open_lock, flags);
311 * this method probes, if an uart sits on given port
313 * return negative error if not found
315 static int snd_uart16550_detect(struct snd_uart16550 *uart)
317 unsigned long io_base = uart->base;
321 /* Do some vague tests for the presence of the uart */
322 if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
323 return -ENODEV; /* Not configured */
326 uart->res_base = request_region(io_base, 8, "Serial MIDI");
327 if (uart->res_base == NULL) {
328 snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
332 /* uart detected unless one of the following tests should fail */
334 /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
335 outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
336 c = inb(io_base + UART_IER);
337 /* The top four bits of the IER should always == 0 */
341 outb(0xaa, io_base + UART_SCR);
342 /* Write arbitrary data into the scratch reg */
343 c = inb(io_base + UART_SCR);
344 /* If it comes back, it's OK */
348 outb(0x55, io_base + UART_SCR);
349 /* Write arbitrary data into the scratch reg */
350 c = inb(io_base + UART_SCR);
351 /* If it comes back, it's OK */
358 static void snd_uart16550_do_open(struct snd_uart16550 * uart)
362 /* Initialize basic variables */
363 uart->buff_in_count = 0;
366 uart->fifo_limit = 1;
367 uart->fifo_count = 0;
368 uart->timer_running = 0;
370 outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */
371 | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */
372 | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */
373 | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */
374 /* NOTE: interrupt generated after T=(time)4-bytes
375 * if less than UART_FCR_TRIGGER bytes received
377 ,uart->base + UART_FCR); /* FIFO Control Register */
379 if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
380 uart->fifo_limit = 16;
381 if (uart->divisor != 0) {
382 uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
383 outb(UART_LCR_DLAB /* Divisor latch access bit */
384 ,uart->base + UART_LCR); /* Line Control Register */
385 uart->old_divisor_lsb = inb(uart->base + UART_DLL);
386 uart->old_divisor_msb = inb(uart->base + UART_DLM);
389 ,uart->base + UART_DLL); /* Divisor Latch Low */
391 ,uart->base + UART_DLM); /* Divisor Latch High */
392 /* DLAB is reset to 0 in next outb() */
394 /* Set serial parameters (parity off, etc) */
395 outb(UART_LCR_WLEN8 /* 8 data-bits */
399 ,uart->base + UART_LCR); /* Line Control Register */
401 switch (uart->adaptor) {
403 outb(UART_MCR_RTS /* Set Request-To-Send line active */
404 | UART_MCR_DTR /* Set Data-Terminal-Ready line active */
405 | UART_MCR_OUT2 /* Set OUT2 - not always required, but when
406 * it is, it is ESSENTIAL for enabling interrupts
408 ,uart->base + UART_MCR); /* Modem Control Register */
410 case SNDRV_SERIAL_MS124W_SA:
411 case SNDRV_SERIAL_MS124W_MB:
412 /* MS-124W can draw power from RTS and DTR if they
413 are in opposite states. */
414 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
415 uart->base + UART_MCR);
417 case SNDRV_SERIAL_MS124T:
418 /* MS-124T can draw power from RTS and/or DTR (preferably
419 both) if they are both asserted. */
420 outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
421 uart->base + UART_MCR);
426 byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
427 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
429 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
430 byte = UART_IER_RDI /* Enable Receiver data interrupt */
431 | UART_IER_MSI /* Enable Modem status interrupt */
433 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
434 byte = UART_IER_RDI /* Enable Receiver data interrupt */
435 | UART_IER_MSI /* Enable Modem status interrupt */
436 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
439 byte = UART_IER_RDI /* Enable Receiver data interrupt */
440 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
443 outb(byte, uart->base + UART_IER); /* Interrupt enable Register */
445 inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */
446 inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */
447 inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */
450 static void snd_uart16550_do_close(struct snd_uart16550 * uart)
453 snd_uart16550_del_timer(uart);
455 /* NOTE: may need to disable interrupts before de-registering out handler.
456 * For now, the consequences are harmless.
459 outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */
460 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
461 ,uart->base + UART_IER); /* Interrupt enable Register */
463 switch (uart->adaptor) {
465 outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */
466 |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */
467 |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */
468 ,uart->base + UART_MCR); /* Modem Control Register */
470 case SNDRV_SERIAL_MS124W_SA:
471 case SNDRV_SERIAL_MS124W_MB:
472 /* MS-124W can draw power from RTS and DTR if they
473 are in opposite states; leave it powered. */
474 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
475 uart->base + UART_MCR);
477 case SNDRV_SERIAL_MS124T:
478 /* MS-124T can draw power from RTS and/or DTR (preferably
479 both) if they are both asserted; leave it powered. */
480 outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
481 uart->base + UART_MCR);
485 inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
487 /* Restore old divisor */
488 if (uart->divisor != 0) {
489 outb(UART_LCR_DLAB /* Divisor latch access bit */
490 ,uart->base + UART_LCR); /* Line Control Register */
491 outb(uart->old_divisor_lsb
492 ,uart->base + UART_DLL); /* Divisor Latch Low */
493 outb(uart->old_divisor_msb
494 ,uart->base + UART_DLM); /* Divisor Latch High */
495 /* Restore old LCR (data bits, stop bits, parity, DLAB) */
496 outb(uart->old_line_ctrl_reg
497 ,uart->base + UART_LCR); /* Line Control Register */
501 static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
504 struct snd_uart16550 *uart = substream->rmidi->private_data;
506 spin_lock_irqsave(&uart->open_lock, flags);
507 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
508 snd_uart16550_do_open(uart);
509 uart->filemode |= SERIAL_MODE_INPUT_OPEN;
510 uart->midi_input[substream->number] = substream;
511 spin_unlock_irqrestore(&uart->open_lock, flags);
515 static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
518 struct snd_uart16550 *uart = substream->rmidi->private_data;
520 spin_lock_irqsave(&uart->open_lock, flags);
521 uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
522 uart->midi_input[substream->number] = NULL;
523 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
524 snd_uart16550_do_close(uart);
525 spin_unlock_irqrestore(&uart->open_lock, flags);
529 static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
533 struct snd_uart16550 *uart = substream->rmidi->private_data;
535 spin_lock_irqsave(&uart->open_lock, flags);
537 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
539 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
540 spin_unlock_irqrestore(&uart->open_lock, flags);
543 static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
546 struct snd_uart16550 *uart = substream->rmidi->private_data;
548 spin_lock_irqsave(&uart->open_lock, flags);
549 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
550 snd_uart16550_do_open(uart);
551 uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
552 uart->midi_output[substream->number] = substream;
553 spin_unlock_irqrestore(&uart->open_lock, flags);
557 static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
560 struct snd_uart16550 *uart = substream->rmidi->private_data;
562 spin_lock_irqsave(&uart->open_lock, flags);
563 uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
564 uart->midi_output[substream->number] = NULL;
565 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
566 snd_uart16550_do_close(uart);
567 spin_unlock_irqrestore(&uart->open_lock, flags);
571 static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
574 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
580 static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
583 unsigned short buff_in = uart->buff_in;
584 if (uart->buff_in_count < TX_BUFF_SIZE) {
585 uart->tx_buff[buff_in] = byte;
587 buff_in &= TX_BUFF_MASK;
588 uart->buff_in = buff_in;
589 uart->buff_in_count++;
590 if (uart->irq < 0) /* polling mode */
591 snd_uart16550_add_timer(uart);
597 static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
598 struct snd_rawmidi_substream *substream,
599 unsigned char midi_byte)
601 if (uart->buff_in_count == 0 /* Buffer empty? */
602 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
603 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
604 (uart->fifo_count == 0 /* FIFO empty? */
605 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
607 /* Tx Buffer Empty - try to write immediately */
608 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
609 /* Transmitter holding register (and Tx FIFO) empty */
610 uart->fifo_count = 1;
611 outb(midi_byte, uart->base + UART_TX);
613 if (uart->fifo_count < uart->fifo_limit) {
615 outb(midi_byte, uart->base + UART_TX);
617 /* Cannot write (buffer empty) -
618 * put char in buffer */
619 snd_uart16550_write_buffer(uart, midi_byte);
623 if (!snd_uart16550_write_buffer(uart, midi_byte)) {
624 snd_printk(KERN_WARNING
625 "%s: Buffer overrun on device at 0x%lx\n",
626 uart->rmidi->name, uart->base);
634 static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
637 unsigned char midi_byte, addr_byte;
638 struct snd_uart16550 *uart = substream->rmidi->private_data;
640 static unsigned long lasttime = 0;
642 /* Interrupts are disabled during the updating of the tx_buff,
643 * since it is 'bad' to have two processes updating the same
644 * variables (ie buff_in & buff_out)
647 spin_lock_irqsave(&uart->open_lock, flags);
649 if (uart->irq < 0) /* polling */
650 snd_uart16550_io_loop(uart);
652 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
655 /* in this mode we need two bytes of space */
656 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
658 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
660 #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
661 /* select exactly one of the four ports */
662 addr_byte = (1 << (substream->number + 4)) | 0x08;
664 /* select any combination of the four ports */
665 addr_byte = (substream->number << 4) | 0x08;
667 if (addr_byte == 0x08)
670 snd_uart16550_output_byte(uart, substream, addr_byte);
672 snd_uart16550_output_byte(uart, substream, midi_byte);
676 while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
677 /* Also send F5 after 3 seconds with no data
678 * to handle device disconnect */
680 (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
681 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
682 (uart->prev_out != substream->number ||
683 time_after(jiffies, lasttime + 3*HZ))) {
685 if (snd_uart16550_buffer_can_write(uart, 3)) {
686 /* Roland Soundcanvas part selection */
687 /* If this substream of the data is
688 * different previous substream
689 * in this uart, send the change part
692 uart->prev_out = substream->number;
694 snd_uart16550_output_byte(uart, substream,
697 snd_uart16550_output_byte(uart, substream,
699 /* If midi_byte is a data byte,
700 * send the previous status byte */
701 if (midi_byte < 0x80 &&
702 uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
703 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
704 } else if (!uart->drop_on_full)
710 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
711 !uart->drop_on_full )
714 if (midi_byte >= 0x80 && midi_byte < 0xf0)
715 uart->prev_status[uart->prev_out] = midi_byte;
718 snd_rawmidi_transmit_ack( substream, 1 );
722 spin_unlock_irqrestore(&uart->open_lock, flags);
725 static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
729 struct snd_uart16550 *uart = substream->rmidi->private_data;
731 spin_lock_irqsave(&uart->open_lock, flags);
733 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
735 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
736 spin_unlock_irqrestore(&uart->open_lock, flags);
738 snd_uart16550_output_write(substream);
741 static const struct snd_rawmidi_ops snd_uart16550_output =
743 .open = snd_uart16550_output_open,
744 .close = snd_uart16550_output_close,
745 .trigger = snd_uart16550_output_trigger,
748 static const struct snd_rawmidi_ops snd_uart16550_input =
750 .open = snd_uart16550_input_open,
751 .close = snd_uart16550_input_close,
752 .trigger = snd_uart16550_input_trigger,
755 static int snd_uart16550_free(struct snd_uart16550 *uart)
758 free_irq(uart->irq, uart);
759 release_and_free_resource(uart->res_base);
764 static int snd_uart16550_dev_free(struct snd_device *device)
766 struct snd_uart16550 *uart = device->device_data;
767 return snd_uart16550_free(uart);
770 static int snd_uart16550_create(struct snd_card *card,
771 unsigned long iobase,
777 struct snd_uart16550 **ruart)
779 static const struct snd_device_ops ops = {
780 .dev_free = snd_uart16550_dev_free,
782 struct snd_uart16550 *uart;
786 if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
788 uart->adaptor = adaptor;
790 spin_lock_init(&uart->open_lock);
793 uart->drop_on_full = droponfull;
795 if ((err = snd_uart16550_detect(uart)) <= 0) {
796 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
797 snd_uart16550_free(uart);
801 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
802 if (request_irq(irq, snd_uart16550_interrupt,
803 0, "Serial MIDI", uart)) {
804 snd_printk(KERN_WARNING
805 "irq %d busy. Using Polling.\n", irq);
810 uart->divisor = base / speed;
811 uart->speed = base / (unsigned int)uart->divisor;
812 uart->speed_base = base;
816 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
817 timer_setup(&uart->buffer_timer, snd_uart16550_buffer_timer, 0);
818 uart->timer_running = 0;
820 /* Register device */
821 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
822 snd_uart16550_free(uart);
826 switch (uart->adaptor) {
827 case SNDRV_SERIAL_MS124W_SA:
828 case SNDRV_SERIAL_MS124W_MB:
829 /* MS-124W can draw power from RTS and DTR if they
830 are in opposite states. */
831 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
833 case SNDRV_SERIAL_MS124T:
834 /* MS-124T can draw power from RTS and/or DTR (preferably
835 both) if they are asserted. */
836 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
848 static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
850 struct snd_rawmidi_substream *substream;
852 list_for_each_entry(substream, &stream->substreams, list) {
853 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
857 static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
859 struct snd_rawmidi **rmidi)
861 struct snd_rawmidi *rrawmidi;
864 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
865 outs, ins, &rrawmidi);
868 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
869 &snd_uart16550_input);
870 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
871 &snd_uart16550_output);
872 strcpy(rrawmidi->name, "Serial MIDI");
873 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
874 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
875 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
876 SNDRV_RAWMIDI_INFO_INPUT |
877 SNDRV_RAWMIDI_INFO_DUPLEX;
878 rrawmidi->private_data = uart;
884 static int snd_serial_probe(struct platform_device *devptr)
886 struct snd_card *card;
887 struct snd_uart16550 *uart;
889 int dev = devptr->id;
891 switch (adaptor[dev]) {
892 case SNDRV_SERIAL_SOUNDCANVAS:
895 case SNDRV_SERIAL_MS124T:
896 case SNDRV_SERIAL_MS124W_SA:
900 case SNDRV_SERIAL_MS124W_MB:
904 case SNDRV_SERIAL_GENERIC:
908 "Adaptor type is out of range 0-%d (%d)\n",
909 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
913 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
915 "Count of outputs is out of range 1-%d (%d)\n",
916 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
920 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
922 "Count of inputs is out of range 1-%d (%d)\n",
923 SNDRV_SERIAL_MAX_INS, ins[dev]);
927 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
932 strcpy(card->driver, "Serial");
933 strcpy(card->shortname, "Serial MIDI (UART16550A)");
935 if ((err = snd_uart16550_create(card,
945 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
949 sprintf(card->longname, "%s [%s] at %#lx, irq %d",
951 adaptor_names[uart->adaptor],
955 if ((err = snd_card_register(card)) < 0)
958 platform_set_drvdata(devptr, card);
966 static int snd_serial_remove(struct platform_device *devptr)
968 snd_card_free(platform_get_drvdata(devptr));
972 #define SND_SERIAL_DRIVER "snd_serial_u16550"
974 static struct platform_driver snd_serial_driver = {
975 .probe = snd_serial_probe,
976 .remove = snd_serial_remove,
978 .name = SND_SERIAL_DRIVER,
982 static void snd_serial_unregister_all(void)
986 for (i = 0; i < ARRAY_SIZE(devices); ++i)
987 platform_device_unregister(devices[i]);
988 platform_driver_unregister(&snd_serial_driver);
991 static int __init alsa_card_serial_init(void)
995 if ((err = platform_driver_register(&snd_serial_driver)) < 0)
999 for (i = 0; i < SNDRV_CARDS; i++) {
1000 struct platform_device *device;
1003 device = platform_device_register_simple(SND_SERIAL_DRIVER,
1007 if (!platform_get_drvdata(device)) {
1008 platform_device_unregister(device);
1011 devices[i] = device;
1016 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
1018 snd_serial_unregister_all();
1024 static void __exit alsa_card_serial_exit(void)
1026 snd_serial_unregister_all();
1029 module_init(alsa_card_serial_init)
1030 module_exit(alsa_card_serial_exit)