blob: 3381a43c592e7fb72a4cae941202cb348074ad43 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * serial.c
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
34#include <linux/init.h>
35#include <linux/interrupt.h>
Takashi Iwai9caf6b52005-11-17 16:02:30 +010036#include <linux/err.h>
37#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/slab.h>
39#include <linux/ioport.h>
40#include <linux/moduleparam.h>
41#include <sound/core.h>
42#include <sound/rawmidi.h>
43#include <sound/initval.h>
44
45#include <linux/serial_reg.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 int 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 int 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
120typedef struct _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
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} snd_uart16550_t;
170
Jesper Juhl77933d72005-07-27 11:46:09 -0700171static inline void snd_uart16550_add_timer(snd_uart16550_t *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 if (! uart->timer_running) {
174 /* timer 38600bps * 10bit * 16byte */
175 uart->buffer_timer.expires = jiffies + (HZ+255)/256;
176 uart->timer_running = 1;
177 add_timer(&uart->buffer_timer);
178 }
179}
180
Jesper Juhl77933d72005-07-27 11:46:09 -0700181static inline void snd_uart16550_del_timer(snd_uart16550_t *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 if (uart->timer_running) {
184 del_timer(&uart->buffer_timer);
185 uart->timer_running = 0;
186 }
187}
188
189/* This macro is only used in snd_uart16550_io_loop */
Jesper Juhl77933d72005-07-27 11:46:09 -0700190static inline void snd_uart16550_buffer_output(snd_uart16550_t *uart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 unsigned short buff_out = uart->buff_out;
193 if( uart->buff_in_count > 0 ) {
194 outb(uart->tx_buff[buff_out], uart->base + UART_TX);
195 uart->fifo_count++;
196 buff_out++;
197 buff_out &= TX_BUFF_MASK;
198 uart->buff_out = buff_out;
199 uart->buff_in_count--;
200 }
201}
202
203/* This loop should be called with interrupts disabled
204 * We don't want to interrupt this,
205 * as we're already handling an interrupt
206 */
207static void snd_uart16550_io_loop(snd_uart16550_t * uart)
208{
209 unsigned char c, status;
210 int substream;
211
212 /* recall previous stream */
213 substream = uart->prev_in;
214
215 /* Read Loop */
216 while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
217 /* while receive data ready */
218 c = inb(uart->base + UART_RX);
219
220 /* keep track of last status byte */
221 if (c & 0x80) {
222 uart->rstatus = c;
223 }
224
225 /* handle stream switch */
226 if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
227 if (uart->rstatus == 0xf5) {
228 if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
229 substream = c - 1;
230 if (c != 0xf5)
231 uart->rstatus = 0; /* prevent future bytes from being interpreted as streams */
232 }
233 else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && (uart->midi_input[substream] != NULL)) {
234 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
235 }
236 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && (uart->midi_input[substream] != NULL)) {
237 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
238 }
239
240 if (status & UART_LSR_OE)
241 snd_printk("%s: Overrun on device at 0x%lx\n",
242 uart->rmidi->name, uart->base);
243 }
244
245 /* remember the last stream */
246 uart->prev_in = substream;
247
248 /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not,
249 buffer is never filled. */
250 /* Check write status */
251 if (status & UART_LSR_THRE) {
252 uart->fifo_count = 0;
253 }
254 if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
255 || uart->adaptor == SNDRV_SERIAL_GENERIC) {
256 /* Can't use FIFO, must send only when CTS is true */
257 status = inb(uart->base + UART_MSR);
258 while( (uart->fifo_count == 0) && (status & UART_MSR_CTS) &&
259 (uart->buff_in_count > 0) ) {
260 snd_uart16550_buffer_output(uart);
261 status = inb( uart->base + UART_MSR );
262 }
263 } else {
264 /* Write loop */
265 while (uart->fifo_count < uart->fifo_limit /* Can we write ? */
266 && uart->buff_in_count > 0) /* Do we want to? */
267 snd_uart16550_buffer_output(uart);
268 }
269 if (uart->irq < 0 && uart->buff_in_count > 0)
270 snd_uart16550_add_timer(uart);
271}
272
273/* NOTES ON SERVICING INTERUPTS
274 * ---------------------------
275 * After receiving a interrupt, it is important to indicate to the UART that
276 * this has been done.
277 * For a Rx interrupt, this is done by reading the received byte.
278 * For a Tx interrupt this is done by either:
279 * a) Writing a byte
280 * b) Reading the IIR
281 * It is particularly important to read the IIR if a Tx interrupt is received
282 * when there is no data in tx_buff[], as in this case there no other
283 * indication that the interrupt has been serviced, and it remains outstanding
284 * indefinitely. This has the curious side effect that and no further interrupts
285 * will be generated from this device AT ALL!!.
286 * It is also desirable to clear outstanding interrupts when the device is
287 * opened/closed.
288 *
289 *
290 * Note that some devices need OUT2 to be set before they will generate
291 * interrupts at all. (Possibly tied to an internal pull-up on CTS?)
292 */
293static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id, struct pt_regs *regs)
294{
295 snd_uart16550_t *uart;
296
297 uart = (snd_uart16550_t *) dev_id;
298 spin_lock(&uart->open_lock);
299 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
300 spin_unlock(&uart->open_lock);
301 return IRQ_NONE;
302 }
303 inb(uart->base + UART_IIR); /* indicate to the UART that the interrupt has been serviced */
304 snd_uart16550_io_loop(uart);
305 spin_unlock(&uart->open_lock);
306 return IRQ_HANDLED;
307}
308
309/* When the polling mode, this function calls snd_uart16550_io_loop. */
310static void snd_uart16550_buffer_timer(unsigned long data)
311{
312 unsigned long flags;
313 snd_uart16550_t *uart;
314
315 uart = (snd_uart16550_t *)data;
316 spin_lock_irqsave(&uart->open_lock, flags);
317 snd_uart16550_del_timer(uart);
318 snd_uart16550_io_loop(uart);
319 spin_unlock_irqrestore(&uart->open_lock, flags);
320}
321
322/*
323 * this method probes, if an uart sits on given port
324 * return 0 if found
325 * return negative error if not found
326 */
327static int __init snd_uart16550_detect(snd_uart16550_t *uart)
328{
329 unsigned long io_base = uart->base;
330 int ok;
331 unsigned char c;
332
333 /* Do some vague tests for the presence of the uart */
334 if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
335 return -ENODEV; /* Not configured */
336 }
337
338 uart->res_base = request_region(io_base, 8, "Serial MIDI");
339 if (uart->res_base == NULL) {
340 snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
341 return -EBUSY;
342 }
343
344 ok = 1; /* uart detected unless one of the following tests should fail */
345 /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */
346 outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */
347 c = inb(io_base + UART_IER);
348 /* The top four bits of the IER should always == 0 */
349 if ((c & 0xf0) != 0)
350 ok = 0; /* failed */
351
352 outb(0xaa, io_base + UART_SCR);
353 /* Write arbitrary data into the scratch reg */
354 c = inb(io_base + UART_SCR);
355 /* If it comes back, it's OK */
356 if (c != 0xaa)
357 ok = 0; /* failed */
358
359 outb(0x55, io_base + UART_SCR);
360 /* Write arbitrary data into the scratch reg */
361 c = inb(io_base + UART_SCR);
362 /* If it comes back, it's OK */
363 if (c != 0x55)
364 ok = 0; /* failed */
365
366 return ok;
367}
368
369static void snd_uart16550_do_open(snd_uart16550_t * uart)
370{
371 char byte;
372
373 /* Initialize basic variables */
374 uart->buff_in_count = 0;
375 uart->buff_in = 0;
376 uart->buff_out = 0;
377 uart->fifo_limit = 1;
378 uart->fifo_count = 0;
379 uart->timer_running = 0;
380
381 outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */
382 | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */
383 | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */
384 | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */
385 /* NOTE: interrupt generated after T=(time)4-bytes
386 * if less than UART_FCR_TRIGGER bytes received
387 */
388 ,uart->base + UART_FCR); /* FIFO Control Register */
389
390 if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
391 uart->fifo_limit = 16;
392 if (uart->divisor != 0) {
393 uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
394 outb(UART_LCR_DLAB /* Divisor latch access bit */
395 ,uart->base + UART_LCR); /* Line Control Register */
396 uart->old_divisor_lsb = inb(uart->base + UART_DLL);
397 uart->old_divisor_msb = inb(uart->base + UART_DLM);
398
399 outb(uart->divisor
400 ,uart->base + UART_DLL); /* Divisor Latch Low */
401 outb(0
402 ,uart->base + UART_DLM); /* Divisor Latch High */
403 /* DLAB is reset to 0 in next outb() */
404 }
405 /* Set serial parameters (parity off, etc) */
406 outb(UART_LCR_WLEN8 /* 8 data-bits */
407 | 0 /* 1 stop-bit */
408 | 0 /* parity off */
409 | 0 /* DLAB = 0 */
410 ,uart->base + UART_LCR); /* Line Control Register */
411
412 switch (uart->adaptor) {
413 default:
414 outb(UART_MCR_RTS /* Set Request-To-Send line active */
415 | UART_MCR_DTR /* Set Data-Terminal-Ready line active */
416 | UART_MCR_OUT2 /* Set OUT2 - not always required, but when
417 * it is, it is ESSENTIAL for enabling interrupts
418 */
419 ,uart->base + UART_MCR); /* Modem Control Register */
420 break;
421 case SNDRV_SERIAL_MS124W_SA:
422 case SNDRV_SERIAL_MS124W_MB:
423 /* MS-124W can draw power from RTS and DTR if they
424 are in opposite states. */
425 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
426 uart->base + UART_MCR);
427 break;
428 case SNDRV_SERIAL_MS124T:
429 /* MS-124T can draw power from RTS and/or DTR (preferably
430 both) if they are both asserted. */
431 outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
432 uart->base + UART_MCR);
433 break;
434 }
435
436 if (uart->irq < 0) {
437 byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */
438 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
439 ;
440 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
441 byte = UART_IER_RDI /* Enable Receiver data interrupt */
442 | UART_IER_MSI /* Enable Modem status interrupt */
443 ;
444 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
445 byte = UART_IER_RDI /* Enable Receiver data interrupt */
446 | UART_IER_MSI /* Enable Modem status interrupt */
447 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
448 ;
449 } else {
450 byte = UART_IER_RDI /* Enable Receiver data interrupt */
451 | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */
452 ;
453 }
454 outb(byte, uart->base + UART_IER); /* Interupt enable Register */
455
456 inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */
457 inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */
458 inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */
459}
460
461static void snd_uart16550_do_close(snd_uart16550_t * uart)
462{
463 if (uart->irq < 0)
464 snd_uart16550_del_timer(uart);
465
466 /* NOTE: may need to disable interrupts before de-registering out handler.
467 * For now, the consequences are harmless.
468 */
469
470 outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */
471 |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */
472 ,uart->base + UART_IER); /* Interupt enable Register */
473
474 switch (uart->adaptor) {
475 default:
476 outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */
477 |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */
478 |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */
479 ,uart->base + UART_MCR); /* Modem Control Register */
480 break;
481 case SNDRV_SERIAL_MS124W_SA:
482 case SNDRV_SERIAL_MS124W_MB:
483 /* MS-124W can draw power from RTS and DTR if they
484 are in opposite states; leave it powered. */
485 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
486 uart->base + UART_MCR);
487 break;
488 case SNDRV_SERIAL_MS124T:
489 /* MS-124T can draw power from RTS and/or DTR (preferably
490 both) if they are both asserted; leave it powered. */
491 outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
492 uart->base + UART_MCR);
493 break;
494 }
495
496 inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */
497
498 /* Restore old divisor */
499 if (uart->divisor != 0) {
500 outb(UART_LCR_DLAB /* Divisor latch access bit */
501 ,uart->base + UART_LCR); /* Line Control Register */
502 outb(uart->old_divisor_lsb
503 ,uart->base + UART_DLL); /* Divisor Latch Low */
504 outb(uart->old_divisor_msb
505 ,uart->base + UART_DLM); /* Divisor Latch High */
506 /* Restore old LCR (data bits, stop bits, parity, DLAB) */
507 outb(uart->old_line_ctrl_reg
508 ,uart->base + UART_LCR); /* Line Control Register */
509 }
510}
511
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100512static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned long flags;
515 snd_uart16550_t *uart = substream->rmidi->private_data;
516
517 spin_lock_irqsave(&uart->open_lock, flags);
518 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
519 snd_uart16550_do_open(uart);
520 uart->filemode |= SERIAL_MODE_INPUT_OPEN;
521 uart->midi_input[substream->number] = substream;
522 spin_unlock_irqrestore(&uart->open_lock, flags);
523 return 0;
524}
525
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100526static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 unsigned long flags;
529 snd_uart16550_t *uart = substream->rmidi->private_data;
530
531 spin_lock_irqsave(&uart->open_lock, flags);
532 uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
533 uart->midi_input[substream->number] = NULL;
534 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
535 snd_uart16550_do_close(uart);
536 spin_unlock_irqrestore(&uart->open_lock, flags);
537 return 0;
538}
539
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100540static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 unsigned long flags;
543 snd_uart16550_t *uart = substream->rmidi->private_data;
544
545 spin_lock_irqsave(&uart->open_lock, flags);
546 if (up) {
547 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
548 } else {
549 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
550 }
551 spin_unlock_irqrestore(&uart->open_lock, flags);
552}
553
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100554static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 unsigned long flags;
557 snd_uart16550_t *uart = substream->rmidi->private_data;
558
559 spin_lock_irqsave(&uart->open_lock, flags);
560 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
561 snd_uart16550_do_open(uart);
562 uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
563 uart->midi_output[substream->number] = substream;
564 spin_unlock_irqrestore(&uart->open_lock, flags);
565 return 0;
566};
567
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100568static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 unsigned long flags;
571 snd_uart16550_t *uart = substream->rmidi->private_data;
572
573 spin_lock_irqsave(&uart->open_lock, flags);
574 uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
575 uart->midi_output[substream->number] = NULL;
576 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
577 snd_uart16550_do_close(uart);
578 spin_unlock_irqrestore(&uart->open_lock, flags);
579 return 0;
580};
581
Jesper Juhl77933d72005-07-27 11:46:09 -0700582static inline int snd_uart16550_buffer_can_write( snd_uart16550_t *uart, int Num )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 if( uart->buff_in_count + Num < TX_BUFF_SIZE )
585 return 1;
586 else
587 return 0;
588}
589
Jesper Juhl77933d72005-07-27 11:46:09 -0700590static inline int snd_uart16550_write_buffer(snd_uart16550_t *uart, unsigned char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 unsigned short buff_in = uart->buff_in;
593 if( uart->buff_in_count < TX_BUFF_SIZE ) {
594 uart->tx_buff[buff_in] = byte;
595 buff_in++;
596 buff_in &= TX_BUFF_MASK;
597 uart->buff_in = buff_in;
598 uart->buff_in_count++;
599 if (uart->irq < 0) /* polling mode */
600 snd_uart16550_add_timer(uart);
601 return 1;
602 } else
603 return 0;
604}
605
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100606static int snd_uart16550_output_byte(snd_uart16550_t *uart, struct snd_rawmidi_substream *substream, unsigned char midi_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 if (uart->buff_in_count == 0 /* Buffer empty? */
609 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
610 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
611 (uart->fifo_count == 0 /* FIFO empty? */
612 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */
613
614 /* Tx Buffer Empty - try to write immediately */
615 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
616 /* Transmitter holding register (and Tx FIFO) empty */
617 uart->fifo_count = 1;
618 outb(midi_byte, uart->base + UART_TX);
619 } else {
620 if (uart->fifo_count < uart->fifo_limit) {
621 uart->fifo_count++;
622 outb(midi_byte, uart->base + UART_TX);
623 } else {
624 /* Cannot write (buffer empty) - put char in buffer */
625 snd_uart16550_write_buffer(uart, midi_byte);
626 }
627 }
628 } else {
629 if( !snd_uart16550_write_buffer(uart, midi_byte) ) {
630 snd_printk("%s: Buffer overrun on device at 0x%lx\n",
631 uart->rmidi->name, uart->base);
632 return 0;
633 }
634 }
635
636 return 1;
637}
638
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100639static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 unsigned long flags;
642 unsigned char midi_byte, addr_byte;
643 snd_uart16550_t *uart = substream->rmidi->private_data;
644 char first;
645 static unsigned long lasttime=0;
646
647 /* Interupts are disabled during the updating of the tx_buff,
648 * since it is 'bad' to have two processes updating the same
649 * variables (ie buff_in & buff_out)
650 */
651
652 spin_lock_irqsave(&uart->open_lock, flags);
653
654 if (uart->irq < 0) //polling
655 snd_uart16550_io_loop(uart);
656
657 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
658 while (1) {
659 /* buffer full? */
660 /* in this mode we need two bytes of space */
661 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
662 break;
663 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
664 break;
665#ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
666 /* select exactly one of the four ports */
667 addr_byte = (1 << (substream->number + 4)) | 0x08;
668#else
669 /* select any combination of the four ports */
670 addr_byte = (substream->number << 4) | 0x08;
671 /* ...except none */
672 if (addr_byte == 0x08) addr_byte = 0xf8;
673#endif
674 snd_uart16550_output_byte(uart, substream, addr_byte);
675 /* send midi byte */
676 snd_uart16550_output_byte(uart, substream, midi_byte);
677 }
678 } else {
679 first = 0;
680 while( 1 == snd_rawmidi_transmit_peek(substream, &midi_byte, 1) ) {
681 /* Also send F5 after 3 seconds with no data to handle device disconnect */
682 if (first == 0 && (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
683 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
684 (uart->prev_out != substream->number || jiffies-lasttime > 3*HZ)) {
685
686 if( snd_uart16550_buffer_can_write( uart, 3 ) ) {
687 /* Roland Soundcanvas part selection */
688 /* If this substream of the data is different previous
689 substream in this uart, send the change part event */
690 uart->prev_out = substream->number;
691 /* change part */
692 snd_uart16550_output_byte(uart, substream, 0xf5);
693 /* data */
694 snd_uart16550_output_byte(uart, substream, uart->prev_out + 1);
695 /* If midi_byte is a data byte, send the previous status byte */
696 if ((midi_byte < 0x80) && (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS))
697 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
698 } else if( !uart->drop_on_full )
699 break;
700
701 }
702
703 /* send midi byte */
704 if( !snd_uart16550_output_byte(uart, substream, midi_byte) && !uart->drop_on_full )
705 break;
706
707 if (midi_byte >= 0x80 && midi_byte < 0xf0)
708 uart->prev_status[uart->prev_out] = midi_byte;
709 first = 1;
710
711 snd_rawmidi_transmit_ack( substream, 1 );
712 }
713 lasttime = jiffies;
714 }
715 spin_unlock_irqrestore(&uart->open_lock, flags);
716}
717
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100718static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 unsigned long flags;
721 snd_uart16550_t *uart = substream->rmidi->private_data;
722
723 spin_lock_irqsave(&uart->open_lock, flags);
724 if (up) {
725 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
726 } else {
727 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
728 }
729 spin_unlock_irqrestore(&uart->open_lock, flags);
730 if (up)
731 snd_uart16550_output_write(substream);
732}
733
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100734static struct snd_rawmidi_ops snd_uart16550_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 .open = snd_uart16550_output_open,
737 .close = snd_uart16550_output_close,
738 .trigger = snd_uart16550_output_trigger,
739};
740
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100741static struct snd_rawmidi_ops snd_uart16550_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 .open = snd_uart16550_input_open,
744 .close = snd_uart16550_input_close,
745 .trigger = snd_uart16550_input_trigger,
746};
747
748static int snd_uart16550_free(snd_uart16550_t *uart)
749{
750 if (uart->irq >= 0)
751 free_irq(uart->irq, (void *)uart);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200752 release_and_free_resource(uart->res_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 kfree(uart);
754 return 0;
755};
756
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100757static int snd_uart16550_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 snd_uart16550_t *uart = device->device_data;
760 return snd_uart16550_free(uart);
761}
762
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100763static int __init snd_uart16550_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 unsigned long iobase,
765 int irq,
766 unsigned int speed,
767 unsigned int base,
768 int adaptor,
769 int droponfull,
770 snd_uart16550_t **ruart)
771{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100772 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 .dev_free = snd_uart16550_dev_free,
774 };
775 snd_uart16550_t *uart;
776 int err;
777
778
Takashi Iwai561b2202005-09-09 14:22:34 +0200779 if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -ENOMEM;
781 uart->adaptor = adaptor;
782 uart->card = card;
783 spin_lock_init(&uart->open_lock);
784 uart->irq = -1;
785 uart->base = iobase;
786 uart->drop_on_full = droponfull;
787
788 if ((err = snd_uart16550_detect(uart)) <= 0) {
789 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
790 return err;
791 }
792
793 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
794 if (request_irq(irq, snd_uart16550_interrupt,
795 SA_INTERRUPT, "Serial MIDI", (void *) uart)) {
796 snd_printk("irq %d busy. Using Polling.\n", irq);
797 } else {
798 uart->irq = irq;
799 }
800 }
801 uart->divisor = base / speed;
802 uart->speed = base / (unsigned int)uart->divisor;
803 uart->speed_base = base;
804 uart->prev_out = -1;
805 uart->prev_in = 0;
806 uart->rstatus = 0;
807 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
808 init_timer(&uart->buffer_timer);
809 uart->buffer_timer.function = snd_uart16550_buffer_timer;
810 uart->buffer_timer.data = (unsigned long)uart;
811 uart->timer_running = 0;
812
813 /* Register device */
814 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
815 snd_uart16550_free(uart);
816 return err;
817 }
818
819 switch (uart->adaptor) {
820 case SNDRV_SERIAL_MS124W_SA:
821 case SNDRV_SERIAL_MS124W_MB:
822 /* MS-124W can draw power from RTS and DTR if they
823 are in opposite states. */
824 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
825 break;
826 case SNDRV_SERIAL_MS124T:
827 /* MS-124T can draw power from RTS and/or DTR (preferably
828 both) if they are asserted. */
829 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
830 break;
831 default:
832 break;
833 }
834
835 if (ruart)
836 *ruart = uart;
837
838 return 0;
839}
840
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100841static void __init snd_uart16550_substreams(struct snd_rawmidi_str *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct list_head *list;
844
845 list_for_each(list, &stream->substreams) {
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100846 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
848 }
849}
850
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100851static int __init snd_uart16550_rmidi(snd_uart16550_t *uart, int device, int outs, int ins, struct snd_rawmidi **rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100853 struct snd_rawmidi *rrawmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int err;
855
856 if ((err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device, outs, ins, &rrawmidi)) < 0)
857 return err;
858 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_uart16550_input);
859 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_uart16550_output);
860 strcpy(rrawmidi->name, "Serial MIDI");
861 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
862 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
863 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
864 SNDRV_RAWMIDI_INFO_INPUT |
865 SNDRV_RAWMIDI_INFO_DUPLEX;
866 rrawmidi->private_data = uart;
867 if (rmidi)
868 *rmidi = rrawmidi;
869 return 0;
870}
871
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100872static int __init snd_serial_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100874 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 snd_uart16550_t *uart;
876 int err;
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100877 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 switch (adaptor[dev]) {
880 case SNDRV_SERIAL_SOUNDCANVAS:
881 ins[dev] = 1;
882 break;
883 case SNDRV_SERIAL_MS124T:
884 case SNDRV_SERIAL_MS124W_SA:
885 outs[dev] = 1;
886 ins[dev] = 1;
887 break;
888 case SNDRV_SERIAL_MS124W_MB:
889 outs[dev] = 16;
890 ins[dev] = 1;
891 break;
892 case SNDRV_SERIAL_GENERIC:
893 break;
894 default:
895 snd_printk("Adaptor type is out of range 0-%d (%d)\n",
896 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
897 return -ENODEV;
898 }
899
900 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
901 snd_printk("Count of outputs is out of range 1-%d (%d)\n",
902 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
903 return -ENODEV;
904 }
905
906 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
907 snd_printk("Count of inputs is out of range 1-%d (%d)\n",
908 SNDRV_SERIAL_MAX_INS, ins[dev]);
909 return -ENODEV;
910 }
911
912 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
913 if (card == NULL)
914 return -ENOMEM;
915
916 strcpy(card->driver, "Serial");
917 strcpy(card->shortname, "Serial MIDI (UART16550A)");
918
919 if ((err = snd_uart16550_create(card,
920 port[dev],
921 irq[dev],
922 speed[dev],
923 base[dev],
924 adaptor[dev],
925 droponfull[dev],
Takashi Iwai16dab542005-09-05 17:17:58 +0200926 &uart)) < 0)
927 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Takashi Iwai16dab542005-09-05 17:17:58 +0200929 if ((err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi)) < 0)
930 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 sprintf(card->longname, "%s at 0x%lx, irq %d speed %d div %d outs %d ins %d adaptor %s droponfull %d",
933 card->shortname,
934 uart->base,
935 uart->irq,
936 uart->speed,
937 (int)uart->divisor,
938 outs[dev],
939 ins[dev],
940 adaptor_names[uart->adaptor],
941 uart->drop_on_full);
942
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100943 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200944
945 if ((err = snd_card_register(card)) < 0)
946 goto _err;
947
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100948 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
Takashi Iwai16dab542005-09-05 17:17:58 +0200950
951 _err:
952 snd_card_free(card);
953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100956static int snd_serial_remove(struct platform_device *devptr)
957{
958 snd_card_free(platform_get_drvdata(devptr));
959 platform_set_drvdata(devptr, NULL);
960 return 0;
961}
962
963#define SND_SERIAL_DRIVER "snd_serial_u16550"
964
965static struct platform_driver snd_serial_driver = {
966 .probe = snd_serial_probe,
967 .remove = snd_serial_remove,
968 .driver = {
969 .name = SND_SERIAL_DRIVER
970 },
971};
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973static int __init alsa_card_serial_init(void)
974{
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100975 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100977 if ((err = platform_driver_register(&snd_serial_driver)) < 0)
978 return err;
979
980 cards = 0;
981 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
982 struct platform_device *device;
983 device = platform_device_register_simple(SND_SERIAL_DRIVER,
984 i, NULL, 0);
985 if (IS_ERR(device)) {
986 err = PTR_ERR(device);
987 goto errout;
988 }
989 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100991 if (! cards) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#ifdef MODULE
993 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
994#endif
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100995 err = -ENODEV;
996 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 return 0;
Takashi Iwai9caf6b52005-11-17 16:02:30 +0100999
1000 errout:
1001 platform_driver_unregister(&snd_serial_driver);
1002 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005static void __exit alsa_card_serial_exit(void)
1006{
Takashi Iwai9caf6b52005-11-17 16:02:30 +01001007 platform_driver_unregister(&snd_serial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010module_init(alsa_card_serial_init)
1011module_exit(alsa_card_serial_exit)