xref: /freebsd/sys/dev/sound/midi/sequencer.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * The sequencer personality manager.
3  *
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  * $FreeBSD$
27  *
28  */
29 
30 /*
31  * This is the newmidi sequencer driver. This driver handles io against
32  * /dev/sequencer, midi input and output event queues and event transmittion
33  * to and from a midi device or synthesizer.
34  */
35 
36 #include <dev/sound/midi/midi.h>
37 #include <dev/sound/midi/sequencer.h>
38 
39 #ifndef DDB
40 #define DDB(x)
41 #endif /* DDB */
42 
43 #define SND_DEV_SEQ	1	/* Sequencer output /dev/sequencer (FM
44 				   synthesizer and MIDI output) */
45 #define SND_DEV_MIDIN	2	/* Raw midi access */
46 #define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
47 
48 #define MIDIDEV_MODE 0x2000
49 
50 /* Length of a sequencer event. */
51 #define EV_SZ 8
52 #define IEV_SZ 8
53 
54 /* Return value from seq_playevent and the helpers. */
55 enum {
56 	MORE,
57 	TIMERARMED,
58 	QUEUEFULL
59 };
60 
61 /* Lookup modes */
62 #define LOOKUP_EXIST	(0)
63 #define LOOKUP_OPEN	(1)
64 #define LOOKUP_CLOSE	(2)
65 
66 /*
67  * These functions goes into seq_op_desc to get called
68  * from sound.c.
69  */
70 
71 static midi_intr_t seq_intr;
72 static seq_callback_t seq_callback;
73 
74 /* These are the entries to the sequencer driver. */
75 static d_open_t seq_open;
76 static d_close_t seq_close;
77 static d_ioctl_t seq_ioctl;
78 static d_read_t seq_read;
79 static d_write_t seq_write;
80 static d_poll_t seq_poll;
81 
82 /*
83  * This is the device descriptor for the midi sequencer.
84  */
85 seqdev_info seq_op_desc = {
86 	"midi sequencer",
87 
88 	0,
89 
90 	seq_open,
91 	seq_close,
92 	seq_read,
93 	seq_write,
94 	seq_ioctl,
95 	seq_poll,
96 
97 	seq_callback,
98 
99 	SEQ_BUFFSIZE, /* Queue Length */
100 
101 	0, /* XXX This is not an *audio* device! */
102 };
103 
104 
105 
106 /* Here is the parameter structure per a device. */
107 struct seq_softc {
108 	seqdev_info *devinfo; /* sequencer device information */
109 
110 	/* Flags (protected by flag_mtx of mididev_info) */
111 	int fflags; /* Access mode */
112 	int queueout_pending; /* Pending for the output queue */
113 
114 	/* Timer counters */
115 	u_long seq_time; /* The beggining time of this sequence */
116 	u_long prev_event_time; /* The time of the previous event output */
117 	u_long prev_input_time; /* The time of the previous event input */
118 	u_long prev_wakeup_time; /* The time of the previous wakeup */
119 	struct callout timeout_ch; /* Timer callout handler */
120 	long timer_current; /* Current timer value */
121 	int timer_running; /* State of timer */
122 	int pending_timer; /* Timer change operation */
123 	int pre_event_timeout; /* Time to wait event input */
124 
125 	/* Device list */
126 	TAILQ_HEAD(,_mididev_info) midi_open; /* Midi devices opened by this sequencer. */
127 
128 	/*
129 	 * XXX not sure to which category these belong.
130 	 * (and some might be no-op)
131 	 */
132 	int output_threshould; /* Sequence output threshould */
133 	snd_sync_parm sync_parm; /* AIOSYNC parameter set */
134 	struct proc *sync_proc; /* AIOSYNCing process */
135 };
136 
137 typedef struct seq_softc *sc_p;
138 
139 static d_open_t seqopen;
140 static d_close_t seqclose;
141 static d_ioctl_t seqioctl;
142 static d_read_t seqread;
143 static d_write_t seqwrite;
144 static d_poll_t seqpoll;
145 
146 #define CDEV_MAJOR SEQ_CDEV_MAJOR
147 static struct cdevsw seq_cdevsw = {
148 	/* open */	seqopen,
149 	/* close */	seqclose,
150 	/* read */	seqread,
151 	/* write */	seqwrite,
152 	/* ioctl */	seqioctl,
153 	/* poll */	seqpoll,
154 	/* mmap */	nommap,
155 	/* strategy */	nostrategy,
156 	/* name */	"midi", /* XXX */
157 	/* maj */	CDEV_MAJOR,
158 	/* dump */	nodump,
159 	/* psize */	nopsize,
160 	/* flags */	0,
161 };
162 
163 static TAILQ_HEAD(,_seqdev_info) seq_info;
164 /* Mutex to protect seq_info and nseq. */
165 static struct mtx seqinfo_mtx;
166 static u_long nseq;	/* total number of sequencers */
167 static dev_t seq_alias = NODEV;
168 
169 /* The followings are the local function. */
170 static int seq_init(void);
171 static int seq_initunit(int unit);
172 static int seq_queue(sc_p scp, u_char *note);
173 static void seq_startplay(sc_p scp);
174 static int seq_playevent(sc_p scp, u_char *event);
175 static u_long seq_gettime(void);
176 static int seq_requesttimer(sc_p scp, int delay);
177 static void seq_stoptimer(sc_p scp);
178 static void seq_midiinput(sc_p scp, mididev_info *md);
179 static int seq_copytoinput(sc_p scp, u_char *event, int len);
180 static int seq_extended(sc_p scp, u_char *event);
181 static int seq_chnvoice(sc_p scp, u_char *event);
182 static int seq_findvoice(mididev_info *md, int chn, int note) __unused;
183 static int seq_allocvoice(sc_p scp, mididev_info *md, int chn, int note) __unused;
184 static int seq_chncommon(sc_p scp, u_char *event);
185 static int seq_timing(sc_p scp, u_char *event);
186 static int seq_local(sc_p scp, u_char *event);
187 static int seq_sysex(sc_p scp, u_char *event);
188 static void seq_timer(void *arg);
189 static int seq_reset(sc_p scp);
190 static int seq_openmidi(sc_p scp, mididev_info *md, int flags, int mode, struct proc *p);
191 static int seq_closemidi(sc_p scp, mididev_info *md, int flags, int mode, struct proc *p);
192 static void seq_panic(sc_p scp);
193 static int seq_sync(sc_p scp);
194 
195 static seqdev_info *get_seqdev_info(dev_t i_dev, int *unit);
196 static seqdev_info *get_seqdev_info_unit(int unit);
197 static seqdev_info *create_seqdev_info_unit(int unit, seqdev_info *seq);
198 static int lookup_mididev(sc_p scp, int unit, int mode, mididev_info **mdp);
199 static void seq_clone(void *arg, char *name, int namelen, dev_t *dev);
200 
201 /*
202  * Here are the main functions to interact to the user process.
203  * These are called from snd* functions in sys/i386/isa/snd/sound.c.
204  */
205 
206 static int
207 seq_init(void)
208 {
209 	DEB(printf("seq: initing.\n"));
210 
211 	mtx_init(&seqinfo_mtx, "seqinf", MTX_DEF);
212 	TAILQ_INIT(&seq_info);
213 
214 	seq_initunit(0);
215 	EVENTHANDLER_REGISTER(dev_clone, seq_clone, 0, 1000);
216 
217 	DEB(printf("seq: inited.\n"));
218 
219 	return (0);
220 }
221 
222 static int
223 seq_initunit(int unit)
224 {
225 	sc_p scp;
226 	seqdev_info *devinfo;
227 	dev_t seqdev;
228 
229 	/* Allocate the softc. */
230 	scp = malloc(sizeof(*scp), M_DEVBUF, M_WAITOK | M_ZERO);
231 	if (scp == (sc_p)NULL) {
232 		printf("seq%d: softc allocation failed.\n", unit);
233 		return (1);
234 	}
235 
236 	/* Fill the softc and the seq_info for this unit. */
237 	scp->seq_time = seq_gettime();
238 	scp->prev_event_time = 0;
239 	scp->prev_input_time = 0;
240 	scp->prev_wakeup_time = scp->seq_time;
241 #if defined(MIDI_OUTOFGIANT)
242 	callout_init(&scp->timeout_ch, 1);
243 #else
244 	callout_init(&scp->timeout_ch, 0);
245 #endif /* MIDI_OUTOFGIANT */
246 	scp->timer_current = 0;
247 	scp->timer_running = 0;
248 	scp->queueout_pending = 0;
249 	TAILQ_INIT(&scp->midi_open);
250 
251 	scp->devinfo = devinfo = create_seqdev_info_unit(unit, &seq_op_desc);
252 	devinfo->midi_dbuf_in.unit_size = devinfo->midi_dbuf_out.unit_size = EV_SZ;
253 	devinfo->softc = scp;
254 	devinfo->flags = 0;
255 	mtx_unlock(&devinfo->flagqueue_mtx);
256 
257 	seqdev = make_dev(&seq_cdevsw, MIDIMKMINOR(unit, SND_DEV_SEQ),
258 			  UID_ROOT, GID_WHEEL, 0666, "sequencer%d", unit);
259 	mtx_lock(&seqinfo_mtx);
260 	if (seq_alias != NODEV) {
261 		destroy_dev(seq_alias);
262 		seq_alias = NODEV;
263 	}
264 	seq_alias = make_dev_alias(seqdev, "sequencer");
265 	mtx_unlock(&seqinfo_mtx);
266 
267 	return (0);
268 }
269 
270 int
271 seq_open(dev_t i_dev, int flags, int mode, struct proc *p)
272 {
273 	int unit;
274 	sc_p scp;
275 	seqdev_info *sd;
276 
277 	unit = MIDIUNIT(i_dev);
278 
279 	DEB(printf("seq%d: opening.\n", unit));
280 
281 	if (unit >= NSEQ_MAX) {
282 		DEB(printf("seq_open: unit %d does not exist.\n", unit));
283 		return (ENXIO);
284 	}
285 
286 	sd = get_seqdev_info(i_dev, &unit);
287 	if (sd == NULL) {
288 		DEB(printf("seq_open: unit %d is not configured.\n", unit));
289 		return (ENXIO);
290 	}
291 	scp = sd->softc;
292 
293 	/* Mark this device busy. */
294 	mtx_lock(&sd->flagqueue_mtx);
295 	if ((sd->flags & SEQ_F_BUSY) != 0) {
296 		mtx_unlock(&sd->flagqueue_mtx);
297 		DEB(printf("seq_open: unit %d is busy.\n", unit));
298 		return (EBUSY);
299 	}
300 	sd->flags |= SEQ_F_BUSY;
301 	sd->flags &= ~(SEQ_F_READING | SEQ_F_WRITING);
302 	scp->fflags = flags;
303 
304 	/* Init the queue. */
305 	midibuf_clear(&sd->midi_dbuf_in);
306 	midibuf_clear(&sd->midi_dbuf_out);
307 
308 	/* Init timestamp. */
309 	scp->seq_time = seq_gettime();
310 	scp->prev_event_time = 0;
311 	scp->prev_input_time = 0;
312 	scp->prev_wakeup_time = scp->seq_time;
313 
314 	mtx_unlock(&sd->flagqueue_mtx);
315 
316 	DEB(printf("seq%d: opened.\n", unit));
317 
318 	return (0);
319 }
320 
321 int
322 seq_close(dev_t i_dev, int flags, int mode, struct proc *p)
323 {
324 	int unit;
325 	sc_p scp;
326 	seqdev_info *sd;
327 	mididev_info *md;
328 
329 	unit = MIDIUNIT(i_dev);
330 
331 	DEB(printf("seq%d: closing.\n", unit));
332 
333 	if (unit >= NSEQ_MAX) {
334 		DEB(printf("seq_close: unit %d does not exist.\n", unit));
335 		return (ENXIO);
336 	}
337 
338 	sd = get_seqdev_info(i_dev, &unit);
339 	if (sd == NULL) {
340 		DEB(printf("seq_close: unit %d is not configured.\n", unit));
341 		return (ENXIO);
342 	}
343 	scp = sd->softc;
344 
345 	mtx_lock(&sd->flagqueue_mtx);
346 
347 	if (!(sd->flags & MIDI_F_NBIO))
348 		seq_sync(scp);
349 
350 	/* Stop the timer. */
351 	seq_stoptimer(scp);
352 
353 	/* Reset the sequencer. */
354 	seq_reset(scp);
355 	seq_sync(scp);
356 
357 	/* Clean up the midi device. */
358 	TAILQ_FOREACH(md, &scp->midi_open, md_linkseq)
359 		lookup_mididev(scp, md->unit, LOOKUP_CLOSE, NULL);
360 	mtx_unlock(&sd->flagqueue_mtx);
361 
362 	/* Stop playing and unmark this device busy. */
363 	mtx_lock(&sd->flagqueue_mtx);
364 	sd->flags &= ~(SEQ_F_BUSY | SEQ_F_READING | SEQ_F_WRITING | SEQ_F_INSYNC);
365 	mtx_unlock(&sd->flagqueue_mtx);
366 
367 	DEB(printf("seq%d: closed.\n", unit));
368 
369 	return (0);
370 }
371 
372 int
373 seq_read(dev_t i_dev, struct uio *buf, int flag)
374 {
375 	int unit, ret, len;
376 	sc_p scp;
377 	seqdev_info *sd;
378 
379 	unit = MIDIUNIT(i_dev);
380 
381 	/*DEB(printf("seq%d: reading.\n", unit));*/
382 
383 	if (unit >= NSEQ_MAX) {
384 		DEB(printf("seq_read: unit %d does not exist.\n", unit));
385 		return (ENXIO);
386 	}
387 
388 	sd = get_seqdev_info(i_dev, &unit);
389 	if (sd == NULL) {
390 		DEB(printf("seq_read: unit %d is not configured.\n", unit));
391 		return (ENXIO);
392 	}
393 	scp = sd->softc;
394 	if ((scp->fflags & FREAD) == 0) {
395 		DEB(printf("seq_read: unit %d is not for reading.\n", unit));
396 		return (EIO);
397 	}
398 
399 	mtx_lock(&sd->flagqueue_mtx);
400 
401 	/* Begin recording. */
402 	if ((sd->flags & SEQ_F_READING) == 0)
403 		sd->callback(sd, SEQ_CB_START | SEQ_CB_RD);
404 
405 	len = 0;
406 
407 	/* Have we got the data to read? */
408 	if ((sd->flags & SEQ_F_NBIO) != 0 && sd->midi_dbuf_in.rl == 0)
409 		ret = EAGAIN;
410 	else {
411 		len = buf->uio_resid;
412 		ret = midibuf_uioread(&sd->midi_dbuf_in, buf, len, &sd->flagqueue_mtx);
413 		if (ret < 0)
414 			ret = -ret;
415 		else
416 			ret = 0;
417 	}
418 	mtx_unlock(&sd->flagqueue_mtx);
419 
420 	return (ret);
421 }
422 
423 int
424 seq_write(dev_t i_dev, struct uio *buf, int flag)
425 {
426 	u_char event[EV_SZ], ev_code;
427 	int unit, count, countorg, midiunit, ev_size, p, ret;
428 	sc_p scp;
429 	seqdev_info *sd;
430 	mididev_info *md;
431 
432 	unit = MIDIUNIT(i_dev);
433 
434 	/*DEB(printf("seq%d: writing.\n", unit));*/
435 
436 	if (unit >= NSEQ_MAX) {
437 		DEB(printf("seq_write: unit %d does not exist.\n", unit));
438 		return (ENXIO);
439 	}
440 
441 	sd = get_seqdev_info(i_dev, &unit);
442 	if (sd == NULL) {
443 		DEB(printf("seq_write: unit %d is not configured.\n", unit));
444 		return (ENXIO);
445 	}
446 	scp = sd->softc;
447 	if ((scp->fflags & FWRITE) == 0) {
448 		DEB(printf("seq_write: unit %d is not for writing.\n", unit));
449 		return (EIO);
450 	}
451 
452 	p = 0;
453 	countorg = buf->uio_resid;
454 	count = countorg;
455 
456 	/* Pick up an event. */
457 	while (count >= 4) {
458 		if (uiomove((caddr_t)event, 4, buf))
459 			printf("seq_write: user memory mangled?\n");
460 		ev_code = event[0];
461 
462 		/* Have a look at the event code. */
463 		if (ev_code == SEQ_FULLSIZE) {
464 
465 			/* A long event, these are the patches/samples for a synthesizer. */
466 			midiunit = *(u_short *)&event[2];
467 			mtx_lock(&sd->flagqueue_mtx);
468 			ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
469 			mtx_unlock(&sd->flagqueue_mtx);
470 			if (ret != 0)
471 				return (ret);
472 
473 			DEB(printf("seq_write: loading a patch to the unit %d.\n", midiunit));
474 
475 			ret = md->synth.loadpatch(md, *(short *)&event[0], buf, p + 4, count, 0);
476 			return (ret);
477 		}
478 
479 		if (ev_code >= 128) {
480 
481 			/* Some sort of an extended event. The size is eight bytes. */
482 #if notyet
483 			if (scp->seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED) {
484 				printf("seq%d: invalid level two event %x.\n", unit, ev_code);
485 				return (EINVAL);
486 			}
487 #endif /* notyet */
488 			ev_size = 8;
489 
490 			if (count < ev_size) {
491 				/* No more data. Start playing now. */
492 				mtx_lock(&sd->flagqueue_mtx);
493 				if ((sd->flags & SEQ_F_WRITING) == 0)
494 					sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
495 				mtx_unlock(&sd->flagqueue_mtx);
496 
497 				return (0);
498 			}
499 			if (uiomove((caddr_t)&event[4], 4, buf))
500 				printf("seq_write: user memory mangled?\n");
501 		} else {
502 
503 			/* Not an extended event. The size is four bytes. */
504 #if notyet
505 			if (scp->seq_mode == SEQ_2) {
506 				printf("seq%d: four byte event in level two mode.\n", unit);
507 				return (EINVAL);
508 			}
509 #endif /* notyet */
510 			ev_size = 4;
511 		}
512 		if (ev_code == SEQ_MIDIPUTC) {
513 			/* An event passed to the midi device itself. */
514 			midiunit = event[2];
515 			mtx_lock(&sd->flagqueue_mtx);
516 			ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
517 			mtx_unlock(&sd->flagqueue_mtx);
518 			if (ret != 0)
519 				return (ret);
520 		}
521 
522 		/*DEB(printf("seq_write: queueing event %d.\n", event[0]));*/
523 		/* Now we queue the event. */
524 		mtx_lock(&sd->flagqueue_mtx);
525 		switch (seq_queue(scp, event)) {
526 		case EAGAIN:
527 			/* The queue is full. Start playing now. */
528 			if ((sd->flags & SEQ_F_WRITING) == 0)
529 				sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
530 			mtx_unlock(&sd->flagqueue_mtx);
531 			return (0);
532 		case EINTR:
533 			mtx_unlock(&sd->flagqueue_mtx);
534 			return (EINTR);
535 		case ERESTART:
536 			mtx_unlock(&sd->flagqueue_mtx);
537 			return (ERESTART);
538 		}
539 		mtx_unlock(&sd->flagqueue_mtx);
540 		p += ev_size;
541 		count -= ev_size;
542 	}
543 
544 	/* We have written every single data. Start playing now. */
545 	mtx_lock(&sd->flagqueue_mtx);
546 	if ((sd->flags & SEQ_F_WRITING) == 0)
547 		sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
548 	mtx_unlock(&sd->flagqueue_mtx);
549 
550 	return (0);
551 }
552 
553 int
554 seq_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc *p)
555 {
556 	int unit, midiunit, ret, tmp, arg2;
557 	sc_p scp;
558 	seqdev_info *sd;
559 	mididev_info *md;
560 	struct synth_info *synthinfo;
561 	struct midi_info *midiinfo;
562 	struct patmgr_info *patinfo;
563 	snd_sync_parm *syncparm;
564 	struct seq_event_rec *event;
565 	struct snd_size *sndsize;
566 
567 	unit = MIDIUNIT(i_dev);
568 
569 	DEB(printf("seq%d: ioctlling, cmd 0x%x.\n", unit, (int)cmd));
570 
571 	if (unit >= NSEQ_MAX) {
572 		DEB(printf("seq_ioctl: unit %d does not exist.\n", unit));
573 		return (ENXIO);
574 	}
575 	sd = get_seqdev_info(i_dev, &unit);
576 	if (sd == NULL) {
577 		DEB(printf("seq_ioctl: unit %d is not configured.\n", unit));
578 		return (ENXIO);
579 	}
580 	scp = sd->softc;
581 
582 	ret = 0;
583 
584 	switch (cmd) {
585 
586 		/*
587 		 * we start with the new ioctl interface.
588 		 */
589 	case AIONWRITE:	/* how many bytes can be written ? */
590 		*(int *)arg = sd->midi_dbuf_out.fl;
591 		break;
592 
593 	case AIOSSIZE:     /* set the current blocksize */
594 		sndsize = (struct snd_size *)arg;
595 		mtx_lock(&sd->flagqueue_mtx);
596 		if (sndsize->play_size <= sd->midi_dbuf_out.unit_size && sndsize->rec_size <= sd->midi_dbuf_in.unit_size) {
597 			sd->midi_dbuf_out.blocksize = sd->midi_dbuf_out.unit_size;
598 			sd->midi_dbuf_in.blocksize = sd->midi_dbuf_in.unit_size;
599 			sndsize->play_size = sd->midi_dbuf_out.blocksize;
600 			sndsize->rec_size = sd->midi_dbuf_in.blocksize;
601 			sd->flags &= ~MIDI_F_HAS_SIZE;
602 			mtx_unlock(&sd->flagqueue_mtx);
603 		}
604 		else {
605 			if (sndsize->play_size > sd->midi_dbuf_out.bufsize / 4)
606 				sndsize->play_size = sd->midi_dbuf_out.bufsize / 4;
607 			if (sndsize->rec_size > sd->midi_dbuf_in.bufsize / 4)
608 				sndsize->rec_size = sd->midi_dbuf_in.bufsize / 4;
609 			/* Round up the size to the multiple of EV_SZ. */
610 			sd->midi_dbuf_out.blocksize =
611 			    ((sndsize->play_size + sd->midi_dbuf_out.unit_size - 1)
612 			     / sd->midi_dbuf_out.unit_size) * sd->midi_dbuf_out.unit_size;
613 			sd->midi_dbuf_in.blocksize =
614 			    ((sndsize->rec_size + sd->midi_dbuf_in.unit_size - 1)
615 			     / sd->midi_dbuf_in.unit_size) * sd->midi_dbuf_in.unit_size;
616 			sndsize->play_size = sd->midi_dbuf_out.blocksize;
617 			sndsize->rec_size = sd->midi_dbuf_in.blocksize;
618 			sd->flags |= MIDI_F_HAS_SIZE;
619 			mtx_unlock(&sd->flagqueue_mtx);
620 		}
621 
622 		ret = 0;
623 		break;
624 
625 	case AIOGSIZE:	/* get the current blocksize */
626 		sndsize = (struct snd_size *)arg;
627 		mtx_lock(&sd->flagqueue_mtx);
628 		sndsize->play_size = sd->midi_dbuf_out.blocksize;
629 		sndsize->rec_size = sd->midi_dbuf_in.blocksize;
630 		mtx_unlock(&sd->flagqueue_mtx);
631 
632 		ret = 0;
633 		break;
634 
635 	case AIOSTOP:
636 		if (*(int *)arg == AIOSYNC_PLAY) {
637 
638 			/* Stop writing. */
639 			mtx_lock(&sd->flagqueue_mtx);
640 			sd->callback(sd, SEQ_CB_ABORT | SEQ_CB_WR);
641 			mtx_unlock(&sd->flagqueue_mtx);
642 
643 			/* Pass the ioctl to the midi devices. */
644 			TAILQ_FOREACH(md, &scp->midi_open, md_linkseq) {
645 				if ((md->flags & MIDI_F_WRITING) != 0) {
646 					arg2 = *(int *)arg;
647 					midi_ioctl(MIDIMKDEV(major(i_dev), md->unit, SND_DEV_MIDIN), cmd, (caddr_t)&arg2, mode, p);
648 				}
649 			}
650 
651 			*(int *)arg = sd->midi_dbuf_out.rl;
652 		}
653 		else if (*(int *)arg == AIOSYNC_CAPTURE) {
654 
655 			/* Stop reading. */
656 			mtx_lock(&sd->flagqueue_mtx);
657 			sd->callback(sd, SEQ_CB_ABORT | SEQ_CB_RD);
658 			mtx_unlock(&sd->flagqueue_mtx);
659 
660 			/* Pass the ioctl to the midi devices. */
661 			TAILQ_FOREACH(md, &scp->midi_open, md_linkseq) {
662 				if ((md->flags & MIDI_F_WRITING) != 0) {
663 					arg2 = *(int *)arg;
664 					midi_ioctl(MIDIMKDEV(major(i_dev), md->unit, SND_DEV_MIDIN), cmd, (caddr_t)&arg2, mode, p);
665 				}
666 			}
667 
668 			*(int *)arg = sd->midi_dbuf_in.rl;
669 		}
670 
671 		ret = 0;
672 		break;
673 
674 	case AIOSYNC:
675 		syncparm = (snd_sync_parm *)arg;
676 		scp->sync_parm = *syncparm;
677 
678 		/* XXX Should select(2) against us watch the blocksize, or sync_parm? */
679 
680 		ret = 0;
681 		break;
682 
683 	case SNDCTL_TMR_TIMEBASE:
684 	case SNDCTL_TMR_TEMPO:
685 	case SNDCTL_TMR_START:
686 	case SNDCTL_TMR_STOP:
687 	case SNDCTL_TMR_CONTINUE:
688 	case SNDCTL_TMR_METRONOME:
689 	case SNDCTL_TMR_SOURCE:
690 #if notyet
691 		if (scp->seq_mode != SEQ_2) {
692 			ret = EINVAL;
693 			break;
694 		}
695 		ret = tmr->ioctl(tmr_no, cmd, arg);
696 #endif /* notyet */
697 		break;
698 	case SNDCTL_TMR_SELECT:
699 #if notyet
700 		if (scp->seq_mode != SEQ_2) {
701 			ret = EINVAL;
702 			break;
703 		}
704 #endif /* notyet */
705 		scp->pending_timer = *(int *)arg;
706 		if (scp->pending_timer < 0 || scp->pending_timer >= /*NTIMER*/1) {
707 			scp->pending_timer = -1;
708 			ret = EINVAL;
709 			break;
710 		}
711 		*(int *)arg = scp->pending_timer;
712 		ret = 0;
713 		break;
714 	case SNDCTL_SEQ_PANIC:
715 		mtx_lock(&scp->devinfo->flagqueue_mtx);
716 		seq_panic(scp);
717 		mtx_unlock(&scp->devinfo->flagqueue_mtx);
718 		ret = 0;
719 		break;
720 	case SNDCTL_SEQ_SYNC:
721 		if (mode == O_RDONLY) {
722 			ret = 0;
723 			break;
724 		}
725 		mtx_lock(&scp->devinfo->flagqueue_mtx);
726 		ret = seq_sync(scp);
727 		mtx_unlock(&scp->devinfo->flagqueue_mtx);
728 		break;
729 	case SNDCTL_SEQ_RESET:
730 		mtx_lock(&scp->devinfo->flagqueue_mtx);
731 		seq_reset(scp);
732 		mtx_unlock(&scp->devinfo->flagqueue_mtx);
733 		ret = 0;
734 		break;
735 	case SNDCTL_SEQ_TESTMIDI:
736 		midiunit = *(int *)arg;
737 		mtx_lock(&sd->flagqueue_mtx);
738 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
739 		mtx_unlock(&sd->flagqueue_mtx);
740 		break;
741 	case SNDCTL_SEQ_GETINCOUNT:
742 		if (mode == O_WRONLY)
743 			*(int *)arg = 0;
744 		else
745 			*(int *)arg = sd->midi_dbuf_in.rl;
746 		ret = 0;
747 		break;
748 	case SNDCTL_SEQ_GETOUTCOUNT:
749 		if (mode == O_RDONLY)
750 			*(int *)arg = 0;
751 		else
752 			*(int *)arg = sd->midi_dbuf_out.fl;
753 		ret = 0;
754 		break;
755 	case SNDCTL_SEQ_CTRLRATE:
756 #if notyet
757 		if (scp->seq_mode != SEQ_2) {
758 			ret = tmr->ioctl(tmr_no, cmd, arg);
759 			break;
760 		}
761 #endif /* notyet */
762 		if (*(int *)arg != 0) {
763 			ret = EINVAL;
764 			break;
765 		}
766 		*(int *)arg = hz;
767 		ret = 0;
768 		break;
769 	case SNDCTL_SEQ_RESETSAMPLES:
770 		midiunit = *(int *)arg;
771 		mtx_lock(&sd->flagqueue_mtx);
772 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
773 		mtx_unlock(&sd->flagqueue_mtx);
774 		if (ret != 0)
775 			break;
776 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), midiunit, SND_DEV_MIDIN), cmd, arg, mode, p);
777 		break;
778 	case SNDCTL_SEQ_NRSYNTHS:
779 		*(int *)arg = mididev_info_number();
780 		ret = 0;
781 		break;
782 	case SNDCTL_SEQ_NRMIDIS:
783 		*(int *)arg = mididev_info_number();
784 		ret = 0;
785 		break;
786 	case SNDCTL_SYNTH_MEMAVL:
787 		midiunit = *(int *)arg;
788 		mtx_lock(&sd->flagqueue_mtx);
789 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
790 		mtx_unlock(&sd->flagqueue_mtx);
791 		if (ret != 0)
792 			break;
793 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), midiunit, SND_DEV_MIDIN), cmd, arg, mode, p);
794 		break;
795 	case SNDCTL_FM_4OP_ENABLE:
796 		midiunit = *(int *)arg;
797 		mtx_lock(&sd->flagqueue_mtx);
798 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
799 		mtx_unlock(&sd->flagqueue_mtx);
800 		if (ret != 0)
801 			break;
802 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), midiunit, SND_DEV_MIDIN), cmd, arg, mode, p);
803 		break;
804 	case SNDCTL_SYNTH_INFO:
805 		synthinfo = (struct synth_info *)arg;
806 		midiunit = synthinfo->device;
807 		mtx_lock(&sd->flagqueue_mtx);
808 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
809 		mtx_unlock(&sd->flagqueue_mtx);
810 		if (ret != 0)
811 			break;
812 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), midiunit, SND_DEV_MIDIN), cmd, arg, mode, p);
813 		break;
814 	case SNDCTL_SEQ_OUTOFBAND:
815 		event = (struct seq_event_rec *)arg;
816 		mtx_lock(&sd->flagqueue_mtx);
817 		ret = seq_playevent(scp, event->arr);
818 		mtx_unlock(&sd->flagqueue_mtx);
819 		break;
820 	case SNDCTL_MIDI_INFO:
821 		midiinfo = (struct midi_info *)arg;
822 		midiunit = midiinfo->device;
823 		mtx_lock(&sd->flagqueue_mtx);
824 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
825 		mtx_unlock(&sd->flagqueue_mtx);
826 		if (ret != 0)
827 			break;
828 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), midiunit, SND_DEV_MIDIN), cmd, arg, mode, p);
829 		break;
830 	case SNDCTL_PMGR_IFACE:
831 		patinfo = (struct patmgr_info *)arg;
832 		midiunit = patinfo->device;
833 		mtx_lock(&sd->flagqueue_mtx);
834 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
835 		mtx_unlock(&sd->flagqueue_mtx);
836 		if (ret != 0)
837 			break;
838 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), midiunit, SND_DEV_MIDIN), cmd, arg, mode, p);
839 		break;
840 	case SNDCTL_PMGR_ACCESS:
841 		patinfo = (struct patmgr_info *)arg;
842 		midiunit = patinfo->device;
843 		mtx_lock(&sd->flagqueue_mtx);
844 		ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md);
845 		mtx_unlock(&sd->flagqueue_mtx);
846 		if (ret != 0)
847 			break;
848 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), midiunit, SND_DEV_MIDIN), cmd, arg, mode, p);
849 		break;
850 	case SNDCTL_SEQ_THRESHOLD:
851 		tmp = *(int *)arg;
852 		RANGE(tmp, 1, sd->midi_dbuf_out.bufsize - 1);
853 		scp->output_threshould = tmp;
854 		ret = 0;
855 		break;
856 	case SNDCTL_MIDI_PRETIME:
857 		tmp = *(int *)arg;
858 		if (tmp < 0)
859 			tmp = 0;
860 		tmp = (hz * tmp) / 10;
861 		scp->pre_event_timeout = tmp;
862 		ret = 0;
863 		break;
864 	default:
865 		if (scp->fflags == O_RDONLY) {
866 			ret = EIO;
867 			break;
868 		}
869 		mtx_lock(&sd->flagqueue_mtx);
870 		ret = lookup_mididev(scp, 0, LOOKUP_OPEN, &md);
871 		mtx_unlock(&sd->flagqueue_mtx);
872 		if (ret != 0)
873 			break;
874 		ret = midi_ioctl(MIDIMKDEV(major(i_dev), 0, SND_DEV_MIDIN), cmd, arg, mode, p);
875 		break;
876 	}
877 
878 	return (ret);
879 }
880 
881 int
882 seq_poll(dev_t i_dev, int events, struct proc *p)
883 {
884 	int unit, ret, lim;
885 	sc_p scp;
886 	seqdev_info *sd;
887 
888 	unit = MIDIUNIT(i_dev);
889 
890 	DEB(printf("seq%d: polling.\n", unit));
891 
892 	if (unit >= NSEQ_MAX) {
893 		DEB(printf("seq_poll: unit %d does not exist.\n", unit));
894 		return (ENXIO);
895 	}
896 	sd = get_seqdev_info(i_dev, &unit);
897 	if (sd == NULL) {
898 		DEB(printf("seq_poll: unit %d is not configured.\n", unit));
899 		return (ENXIO);
900 	}
901 	scp = sd->softc;
902 
903 	mtx_lock(&sd->flagqueue_mtx);
904 
905 	ret = 0;
906 
907 	/* Look up the apropriate queue and select it. */
908 	if ((events & (POLLOUT | POLLWRNORM)) != 0) {
909 		/* Start playing. */
910 		sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
911 
912 		/* Find out the boundary. */
913 		if ((sd->flags & SEQ_F_HAS_SIZE) != 0)
914 			lim = sd->midi_dbuf_out.blocksize;
915 		else
916 			lim = sd->midi_dbuf_out.unit_size;
917 		if (sd->midi_dbuf_out.fl < lim)
918 			/* No enough space, record select. */
919 			selrecord(p, &sd->midi_dbuf_out.sel);
920 		else
921 			/* We can write now. */
922 			ret |= events & (POLLOUT | POLLWRNORM);
923 	}
924 	if ((events & (POLLIN | POLLRDNORM)) != 0) {
925 		/* Start recording. */
926 		sd->callback(sd, SEQ_CB_START | SEQ_CB_RD);
927 
928 		/* Find out the boundary. */
929 		if ((sd->flags & SEQ_F_HAS_SIZE) != 0)
930 			lim = sd->midi_dbuf_in.blocksize;
931 		else
932 			lim = sd->midi_dbuf_in.unit_size;
933 		if (sd->midi_dbuf_in.rl < lim)
934 			/* No data ready, record select. */
935 			selrecord(p, &sd->midi_dbuf_in.sel);
936 		else
937 			/* We can write now. */
938 			ret |= events & (POLLIN | POLLRDNORM);
939 	}
940 
941 	mtx_unlock(&sd->flagqueue_mtx);
942 
943 	return (ret);
944 }
945 
946 static void
947 seq_intr(void *p, mididev_info *md)
948 {
949 	sc_p scp;
950 	seqdev_info *sd;
951 
952 	sd = (seqdev_info *)p;
953 	scp = sd->softc;
954 
955 	mtx_lock(&sd->flagqueue_mtx);
956 
957 	/* Restart playing if we have the data to output. */
958 	if (scp->queueout_pending)
959 		sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
960 	/* Check the midi device if we are reading. */
961 	if ((sd->flags & SEQ_F_READING) != 0)
962 		seq_midiinput(scp, md);
963 
964 	mtx_unlock(&sd->flagqueue_mtx);
965 }
966 
967 static int
968 seq_callback(seqdev_info *sd, int reason)
969 {
970 	int unit;
971 	sc_p scp;
972 
973 	/*DEB(printf("seq_callback: reason 0x%x.\n", reason));*/
974 
975 	if (sd == NULL) {
976 		DEB(printf("seq_callback: device not configured.\n"));
977 		return (ENXIO);
978 	}
979 	scp = sd->softc;
980 	unit = sd->unit;
981 
982 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
983 
984 	switch (reason & SEQ_CB_REASON_MASK) {
985 	case SEQ_CB_START:
986 		if ((reason & SEQ_CB_RD) != 0 && (sd->flags & SEQ_F_READING) == 0)
987 			/* Begin recording. */
988 			sd->flags |= SEQ_F_READING;
989 		if ((reason & SEQ_CB_WR) != 0 && (sd->flags & SEQ_F_WRITING) == 0)
990 			/* Start playing. */
991 			seq_startplay(scp);
992 		break;
993 	case SEQ_CB_STOP:
994 	case SEQ_CB_ABORT:
995 		if ((reason & SEQ_CB_RD) != 0 && (sd->flags & SEQ_F_READING) != 0) {
996 			/* Stop recording. */
997 			sd->flags &= ~SEQ_F_READING;
998 			scp->seq_time = seq_gettime();
999 			scp->prev_input_time = 0;
1000 		}
1001 		if ((reason & SEQ_CB_WR) != 0 && (sd->flags & SEQ_F_WRITING) != 0) {
1002 			/* Stop Playing. */
1003 			sd->flags &= ~SEQ_F_WRITING;
1004 			scp->queueout_pending = 0;
1005 			scp->seq_time = seq_gettime();
1006 			scp->prev_input_time = 0;
1007 
1008 			/* Stop the timer. */
1009 			seq_stoptimer(scp);
1010 		}
1011 	}
1012 
1013 	return (0);
1014 }
1015 
1016 /*
1017  * The functions below here are the libraries for the above ones.
1018  */
1019 
1020 static int
1021 seq_queue(sc_p scp, u_char *note)
1022 {
1023 	int unit, err;
1024 	seqdev_info *sd;
1025 
1026 	sd = scp->devinfo;
1027 	unit = sd->unit;
1028 
1029 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1030 
1031 	/*DEB(printf("seq%d: queueing.\n", unit));*/
1032 
1033 	if ((sd->flags & SEQ_F_INSYNC) != 0) {
1034 		cv_wait(&sd->insync_cv, &sd->flagqueue_mtx);
1035 		cv_signal(&sd->insync_cv);
1036 	}
1037 
1038 	if (sd->midi_dbuf_out.fl < EV_SZ) {
1039 		/* We have no space. Start playing if not yet. */
1040 		if ((sd->flags & SEQ_F_WRITING) == 0)
1041 			sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
1042 		if ((sd->flags & SEQ_F_NBIO) != 0 && sd->midi_dbuf_out.fl < EV_SZ)
1043 			/* We would block. */
1044 			return (EAGAIN);
1045 		else {
1046 			while (sd->midi_dbuf_out.fl < EV_SZ) {
1047 				/* We have no space. Good night. */
1048 				err = msleep(&sd->midi_dbuf_out.tsleep_out, &sd->flagqueue_mtx, PRIBIO | PCATCH, "seqque", 0);
1049 				if (err == EINTR || err == ERESTART) {
1050 					if (err == EINTR)
1051 						sd->callback(sd, SEQ_CB_STOP | SEQ_CB_WR);
1052 					return (err);
1053 				}
1054 			}
1055 		}
1056 	}
1057 
1058 	/* We now have enough space to write. */
1059 	err = midibuf_seqwrite(&sd->midi_dbuf_out, note, EV_SZ, &sd->flagqueue_mtx);
1060 
1061 	if (err < 0)
1062 		err = -err;
1063 	else {
1064 		err = 0;
1065 		/* Start playing if we have some data in the queue. */
1066 		if (sd->midi_dbuf_out.rl >= EV_SZ && ((sd->flags & SEQ_F_WRITING) == 0))
1067 			sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
1068 	}
1069 
1070 	return (err);
1071 }
1072 
1073 static void
1074 seq_startplay(sc_p scp)
1075 {
1076 	int unit;
1077 	u_char event[EV_SZ];
1078 	seqdev_info *sd;
1079 
1080 	sd = scp->devinfo;
1081 	unit = sd->unit;
1082 
1083 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1084 
1085 	/* Dequeue the events to play. */
1086 	while (sd->midi_dbuf_out.rl >= EV_SZ) {
1087 
1088 		midibuf_seqread(&sd->midi_dbuf_out, event, EV_SZ, &sd->flagqueue_mtx);
1089 
1090 		switch (seq_playevent(scp, event)) {
1091 		case TIMERARMED:
1092 			return;
1093 		case QUEUEFULL:
1094 			/* We cannot play any further. */
1095 			midibuf_sequnread(&sd->midi_dbuf_out, event, EV_SZ, &sd->flagqueue_mtx);
1096 			return;
1097 		case MORE:
1098 			break;
1099 		}
1100 	}
1101 
1102 	/* Played every event in the queue. */
1103 	sd->flags &= ~SEQ_F_WRITING;
1104 }
1105 
1106 static int
1107 seq_playevent(sc_p scp, u_char *event)
1108 {
1109 	int unit, ret;
1110 	long *delay;
1111 	seqdev_info *sd;
1112 	mididev_info *md;
1113 
1114 	sd = scp->devinfo;
1115 	unit = sd->unit;
1116 
1117 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1118 
1119 	ret = lookup_mididev(scp, 0, LOOKUP_OPEN, &md);
1120 	if (ret != 0)
1121 		return (MORE);
1122 
1123 	switch(event[0]) {
1124 	case SEQ_NOTEOFF:
1125 		mtx_unlock(&sd->flagqueue_mtx);
1126 		if (md->synth.killnote(md, event[1], 255, event[3]) == EAGAIN) {
1127 			mtx_lock(&sd->flagqueue_mtx);
1128 			ret = QUEUEFULL;
1129 			break;
1130 		}
1131 		mtx_lock(&sd->flagqueue_mtx);
1132 		ret = MORE;
1133 		break;
1134 	case SEQ_NOTEON:
1135 		mtx_unlock(&sd->flagqueue_mtx);
1136 		if ((event[4] < 128 || event[4] == 255) && md->synth.startnote(md, event[1], event[2], event[3]) == EAGAIN) {
1137 			mtx_lock(&sd->flagqueue_mtx);
1138 			ret = QUEUEFULL;
1139 			break;
1140 		}
1141 		mtx_lock(&sd->flagqueue_mtx);
1142 		ret = MORE;
1143 		break;
1144 	case SEQ_WAIT:
1145 
1146 		/* Extract the delay. */
1147 		delay = (long *)event;
1148 		*delay = (*delay >> 8) & 0xffffff;
1149 		if (*delay > 0) {
1150 			/* Arm the timer. */
1151 			sd->flags |= SEQ_F_WRITING;
1152 			if (seq_requesttimer(scp, *delay)) {
1153 				ret = TIMERARMED;
1154 				break;
1155 			}
1156 		}
1157 		ret = MORE;
1158 		break;
1159 	case SEQ_PGMCHANGE:
1160 		mtx_unlock(&sd->flagqueue_mtx);
1161 		if (md->synth.setinstr(md, event[1], event[2]) == EAGAIN) {
1162 			mtx_lock(&sd->flagqueue_mtx);
1163 			ret = QUEUEFULL;
1164 			break;
1165 		}
1166 		mtx_lock(&sd->flagqueue_mtx);
1167 		ret = MORE;
1168 		break;
1169 	case SEQ_SYNCTIMER:
1170 		/* Reset the timer. */
1171 		scp->seq_time = seq_gettime();
1172 		scp->prev_input_time = 0;
1173 		scp->prev_event_time = 0;
1174 		scp->prev_wakeup_time = scp->seq_time;
1175 		ret = MORE;
1176 		break;
1177 	case SEQ_MIDIPUTC:
1178 		/* Pass through to the midi device. */
1179 		ret = lookup_mididev(scp, event[2], LOOKUP_OPEN, &md);
1180 		if (ret != 0) {
1181 			ret = MORE;
1182 			break;
1183 		}
1184 		mtx_unlock(&sd->flagqueue_mtx);
1185 		if (md->synth.writeraw(md, &event[1], sizeof(event[1]), 1) == EAGAIN) {
1186 			mtx_lock(&sd->flagqueue_mtx);
1187 			/* The queue was full. Try again later. */
1188 			ret = QUEUEFULL;
1189 			break;
1190 		}
1191 		mtx_lock(&sd->flagqueue_mtx);
1192 		ret = MORE;
1193 		break;
1194 	case SEQ_ECHO:
1195 		/* Echo this event back. */
1196 		if (seq_copytoinput(scp, event, 4) == EAGAIN) {
1197 			ret = QUEUEFULL;
1198 			break;
1199 		}
1200 		ret = MORE;
1201 		break;
1202 	case SEQ_PRIVATE:
1203 		ret = lookup_mididev(scp, event[1], LOOKUP_OPEN, &md);
1204 		if (ret != 0) {
1205 			ret = MORE;
1206 			break;
1207 		}
1208 		mtx_unlock(&sd->flagqueue_mtx);
1209 		if (md->synth.hwcontrol(md, event) == EAGAIN) {
1210 			mtx_lock(&sd->flagqueue_mtx);
1211 			ret = QUEUEFULL;
1212 			break;
1213 		}
1214 		mtx_lock(&sd->flagqueue_mtx);
1215 		ret = MORE;
1216 		break;
1217 	case SEQ_EXTENDED:
1218 		ret = seq_extended(scp, event);
1219 		break;
1220 	case EV_CHN_VOICE:
1221 		ret = seq_chnvoice(scp, event);
1222 		break;
1223 	case EV_CHN_COMMON:
1224 		ret = seq_chncommon(scp, event);
1225 		break;
1226 	case EV_TIMING:
1227 		ret = seq_timing(scp, event);
1228 		break;
1229 	case EV_SEQ_LOCAL:
1230 		ret = seq_local(scp, event);
1231 		break;
1232 	case EV_SYSEX:
1233 		ret = seq_sysex(scp, event);
1234 		break;
1235 	default:
1236 		ret = MORE;
1237 		break;
1238 	}
1239 
1240 	switch (ret) {
1241 	case QUEUEFULL:
1242 		/*DEB(printf("seq_playevent: the queue is full.\n"));*/
1243 		/* The queue was full. Try again on the interrupt by the midi device. */
1244 		sd->flags |= SEQ_F_WRITING;
1245 		scp->queueout_pending = 1;
1246 		break;
1247 	case TIMERARMED:
1248 		sd->flags |= SEQ_F_WRITING;
1249 		/* FALLTHRU */
1250 	case MORE:
1251 		scp->queueout_pending = 0;
1252 		break;
1253 	}
1254 
1255 	return (ret);
1256 }
1257 
1258 static u_long
1259 seq_gettime(void)
1260 {
1261 	struct timeval  timecopy;
1262 
1263 	getmicrotime(&timecopy);
1264 	return timecopy.tv_usec / (1000000 / hz) + (u_long) timecopy.tv_sec * hz;
1265 }
1266 
1267 static int
1268 seq_requesttimer(sc_p scp, int delay)
1269 {
1270 	u_long cur_time, rel_base;
1271 
1272 	/*DEB(printf("seq%d: requested timer at delay of %d.\n", unit, delay));*/
1273 
1274 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
1275 
1276 	cur_time = seq_gettime();
1277 
1278 	scp->prev_event_time = delay;
1279 	if (delay < 0)
1280 		/* Request a new timer. */
1281 		delay = -delay;
1282 	else {
1283 		rel_base = cur_time - scp->seq_time;
1284 		if (delay <= rel_base) {
1285 			seq_stoptimer(scp);
1286 			return 0;
1287 		}
1288 		delay -= rel_base;
1289 	}
1290 
1291 #if notdef
1292 	/*
1293 	 * Compensate the delay of midi message transmission.
1294 	 * XXX Do we have to consider the accumulation of errors
1295 	 * less than 1/hz second?
1296 	 */
1297 	delay -= (cur_time - scp->prev_wakeup_time);
1298 	if (delay < 1) {
1299 		printf("sequencer: prev = %lu, cur = %lu, delay = %d, skip sleeping.\n",
1300 			scp->prev_wakeup_time, cur_time, delay);
1301 		seq_stoptimer(scp);
1302 		return 0;
1303 	}
1304 #endif /* notdef */
1305 
1306 	callout_reset(&scp->timeout_ch, delay, seq_timer, (void *)scp);
1307 	scp->timer_running = 1;
1308 
1309 	return 1;
1310 }
1311 
1312 static void
1313 seq_stoptimer(sc_p scp)
1314 {
1315 	/*DEB(printf("seq%d: stopping timer.\n", unit));*/
1316 
1317 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
1318 
1319 	if (scp->timer_running) {
1320 		callout_stop(&scp->timeout_ch);
1321 		scp->timer_running = 0;
1322 	}
1323 }
1324 
1325 static void
1326 seq_midiinput(sc_p scp, mididev_info *md)
1327 {
1328 	int unit, midiunit;
1329 	u_long tstamp;
1330 	u_char event[4];
1331 	seqdev_info *sd;
1332 
1333 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
1334 
1335 	sd = scp->devinfo;
1336 	unit = sd->unit;
1337 
1338 	/* Can this midi device interrupt for input? */
1339 	midiunit = md->unit;
1340 	if (lookup_mididev(scp, midiunit, LOOKUP_EXIST, NULL) != 0)
1341 		return;
1342 
1343 	if ((md->flags & MIDI_F_READING) != 0 && md->intrarg == sd) {
1344 		/* Read the input data. */
1345 		while (md->synth.readraw(md, &event[1], sizeof(event[1]), 1) == 0) {
1346 			tstamp = seq_gettime() - scp->seq_time;
1347 			if (tstamp != scp->prev_input_time) {
1348 				/* Insert a wait between events. */
1349 				tstamp = (tstamp << 8) | SEQ_WAIT;
1350 				seq_copytoinput(scp, (u_char *)&tstamp, 4);
1351 				scp->prev_input_time = tstamp;
1352 			}
1353 			event[0] = SEQ_MIDIPUTC;
1354 			event[2] = midiunit;
1355 			event[3] = 0;
1356 			seq_copytoinput(scp, event, sizeof(event));
1357 		}
1358 	}
1359 }
1360 
1361 static int
1362 seq_copytoinput(sc_p scp, u_char *event, int len)
1363 {
1364 	seqdev_info *sd;
1365 
1366 	sd = scp->devinfo;
1367 
1368 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1369 
1370 	if (midibuf_input_intr(&sd->midi_dbuf_in, event, len) == -EAGAIN)
1371 		return (EAGAIN);
1372 
1373 	return (0);
1374 }
1375 
1376 static int
1377 seq_extended(sc_p scp, u_char *event)
1378 {
1379 	int unit;
1380 	seqdev_info *sd;
1381 	mididev_info *md;
1382 
1383 	sd = scp->devinfo;
1384 	unit = sd->unit;
1385 
1386 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1387 
1388 	if (lookup_mididev(scp, event[2], LOOKUP_OPEN, &md) != 0)
1389 		return (MORE);
1390 
1391 	switch (event[1]) {
1392 	case SEQ_NOTEOFF:
1393 		mtx_unlock(&sd->flagqueue_mtx);
1394 		if (md->synth.killnote(md, event[3], event[4], event[5]) == EAGAIN) {
1395 			mtx_lock(&sd->flagqueue_mtx);
1396 			return (QUEUEFULL);
1397 		}
1398 		mtx_lock(&sd->flagqueue_mtx);
1399 		break;
1400 	case SEQ_NOTEON:
1401 		mtx_unlock(&sd->flagqueue_mtx);
1402 		if ((event[4] < 128 || event[4] == 255) && md->synth.startnote(md, event[3], event[4], event[5]) == EAGAIN) {
1403 			mtx_lock(&sd->flagqueue_mtx);
1404 			return (QUEUEFULL);
1405 		}
1406 		mtx_lock(&sd->flagqueue_mtx);
1407 		break;
1408 	case SEQ_PGMCHANGE:
1409 		mtx_unlock(&sd->flagqueue_mtx);
1410 		if (md->synth.setinstr(md, event[3], event[4]) == EAGAIN) {
1411 			mtx_lock(&sd->flagqueue_mtx);
1412 			return (QUEUEFULL);
1413 		}
1414 		mtx_lock(&sd->flagqueue_mtx);
1415 		break;
1416 	case SEQ_AFTERTOUCH:
1417 		mtx_unlock(&sd->flagqueue_mtx);
1418 		if (md->synth.aftertouch(md, event[3], event[4]) == EAGAIN) {
1419 			mtx_lock(&sd->flagqueue_mtx);
1420 			return (QUEUEFULL);
1421 		}
1422 		mtx_lock(&sd->flagqueue_mtx);
1423 		break;
1424 	case SEQ_BALANCE:
1425 		mtx_unlock(&sd->flagqueue_mtx);
1426 		if (md->synth.panning(md, event[3], (char)event[4]) == EAGAIN) {
1427 			mtx_lock(&sd->flagqueue_mtx);
1428 			return (QUEUEFULL);
1429 		}
1430 		mtx_lock(&sd->flagqueue_mtx);
1431 		break;
1432 	case SEQ_CONTROLLER:
1433 		mtx_unlock(&sd->flagqueue_mtx);
1434 		if (md->synth.controller(md, event[3], event[4], *(short *)&event[5]) == EAGAIN) {
1435 			mtx_lock(&sd->flagqueue_mtx);
1436 			return (QUEUEFULL);
1437 		}
1438 		mtx_lock(&sd->flagqueue_mtx);
1439 		break;
1440 	case SEQ_VOLMODE:
1441 		mtx_unlock(&sd->flagqueue_mtx);
1442 		if (md->synth.volumemethod != NULL && md->synth.volumemethod(md, event[3]) == EAGAIN) {
1443 			mtx_lock(&sd->flagqueue_mtx);
1444 			return (QUEUEFULL);
1445 		}
1446 		mtx_lock(&sd->flagqueue_mtx);
1447 		break;
1448 	}
1449 
1450 	return (MORE);
1451 }
1452 
1453 static int
1454 seq_chnvoice(sc_p scp, u_char *event)
1455 {
1456 	int voice;
1457 	seqdev_info *sd;
1458 	mididev_info *md;
1459 	u_char dev, cmd, chn, note, parm;
1460 
1461 	voice = -1;
1462 	dev = event[1];
1463 	cmd = event[2];
1464 	chn = event[3];
1465 	note = event[4];
1466 	parm = event[5];
1467 
1468 	sd = scp->devinfo;
1469 
1470 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1471 
1472 	if (lookup_mididev(scp, dev, LOOKUP_OPEN, &md) != 0)
1473 		return (MORE);
1474 
1475 #if notyet
1476 	if (scp->seq_mode == SEQ_2 && md->synth.allocvoice != NULL)
1477 		voice = seq_allocvoice(scp, md, chn, note);
1478 #endif /* notyet */
1479 	switch (cmd) {
1480 	case MIDI_NOTEON:
1481 		if (note < 128 || note == 255) {
1482 #if notyet
1483 			if (voice == -1 && scp->seq_mode == SEQ_2 && md->synth.allocvoice)
1484 				/* This is an internal synthesizer. (FM, GUS, etc) */
1485 				if ((voice = seq_allocvoice(scp, md, chn, note)) == -EAGAIN)
1486 					return (QUEUEFULL);
1487 #endif /* notyet */
1488 			if (voice == -1)
1489 				voice = chn;
1490 
1491 #if notyet
1492 			if (scp->seq_mode == SEQ_2 && chn == 9) {
1493 				/* This channel is a percussion. The note number is the patch number. */
1494 				mtx_unlock(&sd->flagqueue_mtx);
1495 				if (md->synth.setinstr(md, voice, 128 + note) == EAGAIN) {
1496 					mtx_lock(&sd->flagqueue_mtx);
1497 					return (QUEUEFULL);
1498 				}
1499 				mtx_lock(&sd->flagqueue_mtx);
1500 
1501 				note = 60; /* Middle C. */
1502 			}
1503 			if (scp->seq_mode == SEQ_2) {
1504 				mtx_unlock(&sd->flagqueue_mtx);
1505 				if (md->synth.setupvoice(md, voice, chn) == EAGAIN) {
1506 					mtx_lock(&sd->flagqueue_mtx);
1507 					return (QUEUEFULL);
1508 				}
1509 				mtx_lock(&sd->flagqueue_mtx);
1510 			}
1511 #endif /* notyet */
1512 			mtx_unlock(&sd->flagqueue_mtx);
1513 			if (md->synth.startnote(md, voice, note, parm) == EAGAIN) {
1514 				mtx_lock(&sd->flagqueue_mtx);
1515 				return (QUEUEFULL);
1516 			}
1517 			mtx_lock(&sd->flagqueue_mtx);
1518 		}
1519 		break;
1520 	case MIDI_NOTEOFF:
1521 		if (voice == -1)
1522 			voice = chn;
1523 		mtx_unlock(&sd->flagqueue_mtx);
1524 		if (md->synth.killnote(md, voice, note, parm) == EAGAIN) {
1525 			mtx_lock(&sd->flagqueue_mtx);
1526 			return (QUEUEFULL);
1527 		}
1528 		mtx_lock(&sd->flagqueue_mtx);
1529 		break;
1530 	case MIDI_KEY_PRESSURE:
1531 		if (voice == -1)
1532 			voice = chn;
1533 		mtx_unlock(&sd->flagqueue_mtx);
1534 		if (md->synth.aftertouch(md, voice, parm) == EAGAIN) {
1535 			mtx_lock(&sd->flagqueue_mtx);
1536 			return (QUEUEFULL);
1537 		}
1538 		mtx_lock(&sd->flagqueue_mtx);
1539 		break;
1540 	}
1541 
1542 	return (MORE);
1543 }
1544 
1545 static int
1546 seq_findvoice(mididev_info *md, int chn, int note)
1547 {
1548 	int i;
1549 	u_short key;
1550 
1551 	key = (chn << 8) | (note + 1);
1552 
1553 	mtx_lock(&md->synth.vc_mtx);
1554 	for (i = 0 ; i < md->synth.alloc.max_voice ; i++)
1555 		if (md->synth.alloc.map[i] == key) {
1556 			mtx_unlock(&md->synth.vc_mtx);
1557 			return (i);
1558 		}
1559 	mtx_unlock(&md->synth.vc_mtx);
1560 
1561 	return (-1);
1562 }
1563 
1564 static int
1565 seq_allocvoice(sc_p scp, mididev_info *md, int chn, int note)
1566 {
1567 	int voice;
1568 	u_short key;
1569 
1570 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
1571 
1572 	key = (chn << 8) | (note + 1);
1573 
1574 	mtx_unlock(&scp->devinfo->flagqueue_mtx);
1575 	if ((voice = md->synth.allocvoice(md, chn, note, &md->synth.alloc)) == -EAGAIN) {
1576 		mtx_lock(&scp->devinfo->flagqueue_mtx);
1577 		return (-EAGAIN);
1578 	}
1579 	mtx_lock(&scp->devinfo->flagqueue_mtx);
1580 
1581 	mtx_lock(&md->synth.vc_mtx);
1582 	md->synth.alloc.map[voice] = key;
1583 	md->synth.alloc.alloc_times[voice] = md->synth.alloc.timestamp++;
1584 	mtx_unlock(&md->synth.vc_mtx);
1585 
1586 	return (voice);
1587 }
1588 
1589 static int
1590 seq_chncommon(sc_p scp, u_char *event)
1591 {
1592 	int unit/*, i, val, key*/;
1593 	u_short w14;
1594 	u_char dev, cmd, chn, p1;
1595 	seqdev_info *sd;
1596 	mididev_info *md;
1597 
1598 	dev = event[1];
1599 	cmd = event[2];
1600 	chn = event[3];
1601 	p1 = event[4];
1602 	w14 = *(u_short *)&event[6];
1603 
1604 	sd = scp->devinfo;
1605 	unit = sd->unit;
1606 
1607 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1608 
1609 	if (lookup_mididev(scp, dev, LOOKUP_OPEN, &md) != 0)
1610 		return (MORE);
1611 
1612 	switch (cmd) {
1613 	case MIDI_PGM_CHANGE:
1614 #if notyet
1615 		if (scp->seq_mode == SEQ_2) {
1616 			mtx_lock(&md->synth.vc_mtx);
1617 			md->synth.chn_info[chn].pgm_num = p1;
1618 			mtx_unlock(&md->synth.vc_mtx);
1619 			mtx_unlock(&sd->flagqueue_mtx);
1620 			if (md->synth.setinstr(md, chn, p1) == EAGAIN) {
1621 				mtx_lock(&sd->flagqueue_mtx);
1622 				return (QUEUEFULL);
1623 			}
1624 			mtx_lock(&sd->flagqueue_mtx);
1625 		} else {
1626 #endif /* notyet */
1627 			/* For Mode 1. */
1628 			mtx_unlock(&sd->flagqueue_mtx);
1629 			if (md->synth.setinstr(md, chn, p1) == EAGAIN) {
1630 				mtx_lock(&sd->flagqueue_mtx);
1631 				return (QUEUEFULL);
1632 			}
1633 			mtx_lock(&sd->flagqueue_mtx);
1634 #if notyet
1635 		}
1636 #endif /* notyet */
1637 		break;
1638 	case MIDI_CTL_CHANGE:
1639 		/* mtx_lock(&md->giant); */
1640 #if notyet
1641 		if (scp->seq_mode == SEQ_2) {
1642 			if (chn < 16 && p1 < 128) {
1643 				mtx_lock(&md->synth.vc_mtx);
1644 				md->synth.chn_info[chn].controllers[p1] = w14 & 0x7f;
1645 				if (p1 < 32)
1646 					/* We have set the MSB, clear the LSB. */
1647 					md->synth.chn_info[chn].controllers[p1 + 32] = 0;
1648 				val = w14 & 0x7f;
1649 				if (p1 < 64) {
1650 					/* Combine the MSB and the LSB. */
1651 					val = ((md->synth.chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
1652 					    | (md->synth.chn_info[chn].controllers[p1 | 32] & 0x7f);
1653 					p1 &= ~32;
1654 				}
1655 				/* Handle all of the notes playing on this channel. */
1656 				key = ((int)chn << 8);
1657 				for (i = 0 ; i < md->synth.alloc.max_voice ; i++)
1658 					if ((md->synth.alloc.map[i] & 0xff00) == key) {
1659 						mtx_unlock(&md->synth.vc_mtx);
1660 						mtx_unlock(&sd->flagqueue_mtx);
1661 						if (md->synth.controller(md, i, p1, val) == EAGAIN) {
1662 							mtx_lock(&sd->flagqueue_mtx);
1663 							return (QUEUEFULL);
1664 						}
1665 						mtx_lock(&sd->flagqueue_mtx);
1666 						mtx_lock(&md->synth.vc_mtx);
1667 					}
1668 				mtx_unlock(&md->synth.vc_mtx);
1669 			}
1670 		} else {
1671 #endif /* notyet */
1672 			/* For Mode 1. */
1673 			mtx_unlock(&sd->flagqueue_mtx);
1674 			if (md->synth.controller(md, chn, p1, w14) == EAGAIN) {
1675 				mtx_lock(&sd->flagqueue_mtx);
1676 				return (QUEUEFULL);
1677 			}
1678 			mtx_lock(&sd->flagqueue_mtx);
1679 #if notyet
1680 		}
1681 #endif /* notyet */
1682 		break;
1683 	case MIDI_PITCH_BEND:
1684 #if notyet
1685 		if (scp->seq_mode == SEQ_2) {
1686 			mtx_lock(&md->synth.vc_mtx);
1687 			md->synth.chn_info[chn].bender_value = w14;
1688 			/* Handle all of the notes playing on this channel. */
1689 			key = ((int)chn << 8);
1690 			for (i = 0 ; i < md->synth.alloc.max_voice ; i++)
1691 				if ((md->synth.alloc.map[i] & 0xff00) == key) {
1692 					mtx_unlock(&md->synth.vc_mtx);
1693 					mtx_unlock(&sd->flagqueue_mtx);
1694 					if (md->synth.bender(md, i, w14) == EAGAIN) {
1695 						mtx_lock(&sd->flagqueue_mtx);
1696 						return (QUEUEFULL);
1697 					}
1698 					mtx_lock(&sd->flagqueue_mtx);
1699 					mtx_lock(&md->synth.vc_mtx);
1700 				}
1701 			mtx_unlock(&md->synth.vc_mtx);
1702 		} else {
1703 #endif /* notyet */
1704 			/* For Mode 1. */
1705 			mtx_unlock(&sd->flagqueue_mtx);
1706 			if (md->synth.bender(md, chn, w14) == EAGAIN) {
1707 				mtx_lock(&sd->flagqueue_mtx);
1708 				return (QUEUEFULL);
1709 			}
1710 			mtx_lock(&sd->flagqueue_mtx);
1711 #if notyet
1712 		}
1713 #endif /* notyet */
1714 		break;
1715 	}
1716 
1717 	return (MORE);
1718 }
1719 
1720 static int
1721 seq_timing(sc_p scp, u_char *event)
1722 {
1723 	int unit/*, ret*/;
1724 	long parm;
1725 	seqdev_info *sd;
1726 
1727 	sd = scp->devinfo;
1728 	unit = sd->unit;
1729 
1730 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1731 
1732 	parm = *(long *)&event[4];
1733 
1734 #if notyet
1735 	if (scp->seq_mode == SEQ_2 && (ret = tmr->event(tmr_no, event)) == TIMERARMED)
1736 		return (ret);
1737 #endif /* notyet */
1738 	switch (event[1]) {
1739 	case TMR_WAIT_REL:
1740 	case TMR_WAIT_ABS:
1741 		if (event[1] == TMR_WAIT_REL)
1742 			parm += scp->prev_event_time;
1743 		if (parm > 0) {
1744 			sd->flags |= SEQ_F_WRITING;
1745 			if (seq_requesttimer(scp, parm))
1746 				return (TIMERARMED);
1747 		}
1748 		break;
1749 	case TMR_START:
1750 		scp->seq_time = seq_gettime();
1751 		scp->prev_input_time = 0;
1752 		scp->prev_event_time = 0;
1753 		scp->prev_wakeup_time = scp->seq_time;
1754 		break;
1755 	case TMR_STOP:
1756 		break;
1757 	case TMR_CONTINUE:
1758 		break;
1759 	case TMR_TEMPO:
1760 		break;
1761 	case TMR_ECHO:
1762 #if notyet
1763 		if (scp->seq_mode == SEQ_2)
1764 			seq_copytoinput(scp, event, 8);
1765 		else {
1766 #endif /* notyet */
1767 			parm = (parm << 8 | SEQ_ECHO);
1768 			seq_copytoinput(scp, (u_char *)&parm, 4);
1769 #if notyet
1770 		}
1771 #endif /* notyet */
1772 		break;
1773 	}
1774 
1775 	return (MORE);
1776 }
1777 
1778 static int
1779 seq_local(sc_p scp, u_char *event)
1780 {
1781 	int unit;
1782 	seqdev_info *sd;
1783 
1784 	sd = scp->devinfo;
1785 	unit = sd->unit;
1786 
1787 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1788 
1789 	switch (event[1]) {
1790 	case LOCL_STARTAUDIO:
1791 #if notyet
1792 		DMAbuf_start_devices(*(u_int *)&event[4]);
1793 #endif /* notyet */
1794 		break;
1795 	}
1796 
1797 	return (MORE);
1798 }
1799 
1800 static int
1801 seq_sysex(sc_p scp, u_char *event)
1802 {
1803 	int unit, i, l;
1804 	seqdev_info *sd;
1805 	mididev_info *md;
1806 
1807 	sd = scp->devinfo;
1808 	unit = sd->unit;
1809 
1810 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1811 
1812 	if (lookup_mididev(scp, event[1], LOOKUP_OPEN, &md) != 0)
1813 		return (MORE);
1814 
1815 	l = 0;
1816 	for (i = 0 ; i < 6 && event[i + 2] != 0xff ; i++)
1817 		l = i + 1;
1818 	if (l > 0) {
1819 		mtx_unlock(&sd->flagqueue_mtx);
1820 		if (md->synth.sendsysex(md, &event[2], l) == EAGAIN) {
1821 			mtx_lock(&sd->flagqueue_mtx);
1822 			return (QUEUEFULL);
1823 		}
1824 		mtx_lock(&sd->flagqueue_mtx);
1825 	}
1826 
1827 	return (MORE);
1828 }
1829 
1830 static void
1831 seq_timer(void *arg)
1832 {
1833 	sc_p scp;
1834 	seqdev_info *sd;
1835 
1836 	scp = arg;
1837 	sd = scp->devinfo;
1838 
1839 	/*DEB(printf("seq_timer: timer fired.\n"));*/
1840 
1841 	/* Record the current timestamp. */
1842 	mtx_lock(&sd->flagqueue_mtx);
1843 
1844 	scp->timer_running = 0;
1845 	scp->prev_wakeup_time = seq_gettime();
1846 	seq_startplay(scp);
1847 
1848 	mtx_unlock(&sd->flagqueue_mtx);
1849 }
1850 
1851 static int
1852 seq_openmidi(sc_p scp, mididev_info *md, int flags, int mode, struct proc *p)
1853 {
1854 	int midiunit, err, insync;
1855 
1856 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
1857 
1858 	midiunit = md->unit;
1859 
1860 	DEB(printf("seq_openmidi: opening midi unit %d.\n", midiunit));
1861 
1862 	err = midi_open(MIDIMKDEV(MIDI_CDEV_MAJOR, midiunit, SND_DEV_MIDIN), flags, mode, p);
1863 	if (err != 0) {
1864 		printf("seq_openmidi: failed to open midi device %d.\n", midiunit);
1865 		return (err);
1866 	}
1867 	mtx_lock(&md->synth.status_mtx);
1868 	mtx_lock(&md->flagqueue_mtx);
1869 	md->intr = seq_intr;
1870 	md->intrarg = scp->devinfo;
1871 	mtx_unlock(&md->flagqueue_mtx);
1872 	md->synth.sysex_state = 0;
1873 	mtx_unlock(&md->synth.status_mtx);
1874 
1875 	insync = 0;
1876 	if ((scp->devinfo->flags & SEQ_F_INSYNC) != 0) {
1877 		insync = 1;
1878 		cv_wait(&scp->devinfo->insync_cv, &scp->devinfo->flagqueue_mtx);
1879 	}
1880 
1881 	TAILQ_INSERT_TAIL(&scp->midi_open, md, md_linkseq);
1882 
1883 	if (insync)
1884 		cv_signal(&scp->devinfo->insync_cv);
1885 
1886 	return (0);
1887 }
1888 
1889 static int
1890 seq_closemidi(sc_p scp, mididev_info *md, int flags, int mode, struct proc *p)
1891 {
1892 	int midiunit, insync;
1893 
1894 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
1895 
1896 	if (md == NULL || !MIDICONFED(md)) {
1897 		DEB(printf("seq_closemidi: midi device does not exist.\n"));
1898 		return (ENXIO);
1899 	}
1900 	midiunit = md->unit;
1901 
1902 	DEB(printf("seq_closemidi: closing midi unit %d.\n", midiunit));
1903 
1904 	midi_close(MIDIMKDEV(MIDI_CDEV_MAJOR, midiunit, SND_DEV_MIDIN), flags, mode, p);
1905 	mtx_lock(&md->flagqueue_mtx);
1906 	md->intr = NULL;
1907 	md->intrarg = NULL;
1908 	mtx_unlock(&md->flagqueue_mtx);
1909 
1910 	insync = 0;
1911 	if ((scp->devinfo->flags & SEQ_F_INSYNC) != 0) {
1912 		insync = 1;
1913 		cv_wait(&scp->devinfo->insync_cv, &scp->devinfo->flagqueue_mtx);
1914 	}
1915 
1916 	TAILQ_REMOVE(&scp->midi_open, md, md_linkseq);
1917 
1918 	if (insync)
1919 		cv_signal(&scp->devinfo->insync_cv);
1920 
1921 	return (0);
1922 }
1923 
1924 static void
1925 seq_panic(sc_p scp)
1926 {
1927 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
1928 
1929 	seq_reset(scp);
1930 }
1931 
1932 static int
1933 seq_reset(sc_p scp)
1934 {
1935 	int unit, chn;
1936 	seqdev_info *sd;
1937 	mididev_info *md;
1938 	u_char c[3];
1939 
1940 	sd = scp->devinfo;
1941 	unit = sd->unit;
1942 
1943 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
1944 
1945 	if ((sd->flags & SEQ_F_INSYNC) != 0) {
1946 		cv_wait(&sd->insync_cv, &sd->flagqueue_mtx);
1947 		cv_signal(&sd->insync_cv);
1948 	}
1949 
1950 	/* Stop reading and writing. */
1951 	sd->callback(sd, SEQ_CB_ABORT | SEQ_CB_RD | SEQ_CB_WR);
1952 
1953 	/* Clear the queues. */
1954 	midibuf_clear(&sd->midi_dbuf_in);
1955 	midibuf_clear(&sd->midi_dbuf_out);
1956 
1957 #if notyet
1958 	/* Reset the synthesizers. */
1959 	TAILQ_FOREACH(md, &scp->midi_open, md_linkseq)
1960 		md->synth.reset(md);
1961 #endif /* notyet */
1962 
1963 #if notyet
1964 	if (scp->seq_mode == SEQ_2) {
1965 		for (chn = 0 ; chn < 16 ; chn++) {
1966 			TAILQ_FOREACH(md, &scp->midi_open, md_linkseq) {
1967 				if (md->synth.controller(md, chn, 123, 0) == EAGAIN /* All notes off. */
1968 				    || md->synth.controller(md, chn, 121, 0) == EAGAIN /* Reset all controllers. */
1969 				    || md->synth.bender(md, chn, 1 << 13) == EAGAIN) /* Reset pitch bend. */
1970 					return (EAGAIN);
1971 			}
1972 		}
1973 	} else {
1974 #endif /* notyet */
1975 		TAILQ_FOREACH(md, &scp->midi_open, md_linkseq) {
1976 			for (chn = 0 ; chn < 16 ; chn++) {
1977 				c[0] = 0xb0 | (chn & 0x0f);
1978 				c[1] = (u_char)0x78; /* All sound off */
1979 				c[2] = (u_char)0;
1980 				md->synth.writeraw(md, c, 3, 0);
1981 				c[1] = (u_char)0x7b; /* All note off */
1982 				md->synth.writeraw(md, c, 3, 0);
1983 				c[1] = (u_char)0x79; /* Reset all controller */
1984 				md->synth.writeraw(md, c, 3, 0);
1985 			}
1986 		}
1987 		seq_sync(scp);
1988 		TAILQ_FOREACH(md, &scp->midi_open, md_linkseq)
1989 			lookup_mididev(scp, md->unit, LOOKUP_CLOSE, NULL);
1990 #if notyet
1991 	}
1992 #endif /* notyet */
1993 
1994 	return (0);
1995 }
1996 
1997 #define SEQ_SYNC_TIMEOUT 8
1998 static int
1999 seq_sync(sc_p scp)
2000 {
2001 	int i, rl;
2002 	seqdev_info *sd;
2003 	mididev_info *md;
2004 
2005 	sd = scp->devinfo;
2006 
2007 	mtx_assert(&sd->flagqueue_mtx, MA_OWNED);
2008 
2009 	sd->flags |= SEQ_F_INSYNC;
2010 
2011 	while (sd->midi_dbuf_out.rl >= EV_SZ) {
2012 		if ((sd->flags & SEQ_F_WRITING) == 0)
2013 			sd->callback(sd, SEQ_CB_START | SEQ_CB_WR);
2014 		rl = sd->midi_dbuf_out.rl;
2015 		i = msleep(&sd->midi_dbuf_out.tsleep_out, &sd->flagqueue_mtx, PRIBIO | PCATCH, "seqsnc", SEQ_SYNC_TIMEOUT * hz);
2016 		if (i == EINTR || i == ERESTART) {
2017 			if (i == EINTR)
2018 				sd->callback(sd, SEQ_CB_STOP | SEQ_CB_WR);
2019 			sd->flags &= ~SEQ_F_INSYNC;
2020 			return (i);
2021 		}
2022 		if (i == EWOULDBLOCK && rl == sd->midi_dbuf_out.rl && !scp->timer_running) {
2023 			/* A queue seems to be stuck up. Give up and clear queues. */
2024 			sd->callback(sd, SEQ_CB_STOP | SEQ_CB_WR);
2025 			midibuf_clear(&sd->midi_dbuf_out);
2026 			TAILQ_FOREACH(md, &scp->midi_open, md_linkseq) {
2027 				mtx_lock(&md->flagqueue_mtx);
2028 				md->callback(md, MIDI_CB_ABORT | MIDI_CB_WR);
2029 				midibuf_clear(&md->midi_dbuf_out);
2030 				mtx_unlock(&md->flagqueue_mtx);
2031 			}
2032 			break;
2033 		}
2034 	}
2035 
2036 	/*
2037 	 * Since syncing a midi device might block, unlock sd->flagqueue_mtx.
2038 	 * Keep sd->midi_dbuf_out from writing by setting SEQ_F_INSYNC.
2039 	 * sd->insync_cv is signalled when sync is finished.
2040 	 */
2041 	mtx_unlock(&sd->flagqueue_mtx);
2042 
2043 	TAILQ_FOREACH(md, &scp->midi_open, md_linkseq) {
2044 		mtx_lock(&md->flagqueue_mtx);
2045 		midi_sync(md);
2046 		mtx_unlock(&md->flagqueue_mtx);
2047 	}
2048 
2049 	mtx_lock(&sd->flagqueue_mtx);
2050 	sd->flags &= ~SEQ_F_INSYNC;
2051 	cv_signal(&sd->insync_cv);
2052 
2053 	return (0);
2054 }
2055 
2056 /*
2057  * a small utility function which, given a device number, returns
2058  * a pointer to the associated seqdev_info struct, and sets the unit
2059  * number.
2060  */
2061 static seqdev_info *
2062 get_seqdev_info(dev_t i_dev, int *unit)
2063 {
2064 	int u;
2065 
2066 	if (MIDIDEV(i_dev) != SND_DEV_SEQ && MIDIDEV(i_dev) != SND_DEV_SEQ2)
2067 		return NULL;
2068 	u = MIDIUNIT(i_dev);
2069 	if (unit)
2070 		*unit = u ;
2071 
2072 	return get_seqdev_info_unit(u);
2073 }
2074 
2075 /*
2076  * a small utility function which, given a unit number, returns
2077  * a pointer to the associated mididev_info struct.
2078  */
2079 seqdev_info *
2080 get_seqdev_info_unit(int unit)
2081 {
2082 	seqdev_info *sd;
2083 
2084 	mtx_lock(&seqinfo_mtx);
2085 	TAILQ_FOREACH(sd, &seq_info, sd_link) {
2086 		if (sd->unit == unit)
2087 			break;
2088 	}
2089 	mtx_unlock(&seqinfo_mtx);
2090 
2091 	return sd;
2092 }
2093 
2094 /* Create a new sequencer device info structure. */
2095 seqdev_info *
2096 create_seqdev_info_unit(int unit, seqdev_info *seq)
2097 {
2098 	seqdev_info *sd, *sdnew;
2099 
2100 	/* As malloc(9) might block, allocate seqdev_info now. */
2101 	sdnew = malloc(sizeof(seqdev_info), M_DEVBUF, M_WAITOK | M_ZERO);
2102 	if (sdnew == NULL)
2103 		return NULL;
2104 	bcopy(seq, sdnew, sizeof(seqdev_info));
2105 	sdnew->unit = unit;
2106 	midibuf_init(&sdnew->midi_dbuf_in);
2107 	midibuf_init(&sdnew->midi_dbuf_out);
2108 	mtx_init(&sdnew->flagqueue_mtx, "seqflq", MTX_DEF);
2109 	cv_init(&sdnew->insync_cv, "seqins");
2110 
2111 	mtx_lock(&seqinfo_mtx);
2112 
2113 	TAILQ_FOREACH(sd, &seq_info, sd_link) {
2114 		if (sd->unit == unit) {
2115 			mtx_unlock(&seqinfo_mtx);
2116 			midibuf_destroy(&sdnew->midi_dbuf_in);
2117 			midibuf_destroy(&sdnew->midi_dbuf_out);
2118 			mtx_destroy(&sdnew->flagqueue_mtx);
2119 			cv_destroy(&sdnew->insync_cv);
2120 			free(sdnew, M_DEVBUF);
2121 			return sd;
2122 		}
2123 	}
2124 
2125 	mtx_lock(&sdnew->flagqueue_mtx);
2126 	TAILQ_INSERT_TAIL(&seq_info, sdnew, sd_link);
2127 	nseq++;
2128 
2129 	mtx_unlock(&seqinfo_mtx);
2130 
2131 	return sdnew;
2132 }
2133 
2134 /*
2135  * Look up a midi device by its unit number opened by this sequencer.
2136  * If the device is not opened and mode is LOOKUP_OPEN, open the device.
2137  */
2138 static int
2139 lookup_mididev(sc_p scp, int unit, int mode, mididev_info **mdp)
2140 {
2141 	int ret;
2142 	mididev_info *md;
2143 
2144 	if (mdp == NULL)
2145 		mdp = &md;
2146 
2147 	*mdp = NULL;
2148 
2149 	mtx_assert(&scp->devinfo->flagqueue_mtx, MA_OWNED);
2150 
2151 	TAILQ_FOREACH(md, &scp->midi_open, md_linkseq) {
2152 		if (md->unit == unit) {
2153 			*mdp = md;
2154 			if (mode == LOOKUP_CLOSE)
2155 				return seq_closemidi(scp, md, scp->fflags, MIDIDEV_MODE, curproc);
2156 
2157 			return (md != NULL && MIDICONFED(md)) ? 0 : ENXIO;
2158 		}
2159 	}
2160 
2161 	if (mode == LOOKUP_OPEN) {
2162 		md = get_mididev_info_unit(unit);
2163 		if (md != NULL) {
2164 			*mdp = md;
2165 			ret = seq_openmidi(scp, md, scp->fflags, MIDIDEV_MODE, curproc);
2166 			return ret;
2167 		}
2168 	}
2169 
2170 	return ENXIO;
2171 }
2172 
2173 /* XXX These functions are actually redundant. */
2174 static int
2175 seqopen(dev_t i_dev, int flags, int mode, struct proc * p)
2176 {
2177 	switch (MIDIDEV(i_dev)) {
2178 	case MIDI_DEV_SEQ:
2179 		return seq_open(i_dev, flags, mode, p);
2180 	}
2181 
2182 	return (ENXIO);
2183 }
2184 
2185 static int
2186 seqclose(dev_t i_dev, int flags, int mode, struct proc * p)
2187 {
2188 	switch (MIDIDEV(i_dev)) {
2189 	case MIDI_DEV_SEQ:
2190 		return seq_close(i_dev, flags, mode, p);
2191 	}
2192 
2193 	return (ENXIO);
2194 }
2195 
2196 static int
2197 seqread(dev_t i_dev, struct uio * buf, int flag)
2198 {
2199 	switch (MIDIDEV(i_dev)) {
2200 	case MIDI_DEV_SEQ:
2201 		return seq_read(i_dev, buf, flag);
2202 	}
2203 
2204 	return (ENXIO);
2205 }
2206 
2207 static int
2208 seqwrite(dev_t i_dev, struct uio * buf, int flag)
2209 {
2210 	switch (MIDIDEV(i_dev)) {
2211 	case MIDI_DEV_SEQ:
2212 		return seq_write(i_dev, buf, flag);
2213 	}
2214 
2215 	return (ENXIO);
2216 }
2217 
2218 static int
2219 seqioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc * p)
2220 {
2221 	switch (MIDIDEV(i_dev)) {
2222 	case MIDI_DEV_SEQ:
2223 		return seq_ioctl(i_dev, cmd, arg, mode, p);
2224 	}
2225 
2226 	return (ENXIO);
2227 }
2228 
2229 static int
2230 seqpoll(dev_t i_dev, int events, struct proc * p)
2231 {
2232 	switch (MIDIDEV(i_dev)) {
2233 	case MIDI_DEV_SEQ:
2234 		return seq_poll(i_dev, events, p);
2235 	}
2236 
2237 	return (ENXIO);
2238 }
2239 
2240 static int
2241 seq_modevent(module_t mod, int type, void *data)
2242 {
2243 	int retval;
2244 
2245 	retval = 0;
2246 
2247 	switch (type) {
2248 	case MOD_LOAD:
2249 		seq_init();
2250 		break;
2251 
2252 	case MOD_UNLOAD:
2253 		printf("sequencer: unload not supported yet.\n");
2254 		retval = EOPNOTSUPP;
2255 		break;
2256 
2257 	default:
2258 		break;
2259 	}
2260 
2261 	return retval;
2262 }
2263 
2264 DEV_MODULE(seq, seq_modevent, NULL);
2265 
2266 static void
2267 seq_clone(arg, name, namelen, dev)
2268 	void *arg;
2269 	char *name;
2270 	int namelen;
2271 	dev_t *dev;
2272 {
2273 	int u;
2274 
2275 	if (*dev != NODEV)
2276 		return;
2277 	if (bcmp(name, "sequencer", 9) != 0)
2278 		return;
2279 	if (name[10] != '\0' && name[11] != '\0')
2280 		return;
2281 	u = name[9] - '0';
2282 	if (name[10] != '\0') {
2283 		u *= 10;
2284 		u += name[10] - '0';
2285 	}
2286 	seq_initunit(u);
2287 	*dev = MIDIMKDEV(SEQ_CDEV_MAJOR, u, MIDI_DEV_SEQ);
2288 	return;
2289 }
2290