kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / sound / isa / i386 / midi_synth.c
1 /*
2  * sound/midi_synth.c
3  * 
4  * High level midi sequencer manager for dumb MIDI interfaces.
5  * 
6  * Copyright by Hannu Savolainen 1993
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met: 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer. 2.
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  * 
28  * $DragonFly: src/sys/dev/sound/isa/i386/Attic/midi_synth.c,v 1.2 2003/08/07 21:17:12 dillon Exp $
29  */
30
31 #define USE_SEQ_MACROS
32 #define USE_SIMPLE_MACROS
33
34 #include <stddef.h>
35
36 #include "sound_config.h"
37
38 #if defined(CONFIGURE_SOUNDCARD) /* && defined(CONFIG_MIDI) */
39
40 #define _MIDI_SYNTH_C_
41
42 static int     *sysex_sleeper = NULL;
43 static volatile struct snd_wait sysex_sleep_flag = {0};
44
45 #include "midi_synth.h"
46
47 static int      midi2synth[MAX_MIDI_DEV];
48 static int      sysex_state[MAX_MIDI_DEV] =
49 {0};
50 static unsigned char prev_out_status[MAX_MIDI_DEV];
51
52 #ifndef CONFIG_SEQUENCER
53 #define STORE(cmd)
54 #else
55 #define STORE(cmd) { \
56   int len; \
57   unsigned char obuf[8]; \
58   cmd; \
59   seq_input_event(obuf, len); \
60 }
61 #endif
62
63 #define _seqbuf obuf
64 #define _seqbufptr 0
65 #define _SEQ_ADVBUF(x) len=x
66
67 void
68 do_midi_msg(int synthno, unsigned char *msg, int mlen)
69 {
70     switch (msg[0] & 0xf0) {
71     case 0x90:
72         if (msg[2] != 0) {
73             STORE(SEQ_START_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
74             break;
75         }
76         msg[2] = 64;
77
78     case 0x80:
79         STORE(SEQ_STOP_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
80         break;
81
82     case 0xA0:
83         STORE(SEQ_KEY_PRESSURE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
84         break;
85
86     case 0xB0:
87         STORE(SEQ_CONTROL(synthno, msg[0] & 0x0f, msg[1], msg[2]));
88         break;
89
90     case 0xC0:
91         STORE(SEQ_SET_PATCH(synthno, msg[0] & 0x0f, msg[1]));
92         break;
93
94     case 0xD0:
95         STORE(SEQ_CHN_PRESSURE(synthno, msg[0] & 0x0f, msg[1]));
96         break;
97
98     case 0xE0:
99         STORE(SEQ_BENDER(synthno, msg[0] & 0x0f,
100                          (msg[1] % 0x7f) | ((msg[2] & 0x7f) << 7)));
101         break;
102
103     default:
104         /*
105          * printf ("MPU: Unknown midi channel message %02x\n",
106          * msg[0]);
107          */
108         ;
109     }
110 }
111
112 static void
113 midi_outc(int midi_dev, int data)
114 {
115     int             timeout;
116
117     for (timeout = 0; timeout < 32000; timeout++)
118         if (midi_devs[midi_dev]->putc(midi_dev, (unsigned char) (data & 0xff))) {
119             if (data & 0x80)    /* Status byte */
120                 prev_out_status[midi_dev] =
121                         (unsigned char) (data & 0xff);  /* Store for running
122                                                          * status */
123                 return; /* Mission complete */
124         }
125     /*
126      * Sorry! No space on buffers.
127      */
128     printf("Midi send timed out\n");
129 }
130
131 static int
132 prefix_cmd(int midi_dev, unsigned char status)
133 {
134     if ((char *) midi_devs[midi_dev]->prefix_cmd == NULL)
135         return 1;
136
137     return midi_devs[midi_dev]->prefix_cmd(midi_dev, status);
138 }
139
140 static void
141 midi_synth_input(int orig_dev, unsigned char data)
142 {
143     int             dev;
144     struct midi_input_info *inc;
145
146     static unsigned char len_tab[] = /* # of data bytes following a status */
147     {
148             2,          /* 8x */
149             2,          /* 9x */
150             2,          /* Ax */
151             2,          /* Bx */
152             1,          /* Cx */
153             1,          /* Dx */
154             2,          /* Ex */
155             0           /* Fx */
156     };
157
158     if (orig_dev < 0 || orig_dev > num_midis)
159         return;
160
161     if (data == 0xfe)   /* Ignore active sensing */
162         return;
163
164     dev = midi2synth[orig_dev];
165     inc = &midi_devs[orig_dev]->in_info;
166
167     switch (inc->m_state) {
168     case MST_INIT:
169         if (data & 0x80) {      /* MIDI status byte */
170             if ((data & 0xf0) == 0xf0) {        /* Common message */
171                 switch (data) {
172                 case 0xf0:      /* Sysex */
173                     inc->m_state = MST_SYSEX;
174                     break;      /* Sysex */
175
176                 case 0xf1:      /* MTC quarter frame */
177                 case 0xf3:      /* Song select */
178                     inc->m_state = MST_DATA;
179                     inc->m_ptr = 1;
180                     inc->m_left = 1;
181                     inc->m_buf[0] = data;
182                     break;
183
184                 case 0xf2:      /* Song position pointer */
185                     inc->m_state = MST_DATA;
186                     inc->m_ptr = 1;
187                     inc->m_left = 2;
188                     inc->m_buf[0] = data;
189                     break;
190
191                 default:
192                     inc->m_buf[0] = data;
193                     inc->m_ptr = 1;
194                     do_midi_msg(dev, inc->m_buf, inc->m_ptr);
195                     inc->m_ptr = 0;
196                     inc->m_left = 0;
197                 }
198             } else {
199                 inc->m_state = MST_DATA;
200                 inc->m_ptr = 1;
201                 inc->m_left = len_tab[(data >> 4) - 8];
202                 inc->m_buf[0] = inc->m_prev_status = data;
203             }
204         } else if (inc->m_prev_status & 0x80) { /* Ignore if no previous
205                  * status (yet) *//* Data byte (use running status) */
206             inc->m_state = MST_DATA;
207             inc->m_ptr = 2;
208             inc->m_left = len_tab[(data >> 4) - 8] - 1;
209             inc->m_buf[0] = inc->m_prev_status;
210             inc->m_buf[1] = data;
211         }
212         break;          /* MST_INIT */
213
214     case MST_DATA:
215         inc->m_buf[inc->m_ptr++] = data;
216         if (--inc->m_left <= 0) {
217             inc->m_state = MST_INIT;
218             do_midi_msg(dev, inc->m_buf, inc->m_ptr);
219             inc->m_ptr = 0;
220         }
221         break;          /* MST_DATA */
222
223     case MST_SYSEX:
224         if (data == 0xf7) {     /* Sysex end */
225             inc->m_state = MST_INIT;
226             inc->m_left = 0;
227             inc->m_ptr = 0;
228         }
229         break;          /* MST_SYSEX */
230
231     default:
232         printf("MIDI%d: Unexpected state %d (%02x)\n", orig_dev, inc->m_state,
233                (int) data);
234         inc->m_state = MST_INIT;
235     }
236 }
237
238 static void
239 leave_sysex(int dev)
240 {
241     int             orig_dev = synth_devs[dev]->midi_dev;
242     int             timeout = 0;
243
244     if (!sysex_state[dev])
245         return;
246
247     sysex_state[dev] = 0;
248
249     while (!midi_devs[orig_dev]->putc(orig_dev, 0xf7) && timeout < 1000)
250         timeout++;
251
252     sysex_state[dev] = 0;
253 }
254
255 static void
256 midi_synth_output(int dev)
257 {
258     /*
259      * Currently NOP
260      */
261 }
262
263 int
264 midi_synth_ioctl(int dev, unsigned int cmd, ioctl_arg arg)
265 {
266     /*
267      * int orig_dev = synth_devs[dev]->midi_dev;
268      */
269
270     switch (cmd) {
271
272     case SNDCTL_SYNTH_INFO:
273         bcopy(synth_devs[dev]->info, &(((char *) arg)[0]), sizeof(struct synth_info));
274         return 0;
275         break;
276
277     case SNDCTL_SYNTH_MEMAVL:
278         return 0x7fffffff;
279         break;
280
281     default:
282         return -(EINVAL);
283     }
284 }
285
286 int
287 midi_synth_kill_note(int dev, int channel, int note, int velocity)
288 {
289     int             orig_dev = synth_devs[dev]->midi_dev;
290     int             msg, chn;
291
292     if (note < 0 || note > 127)
293         return 0;
294     if (channel < 0 || channel > 15)
295         return 0;
296     RANGE (velocity, 0, 127 ) ;
297     leave_sysex(dev);
298
299     msg = prev_out_status[orig_dev] & 0xf0;
300     chn = prev_out_status[orig_dev] & 0x0f;
301
302     if (chn == channel && ((msg == 0x90 && velocity == 64) || msg == 0x80)) {   /* Use running status */
303         if (!prefix_cmd(orig_dev, note))
304             return 0;
305
306         midi_outc(orig_dev, note);
307
308         if (msg == 0x90)/* Running status = Note on */
309             midi_outc(orig_dev, 0); /* Note on with velocity 0 == note off */
310         else
311             midi_outc(orig_dev, velocity);
312     } else {
313         if (velocity == 64) {
314             if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
315                 return 0;
316             midi_outc(orig_dev, 0x90 | (channel & 0x0f));       /* Note on */
317             midi_outc(orig_dev, note);
318             midi_outc(orig_dev, 0);     /* Zero G */
319         } else {
320             if (!prefix_cmd(orig_dev, 0x80 | (channel & 0x0f)))
321                 return 0;
322             midi_outc(orig_dev, 0x80 | (channel & 0x0f));       /* Note off */
323             midi_outc(orig_dev, note);
324             midi_outc(orig_dev, velocity);
325         }
326     }
327
328     return 0;
329 }
330
331 int
332 midi_synth_set_instr(int dev, int channel, int instr_no)
333 {
334     int             orig_dev = synth_devs[dev]->midi_dev;
335
336     if (instr_no < 0 || instr_no > 127)
337         return 0;
338     if (channel < 0 || channel > 15)
339         return 0;
340
341     leave_sysex(dev);
342
343     if (!prefix_cmd(orig_dev, 0xc0 | (channel & 0x0f)))
344         return 0;
345     midi_outc(orig_dev, 0xc0 | (channel & 0x0f));       /* Program change */
346     midi_outc(orig_dev, instr_no);
347
348     return 0;
349 }
350
351 int
352 midi_synth_start_note(int dev, int channel, int note, int velocity)
353 {
354     int             orig_dev = synth_devs[dev]->midi_dev;
355     int             msg, chn;
356
357     if (note < 0 || note > 127)
358         return 0;
359     if (channel < 0 || channel > 15)
360         return 0;
361     RANGE (velocity, 0, 127 );
362     leave_sysex(dev);
363
364     msg = prev_out_status[orig_dev] & 0xf0;
365     chn = prev_out_status[orig_dev] & 0x0f;
366
367     if (chn == channel && msg == 0x90) {        /* Use running status */
368         if (!prefix_cmd(orig_dev, note))
369             return 0;
370         midi_outc(orig_dev, note);
371         midi_outc(orig_dev, velocity);
372     } else {
373         if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
374             return 0;
375         midi_outc(orig_dev, 0x90 | (channel & 0x0f));   /* Note on */
376         midi_outc(orig_dev, note);
377         midi_outc(orig_dev, velocity);
378     }
379     return 0;
380 }
381
382 void
383 midi_synth_reset(int dev)
384 {
385
386     leave_sysex(dev);
387 }
388
389 int
390 midi_synth_open(int dev, int mode)
391 {
392     int             orig_dev = synth_devs[dev]->midi_dev;
393     int             err;
394     unsigned long   flags;
395     struct midi_input_info *inc;
396
397     if (orig_dev < 0 || orig_dev > num_midis)
398         return -(ENXIO);
399
400     midi2synth[orig_dev] = dev;
401     sysex_state[dev] = 0;
402     prev_out_status[orig_dev] = 0;
403
404     if ((err = midi_devs[orig_dev]->open(orig_dev, mode,
405                       midi_synth_input, midi_synth_output)) < 0)
406         return err;
407
408     inc = &midi_devs[orig_dev]->in_info;
409
410     flags = splhigh();
411     inc->m_busy = 0;
412     inc->m_state = MST_INIT;
413     inc->m_ptr = 0;
414     inc->m_left = 0;
415     inc->m_prev_status = 0x00;
416     splx(flags);
417
418     sysex_sleep_flag.aborting = 0;
419     sysex_sleep_flag.mode = WK_NONE;
420
421     return 1;
422 }
423
424 void
425 midi_synth_close(int dev)
426 {
427     int             orig_dev = synth_devs[dev]->midi_dev;
428
429     leave_sysex(dev);
430
431     /*
432      * Shut up the synths by sending just single active sensing message.
433      */
434     midi_devs[orig_dev]->putc(orig_dev, 0xfe);
435
436     midi_devs[orig_dev]->close(orig_dev);
437 }
438
439 void
440 midi_synth_hw_control(int dev, unsigned char *event)
441 {
442 }
443
444 int
445 midi_synth_load_patch(int dev, int format, snd_rw_buf * addr,
446                       int offs, int count, int pmgr_flag)
447 {
448     int             orig_dev = synth_devs[dev]->midi_dev;
449
450     struct sysex_info sysex;
451     int             i;
452     unsigned long   left, src_offs, eox_seen = 0;
453     int             first_byte = 1;
454     int             hdr_size = offsetof(struct sysex_info, data);
455
456     leave_sysex(dev);
457
458     if (!prefix_cmd(orig_dev, 0xf0))
459         return 0;
460
461     if (format != SYSEX_PATCH) {
462         printf("MIDI Error: Invalid patch format (key) 0x%x\n", format);
463         return -(EINVAL);
464     }
465     if (count < hdr_size) {
466         printf("MIDI Error: Patch header too short\n");
467         return -(EINVAL);
468     }
469     count -= hdr_size;
470
471     /*
472      * Copy the header from user space but ignore the first bytes which
473      * have been transferred already.
474      */
475
476
477     if (uiomove(&((char *) &sysex)[offs], hdr_size - offs, addr)) {
478         printf("sb: Bad copyin()!\n");
479     };
480
481     if (count < sysex.len) {
482         printf("MIDI Warning: Sysex record too short (%d<%d)\n",
483                count, (int) sysex.len);
484         sysex.len = count;
485     }
486     left = sysex.len;
487     src_offs = 0;
488
489     sysex_sleep_flag.aborting = 0;
490     sysex_sleep_flag.mode = WK_NONE;
491
492     for (i = 0; i < left && !(sysex_sleep_flag.aborting); i++) {
493         unsigned char   data;
494
495         uiomove((char *) &(data), 1, addr);
496
497         eox_seen = (i > 0 && data & 0x80);      /* End of sysex */
498
499         if (eox_seen && data != 0xf7)
500             data = 0xf7;
501
502         if (i == 0) {
503             if (data != 0xf0) {
504                 printf("Error: Sysex start missing\n");
505                 return -(EINVAL);
506             }
507         }
508         while (!midi_devs[orig_dev]->putc(orig_dev,
509                         (unsigned char) (data & 0xff)) &&
510                    !(sysex_sleep_flag.aborting)) {
511             int  chn;
512
513
514             sysex_sleeper = &chn;
515             DO_SLEEP(chn, sysex_sleep_flag, 1);
516
517         };              /* Wait for timeout */
518
519         if (!first_byte && data & 0x80)
520             return 0;
521         first_byte = 0;
522     }
523
524     if (!eox_seen)
525         midi_outc(orig_dev, 0xf7);
526     return 0;
527 }
528
529 void
530 midi_synth_panning(int dev, int channel, int pressure)
531 {
532 }
533
534 void
535 midi_synth_aftertouch(int dev, int channel, int pressure)
536 {
537     int             orig_dev = synth_devs[dev]->midi_dev;
538     int             msg, chn;
539
540     if (pressure < 0 || pressure > 127)
541         return;
542     if (channel < 0 || channel > 15)
543         return;
544
545     leave_sysex(dev);
546
547     msg = prev_out_status[orig_dev] & 0xf0;
548     chn = prev_out_status[orig_dev] & 0x0f;
549
550     if (msg != 0xd0 || chn != channel) {        /* Test for running status */
551         if (!prefix_cmd(orig_dev, 0xd0 | (channel & 0x0f)))
552             return;
553         midi_outc(orig_dev, 0xd0 | (channel & 0x0f));   /* Channel pressure */
554     } else if (!prefix_cmd(orig_dev, pressure))
555         return;
556
557     midi_outc(orig_dev, pressure);
558 }
559
560 void
561 midi_synth_controller(int dev, int channel, int ctrl_num, int value)
562 {
563     int             orig_dev = synth_devs[dev]->midi_dev;
564     int             chn, msg;
565
566     if (ctrl_num < 1 || ctrl_num > 127)
567         return;         /* NOTE! Controller # 0 ignored */
568     if (channel < 0 || channel > 15)
569         return;
570
571     leave_sysex(dev);
572
573     msg = prev_out_status[orig_dev] & 0xf0;
574     chn = prev_out_status[orig_dev] & 0x0f;
575
576     if (msg != 0xb0 || chn != channel) {
577         if (!prefix_cmd(orig_dev, 0xb0 | (channel & 0x0f)))
578             return;
579         midi_outc(orig_dev, 0xb0 | (channel & 0x0f));
580     } else if (!prefix_cmd(orig_dev, ctrl_num))
581         return;
582
583     midi_outc(orig_dev, ctrl_num);
584     midi_outc(orig_dev, value & 0x7f);
585 }
586
587 int
588 midi_synth_patchmgr(int dev, struct patmgr_info * rec)
589 {
590     return -(EINVAL);
591 }
592
593 void
594 midi_synth_bender(int dev, int channel, int value)
595 {
596     int             orig_dev = synth_devs[dev]->midi_dev;
597     int             msg, prev_chn;
598
599     if (channel < 0 || channel > 15)
600         return;
601
602     if (value < 0 || value > 16383)
603         return;
604
605     leave_sysex(dev);
606
607     msg = prev_out_status[orig_dev] & 0xf0;
608     prev_chn = prev_out_status[orig_dev] & 0x0f;
609
610     if (msg != 0xd0 || prev_chn != channel) {   /* Test for running status */
611         if (!prefix_cmd(orig_dev, 0xe0 | (channel & 0x0f)))
612             return;
613         midi_outc(orig_dev, 0xe0 | (channel & 0x0f));
614     } else if (!prefix_cmd(orig_dev, value & 0x7f))
615         return;
616
617     midi_outc(orig_dev, value & 0x7f);
618     midi_outc(orig_dev, (value >> 7) & 0x7f);
619 }
620
621 void
622 midi_synth_setup_voice(int dev, int voice, int channel)
623 {
624 }
625
626 int
627 midi_synth_send_sysex(int dev, unsigned char *bytes, int len)
628 {
629     int             orig_dev = synth_devs[dev]->midi_dev;
630     int             i;
631
632     for (i = 0; i < len; i++) {
633         switch (bytes[i]) {
634         case 0xf0:      /* Start sysex */
635             if (!prefix_cmd(orig_dev, 0xf0))
636                 return 0;
637             sysex_state[dev] = 1;
638             break;
639
640         case 0xf7:      /* End sysex */
641             if (!sysex_state[dev])      /* Orphan sysex end */
642                 return 0;
643             sysex_state[dev] = 0;
644             break;
645
646         default:
647             if (!sysex_state[dev])
648                 return 0;
649
650             if (bytes[i] & 0x80) { /* Error. Another message before sysex end */
651                 bytes[i] = 0xf7;        /* Sysex end */
652                 sysex_state[dev] = 0;
653             }
654         }
655
656         if (!midi_devs[orig_dev]->putc(orig_dev, bytes[i])) {
657             /*
658              * Hardware leve buffer is full. Abort the sysex message.
659              */
660
661             int             timeout = 0;
662
663             bytes[i] = 0xf7;
664             sysex_state[dev] = 0;
665
666             while (!midi_devs[orig_dev]->putc(orig_dev, bytes[i]) &&
667                         timeout < 1000)
668                 timeout++;
669         }
670         if (!sysex_state[dev])
671             return 0;
672     }
673
674     return 0;
675 }
676 #endif