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>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #define USE_SEQ_MACROS
26 #define USE_SIMPLE_MACROS
28 #include "sound_config.h"
33 static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
51 unsigned char version, revision;
52 unsigned int capabilities;
53 #define MPU_CAP_INTLG 0x10000000
54 #define MPU_CAP_SYNC 0x00000010
55 #define MPU_CAP_FSK 0x00000020
56 #define MPU_CAP_CLS 0x00000040
57 #define MPU_CAP_SMPTE 0x00000080
58 #define MPU_CAP_2PORT 0x00000001
62 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
63 {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
65 unsigned char m_buf[MBUF_MAX];
69 unsigned char last_status;
70 void (*inputintr) (int dev, unsigned char data);
76 #define DATAPORT(base) (base)
77 #define COMDPORT(base) (base+1)
78 #define STATPORT(base) (base+1)
81 static void mpu401_close(int dev);
83 static inline int mpu401_status(struct mpu_config *devc)
85 return inb(STATPORT(devc->base));
88 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
89 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
91 static inline void write_command(struct mpu_config *devc, unsigned char cmd)
93 outb(cmd, COMDPORT(devc->base));
96 static inline int read_data(struct mpu_config *devc)
98 return inb(DATAPORT(devc->base));
101 static inline void write_data(struct mpu_config *devc, unsigned char byte)
103 outb(byte, DATAPORT(devc->base));
106 #define OUTPUT_READY 0x40
107 #define INPUT_AVAIL 0x80
109 #define MPU_RESET 0xFF
110 #define UART_MODE_ON 0x3F
112 static struct mpu_config dev_conf[MAX_MIDI_DEV];
114 static int n_mpu_devs;
116 static int reset_mpu401(struct mpu_config *devc);
117 static void set_uart_mode(int dev, struct mpu_config *devc, int arg);
119 static int mpu_timer_init(int midi_dev);
120 static void mpu_timer_interrupt(void);
121 static void timer_ext_event(struct mpu_config *devc, int event, int parm);
123 static struct synth_info mpu_synth_info_proto = {
124 "MPU-401 MIDI interface",
133 static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
136 * States for the input scanner
139 #define ST_INIT 0 /* Ready for timing byte or msg */
140 #define ST_TIMED 1 /* Leading timing byte rcvd */
141 #define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
143 #define ST_SYSMSG 100 /* System message (sysx etc). */
144 #define ST_SYSEX 101 /* System exclusive msg */
145 #define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
146 #define ST_SONGSEL 103 /* Song select */
147 #define ST_SONGPOS 104 /* Song position pointer */
149 static unsigned char len_tab[] = /* # of data bytes following a status
165 unsigned char obuf[8]; \
167 seq_input_event(obuf, len); \
172 #define _SEQ_ADVBUF(x) len=x
174 static int mpu_input_scanner(struct mpu_config *devc, unsigned char midic)
177 switch (devc->m_state)
191 if (devc->timer_flag)
192 mpu_timer_interrupt();
206 printk("<Trk data rq #%d>", midic & 0x0f);
210 printk("<conductor rq>");
214 devc->m_state = ST_SYSMSG;
220 /* printk( "mpu time: %d ", midic); */
221 devc->m_state = ST_TIMED;
224 printk("<MPU: Unknown event %02x> ", midic);
230 int msg = ((int) (midic & 0xf0) >> 4);
232 devc->m_state = ST_DATABYTE;
234 if (msg < 8) /* Data byte */
236 /* printk( "midi msg (running status) "); */
237 msg = ((int) (devc->last_status & 0xf0) >> 4);
239 devc->m_left = len_tab[msg] - 1;
242 devc->m_buf[0] = devc->last_status;
243 devc->m_buf[1] = midic;
245 if (devc->m_left <= 0)
247 devc->m_state = ST_INIT;
248 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
252 else if (msg == 0xf) /* MPU MARK */
254 devc->m_state = ST_INIT;
259 /* printk( "NOP "); */
263 /* printk( "meas end "); */
267 /* printk( "data end "); */
271 printk("Unknown MPU mark %02x\n", midic);
276 devc->last_status = midic;
277 /* printk( "midi msg "); */
279 devc->m_left = len_tab[msg];
282 devc->m_buf[0] = midic;
284 if (devc->m_left <= 0)
286 devc->m_state = ST_INIT;
287 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
299 devc->m_state = ST_SYSEX;
303 devc->m_state = ST_MTC;
307 devc->m_state = ST_SONGPOS;
312 devc->m_state = ST_SONGSEL;
316 /* printk( "tune_request\n"); */
317 devc->m_state = ST_INIT;
324 devc->m_state = ST_INIT;
325 timer_ext_event(devc, TMR_CLOCK, 0);
329 devc->m_state = ST_INIT;
330 timer_ext_event(devc, TMR_START, 0);
334 devc->m_state = ST_INIT;
335 timer_ext_event(devc, TMR_CONTINUE, 0);
339 devc->m_state = ST_INIT;
340 timer_ext_event(devc, TMR_STOP, 0);
345 devc->m_state = ST_INIT;
349 /* printk( "midi hard reset"); */
350 devc->m_state = ST_INIT;
354 printk("unknown MIDI sysmsg %0x\n", midic);
355 devc->m_state = ST_INIT;
360 devc->m_state = ST_INIT;
361 printk("MTC frame %x02\n", midic);
368 devc->m_state = ST_INIT;
371 printk("%02x ", midic);
376 devc->m_buf[devc->m_ptr++] = midic;
377 if (devc->m_ptr == 2)
379 devc->m_state = ST_INIT;
381 timer_ext_event(devc, TMR_SPP,
382 ((devc->m_buf[1] & 0x7f) << 7) |
383 (devc->m_buf[0] & 0x7f));
389 devc->m_buf[devc->m_ptr++] = midic;
390 if ((--devc->m_left) <= 0)
392 devc->m_state = ST_INIT;
393 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
399 printk("Bad state %d ", devc->m_state);
400 devc->m_state = ST_INIT;
405 static void mpu401_input_loop(struct mpu_config *devc)
411 spin_lock_irqsave(&devc->lock,flags);
414 spin_unlock_irqrestore(&devc->lock,flags);
416 if (busy) /* Already inside the scanner */
421 while (input_avail(devc) && n-- > 0)
423 unsigned char c = read_data(devc);
425 if (devc->mode == MODE_SYNTH)
427 mpu_input_scanner(devc, c);
429 else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
430 devc->inputintr(devc->devno, c);
435 int intchk_mpu401(void *dev_id)
437 struct mpu_config *devc;
438 int dev = (int) dev_id;
440 devc = &dev_conf[dev];
441 return input_avail(devc);
444 irqreturn_t mpuintr(int irq, void *dev_id, struct pt_regs *dummy)
446 struct mpu_config *devc;
447 int dev = (int) dev_id;
450 devc = &dev_conf[dev];
452 if (input_avail(devc))
455 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
456 mpu401_input_loop(devc);
459 /* Dummy read (just to acknowledge the interrupt) */
463 return IRQ_RETVAL(handled);
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 (!try_module_get(coprocessor->owner)) {
506 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
508 printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
514 set_uart_mode(dev, devc, 1);
515 devc->mode = MODE_MIDI;
518 mpu401_input_loop(devc);
520 devc->inputintr = input;
526 static void mpu401_close(int dev)
528 struct mpu_config *devc;
529 struct coproc_operations *coprocessor;
531 devc = &dev_conf[dev];
533 reset_mpu401(devc); /*
534 * This disables the UART mode
537 devc->inputintr = NULL;
539 coprocessor = midi_devs[dev]->coproc;
541 coprocessor->close(coprocessor->devc, COPR_MIDI);
542 module_put(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--);
563 spin_lock_irqsave(&devc->lock,flags);
564 if (!output_ready(devc))
566 printk(KERN_WARNING "mpu401: Send data timeout\n");
567 spin_unlock_irqrestore(&devc->lock,flags);
570 write_data(devc, midi_byte);
571 spin_unlock_irqrestore(&devc->lock,flags);
575 static int mpu401_command(int dev, mpu_command_rec * cmd)
580 struct mpu_config *devc;
582 devc = &dev_conf[dev];
584 if (devc->uart_mode) /*
585 * Not possible in UART mode
588 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
592 * Test for input since pending input seems to block the output.
594 if (input_avail(devc))
595 mpu401_input_loop(devc);
598 * Sometimes it takes about 50000 loops before the output becomes ready
599 * (After reset). Normally it takes just about 10 loops.
606 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
609 spin_lock_irqsave(&devc->lock,flags);
611 if (!output_ready(devc))
613 spin_unlock_irqrestore(&devc->lock,flags);
616 write_command(devc, cmd->cmd);
619 for (timeout = 50000; timeout > 0 && !ok; timeout--)
621 if (input_avail(devc))
623 if (devc->opened && devc->mode == MODE_SYNTH)
625 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
630 /* Device is not currently open. Use simpler method */
631 if (read_data(devc) == MPU_ACK)
638 spin_unlock_irqrestore(&devc->lock,flags);
643 for (i = 0; i < cmd->nr_args; i++)
645 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
647 if (!mpu401_out(dev, cmd->data[i]))
649 spin_unlock_irqrestore(&devc->lock,flags);
650 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
660 for (i = 0; i < cmd->nr_returns; i++)
663 for (timeout = 5000; timeout > 0 && !ok; timeout--)
664 if (input_avail(devc))
666 cmd->data[i] = read_data(devc);
671 spin_unlock_irqrestore(&devc->lock,flags);
676 spin_unlock_irqrestore(&devc->lock,flags);
680 static int mpu_cmd(int dev, int cmd, int data)
684 static mpu_command_rec rec;
686 rec.cmd = cmd & 0xff;
687 rec.nr_args = ((cmd & 0xf0) == 0xE0);
688 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
689 rec.data[0] = data & 0xff;
691 if ((ret = mpu401_command(dev, &rec)) < 0)
693 return (unsigned char) rec.data[0];
696 static int mpu401_prefix_cmd(int dev, unsigned char status)
698 struct mpu_config *devc = &dev_conf[dev];
705 if (mpu_cmd(dev, 0xD0, 0) < 0)
712 if (mpu_cmd(dev, 0xDF, 0) < 0)
721 static int mpu401_start_read(int dev)
726 static int mpu401_end_read(int dev)
731 static int mpu401_ioctl(int dev, unsigned cmd, caddr_t arg)
733 struct mpu_config *devc;
737 devc = &dev_conf[dev];
740 case SNDCTL_MIDI_MPUMODE:
741 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
742 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
745 if (get_user(val, (int *)arg))
747 set_uart_mode(dev, devc, !val);
750 case SNDCTL_MIDI_MPUCMD:
751 if (copy_from_user(&rec, arg, sizeof(rec)))
753 if ((ret = mpu401_command(dev, &rec)) < 0)
755 if (copy_to_user(arg, &rec, sizeof(rec)))
764 static void mpu401_kick(int dev)
768 static int mpu401_buffer_status(int dev)
775 static int mpu_synth_ioctl(int dev,
776 unsigned int cmd, caddr_t arg)
779 struct mpu_config *devc;
781 midi_dev = synth_devs[dev]->midi_dev;
783 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
786 devc = &dev_conf[midi_dev];
791 case SNDCTL_SYNTH_INFO:
792 if (copy_to_user((&((char *) arg)[0]),
793 (char *) &mpu_synth_info[midi_dev],
794 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 (!try_module_get(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);
882 module_put(coprocessor->owner);
888 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
889 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
890 #include "midi_synth.h"
892 static struct synth_operations mpu401_synth_proto =
894 .owner = THIS_MODULE,
898 .synth_type = SYNTH_TYPE_MIDI,
900 .open = mpu_synth_open,
901 .close = mpu_synth_close,
902 .ioctl = mpu_synth_ioctl,
903 .kill_note = midi_synth_kill_note,
904 .start_note = midi_synth_start_note,
905 .set_instr = midi_synth_set_instr,
906 .reset = midi_synth_reset,
907 .hw_control = midi_synth_hw_control,
908 .load_patch = midi_synth_load_patch,
909 .aftertouch = midi_synth_aftertouch,
910 .controller = midi_synth_controller,
911 .panning = midi_synth_panning,
912 .bender = midi_synth_bender,
913 .setup_voice = midi_synth_setup_voice,
914 .send_sysex = midi_synth_send_sysex
917 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
919 static struct midi_operations mpu401_midi_proto =
921 .owner = THIS_MODULE,
922 .info = {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
925 .close = mpu401_close,
926 .ioctl = mpu401_ioctl,
927 .outputc = mpu401_out,
928 .start_read = mpu401_start_read,
929 .end_read = mpu401_end_read,
931 .buffer_status = mpu401_buffer_status,
932 .prefix_cmd = mpu401_prefix_cmd
935 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
937 static void mpu401_chk_version(int n, struct mpu_config *devc)
942 devc->version = devc->revision = 0;
944 spin_lock_irqsave(&devc->lock,flags);
945 if ((tmp = mpu_cmd(n, 0xAC, 0)) < 0)
947 spin_unlock_irqrestore(&devc->lock,flags);
950 if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
952 spin_unlock_irqrestore(&devc->lock,flags);
957 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0)
960 spin_unlock_irqrestore(&devc->lock,flags);
963 devc->revision = tmp;
964 spin_unlock_irqrestore(&devc->lock,flags);
967 int attach_mpu401(struct address_info *hw_config, struct module *owner)
973 struct mpu_config *devc;
975 hw_config->slots[1] = -1;
976 m = sound_alloc_mididev();
979 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
984 devc->base = hw_config->io_base;
985 devc->osp = hw_config->osp;
986 devc->irq = hw_config->irq;
989 devc->initialized = 0;
992 devc->capabilities = 0;
993 devc->timer_flag = 0;
995 devc->m_state = ST_INIT;
996 devc->shared_irq = hw_config->always_detect;
997 devc->irq = hw_config->irq;
998 spin_lock_init(&devc->lock);
1003 devc->shared_irq = 1;
1006 if (!hw_config->always_detect)
1008 /* Verify the hardware again */
1009 if (!reset_mpu401(devc))
1011 printk(KERN_WARNING "mpu401: Device didn't respond\n");
1015 if (!devc->shared_irq)
1017 if (request_irq(devc->irq, mpuintr, 0, "mpu401", (void *)m) < 0)
1019 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
1024 spin_lock_irqsave(&devc->lock,flags);
1025 mpu401_chk_version(m, devc);
1026 if (devc->version == 0)
1027 mpu401_chk_version(m, devc);
1028 spin_unlock_irqrestore(&devc->lock,flags);
1031 if (!request_region(hw_config->io_base, 2, "mpu401"))
1037 if (devc->version != 0)
1038 if (mpu_cmd(m, 0xC5, 0) >= 0) /* Set timebase OK */
1039 if (mpu_cmd(m, 0xE0, 120) >= 0) /* Set tempo OK */
1040 devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent mode */
1043 mpu401_synth_operations[m] = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
1045 if (mpu401_synth_operations[m] == NULL)
1047 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1051 if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
1053 memcpy((char *) mpu401_synth_operations[m],
1054 (char *) &std_midi_synth,
1055 sizeof(struct synth_operations));
1059 memcpy((char *) mpu401_synth_operations[m],
1060 (char *) &mpu401_synth_proto,
1061 sizeof(struct synth_operations));
1064 mpu401_synth_operations[m]->owner = owner;
1066 memcpy((char *) &mpu401_midi_operations[m],
1067 (char *) &mpu401_midi_proto,
1068 sizeof(struct midi_operations));
1070 mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1072 memcpy((char *) &mpu_synth_info[m],
1073 (char *) &mpu_synth_info_proto,
1074 sizeof(struct synth_info));
1078 if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
1080 int ports = (devc->revision & 0x08) ? 32 : 16;
1082 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1083 MPU_CAP_CLS | MPU_CAP_2PORT;
1085 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1086 sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
1093 revision_char = devc->revision ? devc->revision + '@' : ' ';
1094 if ((int) devc->revision > ('Z' - '@'))
1095 revision_char = '+';
1097 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1099 if (hw_config->name)
1100 sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
1102 sprintf(mpu_synth_info[m].name,
1103 "MPU-401 %d.%d%c Midi interface #%d",
1104 (int) (devc->version & 0xf0) >> 4,
1105 devc->version & 0x0f,
1110 strcpy(mpu401_midi_operations[m].info.name,
1111 mpu_synth_info[m].name);
1113 conf_printf(mpu_synth_info[m].name, hw_config);
1115 mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1116 mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1118 if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
1119 hw_config->slots[2] = mpu_timer_init(m);
1121 midi_devs[m] = &mpu401_midi_operations[devc->devno];
1124 midi_devs[m]->owner = owner;
1126 hw_config->slots[1] = m;
1132 release_region(hw_config->io_base, 2);
1134 free_irq(devc->irq, (void *)m);
1136 sound_unload_mididev(m);
1141 static int reset_mpu401(struct mpu_config *devc)
1143 unsigned long flags;
1148 * Send the RESET command. Try again if no success at the first time.
1149 * (If the device is in the UART mode, it will not ack the reset cmd).
1154 timeout_limit = devc->initialized ? 30000 : 100000;
1155 devc->initialized = 1;
1157 for (n = 0; n < 2 && !ok; n++)
1159 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1160 ok = output_ready(devc);
1162 write_command(devc, MPU_RESET); /*
1163 * Send MPU-401 RESET Command
1167 * Wait at least 25 msec. This method is not accurate so let's make the
1168 * loop bit longer. Cannot sleep since this is called during boot.
1171 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1173 spin_lock_irqsave(&devc->lock,flags);
1174 if (input_avail(devc))
1175 if (read_data(devc) == MPU_ACK)
1177 spin_unlock_irqrestore(&devc->lock,flags);
1182 devc->m_state = ST_INIT;
1185 devc->last_status = 0;
1186 devc->uart_mode = 0;
1191 static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
1193 if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1195 if ((devc->uart_mode == 0) == (arg == 0))
1196 return; /* Already set */
1197 reset_mpu401(devc); /* This exits the uart mode */
1201 if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
1203 printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1204 devc->uart_mode = 0;
1208 devc->uart_mode = arg;
1212 int probe_mpu401(struct address_info *hw_config)
1215 struct mpu_config tmp_devc;
1217 if (check_region(hw_config->io_base, 2))
1219 printk(KERN_ERR "mpu401: I/O port %x already in use\n\n", hw_config->io_base);
1222 tmp_devc.base = hw_config->io_base;
1223 tmp_devc.irq = hw_config->irq;
1224 tmp_devc.initialized = 0;
1225 tmp_devc.opened = 0;
1226 tmp_devc.osp = hw_config->osp;
1228 if (hw_config->always_detect)
1231 if (inb(hw_config->io_base + 1) == 0xff)
1233 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1234 return 0; /* Just bus float? */
1236 ok = reset_mpu401(&tmp_devc);
1240 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1245 void unload_mpu401(struct address_info *hw_config)
1248 int n=hw_config->slots[1];
1251 release_region(hw_config->io_base, 2);
1252 if (hw_config->always_detect == 0 && hw_config->irq > 0)
1253 free_irq(hw_config->irq, (void *)n);
1254 p=mpu401_synth_operations[n];
1255 sound_unload_mididev(n);
1256 sound_unload_timerdev(hw_config->slots[2]);
1262 /*****************************************************
1264 ****************************************************/
1266 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1267 static volatile int curr_tempo, curr_timebase, hw_timebase;
1268 static int max_timebase = 8; /* 8*24=192 ppqn */
1269 static volatile unsigned long next_event_time;
1270 static volatile unsigned long curr_ticks, curr_clocks;
1271 static unsigned long prev_event_time;
1272 static int metronome_mode;
1274 static unsigned long clocks2ticks(unsigned long clocks)
1277 * The MPU-401 supports just a limited set of possible timebase values.
1278 * Since the applications require more choices, the driver has to
1279 * program the HW to do its best and to convert between the HW and
1282 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1285 static void set_timebase(int midi_dev, int val)
1295 hw_val = (hw_val + 12) / 24;
1296 if (hw_val > max_timebase)
1297 hw_val = max_timebase;
1299 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1301 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
1304 hw_timebase = hw_val * 24;
1305 curr_timebase = val;
1309 static void tmr_reset(struct mpu_config *devc)
1311 unsigned long flags;
1313 spin_lock_irqsave(&devc->lock,flags);
1314 next_event_time = (unsigned long) -1;
1315 prev_event_time = 0;
1316 curr_ticks = curr_clocks = 0;
1317 spin_unlock_irqrestore(&devc->lock,flags);
1320 static void set_timer_mode(int midi_dev)
1322 if (timer_mode & TMR_MODE_CLS)
1323 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1324 else if (timer_mode & TMR_MODE_SMPTE)
1325 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1327 if (timer_mode & TMR_INTERNAL)
1329 mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
1333 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1335 mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
1336 mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
1338 else if (timer_mode & TMR_MODE_FSK)
1339 mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
1343 static void stop_metronome(int midi_dev)
1345 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1348 static void setup_metronome(int midi_dev)
1350 int numerator, denominator;
1351 int clks_per_click, num_32nds_per_beat;
1352 int beats_per_measure;
1354 numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1355 denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1356 clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1357 num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1358 beats_per_measure = (numerator * 4) >> denominator;
1360 if (!metronome_mode)
1361 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1364 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1365 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1366 mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without accents */
1370 static int mpu_start_timer(int midi_dev)
1372 struct mpu_config *devc= &dev_conf[midi_dev];
1375 set_timer_mode(midi_dev);
1378 return TIMER_NOT_ARMED; /* Already running */
1380 if (timer_mode & TMR_INTERNAL)
1382 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1384 return TIMER_NOT_ARMED;
1388 mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
1389 mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
1390 mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
1391 mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive messages to PC */
1396 static int mpu_timer_open(int dev, int mode)
1398 int midi_dev = sound_timer_devs[dev]->devlink;
1399 struct mpu_config *devc= &dev_conf[midi_dev];
1406 mpu_cmd(midi_dev, 0xE0, 50);
1407 curr_timebase = hw_timebase = 120;
1408 set_timebase(midi_dev, 120);
1411 set_timer_mode(midi_dev);
1413 mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
1414 mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
1419 static void mpu_timer_close(int dev)
1421 int midi_dev = sound_timer_devs[dev]->devlink;
1423 timer_open = tmr_running = 0;
1424 mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
1425 mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
1426 mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to host */
1427 stop_metronome(midi_dev);
1430 static int mpu_timer_event(int dev, unsigned char *event)
1432 unsigned char command = event[1];
1433 unsigned long parm = *(unsigned int *) &event[4];
1434 int midi_dev = sound_timer_devs[dev]->devlink;
1439 parm += prev_event_time;
1445 if (parm <= curr_ticks) /* It's the time */
1446 return TIMER_NOT_ARMED;
1448 next_event_time = prev_event_time = time;
1457 return mpu_start_timer(midi_dev);
1460 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1461 stop_metronome(midi_dev);
1468 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1469 setup_metronome(midi_dev);
1480 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1481 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1487 seq_copy_to_input(event, 8);
1491 if (metronome_mode) /* Metronome enabled */
1493 metronome_mode = parm;
1494 setup_metronome(midi_dev);
1500 return TIMER_NOT_ARMED;
1503 static unsigned long mpu_timer_get_time(int dev)
1511 static int mpu_timer_ioctl(int dev, unsigned int command, caddr_t arg)
1513 int midi_dev = sound_timer_devs[dev]->devlink;
1517 case SNDCTL_TMR_SOURCE:
1521 parm = *(int *) arg;
1528 if (timer_mode & TMR_MODE_CLS)
1529 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1530 else if (timer_mode & TMR_MODE_SMPTE)
1531 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1533 return (*(int *) arg = timer_mode);
1537 case SNDCTL_TMR_START:
1538 mpu_start_timer(midi_dev);
1541 case SNDCTL_TMR_STOP:
1543 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1544 stop_metronome(midi_dev);
1547 case SNDCTL_TMR_CONTINUE:
1551 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1554 case SNDCTL_TMR_TIMEBASE:
1560 set_timebase(midi_dev, val);
1561 return (*(int *) arg = curr_timebase);
1565 case SNDCTL_TMR_TEMPO:
1578 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1580 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1585 return (*(int *) arg = curr_tempo);
1589 case SNDCTL_SEQ_CTRLRATE:
1594 if (val != 0) /* Can't change */
1596 return (*(int *) arg = ((curr_tempo * curr_timebase) + 30) / 60);
1600 case SNDCTL_SEQ_GETTIME:
1601 return (*(int *) arg = curr_ticks);
1603 case SNDCTL_TMR_METRONOME:
1604 metronome_mode = *(int *) arg;
1605 setup_metronome(midi_dev);
1613 static void mpu_timer_arm(int dev, long time)
1616 time = curr_ticks + 1;
1617 else if (time <= curr_ticks) /* It's the time */
1619 next_event_time = prev_event_time = time;
1623 static struct sound_timer_operations mpu_timer =
1625 .owner = THIS_MODULE,
1626 .info = {"MPU-401 Timer", 0},
1627 .priority = 10, /* Priority */
1628 .devlink = 0, /* Local device link */
1629 .open = mpu_timer_open,
1630 .close = mpu_timer_close,
1631 .event = mpu_timer_event,
1632 .get_time = mpu_timer_get_time,
1633 .ioctl = mpu_timer_ioctl,
1634 .arm_timer = mpu_timer_arm
1637 static void mpu_timer_interrupt(void)
1646 curr_ticks = clocks2ticks(curr_clocks);
1648 if (curr_ticks >= next_event_time)
1650 next_event_time = (unsigned long) -1;
1655 static void timer_ext_event(struct mpu_config *devc, int event, int parm)
1657 int midi_dev = devc->devno;
1659 if (!devc->timer_flag)
1665 printk("<MIDI clk>");
1669 printk("Ext MIDI start\n");
1672 if (timer_mode & TMR_EXTERNAL)
1675 setup_metronome(midi_dev);
1676 next_event_time = 0;
1677 STORE(SEQ_START_TIMER());
1683 printk("Ext MIDI stop\n");
1684 if (timer_mode & TMR_EXTERNAL)
1687 stop_metronome(midi_dev);
1688 STORE(SEQ_STOP_TIMER());
1693 printk("Ext MIDI continue\n");
1694 if (timer_mode & TMR_EXTERNAL)
1697 setup_metronome(midi_dev);
1698 STORE(SEQ_CONTINUE_TIMER());
1703 printk("Songpos: %d\n", parm);
1704 if (timer_mode & TMR_EXTERNAL)
1706 STORE(SEQ_SONGPOS(parm));
1712 static int mpu_timer_init(int midi_dev)
1714 struct mpu_config *devc;
1717 devc = &dev_conf[midi_dev];
1719 if (timer_initialized)
1720 return -1; /* There is already a similar timer */
1722 timer_initialized = 1;
1724 mpu_timer.devlink = midi_dev;
1725 dev_conf[midi_dev].timer_flag = 1;
1727 n = sound_alloc_timerdev();
1730 sound_timer_devs[n] = &mpu_timer;
1732 if (devc->version < 0x20) /* Original MPU-401 */
1733 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1737 * The version number 2.0 is used (at least) by the
1738 * MusicQuest cards and the Roland Super-MPU.
1740 * MusicQuest has given a special meaning to the bits of the
1741 * revision number. The Super-MPU returns 0.
1745 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1747 if (devc->revision & 0x02)
1748 timer_caps |= TMR_MODE_CLS;
1751 if (devc->revision & 0x40)
1752 max_timebase = 10; /* Has the 216 and 240 ppqn modes */
1755 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1760 EXPORT_SYMBOL(probe_mpu401);
1761 EXPORT_SYMBOL(attach_mpu401);
1762 EXPORT_SYMBOL(unload_mpu401);
1763 EXPORT_SYMBOL(intchk_mpu401);
1764 EXPORT_SYMBOL(mpuintr);
1766 static struct address_info cfg;
1769 static int irq = -1;
1771 MODULE_PARM(irq, "i");
1772 MODULE_PARM(io, "i");
1774 static int __init init_mpu401(void)
1777 /* Can be loaded either for module use or to provide functions
1779 if (io != -1 && irq != -1) {
1782 if (probe_mpu401(&cfg) == 0)
1784 if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
1791 static void __exit cleanup_mpu401(void)
1793 if (io != -1 && irq != -1) {
1794 /* Check for use by, for example, sscape driver */
1795 unload_mpu401(&cfg);
1799 module_init(init_mpu401);
1800 module_exit(cleanup_mpu401);
1803 static int __init setup_mpu401(char *str)
1808 str = get_options(str, ARRAY_SIZE(ints), ints);
1816 __setup("mpu401=", setup_mpu401);
1818 MODULE_LICENSE("GPL");