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
52 #include <linux/init.h>
53 #include "sound_config.h"
55 #include <linux/wavefront.h>
59 struct wf_mpu_config {
61 #define DATAPORT(d) (d)->base
62 #define COMDPORT(d) (d)->base+1
63 #define STATPORT(d) (d)->base+1
73 void (*inputintr) (int dev, unsigned char data);
74 char isvirtual; /* do virtual I/O stuff */
77 static struct wf_mpu_config devs[2];
78 static struct wf_mpu_config *phys_dev = &devs[0];
79 static struct wf_mpu_config *virt_dev = &devs[1];
81 static void start_uart_mode (void);
83 #define OUTPUT_READY 0x40
84 #define INPUT_AVAIL 0x80
86 #define UART_MODE_ON 0x3F
88 static inline int wf_mpu_status (void)
90 return inb (STATPORT (phys_dev));
93 static inline int input_avail (void)
95 return !(wf_mpu_status() & INPUT_AVAIL);
98 static inline int output_ready (void)
100 return !(wf_mpu_status() & OUTPUT_READY);
103 static inline int read_data (void)
105 return inb (DATAPORT (phys_dev));
108 static inline void write_data (unsigned char byte)
110 outb (byte, DATAPORT (phys_dev));
114 * States for the input scanner (should be in dev_table.h)
117 #define MST_SYSMSG 100 /* System message (sysx etc). */
118 #define MST_MTC 102 /* Midi Time Code (MTC) qframe msg */
119 #define MST_SONGSEL 103 /* Song select */
120 #define MST_SONGPOS 104 /* Song position pointer */
121 #define MST_TIMED 105 /* Leading timing byte rcvd */
123 /* buffer space check for input scanner */
125 #define BUFTEST(mi) if (mi->m_ptr >= MI_MAX || mi->m_ptr < 0) \
126 {printk(KERN_ERR "WF-MPU: Invalid buffer pointer %d/%d, s=%d\n", \
127 mi->m_ptr, mi->m_left, mi->m_state);mi->m_ptr--;}
129 static unsigned char len_tab[] = /* # of data bytes following a status
143 wf_mpu_input_scanner (int devno, int synthdev, unsigned char midic)
146 struct midi_input_info *mi = &midi_devs[devno]->in_info;
148 switch (mi->m_state) {
159 /* XXX do something useful with this. If there is
160 an external MIDI timer (e.g. a hardware sequencer,
161 a useful timer can be derived ...
163 For now, no timer support.
185 mi->m_state = MST_SYSMSG;
190 mi->m_state = MST_TIMED;
193 printk (KERN_ERR "<MPU: Unknown event %02x> ",
200 int msg = ((int) (midic & 0xf0) >> 4);
202 mi->m_state = MST_DATA;
204 if (msg < 8) { /* Data byte */
206 msg = ((int) (mi->m_prev_status & 0xf0) >> 4);
208 mi->m_left = len_tab[msg] - 1;
211 mi->m_buf[0] = mi->m_prev_status;
212 mi->m_buf[1] = midic;
214 if (mi->m_left <= 0) {
215 mi->m_state = MST_INIT;
216 do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
219 } else if (msg == 0xf) { /* MPU MARK */
221 mi->m_state = MST_INIT;
237 mi->m_prev_status = midic;
239 mi->m_left = len_tab[msg];
242 mi->m_buf[0] = midic;
244 if (mi->m_left <= 0) {
245 mi->m_state = MST_INIT;
246 do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
256 mi->m_state = MST_SYSEX;
260 mi->m_state = MST_MTC;
264 mi->m_state = MST_SONGPOS;
269 mi->m_state = MST_SONGSEL;
273 mi->m_state = MST_INIT;
280 mi->m_state = MST_INIT;
281 /* XXX need ext MIDI timer support */
285 mi->m_state = MST_INIT;
286 /* XXX need ext MIDI timer support */
290 mi->m_state = MST_INIT;
291 /* XXX need ext MIDI timer support */
295 mi->m_state = MST_INIT;
296 /* XXX need ext MIDI timer support */
301 mi->m_state = MST_INIT;
305 mi->m_state = MST_INIT;
309 printk (KERN_ERR "unknown MIDI sysmsg %0x\n", midic);
310 mi->m_state = MST_INIT;
315 mi->m_state = MST_INIT;
320 mi->m_state = MST_INIT;
328 mi->m_buf[mi->m_ptr++] = midic;
329 if (mi->m_ptr == 2) {
330 mi->m_state = MST_INIT;
332 /* XXX need ext MIDI timer support */
338 mi->m_buf[mi->m_ptr++] = midic;
339 if ((--mi->m_left) <= 0) {
340 mi->m_state = MST_INIT;
341 do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
347 printk (KERN_ERR "Bad state %d ", mi->m_state);
348 mi->m_state = MST_INIT;
355 wf_mpuintr (int irq, void *dev_id, struct pt_regs *dummy)
358 struct wf_mpu_config *physical_dev = dev_id;
359 static struct wf_mpu_config *input_dev = 0;
360 struct midi_input_info *mi = &midi_devs[physical_dev->devno]->in_info;
363 if (!input_avail()) { /* not for us */
367 if (mi->m_busy) return;
372 input_dev = physical_dev;
375 n = 50; /* XXX why ? */
378 unsigned char c = read_data ();
380 if (phys_dev->isvirtual) {
382 if (c == WF_EXTERNAL_SWITCH) {
383 input_dev = virt_dev;
385 } else if (c == WF_INTERNAL_SWITCH) {
386 input_dev = phys_dev;
388 } /* else just leave it as it is */
391 input_dev = phys_dev;
394 if (input_dev->mode == MODE_SYNTH) {
396 wf_mpu_input_scanner (input_dev->devno,
397 input_dev->synthno, c);
399 } else if (input_dev->opened & OPEN_READ) {
401 if (input_dev->inputintr) {
402 input_dev->inputintr (input_dev->devno, c);
406 } while (input_avail() && n-- > 0);
412 wf_mpu_open (int dev, int mode,
413 void (*input) (int dev, unsigned char data),
414 void (*output) (int dev)
417 struct wf_mpu_config *devc;
419 if (dev < 0 || dev >= num_midis || midi_devs[dev]==NULL)
422 if (phys_dev->devno == dev) {
424 } else if (phys_dev->isvirtual && virt_dev->devno == dev) {
427 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
435 devc->mode = MODE_MIDI;
439 devc->inputintr = input;
444 wf_mpu_close (int dev)
446 struct wf_mpu_config *devc;
448 if (dev < 0 || dev >= num_midis || midi_devs[dev]==NULL)
451 if (phys_dev->devno == dev) {
453 } else if (phys_dev->isvirtual && virt_dev->devno == dev) {
456 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
461 devc->inputintr = NULL;
466 wf_mpu_out (int dev, unsigned char midi_byte)
470 static int lastoutdev = -1;
471 unsigned char switchch;
473 if (phys_dev->isvirtual && lastoutdev != dev) {
475 if (dev == phys_dev->devno) {
476 switchch = WF_INTERNAL_SWITCH;
477 } else if (dev == virt_dev->devno) {
478 switchch = WF_EXTERNAL_SWITCH;
480 printk (KERN_ERR "WF-MPU: bad device number %d", dev);
486 for (timeout = 30000; timeout > 0 && !output_ready ();
492 if (!output_ready ()) {
493 printk (KERN_WARNING "WF-MPU: Send switch "
495 restore_flags (flags);
499 write_data (switchch);
500 restore_flags (flags);
506 * Sometimes it takes about 30000 loops before the output becomes ready
507 * (After reset). Normally it takes just about 10 loops.
512 for (timeout = 30000; timeout > 0 && !output_ready (); timeout--);
516 if (!output_ready ()) {
517 printk (KERN_WARNING "WF-MPU: Send data timeout\n");
518 restore_flags (flags);
522 write_data (midi_byte);
523 restore_flags (flags);
528 static inline int wf_mpu_start_read (int dev) {
532 static inline int wf_mpu_end_read (int dev) {
536 static int wf_mpu_ioctl (int dev, unsigned cmd, caddr_t arg)
539 "WF-MPU: Intelligent mode not supported by hardware.\n");
543 static int wf_mpu_buffer_status (int dev)
548 static struct synth_operations wf_mpu_synth_operations[2];
549 static struct midi_operations wf_mpu_midi_operations[2];
551 static struct midi_operations wf_mpu_midi_proto =
554 info: {"WF-MPU MIDI", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
555 in_info: {0}, /* in_info */
560 start_read: wf_mpu_start_read,
561 end_read: wf_mpu_end_read,
562 buffer_status: wf_mpu_buffer_status,
565 static struct synth_info wf_mpu_synth_info_proto =
566 {"WaveFront MPU-401 interface", 0,
567 SYNTH_TYPE_MIDI, MIDI_TYPE_MPU401, 0, 128, 0, 128, SYNTH_CAP_INPUT};
569 static struct synth_info wf_mpu_synth_info[2];
572 wf_mpu_synth_ioctl (int dev,
573 unsigned int cmd, caddr_t arg)
578 midi_dev = synth_devs[dev]->midi_dev;
580 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev]==NULL)
583 if (midi_dev == phys_dev->devno) {
585 } else if (phys_dev->isvirtual && midi_dev == virt_dev->devno) {
593 case SNDCTL_SYNTH_INFO:
594 if(copy_to_user (&((char *) arg)[0],
595 &wf_mpu_synth_info[index],
596 sizeof (struct synth_info)))
600 case SNDCTL_SYNTH_MEMAVL:
609 wf_mpu_synth_open (int dev, int mode)
612 struct wf_mpu_config *devc;
614 midi_dev = synth_devs[dev]->midi_dev;
616 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev]==NULL) {
620 if (phys_dev->devno == midi_dev) {
622 } else if (phys_dev->isvirtual && virt_dev->devno == midi_dev) {
625 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
633 devc->mode = MODE_SYNTH;
636 devc->inputintr = NULL;
641 wf_mpu_synth_close (int dev)
644 struct wf_mpu_config *devc;
646 midi_dev = synth_devs[dev]->midi_dev;
648 if (phys_dev->devno == midi_dev) {
650 } else if (phys_dev->isvirtual && virt_dev->devno == midi_dev) {
653 printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
657 devc->inputintr = NULL;
662 #define _MIDI_SYNTH_C_
663 #define MIDI_SYNTH_NAME "WaveFront (MIDI)"
664 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
665 #include "midi_synth.h"
667 static struct synth_operations wf_mpu_synth_proto =
670 id: "WaveFront (ICS2115)",
671 info: NULL, /* info field, filled in during configuration */
672 midi_dev: 0, /* MIDI dev XXX should this be -1 ? */
673 synth_type: SYNTH_TYPE_MIDI,
674 synth_subtype: SAMPLE_TYPE_WAVEFRONT,
675 open: wf_mpu_synth_open,
676 close: wf_mpu_synth_close,
677 ioctl: wf_mpu_synth_ioctl,
678 kill_note: midi_synth_kill_note,
679 start_note: midi_synth_start_note,
680 set_instr: midi_synth_set_instr,
681 reset: midi_synth_reset,
682 hw_control: midi_synth_hw_control,
683 load_patch: midi_synth_load_patch,
684 aftertouch: midi_synth_aftertouch,
685 controller: midi_synth_controller,
686 panning: midi_synth_panning,
687 bender: midi_synth_bender,
688 setup_voice: midi_synth_setup_voice,
689 send_sysex: midi_synth_send_sysex
693 config_wf_mpu (struct wf_mpu_config *dev)
700 if (dev == phys_dev) {
701 name = "WaveFront internal MIDI";
704 memcpy ((char *) &wf_mpu_synth_operations[index],
705 (char *) &wf_mpu_synth_proto,
706 sizeof (struct synth_operations));
708 name = "WaveFront external MIDI";
711 /* no synth operations for an external MIDI interface */
714 memcpy ((char *) &wf_mpu_synth_info[dev->devno],
715 (char *) &wf_mpu_synth_info_proto,
716 sizeof (struct synth_info));
718 strcpy (wf_mpu_synth_info[index].name, name);
720 wf_mpu_synth_operations[index].midi_dev = dev->devno;
721 wf_mpu_synth_operations[index].info = &wf_mpu_synth_info[index];
723 memcpy ((char *) &wf_mpu_midi_operations[index],
724 (char *) &wf_mpu_midi_proto,
725 sizeof (struct midi_operations));
728 wf_mpu_midi_operations[index].converter = NULL;
730 wf_mpu_midi_operations[index].converter =
731 &wf_mpu_synth_operations[index];
734 strcpy (wf_mpu_midi_operations[index].info.name, name);
736 midi_devs[dev->devno] = &wf_mpu_midi_operations[index];
737 midi_devs[dev->devno]->in_info.m_busy = 0;
738 midi_devs[dev->devno]->in_info.m_state = MST_INIT;
739 midi_devs[dev->devno]->in_info.m_ptr = 0;
740 midi_devs[dev->devno]->in_info.m_left = 0;
741 midi_devs[dev->devno]->in_info.m_prev_status = 0;
743 devs[index].opened = 0;
744 devs[index].mode = 0;
749 int virtual_midi_enable (void)
752 if ((virt_dev->devno < 0) &&
753 (virt_dev->devno = sound_alloc_mididev()) == -1) {
755 "WF-MPU: too many midi devices detected\n");
759 config_wf_mpu (virt_dev);
761 phys_dev->isvirtual = 1;
762 return virt_dev->devno;
766 virtual_midi_disable (void)
774 wf_mpu_close (virt_dev->devno);
775 /* no synth on virt_dev, so no need to call wf_mpu_synth_close() */
776 phys_dev->isvirtual = 0;
778 restore_flags (flags);
783 int __init detect_wf_mpu (int irq, int io_base)
785 if (check_region (io_base, 2)) {
786 printk (KERN_WARNING "WF-MPU: I/O port %x already in use.\n",
791 phys_dev->base = io_base;
793 phys_dev->devno = -1;
794 virt_dev->devno = -1;
799 int __init install_wf_mpu (void)
801 if ((phys_dev->devno = sound_alloc_mididev()) < 0){
803 printk (KERN_ERR "WF-MPU: Too many MIDI devices detected.\n");
808 request_region (phys_dev->base, 2, "wavefront midi");
809 phys_dev->isvirtual = 0;
811 if (config_wf_mpu (phys_dev)) {
814 "WF-MPU: configuration for MIDI device %d failed\n",
816 sound_unload_mididev (phys_dev->devno);
820 /* OK, now we're configured to handle an interrupt ... */
822 if (request_irq (phys_dev->irq, wf_mpuintr, SA_INTERRUPT|SA_SHIRQ,
823 "wavefront midi", phys_dev) < 0) {
825 printk (KERN_ERR "WF-MPU: Failed to allocate IRQ%d\n",
831 /* This being a WaveFront (ICS-2115) emulated MPU-401, we have
832 to switch it into UART (dumb) mode, because otherwise, it
833 won't do anything at all.
838 return phys_dev->devno;
842 uninstall_wf_mpu (void)
845 release_region (phys_dev->base, 2);
846 free_irq (phys_dev->irq, phys_dev);
847 sound_unload_mididev (phys_dev->devno);
849 if (virt_dev->devno >= 0) {
850 sound_unload_mididev (virt_dev->devno);
855 start_uart_mode (void)
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) {
878 restore_flags (flags);