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 <linux/init.h>
34#include <linux/interrupt.h>
35#include <linux/err.h>
36#include <linux/platform_device.h>
37#include <linux/slab.h>
38#include <linux/ioport.h>
39#include <linux/moduleparam.h>
40#include <sound/core.h>
41#include <sound/rawmidi.h>
42#include <sound/initval.h>
43
44#include <linux/serial_reg.h>
45#include <linux/jiffies.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(KERN_WARNING
245 "%s: Overrun on device at 0x%lx\n",
246 uart->rmidi->name, uart->base);
247 }
248
249
250 uart->prev_in = substream;
251
252
253
254
255 if (status & UART_LSR_THRE)
256 uart->fifo_count = 0;
257 if (uart->adaptor == SNDRV_SERIAL_MS124W_SA
258 || uart->adaptor == SNDRV_SERIAL_GENERIC) {
259
260 status = inb(uart->base + UART_MSR);
261 while (uart->fifo_count == 0 && (status & UART_MSR_CTS) &&
262 uart->buff_in_count > 0) {
263 snd_uart16550_buffer_output(uart);
264 status = inb(uart->base + UART_MSR);
265 }
266 } else {
267
268 while (uart->fifo_count < uart->fifo_limit
269 && uart->buff_in_count > 0)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296static irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id)
297{
298 struct snd_uart16550 *uart;
299
300 uart = dev_id;
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 }
306
307 inb(uart->base + UART_IIR);
308 snd_uart16550_io_loop(uart);
309 spin_unlock(&uart->open_lock);
310 return IRQ_HANDLED;
311}
312
313
314static void snd_uart16550_buffer_timer(unsigned long data)
315{
316 unsigned long flags;
317 struct snd_uart16550 *uart;
318
319 uart = (struct snd_uart16550 *)data;
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
328
329
330
331static int __devinit snd_uart16550_detect(struct snd_uart16550 *uart)
332{
333 unsigned long io_base = uart->base;
334 int ok;
335 unsigned char c;
336
337
338 if (io_base == 0 || io_base == SNDRV_AUTO_PORT) {
339 return -ENODEV;
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
348
349 ok = 1;
350
351 outb(UART_LCR_WLEN8, io_base + UART_LCR);
352 c = inb(io_base + UART_IER);
353
354 if ((c & 0xf0) != 0)
355 ok = 0;
356
357 outb(0xaa, io_base + UART_SCR);
358
359 c = inb(io_base + UART_SCR);
360
361 if (c != 0xaa)
362 ok = 0;
363
364 outb(0x55, io_base + UART_SCR);
365
366 c = inb(io_base + UART_SCR);
367
368 if (c != 0x55)
369 ok = 0;
370
371 return ok;
372}
373
374static void snd_uart16550_do_open(struct snd_uart16550 * uart)
375{
376 char byte;
377
378
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
387 | UART_FCR_CLEAR_RCVR
388 | UART_FCR_CLEAR_XMIT
389 | UART_FCR_TRIGGER_4
390
391
392
393 ,uart->base + UART_FCR);
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
400 ,uart->base + UART_LCR);
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);
406 outb(0
407 ,uart->base + UART_DLM);
408
409 }
410
411 outb(UART_LCR_WLEN8
412 | 0
413 | 0
414 | 0
415 ,uart->base + UART_LCR);
416
417 switch (uart->adaptor) {
418 default:
419 outb(UART_MCR_RTS
420 | UART_MCR_DTR
421 | UART_MCR_OUT2
422
423
424 ,uart->base + UART_MCR);
425 break;
426 case SNDRV_SERIAL_MS124W_SA:
427 case SNDRV_SERIAL_MS124W_MB:
428
429
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
435
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)
443 |(0 & UART_IER_THRI)
444 ;
445 } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) {
446 byte = UART_IER_RDI
447 | UART_IER_MSI
448 ;
449 } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) {
450 byte = UART_IER_RDI
451 | UART_IER_MSI
452 | UART_IER_THRI
453 ;
454 } else {
455 byte = UART_IER_RDI
456 | UART_IER_THRI
457 ;
458 }
459 outb(byte, uart->base + UART_IER);
460
461 inb(uart->base + UART_LSR);
462 inb(uart->base + UART_IIR);
463 inb(uart->base + UART_RX);
464}
465
466static void snd_uart16550_do_close(struct snd_uart16550 * uart)
467{
468 if (uart->irq < 0)
469 snd_uart16550_del_timer(uart);
470
471
472
473
474
475 outb((0 & UART_IER_RDI)
476 |(0 & UART_IER_THRI)
477 ,uart->base + UART_IER);
478
479 switch (uart->adaptor) {
480 default:
481 outb((0 & UART_MCR_RTS)
482 |(0 & UART_MCR_DTR)
483 |(0 & UART_MCR_OUT2)
484 ,uart->base + UART_MCR);
485 break;
486 case SNDRV_SERIAL_MS124W_SA:
487 case SNDRV_SERIAL_MS124W_MB:
488
489
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
495
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);
502
503
504 if (uart->divisor != 0) {
505 outb(UART_LCR_DLAB
506 ,uart->base + UART_LCR);
507 outb(uart->old_divisor_lsb
508 ,uart->base + UART_DLL);
509 outb(uart->old_divisor_msb
510 ,uart->base + UART_DLM);
511
512 outb(uart->old_line_ctrl_reg
513 ,uart->base + UART_LCR);
514 }
515}
516
517static int snd_uart16550_input_open(struct snd_rawmidi_substream *substream)
518{
519 unsigned long flags;
520 struct snd_uart16550 *uart = substream->rmidi->private_data;
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
531static int snd_uart16550_input_close(struct snd_rawmidi_substream *substream)
532{
533 unsigned long flags;
534 struct snd_uart16550 *uart = substream->rmidi->private_data;
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
545static void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream,
546 int up)
547{
548 unsigned long flags;
549 struct snd_uart16550 *uart = substream->rmidi->private_data;
550
551 spin_lock_irqsave(&uart->open_lock, flags);
552 if (up)
553 uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED;
554 else
555 uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED;
556 spin_unlock_irqrestore(&uart->open_lock, flags);
557}
558
559static int snd_uart16550_output_open(struct snd_rawmidi_substream *substream)
560{
561 unsigned long flags;
562 struct snd_uart16550 *uart = substream->rmidi->private_data;
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
573static int snd_uart16550_output_close(struct snd_rawmidi_substream *substream)
574{
575 unsigned long flags;
576 struct snd_uart16550 *uart = substream->rmidi->private_data;
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
587static inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart,
588 int Num)
589{
590 if (uart->buff_in_count + Num < TX_BUFF_SIZE)
591 return 1;
592 else
593 return 0;
594}
595
596static inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart,
597 unsigned char byte)
598{
599 unsigned short buff_in = uart->buff_in;
600 if (uart->buff_in_count < TX_BUFF_SIZE) {
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)
607 snd_uart16550_add_timer(uart);
608 return 1;
609 } else
610 return 0;
611}
612
613static int snd_uart16550_output_byte(struct snd_uart16550 *uart,
614 struct snd_rawmidi_substream *substream,
615 unsigned char midi_byte)
616{
617 if (uart->buff_in_count == 0
618 && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA &&
619 uart->adaptor != SNDRV_SERIAL_GENERIC) ||
620 (uart->fifo_count == 0
621 && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) {
622
623
624 if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) {
625
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 {
633
634
635 snd_uart16550_write_buffer(uart, midi_byte);
636 }
637 }
638 } else {
639 if (!snd_uart16550_write_buffer(uart, midi_byte)) {
640 snd_printk(KERN_WARNING
641 "%s: Buffer overrun on device at 0x%lx\n",
642 uart->rmidi->name, uart->base);
643 return 0;
644 }
645 }
646
647 return 1;
648}
649
650static void snd_uart16550_output_write(struct snd_rawmidi_substream *substream)
651{
652 unsigned long flags;
653 unsigned char midi_byte, addr_byte;
654 struct snd_uart16550 *uart = substream->rmidi->private_data;
655 char first;
656 static unsigned long lasttime = 0;
657
658
659
660
661
662
663 spin_lock_irqsave(&uart->open_lock, flags);
664
665 if (uart->irq < 0)
666 snd_uart16550_io_loop(uart);
667
668 if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) {
669 while (1) {
670
671
672 if (uart->buff_in_count > TX_BUFF_SIZE - 2)
673 break;
674 if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1)
675 break;
676#ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO
677
678 addr_byte = (1 << (substream->number + 4)) | 0x08;
679#else
680
681 addr_byte = (substream->number << 4) | 0x08;
682
683 if (addr_byte == 0x08)
684 addr_byte = 0xf8;
685#endif
686 snd_uart16550_output_byte(uart, substream, addr_byte);
687
688 snd_uart16550_output_byte(uart, substream, midi_byte);
689 }
690 } else {
691 first = 0;
692 while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) {
693
694
695 if (first == 0 &&
696 (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS ||
697 uart->adaptor == SNDRV_SERIAL_GENERIC) &&
698 (uart->prev_out != substream->number ||
699 time_after(jiffies, lasttime + 3*HZ))) {
700
701 if (snd_uart16550_buffer_can_write(uart, 3)) {
702
703
704
705
706
707
708 uart->prev_out = substream->number;
709
710 snd_uart16550_output_byte(uart, substream,
711 0xf5);
712
713 snd_uart16550_output_byte(uart, substream,
714 uart->prev_out + 1);
715
716
717 if (midi_byte < 0x80 &&
718 uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS)
719 snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]);
720 } else if (!uart->drop_on_full)
721 break;
722
723 }
724
725
726 if (!snd_uart16550_output_byte(uart, substream, midi_byte) &&
727 !uart->drop_on_full )
728 break;
729
730 if (midi_byte >= 0x80 && midi_byte < 0xf0)
731 uart->prev_status[uart->prev_out] = midi_byte;
732 first = 1;
733
734 snd_rawmidi_transmit_ack( substream, 1 );
735 }
736 lasttime = jiffies;
737 }
738 spin_unlock_irqrestore(&uart->open_lock, flags);
739}
740
741static void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream,
742 int up)
743{
744 unsigned long flags;
745 struct snd_uart16550 *uart = substream->rmidi->private_data;
746
747 spin_lock_irqsave(&uart->open_lock, flags);
748 if (up)
749 uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED;
750 else
751 uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED;
752 spin_unlock_irqrestore(&uart->open_lock, flags);
753 if (up)
754 snd_uart16550_output_write(substream);
755}
756
757static struct snd_rawmidi_ops snd_uart16550_output =
758{
759 .open = snd_uart16550_output_open,
760 .close = snd_uart16550_output_close,
761 .trigger = snd_uart16550_output_trigger,
762};
763
764static struct snd_rawmidi_ops snd_uart16550_input =
765{
766 .open = snd_uart16550_input_open,
767 .close = snd_uart16550_input_close,
768 .trigger = snd_uart16550_input_trigger,
769};
770
771static int snd_uart16550_free(struct snd_uart16550 *uart)
772{
773 if (uart->irq >= 0)
774 free_irq(uart->irq, uart);
775 release_and_free_resource(uart->res_base);
776 kfree(uart);
777 return 0;
778};
779
780static int snd_uart16550_dev_free(struct snd_device *device)
781{
782 struct snd_uart16550 *uart = device->device_data;
783 return snd_uart16550_free(uart);
784}
785
786static int __devinit snd_uart16550_create(struct snd_card *card,
787 unsigned long iobase,
788 int irq,
789 unsigned int speed,
790 unsigned int base,
791 int adaptor,
792 int droponfull,
793 struct snd_uart16550 **ruart)
794{
795 static struct snd_device_ops ops = {
796 .dev_free = snd_uart16550_dev_free,
797 };
798 struct snd_uart16550 *uart;
799 int err;
800
801
802 if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL)
803 return -ENOMEM;
804 uart->adaptor = adaptor;
805 uart->card = card;
806 spin_lock_init(&uart->open_lock);
807 uart->irq = -1;
808 uart->base = iobase;
809 uart->drop_on_full = droponfull;
810
811 if ((err = snd_uart16550_detect(uart)) <= 0) {
812 printk(KERN_ERR "no UART detected at 0x%lx\n", iobase);
813 snd_uart16550_free(uart);
814 return -ENODEV;
815 }
816
817 if (irq >= 0 && irq != SNDRV_AUTO_IRQ) {
818 if (request_irq(irq, snd_uart16550_interrupt,
819 IRQF_DISABLED, "Serial MIDI", uart)) {
820 snd_printk(KERN_WARNING
821 "irq %d busy. Using Polling.\n", irq);
822 } else {
823 uart->irq = irq;
824 }
825 }
826 uart->divisor = base / speed;
827 uart->speed = base / (unsigned int)uart->divisor;
828 uart->speed_base = base;
829 uart->prev_out = -1;
830 uart->prev_in = 0;
831 uart->rstatus = 0;
832 memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS);
833 init_timer(&uart->buffer_timer);
834 uart->buffer_timer.function = snd_uart16550_buffer_timer;
835 uart->buffer_timer.data = (unsigned long)uart;
836 uart->timer_running = 0;
837
838
839 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) {
840 snd_uart16550_free(uart);
841 return err;
842 }
843
844 switch (uart->adaptor) {
845 case SNDRV_SERIAL_MS124W_SA:
846 case SNDRV_SERIAL_MS124W_MB:
847
848
849 outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR);
850 break;
851 case SNDRV_SERIAL_MS124T:
852
853
854 outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR);
855 break;
856 default:
857 break;
858 }
859
860 if (ruart)
861 *ruart = uart;
862
863 return 0;
864}
865
866static void __devinit snd_uart16550_substreams(struct snd_rawmidi_str *stream)
867{
868 struct snd_rawmidi_substream *substream;
869
870 list_for_each_entry(substream, &stream->substreams, list) {
871 sprintf(substream->name, "Serial MIDI %d", substream->number + 1);
872 }
873}
874
875static int __devinit snd_uart16550_rmidi(struct snd_uart16550 *uart, int device,
876 int outs, int ins,
877 struct snd_rawmidi **rmidi)
878{
879 struct snd_rawmidi *rrawmidi;
880 int err;
881
882 err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device,
883 outs, ins, &rrawmidi);
884 if (err < 0)
885 return err;
886 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
887 &snd_uart16550_input);
888 snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
889 &snd_uart16550_output);
890 strcpy(rrawmidi->name, "Serial MIDI");
891 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
892 snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
893 rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
894 SNDRV_RAWMIDI_INFO_INPUT |
895 SNDRV_RAWMIDI_INFO_DUPLEX;
896 rrawmidi->private_data = uart;
897 if (rmidi)
898 *rmidi = rrawmidi;
899 return 0;
900}
901
902static int __devinit snd_serial_probe(struct platform_device *devptr)
903{
904 struct snd_card *card;
905 struct snd_uart16550 *uart;
906 int err;
907 int dev = devptr->id;
908
909 switch (adaptor[dev]) {
910 case SNDRV_SERIAL_SOUNDCANVAS:
911 ins[dev] = 1;
912 break;
913 case SNDRV_SERIAL_MS124T:
914 case SNDRV_SERIAL_MS124W_SA:
915 outs[dev] = 1;
916 ins[dev] = 1;
917 break;
918 case SNDRV_SERIAL_MS124W_MB:
919 outs[dev] = 16;
920 ins[dev] = 1;
921 break;
922 case SNDRV_SERIAL_GENERIC:
923 break;
924 default:
925 snd_printk(KERN_ERR
926 "Adaptor type is out of range 0-%d (%d)\n",
927 SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]);
928 return -ENODEV;
929 }
930
931 if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) {
932 snd_printk(KERN_ERR
933 "Count of outputs is out of range 1-%d (%d)\n",
934 SNDRV_SERIAL_MAX_OUTS, outs[dev]);
935 return -ENODEV;
936 }
937
938 if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) {
939 snd_printk(KERN_ERR
940 "Count of inputs is out of range 1-%d (%d)\n",
941 SNDRV_SERIAL_MAX_INS, ins[dev]);
942 return -ENODEV;
943 }
944
945 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
946 if (err < 0)
947 return err;
948
949 strcpy(card->driver, "Serial");
950 strcpy(card->shortname, "Serial MIDI (UART16550A)");
951
952 if ((err = snd_uart16550_create(card,
953 port[dev],
954 irq[dev],
955 speed[dev],
956 base[dev],
957 adaptor[dev],
958 droponfull[dev],
959 &uart)) < 0)
960 goto _err;
961
962 err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi);
963 if (err < 0)
964 goto _err;
965
966 sprintf(card->longname, "%s [%s] at %#lx, irq %d",
967 card->shortname,
968 adaptor_names[uart->adaptor],
969 uart->base,
970 uart->irq);
971
972 snd_card_set_dev(card, &devptr->dev);
973
974 if ((err = snd_card_register(card)) < 0)
975 goto _err;
976
977 platform_set_drvdata(devptr, card);
978 return 0;
979
980 _err:
981 snd_card_free(card);
982 return err;
983}
984
985static int __devexit snd_serial_remove(struct platform_device *devptr)
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,
996 .remove = __devexit_p( snd_serial_remove),
997 .driver = {
998 .name = SND_SERIAL_DRIVER
999 },
1000};
1001
1002static void snd_serial_unregister_all(void)
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
1011static int __init alsa_card_serial_init(void)
1012{
1013 int i, cards, err;
1014
1015 if ((err = platform_driver_register(&snd_serial_driver)) < 0)
1016 return err;
1017
1018 cards = 0;
1019 for (i = 0; i < SNDRV_CARDS; i++) {
1020 struct platform_device *device;
1021 if (! enable[i])
1022 continue;
1023 device = platform_device_register_simple(SND_SERIAL_DRIVER,
1024 i, NULL, 0);
1025 if (IS_ERR(device))
1026 continue;
1027 if (!platform_get_drvdata(device)) {
1028 platform_device_unregister(device);
1029 continue;
1030 }
1031 devices[i] = device;
1032 cards++;
1033 }
1034 if (! cards) {
1035#ifdef MODULE
1036 printk(KERN_ERR "serial midi soundcard not found or device busy\n");
1037#endif
1038 snd_serial_unregister_all();
1039 return -ENODEV;
1040 }
1041 return 0;
1042}
1043
1044static void __exit alsa_card_serial_exit(void)
1045{
1046 snd_serial_unregister_all();
1047}
1048
1049module_init(alsa_card_serial_init)
1050module_exit(alsa_card_serial_exit)
1051