Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * serial.c
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
5 * Isaku Yamahata <yamahata@private.email.ne.jp>,
6 * George Hansper <ghansper@apana.org.au>,
7 * Hannu Savolainen
8 *
9 * This code is based on the code from ALSA 0.5.9, but heavily rewritten.
10 *
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.
16 *
17 * More documentation can be found in serial-u16550.txt.
18 */
19
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>
27#include <linux/io.h>
28#include <sound/core.h>
29#include <sound/rawmidi.h>
30#include <sound/initval.h>
31
32#include <linux/serial_reg.h>
33#include <linux/jiffies.h>
34
35MODULE_DESCRIPTION("MIDI serial u16550");
36MODULE_LICENSE("GPL");
37
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
44static const char * const adaptor_names[] = {
45 "Soundcanvas",
46 "MS-124T",
47 "MS-124W S/A",
48 "MS-124W M/B",
49 "Generic"
50};
51
52#define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */
53#define SNDRV_SERIAL_DROPBUFF 1 /* Non-blocking discard operation */
54
55static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
56static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
57static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
58static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */
59static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,4,5,7,9,10,11,14,15 */
60static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */
61static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */
62static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
63static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
64static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
65static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
66
67module_param_array(index, int, NULL, 0444);
68MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
69module_param_array(id, charp, NULL, 0444);
70MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
71module_param_array(enable, bool, NULL, 0444);
72MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
73module_param_hw_array(port, long, ioport, NULL, 0444);
74MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
75module_param_hw_array(irq, int, irq, NULL, 0444);
76MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
77module_param_array(speed, int, NULL, 0444);
78MODULE_PARM_DESC(speed, "Speed in bauds.");
79module_param_array(base, int, NULL, 0444);
80MODULE_PARM_DESC(base, "Base for divisor in bauds.");
81module_param_array(outs, int, NULL, 0444);
82MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
83module_param_array(ins, int, NULL, 0444);
84MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
85module_param_array(droponfull, bool, NULL, 0444);
86MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
87
88module_param_array(adaptor, int, NULL, 0444);
89MODULE_PARM_DESC(adaptor, "Type of adaptor.");
90
91/*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/ /* Address outs as 0-3 instead of bitmap */
92
93#define SNDRV_SERIAL_MAX_OUTS 16 /* max 64, min 16 */
94#define SNDRV_SERIAL_MAX_INS 16 /* max 64, min 16 */
95
96#define TX_BUFF_SIZE (1<<15) /* Must be 2^n */
97#define TX_BUFF_MASK (TX_BUFF_SIZE - 1)
98
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)
104
105struct 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];
110
111 int filemode; /* open status of file */
112
113 spinlock_t open_lock;
114
115 int irq;
116
117 unsigned long base;
118 struct resource *res_base;
119
120 unsigned int speed;
121 unsigned int speed_base;
122 unsigned char divisor;
123
124 unsigned char old_divisor_lsb;
125 unsigned char old_divisor_msb;
126 unsigned char old_line_ctrl_reg;
127
128 /* parameter for using of write loop */
129 short int fifo_limit; /* used in uart16550 */
130 short int fifo_count; /* used in uart16550 */
131
132 /* type of adaptor */
133 int adaptor;
134
135 /* inputs */
136 int prev_in;
137 unsigned char rstatus;
138
139 /* outputs */
140 int prev_out;
141 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
142
143 /* write buffer and its writing/reading position */
144 unsigned char tx_buff[TX_BUFF_SIZE];
145 int buff_in_count;
146 int buff_in;
147 int buff_out;
148 int drop_on_full;
149
150 /* wait timer */
151 unsigned int timer_running:1;
152 struct timer_list buffer_timer;
153
154};
155
156static struct platform_device *devices[SNDRV_CARDS];
157
158static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
159{
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;
164 }
165}
166
167static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
168{
169 if (uart->timer_running) {
170 del_timer(&uart->buffer_timer);
171 uart->timer_running = 0;
172 }
173}
174
175/* This macro is only used in snd_uart16550_io_loop */
176static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
177{
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);
181 uart->fifo_count++;
182 buff_out++;
183 buff_out &= TX_BUFF_MASK;
184 uart->buff_out = buff_out;
185 uart->buff_in_count--;
186 }
187}
188
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
192 */
193static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
194{
195 unsigned char c, status;
196 int substream;
197
198 /* recall previous stream */
199 substream = uart->prev_in;
200
201 /* Read Loop */
202 while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
203 /* while receive data ready */
204 c = inb(uart->base + UART_RX);
205
206 /* keep track of last status byte */
207 if (c & 0x80)
208 uart->rstatus = c;
209
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)
214 substream = c - 1;
215 if (c != 0xf5)
216 /* prevent future bytes from being
217 interpreted as streams */
218 uart->rstatus = 0;
219 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN)
220 && uart->midi_input[substream])
221 snd_rawmidi_receive(uart->midi_input[substream],
222 &c, 1);
223 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) &&
224 uart->midi_input[substream])
225 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
226
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);
231 }
232
233 /* remember the last stream */
234 uart->prev_in = substream;
235
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);
249 }
250 } else {
251 /* Write loop */
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);
255 }
256 if (uart->irq < 0 && uart->buff_in_count > 0)
257 snd_uart16550_add_timer(uart);
258}
259
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:
266 * a) Writing a byte
267 * b) Reading the IIR
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
274 * opened/closed.
275 *
276 *
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?)
279 */
280static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
281{
282 struct snd_uart16550 *uart;
283
284 uart = dev_id;
285 spin_lock(&uart->open_lock);
286 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
287 spin_unlock(&uart->open_lock);
288 return IRQ_NONE;
289 }
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);
294 return IRQ_HANDLED;
295}
296
297/* When the polling mode, this function calls snd_uart16550_io_loop. */
298static void snd_uart16550_buffer_timer(struct timer_list *t)
299{
300 unsigned long flags;
301 struct snd_uart16550 *uart;
302
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);
308}
309
310/*
311 * this method probes, if an uart sits on given port
312 * return 0 if found
313 * return negative error if not found
314 */
315static int snd_uart16550_detect(struct snd_uart16550 *uart)
316{
317 unsigned long io_base = uart->base;
318 int ok;
319 unsigned char c;
320
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 */
324 }
325
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);
329 return -EBUSY;
330 }
331
332 /* uart detected unless one of the following tests should fail */
333 ok = 1;
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 */
338 if ((c & 0xf0) != 0)
339 ok = 0; /* failed */
340
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 */
345 if (c != 0xaa)
346 ok = 0; /* failed */
347
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 */
352 if (c != 0x55)
353 ok = 0; /* failed */
354
355 return ok;
356}
357
358static void snd_uart16550_do_open(struct snd_uart16550 * uart)
359{
360 char byte;
361
362 /* Initialize basic variables */
363 uart->buff_in_count = 0;
364 uart->buff_in = 0;
365 uart->buff_out = 0;
366 uart->fifo_limit = 1;
367 uart->fifo_count = 0;
368 uart->timer_running = 0;
369
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
376 */
377 ,uart->base + UART_FCR); /* FIFO Control Register */
378
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);
387
388 outb(uart->divisor
389 ,uart->base + UART_DLL); /* Divisor Latch Low */
390 outb(0
391 ,uart->base + UART_DLM); /* Divisor Latch High */
392 /* DLAB is reset to 0 in next outb() */
393 }
394 /* Set serial parameters (parity off, etc) */
395 outb(UART_LCR_WLEN8 /* 8 data-bits */
396 | 0 /* 1 stop-bit */
397 | 0 /* parity off */
398 | 0 /* DLAB = 0 */
399 ,uart->base + UART_LCR); /* Line Control Register */
400
401 switch (uart->adaptor) {
402 default:
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
407 */
408 ,uart->base + UART_MCR); /* Modem Control Register */
409 break;
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);
416 break;
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);
422 break;
423 }
424
425 if (uart->irq < 0) {
426 byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
427 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
428 ;
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 */
432 ;
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 */
437 ;
438 } else {
439 byte = UART_IER_RDI /* Enable Receiver data interrupt */
440 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
441 ;
442 }
443 outb(byte, uart->base + UART_IER); /* Interrupt enable Register */
444
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 */
448}
449
450static void snd_uart16550_do_close(struct snd_uart16550 * uart)
451{
452 if (uart->irq < 0)
453 snd_uart16550_del_timer(uart);
454
455 /* NOTE: may need to disable interrupts before de-registering out handler.
456 * For now, the consequences are harmless.
457 */
458
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 */
462
463 switch (uart->adaptor) {
464 default:
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 */
469 break;
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);
476 break;
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);
482 break;
483 }
484
485 inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
486
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 */
498 }
499}
500
501static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
502{
503 unsigned long flags;
504 struct snd_uart16550 *uart = substream->rmidi->private_data;
505
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);
512 return 0;
513}
514
515static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
516{
517 unsigned long flags;
518 struct snd_uart16550 *uart = substream->rmidi->private_data;
519
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);
526 return 0;
527}
528
529static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
530 int up)
531{
532 unsigned long flags;
533 struct snd_uart16550 *uart = substream->rmidi->private_data;
534
535 spin_lock_irqsave(&uart->open_lock, flags);
536 if (up)
537 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
538 else
539 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
540 spin_unlock_irqrestore(&uart->open_lock, flags);
541}
542
543static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
544{
545 unsigned long flags;
546 struct snd_uart16550 *uart = substream->rmidi->private_data;
547
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);
554 return 0;
555};
556
557static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
558{
559 unsigned long flags;
560 struct snd_uart16550 *uart = substream->rmidi->private_data;
561
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);
568 return 0;
569};
570
571static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
572 int Num)
573{
574 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
575 return 1;
576 else
577 return 0;
578}
579
580static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
581 unsigned char byte)
582{
583 unsigned short buff_in = uart->buff_in;
584 if (uart->buff_in_count < TX_BUFF_SIZE) {
585 uart->tx_buff[buff_in] = byte;
586 buff_in++;
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);
592 return 1;
593 } else
594 return 0;
595}
596
597static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
598 struct snd_rawmidi_substream *substream,
599 unsigned char midi_byte)
600{
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? */
606
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);
612 } else {
613 if (uart->fifo_count < uart->fifo_limit) {
614 uart->fifo_count++;
615 outb(midi_byte, uart->base + UART_TX);
616 } else {
617 /* Cannot write (buffer empty) -
618 * put char in buffer */
619 snd_uart16550_write_buffer(uart, midi_byte);
620 }
621 }
622 } else {
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);
627 return 0;
628 }
629 }
630
631 return 1;
632}
633
634static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
635{
636 unsigned long flags;
637 unsigned char midi_byte, addr_byte;
638 struct snd_uart16550 *uart = substream->rmidi->private_data;
639 char first;
640 static unsigned long lasttime = 0;
641
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)
645 */
646
647 spin_lock_irqsave(&uart->open_lock, flags);
648
649 if (uart->irq < 0) /* polling */
650 snd_uart16550_io_loop(uart);
651
652 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
653 while (1) {
654 /* buffer full? */
655 /* in this mode we need two bytes of space */
656 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
657 break;
658 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
659 break;
660#ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
661 /* select exactly one of the four ports */
662 addr_byte = (1 << (substream->number + 4)) | 0x08;
663#else
664 /* select any combination of the four ports */
665 addr_byte = (substream->number << 4) | 0x08;
666 /* ...except none */
667 if (addr_byte == 0x08)
668 addr_byte = 0xf8;
669#endif
670 snd_uart16550_output_byte(uart, substream, addr_byte);
671 /* send midi byte */
672 snd_uart16550_output_byte(uart, substream, midi_byte);
673 }
674 } else {
675 first = 0;
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 */
679 if (first == 0 &&
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))) {
684
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
690 * event
691 */
692 uart->prev_out = substream->number;
693 /* change part */
694 snd_uart16550_output_byte(uart, substream,
695 0xf5);
696 /* data */
697 snd_uart16550_output_byte(uart, substream,
698 uart->prev_out + 1);
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)
705 break;
706
707 }
708
709 /* send midi byte */
710 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
711 !uart->drop_on_full )
712 break;
713
714 if (midi_byte >= 0x80 && midi_byte < 0xf0)
715 uart->prev_status[uart->prev_out] = midi_byte;
716 first = 1;
717
718 snd_rawmidi_transmit_ack( substream, 1 );
719 }
720 lasttime = jiffies;
721 }
722 spin_unlock_irqrestore(&uart->open_lock, flags);
723}
724
725static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
726 int up)
727{
728 unsigned long flags;
729 struct snd_uart16550 *uart = substream->rmidi->private_data;
730
731 spin_lock_irqsave(&uart->open_lock, flags);
732 if (up)
733 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
734 else
735 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
736 spin_unlock_irqrestore(&uart->open_lock, flags);
737 if (up)
738 snd_uart16550_output_write(substream);
739}
740
741static const struct snd_rawmidi_ops snd_uart16550_output =
742{
743 .open = snd_uart16550_output_open,
744 .close = snd_uart16550_output_close,
745 .trigger = snd_uart16550_output_trigger,
746};
747
748static const struct snd_rawmidi_ops snd_uart16550_input =
749{
750 .open = snd_uart16550_input_open,
751 .close = snd_uart16550_input_close,
752 .trigger = snd_uart16550_input_trigger,
753};
754
755static int snd_uart16550_free(struct snd_uart16550 *uart)
756{
757 if (uart->irq >= 0)
758 free_irq(uart->irq, uart);
759 release_and_free_resource(uart->res_base);
760 kfree(uart);
761 return 0;
762};
763
764static int snd_uart16550_dev_free(struct snd_device *device)
765{
766 struct snd_uart16550 *uart = device->device_data;
767 return snd_uart16550_free(uart);
768}
769
770static int snd_uart16550_create(struct snd_card *card,
771 unsigned long iobase,
772 int irq,
773 unsigned int speed,
774 unsigned int base,
775 int adaptor,
776 int droponfull,
777 struct snd_uart16550 **ruart)
778{
779 static const struct snd_device_ops ops = {
780 .dev_free = snd_uart16550_dev_free,
781 };
782 struct snd_uart16550 *uart;
783 int err;
784
785
786 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
787 if (!uart)
788 return -ENOMEM;
789 uart->adaptor = adaptor;
790 uart->card = card;
791 spin_lock_init(&uart->open_lock);
792 uart->irq = -1;
793 uart->base = iobase;
794 uart->drop_on_full = droponfull;
795
796 err = snd_uart16550_detect(uart);
797 if (err <= 0) {
798 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
799 snd_uart16550_free(uart);
800 return -ENODEV;
801 }
802
803 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
804 if (request_irq(irq, snd_uart16550_interrupt,
805 0, "Serial MIDI", uart)) {
806 snd_printk(KERN_WARNING
807 "irq %d busy. Using Polling.\n", irq);
808 } else {
809 uart->irq = irq;
810 }
811 }
812 uart->divisor = base / speed;
813 uart->speed = base / (unsigned int)uart->divisor;
814 uart->speed_base = base;
815 uart->prev_out = -1;
816 uart->prev_in = 0;
817 uart->rstatus = 0;
818 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
819 timer_setup(&uart->buffer_timer, snd_uart16550_buffer_timer, 0);
820 uart->timer_running = 0;
821
822 /* Register device */
823 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops);
824 if (err < 0) {
825 snd_uart16550_free(uart);
826 return err;
827 }
828
829 switch (uart->adaptor) {
830 case SNDRV_SERIAL_MS124W_SA:
831 case SNDRV_SERIAL_MS124W_MB:
832 /* MS-124W can draw power from RTS and DTR if they
833 are in opposite states. */
834 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
835 break;
836 case SNDRV_SERIAL_MS124T:
837 /* MS-124T can draw power from RTS and/or DTR (preferably
838 both) if they are asserted. */
839 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
840 break;
841 default:
842 break;
843 }
844
845 if (ruart)
846 *ruart = uart;
847
848 return 0;
849}
850
851static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
852{
853 struct snd_rawmidi_substream *substream;
854
855 list_for_each_entry(substream, &stream->substreams, list) {
856 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
857 }
858}
859
860static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
861 int outs, int ins,
862 struct snd_rawmidi **rmidi)
863{
864 struct snd_rawmidi *rrawmidi;
865 int err;
866
867 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
868 outs, ins, &rrawmidi);
869 if (err < 0)
870 return err;
871 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
872 &snd_uart16550_input);
873 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
874 &snd_uart16550_output);
875 strcpy(rrawmidi->name, "Serial MIDI");
876 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
877 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
878 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
879 SNDRV_RAWMIDI_INFO_INPUT |
880 SNDRV_RAWMIDI_INFO_DUPLEX;
881 rrawmidi->private_data = uart;
882 if (rmidi)
883 *rmidi = rrawmidi;
884 return 0;
885}
886
887static int snd_serial_probe(struct platform_device *devptr)
888{
889 struct snd_card *card;
890 struct snd_uart16550 *uart;
891 int err;
892 int dev = devptr->id;
893
894 switch (adaptor[dev]) {
895 case SNDRV_SERIAL_SOUNDCANVAS:
896 ins[dev] = 1;
897 break;
898 case SNDRV_SERIAL_MS124T:
899 case SNDRV_SERIAL_MS124W_SA:
900 outs[dev] = 1;
901 ins[dev] = 1;
902 break;
903 case SNDRV_SERIAL_MS124W_MB:
904 outs[dev] = 16;
905 ins[dev] = 1;
906 break;
907 case SNDRV_SERIAL_GENERIC:
908 break;
909 default:
910 snd_printk(KERN_ERR
911 "Adaptor type is out of range 0-%d (%d)\n",
912 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
913 return -ENODEV;
914 }
915
916 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
917 snd_printk(KERN_ERR
918 "Count of outputs is out of range 1-%d (%d)\n",
919 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
920 return -ENODEV;
921 }
922
923 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
924 snd_printk(KERN_ERR
925 "Count of inputs is out of range 1-%d (%d)\n",
926 SNDRV_SERIAL_MAX_INS, ins[dev]);
927 return -ENODEV;
928 }
929
930 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
931 0, &card);
932 if (err < 0)
933 return err;
934
935 strcpy(card->driver, "Serial");
936 strcpy(card->shortname, "Serial MIDI (UART16550A)");
937
938 err = snd_uart16550_create(card, port[dev], irq[dev], speed[dev],
939 base[dev], adaptor[dev], droponfull[dev],
940 &uart);
941 if (err < 0)
942 goto _err;
943
944 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
945 if (err < 0)
946 goto _err;
947
948 sprintf(card->longname, "%s [%s] at %#lx, irq %d",
949 card->shortname,
950 adaptor_names[uart->adaptor],
951 uart->base,
952 uart->irq);
953
954 err = snd_card_register(card);
955 if (err < 0)
956 goto _err;
957
958 platform_set_drvdata(devptr, card);
959 return 0;
960
961 _err:
962 snd_card_free(card);
963 return err;
964}
965
966static int snd_serial_remove(struct platform_device *devptr)
967{
968 snd_card_free(platform_get_drvdata(devptr));
969 return 0;
970}
971
972#define SND_SERIAL_DRIVER "snd_serial_u16550"
973
974static struct platform_driver snd_serial_driver = {
975 .probe = snd_serial_probe,
976 .remove = snd_serial_remove,
977 .driver = {
978 .name = SND_SERIAL_DRIVER,
979 },
980};
981
982static void snd_serial_unregister_all(void)
983{
984 int i;
985
986 for (i = 0; i < ARRAY_SIZE(devices); ++i)
987 platform_device_unregister(devices[i]);
988 platform_driver_unregister(&snd_serial_driver);
989}
990
991static int __init alsa_card_serial_init(void)
992{
993 int i, cards, err;
994
995 err = platform_driver_register(&snd_serial_driver);
996 if (err < 0)
997 return err;
998
999 cards = 0;
1000 for (i = 0; i < SNDRV_CARDS; i++) {
1001 struct platform_device *device;
1002 if (! enable[i])
1003 continue;
1004 device = platform_device_register_simple(SND_SERIAL_DRIVER,
1005 i, NULL, 0);
1006 if (IS_ERR(device))
1007 continue;
1008 if (!platform_get_drvdata(device)) {
1009 platform_device_unregister(device);
1010 continue;
1011 }
1012 devices[i] = device;
1013 cards++;
1014 }
1015 if (! cards) {
1016#ifdef MODULE
1017 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
1018#endif
1019 snd_serial_unregister_all();
1020 return -ENODEV;
1021 }
1022 return 0;
1023}
1024
1025static void __exit alsa_card_serial_exit(void)
1026{
1027 snd_serial_unregister_all();
1028}
1029
1030module_init(alsa_card_serial_init)
1031module_exit(alsa_card_serial_exit)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * serial.c
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
5 * Isaku Yamahata <yamahata@private.email.ne.jp>,
6 * George Hansper <ghansper@apana.org.au>,
7 * Hannu Savolainen
8 *
9 * This code is based on the code from ALSA 0.5.9, but heavily rewritten.
10 *
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.
16 *
17 * More documentation can be found in serial-u16550.txt.
18 */
19
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>
27#include <linux/io.h>
28#include <sound/core.h>
29#include <sound/rawmidi.h>
30#include <sound/initval.h>
31
32#include <linux/serial_reg.h>
33#include <linux/jiffies.h>
34
35MODULE_DESCRIPTION("MIDI serial u16550");
36MODULE_LICENSE("GPL");
37MODULE_SUPPORTED_DEVICE("{{ALSA, MIDI serial u16550}}");
38
39#define SNDRV_SERIAL_SOUNDCANVAS 0 /* Roland Soundcanvas; F5 NN selects part */
40#define SNDRV_SERIAL_MS124T 1 /* Midiator MS-124T */
41#define SNDRV_SERIAL_MS124W_SA 2 /* Midiator MS-124W in S/A mode */
42#define SNDRV_SERIAL_MS124W_MB 3 /* Midiator MS-124W in M/B mode */
43#define SNDRV_SERIAL_GENERIC 4 /* Generic Interface */
44#define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC
45static char *adaptor_names[] = {
46 "Soundcanvas",
47 "MS-124T",
48 "MS-124W S/A",
49 "MS-124W M/B",
50 "Generic"
51};
52
53#define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */
54#define SNDRV_SERIAL_DROPBUFF 1 /* Non-blocking discard operation */
55
56static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
57static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
58static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
59static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */
60static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,4,5,7,9,10,11,14,15 */
61static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */
62static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */
63static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
64static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */
65static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
66static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
67
68module_param_array(index, int, NULL, 0444);
69MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
70module_param_array(id, charp, NULL, 0444);
71MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
72module_param_array(enable, bool, NULL, 0444);
73MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
74module_param_hw_array(port, long, ioport, NULL, 0444);
75MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
76module_param_hw_array(irq, int, irq, NULL, 0444);
77MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
78module_param_array(speed, int, NULL, 0444);
79MODULE_PARM_DESC(speed, "Speed in bauds.");
80module_param_array(base, int, NULL, 0444);
81MODULE_PARM_DESC(base, "Base for divisor in bauds.");
82module_param_array(outs, int, NULL, 0444);
83MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
84module_param_array(ins, int, NULL, 0444);
85MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
86module_param_array(droponfull, bool, NULL, 0444);
87MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
88
89module_param_array(adaptor, int, NULL, 0444);
90MODULE_PARM_DESC(adaptor, "Type of adaptor.");
91
92/*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/ /* Address outs as 0-3 instead of bitmap */
93
94#define SNDRV_SERIAL_MAX_OUTS 16 /* max 64, min 16 */
95#define SNDRV_SERIAL_MAX_INS 16 /* max 64, min 16 */
96
97#define TX_BUFF_SIZE (1<<15) /* Must be 2^n */
98#define TX_BUFF_MASK (TX_BUFF_SIZE - 1)
99
100#define SERIAL_MODE_NOT_OPENED (0)
101#define SERIAL_MODE_INPUT_OPEN (1 << 0)
102#define SERIAL_MODE_OUTPUT_OPEN (1 << 1)
103#define SERIAL_MODE_INPUT_TRIGGERED (1 << 2)
104#define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3)
105
106struct snd_uart16550 {
107 struct snd_card *card;
108 struct snd_rawmidi *rmidi;
109 struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS];
110 struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS];
111
112 int filemode; /* open status of file */
113
114 spinlock_t open_lock;
115
116 int irq;
117
118 unsigned long base;
119 struct resource *res_base;
120
121 unsigned int speed;
122 unsigned int speed_base;
123 unsigned char divisor;
124
125 unsigned char old_divisor_lsb;
126 unsigned char old_divisor_msb;
127 unsigned char old_line_ctrl_reg;
128
129 /* parameter for using of write loop */
130 short int fifo_limit; /* used in uart16550 */
131 short int fifo_count; /* used in uart16550 */
132
133 /* type of adaptor */
134 int adaptor;
135
136 /* inputs */
137 int prev_in;
138 unsigned char rstatus;
139
140 /* outputs */
141 int prev_out;
142 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
143
144 /* write buffer and its writing/reading position */
145 unsigned char tx_buff[TX_BUFF_SIZE];
146 int buff_in_count;
147 int buff_in;
148 int buff_out;
149 int drop_on_full;
150
151 /* wait timer */
152 unsigned int timer_running:1;
153 struct timer_list buffer_timer;
154
155};
156
157static struct platform_device *devices[SNDRV_CARDS];
158
159static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
160{
161 if (!uart->timer_running) {
162 /* timer 38600bps * 10bit * 16byte */
163 mod_timer(&uart->buffer_timer, jiffies + (HZ + 255) / 256);
164 uart->timer_running = 1;
165 }
166}
167
168static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
169{
170 if (uart->timer_running) {
171 del_timer(&uart->buffer_timer);
172 uart->timer_running = 0;
173 }
174}
175
176/* This macro is only used in snd_uart16550_io_loop */
177static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
178{
179 unsigned short buff_out = uart->buff_out;
180 if (uart->buff_in_count > 0) {
181 outb(uart->tx_buff[buff_out], uart->base + UART_TX);
182 uart->fifo_count++;
183 buff_out++;
184 buff_out &= TX_BUFF_MASK;
185 uart->buff_out = buff_out;
186 uart->buff_in_count--;
187 }
188}
189
190/* This loop should be called with interrupts disabled
191 * We don't want to interrupt this,
192 * as we're already handling an interrupt
193 */
194static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
195{
196 unsigned char c, status;
197 int substream;
198
199 /* recall previous stream */
200 substream = uart->prev_in;
201
202 /* Read Loop */
203 while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
204 /* while receive data ready */
205 c = inb(uart->base + UART_RX);
206
207 /* keep track of last status byte */
208 if (c & 0x80)
209 uart->rstatus = c;
210
211 /* handle stream switch */
212 if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
213 if (uart->rstatus == 0xf5) {
214 if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
215 substream = c - 1;
216 if (c != 0xf5)
217 /* prevent future bytes from being
218 interpreted as streams */
219 uart->rstatus = 0;
220 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN)
221 && uart->midi_input[substream])
222 snd_rawmidi_receive(uart->midi_input[substream],
223 &c, 1);
224 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) &&
225 uart->midi_input[substream])
226 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
227
228 if (status & UART_LSR_OE)
229 snd_printk(KERN_WARNING
230 "%s: Overrun on device at 0x%lx\n",
231 uart->rmidi->name, uart->base);
232 }
233
234 /* remember the last stream */
235 uart->prev_in = substream;
236
237 /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
238 buffer is never filled. */
239 /* Check write status */
240 if (status & UART_LSR_THRE)
241 uart->fifo_count = 0;
242 if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
243 || uart->adaptor == SNDRV_SERIAL_GENERIC) {
244 /* Can't use FIFO, must send only when CTS is true */
245 status = inb(uart->base + UART_MSR);
246 while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
247 uart->buff_in_count > 0) {
248 snd_uart16550_buffer_output(uart);
249 status = inb(uart->base + UART_MSR);
250 }
251 } else {
252 /* Write loop */
253 while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
254 && uart->buff_in_count > 0) /* Do we want to? */
255 snd_uart16550_buffer_output(uart);
256 }
257 if (uart->irq < 0 && uart->buff_in_count > 0)
258 snd_uart16550_add_timer(uart);
259}
260
261/* NOTES ON SERVICING INTERUPTS
262 * ---------------------------
263 * After receiving a interrupt, it is important to indicate to the UART that
264 * this has been done.
265 * For a Rx interrupt, this is done by reading the received byte.
266 * For a Tx interrupt this is done by either:
267 * a) Writing a byte
268 * b) Reading the IIR
269 * It is particularly important to read the IIR if a Tx interrupt is received
270 * when there is no data in tx_buff[], as in this case there no other
271 * indication that the interrupt has been serviced, and it remains outstanding
272 * indefinitely. This has the curious side effect that and no further interrupts
273 * will be generated from this device AT ALL!!.
274 * It is also desirable to clear outstanding interrupts when the device is
275 * opened/closed.
276 *
277 *
278 * Note that some devices need OUT2 to be set before they will generate
279 * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
280 */
281static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
282{
283 struct snd_uart16550 *uart;
284
285 uart = dev_id;
286 spin_lock(&uart->open_lock);
287 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
288 spin_unlock(&uart->open_lock);
289 return IRQ_NONE;
290 }
291 /* indicate to the UART that the interrupt has been serviced */
292 inb(uart->base + UART_IIR);
293 snd_uart16550_io_loop(uart);
294 spin_unlock(&uart->open_lock);
295 return IRQ_HANDLED;
296}
297
298/* When the polling mode, this function calls snd_uart16550_io_loop. */
299static void snd_uart16550_buffer_timer(struct timer_list *t)
300{
301 unsigned long flags;
302 struct snd_uart16550 *uart;
303
304 uart = from_timer(uart, t, buffer_timer);
305 spin_lock_irqsave(&uart->open_lock, flags);
306 snd_uart16550_del_timer(uart);
307 snd_uart16550_io_loop(uart);
308 spin_unlock_irqrestore(&uart->open_lock, flags);
309}
310
311/*
312 * this method probes, if an uart sits on given port
313 * return 0 if found
314 * return negative error if not found
315 */
316static int snd_uart16550_detect(struct snd_uart16550 *uart)
317{
318 unsigned long io_base = uart->base;
319 int ok;
320 unsigned char c;
321
322 /* Do some vague tests for the presence of the uart */
323 if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
324 return -ENODEV; /* Not configured */
325 }
326
327 uart->res_base = request_region(io_base, 8, "Serial MIDI");
328 if (uart->res_base == NULL) {
329 snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
330 return -EBUSY;
331 }
332
333 /* uart detected unless one of the following tests should fail */
334 ok = 1;
335 /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
336 outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
337 c = inb(io_base + UART_IER);
338 /* The top four bits of the IER should always == 0 */
339 if ((c & 0xf0) != 0)
340 ok = 0; /* failed */
341
342 outb(0xaa, io_base + UART_SCR);
343 /* Write arbitrary data into the scratch reg */
344 c = inb(io_base + UART_SCR);
345 /* If it comes back, it's OK */
346 if (c != 0xaa)
347 ok = 0; /* failed */
348
349 outb(0x55, io_base + UART_SCR);
350 /* Write arbitrary data into the scratch reg */
351 c = inb(io_base + UART_SCR);
352 /* If it comes back, it's OK */
353 if (c != 0x55)
354 ok = 0; /* failed */
355
356 return ok;
357}
358
359static void snd_uart16550_do_open(struct snd_uart16550 * uart)
360{
361 char byte;
362
363 /* Initialize basic variables */
364 uart->buff_in_count = 0;
365 uart->buff_in = 0;
366 uart->buff_out = 0;
367 uart->fifo_limit = 1;
368 uart->fifo_count = 0;
369 uart->timer_running = 0;
370
371 outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */
372 | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */
373 | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */
374 | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */
375 /* NOTE: interrupt generated after T=(time)4-bytes
376 * if less than UART_FCR_TRIGGER bytes received
377 */
378 ,uart->base + UART_FCR); /* FIFO Control Register */
379
380 if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
381 uart->fifo_limit = 16;
382 if (uart->divisor != 0) {
383 uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
384 outb(UART_LCR_DLAB /* Divisor latch access bit */
385 ,uart->base + UART_LCR); /* Line Control Register */
386 uart->old_divisor_lsb = inb(uart->base + UART_DLL);
387 uart->old_divisor_msb = inb(uart->base + UART_DLM);
388
389 outb(uart->divisor
390 ,uart->base + UART_DLL); /* Divisor Latch Low */
391 outb(0
392 ,uart->base + UART_DLM); /* Divisor Latch High */
393 /* DLAB is reset to 0 in next outb() */
394 }
395 /* Set serial parameters (parity off, etc) */
396 outb(UART_LCR_WLEN8 /* 8 data-bits */
397 | 0 /* 1 stop-bit */
398 | 0 /* parity off */
399 | 0 /* DLAB = 0 */
400 ,uart->base + UART_LCR); /* Line Control Register */
401
402 switch (uart->adaptor) {
403 default:
404 outb(UART_MCR_RTS /* Set Request-To-Send line active */
405 | UART_MCR_DTR /* Set Data-Terminal-Ready line active */
406 | UART_MCR_OUT2 /* Set OUT2 - not always required, but when
407 * it is, it is ESSENTIAL for enabling interrupts
408 */
409 ,uart->base + UART_MCR); /* Modem Control Register */
410 break;
411 case SNDRV_SERIAL_MS124W_SA:
412 case SNDRV_SERIAL_MS124W_MB:
413 /* MS-124W can draw power from RTS and DTR if they
414 are in opposite states. */
415 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
416 uart->base + UART_MCR);
417 break;
418 case SNDRV_SERIAL_MS124T:
419 /* MS-124T can draw power from RTS and/or DTR (preferably
420 both) if they are both asserted. */
421 outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
422 uart->base + UART_MCR);
423 break;
424 }
425
426 if (uart->irq < 0) {
427 byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
428 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
429 ;
430 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
431 byte = UART_IER_RDI /* Enable Receiver data interrupt */
432 | UART_IER_MSI /* Enable Modem status interrupt */
433 ;
434 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
435 byte = UART_IER_RDI /* Enable Receiver data interrupt */
436 | UART_IER_MSI /* Enable Modem status interrupt */
437 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
438 ;
439 } else {
440 byte = UART_IER_RDI /* Enable Receiver data interrupt */
441 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
442 ;
443 }
444 outb(byte, uart->base + UART_IER); /* Interrupt enable Register */
445
446 inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */
447 inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */
448 inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */
449}
450
451static void snd_uart16550_do_close(struct snd_uart16550 * uart)
452{
453 if (uart->irq < 0)
454 snd_uart16550_del_timer(uart);
455
456 /* NOTE: may need to disable interrupts before de-registering out handler.
457 * For now, the consequences are harmless.
458 */
459
460 outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */
461 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
462 ,uart->base + UART_IER); /* Interrupt enable Register */
463
464 switch (uart->adaptor) {
465 default:
466 outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */
467 |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */
468 |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */
469 ,uart->base + UART_MCR); /* Modem Control Register */
470 break;
471 case SNDRV_SERIAL_MS124W_SA:
472 case SNDRV_SERIAL_MS124W_MB:
473 /* MS-124W can draw power from RTS and DTR if they
474 are in opposite states; leave it powered. */
475 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
476 uart->base + UART_MCR);
477 break;
478 case SNDRV_SERIAL_MS124T:
479 /* MS-124T can draw power from RTS and/or DTR (preferably
480 both) if they are both asserted; leave it powered. */
481 outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
482 uart->base + UART_MCR);
483 break;
484 }
485
486 inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
487
488 /* Restore old divisor */
489 if (uart->divisor != 0) {
490 outb(UART_LCR_DLAB /* Divisor latch access bit */
491 ,uart->base + UART_LCR); /* Line Control Register */
492 outb(uart->old_divisor_lsb
493 ,uart->base + UART_DLL); /* Divisor Latch Low */
494 outb(uart->old_divisor_msb
495 ,uart->base + UART_DLM); /* Divisor Latch High */
496 /* Restore old LCR (data bits, stop bits, parity, DLAB) */
497 outb(uart->old_line_ctrl_reg
498 ,uart->base + UART_LCR); /* Line Control Register */
499 }
500}
501
502static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
503{
504 unsigned long flags;
505 struct snd_uart16550 *uart = substream->rmidi->private_data;
506
507 spin_lock_irqsave(&uart->open_lock, flags);
508 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
509 snd_uart16550_do_open(uart);
510 uart->filemode |= SERIAL_MODE_INPUT_OPEN;
511 uart->midi_input[substream->number] = substream;
512 spin_unlock_irqrestore(&uart->open_lock, flags);
513 return 0;
514}
515
516static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
517{
518 unsigned long flags;
519 struct snd_uart16550 *uart = substream->rmidi->private_data;
520
521 spin_lock_irqsave(&uart->open_lock, flags);
522 uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
523 uart->midi_input[substream->number] = NULL;
524 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
525 snd_uart16550_do_close(uart);
526 spin_unlock_irqrestore(&uart->open_lock, flags);
527 return 0;
528}
529
530static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
531 int up)
532{
533 unsigned long flags;
534 struct snd_uart16550 *uart = substream->rmidi->private_data;
535
536 spin_lock_irqsave(&uart->open_lock, flags);
537 if (up)
538 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
539 else
540 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
541 spin_unlock_irqrestore(&uart->open_lock, flags);
542}
543
544static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
545{
546 unsigned long flags;
547 struct snd_uart16550 *uart = substream->rmidi->private_data;
548
549 spin_lock_irqsave(&uart->open_lock, flags);
550 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
551 snd_uart16550_do_open(uart);
552 uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
553 uart->midi_output[substream->number] = substream;
554 spin_unlock_irqrestore(&uart->open_lock, flags);
555 return 0;
556};
557
558static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
559{
560 unsigned long flags;
561 struct snd_uart16550 *uart = substream->rmidi->private_data;
562
563 spin_lock_irqsave(&uart->open_lock, flags);
564 uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
565 uart->midi_output[substream->number] = NULL;
566 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
567 snd_uart16550_do_close(uart);
568 spin_unlock_irqrestore(&uart->open_lock, flags);
569 return 0;
570};
571
572static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
573 int Num)
574{
575 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
576 return 1;
577 else
578 return 0;
579}
580
581static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
582 unsigned char byte)
583{
584 unsigned short buff_in = uart->buff_in;
585 if (uart->buff_in_count < TX_BUFF_SIZE) {
586 uart->tx_buff[buff_in] = byte;
587 buff_in++;
588 buff_in &= TX_BUFF_MASK;
589 uart->buff_in = buff_in;
590 uart->buff_in_count++;
591 if (uart->irq < 0) /* polling mode */
592 snd_uart16550_add_timer(uart);
593 return 1;
594 } else
595 return 0;
596}
597
598static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
599 struct snd_rawmidi_substream *substream,
600 unsigned char midi_byte)
601{
602 if (uart->buff_in_count == 0 /* Buffer empty? */
603 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
604 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
605 (uart->fifo_count == 0 /* FIFO empty? */
606 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
607
608 /* Tx Buffer Empty - try to write immediately */
609 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
610 /* Transmitter holding register (and Tx FIFO) empty */
611 uart->fifo_count = 1;
612 outb(midi_byte, uart->base + UART_TX);
613 } else {
614 if (uart->fifo_count < uart->fifo_limit) {
615 uart->fifo_count++;
616 outb(midi_byte, uart->base + UART_TX);
617 } else {
618 /* Cannot write (buffer empty) -
619 * put char in buffer */
620 snd_uart16550_write_buffer(uart, midi_byte);
621 }
622 }
623 } else {
624 if (!snd_uart16550_write_buffer(uart, midi_byte)) {
625 snd_printk(KERN_WARNING
626 "%s: Buffer overrun on device at 0x%lx\n",
627 uart->rmidi->name, uart->base);
628 return 0;
629 }
630 }
631
632 return 1;
633}
634
635static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
636{
637 unsigned long flags;
638 unsigned char midi_byte, addr_byte;
639 struct snd_uart16550 *uart = substream->rmidi->private_data;
640 char first;
641 static unsigned long lasttime = 0;
642
643 /* Interrupts are disabled during the updating of the tx_buff,
644 * since it is 'bad' to have two processes updating the same
645 * variables (ie buff_in & buff_out)
646 */
647
648 spin_lock_irqsave(&uart->open_lock, flags);
649
650 if (uart->irq < 0) /* polling */
651 snd_uart16550_io_loop(uart);
652
653 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
654 while (1) {
655 /* buffer full? */
656 /* in this mode we need two bytes of space */
657 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
658 break;
659 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
660 break;
661#ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
662 /* select exactly one of the four ports */
663 addr_byte = (1 << (substream->number + 4)) | 0x08;
664#else
665 /* select any combination of the four ports */
666 addr_byte = (substream->number << 4) | 0x08;
667 /* ...except none */
668 if (addr_byte == 0x08)
669 addr_byte = 0xf8;
670#endif
671 snd_uart16550_output_byte(uart, substream, addr_byte);
672 /* send midi byte */
673 snd_uart16550_output_byte(uart, substream, midi_byte);
674 }
675 } else {
676 first = 0;
677 while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
678 /* Also send F5 after 3 seconds with no data
679 * to handle device disconnect */
680 if (first == 0 &&
681 (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
682 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
683 (uart->prev_out != substream->number ||
684 time_after(jiffies, lasttime + 3*HZ))) {
685
686 if (snd_uart16550_buffer_can_write(uart, 3)) {
687 /* Roland Soundcanvas part selection */
688 /* If this substream of the data is
689 * different previous substream
690 * in this uart, send the change part
691 * event
692 */
693 uart->prev_out = substream->number;
694 /* change part */
695 snd_uart16550_output_byte(uart, substream,
696 0xf5);
697 /* data */
698 snd_uart16550_output_byte(uart, substream,
699 uart->prev_out + 1);
700 /* If midi_byte is a data byte,
701 * send the previous status byte */
702 if (midi_byte < 0x80 &&
703 uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
704 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
705 } else if (!uart->drop_on_full)
706 break;
707
708 }
709
710 /* send midi byte */
711 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
712 !uart->drop_on_full )
713 break;
714
715 if (midi_byte >= 0x80 && midi_byte < 0xf0)
716 uart->prev_status[uart->prev_out] = midi_byte;
717 first = 1;
718
719 snd_rawmidi_transmit_ack( substream, 1 );
720 }
721 lasttime = jiffies;
722 }
723 spin_unlock_irqrestore(&uart->open_lock, flags);
724}
725
726static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
727 int up)
728{
729 unsigned long flags;
730 struct snd_uart16550 *uart = substream->rmidi->private_data;
731
732 spin_lock_irqsave(&uart->open_lock, flags);
733 if (up)
734 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
735 else
736 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
737 spin_unlock_irqrestore(&uart->open_lock, flags);
738 if (up)
739 snd_uart16550_output_write(substream);
740}
741
742static const struct snd_rawmidi_ops snd_uart16550_output =
743{
744 .open = snd_uart16550_output_open,
745 .close = snd_uart16550_output_close,
746 .trigger = snd_uart16550_output_trigger,
747};
748
749static const struct snd_rawmidi_ops snd_uart16550_input =
750{
751 .open = snd_uart16550_input_open,
752 .close = snd_uart16550_input_close,
753 .trigger = snd_uart16550_input_trigger,
754};
755
756static int snd_uart16550_free(struct snd_uart16550 *uart)
757{
758 if (uart->irq >= 0)
759 free_irq(uart->irq, uart);
760 release_and_free_resource(uart->res_base);
761 kfree(uart);
762 return 0;
763};
764
765static int snd_uart16550_dev_free(struct snd_device *device)
766{
767 struct snd_uart16550 *uart = device->device_data;
768 return snd_uart16550_free(uart);
769}
770
771static int snd_uart16550_create(struct snd_card *card,
772 unsigned long iobase,
773 int irq,
774 unsigned int speed,
775 unsigned int base,
776 int adaptor,
777 int droponfull,
778 struct snd_uart16550 **ruart)
779{
780 static struct snd_device_ops ops = {
781 .dev_free = snd_uart16550_dev_free,
782 };
783 struct snd_uart16550 *uart;
784 int err;
785
786
787 if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
788 return -ENOMEM;
789 uart->adaptor = adaptor;
790 uart->card = card;
791 spin_lock_init(&uart->open_lock);
792 uart->irq = -1;
793 uart->base = iobase;
794 uart->drop_on_full = droponfull;
795
796 if ((err = snd_uart16550_detect(uart)) <= 0) {
797 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
798 snd_uart16550_free(uart);
799 return -ENODEV;
800 }
801
802 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
803 if (request_irq(irq, snd_uart16550_interrupt,
804 0, "Serial MIDI", uart)) {
805 snd_printk(KERN_WARNING
806 "irq %d busy. Using Polling.\n", irq);
807 } else {
808 uart->irq = irq;
809 }
810 }
811 uart->divisor = base / speed;
812 uart->speed = base / (unsigned int)uart->divisor;
813 uart->speed_base = base;
814 uart->prev_out = -1;
815 uart->prev_in = 0;
816 uart->rstatus = 0;
817 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
818 timer_setup(&uart->buffer_timer, snd_uart16550_buffer_timer, 0);
819 uart->timer_running = 0;
820
821 /* Register device */
822 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
823 snd_uart16550_free(uart);
824 return err;
825 }
826
827 switch (uart->adaptor) {
828 case SNDRV_SERIAL_MS124W_SA:
829 case SNDRV_SERIAL_MS124W_MB:
830 /* MS-124W can draw power from RTS and DTR if they
831 are in opposite states. */
832 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
833 break;
834 case SNDRV_SERIAL_MS124T:
835 /* MS-124T can draw power from RTS and/or DTR (preferably
836 both) if they are asserted. */
837 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
838 break;
839 default:
840 break;
841 }
842
843 if (ruart)
844 *ruart = uart;
845
846 return 0;
847}
848
849static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
850{
851 struct snd_rawmidi_substream *substream;
852
853 list_for_each_entry(substream, &stream->substreams, list) {
854 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
855 }
856}
857
858static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
859 int outs, int ins,
860 struct snd_rawmidi **rmidi)
861{
862 struct snd_rawmidi *rrawmidi;
863 int err;
864
865 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
866 outs, ins, &rrawmidi);
867 if (err < 0)
868 return err;
869 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
870 &snd_uart16550_input);
871 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
872 &snd_uart16550_output);
873 strcpy(rrawmidi->name, "Serial MIDI");
874 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
875 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
876 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
877 SNDRV_RAWMIDI_INFO_INPUT |
878 SNDRV_RAWMIDI_INFO_DUPLEX;
879 rrawmidi->private_data = uart;
880 if (rmidi)
881 *rmidi = rrawmidi;
882 return 0;
883}
884
885static int snd_serial_probe(struct platform_device *devptr)
886{
887 struct snd_card *card;
888 struct snd_uart16550 *uart;
889 int err;
890 int dev = devptr->id;
891
892 switch (adaptor[dev]) {
893 case SNDRV_SERIAL_SOUNDCANVAS:
894 ins[dev] = 1;
895 break;
896 case SNDRV_SERIAL_MS124T:
897 case SNDRV_SERIAL_MS124W_SA:
898 outs[dev] = 1;
899 ins[dev] = 1;
900 break;
901 case SNDRV_SERIAL_MS124W_MB:
902 outs[dev] = 16;
903 ins[dev] = 1;
904 break;
905 case SNDRV_SERIAL_GENERIC:
906 break;
907 default:
908 snd_printk(KERN_ERR
909 "Adaptor type is out of range 0-%d (%d)\n",
910 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
911 return -ENODEV;
912 }
913
914 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
915 snd_printk(KERN_ERR
916 "Count of outputs is out of range 1-%d (%d)\n",
917 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
918 return -ENODEV;
919 }
920
921 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
922 snd_printk(KERN_ERR
923 "Count of inputs is out of range 1-%d (%d)\n",
924 SNDRV_SERIAL_MAX_INS, ins[dev]);
925 return -ENODEV;
926 }
927
928 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
929 0, &card);
930 if (err < 0)
931 return err;
932
933 strcpy(card->driver, "Serial");
934 strcpy(card->shortname, "Serial MIDI (UART16550A)");
935
936 if ((err = snd_uart16550_create(card,
937 port[dev],
938 irq[dev],
939 speed[dev],
940 base[dev],
941 adaptor[dev],
942 droponfull[dev],
943 &uart)) < 0)
944 goto _err;
945
946 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
947 if (err < 0)
948 goto _err;
949
950 sprintf(card->longname, "%s [%s] at %#lx, irq %d",
951 card->shortname,
952 adaptor_names[uart->adaptor],
953 uart->base,
954 uart->irq);
955
956 if ((err = snd_card_register(card)) < 0)
957 goto _err;
958
959 platform_set_drvdata(devptr, card);
960 return 0;
961
962 _err:
963 snd_card_free(card);
964 return err;
965}
966
967static int snd_serial_remove(struct platform_device *devptr)
968{
969 snd_card_free(platform_get_drvdata(devptr));
970 return 0;
971}
972
973#define SND_SERIAL_DRIVER "snd_serial_u16550"
974
975static struct platform_driver snd_serial_driver = {
976 .probe = snd_serial_probe,
977 .remove = snd_serial_remove,
978 .driver = {
979 .name = SND_SERIAL_DRIVER,
980 },
981};
982
983static void snd_serial_unregister_all(void)
984{
985 int i;
986
987 for (i = 0; i < ARRAY_SIZE(devices); ++i)
988 platform_device_unregister(devices[i]);
989 platform_driver_unregister(&snd_serial_driver);
990}
991
992static int __init alsa_card_serial_init(void)
993{
994 int i, cards, err;
995
996 if ((err = platform_driver_register(&snd_serial_driver)) < 0)
997 return err;
998
999 cards = 0;
1000 for (i = 0; i < SNDRV_CARDS; i++) {
1001 struct platform_device *device;
1002 if (! enable[i])
1003 continue;
1004 device = platform_device_register_simple(SND_SERIAL_DRIVER,
1005 i, NULL, 0);
1006 if (IS_ERR(device))
1007 continue;
1008 if (!platform_get_drvdata(device)) {
1009 platform_device_unregister(device);
1010 continue;
1011 }
1012 devices[i] = device;
1013 cards++;
1014 }
1015 if (! cards) {
1016#ifdef MODULE
1017 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
1018#endif
1019 snd_serial_unregister_all();
1020 return -ENODEV;
1021 }
1022 return 0;
1023}
1024
1025static void __exit alsa_card_serial_exit(void)
1026{
1027 snd_serial_unregister_all();
1028}
1029
1030module_init(alsa_card_serial_init)
1031module_exit(alsa_card_serial_exit)