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