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>
69 #ifdef HAVE_KERNEL_OPTION_HEADERS
73 #include <dev/sound/midi/midi.h>
74 #include <dev/sound/midi/midiq.h>
77 #include <dev/sound/midi/sequencer.h>
79 #define TMR_TIMERBASE 13
81 #define SND_DEV_SEQ 1 /* Sequencer output /dev/sequencer (FM
82 * synthesizer and MIDI output) */
83 #define SND_DEV_MUSIC 8 /* /dev/music, level 2 interface */
85 /* Length of a sequencer event. */
90 #define LOOKUP_EXIST (0)
91 #define LOOKUP_OPEN (1)
92 #define LOOKUP_CLOSE (2)
94 #define PCMMKMINOR(u, d, c) \
95 ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
96 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
97 #define MIDIUNIT(y) ((dev2unit(y) >> 4) & 0x0f)
98 #define MIDIDEV(y) (dev2unit(y) & 0x0f)
100 /* These are the entries to the sequencer driver. */
101 static d_open_t mseq_open;
102 static d_close_t mseq_close;
103 static d_ioctl_t mseq_ioctl;
104 static d_read_t mseq_read;
105 static d_write_t mseq_write;
106 static d_poll_t mseq_poll;
108 static struct cdevsw seq_cdevsw = {
109 .d_version = D_VERSION,
111 .d_close = mseq_close,
113 .d_write = mseq_write,
114 .d_ioctl = mseq_ioctl,
116 .d_name = "sequencer",
122 struct mtx seq_lock, q_lock;
123 struct cv empty_cv, reset_cv, in_cv, out_cv, state_cv, th_cv;
125 MIDIQ_HEAD(, u_char) in_q, out_q;
128 /* Flags (protected by flag_mtx of mididev_info) */
129 int fflags; /* Access mode */
132 int out_water; /* Sequence output threshould */
133 snd_sync_parm sync_parm; /* AIOSYNC parameter set */
134 struct thread *sync_thread; /* AIOSYNCing thread */
135 struct selinfo in_sel, out_sel;
137 struct cdev *seqdev, *musicdev;
144 struct timeval timerstop, timersub;
145 int timerbase, tempo;
151 int pre_event_timeout;
156 * Module specific stuff, including how many sequecers
160 SYSCTL_NODE(_hw_midi, OID_AUTO, seq, CTLFLAG_RD, 0, "Midi sequencer");
163 /* XXX: should this be moved into debug.midi? */
164 SYSCTL_INT(_hw_midi_seq, OID_AUTO, debug, CTLFLAG_RW, &seq_debug, 0, "");
166 midi_cmdtab cmdtab_seqevent[] = {
167 {SEQ_NOTEOFF, "SEQ_NOTEOFF"},
168 {SEQ_NOTEON, "SEQ_NOTEON"},
169 {SEQ_WAIT, "SEQ_WAIT"},
170 {SEQ_PGMCHANGE, "SEQ_PGMCHANGE"},
171 {SEQ_SYNCTIMER, "SEQ_SYNCTIMER"},
172 {SEQ_MIDIPUTC, "SEQ_MIDIPUTC"},
173 {SEQ_DRUMON, "SEQ_DRUMON"},
174 {SEQ_DRUMOFF, "SEQ_DRUMOFF"},
175 {SEQ_ECHO, "SEQ_ECHO"},
176 {SEQ_AFTERTOUCH, "SEQ_AFTERTOUCH"},
177 {SEQ_CONTROLLER, "SEQ_CONTROLLER"},
178 {SEQ_BALANCE, "SEQ_BALANCE"},
179 {SEQ_VOLMODE, "SEQ_VOLMODE"},
180 {SEQ_FULLSIZE, "SEQ_FULLSIZE"},
181 {SEQ_PRIVATE, "SEQ_PRIVATE"},
182 {SEQ_EXTENDED, "SEQ_EXTENDED"},
183 {EV_SEQ_LOCAL, "EV_SEQ_LOCAL"},
184 {EV_TIMING, "EV_TIMING"},
185 {EV_CHN_COMMON, "EV_CHN_COMMON"},
186 {EV_CHN_VOICE, "EV_CHN_VOICE"},
187 {EV_SYSEX, "EV_SYSEX"},
191 midi_cmdtab cmdtab_seqioctl[] = {
192 {SNDCTL_SEQ_RESET, "SNDCTL_SEQ_RESET"},
193 {SNDCTL_SEQ_SYNC, "SNDCTL_SEQ_SYNC"},
194 {SNDCTL_SYNTH_INFO, "SNDCTL_SYNTH_INFO"},
195 {SNDCTL_SEQ_CTRLRATE, "SNDCTL_SEQ_CTRLRATE"},
196 {SNDCTL_SEQ_GETOUTCOUNT, "SNDCTL_SEQ_GETOUTCOUNT"},
197 {SNDCTL_SEQ_GETINCOUNT, "SNDCTL_SEQ_GETINCOUNT"},
198 {SNDCTL_SEQ_PERCMODE, "SNDCTL_SEQ_PERCMODE"},
199 {SNDCTL_FM_LOAD_INSTR, "SNDCTL_FM_LOAD_INSTR"},
200 {SNDCTL_SEQ_TESTMIDI, "SNDCTL_SEQ_TESTMIDI"},
201 {SNDCTL_SEQ_RESETSAMPLES, "SNDCTL_SEQ_RESETSAMPLES"},
202 {SNDCTL_SEQ_NRSYNTHS, "SNDCTL_SEQ_NRSYNTHS"},
203 {SNDCTL_SEQ_NRMIDIS, "SNDCTL_SEQ_NRMIDIS"},
204 {SNDCTL_SEQ_GETTIME, "SNDCTL_SEQ_GETTIME"},
205 {SNDCTL_MIDI_INFO, "SNDCTL_MIDI_INFO"},
206 {SNDCTL_SEQ_THRESHOLD, "SNDCTL_SEQ_THRESHOLD"},
207 {SNDCTL_SYNTH_MEMAVL, "SNDCTL_SYNTH_MEMAVL"},
208 {SNDCTL_FM_4OP_ENABLE, "SNDCTL_FM_4OP_ENABLE"},
209 {SNDCTL_PMGR_ACCESS, "SNDCTL_PMGR_ACCESS"},
210 {SNDCTL_SEQ_PANIC, "SNDCTL_SEQ_PANIC"},
211 {SNDCTL_SEQ_OUTOFBAND, "SNDCTL_SEQ_OUTOFBAND"},
212 {SNDCTL_TMR_TIMEBASE, "SNDCTL_TMR_TIMEBASE"},
213 {SNDCTL_TMR_START, "SNDCTL_TMR_START"},
214 {SNDCTL_TMR_STOP, "SNDCTL_TMR_STOP"},
215 {SNDCTL_TMR_CONTINUE, "SNDCTL_TMR_CONTINUE"},
216 {SNDCTL_TMR_TEMPO, "SNDCTL_TMR_TEMPO"},
217 {SNDCTL_TMR_SOURCE, "SNDCTL_TMR_SOURCE"},
218 {SNDCTL_TMR_METRONOME, "SNDCTL_TMR_METRONOME"},
219 {SNDCTL_TMR_SELECT, "SNDCTL_TMR_SELECT"},
220 {SNDCTL_MIDI_PRETIME, "SNDCTL_MIDI_PRETIME"},
221 {AIONWRITE, "AIONWRITE"},
222 {AIOGSIZE, "AIOGSIZE"},
223 {AIOSSIZE, "AIOSSIZE"},
224 {AIOGFMT, "AIOGFMT"},
225 {AIOSFMT, "AIOSFMT"},
226 {AIOGMIX, "AIOGMIX"},
227 {AIOSMIX, "AIOSMIX"},
228 {AIOSTOP, "AIOSTOP"},
229 {AIOSYNC, "AIOSYNC"},
230 {AIOGCAP, "AIOGCAP"},
234 midi_cmdtab cmdtab_timer[] = {
235 {TMR_WAIT_REL, "TMR_WAIT_REL"},
236 {TMR_WAIT_ABS, "TMR_WAIT_ABS"},
237 {TMR_STOP, "TMR_STOP"},
238 {TMR_START, "TMR_START"},
239 {TMR_CONTINUE, "TMR_CONTINUE"},
240 {TMR_TEMPO, "TMR_TEMPO"},
241 {TMR_ECHO, "TMR_ECHO"},
242 {TMR_CLOCK, "TMR_CLOCK"},
243 {TMR_SPP, "TMR_SPP"},
244 {TMR_TIMESIG, "TMR_TIMESIG"},
248 midi_cmdtab cmdtab_seqcv[] = {
249 {MIDI_NOTEOFF, "MIDI_NOTEOFF"},
250 {MIDI_NOTEON, "MIDI_NOTEON"},
251 {MIDI_KEY_PRESSURE, "MIDI_KEY_PRESSURE"},
255 midi_cmdtab cmdtab_seqccmn[] = {
256 {MIDI_CTL_CHANGE, "MIDI_CTL_CHANGE"},
257 {MIDI_PGM_CHANGE, "MIDI_PGM_CHANGE"},
258 {MIDI_CHN_PRESSURE, "MIDI_CHN_PRESSURE"},
259 {MIDI_PITCH_BEND, "MIDI_PITCH_BEND"},
260 {MIDI_SYSTEM_PREFIX, "MIDI_SYSTEM_PREFIX"},
264 #ifndef KOBJMETHOD_END
265 #define KOBJMETHOD_END { NULL, NULL }
269 * static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m);
272 static kobj_method_t seq_methods[] = {
273 /* KOBJMETHOD(mpu_provider,mpu401_mprovider), */
277 DEFINE_CLASS(sequencer, seq_methods, 0);
279 /* The followings are the local function. */
280 static int seq_convertold(u_char *event, u_char *out);
283 * static void seq_midiinput(struct seq_softc * scp, void *md);
285 static void seq_reset(struct seq_softc *scp);
286 static int seq_sync(struct seq_softc *scp);
288 static int seq_processevent(struct seq_softc *scp, u_char *event);
290 static int seq_timing(struct seq_softc *scp, u_char *event);
291 static int seq_local(struct seq_softc *scp, u_char *event);
293 static int seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event);
294 static int seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event);
295 static int seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event);
297 static int seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md);
298 void seq_copytoinput(struct seq_softc *scp, u_char *event, int len);
299 int seq_modevent(module_t mod, int type, void *data);
300 struct seq_softc *seqs[10];
301 static struct mtx seqinfo_mtx;
302 static u_long nseq = 0;
304 static void timer_start(struct seq_softc *t);
305 static void timer_stop(struct seq_softc *t);
306 static void timer_setvals(struct seq_softc *t, int tempo, int timerbase);
307 static void timer_wait(struct seq_softc *t, int ticks, int wait_abs);
308 static int timer_now(struct seq_softc *t);
312 timer_start(struct seq_softc *t)
315 getmicrotime(&t->timersub);
319 timer_continue(struct seq_softc *t)
323 if (t->timerrun == 1)
327 timevalsub(&now, &t->timerstop);
328 timevaladd(&t->timersub, &now);
332 timer_stop(struct seq_softc *t)
335 getmicrotime(&t->timerstop);
339 timer_setvals(struct seq_softc *t, int tempo, int timerbase)
342 t->timerbase = timerbase;
346 timer_wait(struct seq_softc *t, int ticks, int wait_abs)
348 struct timeval now, when;
350 unsigned long long i;
352 while (t->timerrun == 0) {
353 SEQ_DEBUG(2, printf("Timer wait when timer isn't running\n"));
355 * The old sequencer used timeouts that only increased
356 * the timer when the timer was running.
357 * Hence the sequencer would stick (?) if the
358 * timer was disabled.
360 cv_wait(&t->reset_cv, &t->seq_lock);
365 i = ticks * 60ull * 1000000ull / (t->tempo * t->timerbase);
367 when.tv_sec = i / 1000000;
368 when.tv_usec = i % 1000000;
371 printf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n",
372 t->tempo, t->timerbase, ticks, wait_abs, i);
377 timevalsub(&now, &t->timersub);
378 timevalsub(&when, &now);
380 if (when.tv_sec < 0 || when.tv_usec < 0) {
382 printf("seq_timer error negative time %lds.%06lds\n",
383 (long)when.tv_sec, (long)when.tv_usec));
386 i = when.tv_sec * 1000000ull;
391 printf("seq_timer usec %llu ticks %llu\n",
392 when.tv_sec * 1000000ull + when.tv_usec, i);
395 ret = cv_timedwait(&t->reset_cv, &t->seq_lock, i + 1);
398 if (ret != EWOULDBLOCK)
399 SEQ_DEBUG(3, printf("seq_timer didn't timeout\n"));
404 timer_now(struct seq_softc *t)
407 unsigned long long i;
410 if (t->timerrun == 0)
415 timevalsub(&now, &t->timersub);
417 i = now.tv_sec * 1000000ull;
425 * printf("timer_now: %llu %d\n", i, ret);
432 seq_eventthread(void *arg)
434 struct seq_softc *scp = arg;
437 mtx_lock(&scp->seq_lock);
438 SEQ_DEBUG(2, printf("seq_eventthread started\n"));
439 while (scp->done == 0) {
441 while (scp->playing == 0) {
442 cv_wait(&scp->state_cv, &scp->seq_lock);
447 while (MIDIQ_EMPTY(scp->out_q)) {
448 cv_broadcast(&scp->empty_cv);
449 cv_wait(&scp->out_cv, &scp->seq_lock);
450 if (scp->playing == 0)
456 MIDIQ_DEQ(scp->out_q, event, EV_SZ);
458 if (MIDIQ_AVAIL(scp->out_q) < scp->out_water) {
459 cv_broadcast(&scp->out_cv);
460 selwakeup(&scp->out_sel);
462 seq_processevent(scp, event);
466 cv_broadcast(&scp->th_cv);
467 mtx_unlock(&scp->seq_lock);
468 SEQ_DEBUG(2, printf("seq_eventthread finished\n"));
473 * seq_processevent: This maybe called by the event thread or the IOCTL
474 * handler for queued and out of band events respectively.
477 seq_processevent(struct seq_softc *scp, u_char *event)
484 if (event[0] == EV_SEQ_LOCAL)
485 ret = seq_local(scp, event);
486 else if (event[0] == EV_TIMING)
487 ret = seq_timing(scp, event);
488 else if (event[0] != EV_CHN_VOICE &&
489 event[0] != EV_CHN_COMMON &&
490 event[0] != EV_SYSEX &&
491 event[0] != SEQ_MIDIPUTC) {
493 SEQ_DEBUG(2, printf("seq_processevent not known %d\n",
495 } else if (seq_fetch_mid(scp, event[1], &m) != 0) {
497 SEQ_DEBUG(2, printf("seq_processevent midi unit not found %d\n",
502 ret = seq_chnvoice(scp, m, event);
505 ret = seq_chncommon(scp, m, event);
508 ret = seq_sysex(scp, m, event);
511 mtx_unlock(&scp->seq_lock);
512 ret = SYNTH_WRITERAW(m, &event[2], 1);
513 mtx_lock(&scp->seq_lock);
522 struct seq_softc *scp;
526 /* Allocate the softc. */
528 scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO);
530 SEQ_DEBUG(1, printf("seq_addunit: softc allocation failed.\n"));
533 kobj_init((kobj_t)scp, &sequencer_class);
535 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
538 MIDIQ_INIT(scp->in_q, buf, EV_SZ * 1024);
539 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
542 MIDIQ_INIT(scp->out_q, buf, EV_SZ * 1024);
545 scp->midis = malloc(sizeof(kobj_t) * 32, M_TEMP, M_NOWAIT | M_ZERO);
546 scp->midi_flags = malloc(sizeof(*scp->midi_flags) * 32, M_TEMP,
549 if (scp->midis == NULL || scp->midi_flags == NULL)
554 mtx_init(&scp->seq_lock, "seqflq", NULL, 0);
555 cv_init(&scp->state_cv, "seqstate");
556 cv_init(&scp->empty_cv, "seqempty");
557 cv_init(&scp->reset_cv, "seqtimer");
558 cv_init(&scp->out_cv, "seqqout");
559 cv_init(&scp->in_cv, "seqqin");
560 cv_init(&scp->th_cv, "seqstart");
563 * Init the damn timer
566 scp->mapper = midimapper_addseq(scp, &scp->unit, &scp->mapper_cookie);
567 if (scp->mapper == NULL)
570 scp->seqdev = make_dev(&seq_cdevsw,
571 MIDIMKMINOR(scp->unit, SND_DEV_SEQ, 0), UID_ROOT,
572 GID_WHEEL, 0666, "sequencer%d", scp->unit);
574 scp->musicdev = make_dev(&seq_cdevsw,
575 MIDIMKMINOR(scp->unit, SND_DEV_MUSIC, 0), UID_ROOT,
576 GID_WHEEL, 0666, "music%d", scp->unit);
578 if (scp->seqdev == NULL || scp->musicdev == NULL)
581 * TODO: Add to list of sequencers this module provides
586 (seq_eventthread, scp, NULL, RFHIGHPID, 0,
587 "sequencer %02d", scp->unit);
592 scp->seqdev->si_drv1 = scp->musicdev->si_drv1 = scp;
594 SEQ_DEBUG(2, printf("sequencer %d created scp %p\n", scp->unit, scp));
598 mtx_lock(&seqinfo_mtx);
600 mtx_unlock(&seqinfo_mtx);
606 if (scp->seqdev != NULL)
607 destroy_dev(scp->seqdev);
608 if (scp->musicdev != NULL)
609 destroy_dev(scp->musicdev);
611 * TODO: Destroy mutex and cv
613 if (scp->midis != NULL)
614 free(scp->midis, M_TEMP);
615 if (scp->midi_flags != NULL)
616 free(scp->midi_flags, M_TEMP);
618 free(scp->out_q.b, M_TEMP);
620 free(scp->in_q.b, M_TEMP);
628 seq_delunit(int unit)
630 struct seq_softc *scp = seqs[unit];
633 //SEQ_DEBUG(4, printf("seq_delunit: %d\n", unit));
634 SEQ_DEBUG(1, printf("seq_delunit: 1 \n"));
635 mtx_lock(&scp->seq_lock);
639 cv_broadcast(&scp->out_cv);
640 cv_broadcast(&scp->state_cv);
641 cv_broadcast(&scp->reset_cv);
642 SEQ_DEBUG(1, printf("seq_delunit: 2 \n"));
643 cv_wait(&scp->th_cv, &scp->seq_lock);
644 SEQ_DEBUG(1, printf("seq_delunit: 3.0 \n"));
645 mtx_unlock(&scp->seq_lock);
646 SEQ_DEBUG(1, printf("seq_delunit: 3.1 \n"));
648 cv_destroy(&scp->state_cv);
649 SEQ_DEBUG(1, printf("seq_delunit: 4 \n"));
650 cv_destroy(&scp->empty_cv);
651 SEQ_DEBUG(1, printf("seq_delunit: 5 \n"));
652 cv_destroy(&scp->reset_cv);
653 SEQ_DEBUG(1, printf("seq_delunit: 6 \n"));
654 cv_destroy(&scp->out_cv);
655 SEQ_DEBUG(1, printf("seq_delunit: 7 \n"));
656 cv_destroy(&scp->in_cv);
657 SEQ_DEBUG(1, printf("seq_delunit: 8 \n"));
658 cv_destroy(&scp->th_cv);
660 SEQ_DEBUG(1, printf("seq_delunit: 10 \n"));
662 destroy_dev(scp->seqdev);
663 SEQ_DEBUG(1, printf("seq_delunit: 11 \n"));
665 destroy_dev(scp->musicdev);
666 SEQ_DEBUG(1, printf("seq_delunit: 12 \n"));
667 scp->seqdev = scp->musicdev = NULL;
668 if (scp->midis != NULL)
669 free(scp->midis, M_TEMP);
670 SEQ_DEBUG(1, printf("seq_delunit: 13 \n"));
671 if (scp->midi_flags != NULL)
672 free(scp->midi_flags, M_TEMP);
673 SEQ_DEBUG(1, printf("seq_delunit: 14 \n"));
674 free(scp->out_q.b, M_TEMP);
675 SEQ_DEBUG(1, printf("seq_delunit: 15 \n"));
676 free(scp->in_q.b, M_TEMP);
678 SEQ_DEBUG(1, printf("seq_delunit: 16 \n"));
680 mtx_destroy(&scp->seq_lock);
681 SEQ_DEBUG(1, printf("seq_delunit: 17 \n"));
684 mtx_lock(&seqinfo_mtx);
685 for (i = unit; i < (nseq - 1); i++)
686 seqs[i] = seqs[i + 1];
688 mtx_unlock(&seqinfo_mtx);
694 seq_modevent(module_t mod, int type, void *data)
702 mtx_init(&seqinfo_mtx, "seqmod", NULL, 0);
703 retval = seq_addunit();
708 r = seq_delunit(nseq - 1);
716 mtx_destroy(&seqinfo_mtx);
728 seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md)
731 if (unit > scp->midi_number || unit < 0)
734 *md = scp->midis[unit];
740 mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
742 struct seq_softc *scp = i_dev->si_drv1;
748 SEQ_DEBUG(3, printf("seq_open: scp %p unit %d, flags 0x%x.\n",
749 scp, scp->unit, flags));
752 * Mark this device busy.
755 mtx_lock(&scp->seq_lock);
757 mtx_unlock(&scp->seq_lock);
758 SEQ_DEBUG(2, printf("seq_open: unit %d is busy.\n", scp->unit));
763 if ((scp->fflags & O_NONBLOCK) != 0)
764 scp->flags |= SEQ_F_NBIO;
766 scp->music = MIDIDEV(i_dev) == SND_DEV_MUSIC;
769 * Enumerate the available midi devices
771 scp->midi_number = 0;
772 scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie);
774 if (scp->maxunits == 0)
775 SEQ_DEBUG(2, printf("seq_open: no midi devices\n"));
777 for (i = 0; i < scp->maxunits; i++) {
778 scp->midis[scp->midi_number] =
779 midimapper_fetch_synth(scp->mapper, scp->mapper_cookie, i);
780 if (scp->midis[scp->midi_number]) {
781 if (SYNTH_OPEN(scp->midis[scp->midi_number], scp,
783 scp->midis[scp->midi_number] = NULL;
785 scp->midi_flags[scp->midi_number] =
786 SYNTH_QUERY(scp->midis[scp->midi_number]);
792 timer_setvals(scp, 60, 100);
797 * actually, if we're in rdonly mode, we should start the timer
800 * TODO: Handle recording now
803 scp->out_water = MIDIQ_SIZE(scp->out_q) / 2;
806 mtx_unlock(&scp->seq_lock);
808 SEQ_DEBUG(2, printf("seq_open: opened, mode %s.\n",
809 scp->music ? "music" : "sequencer"));
811 printf("Sequencer %d %p opened maxunits %d midi_number %d:\n",
812 scp->unit, scp, scp->maxunits, scp->midi_number));
813 for (i = 0; i < scp->midi_number; i++)
814 SEQ_DEBUG(3, printf(" midi %d %p\n", i, scp->midis[i]));
823 mseq_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
826 struct seq_softc *scp = i_dev->si_drv1;
832 SEQ_DEBUG(2, printf("seq_close: unit %d.\n", scp->unit));
834 mtx_lock(&scp->seq_lock);
843 for (i = 0; i < scp->midi_number; i++)
845 SYNTH_CLOSE(scp->midis[i]);
847 midimapper_close(scp->mapper, scp->mapper_cookie);
855 SEQ_DEBUG(3, printf("seq_close: closed ret = %d.\n", ret));
856 mtx_unlock(&scp->seq_lock);
861 mseq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
864 struct seq_softc *scp = i_dev->si_drv1;
867 u_char buf[SEQ_RSIZE];
872 SEQ_DEBUG(7, printf("mseq_read: unit %d, resid %zd.\n",
873 scp->unit, uio->uio_resid));
875 mtx_lock(&scp->seq_lock);
876 if ((scp->fflags & FREAD) == 0) {
877 SEQ_DEBUG(2, printf("mseq_read: unit %d is not for reading.\n",
886 * if ((scp->flags & SEQ_F_READING) == 0)
889 * TODO, start recording if not alread
893 * I think the semantics are to return as soon
895 * Second thought, it doens't seem like midimoutain
896 * expects that at all.
897 * TODO: Look up in some sort of spec
900 while (uio->uio_resid > 0) {
901 while (MIDIQ_EMPTY(scp->in_q)) {
902 retval = EWOULDBLOCK;
904 * I wish I knew which one to care about
907 if (scp->fflags & O_NONBLOCK)
909 if (ioflag & O_NONBLOCK)
912 retval = cv_wait_sig(&scp->in_cv, &scp->seq_lock);
917 used = MIN(MIDIQ_LEN(scp->in_q), uio->uio_resid);
918 used = MIN(used, SEQ_RSIZE);
920 SEQ_DEBUG(8, printf("midiread: uiomove cc=%d\n", used));
921 MIDIQ_DEQ(scp->in_q, buf, used);
922 retval = uiomove(buf, used, uio);
929 mtx_unlock(&scp->seq_lock);
930 SEQ_DEBUG(6, printf("mseq_read: ret %d, resid %zd.\n",
931 retval, uio->uio_resid));
937 mseq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
939 u_char event[EV_SZ], newevent[EV_SZ], ev_code;
940 struct seq_softc *scp = i_dev->si_drv1;
944 SEQ_DEBUG(7, printf("seq_write: unit %d, resid %zd.\n",
945 scp->unit, uio->uio_resid));
950 mtx_lock(&scp->seq_lock);
952 if ((scp->fflags & FWRITE) == 0) {
953 SEQ_DEBUG(2, printf("seq_write: unit %d is not for writing.\n",
958 while (uio->uio_resid > 0) {
959 while (MIDIQ_AVAIL(scp->out_q) == 0) {
960 retval = EWOULDBLOCK;
961 if (scp->fflags & O_NONBLOCK)
963 if (ioflag & O_NONBLOCK)
965 SEQ_DEBUG(8, printf("seq_write cvwait\n"));
968 cv_broadcast(&scp->out_cv);
969 cv_broadcast(&scp->state_cv);
971 retval = cv_wait_sig(&scp->out_cv, &scp->seq_lock);
973 * We slept, maybe things have changed since last
982 if (scp != i_dev->si_drv1)
987 used = MIN(uio->uio_resid, 4);
989 SEQ_DEBUG(8, printf("seqout: resid %zd len %jd avail %jd\n",
990 uio->uio_resid, (intmax_t)MIDIQ_LEN(scp->out_q),
991 (intmax_t)MIDIQ_AVAIL(scp->out_q)));
997 retval = uiomove(event, used, uio);
1002 SEQ_DEBUG(8, printf("seq_write: unit %d, event %s.\n",
1003 scp->unit, midi_cmdname(ev_code, cmdtab_seqevent)));
1005 /* Have a look at the event code. */
1006 if (ev_code == SEQ_FULLSIZE) {
1009 * TODO: restore code for SEQ_FULLSIZE
1013 * A long event, these are the patches/samples for a
1016 midiunit = *(u_short *)&event[2];
1017 mtx_lock(&sd->seq_lock);
1018 ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
1019 mtx_unlock(&sd->seq_lock);
1023 SEQ_DEBUG(printf("seq_write: loading a patch to the unit %d.\n", midiunit));
1025 ret = md->synth.loadpatch(md, *(short *)&event[0], buf,
1030 * For now, just flush the darn buffer
1033 printf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
1034 while (uio->uio_resid > 0) {
1035 retval = uiomove(event, EV_SZ, uio);
1045 if (ev_code >= 128) {
1048 * Some sort of an extended event. The size is eight
1049 * bytes. scoop extra info.
1051 if (scp->music && ev_code == SEQ_EXTENDED) {
1052 SEQ_DEBUG(2, printf("seq_write: invalid level two event %x.\n", ev_code));
1055 if (uiomove((caddr_t)&event[4], 4, uio)) {
1057 printf("seq_write: user memory mangled?\n"));
1065 SEQ_DEBUG(2, printf("seq_write: four byte event in music mode.\n"));
1069 if (ev_code == SEQ_MIDIPUTC) {
1071 * TODO: event[2] is unit number to receive char.
1076 #ifdef not_ever_ever
1077 if (event[0] == EV_TIMING &&
1078 (event[1] == TMR_START || event[1] == TMR_STOP)) {
1080 * For now, try to make midimoutain work by
1081 * forcing these events to be processed
1084 seq_processevent(scp, event);
1086 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1088 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1091 if (seq_convertold(event, newevent) > 0)
1092 MIDIQ_ENQ(scp->out_q, newevent, EV_SZ);
1102 cv_broadcast(&scp->state_cv);
1103 cv_broadcast(&scp->out_cv);
1109 printf("seq_write done: leftover buffer length %zd retval %d\n",
1110 uio->uio_resid, retval));
1111 mtx_unlock(&scp->seq_lock);
1116 mseq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1119 int midiunit, ret, tmp;
1120 struct seq_softc *scp = i_dev->si_drv1;
1121 struct synth_info *synthinfo;
1122 struct midi_info *midiinfo;
1123 u_char event[EV_SZ];
1124 u_char newevent[EV_SZ];
1129 * struct snd_size *sndsize;
1135 SEQ_DEBUG(6, printf("seq_ioctl: unit %d, cmd %s.\n",
1136 scp->unit, midi_cmdname(cmd, cmdtab_seqioctl)));
1141 case SNDCTL_SEQ_GETTIME:
1143 * ioctl needed by libtse
1145 mtx_lock(&scp->seq_lock);
1146 *(int *)arg = timer_now(scp);
1147 mtx_unlock(&scp->seq_lock);
1148 SEQ_DEBUG(6, printf("seq_ioctl: gettime %d.\n", *(int *)arg));
1151 case SNDCTL_TMR_METRONOME:
1153 case SNDCTL_TMR_SOURCE:
1159 case SNDCTL_TMR_TEMPO:
1160 event[1] = TMR_TEMPO;
1161 event[4] = *(int *)arg & 0xFF;
1162 event[5] = (*(int *)arg >> 8) & 0xFF;
1163 event[6] = (*(int *)arg >> 16) & 0xFF;
1164 event[7] = (*(int *)arg >> 24) & 0xFF;
1166 case SNDCTL_TMR_TIMEBASE:
1167 event[1] = TMR_TIMERBASE;
1168 event[4] = *(int *)arg & 0xFF;
1169 event[5] = (*(int *)arg >> 8) & 0xFF;
1170 event[6] = (*(int *)arg >> 16) & 0xFF;
1171 event[7] = (*(int *)arg >> 24) & 0xFF;
1173 case SNDCTL_TMR_START:
1174 event[1] = TMR_START;
1176 case SNDCTL_TMR_STOP:
1177 event[1] = TMR_STOP;
1179 case SNDCTL_TMR_CONTINUE:
1180 event[1] = TMR_CONTINUE;
1182 event[0] = EV_TIMING;
1183 mtx_lock(&scp->seq_lock);
1186 mtx_unlock(&scp->seq_lock);
1189 seq_processevent(scp, event);
1190 mtx_unlock(&scp->seq_lock);
1192 case SNDCTL_TMR_SELECT:
1194 printf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
1197 case SNDCTL_SEQ_SYNC:
1198 if (mode == O_RDONLY) {
1202 mtx_lock(&scp->seq_lock);
1203 ret = seq_sync(scp);
1204 mtx_unlock(&scp->seq_lock);
1206 case SNDCTL_SEQ_PANIC:
1208 case SNDCTL_SEQ_RESET:
1210 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
1212 mtx_lock(&scp->seq_lock);
1214 mtx_unlock(&scp->seq_lock);
1217 case SNDCTL_SEQ_TESTMIDI:
1218 mtx_lock(&scp->seq_lock);
1220 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
1223 mtx_unlock(&scp->seq_lock);
1226 case SNDCTL_SEQ_GETINCOUNT:
1227 if (mode == O_WRONLY)
1230 mtx_lock(&scp->seq_lock);
1231 *(int *)arg = scp->in_q.rl;
1232 mtx_unlock(&scp->seq_lock);
1233 SEQ_DEBUG(printf("seq_ioctl: incount %d.\n",
1238 case SNDCTL_SEQ_GETOUTCOUNT:
1239 if (mode == O_RDONLY)
1242 mtx_lock(&scp->seq_lock);
1243 *(int *)arg = scp->out_q.fl;
1244 mtx_unlock(&scp->seq_lock);
1245 SEQ_DEBUG(printf("seq_ioctl: outcount %d.\n",
1251 case SNDCTL_SEQ_CTRLRATE:
1252 if (*(int *)arg != 0) {
1256 mtx_lock(&scp->seq_lock);
1257 *(int *)arg = scp->timerbase;
1258 mtx_unlock(&scp->seq_lock);
1259 SEQ_DEBUG(3, printf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
1263 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES
1266 case SNDCTL_SEQ_RESETSAMPLES:
1267 mtx_lock(&scp->seq_lock);
1268 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1269 mtx_unlock(&scp->seq_lock);
1272 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1273 SND_DEV_MIDIN), cmd, arg, mode, td);
1276 case SNDCTL_SEQ_NRSYNTHS:
1277 mtx_lock(&scp->seq_lock);
1278 *(int *)arg = scp->midi_number;
1279 mtx_unlock(&scp->seq_lock);
1280 SEQ_DEBUG(3, printf("seq_ioctl: synths %d.\n", *(int *)arg));
1283 case SNDCTL_SEQ_NRMIDIS:
1284 mtx_lock(&scp->seq_lock);
1289 * TODO: count the numbder of devices that can WRITERAW
1291 *(int *)arg = scp->midi_number;
1293 mtx_unlock(&scp->seq_lock);
1294 SEQ_DEBUG(3, printf("seq_ioctl: midis %d.\n", *(int *)arg));
1298 * TODO: ioctl SNDCTL_SYNTH_MEMAVL
1301 case SNDCTL_SYNTH_MEMAVL:
1302 mtx_lock(&scp->seq_lock);
1303 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1304 mtx_unlock(&scp->seq_lock);
1307 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1308 SND_DEV_MIDIN), cmd, arg, mode, td);
1311 case SNDCTL_SEQ_OUTOFBAND:
1312 for (ret = 0; ret < EV_SZ; ret++)
1313 event[ret] = (u_char)arg[0];
1315 mtx_lock(&scp->seq_lock);
1317 ret = seq_processevent(scp, event);
1319 if (seq_convertold(event, newevent) > 0)
1320 ret = seq_processevent(scp, newevent);
1324 mtx_unlock(&scp->seq_lock);
1326 case SNDCTL_SYNTH_INFO:
1327 synthinfo = (struct synth_info *)arg;
1328 midiunit = synthinfo->device;
1329 mtx_lock(&scp->seq_lock);
1330 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1331 bzero(synthinfo, sizeof(*synthinfo));
1332 synthinfo->name[0] = 'f';
1333 synthinfo->name[1] = 'a';
1334 synthinfo->name[2] = 'k';
1335 synthinfo->name[3] = 'e';
1336 synthinfo->name[4] = 's';
1337 synthinfo->name[5] = 'y';
1338 synthinfo->name[6] = 'n';
1339 synthinfo->name[7] = 't';
1340 synthinfo->name[8] = 'h';
1341 synthinfo->device = midiunit;
1342 synthinfo->synth_type = SYNTH_TYPE_MIDI;
1343 synthinfo->capabilities = scp->midi_flags[midiunit];
1347 mtx_unlock(&scp->seq_lock);
1349 case SNDCTL_MIDI_INFO:
1350 midiinfo = (struct midi_info *)arg;
1351 midiunit = midiinfo->device;
1352 mtx_lock(&scp->seq_lock);
1353 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1354 bzero(midiinfo, sizeof(*midiinfo));
1355 midiinfo->name[0] = 'f';
1356 midiinfo->name[1] = 'a';
1357 midiinfo->name[2] = 'k';
1358 midiinfo->name[3] = 'e';
1359 midiinfo->name[4] = 'm';
1360 midiinfo->name[5] = 'i';
1361 midiinfo->name[6] = 'd';
1362 midiinfo->name[7] = 'i';
1363 midiinfo->device = midiunit;
1364 midiinfo->capabilities = scp->midi_flags[midiunit];
1366 * TODO: What devtype?
1368 midiinfo->dev_type = 0x01;
1372 mtx_unlock(&scp->seq_lock);
1374 case SNDCTL_SEQ_THRESHOLD:
1375 mtx_lock(&scp->seq_lock);
1376 RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1);
1377 scp->out_water = *(int *)arg;
1378 mtx_unlock(&scp->seq_lock);
1379 SEQ_DEBUG(3, printf("seq_ioctl: water %d.\n", *(int *)arg));
1382 case SNDCTL_MIDI_PRETIME:
1386 mtx_lock(&scp->seq_lock);
1387 scp->pre_event_timeout = (hz * tmp) / 10;
1388 *(int *)arg = scp->pre_event_timeout;
1389 mtx_unlock(&scp->seq_lock);
1390 SEQ_DEBUG(3, printf("seq_ioctl: pretime %d.\n", *(int *)arg));
1393 case SNDCTL_FM_4OP_ENABLE:
1394 case SNDCTL_PMGR_IFACE:
1395 case SNDCTL_PMGR_ACCESS:
1397 * Patch manager and fm are ded, ded, ded.
1402 * TODO: Consider ioctl default case.
1404 * if ((scp->fflags & O_ACCMODE) == FREAD) {
1408 * Then pass on the ioctl to device 0
1411 printf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
1420 mseq_poll(struct cdev *i_dev, int events, struct thread *td)
1423 struct seq_softc *scp = i_dev->si_drv1;
1425 SEQ_DEBUG(3, printf("seq_poll: unit %d.\n", scp->unit));
1426 SEQ_DEBUG(1, printf("seq_poll: unit %d.\n", scp->unit));
1428 mtx_lock(&scp->seq_lock);
1432 /* Look up the apropriate queue and select it. */
1433 if ((events & (POLLOUT | POLLWRNORM)) != 0) {
1434 /* Start playing. */
1436 cv_broadcast(&scp->state_cv);
1437 cv_broadcast(&scp->out_cv);
1439 lim = scp->out_water;
1441 if (MIDIQ_AVAIL(scp->out_q) < lim)
1442 /* No enough space, record select. */
1443 selrecord(td, &scp->out_sel);
1445 /* We can write now. */
1446 ret |= events & (POLLOUT | POLLWRNORM);
1448 if ((events & (POLLIN | POLLRDNORM)) != 0) {
1449 /* TODO: Start recording. */
1451 /* Find out the boundary. */
1453 if (MIDIQ_LEN(scp->in_q) < lim)
1454 /* No data ready, record select. */
1455 selrecord(td, &scp->in_sel);
1457 /* We can read now. */
1458 ret |= events & (POLLIN | POLLRDNORM);
1460 mtx_unlock(&scp->seq_lock);
1467 sein_qtr(void *p, void /* mididev_info */ *md)
1469 struct seq_softc *scp;
1471 scp = (struct seq_softc *)p;
1473 mtx_lock(&scp->seq_lock);
1475 /* Restart playing if we have the data to output. */
1476 if (scp->queueout_pending)
1477 seq_callback(scp, SEQ_CB_START | SEQ_CB_WR);
1478 /* Check the midi device if we are reading. */
1479 if ((scp->flags & SEQ_F_READING) != 0)
1480 seq_midiinput(scp, md);
1482 mtx_unlock(&scp->seq_lock);
1488 * Was the old playevent. Use this to convert and old
1489 * style /dev/sequencer event to a /dev/music event
1492 seq_convertold(u_char *event, u_char *out)
1495 u_char dev, chn, note, vel;
1497 out[0] = out[1] = out[2] = out[3] = out[4] = out[5] = out[6] =
1509 * TODO: Debug statement
1528 out[0] = EV_CHN_VOICE;
1530 out[2] = MIDI_NOTEOFF;
1538 out[0] = EV_CHN_VOICE;
1540 out[2] = MIDI_NOTEON;
1548 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4]
1552 out[0] = EV_CHN_COMMON;
1554 out[2] = MIDI_PGM_CHANGE;
1563 out[2] = MIDI_PGM_CHANGE;
1567 SEQ_DEBUG(4,printf("seq_playevent: synctimer\n"));
1573 printf("seq_playevent: put data 0x%02x, unit %d.\n",
1574 event[1], event[2]));
1576 * Pass through to the midi device.
1580 out[0] = SEQ_MIDIPUTC;
1588 * This isn't handled here yet because I don't know if I can
1589 * just use four bytes events. There might be consequences
1590 * in the _read routing
1592 if (seq_copytoinput(scp, event, 4) == EAGAIN) {
1608 case SEQ_AFTERTOUCH:
1610 * SYNTH_AFTERTOUCH(md, event[3], event[4])
1614 * SYNTH_PANNING(md, event[3], (char)event[4])
1616 case SEQ_CONTROLLER:
1618 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5])
1622 * SYNTH_VOLUMEMETHOD(md, event[3])
1626 printf("seq_convertold: SEQ_EXTENDED type %d"
1627 "not handled\n", event[1]));
1633 out[1] = TMR_WAIT_REL;
1638 SEQ_DEBUG(5, printf("SEQ_WAIT %d",
1639 event[2] + (event[3] << 8) + (event[4] << 24)));
1649 printf("seq_convertold: event type %d not handled %d %d %d\n",
1650 event[0], event[1], event[2], event[3]));
1657 * Writting to the sequencer buffer never blocks and drops
1658 * input which cannot be queued
1661 seq_copytoinput(struct seq_softc *scp, u_char *event, int len)
1664 mtx_assert(&scp->seq_lock, MA_OWNED);
1666 if (MIDIQ_AVAIL(scp->in_q) < len) {
1668 * ENOROOM? EINPUTDROPPED? ETOUGHLUCK?
1670 SEQ_DEBUG(2, printf("seq_copytoinput: queue full\n"));
1672 MIDIQ_ENQ(scp->in_q, event, len);
1673 selwakeup(&scp->in_sel);
1674 cv_broadcast(&scp->in_cv);
1680 seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
1683 u_char cmd, chn, note, parm;
1691 mtx_assert(&scp->seq_lock, MA_OWNED);
1693 SEQ_DEBUG(5, printf("seq_chnvoice: unit %d, dev %d, cmd %s,"
1694 " chn %d, note %d, parm %d.\n", scp->unit, event[1],
1695 midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm));
1697 voice = SYNTH_ALLOC(md, chn, note);
1699 mtx_unlock(&scp->seq_lock);
1703 if (note < 128 || note == 255) {
1705 if (scp->music && chn == 9) {
1707 * This channel is a percussion. The note
1708 * number is the patch number.
1711 mtx_unlock(&scp->seq_lock);
1712 if (SYNTH_SETINSTR(md, voice, 128 + note)
1714 mtx_lock(&scp->seq_lock);
1717 mtx_lock(&scp->seq_lock);
1719 note = 60; /* Middle C. */
1724 mtx_unlock(&scp->seq_lock);
1725 if (SYNTH_SETUPVOICE(md, voice, chn)
1727 mtx_lock(&scp->seq_lock);
1730 mtx_lock(&scp->seq_lock);
1733 SYNTH_STARTNOTE(md, voice, note, parm);
1737 SYNTH_KILLNOTE(md, voice, note, parm);
1739 case MIDI_KEY_PRESSURE:
1740 SYNTH_AFTERTOUCH(md, voice, parm);
1744 SEQ_DEBUG(2, printf("seq_chnvoice event type %d not handled\n",
1749 mtx_lock(&scp->seq_lock);
1754 seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
1758 u_char cmd, chn, p1;
1764 w14 = *(u_short *)&event[6];
1766 SEQ_DEBUG(5, printf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
1767 " p1 %d, w14 %d.\n", scp->unit, event[1],
1768 midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14));
1769 mtx_unlock(&scp->seq_lock);
1771 case MIDI_PGM_CHANGE:
1772 SEQ_DEBUG(4, printf("seq_chncommon pgmchn chn %d pg %d\n",
1774 SYNTH_SETINSTR(md, chn, p1);
1776 case MIDI_CTL_CHANGE:
1777 SEQ_DEBUG(4, printf("seq_chncommon ctlch chn %d pg %d %d\n",
1779 SYNTH_CONTROLLER(md, chn, p1, w14);
1781 case MIDI_PITCH_BEND:
1784 * TODO: MIDI_PITCH_BEND
1787 mtx_lock(&md->synth.vc_mtx);
1788 md->synth.chn_info[chn].bender_value = w14;
1789 if (md->midiunit >= 0) {
1791 * Handle all of the notes playing on this
1794 key = ((int)chn << 8);
1795 for (i = 0; i < md->synth.alloc.max_voice; i++)
1796 if ((md->synth.alloc.map[i] & 0xff00) == key) {
1797 mtx_unlock(&md->synth.vc_mtx);
1798 mtx_unlock(&scp->seq_lock);
1799 if (md->synth.bender(md, i, w14) == EAGAIN) {
1800 mtx_lock(&scp->seq_lock);
1803 mtx_lock(&scp->seq_lock);
1806 mtx_unlock(&md->synth.vc_mtx);
1807 mtx_unlock(&scp->seq_lock);
1808 if (md->synth.bender(md, chn, w14) == EAGAIN) {
1809 mtx_lock(&scp->seq_lock);
1812 mtx_lock(&scp->seq_lock);
1816 SYNTH_BENDER(md, chn, w14);
1821 printf("seq_chncommon event type %d not handled.\n",
1826 mtx_lock(&scp->seq_lock);
1831 seq_timing(struct seq_softc *scp, u_char *event)
1837 param = event[4] + (event[5] << 8) +
1838 (event[6] << 16) + (event[7] << 24);
1840 SEQ_DEBUG(5, printf("seq_timing: unit %d, cmd %d, param %d.\n",
1841 scp->unit, event[1], param));
1844 timer_wait(scp, param, 0);
1847 timer_wait(scp, param, 1);
1851 cv_broadcast(&scp->reset_cv);
1856 * The following cv_broadcast isn't needed since we only
1857 * wait for 0->1 transitions. It probably won't hurt
1859 cv_broadcast(&scp->reset_cv);
1862 timer_continue(scp);
1863 cv_broadcast(&scp->reset_cv);
1870 SEQ_DEBUG(4, printf("Timer set tempo %d\n", param));
1871 timer_setvals(scp, param, scp->timerbase);
1878 SEQ_DEBUG(4, printf("Timer set timerbase %d\n", param));
1879 timer_setvals(scp, scp->tempo, param);
1883 * TODO: Consider making 4-byte events for /dev/sequencer
1884 * PRO: Maybe needed by legacy apps
1885 * CON: soundcard.h has been warning for a while many years
1886 * to expect 8 byte events.
1890 seq_copytoinput(scp, event, 8);
1892 param = (param << 8 | SEQ_ECHO);
1893 seq_copytoinput(scp, (u_char *)¶m, 4);
1896 seq_copytoinput(scp, event, 8);
1900 SEQ_DEBUG(2, printf("seq_timing event type %d not handled.\n",
1909 seq_local(struct seq_softc *scp, u_char *event)
1914 mtx_assert(&scp->seq_lock, MA_OWNED);
1916 SEQ_DEBUG(5, printf("seq_local: unit %d, cmd %d\n", scp->unit,
1920 SEQ_DEBUG(1, printf("seq_local event type %d not handled\n",
1929 seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event)
1933 mtx_assert(&scp->seq_lock, MA_OWNED);
1934 SEQ_DEBUG(5, printf("seq_sysex: unit %d device %d\n", scp->unit,
1937 for (i = 0; i < 6 && event[i + 2] != 0xff; i++)
1940 mtx_unlock(&scp->seq_lock);
1941 if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) {
1942 mtx_lock(&scp->seq_lock);
1945 mtx_lock(&scp->seq_lock);
1951 * Reset no longer closes the raw devices nor seq_sync's
1952 * Callers are IOCTL and seq_close
1955 seq_reset(struct seq_softc *scp)
1960 mtx_assert(&scp->seq_lock, MA_OWNED);
1962 SEQ_DEBUG(5, printf("seq_reset: unit %d.\n", scp->unit));
1965 * Stop reading and writing.
1968 /* scp->recording = 0; */
1970 cv_broadcast(&scp->state_cv);
1971 cv_broadcast(&scp->out_cv);
1972 cv_broadcast(&scp->reset_cv);
1975 * For now, don't reset the timers.
1977 MIDIQ_CLEAR(scp->in_q);
1978 MIDIQ_CLEAR(scp->out_q);
1980 for (i = 0; i < scp->midi_number; i++) {
1982 mtx_unlock(&scp->seq_lock);
1984 for (chn = 0; chn < 16; chn++) {
1985 SYNTH_CONTROLLER(m, chn, 123, 0);
1986 SYNTH_CONTROLLER(m, chn, 121, 0);
1987 SYNTH_BENDER(m, chn, 1 << 13);
1989 mtx_lock(&scp->seq_lock);
1995 * *really* flush the output queue
1996 * flush the event queue, then flush the synthsisers.
1997 * Callers are IOCTL and close
2000 #define SEQ_SYNC_TIMEOUT 8
2002 seq_sync(struct seq_softc *scp)
2004 int i, rl, sync[16], done;
2006 mtx_assert(&scp->seq_lock, MA_OWNED);
2008 SEQ_DEBUG(4, printf("seq_sync: unit %d.\n", scp->unit));
2011 * Wait until output queue is empty. Check every so often to see if
2012 * the queue is moving along. If it isn't just abort.
2014 while (!MIDIQ_EMPTY(scp->out_q)) {
2016 if (!scp->playing) {
2018 cv_broadcast(&scp->state_cv);
2019 cv_broadcast(&scp->out_cv);
2021 rl = MIDIQ_LEN(scp->out_q);
2023 i = cv_timedwait_sig(&scp->out_cv,
2024 &scp->seq_lock, SEQ_SYNC_TIMEOUT * hz);
2026 if (i == EINTR || i == ERESTART) {
2029 * XXX: I don't know why we stop playing
2032 cv_broadcast(&scp->out_cv);
2036 if (i == EWOULDBLOCK && rl == MIDIQ_LEN(scp->out_q) &&
2037 scp->waiting == 0) {
2039 * A queue seems to be stuck up. Give up and clear
2042 MIDIQ_CLEAR(scp->out_q);
2044 cv_broadcast(&scp->state_cv);
2045 cv_broadcast(&scp->out_cv);
2046 cv_broadcast(&scp->reset_cv);
2049 * TODO: Consider if the raw devices need to be flushed
2052 SEQ_DEBUG(1, printf("seq_sync queue stuck, aborting\n"));
2060 * Since syncing a midi device might block, unlock scp->seq_lock.
2063 mtx_unlock(&scp->seq_lock);
2064 for (i = 0; i < scp->midi_number; i++)
2069 for (i = 0; i < scp->midi_number; i++)
2071 if (SYNTH_INSYNC(scp->midis[i]) == 0)
2081 mtx_lock(&scp->seq_lock);
2086 midi_cmdname(int cmd, midi_cmdtab *tab)
2088 while (tab->name != NULL) {
2089 if (cmd == tab->cmd)