4 * The low level driver for Roland MPU-401 compatible Midi cards.
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
14 * Thomas Sailer ioctl code reworked (vmalloc/vfree removed)
15 * Alan Cox modularisation, use normal request_irq, use dev_id
16 * Bartlomiej Zolnierkiewicz removed some __init to allow using many drivers
17 * Chris Rankin Update the module-usage counter for the coprocessor
18 * Zwane Mwaikambo Changed attach/unload resource freeing
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #define USE_SEQ_MACROS
25 #define USE_SIMPLE_MACROS
27 #include "sound_config.h"
32 static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
50 unsigned char version, revision;
51 unsigned int capabilities;
52 #define MPU_CAP_INTLG 0x10000000
53 #define MPU_CAP_SYNC 0x00000010
54 #define MPU_CAP_FSK 0x00000020
55 #define MPU_CAP_CLS 0x00000040
56 #define MPU_CAP_SMPTE 0x00000080
57 #define MPU_CAP_2PORT 0x00000001
61 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
62 {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
64 unsigned char m_buf[MBUF_MAX];
68 unsigned char last_status;
69 void (*inputintr) (int dev, unsigned char data);
74 #define DATAPORT(base) (base)
75 #define COMDPORT(base) (base+1)
76 #define STATPORT(base) (base+1)
79 static void mpu401_close(int dev);
81 static inline int mpu401_status(struct mpu_config *devc)
83 return inb(STATPORT(devc->base));
86 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
87 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
89 static inline void write_command(struct mpu_config *devc, unsigned char cmd)
91 outb(cmd, COMDPORT(devc->base));
94 static inline int read_data(struct mpu_config *devc)
96 return inb(DATAPORT(devc->base));
99 static inline void write_data(struct mpu_config *devc, unsigned char byte)
101 outb(byte, DATAPORT(devc->base));
104 #define OUTPUT_READY 0x40
105 #define INPUT_AVAIL 0x80
107 #define MPU_RESET 0xFF
108 #define UART_MODE_ON 0x3F
110 static struct mpu_config dev_conf[MAX_MIDI_DEV] =
115 static int n_mpu_devs = 0;
117 static int reset_mpu401(struct mpu_config *devc);
118 static void set_uart_mode(int dev, struct mpu_config *devc, int arg);
120 static int mpu_timer_init(int midi_dev);
121 static void mpu_timer_interrupt(void);
122 static void timer_ext_event(struct mpu_config *devc, int event, int parm);
124 static struct synth_info mpu_synth_info_proto = {
125 "MPU-401 MIDI interface",
134 static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
137 * States for the input scanner
140 #define ST_INIT 0 /* Ready for timing byte or msg */
141 #define ST_TIMED 1 /* Leading timing byte rcvd */
142 #define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
144 #define ST_SYSMSG 100 /* System message (sysx etc). */
145 #define ST_SYSEX 101 /* System exclusive msg */
146 #define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
147 #define ST_SONGSEL 103 /* Song select */
148 #define ST_SONGPOS 104 /* Song position pointer */
150 static unsigned char len_tab[] = /* # of data bytes following a status
166 unsigned char obuf[8]; \
168 seq_input_event(obuf, len); \
173 #define _SEQ_ADVBUF(x) len=x
175 static int mpu_input_scanner(struct mpu_config *devc, unsigned char midic)
178 switch (devc->m_state)
192 if (devc->timer_flag)
193 mpu_timer_interrupt();
207 printk("<Trk data rq #%d>", midic & 0x0f);
211 printk("<conductor rq>");
215 devc->m_state = ST_SYSMSG;
221 /* printk( "mpu time: %d ", midic); */
222 devc->m_state = ST_TIMED;
225 printk("<MPU: Unknown event %02x> ", midic);
231 int msg = ((int) (midic & 0xf0) >> 4);
233 devc->m_state = ST_DATABYTE;
235 if (msg < 8) /* Data byte */
237 /* printk( "midi msg (running status) "); */
238 msg = ((int) (devc->last_status & 0xf0) >> 4);
240 devc->m_left = len_tab[msg] - 1;
243 devc->m_buf[0] = devc->last_status;
244 devc->m_buf[1] = midic;
246 if (devc->m_left <= 0)
248 devc->m_state = ST_INIT;
249 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
253 else if (msg == 0xf) /* MPU MARK */
255 devc->m_state = ST_INIT;
260 /* printk( "NOP "); */
264 /* printk( "meas end "); */
268 /* printk( "data end "); */
272 printk("Unknown MPU mark %02x\n", midic);
277 devc->last_status = midic;
278 /* printk( "midi msg "); */
280 devc->m_left = len_tab[msg];
283 devc->m_buf[0] = midic;
285 if (devc->m_left <= 0)
287 devc->m_state = ST_INIT;
288 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
300 devc->m_state = ST_SYSEX;
304 devc->m_state = ST_MTC;
308 devc->m_state = ST_SONGPOS;
313 devc->m_state = ST_SONGSEL;
317 /* printk( "tune_request\n"); */
318 devc->m_state = ST_INIT;
325 devc->m_state = ST_INIT;
326 timer_ext_event(devc, TMR_CLOCK, 0);
330 devc->m_state = ST_INIT;
331 timer_ext_event(devc, TMR_START, 0);
335 devc->m_state = ST_INIT;
336 timer_ext_event(devc, TMR_CONTINUE, 0);
340 devc->m_state = ST_INIT;
341 timer_ext_event(devc, TMR_STOP, 0);
346 devc->m_state = ST_INIT;
350 /* printk( "midi hard reset"); */
351 devc->m_state = ST_INIT;
355 printk("unknown MIDI sysmsg %0x\n", midic);
356 devc->m_state = ST_INIT;
361 devc->m_state = ST_INIT;
362 printk("MTC frame %x02\n", midic);
369 devc->m_state = ST_INIT;
372 printk("%02x ", midic);
377 devc->m_buf[devc->m_ptr++] = midic;
378 if (devc->m_ptr == 2)
380 devc->m_state = ST_INIT;
382 timer_ext_event(devc, TMR_SPP,
383 ((devc->m_buf[1] & 0x7f) << 7) |
384 (devc->m_buf[0] & 0x7f));
390 devc->m_buf[devc->m_ptr++] = midic;
391 if ((--devc->m_left) <= 0)
393 devc->m_state = ST_INIT;
394 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
400 printk("Bad state %d ", devc->m_state);
401 devc->m_state = ST_INIT;
406 static void mpu401_input_loop(struct mpu_config *devc)
416 restore_flags(flags);
418 if (busy) /* Already inside the scanner */
423 while (input_avail(devc) && n-- > 0)
425 unsigned char c = read_data(devc);
427 if (devc->mode == MODE_SYNTH)
429 mpu_input_scanner(devc, c);
431 else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
432 devc->inputintr(devc->devno, c);
437 int intchk_mpu401(void *dev_id)
439 struct mpu_config *devc;
440 int dev = (int) dev_id;
442 devc = &dev_conf[dev];
443 return input_avail(devc);
446 void mpuintr(int irq, void *dev_id, struct pt_regs *dummy)
448 struct mpu_config *devc;
449 int dev = (int) dev_id;
452 devc = &dev_conf[dev];
454 if (input_avail(devc))
456 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
457 mpu401_input_loop(devc);
460 /* Dummy read (just to acknowledge the interrupt) */
466 static int mpu401_open(int dev, int mode,
467 void (*input) (int dev, unsigned char data),
468 void (*output) (int dev)
472 struct mpu_config *devc;
473 struct coproc_operations *coprocessor;
475 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
478 devc = &dev_conf[dev];
483 * Verify that the device is really running.
484 * Some devices (such as Ensoniq SoundScape don't
485 * work before the on board processor (OBP) is initialized
486 * by downloading its microcode.
489 if (!devc->initialized)
491 if (mpu401_status(devc) == 0xff) /* Bus float */
493 printk(KERN_ERR "mpu401: Device not initialized properly\n");
499 if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
501 if (coprocessor->owner)
502 __MOD_INC_USE_COUNT(coprocessor->owner);
504 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
506 printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
512 set_uart_mode(dev, devc, 1);
513 devc->mode = MODE_MIDI;
516 mpu401_input_loop(devc);
518 devc->inputintr = input;
524 static void mpu401_close(int dev)
526 struct mpu_config *devc;
527 struct coproc_operations *coprocessor;
529 devc = &dev_conf[dev];
531 reset_mpu401(devc); /*
532 * This disables the UART mode
535 devc->inputintr = NULL;
537 coprocessor = midi_devs[dev]->coproc;
539 coprocessor->close(coprocessor->devc, COPR_MIDI);
541 if (coprocessor->owner)
542 __MOD_DEC_USE_COUNT(coprocessor->owner);
547 static int mpu401_out(int dev, unsigned char midi_byte)
552 struct mpu_config *devc;
554 devc = &dev_conf[dev];
557 * Sometimes it takes about 30000 loops before the output becomes ready
558 * (After reset). Normally it takes just about 10 loops.
561 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
565 if (!output_ready(devc))
567 printk(KERN_WARNING "mpu401: Send data timeout\n");
568 restore_flags(flags);
571 write_data(devc, midi_byte);
572 restore_flags(flags);
576 static int mpu401_command(int dev, mpu_command_rec * cmd)
581 struct mpu_config *devc;
583 devc = &dev_conf[dev];
585 if (devc->uart_mode) /*
586 * Not possible in UART mode
589 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
593 * Test for input since pending input seems to block the output.
595 if (input_avail(devc))
596 mpu401_input_loop(devc);
599 * Sometimes it takes about 50000 loops before the output becomes ready
600 * (After reset). Normally it takes just about 10 loops.
607 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
613 if (!output_ready(devc))
615 restore_flags(flags);
618 write_command(devc, cmd->cmd);
621 for (timeout = 50000; timeout > 0 && !ok; timeout--)
623 if (input_avail(devc))
625 if (devc->opened && devc->mode == MODE_SYNTH)
627 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
632 /* Device is not currently open. Use simpler method */
633 if (read_data(devc) == MPU_ACK)
640 restore_flags(flags);
645 for (i = 0; i < cmd->nr_args; i++)
647 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
649 if (!mpu401_out(dev, cmd->data[i]))
651 restore_flags(flags);
652 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
662 for (i = 0; i < cmd->nr_returns; i++)
665 for (timeout = 5000; timeout > 0 && !ok; timeout--)
666 if (input_avail(devc))
668 cmd->data[i] = read_data(devc);
673 restore_flags(flags);
678 restore_flags(flags);
682 static int mpu_cmd(int dev, int cmd, int data)
686 static mpu_command_rec rec;
688 rec.cmd = cmd & 0xff;
689 rec.nr_args = ((cmd & 0xf0) == 0xE0);
690 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
691 rec.data[0] = data & 0xff;
693 if ((ret = mpu401_command(dev, &rec)) < 0)
695 return (unsigned char) rec.data[0];
698 static int mpu401_prefix_cmd(int dev, unsigned char status)
700 struct mpu_config *devc = &dev_conf[dev];
707 if (mpu_cmd(dev, 0xD0, 0) < 0)
714 if (mpu_cmd(dev, 0xDF, 0) < 0)
723 static int mpu401_start_read(int dev)
728 static int mpu401_end_read(int dev)
733 static int mpu401_ioctl(int dev, unsigned cmd, caddr_t arg)
735 struct mpu_config *devc;
739 devc = &dev_conf[dev];
742 case SNDCTL_MIDI_MPUMODE:
743 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
744 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
747 if (get_user(val, (int *)arg))
749 set_uart_mode(dev, devc, !val);
752 case SNDCTL_MIDI_MPUCMD:
753 if (copy_from_user(&rec, arg, sizeof(rec)))
755 if ((ret = mpu401_command(dev, &rec)) < 0)
757 if (copy_to_user(arg, &rec, sizeof(rec)))
766 static void mpu401_kick(int dev)
770 static int mpu401_buffer_status(int dev)
777 static int mpu_synth_ioctl(int dev,
778 unsigned int cmd, caddr_t arg)
781 struct mpu_config *devc;
783 midi_dev = synth_devs[dev]->midi_dev;
785 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
788 devc = &dev_conf[midi_dev];
793 case SNDCTL_SYNTH_INFO:
794 if(copy_to_user((&((char *) arg)[0]), (char *) &mpu_synth_info[midi_dev], sizeof(struct synth_info)))
798 case SNDCTL_SYNTH_MEMAVL:
806 static int mpu_synth_open(int dev, int mode)
809 struct mpu_config *devc;
810 struct coproc_operations *coprocessor;
812 midi_dev = synth_devs[dev]->midi_dev;
814 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
817 devc = &dev_conf[midi_dev];
820 * Verify that the device is really running.
821 * Some devices (such as Ensoniq SoundScape don't
822 * work before the on board processor (OBP) is initialized
823 * by downloading its microcode.
826 if (!devc->initialized)
828 if (mpu401_status(devc) == 0xff) /* Bus float */
830 printk(KERN_ERR "mpu401: Device not initialized properly\n");
837 devc->mode = MODE_SYNTH;
840 devc->inputintr = NULL;
842 coprocessor = midi_devs[midi_dev]->coproc;
844 if (coprocessor->owner)
845 __MOD_INC_USE_COUNT(coprocessor->owner);
847 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
849 printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
856 if (mode & OPEN_READ)
858 mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
859 mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
860 mpu_cmd(midi_dev, 0x87, 0); /* Enable pitch & controller */
865 static void mpu_synth_close(int dev)
868 struct mpu_config *devc;
869 struct coproc_operations *coprocessor;
871 midi_dev = synth_devs[dev]->midi_dev;
873 devc = &dev_conf[midi_dev];
874 mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
875 mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
877 devc->inputintr = NULL;
879 coprocessor = midi_devs[midi_dev]->coproc;
881 coprocessor->close(coprocessor->devc, COPR_MIDI);
883 if (coprocessor->owner)
884 __MOD_DEC_USE_COUNT(coprocessor->owner);
890 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
891 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
892 #include "midi_synth.h"
894 static struct synth_operations mpu401_synth_proto =
900 synth_type: SYNTH_TYPE_MIDI,
902 open: mpu_synth_open,
903 close: mpu_synth_close,
904 ioctl: mpu_synth_ioctl,
905 kill_note: midi_synth_kill_note,
906 start_note: midi_synth_start_note,
907 set_instr: midi_synth_set_instr,
908 reset: midi_synth_reset,
909 hw_control: midi_synth_hw_control,
910 load_patch: midi_synth_load_patch,
911 aftertouch: midi_synth_aftertouch,
912 controller: midi_synth_controller,
913 panning: midi_synth_panning,
914 bender: midi_synth_bender,
915 setup_voice: midi_synth_setup_voice,
916 send_sysex: midi_synth_send_sysex
919 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
921 static struct midi_operations mpu401_midi_proto =
924 info: {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
930 start_read: mpu401_start_read,
931 end_read: mpu401_end_read,
933 buffer_status: mpu401_buffer_status,
934 prefix_cmd: mpu401_prefix_cmd
937 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
939 static void mpu401_chk_version(int n, struct mpu_config *devc)
944 devc->version = devc->revision = 0;
948 if ((tmp = mpu_cmd(n, 0xAC, 0)) < 0)
950 restore_flags(flags);
953 if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
955 restore_flags(flags);
960 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0)
963 restore_flags(flags);
966 devc->revision = tmp;
967 restore_flags(flags);
970 int attach_mpu401(struct address_info *hw_config, struct module *owner)
976 struct mpu_config *devc;
978 hw_config->slots[1] = -1;
979 m = sound_alloc_mididev();
982 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
987 devc->base = hw_config->io_base;
988 devc->osp = hw_config->osp;
989 devc->irq = hw_config->irq;
992 devc->initialized = 0;
995 devc->capabilities = 0;
996 devc->timer_flag = 0;
998 devc->m_state = ST_INIT;
999 devc->shared_irq = hw_config->always_detect;
1000 devc->irq = hw_config->irq;
1005 devc->shared_irq = 1;
1008 if (!hw_config->always_detect)
1010 /* Verify the hardware again */
1011 if (!reset_mpu401(devc))
1013 printk(KERN_WARNING "mpu401: Device didn't respond\n");
1017 if (!devc->shared_irq)
1019 if (request_irq(devc->irq, mpuintr, 0, "mpu401", (void *)m) < 0)
1021 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
1028 mpu401_chk_version(m, devc);
1029 if (devc->version == 0)
1030 mpu401_chk_version(m, devc);
1031 restore_flags(flags);
1034 if (!request_region(hw_config->io_base, 2, "mpu401"))
1040 if (devc->version != 0)
1041 if (mpu_cmd(m, 0xC5, 0) >= 0) /* Set timebase OK */
1042 if (mpu_cmd(m, 0xE0, 120) >= 0) /* Set tempo OK */
1043 devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent mode */
1046 mpu401_synth_operations[m] = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
1048 if (mpu401_synth_operations[m] == NULL)
1050 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1054 if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
1056 memcpy((char *) mpu401_synth_operations[m],
1057 (char *) &std_midi_synth,
1058 sizeof(struct synth_operations));
1062 memcpy((char *) mpu401_synth_operations[m],
1063 (char *) &mpu401_synth_proto,
1064 sizeof(struct synth_operations));
1067 mpu401_synth_operations[m]->owner = owner;
1069 memcpy((char *) &mpu401_midi_operations[m],
1070 (char *) &mpu401_midi_proto,
1071 sizeof(struct midi_operations));
1073 mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1075 memcpy((char *) &mpu_synth_info[m],
1076 (char *) &mpu_synth_info_proto,
1077 sizeof(struct synth_info));
1081 if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
1083 int ports = (devc->revision & 0x08) ? 32 : 16;
1085 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1086 MPU_CAP_CLS | MPU_CAP_2PORT;
1088 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1089 sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
1096 revision_char = devc->revision ? devc->revision + '@' : ' ';
1097 if ((int) devc->revision > ('Z' - '@'))
1098 revision_char = '+';
1100 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1102 if (hw_config->name)
1103 sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
1105 sprintf(mpu_synth_info[m].name,
1106 "MPU-401 %d.%d%c Midi interface #%d",
1107 (int) (devc->version & 0xf0) >> 4,
1108 devc->version & 0x0f,
1113 strcpy(mpu401_midi_operations[m].info.name,
1114 mpu_synth_info[m].name);
1116 conf_printf(mpu_synth_info[m].name, hw_config);
1118 mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1119 mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1121 if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
1122 hw_config->slots[2] = mpu_timer_init(m);
1124 midi_devs[m] = &mpu401_midi_operations[devc->devno];
1127 midi_devs[m]->owner = owner;
1129 hw_config->slots[1] = m;
1135 release_region(hw_config->io_base, 2);
1137 free_irq(devc->irq, (void *)m);
1139 sound_unload_mididev(m);
1144 static int reset_mpu401(struct mpu_config *devc)
1146 unsigned long flags;
1151 * Send the RESET command. Try again if no success at the first time.
1152 * (If the device is in the UART mode, it will not ack the reset cmd).
1157 timeout_limit = devc->initialized ? 30000 : 100000;
1158 devc->initialized = 1;
1160 for (n = 0; n < 2 && !ok; n++)
1162 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1163 ok = output_ready(devc);
1165 write_command(devc, MPU_RESET); /*
1166 * Send MPU-401 RESET Command
1170 * Wait at least 25 msec. This method is not accurate so let's make the
1171 * loop bit longer. Cannot sleep since this is called during boot.
1174 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1178 if (input_avail(devc))
1179 if (read_data(devc) == MPU_ACK)
1181 restore_flags(flags);
1186 devc->m_state = ST_INIT;
1189 devc->last_status = 0;
1190 devc->uart_mode = 0;
1195 static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
1197 if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1199 if ((devc->uart_mode == 0) == (arg == 0))
1200 return; /* Already set */
1201 reset_mpu401(devc); /* This exits the uart mode */
1205 if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
1207 printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1208 devc->uart_mode = 0;
1212 devc->uart_mode = arg;
1216 int probe_mpu401(struct address_info *hw_config)
1219 struct mpu_config tmp_devc;
1221 if (check_region(hw_config->io_base, 2))
1223 printk(KERN_ERR "mpu401: I/O port %x already in use\n\n", hw_config->io_base);
1226 tmp_devc.base = hw_config->io_base;
1227 tmp_devc.irq = hw_config->irq;
1228 tmp_devc.initialized = 0;
1229 tmp_devc.opened = 0;
1230 tmp_devc.osp = hw_config->osp;
1232 if (hw_config->always_detect)
1235 if (inb(hw_config->io_base + 1) == 0xff)
1237 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1238 return 0; /* Just bus float? */
1240 ok = reset_mpu401(&tmp_devc);
1244 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1249 void unload_mpu401(struct address_info *hw_config)
1252 int n=hw_config->slots[1];
1255 release_region(hw_config->io_base, 2);
1256 if (hw_config->always_detect == 0 && hw_config->irq > 0)
1257 free_irq(hw_config->irq, (void *)n);
1258 p=mpu401_synth_operations[n];
1259 sound_unload_mididev(n);
1260 sound_unload_timerdev(hw_config->slots[2]);
1266 /*****************************************************
1268 ****************************************************/
1270 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1271 static volatile int curr_tempo, curr_timebase, hw_timebase;
1272 static int max_timebase = 8; /* 8*24=192 ppqn */
1273 static volatile unsigned long next_event_time;
1274 static volatile unsigned long curr_ticks, curr_clocks;
1275 static unsigned long prev_event_time;
1276 static int metronome_mode;
1278 static unsigned long clocks2ticks(unsigned long clocks)
1281 * The MPU-401 supports just a limited set of possible timebase values.
1282 * Since the applications require more choices, the driver has to
1283 * program the HW to do its best and to convert between the HW and
1286 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1289 static void set_timebase(int midi_dev, int val)
1299 hw_val = (hw_val + 12) / 24;
1300 if (hw_val > max_timebase)
1301 hw_val = max_timebase;
1303 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1305 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
1308 hw_timebase = hw_val * 24;
1309 curr_timebase = val;
1313 static void tmr_reset(void)
1315 unsigned long flags;
1319 next_event_time = (unsigned long) -1;
1320 prev_event_time = 0;
1321 curr_ticks = curr_clocks = 0;
1322 restore_flags(flags);
1325 static void set_timer_mode(int midi_dev)
1327 if (timer_mode & TMR_MODE_CLS)
1328 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1329 else if (timer_mode & TMR_MODE_SMPTE)
1330 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1332 if (timer_mode & TMR_INTERNAL)
1334 mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
1338 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1340 mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
1341 mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
1343 else if (timer_mode & TMR_MODE_FSK)
1344 mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
1348 static void stop_metronome(int midi_dev)
1350 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1353 static void setup_metronome(int midi_dev)
1355 int numerator, denominator;
1356 int clks_per_click, num_32nds_per_beat;
1357 int beats_per_measure;
1359 numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1360 denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1361 clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1362 num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1363 beats_per_measure = (numerator * 4) >> denominator;
1365 if (!metronome_mode)
1366 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1369 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1370 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1371 mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without accents */
1375 static int mpu_start_timer(int midi_dev)
1378 set_timer_mode(midi_dev);
1381 return TIMER_NOT_ARMED; /* Already running */
1383 if (timer_mode & TMR_INTERNAL)
1385 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1387 return TIMER_NOT_ARMED;
1391 mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
1392 mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
1393 mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
1394 mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive messages to PC */
1399 static int mpu_timer_open(int dev, int mode)
1401 int midi_dev = sound_timer_devs[dev]->devlink;
1408 mpu_cmd(midi_dev, 0xE0, 50);
1409 curr_timebase = hw_timebase = 120;
1410 set_timebase(midi_dev, 120);
1413 set_timer_mode(midi_dev);
1415 mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
1416 mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
1421 static void mpu_timer_close(int dev)
1423 int midi_dev = sound_timer_devs[dev]->devlink;
1425 timer_open = tmr_running = 0;
1426 mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
1427 mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
1428 mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to host */
1429 stop_metronome(midi_dev);
1432 static int mpu_timer_event(int dev, unsigned char *event)
1434 unsigned char command = event[1];
1435 unsigned long parm = *(unsigned int *) &event[4];
1436 int midi_dev = sound_timer_devs[dev]->devlink;
1441 parm += prev_event_time;
1447 if (parm <= curr_ticks) /* It's the time */
1448 return TIMER_NOT_ARMED;
1450 next_event_time = prev_event_time = time;
1459 return mpu_start_timer(midi_dev);
1462 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1463 stop_metronome(midi_dev);
1470 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1471 setup_metronome(midi_dev);
1482 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1483 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1489 seq_copy_to_input(event, 8);
1493 if (metronome_mode) /* Metronome enabled */
1495 metronome_mode = parm;
1496 setup_metronome(midi_dev);
1502 return TIMER_NOT_ARMED;
1505 static unsigned long mpu_timer_get_time(int dev)
1513 static int mpu_timer_ioctl(int dev, unsigned int command, caddr_t arg)
1515 int midi_dev = sound_timer_devs[dev]->devlink;
1516 int *p = (int *)arg;
1520 case SNDCTL_TMR_SOURCE:
1524 if (get_user(parm, p))
1532 if (timer_mode & TMR_MODE_CLS)
1533 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1534 else if (timer_mode & TMR_MODE_SMPTE)
1535 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1537 if (put_user(timer_mode, p))
1543 case SNDCTL_TMR_START:
1544 mpu_start_timer(midi_dev);
1547 case SNDCTL_TMR_STOP:
1549 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1550 stop_metronome(midi_dev);
1553 case SNDCTL_TMR_CONTINUE:
1557 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1560 case SNDCTL_TMR_TIMEBASE:
1564 if (get_user(val, p))
1567 set_timebase(midi_dev, val);
1568 if (put_user(curr_timebase, p))
1570 return curr_timebase;
1574 case SNDCTL_TMR_TEMPO:
1579 if (get_user(val, p))
1588 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1590 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1595 if (put_user(curr_tempo, p))
1601 case SNDCTL_SEQ_CTRLRATE:
1605 if (get_user(val, p))
1607 if (val != 0) /* Can't change */
1609 val = (curr_tempo * curr_timebase + 30) / 60;
1610 if (put_user(val, p))
1616 case SNDCTL_SEQ_GETTIME:
1617 if (put_user(curr_ticks, p))
1621 case SNDCTL_TMR_METRONOME:
1622 if (get_user(metronome_mode, p))
1624 setup_metronome(midi_dev);
1632 static void mpu_timer_arm(int dev, long time)
1635 time = curr_ticks + 1;
1636 else if (time <= curr_ticks) /* It's the time */
1638 next_event_time = prev_event_time = time;
1642 static struct sound_timer_operations mpu_timer =
1645 info: {"MPU-401 Timer", 0},
1646 priority: 10, /* Priority */
1647 devlink: 0, /* Local device link */
1648 open: mpu_timer_open,
1649 close: mpu_timer_close,
1650 event: mpu_timer_event,
1651 get_time: mpu_timer_get_time,
1652 ioctl: mpu_timer_ioctl,
1653 arm_timer: mpu_timer_arm
1656 static void mpu_timer_interrupt(void)
1665 curr_ticks = clocks2ticks(curr_clocks);
1667 if (curr_ticks >= next_event_time)
1669 next_event_time = (unsigned long) -1;
1674 static void timer_ext_event(struct mpu_config *devc, int event, int parm)
1676 int midi_dev = devc->devno;
1678 if (!devc->timer_flag)
1684 printk("<MIDI clk>");
1688 printk("Ext MIDI start\n");
1691 if (timer_mode & TMR_EXTERNAL)
1694 setup_metronome(midi_dev);
1695 next_event_time = 0;
1696 STORE(SEQ_START_TIMER());
1702 printk("Ext MIDI stop\n");
1703 if (timer_mode & TMR_EXTERNAL)
1706 stop_metronome(midi_dev);
1707 STORE(SEQ_STOP_TIMER());
1712 printk("Ext MIDI continue\n");
1713 if (timer_mode & TMR_EXTERNAL)
1716 setup_metronome(midi_dev);
1717 STORE(SEQ_CONTINUE_TIMER());
1722 printk("Songpos: %d\n", parm);
1723 if (timer_mode & TMR_EXTERNAL)
1725 STORE(SEQ_SONGPOS(parm));
1731 static int mpu_timer_init(int midi_dev)
1733 struct mpu_config *devc;
1736 devc = &dev_conf[midi_dev];
1738 if (timer_initialized)
1739 return -1; /* There is already a similar timer */
1741 timer_initialized = 1;
1743 mpu_timer.devlink = midi_dev;
1744 dev_conf[midi_dev].timer_flag = 1;
1746 n = sound_alloc_timerdev();
1749 sound_timer_devs[n] = &mpu_timer;
1751 if (devc->version < 0x20) /* Original MPU-401 */
1752 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1756 * The version number 2.0 is used (at least) by the
1757 * MusicQuest cards and the Roland Super-MPU.
1759 * MusicQuest has given a special meaning to the bits of the
1760 * revision number. The Super-MPU returns 0.
1764 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1766 if (devc->revision & 0x02)
1767 timer_caps |= TMR_MODE_CLS;
1770 if (devc->revision & 0x40)
1771 max_timebase = 10; /* Has the 216 and 240 ppqn modes */
1774 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1779 EXPORT_SYMBOL(probe_mpu401);
1780 EXPORT_SYMBOL(attach_mpu401);
1781 EXPORT_SYMBOL(unload_mpu401);
1782 EXPORT_SYMBOL(intchk_mpu401);
1783 EXPORT_SYMBOL(mpuintr);
1785 static struct address_info cfg;
1788 static int irq = -1;
1790 MODULE_PARM(irq, "i");
1791 MODULE_PARM(io, "i");
1793 int __init init_mpu401(void)
1796 /* Can be loaded either for module use or to provide functions
1798 if (io != -1 && irq != -1) {
1801 if (probe_mpu401(&cfg) == 0)
1803 if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
1810 void __exit cleanup_mpu401(void)
1812 if (io != -1 && irq != -1) {
1813 /* Check for use by, for example, sscape driver */
1814 unload_mpu401(&cfg);
1818 module_init(init_mpu401);
1819 module_exit(cleanup_mpu401);
1822 static int __init setup_mpu401(char *str)
1827 str = get_options(str, ARRAY_SIZE(ints), ints);
1835 __setup("mpu401=", setup_mpu401);
1837 MODULE_LICENSE("GPL");