[PATCH] RCU: CREDITS and MAINTAINERS
[linux-2.6/openmoko-kernel/knife-kernel.git] / sound / oss / wf_midi.c
blob75c0c143a7599ba267752bd69695216647946448
1 /*
2 * sound/oss/wf_midi.c
4 * The low level driver for the WaveFront ICS2115 MIDI interface(s)
5 * Note that there is also an MPU-401 emulation (actually, a UART-401
6 * emulation) on the CS4232 on the Tropez Plus. This code has nothing
7 * to do with that interface at all.
9 * The interface is essentially just a UART-401, but is has the
10 * interesting property of supporting what Turtle Beach called
11 * "Virtual MIDI" mode. In this mode, there are effectively *two*
12 * MIDI buses accessible via the interface, one that is routed
13 * solely to/from the external WaveFront synthesizer and the other
14 * corresponding to the pin/socket connector used to link external
15 * MIDI devices to the board.
17 * This driver fully supports this mode, allowing two distinct
18 * midi devices (/dev/midiNN and /dev/midiNN+1) to be used
19 * completely independently, giving 32 channels of MIDI routing,
20 * 16 to the WaveFront synth and 16 to the external MIDI bus.
22 * Switching between the two is accomplished externally by the driver
23 * using the two otherwise unused MIDI bytes. See the code for more details.
25 * NOTE: VIRTUAL MIDI MODE IS ON BY DEFAULT (see wavefront.c)
27 * The main reason to turn off Virtual MIDI mode is when you want to
28 * tightly couple the WaveFront synth with an external MIDI
29 * device. You won't be able to distinguish the source of any MIDI
30 * data except via SysEx ID, but thats probably OK, since for the most
31 * part, the WaveFront won't be sending any MIDI data at all.
33 * The main reason to turn on Virtual MIDI Mode is to provide two
34 * completely independent 16-channel MIDI buses, one to the
35 * WaveFront and one to any external MIDI devices. Given the 32
36 * voice nature of the WaveFront, its pretty easy to find a use
37 * for all 16 channels driving just that synth.
42 * Copyright (C) by Paul Barton-Davis 1998
43 * Some portions of this file are derived from work that is:
45 * CopyriGht (C) by Hannu Savolainen 1993-1996
47 * USS/Lite for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
48 * Version 2 (June 1991). See the "COPYING" file distributed with this software
49 * for more info.
52 #include <linux/init.h>
53 #include <linux/interrupt.h>
54 #include <linux/spinlock.h>
55 #include "sound_config.h"
57 #include <linux/wavefront.h>
59 #ifdef MODULE
61 struct wf_mpu_config {
62 int base;
63 #define DATAPORT(d) (d)->base
64 #define COMDPORT(d) (d)->base+1
65 #define STATPORT(d) (d)->base+1
67 int irq;
68 int opened;
69 int devno;
70 int synthno;
71 int mode;
72 #define MODE_MIDI 1
73 #define MODE_SYNTH 2
75 void (*inputintr) (int dev, unsigned char data);
76 char isvirtual; /* do virtual I/O stuff */
79 static struct wf_mpu_config devs[2];
80 static struct wf_mpu_config *phys_dev = &devs[0];
81 static struct wf_mpu_config *virt_dev = &devs[1];
83 static void start_uart_mode (void);
84 static DEFINE_SPINLOCK(lock);
86 #define OUTPUT_READY 0x40
87 #define INPUT_AVAIL 0x80
88 #define MPU_ACK 0xFE
89 #define UART_MODE_ON 0x3F
91 static inline int wf_mpu_status (void)
93 return inb (STATPORT (phys_dev));
96 static inline int input_avail (void)
98 return !(wf_mpu_status() & INPUT_AVAIL);
101 static inline int output_ready (void)
103 return !(wf_mpu_status() & OUTPUT_READY);
106 static inline int read_data (void)
108 return inb (DATAPORT (phys_dev));
111 static inline void write_data (unsigned char byte)
113 outb (byte, DATAPORT (phys_dev));
117 * States for the input scanner (should be in dev_table.h)
120 #define MST_SYSMSG 100 /* System message (sysx etc). */
121 #define MST_MTC 102 /* Midi Time Code (MTC) qframe msg */
122 #define MST_SONGSEL 103 /* Song select */
123 #define MST_SONGPOS 104 /* Song position pointer */
124 #define MST_TIMED 105 /* Leading timing byte rcvd */
126 /* buffer space check for input scanner */
128 #define BUFTEST(mi) if (mi->m_ptr >= MI_MAX || mi->m_ptr < 0) \
129 {printk(KERN_ERR "WF-MPU: Invalid buffer pointer %d/%d, s=%d\n", \
130 mi->m_ptr, mi->m_left, mi->m_state);mi->m_ptr--;}
132 static unsigned char len_tab[] = /* # of data bytes following a status
135 2, /* 8x */
136 2, /* 9x */
137 2, /* Ax */
138 2, /* Bx */
139 1, /* Cx */
140 1, /* Dx */
141 2, /* Ex */
142 0 /* Fx */
145 static int
146 wf_mpu_input_scanner (int devno, int synthdev, unsigned char midic)
149 struct midi_input_info *mi = &midi_devs[devno]->in_info;
151 switch (mi->m_state) {
152 case MST_INIT:
153 switch (midic) {
154 case 0xf8:
155 /* Timer overflow */
156 break;
158 case 0xfc:
159 break;
161 case 0xfd:
162 /* XXX do something useful with this. If there is
163 an external MIDI timer (e.g. a hardware sequencer,
164 a useful timer can be derived ...
166 For now, no timer support.
168 break;
170 case 0xfe:
171 return MPU_ACK;
172 break;
174 case 0xf0:
175 case 0xf1:
176 case 0xf2:
177 case 0xf3:
178 case 0xf4:
179 case 0xf5:
180 case 0xf6:
181 case 0xf7:
182 break;
184 case 0xf9:
185 break;
187 case 0xff:
188 mi->m_state = MST_SYSMSG;
189 break;
191 default:
192 if (midic <= 0xef) {
193 mi->m_state = MST_TIMED;
195 else
196 printk (KERN_ERR "<MPU: Unknown event %02x> ",
197 midic);
199 break;
201 case MST_TIMED:
203 int msg = ((int) (midic & 0xf0) >> 4);
205 mi->m_state = MST_DATA;
207 if (msg < 8) { /* Data byte */
209 msg = ((int) (mi->m_prev_status & 0xf0) >> 4);
210 msg -= 8;
211 mi->m_left = len_tab[msg] - 1;
213 mi->m_ptr = 2;
214 mi->m_buf[0] = mi->m_prev_status;
215 mi->m_buf[1] = midic;
217 if (mi->m_left <= 0) {
218 mi->m_state = MST_INIT;
219 do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
220 mi->m_ptr = 0;
222 } else if (msg == 0xf) { /* MPU MARK */
224 mi->m_state = MST_INIT;
226 switch (midic) {
227 case 0xf8:
228 break;
230 case 0xf9:
231 break;
233 case 0xfc:
234 break;
236 default:
237 break;
239 } else {
240 mi->m_prev_status = midic;
241 msg -= 8;
242 mi->m_left = len_tab[msg];
244 mi->m_ptr = 1;
245 mi->m_buf[0] = midic;
247 if (mi->m_left <= 0) {
248 mi->m_state = MST_INIT;
249 do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
250 mi->m_ptr = 0;
254 break;
256 case MST_SYSMSG:
257 switch (midic) {
258 case 0xf0:
259 mi->m_state = MST_SYSEX;
260 break;
262 case 0xf1:
263 mi->m_state = MST_MTC;
264 break;
266 case 0xf2:
267 mi->m_state = MST_SONGPOS;
268 mi->m_ptr = 0;
269 break;
271 case 0xf3:
272 mi->m_state = MST_SONGSEL;
273 break;
275 case 0xf6:
276 mi->m_state = MST_INIT;
279 * Real time messages
281 case 0xf8:
282 /* midi clock */
283 mi->m_state = MST_INIT;
284 /* XXX need ext MIDI timer support */
285 break;
287 case 0xfA:
288 mi->m_state = MST_INIT;
289 /* XXX need ext MIDI timer support */
290 break;
292 case 0xFB:
293 mi->m_state = MST_INIT;
294 /* XXX need ext MIDI timer support */
295 break;
297 case 0xFC:
298 mi->m_state = MST_INIT;
299 /* XXX need ext MIDI timer support */
300 break;
302 case 0xFE:
303 /* active sensing */
304 mi->m_state = MST_INIT;
305 break;
307 case 0xff:
308 mi->m_state = MST_INIT;
309 break;
311 default:
312 printk (KERN_ERR "unknown MIDI sysmsg %0x\n", midic);
313 mi->m_state = MST_INIT;
315 break;
317 case MST_MTC:
318 mi->m_state = MST_INIT;
319 break;
321 case MST_SYSEX:
322 if (midic == 0xf7) {
323 mi->m_state = MST_INIT;
324 } else {
325 /* XXX fix me */
327 break;
329 case MST_SONGPOS:
330 BUFTEST (mi);
331 mi->m_buf[mi->m_ptr++] = midic;
332 if (mi->m_ptr == 2) {
333 mi->m_state = MST_INIT;
334 mi->m_ptr = 0;
335 /* XXX need ext MIDI timer support */
337 break;
339 case MST_DATA:
340 BUFTEST (mi);
341 mi->m_buf[mi->m_ptr++] = midic;
342 if ((--mi->m_left) <= 0) {
343 mi->m_state = MST_INIT;
344 do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
345 mi->m_ptr = 0;
347 break;
349 default:
350 printk (KERN_ERR "Bad state %d ", mi->m_state);
351 mi->m_state = MST_INIT;
354 return 1;
357 static irqreturn_t
358 wf_mpuintr(int irq, void *dev_id, struct pt_regs *dummy)
361 struct wf_mpu_config *physical_dev = dev_id;
362 static struct wf_mpu_config *input_dev;
363 struct midi_input_info *mi = &midi_devs[physical_dev->devno]->in_info;
364 int n;
366 if (!input_avail()) { /* not for us */
367 return IRQ_NONE;
370 if (mi->m_busy)
371 return IRQ_HANDLED;
372 spin_lock(&lock);
373 mi->m_busy = 1;
375 if (!input_dev) {
376 input_dev = physical_dev;
379 n = 50; /* XXX why ? */
381 do {
382 unsigned char c = read_data ();
384 if (phys_dev->isvirtual) {
386 if (c == WF_EXTERNAL_SWITCH) {
387 input_dev = virt_dev;
388 continue;
389 } else if (c == WF_INTERNAL_SWITCH) {
390 input_dev = phys_dev;
391 continue;
392 } /* else just leave it as it is */
394 } else {
395 input_dev = phys_dev;
398 if (input_dev->mode == MODE_SYNTH) {
400 wf_mpu_input_scanner (input_dev->devno,
401 input_dev->synthno, c);
403 } else if (input_dev->opened & OPEN_READ) {
405 if (input_dev->inputintr) {
406 input_dev->inputintr (input_dev->devno, c);
410 } while (input_avail() && n-- > 0);
412 mi->m_busy = 0;
413 spin_unlock(&lock);
414 return IRQ_HANDLED;
417 static int
418 wf_mpu_open (int dev, int mode,
419 void (*input) (int dev, unsigned char data),
420 void (*output) (int dev)
423 struct wf_mpu_config *devc;
425 if (dev < 0 || dev >= num_midis || midi_devs[dev]==NULL)
426 return -(ENXIO);
428 if (phys_dev->devno == dev) {
429 devc = phys_dev;
430 } else if (phys_dev->isvirtual && virt_dev->devno == dev) {
431 devc = virt_dev;
432 } else {
433 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
434 return -(EINVAL);
437 if (devc->opened) {
438 return -(EBUSY);
441 devc->mode = MODE_MIDI;
442 devc->opened = mode;
443 devc->synthno = 0;
445 devc->inputintr = input;
446 return 0;
449 static void
450 wf_mpu_close (int dev)
452 struct wf_mpu_config *devc;
454 if (dev < 0 || dev >= num_midis || midi_devs[dev]==NULL)
455 return;
457 if (phys_dev->devno == dev) {
458 devc = phys_dev;
459 } else if (phys_dev->isvirtual && virt_dev->devno == dev) {
460 devc = virt_dev;
461 } else {
462 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
463 return;
466 devc->mode = 0;
467 devc->inputintr = NULL;
468 devc->opened = 0;
471 static int
472 wf_mpu_out (int dev, unsigned char midi_byte)
474 int timeout;
475 unsigned long flags;
476 static int lastoutdev = -1;
477 unsigned char switchch;
479 if (phys_dev->isvirtual && lastoutdev != dev) {
481 if (dev == phys_dev->devno) {
482 switchch = WF_INTERNAL_SWITCH;
483 } else if (dev == virt_dev->devno) {
484 switchch = WF_EXTERNAL_SWITCH;
485 } else {
486 printk (KERN_ERR "WF-MPU: bad device number %d", dev);
487 return (0);
490 /* XXX fix me */
492 for (timeout = 30000; timeout > 0 && !output_ready ();
493 timeout--);
495 spin_lock_irqsave(&lock,flags);
497 if (!output_ready ()) {
498 printk (KERN_WARNING "WF-MPU: Send switch "
499 "byte timeout\n");
500 spin_unlock_irqrestore(&lock,flags);
501 return 0;
504 write_data (switchch);
505 spin_unlock_irqrestore(&lock,flags);
508 lastoutdev = dev;
511 * Sometimes it takes about 30000 loops before the output becomes ready
512 * (After reset). Normally it takes just about 10 loops.
515 /* XXX fix me */
517 for (timeout = 30000; timeout > 0 && !output_ready (); timeout--);
519 spin_lock_irqsave(&lock,flags);
520 if (!output_ready ()) {
521 spin_unlock_irqrestore(&lock,flags);
522 printk (KERN_WARNING "WF-MPU: Send data timeout\n");
523 return 0;
526 write_data (midi_byte);
527 spin_unlock_irqrestore(&lock,flags);
529 return 1;
532 static inline int wf_mpu_start_read (int dev) {
533 return 0;
536 static inline int wf_mpu_end_read (int dev) {
537 return 0;
540 static int wf_mpu_ioctl (int dev, unsigned cmd, void __user *arg)
542 printk (KERN_WARNING
543 "WF-MPU: Intelligent mode not supported by hardware.\n");
544 return -(EINVAL);
547 static int wf_mpu_buffer_status (int dev)
549 return 0;
552 static struct synth_operations wf_mpu_synth_operations[2];
553 static struct midi_operations wf_mpu_midi_operations[2];
555 static struct midi_operations wf_mpu_midi_proto =
557 .owner = THIS_MODULE,
558 .info = {"WF-MPU MIDI", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
559 .in_info = {0}, /* in_info */
560 .open = wf_mpu_open,
561 .close = wf_mpu_close,
562 .ioctl = wf_mpu_ioctl,
563 .outputc = wf_mpu_out,
564 .start_read = wf_mpu_start_read,
565 .end_read = wf_mpu_end_read,
566 .buffer_status = wf_mpu_buffer_status,
569 static struct synth_info wf_mpu_synth_info_proto =
570 {"WaveFront MPU-401 interface", 0,
571 SYNTH_TYPE_MIDI, MIDI_TYPE_MPU401, 0, 128, 0, 128, SYNTH_CAP_INPUT};
573 static struct synth_info wf_mpu_synth_info[2];
575 static int
576 wf_mpu_synth_ioctl (int dev, unsigned int cmd, void __user *arg)
578 int midi_dev;
579 int index;
581 midi_dev = synth_devs[dev]->midi_dev;
583 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev]==NULL)
584 return -(ENXIO);
586 if (midi_dev == phys_dev->devno) {
587 index = 0;
588 } else if (phys_dev->isvirtual && midi_dev == virt_dev->devno) {
589 index = 1;
590 } else {
591 return -(EINVAL);
594 switch (cmd) {
596 case SNDCTL_SYNTH_INFO:
597 if (copy_to_user(arg,
598 &wf_mpu_synth_info[index],
599 sizeof (struct synth_info)))
600 return -EFAULT;
601 return 0;
603 case SNDCTL_SYNTH_MEMAVL:
604 return 0x7fffffff;
606 default:
607 return -EINVAL;
611 static int
612 wf_mpu_synth_open (int dev, int mode)
614 int midi_dev;
615 struct wf_mpu_config *devc;
617 midi_dev = synth_devs[dev]->midi_dev;
619 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev]==NULL) {
620 return -(ENXIO);
623 if (phys_dev->devno == midi_dev) {
624 devc = phys_dev;
625 } else if (phys_dev->isvirtual && virt_dev->devno == midi_dev) {
626 devc = virt_dev;
627 } else {
628 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
629 return -(EINVAL);
632 if (devc->opened) {
633 return -(EBUSY);
636 devc->mode = MODE_SYNTH;
637 devc->synthno = dev;
638 devc->opened = mode;
639 devc->inputintr = NULL;
640 return 0;
643 static void
644 wf_mpu_synth_close (int dev)
646 int midi_dev;
647 struct wf_mpu_config *devc;
649 midi_dev = synth_devs[dev]->midi_dev;
651 if (phys_dev->devno == midi_dev) {
652 devc = phys_dev;
653 } else if (phys_dev->isvirtual && virt_dev->devno == midi_dev) {
654 devc = virt_dev;
655 } else {
656 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
657 return;
660 devc->inputintr = NULL;
661 devc->opened = 0;
662 devc->mode = 0;
665 #define _MIDI_SYNTH_C_
666 #define MIDI_SYNTH_NAME "WaveFront (MIDI)"
667 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
668 #include "midi_synth.h"
670 static struct synth_operations wf_mpu_synth_proto =
672 .owner = THIS_MODULE,
673 .id = "WaveFront (ICS2115)",
674 .info = NULL, /* info field, filled in during configuration */
675 .midi_dev = 0, /* MIDI dev XXX should this be -1 ? */
676 .synth_type = SYNTH_TYPE_MIDI,
677 .synth_subtype = SAMPLE_TYPE_WAVEFRONT,
678 .open = wf_mpu_synth_open,
679 .close = wf_mpu_synth_close,
680 .ioctl = wf_mpu_synth_ioctl,
681 .kill_note = midi_synth_kill_note,
682 .start_note = midi_synth_start_note,
683 .set_instr = midi_synth_set_instr,
684 .reset = midi_synth_reset,
685 .hw_control = midi_synth_hw_control,
686 .load_patch = midi_synth_load_patch,
687 .aftertouch = midi_synth_aftertouch,
688 .controller = midi_synth_controller,
689 .panning = midi_synth_panning,
690 .bender = midi_synth_bender,
691 .setup_voice = midi_synth_setup_voice,
692 .send_sysex = midi_synth_send_sysex
695 static int
696 config_wf_mpu (struct wf_mpu_config *dev)
699 int is_external;
700 char *name;
701 int index;
703 if (dev == phys_dev) {
704 name = "WaveFront internal MIDI";
705 is_external = 0;
706 index = 0;
707 memcpy ((char *) &wf_mpu_synth_operations[index],
708 (char *) &wf_mpu_synth_proto,
709 sizeof (struct synth_operations));
710 } else {
711 name = "WaveFront external MIDI";
712 is_external = 1;
713 index = 1;
714 /* no synth operations for an external MIDI interface */
717 memcpy ((char *) &wf_mpu_synth_info[dev->devno],
718 (char *) &wf_mpu_synth_info_proto,
719 sizeof (struct synth_info));
721 strcpy (wf_mpu_synth_info[index].name, name);
723 wf_mpu_synth_operations[index].midi_dev = dev->devno;
724 wf_mpu_synth_operations[index].info = &wf_mpu_synth_info[index];
726 memcpy ((char *) &wf_mpu_midi_operations[index],
727 (char *) &wf_mpu_midi_proto,
728 sizeof (struct midi_operations));
730 if (is_external) {
731 wf_mpu_midi_operations[index].converter = NULL;
732 } else {
733 wf_mpu_midi_operations[index].converter =
734 &wf_mpu_synth_operations[index];
737 strcpy (wf_mpu_midi_operations[index].info.name, name);
739 midi_devs[dev->devno] = &wf_mpu_midi_operations[index];
740 midi_devs[dev->devno]->in_info.m_busy = 0;
741 midi_devs[dev->devno]->in_info.m_state = MST_INIT;
742 midi_devs[dev->devno]->in_info.m_ptr = 0;
743 midi_devs[dev->devno]->in_info.m_left = 0;
744 midi_devs[dev->devno]->in_info.m_prev_status = 0;
746 devs[index].opened = 0;
747 devs[index].mode = 0;
749 return (0);
752 int virtual_midi_enable (void)
755 if ((virt_dev->devno < 0) &&
756 (virt_dev->devno = sound_alloc_mididev()) == -1) {
757 printk (KERN_ERR
758 "WF-MPU: too many midi devices detected\n");
759 return -1;
762 config_wf_mpu (virt_dev);
764 phys_dev->isvirtual = 1;
765 return virt_dev->devno;
769 virtual_midi_disable (void)
772 unsigned long flags;
774 spin_lock_irqsave(&lock,flags);
776 wf_mpu_close (virt_dev->devno);
777 /* no synth on virt_dev, so no need to call wf_mpu_synth_close() */
778 phys_dev->isvirtual = 0;
780 spin_unlock_irqrestore(&lock,flags);
782 return 0;
785 int __init detect_wf_mpu (int irq, int io_base)
787 if (!request_region(io_base, 2, "wavefront midi")) {
788 printk (KERN_WARNING "WF-MPU: I/O port %x already in use.\n",
789 io_base);
790 return -1;
793 phys_dev->base = io_base;
794 phys_dev->irq = irq;
795 phys_dev->devno = -1;
796 virt_dev->devno = -1;
798 return 0;
801 int __init install_wf_mpu (void)
803 if ((phys_dev->devno = sound_alloc_mididev()) < 0){
805 printk (KERN_ERR "WF-MPU: Too many MIDI devices detected.\n");
806 release_region(phys_dev->base, 2);
807 return -1;
810 phys_dev->isvirtual = 0;
812 if (config_wf_mpu (phys_dev)) {
814 printk (KERN_WARNING
815 "WF-MPU: configuration for MIDI device %d failed\n",
816 phys_dev->devno);
817 sound_unload_mididev (phys_dev->devno);
821 /* OK, now we're configured to handle an interrupt ... */
823 if (request_irq (phys_dev->irq, wf_mpuintr, IRQF_DISABLED|IRQF_SHARED,
824 "wavefront midi", phys_dev) < 0) {
826 printk (KERN_ERR "WF-MPU: Failed to allocate IRQ%d\n",
827 phys_dev->irq);
828 return -1;
832 /* This being a WaveFront (ICS-2115) emulated MPU-401, we have
833 to switch it into UART (dumb) mode, because otherwise, it
834 won't do anything at all.
837 start_uart_mode ();
839 return phys_dev->devno;
842 void
843 uninstall_wf_mpu (void)
846 release_region (phys_dev->base, 2);
847 free_irq (phys_dev->irq, phys_dev);
848 sound_unload_mididev (phys_dev->devno);
850 if (virt_dev->devno >= 0) {
851 sound_unload_mididev (virt_dev->devno);
855 static void
856 start_uart_mode (void)
859 int ok, i;
860 unsigned long flags;
862 spin_lock_irqsave(&lock,flags);
864 /* XXX fix me */
866 for (i = 0; i < 30000 && !output_ready (); i++);
868 outb (UART_MODE_ON, COMDPORT(phys_dev));
870 for (ok = 0, i = 50000; i > 0 && !ok; i--) {
871 if (input_avail ()) {
872 if (read_data () == MPU_ACK) {
873 ok = 1;
878 spin_unlock_irqrestore(&lock,flags);
880 #endif