xref: /freebsd/sys/dev/sound/midi/sequencer.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*
2  * The sequencer personality manager.
3  * (c) 2003 Mathew Kanner
4  * Copyright by Hannu Savolainen 1993
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer. 2.
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/ioccom.h>
33 
34 #include <sys/filio.h>
35 #include <sys/lock.h>
36 #include <sys/sockio.h>
37 #include <sys/fcntl.h>
38 #include <sys/tty.h>
39 #include <sys/proc.h>
40 #include <sys/sysctl.h>
41 
42 #include <sys/kernel.h> /* for DATA_SET */
43 
44 #include <sys/module.h>
45 #include <sys/conf.h>
46 #include <sys/file.h>
47 #include <sys/uio.h>
48 #include <sys/syslog.h>
49 #include <sys/errno.h>
50 #include <sys/malloc.h>
51 #include <sys/bus.h>
52 #include <machine/resource.h>
53 #include <machine/bus.h>
54 #include <machine/clock.h>      /* for DELAY */
55 #include <sys/soundcard.h>
56 #include <sys/rman.h>
57 #include <sys/mman.h>
58 #include <sys/poll.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/kthread.h>
62 #include <sys/unistd.h>
63 #include <sys/selinfo.h>
64 
65 
66 #include <dev/sound/midi/midi.h>
67 #include <dev/sound/midi/midiq.h>
68 #include "synth_if.h"
69 
70 #include <dev/sound/midi/sequencer.h>
71 
72 #define TMR_TIMERBASE 13
73 
74 #define SND_DEV_SEQ	1	/* Sequencer output /dev/sequencer (FM
75 				   synthesizer and MIDI output) */
76 #define SND_DEV_MUSIC	8	/* /dev/music, level 2 interface */
77 
78 /* Length of a sequencer event. */
79 #define EV_SZ 8
80 #define IEV_SZ 8
81 
82 /* Lookup modes */
83 #define LOOKUP_EXIST	(0)
84 #define LOOKUP_OPEN	(1)
85 #define LOOKUP_CLOSE	(2)
86 
87 #define PCMMKMINOR(u, d, c) \
88 	    ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
89 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c)
90 #define MIDIUNIT(y) ((minor(y) >> 4) & 0x0f)
91 #define MIDIDEV(y) (minor(y) & 0x0f)
92 
93 /* These are the entries to the sequencer driver. */
94 static d_open_t seq_open;
95 static d_close_t seq_close;
96 static d_ioctl_t seq_ioctl;
97 static d_read_t seq_read;
98 static d_write_t seq_write;
99 static d_poll_t seq_poll;
100 
101 static struct cdevsw seq_cdevsw = {
102 	.d_version = D_VERSION,
103 	.d_open =	seq_open,
104 	.d_close =	seq_close,
105 	.d_read =	seq_read,
106 	.d_write =	seq_write,
107 	.d_ioctl =	seq_ioctl,
108 	.d_poll =	seq_poll,
109 	.d_name =	"sequencer",
110 };
111 
112 struct seq_softc {
113 	KOBJ_FIELDS;
114 
115 	struct mtx seq_lock, q_lock;
116 	struct cv empty_cv, reset_cv, in_cv, out_cv, state_cv, th_cv;
117 
118 	MIDIQ_HEAD(, u_char) in_q, out_q;
119 
120 	u_long  flags;
121 	/* Flags (protected by flag_mtx of mididev_info) */
122 	int		fflags;		  /* Access mode */
123 	int		music;
124 
125 	int		out_water;	/* Sequence output threshould */
126 	snd_sync_parm	sync_parm;		/* AIOSYNC parameter set */
127 	struct thread	*sync_thread;		/* AIOSYNCing thread */
128 	struct selinfo in_sel, out_sel;
129 	int midi_number;
130 	struct cdev *seqdev, *musicdev;
131 	int unit;
132 	int maxunits;
133 	kobj_t *midis;
134 	int *midi_flags;
135 	kobj_t mapper;
136 	void *mapper_cookie;
137 	struct timeval timerstop, timersub;
138 	int timerbase, tempo;
139 	int timerrun;
140 	int done;
141 	int playing;
142 	int recording;
143 	int busy;
144 	int pre_event_timeout;
145 	int waiting;
146 };
147 
148 /*
149  * Module specific stuff, including how many sequecers
150  * we currently own.
151  */
152 
153 SYSCTL_NODE(_hw_midi, OID_AUTO, seq, CTLFLAG_RD, 0, "Midi sequencer");
154 
155 int					seq_debug;
156 /* XXX: should this be moved into debug.midi? */
157 SYSCTL_INT(_hw_midi_seq, OID_AUTO, debug, CTLFLAG_RW, &seq_debug, 0, "");
158 
159 midi_cmdtab	cmdtab_seqevent[] = {
160 	{SEQ_NOTEOFF,		"SEQ_NOTEOFF"},
161 	{SEQ_NOTEON,		"SEQ_NOTEON"},
162 	{SEQ_WAIT,		"SEQ_WAIT"},
163 	{SEQ_PGMCHANGE,		"SEQ_PGMCHANGE"},
164 	{SEQ_SYNCTIMER,		"SEQ_SYNCTIMER"},
165 	{SEQ_MIDIPUTC,		"SEQ_MIDIPUTC"},
166 	{SEQ_DRUMON,		"SEQ_DRUMON"},
167 	{SEQ_DRUMOFF,		"SEQ_DRUMOFF"},
168 	{SEQ_ECHO,		"SEQ_ECHO"},
169 	{SEQ_AFTERTOUCH,	"SEQ_AFTERTOUCH"},
170 	{SEQ_CONTROLLER,	"SEQ_CONTROLLER"},
171 	{SEQ_BALANCE,		"SEQ_BALANCE"},
172 	{SEQ_VOLMODE,		"SEQ_VOLMODE"},
173 	{SEQ_FULLSIZE,		"SEQ_FULLSIZE"},
174 	{SEQ_PRIVATE,		"SEQ_PRIVATE"},
175 	{SEQ_EXTENDED,		"SEQ_EXTENDED"},
176 	{EV_SEQ_LOCAL,		"EV_SEQ_LOCAL"},
177 	{EV_TIMING,		"EV_TIMING"},
178 	{EV_CHN_COMMON,		"EV_CHN_COMMON"},
179 	{EV_CHN_VOICE,		"EV_CHN_VOICE"},
180 	{EV_SYSEX,		"EV_SYSEX"},
181 	{-1,			NULL},
182 };
183 
184 midi_cmdtab	cmdtab_seqioctl[] = {
185 	{SNDCTL_SEQ_RESET,	"SNDCTL_SEQ_RESET"},
186 	{SNDCTL_SEQ_SYNC,	"SNDCTL_SEQ_SYNC"},
187 	{SNDCTL_SYNTH_INFO,	"SNDCTL_SYNTH_INFO"},
188 	{SNDCTL_SEQ_CTRLRATE,	"SNDCTL_SEQ_CTRLRATE"},
189 	{SNDCTL_SEQ_GETOUTCOUNT,	"SNDCTL_SEQ_GETOUTCOUNT"},
190 	{SNDCTL_SEQ_GETINCOUNT,	"SNDCTL_SEQ_GETINCOUNT"},
191 	{SNDCTL_SEQ_PERCMODE,	"SNDCTL_SEQ_PERCMODE"},
192 	{SNDCTL_FM_LOAD_INSTR,	"SNDCTL_FM_LOAD_INSTR"},
193 	{SNDCTL_SEQ_TESTMIDI,	"SNDCTL_SEQ_TESTMIDI"},
194 	{SNDCTL_SEQ_RESETSAMPLES,	"SNDCTL_SEQ_RESETSAMPLES"},
195 	{SNDCTL_SEQ_NRSYNTHS,	"SNDCTL_SEQ_NRSYNTHS"},
196 	{SNDCTL_SEQ_NRMIDIS,	"SNDCTL_SEQ_NRMIDIS"},
197 	{SNDCTL_SEQ_GETTIME,	"SNDCTL_SEQ_GETTIME"},
198 	{SNDCTL_MIDI_INFO,	"SNDCTL_MIDI_INFO"},
199 	{SNDCTL_SEQ_THRESHOLD,	"SNDCTL_SEQ_THRESHOLD"},
200 	{SNDCTL_SYNTH_MEMAVL,	"SNDCTL_SYNTH_MEMAVL"},
201 	{SNDCTL_FM_4OP_ENABLE,	"SNDCTL_FM_4OP_ENABLE"},
202 	{SNDCTL_PMGR_ACCESS,	"SNDCTL_PMGR_ACCESS"},
203 	{SNDCTL_SEQ_PANIC,	"SNDCTL_SEQ_PANIC"},
204 	{SNDCTL_SEQ_OUTOFBAND,	"SNDCTL_SEQ_OUTOFBAND"},
205 	{SNDCTL_TMR_TIMEBASE,	"SNDCTL_TMR_TIMEBASE"},
206 	{SNDCTL_TMR_START,	"SNDCTL_TMR_START"},
207 	{SNDCTL_TMR_STOP,	"SNDCTL_TMR_STOP"},
208 	{SNDCTL_TMR_CONTINUE,	"SNDCTL_TMR_CONTINUE"},
209 	{SNDCTL_TMR_TEMPO,	"SNDCTL_TMR_TEMPO"},
210 	{SNDCTL_TMR_SOURCE,	"SNDCTL_TMR_SOURCE"},
211 	{SNDCTL_TMR_METRONOME,	"SNDCTL_TMR_METRONOME"},
212 	{SNDCTL_TMR_SELECT,	"SNDCTL_TMR_SELECT"},
213 	{SNDCTL_MIDI_PRETIME,	"SNDCTL_MIDI_PRETIME"},
214 	{AIONWRITE,		"AIONWRITE"},
215 	{AIOGSIZE,		"AIOGSIZE"},
216 	{AIOSSIZE,		"AIOSSIZE"},
217 	{AIOGFMT,		"AIOGFMT"},
218 	{AIOSFMT,		"AIOSFMT"},
219 	{AIOGMIX,		"AIOGMIX"},
220 	{AIOSMIX,		"AIOSMIX"},
221 	{AIOSTOP,		"AIOSTOP"},
222 	{AIOSYNC,		"AIOSYNC"},
223 	{AIOGCAP,		"AIOGCAP"},
224 	{-1,			NULL},
225 };
226 
227 midi_cmdtab	cmdtab_timer[] = {
228 	{TMR_WAIT_REL,	"TMR_WAIT_REL"},
229 	{TMR_WAIT_ABS,	"TMR_WAIT_ABS"},
230 	{TMR_STOP,	"TMR_STOP"},
231 	{TMR_START,	"TMR_START"},
232 	{TMR_CONTINUE,	"TMR_CONTINUE"},
233 	{TMR_TEMPO,	"TMR_TEMPO"},
234 	{TMR_ECHO,	"TMR_ECHO"},
235 	{TMR_CLOCK,	"TMR_CLOCK"},
236 	{TMR_SPP,	"TMR_SPP"},
237 	{TMR_TIMESIG,	"TMR_TIMESIG"},
238 	{-1,		NULL},
239 };
240 
241 midi_cmdtab	cmdtab_seqcv[] = {
242 	{MIDI_NOTEOFF,		"MIDI_NOTEOFF"},
243 	{MIDI_NOTEON,		"MIDI_NOTEON"},
244 	{MIDI_KEY_PRESSURE,	"MIDI_KEY_PRESSURE"},
245 	{-1,			NULL},
246 };
247 
248 midi_cmdtab	cmdtab_seqccmn[] = {
249 	{MIDI_CTL_CHANGE,	"MIDI_CTL_CHANGE"},
250 	{MIDI_PGM_CHANGE,	"MIDI_PGM_CHANGE"},
251 	{MIDI_CHN_PRESSURE,	"MIDI_CHN_PRESSURE"},
252 	{MIDI_PITCH_BEND,	"MIDI_PITCH_BEND"},
253 	{MIDI_SYSTEM_PREFIX,	"MIDI_SYSTEM_PREFIX"},
254 	{-1,			NULL},
255 };
256 
257 /*
258  * static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m);
259  */
260 
261 static kobj_method_t seq_methods[] = {
262 	/* KOBJMETHOD(mpu_provider,mpu401_mprovider), */
263         { 0, 0 }
264 };
265 DEFINE_CLASS(sequencer, seq_methods, 0);
266 
267 /* The followings are the local function. */
268 static int seq_convertold(u_char *event, u_char *out);
269 /*
270  * static void seq_midiinput(struct seq_softc * scp, void *md);
271  */
272 static void seq_reset(struct seq_softc * scp);
273 static int seq_sync(struct seq_softc * scp);
274 
275 static int seq_processevent(struct seq_softc * scp, u_char *event);
276 
277 static int seq_timing(struct seq_softc * scp, u_char *event);
278 static int seq_local(struct seq_softc * scp, u_char *event);
279 
280 static int seq_chnvoice(struct seq_softc * scp, kobj_t md, u_char *event);
281 static int seq_chncommon(struct seq_softc * scp, kobj_t md, u_char *event);
282 static int seq_sysex(struct seq_softc * scp, kobj_t md, u_char *event);
283 
284 static int seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md);
285 void seq_copytoinput(struct seq_softc *scp, u_char *event, int len);
286 int seq_modevent(module_t mod, int type, void *data);
287 struct seq_softc *seqs[10];
288 static struct mtx			seqinfo_mtx;
289 static u_long				nseq = 0;
290 
291 static void timer_start(struct seq_softc *t);
292 static void timer_stop(struct seq_softc *t);
293 static void timer_setvals(struct seq_softc *t, int tempo, int timerbase);
294 static void timer_wait(struct seq_softc *t, int ticks, int wait_abs);
295 static int timer_now(struct seq_softc *t);
296 
297 
298 static void
299 timer_start(struct seq_softc *t)
300 {
301 	t->timerrun = 1;
302 	getmicrotime(&t->timersub);
303 }
304 
305 static void
306 timer_continue(struct seq_softc *t)
307 {
308 	struct timeval now;
309 
310 	if (t->timerrun == 1)
311 	    return;
312 	t->timerrun = 1;
313 	getmicrotime(&now);
314 	timevalsub(&now, &t->timerstop);
315 	timevaladd(&t->timersub, &now);
316 }
317 
318 static void
319 timer_stop(struct seq_softc *t)
320 {
321 	t->timerrun = 0;
322 	getmicrotime(&t->timerstop);
323 }
324 
325 static void
326 timer_setvals(struct seq_softc *t, int tempo, int timerbase)
327 {
328 	t->tempo = tempo;
329 	t->timerbase = timerbase;
330 }
331 
332 static void
333 timer_wait(struct seq_softc *t, int ticks, int wait_abs)
334 {
335 	struct timeval now, when;
336 	int ret;
337 	unsigned long long i;
338 
339 	while (t->timerrun == 0) {
340 	    SEQ_DEBUG(2,printf("Timer wait when timer isn't running\n"));
341 	    /*
342 	     * The old sequencer used timeouts that only increased
343 	     * the timer when the timer was running.
344 	     * Hence the sequencer would stick (?) if the
345 	     * timer was disabled.
346 	     */
347 	    cv_wait(&t->reset_cv, &t->seq_lock);
348 	    if (t->playing == 0)
349 		return;
350 	}
351 
352 	i = ticks * 60ull * 1000000ull / (t->tempo * t->timerbase);
353 
354 	when.tv_sec = i / 1000000;
355 	when.tv_usec = i % 1000000;
356 
357 #if 0
358 	printf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n", t->tempo, t->timerbase, ticks, wait_abs, i);
359 #endif
360 
361 	if (wait_abs != 0) {
362 	    getmicrotime(&now);
363 	    timevalsub(&now, &t->timersub);
364 	    timevalsub(&when, &now);
365 	}
366 
367 	if (when.tv_sec < 0 || when.tv_usec < 0) {
368 	    SEQ_DEBUG(3,printf("seq_timer error negative time %lds.%06lds\n",
369 		    (long)when.tv_sec,(long)when.tv_usec));
370 	    return;
371 	}
372 
373 	i = when.tv_sec * 1000000ull;
374 	i += when.tv_usec;
375 	i *= hz;
376 	i /= 1000000ull;
377 #if 0
378 	printf("seq_timer usec %llu ticks %llu\n", when.tv_sec * 1000000ull + when.tv_usec, i);
379 #endif
380 	t->waiting = 1;
381 	ret = cv_timedwait(&t->reset_cv, &t->seq_lock, i + 1);
382 	t->waiting = 0;
383 
384 	if (ret != EWOULDBLOCK)
385 	    SEQ_DEBUG(3,printf("seq_timer didn't timeout\n"));
386 
387 }
388 
389 static int
390 timer_now(struct seq_softc *t)
391 {
392 	struct timeval now;
393 	unsigned long long i;
394 	int ret;
395 
396 	if (t->timerrun == 0)
397 	    now = t->timerstop;
398 	else
399 	    getmicrotime(&now);
400 
401 	timevalsub(&now, &t->timersub);
402 
403 	i = now.tv_sec * 1000000ull;
404 	i += now.tv_usec;
405 	i *= t->timerbase;
406 /*	i /= t->tempo; */
407 	i /= 1000000ull;
408 
409 	ret = i;
410 	/*
411 	 * printf("timer_now: %llu %d\n", i, ret);
412 	 */
413 
414 	return ret;
415 }
416 
417 static void
418 seq_eventthread(void *arg)
419 {
420 	struct seq_softc *scp = arg;
421 	char event[EV_SZ];
422 
423 	mtx_lock(&scp->seq_lock);
424 	SEQ_DEBUG(2,printf("seq_eventthread started\n"));
425 	while(scp->done == 0) {
426 restart:
427 	    while (scp->playing == 0) {
428 		cv_wait(&scp->state_cv, &scp->seq_lock);
429 		    if (scp->done)
430 			goto done;
431 	    }
432 
433 	    while (MIDIQ_EMPTY(scp->out_q)) {
434 		cv_broadcast(&scp->empty_cv);
435 		cv_wait(&scp->out_cv, &scp->seq_lock);
436 		if (scp->playing == 0)
437 		    goto restart;
438 		if (scp->done)
439 		    goto done;
440 	    }
441 
442 	    MIDIQ_DEQ(scp->out_q, event, EV_SZ);
443 
444 	    if (MIDIQ_AVAIL(scp->out_q) < scp->out_water) {
445 		cv_broadcast(&scp->out_cv);
446 		selwakeup(&scp->out_sel);
447 	    }
448 
449 	    seq_processevent(scp, event);
450 	}
451 
452 done:
453 	cv_broadcast(&scp->th_cv);
454 	mtx_unlock(&scp->seq_lock);
455 	mtx_lock(&Giant);
456 	SEQ_DEBUG(2,printf("seq_eventthread finished\n"));
457 	kthread_exit(0);
458 }
459 
460 /*
461  * seq_processevent:  This maybe called by the event thread or the IOCTL
462  * handler for queued and out of band events respectively.
463  */
464 static int
465 seq_processevent(struct seq_softc *scp, u_char *event)
466 {
467 	int ret;
468 	kobj_t m;
469 
470 	ret = 0;
471 
472 	if (event[0] == EV_SEQ_LOCAL)
473 		ret = seq_local(scp, event);
474 	else if (event[0] == EV_TIMING)
475 		ret = seq_timing(scp, event);
476 	else if (event[0] != EV_CHN_VOICE &&
477 			event[0] != EV_CHN_COMMON &&
478 			event[0] != EV_SYSEX &&
479 			event[0] != SEQ_MIDIPUTC) {
480 		ret = 1;
481 		SEQ_DEBUG(2,printf("seq_processevent not known %d\n", event[0]));
482 	} else if (seq_fetch_mid(scp, event[1], &m) != 0) {
483 		ret = 1;
484 		SEQ_DEBUG(2,printf("seq_processevent midi unit not found %d\n", event[1]));
485 	} else switch(event[0]) {
486 		case EV_CHN_VOICE:
487 			ret = seq_chnvoice(scp, m, event);
488 			break;
489 		case EV_CHN_COMMON:
490 			ret = seq_chncommon(scp, m, event);
491 			break;
492 		case EV_SYSEX:
493 			ret = seq_sysex(scp, m, event);
494 			break;
495 		case SEQ_MIDIPUTC:
496 			mtx_unlock(&scp->seq_lock);
497 			ret = SYNTH_WRITERAW(m, &event[2], 1);
498 			mtx_lock(&scp->seq_lock);
499 			break;
500 	}
501 	return ret;
502 }
503 
504 static int
505 seq_addunit(void)
506 {
507 	struct seq_softc *scp;
508 	int ret;
509 	u_char *buf;
510 
511 	/* Allocate the softc. */
512 	ret = ENOMEM;
513 	scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO);
514 	if (scp == NULL) {
515 		SEQ_DEBUG(1,printf("seq_addunit: softc allocation failed.\n"));
516 		goto err;
517 	}
518 	kobj_init((kobj_t) scp, &sequencer_class);
519 
520 	buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
521 	if (buf == NULL)
522 	    goto err;
523 	MIDIQ_INIT(scp->in_q, buf, EV_SZ * 1024);
524 	buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO);
525 	if (buf == NULL)
526 	    goto err;
527 	MIDIQ_INIT(scp->out_q, buf, EV_SZ * 1024);
528 	ret = EINVAL;
529 
530 	scp->midis = malloc(sizeof(kobj_t) * 32, M_TEMP, M_NOWAIT | M_ZERO);
531 	scp->midi_flags = malloc(sizeof(*scp->midi_flags) * 32, M_TEMP,
532 		M_NOWAIT | M_ZERO);
533 
534 	if ( scp->midis == NULL || scp->midi_flags == NULL)
535 	    goto err;
536 
537 	scp->flags = 0;
538 
539 	mtx_init(&scp->seq_lock, "seqflq", 0, 0);
540 	cv_init(&scp->state_cv, "seqstate");
541 	cv_init(&scp->empty_cv, "seqempty");
542 	cv_init(&scp->reset_cv, "seqtimer");
543 	cv_init(&scp->out_cv, "seqqout");
544 	cv_init(&scp->in_cv, "seqqin");
545 	cv_init(&scp->th_cv, "seqstart");
546 
547 	/*
548 	 * Init the damn timer
549 	 */
550 
551 	scp->mapper = midimapper_addseq(scp, &scp->unit, &scp->mapper_cookie);
552 	if (scp->mapper == NULL)
553 	    goto err;
554 
555 	scp->seqdev = make_dev(&seq_cdevsw,
556 		MIDIMKMINOR(scp->unit, SND_DEV_SEQ,0), UID_ROOT,
557 		GID_WHEEL, 0666, "sequencer%d", scp->unit);
558 
559 	scp->musicdev = make_dev(&seq_cdevsw,
560 		MIDIMKMINOR(scp->unit, SND_DEV_MUSIC,0), UID_ROOT,
561 		GID_WHEEL, 0666, "music%d", scp->unit);
562 
563 	if (scp->seqdev == NULL || scp->musicdev == NULL)
564 	    goto err;
565 	/*
566 	 * TODO: Add to list of sequencers this module provides
567 	 */
568 
569 	ret = kthread_create(seq_eventthread, scp, NULL, RFHIGHPID, 0, "sequencer %02d", scp->unit);
570 
571 	if (ret)
572 	    goto err;
573 
574 	scp->seqdev->si_drv1 = scp->musicdev->si_drv1 = scp;
575 
576 	SEQ_DEBUG(2,printf("sequencer %d created scp %p\n", scp->unit, scp));
577 
578 	ret = 0;
579 
580 	mtx_lock(&seqinfo_mtx);
581 	seqs[nseq++]=scp;
582 	mtx_unlock(&seqinfo_mtx);
583 
584 	goto ok;
585 
586 err:
587 	if (scp != NULL) {
588 	    if (scp->seqdev != NULL)
589 		destroy_dev(scp->seqdev);
590 	    if (scp->musicdev != NULL)
591 		destroy_dev(scp->musicdev);
592 	    /*
593 	     * TODO: Destroy mutex and cv
594 	     */
595 	    if (scp->midis != NULL)
596 		free(scp->midis, M_TEMP);
597 	    if (scp->midi_flags != NULL)
598 		free(scp->midi_flags, M_TEMP);
599 	    if (scp->out_q.b)
600 		free(scp->out_q.b, M_TEMP);
601 	    if (scp->in_q.b)
602 		free(scp->in_q.b, M_TEMP);
603 	    free(scp, M_DEVBUF);
604 	}
605 ok:
606 	return ret;
607 }
608 
609 static int
610 seq_delunit(int unit)
611 {
612 	struct seq_softc *scp = seqs[unit];
613 	int i;
614 
615 	//SEQ_DEBUG(4,printf("seq_delunit: %d\n", unit));
616 	printf("seq_delunit: 1 \n");
617 	mtx_lock(&scp->seq_lock);
618 
619 	scp->playing = 0;
620 	scp->done = 1;
621 	cv_broadcast(&scp->out_cv);
622 	cv_broadcast(&scp->state_cv);
623 	cv_broadcast(&scp->reset_cv);
624 	printf("seq_delunit: 2 \n");
625 	cv_wait(&scp->th_cv, &scp->seq_lock);
626 	printf("seq_delunit: 3.0 \n");
627 	mtx_unlock(&scp->seq_lock);
628 	printf("seq_delunit: 3.1 \n");
629 
630 	cv_destroy(&scp->state_cv);
631 	printf("seq_delunit: 4 \n");
632 	cv_destroy(&scp->empty_cv);
633 	printf("seq_delunit: 5 \n");
634 	cv_destroy(&scp->reset_cv);
635 	printf("seq_delunit: 6 \n");
636 	cv_destroy(&scp->out_cv);
637 	printf("seq_delunit: 7 \n");
638 	cv_destroy(&scp->in_cv);
639 	printf("seq_delunit: 8 \n");
640 	cv_destroy(&scp->th_cv);
641 
642 	printf("seq_delunit: 10 \n");
643 	if (scp->seqdev)
644 	    destroy_dev(scp->seqdev);
645 	printf("seq_delunit: 11 \n");
646 	if (scp->musicdev)
647 	    destroy_dev(scp->musicdev);
648 	printf("seq_delunit: 12 \n");
649 	scp->seqdev = scp->musicdev = NULL;
650 	if (scp->midis != NULL)
651 	    free(scp->midis, M_TEMP);
652 	printf("seq_delunit: 13 \n");
653 	if (scp->midi_flags != NULL)
654 	    free(scp->midi_flags, M_TEMP);
655 	printf("seq_delunit: 14 \n");
656 	free(scp->out_q.b, M_TEMP);
657 	printf("seq_delunit: 15 \n");
658 	free(scp->in_q.b, M_TEMP);
659 
660 	printf("seq_delunit: 16 \n");
661 
662 	mtx_destroy(&scp->seq_lock);
663 	printf("seq_delunit: 17 \n");
664 	free(scp, M_DEVBUF);
665 
666 	mtx_lock(&seqinfo_mtx);
667 	for ( i = unit ; i < (nseq - 1) ; i++ )
668 	    seqs[i] = seqs[i+1];
669 	nseq--;
670 	mtx_unlock(&seqinfo_mtx);
671 
672 	return 0;
673 }
674 
675 int
676 seq_modevent(module_t mod, int type, void *data)
677 {
678 	int retval, r;
679 
680 	retval = 0;
681 
682 	switch (type) {
683 	    case MOD_LOAD:
684 		mtx_init(&seqinfo_mtx, "seqmod", 0, 0);
685 		retval = seq_addunit();
686 		break;
687 
688 	    case MOD_UNLOAD:
689 		while (nseq) {
690 			r=seq_delunit(nseq-1);
691 			if (r) {
692 				retval = r;
693 				break;
694 			}
695 		}
696 		if(nseq == 0) {
697 			retval = 0;
698 			mtx_destroy(&seqinfo_mtx);
699 		}
700 		break;
701 
702 	    default:
703 		break;
704 	}
705 
706 	return retval;
707 }
708 
709 static int
710 seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md)
711 {
712 
713 	if (unit > scp->midi_number || unit < 0)
714 	    return EINVAL;
715 
716 	*md = scp->midis[unit];
717 
718 	return 0;
719 }
720 
721 int
722 seq_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
723 {
724 	struct seq_softc *scp = i_dev->si_drv1;
725 	int i;
726 
727 	if (scp == NULL)
728 	    return ENXIO;
729 
730 	SEQ_DEBUG(3,printf("seq_open: scp %p unit %d, flags 0x%x.\n",
731 		    scp, scp->unit, flags));
732 
733 	/*
734 	 * Mark this device busy.
735 	 */
736 
737 	mtx_lock(&scp->seq_lock);
738 	if (scp->busy) {
739 		mtx_unlock(&scp->seq_lock);
740 		SEQ_DEBUG(2,printf("seq_open: unit %d is busy.\n", scp->unit));
741 		return EBUSY;
742 	}
743 	scp->fflags = flags;
744 	/*
745 	if ((scp->fflags & O_NONBLOCK) != 0)
746 		scp->flags |= SEQ_F_NBIO;
747 		*/
748 	scp->music = MIDIDEV(i_dev) == SND_DEV_MUSIC;
749 
750 	/*
751 	 * Enumerate the available midi devices
752 	 */
753 	scp->midi_number = 0;
754 	scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie);
755 
756 	if (scp->maxunits == 0)
757 	    SEQ_DEBUG(2,printf("seq_open: no midi devices\n"));
758 
759 	for (i = 0 ; i < scp->maxunits; i++) {
760 	    scp->midis[scp->midi_number] =
761 		midimapper_fetch_synth(scp->mapper, scp->mapper_cookie, i);
762 	    if (scp->midis[scp->midi_number]) {
763 		if ( SYNTH_OPEN(scp->midis[scp->midi_number], scp, scp->fflags)
764 			!= 0 )
765 		    scp->midis[scp->midi_number] = NULL;
766 		else {
767 		    scp->midi_flags[scp->midi_number] =
768 			SYNTH_QUERY(scp->midis[scp->midi_number]);
769 		    scp->midi_number++;
770 		}
771 	    }
772 	}
773 
774 	timer_setvals(scp, 60, 100);
775 
776 	timer_start(scp);
777 	timer_stop(scp);
778 	/*
779 	 * actually, if we're in rdonly mode, we should start the timer
780 	 */
781 	/*
782 	 * TODO: Handle recording now
783 	 */
784 
785 	scp->out_water = MIDIQ_SIZE(scp->out_q) / 2;
786 
787 	scp->busy = 1;
788 	mtx_unlock(&scp->seq_lock);
789 
790 	SEQ_DEBUG(2,printf("seq_open: opened, mode %s.\n",
791 		    scp->music ? "music" : "sequencer"));
792 	SEQ_DEBUG(2,printf("Sequencer %d %p opened maxunits %d midi_number %d:\n", scp->unit, scp, scp->maxunits, scp->midi_number));
793 	for (i=0;i<scp->midi_number;i++)
794 		SEQ_DEBUG(3,printf("  midi %d %p\n", i, scp->midis[i]));
795 
796 	return 0;
797 }
798 
799 /*
800  * seq_close
801  */
802 int
803 seq_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
804 {
805 	int i;
806 	struct seq_softc *scp = i_dev->si_drv1;
807 	int ret;
808 
809 	if (scp == NULL)
810 	    return ENXIO;
811 
812 	SEQ_DEBUG(2,printf("seq_close: unit %d.\n", scp->unit));
813 
814 	mtx_lock(&scp->seq_lock);
815 
816 	ret = ENXIO;
817 	if (scp->busy == 0)
818 	    goto err;
819 
820 	seq_reset(scp);
821 	seq_sync(scp);
822 
823 	for (i = 0 ; i < scp->midi_number; i++)
824 	    if (scp->midis[i])
825 		SYNTH_CLOSE(scp->midis[i]);
826 
827 	midimapper_close(scp->mapper, scp->mapper_cookie);
828 
829 	timer_stop(scp);
830 
831 	scp->busy = 0;
832 	ret = 0;
833 
834 err:
835 	SEQ_DEBUG(3,printf("seq_close: closed ret = %d.\n", ret));
836 	mtx_unlock(&scp->seq_lock);
837 	return ret;
838 }
839 
840 int
841 seq_read(struct cdev *i_dev, struct uio *uio, int ioflag)
842 {
843 	int retval, used;
844 	struct seq_softc *scp = i_dev->si_drv1;
845 #define SEQ_RSIZE 32
846 	u_char buf[SEQ_RSIZE];
847 
848 	if (scp == NULL)
849 	    return ENXIO;
850 
851 	SEQ_DEBUG(7,printf("seq_read: unit %d, resid %d.\n",
852 		    scp->unit, uio->uio_resid));
853 
854 	mtx_lock(&scp->seq_lock);
855 	if ((scp->fflags & FREAD) == 0) {
856 	    SEQ_DEBUG(2,printf("seq_read: unit %d is not for reading.\n",
857 			scp->unit));
858 	    retval = EIO;
859 	    goto err1;
860 	}
861 
862 	/*
863 	 * Begin recording.
864 	 */
865 	/*
866 	 * if ((scp->flags & SEQ_F_READING) == 0)
867 	 */
868 	/*
869 	 * TODO, start recording if not alread
870 	 */
871 
872 	/*
873 	 * I think the semantics are to return as soon
874 	 * as possible.
875 	 * Second thought, it doens't seem like midimoutain
876 	 * expects that at all.
877 	 * TODO: Look up in some sort of spec
878 	 */
879 
880 	while (uio->uio_resid > 0) {
881 	    while (MIDIQ_EMPTY(scp->in_q)) {
882 		retval = EWOULDBLOCK;
883 		/*
884 		 * I wish I knew which one to care about
885 		 */
886 
887 		if (scp->fflags & O_NONBLOCK)
888 		    goto err1;
889 		if (ioflag & O_NONBLOCK)
890 		    goto err1;
891 
892 		retval = cv_wait_sig(&scp->in_cv, &scp->seq_lock);
893 		if (retval == EINTR)
894 		    goto err1;
895 	    }
896 
897 	    used = MIN(MIDIQ_LEN(scp->in_q), uio->uio_resid);
898 	    used = MIN(used, SEQ_RSIZE);
899 
900 	    SEQ_DEBUG(8,printf("midiread: uiomove cc=%d\n", used));
901 	    MIDIQ_DEQ(scp->in_q, buf, used);
902 	    retval = uiomove(buf, used, uio);
903 	    if (retval)
904 		goto err1;
905 	}
906 
907 	retval = 0;
908 err1:
909 	mtx_unlock(&scp->seq_lock);
910 	SEQ_DEBUG(6,printf("seq_read: ret %d, resid %d.\n",
911 		    retval, uio->uio_resid));
912 
913 	return retval;
914 }
915 
916 int
917 seq_write(struct cdev *i_dev, struct uio *uio, int ioflag)
918 {
919 	u_char event[EV_SZ], newevent[EV_SZ], ev_code;
920 	struct seq_softc *scp = i_dev->si_drv1;
921 	int retval;
922 	int used;
923 
924 	SEQ_DEBUG(7,printf("seq_write: unit %d, resid %d.\n",
925 		    scp->unit, uio->uio_resid));
926 
927 	if (scp == NULL)
928 	    return ENXIO;
929 
930 	mtx_lock(&scp->seq_lock);
931 
932 	if ((scp->fflags & FWRITE) == 0) {
933 	    SEQ_DEBUG(2,printf("seq_write: unit %d is not for writing.\n",
934 			scp->unit));
935 	    retval = EIO;
936 	    goto err0;
937 	}
938 
939 	while (uio->uio_resid > 0) {
940 	    while (MIDIQ_AVAIL(scp->out_q) == 0) {
941 		retval = EWOULDBLOCK;
942 		if (scp->fflags & O_NONBLOCK)
943 		    goto err0;
944 		if (ioflag & O_NONBLOCK)
945 		    goto err0;
946 		SEQ_DEBUG(8,printf("seq_write cvwait\n"));
947 
948 		scp->playing = 1;
949 		cv_broadcast(&scp->out_cv);
950 		cv_broadcast(&scp->state_cv);
951 
952 		retval = cv_wait_sig(&scp->out_cv, &scp->seq_lock);
953 		    /*
954 		     * We slept, maybe things have changed since last
955 		     * dying check
956 		     */
957 		if (retval == EINTR)
958 		    goto err0;
959 #if 0
960 		    /*
961 		     * Useless test
962 		     */
963 		if (scp != i_dev->si_drv1)
964 		    retval = ENXIO;
965 #endif
966 	    }
967 
968 	    used = MIN(uio->uio_resid, 4);
969 
970 	    SEQ_DEBUG(8,printf("seqout: resid %d len %jd avail %jd\n",
971 			uio->uio_resid, (intmax_t)MIDIQ_LEN(scp->out_q),
972 			(intmax_t)MIDIQ_AVAIL(scp->out_q)));
973 
974 	    if (used != 4) {
975 		retval = ENXIO;
976 		goto err0;
977 	    }
978 
979 	    retval = uiomove(event, used, uio);
980 	    if (retval)
981 		goto err0;
982 
983 	    ev_code = event[0];
984 	    SEQ_DEBUG(8,printf("seq_write: unit %d, event %s.\n",
985 			scp->unit, midi_cmdname(ev_code, cmdtab_seqevent)));
986 
987 	    /* Have a look at the event code. */
988 	    if (ev_code == SEQ_FULLSIZE) {
989 
990 		/*
991 		 * TODO: restore code for SEQ_FULLSIZE
992 		 */
993 #if 0
994 		/* A long event, these are the patches/samples for a synthesizer. */
995 		midiunit = *(u_short *)&event[2];
996 		mtx_lock(&sd->seq_lock);
997 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
998 		mtx_unlock(&sd->seq_lock);
999 		if (ret != 0)
1000 		    return (ret);
1001 
1002 		SEQ_DEBUG(printf("seq_write: loading a patch to the unit %d.\n", midiunit));
1003 
1004 		ret = md->synth.loadpatch(md, *(short *)&event[0], buf, p + 4, count, 0);
1005 		return (ret);
1006 #else
1007 		/*
1008 		 * For now, just flush the darn buffer
1009 		 */
1010 		SEQ_DEBUG(2,printf("seq_write: SEQ_FULLSIZE flusing buffer.\n"));
1011 		while (uio->uio_resid > 0) {
1012 		    retval = uiomove(event, EV_SZ, uio);
1013 		    if (retval)
1014 			goto err0;
1015 
1016 		}
1017 		retval = 0;
1018 		goto err0;
1019 #endif
1020 	    }
1021 
1022 	    retval = EINVAL;
1023 	    if (ev_code >= 128) {
1024 
1025 		/*
1026 		 * Some sort of an extended event. The size is eight bytes.
1027 		 * scoop extra info.
1028 		 */
1029 		if (scp->music && ev_code == SEQ_EXTENDED) {
1030 		    SEQ_DEBUG(2,printf("seq_write: invalid level two event %x.\n", ev_code));
1031 		    goto err0;
1032 		}
1033 		if (uiomove((caddr_t)&event[4], 4, uio)) {
1034 		    SEQ_DEBUG(2,printf("seq_write: user memory mangled?\n"));
1035 		    goto err0;
1036 		}
1037 	    } else {
1038 		/*
1039 		 * Size four event.
1040 		 */
1041 		if (scp->music) {
1042 		    SEQ_DEBUG(2,printf("seq_write: four byte event in music mode.\n"));
1043 		    goto err0;
1044 		}
1045 	    }
1046 	    if (ev_code == SEQ_MIDIPUTC) {
1047 		/*
1048 		 * TODO: event[2] is unit number to receive char.  Range check
1049 		 * it
1050 		 */
1051 	    }
1052 
1053 	    if (scp->music) {
1054 #ifdef not_ever_ever
1055 		if (event[0] == EV_TIMING &&
1056 			(event[1] == TMR_START || event[1] == TMR_STOP) ) {
1057 		    /*
1058 		     * For now, try to make midimoutain work by
1059 		     * forcing these events to be processed immediatly
1060 		     */
1061 		    seq_processevent(scp, event);
1062 		}
1063 		else
1064 		    MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1065 #else
1066 		MIDIQ_ENQ(scp->out_q, event, EV_SZ);
1067 #endif
1068 	    } else {
1069 		if (seq_convertold(event, newevent) > 0)
1070 		    MIDIQ_ENQ(scp->out_q, newevent, EV_SZ);
1071 #if 0
1072 		else
1073 		    goto err0;
1074 #endif
1075 	    }
1076 
1077 	}
1078 
1079 	scp->playing = 1;
1080 	cv_broadcast(&scp->state_cv);
1081 	cv_broadcast(&scp->out_cv);
1082 
1083 	retval = 0;
1084 
1085 err0:
1086 	SEQ_DEBUG(6,printf("seq_write done: leftover buffer length %d retval %d\n",
1087 		    uio->uio_resid, retval));
1088 	mtx_unlock(&scp->seq_lock);
1089 	return retval;
1090 }
1091 
1092 int
1093 seq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
1094 {
1095 	int midiunit, ret, tmp;
1096 	struct seq_softc *scp = i_dev->si_drv1;
1097 	struct synth_info *synthinfo;
1098 	struct midi_info *midiinfo;
1099 	u_char event[EV_SZ];
1100 	u_char newevent[EV_SZ];
1101 
1102 	kobj_t md;
1103 	/*
1104 	 * struct snd_size *sndsize;
1105 	 */
1106 
1107 	if (scp == NULL)
1108 	    return ENXIO;
1109 
1110 	SEQ_DEBUG(6,printf("seq_ioctl: unit %d, cmd %s.\n",
1111 		    scp->unit, midi_cmdname(cmd, cmdtab_seqioctl)));
1112 
1113 	ret = 0;
1114 
1115 	switch (cmd) {
1116 	    case SNDCTL_SEQ_GETTIME:
1117 		/*
1118 		 * ioctl needed by libtse
1119 		 */
1120 		mtx_lock(&scp->seq_lock);
1121 		*(int *)arg = timer_now(scp);
1122 		mtx_unlock(&scp->seq_lock);
1123 		SEQ_DEBUG(6,printf("seq_ioctl: gettime %d.\n", *(int *)arg));
1124 		ret = 0;
1125 		break;
1126 	    case SNDCTL_TMR_METRONOME:
1127 		/* fallthrough */
1128 	    case SNDCTL_TMR_SOURCE:
1129 		/*
1130 		 * Not implemented
1131 		 */
1132 		ret = 0;
1133 		break;
1134 	    case SNDCTL_TMR_TEMPO:
1135 		event[1] = TMR_TEMPO;
1136 		goto timerevent;
1137 	    case SNDCTL_TMR_TIMEBASE:
1138 		event[1] = TMR_TIMERBASE;
1139 		goto timerevent;
1140 	    case SNDCTL_TMR_START:
1141 		event[1] = TMR_START;
1142 		goto timerevent;
1143 	    case SNDCTL_TMR_STOP:
1144 		event[1] = TMR_STOP;
1145 		goto timerevent;
1146 	    case SNDCTL_TMR_CONTINUE:
1147 		event[1] = TMR_CONTINUE;
1148 	    timerevent:
1149 		event[0] = EV_TIMING;
1150 		event[4] = *(int *)arg & 0xFF;
1151 		event[5] = (*(int *)arg >> 8) & 0xFF;
1152 		event[6] = (*(int *)arg >> 16) & 0xFF;
1153 		event[7] = (*(int *)arg >> 24) & 0xFF;
1154 		mtx_lock(&scp->seq_lock);
1155 		if (!scp->music) {
1156 		    ret = EINVAL;
1157 		    mtx_unlock(&scp->seq_lock);
1158 		    break;
1159 		}
1160 		seq_processevent(scp, event);
1161 		mtx_unlock(&scp->seq_lock);
1162 		break;
1163 	    case SNDCTL_TMR_SELECT:
1164 		SEQ_DEBUG(2,printf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
1165 		ret = EINVAL;
1166 		break;
1167 	    case SNDCTL_SEQ_SYNC:
1168 		if (mode == O_RDONLY) {
1169 		    ret = 0;
1170 		    break;
1171 		}
1172 		mtx_lock(&scp->seq_lock);
1173 		ret = seq_sync(scp);
1174 		mtx_unlock(&scp->seq_lock);
1175 		break;
1176 	    case SNDCTL_SEQ_PANIC:
1177 		/* fallthrough */
1178 	    case SNDCTL_SEQ_RESET:
1179 		/*
1180 		 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
1181 		 */
1182 		mtx_lock(&scp->seq_lock);
1183 		seq_reset(scp);
1184 		mtx_unlock(&scp->seq_lock);
1185 		ret = 0;
1186 		break;
1187 	    case SNDCTL_SEQ_TESTMIDI:
1188 		mtx_lock(&scp->seq_lock);
1189 		/*
1190 		 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
1191 		 * device?".
1192 		 */
1193 		mtx_unlock(&scp->seq_lock);
1194 		break;
1195 #if 0
1196 	    case SNDCTL_SEQ_GETINCOUNT:
1197 		if (mode == O_WRONLY)
1198 		    *(int *)arg = 0;
1199 		else {
1200 		    mtx_lock(&scp->seq_lock);
1201 		    *(int *)arg = scp->in_q.rl;
1202 		    mtx_unlock(&scp->seq_lock);
1203 		    SEQ_DEBUG(printf("seq_ioctl: incount %d.\n", *(int *)arg));
1204 		}
1205 		ret = 0;
1206 		break;
1207 	    case SNDCTL_SEQ_GETOUTCOUNT:
1208 		if (mode == O_RDONLY)
1209 		    *(int *)arg = 0;
1210 		else {
1211 		    mtx_lock(&scp->seq_lock);
1212 		    *(int *)arg = scp->out_q.fl;
1213 		    mtx_unlock(&scp->seq_lock);
1214 		    SEQ_DEBUG(printf("seq_ioctl: outcount %d.\n", *(int *)arg));
1215 		}
1216 		ret = 0;
1217 		break;
1218 #endif
1219 	    case SNDCTL_SEQ_CTRLRATE:
1220 		if (*(int *)arg != 0) {
1221 		    ret = EINVAL;
1222 		    break;
1223 		}
1224 		mtx_lock(&scp->seq_lock);
1225 		*(int *)arg = scp->timerbase;
1226 		mtx_unlock(&scp->seq_lock);
1227 		SEQ_DEBUG(3,printf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
1228 		ret = 0;
1229 		break;
1230 		/*
1231 		 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES
1232 		 */
1233 #if 0
1234 	    case SNDCTL_SEQ_RESETSAMPLES:
1235 		mtx_lock(&scp->seq_lock);
1236 		ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1237 		mtx_unlock(&scp->seq_lock);
1238 		if (ret != 0)
1239 		    break;
1240 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg, SND_DEV_MIDIN), cmd, arg, mode, td);
1241 		break;
1242 #endif
1243 	    case SNDCTL_SEQ_NRSYNTHS:
1244 		mtx_lock(&scp->seq_lock);
1245 		    *(int *)arg = scp->midi_number;
1246 		mtx_unlock(&scp->seq_lock);
1247 		SEQ_DEBUG(3,printf("seq_ioctl: synths %d.\n", *(int *)arg));
1248 		ret = 0;
1249 		break;
1250 	    case SNDCTL_SEQ_NRMIDIS:
1251 		mtx_lock(&scp->seq_lock);
1252 		if (scp->music)
1253 		    *(int *)arg = 0;
1254 		else {
1255 		    /*
1256 		     * TODO: count the numbder of devices that can WRITERAW
1257 		     */
1258 		    *(int *)arg = scp->midi_number;
1259 		}
1260 		mtx_unlock(&scp->seq_lock);
1261 		SEQ_DEBUG(3,printf("seq_ioctl: midis %d.\n", *(int *)arg));
1262 		ret = 0;
1263 		break;
1264 		/*
1265 		 * TODO: ioctl SNDCTL_SYNTH_MEMAVL
1266 		 */
1267 #if 0
1268 	    case SNDCTL_SYNTH_MEMAVL:
1269 		mtx_lock(&scp->seq_lock);
1270 		ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1271 		mtx_unlock(&scp->seq_lock);
1272 		if (ret != 0)
1273 		    break;
1274 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg, SND_DEV_MIDIN), cmd, arg, mode, td);
1275 		break;
1276 #endif
1277 	    case SNDCTL_SEQ_OUTOFBAND:
1278 		for (ret = 0; ret < EV_SZ; ret++)
1279 		    event[ret] = (u_char)arg[0];
1280 
1281 		mtx_lock(&scp->seq_lock);
1282 		if (scp->music)
1283 		    ret = seq_processevent(scp, event);
1284 		else {
1285 		    if (seq_convertold(event, newevent) > 0)
1286 			ret = seq_processevent(scp, newevent);
1287 		    else ret = EINVAL;
1288 		}
1289 		mtx_unlock(&scp->seq_lock);
1290 		break;
1291 	    case SNDCTL_SYNTH_INFO:
1292 		synthinfo = (struct synth_info *)arg;
1293 		midiunit = synthinfo->device;
1294 		mtx_lock(&scp->seq_lock);
1295 		if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1296 		    bzero(synthinfo, sizeof(*synthinfo));
1297 		    synthinfo->name[0] = 'f';
1298 		    synthinfo->name[1] = 'a';
1299 		    synthinfo->name[2] = 'k';
1300 		    synthinfo->name[3] = 'e';
1301 		    synthinfo->name[4] = 's';
1302 		    synthinfo->name[5] = 'y';
1303 		    synthinfo->name[6] = 'n';
1304 		    synthinfo->name[7] = 't';
1305 		    synthinfo->name[8] = 'h';
1306 		    synthinfo->device = midiunit;
1307 		    synthinfo->synth_type = SYNTH_TYPE_MIDI;
1308 		    synthinfo->capabilities = scp->midi_flags[midiunit];
1309 		    ret = 0;
1310 		} else
1311 		    ret = EINVAL;
1312 		mtx_unlock(&scp->seq_lock);
1313 		break;
1314 	    case SNDCTL_MIDI_INFO:
1315 		midiinfo = (struct midi_info *)arg;
1316 		midiunit = midiinfo->device;
1317 		mtx_lock(&scp->seq_lock);
1318 		if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1319 		    bzero(midiinfo, sizeof(*midiinfo));
1320 		    midiinfo->name[0] = 'f';
1321 		    midiinfo->name[1] = 'a';
1322 		    midiinfo->name[2] = 'k';
1323 		    midiinfo->name[3] = 'e';
1324 		    midiinfo->name[4] = 'm';
1325 		    midiinfo->name[5] = 'i';
1326 		    midiinfo->name[6] = 'd';
1327 		    midiinfo->name[7] = 'i';
1328 		    midiinfo->device = midiunit;
1329 		    midiinfo->capabilities = scp->midi_flags[midiunit];
1330 		    /*
1331 		     * TODO: What devtype?
1332 		     */
1333 		    midiinfo->dev_type = 0x01;
1334 		    ret = 0;
1335 		} else
1336 		    ret = EINVAL;
1337 		mtx_unlock(&scp->seq_lock);
1338 		break;
1339 	    case SNDCTL_SEQ_THRESHOLD:
1340 		mtx_lock(&scp->seq_lock);
1341 		RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1);
1342 		scp->out_water = *(int *)arg;
1343 		mtx_unlock(&scp->seq_lock);
1344 		SEQ_DEBUG(3,printf("seq_ioctl: water %d.\n", *(int *)arg));
1345 		ret = 0;
1346 		break;
1347 	    case SNDCTL_MIDI_PRETIME:
1348 		tmp = *(int *)arg;
1349 		if (tmp < 0)
1350 		    tmp = 0;
1351 		mtx_lock(&scp->seq_lock);
1352 		scp->pre_event_timeout = (hz * tmp) / 10;
1353 		*(int *)arg = scp->pre_event_timeout;
1354 		mtx_unlock(&scp->seq_lock);
1355 		SEQ_DEBUG(3,printf("seq_ioctl: pretime %d.\n", *(int *)arg));
1356 		ret = 0;
1357 		break;
1358 	    case SNDCTL_FM_4OP_ENABLE:
1359 	    case SNDCTL_PMGR_IFACE:
1360 	    case SNDCTL_PMGR_ACCESS:
1361 		/*
1362 		 * Patch manager and fm are ded, ded, ded.
1363 		 */
1364 		/* fallthrough */
1365 	    default:
1366 		/*
1367 		 * TODO: Consider ioctl default case.
1368 		 * Old code used to
1369 		 * if ((scp->fflags & O_ACCMODE) == FREAD) {
1370 		 *	ret = EIO;
1371 		 *	break;
1372 		 * }
1373 		 * Then pass on the ioctl to device 0
1374 		 */
1375 		SEQ_DEBUG(2,printf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
1376 		ret = EINVAL;
1377 		break;
1378 	}
1379 
1380 	return ret;
1381 }
1382 
1383 int
1384 seq_poll(struct cdev *i_dev, int events, struct thread *td)
1385 {
1386 	int ret, lim;
1387 	struct seq_softc *scp = i_dev->si_drv1;
1388 
1389 	SEQ_DEBUG(3, printf("seq_poll: unit %d.\n", scp->unit));
1390 	printf("seq_poll: unit %d.\n", scp->unit);
1391 
1392 	mtx_lock(&scp->seq_lock);
1393 
1394 	ret = 0;
1395 
1396 	/* Look up the apropriate queue and select it. */
1397 	if ((events & (POLLOUT | POLLWRNORM)) != 0) {
1398 	    /* Start playing. */
1399 	    scp->playing = 1;
1400 	    cv_broadcast(&scp->state_cv);
1401 	    cv_broadcast(&scp->out_cv);
1402 
1403 	    lim = scp->out_water;
1404 
1405 	    if (MIDIQ_AVAIL(scp->out_q) < lim)
1406 		/* No enough space, record select. */
1407 		selrecord(td, &scp->out_sel);
1408 	    else
1409 		/* We can write now. */
1410 		ret |= events & (POLLOUT | POLLWRNORM);
1411 	}
1412 
1413 	if ((events & (POLLIN | POLLRDNORM)) != 0) {
1414 	    /* TODO: Start recording. */
1415 
1416 	    /* Find out the boundary. */
1417 	    lim = 1;
1418 	    if (MIDIQ_LEN(scp->in_q) < lim)
1419 		/* No data ready, record select. */
1420 		selrecord(td, &scp->in_sel);
1421 	    else
1422 		/* We can read now. */
1423 		ret |= events & (POLLIN | POLLRDNORM);
1424 	}
1425 
1426 	mtx_unlock(&scp->seq_lock);
1427 
1428 	return (ret);
1429 }
1430 #if 0
1431 static void
1432 sein_qtr(void *p, void /* mididev_info */ *md)
1433 {
1434 	struct seq_softc *scp;
1435 
1436 	scp = (struct seq_softc *)p;
1437 
1438 	mtx_lock(&scp->seq_lock);
1439 
1440 	/* Restart playing if we have the data to output. */
1441 	if (scp->queueout_pending)
1442 		seq_callback(scp, SEQ_CB_START | SEQ_CB_WR);
1443 	/* Check the midi device if we are reading. */
1444 	if ((scp->flags & SEQ_F_READING) != 0)
1445 		seq_midiinput(scp, md);
1446 
1447 	mtx_unlock(&scp->seq_lock);
1448 }
1449 #endif
1450 /*
1451  * seq_convertold
1452  * Was the old playevent.  Use this to convert and old
1453  * style /dev/sequencer event to a /dev/music event
1454  */
1455 static int
1456 seq_convertold(u_char *event, u_char *out)
1457 {
1458 	int used;
1459 	u_char dev, chn, note, vel;
1460 
1461 	out[0] = out[1] = out[2] = out[3] = out[4]  = out[5]  = out[6]  = out[7]   = 0;
1462 
1463 	dev = 0;
1464 	chn = event[1];
1465 	note = event[2];
1466 	vel = event[3];
1467 
1468 	used = 0;
1469 
1470 restart:
1471 	/*
1472 	 * TODO: Debug statement
1473 	 */
1474 	switch(event[0]) {
1475 	    case EV_TIMING:
1476 	    case EV_CHN_VOICE:
1477 	    case EV_CHN_COMMON:
1478 case EV_SYSEX:
1479 case EV_SEQ_LOCAL:
1480 		out[0] = event[0];
1481 		out[1] = event[1];
1482 		out[2] = event[2];
1483 		out[3] = event[3];
1484 		out[4] = event[4];
1485 		out[5] = event[5];
1486 		out[6] = event[6];
1487 		out[7] = event[7];
1488 		used += 8;
1489 		break;
1490 	    case SEQ_NOTEOFF:
1491 		out[0] = EV_CHN_VOICE;
1492 		out[1] = dev;
1493 		out[2] = MIDI_NOTEOFF;
1494 		out[3] = chn;
1495 		out[4] = note;
1496 		out[5] = 255;
1497 	used += 4;
1498 		break;
1499 
1500 	    case SEQ_NOTEON:
1501 		out[0] = EV_CHN_VOICE;
1502 		out[1] = dev;
1503 		out[2] = MIDI_NOTEON;
1504 		out[3] = chn;
1505 		out[4] = note;
1506 		out[5] = vel;
1507 	used += 4;
1508 		break;
1509 
1510 		/*
1511 		 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4]
1512 		 */
1513 
1514 	    case SEQ_PGMCHANGE:
1515 		out[0] = EV_CHN_COMMON;
1516 		out[1] = dev;
1517 		out[2] = MIDI_PGM_CHANGE;
1518 		out[3] = chn;
1519 		out[4] = note;
1520 		out[5] = vel;
1521 	used += 4;
1522 		break;
1523 /*
1524 		out[0] = EV_TIMING;
1525 		out[1] = dev;
1526 		out[2] = MIDI_PGM_CHANGE;
1527 		out[3] = chn;
1528 		out[4] = note;
1529 		out[5] = vel;
1530 		SEQ_DEBUG(4,printf("seq_playevent: synctimer\n"));
1531 		break;
1532 */
1533 
1534 	    case SEQ_MIDIPUTC:
1535 		SEQ_DEBUG(4,printf("seq_playevent: put data 0x%02x, unit %d.\n",
1536 			    event[1], event[2]));
1537 		/*
1538 		 * Pass through to the midi device.
1539 		 * device = event[2]
1540 		 * data = event[1]
1541 		 */
1542 		out[0] = SEQ_MIDIPUTC;
1543 		out[1] = dev;
1544 		out[2] = chn;
1545 	used += 4;
1546 		break;
1547 #ifdef notyet
1548 	    case SEQ_ECHO:
1549 		/*
1550 		 * This isn't handled here yet because I don't know if I can
1551 		 * just use four bytes events.  There might be consequences
1552 		 * in the _read routing
1553 		 */
1554 		if (seq_copytoinput(scp, event, 4) == EAGAIN) {
1555 		    ret = QUEUEFULL;
1556 		    break;
1557 		}
1558 		ret = MORE;
1559 		break;
1560 #endif
1561 	    case SEQ_EXTENDED:
1562 		switch (event[1]) {
1563 		    case SEQ_NOTEOFF:
1564 		    case SEQ_NOTEON:
1565 		    case SEQ_PGMCHANGE:
1566 			event++;
1567 			used = 4;
1568 			goto restart;
1569 			break;
1570 		    case SEQ_AFTERTOUCH:
1571 			/*
1572 			 * SYNTH_AFTERTOUCH(md, event[3], event[4])
1573 			 */
1574 		    case SEQ_BALANCE:
1575 			/*
1576 			 * SYNTH_PANNING(md, event[3], (char)event[4])
1577 			 */
1578 		    case SEQ_CONTROLLER:
1579 			/*
1580 			 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5])
1581 			 */
1582 		    case SEQ_VOLMODE:
1583 			/*
1584 			 * SYNTH_VOLUMEMETHOD(md, event[3])
1585 			 */
1586 		    default:
1587 			SEQ_DEBUG(2,printf("seq_convertold: SEQ_EXTENDED type %d"
1588 				"not handled\n", event[1]));
1589 			break;
1590 		}
1591 		break;
1592 	    case SEQ_WAIT:
1593 		out[0] = EV_TIMING;
1594 		out[1] = TMR_WAIT_REL;
1595 		out[4] = event[2];
1596 		out[5] = event[3];
1597 		out[6] = event[4];
1598 
1599 		SEQ_DEBUG(5,printf("SEQ_WAIT %d", event[2] + (event[3] << 8) + (event[4] << 24)));
1600 
1601 		used+= 4;
1602 		break;
1603 
1604 	    case SEQ_ECHO:
1605 	    case SEQ_SYNCTIMER:
1606 	    case SEQ_PRIVATE:
1607 	    default:
1608 		SEQ_DEBUG(2,printf("seq_convertold: event type %d not handled %d %d %d\n", event[0], event[1], event[2], event[3]));
1609 		break;
1610 	}
1611 	return used;
1612 }
1613 
1614 /*
1615  * Writting to the sequencer buffer never blocks and drops
1616  * input which cannot be queued
1617  */
1618 void
1619 seq_copytoinput(struct seq_softc *scp, u_char *event, int len)
1620 {
1621 
1622 	mtx_assert(&scp->seq_lock, MA_OWNED);
1623 
1624 	if (MIDIQ_AVAIL(scp->in_q) < len) {
1625 	    /*
1626 	     * ENOROOM?  EINPUTDROPPED? ETOUGHLUCK?
1627 	     */
1628 	  SEQ_DEBUG(2,printf("seq_copytoinput: queue full\n"));
1629 	} else {
1630 	    MIDIQ_ENQ(scp->in_q, event, len);
1631 	    selwakeup(&scp->in_sel);
1632 	    cv_broadcast(&scp->in_cv);
1633 	}
1634 
1635 }
1636 
1637 static int
1638 seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
1639 {
1640 	int ret, voice;
1641 	u_char cmd, chn, note, parm;
1642 
1643 	ret = 0;
1644 	cmd = event[2];
1645 	chn = event[3];
1646 	note = event[4];
1647 	parm = event[5];
1648 
1649 	mtx_assert(&scp->seq_lock, MA_OWNED);
1650 
1651 	SEQ_DEBUG(5,printf("seq_chnvoice: unit %d, dev %d, cmd %s,"
1652 		   " chn %d, note %d, parm %d.\n", scp->unit, event[1],
1653 		   midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm));
1654 
1655 	voice = SYNTH_ALLOC(md, chn, note);
1656 
1657 	mtx_unlock(&scp->seq_lock);
1658 
1659 	switch (cmd) {
1660 	    case MIDI_NOTEON:
1661 		if (note < 128 || note == 255) {
1662 #if 0
1663 		    if (scp->music && chn == 9) {
1664 			/*
1665 			 * This channel is a percussion. The note number is the
1666 			 * patch number.
1667 			 */
1668 			/*
1669 			mtx_unlock(&scp->seq_lock);
1670 			if (SYNTH_SETINSTR(md, voice, 128 + note) == EAGAIN) {
1671 			    mtx_lock(&scp->seq_lock);
1672 			    return (QUEUEFULL);
1673 			}
1674 			mtx_lock(&scp->seq_lock);
1675 			*/
1676 			note = 60; /* Middle C. */
1677 		    }
1678 #endif
1679 		    if (scp->music) {
1680 			/*
1681 			mtx_unlock(&scp->seq_lock);
1682 			if (SYNTH_SETUPVOICE(md, voice, chn) == EAGAIN) {
1683 			    mtx_lock(&scp->seq_lock);
1684 			    return (QUEUEFULL);
1685 			}
1686 			mtx_lock(&scp->seq_lock);
1687 			*/
1688 		    }
1689 		    SYNTH_STARTNOTE(md, voice, note, parm);
1690 		}
1691 		break;
1692 	case MIDI_NOTEOFF:
1693 		SYNTH_KILLNOTE(md, voice, note, parm);
1694 		break;
1695 	case MIDI_KEY_PRESSURE:
1696 		SYNTH_AFTERTOUCH(md, voice, parm);
1697 		break;
1698 	default:
1699 		ret = 1;
1700 		SEQ_DEBUG(2,printf("seq_chnvoice event type %d not handled\n", event[1]));
1701 		break;
1702 	}
1703 
1704 	mtx_lock(&scp->seq_lock);
1705 	return ret;
1706 }
1707 
1708 static int
1709 seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
1710 {
1711 	int ret;
1712 	u_short w14;
1713 	u_char cmd, chn, p1;
1714 
1715 	ret = 0;
1716 	cmd = event[2];
1717 	chn = event[3];
1718 	p1 = event[4];
1719 	w14 = *(u_short *)&event[6];
1720 
1721 	SEQ_DEBUG(5,printf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
1722 		    " p1 %d, w14 %d.\n", scp->unit, event[1],
1723 		    midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14));
1724 	mtx_unlock(&scp->seq_lock);
1725 	switch (cmd) {
1726 	    case MIDI_PGM_CHANGE:
1727 		    SEQ_DEBUG(4,printf("seq_chncommon pgmchn chn %d pg %d\n",
1728 				chn, p1));
1729 		    SYNTH_SETINSTR(md, chn, p1);
1730 		break;
1731 	    case MIDI_CTL_CHANGE:
1732 		SEQ_DEBUG(4,printf("seq_chncommon ctlch chn %d pg %d %d\n",
1733 			    chn, p1, w14));
1734 		SYNTH_CONTROLLER(md, chn, p1, w14);
1735 		break;
1736 	    case MIDI_PITCH_BEND:
1737 		if (scp->music) {
1738 		    /*
1739 		     * TODO: MIDI_PITCH_BEND
1740 		     */
1741 #if 0
1742 		    mtx_lock(&md->synth.vc_mtx);
1743 		    md->synth.chn_info[chn].bender_value = w14;
1744 		    if (md->midiunit >= 0) {
1745 			/* Handle all of the notes playing on this channel. */
1746 			key = ((int)chn << 8);
1747 			for (i = 0 ; i < md->synth.alloc.max_voice ; i++)
1748 			    if ((md->synth.alloc.map[i] & 0xff00) == key) {
1749 				mtx_unlock(&md->synth.vc_mtx);
1750 				mtx_unlock(&scp->seq_lock);
1751 				if (md->synth.bender(md, i, w14) == EAGAIN) {
1752 				    mtx_lock(&scp->seq_lock);
1753 				    return (QUEUEFULL);
1754 				}
1755 				mtx_lock(&scp->seq_lock);
1756 			    }
1757 		    } else {
1758 			mtx_unlock(&md->synth.vc_mtx);
1759 			mtx_unlock(&scp->seq_lock);
1760 			if (md->synth.bender(md, chn, w14) == EAGAIN) {
1761 			    mtx_lock(&scp->seq_lock);
1762 			    return (QUEUEFULL);
1763 			}
1764 			mtx_lock(&scp->seq_lock);
1765 		    }
1766 #endif
1767 		} else
1768 		    SYNTH_BENDER(md, chn, w14);
1769 		break;
1770 	    default:
1771 		ret = 1;
1772 		SEQ_DEBUG(2,printf("seq_chncommon event type %d not handled.\n", event[1]));
1773 		break;
1774 
1775 	}
1776 	mtx_lock(&scp->seq_lock);
1777 	return ret;
1778 }
1779 
1780 static int
1781 seq_timing(struct seq_softc *scp, u_char *event)
1782 {
1783 	int param;
1784 	int ret;
1785 
1786 	ret = 0;
1787 	param = event[4] + (event[5] << 8) +
1788 	    (event[6] << 16) + (event[7] << 24);
1789 
1790 	SEQ_DEBUG(5,printf("seq_timing: unit %d, cmd %d, param %d.\n",
1791 		    scp->unit, event[1], param));
1792 	switch (event[1]) {
1793 	case TMR_WAIT_REL:
1794 	    timer_wait(scp, param, 0);
1795 		break;
1796 	case TMR_WAIT_ABS:
1797 	    timer_wait(scp, param, 1);
1798 		break;
1799 	case TMR_START:
1800 		timer_start(scp);
1801 		cv_broadcast(&scp->reset_cv);
1802 		break;
1803 	case TMR_STOP:
1804 		timer_stop(scp);
1805 		/*
1806 		 * The following cv_broadcast isn't needed since we only
1807 		 * wait for 0->1 transitions.  It probably won't hurt
1808 		 */
1809 		cv_broadcast(&scp->reset_cv);
1810 		break;
1811 	case TMR_CONTINUE:
1812 		timer_continue(scp);
1813 		cv_broadcast(&scp->reset_cv);
1814 		break;
1815 	case TMR_TEMPO:
1816 		if (param < 8)
1817 		    param = 8;
1818 		if (param > 360)
1819 		    param = 360;
1820 		SEQ_DEBUG(4,printf("Timer set tempo %d\n", param));
1821 		timer_setvals(scp, param, scp->timerbase);
1822 		break;
1823 	case TMR_TIMERBASE:
1824 		if (param < 1)
1825 		    param = 1;
1826 		if (param > 1000)
1827 		    param = 1000;
1828 		SEQ_DEBUG(4,printf("Timer set timerbase %d\n", param));
1829 		timer_setvals(scp, scp->tempo, param);
1830 		break;
1831 	case TMR_ECHO:
1832 		/*
1833 		 * TODO: Consider making 4-byte events for /dev/sequencer
1834 		 * PRO: Maybe needed by legacy apps
1835 		 * CON: soundcard.h has been warning for a while many years
1836 		 * to expect 8 byte events.
1837 		 */
1838 #if 0
1839 		if (scp->music)
1840 			seq_copytoinput(scp, event, 8);
1841 		else {
1842 			param = (param << 8 | SEQ_ECHO);
1843 			seq_copytoinput(scp, (u_char *)&param, 4);
1844 		}
1845 #else
1846 		seq_copytoinput(scp, event, 8);
1847 #endif
1848 		break;
1849 	    default:
1850 		SEQ_DEBUG(2,printf("seq_timing event type %d not handled.\n", event[1]));
1851 		ret = 1;
1852 		break;
1853 	}
1854 	return ret;
1855 }
1856 
1857 static int
1858 seq_local(struct seq_softc *scp, u_char *event)
1859 {
1860 	int ret;
1861 
1862 	ret = 0;
1863 	mtx_assert(&scp->seq_lock, MA_OWNED);
1864 
1865 	SEQ_DEBUG(5,printf("seq_local: unit %d, cmd %d\n", scp->unit, event[1]));
1866 	switch (event[1]) {
1867 	    default:
1868 		printf("seq_local event type %d not handled\n", event[1]);
1869 		ret = 1;
1870 		break;
1871 	}
1872 	return ret;
1873 }
1874 
1875 static int
1876 seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event)
1877 {
1878 	int i, l;
1879 
1880 	mtx_assert(&scp->seq_lock, MA_OWNED);
1881 	SEQ_DEBUG(5,printf("seq_sysex: unit %d device %d\n", scp->unit, event[1]));
1882 	l = 0;
1883 	for (i = 0 ; i < 6 && event[i + 2] != 0xff ; i++)
1884 		l = i + 1;
1885 	if (l > 0) {
1886 		mtx_unlock(&scp->seq_lock);
1887 		if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) {
1888 			mtx_lock(&scp->seq_lock);
1889 			return 1;
1890 		}
1891 		mtx_lock(&scp->seq_lock);
1892 	}
1893 	return 0;
1894 }
1895 
1896 /*
1897  * Reset no longer closes the raw devices nor seq_sync's
1898  * Callers are IOCTL and seq_close
1899  */
1900 static void
1901 seq_reset(struct seq_softc *scp)
1902 {
1903 	int chn,  i;
1904 	kobj_t m;
1905 
1906 	mtx_assert(&scp->seq_lock, MA_OWNED);
1907 
1908 	SEQ_DEBUG(5,printf("seq_reset: unit %d.\n", scp->unit));
1909 
1910 	/*
1911 	 * Stop reading and writing.
1912 	 */
1913 
1914 	/* scp->recording = 0; */
1915 	scp->playing = 0;
1916 	cv_broadcast(&scp->state_cv);
1917 	cv_broadcast(&scp->out_cv);
1918 	cv_broadcast(&scp->reset_cv);
1919 
1920 	/*
1921 	 * For now, don't reset the timers.
1922 	 */
1923 	MIDIQ_CLEAR(scp->in_q);
1924 	MIDIQ_CLEAR(scp->out_q);
1925 
1926 	for (i = 0; i < scp->midi_number; i++) {
1927 	    m = scp->midis[i];
1928 	    mtx_unlock(&scp->seq_lock);
1929 	    SYNTH_RESET(m);
1930 	    for (chn = 0 ; chn < 16 ; chn++) {
1931 		SYNTH_CONTROLLER(m, chn, 123, 0) ;
1932 		SYNTH_CONTROLLER(m, chn, 121, 0);
1933 		SYNTH_BENDER(m, chn, 1 << 13);
1934 	    }
1935 	    mtx_lock(&scp->seq_lock);
1936 	}
1937 }
1938 
1939 /*
1940  * seq_sync
1941  * *really* flush the output queue
1942  * flush the event queue, then flush the synthsisers.
1943  * Callers are IOCTL and close
1944  */
1945 
1946 #define SEQ_SYNC_TIMEOUT 8
1947 static int
1948 seq_sync(struct seq_softc *scp)
1949 {
1950 	int i, rl, sync[16], done;
1951 
1952 	mtx_assert(&scp->seq_lock, MA_OWNED);
1953 
1954 	SEQ_DEBUG(4,printf("seq_sync: unit %d.\n", scp->unit));
1955 
1956 	/*
1957 	 * Wait until output queue is empty.  Check every so often to see if
1958 	 * the queue is moving along.  If it isn't just abort.
1959 	 */
1960 	while (!MIDIQ_EMPTY(scp->out_q)) {
1961 
1962 	    if (!scp->playing) {
1963 		scp->playing = 1;
1964 		cv_broadcast(&scp->state_cv);
1965 		cv_broadcast(&scp->out_cv);
1966 	    }
1967 
1968 	    rl = MIDIQ_LEN(scp->out_q);
1969 
1970 	    i = cv_timedwait_sig(&scp->out_cv,
1971 		    &scp->seq_lock, SEQ_SYNC_TIMEOUT * hz);
1972 
1973 	    if (i == EINTR || i == ERESTART) {
1974 		if (i == EINTR) {
1975 		    /*
1976 		     * XXX: I don't know why we stop playing
1977 		     */
1978 		    scp->playing = 0;
1979 		    cv_broadcast(&scp->out_cv);
1980 		}
1981 		return i;
1982 	    }
1983 
1984 	    if (i == EWOULDBLOCK && rl == MIDIQ_LEN(scp->out_q) &&
1985 		    scp->waiting == 0) {
1986 		/*
1987 		 * A queue seems to be stuck up. Give up and clear queues.
1988 		 */
1989 		MIDIQ_CLEAR(scp->out_q);
1990 		scp->playing = 0;
1991 		cv_broadcast(&scp->state_cv);
1992 		cv_broadcast(&scp->out_cv);
1993 		cv_broadcast(&scp->reset_cv);
1994 
1995 		/*
1996 		 * TODO: Consider if the raw devices need to be flushed
1997 		 */
1998 
1999 		SEQ_DEBUG(1,printf("seq_sync queue stuck, aborting\n"));
2000 
2001 		return i;
2002 	    }
2003 	}
2004 
2005 	scp->playing = 0;
2006 	/*
2007 	 * Since syncing a midi device might block, unlock scp->seq_lock.
2008 	 */
2009 
2010 	mtx_unlock(&scp->seq_lock);
2011 	for(i = 0 ; i < scp->midi_number; i++)
2012 	    sync[i] = 1;
2013 
2014 	do {
2015 	    done = 1;
2016 	    for (i = 0 ; i < scp->midi_number; i++)
2017 		if (sync[i]) {
2018 		    if (SYNTH_INSYNC(scp->midis[i]) == 0)
2019 			sync[i] = 0;
2020 		    else
2021 			done = 0;
2022 		}
2023 
2024 	   if (!done)
2025 		DELAY(5000);
2026 
2027 	} while (!done);
2028 
2029 	mtx_lock(&scp->seq_lock);
2030 	return 0;
2031 }
2032 
2033 char *
2034 midi_cmdname(int cmd, midi_cmdtab *tab)
2035 {
2036 	while (tab->name != NULL) {
2037 		if (cmd == tab->cmd)
2038 			return (tab->name);
2039 		tab++;
2040 	}
2041 
2042 	return ("unknown");
2043 }
2044