xref: /freebsd/sys/dev/sound/midi/sequencer.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
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 	SEQ_DEBUG(1, 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 	SEQ_DEBUG(1, printf("seq_delunit: 2 \n"));
625 	cv_wait(&scp->th_cv, &scp->seq_lock);
626 	SEQ_DEBUG(1, printf("seq_delunit: 3.0 \n"));
627 	mtx_unlock(&scp->seq_lock);
628 	SEQ_DEBUG(1, printf("seq_delunit: 3.1 \n"));
629 
630 	cv_destroy(&scp->state_cv);
631 	SEQ_DEBUG(1, printf("seq_delunit: 4 \n"));
632 	cv_destroy(&scp->empty_cv);
633 	SEQ_DEBUG(1, printf("seq_delunit: 5 \n"));
634 	cv_destroy(&scp->reset_cv);
635 	SEQ_DEBUG(1, printf("seq_delunit: 6 \n"));
636 	cv_destroy(&scp->out_cv);
637 	SEQ_DEBUG(1, printf("seq_delunit: 7 \n"));
638 	cv_destroy(&scp->in_cv);
639 	SEQ_DEBUG(1, printf("seq_delunit: 8 \n"));
640 	cv_destroy(&scp->th_cv);
641 
642 	SEQ_DEBUG(1, printf("seq_delunit: 10 \n"));
643 	if (scp->seqdev)
644 	    destroy_dev(scp->seqdev);
645 	SEQ_DEBUG(1, printf("seq_delunit: 11 \n"));
646 	if (scp->musicdev)
647 	    destroy_dev(scp->musicdev);
648 	SEQ_DEBUG(1, printf("seq_delunit: 12 \n"));
649 	scp->seqdev = scp->musicdev = NULL;
650 	if (scp->midis != NULL)
651 	    free(scp->midis, M_TEMP);
652 	SEQ_DEBUG(1, printf("seq_delunit: 13 \n"));
653 	if (scp->midi_flags != NULL)
654 	    free(scp->midi_flags, M_TEMP);
655 	SEQ_DEBUG(1, printf("seq_delunit: 14 \n"));
656 	free(scp->out_q.b, M_TEMP);
657 	SEQ_DEBUG(1, printf("seq_delunit: 15 \n"));
658 	free(scp->in_q.b, M_TEMP);
659 
660 	SEQ_DEBUG(1, printf("seq_delunit: 16 \n"));
661 
662 	mtx_destroy(&scp->seq_lock);
663 	SEQ_DEBUG(1, 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 		event[4] = *(int *)arg & 0xFF;
1137 		event[5] = (*(int *)arg >> 8) & 0xFF;
1138 		event[6] = (*(int *)arg >> 16) & 0xFF;
1139 		event[7] = (*(int *)arg >> 24) & 0xFF;
1140 		goto timerevent;
1141 	    case SNDCTL_TMR_TIMEBASE:
1142 		event[1] = TMR_TIMERBASE;
1143 		event[4] = *(int *)arg & 0xFF;
1144 		event[5] = (*(int *)arg >> 8) & 0xFF;
1145 		event[6] = (*(int *)arg >> 16) & 0xFF;
1146 		event[7] = (*(int *)arg >> 24) & 0xFF;
1147 		goto timerevent;
1148 	    case SNDCTL_TMR_START:
1149 		event[1] = TMR_START;
1150 		goto timerevent;
1151 	    case SNDCTL_TMR_STOP:
1152 		event[1] = TMR_STOP;
1153 		goto timerevent;
1154 	    case SNDCTL_TMR_CONTINUE:
1155 		event[1] = TMR_CONTINUE;
1156 	    timerevent:
1157 		event[0] = EV_TIMING;
1158 		mtx_lock(&scp->seq_lock);
1159 		if (!scp->music) {
1160 		    ret = EINVAL;
1161 		    mtx_unlock(&scp->seq_lock);
1162 		    break;
1163 		}
1164 		seq_processevent(scp, event);
1165 		mtx_unlock(&scp->seq_lock);
1166 		break;
1167 	    case SNDCTL_TMR_SELECT:
1168 		SEQ_DEBUG(2,printf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n"));
1169 		ret = EINVAL;
1170 		break;
1171 	    case SNDCTL_SEQ_SYNC:
1172 		if (mode == O_RDONLY) {
1173 		    ret = 0;
1174 		    break;
1175 		}
1176 		mtx_lock(&scp->seq_lock);
1177 		ret = seq_sync(scp);
1178 		mtx_unlock(&scp->seq_lock);
1179 		break;
1180 	    case SNDCTL_SEQ_PANIC:
1181 		/* fallthrough */
1182 	    case SNDCTL_SEQ_RESET:
1183 		/*
1184 		 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET
1185 		 */
1186 		mtx_lock(&scp->seq_lock);
1187 		seq_reset(scp);
1188 		mtx_unlock(&scp->seq_lock);
1189 		ret = 0;
1190 		break;
1191 	    case SNDCTL_SEQ_TESTMIDI:
1192 		mtx_lock(&scp->seq_lock);
1193 		/*
1194 		 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the
1195 		 * device?".
1196 		 */
1197 		mtx_unlock(&scp->seq_lock);
1198 		break;
1199 #if 0
1200 	    case SNDCTL_SEQ_GETINCOUNT:
1201 		if (mode == O_WRONLY)
1202 		    *(int *)arg = 0;
1203 		else {
1204 		    mtx_lock(&scp->seq_lock);
1205 		    *(int *)arg = scp->in_q.rl;
1206 		    mtx_unlock(&scp->seq_lock);
1207 		    SEQ_DEBUG(printf("seq_ioctl: incount %d.\n", *(int *)arg));
1208 		}
1209 		ret = 0;
1210 		break;
1211 	    case SNDCTL_SEQ_GETOUTCOUNT:
1212 		if (mode == O_RDONLY)
1213 		    *(int *)arg = 0;
1214 		else {
1215 		    mtx_lock(&scp->seq_lock);
1216 		    *(int *)arg = scp->out_q.fl;
1217 		    mtx_unlock(&scp->seq_lock);
1218 		    SEQ_DEBUG(printf("seq_ioctl: outcount %d.\n", *(int *)arg));
1219 		}
1220 		ret = 0;
1221 		break;
1222 #endif
1223 	    case SNDCTL_SEQ_CTRLRATE:
1224 		if (*(int *)arg != 0) {
1225 		    ret = EINVAL;
1226 		    break;
1227 		}
1228 		mtx_lock(&scp->seq_lock);
1229 		*(int *)arg = scp->timerbase;
1230 		mtx_unlock(&scp->seq_lock);
1231 		SEQ_DEBUG(3,printf("seq_ioctl: ctrlrate %d.\n", *(int *)arg));
1232 		ret = 0;
1233 		break;
1234 		/*
1235 		 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES
1236 		 */
1237 #if 0
1238 	    case SNDCTL_SEQ_RESETSAMPLES:
1239 		mtx_lock(&scp->seq_lock);
1240 		ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1241 		mtx_unlock(&scp->seq_lock);
1242 		if (ret != 0)
1243 		    break;
1244 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg, SND_DEV_MIDIN), cmd, arg, mode, td);
1245 		break;
1246 #endif
1247 	    case SNDCTL_SEQ_NRSYNTHS:
1248 		mtx_lock(&scp->seq_lock);
1249 		    *(int *)arg = scp->midi_number;
1250 		mtx_unlock(&scp->seq_lock);
1251 		SEQ_DEBUG(3,printf("seq_ioctl: synths %d.\n", *(int *)arg));
1252 		ret = 0;
1253 		break;
1254 	    case SNDCTL_SEQ_NRMIDIS:
1255 		mtx_lock(&scp->seq_lock);
1256 		if (scp->music)
1257 		    *(int *)arg = 0;
1258 		else {
1259 		    /*
1260 		     * TODO: count the numbder of devices that can WRITERAW
1261 		     */
1262 		    *(int *)arg = scp->midi_number;
1263 		}
1264 		mtx_unlock(&scp->seq_lock);
1265 		SEQ_DEBUG(3,printf("seq_ioctl: midis %d.\n", *(int *)arg));
1266 		ret = 0;
1267 		break;
1268 		/*
1269 		 * TODO: ioctl SNDCTL_SYNTH_MEMAVL
1270 		 */
1271 #if 0
1272 	    case SNDCTL_SYNTH_MEMAVL:
1273 		mtx_lock(&scp->seq_lock);
1274 		ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md);
1275 		mtx_unlock(&scp->seq_lock);
1276 		if (ret != 0)
1277 		    break;
1278 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg, SND_DEV_MIDIN), cmd, arg, mode, td);
1279 		break;
1280 #endif
1281 	    case SNDCTL_SEQ_OUTOFBAND:
1282 		for (ret = 0; ret < EV_SZ; ret++)
1283 		    event[ret] = (u_char)arg[0];
1284 
1285 		mtx_lock(&scp->seq_lock);
1286 		if (scp->music)
1287 		    ret = seq_processevent(scp, event);
1288 		else {
1289 		    if (seq_convertold(event, newevent) > 0)
1290 			ret = seq_processevent(scp, newevent);
1291 		    else ret = EINVAL;
1292 		}
1293 		mtx_unlock(&scp->seq_lock);
1294 		break;
1295 	    case SNDCTL_SYNTH_INFO:
1296 		synthinfo = (struct synth_info *)arg;
1297 		midiunit = synthinfo->device;
1298 		mtx_lock(&scp->seq_lock);
1299 		if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1300 		    bzero(synthinfo, sizeof(*synthinfo));
1301 		    synthinfo->name[0] = 'f';
1302 		    synthinfo->name[1] = 'a';
1303 		    synthinfo->name[2] = 'k';
1304 		    synthinfo->name[3] = 'e';
1305 		    synthinfo->name[4] = 's';
1306 		    synthinfo->name[5] = 'y';
1307 		    synthinfo->name[6] = 'n';
1308 		    synthinfo->name[7] = 't';
1309 		    synthinfo->name[8] = 'h';
1310 		    synthinfo->device = midiunit;
1311 		    synthinfo->synth_type = SYNTH_TYPE_MIDI;
1312 		    synthinfo->capabilities = scp->midi_flags[midiunit];
1313 		    ret = 0;
1314 		} else
1315 		    ret = EINVAL;
1316 		mtx_unlock(&scp->seq_lock);
1317 		break;
1318 	    case SNDCTL_MIDI_INFO:
1319 		midiinfo = (struct midi_info *)arg;
1320 		midiunit = midiinfo->device;
1321 		mtx_lock(&scp->seq_lock);
1322 		if (seq_fetch_mid(scp, midiunit, &md) == 0) {
1323 		    bzero(midiinfo, sizeof(*midiinfo));
1324 		    midiinfo->name[0] = 'f';
1325 		    midiinfo->name[1] = 'a';
1326 		    midiinfo->name[2] = 'k';
1327 		    midiinfo->name[3] = 'e';
1328 		    midiinfo->name[4] = 'm';
1329 		    midiinfo->name[5] = 'i';
1330 		    midiinfo->name[6] = 'd';
1331 		    midiinfo->name[7] = 'i';
1332 		    midiinfo->device = midiunit;
1333 		    midiinfo->capabilities = scp->midi_flags[midiunit];
1334 		    /*
1335 		     * TODO: What devtype?
1336 		     */
1337 		    midiinfo->dev_type = 0x01;
1338 		    ret = 0;
1339 		} else
1340 		    ret = EINVAL;
1341 		mtx_unlock(&scp->seq_lock);
1342 		break;
1343 	    case SNDCTL_SEQ_THRESHOLD:
1344 		mtx_lock(&scp->seq_lock);
1345 		RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1);
1346 		scp->out_water = *(int *)arg;
1347 		mtx_unlock(&scp->seq_lock);
1348 		SEQ_DEBUG(3,printf("seq_ioctl: water %d.\n", *(int *)arg));
1349 		ret = 0;
1350 		break;
1351 	    case SNDCTL_MIDI_PRETIME:
1352 		tmp = *(int *)arg;
1353 		if (tmp < 0)
1354 		    tmp = 0;
1355 		mtx_lock(&scp->seq_lock);
1356 		scp->pre_event_timeout = (hz * tmp) / 10;
1357 		*(int *)arg = scp->pre_event_timeout;
1358 		mtx_unlock(&scp->seq_lock);
1359 		SEQ_DEBUG(3,printf("seq_ioctl: pretime %d.\n", *(int *)arg));
1360 		ret = 0;
1361 		break;
1362 	    case SNDCTL_FM_4OP_ENABLE:
1363 	    case SNDCTL_PMGR_IFACE:
1364 	    case SNDCTL_PMGR_ACCESS:
1365 		/*
1366 		 * Patch manager and fm are ded, ded, ded.
1367 		 */
1368 		/* fallthrough */
1369 	    default:
1370 		/*
1371 		 * TODO: Consider ioctl default case.
1372 		 * Old code used to
1373 		 * if ((scp->fflags & O_ACCMODE) == FREAD) {
1374 		 *	ret = EIO;
1375 		 *	break;
1376 		 * }
1377 		 * Then pass on the ioctl to device 0
1378 		 */
1379 		SEQ_DEBUG(2,printf("seq_ioctl: unsupported IOCTL %ld.\n", cmd));
1380 		ret = EINVAL;
1381 		break;
1382 	}
1383 
1384 	return ret;
1385 }
1386 
1387 int
1388 seq_poll(struct cdev *i_dev, int events, struct thread *td)
1389 {
1390 	int ret, lim;
1391 	struct seq_softc *scp = i_dev->si_drv1;
1392 
1393 	SEQ_DEBUG(3, printf("seq_poll: unit %d.\n", scp->unit));
1394 	SEQ_DEBUG(1, printf("seq_poll: unit %d.\n", scp->unit));
1395 
1396 	mtx_lock(&scp->seq_lock);
1397 
1398 	ret = 0;
1399 
1400 	/* Look up the apropriate queue and select it. */
1401 	if ((events & (POLLOUT | POLLWRNORM)) != 0) {
1402 	    /* Start playing. */
1403 	    scp->playing = 1;
1404 	    cv_broadcast(&scp->state_cv);
1405 	    cv_broadcast(&scp->out_cv);
1406 
1407 	    lim = scp->out_water;
1408 
1409 	    if (MIDIQ_AVAIL(scp->out_q) < lim)
1410 		/* No enough space, record select. */
1411 		selrecord(td, &scp->out_sel);
1412 	    else
1413 		/* We can write now. */
1414 		ret |= events & (POLLOUT | POLLWRNORM);
1415 	}
1416 
1417 	if ((events & (POLLIN | POLLRDNORM)) != 0) {
1418 	    /* TODO: Start recording. */
1419 
1420 	    /* Find out the boundary. */
1421 	    lim = 1;
1422 	    if (MIDIQ_LEN(scp->in_q) < lim)
1423 		/* No data ready, record select. */
1424 		selrecord(td, &scp->in_sel);
1425 	    else
1426 		/* We can read now. */
1427 		ret |= events & (POLLIN | POLLRDNORM);
1428 	}
1429 
1430 	mtx_unlock(&scp->seq_lock);
1431 
1432 	return (ret);
1433 }
1434 #if 0
1435 static void
1436 sein_qtr(void *p, void /* mididev_info */ *md)
1437 {
1438 	struct seq_softc *scp;
1439 
1440 	scp = (struct seq_softc *)p;
1441 
1442 	mtx_lock(&scp->seq_lock);
1443 
1444 	/* Restart playing if we have the data to output. */
1445 	if (scp->queueout_pending)
1446 		seq_callback(scp, SEQ_CB_START | SEQ_CB_WR);
1447 	/* Check the midi device if we are reading. */
1448 	if ((scp->flags & SEQ_F_READING) != 0)
1449 		seq_midiinput(scp, md);
1450 
1451 	mtx_unlock(&scp->seq_lock);
1452 }
1453 #endif
1454 /*
1455  * seq_convertold
1456  * Was the old playevent.  Use this to convert and old
1457  * style /dev/sequencer event to a /dev/music event
1458  */
1459 static int
1460 seq_convertold(u_char *event, u_char *out)
1461 {
1462 	int used;
1463 	u_char dev, chn, note, vel;
1464 
1465 	out[0] = out[1] = out[2] = out[3] = out[4]  = out[5]  = out[6]  = out[7]   = 0;
1466 
1467 	dev = 0;
1468 	chn = event[1];
1469 	note = event[2];
1470 	vel = event[3];
1471 
1472 	used = 0;
1473 
1474 restart:
1475 	/*
1476 	 * TODO: Debug statement
1477 	 */
1478 	switch(event[0]) {
1479 	    case EV_TIMING:
1480 	    case EV_CHN_VOICE:
1481 	    case EV_CHN_COMMON:
1482 case EV_SYSEX:
1483 case EV_SEQ_LOCAL:
1484 		out[0] = event[0];
1485 		out[1] = event[1];
1486 		out[2] = event[2];
1487 		out[3] = event[3];
1488 		out[4] = event[4];
1489 		out[5] = event[5];
1490 		out[6] = event[6];
1491 		out[7] = event[7];
1492 		used += 8;
1493 		break;
1494 	    case SEQ_NOTEOFF:
1495 		out[0] = EV_CHN_VOICE;
1496 		out[1] = dev;
1497 		out[2] = MIDI_NOTEOFF;
1498 		out[3] = chn;
1499 		out[4] = note;
1500 		out[5] = 255;
1501 	used += 4;
1502 		break;
1503 
1504 	    case SEQ_NOTEON:
1505 		out[0] = EV_CHN_VOICE;
1506 		out[1] = dev;
1507 		out[2] = MIDI_NOTEON;
1508 		out[3] = chn;
1509 		out[4] = note;
1510 		out[5] = vel;
1511 	used += 4;
1512 		break;
1513 
1514 		/*
1515 		 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4]
1516 		 */
1517 
1518 	    case SEQ_PGMCHANGE:
1519 		out[0] = EV_CHN_COMMON;
1520 		out[1] = dev;
1521 		out[2] = MIDI_PGM_CHANGE;
1522 		out[3] = chn;
1523 		out[4] = note;
1524 		out[5] = vel;
1525 	used += 4;
1526 		break;
1527 /*
1528 		out[0] = EV_TIMING;
1529 		out[1] = dev;
1530 		out[2] = MIDI_PGM_CHANGE;
1531 		out[3] = chn;
1532 		out[4] = note;
1533 		out[5] = vel;
1534 		SEQ_DEBUG(4,printf("seq_playevent: synctimer\n"));
1535 		break;
1536 */
1537 
1538 	    case SEQ_MIDIPUTC:
1539 		SEQ_DEBUG(4,printf("seq_playevent: put data 0x%02x, unit %d.\n",
1540 			    event[1], event[2]));
1541 		/*
1542 		 * Pass through to the midi device.
1543 		 * device = event[2]
1544 		 * data = event[1]
1545 		 */
1546 		out[0] = SEQ_MIDIPUTC;
1547 		out[1] = dev;
1548 		out[2] = chn;
1549 	used += 4;
1550 		break;
1551 #ifdef notyet
1552 	    case SEQ_ECHO:
1553 		/*
1554 		 * This isn't handled here yet because I don't know if I can
1555 		 * just use four bytes events.  There might be consequences
1556 		 * in the _read routing
1557 		 */
1558 		if (seq_copytoinput(scp, event, 4) == EAGAIN) {
1559 		    ret = QUEUEFULL;
1560 		    break;
1561 		}
1562 		ret = MORE;
1563 		break;
1564 #endif
1565 	    case SEQ_EXTENDED:
1566 		switch (event[1]) {
1567 		    case SEQ_NOTEOFF:
1568 		    case SEQ_NOTEON:
1569 		    case SEQ_PGMCHANGE:
1570 			event++;
1571 			used = 4;
1572 			goto restart;
1573 			break;
1574 		    case SEQ_AFTERTOUCH:
1575 			/*
1576 			 * SYNTH_AFTERTOUCH(md, event[3], event[4])
1577 			 */
1578 		    case SEQ_BALANCE:
1579 			/*
1580 			 * SYNTH_PANNING(md, event[3], (char)event[4])
1581 			 */
1582 		    case SEQ_CONTROLLER:
1583 			/*
1584 			 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5])
1585 			 */
1586 		    case SEQ_VOLMODE:
1587 			/*
1588 			 * SYNTH_VOLUMEMETHOD(md, event[3])
1589 			 */
1590 		    default:
1591 			SEQ_DEBUG(2,printf("seq_convertold: SEQ_EXTENDED type %d"
1592 				"not handled\n", event[1]));
1593 			break;
1594 		}
1595 		break;
1596 	    case SEQ_WAIT:
1597 		out[0] = EV_TIMING;
1598 		out[1] = TMR_WAIT_REL;
1599 		out[4] = event[2];
1600 		out[5] = event[3];
1601 		out[6] = event[4];
1602 
1603 		SEQ_DEBUG(5,printf("SEQ_WAIT %d", event[2] + (event[3] << 8) + (event[4] << 24)));
1604 
1605 		used+= 4;
1606 		break;
1607 
1608 	    case SEQ_ECHO:
1609 	    case SEQ_SYNCTIMER:
1610 	    case SEQ_PRIVATE:
1611 	    default:
1612 		SEQ_DEBUG(2,printf("seq_convertold: event type %d not handled %d %d %d\n", event[0], event[1], event[2], event[3]));
1613 		break;
1614 	}
1615 	return used;
1616 }
1617 
1618 /*
1619  * Writting to the sequencer buffer never blocks and drops
1620  * input which cannot be queued
1621  */
1622 void
1623 seq_copytoinput(struct seq_softc *scp, u_char *event, int len)
1624 {
1625 
1626 	mtx_assert(&scp->seq_lock, MA_OWNED);
1627 
1628 	if (MIDIQ_AVAIL(scp->in_q) < len) {
1629 	    /*
1630 	     * ENOROOM?  EINPUTDROPPED? ETOUGHLUCK?
1631 	     */
1632 	  SEQ_DEBUG(2,printf("seq_copytoinput: queue full\n"));
1633 	} else {
1634 	    MIDIQ_ENQ(scp->in_q, event, len);
1635 	    selwakeup(&scp->in_sel);
1636 	    cv_broadcast(&scp->in_cv);
1637 	}
1638 
1639 }
1640 
1641 static int
1642 seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event)
1643 {
1644 	int ret, voice;
1645 	u_char cmd, chn, note, parm;
1646 
1647 	ret = 0;
1648 	cmd = event[2];
1649 	chn = event[3];
1650 	note = event[4];
1651 	parm = event[5];
1652 
1653 	mtx_assert(&scp->seq_lock, MA_OWNED);
1654 
1655 	SEQ_DEBUG(5,printf("seq_chnvoice: unit %d, dev %d, cmd %s,"
1656 		   " chn %d, note %d, parm %d.\n", scp->unit, event[1],
1657 		   midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm));
1658 
1659 	voice = SYNTH_ALLOC(md, chn, note);
1660 
1661 	mtx_unlock(&scp->seq_lock);
1662 
1663 	switch (cmd) {
1664 	    case MIDI_NOTEON:
1665 		if (note < 128 || note == 255) {
1666 #if 0
1667 		    if (scp->music && chn == 9) {
1668 			/*
1669 			 * This channel is a percussion. The note number is the
1670 			 * patch number.
1671 			 */
1672 			/*
1673 			mtx_unlock(&scp->seq_lock);
1674 			if (SYNTH_SETINSTR(md, voice, 128 + note) == EAGAIN) {
1675 			    mtx_lock(&scp->seq_lock);
1676 			    return (QUEUEFULL);
1677 			}
1678 			mtx_lock(&scp->seq_lock);
1679 			*/
1680 			note = 60; /* Middle C. */
1681 		    }
1682 #endif
1683 		    if (scp->music) {
1684 			/*
1685 			mtx_unlock(&scp->seq_lock);
1686 			if (SYNTH_SETUPVOICE(md, voice, chn) == EAGAIN) {
1687 			    mtx_lock(&scp->seq_lock);
1688 			    return (QUEUEFULL);
1689 			}
1690 			mtx_lock(&scp->seq_lock);
1691 			*/
1692 		    }
1693 		    SYNTH_STARTNOTE(md, voice, note, parm);
1694 		}
1695 		break;
1696 	case MIDI_NOTEOFF:
1697 		SYNTH_KILLNOTE(md, voice, note, parm);
1698 		break;
1699 	case MIDI_KEY_PRESSURE:
1700 		SYNTH_AFTERTOUCH(md, voice, parm);
1701 		break;
1702 	default:
1703 		ret = 1;
1704 		SEQ_DEBUG(2,printf("seq_chnvoice event type %d not handled\n", event[1]));
1705 		break;
1706 	}
1707 
1708 	mtx_lock(&scp->seq_lock);
1709 	return ret;
1710 }
1711 
1712 static int
1713 seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event)
1714 {
1715 	int ret;
1716 	u_short w14;
1717 	u_char cmd, chn, p1;
1718 
1719 	ret = 0;
1720 	cmd = event[2];
1721 	chn = event[3];
1722 	p1 = event[4];
1723 	w14 = *(u_short *)&event[6];
1724 
1725 	SEQ_DEBUG(5,printf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d,"
1726 		    " p1 %d, w14 %d.\n", scp->unit, event[1],
1727 		    midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14));
1728 	mtx_unlock(&scp->seq_lock);
1729 	switch (cmd) {
1730 	    case MIDI_PGM_CHANGE:
1731 		    SEQ_DEBUG(4,printf("seq_chncommon pgmchn chn %d pg %d\n",
1732 				chn, p1));
1733 		    SYNTH_SETINSTR(md, chn, p1);
1734 		break;
1735 	    case MIDI_CTL_CHANGE:
1736 		SEQ_DEBUG(4,printf("seq_chncommon ctlch chn %d pg %d %d\n",
1737 			    chn, p1, w14));
1738 		SYNTH_CONTROLLER(md, chn, p1, w14);
1739 		break;
1740 	    case MIDI_PITCH_BEND:
1741 		if (scp->music) {
1742 		    /*
1743 		     * TODO: MIDI_PITCH_BEND
1744 		     */
1745 #if 0
1746 		    mtx_lock(&md->synth.vc_mtx);
1747 		    md->synth.chn_info[chn].bender_value = w14;
1748 		    if (md->midiunit >= 0) {
1749 			/* Handle all of the notes playing on this channel. */
1750 			key = ((int)chn << 8);
1751 			for (i = 0 ; i < md->synth.alloc.max_voice ; i++)
1752 			    if ((md->synth.alloc.map[i] & 0xff00) == key) {
1753 				mtx_unlock(&md->synth.vc_mtx);
1754 				mtx_unlock(&scp->seq_lock);
1755 				if (md->synth.bender(md, i, w14) == EAGAIN) {
1756 				    mtx_lock(&scp->seq_lock);
1757 				    return (QUEUEFULL);
1758 				}
1759 				mtx_lock(&scp->seq_lock);
1760 			    }
1761 		    } else {
1762 			mtx_unlock(&md->synth.vc_mtx);
1763 			mtx_unlock(&scp->seq_lock);
1764 			if (md->synth.bender(md, chn, w14) == EAGAIN) {
1765 			    mtx_lock(&scp->seq_lock);
1766 			    return (QUEUEFULL);
1767 			}
1768 			mtx_lock(&scp->seq_lock);
1769 		    }
1770 #endif
1771 		} else
1772 		    SYNTH_BENDER(md, chn, w14);
1773 		break;
1774 	    default:
1775 		ret = 1;
1776 		SEQ_DEBUG(2,printf("seq_chncommon event type %d not handled.\n", event[1]));
1777 		break;
1778 
1779 	}
1780 	mtx_lock(&scp->seq_lock);
1781 	return ret;
1782 }
1783 
1784 static int
1785 seq_timing(struct seq_softc *scp, u_char *event)
1786 {
1787 	int param;
1788 	int ret;
1789 
1790 	ret = 0;
1791 	param = event[4] + (event[5] << 8) +
1792 	    (event[6] << 16) + (event[7] << 24);
1793 
1794 	SEQ_DEBUG(5,printf("seq_timing: unit %d, cmd %d, param %d.\n",
1795 		    scp->unit, event[1], param));
1796 	switch (event[1]) {
1797 	case TMR_WAIT_REL:
1798 	    timer_wait(scp, param, 0);
1799 		break;
1800 	case TMR_WAIT_ABS:
1801 	    timer_wait(scp, param, 1);
1802 		break;
1803 	case TMR_START:
1804 		timer_start(scp);
1805 		cv_broadcast(&scp->reset_cv);
1806 		break;
1807 	case TMR_STOP:
1808 		timer_stop(scp);
1809 		/*
1810 		 * The following cv_broadcast isn't needed since we only
1811 		 * wait for 0->1 transitions.  It probably won't hurt
1812 		 */
1813 		cv_broadcast(&scp->reset_cv);
1814 		break;
1815 	case TMR_CONTINUE:
1816 		timer_continue(scp);
1817 		cv_broadcast(&scp->reset_cv);
1818 		break;
1819 	case TMR_TEMPO:
1820 		if (param < 8)
1821 		    param = 8;
1822 		if (param > 360)
1823 		    param = 360;
1824 		SEQ_DEBUG(4,printf("Timer set tempo %d\n", param));
1825 		timer_setvals(scp, param, scp->timerbase);
1826 		break;
1827 	case TMR_TIMERBASE:
1828 		if (param < 1)
1829 		    param = 1;
1830 		if (param > 1000)
1831 		    param = 1000;
1832 		SEQ_DEBUG(4,printf("Timer set timerbase %d\n", param));
1833 		timer_setvals(scp, scp->tempo, param);
1834 		break;
1835 	case TMR_ECHO:
1836 		/*
1837 		 * TODO: Consider making 4-byte events for /dev/sequencer
1838 		 * PRO: Maybe needed by legacy apps
1839 		 * CON: soundcard.h has been warning for a while many years
1840 		 * to expect 8 byte events.
1841 		 */
1842 #if 0
1843 		if (scp->music)
1844 			seq_copytoinput(scp, event, 8);
1845 		else {
1846 			param = (param << 8 | SEQ_ECHO);
1847 			seq_copytoinput(scp, (u_char *)&param, 4);
1848 		}
1849 #else
1850 		seq_copytoinput(scp, event, 8);
1851 #endif
1852 		break;
1853 	    default:
1854 		SEQ_DEBUG(2,printf("seq_timing event type %d not handled.\n", event[1]));
1855 		ret = 1;
1856 		break;
1857 	}
1858 	return ret;
1859 }
1860 
1861 static int
1862 seq_local(struct seq_softc *scp, u_char *event)
1863 {
1864 	int ret;
1865 
1866 	ret = 0;
1867 	mtx_assert(&scp->seq_lock, MA_OWNED);
1868 
1869 	SEQ_DEBUG(5,printf("seq_local: unit %d, cmd %d\n", scp->unit, event[1]));
1870 	switch (event[1]) {
1871 	    default:
1872 		SEQ_DEBUG(1, printf("seq_local event type %d not handled\n", event[1]));
1873 		ret = 1;
1874 		break;
1875 	}
1876 	return ret;
1877 }
1878 
1879 static int
1880 seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event)
1881 {
1882 	int i, l;
1883 
1884 	mtx_assert(&scp->seq_lock, MA_OWNED);
1885 	SEQ_DEBUG(5,printf("seq_sysex: unit %d device %d\n", scp->unit, event[1]));
1886 	l = 0;
1887 	for (i = 0 ; i < 6 && event[i + 2] != 0xff ; i++)
1888 		l = i + 1;
1889 	if (l > 0) {
1890 		mtx_unlock(&scp->seq_lock);
1891 		if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) {
1892 			mtx_lock(&scp->seq_lock);
1893 			return 1;
1894 		}
1895 		mtx_lock(&scp->seq_lock);
1896 	}
1897 	return 0;
1898 }
1899 
1900 /*
1901  * Reset no longer closes the raw devices nor seq_sync's
1902  * Callers are IOCTL and seq_close
1903  */
1904 static void
1905 seq_reset(struct seq_softc *scp)
1906 {
1907 	int chn,  i;
1908 	kobj_t m;
1909 
1910 	mtx_assert(&scp->seq_lock, MA_OWNED);
1911 
1912 	SEQ_DEBUG(5,printf("seq_reset: unit %d.\n", scp->unit));
1913 
1914 	/*
1915 	 * Stop reading and writing.
1916 	 */
1917 
1918 	/* scp->recording = 0; */
1919 	scp->playing = 0;
1920 	cv_broadcast(&scp->state_cv);
1921 	cv_broadcast(&scp->out_cv);
1922 	cv_broadcast(&scp->reset_cv);
1923 
1924 	/*
1925 	 * For now, don't reset the timers.
1926 	 */
1927 	MIDIQ_CLEAR(scp->in_q);
1928 	MIDIQ_CLEAR(scp->out_q);
1929 
1930 	for (i = 0; i < scp->midi_number; i++) {
1931 	    m = scp->midis[i];
1932 	    mtx_unlock(&scp->seq_lock);
1933 	    SYNTH_RESET(m);
1934 	    for (chn = 0 ; chn < 16 ; chn++) {
1935 		SYNTH_CONTROLLER(m, chn, 123, 0) ;
1936 		SYNTH_CONTROLLER(m, chn, 121, 0);
1937 		SYNTH_BENDER(m, chn, 1 << 13);
1938 	    }
1939 	    mtx_lock(&scp->seq_lock);
1940 	}
1941 }
1942 
1943 /*
1944  * seq_sync
1945  * *really* flush the output queue
1946  * flush the event queue, then flush the synthsisers.
1947  * Callers are IOCTL and close
1948  */
1949 
1950 #define SEQ_SYNC_TIMEOUT 8
1951 static int
1952 seq_sync(struct seq_softc *scp)
1953 {
1954 	int i, rl, sync[16], done;
1955 
1956 	mtx_assert(&scp->seq_lock, MA_OWNED);
1957 
1958 	SEQ_DEBUG(4,printf("seq_sync: unit %d.\n", scp->unit));
1959 
1960 	/*
1961 	 * Wait until output queue is empty.  Check every so often to see if
1962 	 * the queue is moving along.  If it isn't just abort.
1963 	 */
1964 	while (!MIDIQ_EMPTY(scp->out_q)) {
1965 
1966 	    if (!scp->playing) {
1967 		scp->playing = 1;
1968 		cv_broadcast(&scp->state_cv);
1969 		cv_broadcast(&scp->out_cv);
1970 	    }
1971 
1972 	    rl = MIDIQ_LEN(scp->out_q);
1973 
1974 	    i = cv_timedwait_sig(&scp->out_cv,
1975 		    &scp->seq_lock, SEQ_SYNC_TIMEOUT * hz);
1976 
1977 	    if (i == EINTR || i == ERESTART) {
1978 		if (i == EINTR) {
1979 		    /*
1980 		     * XXX: I don't know why we stop playing
1981 		     */
1982 		    scp->playing = 0;
1983 		    cv_broadcast(&scp->out_cv);
1984 		}
1985 		return i;
1986 	    }
1987 
1988 	    if (i == EWOULDBLOCK && rl == MIDIQ_LEN(scp->out_q) &&
1989 		    scp->waiting == 0) {
1990 		/*
1991 		 * A queue seems to be stuck up. Give up and clear queues.
1992 		 */
1993 		MIDIQ_CLEAR(scp->out_q);
1994 		scp->playing = 0;
1995 		cv_broadcast(&scp->state_cv);
1996 		cv_broadcast(&scp->out_cv);
1997 		cv_broadcast(&scp->reset_cv);
1998 
1999 		/*
2000 		 * TODO: Consider if the raw devices need to be flushed
2001 		 */
2002 
2003 		SEQ_DEBUG(1,printf("seq_sync queue stuck, aborting\n"));
2004 
2005 		return i;
2006 	    }
2007 	}
2008 
2009 	scp->playing = 0;
2010 	/*
2011 	 * Since syncing a midi device might block, unlock scp->seq_lock.
2012 	 */
2013 
2014 	mtx_unlock(&scp->seq_lock);
2015 	for(i = 0 ; i < scp->midi_number; i++)
2016 	    sync[i] = 1;
2017 
2018 	do {
2019 	    done = 1;
2020 	    for (i = 0 ; i < scp->midi_number; i++)
2021 		if (sync[i]) {
2022 		    if (SYNTH_INSYNC(scp->midis[i]) == 0)
2023 			sync[i] = 0;
2024 		    else
2025 			done = 0;
2026 		}
2027 
2028 	   if (!done)
2029 		DELAY(5000);
2030 
2031 	} while (!done);
2032 
2033 	mtx_lock(&scp->seq_lock);
2034 	return 0;
2035 }
2036 
2037 char *
2038 midi_cmdname(int cmd, midi_cmdtab *tab)
2039 {
2040 	while (tab->name != NULL) {
2041 		if (cmd == tab->cmd)
2042 			return (tab->name);
2043 		tab++;
2044 	}
2045 
2046 	return ("unknown");
2047 }
2048