Import 2.3.18pre1
[davej-history.git] / drivers / sound / midi_synth.c
blob03d7d284f9efd0caa9b3ded1352bffe7c748a8e9
1 /*
2 * sound/midi_synth.c
4 * High level midi sequencer manager for dumb MIDI interfaces.
5 */
6 /*
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
11 * for more info.
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"
24 #ifdef CONFIG_MIDI
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] =
32 {0};
33 static unsigned char prev_out_status[MAX_MIDI_DEV];
35 #ifndef CONFIG_SEQUENCER
36 #define STORE(cmd)
37 #else
38 #define STORE(cmd) \
39 { \
40 int len; \
41 unsigned char obuf[8]; \
42 cmd; \
43 seq_input_event(obuf, len); \
45 #endif
47 #define _seqbuf obuf
48 #define _seqbufptr 0
49 #define _SEQ_ADVBUF(x) len=x
51 void
52 do_midi_msg(int synthno, unsigned char *msg, int mlen)
54 switch (msg[0] & 0xf0)
56 case 0x90:
57 if (msg[2] != 0)
59 STORE(SEQ_START_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
60 break;
62 msg[2] = 64;
64 case 0x80:
65 STORE(SEQ_STOP_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
66 break;
68 case 0xA0:
69 STORE(SEQ_KEY_PRESSURE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
70 break;
72 case 0xB0:
73 STORE(SEQ_CONTROL(synthno, msg[0] & 0x0f,
74 msg[1], msg[2]));
75 break;
77 case 0xC0:
78 STORE(SEQ_SET_PATCH(synthno, msg[0] & 0x0f, msg[1]));
79 break;
81 case 0xD0:
82 STORE(SEQ_CHN_PRESSURE(synthno, msg[0] & 0x0f, msg[1]));
83 break;
85 case 0xE0:
86 STORE(SEQ_BENDER(synthno, msg[0] & 0x0f,
87 (msg[1] & 0x7f) | ((msg[2] & 0x7f) << 7)));
88 break;
90 default:
91 /* printk( "MPU: Unknown midi channel message %02x\n", msg[0]); */
96 static void
97 midi_outc(int midi_dev, int data)
99 int timeout;
101 for (timeout = 0; timeout < 3200; timeout++)
102 if (midi_devs[midi_dev]->outputc(midi_dev, (unsigned char) (data & 0xff)))
104 if (data & 0x80) /*
105 * Status byte
107 prev_out_status[midi_dev] =
108 (unsigned char) (data & 0xff); /*
109 * Store for running status
111 return; /*
112 * Mission complete
116 * Sorry! No space on buffers.
118 printk("Midi send timed out\n");
121 static int
122 prefix_cmd(int midi_dev, unsigned char status)
124 if ((char *) midi_devs[midi_dev]->prefix_cmd == NULL)
125 return 1;
127 return midi_devs[midi_dev]->prefix_cmd(midi_dev, status);
130 static void
131 midi_synth_input(int orig_dev, unsigned char data)
133 int dev;
134 struct midi_input_info *inc;
136 static unsigned char len_tab[] = /* # of data bytes following a status
139 2, /* 8x */
140 2, /* 9x */
141 2, /* Ax */
142 2, /* Bx */
143 1, /* Cx */
144 1, /* Dx */
145 2, /* Ex */
146 0 /* Fx */
149 if (orig_dev < 0 || orig_dev > num_midis || midi_devs[orig_dev] == NULL)
150 return;
152 if (data == 0xfe) /* Ignore active sensing */
153 return;
155 dev = midi2synth[orig_dev];
156 inc = &midi_devs[orig_dev]->in_info;
158 switch (inc->m_state)
160 case MST_INIT:
161 if (data & 0x80) /* MIDI status byte */
163 if ((data & 0xf0) == 0xf0) /* Common message */
165 switch (data)
167 case 0xf0: /* Sysex */
168 inc->m_state = MST_SYSEX;
169 break; /* Sysex */
171 case 0xf1: /* MTC quarter frame */
172 case 0xf3: /* Song select */
173 inc->m_state = MST_DATA;
174 inc->m_ptr = 1;
175 inc->m_left = 1;
176 inc->m_buf[0] = data;
177 break;
179 case 0xf2: /* Song position pointer */
180 inc->m_state = MST_DATA;
181 inc->m_ptr = 1;
182 inc->m_left = 2;
183 inc->m_buf[0] = data;
184 break;
186 default:
187 inc->m_buf[0] = data;
188 inc->m_ptr = 1;
189 do_midi_msg(dev, inc->m_buf, inc->m_ptr);
190 inc->m_ptr = 0;
191 inc->m_left = 0;
193 } else
195 inc->m_state = MST_DATA;
196 inc->m_ptr = 1;
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) */
202 inc->m_ptr = 2;
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;
206 if (inc->m_left > 0)
207 inc->m_state = MST_DATA; /* Not done yet */
208 else {
209 inc->m_state = MST_INIT;
210 do_midi_msg(dev, inc->m_buf, inc->m_ptr);
211 inc->m_ptr = 0;
214 break; /* MST_INIT */
216 case MST_DATA:
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);
222 inc->m_ptr = 0;
224 break; /* MST_DATA */
226 case MST_SYSEX:
227 if (data == 0xf7) /* Sysex end */
229 inc->m_state = MST_INIT;
230 inc->m_left = 0;
231 inc->m_ptr = 0;
233 break; /* MST_SYSEX */
235 default:
236 printk("MIDI%d: Unexpected state %d (%02x)\n", orig_dev, inc->m_state, (int) data);
237 inc->m_state = MST_INIT;
241 static void
242 leave_sysex(int dev)
244 int orig_dev = synth_devs[dev]->midi_dev;
245 int timeout = 0;
247 if (!sysex_state[dev])
248 return;
250 sysex_state[dev] = 0;
252 while (!midi_devs[orig_dev]->outputc(orig_dev, 0xf7) &&
253 timeout < 1000)
254 timeout++;
256 sysex_state[dev] = 0;
259 static void
260 midi_synth_output(int dev)
263 * Currently NOP
267 int midi_synth_ioctl(int dev, unsigned int cmd, caddr_t arg)
270 * int orig_dev = synth_devs[dev]->midi_dev;
273 switch (cmd) {
275 case SNDCTL_SYNTH_INFO:
276 if (__copy_to_user(arg, synth_devs[dev]->info, sizeof(struct synth_info)))
277 return -EFAULT;
278 return 0;
280 case SNDCTL_SYNTH_MEMAVL:
281 return 0x7fffffff;
283 default:
284 return -EINVAL;
289 midi_synth_kill_note(int dev, int channel, int note, int velocity)
291 int orig_dev = synth_devs[dev]->midi_dev;
292 int msg, chn;
294 if (note < 0 || note > 127)
295 return 0;
296 if (channel < 0 || channel > 15)
297 return 0;
298 if (velocity < 0)
299 velocity = 0;
300 if (velocity > 127)
301 velocity = 127;
303 leave_sysex(dev);
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))
309 { /*
310 * Use running status
312 if (!prefix_cmd(orig_dev, note))
313 return 0;
315 midi_outc(orig_dev, note);
317 if (msg == 0x90) /*
318 * Running status = Note on
320 midi_outc(orig_dev, 0); /*
321 * Note on with velocity 0 == note
322 * off
324 else
325 midi_outc(orig_dev, velocity);
326 } else
328 if (velocity == 64)
330 if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
331 return 0;
332 midi_outc(orig_dev, 0x90 | (channel & 0x0f)); /*
333 * Note on
335 midi_outc(orig_dev, note);
336 midi_outc(orig_dev, 0); /*
337 * Zero G
339 } else
341 if (!prefix_cmd(orig_dev, 0x80 | (channel & 0x0f)))
342 return 0;
343 midi_outc(orig_dev, 0x80 | (channel & 0x0f)); /*
344 * Note off
346 midi_outc(orig_dev, note);
347 midi_outc(orig_dev, velocity);
351 return 0;
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)
360 instr_no = 0;
361 if (channel < 0 || channel > 15)
362 return 0;
364 leave_sysex(dev);
366 if (!prefix_cmd(orig_dev, 0xc0 | (channel & 0x0f)))
367 return 0;
368 midi_outc(orig_dev, 0xc0 | (channel & 0x0f)); /*
369 * Program change
371 midi_outc(orig_dev, instr_no);
373 return 0;
377 midi_synth_start_note(int dev, int channel, int note, int velocity)
379 int orig_dev = synth_devs[dev]->midi_dev;
380 int msg, chn;
382 if (note < 0 || note > 127)
383 return 0;
384 if (channel < 0 || channel > 15)
385 return 0;
386 if (velocity < 0)
387 velocity = 0;
388 if (velocity > 127)
389 velocity = 127;
391 leave_sysex(dev);
393 msg = prev_out_status[orig_dev] & 0xf0;
394 chn = prev_out_status[orig_dev] & 0x0f;
396 if (chn == channel && msg == 0x90)
397 { /*
398 * Use running status
400 if (!prefix_cmd(orig_dev, note))
401 return 0;
402 midi_outc(orig_dev, note);
403 midi_outc(orig_dev, velocity);
404 } else
406 if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
407 return 0;
408 midi_outc(orig_dev, 0x90 | (channel & 0x0f)); /*
409 * Note on
411 midi_outc(orig_dev, note);
412 midi_outc(orig_dev, velocity);
414 return 0;
417 void
418 midi_synth_reset(int dev)
421 leave_sysex(dev);
425 midi_synth_open(int dev, int mode)
427 int orig_dev = synth_devs[dev]->midi_dev;
428 int err;
429 unsigned long flags;
430 struct midi_input_info *inc;
432 if (orig_dev < 0 || orig_dev > num_midis || midi_devs[orig_dev] == NULL)
433 return -ENXIO;
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)
441 return err;
442 #ifdef MODULE
443 MOD_INC_USE_COUNT;
444 #endif
445 inc = &midi_devs[orig_dev]->in_info;
447 save_flags(flags);
448 cli();
449 inc->m_busy = 0;
450 inc->m_state = MST_INIT;
451 inc->m_ptr = 0;
452 inc->m_left = 0;
453 inc->m_prev_status = 0x00;
454 restore_flags(flags);
456 return 1;
459 void
460 midi_synth_close(int dev)
462 int orig_dev = synth_devs[dev]->midi_dev;
464 leave_sysex(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);
472 #ifdef MODULE
473 MOD_DEC_USE_COUNT;
474 #endif
477 void
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;
489 int i;
490 unsigned long left, src_offs, eox_seen = 0;
491 int first_byte = 1;
492 int hdr_size = (unsigned long) &sysex.data[0] - (unsigned long) &sysex;
494 leave_sysex(dev);
496 if (!prefix_cmd(orig_dev, 0xf0))
497 return 0;
499 if (format != SYSEX_PATCH)
501 /* printk("MIDI Error: Invalid patch format (key) 0x%x\n", format);*/
502 return -EINVAL;
504 if (count < hdr_size)
506 /* printk("MIDI Error: Patch header too short\n");*/
507 return -EINVAL;
509 count -= hdr_size;
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))
517 return -EFAULT;
519 if (count < sysex.len)
521 /* printk(KERN_WARNING "MIDI Warning: Sysex record too short (%d<%d)\n", count, (int) sysex.len);*/
522 sysex.len = count;
524 left = sysex.len;
525 src_offs = 0;
527 for (i = 0; i < left && !signal_pending(current); i++)
529 unsigned char data;
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)
536 data = 0xf7;
538 if (i == 0)
540 if (data != 0xf0)
542 printk(KERN_WARNING "midi_synth: Sysex start missing\n");
543 return -EINVAL;
546 while (!midi_devs[orig_dev]->outputc(orig_dev, (unsigned char) (data & 0xff)) &&
547 !signal_pending(current))
548 schedule();
550 if (!first_byte && data & 0x80)
551 return 0;
552 first_byte = 0;
555 if (!eox_seen)
556 midi_outc(orig_dev, 0xf7);
557 return 0;
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;
567 int msg, chn;
569 if (pressure < 0 || pressure > 127)
570 return;
571 if (channel < 0 || channel > 15)
572 return;
574 leave_sysex(dev);
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)))
584 return;
585 midi_outc(orig_dev, 0xd0 | (channel & 0x0f)); /*
586 * Channel pressure
588 } else if (!prefix_cmd(orig_dev, pressure))
589 return;
591 midi_outc(orig_dev, pressure);
594 void
595 midi_synth_controller(int dev, int channel, int ctrl_num, int value)
597 int orig_dev = synth_devs[dev]->midi_dev;
598 int chn, msg;
600 if (ctrl_num < 0 || ctrl_num > 127)
601 return;
602 if (channel < 0 || channel > 15)
603 return;
605 leave_sysex(dev);
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)))
613 return;
614 midi_outc(orig_dev, 0xb0 | (channel & 0x0f));
615 } else if (!prefix_cmd(orig_dev, ctrl_num))
616 return;
618 midi_outc(orig_dev, ctrl_num);
619 midi_outc(orig_dev, value & 0x7f);
622 void
623 midi_synth_bender(int dev, int channel, int value)
625 int orig_dev = synth_devs[dev]->midi_dev;
626 int msg, prev_chn;
628 if (channel < 0 || channel > 15)
629 return;
631 if (value < 0 || value > 16383)
632 return;
634 leave_sysex(dev);
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)))
644 return;
645 midi_outc(orig_dev, 0xe0 | (channel & 0x0f));
646 } else if (!prefix_cmd(orig_dev, value & 0x7f))
647 return;
649 midi_outc(orig_dev, value & 0x7f);
650 midi_outc(orig_dev, (value >> 7) & 0x7f);
653 void
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;
662 int i;
664 for (i = 0; i < len; i++)
666 switch (bytes[i])
668 case 0xf0: /* Start sysex */
669 if (!prefix_cmd(orig_dev, 0xf0))
670 return 0;
671 sysex_state[dev] = 1;
672 break;
674 case 0xf7: /* End sysex */
675 if (!sysex_state[dev]) /* Orphan sysex end */
676 return 0;
677 sysex_state[dev] = 0;
678 break;
680 default:
681 if (!sysex_state[dev])
682 return 0;
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.
697 int timeout = 0;
699 bytes[i] = 0xf7;
700 sysex_state[dev] = 0;
702 while (!midi_devs[orig_dev]->outputc(orig_dev, bytes[i]) &&
703 timeout < 1000)
704 timeout++;
706 if (!sysex_state[dev])
707 return 0;
710 return 0;
714 #endif