21 #include <linux/module.h>
22 #include <linux/slab.h>
26 #define USE_SEQ_MACROS
27 #define USE_SIMPLE_MACROS
54 #define MPU_CAP_INTLG 0x10000000
55 #define MPU_CAP_SYNC 0x00000010
56 #define MPU_CAP_FSK 0x00000020
57 #define MPU_CAP_CLS 0x00000040
58 #define MPU_CAP_SMPTE 0x00000080
59 #define MPU_CAP_2PORT 0x00000001
63 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
64 {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
77 #define DATAPORT(base) (base)
78 #define COMDPORT(base) (base+1)
79 #define STATPORT(base) (base+1)
82 static void mpu401_close(
int dev);
84 static inline int mpu401_status(
struct mpu_config *devc)
89 #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
90 #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
92 static inline void write_command(
struct mpu_config *devc,
unsigned char cmd)
97 static inline int read_data(
struct mpu_config *devc)
102 static inline void write_data(
struct mpu_config *devc,
unsigned char byte)
107 #define OUTPUT_READY 0x40
108 #define INPUT_AVAIL 0x80
110 #define MPU_RESET 0xFF
111 #define UART_MODE_ON 0x3F
115 static int n_mpu_devs;
117 static int reset_mpu401(
struct mpu_config *devc);
120 static int mpu_timer_init(
int midi_dev);
121 static void mpu_timer_interrupt(
void);
124 static struct synth_info mpu_synth_info_proto = {
125 "MPU-401 MIDI interface",
142 #define ST_DATABYTE 2
144 #define ST_SYSMSG 100
147 #define ST_SONGSEL 103
148 #define ST_SONGPOS 104
150 static unsigned char len_tab[] =
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)
193 mpu_timer_interrupt();
207 printk(
"<Trk data rq #%d>", midic & 0x0f);
225 printk(
"<MPU: Unknown event %02x> ", midic);
231 int msg = ((
int) (midic & 0xf0) >> 4);
244 devc->
m_buf[1] = midic;
272 printk(
"Unknown MPU mark %02x\n", midic);
283 devc->
m_buf[0] = midic;
355 printk(
"unknown MIDI sysmsg %0x\n", midic);
362 printk(
"MTC frame %x02\n", midic);
378 if (devc->
m_ptr == 2)
383 ((devc->
m_buf[1] & 0x7f) << 7) |
384 (devc->
m_buf[0] & 0x7f));
391 if ((--devc->
m_left) <= 0)
406 static void mpu401_input_loop(
struct mpu_config *devc)
415 spin_unlock_irqrestore(&devc->
lock,flags);
424 unsigned char c = read_data(devc);
428 mpu_input_scanner(devc, c);
442 devc = &dev_conf[
dev];
448 mpu401_input_loop(devc);
458 static int mpu401_open(
int dev,
int mode,
459 void (*
input) (
int dev,
unsigned char data),
460 void (*output) (
int dev)
470 devc = &dev_conf[
dev];
483 if (mpu401_status(devc) == 0xff)
491 if ( (coprocessor = midi_devs[dev]->coproc) !=
NULL )
493 if (!try_module_get(coprocessor->
owner)) {
506 set_uart_mode(dev, devc, 1);
510 mpu401_input_loop(devc);
518 static void mpu401_close(
int dev)
523 devc = &dev_conf[
dev];
534 module_put(coprocessor->
owner);
539 static int mpu401_out(
int dev,
unsigned char midi_byte)
546 devc = &dev_conf[
dev];
553 for (timeout = 30000; timeout > 0 && !
output_ready(devc); timeout--);
559 spin_unlock_irqrestore(&devc->
lock,flags);
562 write_data(devc, midi_byte);
563 spin_unlock_irqrestore(&devc->
lock,flags);
574 devc = &dev_conf[
dev];
587 mpu401_input_loop(devc);
605 spin_unlock_irqrestore(&devc->
lock,flags);
608 write_command(devc, cmd->
cmd);
611 for (timeout = 50000; timeout > 0 && !ok; timeout--)
617 if (mpu_input_scanner(devc, read_data(devc)) ==
MPU_ACK)
623 if (read_data(devc) ==
MPU_ACK)
630 spin_unlock_irqrestore(&devc->
lock,flags);
635 for (i = 0; i < cmd->
nr_args; i++)
637 for (timeout = 3000; timeout > 0 && !
output_ready(devc); timeout--);
639 if (!mpu401_out(dev, cmd->
data[i]))
641 spin_unlock_irqrestore(&devc->
lock,flags);
655 for (timeout = 5000; timeout > 0 && !ok; timeout--)
658 cmd->
data[
i] = read_data(devc);
663 spin_unlock_irqrestore(&devc->
lock,flags);
668 spin_unlock_irqrestore(&devc->
lock,flags);
672 static int mpu_cmd(
int dev,
int cmd,
int data)
678 rec.
cmd = cmd & 0xff;
679 rec.
nr_args = ((cmd & 0xf0) == 0xE0);
681 rec.
data[0] = data & 0xff;
683 if ((ret = mpu401_command(dev, &rec)) < 0)
685 return (
unsigned char) rec.
data[0];
688 static int mpu401_prefix_cmd(
int dev,
unsigned char status)
697 if (mpu_cmd(dev, 0xD0, 0) < 0)
704 if (mpu_cmd(dev, 0xDF, 0) < 0)
713 static int mpu401_start_read(
int dev)
718 static int mpu401_end_read(
int dev)
723 static int mpu401_ioctl(
int dev,
unsigned cmd,
void __user *
arg)
729 devc = &dev_conf[
dev];
737 if (
get_user(val, (
int __user *)arg))
739 set_uart_mode(dev, devc, !val);
745 if ((ret = mpu401_command(dev, &rec)) < 0)
756 static void mpu401_kick(
int dev)
760 static int mpu401_buffer_status(
int dev)
767 static int mpu_synth_ioctl(
int dev,
unsigned int cmd,
void __user *arg)
774 if (midi_dev < 0 || midi_dev >=
num_midis || midi_devs[midi_dev] ==
NULL)
777 devc = &dev_conf[midi_dev];
796 static int mpu_synth_open(
int dev,
int mode)
804 if (midi_dev < 0 || midi_dev >
num_midis || midi_devs[midi_dev] ==
NULL)
807 devc = &dev_conf[midi_dev];
818 if (mpu401_status(devc) == 0xff)
832 coprocessor =
midi_devs[midi_dev]->coproc;
834 if (!try_module_get(coprocessor->
owner))
848 mpu_cmd(midi_dev, 0x8B, 0);
849 mpu_cmd(midi_dev, 0x34, 0);
850 mpu_cmd(midi_dev, 0x87, 0);
855 static void mpu_synth_close(
int dev)
863 devc = &dev_conf[midi_dev];
864 mpu_cmd(midi_dev, 0x15, 0);
865 mpu_cmd(midi_dev, 0x8a, 0);
869 coprocessor =
midi_devs[midi_dev]->coproc;
872 module_put(coprocessor->
owner);
878 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
879 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
890 .open = mpu_synth_open,
891 .close = mpu_synth_close,
892 .ioctl = mpu_synth_ioctl,
915 .close = mpu401_close,
916 .ioctl = mpu401_ioctl,
917 .outputc = mpu401_out,
918 .start_read = mpu401_start_read,
919 .end_read = mpu401_end_read,
921 .buffer_status = mpu401_buffer_status,
922 .prefix_cmd = mpu401_prefix_cmd
927 static void mpu401_chk_version(
int n,
struct mpu_config *devc)
933 tmp = mpu_cmd(n, 0xAC, 0);
936 if ((tmp & 0xf0) > 0x20)
940 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0) {
955 hw_config->
slots[1] = -1;
965 devc->
osp = hw_config->
osp;
966 devc->
irq = hw_config->
irq;
977 devc->
irq = hw_config->
irq;
989 if (!reset_mpu401(devc))
1006 mpu401_chk_version(m, devc);
1008 mpu401_chk_version(m, devc);
1009 spin_unlock_irqrestore(&devc->
lock, flags);
1013 if (mpu_cmd(m, 0xC5, 0) >= 0)
1014 if (mpu_cmd(m, 0xE0, 120) >= 0)
1020 if (mpu401_synth_operations[m] ==
NULL)
1028 memcpy((
char *) mpu401_synth_operations[m],
1029 (
char *) &std_midi_synth,
1034 memcpy((
char *) mpu401_synth_operations[m],
1035 (
char *) &mpu401_synth_proto,
1041 memcpy((
char *) &mpu401_midi_operations[m],
1042 (
char *) &mpu401_midi_proto,
1045 mpu401_midi_operations[
m].converter = mpu401_synth_operations[
m];
1047 memcpy((
char *) &mpu_synth_info[m],
1048 (
char *) &mpu_synth_info_proto,
1060 revision_char = (devc->
revision == 0x7f) ?
'M' :
' ';
1061 sprintf(mpu_synth_info[m].
name,
"MQX-%d%c MIDI Interface #%d",
1069 if ((
int) devc->
revision > (
'Z' -
'@'))
1070 revision_char =
'+';
1074 if (hw_config->
name)
1077 sprintf(mpu_synth_info[m].name,
1078 "MPU-401 %d.%d%c MIDI #%d",
1079 (
int) (devc->
version & 0xf0) >> 4,
1086 mpu_synth_info[m].name);
1091 mpu401_synth_operations[devc->
devno]->
info = &mpu_synth_info[devc->
devno];
1094 hw_config->
slots[2] = mpu_timer_init(m);
1115 static int reset_mpu401(
struct mpu_config *devc)
1117 unsigned long flags;
1128 timeout_limit = devc->
initialized ? 30000 : 100000;
1131 for (n = 0; n < 2 && !ok; n++)
1133 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1145 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1149 if (read_data(devc) ==
MPU_ACK)
1151 spin_unlock_irqrestore(&devc->
lock,flags);
1165 static void set_uart_mode(
int dev,
struct mpu_config *devc,
int arg)
1169 if ((devc->
uart_mode == 0) == (arg == 0))
1192 tmp_devc.
irq = hw_config->
irq;
1195 tmp_devc.
osp = hw_config->
osp;
1205 ok = reset_mpu401(&tmp_devc);
1217 int n=hw_config->
slots[1];
1223 p=mpu401_synth_operations[
n];
1234 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1235 static volatile int curr_tempo, curr_timebase, hw_timebase;
1236 static int max_timebase = 8;
1237 static volatile unsigned long next_event_time;
1238 static volatile unsigned long curr_ticks, curr_clocks;
1239 static unsigned long prev_event_time;
1240 static int metronome_mode;
1242 static unsigned long clocks2ticks(
unsigned long clocks)
1250 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1253 static void set_timebase(
int midi_dev,
int val)
1263 hw_val = (hw_val + 12) / 24;
1264 if (hw_val > max_timebase)
1265 hw_val = max_timebase;
1267 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1272 hw_timebase = hw_val * 24;
1273 curr_timebase =
val;
1277 static void tmr_reset(
struct mpu_config *devc)
1279 unsigned long flags;
1282 next_event_time = (
unsigned long) -1;
1283 prev_event_time = 0;
1284 curr_ticks = curr_clocks = 0;
1285 spin_unlock_irqrestore(&devc->
lock,flags);
1288 static void set_timer_mode(
int midi_dev)
1291 mpu_cmd(midi_dev, 0x3c, 0);
1293 mpu_cmd(midi_dev, 0x3d, 0);
1297 mpu_cmd(midi_dev, 0x80, 0);
1303 mpu_cmd(midi_dev, 0x82, 0);
1304 mpu_cmd(midi_dev, 0x91, 0);
1307 mpu_cmd(midi_dev, 0x81, 0);
1311 static void stop_metronome(
int midi_dev)
1313 mpu_cmd(midi_dev, 0x84, 0);
1316 static void setup_metronome(
int midi_dev)
1318 int numerator, denominator;
1319 int clks_per_click, num_32nds_per_beat;
1320 int beats_per_measure;
1322 numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1323 denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1324 clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1325 num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1326 beats_per_measure = (numerator * 4) >> denominator;
1328 if (!metronome_mode)
1329 mpu_cmd(midi_dev, 0x84, 0);
1332 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1333 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1334 mpu_cmd(midi_dev, 0x83, 0);
1338 static int mpu_start_timer(
int midi_dev)
1340 struct mpu_config *devc= &dev_conf[midi_dev];
1343 set_timer_mode(midi_dev);
1350 mpu_cmd(midi_dev, 0x02, 0);
1356 mpu_cmd(midi_dev, 0x35, 0);
1357 mpu_cmd(midi_dev, 0x38, 0);
1358 mpu_cmd(midi_dev, 0x39, 0);
1359 mpu_cmd(midi_dev, 0x97, 0);
1364 static int mpu_timer_open(
int dev,
int mode)
1367 struct mpu_config *devc= &dev_conf[midi_dev];
1374 mpu_cmd(midi_dev, 0xE0, 50);
1375 curr_timebase = hw_timebase = 120;
1376 set_timebase(midi_dev, 120);
1379 set_timer_mode(midi_dev);
1381 mpu_cmd(midi_dev, 0xe7, 0x04);
1382 mpu_cmd(midi_dev, 0x95, 0);
1387 static void mpu_timer_close(
int dev)
1391 timer_open = tmr_running = 0;
1392 mpu_cmd(midi_dev, 0x15, 0);
1393 mpu_cmd(midi_dev, 0x94, 0);
1394 mpu_cmd(midi_dev, 0x8c, 0);
1395 stop_metronome(midi_dev);
1398 static int mpu_timer_event(
int dev,
unsigned char *
event)
1400 unsigned char command =
event[1];
1401 unsigned long parm = *(
unsigned int *) &event[4];
1407 parm += prev_event_time;
1413 if (parm <= curr_ticks)
1416 next_event_time = prev_event_time =
time;
1425 return mpu_start_timer(midi_dev);
1428 mpu_cmd(midi_dev, 0x01, 0);
1429 stop_metronome(midi_dev);
1436 mpu_cmd(midi_dev, 0x03, 0);
1437 setup_metronome(midi_dev);
1448 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1461 metronome_mode =
parm;
1462 setup_metronome(midi_dev);
1471 static unsigned long mpu_timer_get_time(
int dev)
1479 static int mpu_timer_ioctl(
int dev,
unsigned int command,
void __user *arg)
1499 mpu_cmd(midi_dev, 0x3c, 0);
1501 mpu_cmd(midi_dev, 0x3d, 0);
1510 mpu_start_timer(midi_dev);
1515 mpu_cmd(midi_dev, 0x01, 0);
1516 stop_metronome(midi_dev);
1523 mpu_cmd(midi_dev, 0x03, 0);
1532 set_timebase(midi_dev, val);
1535 return curr_timebase;
1553 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1574 val = ((curr_tempo * curr_timebase) + 30)/60;
1589 setup_metronome(midi_dev);
1597 static void mpu_timer_arm(
int dev,
long time)
1600 time = curr_ticks + 1;
1601 else if (time <= curr_ticks)
1603 next_event_time = prev_event_time =
time;
1610 .info = {
"MPU-401 Timer", 0},
1613 .open = mpu_timer_open,
1614 .close = mpu_timer_close,
1615 .event = mpu_timer_event,
1616 .get_time = mpu_timer_get_time,
1617 .ioctl = mpu_timer_ioctl,
1618 .arm_timer = mpu_timer_arm
1621 static void mpu_timer_interrupt(
void)
1630 curr_ticks = clocks2ticks(curr_clocks);
1632 if (curr_ticks >= next_event_time)
1634 next_event_time = (
unsigned long) -1;
1639 static void timer_ext_event(
struct mpu_config *devc,
int event,
int parm)
1641 int midi_dev = devc->
devno;
1653 printk(
"Ext MIDI start\n");
1659 setup_metronome(midi_dev);
1660 next_event_time = 0;
1667 printk(
"Ext MIDI stop\n");
1671 stop_metronome(midi_dev);
1677 printk(
"Ext MIDI continue\n");
1681 setup_metronome(midi_dev);
1687 printk(
"Songpos: %d\n", parm);
1696 static int mpu_timer_init(
int midi_dev)
1701 devc = &dev_conf[midi_dev];
1703 if (timer_initialized)
1706 timer_initialized = 1;
1709 dev_conf[midi_dev].timer_flag = 1;
1751 static int irq = -1;
1756 static int __init init_mpu401(
void)
1761 if (io != -1 && irq != -1) {
1779 static void __exit cleanup_mpu401(
void)
1781 if (io != -1 && irq != -1) {
1791 static int __init setup_mpu401(
char *
str)
1804 __setup(
"mpu401=", setup_mpu401);