4 * High level midi sequencer manager for dumb MIDI interfaces.
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 * Andrew Veliath : fixed running status in MIDI input state machine
17 #include <linux/config.h>
19 #define USE_SEQ_MACROS
20 #define USE_SIMPLE_MACROS
22 #include "sound_config.h"
26 #define _MIDI_SYNTH_C_
28 #include "midi_synth.h"
30 static int midi2synth
[MAX_MIDI_DEV
];
31 static int sysex_state
[MAX_MIDI_DEV
] =
33 static unsigned char prev_out_status
[MAX_MIDI_DEV
];
35 #ifndef CONFIG_SEQUENCER
41 unsigned char obuf[8]; \
43 seq_input_event(obuf, len); \
49 #define _SEQ_ADVBUF(x) len=x
52 do_midi_msg(int synthno
, unsigned char *msg
, int mlen
)
54 switch (msg
[0] & 0xf0)
59 STORE(SEQ_START_NOTE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
65 STORE(SEQ_STOP_NOTE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
69 STORE(SEQ_KEY_PRESSURE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
73 STORE(SEQ_CONTROL(synthno
, msg
[0] & 0x0f,
78 STORE(SEQ_SET_PATCH(synthno
, msg
[0] & 0x0f, msg
[1]));
82 STORE(SEQ_CHN_PRESSURE(synthno
, msg
[0] & 0x0f, msg
[1]));
86 STORE(SEQ_BENDER(synthno
, msg
[0] & 0x0f,
87 (msg
[1] & 0x7f) | ((msg
[2] & 0x7f) << 7)));
91 /* printk( "MPU: Unknown midi channel message %02x\n", msg[0]); */
97 midi_outc(int midi_dev
, int data
)
101 for (timeout
= 0; timeout
< 3200; timeout
++)
102 if (midi_devs
[midi_dev
]->outputc(midi_dev
, (unsigned char) (data
& 0xff)))
107 prev_out_status
[midi_dev
] =
108 (unsigned char) (data
& 0xff); /*
109 * Store for running status
116 * Sorry! No space on buffers.
118 printk("Midi send timed out\n");
122 prefix_cmd(int midi_dev
, unsigned char status
)
124 if ((char *) midi_devs
[midi_dev
]->prefix_cmd
== NULL
)
127 return midi_devs
[midi_dev
]->prefix_cmd(midi_dev
, status
);
131 midi_synth_input(int orig_dev
, unsigned char data
)
134 struct midi_input_info
*inc
;
136 static unsigned char len_tab
[] = /* # of data bytes following a status
149 if (orig_dev
< 0 || orig_dev
> num_midis
|| midi_devs
[orig_dev
] == NULL
)
152 if (data
== 0xfe) /* Ignore active sensing */
155 dev
= midi2synth
[orig_dev
];
156 inc
= &midi_devs
[orig_dev
]->in_info
;
158 switch (inc
->m_state
)
161 if (data
& 0x80) /* MIDI status byte */
163 if ((data
& 0xf0) == 0xf0) /* Common message */
167 case 0xf0: /* Sysex */
168 inc
->m_state
= MST_SYSEX
;
171 case 0xf1: /* MTC quarter frame */
172 case 0xf3: /* Song select */
173 inc
->m_state
= MST_DATA
;
176 inc
->m_buf
[0] = data
;
179 case 0xf2: /* Song position pointer */
180 inc
->m_state
= MST_DATA
;
183 inc
->m_buf
[0] = data
;
187 inc
->m_buf
[0] = data
;
189 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
195 inc
->m_state
= MST_DATA
;
197 inc
->m_left
= len_tab
[(data
>> 4) - 8];
198 inc
->m_buf
[0] = inc
->m_prev_status
= data
;
200 } else if (inc
->m_prev_status
& 0x80) {
201 /* Data byte (use running status) */
203 inc
->m_buf
[1] = data
;
204 inc
->m_buf
[0] = inc
->m_prev_status
;
205 inc
->m_left
= len_tab
[(inc
->m_buf
[0] >> 4) - 8] - 1;
207 inc
->m_state
= MST_DATA
; /* Not done yet */
209 inc
->m_state
= MST_INIT
;
210 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
214 break; /* MST_INIT */
217 inc
->m_buf
[inc
->m_ptr
++] = data
;
218 if (--inc
->m_left
<= 0)
220 inc
->m_state
= MST_INIT
;
221 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
224 break; /* MST_DATA */
227 if (data
== 0xf7) /* Sysex end */
229 inc
->m_state
= MST_INIT
;
233 break; /* MST_SYSEX */
236 printk("MIDI%d: Unexpected state %d (%02x)\n", orig_dev
, inc
->m_state
, (int) data
);
237 inc
->m_state
= MST_INIT
;
244 int orig_dev
= synth_devs
[dev
]->midi_dev
;
247 if (!sysex_state
[dev
])
250 sysex_state
[dev
] = 0;
252 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, 0xf7) &&
256 sysex_state
[dev
] = 0;
260 midi_synth_output(int dev
)
267 int midi_synth_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
270 * int orig_dev = synth_devs[dev]->midi_dev;
275 case SNDCTL_SYNTH_INFO
:
276 if (__copy_to_user(arg
, synth_devs
[dev
]->info
, sizeof(struct synth_info
)))
280 case SNDCTL_SYNTH_MEMAVL
:
289 midi_synth_kill_note(int dev
, int channel
, int note
, int velocity
)
291 int orig_dev
= synth_devs
[dev
]->midi_dev
;
294 if (note
< 0 || note
> 127)
296 if (channel
< 0 || channel
> 15)
305 msg
= prev_out_status
[orig_dev
] & 0xf0;
306 chn
= prev_out_status
[orig_dev
] & 0x0f;
308 if (chn
== channel
&& ((msg
== 0x90 && velocity
== 64) || msg
== 0x80))
312 if (!prefix_cmd(orig_dev
, note
))
315 midi_outc(orig_dev
, note
);
318 * Running status = Note on
320 midi_outc(orig_dev
, 0); /*
321 * Note on with velocity 0 == note
325 midi_outc(orig_dev
, velocity
);
330 if (!prefix_cmd(orig_dev
, 0x90 | (channel
& 0x0f)))
332 midi_outc(orig_dev
, 0x90 | (channel
& 0x0f)); /*
335 midi_outc(orig_dev
, note
);
336 midi_outc(orig_dev
, 0); /*
341 if (!prefix_cmd(orig_dev
, 0x80 | (channel
& 0x0f)))
343 midi_outc(orig_dev
, 0x80 | (channel
& 0x0f)); /*
346 midi_outc(orig_dev
, note
);
347 midi_outc(orig_dev
, velocity
);
355 midi_synth_set_instr(int dev
, int channel
, int instr_no
)
357 int orig_dev
= synth_devs
[dev
]->midi_dev
;
359 if (instr_no
< 0 || instr_no
> 127)
361 if (channel
< 0 || channel
> 15)
366 if (!prefix_cmd(orig_dev
, 0xc0 | (channel
& 0x0f)))
368 midi_outc(orig_dev
, 0xc0 | (channel
& 0x0f)); /*
371 midi_outc(orig_dev
, instr_no
);
377 midi_synth_start_note(int dev
, int channel
, int note
, int velocity
)
379 int orig_dev
= synth_devs
[dev
]->midi_dev
;
382 if (note
< 0 || note
> 127)
384 if (channel
< 0 || channel
> 15)
393 msg
= prev_out_status
[orig_dev
] & 0xf0;
394 chn
= prev_out_status
[orig_dev
] & 0x0f;
396 if (chn
== channel
&& msg
== 0x90)
400 if (!prefix_cmd(orig_dev
, note
))
402 midi_outc(orig_dev
, note
);
403 midi_outc(orig_dev
, velocity
);
406 if (!prefix_cmd(orig_dev
, 0x90 | (channel
& 0x0f)))
408 midi_outc(orig_dev
, 0x90 | (channel
& 0x0f)); /*
411 midi_outc(orig_dev
, note
);
412 midi_outc(orig_dev
, velocity
);
418 midi_synth_reset(int dev
)
425 midi_synth_open(int dev
, int mode
)
427 int orig_dev
= synth_devs
[dev
]->midi_dev
;
430 struct midi_input_info
*inc
;
432 if (orig_dev
< 0 || orig_dev
> num_midis
|| midi_devs
[orig_dev
] == NULL
)
435 midi2synth
[orig_dev
] = dev
;
436 sysex_state
[dev
] = 0;
437 prev_out_status
[orig_dev
] = 0;
439 if ((err
= midi_devs
[orig_dev
]->open(orig_dev
, mode
,
440 midi_synth_input
, midi_synth_output
)) < 0)
445 inc
= &midi_devs
[orig_dev
]->in_info
;
450 inc
->m_state
= MST_INIT
;
453 inc
->m_prev_status
= 0x00;
454 restore_flags(flags
);
460 midi_synth_close(int dev
)
462 int orig_dev
= synth_devs
[dev
]->midi_dev
;
467 * Shut up the synths by sending just single active sensing message.
469 midi_devs
[orig_dev
]->outputc(orig_dev
, 0xfe);
471 midi_devs
[orig_dev
]->close(orig_dev
);
478 midi_synth_hw_control(int dev
, unsigned char *event
)
483 midi_synth_load_patch(int dev
, int format
, const char *addr
,
484 int offs
, int count
, int pmgr_flag
)
486 int orig_dev
= synth_devs
[dev
]->midi_dev
;
488 struct sysex_info sysex
;
490 unsigned long left
, src_offs
, eox_seen
= 0;
492 int hdr_size
= (unsigned long) &sysex
.data
[0] - (unsigned long) &sysex
;
496 if (!prefix_cmd(orig_dev
, 0xf0))
499 if (format
!= SYSEX_PATCH
)
501 /* printk("MIDI Error: Invalid patch format (key) 0x%x\n", format);*/
504 if (count
< hdr_size
)
506 /* printk("MIDI Error: Patch header too short\n");*/
512 * Copy the header from user space but ignore the first bytes which have
513 * been transferred already.
516 if(copy_from_user(&((char *) &sysex
)[offs
], &(addr
)[offs
], hdr_size
- offs
))
519 if (count
< sysex
.len
)
521 /* printk(KERN_WARNING "MIDI Warning: Sysex record too short (%d<%d)\n", count, (int) sysex.len);*/
527 for (i
= 0; i
< left
&& !signal_pending(current
); i
++)
531 get_user(*(unsigned char *) &data
, (unsigned char *) &((addr
)[hdr_size
+ i
]));
533 eox_seen
= (i
> 0 && data
& 0x80); /* End of sysex */
535 if (eox_seen
&& data
!= 0xf7)
542 printk(KERN_WARNING
"midi_synth: Sysex start missing\n");
546 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, (unsigned char) (data
& 0xff)) &&
547 !signal_pending(current
))
550 if (!first_byte
&& data
& 0x80)
556 midi_outc(orig_dev
, 0xf7);
560 void midi_synth_panning(int dev
, int channel
, int pressure
)
564 void midi_synth_aftertouch(int dev
, int channel
, int pressure
)
566 int orig_dev
= synth_devs
[dev
]->midi_dev
;
569 if (pressure
< 0 || pressure
> 127)
571 if (channel
< 0 || channel
> 15)
576 msg
= prev_out_status
[orig_dev
] & 0xf0;
577 chn
= prev_out_status
[orig_dev
] & 0x0f;
579 if (msg
!= 0xd0 || chn
!= channel
) /*
580 * Test for running status
583 if (!prefix_cmd(orig_dev
, 0xd0 | (channel
& 0x0f)))
585 midi_outc(orig_dev
, 0xd0 | (channel
& 0x0f)); /*
588 } else if (!prefix_cmd(orig_dev
, pressure
))
591 midi_outc(orig_dev
, pressure
);
595 midi_synth_controller(int dev
, int channel
, int ctrl_num
, int value
)
597 int orig_dev
= synth_devs
[dev
]->midi_dev
;
600 if (ctrl_num
< 0 || ctrl_num
> 127)
602 if (channel
< 0 || channel
> 15)
607 msg
= prev_out_status
[orig_dev
] & 0xf0;
608 chn
= prev_out_status
[orig_dev
] & 0x0f;
610 if (msg
!= 0xb0 || chn
!= channel
)
612 if (!prefix_cmd(orig_dev
, 0xb0 | (channel
& 0x0f)))
614 midi_outc(orig_dev
, 0xb0 | (channel
& 0x0f));
615 } else if (!prefix_cmd(orig_dev
, ctrl_num
))
618 midi_outc(orig_dev
, ctrl_num
);
619 midi_outc(orig_dev
, value
& 0x7f);
623 midi_synth_bender(int dev
, int channel
, int value
)
625 int orig_dev
= synth_devs
[dev
]->midi_dev
;
628 if (channel
< 0 || channel
> 15)
631 if (value
< 0 || value
> 16383)
636 msg
= prev_out_status
[orig_dev
] & 0xf0;
637 prev_chn
= prev_out_status
[orig_dev
] & 0x0f;
639 if (msg
!= 0xd0 || prev_chn
!= channel
) /*
640 * Test for running status
643 if (!prefix_cmd(orig_dev
, 0xe0 | (channel
& 0x0f)))
645 midi_outc(orig_dev
, 0xe0 | (channel
& 0x0f));
646 } else if (!prefix_cmd(orig_dev
, value
& 0x7f))
649 midi_outc(orig_dev
, value
& 0x7f);
650 midi_outc(orig_dev
, (value
>> 7) & 0x7f);
654 midi_synth_setup_voice(int dev
, int voice
, int channel
)
659 midi_synth_send_sysex(int dev
, unsigned char *bytes
, int len
)
661 int orig_dev
= synth_devs
[dev
]->midi_dev
;
664 for (i
= 0; i
< len
; i
++)
668 case 0xf0: /* Start sysex */
669 if (!prefix_cmd(orig_dev
, 0xf0))
671 sysex_state
[dev
] = 1;
674 case 0xf7: /* End sysex */
675 if (!sysex_state
[dev
]) /* Orphan sysex end */
677 sysex_state
[dev
] = 0;
681 if (!sysex_state
[dev
])
684 if (bytes
[i
] & 0x80) /* Error. Another message before sysex end */
686 bytes
[i
] = 0xf7; /* Sysex end */
687 sysex_state
[dev
] = 0;
691 if (!midi_devs
[orig_dev
]->outputc(orig_dev
, bytes
[i
]))
694 * Hardware level buffer is full. Abort the sysex message.
700 sysex_state
[dev
] = 0;
702 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, bytes
[i
]) &&
706 if (!sysex_state
[dev
])