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
;
120 unsigned int speed_base
;
121 unsigned char divisor
;
123 unsigned char old_divisor_lsb
;
124 unsigned char old_divisor_msb
;
125 unsigned char old_line_ctrl_reg
;
127 /* parameter for using of write loop */
128 short int fifo_limit
; /* used in uart16550 */
129 short int fifo_count
; /* used in uart16550 */
131 /* type of adaptor */
136 unsigned char rstatus
;
140 unsigned char prev_status
[SNDRV_SERIAL_MAX_OUTS
];
142 /* write buffer and its writing/reading position */
143 unsigned char tx_buff
[TX_BUFF_SIZE
];
150 unsigned int timer_running
:1;
151 struct timer_list buffer_timer
;
155 static struct platform_device
*devices
[SNDRV_CARDS
];
157 static inline void snd_uart16550_add_timer(struct snd_uart16550
*uart
)
159 if (!uart
->timer_running
) {
160 /* timer 38600bps * 10bit * 16byte */
161 mod_timer(&uart
->buffer_timer
, jiffies
+ (HZ
+ 255) / 256);
162 uart
->timer_running
= 1;
166 static inline void snd_uart16550_del_timer(struct snd_uart16550
*uart
)
168 if (uart
->timer_running
) {
169 del_timer(&uart
->buffer_timer
);
170 uart
->timer_running
= 0;
174 /* This macro is only used in snd_uart16550_io_loop */
175 static inline void snd_uart16550_buffer_output(struct snd_uart16550
*uart
)
177 unsigned short buff_out
= uart
->buff_out
;
178 if (uart
->buff_in_count
> 0) {
179 outb(uart
->tx_buff
[buff_out
], uart
->base
+ UART_TX
);
182 buff_out
&= TX_BUFF_MASK
;
183 uart
->buff_out
= buff_out
;
184 uart
->buff_in_count
--;
188 /* This loop should be called with interrupts disabled
189 * We don't want to interrupt this,
190 * as we're already handling an interrupt
192 static void snd_uart16550_io_loop(struct snd_uart16550
* uart
)
194 unsigned char c
, status
;
197 /* recall previous stream */
198 substream
= uart
->prev_in
;
201 while ((status
= inb(uart
->base
+ UART_LSR
)) & UART_LSR_DR
) {
202 /* while receive data ready */
203 c
= inb(uart
->base
+ UART_RX
);
205 /* keep track of last status byte */
209 /* handle stream switch */
210 if (uart
->adaptor
== SNDRV_SERIAL_GENERIC
) {
211 if (uart
->rstatus
== 0xf5) {
212 if (c
<= SNDRV_SERIAL_MAX_INS
&& c
> 0)
215 /* prevent future bytes from being
216 interpreted as streams */
218 } else if ((uart
->filemode
& SERIAL_MODE_INPUT_OPEN
)
219 && uart
->midi_input
[substream
])
220 snd_rawmidi_receive(uart
->midi_input
[substream
],
222 } else if ((uart
->filemode
& SERIAL_MODE_INPUT_OPEN
) &&
223 uart
->midi_input
[substream
])
224 snd_rawmidi_receive(uart
->midi_input
[substream
], &c
, 1);
226 if (status
& UART_LSR_OE
)
227 dev_warn(uart
->card
->dev
,
228 "%s: Overrun on device at 0x%lx\n",
229 uart
->rmidi
->name
, uart
->base
);
232 /* remember the last stream */
233 uart
->prev_in
= substream
;
235 /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
236 buffer is never filled. */
237 /* Check write status */
238 if (status
& UART_LSR_THRE
)
239 uart
->fifo_count
= 0;
240 if (uart
->adaptor
== SNDRV_SERIAL_MS124W_SA
241 || uart
->adaptor
== SNDRV_SERIAL_GENERIC
) {
242 /* Can't use FIFO, must send only when CTS is true */
243 status
= inb(uart
->base
+ UART_MSR
);
244 while (uart
->fifo_count
== 0 && (status
& UART_MSR_CTS
) &&
245 uart
->buff_in_count
> 0) {
246 snd_uart16550_buffer_output(uart
);
247 status
= inb(uart
->base
+ UART_MSR
);
251 while (uart
->fifo_count
< uart
->fifo_limit
/* Can we write ? */
252 && uart
->buff_in_count
> 0) /* Do we want to? */
253 snd_uart16550_buffer_output(uart
);
255 if (uart
->irq
< 0 && uart
->buff_in_count
> 0)
256 snd_uart16550_add_timer(uart
);
259 /* NOTES ON SERVICING INTERUPTS
260 * ---------------------------
261 * After receiving a interrupt, it is important to indicate to the UART that
262 * this has been done.
263 * For a Rx interrupt, this is done by reading the received byte.
264 * For a Tx interrupt this is done by either:
267 * It is particularly important to read the IIR if a Tx interrupt is received
268 * when there is no data in tx_buff[], as in this case there no other
269 * indication that the interrupt has been serviced, and it remains outstanding
270 * indefinitely. This has the curious side effect that and no further interrupts
271 * will be generated from this device AT ALL!!.
272 * It is also desirable to clear outstanding interrupts when the device is
276 * Note that some devices need OUT2 to be set before they will generate
277 * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
279 static irqreturn_t
snd_uart16550_interrupt(int irq
, void *dev_id
)
281 struct snd_uart16550
*uart
;
284 spin_lock(&uart
->open_lock
);
285 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
) {
286 spin_unlock(&uart
->open_lock
);
289 /* indicate to the UART that the interrupt has been serviced */
290 inb(uart
->base
+ UART_IIR
);
291 snd_uart16550_io_loop(uart
);
292 spin_unlock(&uart
->open_lock
);
296 /* When the polling mode, this function calls snd_uart16550_io_loop. */
297 static void snd_uart16550_buffer_timer(struct timer_list
*t
)
300 struct snd_uart16550
*uart
;
302 uart
= from_timer(uart
, t
, buffer_timer
);
303 spin_lock_irqsave(&uart
->open_lock
, flags
);
304 snd_uart16550_del_timer(uart
);
305 snd_uart16550_io_loop(uart
);
306 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
310 * this method probes, if an uart sits on given port
312 * return negative error if not found
314 static int snd_uart16550_detect(struct snd_uart16550
*uart
)
316 unsigned long io_base
= uart
->base
;
320 /* Do some vague tests for the presence of the uart */
321 if (io_base
== 0 || io_base
== SNDRV_AUTO_PORT
) {
322 return -ENODEV
; /* Not configured */
325 if (!devm_request_region(uart
->card
->dev
, io_base
, 8, "Serial MIDI")) {
326 dev_err(uart
->card
->dev
,
327 "u16550: can't grab port 0x%lx\n", io_base
);
331 /* uart detected unless one of the following tests should fail */
333 /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
334 outb(UART_LCR_WLEN8
, io_base
+ UART_LCR
); /* Line Control Register */
335 c
= inb(io_base
+ UART_IER
);
336 /* The top four bits of the IER should always == 0 */
340 outb(0xaa, io_base
+ UART_SCR
);
341 /* Write arbitrary data into the scratch reg */
342 c
= inb(io_base
+ UART_SCR
);
343 /* If it comes back, it's OK */
347 outb(0x55, io_base
+ UART_SCR
);
348 /* Write arbitrary data into the scratch reg */
349 c
= inb(io_base
+ UART_SCR
);
350 /* If it comes back, it's OK */
357 static void snd_uart16550_do_open(struct snd_uart16550
* uart
)
361 /* Initialize basic variables */
362 uart
->buff_in_count
= 0;
365 uart
->fifo_limit
= 1;
366 uart
->fifo_count
= 0;
367 uart
->timer_running
= 0;
369 outb(UART_FCR_ENABLE_FIFO
/* Enable FIFO's (if available) */
370 | UART_FCR_CLEAR_RCVR
/* Clear receiver FIFO */
371 | UART_FCR_CLEAR_XMIT
/* Clear transmitter FIFO */
372 | UART_FCR_TRIGGER_4
/* Set FIFO trigger at 4-bytes */
373 /* NOTE: interrupt generated after T=(time)4-bytes
374 * if less than UART_FCR_TRIGGER bytes received
376 ,uart
->base
+ UART_FCR
); /* FIFO Control Register */
378 if ((inb(uart
->base
+ UART_IIR
) & 0xf0) == 0xc0)
379 uart
->fifo_limit
= 16;
380 if (uart
->divisor
!= 0) {
381 uart
->old_line_ctrl_reg
= inb(uart
->base
+ UART_LCR
);
382 outb(UART_LCR_DLAB
/* Divisor latch access bit */
383 ,uart
->base
+ UART_LCR
); /* Line Control Register */
384 uart
->old_divisor_lsb
= inb(uart
->base
+ UART_DLL
);
385 uart
->old_divisor_msb
= inb(uart
->base
+ UART_DLM
);
388 ,uart
->base
+ UART_DLL
); /* Divisor Latch Low */
390 ,uart
->base
+ UART_DLM
); /* Divisor Latch High */
391 /* DLAB is reset to 0 in next outb() */
393 /* Set serial parameters (parity off, etc) */
394 outb(UART_LCR_WLEN8
/* 8 data-bits */
398 ,uart
->base
+ UART_LCR
); /* Line Control Register */
400 switch (uart
->adaptor
) {
402 outb(UART_MCR_RTS
/* Set Request-To-Send line active */
403 | UART_MCR_DTR
/* Set Data-Terminal-Ready line active */
404 | UART_MCR_OUT2
/* Set OUT2 - not always required, but when
405 * it is, it is ESSENTIAL for enabling interrupts
407 ,uart
->base
+ UART_MCR
); /* Modem Control Register */
409 case SNDRV_SERIAL_MS124W_SA
:
410 case SNDRV_SERIAL_MS124W_MB
:
411 /* MS-124W can draw power from RTS and DTR if they
412 are in opposite states. */
413 outb(UART_MCR_RTS
| (0&UART_MCR_DTR
) | UART_MCR_OUT2
,
414 uart
->base
+ UART_MCR
);
416 case SNDRV_SERIAL_MS124T
:
417 /* MS-124T can draw power from RTS and/or DTR (preferably
418 both) if they are both asserted. */
419 outb(UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
,
420 uart
->base
+ UART_MCR
);
425 byte
= (0 & UART_IER_RDI
) /* Disable Receiver data interrupt */
426 |(0 & UART_IER_THRI
) /* Disable Transmitter holding register empty interrupt */
428 } else if (uart
->adaptor
== SNDRV_SERIAL_MS124W_SA
) {
429 byte
= UART_IER_RDI
/* Enable Receiver data interrupt */
430 | UART_IER_MSI
/* Enable Modem status interrupt */
432 } else if (uart
->adaptor
== SNDRV_SERIAL_GENERIC
) {
433 byte
= UART_IER_RDI
/* Enable Receiver data interrupt */
434 | UART_IER_MSI
/* Enable Modem status interrupt */
435 | UART_IER_THRI
/* Enable Transmitter holding register empty interrupt */
438 byte
= UART_IER_RDI
/* Enable Receiver data interrupt */
439 | UART_IER_THRI
/* Enable Transmitter holding register empty interrupt */
442 outb(byte
, uart
->base
+ UART_IER
); /* Interrupt enable Register */
444 inb(uart
->base
+ UART_LSR
); /* Clear any pre-existing overrun indication */
445 inb(uart
->base
+ UART_IIR
); /* Clear any pre-existing transmit interrupt */
446 inb(uart
->base
+ UART_RX
); /* Clear any pre-existing receive interrupt */
449 static void snd_uart16550_do_close(struct snd_uart16550
* uart
)
452 snd_uart16550_del_timer(uart
);
454 /* NOTE: may need to disable interrupts before de-registering out handler.
455 * For now, the consequences are harmless.
458 outb((0 & UART_IER_RDI
) /* Disable Receiver data interrupt */
459 |(0 & UART_IER_THRI
) /* Disable Transmitter holding register empty interrupt */
460 ,uart
->base
+ UART_IER
); /* Interrupt enable Register */
462 switch (uart
->adaptor
) {
464 outb((0 & UART_MCR_RTS
) /* Deactivate Request-To-Send line */
465 |(0 & UART_MCR_DTR
) /* Deactivate Data-Terminal-Ready line */
466 |(0 & UART_MCR_OUT2
) /* Deactivate OUT2 */
467 ,uart
->base
+ UART_MCR
); /* Modem Control Register */
469 case SNDRV_SERIAL_MS124W_SA
:
470 case SNDRV_SERIAL_MS124W_MB
:
471 /* MS-124W can draw power from RTS and DTR if they
472 are in opposite states; leave it powered. */
473 outb(UART_MCR_RTS
| (0&UART_MCR_DTR
) | (0&UART_MCR_OUT2
),
474 uart
->base
+ UART_MCR
);
476 case SNDRV_SERIAL_MS124T
:
477 /* MS-124T can draw power from RTS and/or DTR (preferably
478 both) if they are both asserted; leave it powered. */
479 outb(UART_MCR_RTS
| UART_MCR_DTR
| (0&UART_MCR_OUT2
),
480 uart
->base
+ UART_MCR
);
484 inb(uart
->base
+ UART_IIR
); /* Clear any outstanding interrupts */
486 /* Restore old divisor */
487 if (uart
->divisor
!= 0) {
488 outb(UART_LCR_DLAB
/* Divisor latch access bit */
489 ,uart
->base
+ UART_LCR
); /* Line Control Register */
490 outb(uart
->old_divisor_lsb
491 ,uart
->base
+ UART_DLL
); /* Divisor Latch Low */
492 outb(uart
->old_divisor_msb
493 ,uart
->base
+ UART_DLM
); /* Divisor Latch High */
494 /* Restore old LCR (data bits, stop bits, parity, DLAB) */
495 outb(uart
->old_line_ctrl_reg
496 ,uart
->base
+ UART_LCR
); /* Line Control Register */
500 static int snd_uart16550_input_open(struct snd_rawmidi_substream
*substream
)
503 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
505 spin_lock_irqsave(&uart
->open_lock
, flags
);
506 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
507 snd_uart16550_do_open(uart
);
508 uart
->filemode
|= SERIAL_MODE_INPUT_OPEN
;
509 uart
->midi_input
[substream
->number
] = substream
;
510 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
514 static int snd_uart16550_input_close(struct snd_rawmidi_substream
*substream
)
517 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
519 spin_lock_irqsave(&uart
->open_lock
, flags
);
520 uart
->filemode
&= ~SERIAL_MODE_INPUT_OPEN
;
521 uart
->midi_input
[substream
->number
] = NULL
;
522 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
523 snd_uart16550_do_close(uart
);
524 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
528 static void snd_uart16550_input_trigger(struct snd_rawmidi_substream
*substream
,
532 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
534 spin_lock_irqsave(&uart
->open_lock
, flags
);
536 uart
->filemode
|= SERIAL_MODE_INPUT_TRIGGERED
;
538 uart
->filemode
&= ~SERIAL_MODE_INPUT_TRIGGERED
;
539 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
542 static int snd_uart16550_output_open(struct snd_rawmidi_substream
*substream
)
545 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
547 spin_lock_irqsave(&uart
->open_lock
, flags
);
548 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
549 snd_uart16550_do_open(uart
);
550 uart
->filemode
|= SERIAL_MODE_OUTPUT_OPEN
;
551 uart
->midi_output
[substream
->number
] = substream
;
552 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
556 static int snd_uart16550_output_close(struct snd_rawmidi_substream
*substream
)
559 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
561 spin_lock_irqsave(&uart
->open_lock
, flags
);
562 uart
->filemode
&= ~SERIAL_MODE_OUTPUT_OPEN
;
563 uart
->midi_output
[substream
->number
] = NULL
;
564 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
565 snd_uart16550_do_close(uart
);
566 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
570 static inline int snd_uart16550_buffer_can_write(struct snd_uart16550
*uart
,
573 if (uart
->buff_in_count
+ Num
< TX_BUFF_SIZE
)
579 static inline int snd_uart16550_write_buffer(struct snd_uart16550
*uart
,
582 unsigned short buff_in
= uart
->buff_in
;
583 if (uart
->buff_in_count
< TX_BUFF_SIZE
) {
584 uart
->tx_buff
[buff_in
] = byte
;
586 buff_in
&= TX_BUFF_MASK
;
587 uart
->buff_in
= buff_in
;
588 uart
->buff_in_count
++;
589 if (uart
->irq
< 0) /* polling mode */
590 snd_uart16550_add_timer(uart
);
596 static int snd_uart16550_output_byte(struct snd_uart16550
*uart
,
597 struct snd_rawmidi_substream
*substream
,
598 unsigned char midi_byte
)
600 if (uart
->buff_in_count
== 0 /* Buffer empty? */
601 && ((uart
->adaptor
!= SNDRV_SERIAL_MS124W_SA
&&
602 uart
->adaptor
!= SNDRV_SERIAL_GENERIC
) ||
603 (uart
->fifo_count
== 0 /* FIFO empty? */
604 && (inb(uart
->base
+ UART_MSR
) & UART_MSR_CTS
)))) { /* CTS? */
606 /* Tx Buffer Empty - try to write immediately */
607 if ((inb(uart
->base
+ UART_LSR
) & UART_LSR_THRE
) != 0) {
608 /* Transmitter holding register (and Tx FIFO) empty */
609 uart
->fifo_count
= 1;
610 outb(midi_byte
, uart
->base
+ UART_TX
);
612 if (uart
->fifo_count
< uart
->fifo_limit
) {
614 outb(midi_byte
, uart
->base
+ UART_TX
);
616 /* Cannot write (buffer empty) -
617 * put char in buffer */
618 snd_uart16550_write_buffer(uart
, midi_byte
);
622 if (!snd_uart16550_write_buffer(uart
, midi_byte
)) {
623 dev_warn(uart
->card
->dev
,
624 "%s: Buffer overrun on device at 0x%lx\n",
625 uart
->rmidi
->name
, uart
->base
);
633 static void snd_uart16550_output_write(struct snd_rawmidi_substream
*substream
)
636 unsigned char midi_byte
, addr_byte
;
637 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
639 static unsigned long lasttime
= 0;
641 /* Interrupts are disabled during the updating of the tx_buff,
642 * since it is 'bad' to have two processes updating the same
643 * variables (ie buff_in & buff_out)
646 spin_lock_irqsave(&uart
->open_lock
, flags
);
648 if (uart
->irq
< 0) /* polling */
649 snd_uart16550_io_loop(uart
);
651 if (uart
->adaptor
== SNDRV_SERIAL_MS124W_MB
) {
654 /* in this mode we need two bytes of space */
655 if (uart
->buff_in_count
> TX_BUFF_SIZE
- 2)
657 if (snd_rawmidi_transmit(substream
, &midi_byte
, 1) != 1)
659 #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
660 /* select exactly one of the four ports */
661 addr_byte
= (1 << (substream
->number
+ 4)) | 0x08;
663 /* select any combination of the four ports */
664 addr_byte
= (substream
->number
<< 4) | 0x08;
666 if (addr_byte
== 0x08)
669 snd_uart16550_output_byte(uart
, substream
, addr_byte
);
671 snd_uart16550_output_byte(uart
, substream
, midi_byte
);
675 while (snd_rawmidi_transmit_peek(substream
, &midi_byte
, 1) == 1) {
676 /* Also send F5 after 3 seconds with no data
677 * to handle device disconnect */
679 (uart
->adaptor
== SNDRV_SERIAL_SOUNDCANVAS
||
680 uart
->adaptor
== SNDRV_SERIAL_GENERIC
) &&
681 (uart
->prev_out
!= substream
->number
||
682 time_after(jiffies
, lasttime
+ 3*HZ
))) {
684 if (snd_uart16550_buffer_can_write(uart
, 3)) {
685 /* Roland Soundcanvas part selection */
686 /* If this substream of the data is
687 * different previous substream
688 * in this uart, send the change part
691 uart
->prev_out
= substream
->number
;
693 snd_uart16550_output_byte(uart
, substream
,
696 snd_uart16550_output_byte(uart
, substream
,
698 /* If midi_byte is a data byte,
699 * send the previous status byte */
700 if (midi_byte
< 0x80 &&
701 uart
->adaptor
== SNDRV_SERIAL_SOUNDCANVAS
)
702 snd_uart16550_output_byte(uart
, substream
, uart
->prev_status
[uart
->prev_out
]);
703 } else if (!uart
->drop_on_full
)
709 if (!snd_uart16550_output_byte(uart
, substream
, midi_byte
) &&
710 !uart
->drop_on_full
)
713 if (midi_byte
>= 0x80 && midi_byte
< 0xf0)
714 uart
->prev_status
[uart
->prev_out
] = midi_byte
;
717 snd_rawmidi_transmit_ack( substream
, 1 );
721 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
724 static void snd_uart16550_output_trigger(struct snd_rawmidi_substream
*substream
,
728 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
730 spin_lock_irqsave(&uart
->open_lock
, flags
);
732 uart
->filemode
|= SERIAL_MODE_OUTPUT_TRIGGERED
;
734 uart
->filemode
&= ~SERIAL_MODE_OUTPUT_TRIGGERED
;
735 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
737 snd_uart16550_output_write(substream
);
740 static const struct snd_rawmidi_ops snd_uart16550_output
=
742 .open
= snd_uart16550_output_open
,
743 .close
= snd_uart16550_output_close
,
744 .trigger
= snd_uart16550_output_trigger
,
747 static const struct snd_rawmidi_ops snd_uart16550_input
=
749 .open
= snd_uart16550_input_open
,
750 .close
= snd_uart16550_input_close
,
751 .trigger
= snd_uart16550_input_trigger
,
754 static int snd_uart16550_create(struct snd_card
*card
,
755 unsigned long iobase
,
761 struct snd_uart16550
**ruart
)
763 struct snd_uart16550
*uart
;
767 uart
= devm_kzalloc(card
->dev
, sizeof(*uart
), GFP_KERNEL
);
770 uart
->adaptor
= adaptor
;
772 spin_lock_init(&uart
->open_lock
);
775 uart
->drop_on_full
= droponfull
;
777 err
= snd_uart16550_detect(uart
);
779 dev_err(card
->dev
, "no UART detected at 0x%lx\n", iobase
);
783 if (irq
>= 0 && irq
!= SNDRV_AUTO_IRQ
) {
784 if (devm_request_irq(card
->dev
, irq
, snd_uart16550_interrupt
,
785 0, "Serial MIDI", uart
)) {
787 "irq %d busy. Using Polling.\n", irq
);
792 uart
->divisor
= base
/ speed
;
793 uart
->speed
= base
/ (unsigned int)uart
->divisor
;
794 uart
->speed_base
= base
;
798 memset(uart
->prev_status
, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS
);
799 timer_setup(&uart
->buffer_timer
, snd_uart16550_buffer_timer
, 0);
800 uart
->timer_running
= 0;
802 switch (uart
->adaptor
) {
803 case SNDRV_SERIAL_MS124W_SA
:
804 case SNDRV_SERIAL_MS124W_MB
:
805 /* MS-124W can draw power from RTS and DTR if they
806 are in opposite states. */
807 outb(UART_MCR_RTS
| (0&UART_MCR_DTR
), uart
->base
+ UART_MCR
);
809 case SNDRV_SERIAL_MS124T
:
810 /* MS-124T can draw power from RTS and/or DTR (preferably
811 both) if they are asserted. */
812 outb(UART_MCR_RTS
| UART_MCR_DTR
, uart
->base
+ UART_MCR
);
824 static void snd_uart16550_substreams(struct snd_rawmidi_str
*stream
)
826 struct snd_rawmidi_substream
*substream
;
828 list_for_each_entry(substream
, &stream
->substreams
, list
) {
829 sprintf(substream
->name
, "Serial MIDI %d", substream
->number
+ 1);
833 static int snd_uart16550_rmidi(struct snd_uart16550
*uart
, int device
,
835 struct snd_rawmidi
**rmidi
)
837 struct snd_rawmidi
*rrawmidi
;
840 err
= snd_rawmidi_new(uart
->card
, "UART Serial MIDI", device
,
841 outs
, ins
, &rrawmidi
);
844 snd_rawmidi_set_ops(rrawmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
845 &snd_uart16550_input
);
846 snd_rawmidi_set_ops(rrawmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
847 &snd_uart16550_output
);
848 strcpy(rrawmidi
->name
, "Serial MIDI");
849 snd_uart16550_substreams(&rrawmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
850 snd_uart16550_substreams(&rrawmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
851 rrawmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
852 SNDRV_RAWMIDI_INFO_INPUT
|
853 SNDRV_RAWMIDI_INFO_DUPLEX
;
854 rrawmidi
->private_data
= uart
;
860 static int snd_serial_probe(struct platform_device
*devptr
)
862 struct snd_card
*card
;
863 struct snd_uart16550
*uart
;
865 int dev
= devptr
->id
;
867 switch (adaptor
[dev
]) {
868 case SNDRV_SERIAL_SOUNDCANVAS
:
871 case SNDRV_SERIAL_MS124T
:
872 case SNDRV_SERIAL_MS124W_SA
:
876 case SNDRV_SERIAL_MS124W_MB
:
880 case SNDRV_SERIAL_GENERIC
:
883 dev_err(&devptr
->dev
,
884 "Adaptor type is out of range 0-%d (%d)\n",
885 SNDRV_SERIAL_MAX_ADAPTOR
, adaptor
[dev
]);
889 if (outs
[dev
] < 1 || outs
[dev
] > SNDRV_SERIAL_MAX_OUTS
) {
890 dev_err(&devptr
->dev
,
891 "Count of outputs is out of range 1-%d (%d)\n",
892 SNDRV_SERIAL_MAX_OUTS
, outs
[dev
]);
896 if (ins
[dev
] < 1 || ins
[dev
] > SNDRV_SERIAL_MAX_INS
) {
897 dev_err(&devptr
->dev
,
898 "Count of inputs is out of range 1-%d (%d)\n",
899 SNDRV_SERIAL_MAX_INS
, ins
[dev
]);
903 err
= snd_devm_card_new(&devptr
->dev
, index
[dev
], id
[dev
], THIS_MODULE
,
908 strcpy(card
->driver
, "Serial");
909 strcpy(card
->shortname
, "Serial MIDI (UART16550A)");
911 err
= snd_uart16550_create(card
, port
[dev
], irq
[dev
], speed
[dev
],
912 base
[dev
], adaptor
[dev
], droponfull
[dev
],
917 err
= snd_uart16550_rmidi(uart
, 0, outs
[dev
], ins
[dev
], &uart
->rmidi
);
921 sprintf(card
->longname
, "%s [%s] at %#lx, irq %d",
923 adaptor_names
[uart
->adaptor
],
927 err
= snd_card_register(card
);
931 platform_set_drvdata(devptr
, card
);
935 #define SND_SERIAL_DRIVER "snd_serial_u16550"
937 static struct platform_driver snd_serial_driver
= {
938 .probe
= snd_serial_probe
,
940 .name
= SND_SERIAL_DRIVER
,
944 static void snd_serial_unregister_all(void)
948 for (i
= 0; i
< ARRAY_SIZE(devices
); ++i
)
949 platform_device_unregister(devices
[i
]);
950 platform_driver_unregister(&snd_serial_driver
);
953 static int __init
alsa_card_serial_init(void)
957 err
= platform_driver_register(&snd_serial_driver
);
962 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
963 struct platform_device
*device
;
966 device
= platform_device_register_simple(SND_SERIAL_DRIVER
,
970 if (!platform_get_drvdata(device
)) {
971 platform_device_unregister(device
);
979 pr_err("serial midi soundcard not found or device busy\n");
981 snd_serial_unregister_all();
987 static void __exit
alsa_card_serial_exit(void)
989 snd_serial_unregister_all();
992 module_init(alsa_card_serial_init
)
993 module_exit(alsa_card_serial_exit
)