1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <sound/driver.h>
34#include <linux/init.h>
35#include <linux/interrupt.h>
36#include <linux/err.h>
37#include <linux/platform_device.h>
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>
46
47#include <asm/io.h>
48
49MODULE_DESCRIPTION("MIDI serial u16550");
50MODULE_LICENSE("GPL");
51MODULE_SUPPORTED_DEVICE("{{ALSA, MIDI serial u16550}}");
52
53#define SNDRV_SERIAL_SOUNDCANVAS 0
54#define SNDRV_SERIAL_MS124T 1
55#define SNDRV_SERIAL_MS124W_SA 2
56#define SNDRV_SERIAL_MS124W_MB 3
57#define SNDRV_SERIAL_GENERIC 4
58#define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC
59static char *adaptor_names[] = {
60 "Soundcanvas",
61 "MS-124T",
62 "MS-124W S/A",
63 "MS-124W M/B",
64 "Generic"
65};
66
67#define SNDRV_SERIAL_NORMALBUFF 0
68#define SNDRV_SERIAL_DROPBUFF 1
69
70static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
71static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
72static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
73static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
74static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
75static int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400};
76static int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200};
77static int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
78static int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
79static int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS};
80static int droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF };
81
82module_param_array(index, int, NULL, 0444);
83MODULE_PARM_DESC(index, "Index value for Serial MIDI.");
84module_param_array(id, charp, NULL, 0444);
85MODULE_PARM_DESC(id, "ID string for Serial MIDI.");
86module_param_array(enable, bool, NULL, 0444);
87MODULE_PARM_DESC(enable, "Enable UART16550A chip.");
88module_param_array(port, long, NULL, 0444);
89MODULE_PARM_DESC(port, "Port # for UART16550A chip.");
90module_param_array(irq, int, NULL, 0444);
91MODULE_PARM_DESC(irq, "IRQ # for UART16550A chip.");
92module_param_array(speed, int, NULL, 0444);
93MODULE_PARM_DESC(speed, "Speed in bauds.");
94module_param_array(base, int, NULL, 0444);
95MODULE_PARM_DESC(base, "Base for divisor in bauds.");
96module_param_array(outs, int, NULL, 0444);
97MODULE_PARM_DESC(outs, "Number of MIDI outputs.");
98module_param_array(ins, int, NULL, 0444);
99MODULE_PARM_DESC(ins, "Number of MIDI inputs.");
100module_param_array(droponfull, bool, NULL, 0444);
101MODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode");
102
103module_param_array(adaptor, int, NULL, 0444);
104MODULE_PARM_DESC(adaptor, "Type of adaptor.");
105
106
107
108#define SNDRV_SERIAL_MAX_OUTS 16
109#define SNDRV_SERIAL_MAX_INS 16
110
111#define TX_BUFF_SIZE (1<<15)
112#define TX_BUFF_MASK (TX_BUFF_SIZE - 1)
113
114#define SERIAL_MODE_NOT_OPENED (0)
115#define SERIAL_MODE_INPUT_OPEN (1 << 0)
116#define SERIAL_MODE_OUTPUT_OPEN (1 << 1)
117#define SERIAL_MODE_INPUT_TRIGGERED (1 << 2)
118#define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3)
119
120struct snd_uart16550 {
121 struct snd_card *card;
122 struct snd_rawmidi *rmidi;
123 struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS];
124 struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS];
125
126 int filemode;
127
128 spinlock_t open_lock;
129
130 int irq;
131
132 unsigned long base;
133 struct resource *res_base;
134
135 unsigned int speed;
136 unsigned int speed_base;
137 unsigned char divisor;
138
139 unsigned char old_divisor_lsb;
140 unsigned char old_divisor_msb;
141 unsigned char old_line_ctrl_reg;
142
143
144 short int fifo_limit;
145 short int fifo_count;
146
147
148 int adaptor;
149
150
151 int prev_in;
152 unsigned char rstatus;
153
154
155 int prev_out;
156 unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS];
157
158
159 unsigned char tx_buff[TX_BUFF_SIZE];
160 int buff_in_count;
161 int buff_in;
162 int buff_out;
163 int drop_on_full;
164
165
166 unsigned int timer_running:1;
167 struct timer_list buffer_timer;
168
169};
170
171static struct platform_device *devices[SNDRV_CARDS];
172
173static inline void snd_uart16550_add_timer(struct snd_uart16550 *uart)
174{
175 if (!uart->timer_running) {
176
177 uart->buffer_timer.expires = jiffies + (HZ+255)/256;
178 uart->timer_running = 1;
179 add_timer(&uart->buffer_timer);
180 }
181}
182
183static inline void snd_uart16550_del_timer(struct snd_uart16550 *uart)
184{
185 if (uart->timer_running) {
186 del_timer(&uart->buffer_timer);
187 uart->timer_running = 0;
188 }
189}
190
191
192static inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart)
193{
194 unsigned short buff_out = uart->buff_out;
195 if (uart->buff_in_count > 0) {
196 outb(uart->tx_buff[buff_out], uart->base + UART_TX);
197 uart->fifo_count++;
198 buff_out++;
199 buff_out &= TX_BUFF_MASK;
200 uart->buff_out = buff_out;
201 uart->buff_in_count--;
202 }
203}
204
205
206
207
208
209static void snd_uart16550_io_loop(struct snd_uart16550 * uart)
210{
211 unsigned char c, status;
212 int substream;
213
214
215 substream = uart->prev_in;
216
217
218 while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) {
219
220 c = inb(uart->base + UART_RX);
221
222
223 if (c & 0x80)
224 uart->rstatus = c;
225
226
227 if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
228 if (uart->rstatus == 0xf5) {
229 if (c <= SNDRV_SERIAL_MAX_INS && c > 0)
230 substream = c - 1;
231 if (c != 0xf5)
232
233
234 uart->rstatus = 0;
235 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN)
236 && uart->midi_input[substream])
237 snd_rawmidi_receive(uart->midi_input[substream],
238 &c, 1);
239 } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) &&
240 uart->midi_input[substream])
241 snd_rawmidi_receive(uart->midi_input[substream], &c, 1);
242
243 if (status & UART_LSR_OE)
244 snd_printk("%s: Overrun on device at 0x%lx\n",
245 uart->rmidi->name, uart->base);
246 }
247
248
249 uart->prev_in = substream;
250
251
252
253
254 if (status & UART_LSR_THRE)
255 uart->fifo_count = 0;
256 if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
257 || uart->adaptor == SNDRV_SERIAL_GENERIC) {
258
259 status = inb(uart->base + UART_MSR);
260 while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
261 uart->buff_in_count > 0) {
262 snd_uart16550_buffer_output(uart);
263 status = inb(uart->base + UART_MSR);
264 }
265 } else {
266
267 while (uart->fifo_count < uart->fifo_limit
268 && uart->buff_in_count > 0)
269 snd_uart16550_buffer_output(uart);
270 }
271 if (uart->irq < 0 && uart->buff_in_count > 0)
272 snd_uart16550_add_timer(uart);
273}
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
296{
297 struct snd_uart16550 *uart;
298
299 uart = dev_id;
300 spin_lock(&uart->open_lock);
301 if (uart->filemode == SERIAL_MODE_NOT_OPENED) {
302 spin_unlock(&uart->open_lock);
303 return IRQ_NONE;
304 }
305
306 inb(uart->base + UART_IIR);
307 snd_uart16550_io_loop(uart);
308 spin_unlock(&uart->open_lock);
309 return IRQ_HANDLED;
310}
311
312
313static void snd_uart16550_buffer_timer(unsigned long data)
314{
315 unsigned long flags;
316 struct snd_uart16550 *uart;
317
318 uart = (struct snd_uart16550 *)data;
319 spin_lock_irqsave(&uart->open_lock, flags);
320 snd_uart16550_del_timer(uart);
321 snd_uart16550_io_loop(uart);
322 spin_unlock_irqrestore(&uart->open_lock, flags);
323}
324
325
326
327
328
329
330static int __devinit snd_uart16550_detect(struct snd_uart16550 *uart)
331{
332 unsigned long io_base = uart->base;
333 int ok;
334 unsigned char c;
335
336
337 if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
338 return -ENODEV;
339 }
340
341 uart->res_base = request_region(io_base, 8, "Serial MIDI");
342 if (uart->res_base == NULL) {
343 snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base);
344 return -EBUSY;
345 }
346
347
348 ok = 1;
349
350 outb(UART_LCR_WLEN8, io_base + UART_LCR);
351 c = inb(io_base + UART_IER);
352
353 if ((c & 0xf0) != 0)
354 ok = 0;
355
356 outb(0xaa, io_base + UART_SCR);
357
358 c = inb(io_base + UART_SCR);
359
360 if (c != 0xaa)
361 ok = 0;
362
363 outb(0x55, io_base + UART_SCR);
364
365 c = inb(io_base + UART_SCR);
366
367 if (c != 0x55)
368 ok = 0;
369
370 return ok;
371}
372
373static void snd_uart16550_do_open(struct snd_uart16550 * uart)
374{
375 char byte;
376
377
378 uart->buff_in_count = 0;
379 uart->buff_in = 0;
380 uart->buff_out = 0;
381 uart->fifo_limit = 1;
382 uart->fifo_count = 0;
383 uart->timer_running = 0;
384
385 outb(UART_FCR_ENABLE_FIFO
386 | UART_FCR_CLEAR_RCVR
387 | UART_FCR_CLEAR_XMIT
388 | UART_FCR_TRIGGER_4
389
390
391
392 ,uart->base + UART_FCR);
393
394 if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0)
395 uart->fifo_limit = 16;
396 if (uart->divisor != 0) {
397 uart->old_line_ctrl_reg = inb(uart->base + UART_LCR);
398 outb(UART_LCR_DLAB
399 ,uart->base + UART_LCR);
400 uart->old_divisor_lsb = inb(uart->base + UART_DLL);
401 uart->old_divisor_msb = inb(uart->base + UART_DLM);
402
403 outb(uart->divisor
404 ,uart->base + UART_DLL);
405 outb(0
406 ,uart->base + UART_DLM);
407
408 }
409
410 outb(UART_LCR_WLEN8
411 | 0
412 | 0
413 | 0
414 ,uart->base + UART_LCR);
415
416 switch (uart->adaptor) {
417 default:
418 outb(UART_MCR_RTS
419 | UART_MCR_DTR
420 | UART_MCR_OUT2
421
422
423 ,uart->base + UART_MCR);
424 break;
425 case SNDRV_SERIAL_MS124W_SA:
426 case SNDRV_SERIAL_MS124W_MB:
427
428
429 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2,
430 uart->base + UART_MCR);
431 break;
432 case SNDRV_SERIAL_MS124T:
433
434
435 outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2,
436 uart->base + UART_MCR);
437 break;
438 }
439
440 if (uart->irq < 0) {
441 byte = (0 & UART_IER_RDI)
442 |(0 & UART_IER_THRI)
443 ;
444 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
445 byte = UART_IER_RDI
446 | UART_IER_MSI
447 ;
448 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
449 byte = UART_IER_RDI
450 | UART_IER_MSI
451 | UART_IER_THRI
452 ;
453 } else {
454 byte = UART_IER_RDI
455 | UART_IER_THRI
456 ;
457 }
458 outb(byte, uart->base + UART_IER);
459
460 inb(uart->base + UART_LSR);
461 inb(uart->base + UART_IIR);
462 inb(uart->base + UART_RX);
463}
464
465static void snd_uart16550_do_close(struct snd_uart16550 * uart)
466{
467 if (uart->irq < 0)
468 snd_uart16550_del_timer(uart);
469
470
471
472
473
474 outb((0 & UART_IER_RDI)
475 |(0 & UART_IER_THRI)
476 ,uart->base + UART_IER);
477
478 switch (uart->adaptor) {
479 default:
480 outb((0 & UART_MCR_RTS)
481 |(0 & UART_MCR_DTR)
482 |(0 & UART_MCR_OUT2)
483 ,uart->base + UART_MCR);
484 break;
485 case SNDRV_SERIAL_MS124W_SA:
486 case SNDRV_SERIAL_MS124W_MB:
487
488
489 outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2),
490 uart->base + UART_MCR);
491 break;
492 case SNDRV_SERIAL_MS124T:
493
494
495 outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2),
496 uart->base + UART_MCR);
497 break;
498 }
499
500 inb(uart->base + UART_IIR);
501
502
503 if (uart->divisor != 0) {
504 outb(UART_LCR_DLAB
505 ,uart->base + UART_LCR);
506 outb(uart->old_divisor_lsb
507 ,uart->base + UART_DLL);
508 outb(uart->old_divisor_msb
509 ,uart->base + UART_DLM);
510
511 outb(uart->old_line_ctrl_reg
512 ,uart->base + UART_LCR);
513 }
514}
515
516static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
517{
518 unsigned long flags;
519 struct snd_uart16550 *uart = substream->rmidi->private_data;
520
521 spin_lock_irqsave(&uart->open_lock, flags);
522 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
523 snd_uart16550_do_open(uart);
524 uart->filemode |= SERIAL_MODE_INPUT_OPEN;
525 uart->midi_input[substream->number] = substream;
526 spin_unlock_irqrestore(&uart->open_lock, flags);
527 return 0;
528}
529
530static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
531{
532 unsigned long flags;
533 struct snd_uart16550 *uart = substream->rmidi->private_data;
534
535 spin_lock_irqsave(&uart->open_lock, flags);
536 uart->filemode &= ~SERIAL_MODE_INPUT_OPEN;
537 uart->midi_input[substream->number] = NULL;
538 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
539 snd_uart16550_do_close(uart);
540 spin_unlock_irqrestore(&uart->open_lock, flags);
541 return 0;
542}
543
544static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
545 int up)
546{
547 unsigned long flags;
548 struct snd_uart16550 *uart = substream->rmidi->private_data;
549
550 spin_lock_irqsave(&uart->open_lock, flags);
551 if (up)
552 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
553 else
554 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
555 spin_unlock_irqrestore(&uart->open_lock, flags);
556}
557
558static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
559{
560 unsigned long flags;
561 struct snd_uart16550 *uart = substream->rmidi->private_data;
562
563 spin_lock_irqsave(&uart->open_lock, flags);
564 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
565 snd_uart16550_do_open(uart);
566 uart->filemode |= SERIAL_MODE_OUTPUT_OPEN;
567 uart->midi_output[substream->number] = substream;
568 spin_unlock_irqrestore(&uart->open_lock, flags);
569 return 0;
570};
571
572static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
573{
574 unsigned long flags;
575 struct snd_uart16550 *uart = substream->rmidi->private_data;
576
577 spin_lock_irqsave(&uart->open_lock, flags);
578 uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN;
579 uart->midi_output[substream->number] = NULL;
580 if (uart->filemode == SERIAL_MODE_NOT_OPENED)
581 snd_uart16550_do_close(uart);
582 spin_unlock_irqrestore(&uart->open_lock, flags);
583 return 0;
584};
585
586static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
587 int Num)
588{
589 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
590 return 1;
591 else
592 return 0;
593}
594
595static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
596 unsigned char byte)
597{
598 unsigned short buff_in = uart->buff_in;
599 if (uart->buff_in_count < TX_BUFF_SIZE) {
600 uart->tx_buff[buff_in] = byte;
601 buff_in++;
602 buff_in &= TX_BUFF_MASK;
603 uart->buff_in = buff_in;
604 uart->buff_in_count++;
605 if (uart->irq < 0)
606 snd_uart16550_add_timer(uart);
607 return 1;
608 } else
609 return 0;
610}
611
612static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
613 struct snd_rawmidi_substream *substream,
614 unsigned char midi_byte)
615{
616 if (uart->buff_in_count == 0
617 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
618 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
619 (uart->fifo_count == 0
620 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) {
621
622
623 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
624
625 uart->fifo_count = 1;
626 outb(midi_byte, uart->base + UART_TX);
627 } else {
628 if (uart->fifo_count < uart->fifo_limit) {
629 uart->fifo_count++;
630 outb(midi_byte, uart->base + UART_TX);
631 } else {
632
633
634 snd_uart16550_write_buffer(uart, midi_byte);
635 }
636 }
637 } else {
638 if (!snd_uart16550_write_buffer(uart, midi_byte)) {
639 snd_printk("%s: Buffer overrun on device at 0x%lx\n",
640 uart->rmidi->name, uart->base);
641 return 0;
642 }
643 }
644
645 return 1;
646}
647
648static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
649{
650 unsigned long flags;
651 unsigned char midi_byte, addr_byte;
652 struct snd_uart16550 *uart = substream->rmidi->private_data;
653 char first;
654 static unsigned long lasttime = 0;
655
656
657
658
659
660
661 spin_lock_irqsave(&uart->open_lock, flags);
662
663 if (uart->irq < 0)
664 snd_uart16550_io_loop(uart);
665
666 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
667 while (1) {
668
669
670 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
671 break;
672 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
673 break;
674#ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
675
676 addr_byte = (1 << (substream->number + 4)) | 0x08;
677#else
678
679 addr_byte = (substream->number << 4) | 0x08;
680
681 if (addr_byte == 0x08)
682 addr_byte = 0xf8;
683#endif
684 snd_uart16550_output_byte(uart, substream, addr_byte);
685
686 snd_uart16550_output_byte(uart, substream, midi_byte);
687 }
688 } else {
689 first = 0;
690 while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
691
692
693 if (first == 0 &&
694 (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
695 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
696 (uart->prev_out != substream->number ||
697 jiffies-lasttime > 3*HZ)) {
698
699 if (snd_uart16550_buffer_can_write(uart, 3)) {
700
701
702
703
704
705
706 uart->prev_out = substream->number;
707
708 snd_uart16550_output_byte(uart, substream,
709 0xf5);
710
711 snd_uart16550_output_byte(uart, substream,
712 uart->prev_out + 1);
713
714
715 if (midi_byte < 0x80 &&
716 uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
717 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
718 } else if (!uart->drop_on_full)
719 break;
720
721 }
722
723
724 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
725 !uart->drop_on_full )
726 break;
727
728 if (midi_byte >= 0x80 && midi_byte < 0xf0)
729 uart->prev_status[uart->prev_out] = midi_byte;
730 first = 1;
731
732 snd_rawmidi_transmit_ack( substream, 1 );
733 }
734 lasttime = jiffies;
735 }
736 spin_unlock_irqrestore(&uart->open_lock, flags);
737}
738
739static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
740 int up)
741{
742 unsigned long flags;
743 struct snd_uart16550 *uart = substream->rmidi->private_data;
744
745 spin_lock_irqsave(&uart->open_lock, flags);
746 if (up)
747 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
748 else
749 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
750 spin_unlock_irqrestore(&uart->open_lock, flags);
751 if (up)
752 snd_uart16550_output_write(substream);
753}
754
755static struct snd_rawmidi_ops snd_uart16550_output =
756{
757 .open = snd_uart16550_output_open,
758 .close = snd_uart16550_output_close,
759 .trigger = snd_uart16550_output_trigger,
760};
761
762static struct snd_rawmidi_ops snd_uart16550_input =
763{
764 .open = snd_uart16550_input_open,
765 .close = snd_uart16550_input_close,
766 .trigger = snd_uart16550_input_trigger,
767};
768
769static int snd_uart16550_free(struct snd_uart16550 *uart)
770{
771 if (uart->irq >= 0)
772 free_irq(uart->irq, uart);
773 release_and_free_resource(uart->res_base);
774 kfree(uart);
775 return 0;
776};
777
778static int snd_uart16550_dev_free(struct snd_device *device)
779{
780 struct snd_uart16550 *uart = device->device_data;
781 return snd_uart16550_free(uart);
782}
783
784static int __devinit snd_uart16550_create(struct snd_card *card,
785 unsigned long iobase,
786 int irq,
787 unsigned int speed,
788 unsigned int base,
789 int adaptor,
790 int droponfull,
791 struct snd_uart16550 **ruart)
792{
793 static struct snd_device_ops ops = {
794 .dev_free = snd_uart16550_dev_free,
795 };
796 struct snd_uart16550 *uart;
797 int err;
798
799
800 if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
801 return -ENOMEM;
802 uart->adaptor = adaptor;
803 uart->card = card;
804 spin_lock_init(&uart->open_lock);
805 uart->irq = -1;
806 uart->base = iobase;
807 uart->drop_on_full = droponfull;
808
809 if ((err = snd_uart16550_detect(uart)) <= 0) {
810 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
811 snd_uart16550_free(uart);
812 return -ENODEV;
813 }
814
815 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
816 if (request_irq(irq, snd_uart16550_interrupt,
817 IRQF_DISABLED, "Serial MIDI", uart)) {
818 snd_printk("irq %d busy. Using Polling.\n", irq);
819 } else {
820 uart->irq = irq;
821 }
822 }
823 uart->divisor = base / speed;
824 uart->speed = base / (unsigned int)uart->divisor;
825 uart->speed_base = base;
826 uart->prev_out = -1;
827 uart->prev_in = 0;
828 uart->rstatus = 0;
829 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
830 init_timer(&uart->buffer_timer);
831 uart->buffer_timer.function = snd_uart16550_buffer_timer;
832 uart->buffer_timer.data = (unsigned long)uart;
833 uart->timer_running = 0;
834
835
836 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
837 snd_uart16550_free(uart);
838 return err;
839 }
840
841 switch (uart->adaptor) {
842 case SNDRV_SERIAL_MS124W_SA:
843 case SNDRV_SERIAL_MS124W_MB:
844
845
846 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
847 break;
848 case SNDRV_SERIAL_MS124T:
849
850
851 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
852 break;
853 default:
854 break;
855 }
856
857 if (ruart)
858 *ruart = uart;
859
860 return 0;
861}
862
863static void __devinit snd_uart16550_substreams(struct snd_rawmidi_str *stream)
864{
865 struct snd_rawmidi_substream *substream;
866
867 list_for_each_entry(substream, &stream->substreams, list) {
868 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
869 }
870}
871
872static int __devinit snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
873 int outs, int ins,
874 struct snd_rawmidi **rmidi)
875{
876 struct snd_rawmidi *rrawmidi;
877 int err;
878
879 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
880 outs, ins, &rrawmidi);
881 if (err < 0)
882 return err;
883 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
884 &snd_uart16550_input);
885 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
886 &snd_uart16550_output);
887 strcpy(rrawmidi->name, "Serial MIDI");
888 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
889 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
890 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
891 SNDRV_RAWMIDI_INFO_INPUT |
892 SNDRV_RAWMIDI_INFO_DUPLEX;
893 rrawmidi->private_data = uart;
894 if (rmidi)
895 *rmidi = rrawmidi;
896 return 0;
897}
898
899static int __devinit snd_serial_probe(struct platform_device *devptr)
900{
901 struct snd_card *card;
902 struct snd_uart16550 *uart;
903 int err;
904 int dev = devptr->id;
905
906 switch (adaptor[dev]) {
907 case SNDRV_SERIAL_SOUNDCANVAS:
908 ins[dev] = 1;
909 break;
910 case SNDRV_SERIAL_MS124T:
911 case SNDRV_SERIAL_MS124W_SA:
912 outs[dev] = 1;
913 ins[dev] = 1;
914 break;
915 case SNDRV_SERIAL_MS124W_MB:
916 outs[dev] = 16;
917 ins[dev] = 1;
918 break;
919 case SNDRV_SERIAL_GENERIC:
920 break;
921 default:
922 snd_printk("Adaptor type is out of range 0-%d (%d)\n",
923 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
924 return -ENODEV;
925 }
926
927 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
928 snd_printk("Count of outputs is out of range 1-%d (%d)\n",
929 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
930 return -ENODEV;
931 }
932
933 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
934 snd_printk("Count of inputs is out of range 1-%d (%d)\n",
935 SNDRV_SERIAL_MAX_INS, ins[dev]);
936 return -ENODEV;
937 }
938
939 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
940 if (card == NULL)
941 return -ENOMEM;
942
943 strcpy(card->driver, "Serial");
944 strcpy(card->shortname, "Serial MIDI (UART16550A)");
945
946 if ((err = snd_uart16550_create(card,
947 port[dev],
948 irq[dev],
949 speed[dev],
950 base[dev],
951 adaptor[dev],
952 droponfull[dev],
953 &uart)) < 0)
954 goto _err;
955
956 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
957 if (err < 0)
958 goto _err;
959
960 sprintf(card->longname, "%s at 0x%lx, irq %d speed %d div %d outs %d ins %d adaptor %s droponfull %d",
961 card->shortname,
962 uart->base,
963 uart->irq,
964 uart->speed,
965 (int)uart->divisor,
966 outs[dev],
967 ins[dev],
968 adaptor_names[uart->adaptor],
969 uart->drop_on_full);
970
971 snd_card_set_dev(card, &devptr->dev);
972
973 if ((err = snd_card_register(card)) < 0)
974 goto _err;
975
976 platform_set_drvdata(devptr, card);
977 return 0;
978
979 _err:
980 snd_card_free(card);
981 return err;
982}
983
984static int __devexit snd_serial_remove(struct platform_device *devptr)
985{
986 snd_card_free(platform_get_drvdata(devptr));
987 platform_set_drvdata(devptr, NULL);
988 return 0;
989}
990
991#define SND_SERIAL_DRIVER "snd_serial_u16550"
992
993static struct platform_driver snd_serial_driver = {
994 .probe = snd_serial_probe,
995 .remove = __devexit_p( snd_serial_remove),
996 .driver = {
997 .name = SND_SERIAL_DRIVER
998 },
999};
1000
1001static void snd_serial_unregister_all(void)
1002{
1003 int i;
1004
1005 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1006 platform_device_unregister(devices[i]);
1007 platform_driver_unregister(&snd_serial_driver);
1008}
1009
1010static int __init alsa_card_serial_init(void)
1011{
1012 int i, cards, err;
1013
1014 if ((err = platform_driver_register(&snd_serial_driver)) < 0)
1015 return err;
1016
1017 cards = 0;
1018 for (i = 0; i < SNDRV_CARDS; i++) {
1019 struct platform_device *device;
1020 if (! enable[i])
1021 continue;
1022 device = platform_device_register_simple(SND_SERIAL_DRIVER,
1023 i, NULL, 0);
1024 if (IS_ERR(device))
1025 continue;
1026 if (!platform_get_drvdata(device)) {
1027 platform_device_unregister(device);
1028 continue;
1029 }
1030 devices[i] = device;
1031 cards++;
1032 }
1033 if (! cards) {
1034#ifdef MODULE
1035 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
1036#endif
1037 snd_serial_unregister_all();
1038 return -ENODEV;
1039 }
1040 return 0;
1041}
1042
1043static void __exit alsa_card_serial_exit(void)
1044{
1045 snd_serial_unregister_all();
1046}
1047
1048module_init(alsa_card_serial_init)
1049module_exit(alsa_card_serial_exit)
1050