2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Mathew Kanner
5 * Copyright (c) 1993 Hannu Savolainen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * The sequencer personality manager.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/ioccom.h>
41 #include <sys/filio.h>
43 #include <sys/sockio.h>
44 #include <sys/fcntl.h>
46 #include <sys/sysctl.h>
48 #include <sys/kernel.h> /* for DATA_SET */
50 #include <sys/module.h>
54 #include <sys/syslog.h>
55 #include <sys/errno.h>
56 #include <sys/malloc.h>
58 #include <machine/resource.h>
59 #include <machine/bus.h>
60 #include <machine/clock.h> /* for DELAY */
61 #include <sys/soundcard.h>
65 #include <sys/mutex.h>
66 #include <sys/condvar.h>
67 #include <sys/kthread.h>
68 #include <sys/unistd.h>
69 #include <sys/selinfo.h>
71 #ifdef HAVE_KERNEL_OPTION_HEADERS
75 #include <dev/sound/midi/midi.h>
76 #include <dev/sound/midi/midiq.h>
79 #include <dev/sound/midi/sequencer.h>
81 #define TMR_TIMERBASE 13
83 #define SND_DEV_SEQ 1 /* Sequencer output /dev/sequencer (FM
84 * synthesizer and MIDI output) */
85 #define SND_DEV_MUSIC 8 /* /dev/music, level 2 interface */
87 /* Length of a sequencer event. */
92 #define LOOKUP_EXIST (0)
93 #define LOOKUP_OPEN (1)
94 #define LOOKUP_CLOSE (2)
96 #define PCMMKMINOR(u, d, c) \
97 ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
98 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
99 #define MIDIUNIT(y) ((dev2unit(y) >> 4) & 0x0f)
100 #define MIDIDEV(y) (dev2unit(y) & 0x0f)
102 /* These are the entries to the sequencer driver. */
103 static d_open_t mseq_open;
104 static d_close_t mseq_close;
105 static d_ioctl_t mseq_ioctl;
106 static d_read_t mseq_read;
107 static d_write_t mseq_write;
108 static d_poll_t mseq_poll;
110 static struct cdevsw seq_cdevsw = {
111 .d_version = D_VERSION,
113 .d_close = mseq_close,
115 .d_write = mseq_write,
116 .d_ioctl = mseq_ioctl,
118 .d_name = "sequencer",
124 struct mtx seq_lock, q_lock;
125 struct cv empty_cv, reset_cv, in_cv, out_cv, state_cv, th_cv;
127 MIDIQ_HEAD(, u_char) in_q, out_q;
130 /* Flags (protected by flag_mtx of mididev_info) */
131 int fflags; /* Access mode */
134 int out_water; /* Sequence output threshould */
135 snd_sync_parm sync_parm; /* AIOSYNC parameter set */
136 struct thread *sync_thread; /* AIOSYNCing thread */
137 struct selinfo in_sel, out_sel;
139 struct cdev *seqdev, *musicdev;
146 struct timeval timerstop, timersub;
147 int timerbase, tempo;
153 int pre_event_timeout;
158 * Module specific stuff, including how many sequecers
162 SYSCTL_NODE(_hw_midi, OID_AUTO, seq, CTLFLAG_RD, 0, "Midi sequencer");
165 /* XXX: should this be moved into debug.midi? */
166 SYSCTL_INT(_hw_midi_seq, OID_AUTO, debug, CTLFLAG_RW, &seq_debug, 0, "");
168 midi_cmdtab cmdtab_seqevent[] = {
169 {SEQ_NOTEOFF, "SEQ_NOTEOFF"},
170 {SEQ_NOTEON, "SEQ_NOTEON"},
171 {SEQ_WAIT, "SEQ_WAIT"},
172 {SEQ_PGMCHANGE, "SEQ_PGMCHANGE"},
173 {SEQ_SYNCTIMER, "SEQ_SYNCTIMER"},
174 {SEQ_MIDIPUTC, "SEQ_MIDIPUTC"},
175 {SEQ_DRUMON, "SEQ_DRUMON"},
176 {SEQ_DRUMOFF, "SEQ_DRUMOFF"},
177 {SEQ_ECHO, "SEQ_ECHO"},
178 {SEQ_AFTERTOUCH, "SEQ_AFTERTOUCH"},
179 {SEQ_CONTROLLER, "SEQ_CONTROLLER"},
180 {SEQ_BALANCE, "SEQ_BALANCE"},
181 {SEQ_VOLMODE, "SEQ_VOLMODE"},
182 {SEQ_FULLSIZE, "SEQ_FULLSIZE"},
183 {SEQ_PRIVATE, "SEQ_PRIVATE"},
184 {SEQ_EXTENDED, "SEQ_EXTENDED"},
185 {EV_SEQ_LOCAL, "EV_SEQ_LOCAL"},
186 {EV_TIMING, "EV_TIMING"},
187 {EV_CHN_COMMON, "EV_CHN_COMMON"},
188 {EV_CHN_VOICE, "EV_CHN_VOICE"},
189 {EV_SYSEX, "EV_SYSEX"},
193 midi_cmdtab cmdtab_seqioctl[] = {
194 {SNDCTL_SEQ_RESET, "SNDCTL_SEQ_RESET"},
195 {SNDCTL_SEQ_SYNC, "SNDCTL_SEQ_SYNC"},
196 {SNDCTL_SYNTH_INFO, "SNDCTL_SYNTH_INFO"},
197 {SNDCTL_SEQ_CTRLRATE, "SNDCTL_SEQ_CTRLRATE"},
198 {SNDCTL_SEQ_GETOUTCOUNT, "SNDCTL_SEQ_GETOUTCOUNT"},
199 {SNDCTL_SEQ_GETINCOUNT, "SNDCTL_SEQ_GETINCOUNT"},
200 {SNDCTL_SEQ_PERCMODE, "SNDCTL_SEQ_PERCMODE"},
201 {SNDCTL_FM_LOAD_INSTR, "SNDCTL_FM_LOAD_INSTR"},
202 {SNDCTL_SEQ_TESTMIDI, "SNDCTL_SEQ_TESTMIDI"},
203 {SNDCTL_SEQ_RESETSAMPLES, "SNDCTL_SEQ_RESETSAMPLES"},
204 {SNDCTL_SEQ_NRSYNTHS, "SNDCTL_SEQ_NRSYNTHS"},
205 {SNDCTL_SEQ_NRMIDIS, "SNDCTL_SEQ_NRMIDIS"},
206 {SNDCTL_SEQ_GETTIME, "SNDCTL_SEQ_GETTIME"},
207 {SNDCTL_MIDI_INFO, "SNDCTL_MIDI_INFO"},
208 {SNDCTL_SEQ_THRESHOLD, "SNDCTL_SEQ_THRESHOLD"},
209 {SNDCTL_SYNTH_MEMAVL, "SNDCTL_SYNTH_MEMAVL"},
210 {SNDCTL_FM_4OP_ENABLE, "SNDCTL_FM_4OP_ENABLE"},
211 {SNDCTL_PMGR_ACCESS, "SNDCTL_PMGR_ACCESS"},
212 {SNDCTL_SEQ_PANIC, "SNDCTL_SEQ_PANIC"},
213 {SNDCTL_SEQ_OUTOFBAND, "SNDCTL_SEQ_OUTOFBAND"},
214 {SNDCTL_TMR_TIMEBASE, "SNDCTL_TMR_TIMEBASE"},
215 {SNDCTL_TMR_START, "SNDCTL_TMR_START"},
216 {SNDCTL_TMR_STOP, "SNDCTL_TMR_STOP"},
217 {SNDCTL_TMR_CONTINUE, "SNDCTL_TMR_CONTINUE"},
218 {SNDCTL_TMR_TEMPO, "SNDCTL_TMR_TEMPO"},
219 {SNDCTL_TMR_SOURCE, "SNDCTL_TMR_SOURCE"},
220 {SNDCTL_TMR_METRONOME, "SNDCTL_TMR_METRONOME"},
221 {SNDCTL_TMR_SELECT, "SNDCTL_TMR_SELECT"},
222 {SNDCTL_MIDI_PRETIME, "SNDCTL_MIDI_PRETIME"},
223 {AIONWRITE, "AIONWRITE"},
224 {AIOGSIZE, "AIOGSIZE"},
225 {AIOSSIZE, "AIOSSIZE"},
226 {AIOGFMT, "AIOGFMT"},
227 {AIOSFMT, "AIOSFMT"},
228 {AIOGMIX, "AIOGMIX"},
229 {AIOSMIX, "AIOSMIX"},
230 {AIOSTOP, "AIOSTOP"},
231 {AIOSYNC, "AIOSYNC"},
232 {AIOGCAP, "AIOGCAP"},
236 midi_cmdtab cmdtab_timer[] = {
237 {TMR_WAIT_REL, "TMR_WAIT_REL"},
238 {TMR_WAIT_ABS, "TMR_WAIT_ABS"},
239 {TMR_STOP, "TMR_STOP"},
240 {TMR_START, "TMR_START"},
241 {TMR_CONTINUE, "TMR_CONTINUE"},
242 {TMR_TEMPO, "TMR_TEMPO"},
243 {TMR_ECHO, "TMR_ECHO"},
244 {TMR_CLOCK, "TMR_CLOCK"},
245 {TMR_SPP, "TMR_SPP"},
246 {TMR_TIMESIG, "TMR_TIMESIG"},
250 midi_cmdtab cmdtab_seqcv[] = {
251 {MIDI_NOTEOFF, "MIDI_NOTEOFF"},
252 {MIDI_NOTEON, "MIDI_NOTEON"},
253 {MIDI_KEY_PRESSURE, "MIDI_KEY_PRESSURE"},
257 midi_cmdtab cmdtab_seqccmn[] = {
258 {MIDI_CTL_CHANGE, "MIDI_CTL_CHANGE"},
259 {MIDI_PGM_CHANGE, "MIDI_PGM_CHANGE"},
260 {MIDI_CHN_PRESSURE, "MIDI_CHN_PRESSURE"},
261 {MIDI_PITCH_BEND, "MIDI_PITCH_BEND"},
262 {MIDI_SYSTEM_PREFIX, "MIDI_SYSTEM_PREFIX"},
266 #ifndef KOBJMETHOD_END
267 #define KOBJMETHOD_END { NULL, NULL }
271 * static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m);
274 static kobj_method_t seq_methods[] = {
275 /* KOBJMETHOD(mpu_provider,mpu401_mprovider), */
279 DEFINE_CLASS(sequencer, seq_methods, 0);
281 /* The followings are the local function. */
282 static int seq_convertold(u_char *event, u_char *out);
285 * static void seq_midiinput(struct seq_softc * scp, void *md);
287 static void seq_reset(struct seq_softc *scp);
288 static int seq_sync(struct seq_softc *scp);
290 static int seq_processevent(struct seq_softc *scp, u_char *event);
292 static int seq_timing(struct seq_softc *scp, u_char *event);
293 static int seq_local(struct seq_softc *scp, u_char *event);
295 static int seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event);
296 static int seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event);
297 static int seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event);
299 static int seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md);
300 void seq_copytoinput(struct seq_softc *scp, u_char *event, int len);
301 int seq_modevent(module_t mod, int type, void *data);
302 struct seq_softc *seqs[10];
303 static struct mtx seqinfo_mtx;
304 static u_long nseq = 0;
306 static void timer_start(struct seq_softc *t);
307 static void timer_stop(struct seq_softc *t);
308 static void timer_setvals(struct seq_softc *t, int tempo, int timerbase);
309 static void timer_wait(struct seq_softc *t, int ticks, int wait_abs);
310 static int timer_now(struct seq_softc *t);
314 timer_start(struct seq_softc *t)
317 getmicrotime(&t->timersub);
321 timer_continue(struct seq_softc *t)
325 if (t->timerrun == 1)
329 timevalsub(&now, &t->timerstop);
330 timevaladd(&t->timersub, &now);
334 timer_stop(struct seq_softc *t)
337 getmicrotime(&t->timerstop);
341 timer_setvals(struct seq_softc *t, int tempo, int timerbase)
344 t->timerbase = timerbase;
348 timer_wait(struct seq_softc *t, int ticks, int wait_abs)
350 struct timeval now, when;
352 unsigned long long i;
354 while (t->timerrun == 0) {
355 SEQ_DEBUG(2, printf("Timer wait when timer isn't running\n"));
357 * The old sequencer used timeouts that only increased
358 * the timer when the timer was running.
359 * Hence the sequencer would stick (?) if the
360 * timer was disabled.
362 cv_wait(&t->reset_cv, &t->seq_lock);
367 i = ticks * 60ull * 1000000ull / (t->tempo * t->timerbase);
369 when.tv_sec = i / 1000000;
370 when.tv_usec = i % 1000000;
373 printf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n",
374 t->tempo, t->timerbase, ticks, wait_abs, i);
379 timevalsub(&now, &t->timersub);
380 timevalsub(&when, &now);
382 if (when.tv_sec < 0 || when.tv_usec < 0) {
384 printf("seq_timer error negative time %lds.%06lds\n",
385 (long)when.tv_sec, (long)when.tv_usec));
388 i = when.tv_sec * 1000000ull;
393 printf("seq_timer usec %llu ticks %llu\n",
394 when.tv_sec * 1000000ull + when.tv_usec, i);
397 ret = cv_timedwait(&t->reset_cv, &t->seq_lock, i + 1);
400 if (ret != EWOULDBLOCK)
401 SEQ_DEBUG(3, printf("seq_timer didn't timeout\n"));
406 timer_now(struct seq_softc *t)
409 unsigned long long i;
412 if (t->timerrun == 0)
417 timevalsub(&now, &t->timersub);
419 i = now.tv_sec * 1000000ull;
427 * printf("timer_now: %llu %d\n", i, ret);
434 seq_eventthread(void *arg)
436 struct seq_softc *scp = arg;
439 mtx_lock(&scp->seq_lock);
440 SEQ_DEBUG(2, printf("seq_eventthread started\n"));
441 while (scp->done == 0) {
443 while (scp->playing == 0) {
444 cv_wait(&scp->state_cv, &scp->seq_lock);
449 while (MIDIQ_EMPTY(scp->out_q)) {
450 cv_broadcast(&scp->empty_cv);
451 cv_wait(&scp->out_cv, &scp->seq_lock);
452 if (scp->playing == 0)
458 MIDIQ_DEQ(scp->out_q, event, EV_SZ);
460 if (MIDIQ_AVAIL(scp->out_q) < scp->out_water) {
461 cv_broadcast(&scp->out_cv);
462 selwakeup(&scp->out_sel);
464 seq_processevent(scp, event);
468 cv_broadcast(&scp->th_cv);
469 mtx_unlock(&scp->seq_lock);
470 SEQ_DEBUG(2, printf("seq_eventthread finished\n"));
475 * seq_processevent: This maybe called by the event thread or the IOCTL
476 * handler for queued and out of band events respectively.
479 seq_processevent(struct seq_softc *scp, u_char *event)
486 if (event[0] == EV_SEQ_LOCAL)
487 ret = seq_local(scp, event);
488 else if (event[0] == EV_TIMING)
489 ret = seq_timing(scp, event);
490 else if (event[0] != EV_CHN_VOICE &&
491 event[0] != EV_CHN_COMMON &&
492 event[0] != EV_SYSEX &&
493 event[0] != SEQ_MIDIPUTC) {
495 SEQ_DEBUG(2, printf("seq_processevent not known %d\n",
497 } else if (seq_fetch_mid(scp, event[1], &m) != 0) {
499 SEQ_DEBUG(2, printf("seq_processevent midi unit not found %d\n",
504 ret = seq_chnvoice(scp, m, event);
507 ret = seq_chncommon(scp, m, event);
510 ret = seq_sysex(scp, m, event);
513 mtx_unlock(&scp->seq_lock);
514 ret = SYNTH_WRITERAW(m, &event[2], 1);
515 mtx_lock(&scp->seq_lock);
524 struct seq_softc *scp;
528 /* Allocate the softc. */
530 scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO);
532 SEQ_DEBUG(1, printf("seq_addunit: softc allocation failed.\n"));
535 kobj_init((kobj_t)scp, &sequencer_class);
537 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
540 MIDIQ_INIT(scp->in_q, buf, EV_SZ * 1024);
541 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
544 MIDIQ_INIT(scp->out_q, buf, EV_SZ * 1024);
547 scp->midis = malloc(sizeof(kobj_t) * 32, M_TEMP, M_NOWAIT | M_ZERO);
548 scp->midi_flags = malloc(sizeof(*scp->midi_flags) * 32, M_TEMP,
551 if (scp->midis == NULL || scp->midi_flags == NULL)
556 mtx_init(&scp->seq_lock, "seqflq", NULL, 0);
557 cv_init(&scp->state_cv, "seqstate");
558 cv_init(&scp->empty_cv, "seqempty");
559 cv_init(&scp->reset_cv, "seqtimer");
560 cv_init(&scp->out_cv, "seqqout");
561 cv_init(&scp->in_cv, "seqqin");
562 cv_init(&scp->th_cv, "seqstart");
565 * Init the damn timer
568 scp->mapper = midimapper_addseq(scp, &scp->unit, &scp->mapper_cookie);
569 if (scp->mapper == NULL)
572 scp->seqdev = make_dev(&seq_cdevsw,
573 MIDIMKMINOR(scp->unit, SND_DEV_SEQ, 0), UID_ROOT,
574 GID_WHEEL, 0666, "sequencer%d", scp->unit);
576 scp->musicdev = make_dev(&seq_cdevsw,
577 MIDIMKMINOR(scp->unit, SND_DEV_MUSIC, 0), UID_ROOT,
578 GID_WHEEL, 0666, "music%d", scp->unit);
580 if (scp->seqdev == NULL || scp->musicdev == NULL)
583 * TODO: Add to list of sequencers this module provides
588 (seq_eventthread, scp, NULL, RFHIGHPID, 0,
589 "sequencer %02d", scp->unit);
594 scp->seqdev->si_drv1 = scp->musicdev->si_drv1 = scp;
596 SEQ_DEBUG(2, printf("sequencer %d created scp %p\n", scp->unit, scp));
600 mtx_lock(&seqinfo_mtx);
602 mtx_unlock(&seqinfo_mtx);
608 if (scp->seqdev != NULL)
609 destroy_dev(scp->seqdev);
610 if (scp->musicdev != NULL)
611 destroy_dev(scp->musicdev);
613 * TODO: Destroy mutex and cv
615 if (scp->midis != NULL)
616 free(scp->midis, M_TEMP);
617 if (scp->midi_flags != NULL)
618 free(scp->midi_flags, M_TEMP);
620 free(scp->out_q.b, M_TEMP);
622 free(scp->in_q.b, M_TEMP);
630 seq_delunit(int unit)
632 struct seq_softc *scp = seqs[unit];
635 //SEQ_DEBUG(4, printf("seq_delunit: %d\n", unit));
636 SEQ_DEBUG(1, printf("seq_delunit: 1 \n"));
637 mtx_lock(&scp->seq_lock);
641 cv_broadcast(&scp->out_cv);
642 cv_broadcast(&scp->state_cv);
643 cv_broadcast(&scp->reset_cv);
644 SEQ_DEBUG(1, printf("seq_delunit: 2 \n"));
645 cv_wait(&scp->th_cv, &scp->seq_lock);
646 SEQ_DEBUG(1, printf("seq_delunit: 3.0 \n"));
647 mtx_unlock(&scp->seq_lock);
648 SEQ_DEBUG(1, printf("seq_delunit: 3.1 \n"));
650 cv_destroy(&scp->state_cv);
651 SEQ_DEBUG(1, printf("seq_delunit: 4 \n"));
652 cv_destroy(&scp->empty_cv);
653 SEQ_DEBUG(1, printf("seq_delunit: 5 \n"));
654 cv_destroy(&scp->reset_cv);
655 SEQ_DEBUG(1, printf("seq_delunit: 6 \n"));
656 cv_destroy(&scp->out_cv);
657 SEQ_DEBUG(1, printf("seq_delunit: 7 \n"));
658 cv_destroy(&scp->in_cv);
659 SEQ_DEBUG(1, printf("seq_delunit: 8 \n"));
660 cv_destroy(&scp->th_cv);
662 SEQ_DEBUG(1, printf("seq_delunit: 10 \n"));
664 destroy_dev(scp->seqdev);
665 SEQ_DEBUG(1, printf("seq_delunit: 11 \n"));
667 destroy_dev(scp->musicdev);
668 SEQ_DEBUG(1, printf("seq_delunit: 12 \n"));
669 scp->seqdev = scp->musicdev = NULL;
670 if (scp->midis != NULL)
671 free(scp->midis, M_TEMP);
672 SEQ_DEBUG(1, printf("seq_delunit: 13 \n"));
673 if (scp->midi_flags != NULL)
674 free(scp->midi_flags, M_TEMP);
675 SEQ_DEBUG(1, printf("seq_delunit: 14 \n"));
676 free(scp->out_q.b, M_TEMP);
677 SEQ_DEBUG(1, printf("seq_delunit: 15 \n"));
678 free(scp->in_q.b, M_TEMP);
680 SEQ_DEBUG(1, printf("seq_delunit: 16 \n"));
682 mtx_destroy(&scp->seq_lock);
683 SEQ_DEBUG(1, printf("seq_delunit: 17 \n"));
686 mtx_lock(&seqinfo_mtx);
687 for (i = unit; i < (nseq - 1); i++)
688 seqs[i] = seqs[i + 1];
690 mtx_unlock(&seqinfo_mtx);
696 seq_modevent(module_t mod, int type, void *data)
704 mtx_init(&seqinfo_mtx, "seqmod", NULL, 0);
705 retval = seq_addunit();
710 r = seq_delunit(nseq - 1);
718 mtx_destroy(&seqinfo_mtx);
730 seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md)
733 if (unit >= scp->midi_number || unit < 0)
736 *md = scp->midis[unit];
742 mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
744 struct seq_softc *scp = i_dev->si_drv1;
750 SEQ_DEBUG(3, printf("seq_open: scp %p unit %d, flags 0x%x.\n",
751 scp, scp->unit, flags));
754 * Mark this device busy.
757 mtx_lock(&scp->seq_lock);
759 mtx_unlock(&scp->seq_lock);
760 SEQ_DEBUG(2, printf("seq_open: unit %d is busy.\n", scp->unit));
765 if ((scp->fflags & O_NONBLOCK) != 0)
766 scp->flags |= SEQ_F_NBIO;
768 scp->music = MIDIDEV(i_dev) == SND_DEV_MUSIC;
771 * Enumerate the available midi devices
773 scp->midi_number = 0;
774 scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie);
776 if (scp->maxunits == 0)
777 SEQ_DEBUG(2, printf("seq_open: no midi devices\n"));
779 for (i = 0; i < scp->maxunits; i++) {
780 scp->midis[scp->midi_number] =
781 midimapper_fetch_synth(scp->mapper, scp->mapper_cookie, i);
782 if (scp->midis[scp->midi_number]) {
783 if (SYNTH_OPEN(scp->midis[scp->midi_number], scp,
785 scp->midis[scp->midi_number] = NULL;
787 scp->midi_flags[scp->midi_number] =
788 SYNTH_QUERY(scp->midis[scp->midi_number]);
794 timer_setvals(scp, 60, 100);
799 * actually, if we're in rdonly mode, we should start the timer
802 * TODO: Handle recording now
805 scp->out_water = MIDIQ_SIZE(scp->out_q) / 2;
808 mtx_unlock(&scp->seq_lock);
810 SEQ_DEBUG(2, printf("seq_open: opened, mode %s.\n",
811 scp->music ? "music" : "sequencer"));
813 printf("Sequencer %d %p opened maxunits %d midi_number %d:\n",
814 scp->unit, scp, scp->maxunits, scp->midi_number));
815 for (i = 0; i < scp->midi_number; i++)
816 SEQ_DEBUG(3, printf(" midi %d %p\n", i, scp->midis[i]));
825 mseq_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
828 struct seq_softc *scp = i_dev->si_drv1;
834 SEQ_DEBUG(2, printf("seq_close: unit %d.\n", scp->unit));
836 mtx_lock(&scp->seq_lock);
845 for (i = 0; i < scp->midi_number; i++)
847 SYNTH_CLOSE(scp->midis[i]);
849 midimapper_close(scp->mapper, scp->mapper_cookie);
857 SEQ_DEBUG(3, printf("seq_close: closed ret = %d.\n", ret));
858 mtx_unlock(&scp->seq_lock);
863 mseq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
866 struct seq_softc *scp = i_dev->si_drv1;
869 u_char buf[SEQ_RSIZE];
874 SEQ_DEBUG(7, printf("mseq_read: unit %d, resid %zd.\n",
875 scp->unit, uio->uio_resid));
877 mtx_lock(&scp->seq_lock);
878 if ((scp->fflags & FREAD) == 0) {
879 SEQ_DEBUG(2, printf("mseq_read: unit %d is not for reading.\n",
888 * if ((scp->flags & SEQ_F_READING) == 0)
891 * TODO, start recording if not alread
895 * I think the semantics are to return as soon
897 * Second thought, it doens't seem like midimoutain
898 * expects that at all.
899 * TODO: Look up in some sort of spec
902 while (uio->uio_resid > 0) {
903 while (MIDIQ_EMPTY(scp->in_q)) {
904 retval = EWOULDBLOCK;
906 * I wish I knew which one to care about
909 if (scp->fflags & O_NONBLOCK)
911 if (ioflag & O_NONBLOCK)
914 retval = cv_wait_sig(&scp->in_cv, &scp->seq_lock);
919 used = MIN(MIDIQ_LEN(scp->in_q), uio->uio_resid);
920 used = MIN(used, SEQ_RSIZE);
922 SEQ_DEBUG(8, printf("midiread: uiomove cc=%d\n", used));
923 MIDIQ_DEQ(scp->in_q, buf, used);
924 mtx_unlock(&scp->seq_lock);
925 retval = uiomove(buf, used, uio);
926 mtx_lock(&scp->seq_lock);
933 mtx_unlock(&scp->seq_lock);
934 SEQ_DEBUG(6, printf("mseq_read: ret %d, resid %zd.\n",
935 retval, uio->uio_resid));
941 mseq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
943 u_char event[EV_SZ], newevent[EV_SZ], ev_code;
944 struct seq_softc *scp = i_dev->si_drv1;
948 SEQ_DEBUG(7, printf("seq_write: unit %d, resid %zd.\n",
949 scp->unit, uio->uio_resid));
954 mtx_lock(&scp->seq_lock);
956 if ((scp->fflags & FWRITE) == 0) {
957 SEQ_DEBUG(2, printf("seq_write: unit %d is not for writing.\n",
962 while (uio->uio_resid > 0) {
963 while (MIDIQ_AVAIL(scp->out_q) == 0) {
964 retval = EWOULDBLOCK;
965 if (scp->fflags & O_NONBLOCK)
967 if (ioflag & O_NONBLOCK)
969 SEQ_DEBUG(8, printf("seq_write cvwait\n"));
972 cv_broadcast(&scp->out_cv);
973 cv_broadcast(&scp->state_cv);
975 retval = cv_wait_sig(&scp->out_cv, &scp->seq_lock);
977 * We slept, maybe things have changed since last
986 if (scp != i_dev->si_drv1)
991 used = MIN(uio->uio_resid, 4);
993 SEQ_DEBUG(8, printf("seqout: resid %zd len %jd avail %jd\n",
994 uio->uio_resid, (intmax_t)MIDIQ_LEN(scp->out_q),
995 (intmax_t)MIDIQ_AVAIL(scp->out_q)));
1001 mtx_unlock(&scp->seq_lock);
1002 retval = uiomove(event, used, uio);
1003 mtx_lock(&scp->seq_lock);
1008 SEQ_DEBUG(8, printf("seq_write: unit %d, event %s.\n",
1009 scp->unit, midi_cmdname(ev_code, cmdtab_seqevent)));
1011 /* Have a look at the event code. */
1012 if (ev_code == SEQ_FULLSIZE) {
1015 * TODO: restore code for SEQ_FULLSIZE
1019 * A long event, these are the patches/samples for a
1022 midiunit = *(u_short *)&event[2];
1023 mtx_lock(&sd->seq_lock);
1024 ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
1025 mtx_unlock(&sd->seq_lock);
1029 SEQ_DEBUG(printf("seq_write: loading a patch to the unit %d.\n", midiunit));
1031 ret = md->synth.loadpatch(md, *(short *)&event[0], buf,
1036 * For now, just flush the darn buffer
1039 printf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
1040 while (uio->uio_resid > 0) {
1041 mtx_unlock(&scp->seq_lock);
1042 retval = uiomove(event, MIN(EV_SZ, uio->uio_resid), uio);
1043 mtx_lock(&scp->seq_lock);
1053 if (ev_code >= 128) {
1057 * Some sort of an extended event. The size is eight
1058 * bytes. scoop extra info.
1060 if (scp->music && ev_code == SEQ_EXTENDED) {
1061 SEQ_DEBUG(2, printf("seq_write: invalid level two event %x.\n", ev_code));
1064 mtx_unlock(&scp->seq_lock);
1065 if (uio->uio_resid < 4)
1068 error = uiomove((caddr_t)&event[4], 4, uio);
1069 mtx_lock(&scp->seq_lock);
1072 printf("seq_write: user memory mangled?\n"));
1080 SEQ_DEBUG(2, printf("seq_write: four byte event in music mode.\n"));
1084 if (ev_code == SEQ_MIDIPUTC) {
1086 * TODO: event[2] is unit number to receive char.
1091 #ifdef not_ever_ever
1092 if (event[0] == EV_TIMING &&
1093 (event[1] == TMR_START || event[1] == TMR_STOP)) {
1095 * For now, try to make midimoutain work by
1096 * forcing these events to be processed
1099 seq_processevent(scp, event);
1101 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1103 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1106 if (seq_convertold(event, newevent) > 0)
1107 MIDIQ_ENQ(scp->out_q, newevent, EV_SZ);
1117 cv_broadcast(&scp->state_cv);
1118 cv_broadcast(&scp->out_cv);
1124 printf("seq_write done: leftover buffer length %zd retval %d\n",
1125 uio->uio_resid, retval));
1126 mtx_unlock(&scp->seq_lock);
1131 mseq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1134 int midiunit, ret, tmp;
1135 struct seq_softc *scp = i_dev->si_drv1;
1136 struct synth_info *synthinfo;
1137 struct midi_info *midiinfo;
1138 u_char event[EV_SZ];
1139 u_char newevent[EV_SZ];
1144 * struct snd_size *sndsize;
1150 SEQ_DEBUG(6, printf("seq_ioctl: unit %d, cmd %s.\n",
1151 scp->unit, midi_cmdname(cmd, cmdtab_seqioctl)));
1156 case SNDCTL_SEQ_GETTIME:
1158 * ioctl needed by libtse
1160 mtx_lock(&scp->seq_lock);
1161 *(int *)arg = timer_now(scp);
1162 mtx_unlock(&scp->seq_lock);
1163 SEQ_DEBUG(6, printf("seq_ioctl: gettime %d.\n", *(int *)arg));
1166 case SNDCTL_TMR_METRONOME:
1168 case SNDCTL_TMR_SOURCE:
1174 case SNDCTL_TMR_TEMPO:
1175 event[1] = TMR_TEMPO;
1176 event[4] = *(int *)arg & 0xFF;
1177 event[5] = (*(int *)arg >> 8) & 0xFF;
1178 event[6] = (*(int *)arg >> 16) & 0xFF;
1179 event[7] = (*(int *)arg >> 24) & 0xFF;
1181 case SNDCTL_TMR_TIMEBASE:
1182 event[1] = TMR_TIMERBASE;
1183 event[4] = *(int *)arg & 0xFF;
1184 event[5] = (*(int *)arg >> 8) & 0xFF;
1185 event[6] = (*(int *)arg >> 16) & 0xFF;
1186 event[7] = (*(int *)arg >> 24) & 0xFF;
1188 case SNDCTL_TMR_START:
1189 event[1] = TMR_START;
1191 case SNDCTL_TMR_STOP:
1192 event[1] = TMR_STOP;
1194 case SNDCTL_TMR_CONTINUE:
1195 event[1] = TMR_CONTINUE;
1197 event[0] = EV_TIMING;
1198 mtx_lock(&scp->seq_lock);
1201 mtx_unlock(&scp->seq_lock);
1204 seq_processevent(scp, event);
1205 mtx_unlock(&scp->seq_lock);
1207 case SNDCTL_TMR_SELECT:
1209 printf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
1212 case SNDCTL_SEQ_SYNC:
1213 if (mode == O_RDONLY) {
1217 mtx_lock(&scp->seq_lock);
1218 ret = seq_sync(scp);
1219 mtx_unlock(&scp->seq_lock);
1221 case SNDCTL_SEQ_PANIC:
1223 case SNDCTL_SEQ_RESET:
1225 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
1227 mtx_lock(&scp->seq_lock);
1229 mtx_unlock(&scp->seq_lock);
1232 case SNDCTL_SEQ_TESTMIDI:
1233 mtx_lock(&scp->seq_lock);
1235 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
1238 mtx_unlock(&scp->seq_lock);
1241 case SNDCTL_SEQ_GETINCOUNT:
1242 if (mode == O_WRONLY)
1245 mtx_lock(&scp->seq_lock);
1246 *(int *)arg = scp->in_q.rl;
1247 mtx_unlock(&scp->seq_lock);
1248 SEQ_DEBUG(printf("seq_ioctl: incount %d.\n",
1253 case SNDCTL_SEQ_GETOUTCOUNT:
1254 if (mode == O_RDONLY)
1257 mtx_lock(&scp->seq_lock);
1258 *(int *)arg = scp->out_q.fl;
1259 mtx_unlock(&scp->seq_lock);
1260 SEQ_DEBUG(printf("seq_ioctl: outcount %d.\n",
1266 case SNDCTL_SEQ_CTRLRATE:
1267 if (*(int *)arg != 0) {
1271 mtx_lock(&scp->seq_lock);
1272 *(int *)arg = scp->timerbase;
1273 mtx_unlock(&scp->seq_lock);
1274 SEQ_DEBUG(3, printf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
1278 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES
1281 case SNDCTL_SEQ_RESETSAMPLES:
1282 mtx_lock(&scp->seq_lock);
1283 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1284 mtx_unlock(&scp->seq_lock);
1287 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1288 SND_DEV_MIDIN), cmd, arg, mode, td);
1291 case SNDCTL_SEQ_NRSYNTHS:
1292 mtx_lock(&scp->seq_lock);
1293 *(int *)arg = scp->midi_number;
1294 mtx_unlock(&scp->seq_lock);
1295 SEQ_DEBUG(3, printf("seq_ioctl: synths %d.\n", *(int *)arg));
1298 case SNDCTL_SEQ_NRMIDIS:
1299 mtx_lock(&scp->seq_lock);
1304 * TODO: count the numbder of devices that can WRITERAW
1306 *(int *)arg = scp->midi_number;
1308 mtx_unlock(&scp->seq_lock);
1309 SEQ_DEBUG(3, printf("seq_ioctl: midis %d.\n", *(int *)arg));
1313 * TODO: ioctl SNDCTL_SYNTH_MEMAVL
1316 case SNDCTL_SYNTH_MEMAVL:
1317 mtx_lock(&scp->seq_lock);
1318 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1319 mtx_unlock(&scp->seq_lock);
1322 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1323 SND_DEV_MIDIN), cmd, arg, mode, td);
1326 case SNDCTL_SEQ_OUTOFBAND:
1327 for (ret = 0; ret < EV_SZ; ret++)
1328 event[ret] = (u_char)arg[0];
1330 mtx_lock(&scp->seq_lock);
1332 ret = seq_processevent(scp, event);
1334 if (seq_convertold(event, newevent) > 0)
1335 ret = seq_processevent(scp, newevent);
1339 mtx_unlock(&scp->seq_lock);
1341 case SNDCTL_SYNTH_INFO:
1342 synthinfo = (struct synth_info *)arg;
1343 midiunit = synthinfo->device;
1344 mtx_lock(&scp->seq_lock);
1345 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1346 bzero(synthinfo, sizeof(*synthinfo));
1347 synthinfo->name[0] = 'f';
1348 synthinfo->name[1] = 'a';
1349 synthinfo->name[2] = 'k';
1350 synthinfo->name[3] = 'e';
1351 synthinfo->name[4] = 's';
1352 synthinfo->name[5] = 'y';
1353 synthinfo->name[6] = 'n';
1354 synthinfo->name[7] = 't';
1355 synthinfo->name[8] = 'h';
1356 synthinfo->device = midiunit;
1357 synthinfo->synth_type = SYNTH_TYPE_MIDI;
1358 synthinfo->capabilities = scp->midi_flags[midiunit];
1362 mtx_unlock(&scp->seq_lock);
1364 case SNDCTL_MIDI_INFO:
1365 midiinfo = (struct midi_info *)arg;
1366 midiunit = midiinfo->device;
1367 mtx_lock(&scp->seq_lock);
1368 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1369 bzero(midiinfo, sizeof(*midiinfo));
1370 midiinfo->name[0] = 'f';
1371 midiinfo->name[1] = 'a';
1372 midiinfo->name[2] = 'k';
1373 midiinfo->name[3] = 'e';
1374 midiinfo->name[4] = 'm';
1375 midiinfo->name[5] = 'i';
1376 midiinfo->name[6] = 'd';
1377 midiinfo->name[7] = 'i';
1378 midiinfo->device = midiunit;
1379 midiinfo->capabilities = scp->midi_flags[midiunit];
1381 * TODO: What devtype?
1383 midiinfo->dev_type = 0x01;
1387 mtx_unlock(&scp->seq_lock);
1389 case SNDCTL_SEQ_THRESHOLD:
1390 mtx_lock(&scp->seq_lock);
1391 RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1);
1392 scp->out_water = *(int *)arg;
1393 mtx_unlock(&scp->seq_lock);
1394 SEQ_DEBUG(3, printf("seq_ioctl: water %d.\n", *(int *)arg));
1397 case SNDCTL_MIDI_PRETIME:
1401 mtx_lock(&scp->seq_lock);
1402 scp->pre_event_timeout = (hz * tmp) / 10;
1403 *(int *)arg = scp->pre_event_timeout;
1404 mtx_unlock(&scp->seq_lock);
1405 SEQ_DEBUG(3, printf("seq_ioctl: pretime %d.\n", *(int *)arg));
1408 case SNDCTL_FM_4OP_ENABLE:
1409 case SNDCTL_PMGR_IFACE:
1410 case SNDCTL_PMGR_ACCESS:
1412 * Patch manager and fm are ded, ded, ded.
1417 * TODO: Consider ioctl default case.
1419 * if ((scp->fflags & O_ACCMODE) == FREAD) {
1423 * Then pass on the ioctl to device 0
1426 printf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
1435 mseq_poll(struct cdev *i_dev, int events, struct thread *td)
1438 struct seq_softc *scp = i_dev->si_drv1;
1440 SEQ_DEBUG(3, printf("seq_poll: unit %d.\n", scp->unit));
1441 SEQ_DEBUG(1, printf("seq_poll: unit %d.\n", scp->unit));
1443 mtx_lock(&scp->seq_lock);
1447 /* Look up the apropriate queue and select it. */
1448 if ((events & (POLLOUT | POLLWRNORM)) != 0) {
1449 /* Start playing. */
1451 cv_broadcast(&scp->state_cv);
1452 cv_broadcast(&scp->out_cv);
1454 lim = scp->out_water;
1456 if (MIDIQ_AVAIL(scp->out_q) < lim)
1457 /* No enough space, record select. */
1458 selrecord(td, &scp->out_sel);
1460 /* We can write now. */
1461 ret |= events & (POLLOUT | POLLWRNORM);
1463 if ((events & (POLLIN | POLLRDNORM)) != 0) {
1464 /* TODO: Start recording. */
1466 /* Find out the boundary. */
1468 if (MIDIQ_LEN(scp->in_q) < lim)
1469 /* No data ready, record select. */
1470 selrecord(td, &scp->in_sel);
1472 /* We can read now. */
1473 ret |= events & (POLLIN | POLLRDNORM);
1475 mtx_unlock(&scp->seq_lock);
1482 sein_qtr(void *p, void /* mididev_info */ *md)
1484 struct seq_softc *scp;
1486 scp = (struct seq_softc *)p;
1488 mtx_lock(&scp->seq_lock);
1490 /* Restart playing if we have the data to output. */
1491 if (scp->queueout_pending)
1492 seq_callback(scp, SEQ_CB_START | SEQ_CB_WR);
1493 /* Check the midi device if we are reading. */
1494 if ((scp->flags & SEQ_F_READING) != 0)
1495 seq_midiinput(scp, md);
1497 mtx_unlock(&scp->seq_lock);
1503 * Was the old playevent. Use this to convert and old
1504 * style /dev/sequencer event to a /dev/music event
1507 seq_convertold(u_char *event, u_char *out)
1510 u_char dev, chn, note, vel;
1512 out[0] = out[1] = out[2] = out[3] = out[4] = out[5] = out[6] =
1524 * TODO: Debug statement
1543 out[0] = EV_CHN_VOICE;
1545 out[2] = MIDI_NOTEOFF;
1553 out[0] = EV_CHN_VOICE;
1555 out[2] = MIDI_NOTEON;
1563 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4]
1567 out[0] = EV_CHN_COMMON;
1569 out[2] = MIDI_PGM_CHANGE;
1578 out[2] = MIDI_PGM_CHANGE;
1582 SEQ_DEBUG(4,printf("seq_playevent: synctimer\n"));
1588 printf("seq_playevent: put data 0x%02x, unit %d.\n",
1589 event[1], event[2]));
1591 * Pass through to the midi device.
1595 out[0] = SEQ_MIDIPUTC;
1603 * This isn't handled here yet because I don't know if I can
1604 * just use four bytes events. There might be consequences
1605 * in the _read routing
1607 if (seq_copytoinput(scp, event, 4) == EAGAIN) {
1623 case SEQ_AFTERTOUCH:
1625 * SYNTH_AFTERTOUCH(md, event[3], event[4])
1629 * SYNTH_PANNING(md, event[3], (char)event[4])
1631 case SEQ_CONTROLLER:
1633 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5])
1637 * SYNTH_VOLUMEMETHOD(md, event[3])
1641 printf("seq_convertold: SEQ_EXTENDED type %d"
1642 "not handled\n", event[1]));
1648 out[1] = TMR_WAIT_REL;
1653 SEQ_DEBUG(5, printf("SEQ_WAIT %d",
1654 event[2] + (event[3] << 8) + (event[4] << 24)));
1664 printf("seq_convertold: event type %d not handled %d %d %d\n",
1665 event[0], event[1], event[2], event[3]));
1672 * Writting to the sequencer buffer never blocks and drops
1673 * input which cannot be queued
1676 seq_copytoinput(struct seq_softc *scp, u_char *event, int len)
1679 mtx_assert(&scp->seq_lock, MA_OWNED);
1681 if (MIDIQ_AVAIL(scp->in_q) < len) {
1683 * ENOROOM? EINPUTDROPPED? ETOUGHLUCK?
1685 SEQ_DEBUG(2, printf("seq_copytoinput: queue full\n"));
1687 MIDIQ_ENQ(scp->in_q, event, len);
1688 selwakeup(&scp->in_sel);
1689 cv_broadcast(&scp->in_cv);
1695 seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
1698 u_char cmd, chn, note, parm;
1706 mtx_assert(&scp->seq_lock, MA_OWNED);
1708 SEQ_DEBUG(5, printf("seq_chnvoice: unit %d, dev %d, cmd %s,"
1709 " chn %d, note %d, parm %d.\n", scp->unit, event[1],
1710 midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm));
1712 voice = SYNTH_ALLOC(md, chn, note);
1714 mtx_unlock(&scp->seq_lock);
1718 if (note < 128 || note == 255) {
1720 if (scp->music && chn == 9) {
1722 * This channel is a percussion. The note
1723 * number is the patch number.
1726 mtx_unlock(&scp->seq_lock);
1727 if (SYNTH_SETINSTR(md, voice, 128 + note)
1729 mtx_lock(&scp->seq_lock);
1732 mtx_lock(&scp->seq_lock);
1734 note = 60; /* Middle C. */
1739 mtx_unlock(&scp->seq_lock);
1740 if (SYNTH_SETUPVOICE(md, voice, chn)
1742 mtx_lock(&scp->seq_lock);
1745 mtx_lock(&scp->seq_lock);
1748 SYNTH_STARTNOTE(md, voice, note, parm);
1752 SYNTH_KILLNOTE(md, voice, note, parm);
1754 case MIDI_KEY_PRESSURE:
1755 SYNTH_AFTERTOUCH(md, voice, parm);
1759 SEQ_DEBUG(2, printf("seq_chnvoice event type %d not handled\n",
1764 mtx_lock(&scp->seq_lock);
1769 seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
1773 u_char cmd, chn, p1;
1779 w14 = *(u_short *)&event[6];
1781 SEQ_DEBUG(5, printf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
1782 " p1 %d, w14 %d.\n", scp->unit, event[1],
1783 midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14));
1784 mtx_unlock(&scp->seq_lock);
1786 case MIDI_PGM_CHANGE:
1787 SEQ_DEBUG(4, printf("seq_chncommon pgmchn chn %d pg %d\n",
1789 SYNTH_SETINSTR(md, chn, p1);
1791 case MIDI_CTL_CHANGE:
1792 SEQ_DEBUG(4, printf("seq_chncommon ctlch chn %d pg %d %d\n",
1794 SYNTH_CONTROLLER(md, chn, p1, w14);
1796 case MIDI_PITCH_BEND:
1799 * TODO: MIDI_PITCH_BEND
1802 mtx_lock(&md->synth.vc_mtx);
1803 md->synth.chn_info[chn].bender_value = w14;
1804 if (md->midiunit >= 0) {
1806 * Handle all of the notes playing on this
1809 key = ((int)chn << 8);
1810 for (i = 0; i < md->synth.alloc.max_voice; i++)
1811 if ((md->synth.alloc.map[i] & 0xff00) == key) {
1812 mtx_unlock(&md->synth.vc_mtx);
1813 mtx_unlock(&scp->seq_lock);
1814 if (md->synth.bender(md, i, w14) == EAGAIN) {
1815 mtx_lock(&scp->seq_lock);
1818 mtx_lock(&scp->seq_lock);
1821 mtx_unlock(&md->synth.vc_mtx);
1822 mtx_unlock(&scp->seq_lock);
1823 if (md->synth.bender(md, chn, w14) == EAGAIN) {
1824 mtx_lock(&scp->seq_lock);
1827 mtx_lock(&scp->seq_lock);
1831 SYNTH_BENDER(md, chn, w14);
1836 printf("seq_chncommon event type %d not handled.\n",
1841 mtx_lock(&scp->seq_lock);
1846 seq_timing(struct seq_softc *scp, u_char *event)
1852 param = event[4] + (event[5] << 8) +
1853 (event[6] << 16) + (event[7] << 24);
1855 SEQ_DEBUG(5, printf("seq_timing: unit %d, cmd %d, param %d.\n",
1856 scp->unit, event[1], param));
1859 timer_wait(scp, param, 0);
1862 timer_wait(scp, param, 1);
1866 cv_broadcast(&scp->reset_cv);
1871 * The following cv_broadcast isn't needed since we only
1872 * wait for 0->1 transitions. It probably won't hurt
1874 cv_broadcast(&scp->reset_cv);
1877 timer_continue(scp);
1878 cv_broadcast(&scp->reset_cv);
1885 SEQ_DEBUG(4, printf("Timer set tempo %d\n", param));
1886 timer_setvals(scp, param, scp->timerbase);
1893 SEQ_DEBUG(4, printf("Timer set timerbase %d\n", param));
1894 timer_setvals(scp, scp->tempo, param);
1898 * TODO: Consider making 4-byte events for /dev/sequencer
1899 * PRO: Maybe needed by legacy apps
1900 * CON: soundcard.h has been warning for a while many years
1901 * to expect 8 byte events.
1905 seq_copytoinput(scp, event, 8);
1907 param = (param << 8 | SEQ_ECHO);
1908 seq_copytoinput(scp, (u_char *)¶m, 4);
1911 seq_copytoinput(scp, event, 8);
1915 SEQ_DEBUG(2, printf("seq_timing event type %d not handled.\n",
1924 seq_local(struct seq_softc *scp, u_char *event)
1929 mtx_assert(&scp->seq_lock, MA_OWNED);
1931 SEQ_DEBUG(5, printf("seq_local: unit %d, cmd %d\n", scp->unit,
1935 SEQ_DEBUG(1, printf("seq_local event type %d not handled\n",
1944 seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event)
1948 mtx_assert(&scp->seq_lock, MA_OWNED);
1949 SEQ_DEBUG(5, printf("seq_sysex: unit %d device %d\n", scp->unit,
1952 for (i = 0; i < 6 && event[i + 2] != 0xff; i++)
1955 mtx_unlock(&scp->seq_lock);
1956 if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) {
1957 mtx_lock(&scp->seq_lock);
1960 mtx_lock(&scp->seq_lock);
1966 * Reset no longer closes the raw devices nor seq_sync's
1967 * Callers are IOCTL and seq_close
1970 seq_reset(struct seq_softc *scp)
1975 mtx_assert(&scp->seq_lock, MA_OWNED);
1977 SEQ_DEBUG(5, printf("seq_reset: unit %d.\n", scp->unit));
1980 * Stop reading and writing.
1983 /* scp->recording = 0; */
1985 cv_broadcast(&scp->state_cv);
1986 cv_broadcast(&scp->out_cv);
1987 cv_broadcast(&scp->reset_cv);
1990 * For now, don't reset the timers.
1992 MIDIQ_CLEAR(scp->in_q);
1993 MIDIQ_CLEAR(scp->out_q);
1995 for (i = 0; i < scp->midi_number; i++) {
1997 mtx_unlock(&scp->seq_lock);
1999 for (chn = 0; chn < 16; chn++) {
2000 SYNTH_CONTROLLER(m, chn, 123, 0);
2001 SYNTH_CONTROLLER(m, chn, 121, 0);
2002 SYNTH_BENDER(m, chn, 1 << 13);
2004 mtx_lock(&scp->seq_lock);
2010 * *really* flush the output queue
2011 * flush the event queue, then flush the synthsisers.
2012 * Callers are IOCTL and close
2015 #define SEQ_SYNC_TIMEOUT 8
2017 seq_sync(struct seq_softc *scp)
2019 int i, rl, sync[16], done;
2021 mtx_assert(&scp->seq_lock, MA_OWNED);
2023 SEQ_DEBUG(4, printf("seq_sync: unit %d.\n", scp->unit));
2026 * Wait until output queue is empty. Check every so often to see if
2027 * the queue is moving along. If it isn't just abort.
2029 while (!MIDIQ_EMPTY(scp->out_q)) {
2031 if (!scp->playing) {
2033 cv_broadcast(&scp->state_cv);
2034 cv_broadcast(&scp->out_cv);
2036 rl = MIDIQ_LEN(scp->out_q);
2038 i = cv_timedwait_sig(&scp->out_cv,
2039 &scp->seq_lock, SEQ_SYNC_TIMEOUT * hz);
2041 if (i == EINTR || i == ERESTART) {
2044 * XXX: I don't know why we stop playing
2047 cv_broadcast(&scp->out_cv);
2051 if (i == EWOULDBLOCK && rl == MIDIQ_LEN(scp->out_q) &&
2052 scp->waiting == 0) {
2054 * A queue seems to be stuck up. Give up and clear
2057 MIDIQ_CLEAR(scp->out_q);
2059 cv_broadcast(&scp->state_cv);
2060 cv_broadcast(&scp->out_cv);
2061 cv_broadcast(&scp->reset_cv);
2064 * TODO: Consider if the raw devices need to be flushed
2067 SEQ_DEBUG(1, printf("seq_sync queue stuck, aborting\n"));
2075 * Since syncing a midi device might block, unlock scp->seq_lock.
2078 mtx_unlock(&scp->seq_lock);
2079 for (i = 0; i < scp->midi_number; i++)
2084 for (i = 0; i < scp->midi_number; i++)
2086 if (SYNTH_INSYNC(scp->midis[i]) == 0)
2096 mtx_lock(&scp->seq_lock);
2101 midi_cmdname(int cmd, midi_cmdtab *tab)
2103 while (tab->name != NULL) {
2104 if (cmd == tab->cmd)