]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - sound/oss/midi_synth.c
2 * sound/oss/midi_synth.c
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 #define USE_SEQ_MACROS
18 #define USE_SIMPLE_MACROS
20 #include "sound_config.h"
22 #define _MIDI_SYNTH_C_
24 #include "midi_synth.h"
26 static int midi2synth
[MAX_MIDI_DEV
];
27 static int sysex_state
[MAX_MIDI_DEV
] =
29 static unsigned char prev_out_status
[MAX_MIDI_DEV
];
34 unsigned char obuf[8]; \
36 seq_input_event(obuf, len); \
41 #define _SEQ_ADVBUF(x) len=x
44 do_midi_msg(int synthno
, unsigned char *msg
, int mlen
)
46 switch (msg
[0] & 0xf0)
51 STORE(SEQ_START_NOTE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
57 STORE(SEQ_STOP_NOTE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
61 STORE(SEQ_KEY_PRESSURE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
65 STORE(SEQ_CONTROL(synthno
, msg
[0] & 0x0f,
70 STORE(SEQ_SET_PATCH(synthno
, msg
[0] & 0x0f, msg
[1]));
74 STORE(SEQ_CHN_PRESSURE(synthno
, msg
[0] & 0x0f, msg
[1]));
78 STORE(SEQ_BENDER(synthno
, msg
[0] & 0x0f,
79 (msg
[1] & 0x7f) | ((msg
[2] & 0x7f) << 7)));
83 /* printk( "MPU: Unknown midi channel message %02x\n", msg[0]); */
87 EXPORT_SYMBOL(do_midi_msg
);
90 midi_outc(int midi_dev
, int data
)
94 for (timeout
= 0; timeout
< 3200; timeout
++)
95 if (midi_devs
[midi_dev
]->outputc(midi_dev
, (unsigned char) (data
& 0xff)))
100 prev_out_status
[midi_dev
] =
101 (unsigned char) (data
& 0xff); /*
102 * Store for running status
109 * Sorry! No space on buffers.
111 printk("Midi send timed out\n");
115 prefix_cmd(int midi_dev
, unsigned char status
)
117 if ((char *) midi_devs
[midi_dev
]->prefix_cmd
== NULL
)
120 return midi_devs
[midi_dev
]->prefix_cmd(midi_dev
, status
);
124 midi_synth_input(int orig_dev
, unsigned char data
)
127 struct midi_input_info
*inc
;
129 static unsigned char len_tab
[] = /* # of data bytes following a status
142 if (orig_dev
< 0 || orig_dev
> num_midis
|| midi_devs
[orig_dev
] == NULL
)
145 if (data
== 0xfe) /* Ignore active sensing */
148 dev
= midi2synth
[orig_dev
];
149 inc
= &midi_devs
[orig_dev
]->in_info
;
151 switch (inc
->m_state
)
154 if (data
& 0x80) /* MIDI status byte */
156 if ((data
& 0xf0) == 0xf0) /* Common message */
160 case 0xf0: /* Sysex */
161 inc
->m_state
= MST_SYSEX
;
164 case 0xf1: /* MTC quarter frame */
165 case 0xf3: /* Song select */
166 inc
->m_state
= MST_DATA
;
169 inc
->m_buf
[0] = data
;
172 case 0xf2: /* Song position pointer */
173 inc
->m_state
= MST_DATA
;
176 inc
->m_buf
[0] = data
;
180 inc
->m_buf
[0] = data
;
182 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
188 inc
->m_state
= MST_DATA
;
190 inc
->m_left
= len_tab
[(data
>> 4) - 8];
191 inc
->m_buf
[0] = inc
->m_prev_status
= data
;
193 } else if (inc
->m_prev_status
& 0x80) {
194 /* Data byte (use running status) */
196 inc
->m_buf
[1] = data
;
197 inc
->m_buf
[0] = inc
->m_prev_status
;
198 inc
->m_left
= len_tab
[(inc
->m_buf
[0] >> 4) - 8] - 1;
200 inc
->m_state
= MST_DATA
; /* Not done yet */
202 inc
->m_state
= MST_INIT
;
203 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
207 break; /* MST_INIT */
210 inc
->m_buf
[inc
->m_ptr
++] = data
;
211 if (--inc
->m_left
<= 0)
213 inc
->m_state
= MST_INIT
;
214 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
217 break; /* MST_DATA */
220 if (data
== 0xf7) /* Sysex end */
222 inc
->m_state
= MST_INIT
;
226 break; /* MST_SYSEX */
229 printk("MIDI%d: Unexpected state %d (%02x)\n", orig_dev
, inc
->m_state
, (int) data
);
230 inc
->m_state
= MST_INIT
;
237 int orig_dev
= synth_devs
[dev
]->midi_dev
;
240 if (!sysex_state
[dev
])
243 sysex_state
[dev
] = 0;
245 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, 0xf7) &&
249 sysex_state
[dev
] = 0;
253 midi_synth_output(int dev
)
260 int midi_synth_ioctl(int dev
, unsigned int cmd
, void __user
*arg
)
263 * int orig_dev = synth_devs[dev]->midi_dev;
268 case SNDCTL_SYNTH_INFO
:
269 if (__copy_to_user(arg
, synth_devs
[dev
]->info
, sizeof(struct synth_info
)))
273 case SNDCTL_SYNTH_MEMAVL
:
280 EXPORT_SYMBOL(midi_synth_ioctl
);
283 midi_synth_kill_note(int dev
, int channel
, int note
, int velocity
)
285 int orig_dev
= synth_devs
[dev
]->midi_dev
;
288 if (note
< 0 || note
> 127)
290 if (channel
< 0 || channel
> 15)
299 msg
= prev_out_status
[orig_dev
] & 0xf0;
300 chn
= prev_out_status
[orig_dev
] & 0x0f;
302 if (chn
== channel
&& ((msg
== 0x90 && velocity
== 64) || msg
== 0x80))
306 if (!prefix_cmd(orig_dev
, note
))
309 midi_outc(orig_dev
, note
);
312 * Running status = Note on
314 midi_outc(orig_dev
, 0); /*
315 * Note on with velocity 0 == note
319 midi_outc(orig_dev
, velocity
);
324 if (!prefix_cmd(orig_dev
, 0x90 | (channel
& 0x0f)))
326 midi_outc(orig_dev
, 0x90 | (channel
& 0x0f)); /*
329 midi_outc(orig_dev
, note
);
330 midi_outc(orig_dev
, 0); /*
335 if (!prefix_cmd(orig_dev
, 0x80 | (channel
& 0x0f)))
337 midi_outc(orig_dev
, 0x80 | (channel
& 0x0f)); /*
340 midi_outc(orig_dev
, note
);
341 midi_outc(orig_dev
, velocity
);
347 EXPORT_SYMBOL(midi_synth_kill_note
);
350 midi_synth_set_instr(int dev
, int channel
, int instr_no
)
352 int orig_dev
= synth_devs
[dev
]->midi_dev
;
354 if (instr_no
< 0 || instr_no
> 127)
356 if (channel
< 0 || channel
> 15)
361 if (!prefix_cmd(orig_dev
, 0xc0 | (channel
& 0x0f)))
363 midi_outc(orig_dev
, 0xc0 | (channel
& 0x0f)); /*
366 midi_outc(orig_dev
, instr_no
);
370 EXPORT_SYMBOL(midi_synth_set_instr
);
373 midi_synth_start_note(int dev
, int channel
, int note
, int velocity
)
375 int orig_dev
= synth_devs
[dev
]->midi_dev
;
378 if (note
< 0 || note
> 127)
380 if (channel
< 0 || channel
> 15)
389 msg
= prev_out_status
[orig_dev
] & 0xf0;
390 chn
= prev_out_status
[orig_dev
] & 0x0f;
392 if (chn
== channel
&& msg
== 0x90)
396 if (!prefix_cmd(orig_dev
, note
))
398 midi_outc(orig_dev
, note
);
399 midi_outc(orig_dev
, velocity
);
402 if (!prefix_cmd(orig_dev
, 0x90 | (channel
& 0x0f)))
404 midi_outc(orig_dev
, 0x90 | (channel
& 0x0f)); /*
407 midi_outc(orig_dev
, note
);
408 midi_outc(orig_dev
, velocity
);
412 EXPORT_SYMBOL(midi_synth_start_note
);
415 midi_synth_reset(int dev
)
420 EXPORT_SYMBOL(midi_synth_reset
);
423 midi_synth_open(int dev
, int mode
)
425 int orig_dev
= synth_devs
[dev
]->midi_dev
;
427 struct midi_input_info
*inc
;
429 if (orig_dev
< 0 || orig_dev
>= num_midis
|| midi_devs
[orig_dev
] == NULL
)
432 midi2synth
[orig_dev
] = dev
;
433 sysex_state
[dev
] = 0;
434 prev_out_status
[orig_dev
] = 0;
436 if ((err
= midi_devs
[orig_dev
]->open(orig_dev
, mode
,
437 midi_synth_input
, midi_synth_output
)) < 0)
439 inc
= &midi_devs
[orig_dev
]->in_info
;
441 /* save_flags(flags);
443 don't know against what irqhandler to protect*/
445 inc
->m_state
= MST_INIT
;
448 inc
->m_prev_status
= 0x00;
449 /* restore_flags(flags); */
453 EXPORT_SYMBOL(midi_synth_open
);
456 midi_synth_close(int dev
)
458 int orig_dev
= synth_devs
[dev
]->midi_dev
;
463 * Shut up the synths by sending just single active sensing message.
465 midi_devs
[orig_dev
]->outputc(orig_dev
, 0xfe);
467 midi_devs
[orig_dev
]->close(orig_dev
);
469 EXPORT_SYMBOL(midi_synth_close
);
472 midi_synth_hw_control(int dev
, unsigned char *event
)
475 EXPORT_SYMBOL(midi_synth_hw_control
);
478 midi_synth_load_patch(int dev
, int format
, const char __user
*addr
,
479 int offs
, int count
, int pmgr_flag
)
481 int orig_dev
= synth_devs
[dev
]->midi_dev
;
483 struct sysex_info sysex
;
485 unsigned long left
, src_offs
, eox_seen
= 0;
487 int hdr_size
= (unsigned long) &sysex
.data
[0] - (unsigned long) &sysex
;
491 if (!prefix_cmd(orig_dev
, 0xf0))
494 if (format
!= SYSEX_PATCH
)
496 /* printk("MIDI Error: Invalid patch format (key) 0x%x\n", format);*/
499 if (count
< hdr_size
)
501 /* printk("MIDI Error: Patch header too short\n");*/
507 * Copy the header from user space but ignore the first bytes which have
508 * been transferred already.
511 if(copy_from_user(&((char *) &sysex
)[offs
], &(addr
)[offs
], hdr_size
- offs
))
514 if (count
< sysex
.len
)
516 /* printk(KERN_WARNING "MIDI Warning: Sysex record too short (%d<%d)\n", count, (int) sysex.len);*/
522 for (i
= 0; i
< left
&& !signal_pending(current
); i
++)
527 (unsigned char __user
*)(addr
+ hdr_size
+ i
)))
530 eox_seen
= (i
> 0 && data
& 0x80); /* End of sysex */
532 if (eox_seen
&& data
!= 0xf7)
539 printk(KERN_WARNING
"midi_synth: Sysex start missing\n");
543 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, (unsigned char) (data
& 0xff)) &&
544 !signal_pending(current
))
547 if (!first_byte
&& data
& 0x80)
553 midi_outc(orig_dev
, 0xf7);
556 EXPORT_SYMBOL(midi_synth_load_patch
);
558 void midi_synth_panning(int dev
, int channel
, int pressure
)
561 EXPORT_SYMBOL(midi_synth_panning
);
563 void midi_synth_aftertouch(int dev
, int channel
, int pressure
)
565 int orig_dev
= synth_devs
[dev
]->midi_dev
;
568 if (pressure
< 0 || pressure
> 127)
570 if (channel
< 0 || channel
> 15)
575 msg
= prev_out_status
[orig_dev
] & 0xf0;
576 chn
= prev_out_status
[orig_dev
] & 0x0f;
578 if (msg
!= 0xd0 || chn
!= channel
) /*
579 * Test for running status
582 if (!prefix_cmd(orig_dev
, 0xd0 | (channel
& 0x0f)))
584 midi_outc(orig_dev
, 0xd0 | (channel
& 0x0f)); /*
587 } else if (!prefix_cmd(orig_dev
, pressure
))
590 midi_outc(orig_dev
, pressure
);
592 EXPORT_SYMBOL(midi_synth_aftertouch
);
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);
621 EXPORT_SYMBOL(midi_synth_controller
);
624 midi_synth_bender(int dev
, int channel
, int value
)
626 int orig_dev
= synth_devs
[dev
]->midi_dev
;
629 if (channel
< 0 || channel
> 15)
632 if (value
< 0 || value
> 16383)
637 msg
= prev_out_status
[orig_dev
] & 0xf0;
638 prev_chn
= prev_out_status
[orig_dev
] & 0x0f;
640 if (msg
!= 0xd0 || prev_chn
!= channel
) /*
641 * Test for running status
644 if (!prefix_cmd(orig_dev
, 0xe0 | (channel
& 0x0f)))
646 midi_outc(orig_dev
, 0xe0 | (channel
& 0x0f));
647 } else if (!prefix_cmd(orig_dev
, value
& 0x7f))
650 midi_outc(orig_dev
, value
& 0x7f);
651 midi_outc(orig_dev
, (value
>> 7) & 0x7f);
653 EXPORT_SYMBOL(midi_synth_bender
);
656 midi_synth_setup_voice(int dev
, int voice
, int channel
)
659 EXPORT_SYMBOL(midi_synth_setup_voice
);
662 midi_synth_send_sysex(int dev
, unsigned char *bytes
, int len
)
664 int orig_dev
= synth_devs
[dev
]->midi_dev
;
667 for (i
= 0; i
< len
; i
++)
671 case 0xf0: /* Start sysex */
672 if (!prefix_cmd(orig_dev
, 0xf0))
674 sysex_state
[dev
] = 1;
677 case 0xf7: /* End sysex */
678 if (!sysex_state
[dev
]) /* Orphan sysex end */
680 sysex_state
[dev
] = 0;
684 if (!sysex_state
[dev
])
687 if (bytes
[i
] & 0x80) /* Error. Another message before sysex end */
689 bytes
[i
] = 0xf7; /* Sysex end */
690 sysex_state
[dev
] = 0;
694 if (!midi_devs
[orig_dev
]->outputc(orig_dev
, bytes
[i
]))
697 * Hardware level buffer is full. Abort the sysex message.
703 sysex_state
[dev
] = 0;
705 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, bytes
[i
]) &&
709 if (!sysex_state
[dev
])
715 EXPORT_SYMBOL(midi_synth_send_sysex
);