2 * Copyright (c) 2003 Mathew Kanner
3 * Copyright (c) 1993 Hannu Savolainen
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * The sequencer personality manager.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/ioccom.h>
39 #include <sys/filio.h>
41 #include <sys/sockio.h>
42 #include <sys/fcntl.h>
44 #include <sys/sysctl.h>
46 #include <sys/kernel.h> /* for DATA_SET */
48 #include <sys/module.h>
52 #include <sys/syslog.h>
53 #include <sys/errno.h>
54 #include <sys/malloc.h>
56 #include <machine/resource.h>
57 #include <machine/bus.h>
58 #include <machine/clock.h> /* for DELAY */
59 #include <sys/soundcard.h>
63 #include <sys/mutex.h>
64 #include <sys/condvar.h>
65 #include <sys/kthread.h>
66 #include <sys/unistd.h>
67 #include <sys/selinfo.h>
70 #include <dev/sound/midi/midi.h>
71 #include <dev/sound/midi/midiq.h>
74 #include <dev/sound/midi/sequencer.h>
76 #define TMR_TIMERBASE 13
78 #define SND_DEV_SEQ 1 /* Sequencer output /dev/sequencer (FM
79 * synthesizer and MIDI output) */
80 #define SND_DEV_MUSIC 8 /* /dev/music, level 2 interface */
82 /* Length of a sequencer event. */
87 #define LOOKUP_EXIST (0)
88 #define LOOKUP_OPEN (1)
89 #define LOOKUP_CLOSE (2)
91 #define PCMMKMINOR(u, d, c) \
92 ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
93 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
94 #define MIDIUNIT(y) ((minor(y) >> 4) & 0x0f)
95 #define MIDIDEV(y) (minor(y) & 0x0f)
97 /* These are the entries to the sequencer driver. */
98 static d_open_t seq_open;
99 static d_close_t seq_close;
100 static d_ioctl_t seq_ioctl;
101 static d_read_t seq_read;
102 static d_write_t seq_write;
103 static d_poll_t seq_poll;
105 static struct cdevsw seq_cdevsw = {
106 .d_version = D_VERSION,
108 .d_close = seq_close,
110 .d_write = seq_write,
111 .d_ioctl = seq_ioctl,
113 .d_name = "sequencer",
119 struct mtx seq_lock, q_lock;
120 struct cv empty_cv, reset_cv, in_cv, out_cv, state_cv, th_cv;
122 MIDIQ_HEAD(, u_char) in_q, out_q;
125 /* Flags (protected by flag_mtx of mididev_info) */
126 int fflags; /* Access mode */
129 int out_water; /* Sequence output threshould */
130 snd_sync_parm sync_parm; /* AIOSYNC parameter set */
131 struct thread *sync_thread; /* AIOSYNCing thread */
132 struct selinfo in_sel, out_sel;
134 struct cdev *seqdev, *musicdev;
141 struct timeval timerstop, timersub;
142 int timerbase, tempo;
148 int pre_event_timeout;
153 * Module specific stuff, including how many sequecers
157 SYSCTL_NODE(_hw_midi, OID_AUTO, seq, CTLFLAG_RD, 0, "Midi sequencer");
160 /* XXX: should this be moved into debug.midi? */
161 SYSCTL_INT(_hw_midi_seq, OID_AUTO, debug, CTLFLAG_RW, &seq_debug, 0, "");
163 midi_cmdtab cmdtab_seqevent[] = {
164 {SEQ_NOTEOFF, "SEQ_NOTEOFF"},
165 {SEQ_NOTEON, "SEQ_NOTEON"},
166 {SEQ_WAIT, "SEQ_WAIT"},
167 {SEQ_PGMCHANGE, "SEQ_PGMCHANGE"},
168 {SEQ_SYNCTIMER, "SEQ_SYNCTIMER"},
169 {SEQ_MIDIPUTC, "SEQ_MIDIPUTC"},
170 {SEQ_DRUMON, "SEQ_DRUMON"},
171 {SEQ_DRUMOFF, "SEQ_DRUMOFF"},
172 {SEQ_ECHO, "SEQ_ECHO"},
173 {SEQ_AFTERTOUCH, "SEQ_AFTERTOUCH"},
174 {SEQ_CONTROLLER, "SEQ_CONTROLLER"},
175 {SEQ_BALANCE, "SEQ_BALANCE"},
176 {SEQ_VOLMODE, "SEQ_VOLMODE"},
177 {SEQ_FULLSIZE, "SEQ_FULLSIZE"},
178 {SEQ_PRIVATE, "SEQ_PRIVATE"},
179 {SEQ_EXTENDED, "SEQ_EXTENDED"},
180 {EV_SEQ_LOCAL, "EV_SEQ_LOCAL"},
181 {EV_TIMING, "EV_TIMING"},
182 {EV_CHN_COMMON, "EV_CHN_COMMON"},
183 {EV_CHN_VOICE, "EV_CHN_VOICE"},
184 {EV_SYSEX, "EV_SYSEX"},
188 midi_cmdtab cmdtab_seqioctl[] = {
189 {SNDCTL_SEQ_RESET, "SNDCTL_SEQ_RESET"},
190 {SNDCTL_SEQ_SYNC, "SNDCTL_SEQ_SYNC"},
191 {SNDCTL_SYNTH_INFO, "SNDCTL_SYNTH_INFO"},
192 {SNDCTL_SEQ_CTRLRATE, "SNDCTL_SEQ_CTRLRATE"},
193 {SNDCTL_SEQ_GETOUTCOUNT, "SNDCTL_SEQ_GETOUTCOUNT"},
194 {SNDCTL_SEQ_GETINCOUNT, "SNDCTL_SEQ_GETINCOUNT"},
195 {SNDCTL_SEQ_PERCMODE, "SNDCTL_SEQ_PERCMODE"},
196 {SNDCTL_FM_LOAD_INSTR, "SNDCTL_FM_LOAD_INSTR"},
197 {SNDCTL_SEQ_TESTMIDI, "SNDCTL_SEQ_TESTMIDI"},
198 {SNDCTL_SEQ_RESETSAMPLES, "SNDCTL_SEQ_RESETSAMPLES"},
199 {SNDCTL_SEQ_NRSYNTHS, "SNDCTL_SEQ_NRSYNTHS"},
200 {SNDCTL_SEQ_NRMIDIS, "SNDCTL_SEQ_NRMIDIS"},
201 {SNDCTL_SEQ_GETTIME, "SNDCTL_SEQ_GETTIME"},
202 {SNDCTL_MIDI_INFO, "SNDCTL_MIDI_INFO"},
203 {SNDCTL_SEQ_THRESHOLD, "SNDCTL_SEQ_THRESHOLD"},
204 {SNDCTL_SYNTH_MEMAVL, "SNDCTL_SYNTH_MEMAVL"},
205 {SNDCTL_FM_4OP_ENABLE, "SNDCTL_FM_4OP_ENABLE"},
206 {SNDCTL_PMGR_ACCESS, "SNDCTL_PMGR_ACCESS"},
207 {SNDCTL_SEQ_PANIC, "SNDCTL_SEQ_PANIC"},
208 {SNDCTL_SEQ_OUTOFBAND, "SNDCTL_SEQ_OUTOFBAND"},
209 {SNDCTL_TMR_TIMEBASE, "SNDCTL_TMR_TIMEBASE"},
210 {SNDCTL_TMR_START, "SNDCTL_TMR_START"},
211 {SNDCTL_TMR_STOP, "SNDCTL_TMR_STOP"},
212 {SNDCTL_TMR_CONTINUE, "SNDCTL_TMR_CONTINUE"},
213 {SNDCTL_TMR_TEMPO, "SNDCTL_TMR_TEMPO"},
214 {SNDCTL_TMR_SOURCE, "SNDCTL_TMR_SOURCE"},
215 {SNDCTL_TMR_METRONOME, "SNDCTL_TMR_METRONOME"},
216 {SNDCTL_TMR_SELECT, "SNDCTL_TMR_SELECT"},
217 {SNDCTL_MIDI_PRETIME, "SNDCTL_MIDI_PRETIME"},
218 {AIONWRITE, "AIONWRITE"},
219 {AIOGSIZE, "AIOGSIZE"},
220 {AIOSSIZE, "AIOSSIZE"},
221 {AIOGFMT, "AIOGFMT"},
222 {AIOSFMT, "AIOSFMT"},
223 {AIOGMIX, "AIOGMIX"},
224 {AIOSMIX, "AIOSMIX"},
225 {AIOSTOP, "AIOSTOP"},
226 {AIOSYNC, "AIOSYNC"},
227 {AIOGCAP, "AIOGCAP"},
231 midi_cmdtab cmdtab_timer[] = {
232 {TMR_WAIT_REL, "TMR_WAIT_REL"},
233 {TMR_WAIT_ABS, "TMR_WAIT_ABS"},
234 {TMR_STOP, "TMR_STOP"},
235 {TMR_START, "TMR_START"},
236 {TMR_CONTINUE, "TMR_CONTINUE"},
237 {TMR_TEMPO, "TMR_TEMPO"},
238 {TMR_ECHO, "TMR_ECHO"},
239 {TMR_CLOCK, "TMR_CLOCK"},
240 {TMR_SPP, "TMR_SPP"},
241 {TMR_TIMESIG, "TMR_TIMESIG"},
245 midi_cmdtab cmdtab_seqcv[] = {
246 {MIDI_NOTEOFF, "MIDI_NOTEOFF"},
247 {MIDI_NOTEON, "MIDI_NOTEON"},
248 {MIDI_KEY_PRESSURE, "MIDI_KEY_PRESSURE"},
252 midi_cmdtab cmdtab_seqccmn[] = {
253 {MIDI_CTL_CHANGE, "MIDI_CTL_CHANGE"},
254 {MIDI_PGM_CHANGE, "MIDI_PGM_CHANGE"},
255 {MIDI_CHN_PRESSURE, "MIDI_CHN_PRESSURE"},
256 {MIDI_PITCH_BEND, "MIDI_PITCH_BEND"},
257 {MIDI_SYSTEM_PREFIX, "MIDI_SYSTEM_PREFIX"},
262 * static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m);
265 static kobj_method_t seq_methods[] = {
266 /* KOBJMETHOD(mpu_provider,mpu401_mprovider), */
270 DEFINE_CLASS(sequencer, seq_methods, 0);
272 /* The followings are the local function. */
273 static int seq_convertold(u_char *event, u_char *out);
276 * static void seq_midiinput(struct seq_softc * scp, void *md);
278 static void seq_reset(struct seq_softc *scp);
279 static int seq_sync(struct seq_softc *scp);
281 static int seq_processevent(struct seq_softc *scp, u_char *event);
283 static int seq_timing(struct seq_softc *scp, u_char *event);
284 static int seq_local(struct seq_softc *scp, u_char *event);
286 static int seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event);
287 static int seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event);
288 static int seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event);
290 static int seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md);
291 void seq_copytoinput(struct seq_softc *scp, u_char *event, int len);
292 int seq_modevent(module_t mod, int type, void *data);
293 struct seq_softc *seqs[10];
294 static struct mtx seqinfo_mtx;
295 static u_long nseq = 0;
297 static void timer_start(struct seq_softc *t);
298 static void timer_stop(struct seq_softc *t);
299 static void timer_setvals(struct seq_softc *t, int tempo, int timerbase);
300 static void timer_wait(struct seq_softc *t, int ticks, int wait_abs);
301 static int timer_now(struct seq_softc *t);
305 timer_start(struct seq_softc *t)
308 getmicrotime(&t->timersub);
312 timer_continue(struct seq_softc *t)
316 if (t->timerrun == 1)
320 timevalsub(&now, &t->timerstop);
321 timevaladd(&t->timersub, &now);
325 timer_stop(struct seq_softc *t)
328 getmicrotime(&t->timerstop);
332 timer_setvals(struct seq_softc *t, int tempo, int timerbase)
335 t->timerbase = timerbase;
339 timer_wait(struct seq_softc *t, int ticks, int wait_abs)
341 struct timeval now, when;
343 unsigned long long i;
345 while (t->timerrun == 0) {
346 SEQ_DEBUG(2, printf("Timer wait when timer isn't running\n"));
348 * The old sequencer used timeouts that only increased
349 * the timer when the timer was running.
350 * Hence the sequencer would stick (?) if the
351 * timer was disabled.
353 cv_wait(&t->reset_cv, &t->seq_lock);
358 i = ticks * 60ull * 1000000ull / (t->tempo * t->timerbase);
360 when.tv_sec = i / 1000000;
361 when.tv_usec = i % 1000000;
364 printf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n",
365 t->tempo, t->timerbase, ticks, wait_abs, i);
370 timevalsub(&now, &t->timersub);
371 timevalsub(&when, &now);
373 if (when.tv_sec < 0 || when.tv_usec < 0) {
375 printf("seq_timer error negative time %lds.%06lds\n",
376 (long)when.tv_sec, (long)when.tv_usec));
379 i = when.tv_sec * 1000000ull;
384 printf("seq_timer usec %llu ticks %llu\n",
385 when.tv_sec * 1000000ull + when.tv_usec, i);
388 ret = cv_timedwait(&t->reset_cv, &t->seq_lock, i + 1);
391 if (ret != EWOULDBLOCK)
392 SEQ_DEBUG(3, printf("seq_timer didn't timeout\n"));
397 timer_now(struct seq_softc *t)
400 unsigned long long i;
403 if (t->timerrun == 0)
408 timevalsub(&now, &t->timersub);
410 i = now.tv_sec * 1000000ull;
418 * printf("timer_now: %llu %d\n", i, ret);
425 seq_eventthread(void *arg)
427 struct seq_softc *scp = arg;
430 mtx_lock(&scp->seq_lock);
431 SEQ_DEBUG(2, printf("seq_eventthread started\n"));
432 while (scp->done == 0) {
434 while (scp->playing == 0) {
435 cv_wait(&scp->state_cv, &scp->seq_lock);
440 while (MIDIQ_EMPTY(scp->out_q)) {
441 cv_broadcast(&scp->empty_cv);
442 cv_wait(&scp->out_cv, &scp->seq_lock);
443 if (scp->playing == 0)
449 MIDIQ_DEQ(scp->out_q, event, EV_SZ);
451 if (MIDIQ_AVAIL(scp->out_q) < scp->out_water) {
452 cv_broadcast(&scp->out_cv);
453 selwakeup(&scp->out_sel);
455 seq_processevent(scp, event);
459 cv_broadcast(&scp->th_cv);
460 mtx_unlock(&scp->seq_lock);
461 SEQ_DEBUG(2, printf("seq_eventthread finished\n"));
466 * seq_processevent: This maybe called by the event thread or the IOCTL
467 * handler for queued and out of band events respectively.
470 seq_processevent(struct seq_softc *scp, u_char *event)
477 if (event[0] == EV_SEQ_LOCAL)
478 ret = seq_local(scp, event);
479 else if (event[0] == EV_TIMING)
480 ret = seq_timing(scp, event);
481 else if (event[0] != EV_CHN_VOICE &&
482 event[0] != EV_CHN_COMMON &&
483 event[0] != EV_SYSEX &&
484 event[0] != SEQ_MIDIPUTC) {
486 SEQ_DEBUG(2, printf("seq_processevent not known %d\n",
488 } else if (seq_fetch_mid(scp, event[1], &m) != 0) {
490 SEQ_DEBUG(2, printf("seq_processevent midi unit not found %d\n",
495 ret = seq_chnvoice(scp, m, event);
498 ret = seq_chncommon(scp, m, event);
501 ret = seq_sysex(scp, m, event);
504 mtx_unlock(&scp->seq_lock);
505 ret = SYNTH_WRITERAW(m, &event[2], 1);
506 mtx_lock(&scp->seq_lock);
515 struct seq_softc *scp;
519 /* Allocate the softc. */
521 scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO);
523 SEQ_DEBUG(1, printf("seq_addunit: softc allocation failed.\n"));
526 kobj_init((kobj_t)scp, &sequencer_class);
528 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
531 MIDIQ_INIT(scp->in_q, buf, EV_SZ * 1024);
532 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
535 MIDIQ_INIT(scp->out_q, buf, EV_SZ * 1024);
538 scp->midis = malloc(sizeof(kobj_t) * 32, M_TEMP, M_NOWAIT | M_ZERO);
539 scp->midi_flags = malloc(sizeof(*scp->midi_flags) * 32, M_TEMP,
542 if (scp->midis == NULL || scp->midi_flags == NULL)
547 mtx_init(&scp->seq_lock, "seqflq", NULL, 0);
548 cv_init(&scp->state_cv, "seqstate");
549 cv_init(&scp->empty_cv, "seqempty");
550 cv_init(&scp->reset_cv, "seqtimer");
551 cv_init(&scp->out_cv, "seqqout");
552 cv_init(&scp->in_cv, "seqqin");
553 cv_init(&scp->th_cv, "seqstart");
556 * Init the damn timer
559 scp->mapper = midimapper_addseq(scp, &scp->unit, &scp->mapper_cookie);
560 if (scp->mapper == NULL)
563 scp->seqdev = make_dev(&seq_cdevsw,
564 MIDIMKMINOR(scp->unit, SND_DEV_SEQ, 0), UID_ROOT,
565 GID_WHEEL, 0666, "sequencer%d", scp->unit);
567 scp->musicdev = make_dev(&seq_cdevsw,
568 MIDIMKMINOR(scp->unit, SND_DEV_MUSIC, 0), UID_ROOT,
569 GID_WHEEL, 0666, "music%d", scp->unit);
571 if (scp->seqdev == NULL || scp->musicdev == NULL)
574 * TODO: Add to list of sequencers this module provides
577 ret = kproc_create(seq_eventthread, scp, NULL, RFHIGHPID, 0,
578 "sequencer %02d", scp->unit);
583 scp->seqdev->si_drv1 = scp->musicdev->si_drv1 = scp;
585 SEQ_DEBUG(2, printf("sequencer %d created scp %p\n", scp->unit, scp));
589 mtx_lock(&seqinfo_mtx);
591 mtx_unlock(&seqinfo_mtx);
597 if (scp->seqdev != NULL)
598 destroy_dev(scp->seqdev);
599 if (scp->musicdev != NULL)
600 destroy_dev(scp->musicdev);
602 * TODO: Destroy mutex and cv
604 if (scp->midis != NULL)
605 free(scp->midis, M_TEMP);
606 if (scp->midi_flags != NULL)
607 free(scp->midi_flags, M_TEMP);
609 free(scp->out_q.b, M_TEMP);
611 free(scp->in_q.b, M_TEMP);
619 seq_delunit(int unit)
621 struct seq_softc *scp = seqs[unit];
624 //SEQ_DEBUG(4, printf("seq_delunit: %d\n", unit));
625 SEQ_DEBUG(1, printf("seq_delunit: 1 \n"));
626 mtx_lock(&scp->seq_lock);
630 cv_broadcast(&scp->out_cv);
631 cv_broadcast(&scp->state_cv);
632 cv_broadcast(&scp->reset_cv);
633 SEQ_DEBUG(1, printf("seq_delunit: 2 \n"));
634 cv_wait(&scp->th_cv, &scp->seq_lock);
635 SEQ_DEBUG(1, printf("seq_delunit: 3.0 \n"));
636 mtx_unlock(&scp->seq_lock);
637 SEQ_DEBUG(1, printf("seq_delunit: 3.1 \n"));
639 cv_destroy(&scp->state_cv);
640 SEQ_DEBUG(1, printf("seq_delunit: 4 \n"));
641 cv_destroy(&scp->empty_cv);
642 SEQ_DEBUG(1, printf("seq_delunit: 5 \n"));
643 cv_destroy(&scp->reset_cv);
644 SEQ_DEBUG(1, printf("seq_delunit: 6 \n"));
645 cv_destroy(&scp->out_cv);
646 SEQ_DEBUG(1, printf("seq_delunit: 7 \n"));
647 cv_destroy(&scp->in_cv);
648 SEQ_DEBUG(1, printf("seq_delunit: 8 \n"));
649 cv_destroy(&scp->th_cv);
651 SEQ_DEBUG(1, printf("seq_delunit: 10 \n"));
653 destroy_dev(scp->seqdev);
654 SEQ_DEBUG(1, printf("seq_delunit: 11 \n"));
656 destroy_dev(scp->musicdev);
657 SEQ_DEBUG(1, printf("seq_delunit: 12 \n"));
658 scp->seqdev = scp->musicdev = NULL;
659 if (scp->midis != NULL)
660 free(scp->midis, M_TEMP);
661 SEQ_DEBUG(1, printf("seq_delunit: 13 \n"));
662 if (scp->midi_flags != NULL)
663 free(scp->midi_flags, M_TEMP);
664 SEQ_DEBUG(1, printf("seq_delunit: 14 \n"));
665 free(scp->out_q.b, M_TEMP);
666 SEQ_DEBUG(1, printf("seq_delunit: 15 \n"));
667 free(scp->in_q.b, M_TEMP);
669 SEQ_DEBUG(1, printf("seq_delunit: 16 \n"));
671 mtx_destroy(&scp->seq_lock);
672 SEQ_DEBUG(1, printf("seq_delunit: 17 \n"));
675 mtx_lock(&seqinfo_mtx);
676 for (i = unit; i < (nseq - 1); i++)
677 seqs[i] = seqs[i + 1];
679 mtx_unlock(&seqinfo_mtx);
685 seq_modevent(module_t mod, int type, void *data)
693 mtx_init(&seqinfo_mtx, "seqmod", NULL, 0);
694 retval = seq_addunit();
699 r = seq_delunit(nseq - 1);
707 mtx_destroy(&seqinfo_mtx);
719 seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md)
722 if (unit > scp->midi_number || unit < 0)
725 *md = scp->midis[unit];
731 seq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
733 struct seq_softc *scp = i_dev->si_drv1;
739 SEQ_DEBUG(3, printf("seq_open: scp %p unit %d, flags 0x%x.\n",
740 scp, scp->unit, flags));
743 * Mark this device busy.
746 mtx_lock(&scp->seq_lock);
748 mtx_unlock(&scp->seq_lock);
749 SEQ_DEBUG(2, printf("seq_open: unit %d is busy.\n", scp->unit));
754 if ((scp->fflags & O_NONBLOCK) != 0)
755 scp->flags |= SEQ_F_NBIO;
757 scp->music = MIDIDEV(i_dev) == SND_DEV_MUSIC;
760 * Enumerate the available midi devices
762 scp->midi_number = 0;
763 scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie);
765 if (scp->maxunits == 0)
766 SEQ_DEBUG(2, printf("seq_open: no midi devices\n"));
768 for (i = 0; i < scp->maxunits; i++) {
769 scp->midis[scp->midi_number] =
770 midimapper_fetch_synth(scp->mapper, scp->mapper_cookie, i);
771 if (scp->midis[scp->midi_number]) {
772 if (SYNTH_OPEN(scp->midis[scp->midi_number], scp,
774 scp->midis[scp->midi_number] = NULL;
776 scp->midi_flags[scp->midi_number] =
777 SYNTH_QUERY(scp->midis[scp->midi_number]);
783 timer_setvals(scp, 60, 100);
788 * actually, if we're in rdonly mode, we should start the timer
791 * TODO: Handle recording now
794 scp->out_water = MIDIQ_SIZE(scp->out_q) / 2;
797 mtx_unlock(&scp->seq_lock);
799 SEQ_DEBUG(2, printf("seq_open: opened, mode %s.\n",
800 scp->music ? "music" : "sequencer"));
802 printf("Sequencer %d %p opened maxunits %d midi_number %d:\n",
803 scp->unit, scp, scp->maxunits, scp->midi_number));
804 for (i = 0; i < scp->midi_number; i++)
805 SEQ_DEBUG(3, printf(" midi %d %p\n", i, scp->midis[i]));
814 seq_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
817 struct seq_softc *scp = i_dev->si_drv1;
823 SEQ_DEBUG(2, printf("seq_close: unit %d.\n", scp->unit));
825 mtx_lock(&scp->seq_lock);
834 for (i = 0; i < scp->midi_number; i++)
836 SYNTH_CLOSE(scp->midis[i]);
838 midimapper_close(scp->mapper, scp->mapper_cookie);
846 SEQ_DEBUG(3, printf("seq_close: closed ret = %d.\n", ret));
847 mtx_unlock(&scp->seq_lock);
852 seq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
855 struct seq_softc *scp = i_dev->si_drv1;
858 u_char buf[SEQ_RSIZE];
863 SEQ_DEBUG(7, printf("seq_read: unit %d, resid %d.\n",
864 scp->unit, uio->uio_resid));
866 mtx_lock(&scp->seq_lock);
867 if ((scp->fflags & FREAD) == 0) {
868 SEQ_DEBUG(2, printf("seq_read: unit %d is not for reading.\n",
877 * if ((scp->flags & SEQ_F_READING) == 0)
880 * TODO, start recording if not alread
884 * I think the semantics are to return as soon
886 * Second thought, it doens't seem like midimoutain
887 * expects that at all.
888 * TODO: Look up in some sort of spec
891 while (uio->uio_resid > 0) {
892 while (MIDIQ_EMPTY(scp->in_q)) {
893 retval = EWOULDBLOCK;
895 * I wish I knew which one to care about
898 if (scp->fflags & O_NONBLOCK)
900 if (ioflag & O_NONBLOCK)
903 retval = cv_wait_sig(&scp->in_cv, &scp->seq_lock);
908 used = MIN(MIDIQ_LEN(scp->in_q), uio->uio_resid);
909 used = MIN(used, SEQ_RSIZE);
911 SEQ_DEBUG(8, printf("midiread: uiomove cc=%d\n", used));
912 MIDIQ_DEQ(scp->in_q, buf, used);
913 retval = uiomove(buf, used, uio);
920 mtx_unlock(&scp->seq_lock);
921 SEQ_DEBUG(6, printf("seq_read: ret %d, resid %d.\n",
922 retval, uio->uio_resid));
928 seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
930 u_char event[EV_SZ], newevent[EV_SZ], ev_code;
931 struct seq_softc *scp = i_dev->si_drv1;
935 SEQ_DEBUG(7, printf("seq_write: unit %d, resid %d.\n",
936 scp->unit, uio->uio_resid));
941 mtx_lock(&scp->seq_lock);
943 if ((scp->fflags & FWRITE) == 0) {
944 SEQ_DEBUG(2, printf("seq_write: unit %d is not for writing.\n",
949 while (uio->uio_resid > 0) {
950 while (MIDIQ_AVAIL(scp->out_q) == 0) {
951 retval = EWOULDBLOCK;
952 if (scp->fflags & O_NONBLOCK)
954 if (ioflag & O_NONBLOCK)
956 SEQ_DEBUG(8, printf("seq_write cvwait\n"));
959 cv_broadcast(&scp->out_cv);
960 cv_broadcast(&scp->state_cv);
962 retval = cv_wait_sig(&scp->out_cv, &scp->seq_lock);
964 * We slept, maybe things have changed since last
973 if (scp != i_dev->si_drv1)
978 used = MIN(uio->uio_resid, 4);
980 SEQ_DEBUG(8, printf("seqout: resid %d len %jd avail %jd\n",
981 uio->uio_resid, (intmax_t)MIDIQ_LEN(scp->out_q),
982 (intmax_t)MIDIQ_AVAIL(scp->out_q)));
988 retval = uiomove(event, used, uio);
993 SEQ_DEBUG(8, printf("seq_write: unit %d, event %s.\n",
994 scp->unit, midi_cmdname(ev_code, cmdtab_seqevent)));
996 /* Have a look at the event code. */
997 if (ev_code == SEQ_FULLSIZE) {
1000 * TODO: restore code for SEQ_FULLSIZE
1004 * A long event, these are the patches/samples for a
1007 midiunit = *(u_short *)&event[2];
1008 mtx_lock(&sd->seq_lock);
1009 ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
1010 mtx_unlock(&sd->seq_lock);
1014 SEQ_DEBUG(printf("seq_write: loading a patch to the unit %d.\n", midiunit));
1016 ret = md->synth.loadpatch(md, *(short *)&event[0], buf,
1021 * For now, just flush the darn buffer
1024 printf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
1025 while (uio->uio_resid > 0) {
1026 retval = uiomove(event, EV_SZ, uio);
1036 if (ev_code >= 128) {
1039 * Some sort of an extended event. The size is eight
1040 * bytes. scoop extra info.
1042 if (scp->music && ev_code == SEQ_EXTENDED) {
1043 SEQ_DEBUG(2, printf("seq_write: invalid level two event %x.\n", ev_code));
1046 if (uiomove((caddr_t)&event[4], 4, uio)) {
1048 printf("seq_write: user memory mangled?\n"));
1056 SEQ_DEBUG(2, printf("seq_write: four byte event in music mode.\n"));
1060 if (ev_code == SEQ_MIDIPUTC) {
1062 * TODO: event[2] is unit number to receive char.
1067 #ifdef not_ever_ever
1068 if (event[0] == EV_TIMING &&
1069 (event[1] == TMR_START || event[1] == TMR_STOP)) {
1071 * For now, try to make midimoutain work by
1072 * forcing these events to be processed
1075 seq_processevent(scp, event);
1077 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1079 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1082 if (seq_convertold(event, newevent) > 0)
1083 MIDIQ_ENQ(scp->out_q, newevent, EV_SZ);
1093 cv_broadcast(&scp->state_cv);
1094 cv_broadcast(&scp->out_cv);
1100 printf("seq_write done: leftover buffer length %d retval %d\n",
1101 uio->uio_resid, retval));
1102 mtx_unlock(&scp->seq_lock);
1107 seq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1110 int midiunit, ret, tmp;
1111 struct seq_softc *scp = i_dev->si_drv1;
1112 struct synth_info *synthinfo;
1113 struct midi_info *midiinfo;
1114 u_char event[EV_SZ];
1115 u_char newevent[EV_SZ];
1120 * struct snd_size *sndsize;
1126 SEQ_DEBUG(6, printf("seq_ioctl: unit %d, cmd %s.\n",
1127 scp->unit, midi_cmdname(cmd, cmdtab_seqioctl)));
1132 case SNDCTL_SEQ_GETTIME:
1134 * ioctl needed by libtse
1136 mtx_lock(&scp->seq_lock);
1137 *(int *)arg = timer_now(scp);
1138 mtx_unlock(&scp->seq_lock);
1139 SEQ_DEBUG(6, printf("seq_ioctl: gettime %d.\n", *(int *)arg));
1142 case SNDCTL_TMR_METRONOME:
1144 case SNDCTL_TMR_SOURCE:
1150 case SNDCTL_TMR_TEMPO:
1151 event[1] = TMR_TEMPO;
1152 event[4] = *(int *)arg & 0xFF;
1153 event[5] = (*(int *)arg >> 8) & 0xFF;
1154 event[6] = (*(int *)arg >> 16) & 0xFF;
1155 event[7] = (*(int *)arg >> 24) & 0xFF;
1157 case SNDCTL_TMR_TIMEBASE:
1158 event[1] = TMR_TIMERBASE;
1159 event[4] = *(int *)arg & 0xFF;
1160 event[5] = (*(int *)arg >> 8) & 0xFF;
1161 event[6] = (*(int *)arg >> 16) & 0xFF;
1162 event[7] = (*(int *)arg >> 24) & 0xFF;
1164 case SNDCTL_TMR_START:
1165 event[1] = TMR_START;
1167 case SNDCTL_TMR_STOP:
1168 event[1] = TMR_STOP;
1170 case SNDCTL_TMR_CONTINUE:
1171 event[1] = TMR_CONTINUE;
1173 event[0] = EV_TIMING;
1174 mtx_lock(&scp->seq_lock);
1177 mtx_unlock(&scp->seq_lock);
1180 seq_processevent(scp, event);
1181 mtx_unlock(&scp->seq_lock);
1183 case SNDCTL_TMR_SELECT:
1185 printf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
1188 case SNDCTL_SEQ_SYNC:
1189 if (mode == O_RDONLY) {
1193 mtx_lock(&scp->seq_lock);
1194 ret = seq_sync(scp);
1195 mtx_unlock(&scp->seq_lock);
1197 case SNDCTL_SEQ_PANIC:
1199 case SNDCTL_SEQ_RESET:
1201 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
1203 mtx_lock(&scp->seq_lock);
1205 mtx_unlock(&scp->seq_lock);
1208 case SNDCTL_SEQ_TESTMIDI:
1209 mtx_lock(&scp->seq_lock);
1211 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
1214 mtx_unlock(&scp->seq_lock);
1217 case SNDCTL_SEQ_GETINCOUNT:
1218 if (mode == O_WRONLY)
1221 mtx_lock(&scp->seq_lock);
1222 *(int *)arg = scp->in_q.rl;
1223 mtx_unlock(&scp->seq_lock);
1224 SEQ_DEBUG(printf("seq_ioctl: incount %d.\n",
1229 case SNDCTL_SEQ_GETOUTCOUNT:
1230 if (mode == O_RDONLY)
1233 mtx_lock(&scp->seq_lock);
1234 *(int *)arg = scp->out_q.fl;
1235 mtx_unlock(&scp->seq_lock);
1236 SEQ_DEBUG(printf("seq_ioctl: outcount %d.\n",
1242 case SNDCTL_SEQ_CTRLRATE:
1243 if (*(int *)arg != 0) {
1247 mtx_lock(&scp->seq_lock);
1248 *(int *)arg = scp->timerbase;
1249 mtx_unlock(&scp->seq_lock);
1250 SEQ_DEBUG(3, printf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
1254 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES
1257 case SNDCTL_SEQ_RESETSAMPLES:
1258 mtx_lock(&scp->seq_lock);
1259 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1260 mtx_unlock(&scp->seq_lock);
1263 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1264 SND_DEV_MIDIN), cmd, arg, mode, td);
1267 case SNDCTL_SEQ_NRSYNTHS:
1268 mtx_lock(&scp->seq_lock);
1269 *(int *)arg = scp->midi_number;
1270 mtx_unlock(&scp->seq_lock);
1271 SEQ_DEBUG(3, printf("seq_ioctl: synths %d.\n", *(int *)arg));
1274 case SNDCTL_SEQ_NRMIDIS:
1275 mtx_lock(&scp->seq_lock);
1280 * TODO: count the numbder of devices that can WRITERAW
1282 *(int *)arg = scp->midi_number;
1284 mtx_unlock(&scp->seq_lock);
1285 SEQ_DEBUG(3, printf("seq_ioctl: midis %d.\n", *(int *)arg));
1289 * TODO: ioctl SNDCTL_SYNTH_MEMAVL
1292 case SNDCTL_SYNTH_MEMAVL:
1293 mtx_lock(&scp->seq_lock);
1294 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1295 mtx_unlock(&scp->seq_lock);
1298 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1299 SND_DEV_MIDIN), cmd, arg, mode, td);
1302 case SNDCTL_SEQ_OUTOFBAND:
1303 for (ret = 0; ret < EV_SZ; ret++)
1304 event[ret] = (u_char)arg[0];
1306 mtx_lock(&scp->seq_lock);
1308 ret = seq_processevent(scp, event);
1310 if (seq_convertold(event, newevent) > 0)
1311 ret = seq_processevent(scp, newevent);
1315 mtx_unlock(&scp->seq_lock);
1317 case SNDCTL_SYNTH_INFO:
1318 synthinfo = (struct synth_info *)arg;
1319 midiunit = synthinfo->device;
1320 mtx_lock(&scp->seq_lock);
1321 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1322 bzero(synthinfo, sizeof(*synthinfo));
1323 synthinfo->name[0] = 'f';
1324 synthinfo->name[1] = 'a';
1325 synthinfo->name[2] = 'k';
1326 synthinfo->name[3] = 'e';
1327 synthinfo->name[4] = 's';
1328 synthinfo->name[5] = 'y';
1329 synthinfo->name[6] = 'n';
1330 synthinfo->name[7] = 't';
1331 synthinfo->name[8] = 'h';
1332 synthinfo->device = midiunit;
1333 synthinfo->synth_type = SYNTH_TYPE_MIDI;
1334 synthinfo->capabilities = scp->midi_flags[midiunit];
1338 mtx_unlock(&scp->seq_lock);
1340 case SNDCTL_MIDI_INFO:
1341 midiinfo = (struct midi_info *)arg;
1342 midiunit = midiinfo->device;
1343 mtx_lock(&scp->seq_lock);
1344 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1345 bzero(midiinfo, sizeof(*midiinfo));
1346 midiinfo->name[0] = 'f';
1347 midiinfo->name[1] = 'a';
1348 midiinfo->name[2] = 'k';
1349 midiinfo->name[3] = 'e';
1350 midiinfo->name[4] = 'm';
1351 midiinfo->name[5] = 'i';
1352 midiinfo->name[6] = 'd';
1353 midiinfo->name[7] = 'i';
1354 midiinfo->device = midiunit;
1355 midiinfo->capabilities = scp->midi_flags[midiunit];
1357 * TODO: What devtype?
1359 midiinfo->dev_type = 0x01;
1363 mtx_unlock(&scp->seq_lock);
1365 case SNDCTL_SEQ_THRESHOLD:
1366 mtx_lock(&scp->seq_lock);
1367 RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1);
1368 scp->out_water = *(int *)arg;
1369 mtx_unlock(&scp->seq_lock);
1370 SEQ_DEBUG(3, printf("seq_ioctl: water %d.\n", *(int *)arg));
1373 case SNDCTL_MIDI_PRETIME:
1377 mtx_lock(&scp->seq_lock);
1378 scp->pre_event_timeout = (hz * tmp) / 10;
1379 *(int *)arg = scp->pre_event_timeout;
1380 mtx_unlock(&scp->seq_lock);
1381 SEQ_DEBUG(3, printf("seq_ioctl: pretime %d.\n", *(int *)arg));
1384 case SNDCTL_FM_4OP_ENABLE:
1385 case SNDCTL_PMGR_IFACE:
1386 case SNDCTL_PMGR_ACCESS:
1388 * Patch manager and fm are ded, ded, ded.
1393 * TODO: Consider ioctl default case.
1395 * if ((scp->fflags & O_ACCMODE) == FREAD) {
1399 * Then pass on the ioctl to device 0
1402 printf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
1411 seq_poll(struct cdev *i_dev, int events, struct thread *td)
1414 struct seq_softc *scp = i_dev->si_drv1;
1416 SEQ_DEBUG(3, printf("seq_poll: unit %d.\n", scp->unit));
1417 SEQ_DEBUG(1, printf("seq_poll: unit %d.\n", scp->unit));
1419 mtx_lock(&scp->seq_lock);
1423 /* Look up the apropriate queue and select it. */
1424 if ((events & (POLLOUT | POLLWRNORM)) != 0) {
1425 /* Start playing. */
1427 cv_broadcast(&scp->state_cv);
1428 cv_broadcast(&scp->out_cv);
1430 lim = scp->out_water;
1432 if (MIDIQ_AVAIL(scp->out_q) < lim)
1433 /* No enough space, record select. */
1434 selrecord(td, &scp->out_sel);
1436 /* We can write now. */
1437 ret |= events & (POLLOUT | POLLWRNORM);
1439 if ((events & (POLLIN | POLLRDNORM)) != 0) {
1440 /* TODO: Start recording. */
1442 /* Find out the boundary. */
1444 if (MIDIQ_LEN(scp->in_q) < lim)
1445 /* No data ready, record select. */
1446 selrecord(td, &scp->in_sel);
1448 /* We can read now. */
1449 ret |= events & (POLLIN | POLLRDNORM);
1451 mtx_unlock(&scp->seq_lock);
1458 sein_qtr(void *p, void /* mididev_info */ *md)
1460 struct seq_softc *scp;
1462 scp = (struct seq_softc *)p;
1464 mtx_lock(&scp->seq_lock);
1466 /* Restart playing if we have the data to output. */
1467 if (scp->queueout_pending)
1468 seq_callback(scp, SEQ_CB_START | SEQ_CB_WR);
1469 /* Check the midi device if we are reading. */
1470 if ((scp->flags & SEQ_F_READING) != 0)
1471 seq_midiinput(scp, md);
1473 mtx_unlock(&scp->seq_lock);
1479 * Was the old playevent. Use this to convert and old
1480 * style /dev/sequencer event to a /dev/music event
1483 seq_convertold(u_char *event, u_char *out)
1486 u_char dev, chn, note, vel;
1488 out[0] = out[1] = out[2] = out[3] = out[4] = out[5] = out[6] =
1500 * TODO: Debug statement
1519 out[0] = EV_CHN_VOICE;
1521 out[2] = MIDI_NOTEOFF;
1529 out[0] = EV_CHN_VOICE;
1531 out[2] = MIDI_NOTEON;
1539 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4]
1543 out[0] = EV_CHN_COMMON;
1545 out[2] = MIDI_PGM_CHANGE;
1554 out[2] = MIDI_PGM_CHANGE;
1558 SEQ_DEBUG(4,printf("seq_playevent: synctimer\n"));
1564 printf("seq_playevent: put data 0x%02x, unit %d.\n",
1565 event[1], event[2]));
1567 * Pass through to the midi device.
1571 out[0] = SEQ_MIDIPUTC;
1579 * This isn't handled here yet because I don't know if I can
1580 * just use four bytes events. There might be consequences
1581 * in the _read routing
1583 if (seq_copytoinput(scp, event, 4) == EAGAIN) {
1599 case SEQ_AFTERTOUCH:
1601 * SYNTH_AFTERTOUCH(md, event[3], event[4])
1605 * SYNTH_PANNING(md, event[3], (char)event[4])
1607 case SEQ_CONTROLLER:
1609 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5])
1613 * SYNTH_VOLUMEMETHOD(md, event[3])
1617 printf("seq_convertold: SEQ_EXTENDED type %d"
1618 "not handled\n", event[1]));
1624 out[1] = TMR_WAIT_REL;
1629 SEQ_DEBUG(5, printf("SEQ_WAIT %d",
1630 event[2] + (event[3] << 8) + (event[4] << 24)));
1640 printf("seq_convertold: event type %d not handled %d %d %d\n",
1641 event[0], event[1], event[2], event[3]));
1648 * Writting to the sequencer buffer never blocks and drops
1649 * input which cannot be queued
1652 seq_copytoinput(struct seq_softc *scp, u_char *event, int len)
1655 mtx_assert(&scp->seq_lock, MA_OWNED);
1657 if (MIDIQ_AVAIL(scp->in_q) < len) {
1659 * ENOROOM? EINPUTDROPPED? ETOUGHLUCK?
1661 SEQ_DEBUG(2, printf("seq_copytoinput: queue full\n"));
1663 MIDIQ_ENQ(scp->in_q, event, len);
1664 selwakeup(&scp->in_sel);
1665 cv_broadcast(&scp->in_cv);
1671 seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
1674 u_char cmd, chn, note, parm;
1682 mtx_assert(&scp->seq_lock, MA_OWNED);
1684 SEQ_DEBUG(5, printf("seq_chnvoice: unit %d, dev %d, cmd %s,"
1685 " chn %d, note %d, parm %d.\n", scp->unit, event[1],
1686 midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm));
1688 voice = SYNTH_ALLOC(md, chn, note);
1690 mtx_unlock(&scp->seq_lock);
1694 if (note < 128 || note == 255) {
1696 if (scp->music && chn == 9) {
1698 * This channel is a percussion. The note
1699 * number is the patch number.
1702 mtx_unlock(&scp->seq_lock);
1703 if (SYNTH_SETINSTR(md, voice, 128 + note)
1705 mtx_lock(&scp->seq_lock);
1708 mtx_lock(&scp->seq_lock);
1710 note = 60; /* Middle C. */
1715 mtx_unlock(&scp->seq_lock);
1716 if (SYNTH_SETUPVOICE(md, voice, chn)
1718 mtx_lock(&scp->seq_lock);
1721 mtx_lock(&scp->seq_lock);
1724 SYNTH_STARTNOTE(md, voice, note, parm);
1728 SYNTH_KILLNOTE(md, voice, note, parm);
1730 case MIDI_KEY_PRESSURE:
1731 SYNTH_AFTERTOUCH(md, voice, parm);
1735 SEQ_DEBUG(2, printf("seq_chnvoice event type %d not handled\n",
1740 mtx_lock(&scp->seq_lock);
1745 seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
1749 u_char cmd, chn, p1;
1755 w14 = *(u_short *)&event[6];
1757 SEQ_DEBUG(5, printf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
1758 " p1 %d, w14 %d.\n", scp->unit, event[1],
1759 midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14));
1760 mtx_unlock(&scp->seq_lock);
1762 case MIDI_PGM_CHANGE:
1763 SEQ_DEBUG(4, printf("seq_chncommon pgmchn chn %d pg %d\n",
1765 SYNTH_SETINSTR(md, chn, p1);
1767 case MIDI_CTL_CHANGE:
1768 SEQ_DEBUG(4, printf("seq_chncommon ctlch chn %d pg %d %d\n",
1770 SYNTH_CONTROLLER(md, chn, p1, w14);
1772 case MIDI_PITCH_BEND:
1775 * TODO: MIDI_PITCH_BEND
1778 mtx_lock(&md->synth.vc_mtx);
1779 md->synth.chn_info[chn].bender_value = w14;
1780 if (md->midiunit >= 0) {
1782 * Handle all of the notes playing on this
1785 key = ((int)chn << 8);
1786 for (i = 0; i < md->synth.alloc.max_voice; i++)
1787 if ((md->synth.alloc.map[i] & 0xff00) == key) {
1788 mtx_unlock(&md->synth.vc_mtx);
1789 mtx_unlock(&scp->seq_lock);
1790 if (md->synth.bender(md, i, w14) == EAGAIN) {
1791 mtx_lock(&scp->seq_lock);
1794 mtx_lock(&scp->seq_lock);
1797 mtx_unlock(&md->synth.vc_mtx);
1798 mtx_unlock(&scp->seq_lock);
1799 if (md->synth.bender(md, chn, w14) == EAGAIN) {
1800 mtx_lock(&scp->seq_lock);
1803 mtx_lock(&scp->seq_lock);
1807 SYNTH_BENDER(md, chn, w14);
1812 printf("seq_chncommon event type %d not handled.\n",
1817 mtx_lock(&scp->seq_lock);
1822 seq_timing(struct seq_softc *scp, u_char *event)
1828 param = event[4] + (event[5] << 8) +
1829 (event[6] << 16) + (event[7] << 24);
1831 SEQ_DEBUG(5, printf("seq_timing: unit %d, cmd %d, param %d.\n",
1832 scp->unit, event[1], param));
1835 timer_wait(scp, param, 0);
1838 timer_wait(scp, param, 1);
1842 cv_broadcast(&scp->reset_cv);
1847 * The following cv_broadcast isn't needed since we only
1848 * wait for 0->1 transitions. It probably won't hurt
1850 cv_broadcast(&scp->reset_cv);
1853 timer_continue(scp);
1854 cv_broadcast(&scp->reset_cv);
1861 SEQ_DEBUG(4, printf("Timer set tempo %d\n", param));
1862 timer_setvals(scp, param, scp->timerbase);
1869 SEQ_DEBUG(4, printf("Timer set timerbase %d\n", param));
1870 timer_setvals(scp, scp->tempo, param);
1874 * TODO: Consider making 4-byte events for /dev/sequencer
1875 * PRO: Maybe needed by legacy apps
1876 * CON: soundcard.h has been warning for a while many years
1877 * to expect 8 byte events.
1881 seq_copytoinput(scp, event, 8);
1883 param = (param << 8 | SEQ_ECHO);
1884 seq_copytoinput(scp, (u_char *)¶m, 4);
1887 seq_copytoinput(scp, event, 8);
1891 SEQ_DEBUG(2, printf("seq_timing event type %d not handled.\n",
1900 seq_local(struct seq_softc *scp, u_char *event)
1905 mtx_assert(&scp->seq_lock, MA_OWNED);
1907 SEQ_DEBUG(5, printf("seq_local: unit %d, cmd %d\n", scp->unit,
1911 SEQ_DEBUG(1, printf("seq_local event type %d not handled\n",
1920 seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event)
1924 mtx_assert(&scp->seq_lock, MA_OWNED);
1925 SEQ_DEBUG(5, printf("seq_sysex: unit %d device %d\n", scp->unit,
1928 for (i = 0; i < 6 && event[i + 2] != 0xff; i++)
1931 mtx_unlock(&scp->seq_lock);
1932 if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) {
1933 mtx_lock(&scp->seq_lock);
1936 mtx_lock(&scp->seq_lock);
1942 * Reset no longer closes the raw devices nor seq_sync's
1943 * Callers are IOCTL and seq_close
1946 seq_reset(struct seq_softc *scp)
1951 mtx_assert(&scp->seq_lock, MA_OWNED);
1953 SEQ_DEBUG(5, printf("seq_reset: unit %d.\n", scp->unit));
1956 * Stop reading and writing.
1959 /* scp->recording = 0; */
1961 cv_broadcast(&scp->state_cv);
1962 cv_broadcast(&scp->out_cv);
1963 cv_broadcast(&scp->reset_cv);
1966 * For now, don't reset the timers.
1968 MIDIQ_CLEAR(scp->in_q);
1969 MIDIQ_CLEAR(scp->out_q);
1971 for (i = 0; i < scp->midi_number; i++) {
1973 mtx_unlock(&scp->seq_lock);
1975 for (chn = 0; chn < 16; chn++) {
1976 SYNTH_CONTROLLER(m, chn, 123, 0);
1977 SYNTH_CONTROLLER(m, chn, 121, 0);
1978 SYNTH_BENDER(m, chn, 1 << 13);
1980 mtx_lock(&scp->seq_lock);
1986 * *really* flush the output queue
1987 * flush the event queue, then flush the synthsisers.
1988 * Callers are IOCTL and close
1991 #define SEQ_SYNC_TIMEOUT 8
1993 seq_sync(struct seq_softc *scp)
1995 int i, rl, sync[16], done;
1997 mtx_assert(&scp->seq_lock, MA_OWNED);
1999 SEQ_DEBUG(4, printf("seq_sync: unit %d.\n", scp->unit));
2002 * Wait until output queue is empty. Check every so often to see if
2003 * the queue is moving along. If it isn't just abort.
2005 while (!MIDIQ_EMPTY(scp->out_q)) {
2007 if (!scp->playing) {
2009 cv_broadcast(&scp->state_cv);
2010 cv_broadcast(&scp->out_cv);
2012 rl = MIDIQ_LEN(scp->out_q);
2014 i = cv_timedwait_sig(&scp->out_cv,
2015 &scp->seq_lock, SEQ_SYNC_TIMEOUT * hz);
2017 if (i == EINTR || i == ERESTART) {
2020 * XXX: I don't know why we stop playing
2023 cv_broadcast(&scp->out_cv);
2027 if (i == EWOULDBLOCK && rl == MIDIQ_LEN(scp->out_q) &&
2028 scp->waiting == 0) {
2030 * A queue seems to be stuck up. Give up and clear
2033 MIDIQ_CLEAR(scp->out_q);
2035 cv_broadcast(&scp->state_cv);
2036 cv_broadcast(&scp->out_cv);
2037 cv_broadcast(&scp->reset_cv);
2040 * TODO: Consider if the raw devices need to be flushed
2043 SEQ_DEBUG(1, printf("seq_sync queue stuck, aborting\n"));
2051 * Since syncing a midi device might block, unlock scp->seq_lock.
2054 mtx_unlock(&scp->seq_lock);
2055 for (i = 0; i < scp->midi_number; i++)
2060 for (i = 0; i < scp->midi_number; i++)
2062 if (SYNTH_INSYNC(scp->midis[i]) == 0)
2072 mtx_lock(&scp->seq_lock);
2077 midi_cmdname(int cmd, midi_cmdtab *tab)
2079 while (tab->name != NULL) {
2080 if (cmd == tab->cmd)