4 * The low level driver for Roland MPU-401 compatible Midi cards.
6 * Copyright by Hannu Savolainen 1993
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 2.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Modified: Riccardo Facchetti 24 Mar 1995 - Added the Audio Excel DSP 16
29 * initialization routine.
32 #define USE_SEQ_MACROS
33 #define USE_SIMPLE_MACROS
35 #include <i386/isa/sound/sound_config.h>
37 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
38 #include <i386/isa/sound/coproc.h>
40 static int init_sequence[20]; /* NOTE! pos 0 = len, start pos 1. */
42 #ifdef CONFIG_SEQUENCER
43 static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
48 int base; /* I/O base */
50 int opened; /* Open mode */
58 u_char version, revision;
60 #define MPU_CAP_INTLG 0x10000000
61 #define MPU_CAP_SYNC 0x00000010
62 #define MPU_CAP_FSK 0x00000020
63 #define MPU_CAP_CLS 0x00000040
64 #define MPU_CAP_SMPTE 0x00000080
65 #define MPU_CAP_2PORT 0x00000001
69 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
70 {printf("MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
72 u_char m_buf[MBUF_MAX];
77 void (*inputintr) (int dev, u_char data);
83 #define DATAPORT(base) (base)
84 #define COMDPORT(base) (base+2)
85 #define STATPORT(base) (base+2)
87 #define DATAPORT(base) (base)
88 #define COMDPORT(base) (base+1)
89 #define STATPORT(base) (base+1)
92 #define mpu401_status(devc) inb( STATPORT((devc)->base))
93 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
94 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
95 #define write_command(devc, cmd) outb( COMDPORT((devc)->base), cmd)
96 #define read_data(devc) inb( DATAPORT((devc)->base))
98 #define write_data(devc, byte) outb( DATAPORT((devc)->base), byte)
100 #define OUTPUT_READY 0x40
101 #define INPUT_AVAIL 0x80
103 #define MPU_RESET 0xFF
104 #define UART_MODE_ON 0x3F
106 static struct mpu_config dev_conf[MAX_MIDI_DEV] = { {0}};
108 static int n_mpu_devs = 0;
109 static volatile int irq2dev[17] =
110 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
112 static int reset_mpu401(struct mpu_config * devc);
113 static void set_uart_mode(int dev, struct mpu_config * devc, int arg);
115 static void mpu_timer_init(int midi_dev);
116 static void mpu_timer_interrupt(void);
117 static void timer_ext_event(struct mpu_config * devc, int event, int parm);
119 static struct synth_info mpu_synth_info_proto =
120 {"MPU-401 MIDI interface", 0, SYNTH_TYPE_MIDI, 0, 0, 128, 0, 128, SYNTH_CAP_INPUT};
122 static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
125 * States for the input scanner
128 #define ST_INIT 0 /* Ready for timing byte or msg */
129 #define ST_TIMED 1 /* Leading timing byte rcvd */
130 #define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
132 #define ST_SYSMSG 100 /* System message (sysx etc). */
133 #define ST_SYSEX 101 /* System exclusive msg */
134 #define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
135 #define ST_SONGSEL 103 /* Song select */
136 #define ST_SONGPOS 104 /* Song position pointer */
138 static u_char len_tab[] =/* # of data bytes following a status */
150 #ifndef CONFIG_SEQUENCER
158 seq_input_event(obuf, len); \
164 #define _SEQ_ADVBUF(x) len=x
167 mpu_input_scanner(struct mpu_config * devc, u_char midic)
170 switch (devc->m_state) {
182 if (devc->timer_flag)
183 mpu_timer_interrupt();
198 printf("<Trk data rq #%d>", midic & 0x0f);
202 printf("<conductor rq>");
206 devc->m_state = ST_SYSMSG;
211 /* printf("mpu time: %d ", midic); */
212 devc->m_state = ST_TIMED;
214 printf("<MPU: Unknown event %02x> ", midic);
220 int msg = ((int) (midic & 0xf0) >> 4);
222 devc->m_state = ST_DATABYTE;
224 if (msg < 8) { /* Data byte */
225 /* printf("midi msg (running status) "); */
226 msg = ((int) (devc->last_status & 0xf0) >> 4);
228 devc->m_left = len_tab[msg] - 1;
231 devc->m_buf[0] = devc->last_status;
232 devc->m_buf[1] = midic;
234 if (devc->m_left <= 0) {
235 devc->m_state = ST_INIT;
236 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
239 } else if (msg == 0xf) { /* MPU MARK */
240 devc->m_state = ST_INIT;
244 /* printf("NOP "); */
248 /* printf("meas end "); */
252 /* printf("data end "); */
256 printf("Unknown MPU mark %02x\n", midic);
259 devc->last_status = midic;
260 /* printf ("midi msg "); */
262 devc->m_left = len_tab[msg];
265 devc->m_buf[0] = midic;
267 if (devc->m_left <= 0) {
268 devc->m_state = ST_INIT;
269 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
280 devc->m_state = ST_SYSEX;
284 devc->m_state = ST_MTC;
288 devc->m_state = ST_SONGPOS;
293 devc->m_state = ST_SONGSEL;
297 /* printf("tune_request\n"); */
298 devc->m_state = ST_INIT;
299 /* XXX do we need a break here ? - lr 970710 */
306 devc->m_state = ST_INIT;
307 timer_ext_event(devc, TMR_CLOCK, 0);
311 devc->m_state = ST_INIT;
312 timer_ext_event(devc, TMR_START, 0);
316 devc->m_state = ST_INIT;
317 timer_ext_event(devc, TMR_CONTINUE, 0);
321 devc->m_state = ST_INIT;
322 timer_ext_event(devc, TMR_STOP, 0);
327 devc->m_state = ST_INIT;
331 /* printf("midi hard reset"); */
332 devc->m_state = ST_INIT;
336 printf("unknown MIDI sysmsg %0x\n", midic);
337 devc->m_state = ST_INIT;
342 devc->m_state = ST_INIT;
343 printf("MTC frame %x02\n", midic);
349 devc->m_state = ST_INIT;
351 printf("%02x ", midic);
356 devc->m_buf[devc->m_ptr++] = midic;
357 if (devc->m_ptr == 2) {
358 devc->m_state = ST_INIT;
360 timer_ext_event(devc, TMR_SPP,
361 ((devc->m_buf[1] & 0x7f) << 7) | (devc->m_buf[0] & 0x7f));
367 devc->m_buf[devc->m_ptr++] = midic;
368 if ((--devc->m_left) <= 0) {
369 devc->m_state = ST_INIT;
370 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
376 printf("Bad state %d ", devc->m_state);
377 devc->m_state = ST_INIT;
384 mpu401_input_loop(struct mpu_config * devc)
394 if (busy) /* Already inside the scanner */
399 while (input_avail(devc) && n-- > 0) {
400 u_char c = read_data(devc);
402 if (devc->mode == MODE_SYNTH) {
403 mpu_input_scanner(devc, c);
404 } else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
405 devc->inputintr(devc->devno, c);
414 struct mpu_config *devc;
418 * FreeBSD (and some others) pass unit number to the interrupt
419 * handler. In this case we have to scan the table for first handler.
422 if (irq < 1 || irq > 15) {
430 for (irq = 0; irq <= 16; irq++)
431 if (irq2dev[irq] != -1)
434 printf("MPU-401: Bogus interrupt #%d?\n", origirq);
438 devc = &dev_conf[dev];
440 devc = &dev_conf[dev];
442 if (input_avail(devc))
443 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
444 mpu401_input_loop(devc);
446 /* Dummy read (just to acknowledge the interrupt) */
453 mpu401_open(int dev, int mode,
454 void (*input) (int dev, u_char data), void (*output) (int dev))
457 struct mpu_config *devc;
459 if (dev < 0 || dev >= num_midis)
462 devc = &dev_conf[dev];
465 printf("MPU-401: Midi busy\n");
469 * Verify that the device is really running. Some devices (such as
470 * Ensoniq SoundScape don't work before the on board processor (OBP)
471 * is initialized by downloading its microcode.
474 if (!devc->initialized) {
475 if (mpu401_status(devc) == 0xff) { /* Bus float */
476 printf("MPU-401: Device not initialized properly\n");
481 irq2dev[devc->irq] = dev;
483 if (midi_devs[dev]->coproc)
484 if ((err = midi_devs[dev]->coproc->
485 open(midi_devs[dev]->coproc->devc, COPR_MIDI)) < 0) {
486 printf("MPU-401: Can't access coprocessor device\n");
490 set_uart_mode(dev, devc, 1);
491 devc->mode = MODE_MIDI;
494 mpu401_input_loop(devc);
496 devc->inputintr = input;
503 mpu401_close(int dev)
505 struct mpu_config *devc;
507 devc = &dev_conf[dev];
510 reset_mpu401(devc); /* This disables the UART mode */
513 devc->inputintr = NULL;
515 if (midi_devs[dev]->coproc)
516 midi_devs[dev]->coproc->close(midi_devs[dev]->coproc->devc, COPR_MIDI);
521 mpu401_out(int dev, u_char midi_byte)
526 struct mpu_config *devc;
528 devc = &dev_conf[dev];
531 * Sometimes it takes about 13000 loops before the output becomes
532 * ready (After reset). Normally it takes just about 10 loops.
536 for (timeout = 23000; timeout > 0 && !output_ready(devc); timeout--);
538 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
542 if (!output_ready(devc)) {
543 printf("MPU-401: Send data timeout\n");
547 write_data(devc, midi_byte);
553 mpu401_command(int dev, mpu_command_rec * cmd)
558 struct mpu_config *devc;
560 devc = &dev_conf[dev];
562 if (devc->uart_mode) { /* Not possible in UART mode */
563 printf("MPU-401 commands not possible in the UART mode\n");
567 * Test for input since pending input seems to block the output.
569 if (input_avail(devc))
570 mpu401_input_loop(devc);
573 * Sometimes it takes about 30000 loops before the output becomes
574 * ready (After reset). Normally it takes just about 10 loops.
583 if (timeout-- <= 0) {
584 printf("MPU-401: Command (0x%x) timeout\n", (int) cmd->cmd);
589 if (!output_ready(devc)) {
593 write_command(devc, cmd->cmd);
595 for (timeout = 50000; timeout > 0 && !ok; timeout--)
596 if (input_avail(devc))
597 if (devc->opened && devc->mode == MODE_SYNTH) {
598 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
600 } else {/* Device is not currently open. Use simplier method */
601 if (read_data(devc) == MPU_ACK)
607 /* printf ("MPU: No ACK to command (0x%x)\n", (int) cmd->cmd); */
611 for (i = 0; i < cmd->nr_args; i++) {
612 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
613 if (!mpu401_out(dev, cmd->data[i])) {
615 printf("MPU: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
624 for (i = 0; i < cmd->nr_returns; i++) {
626 for (timeout = 5000; timeout > 0 && !ok; timeout--)
627 if (input_avail(devc)) {
628 cmd->data[i] = read_data(devc);
633 /* printf ("MPU: No response(%d) to command (0x%x)\n",
634 * i, (int) cmd->cmd);
646 mpu_cmd(int dev, int cmd, int data)
650 static mpu_command_rec rec;
652 rec.cmd = cmd & 0xff;
653 rec.nr_args = ((cmd & 0xf0) == 0xE0);
654 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
655 rec.data[0] = data & 0xff;
657 if ((ret = mpu401_command(dev, &rec)) < 0) {
660 return (u_char) rec.data[0];
664 mpu401_prefix_cmd(int dev, u_char status)
666 struct mpu_config *devc = &dev_conf[dev];
672 if (mpu_cmd(dev, 0xD0, 0) < 0) {
679 if (mpu_cmd(dev, 0xDF, 0) < 0) {
692 mpu401_start_read(int dev)
698 mpu401_end_read(int dev)
704 mpu401_ioctl(int dev, u_int cmd, ioctl_arg arg)
706 struct mpu_config *devc;
708 devc = &dev_conf[dev];
712 bcopy(&(((char *) arg)[0]), (char *) init_sequence, sizeof(init_sequence));
716 case SNDCTL_MIDI_MPUMODE:
717 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
718 printf("MPU-401: Intelligent mode not supported by the HW\n");
721 set_uart_mode(dev, devc, !(*(int *) arg));
725 case SNDCTL_MIDI_MPUCMD:
730 bcopy(&(((char *) arg)[0]), (char *) &rec, sizeof(rec));
732 if ((ret = mpu401_command(dev, &rec)) < 0)
734 bcopy((char *) &rec, &(((char *) arg)[0]), sizeof(rec));
750 mpu401_buffer_status(int dev)
752 return 0; /* No data in buffers */
756 mpu_synth_ioctl(int dev,
757 u_int cmd, ioctl_arg arg)
760 struct mpu_config *devc;
762 midi_dev = synth_devs[dev]->midi_dev;
764 if (midi_dev < 0 || midi_dev > num_midis)
767 devc = &dev_conf[midi_dev];
771 case SNDCTL_SYNTH_INFO:
772 bcopy(&mpu_synth_info[midi_dev], &(((char *) arg)[0]), sizeof(struct synth_info));
776 case SNDCTL_SYNTH_MEMAVL:
786 mpu_synth_open(int dev, int mode)
789 struct mpu_config *devc;
791 midi_dev = synth_devs[dev]->midi_dev;
793 if (midi_dev < 0 || midi_dev > num_midis) {
796 devc = &dev_conf[midi_dev];
799 * Verify that the device is really running. Some devices (such as
800 * Ensoniq SoundScape don't work before the on board processor (OBP)
801 * is initialized by downloading its microcode.
804 if (!devc->initialized) {
805 if (mpu401_status(devc) == 0xff) { /* Bus float */
806 printf("MPU-401: Device not initialized properly\n");
812 printf("MPU-401: Midi busy\n");
815 devc->mode = MODE_SYNTH;
818 devc->inputintr = NULL;
819 irq2dev[devc->irq] = midi_dev;
821 if (midi_devs[midi_dev]->coproc)
822 if ((err = midi_devs[midi_dev]->coproc->
823 open(midi_devs[midi_dev]->coproc->devc, COPR_MIDI)) < 0) {
824 printf("MPU-401: Can't access coprocessor device\n");
831 if (mode & OPEN_READ) {
832 mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
833 mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
839 mpu_synth_close(int dev)
842 struct mpu_config *devc;
844 midi_dev = synth_devs[dev]->midi_dev;
846 devc = &dev_conf[midi_dev];
847 mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
848 mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
850 devc->inputintr = NULL;
852 if (midi_devs[midi_dev]->coproc)
853 midi_devs[midi_dev]->coproc->close(midi_devs[midi_dev]->coproc->devc, COPR_MIDI);
858 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
859 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
860 #include <i386/isa/sound/midi_synth.h>
862 static struct synth_operations mpu401_synth_proto =
871 midi_synth_kill_note,
872 midi_synth_start_note,
873 midi_synth_set_instr,
875 midi_synth_hw_control,
876 midi_synth_load_patch,
877 midi_synth_aftertouch,
878 midi_synth_controller,
884 midi_synth_setup_voice,
885 midi_synth_send_sysex
888 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
890 static struct midi_operations mpu401_midi_proto =
892 {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
903 mpu401_buffer_status,
907 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
910 mpu401_chk_version(struct mpu_config * devc)
914 devc->version = devc->revision = 0;
916 if ((tmp = mpu_cmd(num_midis, 0xAC, 0)) < 0)
919 if ((tmp & 0xf0) > 0x20)/* Why is it larger than 2.x ??? */
924 if ((tmp = mpu_cmd(num_midis, 0xAD, 0)) < 0) {
928 devc->revision = tmp;
932 attach_mpu401(struct address_info * hw_config)
937 struct mpu_config *devc;
939 if (num_midis >= MAX_MIDI_DEV) {
940 printf("MPU-401: Too many midi devices detected\n");
943 devc = &dev_conf[num_midis];
945 devc->base = hw_config->io_base;
946 devc->osp = hw_config->osp;
947 devc->irq = hw_config->irq;
950 devc->initialized = 0;
953 devc->capabilities = 0;
954 devc->timer_flag = 0;
956 devc->m_state = ST_INIT;
957 devc->shared_irq = hw_config->always_detect;
958 devc->irq = hw_config->irq;
962 devc->shared_irq = 1;
964 irq2dev[devc->irq] = num_midis;
966 if (!hw_config->always_detect) {
967 /* Verify the hardware again */
968 if (!reset_mpu401(devc))
971 if (!devc->shared_irq)
972 if (snd_set_irq_handler(devc->irq, mpuintr, devc->osp) < 0) {
976 mpu401_chk_version(devc);
977 if (devc->version == 0)
978 mpu401_chk_version(devc);
982 if (devc->version != 0)
983 if (mpu_cmd(num_midis, 0xC5, 0) >= 0) /* Set timebase OK */
984 if (mpu_cmd(num_midis, 0xE0, 120) >= 0) /* Set tempo OK */
985 devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent
988 mpu401_synth_operations[num_midis] = (struct synth_operations *) malloc(sizeof(struct synth_operations), M_DEVBUF, M_NOWAIT);
990 if (!mpu401_synth_operations[num_midis])
991 panic("SOUND: Cannot allocate memory\n");
993 if (mpu401_synth_operations[num_midis] == NULL) {
994 printf("mpu401: Can't allocate memory\n");
997 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
998 bcopy((char *) &std_midi_synth, (char *) mpu401_synth_operations[num_midis], sizeof(struct synth_operations));
1000 bcopy((char *) &mpu401_synth_proto, (char *) mpu401_synth_operations[num_midis], sizeof(struct synth_operations));
1003 bcopy((char *) &mpu401_midi_proto, (char *) &mpu401_midi_operations[num_midis], sizeof(struct midi_operations));
1005 mpu401_midi_operations[num_midis].converter =
1006 mpu401_synth_operations[num_midis];
1008 bcopy((char *) &mpu_synth_info_proto, (char *) &mpu_synth_info[num_midis], sizeof(struct synth_info));
1012 if (devc->version == 0x20 && devc->revision >= 0x07) { /* MusicQuest interface */
1013 int ports = (devc->revision & 0x08) ? 32 : 16;
1015 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1016 MPU_CAP_CLS | MPU_CAP_2PORT;
1018 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1019 snprintf(mpu_synth_info[num_midis].name,
1020 sizeof(mpu_synth_info[num_midis].name),
1021 "MQX-%d%c MIDI Interface #%d",
1027 revision_char = devc->revision ? devc->revision + '@' : ' ';
1028 if ((int) devc->revision > ('Z' - '@'))
1029 revision_char = '+';
1031 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1033 snprintf(mpu_synth_info[num_midis].name,
1034 sizeof(mpu_synth_info[num_midis].name),
1035 "MPU-401 %d.%d%c Midi interface",
1036 (int) (devc->version & 0xf0) >> 4,
1037 devc->version & 0x0f,
1041 strcpy(mpu401_midi_operations[num_midis].info.name,
1042 mpu_synth_info[num_midis].name);
1044 conf_printf(mpu_synth_info[num_midis].name, hw_config);
1046 mpu401_synth_operations[num_midis]->midi_dev = devc->devno = num_midis;
1047 mpu401_synth_operations[devc->devno]->info =
1048 &mpu_synth_info[devc->devno];
1050 if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
1051 mpu_timer_init(num_midis);
1053 irq2dev[devc->irq] = num_midis;
1054 midi_devs[num_midis++] = &mpu401_midi_operations[devc->devno];
1059 reset_mpu401(struct mpu_config * devc)
1066 * Send the RESET command. Try again if no success at the first time.
1067 * (If the device is in the UART mode, it will not ack the reset
1073 timeout_limit = devc->initialized ? 30000 : 100000;
1074 devc->initialized = 1;
1076 for (n = 0; n < 2 && !ok; n++) {
1077 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1078 ok = output_ready(devc);
1080 write_command(devc, MPU_RESET); /* Send MPU-401 RESET Command */
1083 * Wait at least 25 msec. This method is not accurate so
1084 * let's make the loop bit longer. Cannot sleep since this is
1085 * called during boot.
1088 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--) {
1090 if ( (input_avail(devc)) && (read_data(devc) == MPU_ACK) )
1097 devc->m_state = ST_INIT;
1100 devc->last_status = 0;
1101 devc->uart_mode = 0;
1107 set_uart_mode(int dev, struct mpu_config * devc, int arg)
1109 if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1111 if ((devc->uart_mode == 0) == (arg == 0))
1112 return; /* Already set */
1113 reset_mpu401(devc); /* This exits the uart mode */
1115 if (arg && (mpu_cmd(dev, UART_MODE_ON, 0) < 0) ) {
1116 printf("MPU%d: Can't enter UART mode\n", devc->devno);
1117 devc->uart_mode = 0;
1120 devc->uart_mode = arg;
1124 probe_mpu401(struct address_info * hw_config)
1127 struct mpu_config tmp_devc;
1129 tmp_devc.base = hw_config->io_base;
1130 tmp_devc.irq = hw_config->irq;
1131 tmp_devc.initialized = 0;
1132 tmp_devc.opened = 0;
1133 tmp_devc.osp = hw_config->osp;
1135 #if defined(CONFIG_AEDSP16) && defined(AEDSP16_MPU401)
1137 * Initialize Audio Excel DSP 16 to MPU-401, before any operation.
1139 InitAEDSP16_MPU401(hw_config);
1142 if (hw_config->always_detect)
1145 if (mpu401_status(&tmp_devc) == 0xff) {
1146 DDB(printf("MPU401: Port %x looks dead.\n", hw_config->io_base));
1147 return 0; /* Just bus float? */
1149 ok = reset_mpu401(&tmp_devc);
1152 DDB(printf("MPU401: Reset failed on port %x\n", hw_config->io_base));
1162 #if defined(CONFIG_SEQUENCER)
1164 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1165 static volatile int curr_tempo, curr_timebase, hw_timebase;
1166 static int max_timebase = 8; /* 8*24=192 ppqn */
1167 static volatile u_long next_event_time;
1168 static volatile u_long curr_ticks, curr_clocks;
1169 static u_long prev_event_time;
1170 static int metronome_mode;
1173 clocks2ticks(u_long clocks)
1176 * The MPU-401 supports just a limited set of possible timebase
1177 * values. Since the applications require more choices, the driver
1178 * has to program the HW to do its best and to convert between the
1179 * HW and actual timebases.
1182 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1186 set_timebase(int midi_dev, int val)
1196 hw_val = (hw_val + 12) / 24;
1197 if (hw_val > max_timebase)
1198 hw_val = max_timebase;
1200 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0) {
1201 printf("MPU: Can't set HW timebase to %d\n", hw_val * 24);
1204 hw_timebase = hw_val * 24;
1205 curr_timebase = val;
1214 next_event_time = 0xffffffff;
1215 prev_event_time = 0;
1216 curr_ticks = curr_clocks = 0;
1221 set_timer_mode(int midi_dev)
1223 if (timer_mode & TMR_MODE_CLS)
1224 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1225 else if (timer_mode & TMR_MODE_SMPTE)
1226 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1228 if (timer_mode & TMR_INTERNAL)
1229 mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
1231 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS)) {
1232 mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
1233 mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
1234 } else if (timer_mode & TMR_MODE_FSK)
1235 mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
1240 stop_metronome(int midi_dev)
1242 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1246 setup_metronome(int midi_dev)
1248 int numerator, denominator;
1249 int clks_per_click, num_32nds_per_beat;
1250 int beats_per_measure;
1252 numerator = ((u_int) metronome_mode >> 24) & 0xff;
1253 denominator = ((u_int) metronome_mode >> 16) & 0xff;
1254 clks_per_click = ((u_int) metronome_mode >> 8) & 0xff;
1255 num_32nds_per_beat = (u_int) metronome_mode & 0xff;
1256 beats_per_measure = (numerator * 4) >> denominator;
1258 if (!metronome_mode)
1259 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1261 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1262 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1263 mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without
1269 mpu_start_timer(int midi_dev)
1272 set_timer_mode(midi_dev);
1275 return TIMER_NOT_ARMED; /* Already running */
1277 if (timer_mode & TMR_INTERNAL) {
1278 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1280 return TIMER_NOT_ARMED;
1282 mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
1283 mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
1284 mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
1285 mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive
1293 mpu_timer_open(int dev, int mode)
1295 int midi_dev = sound_timer_devs[dev]->devlink;
1302 mpu_cmd(midi_dev, 0xE0, 50);
1303 curr_timebase = hw_timebase = 120;
1304 set_timebase(midi_dev, 120);
1307 set_timer_mode(midi_dev);
1309 mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
1310 mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
1316 mpu_timer_close(int dev)
1318 int midi_dev = sound_timer_devs[dev]->devlink;
1320 timer_open = tmr_running = 0;
1321 mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
1322 mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
1323 mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to
1325 stop_metronome(midi_dev);
1329 mpu_timer_event(int dev, u_char *event)
1331 u_char command = event[1];
1332 u_long parm = *(u_int *) &event[4];
1333 int midi_dev = sound_timer_devs[dev]->devlink;
1337 parm += prev_event_time;
1342 if (parm <= curr_ticks) /* It's the time */
1343 return TIMER_NOT_ARMED;
1346 next_event_time = prev_event_time = time;
1355 return mpu_start_timer(midi_dev);
1359 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1360 stop_metronome(midi_dev);
1367 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1368 setup_metronome(midi_dev);
1379 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1380 printf("MPU: Can't set tempo to %d\n", (int) parm);
1386 seq_copy_to_input(event, 8);
1390 if (metronome_mode) { /* Metronome enabled */
1391 metronome_mode = parm;
1392 setup_metronome(midi_dev);
1399 return TIMER_NOT_ARMED;
1403 mpu_timer_get_time(int dev)
1412 mpu_timer_ioctl(int dev, u_int command, ioctl_arg arg)
1414 int midi_dev = sound_timer_devs[dev]->devlink;
1417 case SNDCTL_TMR_SOURCE:
1419 int parm = (int) (*(int *) arg) & timer_caps;
1424 if (timer_mode & TMR_MODE_CLS)
1425 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1426 else if (timer_mode & TMR_MODE_SMPTE)
1427 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1429 return *(int *) arg = timer_mode;
1433 case SNDCTL_TMR_START:
1434 mpu_start_timer(midi_dev);
1438 case SNDCTL_TMR_STOP:
1440 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1441 stop_metronome(midi_dev);
1445 case SNDCTL_TMR_CONTINUE:
1449 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1453 case SNDCTL_TMR_TIMEBASE:
1455 int val = (int) (*(int *) arg);
1458 set_timebase(midi_dev, val);
1460 return *(int *) arg = curr_timebase;
1464 case SNDCTL_TMR_TEMPO:
1466 int val = (int) (*(int *) arg);
1470 RANGE (val, 8 , 250 );
1471 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0) {
1472 printf("MPU: Can't set tempo to %d\n", (int) val);
1477 return *(int *) arg = curr_tempo;
1481 case SNDCTL_SEQ_CTRLRATE:
1482 if ((*(int *) arg) != 0) /* Can't change */
1485 return *(int *) arg = ((curr_tempo * curr_timebase) + 30) / 60;
1488 case SNDCTL_TMR_METRONOME:
1489 metronome_mode = (int) (*(int *) arg);
1490 setup_metronome(midi_dev);
1501 mpu_timer_arm(int dev, long time)
1504 time = curr_ticks + 1;
1505 else if (time <= curr_ticks) /* It's the time */
1508 next_event_time = prev_event_time = time;
1513 static struct sound_timer_operations mpu_timer =
1515 {"MPU-401 Timer", 0},
1517 0, /* Local device link */
1527 mpu_timer_interrupt(void)
1537 curr_ticks = clocks2ticks(curr_clocks);
1539 if (curr_ticks >= next_event_time) {
1540 next_event_time = 0xffffffff;
1546 timer_ext_event(struct mpu_config * devc, int event, int parm)
1548 int midi_dev = devc->devno;
1550 if (!devc->timer_flag)
1555 printf("<MIDI clk>");
1559 printf("Ext MIDI start\n");
1561 if (timer_mode & TMR_EXTERNAL) {
1563 setup_metronome(midi_dev);
1564 next_event_time = 0;
1565 STORE(SEQ_START_TIMER());
1570 printf("Ext MIDI stop\n");
1571 if (timer_mode & TMR_EXTERNAL) {
1573 stop_metronome(midi_dev);
1574 STORE(SEQ_STOP_TIMER());
1579 printf("Ext MIDI continue\n");
1580 if (timer_mode & TMR_EXTERNAL) {
1582 setup_metronome(midi_dev);
1583 STORE(SEQ_CONTINUE_TIMER());
1588 printf("Songpos: %d\n", parm);
1589 if (timer_mode & TMR_EXTERNAL) {
1590 STORE(SEQ_SONGPOS(parm));
1597 mpu_timer_init(int midi_dev)
1599 struct mpu_config *devc;
1602 devc = &dev_conf[midi_dev];
1604 if (timer_initialized)
1605 return; /* There is already a similar timer */
1607 timer_initialized = 1;
1609 mpu_timer.devlink = midi_dev;
1610 dev_conf[midi_dev].timer_flag = 1;
1612 if (num_sound_timers >= MAX_TIMER_DEV)
1613 n = 0; /* Overwrite the system timer */
1615 n = num_sound_timers++;
1616 sound_timer_devs[n] = &mpu_timer;
1618 if (devc->version < 0x20) /* Original MPU-401 */
1619 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1622 * The version number 2.0 is used (at least) by the
1623 * MusicQuest cards and the Roland Super-MPU.
1625 * MusicQuest has given a special meaning to the bits of the
1626 * revision number. The Super-MPU returns 0.
1630 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1632 if (devc->revision & 0x02)
1633 timer_caps |= TMR_MODE_CLS;
1636 if (devc->revision & 0x40)
1637 max_timebase = 10; /* Has the 216 and 240 ppqn modes */
1639 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;