0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <linux/init.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/err.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/slab.h>
0025 #include <linux/ioport.h>
0026 #include <linux/module.h>
0027 #include <linux/io.h>
0028 #include <sound/core.h>
0029 #include <sound/rawmidi.h>
0030 #include <sound/initval.h>
0031
0032 #include <linux/serial_reg.h>
0033 #include <linux/jiffies.h>
0034
0035 MODULE_DESCRIPTION("MIDI serial u16550");
0036 MODULE_LICENSE("GPL");
0037
0038 #define SNDRV_SERIAL_SOUNDCANVAS 0
0039 #define SNDRV_SERIAL_MS124T 1
0040 #define SNDRV_SERIAL_MS124W_SA 2
0041 #define SNDRV_SERIAL_MS124W_MB 3
0042 #define SNDRV_SERIAL_GENERIC 4
0043 #define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC
0044 static const char * const adaptor_names[] = {
0045 "Soundcanvas",
0046 "MS-124T",
0047 "MS-124W S/A",
0048 "MS-124W M/B",
0049 "Generic"
0050 };
0051
0052 #define SNDRV_SERIAL_NORMALBUFF 0
0053 #define SNDRV_SERIAL_DROPBUFF 1
0054
0055 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0056 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0057 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
0058 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
0059 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
0060 static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400};
0061 static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200};
0062 static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
0063 static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
0064 static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
0065 static bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
0066
0067 module_param_array(index, int, NULL, 0444);
0068 MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
0069 module_param_array(id, charp, NULL, 0444);
0070 MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
0071 module_param_array(enable, bool, NULL, 0444);
0072 MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
0073 module_param_hw_array(port, long, ioport, NULL, 0444);
0074 MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
0075 module_param_hw_array(irq, int, irq, NULL, 0444);
0076 MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
0077 module_param_array(speed, int, NULL, 0444);
0078 MODULE_PARM_DESC(speed, "Speed in bauds.");
0079 module_param_array(base, int, NULL, 0444);
0080 MODULE_PARM_DESC(base, "Base for divisor in bauds.");
0081 module_param_array(outs, int, NULL, 0444);
0082 MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
0083 module_param_array(ins, int, NULL, 0444);
0084 MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
0085 module_param_array(droponfull, bool, NULL, 0444);
0086 MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
0087
0088 module_param_array(adaptor, int, NULL, 0444);
0089 MODULE_PARM_DESC(adaptor, "Type of adaptor.");
0090
0091
0092
0093 #define SNDRV_SERIAL_MAX_OUTS 16
0094 #define SNDRV_SERIAL_MAX_INS 16
0095
0096 #define TX_BUFF_SIZE (1<<15)
0097 #define TX_BUFF_MASK (TX_BUFF_SIZE - 1)
0098
0099 #define SERIAL_MODE_NOT_OPENED (0)
0100 #define SERIAL_MODE_INPUT_OPEN (1 << 0)
0101 #define SERIAL_MODE_OUTPUT_OPEN (1 << 1)
0102 #define SERIAL_MODE_INPUT_TRIGGERED (1 << 2)
0103 #define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3)
0104
0105 struct snd_uart16550 {
0106 struct snd_card *card;
0107 struct snd_rawmidi *rmidi;
0108 struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS];
0109 struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS];
0110
0111 int filemode;
0112
0113 spinlock_t open_lock;
0114
0115 int irq;
0116
0117 unsigned long base;
0118
0119 unsigned int speed;
0120 unsigned int speed_base;
0121 unsigned char divisor;
0122
0123 unsigned char old_divisor_lsb;
0124 unsigned char old_divisor_msb;
0125 unsigned char old_line_ctrl_reg;
0126
0127
0128 short int fifo_limit;
0129 short int fifo_count;
0130
0131
0132 int adaptor;
0133
0134
0135 int prev_in;
0136 unsigned char rstatus;
0137
0138
0139 int prev_out;
0140 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
0141
0142
0143 unsigned char tx_buff[TX_BUFF_SIZE];
0144 int buff_in_count;
0145 int buff_in;
0146 int buff_out;
0147 int drop_on_full;
0148
0149
0150 unsigned int timer_running:1;
0151 struct timer_list buffer_timer;
0152
0153 };
0154
0155 static struct platform_device *devices[SNDRV_CARDS];
0156
0157 static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
0158 {
0159 if (!uart->timer_running) {
0160
0161 mod_timer(&uart->buffer_timer, jiffies + (HZ + 255) / 256);
0162 uart->timer_running = 1;
0163 }
0164 }
0165
0166 static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
0167 {
0168 if (uart->timer_running) {
0169 del_timer(&uart->buffer_timer);
0170 uart->timer_running = 0;
0171 }
0172 }
0173
0174
0175 static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
0176 {
0177 unsigned short buff_out = uart->buff_out;
0178 if (uart->buff_in_count > 0) {
0179 outb(uart->tx_buff[buff_out], uart->base + UART_TX);
0180 uart->fifo_count++;
0181 buff_out++;
0182 buff_out &= TX_BUFF_MASK;
0183 uart->buff_out = buff_out;
0184 uart->buff_in_count--;
0185 }
0186 }
0187
0188
0189
0190
0191
0192 static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
0193 {
0194 unsigned char c, status;
0195 int substream;
0196
0197
0198 substream = uart->prev_in;
0199
0200
0201 while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
0202
0203 c = inb(uart->base + UART_RX);
0204
0205
0206 if (c & 0x80)
0207 uart->rstatus = c;
0208
0209
0210 if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
0211 if (uart->rstatus == 0xf5) {
0212 if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
0213 substream = c - 1;
0214 if (c != 0xf5)
0215
0216
0217 uart->rstatus = 0;
0218 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN)
0219 && uart->midi_input[substream])
0220 snd_rawmidi_receive(uart->midi_input[substream],
0221 &c, 1);
0222 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) &&
0223 uart->midi_input[substream])
0224 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
0225
0226 if (status & UART_LSR_OE)
0227 snd_printk(KERN_WARNING
0228 "%s: Overrun on device at 0x%lx\n",
0229 uart->rmidi->name, uart->base);
0230 }
0231
0232
0233 uart->prev_in = substream;
0234
0235
0236
0237
0238 if (status & UART_LSR_THRE)
0239 uart->fifo_count = 0;
0240 if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
0241 || uart->adaptor == SNDRV_SERIAL_GENERIC) {
0242
0243 status = inb(uart->base + UART_MSR);
0244 while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
0245 uart->buff_in_count > 0) {
0246 snd_uart16550_buffer_output(uart);
0247 status = inb(uart->base + UART_MSR);
0248 }
0249 } else {
0250
0251 while (uart->fifo_count < uart->fifo_limit
0252 && uart->buff_in_count > 0)
0253 snd_uart16550_buffer_output(uart);
0254 }
0255 if (uart->irq < 0 && uart->buff_in_count > 0)
0256 snd_uart16550_add_timer(uart);
0257 }
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
0280 {
0281 struct snd_uart16550 *uart;
0282
0283 uart = dev_id;
0284 spin_lock(&uart->open_lock);
0285 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
0286 spin_unlock(&uart->open_lock);
0287 return IRQ_NONE;
0288 }
0289
0290 inb(uart->base + UART_IIR);
0291 snd_uart16550_io_loop(uart);
0292 spin_unlock(&uart->open_lock);
0293 return IRQ_HANDLED;
0294 }
0295
0296
0297 static void snd_uart16550_buffer_timer(struct timer_list *t)
0298 {
0299 unsigned long flags;
0300 struct snd_uart16550 *uart;
0301
0302 uart = from_timer(uart, t, buffer_timer);
0303 spin_lock_irqsave(&uart->open_lock, flags);
0304 snd_uart16550_del_timer(uart);
0305 snd_uart16550_io_loop(uart);
0306 spin_unlock_irqrestore(&uart->open_lock, flags);
0307 }
0308
0309
0310
0311
0312
0313
0314 static int snd_uart16550_detect(struct snd_uart16550 *uart)
0315 {
0316 unsigned long io_base = uart->base;
0317 int ok;
0318 unsigned char c;
0319
0320
0321 if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
0322 return -ENODEV;
0323 }
0324
0325 if (!devm_request_region(uart->card->dev, io_base, 8, "Serial MIDI")) {
0326 snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
0327 return -EBUSY;
0328 }
0329
0330
0331 ok = 1;
0332
0333 outb(UART_LCR_WLEN8, io_base + UART_LCR);
0334 c = inb(io_base + UART_IER);
0335
0336 if ((c & 0xf0) != 0)
0337 ok = 0;
0338
0339 outb(0xaa, io_base + UART_SCR);
0340
0341 c = inb(io_base + UART_SCR);
0342
0343 if (c != 0xaa)
0344 ok = 0;
0345
0346 outb(0x55, io_base + UART_SCR);
0347
0348 c = inb(io_base + UART_SCR);
0349
0350 if (c != 0x55)
0351 ok = 0;
0352
0353 return ok;
0354 }
0355
0356 static void snd_uart16550_do_open(struct snd_uart16550 * uart)
0357 {
0358 char byte;
0359
0360
0361 uart->buff_in_count = 0;
0362 uart->buff_in = 0;
0363 uart->buff_out = 0;
0364 uart->fifo_limit = 1;
0365 uart->fifo_count = 0;
0366 uart->timer_running = 0;
0367
0368 outb(UART_FCR_ENABLE_FIFO
0369 | UART_FCR_CLEAR_RCVR
0370 | UART_FCR_CLEAR_XMIT
0371 | UART_FCR_TRIGGER_4
0372
0373
0374
0375 ,uart->base + UART_FCR);
0376
0377 if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
0378 uart->fifo_limit = 16;
0379 if (uart->divisor != 0) {
0380 uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
0381 outb(UART_LCR_DLAB
0382 ,uart->base + UART_LCR);
0383 uart->old_divisor_lsb = inb(uart->base + UART_DLL);
0384 uart->old_divisor_msb = inb(uart->base + UART_DLM);
0385
0386 outb(uart->divisor
0387 ,uart->base + UART_DLL);
0388 outb(0
0389 ,uart->base + UART_DLM);
0390
0391 }
0392
0393 outb(UART_LCR_WLEN8
0394 | 0
0395 | 0
0396 | 0
0397 ,uart->base + UART_LCR);
0398
0399 switch (uart->adaptor) {
0400 default:
0401 outb(UART_MCR_RTS
0402 | UART_MCR_DTR
0403 | UART_MCR_OUT2
0404
0405
0406 ,uart->base + UART_MCR);
0407 break;
0408 case SNDRV_SERIAL_MS124W_SA:
0409 case SNDRV_SERIAL_MS124W_MB:
0410
0411
0412 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
0413 uart->base + UART_MCR);
0414 break;
0415 case SNDRV_SERIAL_MS124T:
0416
0417
0418 outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
0419 uart->base + UART_MCR);
0420 break;
0421 }
0422
0423 if (uart->irq < 0) {
0424 byte = (0 & UART_IER_RDI)
0425 |(0 & UART_IER_THRI)
0426 ;
0427 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
0428 byte = UART_IER_RDI
0429 | UART_IER_MSI
0430 ;
0431 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
0432 byte = UART_IER_RDI
0433 | UART_IER_MSI
0434 | UART_IER_THRI
0435 ;
0436 } else {
0437 byte = UART_IER_RDI
0438 | UART_IER_THRI
0439 ;
0440 }
0441 outb(byte, uart->base + UART_IER);
0442
0443 inb(uart->base + UART_LSR);
0444 inb(uart->base + UART_IIR);
0445 inb(uart->base + UART_RX);
0446 }
0447
0448 static void snd_uart16550_do_close(struct snd_uart16550 * uart)
0449 {
0450 if (uart->irq < 0)
0451 snd_uart16550_del_timer(uart);
0452
0453
0454
0455
0456
0457 outb((0 & UART_IER_RDI)
0458 |(0 & UART_IER_THRI)
0459 ,uart->base + UART_IER);
0460
0461 switch (uart->adaptor) {
0462 default:
0463 outb((0 & UART_MCR_RTS)
0464 |(0 & UART_MCR_DTR)
0465 |(0 & UART_MCR_OUT2)
0466 ,uart->base + UART_MCR);
0467 break;
0468 case SNDRV_SERIAL_MS124W_SA:
0469 case SNDRV_SERIAL_MS124W_MB:
0470
0471
0472 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
0473 uart->base + UART_MCR);
0474 break;
0475 case SNDRV_SERIAL_MS124T:
0476
0477
0478 outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
0479 uart->base + UART_MCR);
0480 break;
0481 }
0482
0483 inb(uart->base + UART_IIR);
0484
0485
0486 if (uart->divisor != 0) {
0487 outb(UART_LCR_DLAB
0488 ,uart->base + UART_LCR);
0489 outb(uart->old_divisor_lsb
0490 ,uart->base + UART_DLL);
0491 outb(uart->old_divisor_msb
0492 ,uart->base + UART_DLM);
0493
0494 outb(uart->old_line_ctrl_reg
0495 ,uart->base + UART_LCR);
0496 }
0497 }
0498
0499 static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
0500 {
0501 unsigned long flags;
0502 struct snd_uart16550 *uart = substream->rmidi->private_data;
0503
0504 spin_lock_irqsave(&uart->open_lock, flags);
0505 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
0506 snd_uart16550_do_open(uart);
0507 uart->filemode |= SERIAL_MODE_INPUT_OPEN;
0508 uart->midi_input[substream->number] = substream;
0509 spin_unlock_irqrestore(&uart->open_lock, flags);
0510 return 0;
0511 }
0512
0513 static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
0514 {
0515 unsigned long flags;
0516 struct snd_uart16550 *uart = substream->rmidi->private_data;
0517
0518 spin_lock_irqsave(&uart->open_lock, flags);
0519 uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
0520 uart->midi_input[substream->number] = NULL;
0521 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
0522 snd_uart16550_do_close(uart);
0523 spin_unlock_irqrestore(&uart->open_lock, flags);
0524 return 0;
0525 }
0526
0527 static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
0528 int up)
0529 {
0530 unsigned long flags;
0531 struct snd_uart16550 *uart = substream->rmidi->private_data;
0532
0533 spin_lock_irqsave(&uart->open_lock, flags);
0534 if (up)
0535 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
0536 else
0537 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
0538 spin_unlock_irqrestore(&uart->open_lock, flags);
0539 }
0540
0541 static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
0542 {
0543 unsigned long flags;
0544 struct snd_uart16550 *uart = substream->rmidi->private_data;
0545
0546 spin_lock_irqsave(&uart->open_lock, flags);
0547 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
0548 snd_uart16550_do_open(uart);
0549 uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
0550 uart->midi_output[substream->number] = substream;
0551 spin_unlock_irqrestore(&uart->open_lock, flags);
0552 return 0;
0553 };
0554
0555 static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
0556 {
0557 unsigned long flags;
0558 struct snd_uart16550 *uart = substream->rmidi->private_data;
0559
0560 spin_lock_irqsave(&uart->open_lock, flags);
0561 uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
0562 uart->midi_output[substream->number] = NULL;
0563 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
0564 snd_uart16550_do_close(uart);
0565 spin_unlock_irqrestore(&uart->open_lock, flags);
0566 return 0;
0567 };
0568
0569 static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
0570 int Num)
0571 {
0572 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
0573 return 1;
0574 else
0575 return 0;
0576 }
0577
0578 static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
0579 unsigned char byte)
0580 {
0581 unsigned short buff_in = uart->buff_in;
0582 if (uart->buff_in_count < TX_BUFF_SIZE) {
0583 uart->tx_buff[buff_in] = byte;
0584 buff_in++;
0585 buff_in &= TX_BUFF_MASK;
0586 uart->buff_in = buff_in;
0587 uart->buff_in_count++;
0588 if (uart->irq < 0)
0589 snd_uart16550_add_timer(uart);
0590 return 1;
0591 } else
0592 return 0;
0593 }
0594
0595 static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
0596 struct snd_rawmidi_substream *substream,
0597 unsigned char midi_byte)
0598 {
0599 if (uart->buff_in_count == 0
0600 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
0601 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
0602 (uart->fifo_count == 0
0603 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) {
0604
0605
0606 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
0607
0608 uart->fifo_count = 1;
0609 outb(midi_byte, uart->base + UART_TX);
0610 } else {
0611 if (uart->fifo_count < uart->fifo_limit) {
0612 uart->fifo_count++;
0613 outb(midi_byte, uart->base + UART_TX);
0614 } else {
0615
0616
0617 snd_uart16550_write_buffer(uart, midi_byte);
0618 }
0619 }
0620 } else {
0621 if (!snd_uart16550_write_buffer(uart, midi_byte)) {
0622 snd_printk(KERN_WARNING
0623 "%s: Buffer overrun on device at 0x%lx\n",
0624 uart->rmidi->name, uart->base);
0625 return 0;
0626 }
0627 }
0628
0629 return 1;
0630 }
0631
0632 static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
0633 {
0634 unsigned long flags;
0635 unsigned char midi_byte, addr_byte;
0636 struct snd_uart16550 *uart = substream->rmidi->private_data;
0637 char first;
0638 static unsigned long lasttime = 0;
0639
0640
0641
0642
0643
0644
0645 spin_lock_irqsave(&uart->open_lock, flags);
0646
0647 if (uart->irq < 0)
0648 snd_uart16550_io_loop(uart);
0649
0650 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
0651 while (1) {
0652
0653
0654 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
0655 break;
0656 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
0657 break;
0658 #ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
0659
0660 addr_byte = (1 << (substream->number + 4)) | 0x08;
0661 #else
0662
0663 addr_byte = (substream->number << 4) | 0x08;
0664
0665 if (addr_byte == 0x08)
0666 addr_byte = 0xf8;
0667 #endif
0668 snd_uart16550_output_byte(uart, substream, addr_byte);
0669
0670 snd_uart16550_output_byte(uart, substream, midi_byte);
0671 }
0672 } else {
0673 first = 0;
0674 while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
0675
0676
0677 if (first == 0 &&
0678 (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
0679 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
0680 (uart->prev_out != substream->number ||
0681 time_after(jiffies, lasttime + 3*HZ))) {
0682
0683 if (snd_uart16550_buffer_can_write(uart, 3)) {
0684
0685
0686
0687
0688
0689
0690 uart->prev_out = substream->number;
0691
0692 snd_uart16550_output_byte(uart, substream,
0693 0xf5);
0694
0695 snd_uart16550_output_byte(uart, substream,
0696 uart->prev_out + 1);
0697
0698
0699 if (midi_byte < 0x80 &&
0700 uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
0701 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
0702 } else if (!uart->drop_on_full)
0703 break;
0704
0705 }
0706
0707
0708 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
0709 !uart->drop_on_full )
0710 break;
0711
0712 if (midi_byte >= 0x80 && midi_byte < 0xf0)
0713 uart->prev_status[uart->prev_out] = midi_byte;
0714 first = 1;
0715
0716 snd_rawmidi_transmit_ack( substream, 1 );
0717 }
0718 lasttime = jiffies;
0719 }
0720 spin_unlock_irqrestore(&uart->open_lock, flags);
0721 }
0722
0723 static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
0724 int up)
0725 {
0726 unsigned long flags;
0727 struct snd_uart16550 *uart = substream->rmidi->private_data;
0728
0729 spin_lock_irqsave(&uart->open_lock, flags);
0730 if (up)
0731 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
0732 else
0733 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
0734 spin_unlock_irqrestore(&uart->open_lock, flags);
0735 if (up)
0736 snd_uart16550_output_write(substream);
0737 }
0738
0739 static const struct snd_rawmidi_ops snd_uart16550_output =
0740 {
0741 .open = snd_uart16550_output_open,
0742 .close = snd_uart16550_output_close,
0743 .trigger = snd_uart16550_output_trigger,
0744 };
0745
0746 static const struct snd_rawmidi_ops snd_uart16550_input =
0747 {
0748 .open = snd_uart16550_input_open,
0749 .close = snd_uart16550_input_close,
0750 .trigger = snd_uart16550_input_trigger,
0751 };
0752
0753 static int snd_uart16550_create(struct snd_card *card,
0754 unsigned long iobase,
0755 int irq,
0756 unsigned int speed,
0757 unsigned int base,
0758 int adaptor,
0759 int droponfull,
0760 struct snd_uart16550 **ruart)
0761 {
0762 struct snd_uart16550 *uart;
0763 int err;
0764
0765
0766 uart = devm_kzalloc(card->dev, sizeof(*uart), GFP_KERNEL);
0767 if (!uart)
0768 return -ENOMEM;
0769 uart->adaptor = adaptor;
0770 uart->card = card;
0771 spin_lock_init(&uart->open_lock);
0772 uart->irq = -1;
0773 uart->base = iobase;
0774 uart->drop_on_full = droponfull;
0775
0776 err = snd_uart16550_detect(uart);
0777 if (err <= 0) {
0778 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
0779 return -ENODEV;
0780 }
0781
0782 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
0783 if (devm_request_irq(card->dev, irq, snd_uart16550_interrupt,
0784 0, "Serial MIDI", uart)) {
0785 snd_printk(KERN_WARNING
0786 "irq %d busy. Using Polling.\n", irq);
0787 } else {
0788 uart->irq = irq;
0789 }
0790 }
0791 uart->divisor = base / speed;
0792 uart->speed = base / (unsigned int)uart->divisor;
0793 uart->speed_base = base;
0794 uart->prev_out = -1;
0795 uart->prev_in = 0;
0796 uart->rstatus = 0;
0797 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
0798 timer_setup(&uart->buffer_timer, snd_uart16550_buffer_timer, 0);
0799 uart->timer_running = 0;
0800
0801 switch (uart->adaptor) {
0802 case SNDRV_SERIAL_MS124W_SA:
0803 case SNDRV_SERIAL_MS124W_MB:
0804
0805
0806 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
0807 break;
0808 case SNDRV_SERIAL_MS124T:
0809
0810
0811 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
0812 break;
0813 default:
0814 break;
0815 }
0816
0817 if (ruart)
0818 *ruart = uart;
0819
0820 return 0;
0821 }
0822
0823 static void snd_uart16550_substreams(struct snd_rawmidi_str *stream)
0824 {
0825 struct snd_rawmidi_substream *substream;
0826
0827 list_for_each_entry(substream, &stream->substreams, list) {
0828 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
0829 }
0830 }
0831
0832 static int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
0833 int outs, int ins,
0834 struct snd_rawmidi **rmidi)
0835 {
0836 struct snd_rawmidi *rrawmidi;
0837 int err;
0838
0839 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
0840 outs, ins, &rrawmidi);
0841 if (err < 0)
0842 return err;
0843 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
0844 &snd_uart16550_input);
0845 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
0846 &snd_uart16550_output);
0847 strcpy(rrawmidi->name, "Serial MIDI");
0848 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
0849 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
0850 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
0851 SNDRV_RAWMIDI_INFO_INPUT |
0852 SNDRV_RAWMIDI_INFO_DUPLEX;
0853 rrawmidi->private_data = uart;
0854 if (rmidi)
0855 *rmidi = rrawmidi;
0856 return 0;
0857 }
0858
0859 static int snd_serial_probe(struct platform_device *devptr)
0860 {
0861 struct snd_card *card;
0862 struct snd_uart16550 *uart;
0863 int err;
0864 int dev = devptr->id;
0865
0866 switch (adaptor[dev]) {
0867 case SNDRV_SERIAL_SOUNDCANVAS:
0868 ins[dev] = 1;
0869 break;
0870 case SNDRV_SERIAL_MS124T:
0871 case SNDRV_SERIAL_MS124W_SA:
0872 outs[dev] = 1;
0873 ins[dev] = 1;
0874 break;
0875 case SNDRV_SERIAL_MS124W_MB:
0876 outs[dev] = 16;
0877 ins[dev] = 1;
0878 break;
0879 case SNDRV_SERIAL_GENERIC:
0880 break;
0881 default:
0882 snd_printk(KERN_ERR
0883 "Adaptor type is out of range 0-%d (%d)\n",
0884 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
0885 return -ENODEV;
0886 }
0887
0888 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
0889 snd_printk(KERN_ERR
0890 "Count of outputs is out of range 1-%d (%d)\n",
0891 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
0892 return -ENODEV;
0893 }
0894
0895 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
0896 snd_printk(KERN_ERR
0897 "Count of inputs is out of range 1-%d (%d)\n",
0898 SNDRV_SERIAL_MAX_INS, ins[dev]);
0899 return -ENODEV;
0900 }
0901
0902 err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
0903 0, &card);
0904 if (err < 0)
0905 return err;
0906
0907 strcpy(card->driver, "Serial");
0908 strcpy(card->shortname, "Serial MIDI (UART16550A)");
0909
0910 err = snd_uart16550_create(card, port[dev], irq[dev], speed[dev],
0911 base[dev], adaptor[dev], droponfull[dev],
0912 &uart);
0913 if (err < 0)
0914 return err;
0915
0916 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
0917 if (err < 0)
0918 return err;
0919
0920 sprintf(card->longname, "%s [%s] at %#lx, irq %d",
0921 card->shortname,
0922 adaptor_names[uart->adaptor],
0923 uart->base,
0924 uart->irq);
0925
0926 err = snd_card_register(card);
0927 if (err < 0)
0928 return err;
0929
0930 platform_set_drvdata(devptr, card);
0931 return 0;
0932 }
0933
0934 #define SND_SERIAL_DRIVER "snd_serial_u16550"
0935
0936 static struct platform_driver snd_serial_driver = {
0937 .probe = snd_serial_probe,
0938 .driver = {
0939 .name = SND_SERIAL_DRIVER,
0940 },
0941 };
0942
0943 static void snd_serial_unregister_all(void)
0944 {
0945 int i;
0946
0947 for (i = 0; i < ARRAY_SIZE(devices); ++i)
0948 platform_device_unregister(devices[i]);
0949 platform_driver_unregister(&snd_serial_driver);
0950 }
0951
0952 static int __init alsa_card_serial_init(void)
0953 {
0954 int i, cards, err;
0955
0956 err = platform_driver_register(&snd_serial_driver);
0957 if (err < 0)
0958 return err;
0959
0960 cards = 0;
0961 for (i = 0; i < SNDRV_CARDS; i++) {
0962 struct platform_device *device;
0963 if (! enable[i])
0964 continue;
0965 device = platform_device_register_simple(SND_SERIAL_DRIVER,
0966 i, NULL, 0);
0967 if (IS_ERR(device))
0968 continue;
0969 if (!platform_get_drvdata(device)) {
0970 platform_device_unregister(device);
0971 continue;
0972 }
0973 devices[i] = device;
0974 cards++;
0975 }
0976 if (! cards) {
0977 #ifdef MODULE
0978 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
0979 #endif
0980 snd_serial_unregister_all();
0981 return -ENODEV;
0982 }
0983 return 0;
0984 }
0985
0986 static void __exit alsa_card_serial_exit(void)
0987 {
0988 snd_serial_unregister_all();
0989 }
0990
0991 module_init(alsa_card_serial_init)
0992 module_exit(alsa_card_serial_exit)