kernel: Remove <sys/mutex.h> from all files that don't need it (2/2).
[dragonfly.git] / sys / dev / sound / midi / sequencer.c
blobfd4b1054ad402e1a2979e760ff27af651b0315b7
1 /*-
2 * Copyright (c) 2003 Mathew Kanner
3 * Copyright (c) 1993 Hannu Savolainen
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
29 * The sequencer personality manager.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: head/sys/dev/sound/midi/sequencer.c 274035 2014-11-03 11:11:45Z bapt $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/filio.h>
39 #include <sys/lock.h>
40 #include <sys/sockio.h>
41 #include <sys/fcntl.h>
42 #include <sys/proc.h>
43 #include <sys/sysctl.h>
45 #include <sys/kernel.h> /* for DATA_SET */
47 #include <sys/module.h>
48 #include <sys/conf.h>
49 #include <sys/file.h>
50 #include <sys/uio.h>
51 #include <sys/syslog.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
54 #include <sys/bus.h>
55 #include <machine/clock.h> /* for DELAY */
56 #include <sys/soundcard.h>
57 #include <sys/rman.h>
58 #include <sys/mman.h>
59 #include <sys/poll.h>
60 #include <sys/condvar.h>
61 #include <sys/kthread.h>
62 #include <sys/unistd.h>
63 #include <sys/device.h>
65 #ifdef HAVE_KERNEL_OPTION_HEADERS
66 #include "opt_snd.h"
67 #endif
69 #include <dev/sound/midi/midi.h>
70 #include <dev/sound/midi/midiq.h>
71 #include "synth_if.h"
73 #include <dev/sound/midi/sequencer.h>
75 #define TMR_TIMERBASE 13
77 #define SND_DEV_SEQ 1 /* Sequencer output /dev/sequencer (FM
78 * synthesizer and MIDI output) */
79 #define SND_DEV_MUSIC 8 /* /dev/music, level 2 interface */
81 /* Length of a sequencer event. */
82 #define EV_SZ 8
83 #define IEV_SZ 8
85 /* Lookup modes */
86 #define LOOKUP_EXIST (0)
87 #define LOOKUP_OPEN (1)
88 #define LOOKUP_CLOSE (2)
90 #define PCMMKMINOR(u, d, c) \
91 ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
92 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
93 #define MIDIUNIT(y) ((dev2unit(y) >> 4) & 0x0f)
94 #define MIDIDEV(y) (dev2unit(y) & 0x0f)
96 /* These are the entries to the sequencer driver. */
97 static d_open_t seq_open;
98 static d_close_t seq_close;
99 static d_ioctl_t seq_ioctl;
100 static d_read_t seq_read;
101 static d_write_t seq_write;
102 static d_kqfilter_t seq_kqfilter;
104 static void seq_filter_detach(struct knote *);
105 static int seq_filter_read(struct knote *, long);
106 static int seq_filter_write(struct knote *, long);
108 static struct dev_ops seq_ops = {
109 { "sequencer", 0, D_MPSAFE },
110 .d_open = seq_open,
111 .d_close = seq_close,
112 .d_read = seq_read,
113 .d_write = seq_write,
114 .d_ioctl = seq_ioctl,
115 .d_kqfilter = seq_kqfilter,
118 static struct filterops seq_read_filterops =
119 { FILTEROP_ISFD, NULL, seq_filter_detach, seq_filter_read };
120 static struct filterops seq_write_filterops =
121 { FILTEROP_ISFD, NULL, seq_filter_detach, seq_filter_write };
123 struct seq_softc {
124 KOBJ_FIELDS;
126 struct lock seq_lock, q_lock;
127 struct cv empty_cv, reset_cv, in_cv, out_cv, state_cv, th_cv;
129 MIDIQ_HEAD(, u_char) in_q, out_q;
131 u_long flags;
132 /* Flags (protected by flag_mtx of mididev_info) */
133 int fflags; /* Access mode */
134 int music;
136 int out_water; /* Sequence output threshould */
137 snd_sync_parm sync_parm; /* AIOSYNC parameter set */
138 struct thread *sync_thread; /* AIOSYNCing thread */
139 struct kqinfo in_kq, out_kq;
140 int midi_number;
141 struct cdev *seqdev, *musicdev;
142 int unit;
143 int maxunits;
144 kobj_t *midis;
145 int *midi_flags;
146 kobj_t mapper;
147 void *mapper_cookie;
148 struct timeval timerstop, timersub;
149 int timerbase, tempo;
150 int timerrun;
151 int done;
152 int playing;
153 int recording;
154 int busy;
155 int pre_event_timeout;
156 int waiting;
160 * Module specific stuff, including how many sequecers
161 * we currently own.
164 SYSCTL_NODE(_hw_midi, OID_AUTO, seq, CTLFLAG_RD, 0, "Midi sequencer");
166 int seq_debug;
167 /* XXX: should this be moved into debug.midi? */
168 SYSCTL_INT(_hw_midi_seq, OID_AUTO, debug, CTLFLAG_RW, &seq_debug, 0, "");
170 midi_cmdtab cmdtab_seqevent[] = {
171 {SEQ_NOTEOFF, "SEQ_NOTEOFF"},
172 {SEQ_NOTEON, "SEQ_NOTEON"},
173 {SEQ_WAIT, "SEQ_WAIT"},
174 {SEQ_PGMCHANGE, "SEQ_PGMCHANGE"},
175 {SEQ_SYNCTIMER, "SEQ_SYNCTIMER"},
176 {SEQ_MIDIPUTC, "SEQ_MIDIPUTC"},
177 {SEQ_DRUMON, "SEQ_DRUMON"},
178 {SEQ_DRUMOFF, "SEQ_DRUMOFF"},
179 {SEQ_ECHO, "SEQ_ECHO"},
180 {SEQ_AFTERTOUCH, "SEQ_AFTERTOUCH"},
181 {SEQ_CONTROLLER, "SEQ_CONTROLLER"},
182 {SEQ_BALANCE, "SEQ_BALANCE"},
183 {SEQ_VOLMODE, "SEQ_VOLMODE"},
184 {SEQ_FULLSIZE, "SEQ_FULLSIZE"},
185 {SEQ_PRIVATE, "SEQ_PRIVATE"},
186 {SEQ_EXTENDED, "SEQ_EXTENDED"},
187 {EV_SEQ_LOCAL, "EV_SEQ_LOCAL"},
188 {EV_TIMING, "EV_TIMING"},
189 {EV_CHN_COMMON, "EV_CHN_COMMON"},
190 {EV_CHN_VOICE, "EV_CHN_VOICE"},
191 {EV_SYSEX, "EV_SYSEX"},
192 {-1, NULL},
195 midi_cmdtab cmdtab_seqioctl[] = {
196 {SNDCTL_SEQ_RESET, "SNDCTL_SEQ_RESET"},
197 {SNDCTL_SEQ_SYNC, "SNDCTL_SEQ_SYNC"},
198 {SNDCTL_SYNTH_INFO, "SNDCTL_SYNTH_INFO"},
199 {SNDCTL_SEQ_CTRLRATE, "SNDCTL_SEQ_CTRLRATE"},
200 {SNDCTL_SEQ_GETOUTCOUNT, "SNDCTL_SEQ_GETOUTCOUNT"},
201 {SNDCTL_SEQ_GETINCOUNT, "SNDCTL_SEQ_GETINCOUNT"},
202 {SNDCTL_SEQ_PERCMODE, "SNDCTL_SEQ_PERCMODE"},
203 {SNDCTL_FM_LOAD_INSTR, "SNDCTL_FM_LOAD_INSTR"},
204 {SNDCTL_SEQ_TESTMIDI, "SNDCTL_SEQ_TESTMIDI"},
205 {SNDCTL_SEQ_RESETSAMPLES, "SNDCTL_SEQ_RESETSAMPLES"},
206 {SNDCTL_SEQ_NRSYNTHS, "SNDCTL_SEQ_NRSYNTHS"},
207 {SNDCTL_SEQ_NRMIDIS, "SNDCTL_SEQ_NRMIDIS"},
208 {SNDCTL_SEQ_GETTIME, "SNDCTL_SEQ_GETTIME"},
209 {SNDCTL_MIDI_INFO, "SNDCTL_MIDI_INFO"},
210 {SNDCTL_SEQ_THRESHOLD, "SNDCTL_SEQ_THRESHOLD"},
211 {SNDCTL_SYNTH_MEMAVL, "SNDCTL_SYNTH_MEMAVL"},
212 {SNDCTL_FM_4OP_ENABLE, "SNDCTL_FM_4OP_ENABLE"},
213 {SNDCTL_PMGR_ACCESS, "SNDCTL_PMGR_ACCESS"},
214 {SNDCTL_SEQ_PANIC, "SNDCTL_SEQ_PANIC"},
215 {SNDCTL_SEQ_OUTOFBAND, "SNDCTL_SEQ_OUTOFBAND"},
216 {SNDCTL_TMR_TIMEBASE, "SNDCTL_TMR_TIMEBASE"},
217 {SNDCTL_TMR_START, "SNDCTL_TMR_START"},
218 {SNDCTL_TMR_STOP, "SNDCTL_TMR_STOP"},
219 {SNDCTL_TMR_CONTINUE, "SNDCTL_TMR_CONTINUE"},
220 {SNDCTL_TMR_TEMPO, "SNDCTL_TMR_TEMPO"},
221 {SNDCTL_TMR_SOURCE, "SNDCTL_TMR_SOURCE"},
222 {SNDCTL_TMR_METRONOME, "SNDCTL_TMR_METRONOME"},
223 {SNDCTL_TMR_SELECT, "SNDCTL_TMR_SELECT"},
224 {SNDCTL_MIDI_PRETIME, "SNDCTL_MIDI_PRETIME"},
225 {AIONWRITE, "AIONWRITE"},
226 {AIOGSIZE, "AIOGSIZE"},
227 {AIOSSIZE, "AIOSSIZE"},
228 {AIOGFMT, "AIOGFMT"},
229 {AIOSFMT, "AIOSFMT"},
230 {AIOGMIX, "AIOGMIX"},
231 {AIOSMIX, "AIOSMIX"},
232 {AIOSTOP, "AIOSTOP"},
233 {AIOSYNC, "AIOSYNC"},
234 {AIOGCAP, "AIOGCAP"},
235 {-1, NULL},
238 midi_cmdtab cmdtab_timer[] = {
239 {TMR_WAIT_REL, "TMR_WAIT_REL"},
240 {TMR_WAIT_ABS, "TMR_WAIT_ABS"},
241 {TMR_STOP, "TMR_STOP"},
242 {TMR_START, "TMR_START"},
243 {TMR_CONTINUE, "TMR_CONTINUE"},
244 {TMR_TEMPO, "TMR_TEMPO"},
245 {TMR_ECHO, "TMR_ECHO"},
246 {TMR_CLOCK, "TMR_CLOCK"},
247 {TMR_SPP, "TMR_SPP"},
248 {TMR_TIMESIG, "TMR_TIMESIG"},
249 {-1, NULL},
252 midi_cmdtab cmdtab_seqcv[] = {
253 {MIDI_NOTEOFF, "MIDI_NOTEOFF"},
254 {MIDI_NOTEON, "MIDI_NOTEON"},
255 {MIDI_KEY_PRESSURE, "MIDI_KEY_PRESSURE"},
256 {-1, NULL},
259 midi_cmdtab cmdtab_seqccmn[] = {
260 {MIDI_CTL_CHANGE, "MIDI_CTL_CHANGE"},
261 {MIDI_PGM_CHANGE, "MIDI_PGM_CHANGE"},
262 {MIDI_CHN_PRESSURE, "MIDI_CHN_PRESSURE"},
263 {MIDI_PITCH_BEND, "MIDI_PITCH_BEND"},
264 {MIDI_SYSTEM_PREFIX, "MIDI_SYSTEM_PREFIX"},
265 {-1, NULL},
268 #ifndef KOBJMETHOD_END
269 #define KOBJMETHOD_END { NULL, NULL }
270 #endif
273 * static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m);
276 static kobj_method_t seq_methods[] = {
277 /* KOBJMETHOD(mpu_provider,mpu401_mprovider), */
278 KOBJMETHOD_END
281 DEFINE_CLASS(sequencer, seq_methods, 0);
283 /* The followings are the local function. */
284 static int seq_convertold(u_char *event, u_char *out);
287 * static void seq_midiinput(struct seq_softc * scp, void *md);
289 static void seq_reset(struct seq_softc *scp);
290 static int seq_sync(struct seq_softc *scp);
292 static int seq_processevent(struct seq_softc *scp, u_char *event);
294 static int seq_timing(struct seq_softc *scp, u_char *event);
295 static int seq_local(struct seq_softc *scp, u_char *event);
297 static int seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event);
298 static int seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event);
299 static int seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event);
301 static int seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md);
302 void seq_copytoinput(struct seq_softc *scp, u_char *event, int len);
303 int seq_modevent(module_t mod, int type, void *data);
304 struct seq_softc *seqs[10];
305 static struct lock seqinfo_lock;
306 static u_long nseq = 0;
308 static void timer_start(struct seq_softc *t);
309 static void timer_stop(struct seq_softc *t);
310 static void timer_setvals(struct seq_softc *t, int tempo, int timerbase);
311 static void timer_wait(struct seq_softc *t, int ticks, int wait_abs);
312 static int timer_now(struct seq_softc *t);
315 static void
316 timer_start(struct seq_softc *t)
318 t->timerrun = 1;
319 getmicrotime(&t->timersub);
322 static void
323 timer_continue(struct seq_softc *t)
325 struct timeval now;
327 if (t->timerrun == 1)
328 return;
329 t->timerrun = 1;
330 getmicrotime(&now);
331 timevalsub(&now, &t->timerstop);
332 timevaladd(&t->timersub, &now);
335 static void
336 timer_stop(struct seq_softc *t)
338 t->timerrun = 0;
339 getmicrotime(&t->timerstop);
342 static void
343 timer_setvals(struct seq_softc *t, int tempo, int timerbase)
345 t->tempo = tempo;
346 t->timerbase = timerbase;
349 static void
350 timer_wait(struct seq_softc *t, int ticks, int wait_abs)
352 struct timeval now, when;
353 int ret;
354 unsigned long long i;
356 while (t->timerrun == 0) {
357 SEQ_DEBUG(2, kprintf("Timer wait when timer isn't running\n"));
359 * The old sequencer used timeouts that only increased
360 * the timer when the timer was running.
361 * Hence the sequencer would stick (?) if the
362 * timer was disabled.
364 cv_wait(&t->reset_cv, &t->seq_lock);
365 if (t->playing == 0)
366 return;
369 i = ticks * 60ull * 1000000ull / (t->tempo * t->timerbase);
371 when.tv_sec = i / 1000000;
372 when.tv_usec = i % 1000000;
374 #if 0
375 kprintf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n",
376 t->tempo, t->timerbase, ticks, wait_abs, i);
377 #endif
379 if (wait_abs != 0) {
380 getmicrotime(&now);
381 timevalsub(&now, &t->timersub);
382 timevalsub(&when, &now);
384 if (when.tv_sec < 0 || when.tv_usec < 0) {
385 SEQ_DEBUG(3,
386 kprintf("seq_timer error negative time %lds.%06lds\n",
387 (long)when.tv_sec, (long)when.tv_usec));
388 return;
390 i = when.tv_sec * 1000000ull;
391 i += when.tv_usec;
392 i *= hz;
393 i /= 1000000ull;
394 #if 0
395 kprintf("seq_timer usec %llu ticks %llu\n",
396 when.tv_sec * 1000000ull + when.tv_usec, i);
397 #endif
398 t->waiting = 1;
399 ret = cv_timedwait(&t->reset_cv, &t->seq_lock, i + 1);
400 t->waiting = 0;
402 if (ret != EWOULDBLOCK)
403 SEQ_DEBUG(3, kprintf("seq_timer didn't timeout\n"));
407 static int
408 timer_now(struct seq_softc *t)
410 struct timeval now;
411 unsigned long long i;
412 int ret;
414 if (t->timerrun == 0)
415 now = t->timerstop;
416 else
417 getmicrotime(&now);
419 timevalsub(&now, &t->timersub);
421 i = now.tv_sec * 1000000ull;
422 i += now.tv_usec;
423 i *= t->timerbase;
424 /* i /= t->tempo; */
425 i /= 1000000ull;
427 ret = i;
429 * kprintf("timer_now: %llu %d\n", i, ret);
432 return ret;
435 static void
436 seq_eventthread(void *arg)
438 struct seq_softc *scp = arg;
439 char event[EV_SZ];
441 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
442 SEQ_DEBUG(2, kprintf("seq_eventthread started\n"));
443 while (scp->done == 0) {
444 restart:
445 while (scp->playing == 0) {
446 cv_wait(&scp->state_cv, &scp->seq_lock);
447 if (scp->done)
448 goto done;
451 while (MIDIQ_EMPTY(scp->out_q)) {
452 cv_broadcast(&scp->empty_cv);
453 cv_wait(&scp->out_cv, &scp->seq_lock);
454 if (scp->playing == 0)
455 goto restart;
456 if (scp->done)
457 goto done;
460 MIDIQ_DEQ(scp->out_q, event, EV_SZ);
462 if (MIDIQ_AVAIL(scp->out_q) < scp->out_water) {
463 cv_broadcast(&scp->out_cv);
464 KNOTE(&scp->out_kq.ki_note, 0);
466 seq_processevent(scp, event);
469 done:
470 cv_broadcast(&scp->th_cv);
471 lockmgr(&scp->seq_lock, LK_RELEASE);
472 SEQ_DEBUG(2, kprintf("seq_eventthread finished\n"));
473 kthread_exit();
477 * seq_processevent: This maybe called by the event thread or the IOCTL
478 * handler for queued and out of band events respectively.
480 static int
481 seq_processevent(struct seq_softc *scp, u_char *event)
483 int ret;
484 kobj_t m;
486 ret = 0;
488 if (event[0] == EV_SEQ_LOCAL)
489 ret = seq_local(scp, event);
490 else if (event[0] == EV_TIMING)
491 ret = seq_timing(scp, event);
492 else if (event[0] != EV_CHN_VOICE &&
493 event[0] != EV_CHN_COMMON &&
494 event[0] != EV_SYSEX &&
495 event[0] != SEQ_MIDIPUTC) {
496 ret = 1;
497 SEQ_DEBUG(2, kprintf("seq_processevent not known %d\n",
498 event[0]));
499 } else if (seq_fetch_mid(scp, event[1], &m) != 0) {
500 ret = 1;
501 SEQ_DEBUG(2, kprintf("seq_processevent midi unit not found %d\n",
502 event[1]));
503 } else
504 switch (event[0]) {
505 case EV_CHN_VOICE:
506 ret = seq_chnvoice(scp, m, event);
507 break;
508 case EV_CHN_COMMON:
509 ret = seq_chncommon(scp, m, event);
510 break;
511 case EV_SYSEX:
512 ret = seq_sysex(scp, m, event);
513 break;
514 case SEQ_MIDIPUTC:
515 lockmgr(&scp->seq_lock, LK_RELEASE);
516 ret = SYNTH_WRITERAW(m, &event[2], 1);
517 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
518 break;
520 return ret;
523 static int
524 seq_addunit(void)
526 struct seq_softc *scp;
527 int ret;
528 u_char *buf;
530 /* Allocate the softc. */
531 ret = ENOMEM;
532 scp = kmalloc(sizeof(*scp), M_DEVBUF, M_WAITOK | M_ZERO);
533 kobj_init((kobj_t)scp, &sequencer_class);
535 buf = kmalloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_WAITOK | M_ZERO);
536 MIDIQ_INIT(scp->in_q, buf, EV_SZ * 1024);
537 buf = kmalloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_WAITOK | M_ZERO);
538 MIDIQ_INIT(scp->out_q, buf, EV_SZ * 1024);
539 ret = EINVAL;
541 scp->midis = kmalloc(sizeof(kobj_t) * 32, M_TEMP, M_WAITOK | M_ZERO);
542 scp->midi_flags = kmalloc(sizeof(*scp->midi_flags) * 32, M_TEMP,
543 M_WAITOK | M_ZERO);
545 scp->flags = 0;
547 lockinit(&scp->seq_lock, "seqflq", 0, LK_CANRECURSE);
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)
561 goto err;
563 scp->seqdev = make_dev(&seq_ops,
564 MIDIMKMINOR(scp->unit, SND_DEV_SEQ, 0), UID_ROOT,
565 GID_WHEEL, 0666, "sequencer%d", scp->unit);
567 scp->musicdev = make_dev(&seq_ops,
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)
572 goto err;
574 * TODO: Add to list of sequencers this module provides
577 ret =
578 kthread_create
579 (seq_eventthread, scp, NULL,
580 "sequencer %02d", scp->unit);
582 if (ret)
583 goto err;
585 scp->seqdev->si_drv1 = scp->musicdev->si_drv1 = scp;
587 SEQ_DEBUG(2, kprintf("sequencer %d created scp %p\n", scp->unit, scp));
589 ret = 0;
591 lockmgr(&seqinfo_lock, LK_EXCLUSIVE);
592 seqs[nseq++] = scp;
593 lockmgr(&seqinfo_lock, LK_RELEASE);
595 goto ok;
597 err:
598 if (scp != NULL) {
599 if (scp->seqdev != NULL)
600 destroy_dev(scp->seqdev);
601 if (scp->musicdev != NULL)
602 destroy_dev(scp->musicdev);
604 * TODO: Destroy mutex and cv
606 if (scp->midis != NULL)
607 kfree(scp->midis, M_TEMP);
608 if (scp->midi_flags != NULL)
609 kfree(scp->midi_flags, M_TEMP);
610 if (scp->out_q.b)
611 kfree(scp->out_q.b, M_TEMP);
612 if (scp->in_q.b)
613 kfree(scp->in_q.b, M_TEMP);
614 kfree(scp, M_DEVBUF);
617 return ret;
620 static int
621 seq_delunit(int unit)
623 struct seq_softc *scp = seqs[unit];
624 int i;
626 //SEQ_DEBUG(4, kprintf("seq_delunit: %d\n", unit));
627 SEQ_DEBUG(1, kprintf("seq_delunit: 1 \n"));
628 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
630 scp->playing = 0;
631 scp->done = 1;
632 cv_broadcast(&scp->out_cv);
633 cv_broadcast(&scp->state_cv);
634 cv_broadcast(&scp->reset_cv);
635 SEQ_DEBUG(1, kprintf("seq_delunit: 2 \n"));
636 cv_wait(&scp->th_cv, &scp->seq_lock);
637 SEQ_DEBUG(1, kprintf("seq_delunit: 3.0 \n"));
638 lockmgr(&scp->seq_lock, LK_RELEASE);
639 SEQ_DEBUG(1, kprintf("seq_delunit: 3.1 \n"));
641 cv_destroy(&scp->state_cv);
642 SEQ_DEBUG(1, kprintf("seq_delunit: 4 \n"));
643 cv_destroy(&scp->empty_cv);
644 SEQ_DEBUG(1, kprintf("seq_delunit: 5 \n"));
645 cv_destroy(&scp->reset_cv);
646 SEQ_DEBUG(1, kprintf("seq_delunit: 6 \n"));
647 cv_destroy(&scp->out_cv);
648 SEQ_DEBUG(1, kprintf("seq_delunit: 7 \n"));
649 cv_destroy(&scp->in_cv);
650 SEQ_DEBUG(1, kprintf("seq_delunit: 8 \n"));
651 cv_destroy(&scp->th_cv);
653 SEQ_DEBUG(1, kprintf("seq_delunit: 10 \n"));
654 if (scp->seqdev)
655 destroy_dev(scp->seqdev);
656 SEQ_DEBUG(1, kprintf("seq_delunit: 11 \n"));
657 if (scp->musicdev)
658 destroy_dev(scp->musicdev);
659 SEQ_DEBUG(1, kprintf("seq_delunit: 12 \n"));
660 scp->seqdev = scp->musicdev = NULL;
661 if (scp->midis != NULL)
662 kfree(scp->midis, M_TEMP);
663 SEQ_DEBUG(1, kprintf("seq_delunit: 13 \n"));
664 if (scp->midi_flags != NULL)
665 kfree(scp->midi_flags, M_TEMP);
666 SEQ_DEBUG(1, kprintf("seq_delunit: 14 \n"));
667 kfree(scp->out_q.b, M_TEMP);
668 SEQ_DEBUG(1, kprintf("seq_delunit: 15 \n"));
669 kfree(scp->in_q.b, M_TEMP);
671 SEQ_DEBUG(1, kprintf("seq_delunit: 16 \n"));
673 lockuninit(&scp->seq_lock);
674 SEQ_DEBUG(1, kprintf("seq_delunit: 17 \n"));
675 kfree(scp, M_DEVBUF);
677 lockmgr(&seqinfo_lock, LK_EXCLUSIVE);
678 for (i = unit; i < (nseq - 1); i++)
679 seqs[i] = seqs[i + 1];
680 nseq--;
681 lockmgr(&seqinfo_lock, LK_RELEASE);
683 return 0;
687 seq_modevent(module_t mod, int type, void *data)
689 int retval, r;
691 retval = 0;
693 switch (type) {
694 case MOD_LOAD:
695 lockinit(&seqinfo_lock, "seqmod", 0, LK_CANRECURSE);
696 retval = seq_addunit();
697 break;
699 case MOD_UNLOAD:
700 while (nseq) {
701 r = seq_delunit(nseq - 1);
702 if (r) {
703 retval = r;
704 break;
707 if (nseq == 0) {
708 retval = 0;
709 lockuninit(&seqinfo_lock);
711 break;
713 default:
714 break;
717 return retval;
720 static int
721 seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md)
724 if (unit > scp->midi_number || unit < 0)
725 return EINVAL;
727 *md = scp->midis[unit];
729 return 0;
733 seq_open(struct dev_open_args *ap)
735 cdev_t i_dev = ap->a_head.a_dev;
736 int flags = ap->a_oflags;
737 struct seq_softc *scp = i_dev->si_drv1;
738 int i;
740 if (scp == NULL)
741 return ENXIO;
743 SEQ_DEBUG(3, kprintf("seq_open: scp %p unit %d, flags 0x%x.\n",
744 scp, scp->unit, flags));
747 * Mark this device busy.
750 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
751 if (scp->busy) {
752 lockmgr(&scp->seq_lock, LK_RELEASE);
753 SEQ_DEBUG(2, kprintf("seq_open: unit %d is busy.\n", scp->unit));
754 return EBUSY;
756 scp->fflags = flags;
758 if ((scp->fflags & O_NONBLOCK) != 0)
759 scp->flags |= SEQ_F_NBIO;
761 scp->music = MIDIDEV(i_dev) == SND_DEV_MUSIC;
764 * Enumerate the available midi devices
766 scp->midi_number = 0;
767 scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie);
769 if (scp->maxunits == 0)
770 SEQ_DEBUG(2, kprintf("seq_open: no midi devices\n"));
772 for (i = 0; i < scp->maxunits; i++) {
773 scp->midis[scp->midi_number] =
774 midimapper_fetch_synth(scp->mapper, scp->mapper_cookie, i);
775 if (scp->midis[scp->midi_number]) {
776 if (SYNTH_OPEN(scp->midis[scp->midi_number], scp,
777 scp->fflags) != 0)
778 scp->midis[scp->midi_number] = NULL;
779 else {
780 scp->midi_flags[scp->midi_number] =
781 SYNTH_QUERY(scp->midis[scp->midi_number]);
782 scp->midi_number++;
787 timer_setvals(scp, 60, 100);
789 timer_start(scp);
790 timer_stop(scp);
792 * actually, if we're in rdonly mode, we should start the timer
795 * TODO: Handle recording now
798 scp->out_water = MIDIQ_SIZE(scp->out_q) / 2;
800 scp->busy = 1;
801 lockmgr(&scp->seq_lock, LK_RELEASE);
803 SEQ_DEBUG(2, kprintf("seq_open: opened, mode %s.\n",
804 scp->music ? "music" : "sequencer"));
805 SEQ_DEBUG(2,
806 kprintf("Sequencer %d %p opened maxunits %d midi_number %d:\n",
807 scp->unit, scp, scp->maxunits, scp->midi_number));
808 for (i = 0; i < scp->midi_number; i++)
809 SEQ_DEBUG(3, kprintf(" midi %d %p\n", i, scp->midis[i]));
811 return 0;
815 * seq_close
818 seq_close(struct dev_close_args *ap)
820 cdev_t i_dev = ap->a_head.a_dev;
821 int i;
822 struct seq_softc *scp = i_dev->si_drv1;
823 int ret;
825 if (scp == NULL)
826 return ENXIO;
828 SEQ_DEBUG(2, kprintf("seq_close: unit %d.\n", scp->unit));
830 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
832 ret = ENXIO;
833 if (scp->busy == 0)
834 goto err;
836 seq_reset(scp);
837 seq_sync(scp);
839 for (i = 0; i < scp->midi_number; i++)
840 if (scp->midis[i])
841 SYNTH_CLOSE(scp->midis[i]);
843 midimapper_close(scp->mapper, scp->mapper_cookie);
845 timer_stop(scp);
847 scp->busy = 0;
848 ret = 0;
850 err:
851 SEQ_DEBUG(3, kprintf("seq_close: closed ret = %d.\n", ret));
852 lockmgr(&scp->seq_lock, LK_RELEASE);
853 return ret;
857 seq_read(struct dev_read_args *ap)
859 cdev_t i_dev = ap->a_head.a_dev;
860 struct uio *uio = ap->a_uio;
861 int ioflag = ap->a_ioflag;
862 int retval, used;
863 struct seq_softc *scp = i_dev->si_drv1;
865 #define SEQ_RSIZE 32
866 u_char buf[SEQ_RSIZE];
868 if (scp == NULL)
869 return ENXIO;
871 SEQ_DEBUG(7, kprintf("seq_read: unit %d, resid %zd.\n",
872 scp->unit, uio->uio_resid));
874 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
875 if ((scp->fflags & FREAD) == 0) {
876 SEQ_DEBUG(2, kprintf("seq_read: unit %d is not for reading.\n",
877 scp->unit));
878 retval = EIO;
879 goto err1;
882 * Begin recording.
885 * if ((scp->flags & SEQ_F_READING) == 0)
888 * TODO, start recording if not alread
892 * I think the semantics are to return as soon
893 * as possible.
894 * Second thought, it doens't seem like midimoutain
895 * expects that at all.
896 * TODO: Look up in some sort of spec
899 while (uio->uio_resid > 0) {
900 while (MIDIQ_EMPTY(scp->in_q)) {
901 retval = EWOULDBLOCK;
903 * I wish I knew which one to care about
906 if (scp->fflags & O_NONBLOCK)
907 goto err1;
908 if (ioflag & O_NONBLOCK)
909 goto err1;
911 retval = cv_wait_sig(&scp->in_cv, &scp->seq_lock);
912 if (retval == EINTR)
913 goto err1;
916 used = MIN(MIDIQ_LEN(scp->in_q), uio->uio_resid);
917 used = MIN(used, SEQ_RSIZE);
919 SEQ_DEBUG(8, kprintf("midiread: uiomove cc=%d\n", used));
920 MIDIQ_DEQ(scp->in_q, buf, used);
921 retval = uiomove(buf, used, uio);
922 if (retval)
923 goto err1;
926 retval = 0;
927 err1:
928 lockmgr(&scp->seq_lock, LK_RELEASE);
929 SEQ_DEBUG(6, kprintf("seq_read: ret %d, resid %zd.\n",
930 retval, uio->uio_resid));
932 return retval;
936 seq_write(struct dev_write_args *ap)
938 cdev_t i_dev = ap->a_head.a_dev;
939 struct uio *uio = ap->a_uio;
940 int ioflag = ap->a_ioflag;
941 u_char event[EV_SZ], newevent[EV_SZ], ev_code;
942 struct seq_softc *scp = i_dev->si_drv1;
943 int retval;
944 int used;
946 SEQ_DEBUG(7, kprintf("seq_write: unit %d, resid %zd.\n",
947 scp->unit, uio->uio_resid));
949 if (scp == NULL)
950 return ENXIO;
952 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
954 if ((scp->fflags & FWRITE) == 0) {
955 SEQ_DEBUG(2, kprintf("seq_write: unit %d is not for writing.\n",
956 scp->unit));
957 retval = EIO;
958 goto err0;
960 while (uio->uio_resid > 0) {
961 while (MIDIQ_AVAIL(scp->out_q) == 0) {
962 retval = EWOULDBLOCK;
963 if (scp->fflags & O_NONBLOCK)
964 goto err0;
965 if (ioflag & O_NONBLOCK)
966 goto err0;
967 SEQ_DEBUG(8, kprintf("seq_write cvwait\n"));
969 scp->playing = 1;
970 cv_broadcast(&scp->out_cv);
971 cv_broadcast(&scp->state_cv);
973 retval = cv_wait_sig(&scp->out_cv, &scp->seq_lock);
975 * We slept, maybe things have changed since last
976 * dying check
978 if (retval == EINTR)
979 goto err0;
980 #if 0
982 * Useless test
984 if (scp != i_dev->si_drv1)
985 retval = ENXIO;
986 #endif
989 used = MIN(uio->uio_resid, 4);
991 SEQ_DEBUG(8, kprintf("seqout: resid %zd len %jd avail %jd\n",
992 uio->uio_resid, (intmax_t)MIDIQ_LEN(scp->out_q),
993 (intmax_t)MIDIQ_AVAIL(scp->out_q)));
995 if (used != 4) {
996 retval = ENXIO;
997 goto err0;
999 retval = uiomove(event, used, uio);
1000 if (retval)
1001 goto err0;
1003 ev_code = event[0];
1004 SEQ_DEBUG(8, kprintf("seq_write: unit %d, event %s.\n",
1005 scp->unit, midi_cmdname(ev_code, cmdtab_seqevent)));
1007 /* Have a look at the event code. */
1008 if (ev_code == SEQ_FULLSIZE) {
1011 * TODO: restore code for SEQ_FULLSIZE
1013 #if 0
1015 * A long event, these are the patches/samples for a
1016 * synthesizer.
1018 midiunit = *(u_short *)&event[2];
1019 lockmgr(&sd->seq_lock, LK_EXCLUSIVE);
1020 ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
1021 lockmgr(&sd->seq_lock, LK_RELEASE);
1022 if (ret != 0)
1023 return (ret);
1025 SEQ_DEBUG(kprintf("seq_write: loading a patch to the unit %d.\n", midiunit));
1027 ret = md->synth.loadpatch(md, *(short *)&event[0], buf,
1028 p + 4, count, 0);
1029 return (ret);
1030 #else
1032 * For now, just flush the darn buffer
1034 SEQ_DEBUG(2,
1035 kprintf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
1036 while (uio->uio_resid > 0) {
1037 retval = uiomove(event, EV_SZ, uio);
1038 if (retval)
1039 goto err0;
1042 retval = 0;
1043 goto err0;
1044 #endif
1046 retval = EINVAL;
1047 if (ev_code >= 128) {
1050 * Some sort of an extended event. The size is eight
1051 * bytes. scoop extra info.
1053 if (scp->music && ev_code == SEQ_EXTENDED) {
1054 SEQ_DEBUG(2, kprintf("seq_write: invalid level two event %x.\n", ev_code));
1055 goto err0;
1057 if (uiomove((caddr_t)&event[4], 4, uio)) {
1058 SEQ_DEBUG(2,
1059 kprintf("seq_write: user memory mangled?\n"));
1060 goto err0;
1062 } else {
1064 * Size four event.
1066 if (scp->music) {
1067 SEQ_DEBUG(2, kprintf("seq_write: four byte event in music mode.\n"));
1068 goto err0;
1071 if (ev_code == SEQ_MIDIPUTC) {
1073 * TODO: event[2] is unit number to receive char.
1074 * Range check it.
1077 if (scp->music) {
1078 #ifdef not_ever_ever
1079 if (event[0] == EV_TIMING &&
1080 (event[1] == TMR_START || event[1] == TMR_STOP)) {
1082 * For now, try to make midimoutain work by
1083 * forcing these events to be processed
1084 * immediatly.
1086 seq_processevent(scp, event);
1087 } else
1088 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1089 #else
1090 MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1091 #endif
1092 } else {
1093 if (seq_convertold(event, newevent) > 0)
1094 MIDIQ_ENQ(scp->out_q, newevent, EV_SZ);
1095 #if 0
1096 else
1097 goto err0;
1098 #endif
1103 scp->playing = 1;
1104 cv_broadcast(&scp->state_cv);
1105 cv_broadcast(&scp->out_cv);
1107 retval = 0;
1109 err0:
1110 SEQ_DEBUG(6,
1111 kprintf("seq_write done: leftover buffer length %zd retval %d\n",
1112 uio->uio_resid, retval));
1113 lockmgr(&scp->seq_lock, LK_RELEASE);
1114 return retval;
1118 seq_ioctl(struct dev_ioctl_args *ap)
1120 cdev_t i_dev = ap->a_head.a_dev;
1121 u_long cmd = ap->a_cmd;
1122 caddr_t arg = ap->a_data;
1123 int mode = ap->a_fflag;
1124 int midiunit, ret, tmp;
1125 struct seq_softc *scp = i_dev->si_drv1;
1126 struct synth_info *synthinfo;
1127 struct midi_info *midiinfo;
1128 u_char event[EV_SZ];
1129 u_char newevent[EV_SZ];
1131 kobj_t md;
1134 * struct snd_size *sndsize;
1137 if (scp == NULL)
1138 return ENXIO;
1140 SEQ_DEBUG(6, kprintf("seq_ioctl: unit %d, cmd %s.\n",
1141 scp->unit, midi_cmdname(cmd, cmdtab_seqioctl)));
1143 ret = 0;
1145 switch (cmd) {
1146 case SNDCTL_SEQ_GETTIME:
1148 * ioctl needed by libtse
1150 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1151 *(int *)arg = timer_now(scp);
1152 lockmgr(&scp->seq_lock, LK_RELEASE);
1153 SEQ_DEBUG(6, kprintf("seq_ioctl: gettime %d.\n", *(int *)arg));
1154 ret = 0;
1155 break;
1156 case SNDCTL_TMR_METRONOME:
1157 /* fallthrough */
1158 case SNDCTL_TMR_SOURCE:
1160 * Not implemented
1162 ret = 0;
1163 break;
1164 case SNDCTL_TMR_TEMPO:
1165 event[1] = TMR_TEMPO;
1166 event[4] = *(int *)arg & 0xFF;
1167 event[5] = (*(int *)arg >> 8) & 0xFF;
1168 event[6] = (*(int *)arg >> 16) & 0xFF;
1169 event[7] = (*(int *)arg >> 24) & 0xFF;
1170 goto timerevent;
1171 case SNDCTL_TMR_TIMEBASE:
1172 event[1] = TMR_TIMERBASE;
1173 event[4] = *(int *)arg & 0xFF;
1174 event[5] = (*(int *)arg >> 8) & 0xFF;
1175 event[6] = (*(int *)arg >> 16) & 0xFF;
1176 event[7] = (*(int *)arg >> 24) & 0xFF;
1177 goto timerevent;
1178 case SNDCTL_TMR_START:
1179 event[1] = TMR_START;
1180 goto timerevent;
1181 case SNDCTL_TMR_STOP:
1182 event[1] = TMR_STOP;
1183 goto timerevent;
1184 case SNDCTL_TMR_CONTINUE:
1185 event[1] = TMR_CONTINUE;
1186 timerevent:
1187 event[0] = EV_TIMING;
1188 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1189 if (!scp->music) {
1190 ret = EINVAL;
1191 lockmgr(&scp->seq_lock, LK_RELEASE);
1192 break;
1194 seq_processevent(scp, event);
1195 lockmgr(&scp->seq_lock, LK_RELEASE);
1196 break;
1197 case SNDCTL_TMR_SELECT:
1198 SEQ_DEBUG(2,
1199 kprintf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
1200 ret = EINVAL;
1201 break;
1202 case SNDCTL_SEQ_SYNC:
1203 if (mode == O_RDONLY) {
1204 ret = 0;
1205 break;
1207 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1208 ret = seq_sync(scp);
1209 lockmgr(&scp->seq_lock, LK_RELEASE);
1210 break;
1211 case SNDCTL_SEQ_PANIC:
1212 /* fallthrough */
1213 case SNDCTL_SEQ_RESET:
1215 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
1217 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1218 seq_reset(scp);
1219 lockmgr(&scp->seq_lock, LK_RELEASE);
1220 ret = 0;
1221 break;
1222 case SNDCTL_SEQ_TESTMIDI:
1223 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1225 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
1226 * device?".
1228 lockmgr(&scp->seq_lock, LK_RELEASE);
1229 break;
1230 #if 0
1231 case SNDCTL_SEQ_GETINCOUNT:
1232 if (mode == O_WRONLY)
1233 *(int *)arg = 0;
1234 else {
1235 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1236 *(int *)arg = scp->in_q.rl;
1237 lockmgr(&scp->seq_lock, LK_RELEASE);
1238 SEQ_DEBUG(kprintf("seq_ioctl: incount %d.\n",
1239 *(int *)arg));
1241 ret = 0;
1242 break;
1243 case SNDCTL_SEQ_GETOUTCOUNT:
1244 if (mode == O_RDONLY)
1245 *(int *)arg = 0;
1246 else {
1247 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1248 *(int *)arg = scp->out_q.fl;
1249 lockmgr(&scp->seq_lock, LK_RELEASE);
1250 SEQ_DEBUG(kprintf("seq_ioctl: outcount %d.\n",
1251 *(int *)arg));
1253 ret = 0;
1254 break;
1255 #endif
1256 case SNDCTL_SEQ_CTRLRATE:
1257 if (*(int *)arg != 0) {
1258 ret = EINVAL;
1259 break;
1261 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1262 *(int *)arg = scp->timerbase;
1263 lockmgr(&scp->seq_lock, LK_RELEASE);
1264 SEQ_DEBUG(3, kprintf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
1265 ret = 0;
1266 break;
1268 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES
1270 #if 0
1271 case SNDCTL_SEQ_RESETSAMPLES:
1272 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1273 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1274 lockmgr(&scp->seq_lock, LK_RELEASE);
1275 if (ret != 0)
1276 break;
1277 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1278 SND_DEV_MIDIN), cmd, arg, mode, td);
1279 break;
1280 #endif
1281 case SNDCTL_SEQ_NRSYNTHS:
1282 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1283 *(int *)arg = scp->midi_number;
1284 lockmgr(&scp->seq_lock, LK_RELEASE);
1285 SEQ_DEBUG(3, kprintf("seq_ioctl: synths %d.\n", *(int *)arg));
1286 ret = 0;
1287 break;
1288 case SNDCTL_SEQ_NRMIDIS:
1289 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1290 if (scp->music)
1291 *(int *)arg = 0;
1292 else {
1294 * TODO: count the numbder of devices that can WRITERAW
1296 *(int *)arg = scp->midi_number;
1298 lockmgr(&scp->seq_lock, LK_RELEASE);
1299 SEQ_DEBUG(3, kprintf("seq_ioctl: midis %d.\n", *(int *)arg));
1300 ret = 0;
1301 break;
1303 * TODO: ioctl SNDCTL_SYNTH_MEMAVL
1305 #if 0
1306 case SNDCTL_SYNTH_MEMAVL:
1307 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1308 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1309 lockmgr(&scp->seq_lock, LK_RELEASE);
1310 if (ret != 0)
1311 break;
1312 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg,
1313 SND_DEV_MIDIN), cmd, arg, mode, td);
1314 break;
1315 #endif
1316 case SNDCTL_SEQ_OUTOFBAND:
1317 for (ret = 0; ret < EV_SZ; ret++)
1318 event[ret] = (u_char)arg[0];
1320 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1321 if (scp->music)
1322 ret = seq_processevent(scp, event);
1323 else {
1324 if (seq_convertold(event, newevent) > 0)
1325 ret = seq_processevent(scp, newevent);
1326 else
1327 ret = EINVAL;
1329 lockmgr(&scp->seq_lock, LK_RELEASE);
1330 break;
1331 case SNDCTL_SYNTH_INFO:
1332 synthinfo = (struct synth_info *)arg;
1333 midiunit = synthinfo->device;
1334 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1335 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1336 bzero(synthinfo, sizeof(*synthinfo));
1337 synthinfo->name[0] = 'f';
1338 synthinfo->name[1] = 'a';
1339 synthinfo->name[2] = 'k';
1340 synthinfo->name[3] = 'e';
1341 synthinfo->name[4] = 's';
1342 synthinfo->name[5] = 'y';
1343 synthinfo->name[6] = 'n';
1344 synthinfo->name[7] = 't';
1345 synthinfo->name[8] = 'h';
1346 synthinfo->device = midiunit;
1347 synthinfo->synth_type = SYNTH_TYPE_MIDI;
1348 synthinfo->capabilities = scp->midi_flags[midiunit];
1349 ret = 0;
1350 } else
1351 ret = EINVAL;
1352 lockmgr(&scp->seq_lock, LK_RELEASE);
1353 break;
1354 case SNDCTL_MIDI_INFO:
1355 midiinfo = (struct midi_info *)arg;
1356 midiunit = midiinfo->device;
1357 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1358 if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1359 bzero(midiinfo, sizeof(*midiinfo));
1360 midiinfo->name[0] = 'f';
1361 midiinfo->name[1] = 'a';
1362 midiinfo->name[2] = 'k';
1363 midiinfo->name[3] = 'e';
1364 midiinfo->name[4] = 'm';
1365 midiinfo->name[5] = 'i';
1366 midiinfo->name[6] = 'd';
1367 midiinfo->name[7] = 'i';
1368 midiinfo->device = midiunit;
1369 midiinfo->capabilities = scp->midi_flags[midiunit];
1371 * TODO: What devtype?
1373 midiinfo->dev_type = 0x01;
1374 ret = 0;
1375 } else
1376 ret = EINVAL;
1377 lockmgr(&scp->seq_lock, LK_RELEASE);
1378 break;
1379 case SNDCTL_SEQ_THRESHOLD:
1380 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1381 RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1);
1382 scp->out_water = *(int *)arg;
1383 lockmgr(&scp->seq_lock, LK_RELEASE);
1384 SEQ_DEBUG(3, kprintf("seq_ioctl: water %d.\n", *(int *)arg));
1385 ret = 0;
1386 break;
1387 case SNDCTL_MIDI_PRETIME:
1388 tmp = *(int *)arg;
1389 if (tmp < 0)
1390 tmp = 0;
1391 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1392 scp->pre_event_timeout = (hz * tmp) / 10;
1393 *(int *)arg = scp->pre_event_timeout;
1394 lockmgr(&scp->seq_lock, LK_RELEASE);
1395 SEQ_DEBUG(3, kprintf("seq_ioctl: pretime %d.\n", *(int *)arg));
1396 ret = 0;
1397 break;
1398 case SNDCTL_FM_4OP_ENABLE:
1399 case SNDCTL_PMGR_IFACE:
1400 case SNDCTL_PMGR_ACCESS:
1402 * Patch manager and fm are ded, ded, ded.
1404 /* fallthrough */
1405 default:
1407 * TODO: Consider ioctl default case.
1408 * Old code used to
1409 * if ((scp->fflags & O_ACCMODE) == FREAD) {
1410 * ret = EIO;
1411 * break;
1413 * Then pass on the ioctl to device 0
1415 SEQ_DEBUG(2,
1416 kprintf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
1417 ret = EINVAL;
1418 break;
1421 return ret;
1425 seq_kqfilter(struct dev_kqfilter_args *ap)
1427 cdev_t dev = ap->a_head.a_dev;
1428 struct knote *kn = ap->a_kn;
1429 struct seq_softc *scp;
1430 struct klist *klist;
1432 ap->a_result = 0;
1433 scp = dev->si_drv1;
1435 switch (kn->kn_filter) {
1436 case EVFILT_READ:
1437 kn->kn_fop = &seq_read_filterops;
1438 kn->kn_hook = (caddr_t)scp;
1439 klist = &scp->in_kq.ki_note;
1440 break;
1441 case EVFILT_WRITE:
1442 kn->kn_fop = &seq_write_filterops;
1443 kn->kn_hook = (caddr_t)scp;
1444 klist = &scp->out_kq.ki_note;
1445 break;
1446 default:
1447 ap->a_result = EOPNOTSUPP;
1448 return (0);
1451 knote_insert(klist, kn);
1453 return(0);
1456 static void
1457 seq_filter_detach(struct knote *kn)
1459 struct seq_softc *scp = (struct seq_softc *)kn->kn_hook;
1460 struct klist *in_klist = &scp->in_kq.ki_note;
1461 struct klist *out_klist = &scp->out_kq.ki_note;
1463 knote_remove(in_klist, kn);
1464 knote_remove(out_klist, kn);
1467 static int
1468 seq_filter_read(struct knote *kn, long hint)
1470 struct seq_softc *scp = (struct seq_softc *)kn->kn_hook;
1471 int ready = 0;
1472 int lim;
1474 SEQ_DEBUG(3, kprintf("seq_filter_read: unit %d.\n", scp->unit));
1475 SEQ_DEBUG(1, kprintf("seq_filter_read: unit %d.\n", scp->unit));
1477 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1479 /* Look up the apropriate queue and select it. */
1481 /* TODO: Start recording. */
1483 /* Find out the boundary. */
1484 lim = 1;
1485 if (MIDIQ_LEN(scp->in_q) >= lim) {
1486 /* We can read now. */
1487 ready = 1;
1489 lockmgr(&scp->seq_lock, LK_RELEASE);
1491 return (ready);
1494 static int
1495 seq_filter_write(struct knote *kn, long hint)
1497 struct seq_softc *scp = (struct seq_softc *)kn->kn_hook;
1498 int ready = 0;
1499 int lim;
1501 SEQ_DEBUG(3, kprintf("seq_filter_write: unit %d.\n", scp->unit));
1502 SEQ_DEBUG(1, kprintf("seq_filter_write: unit %d.\n", scp->unit));
1504 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1506 /* Look up the apropriate queue and select it. */
1508 /* Start playing. */
1509 scp->playing = 1;
1510 cv_broadcast(&scp->state_cv);
1511 cv_broadcast(&scp->out_cv);
1513 lim = scp->out_water;
1515 if (MIDIQ_AVAIL(scp->out_q) >= lim) {
1516 /* We can write now. */
1517 ready = 1;
1519 lockmgr(&scp->seq_lock, LK_RELEASE);
1521 return (ready);
1524 #if 0
1525 static void
1526 sein_qtr(void *p, void /* mididev_info */ *md)
1528 struct seq_softc *scp;
1530 scp = (struct seq_softc *)p;
1532 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1534 /* Restart playing if we have the data to output. */
1535 if (scp->queueout_pending)
1536 seq_callback(scp, SEQ_CB_START | SEQ_CB_WR);
1537 /* Check the midi device if we are reading. */
1538 if ((scp->flags & SEQ_F_READING) != 0)
1539 seq_midiinput(scp, md);
1541 lockmgr(&scp->seq_lock, LK_RELEASE);
1544 #endif
1546 * seq_convertold
1547 * Was the old playevent. Use this to convert and old
1548 * style /dev/sequencer event to a /dev/music event
1550 static int
1551 seq_convertold(u_char *event, u_char *out)
1553 int used;
1554 u_char dev, chn, note, vel;
1556 out[0] = out[1] = out[2] = out[3] = out[4] = out[5] = out[6] =
1557 out[7] = 0;
1559 dev = 0;
1560 chn = event[1];
1561 note = event[2];
1562 vel = event[3];
1564 used = 0;
1566 restart:
1568 * TODO: Debug statement
1570 switch (event[0]) {
1571 case EV_TIMING:
1572 case EV_CHN_VOICE:
1573 case EV_CHN_COMMON:
1574 case EV_SYSEX:
1575 case EV_SEQ_LOCAL:
1576 out[0] = event[0];
1577 out[1] = event[1];
1578 out[2] = event[2];
1579 out[3] = event[3];
1580 out[4] = event[4];
1581 out[5] = event[5];
1582 out[6] = event[6];
1583 out[7] = event[7];
1584 used += 8;
1585 break;
1586 case SEQ_NOTEOFF:
1587 out[0] = EV_CHN_VOICE;
1588 out[1] = dev;
1589 out[2] = MIDI_NOTEOFF;
1590 out[3] = chn;
1591 out[4] = note;
1592 out[5] = 255;
1593 used += 4;
1594 break;
1596 case SEQ_NOTEON:
1597 out[0] = EV_CHN_VOICE;
1598 out[1] = dev;
1599 out[2] = MIDI_NOTEON;
1600 out[3] = chn;
1601 out[4] = note;
1602 out[5] = vel;
1603 used += 4;
1604 break;
1607 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4]
1610 case SEQ_PGMCHANGE:
1611 out[0] = EV_CHN_COMMON;
1612 out[1] = dev;
1613 out[2] = MIDI_PGM_CHANGE;
1614 out[3] = chn;
1615 out[4] = note;
1616 out[5] = vel;
1617 used += 4;
1618 break;
1620 out[0] = EV_TIMING;
1621 out[1] = dev;
1622 out[2] = MIDI_PGM_CHANGE;
1623 out[3] = chn;
1624 out[4] = note;
1625 out[5] = vel;
1626 SEQ_DEBUG(4,kprintf("seq_playevent: synctimer\n"));
1627 break;
1630 case SEQ_MIDIPUTC:
1631 SEQ_DEBUG(4,
1632 kprintf("seq_playevent: put data 0x%02x, unit %d.\n",
1633 event[1], event[2]));
1635 * Pass through to the midi device.
1636 * device = event[2]
1637 * data = event[1]
1639 out[0] = SEQ_MIDIPUTC;
1640 out[1] = dev;
1641 out[2] = chn;
1642 used += 4;
1643 break;
1644 #ifdef notyet
1645 case SEQ_ECHO:
1647 * This isn't handled here yet because I don't know if I can
1648 * just use four bytes events. There might be consequences
1649 * in the _read routing
1651 if (seq_copytoinput(scp, event, 4) == EAGAIN) {
1652 ret = QUEUEFULL;
1653 break;
1655 ret = MORE;
1656 break;
1657 #endif
1658 case SEQ_EXTENDED:
1659 switch (event[1]) {
1660 case SEQ_NOTEOFF:
1661 case SEQ_NOTEON:
1662 case SEQ_PGMCHANGE:
1663 event++;
1664 used = 4;
1665 goto restart;
1666 break;
1667 case SEQ_AFTERTOUCH:
1669 * SYNTH_AFTERTOUCH(md, event[3], event[4])
1671 case SEQ_BALANCE:
1673 * SYNTH_PANNING(md, event[3], (char)event[4])
1675 case SEQ_CONTROLLER:
1677 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5])
1679 case SEQ_VOLMODE:
1681 * SYNTH_VOLUMEMETHOD(md, event[3])
1683 default:
1684 SEQ_DEBUG(2,
1685 kprintf("seq_convertold: SEQ_EXTENDED type %d"
1686 "not handled\n", event[1]));
1687 break;
1689 break;
1690 case SEQ_WAIT:
1691 out[0] = EV_TIMING;
1692 out[1] = TMR_WAIT_REL;
1693 out[4] = event[2];
1694 out[5] = event[3];
1695 out[6] = event[4];
1697 SEQ_DEBUG(5, kprintf("SEQ_WAIT %d",
1698 event[2] + (event[3] << 8) + (event[4] << 24)));
1700 used += 4;
1701 break;
1703 case SEQ_ECHO:
1704 case SEQ_SYNCTIMER:
1705 case SEQ_PRIVATE:
1706 default:
1707 SEQ_DEBUG(2,
1708 kprintf("seq_convertold: event type %d not handled %d %d %d\n",
1709 event[0], event[1], event[2], event[3]));
1710 break;
1712 return used;
1716 * Writting to the sequencer buffer never blocks and drops
1717 * input which cannot be queued
1719 void
1720 seq_copytoinput(struct seq_softc *scp, u_char *event, int len)
1723 KKASSERT(lockowned(&scp->seq_lock));
1725 if (MIDIQ_AVAIL(scp->in_q) < len) {
1727 * ENOROOM? EINPUTDROPPED? ETOUGHLUCK?
1729 SEQ_DEBUG(2, kprintf("seq_copytoinput: queue full\n"));
1730 } else {
1731 MIDIQ_ENQ(scp->in_q, event, len);
1732 KNOTE(&scp->in_kq.ki_note, 0);
1733 cv_broadcast(&scp->in_cv);
1738 static int
1739 seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
1741 int ret, voice;
1742 u_char cmd, chn, note, parm;
1744 ret = 0;
1745 cmd = event[2];
1746 chn = event[3];
1747 note = event[4];
1748 parm = event[5];
1750 KKASSERT(lockowned(&scp->seq_lock));
1752 SEQ_DEBUG(5, kprintf("seq_chnvoice: unit %d, dev %d, cmd %s,"
1753 " chn %d, note %d, parm %d.\n", scp->unit, event[1],
1754 midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm));
1756 voice = SYNTH_ALLOC(md, chn, note);
1758 lockmgr(&scp->seq_lock, LK_RELEASE);
1760 switch (cmd) {
1761 case MIDI_NOTEON:
1762 if (note < 128 || note == 255) {
1763 #if 0
1764 if (scp->music && chn == 9) {
1766 * This channel is a percussion. The note
1767 * number is the patch number.
1770 lockmgr(&scp->seq_lock, LK_RELEASE);
1771 if (SYNTH_SETINSTR(md, voice, 128 + note)
1772 == EAGAIN) {
1773 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1774 return (QUEUEFULL);
1776 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1778 note = 60; /* Middle C. */
1780 #endif
1781 if (scp->music) {
1783 lockmgr(&scp->seq_lock, LK_RELEASE);
1784 if (SYNTH_SETUPVOICE(md, voice, chn)
1785 == EAGAIN) {
1786 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1787 return (QUEUEFULL);
1789 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1792 SYNTH_STARTNOTE(md, voice, note, parm);
1794 break;
1795 case MIDI_NOTEOFF:
1796 SYNTH_KILLNOTE(md, voice, note, parm);
1797 break;
1798 case MIDI_KEY_PRESSURE:
1799 SYNTH_AFTERTOUCH(md, voice, parm);
1800 break;
1801 default:
1802 ret = 1;
1803 SEQ_DEBUG(2, kprintf("seq_chnvoice event type %d not handled\n",
1804 event[1]));
1805 break;
1808 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1809 return ret;
1812 static int
1813 seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
1815 int ret;
1816 u_short w14;
1817 u_char cmd, chn, p1;
1819 ret = 0;
1820 cmd = event[2];
1821 chn = event[3];
1822 p1 = event[4];
1823 w14 = *(u_short *)&event[6];
1825 SEQ_DEBUG(5, kprintf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
1826 " p1 %d, w14 %d.\n", scp->unit, event[1],
1827 midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14));
1828 lockmgr(&scp->seq_lock, LK_RELEASE);
1829 switch (cmd) {
1830 case MIDI_PGM_CHANGE:
1831 SEQ_DEBUG(4, kprintf("seq_chncommon pgmchn chn %d pg %d\n",
1832 chn, p1));
1833 SYNTH_SETINSTR(md, chn, p1);
1834 break;
1835 case MIDI_CTL_CHANGE:
1836 SEQ_DEBUG(4, kprintf("seq_chncommon ctlch chn %d pg %d %d\n",
1837 chn, p1, w14));
1838 SYNTH_CONTROLLER(md, chn, p1, w14);
1839 break;
1840 case MIDI_PITCH_BEND:
1841 if (scp->music) {
1843 * TODO: MIDI_PITCH_BEND
1845 #if 0
1846 lockmgr(&md->synth.vc_lock, LK_EXCLUSIVE);
1847 md->synth.chn_info[chn].bender_value = w14;
1848 if (md->midiunit >= 0) {
1850 * Handle all of the notes playing on this
1851 * channel.
1853 key = ((int)chn << 8);
1854 for (i = 0; i < md->synth.alloc.max_voice; i++)
1855 if ((md->synth.alloc.map[i] & 0xff00) == key) {
1856 lockmgr(&md->synth.vc_lock, LK_RELEASE);
1857 lockmgr(&scp->seq_lock, LK_RELEASE);
1858 if (md->synth.bender(md, i, w14) == EAGAIN) {
1859 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1860 return (QUEUEFULL);
1862 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1864 } else {
1865 lockmgr(&md->synth.vc_lock, LK_RELEASE);
1866 lockmgr(&scp->seq_lock, LK_RELEASE);
1867 if (md->synth.bender(md, chn, w14) == EAGAIN) {
1868 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1869 return (QUEUEFULL);
1871 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1873 #endif
1874 } else
1875 SYNTH_BENDER(md, chn, w14);
1876 break;
1877 default:
1878 ret = 1;
1879 SEQ_DEBUG(2,
1880 kprintf("seq_chncommon event type %d not handled.\n",
1881 event[1]));
1882 break;
1885 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
1886 return ret;
1889 static int
1890 seq_timing(struct seq_softc *scp, u_char *event)
1892 int param;
1893 int ret;
1895 ret = 0;
1896 param = event[4] + (event[5] << 8) +
1897 (event[6] << 16) + (event[7] << 24);
1899 SEQ_DEBUG(5, kprintf("seq_timing: unit %d, cmd %d, param %d.\n",
1900 scp->unit, event[1], param));
1901 switch (event[1]) {
1902 case TMR_WAIT_REL:
1903 timer_wait(scp, param, 0);
1904 break;
1905 case TMR_WAIT_ABS:
1906 timer_wait(scp, param, 1);
1907 break;
1908 case TMR_START:
1909 timer_start(scp);
1910 cv_broadcast(&scp->reset_cv);
1911 break;
1912 case TMR_STOP:
1913 timer_stop(scp);
1915 * The following cv_broadcast isn't needed since we only
1916 * wait for 0->1 transitions. It probably won't hurt
1918 cv_broadcast(&scp->reset_cv);
1919 break;
1920 case TMR_CONTINUE:
1921 timer_continue(scp);
1922 cv_broadcast(&scp->reset_cv);
1923 break;
1924 case TMR_TEMPO:
1925 if (param < 8)
1926 param = 8;
1927 if (param > 360)
1928 param = 360;
1929 SEQ_DEBUG(4, kprintf("Timer set tempo %d\n", param));
1930 timer_setvals(scp, param, scp->timerbase);
1931 break;
1932 case TMR_TIMERBASE:
1933 if (param < 1)
1934 param = 1;
1935 if (param > 1000)
1936 param = 1000;
1937 SEQ_DEBUG(4, kprintf("Timer set timerbase %d\n", param));
1938 timer_setvals(scp, scp->tempo, param);
1939 break;
1940 case TMR_ECHO:
1942 * TODO: Consider making 4-byte events for /dev/sequencer
1943 * PRO: Maybe needed by legacy apps
1944 * CON: soundcard.h has been warning for a while many years
1945 * to expect 8 byte events.
1947 #if 0
1948 if (scp->music)
1949 seq_copytoinput(scp, event, 8);
1950 else {
1951 param = (param << 8 | SEQ_ECHO);
1952 seq_copytoinput(scp, (u_char *)&param, 4);
1954 #else
1955 seq_copytoinput(scp, event, 8);
1956 #endif
1957 break;
1958 default:
1959 SEQ_DEBUG(2, kprintf("seq_timing event type %d not handled.\n",
1960 event[1]));
1961 ret = 1;
1962 break;
1964 return ret;
1967 static int
1968 seq_local(struct seq_softc *scp, u_char *event)
1970 int ret;
1972 ret = 0;
1973 KKASSERT(lockowned(&scp->seq_lock));
1975 SEQ_DEBUG(5, kprintf("seq_local: unit %d, cmd %d\n", scp->unit,
1976 event[1]));
1977 switch (event[1]) {
1978 default:
1979 SEQ_DEBUG(1, kprintf("seq_local event type %d not handled\n",
1980 event[1]));
1981 ret = 1;
1982 break;
1984 return ret;
1987 static int
1988 seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event)
1990 int i, l;
1992 KKASSERT(lockowned(&scp->seq_lock));
1993 SEQ_DEBUG(5, kprintf("seq_sysex: unit %d device %d\n", scp->unit,
1994 event[1]));
1995 l = 0;
1996 for (i = 0; i < 6 && event[i + 2] != 0xff; i++)
1997 l = i + 1;
1998 if (l > 0) {
1999 lockmgr(&scp->seq_lock, LK_RELEASE);
2000 if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) {
2001 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
2002 return 1;
2004 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
2006 return 0;
2010 * Reset no longer closes the raw devices nor seq_sync's
2011 * Callers are IOCTL and seq_close
2013 static void
2014 seq_reset(struct seq_softc *scp)
2016 int chn, i;
2017 kobj_t m;
2019 KKASSERT(lockowned(&scp->seq_lock));
2021 SEQ_DEBUG(5, kprintf("seq_reset: unit %d.\n", scp->unit));
2024 * Stop reading and writing.
2027 /* scp->recording = 0; */
2028 scp->playing = 0;
2029 cv_broadcast(&scp->state_cv);
2030 cv_broadcast(&scp->out_cv);
2031 cv_broadcast(&scp->reset_cv);
2034 * For now, don't reset the timers.
2036 MIDIQ_CLEAR(scp->in_q);
2037 MIDIQ_CLEAR(scp->out_q);
2039 for (i = 0; i < scp->midi_number; i++) {
2040 m = scp->midis[i];
2041 lockmgr(&scp->seq_lock, LK_RELEASE);
2042 SYNTH_RESET(m);
2043 for (chn = 0; chn < 16; chn++) {
2044 SYNTH_CONTROLLER(m, chn, 123, 0);
2045 SYNTH_CONTROLLER(m, chn, 121, 0);
2046 SYNTH_BENDER(m, chn, 1 << 13);
2048 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
2053 * seq_sync
2054 * *really* flush the output queue
2055 * flush the event queue, then flush the synthsisers.
2056 * Callers are IOCTL and close
2059 #define SEQ_SYNC_TIMEOUT 8
2060 static int
2061 seq_sync(struct seq_softc *scp)
2063 int i, rl, sync[16], done;
2065 KKASSERT(lockowned(&scp->seq_lock));
2067 SEQ_DEBUG(4, kprintf("seq_sync: unit %d.\n", scp->unit));
2070 * Wait until output queue is empty. Check every so often to see if
2071 * the queue is moving along. If it isn't just abort.
2073 while (!MIDIQ_EMPTY(scp->out_q)) {
2075 if (!scp->playing) {
2076 scp->playing = 1;
2077 cv_broadcast(&scp->state_cv);
2078 cv_broadcast(&scp->out_cv);
2080 rl = MIDIQ_LEN(scp->out_q);
2082 i = cv_timedwait_sig(&scp->out_cv,
2083 &scp->seq_lock, SEQ_SYNC_TIMEOUT * hz);
2085 if (i == EINTR || i == ERESTART) {
2086 if (i == EINTR) {
2088 * XXX: I don't know why we stop playing
2090 scp->playing = 0;
2091 cv_broadcast(&scp->out_cv);
2093 return i;
2095 if (i == EWOULDBLOCK && rl == MIDIQ_LEN(scp->out_q) &&
2096 scp->waiting == 0) {
2098 * A queue seems to be stuck up. Give up and clear
2099 * queues.
2101 MIDIQ_CLEAR(scp->out_q);
2102 scp->playing = 0;
2103 cv_broadcast(&scp->state_cv);
2104 cv_broadcast(&scp->out_cv);
2105 cv_broadcast(&scp->reset_cv);
2108 * TODO: Consider if the raw devices need to be flushed
2111 SEQ_DEBUG(1, kprintf("seq_sync queue stuck, aborting\n"));
2113 return i;
2117 scp->playing = 0;
2119 * Since syncing a midi device might block, unlock scp->seq_lock.
2122 lockmgr(&scp->seq_lock, LK_RELEASE);
2123 for (i = 0; i < scp->midi_number; i++)
2124 sync[i] = 1;
2126 do {
2127 done = 1;
2128 for (i = 0; i < scp->midi_number; i++)
2129 if (sync[i]) {
2130 if (SYNTH_INSYNC(scp->midis[i]) == 0)
2131 sync[i] = 0;
2132 else
2133 done = 0;
2135 if (!done)
2136 DELAY(5000);
2138 } while (!done);
2140 lockmgr(&scp->seq_lock, LK_EXCLUSIVE);
2141 return 0;
2144 char *
2145 midi_cmdname(int cmd, midi_cmdtab *tab)
2147 while (tab->name != NULL) {
2148 if (cmd == tab->cmd)
2149 return (tab->name);
2150 tab++;
2153 return ("unknown");