blob: e0bf5e77b43ae757d1f607b5c86a3636a4e371df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * serial.c
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
34#include <linux/interrupt.h>
Takashi Iwai9caf6b52005-11-17 16:02:30 +010035#include <linux/err.h>
36#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
38#include <linux/ioport.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040039#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <sound/core.h>
41#include <sound/rawmidi.h>
42#include <sound/initval.h>
43
44#include <linux/serial_reg.h>
Julia Lawallf11b7992008-01-07 13:33:45 +010045#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103072static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static 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};
Rusty Russella67ff6a2011-12-15 13:49:36 +103080static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100120struct snd_uart16550 {
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100121 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100126 int filemode; /* open status of file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100143 /* parameter for using of write loop */
144 short int fifo_limit; /* used in uart16550 */
145 short int fifo_count; /* used in uart16550 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100147 /* type of adaptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int adaptor;
149
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100150 /* inputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int prev_in;
152 unsigned char rstatus;
153
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100154 /* outputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int prev_out;
156 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
157
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100158 /* write buffer and its writing/reading position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100165 /* wait timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned int timer_running:1;
167 struct timer_list buffer_timer;
168
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100169};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Clemens Ladischf7a92752005-12-07 09:13:42 +0100171static struct platform_device *devices[SNDRV_CARDS];
172
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100173static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100175 if (!uart->timer_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* 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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100183static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100192static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 unsigned short buff_out = uart->buff_out;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100195 if (uart->buff_in_count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100209static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100223 if (c & 0x80)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 uart->rstatus = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
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)
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100232 /* 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])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (status & UART_LSR_OE)
Takashi Iwai45203832009-02-05 15:51:50 +0100244 snd_printk(KERN_WARNING
245 "%s: Overrun on device at 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100255 if (status & UART_LSR_THRE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 uart->fifo_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 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);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100261 while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
262 uart->buff_in_count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 snd_uart16550_buffer_output(uart);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100264 status = inb(uart->base + UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 } else {
267 /* Write loop */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100268 while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 && 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 */
David Howells7d12e782006-10-05 14:55:46 +0100296static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100298 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100300 uart = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_lock(&uart->open_lock);
302 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
303 spin_unlock(&uart->open_lock);
304 return IRQ_NONE;
305 }
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100306 /* indicate to the UART that the interrupt has been serviced */
307 inb(uart->base + UART_IIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 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;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100317 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100319 uart = (struct snd_uart16550 *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 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 */
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500331static int snd_uart16550_detect(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100348 /* uart detected unless one of the following tests should fail */
349 ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /* 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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100374static void snd_uart16550_do_open(struct snd_uart16550 * uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
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 }
Joe Perches561de312007-12-18 13:13:47 +0100459 outb(byte, uart->base + UART_IER); /* Interrupt enable Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100466static void snd_uart16550_do_close(struct snd_uart16550 * uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
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 */
Joe Perches561de312007-12-18 13:13:47 +0100477 ,uart->base + UART_IER); /* Interrupt enable Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
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
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100517static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100520 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
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
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100531static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100534 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100545static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
546 int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100549 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 spin_lock_irqsave(&uart->open_lock, flags);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100552 if (up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100554 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 spin_unlock_irqrestore(&uart->open_lock, flags);
557}
558
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100559static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100562 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
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
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100573static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100576 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100587static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
588 int Num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100590 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 1;
592 else
593 return 0;
594}
595
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100596static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
597 unsigned char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 unsigned short buff_in = uart->buff_in;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100600 if (uart->buff_in_count < TX_BUFF_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100613static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
614 struct snd_rawmidi_substream *substream,
615 unsigned char midi_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100617 if (uart->buff_in_count == 0 /* Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
619 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100620 (uart->fifo_count == 0 /* FIFO empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 && (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 {
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100633 /* Cannot write (buffer empty) -
634 * put char in buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 snd_uart16550_write_buffer(uart, midi_byte);
636 }
637 }
638 } else {
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100639 if (!snd_uart16550_write_buffer(uart, midi_byte)) {
Takashi Iwai45203832009-02-05 15:51:50 +0100640 snd_printk(KERN_WARNING
641 "%s: Buffer overrun on device at 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 uart->rmidi->name, uart->base);
643 return 0;
644 }
645 }
646
647 return 1;
648}
649
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100650static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 unsigned long flags;
653 unsigned char midi_byte, addr_byte;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100654 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 char first;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100656 static unsigned long lasttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Joe Perches561de312007-12-18 13:13:47 +0100658 /* Interrupts are disabled during the updating of the tx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * 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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100665 if (uart->irq < 0) /* polling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 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 */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100683 if (addr_byte == 0x08)
684 addr_byte = 0xf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#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;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100692 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 ||
Julia Lawallf11b7992008-01-07 13:33:45 +0100699 time_after(jiffies, lasttime + 3*HZ))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100701 if (snd_uart16550_buffer_can_write(uart, 3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* Roland Soundcanvas part selection */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100703 /* If this substream of the data is
704 * different previous substream
705 * in this uart, send the change part
706 * event
707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 uart->prev_out = substream->number;
709 /* change part */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100710 snd_uart16550_output_byte(uart, substream,
711 0xf5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* data */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100713 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100720 } else if (!uart->drop_on_full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722
723 }
724
725 /* send midi byte */
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100726 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
727 !uart->drop_on_full )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 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
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100741static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
742 int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 unsigned long flags;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100745 struct snd_uart16550 *uart = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 spin_lock_irqsave(&uart->open_lock, flags);
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100748 if (up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100750 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 spin_unlock_irqrestore(&uart->open_lock, flags);
753 if (up)
754 snd_uart16550_output_write(substream);
755}
756
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100757static struct snd_rawmidi_ops snd_uart16550_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 .open = snd_uart16550_output_open,
760 .close = snd_uart16550_output_close,
761 .trigger = snd_uart16550_output_trigger,
762};
763
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100764static struct snd_rawmidi_ops snd_uart16550_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 .open = snd_uart16550_input_open,
767 .close = snd_uart16550_input_close,
768 .trigger = snd_uart16550_input_trigger,
769};
770
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100771static int snd_uart16550_free(struct snd_uart16550 *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 if (uart->irq >= 0)
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100774 free_irq(uart->irq, uart);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200775 release_and_free_resource(uart->res_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 kfree(uart);
777 return 0;
778};
779
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100780static int snd_uart16550_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100782 struct snd_uart16550 *uart = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return snd_uart16550_free(uart);
784}
785
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500786static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100795 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 .dev_free = snd_uart16550_dev_free,
797 };
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100798 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int err;
800
801
Takashi Iwai561b2202005-09-09 14:22:34 +0200802 if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 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);
Adrian Bunk202728d2006-03-14 09:44:19 +0100813 snd_uart16550_free(uart);
Takashi Iwai68b8bc02006-01-23 15:21:28 +0100814 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
818 if (request_irq(irq, snd_uart16550_interrupt,
Yong Zhang88e24c32011-09-22 16:59:20 +0800819 0, "Serial MIDI", uart)) {
Takashi Iwai45203832009-02-05 15:51:50 +0100820 snd_printk(KERN_WARNING
821 "irq %d busy. Using Polling.\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 } 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
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500866static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100868 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100870 list_for_each_entry(substream, &stream->substreams, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
872 }
873}
874
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500875static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
876 int outs, int ins,
877 struct snd_rawmidi **rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100879 struct snd_rawmidi *rrawmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int err;
881
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100882 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
883 outs, ins, &rrawmidi);
884 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return err;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100886 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 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
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500902static int snd_serial_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100904 struct snd_card *card;
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100905 struct snd_uart16550 *uart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int err;
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100907 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
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:
Takashi Iwai45203832009-02-05 15:51:50 +0100925 snd_printk(KERN_ERR
926 "Adaptor type is out of range 0-%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
928 return -ENODEV;
929 }
930
931 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
Takashi Iwai45203832009-02-05 15:51:50 +0100932 snd_printk(KERN_ERR
933 "Count of outputs is out of range 1-%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
935 return -ENODEV;
936 }
937
938 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
Takashi Iwai45203832009-02-05 15:51:50 +0100939 snd_printk(KERN_ERR
940 "Count of inputs is out of range 1-%d (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 SNDRV_SERIAL_MAX_INS, ins[dev]);
942 return -ENODEV;
943 }
944
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100945 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
946 if (err < 0)
947 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 strcpy(card->driver, "Serial");
950 strcpy(card->shortname, "Serial MIDI (UART16550A)");
951
952 if ((err = snd_uart16550_create(card,
953 port[dev],
954 irq[dev],
955 speed[dev],
956 base[dev],
957 adaptor[dev],
958 droponfull[dev],
Takashi Iwai16dab542005-09-05 17:17:58 +0200959 &uart)) < 0)
960 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Takashi Iwai0b830bac2006-11-24 16:13:57 +0100962 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
963 if (err < 0)
Takashi Iwai16dab542005-09-05 17:17:58 +0200964 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Clemens Ladischbd0185c2009-05-05 15:39:39 +0200966 sprintf(card->longname, "%s [%s] at %#lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 card->shortname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 adaptor_names[uart->adaptor],
Clemens Ladischbd0185c2009-05-05 15:39:39 +0200969 uart->base,
970 uart->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100972 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200973
974 if ((err = snd_card_register(card)) < 0)
975 goto _err;
976
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100977 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
Takashi Iwai16dab542005-09-05 17:17:58 +0200979
980 _err:
981 snd_card_free(card);
982 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500985static int snd_serial_remove(struct platform_device *devptr)
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100986{
987 snd_card_free(platform_get_drvdata(devptr));
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100988 return 0;
989}
990
991#define SND_SERIAL_DRIVER "snd_serial_u16550"
992
993static struct platform_driver snd_serial_driver = {
994 .probe = snd_serial_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500995 .remove = snd_serial_remove,
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100996 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +0200997 .name = SND_SERIAL_DRIVER,
998 .owner = THIS_MODULE,
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100999 },
1000};
1001
Randy Dunlapbdec0c72007-06-25 12:07:38 +02001002static void snd_serial_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +01001003{
1004 int i;
1005
1006 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1007 platform_device_unregister(devices[i]);
1008 platform_driver_unregister(&snd_serial_driver);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011static int __init alsa_card_serial_init(void)
1012{
Takashi Iwai9caf6b52005-11-17 16:02:30 +01001013 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Takashi Iwai9caf6b52005-11-17 16:02:30 +01001015 if ((err = platform_driver_register(&snd_serial_driver)) < 0)
1016 return err;
1017
1018 cards = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +01001019 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai9caf6b52005-11-17 16:02:30 +01001020 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +01001021 if (! enable[i])
1022 continue;
Takashi Iwai9caf6b52005-11-17 16:02:30 +01001023 device = platform_device_register_simple(SND_SERIAL_DRIVER,
1024 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +02001025 if (IS_ERR(device))
1026 continue;
Rene Herman71524472006-04-13 12:58:06 +02001027 if (!platform_get_drvdata(device)) {
1028 platform_device_unregister(device);
1029 continue;
1030 }
Clemens Ladischf7a92752005-12-07 09:13:42 +01001031 devices[i] = device;
Takashi Iwai9caf6b52005-11-17 16:02:30 +01001032 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Takashi Iwai9caf6b52005-11-17 16:02:30 +01001034 if (! cards) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035#ifdef MODULE
1036 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
1037#endif
Rene Hermana182ee92006-04-13 12:57:11 +02001038 snd_serial_unregister_all();
1039 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 return 0;
1042}
1043
1044static void __exit alsa_card_serial_exit(void)
1045{
Clemens Ladischf7a92752005-12-07 09:13:42 +01001046 snd_serial_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049module_init(alsa_card_serial_init)
1050module_exit(alsa_card_serial_exit)