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