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