3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
4 * Isaku Yamahata <yamahata@private.email.ne.jp>,
5 * George Hansper <ghansper@apana.org.au>,
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 <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/err.h>
36 #include <linux/platform_device.h>
37 #include <linux/slab.h>
38 #include <linux/ioport.h>
39 #include <linux/moduleparam.h>
40 #include <sound/core.h>
41 #include <sound/rawmidi.h>
42 #include <sound/initval.h>
44 #include <linux/serial_reg.h>
45 #include <linux/jiffies.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
[] = {
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 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
;
133 struct resource
*res_base
;
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 */
152 unsigned char rstatus
;
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
];
166 unsigned int timer_running
:1;
167 struct timer_list buffer_timer
;
171 static struct platform_device
*devices
[SNDRV_CARDS
];
173 static inline void snd_uart16550_add_timer(struct snd_uart16550
*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(struct snd_uart16550
*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(struct snd_uart16550
*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
);
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(struct snd_uart16550
* uart
)
211 unsigned char c
, status
;
214 /* recall previous stream */
215 substream
= uart
->prev_in
;
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 */
226 /* handle stream switch */
227 if (uart
->adaptor
== SNDRV_SERIAL_GENERIC
) {
228 if (uart
->rstatus
== 0xf5) {
229 if (c
<= SNDRV_SERIAL_MAX_INS
&& c
> 0)
232 /* prevent future bytes from being
233 interpreted as streams */
235 } else if ((uart
->filemode
& SERIAL_MODE_INPUT_OPEN
)
236 && uart
->midi_input
[substream
])
237 snd_rawmidi_receive(uart
->midi_input
[substream
],
239 } else if ((uart
->filemode
& SERIAL_MODE_INPUT_OPEN
) &&
240 uart
->midi_input
[substream
])
241 snd_rawmidi_receive(uart
->midi_input
[substream
], &c
, 1);
243 if (status
& UART_LSR_OE
)
244 snd_printk(KERN_WARNING
245 "%s: Overrun on device at 0x%lx\n",
246 uart
->rmidi
->name
, uart
->base
);
249 /* remember the last stream */
250 uart
->prev_in
= substream
;
252 /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
253 buffer is never filled. */
254 /* Check write status */
255 if (status
& UART_LSR_THRE
)
256 uart
->fifo_count
= 0;
257 if (uart
->adaptor
== SNDRV_SERIAL_MS124W_SA
258 || uart
->adaptor
== SNDRV_SERIAL_GENERIC
) {
259 /* Can't use FIFO, must send only when CTS is true */
260 status
= inb(uart
->base
+ UART_MSR
);
261 while (uart
->fifo_count
== 0 && (status
& UART_MSR_CTS
) &&
262 uart
->buff_in_count
> 0) {
263 snd_uart16550_buffer_output(uart
);
264 status
= inb(uart
->base
+ UART_MSR
);
268 while (uart
->fifo_count
< uart
->fifo_limit
/* Can we write ? */
269 && uart
->buff_in_count
> 0) /* Do we want to? */
270 snd_uart16550_buffer_output(uart
);
272 if (uart
->irq
< 0 && uart
->buff_in_count
> 0)
273 snd_uart16550_add_timer(uart
);
276 /* NOTES ON SERVICING INTERUPTS
277 * ---------------------------
278 * After receiving a interrupt, it is important to indicate to the UART that
279 * this has been done.
280 * For a Rx interrupt, this is done by reading the received byte.
281 * For a Tx interrupt this is done by either:
284 * It is particularly important to read the IIR if a Tx interrupt is received
285 * when there is no data in tx_buff[], as in this case there no other
286 * indication that the interrupt has been serviced, and it remains outstanding
287 * indefinitely. This has the curious side effect that and no further interrupts
288 * will be generated from this device AT ALL!!.
289 * It is also desirable to clear outstanding interrupts when the device is
293 * Note that some devices need OUT2 to be set before they will generate
294 * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
296 static irqreturn_t
snd_uart16550_interrupt(int irq
, void *dev_id
)
298 struct snd_uart16550
*uart
;
301 spin_lock(&uart
->open_lock
);
302 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
) {
303 spin_unlock(&uart
->open_lock
);
306 /* indicate to the UART that the interrupt has been serviced */
307 inb(uart
->base
+ UART_IIR
);
308 snd_uart16550_io_loop(uart
);
309 spin_unlock(&uart
->open_lock
);
313 /* When the polling mode, this function calls snd_uart16550_io_loop. */
314 static void snd_uart16550_buffer_timer(unsigned long data
)
317 struct snd_uart16550
*uart
;
319 uart
= (struct snd_uart16550
*)data
;
320 spin_lock_irqsave(&uart
->open_lock
, flags
);
321 snd_uart16550_del_timer(uart
);
322 snd_uart16550_io_loop(uart
);
323 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
327 * this method probes, if an uart sits on given port
329 * return negative error if not found
331 static int __devinit
snd_uart16550_detect(struct snd_uart16550
*uart
)
333 unsigned long io_base
= uart
->base
;
337 /* Do some vague tests for the presence of the uart */
338 if (io_base
== 0 || io_base
== SNDRV_AUTO_PORT
) {
339 return -ENODEV
; /* Not configured */
342 uart
->res_base
= request_region(io_base
, 8, "Serial MIDI");
343 if (uart
->res_base
== NULL
) {
344 snd_printk(KERN_ERR
"u16550: can't grab port 0x%lx\n", io_base
);
348 /* uart detected unless one of the following tests should fail */
350 /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
351 outb(UART_LCR_WLEN8
, io_base
+ UART_LCR
); /* Line Control Register */
352 c
= inb(io_base
+ UART_IER
);
353 /* The top four bits of the IER should always == 0 */
357 outb(0xaa, io_base
+ UART_SCR
);
358 /* Write arbitrary data into the scratch reg */
359 c
= inb(io_base
+ UART_SCR
);
360 /* If it comes back, it's OK */
364 outb(0x55, io_base
+ UART_SCR
);
365 /* Write arbitrary data into the scratch reg */
366 c
= inb(io_base
+ UART_SCR
);
367 /* If it comes back, it's OK */
374 static void snd_uart16550_do_open(struct snd_uart16550
* uart
)
378 /* Initialize basic variables */
379 uart
->buff_in_count
= 0;
382 uart
->fifo_limit
= 1;
383 uart
->fifo_count
= 0;
384 uart
->timer_running
= 0;
386 outb(UART_FCR_ENABLE_FIFO
/* Enable FIFO's (if available) */
387 | UART_FCR_CLEAR_RCVR
/* Clear receiver FIFO */
388 | UART_FCR_CLEAR_XMIT
/* Clear transmitter FIFO */
389 | UART_FCR_TRIGGER_4
/* Set FIFO trigger at 4-bytes */
390 /* NOTE: interrupt generated after T=(time)4-bytes
391 * if less than UART_FCR_TRIGGER bytes received
393 ,uart
->base
+ UART_FCR
); /* FIFO Control Register */
395 if ((inb(uart
->base
+ UART_IIR
) & 0xf0) == 0xc0)
396 uart
->fifo_limit
= 16;
397 if (uart
->divisor
!= 0) {
398 uart
->old_line_ctrl_reg
= inb(uart
->base
+ UART_LCR
);
399 outb(UART_LCR_DLAB
/* Divisor latch access bit */
400 ,uart
->base
+ UART_LCR
); /* Line Control Register */
401 uart
->old_divisor_lsb
= inb(uart
->base
+ UART_DLL
);
402 uart
->old_divisor_msb
= inb(uart
->base
+ UART_DLM
);
405 ,uart
->base
+ UART_DLL
); /* Divisor Latch Low */
407 ,uart
->base
+ UART_DLM
); /* Divisor Latch High */
408 /* DLAB is reset to 0 in next outb() */
410 /* Set serial parameters (parity off, etc) */
411 outb(UART_LCR_WLEN8
/* 8 data-bits */
415 ,uart
->base
+ UART_LCR
); /* Line Control Register */
417 switch (uart
->adaptor
) {
419 outb(UART_MCR_RTS
/* Set Request-To-Send line active */
420 | UART_MCR_DTR
/* Set Data-Terminal-Ready line active */
421 | UART_MCR_OUT2
/* Set OUT2 - not always required, but when
422 * it is, it is ESSENTIAL for enabling interrupts
424 ,uart
->base
+ UART_MCR
); /* Modem Control Register */
426 case SNDRV_SERIAL_MS124W_SA
:
427 case SNDRV_SERIAL_MS124W_MB
:
428 /* MS-124W can draw power from RTS and DTR if they
429 are in opposite states. */
430 outb(UART_MCR_RTS
| (0&UART_MCR_DTR
) | UART_MCR_OUT2
,
431 uart
->base
+ UART_MCR
);
433 case SNDRV_SERIAL_MS124T
:
434 /* MS-124T can draw power from RTS and/or DTR (preferably
435 both) if they are both asserted. */
436 outb(UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
,
437 uart
->base
+ UART_MCR
);
442 byte
= (0 & UART_IER_RDI
) /* Disable Receiver data interrupt */
443 |(0 & UART_IER_THRI
) /* Disable Transmitter holding register empty interrupt */
445 } else if (uart
->adaptor
== SNDRV_SERIAL_MS124W_SA
) {
446 byte
= UART_IER_RDI
/* Enable Receiver data interrupt */
447 | UART_IER_MSI
/* Enable Modem status interrupt */
449 } else if (uart
->adaptor
== SNDRV_SERIAL_GENERIC
) {
450 byte
= UART_IER_RDI
/* Enable Receiver data interrupt */
451 | UART_IER_MSI
/* Enable Modem status interrupt */
452 | UART_IER_THRI
/* Enable Transmitter holding register empty interrupt */
455 byte
= UART_IER_RDI
/* Enable Receiver data interrupt */
456 | UART_IER_THRI
/* Enable Transmitter holding register empty interrupt */
459 outb(byte
, uart
->base
+ UART_IER
); /* Interrupt enable Register */
461 inb(uart
->base
+ UART_LSR
); /* Clear any pre-existing overrun indication */
462 inb(uart
->base
+ UART_IIR
); /* Clear any pre-existing transmit interrupt */
463 inb(uart
->base
+ UART_RX
); /* Clear any pre-existing receive interrupt */
466 static void snd_uart16550_do_close(struct snd_uart16550
* uart
)
469 snd_uart16550_del_timer(uart
);
471 /* NOTE: may need to disable interrupts before de-registering out handler.
472 * For now, the consequences are harmless.
475 outb((0 & UART_IER_RDI
) /* Disable Receiver data interrupt */
476 |(0 & UART_IER_THRI
) /* Disable Transmitter holding register empty interrupt */
477 ,uart
->base
+ UART_IER
); /* Interrupt enable Register */
479 switch (uart
->adaptor
) {
481 outb((0 & UART_MCR_RTS
) /* Deactivate Request-To-Send line */
482 |(0 & UART_MCR_DTR
) /* Deactivate Data-Terminal-Ready line */
483 |(0 & UART_MCR_OUT2
) /* Deactivate OUT2 */
484 ,uart
->base
+ UART_MCR
); /* Modem Control Register */
486 case SNDRV_SERIAL_MS124W_SA
:
487 case SNDRV_SERIAL_MS124W_MB
:
488 /* MS-124W can draw power from RTS and DTR if they
489 are in opposite states; leave it powered. */
490 outb(UART_MCR_RTS
| (0&UART_MCR_DTR
) | (0&UART_MCR_OUT2
),
491 uart
->base
+ UART_MCR
);
493 case SNDRV_SERIAL_MS124T
:
494 /* MS-124T can draw power from RTS and/or DTR (preferably
495 both) if they are both asserted; leave it powered. */
496 outb(UART_MCR_RTS
| UART_MCR_DTR
| (0&UART_MCR_OUT2
),
497 uart
->base
+ UART_MCR
);
501 inb(uart
->base
+ UART_IIR
); /* Clear any outstanding interrupts */
503 /* Restore old divisor */
504 if (uart
->divisor
!= 0) {
505 outb(UART_LCR_DLAB
/* Divisor latch access bit */
506 ,uart
->base
+ UART_LCR
); /* Line Control Register */
507 outb(uart
->old_divisor_lsb
508 ,uart
->base
+ UART_DLL
); /* Divisor Latch Low */
509 outb(uart
->old_divisor_msb
510 ,uart
->base
+ UART_DLM
); /* Divisor Latch High */
511 /* Restore old LCR (data bits, stop bits, parity, DLAB) */
512 outb(uart
->old_line_ctrl_reg
513 ,uart
->base
+ UART_LCR
); /* Line Control Register */
517 static int snd_uart16550_input_open(struct snd_rawmidi_substream
*substream
)
520 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
522 spin_lock_irqsave(&uart
->open_lock
, flags
);
523 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
524 snd_uart16550_do_open(uart
);
525 uart
->filemode
|= SERIAL_MODE_INPUT_OPEN
;
526 uart
->midi_input
[substream
->number
] = substream
;
527 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
531 static int snd_uart16550_input_close(struct snd_rawmidi_substream
*substream
)
534 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
536 spin_lock_irqsave(&uart
->open_lock
, flags
);
537 uart
->filemode
&= ~SERIAL_MODE_INPUT_OPEN
;
538 uart
->midi_input
[substream
->number
] = NULL
;
539 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
540 snd_uart16550_do_close(uart
);
541 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
545 static void snd_uart16550_input_trigger(struct snd_rawmidi_substream
*substream
,
549 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
551 spin_lock_irqsave(&uart
->open_lock
, flags
);
553 uart
->filemode
|= SERIAL_MODE_INPUT_TRIGGERED
;
555 uart
->filemode
&= ~SERIAL_MODE_INPUT_TRIGGERED
;
556 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
559 static int snd_uart16550_output_open(struct snd_rawmidi_substream
*substream
)
562 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
564 spin_lock_irqsave(&uart
->open_lock
, flags
);
565 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
566 snd_uart16550_do_open(uart
);
567 uart
->filemode
|= SERIAL_MODE_OUTPUT_OPEN
;
568 uart
->midi_output
[substream
->number
] = substream
;
569 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
573 static int snd_uart16550_output_close(struct snd_rawmidi_substream
*substream
)
576 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
578 spin_lock_irqsave(&uart
->open_lock
, flags
);
579 uart
->filemode
&= ~SERIAL_MODE_OUTPUT_OPEN
;
580 uart
->midi_output
[substream
->number
] = NULL
;
581 if (uart
->filemode
== SERIAL_MODE_NOT_OPENED
)
582 snd_uart16550_do_close(uart
);
583 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
587 static inline int snd_uart16550_buffer_can_write(struct snd_uart16550
*uart
,
590 if (uart
->buff_in_count
+ Num
< TX_BUFF_SIZE
)
596 static inline int snd_uart16550_write_buffer(struct snd_uart16550
*uart
,
599 unsigned short buff_in
= uart
->buff_in
;
600 if (uart
->buff_in_count
< TX_BUFF_SIZE
) {
601 uart
->tx_buff
[buff_in
] = byte
;
603 buff_in
&= TX_BUFF_MASK
;
604 uart
->buff_in
= buff_in
;
605 uart
->buff_in_count
++;
606 if (uart
->irq
< 0) /* polling mode */
607 snd_uart16550_add_timer(uart
);
613 static int snd_uart16550_output_byte(struct snd_uart16550
*uart
,
614 struct snd_rawmidi_substream
*substream
,
615 unsigned char midi_byte
)
617 if (uart
->buff_in_count
== 0 /* Buffer empty? */
618 && ((uart
->adaptor
!= SNDRV_SERIAL_MS124W_SA
&&
619 uart
->adaptor
!= SNDRV_SERIAL_GENERIC
) ||
620 (uart
->fifo_count
== 0 /* FIFO empty? */
621 && (inb(uart
->base
+ UART_MSR
) & UART_MSR_CTS
)))) { /* CTS? */
623 /* Tx Buffer Empty - try to write immediately */
624 if ((inb(uart
->base
+ UART_LSR
) & UART_LSR_THRE
) != 0) {
625 /* Transmitter holding register (and Tx FIFO) empty */
626 uart
->fifo_count
= 1;
627 outb(midi_byte
, uart
->base
+ UART_TX
);
629 if (uart
->fifo_count
< uart
->fifo_limit
) {
631 outb(midi_byte
, uart
->base
+ UART_TX
);
633 /* Cannot write (buffer empty) -
634 * put char in buffer */
635 snd_uart16550_write_buffer(uart
, midi_byte
);
639 if (!snd_uart16550_write_buffer(uart
, midi_byte
)) {
640 snd_printk(KERN_WARNING
641 "%s: Buffer overrun on device at 0x%lx\n",
642 uart
->rmidi
->name
, uart
->base
);
650 static void snd_uart16550_output_write(struct snd_rawmidi_substream
*substream
)
653 unsigned char midi_byte
, addr_byte
;
654 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
656 static unsigned long lasttime
= 0;
658 /* Interrupts are disabled during the updating of the tx_buff,
659 * since it is 'bad' to have two processes updating the same
660 * variables (ie buff_in & buff_out)
663 spin_lock_irqsave(&uart
->open_lock
, flags
);
665 if (uart
->irq
< 0) /* polling */
666 snd_uart16550_io_loop(uart
);
668 if (uart
->adaptor
== SNDRV_SERIAL_MS124W_MB
) {
671 /* in this mode we need two bytes of space */
672 if (uart
->buff_in_count
> TX_BUFF_SIZE
- 2)
674 if (snd_rawmidi_transmit(substream
, &midi_byte
, 1) != 1)
676 #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
677 /* select exactly one of the four ports */
678 addr_byte
= (1 << (substream
->number
+ 4)) | 0x08;
680 /* select any combination of the four ports */
681 addr_byte
= (substream
->number
<< 4) | 0x08;
683 if (addr_byte
== 0x08)
686 snd_uart16550_output_byte(uart
, substream
, addr_byte
);
688 snd_uart16550_output_byte(uart
, substream
, midi_byte
);
692 while (snd_rawmidi_transmit_peek(substream
, &midi_byte
, 1) == 1) {
693 /* Also send F5 after 3 seconds with no data
694 * to handle device disconnect */
696 (uart
->adaptor
== SNDRV_SERIAL_SOUNDCANVAS
||
697 uart
->adaptor
== SNDRV_SERIAL_GENERIC
) &&
698 (uart
->prev_out
!= substream
->number
||
699 time_after(jiffies
, lasttime
+ 3*HZ
))) {
701 if (snd_uart16550_buffer_can_write(uart
, 3)) {
702 /* Roland Soundcanvas part selection */
703 /* If this substream of the data is
704 * different previous substream
705 * in this uart, send the change part
708 uart
->prev_out
= substream
->number
;
710 snd_uart16550_output_byte(uart
, substream
,
713 snd_uart16550_output_byte(uart
, substream
,
715 /* If midi_byte is a data byte,
716 * send the previous status byte */
717 if (midi_byte
< 0x80 &&
718 uart
->adaptor
== SNDRV_SERIAL_SOUNDCANVAS
)
719 snd_uart16550_output_byte(uart
, substream
, uart
->prev_status
[uart
->prev_out
]);
720 } else if (!uart
->drop_on_full
)
726 if (!snd_uart16550_output_byte(uart
, substream
, midi_byte
) &&
727 !uart
->drop_on_full
)
730 if (midi_byte
>= 0x80 && midi_byte
< 0xf0)
731 uart
->prev_status
[uart
->prev_out
] = midi_byte
;
734 snd_rawmidi_transmit_ack( substream
, 1 );
738 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
741 static void snd_uart16550_output_trigger(struct snd_rawmidi_substream
*substream
,
745 struct snd_uart16550
*uart
= substream
->rmidi
->private_data
;
747 spin_lock_irqsave(&uart
->open_lock
, flags
);
749 uart
->filemode
|= SERIAL_MODE_OUTPUT_TRIGGERED
;
751 uart
->filemode
&= ~SERIAL_MODE_OUTPUT_TRIGGERED
;
752 spin_unlock_irqrestore(&uart
->open_lock
, flags
);
754 snd_uart16550_output_write(substream
);
757 static struct snd_rawmidi_ops snd_uart16550_output
=
759 .open
= snd_uart16550_output_open
,
760 .close
= snd_uart16550_output_close
,
761 .trigger
= snd_uart16550_output_trigger
,
764 static struct snd_rawmidi_ops snd_uart16550_input
=
766 .open
= snd_uart16550_input_open
,
767 .close
= snd_uart16550_input_close
,
768 .trigger
= snd_uart16550_input_trigger
,
771 static int snd_uart16550_free(struct snd_uart16550
*uart
)
774 free_irq(uart
->irq
, uart
);
775 release_and_free_resource(uart
->res_base
);
780 static int snd_uart16550_dev_free(struct snd_device
*device
)
782 struct snd_uart16550
*uart
= device
->device_data
;
783 return snd_uart16550_free(uart
);
786 static int __devinit
snd_uart16550_create(struct snd_card
*card
,
787 unsigned long iobase
,
793 struct snd_uart16550
**ruart
)
795 static struct snd_device_ops ops
= {
796 .dev_free
= snd_uart16550_dev_free
,
798 struct snd_uart16550
*uart
;
802 if ((uart
= kzalloc(sizeof(*uart
), GFP_KERNEL
)) == NULL
)
804 uart
->adaptor
= adaptor
;
806 spin_lock_init(&uart
->open_lock
);
809 uart
->drop_on_full
= droponfull
;
811 if ((err
= snd_uart16550_detect(uart
)) <= 0) {
812 printk(KERN_ERR
"no UART detected at 0x%lx\n", iobase
);
813 snd_uart16550_free(uart
);
817 if (irq
>= 0 && irq
!= SNDRV_AUTO_IRQ
) {
818 if (request_irq(irq
, snd_uart16550_interrupt
,
819 IRQF_DISABLED
, "Serial MIDI", uart
)) {
820 snd_printk(KERN_WARNING
821 "irq %d busy. Using Polling.\n", irq
);
826 uart
->divisor
= base
/ speed
;
827 uart
->speed
= base
/ (unsigned int)uart
->divisor
;
828 uart
->speed_base
= base
;
832 memset(uart
->prev_status
, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS
);
833 init_timer(&uart
->buffer_timer
);
834 uart
->buffer_timer
.function
= snd_uart16550_buffer_timer
;
835 uart
->buffer_timer
.data
= (unsigned long)uart
;
836 uart
->timer_running
= 0;
838 /* Register device */
839 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, uart
, &ops
)) < 0) {
840 snd_uart16550_free(uart
);
844 switch (uart
->adaptor
) {
845 case SNDRV_SERIAL_MS124W_SA
:
846 case SNDRV_SERIAL_MS124W_MB
:
847 /* MS-124W can draw power from RTS and DTR if they
848 are in opposite states. */
849 outb(UART_MCR_RTS
| (0&UART_MCR_DTR
), uart
->base
+ UART_MCR
);
851 case SNDRV_SERIAL_MS124T
:
852 /* MS-124T can draw power from RTS and/or DTR (preferably
853 both) if they are asserted. */
854 outb(UART_MCR_RTS
| UART_MCR_DTR
, uart
->base
+ UART_MCR
);
866 static void __devinit
snd_uart16550_substreams(struct snd_rawmidi_str
*stream
)
868 struct snd_rawmidi_substream
*substream
;
870 list_for_each_entry(substream
, &stream
->substreams
, list
) {
871 sprintf(substream
->name
, "Serial MIDI %d", substream
->number
+ 1);
875 static int __devinit
snd_uart16550_rmidi(struct snd_uart16550
*uart
, int device
,
877 struct snd_rawmidi
**rmidi
)
879 struct snd_rawmidi
*rrawmidi
;
882 err
= snd_rawmidi_new(uart
->card
, "UART Serial MIDI", device
,
883 outs
, ins
, &rrawmidi
);
886 snd_rawmidi_set_ops(rrawmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
887 &snd_uart16550_input
);
888 snd_rawmidi_set_ops(rrawmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
889 &snd_uart16550_output
);
890 strcpy(rrawmidi
->name
, "Serial MIDI");
891 snd_uart16550_substreams(&rrawmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
]);
892 snd_uart16550_substreams(&rrawmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
]);
893 rrawmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
894 SNDRV_RAWMIDI_INFO_INPUT
|
895 SNDRV_RAWMIDI_INFO_DUPLEX
;
896 rrawmidi
->private_data
= uart
;
902 static int __devinit
snd_serial_probe(struct platform_device
*devptr
)
904 struct snd_card
*card
;
905 struct snd_uart16550
*uart
;
907 int dev
= devptr
->id
;
909 switch (adaptor
[dev
]) {
910 case SNDRV_SERIAL_SOUNDCANVAS
:
913 case SNDRV_SERIAL_MS124T
:
914 case SNDRV_SERIAL_MS124W_SA
:
918 case SNDRV_SERIAL_MS124W_MB
:
922 case SNDRV_SERIAL_GENERIC
:
926 "Adaptor type is out of range 0-%d (%d)\n",
927 SNDRV_SERIAL_MAX_ADAPTOR
, adaptor
[dev
]);
931 if (outs
[dev
] < 1 || outs
[dev
] > SNDRV_SERIAL_MAX_OUTS
) {
933 "Count of outputs is out of range 1-%d (%d)\n",
934 SNDRV_SERIAL_MAX_OUTS
, outs
[dev
]);
938 if (ins
[dev
] < 1 || ins
[dev
] > SNDRV_SERIAL_MAX_INS
) {
940 "Count of inputs is out of range 1-%d (%d)\n",
941 SNDRV_SERIAL_MAX_INS
, ins
[dev
]);
945 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
, 0, &card
);
949 strcpy(card
->driver
, "Serial");
950 strcpy(card
->shortname
, "Serial MIDI (UART16550A)");
952 if ((err
= snd_uart16550_create(card
,
962 err
= snd_uart16550_rmidi(uart
, 0, outs
[dev
], ins
[dev
], &uart
->rmidi
);
966 sprintf(card
->longname
, "%s at 0x%lx, irq %d speed %d div %d outs %d ins %d adaptor %s droponfull %d",
974 adaptor_names
[uart
->adaptor
],
977 snd_card_set_dev(card
, &devptr
->dev
);
979 if ((err
= snd_card_register(card
)) < 0)
982 platform_set_drvdata(devptr
, card
);
990 static int __devexit
snd_serial_remove(struct platform_device
*devptr
)
992 snd_card_free(platform_get_drvdata(devptr
));
993 platform_set_drvdata(devptr
, NULL
);
997 #define SND_SERIAL_DRIVER "snd_serial_u16550"
999 static struct platform_driver snd_serial_driver
= {
1000 .probe
= snd_serial_probe
,
1001 .remove
= __devexit_p( snd_serial_remove
),
1003 .name
= SND_SERIAL_DRIVER
1007 static void snd_serial_unregister_all(void)
1011 for (i
= 0; i
< ARRAY_SIZE(devices
); ++i
)
1012 platform_device_unregister(devices
[i
]);
1013 platform_driver_unregister(&snd_serial_driver
);
1016 static int __init
alsa_card_serial_init(void)
1020 if ((err
= platform_driver_register(&snd_serial_driver
)) < 0)
1024 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1025 struct platform_device
*device
;
1028 device
= platform_device_register_simple(SND_SERIAL_DRIVER
,
1032 if (!platform_get_drvdata(device
)) {
1033 platform_device_unregister(device
);
1036 devices
[i
] = device
;
1041 printk(KERN_ERR
"serial midi soundcard not found or device busy\n");
1043 snd_serial_unregister_all();
1049 static void __exit
alsa_card_serial_exit(void)
1051 snd_serial_unregister_all();
1054 module_init(alsa_card_serial_init
)
1055 module_exit(alsa_card_serial_exit
)