blob: 3cbc7a4adcb46443baf422840c885a2dff5eb34e [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * serial.c
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/interrupt.h>
Takashi Iwai9caf6b52005-11-17 16:02:30 +010022#include <linux/err.h>
23#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/ioport.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040026#include <linux/module.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010027#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/rawmidi.h>
30#include <sound/initval.h>
31
32#include <linux/serial_reg.h>
Julia Lawallf11b7992008-01-07 13:33:45 +010033#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035MODULE_DESCRIPTION("MIDI serial u16550");
36MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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
Takashi Iwai6c8454882020-01-05 15:48:13 +010044static const char * const adaptor_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 "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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103057static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static 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};
Rusty Russella67ff6a2011-12-15 13:49:36 +103065static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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.");
David Howellsb11ce422017-04-04 16:54:30 +010073module_param_hw_array(port, long, ioport, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
David Howellsb11ce422017-04-04 16:54:30 +010075module_param_hw_array(irq, int, irq, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100105struct snd_uart16550 {
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100106 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100111 int filemode; /* open status of file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 spinlock_t open_lock;
114
115 int irq;
116
117 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100127 /* parameter for using of write loop */
128 short int fifo_limit; /* used in uart16550 */
129 short int fifo_count; /* used in uart16550 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100131 /* type of adaptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int adaptor;
133
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100134 /* inputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int prev_in;
136 unsigned char rstatus;
137
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100138 /* outputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int prev_out;
140 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
141
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100142 /* write buffer and its writing/reading position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100149 /* wait timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned int timer_running:1;
151 struct timer_list buffer_timer;
152
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100153};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Clemens Ladischf7a92752005-12-07 09:13:42 +0100155static struct platform_device *devices[SNDRV_CARDS];
156
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100157static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100159 if (!uart->timer_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* timer 38600bps * 10bit * 16byte */
Takashi Iwaid80d05f2015-01-19 11:29:09 +0100161 mod_timer(&uart->buffer_timer, jiffies + (HZ + 255) / 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 uart->timer_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164}
165
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100166static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100175static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 unsigned short buff_out = uart->buff_out;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100178 if (uart->buff_in_count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100192static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100206 if (c & 0x80)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 uart->rstatus = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
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)
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100215 /* 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])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (status & UART_LSR_OE)
Takashi Iwai45203832009-02-05 15:51:50 +0100227 snd_printk(KERN_WARNING
228 "%s: Overrun on device at 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100238 if (status & UART_LSR_THRE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 uart->fifo_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100244 while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
245 uart->buff_in_count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 snd_uart16550_buffer_output(uart);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100247 status = inb(uart->base + UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 } else {
250 /* Write loop */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100251 while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 && 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 */
David Howells7d12e782006-10-05 14:55:46 +0100279static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100281 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100283 uart = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 spin_lock(&uart->open_lock);
285 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
286 spin_unlock(&uart->open_lock);
287 return IRQ_NONE;
288 }
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100289 /* indicate to the UART that the interrupt has been serviced */
290 inb(uart->base + UART_IIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 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. */
Kees Cookbc47ba92017-10-24 08:34:29 -0700297static void snd_uart16550_buffer_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100300 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Kees Cookbc47ba92017-10-24 08:34:29 -0700302 uart = from_timer(uart, t, buffer_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 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 */
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500314static int snd_uart16550_detect(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
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
Takashi Iwai60d03de2021-07-15 09:59:37 +0200325 if (!devm_request_region(uart->card->dev, io_base, 8, "Serial MIDI")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
327 return -EBUSY;
328 }
329
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100330 /* uart detected unless one of the following tests should fail */
331 ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
333 outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
334 c = inb(io_base + UART_IER);
335 /* The top four bits of the IER should always == 0 */
336 if ((c & 0xf0) != 0)
337 ok = 0; /* failed */
338
339 outb(0xaa, io_base + UART_SCR);
340 /* Write arbitrary data into the scratch reg */
341 c = inb(io_base + UART_SCR);
342 /* If it comes back, it's OK */
343 if (c != 0xaa)
344 ok = 0; /* failed */
345
346 outb(0x55, io_base + UART_SCR);
347 /* Write arbitrary data into the scratch reg */
348 c = inb(io_base + UART_SCR);
349 /* If it comes back, it's OK */
350 if (c != 0x55)
351 ok = 0; /* failed */
352
353 return ok;
354}
355
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100356static void snd_uart16550_do_open(struct snd_uart16550 * uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 char byte;
359
360 /* Initialize basic variables */
361 uart->buff_in_count = 0;
362 uart->buff_in = 0;
363 uart->buff_out = 0;
364 uart->fifo_limit = 1;
365 uart->fifo_count = 0;
366 uart->timer_running = 0;
367
368 outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */
369 | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */
370 | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */
371 | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */
372 /* NOTE: interrupt generated after T=(time)4-bytes
373 * if less than UART_FCR_TRIGGER bytes received
374 */
375 ,uart->base + UART_FCR); /* FIFO Control Register */
376
377 if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
378 uart->fifo_limit = 16;
379 if (uart->divisor != 0) {
380 uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
381 outb(UART_LCR_DLAB /* Divisor latch access bit */
382 ,uart->base + UART_LCR); /* Line Control Register */
383 uart->old_divisor_lsb = inb(uart->base + UART_DLL);
384 uart->old_divisor_msb = inb(uart->base + UART_DLM);
385
386 outb(uart->divisor
387 ,uart->base + UART_DLL); /* Divisor Latch Low */
388 outb(0
389 ,uart->base + UART_DLM); /* Divisor Latch High */
390 /* DLAB is reset to 0 in next outb() */
391 }
392 /* Set serial parameters (parity off, etc) */
393 outb(UART_LCR_WLEN8 /* 8 data-bits */
394 | 0 /* 1 stop-bit */
395 | 0 /* parity off */
396 | 0 /* DLAB = 0 */
397 ,uart->base + UART_LCR); /* Line Control Register */
398
399 switch (uart->adaptor) {
400 default:
401 outb(UART_MCR_RTS /* Set Request-To-Send line active */
402 | UART_MCR_DTR /* Set Data-Terminal-Ready line active */
403 | UART_MCR_OUT2 /* Set OUT2 - not always required, but when
404 * it is, it is ESSENTIAL for enabling interrupts
405 */
406 ,uart->base + UART_MCR); /* Modem Control Register */
407 break;
408 case SNDRV_SERIAL_MS124W_SA:
409 case SNDRV_SERIAL_MS124W_MB:
410 /* MS-124W can draw power from RTS and DTR if they
411 are in opposite states. */
412 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
413 uart->base + UART_MCR);
414 break;
415 case SNDRV_SERIAL_MS124T:
416 /* MS-124T can draw power from RTS and/or DTR (preferably
417 both) if they are both asserted. */
418 outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
419 uart->base + UART_MCR);
420 break;
421 }
422
423 if (uart->irq < 0) {
424 byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
425 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
426 ;
427 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
428 byte = UART_IER_RDI /* Enable Receiver data interrupt */
429 | UART_IER_MSI /* Enable Modem status interrupt */
430 ;
431 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
432 byte = UART_IER_RDI /* Enable Receiver data interrupt */
433 | UART_IER_MSI /* Enable Modem status interrupt */
434 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
435 ;
436 } else {
437 byte = UART_IER_RDI /* Enable Receiver data interrupt */
438 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
439 ;
440 }
Joe Perches561de312007-12-18 13:13:47 +0100441 outb(byte, uart->base + UART_IER); /* Interrupt enable Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */
444 inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */
445 inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */
446}
447
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100448static void snd_uart16550_do_close(struct snd_uart16550 * uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 if (uart->irq < 0)
451 snd_uart16550_del_timer(uart);
452
453 /* NOTE: may need to disable interrupts before de-registering out handler.
454 * For now, the consequences are harmless.
455 */
456
457 outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */
458 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
Joe Perches561de312007-12-18 13:13:47 +0100459 ,uart->base + UART_IER); /* Interrupt enable Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 switch (uart->adaptor) {
462 default:
463 outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */
464 |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */
465 |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */
466 ,uart->base + UART_MCR); /* Modem Control Register */
467 break;
468 case SNDRV_SERIAL_MS124W_SA:
469 case SNDRV_SERIAL_MS124W_MB:
470 /* MS-124W can draw power from RTS and DTR if they
471 are in opposite states; leave it powered. */
472 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
473 uart->base + UART_MCR);
474 break;
475 case SNDRV_SERIAL_MS124T:
476 /* MS-124T can draw power from RTS and/or DTR (preferably
477 both) if they are both asserted; leave it powered. */
478 outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
479 uart->base + UART_MCR);
480 break;
481 }
482
483 inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
484
485 /* Restore old divisor */
486 if (uart->divisor != 0) {
487 outb(UART_LCR_DLAB /* Divisor latch access bit */
488 ,uart->base + UART_LCR); /* Line Control Register */
489 outb(uart->old_divisor_lsb
490 ,uart->base + UART_DLL); /* Divisor Latch Low */
491 outb(uart->old_divisor_msb
492 ,uart->base + UART_DLM); /* Divisor Latch High */
493 /* Restore old LCR (data bits, stop bits, parity, DLAB) */
494 outb(uart->old_line_ctrl_reg
495 ,uart->base + UART_LCR); /* Line Control Register */
496 }
497}
498
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100499static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100502 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 spin_lock_irqsave(&uart->open_lock, flags);
505 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
506 snd_uart16550_do_open(uart);
507 uart->filemode |= SERIAL_MODE_INPUT_OPEN;
508 uart->midi_input[substream->number] = substream;
509 spin_unlock_irqrestore(&uart->open_lock, flags);
510 return 0;
511}
512
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100513static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100516 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 spin_lock_irqsave(&uart->open_lock, flags);
519 uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
520 uart->midi_input[substream->number] = NULL;
521 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
522 snd_uart16550_do_close(uart);
523 spin_unlock_irqrestore(&uart->open_lock, flags);
524 return 0;
525}
526
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100527static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
528 int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100531 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 spin_lock_irqsave(&uart->open_lock, flags);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100534 if (up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100536 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 spin_unlock_irqrestore(&uart->open_lock, flags);
539}
540
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100541static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100544 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 spin_lock_irqsave(&uart->open_lock, flags);
547 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
548 snd_uart16550_do_open(uart);
549 uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
550 uart->midi_output[substream->number] = substream;
551 spin_unlock_irqrestore(&uart->open_lock, flags);
552 return 0;
553};
554
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100555static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100558 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 spin_lock_irqsave(&uart->open_lock, flags);
561 uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
562 uart->midi_output[substream->number] = NULL;
563 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
564 snd_uart16550_do_close(uart);
565 spin_unlock_irqrestore(&uart->open_lock, flags);
566 return 0;
567};
568
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100569static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
570 int Num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100572 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return 1;
574 else
575 return 0;
576}
577
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100578static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
579 unsigned char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 unsigned short buff_in = uart->buff_in;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100582 if (uart->buff_in_count < TX_BUFF_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 uart->tx_buff[buff_in] = byte;
584 buff_in++;
585 buff_in &= TX_BUFF_MASK;
586 uart->buff_in = buff_in;
587 uart->buff_in_count++;
588 if (uart->irq < 0) /* polling mode */
589 snd_uart16550_add_timer(uart);
590 return 1;
591 } else
592 return 0;
593}
594
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100595static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
596 struct snd_rawmidi_substream *substream,
597 unsigned char midi_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100599 if (uart->buff_in_count == 0 /* Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
601 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100602 (uart->fifo_count == 0 /* FIFO empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
604
605 /* Tx Buffer Empty - try to write immediately */
606 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
607 /* Transmitter holding register (and Tx FIFO) empty */
608 uart->fifo_count = 1;
609 outb(midi_byte, uart->base + UART_TX);
610 } else {
611 if (uart->fifo_count < uart->fifo_limit) {
612 uart->fifo_count++;
613 outb(midi_byte, uart->base + UART_TX);
614 } else {
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100615 /* Cannot write (buffer empty) -
616 * put char in buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 snd_uart16550_write_buffer(uart, midi_byte);
618 }
619 }
620 } else {
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100621 if (!snd_uart16550_write_buffer(uart, midi_byte)) {
Takashi Iwai45203832009-02-05 15:51:50 +0100622 snd_printk(KERN_WARNING
623 "%s: Buffer overrun on device at 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 uart->rmidi->name, uart->base);
625 return 0;
626 }
627 }
628
629 return 1;
630}
631
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100632static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 unsigned long flags;
635 unsigned char midi_byte, addr_byte;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100636 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 char first;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100638 static unsigned long lasttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Joe Perches561de312007-12-18 13:13:47 +0100640 /* Interrupts are disabled during the updating of the tx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * since it is 'bad' to have two processes updating the same
642 * variables (ie buff_in & buff_out)
643 */
644
645 spin_lock_irqsave(&uart->open_lock, flags);
646
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100647 if (uart->irq < 0) /* polling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 snd_uart16550_io_loop(uart);
649
650 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
651 while (1) {
652 /* buffer full? */
653 /* in this mode we need two bytes of space */
654 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
655 break;
656 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
657 break;
658#ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
659 /* select exactly one of the four ports */
660 addr_byte = (1 << (substream->number + 4)) | 0x08;
661#else
662 /* select any combination of the four ports */
663 addr_byte = (substream->number << 4) | 0x08;
664 /* ...except none */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100665 if (addr_byte == 0x08)
666 addr_byte = 0xf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
668 snd_uart16550_output_byte(uart, substream, addr_byte);
669 /* send midi byte */
670 snd_uart16550_output_byte(uart, substream, midi_byte);
671 }
672 } else {
673 first = 0;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100674 while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
675 /* Also send F5 after 3 seconds with no data
676 * to handle device disconnect */
677 if (first == 0 &&
678 (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
679 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
680 (uart->prev_out != substream->number ||
Julia Lawallf11b7992008-01-07 13:33:45 +0100681 time_after(jiffies, lasttime + 3*HZ))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100683 if (snd_uart16550_buffer_can_write(uart, 3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* Roland Soundcanvas part selection */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100685 /* If this substream of the data is
686 * different previous substream
687 * in this uart, send the change part
688 * event
689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 uart->prev_out = substream->number;
691 /* change part */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100692 snd_uart16550_output_byte(uart, substream,
693 0xf5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* data */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100695 snd_uart16550_output_byte(uart, substream,
696 uart->prev_out + 1);
697 /* If midi_byte is a data byte,
698 * send the previous status byte */
699 if (midi_byte < 0x80 &&
700 uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100702 } else if (!uart->drop_on_full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704
705 }
706
707 /* send midi byte */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100708 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
709 !uart->drop_on_full )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711
712 if (midi_byte >= 0x80 && midi_byte < 0xf0)
713 uart->prev_status[uart->prev_out] = midi_byte;
714 first = 1;
715
716 snd_rawmidi_transmit_ack( substream, 1 );
717 }
718 lasttime = jiffies;
719 }
720 spin_unlock_irqrestore(&uart->open_lock, flags);
721}
722
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100723static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
724 int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100727 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 spin_lock_irqsave(&uart->open_lock, flags);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100730 if (up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100732 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 spin_unlock_irqrestore(&uart->open_lock, flags);
735 if (up)
736 snd_uart16550_output_write(substream);
737}
738
Takashi Iwaic36f4862017-01-05 17:28:39 +0100739static const struct snd_rawmidi_ops snd_uart16550_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 .open = snd_uart16550_output_open,
742 .close = snd_uart16550_output_close,
743 .trigger = snd_uart16550_output_trigger,
744};
745
Takashi Iwaic36f4862017-01-05 17:28:39 +0100746static const struct snd_rawmidi_ops snd_uart16550_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 .open = snd_uart16550_input_open,
749 .close = snd_uart16550_input_close,
750 .trigger = snd_uart16550_input_trigger,
751};
752
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500753static int snd_uart16550_create(struct snd_card *card,
754 unsigned long iobase,
755 int irq,
756 unsigned int speed,
757 unsigned int base,
758 int adaptor,
759 int droponfull,
760 struct snd_uart16550 **ruart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100762 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int err;
764
765
Takashi Iwai60d03de2021-07-15 09:59:37 +0200766 uart = devm_kzalloc(card->dev, sizeof(*uart), GFP_KERNEL);
Takashi Iwaid0ad13e2021-06-08 16:05:36 +0200767 if (!uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -ENOMEM;
769 uart->adaptor = adaptor;
770 uart->card = card;
771 spin_lock_init(&uart->open_lock);
772 uart->irq = -1;
773 uart->base = iobase;
774 uart->drop_on_full = droponfull;
775
Takashi Iwaid0ad13e2021-06-08 16:05:36 +0200776 err = snd_uart16550_detect(uart);
777 if (err <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
Takashi Iwai68b8bc02006-01-23 15:21:28 +0100779 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
782 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
Takashi Iwai60d03de2021-07-15 09:59:37 +0200783 if (devm_request_irq(card->dev, irq, snd_uart16550_interrupt,
784 0, "Serial MIDI", uart)) {
Takashi Iwai45203832009-02-05 15:51:50 +0100785 snd_printk(KERN_WARNING
786 "irq %d busy. Using Polling.\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 } else {
788 uart->irq = irq;
789 }
790 }
791 uart->divisor = base / speed;
792 uart->speed = base / (unsigned int)uart->divisor;
793 uart->speed_base = base;
794 uart->prev_out = -1;
795 uart->prev_in = 0;
796 uart->rstatus = 0;
797 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
Kees Cookbc47ba92017-10-24 08:34:29 -0700798 timer_setup(&uart->buffer_timer, snd_uart16550_buffer_timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 uart->timer_running = 0;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 switch (uart->adaptor) {
802 case SNDRV_SERIAL_MS124W_SA:
803 case SNDRV_SERIAL_MS124W_MB:
804 /* MS-124W can draw power from RTS and DTR if they
805 are in opposite states. */
806 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
807 break;
808 case SNDRV_SERIAL_MS124T:
809 /* MS-124T can draw power from RTS and/or DTR (preferably
810 both) if they are asserted. */
811 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
812 break;
813 default:
814 break;
815 }
816
817 if (ruart)
818 *ruart = uart;
819
820 return 0;
821}
822
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500823static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100825 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100827 list_for_each_entry(substream, &stream->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
829 }
830}
831
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500832static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
833 int outs, int ins,
834 struct snd_rawmidi **rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100836 struct snd_rawmidi *rrawmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int err;
838
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100839 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
840 outs, ins, &rrawmidi);
841 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return err;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100843 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
844 &snd_uart16550_input);
845 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
846 &snd_uart16550_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 strcpy(rrawmidi->name, "Serial MIDI");
848 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
849 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
850 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
851 SNDRV_RAWMIDI_INFO_INPUT |
852 SNDRV_RAWMIDI_INFO_DUPLEX;
853 rrawmidi->private_data = uart;
854 if (rmidi)
855 *rmidi = rrawmidi;
856 return 0;
857}
858
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500859static int snd_serial_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100861 struct snd_card *card;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100862 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int err;
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100864 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 switch (adaptor[dev]) {
867 case SNDRV_SERIAL_SOUNDCANVAS:
868 ins[dev] = 1;
869 break;
870 case SNDRV_SERIAL_MS124T:
871 case SNDRV_SERIAL_MS124W_SA:
872 outs[dev] = 1;
873 ins[dev] = 1;
874 break;
875 case SNDRV_SERIAL_MS124W_MB:
876 outs[dev] = 16;
877 ins[dev] = 1;
878 break;
879 case SNDRV_SERIAL_GENERIC:
880 break;
881 default:
Takashi Iwai45203832009-02-05 15:51:50 +0100882 snd_printk(KERN_ERR
883 "Adaptor type is out of range 0-%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
885 return -ENODEV;
886 }
887
888 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
Takashi Iwai45203832009-02-05 15:51:50 +0100889 snd_printk(KERN_ERR
890 "Count of outputs is out of range 1-%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
892 return -ENODEV;
893 }
894
895 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
Takashi Iwai45203832009-02-05 15:51:50 +0100896 snd_printk(KERN_ERR
897 "Count of inputs is out of range 1-%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 SNDRV_SERIAL_MAX_INS, ins[dev]);
899 return -ENODEV;
900 }
901
Takashi Iwai60d03de2021-07-15 09:59:37 +0200902 err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
903 0, &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100904 if (err < 0)
905 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 strcpy(card->driver, "Serial");
908 strcpy(card->shortname, "Serial MIDI (UART16550A)");
909
Takashi Iwaid0ad13e2021-06-08 16:05:36 +0200910 err = snd_uart16550_create(card, port[dev], irq[dev], speed[dev],
911 base[dev], adaptor[dev], droponfull[dev],
912 &uart);
913 if (err < 0)
Takashi Iwai60d03de2021-07-15 09:59:37 +0200914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100916 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
917 if (err < 0)
Takashi Iwai60d03de2021-07-15 09:59:37 +0200918 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Clemens Ladischbd0185c2009-05-05 15:39:39 +0200920 sprintf(card->longname, "%s [%s] at %#lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 card->shortname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 adaptor_names[uart->adaptor],
Clemens Ladischbd0185c2009-05-05 15:39:39 +0200923 uart->base,
924 uart->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Takashi Iwaid0ad13e2021-06-08 16:05:36 +0200926 err = snd_card_register(card);
927 if (err < 0)
Takashi Iwai60d03de2021-07-15 09:59:37 +0200928 return err;
Takashi Iwai16dab542005-09-05 17:17:58 +0200929
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100930 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100932}
933
934#define SND_SERIAL_DRIVER "snd_serial_u16550"
935
936static struct platform_driver snd_serial_driver = {
937 .probe = snd_serial_probe,
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100938 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +0200939 .name = SND_SERIAL_DRIVER,
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100940 },
941};
942
Randy Dunlapbdec0c72007-06-25 12:07:38 +0200943static void snd_serial_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100944{
945 int i;
946
947 for (i = 0; i < ARRAY_SIZE(devices); ++i)
948 platform_device_unregister(devices[i]);
949 platform_driver_unregister(&snd_serial_driver);
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952static int __init alsa_card_serial_init(void)
953{
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100954 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Takashi Iwaid0ad13e2021-06-08 16:05:36 +0200956 err = platform_driver_register(&snd_serial_driver);
957 if (err < 0)
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100958 return err;
959
960 cards = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100961 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100962 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100963 if (! enable[i])
964 continue;
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100965 device = platform_device_register_simple(SND_SERIAL_DRIVER,
966 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200967 if (IS_ERR(device))
968 continue;
Rene Herman71524472006-04-13 12:58:06 +0200969 if (!platform_get_drvdata(device)) {
970 platform_device_unregister(device);
971 continue;
972 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100973 devices[i] = device;
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100974 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100976 if (! cards) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#ifdef MODULE
978 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
979#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200980 snd_serial_unregister_all();
981 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 return 0;
984}
985
986static void __exit alsa_card_serial_exit(void)
987{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100988 snd_serial_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991module_init(alsa_card_serial_init)
992module_exit(alsa_card_serial_exit)