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 static irqreturn_t mpuintr(int irq, void *dev_id)
437 struct mpu_config *devc;
438 int dev = (int)(unsigned long) dev_id;
441 devc = &dev_conf[dev];
443 if (input_avail(devc))
446 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
447 mpu401_input_loop(devc);
450 /* Dummy read (just to acknowledge the interrupt) */
454 return IRQ_RETVAL(handled);
457 static int mpu401_open(int dev, int mode,
458 void (*input) (int dev, unsigned char data),
459 void (*output) (int dev)
463 struct mpu_config *devc;
464 struct coproc_operations *coprocessor;
466 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
469 devc = &dev_conf[dev];
474 * Verify that the device is really running.
475 * Some devices (such as Ensoniq SoundScape don't
476 * work before the on board processor (OBP) is initialized
477 * by downloading its microcode.
480 if (!devc->initialized)
482 if (mpu401_status(devc) == 0xff) /* Bus float */
484 printk(KERN_ERR "mpu401: Device not initialized properly\n");
490 if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
492 if (!try_module_get(coprocessor->owner)) {
497 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
499 printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
505 set_uart_mode(dev, devc, 1);
506 devc->mode = MODE_MIDI;
509 mpu401_input_loop(devc);
511 devc->inputintr = input;
517 static void mpu401_close(int dev)
519 struct mpu_config *devc;
520 struct coproc_operations *coprocessor;
522 devc = &dev_conf[dev];
524 reset_mpu401(devc); /*
525 * This disables the UART mode
528 devc->inputintr = NULL;
530 coprocessor = midi_devs[dev]->coproc;
532 coprocessor->close(coprocessor->devc, COPR_MIDI);
533 module_put(coprocessor->owner);
538 static int mpu401_out(int dev, unsigned char midi_byte)
543 struct mpu_config *devc;
545 devc = &dev_conf[dev];
548 * Sometimes it takes about 30000 loops before the output becomes ready
549 * (After reset). Normally it takes just about 10 loops.
552 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
554 spin_lock_irqsave(&devc->lock,flags);
555 if (!output_ready(devc))
557 printk(KERN_WARNING "mpu401: Send data timeout\n");
558 spin_unlock_irqrestore(&devc->lock,flags);
561 write_data(devc, midi_byte);
562 spin_unlock_irqrestore(&devc->lock,flags);
566 static int mpu401_command(int dev, mpu_command_rec * cmd)
571 struct mpu_config *devc;
573 devc = &dev_conf[dev];
575 if (devc->uart_mode) /*
576 * Not possible in UART mode
579 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
583 * Test for input since pending input seems to block the output.
585 if (input_avail(devc))
586 mpu401_input_loop(devc);
589 * Sometimes it takes about 50000 loops before the output becomes ready
590 * (After reset). Normally it takes just about 10 loops.
597 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
600 spin_lock_irqsave(&devc->lock,flags);
602 if (!output_ready(devc))
604 spin_unlock_irqrestore(&devc->lock,flags);
607 write_command(devc, cmd->cmd);
610 for (timeout = 50000; timeout > 0 && !ok; timeout--)
612 if (input_avail(devc))
614 if (devc->opened && devc->mode == MODE_SYNTH)
616 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
621 /* Device is not currently open. Use simpler method */
622 if (read_data(devc) == MPU_ACK)
629 spin_unlock_irqrestore(&devc->lock,flags);
634 for (i = 0; i < cmd->nr_args; i++)
636 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
638 if (!mpu401_out(dev, cmd->data[i]))
640 spin_unlock_irqrestore(&devc->lock,flags);
641 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
651 for (i = 0; i < cmd->nr_returns; i++)
654 for (timeout = 5000; timeout > 0 && !ok; timeout--)
655 if (input_avail(devc))
657 cmd->data[i] = read_data(devc);
662 spin_unlock_irqrestore(&devc->lock,flags);
667 spin_unlock_irqrestore(&devc->lock,flags);
671 static int mpu_cmd(int dev, int cmd, int data)
675 static mpu_command_rec rec;
677 rec.cmd = cmd & 0xff;
678 rec.nr_args = ((cmd & 0xf0) == 0xE0);
679 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
680 rec.data[0] = data & 0xff;
682 if ((ret = mpu401_command(dev, &rec)) < 0)
684 return (unsigned char) rec.data[0];
687 static int mpu401_prefix_cmd(int dev, unsigned char status)
689 struct mpu_config *devc = &dev_conf[dev];
696 if (mpu_cmd(dev, 0xD0, 0) < 0)
703 if (mpu_cmd(dev, 0xDF, 0) < 0)
712 static int mpu401_start_read(int dev)
717 static int mpu401_end_read(int dev)
722 static int mpu401_ioctl(int dev, unsigned cmd, void __user *arg)
724 struct mpu_config *devc;
728 devc = &dev_conf[dev];
731 case SNDCTL_MIDI_MPUMODE:
732 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
733 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
736 if (get_user(val, (int __user *)arg))
738 set_uart_mode(dev, devc, !val);
741 case SNDCTL_MIDI_MPUCMD:
742 if (copy_from_user(&rec, arg, sizeof(rec)))
744 if ((ret = mpu401_command(dev, &rec)) < 0)
746 if (copy_to_user(arg, &rec, sizeof(rec)))
755 static void mpu401_kick(int dev)
759 static int mpu401_buffer_status(int dev)
766 static int mpu_synth_ioctl(int dev, unsigned int cmd, void __user *arg)
769 struct mpu_config *devc;
771 midi_dev = synth_devs[dev]->midi_dev;
773 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
776 devc = &dev_conf[midi_dev];
781 case SNDCTL_SYNTH_INFO:
782 if (copy_to_user(arg, &mpu_synth_info[midi_dev],
783 sizeof(struct synth_info)))
787 case SNDCTL_SYNTH_MEMAVL:
795 static int mpu_synth_open(int dev, int mode)
798 struct mpu_config *devc;
799 struct coproc_operations *coprocessor;
801 midi_dev = synth_devs[dev]->midi_dev;
803 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
806 devc = &dev_conf[midi_dev];
809 * Verify that the device is really running.
810 * Some devices (such as Ensoniq SoundScape don't
811 * work before the on board processor (OBP) is initialized
812 * by downloading its microcode.
815 if (!devc->initialized)
817 if (mpu401_status(devc) == 0xff) /* Bus float */
819 printk(KERN_ERR "mpu401: Device not initialized properly\n");
826 devc->mode = MODE_SYNTH;
829 devc->inputintr = NULL;
831 coprocessor = midi_devs[midi_dev]->coproc;
833 if (!try_module_get(coprocessor->owner))
836 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
838 printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
845 if (mode & OPEN_READ)
847 mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
848 mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
849 mpu_cmd(midi_dev, 0x87, 0); /* Enable pitch & controller */
854 static void mpu_synth_close(int dev)
857 struct mpu_config *devc;
858 struct coproc_operations *coprocessor;
860 midi_dev = synth_devs[dev]->midi_dev;
862 devc = &dev_conf[midi_dev];
863 mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
864 mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
866 devc->inputintr = NULL;
868 coprocessor = midi_devs[midi_dev]->coproc;
870 coprocessor->close(coprocessor->devc, COPR_MIDI);
871 module_put(coprocessor->owner);
877 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
878 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
879 #include "midi_synth.h"
881 static struct synth_operations mpu401_synth_proto =
883 .owner = THIS_MODULE,
887 .synth_type = SYNTH_TYPE_MIDI,
889 .open = mpu_synth_open,
890 .close = mpu_synth_close,
891 .ioctl = mpu_synth_ioctl,
892 .kill_note = midi_synth_kill_note,
893 .start_note = midi_synth_start_note,
894 .set_instr = midi_synth_set_instr,
895 .reset = midi_synth_reset,
896 .hw_control = midi_synth_hw_control,
897 .load_patch = midi_synth_load_patch,
898 .aftertouch = midi_synth_aftertouch,
899 .controller = midi_synth_controller,
900 .panning = midi_synth_panning,
901 .bender = midi_synth_bender,
902 .setup_voice = midi_synth_setup_voice,
903 .send_sysex = midi_synth_send_sysex
906 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
908 static struct midi_operations mpu401_midi_proto =
910 .owner = THIS_MODULE,
911 .info = {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
914 .close = mpu401_close,
915 .ioctl = mpu401_ioctl,
916 .outputc = mpu401_out,
917 .start_read = mpu401_start_read,
918 .end_read = mpu401_end_read,
920 .buffer_status = mpu401_buffer_status,
921 .prefix_cmd = mpu401_prefix_cmd
924 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
926 static void mpu401_chk_version(int n, struct mpu_config *devc)
931 devc->version = devc->revision = 0;
933 spin_lock_irqsave(&devc->lock,flags);
934 if ((tmp = mpu_cmd(n, 0xAC, 0)) < 0)
936 spin_unlock_irqrestore(&devc->lock,flags);
939 if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
941 spin_unlock_irqrestore(&devc->lock,flags);
946 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0)
949 spin_unlock_irqrestore(&devc->lock,flags);
952 devc->revision = tmp;
953 spin_unlock_irqrestore(&devc->lock,flags);
956 int attach_mpu401(struct address_info *hw_config, struct module *owner)
962 struct mpu_config *devc;
964 hw_config->slots[1] = -1;
965 m = sound_alloc_mididev();
968 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
973 devc->base = hw_config->io_base;
974 devc->osp = hw_config->osp;
975 devc->irq = hw_config->irq;
978 devc->initialized = 0;
981 devc->capabilities = 0;
982 devc->timer_flag = 0;
984 devc->m_state = ST_INIT;
985 devc->shared_irq = hw_config->always_detect;
986 devc->irq = hw_config->irq;
987 spin_lock_init(&devc->lock);
992 devc->shared_irq = 1;
995 if (!hw_config->always_detect)
997 /* Verify the hardware again */
998 if (!reset_mpu401(devc))
1000 printk(KERN_WARNING "mpu401: Device didn't respond\n");
1004 if (!devc->shared_irq)
1006 if (request_irq(devc->irq, mpuintr, 0, "mpu401",
1009 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
1014 spin_lock_irqsave(&devc->lock,flags);
1015 mpu401_chk_version(m, devc);
1016 if (devc->version == 0)
1017 mpu401_chk_version(m, devc);
1018 spin_unlock_irqrestore(&devc->lock,flags);
1021 if (devc->version != 0)
1022 if (mpu_cmd(m, 0xC5, 0) >= 0) /* Set timebase OK */
1023 if (mpu_cmd(m, 0xE0, 120) >= 0) /* Set tempo OK */
1024 devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent mode */
1027 mpu401_synth_operations[m] = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
1029 if (mpu401_synth_operations[m] == NULL)
1031 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1035 if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
1037 memcpy((char *) mpu401_synth_operations[m],
1038 (char *) &std_midi_synth,
1039 sizeof(struct synth_operations));
1043 memcpy((char *) mpu401_synth_operations[m],
1044 (char *) &mpu401_synth_proto,
1045 sizeof(struct synth_operations));
1048 mpu401_synth_operations[m]->owner = owner;
1050 memcpy((char *) &mpu401_midi_operations[m],
1051 (char *) &mpu401_midi_proto,
1052 sizeof(struct midi_operations));
1054 mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1056 memcpy((char *) &mpu_synth_info[m],
1057 (char *) &mpu_synth_info_proto,
1058 sizeof(struct synth_info));
1062 if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
1064 int ports = (devc->revision & 0x08) ? 32 : 16;
1066 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1067 MPU_CAP_CLS | MPU_CAP_2PORT;
1069 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1070 sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
1077 revision_char = devc->revision ? devc->revision + '@' : ' ';
1078 if ((int) devc->revision > ('Z' - '@'))
1079 revision_char = '+';
1081 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1083 if (hw_config->name)
1084 sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
1086 sprintf(mpu_synth_info[m].name,
1087 "MPU-401 %d.%d%c Midi interface #%d",
1088 (int) (devc->version & 0xf0) >> 4,
1089 devc->version & 0x0f,
1094 strcpy(mpu401_midi_operations[m].info.name,
1095 mpu_synth_info[m].name);
1097 conf_printf(mpu_synth_info[m].name, hw_config);
1099 mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1100 mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1102 if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
1103 hw_config->slots[2] = mpu_timer_init(m);
1105 midi_devs[m] = &mpu401_midi_operations[devc->devno];
1108 midi_devs[m]->owner = owner;
1110 hw_config->slots[1] = m;
1116 free_irq(devc->irq, hw_config);
1118 sound_unload_mididev(m);
1120 release_region(hw_config->io_base, 2);
1124 static int reset_mpu401(struct mpu_config *devc)
1126 unsigned long flags;
1131 * Send the RESET command. Try again if no success at the first time.
1132 * (If the device is in the UART mode, it will not ack the reset cmd).
1137 timeout_limit = devc->initialized ? 30000 : 100000;
1138 devc->initialized = 1;
1140 for (n = 0; n < 2 && !ok; n++)
1142 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1143 ok = output_ready(devc);
1145 write_command(devc, MPU_RESET); /*
1146 * Send MPU-401 RESET Command
1150 * Wait at least 25 msec. This method is not accurate so let's make the
1151 * loop bit longer. Cannot sleep since this is called during boot.
1154 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1156 spin_lock_irqsave(&devc->lock,flags);
1157 if (input_avail(devc))
1158 if (read_data(devc) == MPU_ACK)
1160 spin_unlock_irqrestore(&devc->lock,flags);
1165 devc->m_state = ST_INIT;
1168 devc->last_status = 0;
1169 devc->uart_mode = 0;
1174 static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
1176 if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1178 if ((devc->uart_mode == 0) == (arg == 0))
1179 return; /* Already set */
1180 reset_mpu401(devc); /* This exits the uart mode */
1184 if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
1186 printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1187 devc->uart_mode = 0;
1191 devc->uart_mode = arg;
1195 int probe_mpu401(struct address_info *hw_config, struct resource *ports)
1198 struct mpu_config tmp_devc;
1200 tmp_devc.base = hw_config->io_base;
1201 tmp_devc.irq = hw_config->irq;
1202 tmp_devc.initialized = 0;
1203 tmp_devc.opened = 0;
1204 tmp_devc.osp = hw_config->osp;
1206 if (hw_config->always_detect)
1209 if (inb(hw_config->io_base + 1) == 0xff)
1211 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1212 return 0; /* Just bus float? */
1214 ok = reset_mpu401(&tmp_devc);
1218 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1223 void unload_mpu401(struct address_info *hw_config)
1226 int n=hw_config->slots[1];
1229 release_region(hw_config->io_base, 2);
1230 if (hw_config->always_detect == 0 && hw_config->irq > 0)
1231 free_irq(hw_config->irq, hw_config);
1232 p=mpu401_synth_operations[n];
1233 sound_unload_mididev(n);
1234 sound_unload_timerdev(hw_config->slots[2]);
1239 /*****************************************************
1241 ****************************************************/
1243 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1244 static volatile int curr_tempo, curr_timebase, hw_timebase;
1245 static int max_timebase = 8; /* 8*24=192 ppqn */
1246 static volatile unsigned long next_event_time;
1247 static volatile unsigned long curr_ticks, curr_clocks;
1248 static unsigned long prev_event_time;
1249 static int metronome_mode;
1251 static unsigned long clocks2ticks(unsigned long clocks)
1254 * The MPU-401 supports just a limited set of possible timebase values.
1255 * Since the applications require more choices, the driver has to
1256 * program the HW to do its best and to convert between the HW and
1259 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1262 static void set_timebase(int midi_dev, int val)
1272 hw_val = (hw_val + 12) / 24;
1273 if (hw_val > max_timebase)
1274 hw_val = max_timebase;
1276 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1278 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
1281 hw_timebase = hw_val * 24;
1282 curr_timebase = val;
1286 static void tmr_reset(struct mpu_config *devc)
1288 unsigned long flags;
1290 spin_lock_irqsave(&devc->lock,flags);
1291 next_event_time = (unsigned long) -1;
1292 prev_event_time = 0;
1293 curr_ticks = curr_clocks = 0;
1294 spin_unlock_irqrestore(&devc->lock,flags);
1297 static void set_timer_mode(int midi_dev)
1299 if (timer_mode & TMR_MODE_CLS)
1300 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1301 else if (timer_mode & TMR_MODE_SMPTE)
1302 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1304 if (timer_mode & TMR_INTERNAL)
1306 mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
1310 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1312 mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
1313 mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
1315 else if (timer_mode & TMR_MODE_FSK)
1316 mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
1320 static void stop_metronome(int midi_dev)
1322 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1325 static void setup_metronome(int midi_dev)
1327 int numerator, denominator;
1328 int clks_per_click, num_32nds_per_beat;
1329 int beats_per_measure;
1331 numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1332 denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1333 clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1334 num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1335 beats_per_measure = (numerator * 4) >> denominator;
1337 if (!metronome_mode)
1338 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1341 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1342 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1343 mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without accents */
1347 static int mpu_start_timer(int midi_dev)
1349 struct mpu_config *devc= &dev_conf[midi_dev];
1352 set_timer_mode(midi_dev);
1355 return TIMER_NOT_ARMED; /* Already running */
1357 if (timer_mode & TMR_INTERNAL)
1359 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1361 return TIMER_NOT_ARMED;
1365 mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
1366 mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
1367 mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
1368 mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive messages to PC */
1373 static int mpu_timer_open(int dev, int mode)
1375 int midi_dev = sound_timer_devs[dev]->devlink;
1376 struct mpu_config *devc= &dev_conf[midi_dev];
1383 mpu_cmd(midi_dev, 0xE0, 50);
1384 curr_timebase = hw_timebase = 120;
1385 set_timebase(midi_dev, 120);
1388 set_timer_mode(midi_dev);
1390 mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
1391 mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
1396 static void mpu_timer_close(int dev)
1398 int midi_dev = sound_timer_devs[dev]->devlink;
1400 timer_open = tmr_running = 0;
1401 mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
1402 mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
1403 mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to host */
1404 stop_metronome(midi_dev);
1407 static int mpu_timer_event(int dev, unsigned char *event)
1409 unsigned char command = event[1];
1410 unsigned long parm = *(unsigned int *) &event[4];
1411 int midi_dev = sound_timer_devs[dev]->devlink;
1416 parm += prev_event_time;
1422 if (parm <= curr_ticks) /* It's the time */
1423 return TIMER_NOT_ARMED;
1425 next_event_time = prev_event_time = time;
1434 return mpu_start_timer(midi_dev);
1437 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1438 stop_metronome(midi_dev);
1445 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1446 setup_metronome(midi_dev);
1457 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1458 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1464 seq_copy_to_input(event, 8);
1468 if (metronome_mode) /* Metronome enabled */
1470 metronome_mode = parm;
1471 setup_metronome(midi_dev);
1477 return TIMER_NOT_ARMED;
1480 static unsigned long mpu_timer_get_time(int dev)
1488 static int mpu_timer_ioctl(int dev, unsigned int command, void __user *arg)
1490 int midi_dev = sound_timer_devs[dev]->devlink;
1491 int __user *p = (int __user *)arg;
1495 case SNDCTL_TMR_SOURCE:
1499 if (get_user(parm, p))
1507 if (timer_mode & TMR_MODE_CLS)
1508 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1509 else if (timer_mode & TMR_MODE_SMPTE)
1510 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1512 if (put_user(timer_mode, p))
1518 case SNDCTL_TMR_START:
1519 mpu_start_timer(midi_dev);
1522 case SNDCTL_TMR_STOP:
1524 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1525 stop_metronome(midi_dev);
1528 case SNDCTL_TMR_CONTINUE:
1532 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1535 case SNDCTL_TMR_TIMEBASE:
1538 if (get_user(val, p))
1541 set_timebase(midi_dev, val);
1542 if (put_user(curr_timebase, p))
1544 return curr_timebase;
1548 case SNDCTL_TMR_TEMPO:
1553 if (get_user(val, p))
1562 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1564 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1569 if (put_user(curr_tempo, p))
1575 case SNDCTL_SEQ_CTRLRATE:
1578 if (get_user(val, p))
1581 if (val != 0) /* Can't change */
1583 val = ((curr_tempo * curr_timebase) + 30)/60;
1584 if (put_user(val, p))
1590 case SNDCTL_SEQ_GETTIME:
1591 if (put_user(curr_ticks, p))
1595 case SNDCTL_TMR_METRONOME:
1596 if (get_user(metronome_mode, p))
1598 setup_metronome(midi_dev);
1606 static void mpu_timer_arm(int dev, long time)
1609 time = curr_ticks + 1;
1610 else if (time <= curr_ticks) /* It's the time */
1612 next_event_time = prev_event_time = time;
1616 static struct sound_timer_operations mpu_timer =
1618 .owner = THIS_MODULE,
1619 .info = {"MPU-401 Timer", 0},
1620 .priority = 10, /* Priority */
1621 .devlink = 0, /* Local device link */
1622 .open = mpu_timer_open,
1623 .close = mpu_timer_close,
1624 .event = mpu_timer_event,
1625 .get_time = mpu_timer_get_time,
1626 .ioctl = mpu_timer_ioctl,
1627 .arm_timer = mpu_timer_arm
1630 static void mpu_timer_interrupt(void)
1639 curr_ticks = clocks2ticks(curr_clocks);
1641 if (curr_ticks >= next_event_time)
1643 next_event_time = (unsigned long) -1;
1648 static void timer_ext_event(struct mpu_config *devc, int event, int parm)
1650 int midi_dev = devc->devno;
1652 if (!devc->timer_flag)
1658 printk("<MIDI clk>");
1662 printk("Ext MIDI start\n");
1665 if (timer_mode & TMR_EXTERNAL)
1668 setup_metronome(midi_dev);
1669 next_event_time = 0;
1670 STORE(SEQ_START_TIMER());
1676 printk("Ext MIDI stop\n");
1677 if (timer_mode & TMR_EXTERNAL)
1680 stop_metronome(midi_dev);
1681 STORE(SEQ_STOP_TIMER());
1686 printk("Ext MIDI continue\n");
1687 if (timer_mode & TMR_EXTERNAL)
1690 setup_metronome(midi_dev);
1691 STORE(SEQ_CONTINUE_TIMER());
1696 printk("Songpos: %d\n", parm);
1697 if (timer_mode & TMR_EXTERNAL)
1699 STORE(SEQ_SONGPOS(parm));
1705 static int mpu_timer_init(int midi_dev)
1707 struct mpu_config *devc;
1710 devc = &dev_conf[midi_dev];
1712 if (timer_initialized)
1713 return -1; /* There is already a similar timer */
1715 timer_initialized = 1;
1717 mpu_timer.devlink = midi_dev;
1718 dev_conf[midi_dev].timer_flag = 1;
1720 n = sound_alloc_timerdev();
1723 sound_timer_devs[n] = &mpu_timer;
1725 if (devc->version < 0x20) /* Original MPU-401 */
1726 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1730 * The version number 2.0 is used (at least) by the
1731 * MusicQuest cards and the Roland Super-MPU.
1733 * MusicQuest has given a special meaning to the bits of the
1734 * revision number. The Super-MPU returns 0.
1738 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1740 if (devc->revision & 0x02)
1741 timer_caps |= TMR_MODE_CLS;
1744 if (devc->revision & 0x40)
1745 max_timebase = 10; /* Has the 216 and 240 ppqn modes */
1748 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1753 EXPORT_SYMBOL(probe_mpu401);
1754 EXPORT_SYMBOL(attach_mpu401);
1755 EXPORT_SYMBOL(unload_mpu401);
1757 static struct address_info cfg;
1760 static int irq = -1;
1762 module_param(irq, int, 0);
1763 module_param(io, int, 0);
1765 static int __init init_mpu401(void)
1768 /* Can be loaded either for module use or to provide functions
1770 if (io != -1 && irq != -1) {
1771 struct resource *ports;
1774 ports = request_region(io, 2, "mpu401");
1777 if (probe_mpu401(&cfg, ports) == 0) {
1778 release_region(io, 2);
1781 if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
1788 static void __exit cleanup_mpu401(void)
1790 if (io != -1 && irq != -1) {
1791 /* Check for use by, for example, sscape driver */
1792 unload_mpu401(&cfg);
1796 module_init(init_mpu401);
1797 module_exit(cleanup_mpu401);
1800 static int __init setup_mpu401(char *str)
1805 str = get_options(str, ARRAY_SIZE(ints), ints);
1813 __setup("mpu401=", setup_mpu401);
1815 MODULE_LICENSE("GPL");